libopus-wasm 0.0.1 → 0.1.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/CHANGELOG.md +28 -0
- package/README.md +202 -41
- package/dist/discordjs.d.ts +20 -0
- package/dist/discordjs.d.ts.map +1 -0
- package/dist/discordjs.js +108 -0
- package/dist/generated/libopus.generated.mjs +0 -0
- package/dist/index.d.ts +109 -15
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +476 -70
- package/package.json +14 -3
package/dist/index.js
CHANGED
|
@@ -4,132 +4,348 @@ export const Application = {
|
|
|
4
4
|
Audio: 2049,
|
|
5
5
|
RestrictedLowDelay: 2051,
|
|
6
6
|
};
|
|
7
|
+
export const Signal = {
|
|
8
|
+
Auto: -1000,
|
|
9
|
+
Voice: 3001,
|
|
10
|
+
Music: 3002,
|
|
11
|
+
};
|
|
12
|
+
export const Bitrate = {
|
|
13
|
+
Auto: -1000,
|
|
14
|
+
Max: -1,
|
|
15
|
+
};
|
|
16
|
+
export const Bandwidth = {
|
|
17
|
+
Narrowband: 1101,
|
|
18
|
+
Mediumband: 1102,
|
|
19
|
+
Wideband: 1103,
|
|
20
|
+
Superwideband: 1104,
|
|
21
|
+
Fullband: 1105,
|
|
22
|
+
};
|
|
23
|
+
export const EncoderCtl = {
|
|
24
|
+
SetApplication: 4000,
|
|
25
|
+
SetBitrate: 4002,
|
|
26
|
+
SetMaxBandwidth: 4004,
|
|
27
|
+
SetVbr: 4006,
|
|
28
|
+
SetBandwidth: 4008,
|
|
29
|
+
SetComplexity: 4010,
|
|
30
|
+
SetInBandFec: 4012,
|
|
31
|
+
SetPacketLossPercent: 4014,
|
|
32
|
+
SetDtx: 4016,
|
|
33
|
+
SetVbrConstraint: 4020,
|
|
34
|
+
SetForceChannels: 4022,
|
|
35
|
+
SetSignal: 4024,
|
|
36
|
+
SetLsbDepth: 4036,
|
|
37
|
+
SetExpertFrameDuration: 4040,
|
|
38
|
+
SetPredictionDisabled: 4042,
|
|
39
|
+
SetPhaseInversionDisabled: 4046,
|
|
40
|
+
};
|
|
41
|
+
export const DecoderCtl = {
|
|
42
|
+
SetGain: 4034,
|
|
43
|
+
SetPhaseInversionDisabled: 4046,
|
|
44
|
+
};
|
|
45
|
+
const DEFAULT_CHANNELS = 2;
|
|
46
|
+
const DEFAULT_FRAME_DURATION_MS = 20;
|
|
47
|
+
const MAX_PACKET_DURATION_MS = 120;
|
|
48
|
+
const DEFAULT_MAX_PACKET_BYTES = 4000;
|
|
49
|
+
const DEFAULT_SAMPLE_RATE = 48_000;
|
|
50
|
+
const DECODER_INTEGER_CTL_REQUESTS = new Set(Object.values(DecoderCtl));
|
|
51
|
+
const ENCODER_INTEGER_CTL_REQUESTS = new Set(Object.values(EncoderCtl));
|
|
52
|
+
const ENCODE_FRAME_DURATIONS_MS = [2.5, 5, 10, 20, 40, 60];
|
|
53
|
+
const VALID_SAMPLE_RATES = [8000, 12000, 16000, 24000, 48000];
|
|
7
54
|
let modulePromise;
|
|
8
55
|
export async function loadLibopus() {
|
|
9
56
|
const module = await getModule();
|
|
10
57
|
return { version: module.UTF8ToString(module._oc_get_version_string()) };
|
|
11
58
|
}
|
|
12
|
-
export async function createEncoder(options) {
|
|
59
|
+
export async function createEncoder(options = {}) {
|
|
13
60
|
const module = await getModule();
|
|
14
|
-
return new WasmOpusEncoder(module, options);
|
|
61
|
+
return new WasmOpusEncoder(module, normalizeEncoderOptions(options));
|
|
15
62
|
}
|
|
16
|
-
export async function createDecoder(options) {
|
|
63
|
+
export async function createDecoder(options = {}) {
|
|
17
64
|
const module = await getModule();
|
|
18
|
-
return new WasmOpusDecoder(module, options);
|
|
65
|
+
return new WasmOpusDecoder(module, normalizeDecoderOptions(options));
|
|
66
|
+
}
|
|
67
|
+
export async function getPacketInfo(packet, options = {}) {
|
|
68
|
+
const sampleRate = options.sampleRate ?? DEFAULT_SAMPLE_RATE;
|
|
69
|
+
validateCodecOptions({ channels: DEFAULT_CHANNELS, sampleRate });
|
|
70
|
+
if (packet.byteLength === 0) {
|
|
71
|
+
throw new RangeError("packet must not be empty");
|
|
72
|
+
}
|
|
73
|
+
const module = await getModule();
|
|
74
|
+
const packetPtr = checkedMalloc(module, packet.byteLength);
|
|
75
|
+
try {
|
|
76
|
+
module.HEAPU8.set(packet, packetPtr);
|
|
77
|
+
const decodedSamples = module._oc_packet_validate_decode(packetPtr, packet.byteLength, sampleRate);
|
|
78
|
+
if (decodedSamples < 0) {
|
|
79
|
+
throw createOpusError(module, decodedSamples, "getPacketInfo");
|
|
80
|
+
}
|
|
81
|
+
const frames = module._oc_packet_parse(packetPtr, packet.byteLength);
|
|
82
|
+
if (frames < 0) {
|
|
83
|
+
throw createOpusError(module, frames, "getPacketInfo");
|
|
84
|
+
}
|
|
85
|
+
const samples = module._oc_packet_get_nb_samples(packetPtr, packet.byteLength, sampleRate);
|
|
86
|
+
if (samples < 0) {
|
|
87
|
+
throw createOpusError(module, samples, "getPacketInfo");
|
|
88
|
+
}
|
|
89
|
+
const channels = module._oc_packet_get_nb_channels(packetPtr);
|
|
90
|
+
if (channels !== 1 && channels !== 2) {
|
|
91
|
+
throw new OpusError(channels, `libopus getPacketInfo failed (${channels}): invalid channel count`);
|
|
92
|
+
}
|
|
93
|
+
const bandwidth = module._oc_packet_get_bandwidth(packetPtr);
|
|
94
|
+
if (bandwidth < 0) {
|
|
95
|
+
throw createOpusError(module, bandwidth, "getPacketInfo");
|
|
96
|
+
}
|
|
97
|
+
validateBandwidth(bandwidth, "packet bandwidth");
|
|
98
|
+
return {
|
|
99
|
+
bandwidth: bandwidth,
|
|
100
|
+
channels,
|
|
101
|
+
durationMs: (samples / sampleRate) * 1000,
|
|
102
|
+
frames,
|
|
103
|
+
samples,
|
|
104
|
+
samplesPerFrame: module._oc_packet_get_samples_per_frame(packetPtr, sampleRate),
|
|
105
|
+
sampleRate,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
finally {
|
|
109
|
+
module._free(packetPtr);
|
|
110
|
+
}
|
|
19
111
|
}
|
|
20
112
|
class WasmOpusEncoder {
|
|
113
|
+
application;
|
|
21
114
|
channels;
|
|
115
|
+
frameSize;
|
|
22
116
|
sampleRate;
|
|
23
117
|
#freed = false;
|
|
24
118
|
#module;
|
|
119
|
+
#packetBytes = 0;
|
|
120
|
+
#packetPtr = 0;
|
|
121
|
+
#pcmBytes = 0;
|
|
122
|
+
#pcmPtr = 0;
|
|
25
123
|
#ptr;
|
|
26
124
|
constructor(module, options) {
|
|
27
|
-
validateCodecOptions(options);
|
|
28
125
|
this.#module = module;
|
|
126
|
+
this.application = options.application;
|
|
29
127
|
this.channels = options.channels;
|
|
128
|
+
this.frameSize = options.frameSize;
|
|
30
129
|
this.sampleRate = options.sampleRate;
|
|
31
130
|
const errorPtr = module._malloc(4);
|
|
32
131
|
try {
|
|
33
|
-
const ptr = module._oc_create_encoder(options.sampleRate, options.channels, options.application
|
|
34
|
-
const error = module.
|
|
132
|
+
const ptr = module._oc_create_encoder(options.sampleRate, options.channels, options.application, errorPtr);
|
|
133
|
+
const error = module.HEAP32[errorPtr >> 2] ?? 0;
|
|
35
134
|
if (!ptr || error !== 0) {
|
|
36
|
-
throw
|
|
135
|
+
throw createOpusError(module, error, "createEncoder");
|
|
37
136
|
}
|
|
38
137
|
this.#ptr = ptr;
|
|
39
138
|
}
|
|
40
139
|
finally {
|
|
41
140
|
module._free(errorPtr);
|
|
42
141
|
}
|
|
43
|
-
|
|
44
|
-
|
|
142
|
+
this.setBitrate(options.bitrate);
|
|
143
|
+
this.setComplexity(options.complexity);
|
|
144
|
+
this.setDtx(options.dtx);
|
|
145
|
+
this.setFec(options.fec);
|
|
146
|
+
if (options.maxBandwidth !== undefined) {
|
|
147
|
+
this.setMaxBandwidth(options.maxBandwidth);
|
|
45
148
|
}
|
|
46
|
-
|
|
47
|
-
|
|
149
|
+
this.setPacketLossPercent(options.packetLossPercent);
|
|
150
|
+
this.setSignal(options.signal);
|
|
151
|
+
if (options.vbr !== undefined) {
|
|
152
|
+
this.setVbr(options.vbr);
|
|
48
153
|
}
|
|
49
|
-
if (options.
|
|
50
|
-
this.
|
|
154
|
+
if (options.vbrConstraint !== undefined) {
|
|
155
|
+
this.setVbrConstraint(options.vbrConstraint);
|
|
51
156
|
}
|
|
52
157
|
}
|
|
53
|
-
|
|
158
|
+
encode(pcm, options = {}) {
|
|
54
159
|
this.#assertLive();
|
|
55
|
-
|
|
160
|
+
const frameSize = options.frameSize ?? this.frameSize;
|
|
161
|
+
validateEncodeFrameSize(frameSize, this.sampleRate, "frameSize");
|
|
56
162
|
const pcmBytes = toUint8Array(pcm);
|
|
57
163
|
const expectedBytes = frameSize * this.channels * 2;
|
|
58
164
|
if (pcmBytes.byteLength !== expectedBytes) {
|
|
59
165
|
throw new RangeError(`PCM frame has ${pcmBytes.byteLength} bytes; expected ${expectedBytes} for ${frameSize} samples and ${this.channels} channel(s)`);
|
|
60
166
|
}
|
|
61
|
-
const maxPacketBytes = options.maxPacketBytes ??
|
|
62
|
-
|
|
63
|
-
|
|
167
|
+
const maxPacketBytes = options.maxPacketBytes ?? DEFAULT_MAX_PACKET_BYTES;
|
|
168
|
+
validatePositiveInteger(maxPacketBytes, "maxPacketBytes");
|
|
169
|
+
const pcmPtr = this.#ensurePcmBytes(pcmBytes.byteLength);
|
|
170
|
+
const packetPtr = this.#ensurePacketBytes(maxPacketBytes);
|
|
171
|
+
this.#module.HEAPU8.set(pcmBytes, pcmPtr);
|
|
172
|
+
const encodedBytes = this.#module._oc_encode(this.#ptr, pcmPtr, frameSize, packetPtr, maxPacketBytes);
|
|
173
|
+
if (encodedBytes < 0) {
|
|
174
|
+
throw createOpusError(this.#module, encodedBytes, "encode");
|
|
64
175
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
176
|
+
return this.#module.HEAPU8.slice(packetPtr, packetPtr + encodedBytes);
|
|
177
|
+
}
|
|
178
|
+
encodeFloat(pcm, options = {}) {
|
|
179
|
+
this.#assertLive();
|
|
180
|
+
const frameSize = options.frameSize ?? this.frameSize;
|
|
181
|
+
validateEncodeFrameSize(frameSize, this.sampleRate, "frameSize");
|
|
182
|
+
const expectedSamples = frameSize * this.channels;
|
|
183
|
+
if (pcm.length !== expectedSamples) {
|
|
184
|
+
throw new RangeError(`Float32 PCM frame has ${pcm.length} samples; expected ${expectedSamples} for ${frameSize} samples and ${this.channels} channel(s)`);
|
|
74
185
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
186
|
+
const maxPacketBytes = options.maxPacketBytes ?? DEFAULT_MAX_PACKET_BYTES;
|
|
187
|
+
validatePositiveInteger(maxPacketBytes, "maxPacketBytes");
|
|
188
|
+
const pcmPtr = this.#ensurePcmBytes(pcm.byteLength);
|
|
189
|
+
const packetPtr = this.#ensurePacketBytes(maxPacketBytes);
|
|
190
|
+
this.#module.HEAPF32.set(pcm, pcmPtr >> 2);
|
|
191
|
+
const encodedBytes = this.#module._oc_encode_float(this.#ptr, pcmPtr, frameSize, packetPtr, maxPacketBytes);
|
|
192
|
+
if (encodedBytes < 0) {
|
|
193
|
+
throw createOpusError(this.#module, encodedBytes, "encodeFloat");
|
|
78
194
|
}
|
|
195
|
+
return this.#module.HEAPU8.slice(packetPtr, packetPtr + encodedBytes);
|
|
196
|
+
}
|
|
197
|
+
encodeFrames(frames, options = {}) {
|
|
198
|
+
return frames.map((frame) => this.encode(frame, options));
|
|
199
|
+
}
|
|
200
|
+
encodeFloatFrames(frames, options = {}) {
|
|
201
|
+
return frames.map((frame) => this.encodeFloat(frame, options));
|
|
202
|
+
}
|
|
203
|
+
encoderCtl(request, value) {
|
|
204
|
+
this.#assertLive();
|
|
205
|
+
validateInteger(request, "request");
|
|
206
|
+
validateInteger(value, "value");
|
|
207
|
+
if (!ENCODER_INTEGER_CTL_REQUESTS.has(request)) {
|
|
208
|
+
throw new RangeError("encoderCtl only supports integer setter requests");
|
|
209
|
+
}
|
|
210
|
+
this.#check(this.#module._oc_encoder_ctl(this.#ptr, request, value), "encoderCtl");
|
|
79
211
|
}
|
|
80
212
|
setBitrate(bitrate) {
|
|
213
|
+
this.encoderCtl(EncoderCtl.SetBitrate, normalizeBitrate(bitrate));
|
|
214
|
+
}
|
|
215
|
+
getBitrate() {
|
|
81
216
|
this.#assertLive();
|
|
82
|
-
|
|
83
|
-
|
|
217
|
+
const bitrate = this.#module._oc_encoder_ctl_get_bitrate(this.#ptr);
|
|
218
|
+
if (bitrate < 0) {
|
|
219
|
+
throw createOpusError(this.#module, bitrate, "getBitrate");
|
|
84
220
|
}
|
|
85
|
-
|
|
221
|
+
return bitrate;
|
|
86
222
|
}
|
|
87
|
-
|
|
223
|
+
getLookahead() {
|
|
88
224
|
this.#assertLive();
|
|
89
|
-
this.#
|
|
225
|
+
const lookahead = this.#module._oc_encoder_ctl_get_lookahead(this.#ptr);
|
|
226
|
+
if (lookahead < 0) {
|
|
227
|
+
throw createOpusError(this.#module, lookahead, "getLookahead");
|
|
228
|
+
}
|
|
229
|
+
return lookahead;
|
|
90
230
|
}
|
|
91
|
-
|
|
231
|
+
getInDtx() {
|
|
92
232
|
this.#assertLive();
|
|
93
|
-
|
|
94
|
-
|
|
233
|
+
const inDtx = this.#module._oc_encoder_ctl_get_in_dtx(this.#ptr);
|
|
234
|
+
if (inDtx < 0) {
|
|
235
|
+
throw createOpusError(this.#module, inDtx, "getInDtx");
|
|
95
236
|
}
|
|
96
|
-
|
|
237
|
+
return inDtx !== 0;
|
|
238
|
+
}
|
|
239
|
+
setComplexity(complexity) {
|
|
240
|
+
validateIntegerRange(complexity, 0, 10, "complexity");
|
|
241
|
+
this.encoderCtl(EncoderCtl.SetComplexity, complexity);
|
|
242
|
+
}
|
|
243
|
+
setDtx(enabled) {
|
|
244
|
+
this.encoderCtl(EncoderCtl.SetDtx, enabled ? 1 : 0);
|
|
245
|
+
}
|
|
246
|
+
setFec(enabled) {
|
|
247
|
+
this.encoderCtl(EncoderCtl.SetInBandFec, enabled ? 1 : 0);
|
|
248
|
+
}
|
|
249
|
+
setMaxBandwidth(bandwidth) {
|
|
250
|
+
validateBandwidth(bandwidth, "maxBandwidth");
|
|
251
|
+
this.encoderCtl(EncoderCtl.SetMaxBandwidth, bandwidth);
|
|
252
|
+
}
|
|
253
|
+
setPacketLossPercent(percentage) {
|
|
254
|
+
validateIntegerRange(percentage, 0, 100, "packetLossPercent");
|
|
255
|
+
this.encoderCtl(EncoderCtl.SetPacketLossPercent, percentage);
|
|
256
|
+
}
|
|
257
|
+
setSignal(signal) {
|
|
258
|
+
if (!Object.values(Signal).includes(signal)) {
|
|
259
|
+
throw new RangeError("signal must be Signal.Auto, Signal.Voice, or Signal.Music");
|
|
260
|
+
}
|
|
261
|
+
this.encoderCtl(EncoderCtl.SetSignal, signal);
|
|
262
|
+
}
|
|
263
|
+
setVbr(enabled) {
|
|
264
|
+
this.encoderCtl(EncoderCtl.SetVbr, enabled ? 1 : 0);
|
|
265
|
+
}
|
|
266
|
+
setVbrConstraint(enabled) {
|
|
267
|
+
this.encoderCtl(EncoderCtl.SetVbrConstraint, enabled ? 1 : 0);
|
|
97
268
|
}
|
|
98
269
|
free() {
|
|
99
270
|
if (this.#freed) {
|
|
100
271
|
return;
|
|
101
272
|
}
|
|
273
|
+
this.#freeScratch();
|
|
102
274
|
this.#module._oc_destroy_encoder(this.#ptr);
|
|
103
275
|
this.#freed = true;
|
|
104
276
|
}
|
|
277
|
+
[Symbol.dispose]() {
|
|
278
|
+
this.free();
|
|
279
|
+
}
|
|
105
280
|
#assertLive() {
|
|
106
281
|
if (this.#freed) {
|
|
107
282
|
throw new Error("OpusEncoder has been freed");
|
|
108
283
|
}
|
|
109
284
|
}
|
|
110
|
-
#
|
|
285
|
+
#check(code, operation) {
|
|
111
286
|
if (code < 0) {
|
|
112
|
-
throw
|
|
287
|
+
throw createOpusError(this.#module, code, operation);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
#ensurePacketBytes(requiredBytes) {
|
|
291
|
+
if (this.#packetPtr !== 0 && this.#packetBytes >= requiredBytes) {
|
|
292
|
+
return this.#packetPtr;
|
|
293
|
+
}
|
|
294
|
+
const nextPtr = checkedMalloc(this.#module, requiredBytes);
|
|
295
|
+
if (this.#packetPtr !== 0) {
|
|
296
|
+
this.#module._free(this.#packetPtr);
|
|
297
|
+
}
|
|
298
|
+
this.#packetPtr = nextPtr;
|
|
299
|
+
this.#packetBytes = requiredBytes;
|
|
300
|
+
return this.#packetPtr;
|
|
301
|
+
}
|
|
302
|
+
#ensurePcmBytes(requiredBytes) {
|
|
303
|
+
if (this.#pcmPtr !== 0 && this.#pcmBytes >= requiredBytes) {
|
|
304
|
+
return this.#pcmPtr;
|
|
305
|
+
}
|
|
306
|
+
const nextPtr = checkedMalloc(this.#module, requiredBytes);
|
|
307
|
+
if (this.#pcmPtr !== 0) {
|
|
308
|
+
this.#module._free(this.#pcmPtr);
|
|
113
309
|
}
|
|
310
|
+
this.#pcmPtr = nextPtr;
|
|
311
|
+
this.#pcmBytes = requiredBytes;
|
|
312
|
+
return this.#pcmPtr;
|
|
313
|
+
}
|
|
314
|
+
#freeScratch() {
|
|
315
|
+
if (this.#packetPtr !== 0) {
|
|
316
|
+
this.#module._free(this.#packetPtr);
|
|
317
|
+
}
|
|
318
|
+
if (this.#pcmPtr !== 0) {
|
|
319
|
+
this.#module._free(this.#pcmPtr);
|
|
320
|
+
}
|
|
321
|
+
this.#packetPtr = 0;
|
|
322
|
+
this.#packetBytes = 0;
|
|
323
|
+
this.#pcmPtr = 0;
|
|
324
|
+
this.#pcmBytes = 0;
|
|
114
325
|
}
|
|
115
326
|
}
|
|
116
327
|
class WasmOpusDecoder {
|
|
117
328
|
channels;
|
|
329
|
+
maxFrameSize;
|
|
118
330
|
sampleRate;
|
|
119
331
|
#freed = false;
|
|
120
332
|
#module;
|
|
333
|
+
#packetBytes = 0;
|
|
334
|
+
#packetPtr = 0;
|
|
335
|
+
#pcmBytes = 0;
|
|
336
|
+
#pcmPtr = 0;
|
|
121
337
|
#ptr;
|
|
122
338
|
constructor(module, options) {
|
|
123
|
-
validateCodecOptions(options);
|
|
124
339
|
this.#module = module;
|
|
125
340
|
this.channels = options.channels;
|
|
341
|
+
this.maxFrameSize = options.maxFrameSize;
|
|
126
342
|
this.sampleRate = options.sampleRate;
|
|
127
343
|
const errorPtr = module._malloc(4);
|
|
128
344
|
try {
|
|
129
345
|
const ptr = module._oc_create_decoder(options.sampleRate, options.channels, errorPtr);
|
|
130
|
-
const error = module.
|
|
346
|
+
const error = module.HEAP32[errorPtr >> 2] ?? 0;
|
|
131
347
|
if (!ptr || error !== 0) {
|
|
132
|
-
throw
|
|
348
|
+
throw createOpusError(module, error, "createDecoder");
|
|
133
349
|
}
|
|
134
350
|
this.#ptr = ptr;
|
|
135
351
|
}
|
|
@@ -137,73 +353,263 @@ class WasmOpusDecoder {
|
|
|
137
353
|
module._free(errorPtr);
|
|
138
354
|
}
|
|
139
355
|
}
|
|
140
|
-
|
|
356
|
+
decode(packet, options = {}) {
|
|
141
357
|
this.#assertLive();
|
|
142
|
-
|
|
143
|
-
if (packet.byteLength === 0) {
|
|
144
|
-
throw new RangeError("packet must not be empty");
|
|
145
|
-
}
|
|
146
|
-
const packetPtr = this.#module._malloc(packet.byteLength);
|
|
358
|
+
const frameSize = this.#resolveDecodeFrameSize(packet, options);
|
|
147
359
|
const pcmBytes = frameSize * this.channels * 2;
|
|
148
|
-
const pcmPtr = this.#
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
throw new OpusError(decodedSamples, formatOpusError(this.#module, decodedSamples));
|
|
154
|
-
}
|
|
155
|
-
const sampleCount = decodedSamples * this.channels;
|
|
156
|
-
return this.#module.HEAP16.slice(pcmPtr >> 1, (pcmPtr >> 1) + sampleCount);
|
|
360
|
+
const pcmPtr = this.#ensurePcmBytes(pcmBytes);
|
|
361
|
+
const { packetLength, packetPtr } = this.#copyPacket(packet, options.decodeFec);
|
|
362
|
+
const decodedSamples = this.#module._oc_decode(this.#ptr, packetPtr, packetLength, pcmPtr, frameSize, options.decodeFec ? 1 : 0);
|
|
363
|
+
if (decodedSamples < 0) {
|
|
364
|
+
throw createOpusError(this.#module, decodedSamples, packet === null ? "decodePacketLoss" : "decode");
|
|
157
365
|
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
366
|
+
const sampleCount = decodedSamples * this.channels;
|
|
367
|
+
return this.#module.HEAP16.slice(pcmPtr >> 1, (pcmPtr >> 1) + sampleCount);
|
|
368
|
+
}
|
|
369
|
+
decodeFloat(packet, options = {}) {
|
|
370
|
+
this.#assertLive();
|
|
371
|
+
const frameSize = this.#resolveDecodeFrameSize(packet, options);
|
|
372
|
+
const pcmBytes = frameSize * this.channels * 4;
|
|
373
|
+
const pcmPtr = this.#ensurePcmBytes(pcmBytes);
|
|
374
|
+
const { packetLength, packetPtr } = this.#copyPacket(packet, options.decodeFec);
|
|
375
|
+
const decodedSamples = this.#module._oc_decode_float(this.#ptr, packetPtr, packetLength, pcmPtr, frameSize, options.decodeFec ? 1 : 0);
|
|
376
|
+
if (decodedSamples < 0) {
|
|
377
|
+
throw createOpusError(this.#module, decodedSamples, packet === null ? "decodePacketLossFloat" : "decodeFloat");
|
|
378
|
+
}
|
|
379
|
+
const sampleCount = decodedSamples * this.channels;
|
|
380
|
+
return this.#module.HEAPF32.slice(pcmPtr >> 2, (pcmPtr >> 2) + sampleCount);
|
|
381
|
+
}
|
|
382
|
+
decodeFrames(packets, options = {}) {
|
|
383
|
+
return packets.map((packet) => this.decode(packet, options));
|
|
384
|
+
}
|
|
385
|
+
decodeFloatFrames(packets, options = {}) {
|
|
386
|
+
return packets.map((packet) => this.decodeFloat(packet, options));
|
|
387
|
+
}
|
|
388
|
+
decodePacketLoss(frameSize = samplesForDuration(this.sampleRate, DEFAULT_FRAME_DURATION_MS)) {
|
|
389
|
+
return this.decode(null, { frameSize });
|
|
390
|
+
}
|
|
391
|
+
decodePacketLossFloat(frameSize = samplesForDuration(this.sampleRate, DEFAULT_FRAME_DURATION_MS)) {
|
|
392
|
+
return this.decodeFloat(null, { frameSize });
|
|
393
|
+
}
|
|
394
|
+
decoderCtl(request, value) {
|
|
395
|
+
this.#assertLive();
|
|
396
|
+
validateInteger(request, "request");
|
|
397
|
+
validateInteger(value, "value");
|
|
398
|
+
if (!DECODER_INTEGER_CTL_REQUESTS.has(request)) {
|
|
399
|
+
throw new RangeError("decoderCtl only supports integer setter requests");
|
|
400
|
+
}
|
|
401
|
+
const code = this.#module._oc_decoder_ctl(this.#ptr, request, value);
|
|
402
|
+
if (code < 0) {
|
|
403
|
+
throw createOpusError(this.#module, code, "decoderCtl");
|
|
161
404
|
}
|
|
162
405
|
}
|
|
163
406
|
free() {
|
|
164
407
|
if (this.#freed) {
|
|
165
408
|
return;
|
|
166
409
|
}
|
|
410
|
+
this.#freeScratch();
|
|
167
411
|
this.#module._oc_destroy_decoder(this.#ptr);
|
|
168
412
|
this.#freed = true;
|
|
169
413
|
}
|
|
414
|
+
[Symbol.dispose]() {
|
|
415
|
+
this.free();
|
|
416
|
+
}
|
|
170
417
|
#assertLive() {
|
|
171
418
|
if (this.#freed) {
|
|
172
419
|
throw new Error("OpusDecoder has been freed");
|
|
173
420
|
}
|
|
174
421
|
}
|
|
422
|
+
#copyPacket(packet, decodeFec) {
|
|
423
|
+
if (packet === null) {
|
|
424
|
+
if (decodeFec) {
|
|
425
|
+
throw new RangeError("decodeFec requires a packet");
|
|
426
|
+
}
|
|
427
|
+
return { packetLength: 0, packetPtr: 0 };
|
|
428
|
+
}
|
|
429
|
+
if (packet.byteLength === 0) {
|
|
430
|
+
throw new RangeError("packet must not be empty; use null or decodePacketLoss() for PLC");
|
|
431
|
+
}
|
|
432
|
+
const packetPtr = this.#ensurePacketBytes(packet.byteLength);
|
|
433
|
+
this.#module.HEAPU8.set(packet, packetPtr);
|
|
434
|
+
return { packetLength: packet.byteLength, packetPtr };
|
|
435
|
+
}
|
|
436
|
+
#ensurePacketBytes(requiredBytes) {
|
|
437
|
+
if (this.#packetPtr !== 0 && this.#packetBytes >= requiredBytes) {
|
|
438
|
+
return this.#packetPtr;
|
|
439
|
+
}
|
|
440
|
+
const nextPtr = checkedMalloc(this.#module, requiredBytes);
|
|
441
|
+
if (this.#packetPtr !== 0) {
|
|
442
|
+
this.#module._free(this.#packetPtr);
|
|
443
|
+
}
|
|
444
|
+
this.#packetPtr = nextPtr;
|
|
445
|
+
this.#packetBytes = requiredBytes;
|
|
446
|
+
return this.#packetPtr;
|
|
447
|
+
}
|
|
448
|
+
#ensurePcmBytes(requiredBytes) {
|
|
449
|
+
if (this.#pcmPtr !== 0 && this.#pcmBytes >= requiredBytes) {
|
|
450
|
+
return this.#pcmPtr;
|
|
451
|
+
}
|
|
452
|
+
const nextPtr = checkedMalloc(this.#module, requiredBytes);
|
|
453
|
+
if (this.#pcmPtr !== 0) {
|
|
454
|
+
this.#module._free(this.#pcmPtr);
|
|
455
|
+
}
|
|
456
|
+
this.#pcmPtr = nextPtr;
|
|
457
|
+
this.#pcmBytes = requiredBytes;
|
|
458
|
+
return this.#pcmPtr;
|
|
459
|
+
}
|
|
460
|
+
#freeScratch() {
|
|
461
|
+
if (this.#packetPtr !== 0) {
|
|
462
|
+
this.#module._free(this.#packetPtr);
|
|
463
|
+
}
|
|
464
|
+
if (this.#pcmPtr !== 0) {
|
|
465
|
+
this.#module._free(this.#pcmPtr);
|
|
466
|
+
}
|
|
467
|
+
this.#packetPtr = 0;
|
|
468
|
+
this.#packetBytes = 0;
|
|
469
|
+
this.#pcmPtr = 0;
|
|
470
|
+
this.#pcmBytes = 0;
|
|
471
|
+
}
|
|
472
|
+
#resolveDecodeFrameSize(packet, options) {
|
|
473
|
+
const frameSize = packet === null || options.decodeFec
|
|
474
|
+
? (options.frameSize ?? options.maxFrameSize ?? samplesForDuration(this.sampleRate, DEFAULT_FRAME_DURATION_MS))
|
|
475
|
+
: (options.maxFrameSize ?? this.maxFrameSize);
|
|
476
|
+
if (packet === null || options.decodeFec) {
|
|
477
|
+
validatePlcFrameSize(frameSize, this.sampleRate, "frameSize");
|
|
478
|
+
return frameSize;
|
|
479
|
+
}
|
|
480
|
+
validateDecodeCapacity(frameSize, this.sampleRate, "maxFrameSize");
|
|
481
|
+
return frameSize;
|
|
482
|
+
}
|
|
175
483
|
}
|
|
176
484
|
export class OpusError extends Error {
|
|
177
485
|
code;
|
|
178
|
-
|
|
486
|
+
operation;
|
|
487
|
+
constructor(code, message, operation) {
|
|
179
488
|
super(message);
|
|
180
489
|
this.name = "OpusError";
|
|
181
490
|
this.code = code;
|
|
491
|
+
this.operation = operation;
|
|
182
492
|
}
|
|
183
493
|
}
|
|
184
494
|
async function getModule() {
|
|
185
495
|
modulePromise ??= createLibopusModule();
|
|
186
496
|
return await modulePromise;
|
|
187
497
|
}
|
|
188
|
-
function
|
|
498
|
+
function createOpusError(module, code, operation) {
|
|
189
499
|
const message = module.UTF8ToString(module._oc_strerror(code));
|
|
190
|
-
return `libopus
|
|
500
|
+
return new OpusError(code, `libopus ${operation} failed (${code}): ${message}`, operation);
|
|
191
501
|
}
|
|
192
502
|
function toUint8Array(input) {
|
|
193
503
|
return input instanceof Uint8Array
|
|
194
504
|
? input
|
|
195
505
|
: new Uint8Array(input.buffer, input.byteOffset, input.byteLength);
|
|
196
506
|
}
|
|
507
|
+
function normalizeEncoderOptions(options) {
|
|
508
|
+
const sampleRate = options.sampleRate ?? DEFAULT_SAMPLE_RATE;
|
|
509
|
+
const channels = options.channels ?? DEFAULT_CHANNELS;
|
|
510
|
+
validateCodecOptions({ channels, sampleRate });
|
|
511
|
+
const frameSize = options.frameSize ?? samplesForDuration(sampleRate, DEFAULT_FRAME_DURATION_MS);
|
|
512
|
+
validateEncodeFrameSize(frameSize, sampleRate, "frameSize");
|
|
513
|
+
if (options.maxBandwidth !== undefined) {
|
|
514
|
+
validateBandwidth(options.maxBandwidth, "maxBandwidth");
|
|
515
|
+
}
|
|
516
|
+
return {
|
|
517
|
+
application: options.application ?? Application.Audio,
|
|
518
|
+
bitrate: normalizeBitrate(options.bitrate ?? 64_000),
|
|
519
|
+
channels,
|
|
520
|
+
complexity: options.complexity ?? 10,
|
|
521
|
+
dtx: options.dtx ?? false,
|
|
522
|
+
fec: options.fec ?? false,
|
|
523
|
+
frameSize,
|
|
524
|
+
maxBandwidth: options.maxBandwidth,
|
|
525
|
+
packetLossPercent: options.packetLossPercent ?? 0,
|
|
526
|
+
sampleRate,
|
|
527
|
+
signal: options.signal ?? Signal.Auto,
|
|
528
|
+
vbr: options.vbr,
|
|
529
|
+
vbrConstraint: options.vbrConstraint,
|
|
530
|
+
};
|
|
531
|
+
}
|
|
532
|
+
function normalizeDecoderOptions(options) {
|
|
533
|
+
const sampleRate = options.sampleRate ?? DEFAULT_SAMPLE_RATE;
|
|
534
|
+
const channels = options.channels ?? DEFAULT_CHANNELS;
|
|
535
|
+
validateCodecOptions({ channels, sampleRate });
|
|
536
|
+
const maxFrameSize = options.maxFrameSize ?? samplesForDuration(sampleRate, MAX_PACKET_DURATION_MS);
|
|
537
|
+
validateDecodeCapacity(maxFrameSize, sampleRate, "maxFrameSize");
|
|
538
|
+
return { channels, maxFrameSize, sampleRate };
|
|
539
|
+
}
|
|
540
|
+
function samplesForDuration(sampleRate, durationMs) {
|
|
541
|
+
return (sampleRate / 1000) * durationMs;
|
|
542
|
+
}
|
|
197
543
|
function validateCodecOptions(options) {
|
|
198
|
-
if (!
|
|
544
|
+
if (!VALID_SAMPLE_RATES.includes(options.sampleRate)) {
|
|
199
545
|
throw new RangeError("sampleRate must be 8000, 12000, 16000, 24000, or 48000");
|
|
200
546
|
}
|
|
201
547
|
if (options.channels !== 1 && options.channels !== 2) {
|
|
202
548
|
throw new RangeError("channels must be 1 or 2");
|
|
203
549
|
}
|
|
204
550
|
}
|
|
205
|
-
function
|
|
206
|
-
if (
|
|
207
|
-
|
|
551
|
+
function normalizeBitrate(bitrate) {
|
|
552
|
+
if (bitrate === "auto") {
|
|
553
|
+
return Bitrate.Auto;
|
|
554
|
+
}
|
|
555
|
+
if (bitrate === "max") {
|
|
556
|
+
return Bitrate.Max;
|
|
557
|
+
}
|
|
558
|
+
if (bitrate === Bitrate.Auto || bitrate === Bitrate.Max) {
|
|
559
|
+
return bitrate;
|
|
560
|
+
}
|
|
561
|
+
validatePositiveInteger(bitrate, "bitrate");
|
|
562
|
+
return bitrate;
|
|
563
|
+
}
|
|
564
|
+
function validateBandwidth(bandwidth, name) {
|
|
565
|
+
if (!Object.values(Bandwidth).includes(bandwidth)) {
|
|
566
|
+
throw new RangeError(`${name} must be Bandwidth.Narrowband, Bandwidth.Mediumband, Bandwidth.Wideband, Bandwidth.Superwideband, or Bandwidth.Fullband`);
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
function validateEncodeFrameSize(frameSize, sampleRate, name) {
|
|
570
|
+
validateFrameSizeForDurations(frameSize, sampleRate, name, ENCODE_FRAME_DURATIONS_MS);
|
|
571
|
+
}
|
|
572
|
+
function validateDecodeCapacity(frameSize, sampleRate, name) {
|
|
573
|
+
const maxFrameSize = samplesForDuration(sampleRate, MAX_PACKET_DURATION_MS);
|
|
574
|
+
if (!Number.isInteger(frameSize) || frameSize <= 0 || frameSize > maxFrameSize) {
|
|
575
|
+
throw new RangeError(`${name} must be an integer from 1 to ${maxFrameSize} samples at ${sampleRate} Hz`);
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
function validatePlcFrameSize(frameSize, sampleRate, name) {
|
|
579
|
+
const minFrameSize = samplesForDuration(sampleRate, 2.5);
|
|
580
|
+
const maxFrameSize = samplesForDuration(sampleRate, MAX_PACKET_DURATION_MS);
|
|
581
|
+
if (!Number.isInteger(frameSize) ||
|
|
582
|
+
frameSize < minFrameSize ||
|
|
583
|
+
frameSize > maxFrameSize ||
|
|
584
|
+
frameSize % minFrameSize !== 0) {
|
|
585
|
+
throw new RangeError(`${name} must be a multiple of ${minFrameSize} samples from ${minFrameSize} to ${maxFrameSize} at ${sampleRate} Hz`);
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
function validateFrameSizeForDurations(frameSize, sampleRate, name, durationsMs) {
|
|
589
|
+
const validFrameSizes = durationsMs.map((durationMs) => samplesForDuration(sampleRate, durationMs));
|
|
590
|
+
if (!Number.isInteger(frameSize) || !validFrameSizes.includes(frameSize)) {
|
|
591
|
+
throw new RangeError(`${name} must be one of ${validFrameSizes.join(", ")} samples at ${sampleRate} Hz`);
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
function validateInteger(value, name) {
|
|
595
|
+
if (!Number.isInteger(value)) {
|
|
596
|
+
throw new RangeError(`${name} must be an integer`);
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
function validatePositiveInteger(value, name) {
|
|
600
|
+
if (!Number.isInteger(value) || value <= 0) {
|
|
601
|
+
throw new RangeError(`${name} must be a positive integer`);
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
function validateIntegerRange(value, min, max, name) {
|
|
605
|
+
if (!Number.isInteger(value) || value < min || value > max) {
|
|
606
|
+
throw new RangeError(`${name} must be an integer from ${min} to ${max}`);
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
function checkedMalloc(module, bytes) {
|
|
610
|
+
const ptr = module._malloc(bytes);
|
|
611
|
+
if (ptr === 0) {
|
|
612
|
+
throw new Error(`WASM malloc failed for ${bytes} bytes`);
|
|
208
613
|
}
|
|
614
|
+
return ptr;
|
|
209
615
|
}
|