carta1 1.8.0 → 1.9.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.
@@ -9,49 +9,70 @@ import {
9
9
  SPECS_PER_BFU,
10
10
  } from '../core/tables.js'
11
11
 
12
+ /**
13
+ * @typedef {object} SoundUnitAllocationSourceScratch
14
+ * @property {Array<Float32Array>} bfuData
15
+ * @property {Array<number>} bfuSizes
16
+ */
17
+
12
18
  /**
13
19
  * Group spectral coefficients into block floating units.
14
20
  *
15
21
  * @param {Float32Array} coefficients
16
22
  * @param {Array<number>} blockModes
23
+ * @param {SoundUnitAllocationSourceScratch} [scratch]
17
24
  * @returns {object} Grouped bit-frequency units.
18
25
  */
19
- export function groupIntoBFUs(coefficients, blockModes) {
20
- const bfuData = []
21
- const bfuSizes = []
22
- let coeffIndex = 0
26
+ export function groupIntoBFUs(coefficients, blockModes, scratch = null) {
27
+ /** @type {Array<Float32Array>} */
28
+ const bfuData = scratch ? scratch.bfuData : []
29
+ /** @type {Array<number>} */
30
+ const bfuSizes = scratch ? scratch.bfuSizes : []
31
+ let coefficientOffset = 0
23
32
  let bfuIndex = 0
24
33
 
25
34
  for (let band = 0; band < 3; band++) {
26
- const bandStart = coeffIndex
35
+ const bandStart = coefficientOffset
27
36
  const bandSize = band === 2 ? 256 : 128
28
37
  const bandEnd = band < 2 ? BFU_BAND_BOUNDARIES[band] : SPECS_PER_BFU.length
38
+ /** @type {Int32Array} */
29
39
  const startPositions =
30
40
  blockModes[band] === 0 ? BFU_START_LONG : BFU_START_SHORT
31
41
 
32
42
  while (bfuIndex < bandEnd) {
33
43
  const size = SPECS_PER_BFU[bfuIndex]
34
- const startPos = startPositions[bfuIndex] - bandStart
35
- const endPos = startPos + size
36
- const bfu = new Float32Array(size)
44
+ const startOffset = startPositions[bfuIndex] - bandStart
45
+ const endOffset = startOffset + size
46
+ /** @type {Float32Array} */
47
+ const bfu = scratch ? bfuData[bfuIndex] : new Float32Array(size)
37
48
 
38
- if (startPos >= 0 && endPos <= bandSize) {
39
- bfu.set(coefficients.subarray(bandStart + startPos, bandStart + endPos))
40
- } else if (startPos < bandSize && endPos > 0) {
41
- const srcStart = Math.max(0, startPos)
42
- const srcEnd = Math.min(bandSize, endPos)
43
- bfu.set(
44
- coefficients.subarray(bandStart + srcStart, bandStart + srcEnd),
45
- Math.max(0, -startPos)
46
- )
49
+ if (startOffset >= 0 && endOffset <= bandSize) {
50
+ const sourceBase = bandStart + startOffset
51
+ for (let index = 0; index < size; index++) {
52
+ bfu[index] = coefficients[sourceBase + index]
53
+ }
54
+ } else if (startOffset < bandSize && endOffset > 0) {
55
+ bfu.fill(0)
56
+ const sourceStart = Math.max(0, startOffset)
57
+ const sourceEnd = Math.min(bandSize, endOffset)
58
+ const destinationOffset = Math.max(0, -startOffset)
59
+ const count = sourceEnd - sourceStart
60
+ for (let index = 0; index < count; index++) {
61
+ bfu[destinationOffset + index] =
62
+ coefficients[bandStart + sourceStart + index]
63
+ }
64
+ } else if (scratch) {
65
+ bfu.fill(0)
47
66
  }
48
67
 
49
- bfuData.push(bfu)
50
- bfuSizes.push(size)
68
+ if (!scratch) {
69
+ bfuData.push(bfu)
70
+ bfuSizes.push(size)
71
+ }
51
72
  bfuIndex++
52
73
  }
53
74
 
54
- coeffIndex += bandSize
75
+ coefficientOffset += bandSize
55
76
  }
56
77
 
57
78
  return { bfuData, bfuSizes, bfuCount: bfuIndex }
@@ -62,8 +83,9 @@ export function groupIntoBFUs(coefficients, blockModes) {
62
83
  *
63
84
  * @param {Float32Array} coefficients
64
85
  * @param {Array<number>} blockModes
86
+ * @param {SoundUnitAllocationSourceScratch} [scratch]
65
87
  * @returns {object} Allocation source.
66
88
  */
67
- export function prepare(coefficients, blockModes) {
68
- return groupIntoBFUs(coefficients, blockModes)
89
+ export function prepare(coefficients, blockModes, scratch = null) {
90
+ return groupIntoBFUs(coefficients, blockModes, scratch)
69
91
  }
@@ -15,6 +15,7 @@ import { solve } from './solve.js'
15
15
  export function allocate(context) {
16
16
  const options =
17
17
  context?.options ?? throwError('allocate: options is required')
18
+ const scratch = context?.bufferPool?.encoder?.scratch?.allocation ?? null
18
19
 
19
20
  /**
20
21
  * @param {object} input
@@ -22,8 +23,19 @@ export function allocate(context) {
22
23
  */
23
24
  return (input) => {
24
25
  const { coefficients, blockModes } = input
25
- const { bfuData, bfuSizes, bfuCount } = prepare(coefficients, blockModes)
26
- const result = solve(bfuData, bfuSizes, bfuCount, options.allocationBias)
26
+ const { bfuData, bfuSizes, bfuCount } = prepare(
27
+ coefficients,
28
+ blockModes,
29
+ scratch
30
+ )
31
+ const result = solve(
32
+ bfuData,
33
+ bfuSizes,
34
+ bfuCount,
35
+ options.allocationBias,
36
+ blockModes,
37
+ scratch
38
+ )
27
39
  const nBfu = result.bfuCount
28
40
 
29
41
  return {
@@ -31,7 +43,7 @@ export function allocate(context) {
31
43
  bfuSizes,
32
44
  nBfu,
33
45
  scaleFactorIndices: result.scaleFactorIndices.slice(0, nBfu),
34
- wordLengthIndices: result.allocation.slice(0, nBfu),
46
+ wordLengthIndices: result.allocation,
35
47
  blockModes,
36
48
  }
37
49
  }
@@ -40,8 +40,9 @@ function analyzePair(input, delay, scratch) {
40
40
  export function analyzeBands(context) {
41
41
  const bufferPool =
42
42
  context?.bufferPool ?? throwError('analyzeBands: bufferPool is required')
43
- const delays = bufferPool.qmfAnalysisDelays
44
- const scratch = bufferPool.qmfScratch
43
+ const state = bufferPool.encoder.state
44
+ const delays = state.qmfAnalysisDelays
45
+ const scratch = bufferPool.encoder.scratch.qmf
45
46
 
46
47
  /**
47
48
  * @param {Float32Array} pcmSamples
@@ -52,14 +53,14 @@ export function analyzeBands(context) {
52
53
  const stage2 = analyzePair(stage1[0], delays.midBand, scratch)
53
54
 
54
55
  const delayedHigh = scratch.highBandWindows[stage1[1].length]
55
- delayedHigh.set(bufferPool.qmfHighBandDelay)
56
- delayedHigh.set(stage1[1], bufferPool.qmfHighBandDelay.length)
56
+ delayedHigh.set(state.qmfHighBandDelay)
57
+ delayedHigh.set(stage1[1], state.qmfHighBandDelay.length)
57
58
 
58
59
  const highBand = delayedHigh.slice(0, stage1[1].length)
59
- bufferPool.qmfHighBandDelay.set(
60
+ state.qmfHighBandDelay.set(
60
61
  delayedHigh.subarray(
61
62
  stage1[1].length,
62
- stage1[1].length + bufferPool.qmfHighBandDelay.length
63
+ stage1[1].length + state.qmfHighBandDelay.length
63
64
  )
64
65
  )
65
66
 
@@ -188,11 +188,19 @@ function calculateTransientScore(features) {
188
188
  * Perform FFT on time-domain samples and return magnitude spectrum
189
189
  * @param {Float32Array} samples - Time-domain samples
190
190
  * @param {i32} fftSize - FFT size (must be power of 2)
191
+ * @param {object|null} [scratch=null] - Optional reusable FFT work buffers
191
192
  * @returns {Float32Array} Magnitude spectrum (positive frequencies only)
192
193
  */
193
- export function performFFT(samples, fftSize) {
194
- const real = new Float32Array(fftSize)
195
- const imag = new Float32Array(fftSize)
194
+ export function performFFT(samples, fftSize, scratch = null) {
195
+ const work = scratch?.fft?.[fftSize]
196
+ /** @type {Float32Array} */
197
+ const real = work ? work.real : new Float32Array(fftSize)
198
+ /** @type {Float32Array} */
199
+ const imag = work ? work.imaginary : new Float32Array(fftSize)
200
+ if (work) {
201
+ real.fill(0)
202
+ imag.fill(0)
203
+ }
196
204
 
197
205
  // Copy samples with zero-padding if necessary
198
206
  const copyLen = Math.min(samples.length, fftSize)
@@ -202,7 +210,8 @@ export function performFFT(samples, fftSize) {
202
210
  FFT.transform(real, imag)
203
211
 
204
212
  // Calculate magnitude spectrum for positive frequencies
205
- const magnitude = new Float32Array(fftSize / 2)
213
+ /** @type {Float32Array} */
214
+ const magnitude = work ? work.magnitudes : new Float32Array(fftSize / 2)
206
215
  for (let i = 0; i < fftSize / 2; i++) {
207
216
  magnitude[i] = Math.sqrt(real[i] * real[i] + imag[i] * imag[i])
208
217
  }
@@ -242,6 +251,8 @@ export function selectBlocks(context) {
242
251
  context?.bufferPool ?? throwError('selectBlocks: bufferPool is required')
243
252
  const options =
244
253
  context?.options ?? throwError('selectBlocks: options is required')
254
+ const state = bufferPool.encoder.state
255
+ const scratch = bufferPool.encoder.scratch.transient
245
256
  const fftSizes = [FFT_SIZE_LOW, FFT_SIZE_MID, FFT_SIZE_HIGH]
246
257
 
247
258
  /**
@@ -261,13 +272,13 @@ export function selectBlocks(context) {
261
272
  options.transientThresholdHigh,
262
273
  ]
263
274
  blockModes = bands.map((bandSamples, bandIndex) => {
264
- const coeffs = performFFT(bandSamples, fftSizes[bandIndex])
275
+ const magnitudes = performFFT(bandSamples, fftSizes[bandIndex], scratch)
265
276
  const transient = detectTransient(
266
- coeffs,
267
- bufferPool.transientDetection[bandIndex],
277
+ magnitudes,
278
+ state.transientDetection[bandIndex],
268
279
  thresholds[bandIndex]
269
280
  )
270
- bufferPool.transientDetection[bandIndex] = coeffs
281
+ state.transientDetection[bandIndex].set(magnitudes)
271
282
  return transient * Math.max(bandIndex + 1, 2)
272
283
  })
273
284
  }
@@ -31,7 +31,9 @@ export function transformSpectrum(context) {
31
31
  const bufferPool =
32
32
  context?.bufferPool ??
33
33
  throwError('transformSpectrum: bufferPool is required')
34
- const overlapBuffers = bufferPool.mdctOverlap
34
+ const state = bufferPool.encoder.state
35
+ const scratch = bufferPool.encoder.scratch.mdct
36
+ const overlapBuffers = state.mdctOverlap
35
37
 
36
38
  /**
37
39
  * @param {Float32Array} samples
@@ -71,19 +73,16 @@ export function transformSpectrum(context) {
71
73
  transformFunction,
72
74
  overlapBuffer
73
75
  ) {
74
- const mdctInput = bufferPool.transformBuffers[transformFunction.size]
76
+ const mdctInput = scratch.transformBuffers[transformFunction.size]
75
77
  mdctInput.fill(0)
76
78
  mdctInput.set(overlapBuffer, config.windowStart)
77
79
 
78
80
  applyTailWindowing(samples, overlapBuffer, config.size)
79
81
  mdctInput.set(samples, config.windowStart + MDCT_OVERLAP_SIZE)
80
82
 
81
- let spectrum = transformFunction.transform(
82
- mdctInput,
83
- bufferPool.mdctBuffers
84
- )
83
+ let spectrum = transformFunction.transform(mdctInput, scratch.fftBuffers)
85
84
  if (bandIndex > 0) {
86
- spectrum = reverseSpectrum(spectrum, bufferPool.reversalBuffers)
85
+ spectrum = reverseSpectrum(spectrum, scratch.reversalBuffers)
87
86
  }
88
87
  return spectrum
89
88
  }
@@ -105,7 +104,7 @@ export function transformSpectrum(context) {
105
104
  blockStart,
106
105
  blockStart + MDCT_SHORT_BLOCK_SIZE
107
106
  )
108
- const mdctInput = bufferPool.transformBuffers[MDCT_SIZE_SHORT]
107
+ const mdctInput = scratch.transformBuffers[MDCT_SIZE_SHORT]
109
108
  mdctInput.fill(0)
110
109
  mdctInput.set(overlapBuffer, 0)
111
110
 
@@ -114,10 +113,10 @@ export function transformSpectrum(context) {
114
113
 
115
114
  let spectrum = shortMdcts[bandIndex].transform(
116
115
  mdctInput,
117
- bufferPool.mdctBuffers
116
+ scratch.fftBuffers
118
117
  )
119
118
  if (bandIndex > 0) {
120
- spectrum = reverseSpectrum(spectrum, bufferPool.reversalBuffers)
119
+ spectrum = reverseSpectrum(spectrum, scratch.reversalBuffers)
121
120
  }
122
121
  output.set(spectrum, blockStart)
123
122
  }
@@ -22,7 +22,7 @@ export const STREAM_FLUSH_TAIL_FRAMES = 1
22
22
 
23
23
  export const SOUND_UNIT_SIZE = 212
24
24
  export const FRAME_BITS = SOUND_UNIT_SIZE * 8
25
- export const FRAME_OVERHEAD_BITS = 40
25
+ export const FRAME_OVERHEAD_BITS = 32
26
26
  export const BITRATE_PER_CHANNEL = SOUND_UNIT_SIZE * FRAME_RATE * 8
27
27
 
28
28
  export const NUM_BFUS = 52
@@ -65,3 +65,15 @@ export const FRAME_SCALE_FACTOR_BITS = 6
65
65
 
66
66
  export const QUANTIZATION_SIGN_BIT_SHIFT = 1
67
67
  export const MAX_WORD_LENGTH_INDEX = 15
68
+
69
+ export const NUM_WORD_LENGTHS = 16
70
+ export const BFU_DISTORTION_CACHE_STRIDE = 16
71
+ export const TUNE_BINARY_SEARCH_STEPS = 10
72
+ export const PARETO_EXCHANGE_PASSES = 4
73
+ export const MIN_SLACK_FILL_BITS = 4
74
+ export const MASK_EXPONENT_LONG = 0.4
75
+ export const MASK_EXPONENT_SHORT = 0.3
76
+ export const MASK_RELATIVE_FLOOR = 1e-6
77
+ export const MASK_MIN_FLOOR = 1e-30
78
+ export const HEARING_THRESHOLD_ENERGY_GATE = 1e-9
79
+ export const EAR_SENSITIVITY_FLOOR_DB = -9.0
@@ -3,9 +3,13 @@
3
3
  */
4
4
 
5
5
  import {
6
+ EAR_SENSITIVITY_FLOOR_DB,
7
+ MAX_BFU_SIZE,
6
8
  MAX_WORD_LENGTH_INDEX,
7
9
  MDCT_SIZE_LONG_HIGH,
8
10
  MDCT_SIZE_LONG_LOW_MID,
11
+ NUM_BFUS,
12
+ NUM_WORD_LENGTHS,
9
13
  QMF_TAPS,
10
14
  } from './constants.js'
11
15
 
@@ -109,30 +113,97 @@ export const SCALE_FACTORS = (() => {
109
113
  return table
110
114
  })()
111
115
 
112
- export const INV_POWER_OF_TWO = (() => {
113
- const maxBits = WORD_LENGTH_BITS[MAX_WORD_LENGTH_INDEX]
114
- const table = new Float64Array(maxBits + 1)
115
- for (let bits = 0; bits <= maxBits; bits++) {
116
- table[bits] = Math.pow(2, -bits)
116
+ /** Simultaneous upward masking spread across adjacent perceptual units. */
117
+ export const SPREAD_UP = new Float64Array([
118
+ 1.0, 0.03162277660168379, 0.001, 0.00003162277660168379,
119
+ ])
120
+
121
+ /** Simultaneous downward masking spread across adjacent perceptual units. */
122
+ export const SPREAD_DOWN = new Float64Array([
123
+ 1.0, 0.005011872336272725, 0.000019952623149688796,
124
+ ])
125
+
126
+ /** Maximum signed quantizer code per word-length index. */
127
+ export const QUANTIZER_RANGES = (() => {
128
+ const table = new Int32Array(NUM_WORD_LENGTHS)
129
+ for (let wordLength = 1; wordLength <= MAX_WORD_LENGTH_INDEX; wordLength++) {
130
+ const bits = WORD_LENGTH_BITS[wordLength]
131
+ table[wordLength] = (1 << (bits - 1)) - 1
132
+ }
133
+ return table
134
+ })()
135
+
136
+ /** Outer-bin peak ratio where lowering scale-factor index by 1 reduces MSE. */
137
+ export const SCALE_FACTOR_TRIM_RATIOS = (() => {
138
+ const table = new Float64Array(NUM_WORD_LENGTHS)
139
+ for (let wordLength = 1; wordLength <= MAX_WORD_LENGTH_INDEX; wordLength++) {
140
+ const quantizerRange = QUANTIZER_RANGES[wordLength]
141
+ table[wordLength] = (2.0 * quantizerRange) / (2.0 * quantizerRange + 1.0)
142
+ }
143
+ return table
144
+ })()
145
+
146
+ /** Reciprocal natural log of BFU spectral width for tonality normalization. */
147
+ export const INVERSE_LOG_BFU_SIZES = (() => {
148
+ const table = new Float64Array(MAX_BFU_SIZE + 1)
149
+ for (let size = 2; size <= MAX_BFU_SIZE; size++) {
150
+ table[size] = 1.0 / Math.log(size)
117
151
  }
118
152
  return table
119
153
  })()
120
154
 
121
- export const WORD_LENGTH_DELTA_BITS = (() => {
122
- const table = new Int32Array(MAX_WORD_LENGTH_INDEX)
123
- for (let i = 0; i < MAX_WORD_LENGTH_INDEX; i++) {
124
- table[i] = (WORD_LENGTH_BITS[i + 1] - WORD_LENGTH_BITS[i]) | 0
155
+ /** Bounded outer/middle-ear sensitivity weights for long-block BFUs. */
156
+ export const EAR_WEIGHT_LONG = (() => {
157
+ const table = new Float64Array(NUM_BFUS)
158
+ for (let unit = 0; unit < NUM_BFUS; unit++) {
159
+ const centerBin = BFU_START_LONG[unit] + SPECS_PER_BFU[unit] * 0.5
160
+ const frequencyKhz = Math.max(0.05, (centerBin / 512.0) * 22.05)
161
+ const weightDb = Math.max(
162
+ EAR_SENSITIVITY_FLOOR_DB,
163
+ -0.6 * 3.64 * Math.pow(frequencyKhz, -0.8) +
164
+ 6.5 * Math.exp(-0.6 * (frequencyKhz - 3.3) * (frequencyKhz - 3.3)) -
165
+ 1e-3 * Math.pow(frequencyKhz, 3.6)
166
+ )
167
+ table[unit] = Math.pow(10.0, weightDb / 10.0)
125
168
  }
126
169
  return table
127
170
  })()
128
171
 
129
- export const DISTORTION_DELTA_FACTORS = (() => {
130
- const table = new Float64Array(MAX_WORD_LENGTH_INDEX)
131
- table[0] = 2.0 - INV_POWER_OF_TWO[WORD_LENGTH_BITS[1]]
132
- for (let i = 1; i < MAX_WORD_LENGTH_INDEX; i++) {
133
- const bits1 = WORD_LENGTH_BITS[i] | 0
134
- const bits2 = WORD_LENGTH_BITS[i + 1] | 0
135
- table[i] = INV_POWER_OF_TWO[bits1] - INV_POWER_OF_TWO[bits2]
172
+ /** Bounded outer/middle-ear sensitivity weights for short-block BFUs. */
173
+ export const EAR_WEIGHT_SHORT = (() => {
174
+ const table = new Float64Array(NUM_BFUS)
175
+ const lowOffsets = new Int32Array([0, 8, 12, 20, 26])
176
+ const midOffsets = new Int32Array([0, 6, 13, 22])
177
+ const highOffsets = new Int32Array([0, 12])
178
+ for (let unit = 0; unit < NUM_BFUS; unit++) {
179
+ let frequencyKhz = 0.05
180
+ if (unit < 20) {
181
+ const frequencyGroup = unit >> 2
182
+ frequencyKhz = Math.max(
183
+ 0.05,
184
+ ((lowOffsets[frequencyGroup] + SPECS_PER_BFU[unit] * 0.5) / 32.0) *
185
+ 5.5125
186
+ )
187
+ } else if (unit < 36) {
188
+ const frequencyGroup = (unit - 20) >> 2
189
+ frequencyKhz =
190
+ 5.5125 +
191
+ ((midOffsets[frequencyGroup] + SPECS_PER_BFU[unit] * 0.5) / 32.0) *
192
+ 5.5125
193
+ } else {
194
+ const frequencyGroup = (unit - 36) >> 3
195
+ frequencyKhz =
196
+ 11.025 +
197
+ ((highOffsets[frequencyGroup] + SPECS_PER_BFU[unit] * 0.5) / 32.0) *
198
+ 11.025
199
+ }
200
+ const weightDb = Math.max(
201
+ EAR_SENSITIVITY_FLOOR_DB,
202
+ -0.6 * 3.64 * Math.pow(frequencyKhz, -0.8) +
203
+ 6.5 * Math.exp(-0.6 * (frequencyKhz - 3.3) * (frequencyKhz - 3.3)) -
204
+ 1e-3 * Math.pow(frequencyKhz, 3.6)
205
+ )
206
+ table[unit] = Math.pow(10.0, weightDb / 10.0)
136
207
  }
137
208
  return table
138
209
  })()
package/codec/index.js CHANGED
@@ -10,7 +10,7 @@ import { quantize, dequantize } from './quantization/spectrum.js'
10
10
  import { analyzeBands } from './analysis/bands.js'
11
11
  import { transformSpectrum } from './analysis/spectrum.js'
12
12
  import { pipe } from './utils.js'
13
- import { BufferPool } from './state.js'
13
+ import { BufferPool } from './state/storage.js'
14
14
  import { EncoderOptions } from './core/options.js'
15
15
  import { AudioProcessor } from './boundary/processor.js'
16
16
  import { decodeAeaPcm, encodeAeaPcm } from './boundary/stream.js'
@@ -6,7 +6,8 @@
6
6
  */
7
7
 
8
8
  import { dequantize } from '../dequantization.js'
9
- import { BufferPool } from '../state.js'
9
+ import { initializeDecoderStream } from '../state/decoder.js'
10
+ import { BufferPool } from '../state/storage.js'
10
11
  import { synthesize } from '../synthesis/stage.js'
11
12
  import { pipe } from '../utils.js'
12
13
 
@@ -17,6 +18,7 @@ import { pipe } from '../utils.js'
17
18
  * @returns {Function}
18
19
  */
19
20
  export function decode(bufferPool = new BufferPool()) {
21
+ initializeDecoderStream(bufferPool.decoder)
20
22
  const context = { bufferPool }
21
23
  return pipe(context, dequantize, synthesize)
22
24
  }
@@ -9,7 +9,8 @@ import { allocate } from '../allocation/stage.js'
9
9
  import { analyze } from '../analysis/stage.js'
10
10
  import { EncoderOptions } from '../core/options.js'
11
11
  import { quantize } from '../quantization/stage.js'
12
- import { BufferPool } from '../state.js'
12
+ import { initializeEncoderStream } from '../state/encoder.js'
13
+ import { BufferPool } from '../state/storage.js'
13
14
  import { pipe } from '../utils.js'
14
15
 
15
16
  /**
@@ -23,6 +24,7 @@ export function encode(
23
24
  options = new EncoderOptions(),
24
25
  bufferPool = new BufferPool()
25
26
  ) {
27
+ initializeEncoderStream(bufferPool.encoder)
26
28
  const context = { options, bufferPool }
27
29
  return pipe(context, analyze, allocate, quantize)
28
30
  }
@@ -0,0 +1,71 @@
1
+ /** Carta1 Audio Codec - Decoder state and stream initialization. */
2
+
3
+ import {
4
+ MDCT_SIZE_LONG_HIGH,
5
+ MDCT_SIZE_LONG_LOW_MID,
6
+ QMF_HIGH_BAND_DELAY,
7
+ QMF_SYNTHESIS_DELAY_SAMPLES,
8
+ } from '../core/constants.js'
9
+
10
+ /** Persistent delay state for one two-band QMF synthesis stage. */
11
+ export class QmfSynthesisState {
12
+ /** Allocate the circular polyphase delay ring at row zero. */
13
+ constructor() {
14
+ this.delay = new Float32Array(QMF_SYNTHESIS_DELAY_SAMPLES)
15
+ this.delayRow = 0
16
+ }
17
+
18
+ /**
19
+ * Reset the delay ring and row cursor to neutral silence.
20
+ *
21
+ * @returns {QmfSynthesisState} This reset synthesis state.
22
+ */
23
+ reset() {
24
+ this.delay.fill(0)
25
+ this.delayRow = 0
26
+ return this
27
+ }
28
+ }
29
+
30
+ /** Committed cross-frame history for ATRAC1 decoding. */
31
+ export class DecoderState {
32
+ /** Allocate neutral QMF and IMDCT overlap histories. */
33
+ constructor() {
34
+ this.qmfSynthesisStates = {
35
+ lowBand: new QmfSynthesisState(),
36
+ midBand: new QmfSynthesisState(),
37
+ }
38
+ this.qmfHighBandDelay = new Float32Array(QMF_HIGH_BAND_DELAY)
39
+ this.imdctOverlap = [
40
+ new Float32Array(MDCT_SIZE_LONG_LOW_MID),
41
+ new Float32Array(MDCT_SIZE_LONG_LOW_MID),
42
+ new Float32Array(MDCT_SIZE_LONG_HIGH),
43
+ ]
44
+ }
45
+
46
+ /**
47
+ * Reset committed cross-frame decoder history to neutral silence.
48
+ *
49
+ * @returns {DecoderState} This reset decoder state.
50
+ */
51
+ reset() {
52
+ this.qmfSynthesisStates.lowBand.reset()
53
+ this.qmfSynthesisStates.midBand.reset()
54
+ this.qmfHighBandDelay.fill(0)
55
+ for (let band = 0; band < this.imdctOverlap.length; band++) {
56
+ this.imdctOverlap[band].fill(0)
57
+ }
58
+ return this
59
+ }
60
+ }
61
+
62
+ /**
63
+ * Reset persistent decoder histories before starting one stream.
64
+ *
65
+ * @param {object} decoder Decoder pool ownership root.
66
+ * @returns {object} Initialized decoder storage.
67
+ */
68
+ export function initializeDecoderStream(decoder) {
69
+ decoder.state.reset()
70
+ return decoder
71
+ }
@@ -0,0 +1,113 @@
1
+ /** Carta1 Audio Codec - Encoder state and reusable analysis/allocation scratch. */
2
+
3
+ import {
4
+ BFU_DISTORTION_CACHE_STRIDE,
5
+ FFT_SIZE_HIGH,
6
+ FFT_SIZE_LOW,
7
+ FFT_SIZE_MID,
8
+ MDCT_OVERLAP_SIZE,
9
+ NUM_BFUS,
10
+ QMF_ANALYSIS_DELAY_SAMPLES,
11
+ QMF_HIGH_BAND_DELAY,
12
+ } from '../core/constants.js'
13
+ import { SPECS_PER_BFU } from '../core/tables.js'
14
+
15
+ /** Committed cross-frame history for ATRAC1 encoding. */
16
+ export class EncoderState {
17
+ /** Allocate neutral QMF delays, transient history, and MDCT overlap rows. */
18
+ constructor() {
19
+ this.qmfAnalysisDelays = {
20
+ lowBand: new Float32Array(QMF_ANALYSIS_DELAY_SAMPLES),
21
+ midBand: new Float32Array(QMF_ANALYSIS_DELAY_SAMPLES),
22
+ }
23
+ this.qmfHighBandDelay = new Float32Array(QMF_HIGH_BAND_DELAY)
24
+ this.transientDetection = [
25
+ new Float32Array(FFT_SIZE_LOW / 2),
26
+ new Float32Array(FFT_SIZE_MID / 2),
27
+ new Float32Array(FFT_SIZE_HIGH / 2),
28
+ ]
29
+ this.mdctOverlap = [
30
+ new Float32Array(MDCT_OVERLAP_SIZE),
31
+ new Float32Array(MDCT_OVERLAP_SIZE),
32
+ new Float32Array(MDCT_OVERLAP_SIZE),
33
+ ]
34
+ }
35
+
36
+ /**
37
+ * Reset committed cross-frame encoder history to neutral silence.
38
+ *
39
+ * @returns {EncoderState} This reset encoder state.
40
+ */
41
+ reset() {
42
+ this.qmfAnalysisDelays.lowBand.fill(0)
43
+ this.qmfAnalysisDelays.midBand.fill(0)
44
+ this.qmfHighBandDelay.fill(0)
45
+ for (let band = 0; band < this.transientDetection.length; band++) {
46
+ this.transientDetection[band].fill(0)
47
+ }
48
+ for (let band = 0; band < this.mdctOverlap.length; band++) {
49
+ this.mdctOverlap[band].fill(0)
50
+ }
51
+ return this
52
+ }
53
+ }
54
+
55
+ /** Fixed FFT work buffers for transient detection across subbands. */
56
+ export class TransientAnalysisScratch {
57
+ /** Allocate reusable complex FFT and magnitude rows per FFT size. */
58
+ constructor() {
59
+ this.fft = {
60
+ 128: {
61
+ real: new Float32Array(FFT_SIZE_LOW),
62
+ imaginary: new Float32Array(FFT_SIZE_LOW),
63
+ magnitudes: new Float32Array(FFT_SIZE_LOW / 2),
64
+ },
65
+ 256: {
66
+ real: new Float32Array(FFT_SIZE_HIGH),
67
+ imaginary: new Float32Array(FFT_SIZE_HIGH),
68
+ magnitudes: new Float32Array(FFT_SIZE_HIGH / 2),
69
+ },
70
+ }
71
+ }
72
+ }
73
+
74
+ /** Reusable per-stream scratch storage for Sound-Unit bit allocation. */
75
+ export class SoundUnitAllocationScratch {
76
+ /** Allocate fixed BFU coefficient rows and solver work arrays. */
77
+ constructor() {
78
+ /** @type {Array<Float32Array>} */
79
+ const bfuData = []
80
+ /** @type {Array<number>} */
81
+ const bfuSizes = []
82
+ for (let i = 0; i < NUM_BFUS; i++) {
83
+ const size = SPECS_PER_BFU[i]
84
+ bfuData.push(new Float32Array(size))
85
+ bfuSizes.push(size)
86
+ }
87
+ this.bfuData = bfuData
88
+ this.bfuSizes = bfuSizes
89
+ this.energies = new Float64Array(NUM_BFUS)
90
+ this.maxAmplitudes = new Float64Array(NUM_BFUS)
91
+ this.initialScaleFactorIndices = new Int32Array(NUM_BFUS)
92
+ this.maskThresholds = new Float64Array(NUM_BFUS)
93
+ this.perceptualWeights = new Float64Array(NUM_BFUS)
94
+ this.seedThresholds = new Float64Array(NUM_BFUS)
95
+ this.distortionCache = new Float64Array(
96
+ NUM_BFUS * BFU_DISTORTION_CACHE_STRIDE
97
+ )
98
+ this.candidateWordLengths = new Int32Array(NUM_BFUS)
99
+ this.selectedWordLengths = new Int32Array(NUM_BFUS)
100
+ this.refinedScaleFactorIndices = new Int32Array(NUM_BFUS)
101
+ }
102
+ }
103
+
104
+ /**
105
+ * Reset persistent encoder histories before starting one stream.
106
+ *
107
+ * @param {object} encoder Encoder pool ownership root.
108
+ * @returns {object} Initialized encoder storage.
109
+ */
110
+ export function initializeEncoderStream(encoder) {
111
+ encoder.state.reset()
112
+ return encoder
113
+ }