carta1 1.6.0 → 1.6.1

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.
@@ -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 - Optional work buffers for FFT
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?.[this.fftSize] ??
57
- throwError(`MDCT.transform: mdctBuffers[${this.fftSize}] is required`)
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[${this.fftSize}].imaginary is required`
73
+ `MDCT.transform: mdctBuffers[${fftSize}].imaginary is required`
67
74
  )
68
75
 
69
- const n4 = this.quarterSize
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 = this.sinCosTable[i]
85
- const s = this.sinCosTable[i + 1]
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 < this.halfSize; i += 2) {
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 = this.sinCosTable[i]
101
- const s = this.sinCosTable[i + 1]
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(this.halfSize)
111
- for (let i = 0; i < this.fftSize; i++) {
112
- const c = this.sinCosTable[i * 2]
113
- const s = this.sinCosTable[i * 2 + 1]
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 + this.halfSize - 1 - i * 2] = -re * s + im * c
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 - Optional work buffers for FFT
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?.[this.fftSize] ??
147
- throwError(`IMDCT.transform: mdctBuffers[${this.fftSize}] is required`)
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[${this.fftSize}].imaginary is required`
166
+ `IMDCT.transform: mdctBuffers[${fftSize}].imaginary is required`
157
167
  )
158
168
 
159
- const n4 = this.quarterSize
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 < this.fftSize; 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[this.halfSize - 1 - i2]
170
- const c = this.sinCosTable[i2]
171
- const s = this.sinCosTable[i2 + 1]
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(this.size)
190
+ const output = destination ?? new Float32Array(size)
181
191
 
