carta1 1.7.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.
- package/codec/allocation/distortion.js +371 -0
- package/codec/allocation/refine.js +461 -0
- package/codec/allocation/solve.js +247 -318
- package/codec/allocation/source.js +44 -22
- package/codec/allocation/stage.js +15 -3
- package/codec/analysis/bands.js +7 -6
- package/codec/analysis/blocks.js +25 -9
- package/codec/analysis/spectrum.js +9 -10
- package/codec/core/constants.js +13 -1
- package/codec/core/tables.js +87 -16
- package/codec/index.js +1 -1
- package/codec/pipeline/decoder.js +3 -1
- package/codec/pipeline/encoder.js +3 -1
- package/codec/state/decoder.js +71 -0
- package/codec/state/encoder.js +113 -0
- package/codec/state/storage.js +38 -0
- package/codec/state/transform.js +61 -0
- package/codec/syntax/frame.js +3 -4
- package/codec/synthesis/bands.js +9 -13
- package/codec/synthesis/pcm.js +7 -6
- package/dist/carta1-worker.min.js +1 -1
- package/dist/carta1-worker.min.js.map +1 -1
- package/dist/carta1.min.js +1 -1
- package/dist/carta1.min.js.map +1 -1
- package/package.json +1 -1
- package/codec/state.js +0 -112
|
@@ -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
|
-
|
|
21
|
-
const
|
|
22
|
-
|
|
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 =
|
|
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
|
|
35
|
-
const
|
|
36
|
-
|
|
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 (
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
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
|
-
|
|
50
|
-
|
|
68
|
+
if (!scratch) {
|
|
69
|
+
bfuData.push(bfu)
|
|
70
|
+
bfuSizes.push(size)
|
|
71
|
+
}
|
|
51
72
|
bfuIndex++
|
|
52
73
|
}
|
|
53
74
|
|
|
54
|
-
|
|
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(
|
|
26
|
-
|
|
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
|
|
46
|
+
wordLengthIndices: result.allocation,
|
|
35
47
|
blockModes,
|
|
36
48
|
}
|
|
37
49
|
}
|
package/codec/analysis/bands.js
CHANGED
|
@@ -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
|
|
44
|
-
const
|
|
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(
|
|
56
|
-
delayedHigh.set(stage1[1],
|
|
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
|
-
|
|
60
|
+
state.qmfHighBandDelay.set(
|
|
60
61
|
delayedHigh.subarray(
|
|
61
62
|
stage1[1].length,
|
|
62
|
-
stage1[1].length +
|
|
63
|
+
stage1[1].length + state.qmfHighBandDelay.length
|
|
63
64
|
)
|
|
64
65
|
)
|
|
65
66
|
|
package/codec/analysis/blocks.js
CHANGED
|
@@ -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
|
|
195
|
-
|
|
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
|
-
|
|
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
|
/**
|
|
@@ -255,14 +266,19 @@ export function selectBlocks(context) {
|
|
|
255
266
|
if (options.fixedBlockModes) {
|
|
256
267
|
blockModes = options.fixedBlockModes
|
|
257
268
|
} else {
|
|
269
|
+
const thresholds = [
|
|
270
|
+
options.transientThresholdLow,
|
|
271
|
+
options.transientThresholdMid,
|
|
272
|
+
options.transientThresholdHigh,
|
|
273
|
+
]
|
|
258
274
|
blockModes = bands.map((bandSamples, bandIndex) => {
|
|
259
|
-
const
|
|
275
|
+
const magnitudes = performFFT(bandSamples, fftSizes[bandIndex], scratch)
|
|
260
276
|
const transient = detectTransient(
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
277
|
+
magnitudes,
|
|
278
|
+
state.transientDetection[bandIndex],
|
|
279
|
+
thresholds[bandIndex]
|
|
264
280
|
)
|
|
265
|
-
|
|
281
|
+
state.transientDetection[bandIndex].set(magnitudes)
|
|
266
282
|
return transient * Math.max(bandIndex + 1, 2)
|
|
267
283
|
})
|
|
268
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
|
|
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 =
|
|
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,
|
|
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 =
|
|
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
|
-
|
|
116
|
+
scratch.fftBuffers
|
|
118
117
|
)
|
|
119
118
|
if (bandIndex > 0) {
|
|
120
|
-
spectrum = reverseSpectrum(spectrum,
|
|
119
|
+
spectrum = reverseSpectrum(spectrum, scratch.reversalBuffers)
|
|
121
120
|
}
|
|
122
121
|
output.set(spectrum, blockStart)
|
|
123
122
|
}
|
package/codec/core/constants.js
CHANGED
|
@@ -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 =
|
|
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
|
package/codec/core/tables.js
CHANGED
|
@@ -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
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
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
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
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
|
-
|
|
130
|
-
|
|
131
|
-
table
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
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 {
|
|
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 {
|
|
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
|
+
}
|