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
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/** Carta1 Audio Codec - Reusable typed-array ownership. */
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
EncoderState,
|
|
5
|
+
SoundUnitAllocationScratch,
|
|
6
|
+
TransientAnalysisScratch,
|
|
7
|
+
} from './encoder.js'
|
|
8
|
+
import { DecoderState } from './decoder.js'
|
|
9
|
+
import { MdctScratch, QmfScratch } from './transform.js'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Own reusable codec storage according to the lifetime of its contents.
|
|
13
|
+
*
|
|
14
|
+
* - `state` is committed history with meaning across chronological frames;
|
|
15
|
+
* - `scratch` is private to one algorithm call and carries no stage meaning.
|
|
16
|
+
*/
|
|
17
|
+
export class BufferPool {
|
|
18
|
+
/** Allocate neutral encoder and decoder storage. */
|
|
19
|
+
constructor() {
|
|
20
|
+
this.encoder = {
|
|
21
|
+
state: new EncoderState(),
|
|
22
|
+
scratch: {
|
|
23
|
+
qmf: new QmfScratch(),
|
|
24
|
+
transient: new TransientAnalysisScratch(),
|
|
25
|
+
mdct: new MdctScratch(),
|
|
26
|
+
allocation: new SoundUnitAllocationScratch(),
|
|
27
|
+
},
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
this.decoder = {
|
|
31
|
+
state: new DecoderState(),
|
|
32
|
+
scratch: {
|
|
33
|
+
mdct: new MdctScratch(),
|
|
34
|
+
qmf: new QmfScratch(),
|
|
35
|
+
},
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/** Fixed reusable transform storage for ATRAC1 QMF and MDCT kernels. */
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
MDCT_SIZE_LONG_HIGH,
|
|
5
|
+
MDCT_SIZE_LONG_LOW_MID,
|
|
6
|
+
MDCT_SIZE_SHORT,
|
|
7
|
+
QMF_ANALYSIS_DELAY_SAMPLES,
|
|
8
|
+
QMF_BANDS,
|
|
9
|
+
QMF_HIGH_BAND_DELAY,
|
|
10
|
+
} from '../core/constants.js'
|
|
11
|
+
|
|
12
|
+
/** Reusable windows and polyphase accumulators for two-band QMF stages. */
|
|
13
|
+
export class QmfScratch {
|
|
14
|
+
/** Allocate sliding analysis windows, high-band delay windows, and work rows. */
|
|
15
|
+
constructor() {
|
|
16
|
+
this.analysisWindows = {
|
|
17
|
+
128: new Float32Array(QMF_ANALYSIS_DELAY_SAMPLES + 128),
|
|
18
|
+
256: new Float32Array(QMF_ANALYSIS_DELAY_SAMPLES + 256),
|
|
19
|
+
512: new Float32Array(QMF_ANALYSIS_DELAY_SAMPLES + 512),
|
|
20
|
+
}
|
|
21
|
+
this.highBandWindows = {
|
|
22
|
+
128: new Float32Array(QMF_HIGH_BAND_DELAY + 128),
|
|
23
|
+
256: new Float32Array(QMF_HIGH_BAND_DELAY + 256),
|
|
24
|
+
}
|
|
25
|
+
this.polyphase = new Float32Array(QMF_BANDS)
|
|
26
|
+
this.extended = new Float64Array(QMF_BANDS)
|
|
27
|
+
this.bands = new Float32Array(QMF_BANDS)
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Fixed work vectors for forward MDCT and inverse IMDCT band transforms. */
|
|
32
|
+
export class MdctScratch {
|
|
33
|
+
/** Allocate input/output framing buffers, complex FFT rows, and reversal buffers. */
|
|
34
|
+
constructor() {
|
|
35
|
+
this.transformBuffers = {
|
|
36
|
+
64: new Float32Array(MDCT_SIZE_SHORT),
|
|
37
|
+
128: new Float32Array(128),
|
|
38
|
+
256: new Float32Array(MDCT_SIZE_LONG_LOW_MID),
|
|
39
|
+
512: new Float32Array(MDCT_SIZE_LONG_HIGH),
|
|
40
|
+
}
|
|
41
|
+
this.fftBuffers = {
|
|
42
|
+
16: {
|
|
43
|
+
real: new Float32Array(16),
|
|
44
|
+
imaginary: new Float32Array(16),
|
|
45
|
+
},
|
|
46
|
+
64: {
|
|
47
|
+
real: new Float32Array(64),
|
|
48
|
+
imaginary: new Float32Array(64),
|
|
49
|
+
},
|
|
50
|
+
128: {
|
|
51
|
+
real: new Float32Array(128),
|
|
52
|
+
imaginary: new Float32Array(128),
|
|
53
|
+
},
|
|
54
|
+
}
|
|
55
|
+
this.reversalBuffers = {
|
|
56
|
+
32: new Float32Array(32),
|
|
57
|
+
128: new Float32Array(128),
|
|
58
|
+
256: new Float32Array(256),
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
package/codec/syntax/frame.js
CHANGED
|
@@ -85,10 +85,9 @@ export function serializeFrame(frameData) {
|
|
|
85
85
|
}
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
-
//
|
|
89
|
-
buffer[SOUND_UNIT_SIZE -
|
|
90
|
-
buffer[SOUND_UNIT_SIZE -
|
|
91
|
-
buffer[SOUND_UNIT_SIZE - 1] = 0
|
|
88
|
+
// Mirror 16-bit Sound-Unit header into final two trailer bytes
|
|
89
|
+
buffer[SOUND_UNIT_SIZE - 2] = buffer[0]
|
|
90
|
+
buffer[SOUND_UNIT_SIZE - 1] = buffer[1]
|
|
92
91
|
|
|
93
92
|
return buffer
|
|
94
93
|
}
|
package/codec/synthesis/bands.js
CHANGED
|
@@ -53,7 +53,9 @@ export function synthesizeSpectrum(context) {
|
|
|
53
53
|
const bufferPool =
|
|
54
54
|
context?.bufferPool ??
|
|
55
55
|
throwError('synthesizeSpectrum: bufferPool is required')
|
|
56
|
-
const
|
|
56
|
+
const state = bufferPool.decoder.state
|
|
57
|
+
const scratch = bufferPool.decoder.scratch.mdct
|
|
58
|
+
const overlapBuffers = state.imdctOverlap
|
|
57
59
|
|
|
58
60
|
/**
|
|
59
61
|
* @param {Float32Array} coefficients
|
|
@@ -100,15 +102,15 @@ export function synthesizeSpectrum(context) {
|
|
|
100
102
|
) {
|
|
101
103
|
let blockSpectrum = coefficients
|
|
102
104
|
if (bandIndex > 0) {
|
|
103
|
-
blockSpectrum = reverseSpectrum(coefficients,
|
|
105
|
+
blockSpectrum = reverseSpectrum(coefficients, scratch.reversalBuffers)
|
|
104
106
|
}
|
|
105
107
|
|
|
106
108
|
const inverse = transformFunction.transform(
|
|
107
109
|
blockSpectrum,
|
|
108
|
-
|
|
110
|
+
scratch.fftBuffers
|
|
109
111
|
)
|
|
110
112
|
const inverseStart = inverse.length / 4
|
|
111
|
-
const inverseBuffer =
|
|
113
|
+
const inverseBuffer = scratch.transformBuffers[MAX_MDCT_SIZE]
|
|
112
114
|
inverseBuffer.fill(0)
|
|
113
115
|
|
|
114
116
|
for (let i = 0; i < config.size; i++) {
|
|
@@ -152,7 +154,7 @@ export function synthesizeSpectrum(context) {
|
|
|
152
154
|
*/
|
|
153
155
|
function inverseShortBlocks(coefficients, bandIndex, overlapBuffer, config) {
|
|
154
156
|
const numBlocks = 1 << (config.size === 256 ? 3 : 2)
|
|
155
|
-
const inverseBuffer =
|
|
157
|
+
const inverseBuffer = scratch.transformBuffers[MAX_MDCT_SIZE]
|
|
156
158
|
inverseBuffer.fill(0)
|
|
157
159
|
|
|
158
160
|
const previous = overlapBuffer.slice(
|
|
@@ -168,16 +170,10 @@ export function synthesizeSpectrum(context) {
|
|
|
168
170
|
position + MDCT_SHORT_BLOCK_SIZE
|
|
169
171
|
)
|
|
170
172
|
if (bandIndex > 0) {
|
|
171
|
-
blockSpectrum = reverseSpectrum(
|
|
172
|
-
blockSpectrum,
|
|
173
|
-
bufferPool.reversalBuffers
|
|
174
|
-
)
|
|
173
|
+
blockSpectrum = reverseSpectrum(blockSpectrum, scratch.reversalBuffers)
|
|
175
174
|
}
|
|
176
175
|
|
|
177
|
-
const inverse = shortImdct.transform(
|
|
178
|
-
blockSpectrum,
|
|
179
|
-
bufferPool.mdctBuffers
|
|
180
|
-
)
|
|
176
|
+
const inverse = shortImdct.transform(blockSpectrum, scratch.fftBuffers)
|
|
181
177
|
const inverseStart = inverse.length / 4
|
|
182
178
|
for (let i = 0; i < MDCT_SHORT_BLOCK_SIZE; i++) {
|
|
183
179
|
inverseBuffer[start + i] = inverse[inverseStart + i]
|
package/codec/synthesis/pcm.js
CHANGED
|
@@ -36,8 +36,9 @@ function synthesizePair(bands, state, scratch) {
|
|
|
36
36
|
export function synthesizePcm(context) {
|
|
37
37
|
const bufferPool =
|
|
38
38
|
context?.bufferPool ?? throwError('synthesizePcm: bufferPool is required')
|
|
39
|
-
const
|
|
40
|
-
const
|
|
39
|
+
const state = bufferPool.decoder.state
|
|
40
|
+
const states = state.qmfSynthesisStates
|
|
41
|
+
const scratch = bufferPool.decoder.scratch.qmf
|
|
41
42
|
|
|
42
43
|
/**
|
|
43
44
|
* @param {Array<Float32Array>} bands
|
|
@@ -45,14 +46,14 @@ export function synthesizePcm(context) {
|
|
|
45
46
|
*/
|
|
46
47
|
return (bands) => {
|
|
47
48
|
const delayedHigh = scratch.highBandWindows[bands[2].length]
|
|
48
|
-
delayedHigh.set(
|
|
49
|
-
delayedHigh.set(bands[2],
|
|
49
|
+
delayedHigh.set(state.qmfHighBandDelay)
|
|
50
|
+
delayedHigh.set(bands[2], state.qmfHighBandDelay.length)
|
|
50
51
|
|
|
51
52
|
const highBand = delayedHigh.slice(0, bands[0].length * 2)
|
|
52
|
-
|
|
53
|
+
state.qmfHighBandDelay.set(
|
|
53
54
|
delayedHigh.subarray(
|
|
54
55
|
bands[0].length * 2,
|
|
55
|
-
bands[0].length * 2 +
|
|
56
|
+
bands[0].length * 2 + state.qmfHighBandDelay.length
|
|
56
57
|
)
|
|
57
58
|
)
|
|
58
59
|
|