182
- for (let i = 0; i < this.fftSize / 2; i++) {
192
+ for (let i = 0; i < fftSize / 2; i++) {
183
193
  const i2 = i * 2
184
- const c = this.sinCosTable[i2]
185
- const s = this.sinCosTable[i2 + 1]
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 = this.fftSize / 2; i < this.fftSize; i++) {
199
- const idx = (i - this.fftSize / 2) * 2 + n4
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 = this.sinCosTable[i2]
202
- const s = this.sinCosTable[i2 + 1]
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
 
@@ -25,14 +25,19 @@ import {
25
25
  QMF_SYNTHESIS_MODULATION_COEFFICIENTS,
26
26
  } from '../core/tables.js'
27
27
 
28
- const float32Round = Math.fround
28
+ /**
29
+ * @typedef {object} QmfScratch
30
+ * @property {Float32Array} polyphase - Filter components.
31
+ * @property {Float64Array} extended - Extended-precision components.
32
+ * @property {Float32Array} bands - Modulated subband samples.
33
+ */
29
34
 
30
35
  /**
31
36
  * Preserve the reference spill order of the native sixteen-band polyphase sum.
32
37
  * Other supported widths use the dense configurable path below.
33
38
  *
34
39
  * @param {Float32Array} input
35
- * @param {number} inputOffset
40
+ * @param {i32} inputOffset
36
41
  * @param {Float32Array} accumulator
37
42
  * @param {Float64Array} extended
38
43
  */
@@ -44,9 +49,9 @@ function filter16Band(input, inputOffset, accumulator, extended) {
44
49
  const firstSample = component < 8 ? component : component + 8
45
50
  const secondSample = component < 8 ? 15 - component : 39 - component
46
51
  const firstProduct = input[inputOffset + firstSample] * c[coefficientOffset]
47
- accumulator[component] = float32Round(
52
+ accumulator[component] = Math.fround(
48
53
  input[inputOffset + secondSample] * c[coefficientOffset + 1] +
49
- (component % 4 === 0 ? float32Round(firstProduct) : firstProduct)
54
+ (component % 4 === 0 ? Math.fround(firstProduct) : firstProduct)
50
55
  )
51
56
  }
52
57
 
@@ -63,47 +68,47 @@ function filter16Band(input, inputOffset, accumulator, extended) {
63
68
  const inputBase = inputOffset + segment * 32
64
69
  const term = segment * 2
65
70
 
66
- const product0 = float32Round(input[inputBase] * c[term])
67
- accumulator[0] = float32Round(
71
+ const product0 = Math.fround(input[inputBase] * c[term])
72
+ accumulator[0] = Math.fround(
68
73
  accumulator[0] + product0 + input[inputBase + 15] * c[term + 1]
69
74
  )
70
- accumulator[1] = float32Round(
75
+ accumulator[1] = Math.fround(
71
76
  input[inputBase + 1] * c[QMF_ANALYSIS_TERMS + term] +
72
- float32Round(
77
+ Math.fround(
73
78
  input[inputBase + 14] * c[QMF_ANALYSIS_TERMS + term + 1] +
74
79
  accumulator[1]
75
80
  )
76
81
  )
77
- const product2 = float32Round(
82
+ const product2 = Math.fround(
78
83
  input[inputBase + 2] * c[QMF_ANALYSIS_TERMS * 2 + term]
79
84
  )
80
- accumulator[2] = float32Round(
85
+ accumulator[2] = Math.fround(
81
86
  accumulator[2] +
82
87
  product2 +
83
88
  input[inputBase + 13] * c[QMF_ANALYSIS_TERMS * 2 + term + 1]
84
89
  )
85
- accumulator[3] = float32Round(
90
+ accumulator[3] = Math.fround(
86
91
  accumulator[3] +
87
92
  input[inputBase + 3] * c[QMF_ANALYSIS_TERMS * 3 + term] +
88
93
  input[inputBase + 12] * c[QMF_ANALYSIS_TERMS * 3 + term + 1]
89
94
  )
90
- const product4 = float32Round(
95
+ const product4 = Math.fround(
91
96
  input[inputBase + 4] * c[QMF_ANALYSIS_TERMS * 4 + term]
92
97
  )
93
98
  const product4Sum =
94
99
  product4 + input[inputBase + 11] * c[QMF_ANALYSIS_TERMS * 4 + term + 1]
95
- accumulator[4] = float32Round(accumulator[4] + product4Sum)
96
- accumulator[5] = float32Round(
100
+ accumulator[4] = Math.fround(accumulator[4] + product4Sum)
101
+ accumulator[5] = Math.fround(
97
102
  input[inputBase + 5] * c[QMF_ANALYSIS_TERMS * 5 + term] +
98
- float32Round(
103
+ Math.fround(
99
104
  input[inputBase + 10] * c[QMF_ANALYSIS_TERMS * 5 + term + 1] +
100
105
  accumulator[5]
101
106
  )
102
107
  )
103
- const product6 = float32Round(
108
+ const product6 = Math.fround(
104
109
  input[inputBase + 6] * c[QMF_ANALYSIS_TERMS * 6 + term]
105
110
  )
106
- accumulator[6] = float32Round(
111
+ accumulator[6] = Math.fround(
107
112
  accumulator[6] +
108
113
  product6 +
109
114
  input[inputBase + 9] * c[QMF_ANALYSIS_TERMS * 6 + term + 1]
@@ -112,16 +117,16 @@ function filter16Band(input, inputOffset, accumulator, extended) {
112
117
  accumulator[7] +
113
118
  (input[inputBase + 7] * c[QMF_ANALYSIS_TERMS * 7 + term] +
114
119
  input[inputBase + 8] * c[QMF_ANALYSIS_TERMS * 7 + term + 1])
115
- accumulator[7] = float32Round(extended7)
120
+ accumulator[7] = Math.fround(extended7)
116
121
 
117
122
  extended8 =
118
123
  input[inputBase + 31] * c[QMF_ANALYSIS_TERMS * 8 + term + 1] +
119
124
  input[inputBase + 16] * c[QMF_ANALYSIS_TERMS * 8 + term] +
120
125
  accumulator[8]
121
- accumulator[8] = float32Round(extended8)
122
- accumulator[9] = float32Round(
126
+ accumulator[8] = Math.fround(extended8)
127
+ accumulator[9] = Math.fround(
123
128
  input[inputBase + 17] * c[QMF_ANALYSIS_TERMS * 9 + term] +
124
- float32Round(
129
+ Math.fround(
125
130
  input[inputBase + 30] * c[QMF_ANALYSIS_TERMS * 9 + term + 1] +
126
131
  accumulator[9]
127
132
  )
@@ -130,32 +135,32 @@ function filter16Band(input, inputOffset, accumulator, extended) {
130
135
  accumulator[10] +
131
136
  (input[inputBase + 18] * c[QMF_ANALYSIS_TERMS * 10 + term] +
132
137
  input[inputBase + 29] * c[QMF_ANALYSIS_TERMS * 10 + term + 1])
133
- accumulator[10] = float32Round(extended10)
138
+ accumulator[10] = Math.fround(extended10)
134
139
  extended11 =
135
140
  accumulator[11] +
136
141
  (input[inputBase + 19] * c[QMF_ANALYSIS_TERMS * 11 + term] +
137
142
  input[inputBase + 28] * c[QMF_ANALYSIS_TERMS * 11 + term + 1])
138
- accumulator[11] = float32Round(extended11)
143
+ accumulator[11] = Math.fround(extended11)
139
144
  extended12 =
140
145
  accumulator[12] +
141
146
  (input[inputBase + 20] * c[QMF_ANALYSIS_TERMS * 12 + term] +
142
147
  input[inputBase + 27] * c[QMF_ANALYSIS_TERMS * 12 + term + 1])
143
- accumulator[12] = float32Round(extended12)
148
+ accumulator[12] = Math.fround(extended12)
144
149
  extended13 =
145
150
  accumulator[13] +
146
151
  (input[inputBase + 21] * c[QMF_ANALYSIS_TERMS * 13 + term] +
147
152
  input[inputBase + 26] * c[QMF_ANALYSIS_TERMS * 13 + term + 1])
148
- accumulator[13] = float32Round(extended13)
153
+ accumulator[13] = Math.fround(extended13)
149
154
  extended14 =
150
155
  accumulator[14] +
151
156
  (input[inputBase + 22] * c[QMF_ANALYSIS_TERMS * 14 + term] +
152
157
  input[inputBase + 25] * c[QMF_ANALYSIS_TERMS * 14 + term + 1])
153
- accumulator[14] = float32Round(extended14)
158
+ accumulator[14] = Math.fround(extended14)
154
159
  const product15 =
155
160
  input[inputBase + 24] * c[QMF_ANALYSIS_TERMS * 15 + term + 1] +
156
161
  input[inputBase + 23] * c[QMF_ANALYSIS_TERMS * 15 + term]
157
162
  extended15 = accumulator[15] + product15
158
- accumulator[15] = float32Round(extended15)
163
+ accumulator[15] = Math.fround(extended15)
159
164
  }
160
165
 
161
166
  extended[7] = extended7
@@ -189,66 +194,64 @@ function modulate16Band(polyphase, extended, output) {
189
194
  const rotated7Extended = extended[7] * cos64[0]
190
195
  const rotated0Extended = polyphase[0] * cos64[7]
191
196
  const rotated13Extended = extended[13] * cos64[13]
192
- const rotated7 = float32Round(rotated7Extended)
193
- const rotated5 = float32Round(polyphase[5] * cos64[2])
197
+ const rotated7 = Math.fround(rotated7Extended)
198
+ const rotated5 = Math.fround(polyphase[5] * cos64[2])
194
199
  const rotated6Extended = polyphase[6] * cos64[1]
195
- const rotated6 = float32Round(rotated6Extended)
200
+ const rotated6 = Math.fround(rotated6Extended)
196
201
  const rotated4Extended = polyphase[4] * cos64[3]
197
- const rotated4 = float32Round(rotated4Extended)
198
- const rotated3 = float32Round(polyphase[3] * cos64[4])
202
+ const rotated4 = Math.fround(rotated4Extended)
203
+ const rotated3 = Math.fround(polyphase[3] * cos64[4])
199
204
  const rotated3Extended = rotated3
200
- const rotated2 = float32Round(polyphase[2] * cos64[5])
201
- const rotated0 = float32Round(rotated0Extended)
205
+ const rotated2 = Math.fround(polyphase[2] * cos64[5])
206
+ const rotated0 = Math.fround(rotated0Extended)
202
207
  const rotated1Extended = polyphase[1] * cos64[6]
203
208
  const rotated8Extended = polyphase[8] * cos64[8]
204
- const rotated8 = float32Round(rotated8Extended)
209
+ const rotated8 = Math.fround(rotated8Extended)
205
210
  const rotated14Extended = extended[14] * cos64[14]
206
- const rotated14 = float32Round(rotated14Extended)
211
+ const rotated14 = Math.fround(rotated14Extended)
207
212
  const rotated15Extended = extended[15] * cos64[15]
208
213
  const rotated9Extended = polyphase[9] * cos64[9]
209
- const rotated9 = float32Round(rotated9Extended)
210
- const rotated13 = float32Round(rotated13Extended)
214
+ const rotated9 = Math.fround(rotated9Extended)
215
+ const rotated13 = Math.fround(rotated13Extended)
211
216
  const rotated10Extended = extended[10] * cos64[10]
212
- const rotated10 = float32Round(rotated10Extended)
217
+ const rotated10 = Math.fround(rotated10Extended)
213
218
  const rotated11Extended = extended[11] * cos64[11]
214
- const rotated11 = float32Round(rotated11Extended)
219
+ const rotated11 = Math.fround(rotated11Extended)
215
220
  const rotated12Extended = extended[12] * cos64[12]
216
221
 
217
222
  const sum0Extended = rotated0 + rotated8 + (rotated7 + rotated15Extended)
218
223
  const mix0Term0Extended = (rotated7 - rotated15Extended) * twoCosPi32
219
224
  const mix0Term1Extended = (rotated0 - rotated8) * cos32[7]
220
225
  const mix0Extended = mix0Term0Extended + mix0Term1Extended
221
- const mix0 = float32Round(mix0Extended)
226
+ const mix0 = Math.fround(mix0Extended)
222
227
  const mixASum0Extended = rotated7 + rotated15Extended
223
228
  const mixASum1Extended = rotated0 + rotated8
224
229
  const mixAExtended = twoCosPi16 * (mixASum0Extended - mixASum1Extended)
225
- const mix1 = float32Round(
226
- twoCosPi16 * (mix0Term0Extended - mix0Term1Extended)
227
- )
230
+ const mix1 = Math.fround(twoCosPi16 * (mix0Term0Extended - mix0Term1Extended))
228
231
 
229
232
  const sum1Extended = rotated14 + rotated1Extended + (rotated6 + rotated9)
230
- const sum1Float32 = float32Round(sum1Extended)
233
+ const sum1Float32 = Math.fround(sum1Extended)
231
234
  const mix2Term0Extended = (rotated1Extended - rotated9) * cos32[6]
232
235
  const mixBInnerExtended = rotated6 + rotated14 - rotated1Extended - rotated9
233
- const mixBInnerFloat32 = float32Round(mixBInnerExtended)
236
+ const mixBInnerFloat32 = Math.fround(mixBInnerExtended)
234
237
  const mixBExtended = cos16[1] * mixBInnerFloat32
235
- const mixB = float32Round(mixBExtended)
236
- const mix2Term1Extended = float32Round(rotated6 - rotated14) * cos32[1]
237
- const mix2 = float32Round(mix2Term1Extended + mix2Term0Extended)
238
- const mix3 = float32Round(cos16[1] * (mix2Term1Extended - mix2Term0Extended))
238
+ const mixB = Math.fround(mixBExtended)
239
+ const mix2Term1Extended = Math.fround(rotated6 - rotated14) * cos32[1]
240
+ const mix2 = Math.fround(mix2Term1Extended + mix2Term0Extended)
241
+ const mix3 = Math.fround(cos16[1] * (mix2Term1Extended - mix2Term0Extended))
239
242
 
240
243
  const sum2Pair0Extended = rotated5 + rotated13
241
244
  const sum2Pair1Extended = rotated2 + rotated10
242
245
  const sum2Extended = sum2Pair0Extended + sum2Pair1Extended
243
- const sum2Float32 = float32Round(sum2Extended)
246
+ const sum2Float32 = Math.fround(sum2Extended)
244
247
  const mixCInnerExtended = sum2Pair0Extended - rotated2 - rotated10
245
- const mixCInnerFloat32 = float32Round(mixCInnerExtended)
248
+ const mixCInnerFloat32 = Math.fround(mixCInnerExtended)
246
249
  const mixCExtended = cos16[2] * mixCInnerFloat32
247
- const mixC = float32Round(mixCExtended)
250
+ const mixC = Math.fround(mixCExtended)
248
251
  const mix4Term0Extended = (rotated5 - rotated13) * cos32[2]
249
252
  const mix4Term1Extended = (rotated2 - rotated10) * cos32[5]
250
- const mix4 = float32Round(mix4Term0Extended + mix4Term1Extended)
251
- const mix5 = float32Round(cos16[2] * (mix4Term0Extended - mix4Term1Extended))
253
+ const mix4 = Math.fround(mix4Term0Extended + mix4Term1Extended)
254
+ const mix5 = Math.fround(cos16[2] * (mix4Term0Extended - mix4Term1Extended))
252
255
 
253
256
  const sum3Extended =
254
257
  rotated3 + rotated4 + rotated11Extended + rotated12Extended
@@ -257,61 +260,61 @@ function modulate16Band(polyphase, extended, output) {
257
260
  rotated4 + rotated12Extended - rotated3Extended - rotated11Extended
258
261
  const mixDExtended = cos16[3] * mixDInnerExtended
259
262
  const mix6Term1Extended = (rotated3Extended - rotated11) * cos32[4]
260
- const mix6 = float32Round(mix6Term0Extended + mix6Term1Extended)
261
- const mix6Term0SpilledExtended = float32Round(mix6Term0Extended)
262
- const mix7 = float32Round(
263
+ const mix6 = Math.fround(mix6Term0Extended + mix6Term1Extended)
264
+ const mix6Term0SpilledExtended = Math.fround(mix6Term0Extended)
265
+ const mix7 = Math.fround(
263
266
  cos16[3] * (mix6Term0SpilledExtended - mix6Term1Extended)
264
267
  )
265
268
 
266
269
  const sumAllExtended = sum0Extended + sum1Float32 + sum2Float32 + sum3Extended
267
270
  const out0Extended = sumAllExtended * 0.5
268
- output[0] = float32Round(out0Extended)
271
+ output[0] = Math.fround(out0Extended)
269
272
  const mid0Extended =
270
273
  cosPi4 * (sum0Extended - sum1Float32 - sum2Float32 + sum3Extended)
271
- const mid0 = float32Round(mid0Extended)
274
+ const mid0 = Math.fround(mid0Extended)
272
275
  const diff03Extended = (sum0Extended - sum3Extended) * twoCosPi8
273
- const diff12Extended = float32Round(sum1Float32 - sum2Float32) * pi8[1]
274
- const diff12 = float32Round(diff12Extended)
276
+ const diff12Extended = Math.fround(sum1Float32 - sum2Float32) * pi8[1]
277
+ const diff12 = Math.fround(diff12Extended)
275
278
  const mid1Extended = half[1] * diff03Extended - half[2] * diff12
276
- const mid1 = float32Round(mid1Extended)
279
+ const mid1 = Math.fround(mid1Extended)
277
280
  const mid2Extended = (mixAExtended - mixDExtended) * pi8[2]
278
- const mid2 = float32Round(mid2Extended)
281
+ const mid2 = Math.fround(mid2Extended)
279
282
  const mid3Extended = (mixB - mixC) * pi8[3]
280
283
  const avg2Pair0Extended = mixB + mixC
281
284
  const avg2Pair1Extended = mixAExtended + mixDExtended
282
285
  const avg2Extended = (avg2Pair0Extended + avg2Pair1Extended) * 0.5
283
286
  const mid4Extended = (mid2 + mid3Extended) * 0.5 - avg2Extended
284
- const mid4 = float32Round(mid4Extended)
287
+ const mid4 = Math.fround(mid4Extended)
285
288
  const mid5Extended =
286
289
  cosPi4 * (mixAExtended - mixB - mixC + mixDExtended) - mid4Extended
287
290
  const mid6Extended = half[1] * mid2 - half[2] * mid3Extended - mid5Extended
288
- const mid6 = float32Round(mid6Extended)
291
+ const mid6 = Math.fround(mid6Extended)
289
292
 
290
293
  const avg1Extended = (mix0 + mix2 + mix4 + mix6) * 0.5
291
294
  const out1Extended = avg1Extended - output[0]
292
- output[1] = float32Round(out1Extended)
295
+ output[1] = Math.fround(out1Extended)
293
296
  const out2Extended = avg2Extended - out1Extended
294
- output[2] = float32Round(out2Extended)
297
+ output[2] = Math.fround(out2Extended)
295
298
  const mid7Extended = cosPi4 * (mix0 - mix2 - mix4 + mix6)
296
- const mid7 = float32Round(mid7Extended)
299
+ const mid7 = Math.fround(mid7Extended)
297
300
  const mid8Extended = twoCosPi8 * (mix0 - mix6)
298
- const mid8 = float32Round(mid8Extended)
299
- const mid9 = float32Round(pi8[1] * float32Round(mix2 - mix4))
301
+ const mid8 = Math.fround(mid8Extended)
302
+ const mid9 = Math.fround(pi8[1] * Math.fround(mix2 - mix4))
300
303
  const mid10Extended = half[1] * mid8 - half[2] * mid9
301
- const mid10 = float32Round(mid10Extended)
304
+ const mid10 = Math.fround(mid10Extended)
302
305
  const avg3Extended = (mix1 + mix3 + mix5 + mix7) * 0.5 - avg1Extended
303
306
  const out3Extended = avg3Extended - out2Extended
304
- output[3] = float32Round(out3Extended)
307
+ output[3] = Math.fround(out3Extended)
305
308
  const mid11Extended = (mid8 + mid9) * 0.5 - avg3Extended
306
- const mid11 = float32Round(mid11Extended)
309
+ const mid11 = Math.fround(mid11Extended)
307
310
  const out4Extended = (diff03Extended + diff12) * 0.5 - out3Extended
308
- output[4] = float32Round(out4Extended)
311
+ output[4] = Math.fround(out4Extended)
309
312
  const mid12Extended = (mix1 - mix7) * pi8[2]
310
313
  const mid13Extended = (mix3 - mix5) * pi8[3]
311
314
  const out5Extended = mid11Extended - out4Extended
312
- output[5] = float32Round(out5Extended)
315
+ output[5] = Math.fround(out5Extended)
313
316
  const out6Extended = mid4 - out5Extended
314
- output[6] = float32Round(out6Extended)
317
+ output[6] = Math.fround(out6Extended)
315
318
  const mid14Extended =
316
319
  mid12Extended + mid13Extended - mix1 - mix3 - mix5 - mix7
317
320
  const mid14HalfExtended = mid14Extended * 0.5
@@ -320,27 +323,27 @@ function modulate16Band(polyphase, extended, output) {
320
323
  half[1] * mid12Extended - half[2] * mid13Extended - mid15Extended
321
324
  const mid17Extended = mid14HalfExtended - mid11
322
325
  const out7Extended = mid17Extended - output[6]
323
- output[7] = float32Round(out7Extended)
326
+ output[7] = Math.fround(out7Extended)
324
327
  const mid18Extended = mid7 - mid17Extended
325
328
  const out8Extended = mid0 - out7Extended
326
- output[8] = float32Round(out8Extended)
329
+ output[8] = Math.fround(out8Extended)
327
330
  const out9Extended = mid18Extended - out8Extended
328
- output[9] = float32Round(out9Extended)
331
+ output[9] = Math.fround(out9Extended)
329
332
  const mid19Extended = mid15Extended - mid18Extended
330
333
  const out10Extended = mid5Extended - out9Extended
331
- output[10] = float32Round(out10Extended)
334
+ output[10] = Math.fround(out10Extended)
332
335
  const out11Extended = mid19Extended - out10Extended
333
- output[11] = float32Round(out11Extended)
336
+ output[11] = Math.fround(out11Extended)
334
337
  const mid20Extended = mid10 - mid19Extended
335
338
  const out12Extended = mid1 - out11Extended
336
- output[12] = float32Round(out12Extended)
339
+ output[12] = Math.fround(out12Extended)
337
340
  const out13Extended = mid20Extended - out12Extended
338
- output[13] = float32Round(out13Extended)
341
+ output[13] = Math.fround(out13Extended)
339
342
  const mid21Extended = mid16Extended - mid20Extended
340
343
  const out14Extended = mid6 - out13Extended
341
- output[14] = float32Round(out14Extended)
344
+ output[14] = Math.fround(out14Extended)
342
345
  const out15Extended = mid21Extended - out14Extended
343
- output[15] = float32Round(out15Extended)
346
+ output[15] = Math.fround(out15Extended)
344
347
  }
345
348
 
346
349
  /**
@@ -350,6 +353,7 @@ function modulate16Band(polyphase, extended, output) {
350
353
  * @param {Float32Array[]} outputBands Caller-owned chronological subband rows.
351
354
  * @param {QmfScratch} scratch Reusable component and modulation storage.
352
355
  * @returns {Float32Array[]} The completed subband rows.
356
+ * @nativeUnchecked Native callers supply frame-bounded buffers and indices.
353
357
  */
354
358
  export function analyzeQmf(input, outputBands, scratch) {
355
359
  if (
@@ -446,10 +450,11 @@ export function analyzeQmf(input, outputBands, scratch) {
446
450
  *
447
451
  * @param {Float32Array[]} inputBands Chronological subband rows.
448
452
  * @param {Float32Array} delay Caller-owned component delay ring.
449
- * @param {number} delayRow Current delay-ring row.
453
+ * @param {i32} delayRow Current delay-ring row.
450
454
  * @param {Float32Array} output Caller-owned interleaved PCM output.
451
455
  * @param {QmfScratch} scratch Reusable component and modulation storage.
452
456
  * @returns {number} Next delay-ring row.
457
+ * @nativeUnchecked Native callers supply frame-bounded buffers and indices.
453
458
  */
454
459
  export function synthesizeQmf(inputBands, delay, delayRow, output, scratch) {
455
460
  if (
@@ -507,7 +512,7 @@ export function synthesizeQmf(inputBands, delay, delayRow, output, scratch) {
507
512
  const secondRow =
508
513
  (delayRow + QMF_SYNTHESIS_DELAY_ROW_OFFSETS[second]) %
509
514
  QMF_SYNTHESIS_DELAY_ROWS
510
- sum = float32Round(
515
+ sum = Math.fround(
511
516
  delay[
512
517
  firstRow * QMF_BANDS + QMF_SYNTHESIS_DELAY_COMPONENTS[first]
513
518
  ] *
@@ -8,7 +8,7 @@
8
8
  /**
9
9
  * Reject field widths outside the shared bitstream primitive's safe range.
10
10
  *
11
- * @param {number} bitCount
11
+ * @param {i32} bitCount
12
12
  * @returns {void}
13
13
  */
14
14
  function assertBitCount(bitCount) {
@@ -22,9 +22,9 @@ function assertBitCount(bitCount) {
22
22
  /**
23
23
  * Pack bits into a buffer at a specific bit position
24
24
  * @param {Uint8Array} buffer - Destination buffer
25
- * @param {number} bitPosition - Bit position to start writing at
25
+ * @param {i32} bitPosition - Bit position to start writing at
26
26
  * @param {number} value - Value to pack
27
- * @param {number} bitCount - Number of bits to pack
27
+ * @param {i32} bitCount - Number of bits to pack
28
28
  * @returns {void}
29
29
  */
30
30
  export function packBits(buffer, bitPosition, value, bitCount) {
@@ -34,7 +34,7 @@ export function packBits(buffer, bitPosition, value, bitCount) {
34
34
  let byteIndex = Math.floor(bitPosition / 8)
35
35
  let bitOffset = bitPosition % 8
36
36
 
37
- const unsignedValue = Number(value) >>> 0
37
+ const packedValue = Number(value) | 0
38
38
 
39
39
  let bitsWritten = 0
40
40
  while (bitsWritten < bitCount && byteIndex < buffer.length) {
@@ -42,7 +42,7 @@ export function packBits(buffer, bitPosition, value, bitCount) {
42
42
  const bitsToWrite = Math.min(bitCount - bitsWritten, bitsAvailable)
43
43
 
44
44
  const shift = bitCount - bitsWritten - bitsToWrite
45
- const valueBits = (unsignedValue >>> shift) & (2 ** bitsToWrite - 1)
45
+ const valueBits = (packedValue >>> shift) & (2 ** bitsToWrite - 1)
46
46
 
47
47
  const mask = (2 ** bitsToWrite - 1) << (bitsAvailable - bitsToWrite)
48
48
  buffer[byteIndex] =
@@ -57,8 +57,8 @@ export function packBits(buffer, bitPosition, value, bitCount) {
57
57
  /**
58
58
  * Unpack bits from a buffer at a specific bit position
59
59
  * @param {Uint8Array} buffer - Source buffer
60
- * @param {number} bitPosition - Bit position to start reading from
61
- * @param {number} bitCount - Number of bits to unpack
60
+ * @param {i32} bitPosition - Bit position to start reading from
61
+ * @param {i32} bitCount - Number of bits to unpack
62
62
  * @returns {number} Unpacked unsigned value
63
63
  */
64
64
  export function unpackBits(buffer, bitPosition, bitCount) {
@@ -88,8 +88,8 @@ export function unpackBits(buffer, bitPosition, bitCount) {
88
88
  /**
89
89
  * Unpack signed bits from a buffer at a specific bit position
90
90
  * @param {Uint8Array} buffer - Source buffer
91
- * @param {number} bitPosition - Bit position to start reading from
92
- * @param {number} bitCount - Number of bits to unpack
91
+ * @param {i32} bitPosition - Bit position to start reading from
92
+ * @param {i32} bitCount - Number of bits to unpack
93
93
  * @returns {number} Unpacked signed value (two's complement)
94
94
  */
95
95
  export function unpackSignedBits(buffer, bitPosition, bitCount) {