libopus-wasm 0.4.0 → 0.5.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.
- package/CHANGELOG.md +27 -0
- package/README.md +1 -1
- package/dist/discordjs.d.ts.map +1 -1
- package/dist/discordjs.js +29 -27
- package/dist/generated/libopus.generated.mjs +0 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +151 -224
- package/package.json +24 -15
- package/scripts/release-smoke.mjs +38 -0
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,33 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
## 0.5.1 - 2026-12-05
|
|
6
|
+
|
|
7
|
+
**Highlights:** Keep encoder application state accurate and reject empty batches on freed codecs, with clearer browser capture diagnostics.
|
|
8
|
+
|
|
9
|
+
- Keep `encoder.application` in sync with successful raw application changes.
|
|
10
|
+
- Empty PCM and packet batches now reject calls on a freed codec handle, matching single-frame operations.
|
|
11
|
+
- Report silent capture in the browser tone example when the decoded packets contain no audible signal.
|
|
12
|
+
- Correct the numeric Opus bandwidth constants in the encoder tuning guide.
|
|
13
|
+
- Retry GitHub Pages deployments using the site artifact from the successful build. A full rebuild uploads a new artifact for that attempt.
|
|
14
|
+
- Pin the reusable npm release workflow so retries find existing drafts and validate every release/asset listing page before writing.
|
|
15
|
+
|
|
16
|
+
## 0.5.0 - 2026-11-03
|
|
17
|
+
|
|
18
|
+
- Add encoder/decoder `reset()` for independent streams without reallocating codec handles; preserve configured settings.
|
|
19
|
+
- Support module Workers explicitly in the WASM build, with a Chromium-tested AudioWorklet capture/Worker example and a Prism demux-to-PCM example.
|
|
20
|
+
- Add zero-dependency and browser bundle size guards, plus an allocation/latency profiling command. Clarify asynchronous Discord compatibility.
|
|
21
|
+
- Resolve canonical URLs relative to each generated page when building nested documentation without a CNAME.
|
|
22
|
+
- Bound encoder scratch capacity to libopus's effective packet budget so large `maxPacketBytes` ceilings cannot exhaust the WebAssembly stack on long frames.
|
|
23
|
+
|
|
24
|
+
## 0.4.1 - 2026-09-24
|
|
25
|
+
|
|
26
|
+
**Highlights:** Safer numeric settings and codec initialization, with accurate guidance for decoder output formats.
|
|
27
|
+
|
|
28
|
+
- Reject CTL values and numeric bitrates outside signed 32-bit bounds and invalid application modes before WebAssembly can coerce them into different settings; correct the Q8 decoder gain examples. (#19)
|
|
29
|
+
- Free partially initialized Discord adapters when either codec fails, and reject failed WASM error-buffer allocations before creating native codecs. (#20)
|
|
30
|
+
- Correct decoder documentation to describe selectable output sample rates and mono/stereo conversion, and clarify that source builds recompile cached libopus sources. (#23)
|
|
31
|
+
|
|
5
32
|
## 0.4.0 - 2026-09-13
|
|
6
33
|
|
|
7
34
|
- Reserve enough WebAssembly stack for 40 ms and 60 ms stereo frames so encoding cannot corrupt later codec operations. (#18)
|
package/README.md
CHANGED
|
@@ -16,7 +16,7 @@ packets, no Ogg/WebM container layer.
|
|
|
16
16
|
- **Int16 and Float32 PCM** — use whatever your pipeline already speaks.
|
|
17
17
|
- **Loss-resilient** — in-band FEC and packet-loss concealment.
|
|
18
18
|
- **Tunable** — bitrate, VBR/CBR, complexity, signal, bandwidth, DTX, plus a curated CTL passthrough.
|
|
19
|
-
- **
|
|
19
|
+
- **Async `@discordjs/opus`-style adapter** — familiar methods, no node-gyp.
|
|
20
20
|
|
|
21
21
|
📖 **Full documentation: [libopus-wasm.dev](https://libopus-wasm.dev)**
|
|
22
22
|
|
package/dist/discordjs.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"discordjs.d.ts","sourceRoot":"","sources":["../src/discordjs.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAIrC,qBAAa,WAAW;;IACtB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;IAM9B,YAAY,IAAI,SAAS,EAAE,QAAQ,SAAI,
|
|
1
|
+
{"version":3,"file":"discordjs.d.ts","sourceRoot":"","sources":["../src/discordjs.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAIrC,qBAAa,WAAW;;IACtB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;IAM9B,YAAY,IAAI,SAAS,EAAE,QAAQ,SAAI,EAqCtC;IAED,OAAa,MAAM,CAAC,IAAI,SAAS,EAAE,QAAQ,SAAI,GAAG,OAAO,CAAC,WAAW,CAAC,CAIrE;IAED,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,GAAG,MAAM,CAKvC;IAED,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,GAAG,MAAM,CAIvC;IAED,eAAe,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAEhD;IAED,eAAe,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,CAEhD;IAED,UAAU,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAEhC;IAED,UAAU,IAAI,MAAM,CAEnB;IAED,MAAM,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI,CAE7B;IAED,MAAM,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI,CAE/B;IAED,IAAI,IAAI,IAAI,CAOX;IAED,CAAC,MAAM,CAAC,OAAO,CAAC,IAAI,IAAI,CAEvB;CAWF"}
|
package/dist/discordjs.js
CHANGED
|
@@ -11,7 +11,7 @@ export class OpusEncoder {
|
|
|
11
11
|
constructor(rate = 48_000, channels = 2) {
|
|
12
12
|
this.rate = rate;
|
|
13
13
|
this.channels = channels;
|
|
14
|
-
this.ready = Promise.
|
|
14
|
+
this.ready = Promise.allSettled([
|
|
15
15
|
createEncoder({
|
|
16
16
|
channels: channels,
|
|
17
17
|
sampleRate: rate,
|
|
@@ -20,7 +20,19 @@ export class OpusEncoder {
|
|
|
20
20
|
channels: channels,
|
|
21
21
|
sampleRate: rate,
|
|
22
22
|
}),
|
|
23
|
-
]).then(([
|
|
23
|
+
]).then(([encoderResult, decoderResult]) => {
|
|
24
|
+
if (encoderResult.status === "rejected") {
|
|
25
|
+
if (decoderResult.status === "fulfilled") {
|
|
26
|
+
decoderResult.value.free();
|
|
27
|
+
}
|
|
28
|
+
throw encoderResult.reason;
|
|
29
|
+
}
|
|
30
|
+
const encoder = encoderResult.value;
|
|
31
|
+
if (decoderResult.status === "rejected") {
|
|
32
|
+
encoder.free();
|
|
33
|
+
throw decoderResult.reason;
|
|
34
|
+
}
|
|
35
|
+
const decoder = decoderResult.value;
|
|
24
36
|
if (this.#freed) {
|
|
25
37
|
encoder.free();
|
|
26
38
|
decoder.free();
|
|
@@ -40,32 +52,33 @@ export class OpusEncoder {
|
|
|
40
52
|
return encoder;
|
|
41
53
|
}
|
|
42
54
|
encode(buf) {
|
|
43
|
-
const encoder = this.#
|
|
55
|
+
const encoder = this.#requireCodec(this.#encoder);
|
|
44
56
|
const frameSize = inferFrameSize(buf.byteLength, this.channels);
|
|
45
|
-
|
|
57
|
+
const packet = encoder.encode(buf, { frameSize });
|
|
58
|
+
return Buffer.from(packet.buffer, packet.byteOffset, packet.byteLength);
|
|
46
59
|
}
|
|
47
60
|
decode(buf) {
|
|
48
|
-
const decoder = this.#
|
|
61
|
+
const decoder = this.#requireCodec(this.#decoder);
|
|
49
62
|
const pcm = decoder.decode(buf);
|
|
50
63
|
return Buffer.from(pcm.buffer, pcm.byteOffset, pcm.byteLength);
|
|
51
64
|
}
|
|
52
65
|
applyEncoderCTL(ctl, value) {
|
|
53
|
-
this.#
|
|
66
|
+
this.#requireCodec(this.#encoder).encoderCtl(ctl, value);
|
|
54
67
|
}
|
|
55
68
|
applyDecoderCTL(ctl, value) {
|
|
56
|
-
this.#
|
|
69
|
+
this.#requireCodec(this.#decoder).decoderCtl(ctl, value);
|
|
57
70
|
}
|
|
58
71
|
setBitrate(bitrate) {
|
|
59
|
-
this.#
|
|
72
|
+
this.#requireCodec(this.#encoder).setBitrate(bitrate);
|
|
60
73
|
}
|
|
61
74
|
getBitrate() {
|
|
62
|
-
return this.#
|
|
75
|
+
return this.#requireCodec(this.#encoder).getBitrate();
|
|
63
76
|
}
|
|
64
77
|
setFEC(enabled) {
|
|
65
|
-
this.#
|
|
78
|
+
this.#requireCodec(this.#encoder).setFec(enabled);
|
|
66
79
|
}
|
|
67
80
|
setPLP(percentage) {
|
|
68
|
-
this.#
|
|
81
|
+
this.#requireCodec(this.#encoder).setPacketLossPercent(percentage);
|
|
69
82
|
}
|
|
70
83
|
free() {
|
|
71
84
|
if (this.#freed) {
|
|
@@ -78,25 +91,14 @@ export class OpusEncoder {
|
|
|
78
91
|
[Symbol.dispose]() {
|
|
79
92
|
this.free();
|
|
80
93
|
}
|
|
81
|
-
#
|
|
82
|
-
this.#
|
|
83
|
-
|
|
84
|
-
throw new Error("OpusEncoder is not ready; await encoder.ready or use OpusEncoder.create()");
|
|
94
|
+
#requireCodec(codec) {
|
|
95
|
+
if (this.#initError) {
|
|
96
|
+
throw new Error("OpusEncoder failed to initialize", { cause: this.#initError });
|
|
85
97
|
}
|
|
86
|
-
|
|
87
|
-
}
|
|
88
|
-
#requireDecoder() {
|
|
89
|
-
this.#throwInitErrorIfAny();
|
|
90
|
-
if (!this.#decoder || this.#freed) {
|
|
98
|
+
if (!codec || this.#freed) {
|
|
91
99
|
throw new Error("OpusEncoder is not ready; await encoder.ready or use OpusEncoder.create()");
|
|
92
100
|
}
|
|
93
|
-
return
|
|
94
|
-
}
|
|
95
|
-
#throwInitErrorIfAny() {
|
|
96
|
-
if (!this.#initError) {
|
|
97
|
-
return;
|
|
98
|
-
}
|
|
99
|
-
throw new Error("OpusEncoder failed to initialize", { cause: this.#initError });
|
|
101
|
+
return codec;
|
|
100
102
|
}
|
|
101
103
|
}
|
|
102
104
|
function inferFrameSize(byteLength, channels) {
|
|
Binary file
|
package/dist/index.d.ts
CHANGED
|
@@ -98,6 +98,8 @@ export type OpusEncoderHandle = {
|
|
|
98
98
|
encodeFrames(frames: readonly (Int16Array | Uint8Array)[], options?: EncodeOptions): Uint8Array[];
|
|
99
99
|
encodeFloatFrames(frames: readonly Float32Array[], options?: EncodeOptions): Uint8Array[];
|
|
100
100
|
encoderCtl(request: number, value: number): void;
|
|
101
|
+
/** Clear stream history while preserving the configured encoder settings. */
|
|
102
|
+
reset(): void;
|
|
101
103
|
free(): void;
|
|
102
104
|
getBitrate(): number;
|
|
103
105
|
getInDtx(): boolean;
|
|
@@ -124,6 +126,8 @@ export type OpusDecoderHandle = {
|
|
|
124
126
|
decodePacketLoss(frameSize?: number): Int16Array;
|
|
125
127
|
decodePacketLossFloat(frameSize?: number): Float32Array;
|
|
126
128
|
decoderCtl(request: number, value: number): void;
|
|
129
|
+
/** Clear stream history before decoding an independent stream. */
|
|
130
|
+
reset(): void;
|
|
127
131
|
free(): void;
|
|
128
132
|
[Symbol.dispose](): void;
|
|
129
133
|
};
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,WAAW;aACtB,IAAI,EAAE,IAAI;aACV,KAAK,EAAE,IAAI;aACX,kBAAkB,EAAE,IAAI;CAChB,CAAC;AAEX,eAAO,MAAM,MAAM;aACjB,IAAI,EAAE,CAAC,IAAI;aACX,KAAK,EAAE,IAAI;aACX,KAAK,EAAE,IAAI;CACH,CAAC;AAEX,eAAO,MAAM,OAAO;aAClB,IAAI,EAAE,CAAC,IAAI;aACX,GAAG,EAAE,CAAC,CAAC;CACC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AAC;AAE3D,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,SAAS,CAoB9D"}
|
package/dist/index.js
CHANGED
|
@@ -46,6 +46,8 @@ const DEFAULT_CHANNELS = 2;
|
|
|
46
46
|
const DEFAULT_FRAME_DURATION_MS = 20;
|
|
47
47
|
const MAX_PACKET_DURATION_MS = 120;
|
|
48
48
|
const DEFAULT_MAX_PACKET_BYTES = 4000;
|
|
49
|
+
const MAX_ENCODER_PACKET_BYTES = 1276 * 6;
|
|
50
|
+
const MIN_I32 = -0x8000_0000;
|
|
49
51
|
const MAX_I32 = 0x7fff_ffff;
|
|
50
52
|
const DEFAULT_SAMPLE_RATE = 48_000;
|
|
51
53
|
const DECODER_INTEGER_CTL_REQUESTS = new Set(Object.values(DecoderCtl));
|
|
@@ -55,7 +57,7 @@ const VALID_SAMPLE_RATES = [8000, 12000, 16000, 24000, 48000];
|
|
|
55
57
|
let modulePromise;
|
|
56
58
|
export async function loadLibopus() {
|
|
57
59
|
const module = await getModule();
|
|
58
|
-
return { version: module.UTF8ToString(module.
|
|
60
|
+
return { version: module.UTF8ToString(module._opus_get_version_string()) };
|
|
59
61
|
}
|
|
60
62
|
export async function createEncoder(options = {}) {
|
|
61
63
|
const module = await getModule();
|
|
@@ -75,34 +77,21 @@ export async function getPacketInfo(packet, options = {}) {
|
|
|
75
77
|
const packetPtr = checkedMalloc(module, packet.byteLength);
|
|
76
78
|
try {
|
|
77
79
|
module.HEAPU8.set(packet, packetPtr);
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
}
|
|
82
|
-
const frames = module._oc_packet_parse(packetPtr, packet.byteLength);
|
|
83
|
-
if (frames < 0) {
|
|
84
|
-
throw createOpusError(module, frames, "getPacketInfo");
|
|
85
|
-
}
|
|
86
|
-
const samples = module._oc_packet_get_nb_samples(packetPtr, packet.byteLength, sampleRate);
|
|
87
|
-
if (samples < 0) {
|
|
88
|
-
throw createOpusError(module, samples, "getPacketInfo");
|
|
89
|
-
}
|
|
90
|
-
const channels = module._oc_packet_get_nb_channels(packetPtr);
|
|
80
|
+
const samples = checkOpusResult(module, module._oc_packet_validate_decode(packetPtr, packet.byteLength, sampleRate), "getPacketInfo");
|
|
81
|
+
const samplesPerFrame = module._opus_packet_get_samples_per_frame(packetPtr, sampleRate);
|
|
82
|
+
const channels = module._opus_packet_get_nb_channels(packetPtr);
|
|
91
83
|
if (channels !== 1 && channels !== 2) {
|
|
92
84
|
throw new OpusError(channels, `libopus getPacketInfo failed (${channels}): invalid channel count`);
|
|
93
85
|
}
|
|
94
|
-
const bandwidth = module.
|
|
95
|
-
if (bandwidth < 0) {
|
|
96
|
-
throw createOpusError(module, bandwidth, "getPacketInfo");
|
|
97
|
-
}
|
|
86
|
+
const bandwidth = checkOpusResult(module, module._opus_packet_get_bandwidth(packetPtr), "getPacketInfo");
|
|
98
87
|
validateBandwidth(bandwidth, "packet bandwidth");
|
|
99
88
|
return {
|
|
100
89
|
bandwidth: bandwidth,
|
|
101
90
|
channels,
|
|
102
91
|
durationMs: (samples / sampleRate) * 1000,
|
|
103
|
-
frames,
|
|
92
|
+
frames: samples / samplesPerFrame,
|
|
104
93
|
samples,
|
|
105
|
-
samplesPerFrame
|
|
94
|
+
samplesPerFrame,
|
|
106
95
|
sampleRate,
|
|
107
96
|
};
|
|
108
97
|
}
|
|
@@ -110,6 +99,31 @@ export async function getPacketInfo(packet, options = {}) {
|
|
|
110
99
|
module._free(packetPtr);
|
|
111
100
|
}
|
|
112
101
|
}
|
|
102
|
+
class WasmBuffer {
|
|
103
|
+
module;
|
|
104
|
+
#bytes = 0;
|
|
105
|
+
#ptr = 0;
|
|
106
|
+
constructor(module) {
|
|
107
|
+
this.module = module;
|
|
108
|
+
}
|
|
109
|
+
ensure(requiredBytes) {
|
|
110
|
+
if (this.#ptr !== 0 && this.#bytes >= requiredBytes) {
|
|
111
|
+
return this.#ptr;
|
|
112
|
+
}
|
|
113
|
+
const nextPtr = checkedMalloc(this.module, requiredBytes);
|
|
114
|
+
this.free();
|
|
115
|
+
this.#ptr = nextPtr;
|
|
116
|
+
this.#bytes = requiredBytes;
|
|
117
|
+
return this.#ptr;
|
|
118
|
+
}
|
|
119
|
+
free() {
|
|
120
|
+
if (this.#ptr !== 0) {
|
|
121
|
+
this.module._free(this.#ptr);
|
|
122
|
+
}
|
|
123
|
+
this.#ptr = 0;
|
|
124
|
+
this.#bytes = 0;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
113
127
|
class WasmOpusEncoder {
|
|
114
128
|
application;
|
|
115
129
|
channels;
|
|
@@ -117,30 +131,18 @@ class WasmOpusEncoder {
|
|
|
117
131
|
sampleRate;
|
|
118
132
|
#freed = false;
|
|
119
133
|
#module;
|
|
120
|
-
#
|
|
121
|
-
#
|
|
122
|
-
#pcmBytes = 0;
|
|
123
|
-
#pcmPtr = 0;
|
|
134
|
+
#packet;
|
|
135
|
+
#pcm;
|
|
124
136
|
#ptr;
|
|
125
137
|
constructor(module, options) {
|
|
126
138
|
this.#module = module;
|
|
139
|
+
this.#packet = new WasmBuffer(module);
|
|
140
|
+
this.#pcm = new WasmBuffer(module);
|
|
127
141
|
this.application = options.application;
|
|
128
142
|
this.channels = options.channels;
|
|
129
143
|
this.frameSize = options.frameSize;
|
|
130
144
|
this.sampleRate = options.sampleRate;
|
|
131
|
-
|
|
132
|
-
try {
|
|
133
|
-
const ptr = module._oc_create_encoder(options.sampleRate, options.channels, options.application, errorPtr);
|
|
134
|
-
const error = module.HEAP32[errorPtr >> 2] ?? 0;
|
|
135
|
-
if (!ptr || error !== 0) {
|
|
136
|
-
throw createOpusError(module, error, "createEncoder");
|
|
137
|
-
}
|
|
138
|
-
this.#ptr = ptr;
|
|
139
|
-
}
|
|
140
|
-
finally {
|
|
141
|
-
module._free(errorPtr);
|
|
142
|
-
}
|
|
143
|
-
let configured = false;
|
|
145
|
+
this.#ptr = createCodecHandle(module, "createEncoder", (errorPtr) => module._opus_encoder_create(options.sampleRate, options.channels, options.application, errorPtr));
|
|
144
146
|
try {
|
|
145
147
|
this.setBitrate(options.bitrate);
|
|
146
148
|
this.setComplexity(options.complexity);
|
|
@@ -157,93 +159,83 @@ class WasmOpusEncoder {
|
|
|
157
159
|
if (options.vbrConstraint !== undefined) {
|
|
158
160
|
this.setVbrConstraint(options.vbrConstraint);
|
|
159
161
|
}
|
|
160
|
-
configured = true;
|
|
161
162
|
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
}
|
|
163
|
+
catch (error) {
|
|
164
|
+
this.free();
|
|
165
|
+
throw error;
|
|
166
166
|
}
|
|
167
167
|
}
|
|
168
168
|
encode(pcm, options = {}) {
|
|
169
|
-
this.#
|
|
170
|
-
const frameSize = options.frameSize ?? this.frameSize;
|
|
171
|
-
validateEncodeFrameSize(frameSize, this.sampleRate, "frameSize");
|
|
172
|
-
const pcmBytes = toUint8Array(pcm);
|
|
173
|
-
const expectedBytes = frameSize * this.channels * 2;
|
|
174
|
-
if (pcmBytes.byteLength !== expectedBytes) {
|
|
175
|
-
throw new RangeError(`PCM frame has ${pcmBytes.byteLength} bytes; expected ${expectedBytes} for ${frameSize} samples and ${this.channels} channel(s)`);
|
|
176
|
-
}
|
|
177
|
-
const maxPacketBytes = options.maxPacketBytes ?? DEFAULT_MAX_PACKET_BYTES;
|
|
178
|
-
validateIntegerRange(maxPacketBytes, 1, MAX_I32, "maxPacketBytes");
|
|
179
|
-
const pcmPtr = this.#ensurePcmBytes(pcmBytes.byteLength);
|
|
180
|
-
const packetPtr = this.#ensurePacketBytes(maxPacketBytes);
|
|
181
|
-
this.#module.HEAPU8.set(pcmBytes, pcmPtr);
|
|
182
|
-
const encodedBytes = this.#module._oc_encode(this.#ptr, pcmPtr, frameSize, packetPtr, maxPacketBytes);
|
|
183
|
-
if (encodedBytes < 0) {
|
|
184
|
-
throw createOpusError(this.#module, encodedBytes, "encode");
|
|
185
|
-
}
|
|
186
|
-
return this.#module.HEAPU8.slice(packetPtr, packetPtr + encodedBytes);
|
|
169
|
+
return this.#encode(pcm, options, false);
|
|
187
170
|
}
|
|
188
171
|
encodeFloat(pcm, options = {}) {
|
|
172
|
+
return this.#encode(pcm, options, true);
|
|
173
|
+
}
|
|
174
|
+
#encode(pcm, options, float) {
|
|
189
175
|
this.#assertLive();
|
|
190
176
|
const frameSize = options.frameSize ?? this.frameSize;
|
|
191
|
-
validateEncodeFrameSize(frameSize, this.sampleRate
|
|
177
|
+
validateEncodeFrameSize(frameSize, this.sampleRate);
|
|
178
|
+
const input = float ? pcm : toUint8Array(pcm);
|
|
192
179
|
const expectedSamples = frameSize * this.channels;
|
|
193
|
-
if (
|
|
194
|
-
|
|
180
|
+
if (float) {
|
|
181
|
+
if (input.length !== expectedSamples) {
|
|
182
|
+
throw new RangeError(`Float32 PCM frame has ${input.length} samples; expected ${expectedSamples} for ${frameSize} samples and ${this.channels} channel(s)`);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
else if (input.byteLength !== expectedSamples * 2) {
|
|
186
|
+
throw new RangeError(`PCM frame has ${input.byteLength} bytes; expected ${expectedSamples * 2} for ${frameSize} samples and ${this.channels} channel(s)`);
|
|
195
187
|
}
|
|
196
188
|
const maxPacketBytes = options.maxPacketBytes ?? DEFAULT_MAX_PACKET_BYTES;
|
|
197
189
|
validateIntegerRange(maxPacketBytes, 1, MAX_I32, "maxPacketBytes");
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
const
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
190
|
+
// libopus 1.6.1 caps its effective budget at 1276 * 6, but long-frame
|
|
191
|
+
// repacketization allocates stack scratch from the supplied capacity.
|
|
192
|
+
const packetCapacity = Math.min(maxPacketBytes, MAX_ENCODER_PACKET_BYTES);
|
|
193
|
+
const pcmPtr = this.#pcm.ensure(input.byteLength);
|
|
194
|
+
const packetPtr = this.#packet.ensure(packetCapacity);
|
|
195
|
+
// Allocations can replace heap views; keep format-specific writes after both.
|
|
196
|
+
if (float) {
|
|
197
|
+
this.#module.HEAPF32.set(input, pcmPtr >> 2);
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
200
|
+
this.#module.HEAPU8.set(input, pcmPtr);
|
|
201
|
+
}
|
|
202
|
+
const encodedBytes = checkOpusResult(this.#module, this.#module[float ? "_opus_encode_float" : "_opus_encode"](this.#ptr, pcmPtr, frameSize, packetPtr, packetCapacity), float ? "encodeFloat" : "encode");
|
|
205
203
|
return this.#module.HEAPU8.slice(packetPtr, packetPtr + encodedBytes);
|
|
206
204
|
}
|
|
207
205
|
encodeFrames(frames, options = {}) {
|
|
206
|
+
this.#assertLive();
|
|
208
207
|
return frames.map((frame) => this.encode(frame, options));
|
|
209
208
|
}
|
|
210
209
|
encodeFloatFrames(frames, options = {}) {
|
|
210
|
+
this.#assertLive();
|
|
211
211
|
return frames.map((frame) => this.encodeFloat(frame, options));
|
|
212
212
|
}
|
|
213
213
|
encoderCtl(request, value) {
|
|
214
214
|
this.#assertLive();
|
|
215
215
|
validateInteger(request, "request");
|
|
216
|
-
|
|
216
|
+
validateIntegerRange(value, MIN_I32, MAX_I32, "value");
|
|
217
217
|
if (!ENCODER_INTEGER_CTL_REQUESTS.has(request)) {
|
|
218
218
|
throw new RangeError("encoderCtl only supports integer setter requests");
|
|
219
219
|
}
|
|
220
|
-
this.#
|
|
220
|
+
checkOpusResult(this.#module, this.#module._oc_encoder_ctl(this.#ptr, request, value), "encoderCtl");
|
|
221
|
+
if (request === EncoderCtl.SetApplication && isApplication(value)) {
|
|
222
|
+
this.application = value;
|
|
223
|
+
}
|
|
221
224
|
}
|
|
222
225
|
setBitrate(bitrate) {
|
|
223
226
|
this.encoderCtl(EncoderCtl.SetBitrate, normalizeBitrate(bitrate));
|
|
224
227
|
}
|
|
225
228
|
getBitrate() {
|
|
226
229
|
this.#assertLive();
|
|
227
|
-
|
|
228
|
-
if (bitrate < 0) {
|
|
229
|
-
throw createOpusError(this.#module, bitrate, "getBitrate");
|
|
230
|
-
}
|
|
231
|
-
return bitrate;
|
|
230
|
+
return checkOpusResult(this.#module, this.#module._oc_encoder_ctl_get_bitrate(this.#ptr), "getBitrate");
|
|
232
231
|
}
|
|
233
232
|
getLookahead() {
|
|
234
233
|
this.#assertLive();
|
|
235
|
-
|
|
236
|
-
if (lookahead < 0) {
|
|
237
|
-
throw createOpusError(this.#module, lookahead, "getLookahead");
|
|
238
|
-
}
|
|
239
|
-
return lookahead;
|
|
234
|
+
return checkOpusResult(this.#module, this.#module._oc_encoder_ctl_get_lookahead(this.#ptr), "getLookahead");
|
|
240
235
|
}
|
|
241
236
|
getInDtx() {
|
|
242
237
|
this.#assertLive();
|
|
243
|
-
const inDtx = this.#module._oc_encoder_ctl_get_in_dtx(this.#ptr);
|
|
244
|
-
if (inDtx < 0) {
|
|
245
|
-
throw createOpusError(this.#module, inDtx, "getInDtx");
|
|
246
|
-
}
|
|
238
|
+
const inDtx = checkOpusResult(this.#module, this.#module._oc_encoder_ctl_get_in_dtx(this.#ptr), "getInDtx");
|
|
247
239
|
return inDtx !== 0;
|
|
248
240
|
}
|
|
249
241
|
setComplexity(complexity) {
|
|
@@ -276,12 +268,17 @@ class WasmOpusEncoder {
|
|
|
276
268
|
setVbrConstraint(enabled) {
|
|
277
269
|
this.encoderCtl(EncoderCtl.SetVbrConstraint, enabled ? 1 : 0);
|
|
278
270
|
}
|
|
271
|
+
reset() {
|
|
272
|
+
this.#assertLive();
|
|
273
|
+
checkOpusResult(this.#module, this.#module._oc_reset_encoder(this.#ptr), "resetEncoder");
|
|
274
|
+
}
|
|
279
275
|
free() {
|
|
280
276
|
if (this.#freed) {
|
|
281
277
|
return;
|
|
282
278
|
}
|
|
283
|
-
this.#
|
|
284
|
-
this.#
|
|
279
|
+
this.#packet.free();
|
|
280
|
+
this.#pcm.free();
|
|
281
|
+
this.#module._opus_encoder_destroy(this.#ptr);
|
|
285
282
|
this.#freed = true;
|
|
286
283
|
}
|
|
287
284
|
[Symbol.dispose]() {
|
|
@@ -292,47 +289,6 @@ class WasmOpusEncoder {
|
|
|
292
289
|
throw new Error("OpusEncoder has been freed");
|
|
293
290
|
}
|
|
294
291
|
}
|
|
295
|
-
#check(code, operation) {
|
|
296
|
-
if (code < 0) {
|
|
297
|
-
throw createOpusError(this.#module, code, operation);
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
#ensurePacketBytes(requiredBytes) {
|
|
301
|
-
if (this.#packetPtr !== 0 && this.#packetBytes >= requiredBytes) {
|
|
302
|
-
return this.#packetPtr;
|
|
303
|
-
}
|
|
304
|
-
const nextPtr = checkedMalloc(this.#module, requiredBytes);
|
|
305
|
-
if (this.#packetPtr !== 0) {
|
|
306
|
-
this.#module._free(this.#packetPtr);
|
|
307
|
-
}
|
|
308
|
-
this.#packetPtr = nextPtr;
|
|
309
|
-
this.#packetBytes = requiredBytes;
|
|
310
|
-
return this.#packetPtr;
|
|
311
|
-
}
|
|
312
|
-
#ensurePcmBytes(requiredBytes) {
|
|
313
|
-
if (this.#pcmPtr !== 0 && this.#pcmBytes >= requiredBytes) {
|
|
314
|
-
return this.#pcmPtr;
|
|
315
|
-
}
|
|
316
|
-
const nextPtr = checkedMalloc(this.#module, requiredBytes);
|
|
317
|
-
if (this.#pcmPtr !== 0) {
|
|
318
|
-
this.#module._free(this.#pcmPtr);
|
|
319
|
-
}
|
|
320
|
-
this.#pcmPtr = nextPtr;
|
|
321
|
-
this.#pcmBytes = requiredBytes;
|
|
322
|
-
return this.#pcmPtr;
|
|
323
|
-
}
|
|
324
|
-
#freeScratch() {
|
|
325
|
-
if (this.#packetPtr !== 0) {
|
|
326
|
-
this.#module._free(this.#packetPtr);
|
|
327
|
-
}
|
|
328
|
-
if (this.#pcmPtr !== 0) {
|
|
329
|
-
this.#module._free(this.#pcmPtr);
|
|
330
|
-
}
|
|
331
|
-
this.#packetPtr = 0;
|
|
332
|
-
this.#packetBytes = 0;
|
|
333
|
-
this.#pcmPtr = 0;
|
|
334
|
-
this.#pcmBytes = 0;
|
|
335
|
-
}
|
|
336
292
|
}
|
|
337
293
|
class WasmOpusDecoder {
|
|
338
294
|
channels;
|
|
@@ -340,59 +296,45 @@ class WasmOpusDecoder {
|
|
|
340
296
|
sampleRate;
|
|
341
297
|
#freed = false;
|
|
342
298
|
#module;
|
|
343
|
-
#
|
|
344
|
-
#
|
|
345
|
-
#pcmBytes = 0;
|
|
346
|
-
#pcmPtr = 0;
|
|
299
|
+
#packet;
|
|
300
|
+
#pcm;
|
|
347
301
|
#ptr;
|
|
348
302
|
constructor(module, options) {
|
|
349
303
|
this.#module = module;
|
|
304
|
+
this.#packet = new WasmBuffer(module);
|
|
305
|
+
this.#pcm = new WasmBuffer(module);
|
|
350
306
|
this.channels = options.channels;
|
|
351
307
|
this.maxFrameSize = options.maxFrameSize;
|
|
352
308
|
this.sampleRate = options.sampleRate;
|
|
353
|
-
|
|
354
|
-
try {
|
|
355
|
-
const ptr = module._oc_create_decoder(options.sampleRate, options.channels, errorPtr);
|
|
356
|
-
const error = module.HEAP32[errorPtr >> 2] ?? 0;
|
|
357
|
-
if (!ptr || error !== 0) {
|
|
358
|
-
throw createOpusError(module, error, "createDecoder");
|
|
359
|
-
}
|
|
360
|
-
this.#ptr = ptr;
|
|
361
|
-
}
|
|
362
|
-
finally {
|
|
363
|
-
module._free(errorPtr);
|
|
364
|
-
}
|
|
309
|
+
this.#ptr = createCodecHandle(module, "createDecoder", (errorPtr) => module._opus_decoder_create(options.sampleRate, options.channels, errorPtr));
|
|
365
310
|
}
|
|
366
311
|
decode(packet, options = {}) {
|
|
367
|
-
this.#
|
|
368
|
-
const frameSize = this.#resolveDecodeFrameSize(packet, options);
|
|
369
|
-
const pcmBytes = frameSize * this.channels * 2;
|
|
370
|
-
const pcmPtr = this.#ensurePcmBytes(pcmBytes);
|
|
371
|
-
const { packetLength, packetPtr } = this.#copyPacket(packet, options.decodeFec);
|
|
372
|
-
const decodedSamples = this.#module._oc_decode(this.#ptr, packetPtr, packetLength, pcmPtr, frameSize, options.decodeFec ? 1 : 0);
|
|
373
|
-
if (decodedSamples < 0) {
|
|
374
|
-
throw createOpusError(this.#module, decodedSamples, packet === null ? "decodePacketLoss" : "decode");
|
|
375
|
-
}
|
|
376
|
-
const sampleCount = decodedSamples * this.channels;
|
|
377
|
-
return this.#module.HEAP16.slice(pcmPtr >> 1, (pcmPtr >> 1) + sampleCount);
|
|
312
|
+
return this.#decode(packet, options, false);
|
|
378
313
|
}
|
|
379
314
|
decodeFloat(packet, options = {}) {
|
|
315
|
+
return this.#decode(packet, options, true);
|
|
316
|
+
}
|
|
317
|
+
#decode(packet, options, float) {
|
|
380
318
|
this.#assertLive();
|
|
381
319
|
const frameSize = this.#resolveDecodeFrameSize(packet, options);
|
|
382
|
-
const
|
|
383
|
-
const pcmPtr = this.#ensurePcmBytes(pcmBytes);
|
|
320
|
+
const pcmPtr = this.#pcm.ensure(frameSize * this.channels * (float ? 4 : 2));
|
|
384
321
|
const { packetLength, packetPtr } = this.#copyPacket(packet, options.decodeFec);
|
|
385
|
-
const
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
322
|
+
const operation = packet === null
|
|
323
|
+
? (float ? "decodePacketLossFloat" : "decodePacketLoss")
|
|
324
|
+
: (float ? "decodeFloat" : "decode");
|
|
325
|
+
const decodedSamples = checkOpusResult(this.#module, this.#module[float ? "_opus_decode_float" : "_opus_decode"](this.#ptr, packetPtr, packetLength, pcmPtr, frameSize, options.decodeFec ? 1 : 0), operation);
|
|
389
326
|
const sampleCount = decodedSamples * this.channels;
|
|
390
|
-
|
|
327
|
+
// Read the current heap after native decoding and return an owned copy.
|
|
328
|
+
return float
|
|
329
|
+
? this.#module.HEAPF32.slice(pcmPtr >> 2, (pcmPtr >> 2) + sampleCount)
|
|
330
|
+
: this.#module.HEAP16.slice(pcmPtr >> 1, (pcmPtr >> 1) + sampleCount);
|
|
391
331
|
}
|
|
392
332
|
decodeFrames(packets, options = {}) {
|
|
333
|
+
this.#assertLive();
|
|
393
334
|
return packets.map((packet) => this.decode(packet, options));
|
|
394
335
|
}
|
|
395
336
|
decodeFloatFrames(packets, options = {}) {
|
|
337
|
+
this.#assertLive();
|
|
396
338
|
return packets.map((packet) => this.decodeFloat(packet, options));
|
|
397
339
|
}
|
|
398
340
|
decodePacketLoss(frameSize = samplesForDuration(this.sampleRate, DEFAULT_FRAME_DURATION_MS)) {
|
|
@@ -404,21 +346,23 @@ class WasmOpusDecoder {
|
|
|
404
346
|
decoderCtl(request, value) {
|
|
405
347
|
this.#assertLive();
|
|
406
348
|
validateInteger(request, "request");
|
|
407
|
-
|
|
349
|
+
validateIntegerRange(value, MIN_I32, MAX_I32, "value");
|
|
408
350
|
if (!DECODER_INTEGER_CTL_REQUESTS.has(request)) {
|
|
409
351
|
throw new RangeError("decoderCtl only supports integer setter requests");
|
|
410
352
|
}
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
353
|
+
checkOpusResult(this.#module, this.#module._oc_decoder_ctl(this.#ptr, request, value), "decoderCtl");
|
|
354
|
+
}
|
|
355
|
+
reset() {
|
|
356
|
+
this.#assertLive();
|
|
357
|
+
checkOpusResult(this.#module, this.#module._oc_reset_decoder(this.#ptr), "resetDecoder");
|
|
415
358
|
}
|
|
416
359
|
free() {
|
|
417
360
|
if (this.#freed) {
|
|
418
361
|
return;
|
|
419
362
|
}
|
|
420
|
-
this.#
|
|
421
|
-
this.#
|
|
363
|
+
this.#packet.free();
|
|
364
|
+
this.#pcm.free();
|
|
365
|
+
this.#module._opus_decoder_destroy(this.#ptr);
|
|
422
366
|
this.#freed = true;
|
|
423
367
|
}
|
|
424
368
|
[Symbol.dispose]() {
|
|
@@ -439,46 +383,10 @@ class WasmOpusDecoder {
|
|
|
439
383
|
if (packet.byteLength === 0) {
|
|
440
384
|
throw new RangeError("packet must not be empty; use null or decodePacketLoss() for PLC");
|
|
441
385
|
}
|
|
442
|
-
const packetPtr = this.#
|
|
386
|
+
const packetPtr = this.#packet.ensure(packet.byteLength);
|
|
443
387
|
this.#module.HEAPU8.set(packet, packetPtr);
|
|
444
388
|
return { packetLength: packet.byteLength, packetPtr };
|
|
445
389
|
}
|
|
446
|
-
#ensurePacketBytes(requiredBytes) {
|
|
447
|
-
if (this.#packetPtr !== 0 && this.#packetBytes >= requiredBytes) {
|
|
448
|
-
return this.#packetPtr;
|
|
449
|
-
}
|
|
450
|
-
const nextPtr = checkedMalloc(this.#module, requiredBytes);
|
|
451
|
-
if (this.#packetPtr !== 0) {
|
|
452
|
-
this.#module._free(this.#packetPtr);
|
|
453
|
-
}
|
|
454
|
-
this.#packetPtr = nextPtr;
|
|
455
|
-
this.#packetBytes = requiredBytes;
|
|
456
|
-
return this.#packetPtr;
|
|
457
|
-
}
|
|
458
|
-
#ensurePcmBytes(requiredBytes) {
|
|
459
|
-
if (this.#pcmPtr !== 0 && this.#pcmBytes >= requiredBytes) {
|
|
460
|
-
return this.#pcmPtr;
|
|
461
|
-
}
|
|
462
|
-
const nextPtr = checkedMalloc(this.#module, requiredBytes);
|
|
463
|
-
if (this.#pcmPtr !== 0) {
|
|
464
|
-
this.#module._free(this.#pcmPtr);
|
|
465
|
-
}
|
|
466
|
-
this.#pcmPtr = nextPtr;
|
|
467
|
-
this.#pcmBytes = requiredBytes;
|
|
468
|
-
return this.#pcmPtr;
|
|
469
|
-
}
|
|
470
|
-
#freeScratch() {
|
|
471
|
-
if (this.#packetPtr !== 0) {
|
|
472
|
-
this.#module._free(this.#packetPtr);
|
|
473
|
-
}
|
|
474
|
-
if (this.#pcmPtr !== 0) {
|
|
475
|
-
this.#module._free(this.#pcmPtr);
|
|
476
|
-
}
|
|
477
|
-
this.#packetPtr = 0;
|
|
478
|
-
this.#packetBytes = 0;
|
|
479
|
-
this.#pcmPtr = 0;
|
|
480
|
-
this.#pcmBytes = 0;
|
|
481
|
-
}
|
|
482
390
|
#resolveDecodeFrameSize(packet, options) {
|
|
483
391
|
const frameSize = packet === null || options.decodeFec
|
|
484
392
|
? (options.frameSize ?? options.maxFrameSize ?? samplesForDuration(this.sampleRate, DEFAULT_FRAME_DURATION_MS))
|
|
@@ -538,20 +446,33 @@ function resolveOpusErrorCodeName(code) {
|
|
|
538
446
|
return undefined;
|
|
539
447
|
}
|
|
540
448
|
function createOpusError(module, code, operation) {
|
|
541
|
-
const message = module.UTF8ToString(module.
|
|
449
|
+
const message = module.UTF8ToString(module._opus_strerror(code));
|
|
542
450
|
return new OpusError(code, `libopus ${operation} failed (${code}): ${message}`, operation);
|
|
543
451
|
}
|
|
452
|
+
function checkOpusResult(module, code, operation) {
|
|
453
|
+
if (code < 0) {
|
|
454
|
+
throw createOpusError(module, code, operation);
|
|
455
|
+
}
|
|
456
|
+
return code;
|
|
457
|
+
}
|
|
544
458
|
function toUint8Array(input) {
|
|
545
459
|
return input instanceof Uint8Array
|
|
546
460
|
? input
|
|
547
461
|
: new Uint8Array(input.buffer, input.byteOffset, input.byteLength);
|
|
548
462
|
}
|
|
463
|
+
function isApplication(value) {
|
|
464
|
+
return Object.values(Application).some((mode) => mode === value);
|
|
465
|
+
}
|
|
549
466
|
function normalizeEncoderOptions(options) {
|
|
550
467
|
const sampleRate = options.sampleRate ?? DEFAULT_SAMPLE_RATE;
|
|
551
468
|
const channels = options.channels ?? DEFAULT_CHANNELS;
|
|
552
469
|
validateCodecOptions({ channels, sampleRate });
|
|
470
|
+
const application = options.application ?? Application.Audio;
|
|
471
|
+
if (!isApplication(application)) {
|
|
472
|
+
throw new RangeError("application must be Application.Voip, Application.Audio, or Application.RestrictedLowDelay");
|
|
473
|
+
}
|
|
553
474
|
const frameSize = options.frameSize ?? samplesForDuration(sampleRate, DEFAULT_FRAME_DURATION_MS);
|
|
554
|
-
validateEncodeFrameSize(frameSize, sampleRate
|
|
475
|
+
validateEncodeFrameSize(frameSize, sampleRate);
|
|
555
476
|
if (options.maxBandwidth !== undefined) {
|
|
556
477
|
validateBandwidth(options.maxBandwidth, "maxBandwidth");
|
|
557
478
|
}
|
|
@@ -564,7 +485,7 @@ function normalizeEncoderOptions(options) {
|
|
|
564
485
|
throw new RangeError("signal must be Signal.Auto, Signal.Voice, or Signal.Music");
|
|
565
486
|
}
|
|
566
487
|
return {
|
|
567
|
-
application
|
|
488
|
+
application,
|
|
568
489
|
bitrate: normalizeBitrate(options.bitrate ?? 64_000),
|
|
569
490
|
channels,
|
|
570
491
|
complexity,
|
|
@@ -608,7 +529,7 @@ function normalizeBitrate(bitrate) {
|
|
|
608
529
|
if (bitrate === Bitrate.Auto || bitrate === Bitrate.Max) {
|
|
609
530
|
return bitrate;
|
|
610
531
|
}
|
|
611
|
-
|
|
532
|
+
validateIntegerRange(bitrate, 1, MAX_I32, "bitrate");
|
|
612
533
|
return bitrate;
|
|
613
534
|
}
|
|
614
535
|
function validateBandwidth(bandwidth, name) {
|
|
@@ -616,8 +537,11 @@ function validateBandwidth(bandwidth, name) {
|
|
|
616
537
|
throw new RangeError(`${name} must be Bandwidth.Narrowband, Bandwidth.Mediumband, Bandwidth.Wideband, Bandwidth.Superwideband, or Bandwidth.Fullband`);
|
|
617
538
|
}
|
|
618
539
|
}
|
|
619
|
-
function validateEncodeFrameSize(frameSize, sampleRate
|
|
620
|
-
|
|
540
|
+
function validateEncodeFrameSize(frameSize, sampleRate) {
|
|
541
|
+
const validFrameSizes = ENCODE_FRAME_DURATIONS_MS.map((durationMs) => samplesForDuration(sampleRate, durationMs));
|
|
542
|
+
if (!Number.isInteger(frameSize) || !validFrameSizes.includes(frameSize)) {
|
|
543
|
+
throw new RangeError(`frameSize must be one of ${validFrameSizes.join(", ")} samples at ${sampleRate} Hz`);
|
|
544
|
+
}
|
|
621
545
|
}
|
|
622
546
|
function validateDecodeCapacity(frameSize, sampleRate, name) {
|
|
623
547
|
const maxFrameSize = samplesForDuration(sampleRate, MAX_PACKET_DURATION_MS);
|
|
@@ -635,27 +559,30 @@ function validatePlcFrameSize(frameSize, sampleRate, name) {
|
|
|
635
559
|
throw new RangeError(`${name} must be a multiple of ${minFrameSize} samples from ${minFrameSize} to ${maxFrameSize} at ${sampleRate} Hz`);
|
|
636
560
|
}
|
|
637
561
|
}
|
|
638
|
-
function validateFrameSizeForDurations(frameSize, sampleRate, name, durationsMs) {
|
|
639
|
-
const validFrameSizes = durationsMs.map((durationMs) => samplesForDuration(sampleRate, durationMs));
|
|
640
|
-
if (!Number.isInteger(frameSize) || !validFrameSizes.includes(frameSize)) {
|
|
641
|
-
throw new RangeError(`${name} must be one of ${validFrameSizes.join(", ")} samples at ${sampleRate} Hz`);
|
|
642
|
-
}
|
|
643
|
-
}
|
|
644
562
|
function validateInteger(value, name) {
|
|
645
563
|
if (!Number.isInteger(value)) {
|
|
646
564
|
throw new RangeError(`${name} must be an integer`);
|
|
647
565
|
}
|
|
648
566
|
}
|
|
649
|
-
function validatePositiveInteger(value, name) {
|
|
650
|
-
if (!Number.isInteger(value) || value <= 0) {
|
|
651
|
-
throw new RangeError(`${name} must be a positive integer`);
|
|
652
|
-
}
|
|
653
|
-
}
|
|
654
567
|
function validateIntegerRange(value, min, max, name) {
|
|
655
568
|
if (!Number.isInteger(value) || value < min || value > max) {
|
|
656
569
|
throw new RangeError(`${name} must be an integer from ${min} to ${max}`);
|
|
657
570
|
}
|
|
658
571
|
}
|
|
572
|
+
function createCodecHandle(module, operation, create) {
|
|
573
|
+
const errorPtr = checkedMalloc(module, 4);
|
|
574
|
+
try {
|
|
575
|
+
const ptr = create(errorPtr);
|
|
576
|
+
const error = module.HEAP32[errorPtr >> 2] ?? 0;
|
|
577
|
+
if (!ptr || error !== 0) {
|
|
578
|
+
throw createOpusError(module, error, operation);
|
|
579
|
+
}
|
|
580
|
+
return ptr;
|
|
581
|
+
}
|
|
582
|
+
finally {
|
|
583
|
+
module._free(errorPtr);
|
|
584
|
+
}
|
|
585
|
+
}
|
|
659
586
|
function checkedMalloc(module, bytes) {
|
|
660
587
|
validateIntegerRange(bytes, 1, MAX_I32, "malloc size");
|
|
661
588
|
const ptr = module._malloc(bytes);
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "libopus-wasm",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.1",
|
|
4
|
+
"packageManager": "pnpm@11.27.1",
|
|
4
5
|
"description": "Small, modern WASM bindings for libopus raw packet encode/decode.",
|
|
5
6
|
"type": "module",
|
|
6
7
|
"sideEffects": false,
|
|
@@ -32,27 +33,35 @@
|
|
|
32
33
|
"CHANGELOG.md",
|
|
33
34
|
"LICENSE",
|
|
34
35
|
"README.md",
|
|
35
|
-
"THIRD_PARTY_NOTICES.md"
|
|
36
|
+
"THIRD_PARTY_NOTICES.md",
|
|
37
|
+
"scripts/release-smoke.mjs"
|
|
36
38
|
],
|
|
37
|
-
"devDependencies": {
|
|
38
|
-
"@discordjs/opus": "0.10.0",
|
|
39
|
-
"@types/node": "26.4.1",
|
|
40
|
-
"@vitest/coverage-v8": "5.0.0",
|
|
41
|
-
"typescript": "^7.0.2",
|
|
42
|
-
"vite": "8.2.2",
|
|
43
|
-
"vitest": "5.0.0"
|
|
44
|
-
},
|
|
45
|
-
"engines": {
|
|
46
|
-
"node": ">=22"
|
|
47
|
-
},
|
|
48
39
|
"scripts": {
|
|
49
40
|
"build": "pnpm build:wasm && tsc -p tsconfig.json && node scripts/copy-generated.mjs",
|
|
50
41
|
"build:wasm": "node scripts/build-libopus-wasm.mjs",
|
|
51
42
|
"benchmark": "pnpm build && node scripts/benchmark-native.mjs",
|
|
52
43
|
"clean": "rm -rf dist .cache",
|
|
53
44
|
"docs:build": "node scripts/build-docs-site.mjs",
|
|
45
|
+
"prepack": "pnpm build",
|
|
54
46
|
"test": "pnpm build:wasm && vitest run",
|
|
55
47
|
"test:coverage": "pnpm build:wasm && vitest run --coverage",
|
|
56
|
-
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
48
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
49
|
+
"test:browser": "pnpm build && playwright test",
|
|
50
|
+
"size:check": "node scripts/check-size.mjs",
|
|
51
|
+
"test:integrations": "node scripts/check-integrations.mjs",
|
|
52
|
+
"profile:allocations": "node --expose-gc scripts/profile-allocations.mjs"
|
|
53
|
+
},
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"@discordjs/opus": "0.10.0",
|
|
56
|
+
"@types/node": "26.6.6",
|
|
57
|
+
"@vitest/coverage-v8": "5.0.3",
|
|
58
|
+
"typescript": "^7.0.2",
|
|
59
|
+
"vite": "8.3.5",
|
|
60
|
+
"vitest": "5.0.3",
|
|
61
|
+
"prism-media": "1.3.5",
|
|
62
|
+
"@playwright/test": "1.64.1"
|
|
63
|
+
},
|
|
64
|
+
"engines": {
|
|
65
|
+
"node": ">=22"
|
|
57
66
|
}
|
|
58
|
-
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { createDecoder, createEncoder, getPacketInfo } from "libopus-wasm";
|
|
3
|
+
import { OpusEncoder } from "libopus-wasm/discordjs";
|
|
4
|
+
|
|
5
|
+
const encoder = await createEncoder();
|
|
6
|
+
const decoder = await createDecoder();
|
|
7
|
+
try {
|
|
8
|
+
const pcm = makePcm(encoder.frameSize, encoder.channels, encoder.sampleRate);
|
|
9
|
+
const packet = encoder.encode(pcm);
|
|
10
|
+
assert.ok(packet.byteLength > 0);
|
|
11
|
+
assert.equal((await getPacketInfo(packet)).samples, encoder.frameSize);
|
|
12
|
+
decoder.reset();
|
|
13
|
+
const decoded = decoder.decode(packet);
|
|
14
|
+
assert.equal(decoded.length, pcm.length);
|
|
15
|
+
assert.ok(decoded.some((sample) => sample !== 0), "decoded audio must not be silent");
|
|
16
|
+
} finally {
|
|
17
|
+
encoder.free();
|
|
18
|
+
decoder.free();
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const opus = await OpusEncoder.create(48000, 2);
|
|
22
|
+
try {
|
|
23
|
+
const pcm = Buffer.from(makePcm(960, 2, 48000).buffer);
|
|
24
|
+
const packet = opus.encode(pcm);
|
|
25
|
+
assert.ok(packet.byteLength > 0);
|
|
26
|
+
const decoded = opus.decode(packet);
|
|
27
|
+
assert.equal(decoded.length, pcm.length);
|
|
28
|
+
assert.ok(decoded.some((byte) => byte !== 0), "adapter audio must not be silent");
|
|
29
|
+
} finally {
|
|
30
|
+
opus.free();
|
|
31
|
+
}
|
|
32
|
+
console.log("Packed ESM and Discord adapter encode/decode passed.");
|
|
33
|
+
|
|
34
|
+
function makePcm(frameSize, channels, sampleRate) {
|
|
35
|
+
return Int16Array.from({ length: frameSize * channels }, (_, index) =>
|
|
36
|
+
Math.round(Math.sin((Math.floor(index / channels) * 2 * Math.PI * 440) / sampleRate) * 8000),
|
|
37
|
+
);
|
|
38
|
+
}
|