libopus-wasm 0.0.1 → 0.2.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 +34 -0
- package/README.md +205 -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 +121 -15
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +508 -70
- package/package.json +16 -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 = {}) {
|
|
60
|
+
const module = await getModule();
|
|
61
|
+
return new WasmOpusEncoder(module, normalizeEncoderOptions(options));
|
|
62
|
+
}
|
|
63
|
+
export async function createDecoder(options = {}) {
|
|
13
64
|
const module = await getModule();
|
|
14
|
-
return new
|
|
65
|
+
return new WasmOpusDecoder(module, normalizeDecoderOptions(options));
|
|
15
66
|
}
|
|
16
|
-
export async function
|
|
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
|
+
}
|
|
17
73
|
const module = await getModule();
|
|
18
|
-
|
|
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");
|
|
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");
|
|
78
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() {
|
|
224
|
+
this.#assertLive();
|
|
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;
|
|
230
|
+
}
|
|
231
|
+
getInDtx() {
|
|
88
232
|
this.#assertLive();
|
|
89
|
-
this.#
|
|
233
|
+
const inDtx = this.#module._oc_encoder_ctl_get_in_dtx(this.#ptr);
|
|
234
|
+
if (inDtx < 0) {
|
|
235
|
+
throw createOpusError(this.#module, inDtx, "getInDtx");
|
|
236
|
+
}
|
|
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);
|
|
90
252
|
}
|
|
91
253
|
setPacketLossPercent(percentage) {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
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");
|
|
95
260
|
}
|
|
96
|
-
this
|
|
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);
|
|
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);
|
|
113
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,295 @@ 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
|
+
codeName;
|
|
487
|
+
operation;
|
|
488
|
+
constructor(code, message, operation) {
|
|
179
489
|
super(message);
|
|
180
490
|
this.name = "OpusError";
|
|
181
491
|
this.code = code;
|
|
492
|
+
this.codeName = resolveOpusErrorCodeName(code);
|
|
493
|
+
this.operation = operation;
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
export const OpusErrorCode = {
|
|
497
|
+
BadArg: -1,
|
|
498
|
+
BufferTooSmall: -2,
|
|
499
|
+
InternalError: -3,
|
|
500
|
+
InvalidPacket: -4,
|
|
501
|
+
Unimplemented: -5,
|
|
502
|
+
InvalidState: -6,
|
|
503
|
+
AllocFail: -7,
|
|
504
|
+
};
|
|
505
|
+
export function isOpusError(error) {
|
|
506
|
+
if (error instanceof OpusError) {
|
|
507
|
+
return true;
|
|
182
508
|
}
|
|
509
|
+
const candidate = error;
|
|
510
|
+
return (Boolean(error) &&
|
|
511
|
+
typeof error === "object" &&
|
|
512
|
+
candidate.name === "OpusError" &&
|
|
513
|
+
typeof candidate.message === "string" &&
|
|
514
|
+
typeof candidate.code === "number" &&
|
|
515
|
+
(typeof candidate.codeName === "string" || candidate.codeName === undefined) &&
|
|
516
|
+
(typeof candidate.operation === "string" || candidate.operation === undefined));
|
|
183
517
|
}
|
|
184
518
|
async function getModule() {
|
|
185
519
|
modulePromise ??= createLibopusModule();
|
|
186
520
|
return await modulePromise;
|
|
187
521
|
}
|
|
188
|
-
function
|
|
522
|
+
function resolveOpusErrorCodeName(code) {
|
|
523
|
+
for (const [name, value] of Object.entries(OpusErrorCode)) {
|
|
524
|
+
if (value === code) {
|
|
525
|
+
return name;
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
return undefined;
|
|
529
|
+
}
|
|
530
|
+
function createOpusError(module, code, operation) {
|
|
189
531
|
const message = module.UTF8ToString(module._oc_strerror(code));
|
|
190
|
-
return `libopus
|
|
532
|
+
return new OpusError(code, `libopus ${operation} failed (${code}): ${message}`, operation);
|
|
191
533
|
}
|
|
192
534
|
function toUint8Array(input) {
|
|
193
535
|
return input instanceof Uint8Array
|
|
194
536
|
? input
|
|
195
537
|
: new Uint8Array(input.buffer, input.byteOffset, input.byteLength);
|
|
196
538
|
}
|
|
539
|
+
function normalizeEncoderOptions(options) {
|
|
540
|
+
const sampleRate = options.sampleRate ?? DEFAULT_SAMPLE_RATE;
|
|
541
|
+
const channels = options.channels ?? DEFAULT_CHANNELS;
|
|
542
|
+
validateCodecOptions({ channels, sampleRate });
|
|
543
|
+
const frameSize = options.frameSize ?? samplesForDuration(sampleRate, DEFAULT_FRAME_DURATION_MS);
|
|
544
|
+
validateEncodeFrameSize(frameSize, sampleRate, "frameSize");
|
|
545
|
+
if (options.maxBandwidth !== undefined) {
|
|
546
|
+
validateBandwidth(options.maxBandwidth, "maxBandwidth");
|
|
547
|
+
}
|
|
548
|
+
return {
|
|
549
|
+
application: options.application ?? Application.Audio,
|
|
550
|
+
bitrate: normalizeBitrate(options.bitrate ?? 64_000),
|
|
551
|
+
channels,
|
|
552
|
+
complexity: options.complexity ?? 10,
|
|
553
|
+
dtx: options.dtx ?? false,
|
|
554
|
+
fec: options.fec ?? false,
|
|
555
|
+
frameSize,
|
|
556
|
+
maxBandwidth: options.maxBandwidth,
|
|
557
|
+
packetLossPercent: options.packetLossPercent ?? 0,
|
|
558
|
+
sampleRate,
|
|
559
|
+
signal: options.signal ?? Signal.Auto,
|
|
560
|
+
vbr: options.vbr,
|
|
561
|
+
vbrConstraint: options.vbrConstraint,
|
|
562
|
+
};
|
|
563
|
+
}
|
|
564
|
+
function normalizeDecoderOptions(options) {
|
|
565
|
+
const sampleRate = options.sampleRate ?? DEFAULT_SAMPLE_RATE;
|
|
566
|
+
const channels = options.channels ?? DEFAULT_CHANNELS;
|
|
567
|
+
validateCodecOptions({ channels, sampleRate });
|
|
568
|
+
const maxFrameSize = options.maxFrameSize ?? samplesForDuration(sampleRate, MAX_PACKET_DURATION_MS);
|
|
569
|
+
validateDecodeCapacity(maxFrameSize, sampleRate, "maxFrameSize");
|
|
570
|
+
return { channels, maxFrameSize, sampleRate };
|
|
571
|
+
}
|
|
572
|
+
function samplesForDuration(sampleRate, durationMs) {
|
|
573
|
+
return (sampleRate / 1000) * durationMs;
|
|
574
|
+
}
|
|
197
575
|
function validateCodecOptions(options) {
|
|
198
|
-
if (!
|
|
576
|
+
if (!VALID_SAMPLE_RATES.includes(options.sampleRate)) {
|
|
199
577
|
throw new RangeError("sampleRate must be 8000, 12000, 16000, 24000, or 48000");
|
|
200
578
|
}
|
|
201
579
|
if (options.channels !== 1 && options.channels !== 2) {
|
|
202
580
|
throw new RangeError("channels must be 1 or 2");
|
|
203
581
|
}
|
|
204
582
|
}
|
|
205
|
-
function
|
|
206
|
-
if (
|
|
207
|
-
|
|
583
|
+
function normalizeBitrate(bitrate) {
|
|
584
|
+
if (bitrate === "auto") {
|
|
585
|
+
return Bitrate.Auto;
|
|
586
|
+
}
|
|
587
|
+
if (bitrate === "max") {
|
|
588
|
+
return Bitrate.Max;
|
|
589
|
+
}
|
|
590
|
+
if (bitrate === Bitrate.Auto || bitrate === Bitrate.Max) {
|
|
591
|
+
return bitrate;
|
|
592
|
+
}
|
|
593
|
+
validatePositiveInteger(bitrate, "bitrate");
|
|
594
|
+
return bitrate;
|
|
595
|
+
}
|
|
596
|
+
function validateBandwidth(bandwidth, name) {
|
|
597
|
+
if (!Object.values(Bandwidth).includes(bandwidth)) {
|
|
598
|
+
throw new RangeError(`${name} must be Bandwidth.Narrowband, Bandwidth.Mediumband, Bandwidth.Wideband, Bandwidth.Superwideband, or Bandwidth.Fullband`);
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
function validateEncodeFrameSize(frameSize, sampleRate, name) {
|
|
602
|
+
validateFrameSizeForDurations(frameSize, sampleRate, name, ENCODE_FRAME_DURATIONS_MS);
|
|
603
|
+
}
|
|
604
|
+
function validateDecodeCapacity(frameSize, sampleRate, name) {
|
|
605
|
+
const maxFrameSize = samplesForDuration(sampleRate, MAX_PACKET_DURATION_MS);
|
|
606
|
+
if (!Number.isInteger(frameSize) || frameSize <= 0 || frameSize > maxFrameSize) {
|
|
607
|
+
throw new RangeError(`${name} must be an integer from 1 to ${maxFrameSize} samples at ${sampleRate} Hz`);
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
function validatePlcFrameSize(frameSize, sampleRate, name) {
|
|
611
|
+
const minFrameSize = samplesForDuration(sampleRate, 2.5);
|
|
612
|
+
const maxFrameSize = samplesForDuration(sampleRate, MAX_PACKET_DURATION_MS);
|
|
613
|
+
if (!Number.isInteger(frameSize) ||
|
|
614
|
+
frameSize < minFrameSize ||
|
|
615
|
+
frameSize > maxFrameSize ||
|
|
616
|
+
frameSize % minFrameSize !== 0) {
|
|
617
|
+
throw new RangeError(`${name} must be a multiple of ${minFrameSize} samples from ${minFrameSize} to ${maxFrameSize} at ${sampleRate} Hz`);
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
function validateFrameSizeForDurations(frameSize, sampleRate, name, durationsMs) {
|
|
621
|
+
const validFrameSizes = durationsMs.map((durationMs) => samplesForDuration(sampleRate, durationMs));
|
|
622
|
+
if (!Number.isInteger(frameSize) || !validFrameSizes.includes(frameSize)) {
|
|
623
|
+
throw new RangeError(`${name} must be one of ${validFrameSizes.join(", ")} samples at ${sampleRate} Hz`);
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
function validateInteger(value, name) {
|
|
627
|
+
if (!Number.isInteger(value)) {
|
|
628
|
+
throw new RangeError(`${name} must be an integer`);
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
function validatePositiveInteger(value, name) {
|
|
632
|
+
if (!Number.isInteger(value) || value <= 0) {
|
|
633
|
+
throw new RangeError(`${name} must be a positive integer`);
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
function validateIntegerRange(value, min, max, name) {
|
|
637
|
+
if (!Number.isInteger(value) || value < min || value > max) {
|
|
638
|
+
throw new RangeError(`${name} must be an integer from ${min} to ${max}`);
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
function checkedMalloc(module, bytes) {
|
|
642
|
+
const ptr = module._malloc(bytes);
|
|
643
|
+
if (ptr === 0) {
|
|
644
|
+
throw new Error(`WASM malloc failed for ${bytes} bytes`);
|
|
208
645
|
}
|
|
646
|
+
return ptr;
|
|
209
647
|
}
|