carta1 1.6.0 → 1.7.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 +4 -61
- package/bin/cli.js +508 -239
- package/codec/allocation/solve.js +46 -28
- package/codec/analysis/blocks.js +4 -1
- package/codec/analysis/spectrum.js +17 -15
- package/codec/boundary/container.js +1 -1
- package/codec/boundary/pcm.js +2 -88
- package/codec/boundary/processor.js +4 -4
- package/codec/boundary/reader.js +2 -2
- package/codec/boundary/stream.js +56 -50
- package/codec/core/constants.js +6 -5
- package/codec/core/geometry.js +2 -2
- package/codec/core/tables.js +21 -98
- package/codec/quantization/spectrum.js +6 -6
- package/codec/signal/fft.js +1 -0
- package/codec/signal/mdct.js +48 -38
- package/codec/signal/qmf.js +37 -465
- package/codec/syntax/bitstream.js +9 -9
- package/codec/syntax/frame.js +11 -2
- package/codec/synthesis/bands.js +13 -14
- package/codec/synthesis/pcm.js +4 -0
- 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 +4 -16
- package/bin/options.js +0 -41
- package/codec/boundary/inspect.js +0 -96
- package/dist/carta1.wme +0 -0
- package/dist/wme/index.js +0 -2
- package/dist/wme/index.js.map +0 -1
- package/docs/native.md +0 -107
- package/docs/porffor.md +0 -23
package/codec/core/tables.js
CHANGED
|
@@ -4,10 +4,8 @@
|
|
|
4
4
|
|
|
5
5
|
import {
|
|
6
6
|
MAX_WORD_LENGTH_INDEX,
|
|
7
|
-
|
|
8
|
-
|
|
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
|
-
|
|
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
|
-
{
|
|
173
|
-
|
|
174
|
-
|
|
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([
|
|
@@ -16,14 +16,14 @@ import { SCALE_FACTORS } from '../core/tables.js'
|
|
|
16
16
|
/**
|
|
17
17
|
* Quantize coefficients.
|
|
18
18
|
* @param {Float32Array} coefficients
|
|
19
|
-
* @param {
|
|
20
|
-
* @param {
|
|
19
|
+
* @param {i32} scaleFactorIndex
|
|
20
|
+
* @param {i32} bitsPerSample
|
|
21
21
|
* @returns {Int32Array} out
|
|
22
22
|
*/
|
|
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
|
|
26
|
+
if (bitsPerSample === 0) {
|
|
27
27
|
out.fill(0, 0, length)
|
|
28
28
|
return out
|
|
29
29
|
}
|
|
@@ -47,12 +47,12 @@ export function quantize(coefficients, scaleFactorIndex, bitsPerSample) {
|
|
|
47
47
|
/**
|
|
48
48
|
* Dequantize coefficients.
|
|
49
49
|
* @param {Int32Array} quantized
|
|
50
|
-
* @param {
|
|
51
|
-
* @param {
|
|
50
|
+
* @param {i32} scaleFactorIndex
|
|
51
|
+
* @param {i32} bitsPerSample
|
|
52
52
|
* @returns {Float32Array}
|
|
53
53
|
*/
|
|
54
54
|
export function dequantize(quantized, scaleFactorIndex, bitsPerSample) {
|
|
55
|
-
if (bitsPerSample === 0
|
|
55
|
+
if (bitsPerSample === 0) {
|
|
56
56
|
return new Float32Array(quantized.length)
|
|
57
57
|
}
|
|
58
58
|
|
package/codec/signal/fft.js
CHANGED
|
@@ -6,6 +6,7 @@ export class FFT {
|
|
|
6
6
|
*
|
|
7
7
|
* @param {Float32Array} real Real input and output components.
|
|
8
8
|
* @param {Float32Array} imaginary Imaginary input and output components.
|
|
9
|
+
* @nativeUnchecked Native callers supply frame-bounded buffers and indices.
|
|
9
10
|
*/
|
|
10
11
|
static transform(real, imaginary) {
|
|
11
12
|
const size = real.length
|
package/codec/signal/mdct.js
CHANGED
|
@@ -13,6 +13,10 @@ import { throwError } from '../utils.js'
|
|
|
13
13
|
* Base class for MDCT and IMDCT transforms
|
|
14
14
|
*/
|
|
15
15
|
class MdctBase {
|
|
16
|
+
/**
|
|
17
|
+
* @param {number} size - Transform size in samples
|
|
18
|
+
* @param {number} scale - Transform normalization factor
|
|
19
|
+
*/
|
|
16
20
|
constructor(size, scale) {
|
|
17
21
|
this.size = size
|
|
18
22
|
this.halfSize = size >> 1
|
|
@@ -43,8 +47,12 @@ export class MDCT extends MdctBase {
|
|
|
43
47
|
/**
|
|
44
48
|
* Perform forward MDCT transform
|
|
45
49
|
* @param {Float32Array} input - Time-domain input samples
|
|
46
|
-
* @param {Object} mdctBuffers -
|
|
50
|
+
* @param {Object} mdctBuffers - Required work buffers keyed by FFT size
|
|
51
|
+
* @param {Float32Array|null} [destination=null] - Optional output buffer
|
|
52
|
+
* @param {i32} [destinationOffset=0] - First output index in destination
|
|
47
53
|
* @returns {Float32Array} Frequency-domain MDCT coefficients
|
|
54
|
+
* @throws {Error} If the required FFT work buffers are missing
|
|
55
|
+
* @nativeUnchecked Native callers supply frame-bounded buffers and indices.
|
|
48
56
|
*/
|
|
49
57
|
transform(
|
|
50
58
|
input,
|
|
@@ -52,21 +60,20 @@ export class MDCT extends MdctBase {
|
|
|
52
60
|
destination = null,
|
|
53
61
|
destinationOffset = 0
|
|
54
62
|
) {
|
|
63
|
+
const { halfSize, quarterSize, fftSize, sinCosTable } = this
|
|
55
64
|
const buffers =
|
|
56
|
-
mdctBuffers?.[
|
|
57
|
-
throwError(`MDCT.transform: mdctBuffers[${
|
|
65
|
+
mdctBuffers?.[fftSize] ??
|
|
66
|
+
throwError(`MDCT.transform: mdctBuffers[${fftSize}] is required`)
|
|
58
67
|
const real =
|
|
59
68
|
buffers?.real ??
|
|
60
|
-
throwError(
|
|
61
|
-
`MDCT.transform: mdctBuffers[${this.fftSize}].real is required`
|
|
62
|
-
)
|
|
69
|
+
throwError(`MDCT.transform: mdctBuffers[${fftSize}].real is required`)
|
|
63
70
|
const imag =
|
|
64
71
|
buffers?.imaginary ??
|
|
65
72
|
throwError(
|
|
66
|
-
`MDCT.transform: mdctBuffers[${
|
|
73
|
+
`MDCT.transform: mdctBuffers[${fftSize}].imaginary is required`
|
|
67
74
|
)
|
|
68
75
|
|
|
69
|
-
const n4 =
|
|
76
|
+
const n4 = quarterSize
|
|
70
77
|
const n34 = 3 * n4
|
|
71
78
|
|
|
72
79
|
real.fill(0)
|
|
@@ -81,14 +88,14 @@ export class MDCT extends MdctBase {
|
|
|
81
88
|
|
|
82
89
|
const r = input[i1] + input[i2]
|
|
83
90
|
const im = input[i3] - input[i4]
|
|
84
|
-
const c =
|
|
85
|
-
const s =
|
|
91
|
+
const c = sinCosTable[i]
|
|
92
|
+
const s = sinCosTable[i + 1]
|
|
86
93
|
|
|
87
94
|
real[i >> 1] = r * c + im * s
|
|
88
95
|
imag[i >> 1] = im * c - r * s
|
|
89
96
|
}
|
|
90
97
|
|
|
91
|
-
for (let i = n4; i <
|
|
98
|
+
for (let i = n4; i < halfSize; i += 2) {
|
|
92
99
|
const idx = i >> 1
|
|
93
100
|
const i1 = n34 - 1 - i
|
|
94
101
|
const i2 = i - n4
|
|
@@ -97,8 +104,8 @@ export class MDCT extends MdctBase {
|
|
|
97
104
|
|
|
98
105
|
const r = input[i1] - input[i2]
|
|
99
106
|
const im = input[i3] + input[i4]
|
|
100
|
-
const c =
|
|
101
|
-
const s =
|
|
107
|
+
const c = sinCosTable[i]
|
|
108
|
+
const s = sinCosTable[i + 1]
|
|
102
109
|
|
|
103
110
|
real[idx] = r * c + im * s
|
|
104
111
|
imag[idx] = im * c - r * s
|
|
@@ -107,15 +114,15 @@ export class MDCT extends MdctBase {
|
|
|
107
114
|
FFT.transform(real, imag)
|
|
108
115
|
|
|
109
116
|
// Post-FFT processing
|
|
110
|
-
const output = destination ?? new Float32Array(
|
|
111
|
-
for (let i = 0; i <
|
|
112
|
-
const c =
|
|
113
|
-
const s =
|
|
117
|
+
const output = destination ?? new Float32Array(halfSize)
|
|
118
|
+
for (let i = 0; i < fftSize; i++) {
|
|
119
|
+
const c = sinCosTable[i * 2]
|
|
120
|
+
const s = sinCosTable[i * 2 + 1]
|
|
114
121
|
const re = real[i]
|
|
115
122
|
const im = imag[i]
|
|
116
123
|
|
|
117
124
|
output[destinationOffset + i * 2] = -re * c - im * s
|
|
118
|
-
output[destinationOffset +
|
|
125
|
+
output[destinationOffset + halfSize - 1 - i * 2] = -re * s + im * c
|
|
119
126
|
}
|
|
120
127
|
|
|
121
128
|
return output
|
|
@@ -133,8 +140,12 @@ export class IMDCT extends MdctBase {
|
|
|
133
140
|
/**
|
|
134
141
|
* Perform inverse MDCT transform
|
|
135
142
|
* @param {Float32Array} input - Frequency-domain MDCT coefficients
|
|
136
|
-
* @param {Object} mdctBuffers -
|
|
143
|
+
* @param {Object} mdctBuffers - Required work buffers keyed by FFT size
|
|
144
|
+
* @param {Float32Array|null} [destination=null] - Optional output buffer
|
|
145
|
+
* @param {i32} [destinationOffset=0] - First output index in destination
|
|
137
146
|
* @returns {Float32Array} Time-domain output samples
|
|
147
|
+
* @throws {Error} If the required FFT work buffers are missing
|
|
148
|
+
* @nativeUnchecked Native callers supply frame-bounded buffers and indices.
|
|
138
149
|
*/
|
|
139
150
|
transform(
|
|
140
151
|
input,
|
|
@@ -142,33 +153,32 @@ export class IMDCT extends MdctBase {
|
|
|
142
153
|
destination = null,
|
|
143
154
|
destinationOffset = 0
|
|
144
155
|
) {
|
|
156
|
+
const { size, halfSize, quarterSize, fftSize, sinCosTable } = this
|
|
145
157
|
const buffers =
|
|
146
|
-
mdctBuffers?.[
|
|
147
|
-
throwError(`IMDCT.transform: mdctBuffers[${
|
|
158
|
+
mdctBuffers?.[fftSize] ??
|
|
159
|
+
throwError(`IMDCT.transform: mdctBuffers[${fftSize}] is required`)
|
|
148
160
|
const real =
|
|
149
161
|
buffers?.real ??
|
|
150
|
-
throwError(
|
|
151
|
-
`IMDCT.transform: mdctBuffers[${this.fftSize}].real is required`
|
|
152
|
-
)
|
|
162
|
+
throwError(`IMDCT.transform: mdctBuffers[${fftSize}].real is required`)
|
|
153
163
|
const imag =
|
|
154
164
|
buffers?.imaginary ??
|
|
155
165
|
throwError(
|
|
156
|
-
`IMDCT.transform: mdctBuffers[${
|
|
166
|
+
`IMDCT.transform: mdctBuffers[${fftSize}].imaginary is required`
|
|
157
167
|
)
|
|
158
168
|
|
|
159
|
-
const n4 =
|
|
169
|
+
const n4 = quarterSize
|
|
160
170
|
const n34 = 3 * n4
|
|
161
171
|
|
|
162
172
|
real.fill(0)
|
|
163
173
|
imag.fill(0)
|
|
164
174
|
|
|
165
175
|
// Pre-FFT processing
|
|
166
|
-
for (let i = 0; i <
|
|
176
|
+
for (let i = 0; i < fftSize; i++) {
|
|
167
177
|
const i2 = i * 2
|
|
168
178
|
const r = -input[i2]
|
|
169
|
-
const im = -input[
|
|
170
|
-
const c =
|
|
171
|
-
const s =
|
|
179
|
+
const im = -input[halfSize - 1 - i2]
|
|
180
|
+
const c = sinCosTable[i2]
|
|
181
|
+
const s = sinCosTable[i2 + 1]
|
|
172
182
|
|
|
173
183
|
real[i] = im * s + r * c
|
|
174
184
|
imag[i] = im * c - r * s
|
|
@@ -177,12 +187,12 @@ export class IMDCT extends MdctBase {
|
|
|
177
187
|
FFT.transform(real, imag)
|
|
178
188
|
|
|
179
189
|
// Post-FFT butterfly
|
|
180
|
-
const output = destination ?? new Float32Array(
|
|
190
|
+
const output = destination ?? new Float32Array(size)
|
|
181
191
|
|
|
182
|
-
for (let i = 0; i <
|
|
192
|
+
for (let i = 0; i < fftSize / 2; i++) {
|
|
183
193
|
const i2 = i * 2
|
|
184
|
-
const c =
|
|
185
|
-
const s =
|
|
194
|
+
const c = sinCosTable[i2]
|
|
195
|
+
const s = sinCosTable[i2 + 1]
|
|
186
196
|
const re = real[i]
|
|
187
197
|
const im = imag[i]
|
|
188
198
|
|
|
@@ -195,11 +205,11 @@ export class IMDCT extends MdctBase {
|
|
|
195
205
|
output[destinationOffset + n4 - 1 - i2] = -i1
|
|
196
206
|
}
|
|
197
207
|
|
|
198
|
-
for (let i =
|
|
199
|
-
const idx = (i -
|
|
208
|
+
for (let i = fftSize / 2; i < fftSize; i++) {
|
|
209
|
+
const idx = (i - fftSize / 2) * 2 + n4
|
|
200
210
|
const i2 = i * 2
|
|
201
|
-
const c =
|
|
202
|
-
const s =
|
|
211
|
+
const c = sinCosTable[i2]
|
|
212
|
+
const s = sinCosTable[i2 + 1]
|
|
203
213
|
const re = real[i]
|
|
204
214
|
const im = imag[i]
|
|
205
215
|
|