carta1 1.6.1 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # Carta1
2
2
 
3
3
  Carta1 is an ATRAC1 encoder and decoder in JavaScript. It converts PCM WAVE
4
- audio to Sony's AEA container, decodes AEA back to PCM WAVE, exposes stateful
4
+ audio to the AEA container, decodes AEA back to PCM WAVE, exposes stateful
5
5
  frame and stream APIs, and ships browser Web Worker bundles.
6
6
 
7
7
  ## Supported format
@@ -182,8 +182,8 @@ npm test # Run the test suite once
182
182
  npm run test:watch # Re-run tests while editing
183
183
  npm run lint # Check JavaScript and formatting
184
184
  npm run format # Apply repository formatting
185
- npm run build # Build browser bundles and the WME plugin
186
- npm run check # Run lint, tests, builds, and packaged WME checks
185
+ npm run build # Build browser bundles
186
+ npm run check # Run lint, tests, and browser builds
187
187
  ```
188
188
 
189
189
  Run `npm run check` before submitting a change. Pull requests and pushes run
@@ -199,54 +199,3 @@ implementation used to understand the ATRAC1 format and codec behavior.
199
199
  ## License
200
200
 
201
201
  ISC
202
-
203
-
204
- ## Experimental Web MiniDisc encoder plugin
205
-
206
- `npm run build` also emits `dist/carta1.wme`, alongside the three existing
207
- browser bundles. GitHub Releases attach the WME, all three JavaScript bundles,
208
- and their source maps. The npm package includes the same build outputs. Release
209
- CDN links are pinned to that release's version.
210
-
211
- **Carta1 (ATRAC1, experimental)** is an ATRAC1 codec for standard MD SP, not
212
- ATRAC3 LP2/LP4 or ATRAC3plus Hi-MD. Current Web MiniDisc API v1 does not support
213
- software ATRAC1 output: its SP modes request PCM for the recorder to encode in
214
- hardware. Installing this plugin does not enable Carta1 recording in the
215
- unmodified app. All current API v1 codecs (PCM, MP3, AT3, A3+) are explicitly
216
- reported as unsupported so there is no silent hardware-encoding fallback.
217
-
218
- ### Proposed host extension for testing
219
-
220
- The archive uses the standard API v1 SAR container and default encoder class,
221
- with the usual `init`, `deinit`, `getSupportFor`, `transcode`, and progress hooks.
222
- It adds two **proposed, nonstandard** export codec names, using the host's
223
- existing recording-mode names:
224
-
225
- | `exportParams.format` | PCM channels | Output |
226
- | --- | --- | --- |
227
- | `{ codec: 'SPS', bitrate: 292 }` | 2 | ATRAC1 stereo SP |
228
- | `{ codec: 'SPM', bitrate: 146 }` | 1 | ATRAC1 mono SP |
229
-
230
- Bitrates are nominal kbps; ATRAC1 uses 212-byte sound units per channel per
231
- 512 samples at 44.1 kHz. The host's built-in FFmpeg decodes/resamples sources
232
- and optionally applies ReplayGain; the existing Carta1 worker encodes them.
233
- `transcode` returns raw sound units **without the 2048-byte AEA header**.
234
- Stereo sound units alternate left/right. Canonical priming and drain units
235
- are retained; gapless is not advertised and AEA timeline metadata is omitted.
236
-
237
- A host implementing this proposal must extend its export-codec interface,
238
- preserve SPS/SPM through capability checks and transcoding instead of mapping
239
- them to PCM, and route the result to a compatible pre-encoded ATRAC1 transfer
240
- path (such as a supported device's homebrew SP uploader). It must handle channel
241
- mode, transfer framing/padding, and device capabilities there. Ordinary SP PCM
242
- upload must never receive these compressed bytes. Neither the proposed names
243
- nor device transfer compatibility are part of the current WMD API contract;
244
- the WMD team can revise them when implementing support. No host changes are
245
- included here.
246
-
247
- `npm run test:wme` exercises the built archive and its bundled worker for both
248
- proposed formats, comparing the returned raw bytes with `encodeAeaPcm` across
249
- independent tracks. Lifecycle and failure tests run in the normal test suite.
250
-
251
- The repository-only WME packager lives in `build/wme.js`. Build tooling is
252
- excluded from the npm package; the generated `dist/` assets are included.
@@ -88,7 +88,8 @@ function search(
88
88
  bfuSizes,
89
89
  remainingBits,
90
90
  biasedScaleFactors,
91
- allScaleFactorIndices
91
+ allScaleFactorIndices,
92
+ zeroBitDistortions
92
93
  )
93
94
  const distortion = measureDistortion(
94
95
  activeBfuCount,
@@ -136,9 +137,10 @@ function measureDistortion(
136
137
  }
137
138
 
138
139
  const sfi = scaleFactorIndices[i]
139
- if (sfi === 0) continue
140
-
141
- const effectiveScaleFactor = biasedScaleFactors[sfi]
140
+ const effectiveScaleFactor =
141
+ sfi === 0
142
+ ? zeroBitDistortions[i] / (2 * bfuSizes[i])
143
+ : biasedScaleFactors[sfi]
142
144
  const distortionFactor = INV_POWER_OF_TWO[bits]
143
145
 
144
146
  totalDistortion += effectiveScaleFactor * distortionFactor * bfuSizes[i]
@@ -160,6 +162,7 @@ function measureDistortion(
160
162
  * @param {i32} remainingBits
161
163
  * @param {Float64Array} biasedScaleFactors
162
164
  * @param {Int32Array} allScaleFactorIndices
165
+ * @param {Float32Array} zeroBitDistortions Nonzero for nonsilent BFUs.
163
166
  * @returns {object} Retained allocation decisions.
164
167
  * @nativeUnchecked Native callers supply frame-bounded buffers and indices.
165
168
  */
@@ -168,7 +171,8 @@ function distributeBits(
168
171
  bfuSizes,
169
172
  remainingBits,
170
173
  biasedScaleFactors,
171
- allScaleFactorIndices
174
+ allScaleFactorIndices,
175
+ zeroBitDistortions
172
176
  ) {
173
177
  const wordLengths = new Int32Array(activeBfuCount)
174
178
 
@@ -181,13 +185,16 @@ function distributeBits(
181
185
  if (sz === 0) continue
182
186
 
183
187
  const sfi = allScaleFactorIndices[bfuIndex]
184
- if (sfi === 0) continue
188
+ if (zeroBitDistortions[bfuIndex] === 0) continue
185
189
 
186
190
  const deltaBits = WORD_LENGTH_DELTA_BITS[0]
187
191
  if (deltaBits <= 0) continue
188
192
 
189
- const distortionDelta =
190
- biasedScaleFactors[sfi] * DISTORTION_DELTA_FACTORS[0]
193
+ const effectiveScaleFactor =
194
+ sfi === 0
195
+ ? zeroBitDistortions[bfuIndex] / (2 * sz)
196
+ : biasedScaleFactors[sfi]
197
+ const distortionDelta = effectiveScaleFactor * DISTORTION_DELTA_FACTORS[0]
191
198
 
192
199
  heapIndices[heapSize] = bfuIndex
193
200
  heapPriorities[heapSize] = distortionDelta / deltaBits
@@ -227,8 +234,10 @@ function distributeBits(
227
234
  const deltaBitsNext = WORD_LENGTH_DELTA_BITS[nxt]
228
235
  if (nxt < MAX_WORD_LENGTH_INDEX && deltaBitsNext > 0) {
229
236
  const sfi = allScaleFactorIndices[bfu]
237
+ const effectiveScaleFactor =
238
+ sfi === 0 ? zeroBitDistortions[bfu] / (2 * sz) : biasedScaleFactors[sfi]
230
239
  const distortionDelta =
231
- biasedScaleFactors[sfi] * DISTORTION_DELTA_FACTORS[nxt]
240
+ effectiveScaleFactor * DISTORTION_DELTA_FACTORS[nxt]
232
241
  heapPriorities[0] = distortionDelta / deltaBitsNext
233
242
  siftDown(heapIndices, heapPriorities, 0, heapSize)
234
243
  } else {
@@ -308,10 +317,16 @@ export function solve(bfuData, bfuSizes, maxBfuCount, allocationBias) {
308
317
 
309
318
  const sfi = findScaleFactor(bfuData[i], sz)
310
319
  allScaleFactorIndices[i] = sfi
311
- if (sfi > 0) {
312
- const effectiveScaleFactor = biasedScaleFactors[sfi]
313
- zeroBitDistortions[i] = effectiveScaleFactor * 2.0 * sz
320
+ // Index zero covers both silence and all values below the smallest scale.
321
+ // Use the actual peak there so tiny BFUs do not all receive equal priority.
322
+ let effectiveScaleFactor = biasedScaleFactors[sfi]
323
+ if (sfi === 0) {
324
+ let peak = 0
325
+ for (let j = 0; j < sz; j++)
326
+ peak = Math.max(peak, Math.abs(bfuData[i][j]))
327
+ effectiveScaleFactor = peak === 0 ? 0 : Math.pow(peak, allocationBias)
314
328
  }
329
+ zeroBitDistortions[i] = effectiveScaleFactor * 2.0 * sz
315
330
  }
316
331
 
317
332
  let bestResult = null
@@ -255,12 +255,17 @@ export function selectBlocks(context) {
255
255
  if (options.fixedBlockModes) {
256
256
  blockModes = options.fixedBlockModes
257
257
  } else {
258
+ const thresholds = [
259
+ options.transientThresholdLow,
260
+ options.transientThresholdMid,
261
+ options.transientThresholdHigh,
262
+ ]
258
263
  blockModes = bands.map((bandSamples, bandIndex) => {
259
264
  const coeffs = performFFT(bandSamples, fftSizes[bandIndex])
260
265
  const transient = detectTransient(
261
266
  coeffs,
262
267
  bufferPool.transientDetection[bandIndex],
263
- options.transientThresholdLow
268
+ thresholds[bandIndex]
264
269
  )
265
270
  bufferPool.transientDetection[bandIndex] = coeffs
266
271
  return transient * Math.max(bandIndex + 1, 2)
@@ -5,8 +5,6 @@
5
5
  import {
6
6
  MDCT_OVERLAP_SIZE,
7
7
  MDCT_SHORT_BLOCK_SIZE,
8
- MDCT_SIZE_LONG,
9
- MDCT_SIZE_MID,
10
8
  MDCT_SIZE_SHORT,
11
9
  } from '../core/constants.js'
12
10
  import { calculateBandOffset } from '../core/geometry.js'
@@ -15,9 +13,12 @@ import { MDCT } from '../signal/mdct.js'
15
13
  import { reverseSpectrum } from '../signal/order.js'
16
14
  import { throwError } from '../utils.js'
17
15
 
18
- const shortMdct = new MDCT(MDCT_SIZE_SHORT, 0.5)
19
- const middleMdct = new MDCT(MDCT_SIZE_MID, 0.5)
20
- const longMdct = new MDCT(MDCT_SIZE_LONG, 1)
16
+ const shortMdcts = MDCT_BAND_CONFIGS.map(
17
+ ({ analysisScale }) => new MDCT(MDCT_SIZE_SHORT, analysisScale)
18
+ )
19
+ const longMdcts = MDCT_BAND_CONFIGS.map(
20
+ ({ longMdctSize, analysisScale }) => new MDCT(longMdctSize, analysisScale)
21
+ )
21
22
 
22
23
  /**
23
24
  * Build the ATRAC1 spectrum from analyzed bands.
@@ -31,7 +32,6 @@ export function transformSpectrum(context) {
31
32
  context?.bufferPool ??
32
33
  throwError('transformSpectrum: bufferPool is required')
33
34
  const overlapBuffers = bufferPool.mdctOverlap
34
- const transformFunctions = [middleMdct, middleMdct, longMdct]
35
35
 
36
36
  /**
37
37
  * @param {Float32Array} samples
@@ -48,7 +48,7 @@ export function transformSpectrum(context) {
48
48
  samples,
49
49
  bandIndex,
50
50
  config,
51
- transformFunctions[bandIndex],
51
+ longMdcts[bandIndex],
52
52
  overlapBuffer
53
53
  )
54
54
  }
@@ -71,8 +71,7 @@ export function transformSpectrum(context) {
71
71
  transformFunction,
72
72
  overlapBuffer
73
73
  ) {
74
- const mdctSize = bandIndex === 2 ? MDCT_SIZE_LONG : MDCT_SIZE_MID
75
- const mdctInput = bufferPool.transformBuffers[mdctSize]
74
+ const mdctInput = bufferPool.transformBuffers[transformFunction.size]
76
75
  mdctInput.fill(0)
77
76
  mdctInput.set(overlapBuffer, config.windowStart)
78
77
 
@@ -113,7 +112,10 @@ export function transformSpectrum(context) {
113
112
  applyTailWindowing(blockSamples, overlapBuffer, MDCT_SHORT_BLOCK_SIZE)
114
113
  mdctInput.set(blockSamples, MDCT_OVERLAP_SIZE)
115
114
 
116
- let spectrum = shortMdct.transform(mdctInput, bufferPool.mdctBuffers)
115
+ let spectrum = shortMdcts[bandIndex].transform(
116
+ mdctInput,
117
+ bufferPool.mdctBuffers
118
+ )
117
119
  if (bandIndex > 0) {
118
120
  spectrum = reverseSpectrum(spectrum, bufferPool.reversalBuffers)
119
121
  }
@@ -2,6 +2,9 @@
2
2
  * Carta1 scalar format constants.
3
3
  */
4
4
 
5
+ export const PCM16_FULL_SCALE = 32768
6
+ export const PCM16_NORM_GAIN = 2
7
+
5
8
  export const SAMPLE_RATE = 44100
6
9
  export const SAMPLES_PER_FRAME = 512
7
10
  export const FRAME_RATE = SAMPLE_RATE / SAMPLES_PER_FRAME
@@ -28,18 +31,16 @@ export const BITS_PER_BFU_METADATA = 10
28
31
  export const BFU_AMOUNTS_COUNT = 8
29
32
 
30
33
  export const MDCT_SIZE_SHORT = 64
31
- export const MDCT_SIZE_MID = 256
32
- export const MDCT_SIZE_LONG = 512
34
+ export const MDCT_SIZE_LONG_LOW_MID = 256
35
+ export const MDCT_SIZE_LONG_HIGH = 512
36
+ export const MAX_MDCT_SIZE = MDCT_SIZE_LONG_HIGH
33
37
  export const MDCT_SHORT_BLOCK_SIZE = 32
34
38
  export const MDCT_OVERLAP_SIZE = 32
35
39
  export const MDCT_TAIL_WINDOW_SIZE = 16
36
40
 
37
41
  export const QMF_BANDS = 2
38
42
  export const QMF_TAPS = 48
39
- export const QMF_ANALYSIS_START_SAMPLE = 0
40
- export const QMF_ANALYSIS_STEP_SAMPLES = QMF_BANDS
41
43
  export const QMF_ANALYSIS_WINDOW_SAMPLES = QMF_TAPS
42
- export const QMF_ANALYSIS_MODULATION_SCALE = 1
43
44
  export const QMF_ANALYSIS_TERMS = 24
44
45
  export const QMF_ANALYSIS_DELAY_SAMPLES = QMF_TAPS - QMF_BANDS
45
46
  export const QMF_SYNTHESIS_DELAY_ROWS = 24
@@ -4,10 +4,8 @@
4
4
 
5
5
  import {
6
6
  MAX_WORD_LENGTH_INDEX,
7
- QMF_ANALYSIS_TERMS,
8
- QMF_BANDS,
9
- QMF_SYNTHESIS_DELAY_ROWS,
10
- QMF_SYNTHESIS_TERMS,
7
+ MDCT_SIZE_LONG_HIGH,
8
+ MDCT_SIZE_LONG_LOW_MID,
11
9
  QMF_TAPS,
12
10
  } from './constants.js'
13
11
 
@@ -77,101 +75,26 @@ export const QMF_ODD = (() => {
77
75
  return odd
78
76
  })()
79
77
 
80
- /** Sample positions read by each component of the configurable QMF analysis. */
81
- export const QMF_ANALYSIS_SAMPLE_OFFSETS = (() => {
82
- const offsets = new Uint8Array(QMF_BANDS * QMF_ANALYSIS_TERMS)
83
- for (let term = 0; term < QMF_ANALYSIS_TERMS; term++) {
84
- offsets[term] = QMF_TAPS - 1 - term * QMF_BANDS
85
- offsets[QMF_ANALYSIS_TERMS + term] = QMF_TAPS - QMF_BANDS - term * QMF_BANDS
86
- }
87
- return offsets
88
- })()
89
-
90
- /** Prototype-filter coefficients in component-major QMF analysis order. */
91
- export const QMF_ANALYSIS_FILTER_COEFFICIENTS = (() => {
92
- const coefficients = new Float32Array(QMF_BANDS * QMF_ANALYSIS_TERMS)
93
- coefficients.set(QMF_EVEN)
94
- coefficients.set(QMF_ODD, QMF_ANALYSIS_TERMS)
95
- return coefficients
96
- })()
97
-
98
- /** Dense modulation applied after Carta1 QMF analysis filtering. */
99
- export const QMF_ANALYSIS_MODULATION_COEFFICIENTS = new Float32Array([
100
- 1, 1, 1, -1,
101
- ])
102
-
103
- /* eslint-disable no-loss-of-precision -- Float32Array applies the intended float32 rounding. */
104
-
105
- /** Reference factors used by the optimized sixteen-band modulation path. */
106
- export const QMF_ANALYSIS_PI_OVER_8_BUTTERFLY_SCALES = new Float32Array([
107
- 1.8477590084075928, 0.7653668522834778, 1.8477590084075928,
108
- 0.7653668522834778,
109
- ])
110
-
111
- /** Reference half-angle factors used by sixteen-band modulation. */
112
- export const QMF_ANALYSIS_HALF_BUTTERFLY_SCALES = new Float32Array([
113
- 0.7071067690849304, 0.2071067839860916, 1.2071068286895752,
114
- ])
115
-
116
- /** Reference odd pi/32 cosines used by sixteen-band modulation. */
117
- export const QMF_ANALYSIS_ODD_PI_OVER_32_COSINES = new Float32Array([
118
- 1.990369439125061, 1.913880705833435, 1.7638425827026367, 1.5460208654403687,
119
- 1.2687865495681763, 0.9427934885025024, 0.5805693864822388,
120
- 0.1960342824459076,
121
- ])
122
-
123
- /** Reference odd pi/16 cosines used by sixteen-band modulation. */
124
- export const QMF_ANALYSIS_ODD_PI_OVER_16_COSINES = new Float32Array([
125
- 1.9615705013275146, 1.662939190864563, 1.111140489578247, 0.39018064737319946,
126
- ])
127
-
128
- /** Reference odd pi/64 cosines used by sixteen-band modulation. */
129
- export const QMF_ANALYSIS_ODD_PI_OVER_64_COSINES = new Float32Array([
130
- 1.9975908994674683, 1.9783530235290527, 1.9400625228881836,
131
- 1.8830881118774414, 1.807978630065918, 1.7154572010040283, 1.606415033340454,
132
- 1.4819022417068481, 1.3431179523468018, 1.1913986206054688,
133
- 1.0282055139541626, 0.8551101684570312, 0.6737797260284424,
134
- 0.48596036434173584, 0.2934609651565552, 0.09813535213470459,
135
- ])
136
-
137
- /* eslint-enable no-loss-of-precision */
138
-
139
- /** Delay-row offsets read by each sample of configurable QMF synthesis. */
140
- export const QMF_SYNTHESIS_DELAY_ROW_OFFSETS = (() => {
141
- const offsets = new Uint8Array(QMF_BANDS * QMF_SYNTHESIS_TERMS)
142
- for (let sample = 0; sample < QMF_BANDS; sample++) {
143
- const sampleOffset = sample * QMF_SYNTHESIS_TERMS
144
- for (let term = 0; term < QMF_SYNTHESIS_TERMS; term++) {
145
- offsets[sampleOffset + term] = QMF_SYNTHESIS_DELAY_ROWS - 1 - term
146
- }
147
- }
148
- return offsets
149
- })()
150
-
151
- /** Delay components read by each sample of configurable QMF synthesis. */
152
- export const QMF_SYNTHESIS_DELAY_COMPONENTS = (() => {
153
- const components = new Uint8Array(QMF_BANDS * QMF_SYNTHESIS_TERMS)
154
- components.fill(1, 0, QMF_SYNTHESIS_TERMS)
155
- return components
156
- })()
157
-
158
- /** Prototype-filter coefficients in output-sample-major QMF synthesis order. */
159
- export const QMF_SYNTHESIS_FILTER_COEFFICIENTS = (() => {
160
- const coefficients = new Float64Array(QMF_BANDS * QMF_SYNTHESIS_TERMS)
161
- coefficients.set(QMF_ODD)
162
- coefficients.set(QMF_EVEN, QMF_SYNTHESIS_TERMS)
163
- return coefficients
164
- })()
165
-
166
- /** Dense modulation applied before Carta1 QMF synthesis filtering. */
167
- export const QMF_SYNTHESIS_MODULATION_COEFFICIENTS = new Float64Array([
168
- 0.5, 0.5, 0.5, -0.5,
169
- ])
170
-
78
+ // Band geometry and encoder-side analysis gains.
171
79
  export const MDCT_BAND_CONFIGS = [
172
- { size: 128, windowStart: 48 },
173
- { size: 128, windowStart: 48 },
174
- { size: 256, windowStart: 112 },
80
+ {
81
+ size: 128,
82
+ longMdctSize: MDCT_SIZE_LONG_LOW_MID,
83
+ windowStart: 48,
84
+ analysisScale: 0.5,
85
+ },
86
+ {
87
+ size: 128,
88
+ longMdctSize: MDCT_SIZE_LONG_LOW_MID,
89
+ windowStart: 48,
90
+ analysisScale: 0.5,
91
+ },
92
+ {
93
+ size: 256,
94
+ longMdctSize: MDCT_SIZE_LONG_HIGH,
95
+ windowStart: 112,
96
+ analysisScale: 1,
97
+ },
175
98
  ]
176
99
 
177
100
  export const WORD_LENGTH_BITS = new Int32Array([
@@ -23,7 +23,7 @@ import { SCALE_FACTORS } from '../core/tables.js'
23
23
  export function quantize(coefficients, scaleFactorIndex, bitsPerSample) {
24
24
  const length = coefficients.length
25
25
  const out = new Int32Array(length)
26
- if (bitsPerSample === 0 || scaleFactorIndex === 0) {
26
+ if (bitsPerSample === 0) {
27
27
  out.fill(0, 0, length)
28
28
  return out
29
29
  }
@@ -52,7 +52,7 @@ export function quantize(coefficients, scaleFactorIndex, bitsPerSample) {
52
52
  * @returns {Float32Array}
53
53
  */
54
54
  export function dequantize(quantized, scaleFactorIndex, bitsPerSample) {
55
- if (bitsPerSample === 0 || scaleFactorIndex === 0) {
55
+ if (bitsPerSample === 0) {
56
56
  return new Float32Array(quantized.length)
57
57
  }
58
58