libopus-wasm 0.0.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/LICENSE +21 -0
- package/README.md +87 -0
- package/THIRD_PARTY_NOTICES.md +12 -0
- package/dist/generated/libopus.generated.mjs +0 -0
- package/dist/index.d.ts +47 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +209 -0
- package/package.json +45 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 openclaw
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# libopus-wasm
|
|
2
|
+
|
|
3
|
+
[](https://github.com/openclaw/libopus-wasm/actions/workflows/ci.yml)
|
|
4
|
+
|
|
5
|
+
Small, modern WASM bindings for libopus raw packet encode/decode.
|
|
6
|
+
|
|
7
|
+
This package targets the narrow realtime voice use case: 48 kHz PCM and raw
|
|
8
|
+
Opus packets, with no Ogg/WebM container layer.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm install libopus-wasm
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import { Application, createDecoder, createEncoder } from "libopus-wasm";
|
|
20
|
+
|
|
21
|
+
const encoder = await createEncoder({
|
|
22
|
+
sampleRate: 48_000,
|
|
23
|
+
channels: 2,
|
|
24
|
+
application: Application.Audio,
|
|
25
|
+
});
|
|
26
|
+
const decoder = await createDecoder({ sampleRate: 48_000, channels: 2 });
|
|
27
|
+
|
|
28
|
+
try {
|
|
29
|
+
const frameSize = 960; // 20 ms at 48 kHz
|
|
30
|
+
const pcm = new Int16Array(frameSize * 2);
|
|
31
|
+
const packet = encoder.encodePcm16(pcm, frameSize);
|
|
32
|
+
const decoded = decoder.decodeFrame(packet, frameSize);
|
|
33
|
+
console.log(decoded.length);
|
|
34
|
+
} finally {
|
|
35
|
+
encoder.free();
|
|
36
|
+
decoder.free();
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Build
|
|
41
|
+
|
|
42
|
+
The npm package ships compiled output. Building from source requires
|
|
43
|
+
Emscripten (`emcc`) on `PATH`.
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
pnpm install
|
|
47
|
+
pnpm build
|
|
48
|
+
pnpm test
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
`pnpm build` downloads `libopus 1.6.1` from Xiph.Org, verifies the pinned
|
|
52
|
+
SHA-256, builds it with Emscripten, and emits a single-file ES module under
|
|
53
|
+
`dist/generated/`.
|
|
54
|
+
|
|
55
|
+
## API
|
|
56
|
+
|
|
57
|
+
- `loadLibopus()`: returns the bundled libopus version string.
|
|
58
|
+
- `createEncoder(options)`: creates a raw packet encoder.
|
|
59
|
+
- `createDecoder(options)`: creates a raw packet decoder.
|
|
60
|
+
- `encoder.encodePcm16(pcm, frameSize)`: encodes one PCM frame.
|
|
61
|
+
- `decoder.decodeFrame(packet, frameSize)`: decodes one raw Opus packet.
|
|
62
|
+
- `encoder.setBitrate`, `encoder.setFec`, `encoder.setPacketLossPercent`: small CTL surface for realtime voice tuning.
|
|
63
|
+
- `free()`: releases the underlying libopus encoder/decoder.
|
|
64
|
+
|
|
65
|
+
Supported sample rates are `8000`, `12000`, `16000`, `24000`, and `48000`.
|
|
66
|
+
Supported channel counts are mono and stereo.
|
|
67
|
+
|
|
68
|
+
## Benchmark
|
|
69
|
+
|
|
70
|
+
Native comparison requires `@discordjs/opus` to build on the host:
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
pnpm benchmark
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Local Apple Silicon result on Node 26, 20k iterations, 48 kHz stereo, 20 ms
|
|
77
|
+
frames:
|
|
78
|
+
|
|
79
|
+
```text
|
|
80
|
+
wasm encode: 15,304 ops/sec
|
|
81
|
+
native encode: 15,741 ops/sec
|
|
82
|
+
wasm decode: 38,416 ops/sec
|
|
83
|
+
native decode: 41,280 ops/sec
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
The benchmark is intended as a regression check, not a stable cross-machine
|
|
87
|
+
score. CI also exposes a manual `Benchmark` workflow.
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# Third-party notices
|
|
2
|
+
|
|
3
|
+
This package builds and distributes WebAssembly generated from Xiph.Org's
|
|
4
|
+
`libopus`.
|
|
5
|
+
|
|
6
|
+
## libopus
|
|
7
|
+
|
|
8
|
+
- Source: https://github.com/xiph/opus
|
|
9
|
+
- Release source: https://downloads.xiph.org/releases/opus/
|
|
10
|
+
- License: BSD-style license, see the upstream source archive for the full text.
|
|
11
|
+
|
|
12
|
+
The JavaScript and TypeScript wrapper code in this repository is MIT licensed.
|
|
Binary file
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
export declare const Application: {
|
|
2
|
+
readonly Voip: 2048;
|
|
3
|
+
readonly Audio: 2049;
|
|
4
|
+
readonly RestrictedLowDelay: 2051;
|
|
5
|
+
};
|
|
6
|
+
export type Application = (typeof Application)[keyof typeof Application];
|
|
7
|
+
export type CodecOptions = {
|
|
8
|
+
channels: 1 | 2;
|
|
9
|
+
sampleRate: 8000 | 12000 | 16000 | 24000 | 48000;
|
|
10
|
+
};
|
|
11
|
+
export type EncoderOptions = CodecOptions & {
|
|
12
|
+
application?: Application;
|
|
13
|
+
bitrate?: number;
|
|
14
|
+
fec?: boolean;
|
|
15
|
+
packetLossPercent?: number;
|
|
16
|
+
};
|
|
17
|
+
export type DecodeOptions = {
|
|
18
|
+
decodeFec?: boolean;
|
|
19
|
+
};
|
|
20
|
+
export type EncodeOptions = {
|
|
21
|
+
maxPacketBytes?: number;
|
|
22
|
+
};
|
|
23
|
+
export type OpusEncoder = {
|
|
24
|
+
readonly channels: 1 | 2;
|
|
25
|
+
readonly sampleRate: CodecOptions["sampleRate"];
|
|
26
|
+
encodePcm16(pcm: Int16Array | Uint8Array, frameSize: number, options?: EncodeOptions): Uint8Array;
|
|
27
|
+
free(): void;
|
|
28
|
+
setBitrate(bitrate: number): void;
|
|
29
|
+
setFec(enabled: boolean): void;
|
|
30
|
+
setPacketLossPercent(percentage: number): void;
|
|
31
|
+
};
|
|
32
|
+
export type OpusDecoder = {
|
|
33
|
+
readonly channels: 1 | 2;
|
|
34
|
+
readonly sampleRate: CodecOptions["sampleRate"];
|
|
35
|
+
decodeFrame(packet: Uint8Array, frameSize: number, options?: DecodeOptions): Int16Array;
|
|
36
|
+
free(): void;
|
|
37
|
+
};
|
|
38
|
+
export declare function loadLibopus(): Promise<{
|
|
39
|
+
version: string;
|
|
40
|
+
}>;
|
|
41
|
+
export declare function createEncoder(options: EncoderOptions): Promise<OpusEncoder>;
|
|
42
|
+
export declare function createDecoder(options: CodecOptions): Promise<OpusDecoder>;
|
|
43
|
+
export declare class OpusError extends Error {
|
|
44
|
+
readonly code: number;
|
|
45
|
+
constructor(code: number, message: string);
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,WAAW;;;;CAId,CAAC;AAEX,MAAM,MAAM,WAAW,GAAG,CAAC,OAAO,WAAW,CAAC,CAAC,MAAM,OAAO,WAAW,CAAC,CAAC;AAEzE,MAAM,MAAM,YAAY,GAAG;IACzB,QAAQ,EAAE,CAAC,GAAG,CAAC,CAAC;IAChB,UAAU,EAAE,IAAI,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,CAAC;CAClD,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG,YAAY,GAAG;IAC1C,WAAW,CAAC,EAAE,WAAW,CAAC;IAC1B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,GAAG,CAAC,EAAE,OAAO,CAAC;IACd,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC5B,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG;IAC1B,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG;IAC1B,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB,CAAC;AAEF,MAAM,MAAM,WAAW,GAAG;IACxB,QAAQ,CAAC,QAAQ,EAAE,CAAC,GAAG,CAAC,CAAC;IACzB,QAAQ,CAAC,UAAU,EAAE,YAAY,CAAC,YAAY,CAAC,CAAC;IAChD,WAAW,CAAC,GAAG,EAAE,UAAU,GAAG,UAAU,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,UAAU,CAAC;IAClG,IAAI,IAAI,IAAI,CAAC;IACb,UAAU,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IAClC,MAAM,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI,CAAC;IAC/B,oBAAoB,CAAC,UAAU,EAAE,MAAM,GAAG,IAAI,CAAC;CAChD,CAAC;AAEF,MAAM,MAAM,WAAW,GAAG;IACxB,QAAQ,CAAC,QAAQ,EAAE,CAAC,GAAG,CAAC,CAAC;IACzB,QAAQ,CAAC,UAAU,EAAE,YAAY,CAAC,YAAY,CAAC,CAAC;IAChD,WAAW,CAAC,MAAM,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,UAAU,CAAC;IACxF,IAAI,IAAI,IAAI,CAAC;CACd,CAAC;AAMF,wBAAsB,WAAW,IAAI,OAAO,CAAC;IAC3C,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC,CAGD;AAED,wBAAsB,aAAa,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,WAAW,CAAC,CAGjF;AAED,wBAAsB,aAAa,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,WAAW,CAAC,CAG/E;AA4LD,qBAAa,SAAU,SAAQ,KAAK;IAClC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;gBAEV,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;CAK1C"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import createLibopusModule from "./generated/libopus.generated.mjs";
|
|
2
|
+
export const Application = {
|
|
3
|
+
Voip: 2048,
|
|
4
|
+
Audio: 2049,
|
|
5
|
+
RestrictedLowDelay: 2051,
|
|
6
|
+
};
|
|
7
|
+
let modulePromise;
|
|
8
|
+
export async function loadLibopus() {
|
|
9
|
+
const module = await getModule();
|
|
10
|
+
return { version: module.UTF8ToString(module._oc_get_version_string()) };
|
|
11
|
+
}
|
|
12
|
+
export async function createEncoder(options) {
|
|
13
|
+
const module = await getModule();
|
|
14
|
+
return new WasmOpusEncoder(module, options);
|
|
15
|
+
}
|
|
16
|
+
export async function createDecoder(options) {
|
|
17
|
+
const module = await getModule();
|
|
18
|
+
return new WasmOpusDecoder(module, options);
|
|
19
|
+
}
|
|
20
|
+
class WasmOpusEncoder {
|
|
21
|
+
channels;
|
|
22
|
+
sampleRate;
|
|
23
|
+
#freed = false;
|
|
24
|
+
#module;
|
|
25
|
+
#ptr;
|
|
26
|
+
constructor(module, options) {
|
|
27
|
+
validateCodecOptions(options);
|
|
28
|
+
this.#module = module;
|
|
29
|
+
this.channels = options.channels;
|
|
30
|
+
this.sampleRate = options.sampleRate;
|
|
31
|
+
const errorPtr = module._malloc(4);
|
|
32
|
+
try {
|
|
33
|
+
const ptr = module._oc_create_encoder(options.sampleRate, options.channels, options.application ?? Application.Audio, errorPtr);
|
|
34
|
+
const error = module.HEAP16[errorPtr >> 1] ?? 0;
|
|
35
|
+
if (!ptr || error !== 0) {
|
|
36
|
+
throw new OpusError(error, formatOpusError(module, error));
|
|
37
|
+
}
|
|
38
|
+
this.#ptr = ptr;
|
|
39
|
+
}
|
|
40
|
+
finally {
|
|
41
|
+
module._free(errorPtr);
|
|
42
|
+
}
|
|
43
|
+
if (options.bitrate !== undefined) {
|
|
44
|
+
this.setBitrate(options.bitrate);
|
|
45
|
+
}
|
|
46
|
+
if (options.fec !== undefined) {
|
|
47
|
+
this.setFec(options.fec);
|
|
48
|
+
}
|
|
49
|
+
if (options.packetLossPercent !== undefined) {
|
|
50
|
+
this.setPacketLossPercent(options.packetLossPercent);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
encodePcm16(pcm, frameSize, options = {}) {
|
|
54
|
+
this.#assertLive();
|
|
55
|
+
validateFrameSize(frameSize);
|
|
56
|
+
const pcmBytes = toUint8Array(pcm);
|
|
57
|
+
const expectedBytes = frameSize * this.channels * 2;
|
|
58
|
+
if (pcmBytes.byteLength !== expectedBytes) {
|
|
59
|
+
throw new RangeError(`PCM frame has ${pcmBytes.byteLength} bytes; expected ${expectedBytes} for ${frameSize} samples and ${this.channels} channel(s)`);
|
|
60
|
+
}
|
|
61
|
+
const maxPacketBytes = options.maxPacketBytes ?? 4000;
|
|
62
|
+
if (!Number.isInteger(maxPacketBytes) || maxPacketBytes <= 0) {
|
|
63
|
+
throw new RangeError("maxPacketBytes must be a positive integer");
|
|
64
|
+
}
|
|
65
|
+
const pcmPtr = this.#module._malloc(pcmBytes.byteLength);
|
|
66
|
+
const packetPtr = this.#module._malloc(maxPacketBytes);
|
|
67
|
+
try {
|
|
68
|
+
this.#module.HEAPU8.set(pcmBytes, pcmPtr);
|
|
69
|
+
const encodedBytes = this.#module._oc_encode(this.#ptr, pcmPtr, frameSize, packetPtr, maxPacketBytes);
|
|
70
|
+
if (encodedBytes < 0) {
|
|
71
|
+
throw new OpusError(encodedBytes, formatOpusError(this.#module, encodedBytes));
|
|
72
|
+
}
|
|
73
|
+
return this.#module.HEAPU8.slice(packetPtr, packetPtr + encodedBytes);
|
|
74
|
+
}
|
|
75
|
+
finally {
|
|
76
|
+
this.#module._free(packetPtr);
|
|
77
|
+
this.#module._free(pcmPtr);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
setBitrate(bitrate) {
|
|
81
|
+
this.#assertLive();
|
|
82
|
+
if (!Number.isInteger(bitrate) || bitrate <= 0) {
|
|
83
|
+
throw new RangeError("bitrate must be a positive integer");
|
|
84
|
+
}
|
|
85
|
+
this.#checkCtl(this.#module._oc_encoder_ctl_set_bitrate(this.#ptr, bitrate));
|
|
86
|
+
}
|
|
87
|
+
setFec(enabled) {
|
|
88
|
+
this.#assertLive();
|
|
89
|
+
this.#checkCtl(this.#module._oc_encoder_ctl_set_fec(this.#ptr, enabled ? 1 : 0));
|
|
90
|
+
}
|
|
91
|
+
setPacketLossPercent(percentage) {
|
|
92
|
+
this.#assertLive();
|
|
93
|
+
if (!Number.isInteger(percentage) || percentage < 0 || percentage > 100) {
|
|
94
|
+
throw new RangeError("packetLossPercent must be an integer from 0 to 100");
|
|
95
|
+
}
|
|
96
|
+
this.#checkCtl(this.#module._oc_encoder_ctl_set_packet_loss_percent(this.#ptr, percentage));
|
|
97
|
+
}
|
|
98
|
+
free() {
|
|
99
|
+
if (this.#freed) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
this.#module._oc_destroy_encoder(this.#ptr);
|
|
103
|
+
this.#freed = true;
|
|
104
|
+
}
|
|
105
|
+
#assertLive() {
|
|
106
|
+
if (this.#freed) {
|
|
107
|
+
throw new Error("OpusEncoder has been freed");
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
#checkCtl(code) {
|
|
111
|
+
if (code < 0) {
|
|
112
|
+
throw new OpusError(code, formatOpusError(this.#module, code));
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
class WasmOpusDecoder {
|
|
117
|
+
channels;
|
|
118
|
+
sampleRate;
|
|
119
|
+
#freed = false;
|
|
120
|
+
#module;
|
|
121
|
+
#ptr;
|
|
122
|
+
constructor(module, options) {
|
|
123
|
+
validateCodecOptions(options);
|
|
124
|
+
this.#module = module;
|
|
125
|
+
this.channels = options.channels;
|
|
126
|
+
this.sampleRate = options.sampleRate;
|
|
127
|
+
const errorPtr = module._malloc(4);
|
|
128
|
+
try {
|
|
129
|
+
const ptr = module._oc_create_decoder(options.sampleRate, options.channels, errorPtr);
|
|
130
|
+
const error = module.HEAP16[errorPtr >> 1] ?? 0;
|
|
131
|
+
if (!ptr || error !== 0) {
|
|
132
|
+
throw new OpusError(error, formatOpusError(module, error));
|
|
133
|
+
}
|
|
134
|
+
this.#ptr = ptr;
|
|
135
|
+
}
|
|
136
|
+
finally {
|
|
137
|
+
module._free(errorPtr);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
decodeFrame(packet, frameSize, options = {}) {
|
|
141
|
+
this.#assertLive();
|
|
142
|
+
validateFrameSize(frameSize);
|
|
143
|
+
if (packet.byteLength === 0) {
|
|
144
|
+
throw new RangeError("packet must not be empty");
|
|
145
|
+
}
|
|
146
|
+
const packetPtr = this.#module._malloc(packet.byteLength);
|
|
147
|
+
const pcmBytes = frameSize * this.channels * 2;
|
|
148
|
+
const pcmPtr = this.#module._malloc(pcmBytes);
|
|
149
|
+
try {
|
|
150
|
+
this.#module.HEAPU8.set(packet, packetPtr);
|
|
151
|
+
const decodedSamples = this.#module._oc_decode(this.#ptr, packetPtr, packet.byteLength, pcmPtr, frameSize, options.decodeFec ? 1 : 0);
|
|
152
|
+
if (decodedSamples < 0) {
|
|
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);
|
|
157
|
+
}
|
|
158
|
+
finally {
|
|
159
|
+
this.#module._free(pcmPtr);
|
|
160
|
+
this.#module._free(packetPtr);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
free() {
|
|
164
|
+
if (this.#freed) {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
this.#module._oc_destroy_decoder(this.#ptr);
|
|
168
|
+
this.#freed = true;
|
|
169
|
+
}
|
|
170
|
+
#assertLive() {
|
|
171
|
+
if (this.#freed) {
|
|
172
|
+
throw new Error("OpusDecoder has been freed");
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
export class OpusError extends Error {
|
|
177
|
+
code;
|
|
178
|
+
constructor(code, message) {
|
|
179
|
+
super(message);
|
|
180
|
+
this.name = "OpusError";
|
|
181
|
+
this.code = code;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
async function getModule() {
|
|
185
|
+
modulePromise ??= createLibopusModule();
|
|
186
|
+
return await modulePromise;
|
|
187
|
+
}
|
|
188
|
+
function formatOpusError(module, code) {
|
|
189
|
+
const message = module.UTF8ToString(module._oc_strerror(code));
|
|
190
|
+
return `libopus error ${code}: ${message}`;
|
|
191
|
+
}
|
|
192
|
+
function toUint8Array(input) {
|
|
193
|
+
return input instanceof Uint8Array
|
|
194
|
+
? input
|
|
195
|
+
: new Uint8Array(input.buffer, input.byteOffset, input.byteLength);
|
|
196
|
+
}
|
|
197
|
+
function validateCodecOptions(options) {
|
|
198
|
+
if (![8000, 12000, 16000, 24000, 48000].includes(options.sampleRate)) {
|
|
199
|
+
throw new RangeError("sampleRate must be 8000, 12000, 16000, 24000, or 48000");
|
|
200
|
+
}
|
|
201
|
+
if (options.channels !== 1 && options.channels !== 2) {
|
|
202
|
+
throw new RangeError("channels must be 1 or 2");
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
function validateFrameSize(frameSize) {
|
|
206
|
+
if (!Number.isInteger(frameSize) || frameSize <= 0 || frameSize > 5760) {
|
|
207
|
+
throw new RangeError("frameSize must be an integer from 1 to 5760");
|
|
208
|
+
}
|
|
209
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "libopus-wasm",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Small, modern WASM bindings for libopus raw packet encode/decode.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/openclaw/libopus-wasm.git"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/openclaw/libopus-wasm#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/openclaw/libopus-wasm/issues"
|
|
14
|
+
},
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"import": "./dist/index.js"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist",
|
|
23
|
+
"LICENSE",
|
|
24
|
+
"README.md",
|
|
25
|
+
"THIRD_PARTY_NOTICES.md"
|
|
26
|
+
],
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "pnpm build:wasm && tsc -p tsconfig.json && node scripts/copy-generated.mjs",
|
|
29
|
+
"build:wasm": "node scripts/build-libopus-wasm.mjs",
|
|
30
|
+
"benchmark": "pnpm build && node scripts/benchmark-native.mjs",
|
|
31
|
+
"clean": "rm -rf dist .cache",
|
|
32
|
+
"prepack": "pnpm build",
|
|
33
|
+
"test": "pnpm build:wasm && vitest run",
|
|
34
|
+
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@discordjs/opus": "0.10.0",
|
|
38
|
+
"@types/node": "24.10.2",
|
|
39
|
+
"typescript": "5.9.3",
|
|
40
|
+
"vitest": "4.0.14"
|
|
41
|
+
},
|
|
42
|
+
"engines": {
|
|
43
|
+
"node": ">=20"
|
|
44
|
+
}
|
|
45
|
+
}
|