-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
opus.ts
171 lines (146 loc) Β· 4.19 KB
/
opus.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import _opus from "./pack/opuswasm.js";
// deno-lint-ignore no-explicit-any
let opus: any = null;
/** Opus application type */
export enum OpusApplication {
/** Voice Over IP */
VOIP = 2048,
/** Audio */
AUDIO = 2049,
/** Restricted Low-Delay */
RESTRICTED_LOWDELAY = 2051,
}
export enum OpusError {
"OK" = 0,
"Bad argument" = -1,
"Buffer too small" = -2,
"Internal error" = -3,
"Invalid packet" = -4,
"Unimplemented" = -5,
"Invalid state" = -6,
"Memory allocation fail" = -7,
}
/** Valid audio sampling rates */
export const VALID_SAMPLING_RATES = [8000, 12000, 16000, 24000, 48000];
/** Maximum bytes in a frame */
export const MAX_FRAME_SIZE = 2880;
/** Maximum bytes in a packet */
export const MAX_PACKET_SIZE = 3828;
export class Opus {
samplingRate: number;
channels: number;
application: OpusApplication;
// deno-lint-ignore no-explicit-any
handler: any;
inPCMLength: number;
inPCMPointer: number;
inPCM: Uint16Array;
outPCMLength: number;
outPCMPointer: number;
outPCM: Uint16Array;
inOpusPointer: number;
inOpus: Uint8Array;
outOpusPointer: number;
outOpus: Uint8Array;
/** Loads opus wasm instance */
static async load() {
if (!opus) {
opus = await _opus;
}
}
/** Create a new Opus en/decoder */
constructor(
samplingRate: number,
channels?: number,
application?: OpusApplication,
) {
if (!opus) {
throw new Error(
"Opus.load() needs to be called before interacting with the Opus class",
);
}
if (!~VALID_SAMPLING_RATES.indexOf(samplingRate)) {
throw new RangeError(`${samplingRate} is an invalid sampling rate.`);
}
this.samplingRate = samplingRate;
this.channels = channels || 1;
this.application = application || OpusApplication.AUDIO;
let handler = null;
handler = opus;
this.handler = new handler.OpusScriptHandler(
this.samplingRate,
this.channels,
this.application,
);
this.inPCMLength = MAX_FRAME_SIZE * this.channels * 2;
this.inPCMPointer = handler._malloc(this.inPCMLength);
this.inPCM = handler.HEAPU16.subarray(
this.inPCMPointer,
this.inPCMPointer + this.inPCMLength,
);
this.inOpusPointer = handler._malloc(MAX_PACKET_SIZE);
this.inOpus = handler.HEAPU8.subarray(
this.inOpusPointer,
this.inOpusPointer + MAX_PACKET_SIZE,
);
this.outOpusPointer = handler._malloc(MAX_PACKET_SIZE);
this.outOpus = handler.HEAPU8.subarray(
this.outOpusPointer,
this.outOpusPointer + MAX_PACKET_SIZE,
);
this.outPCMLength = MAX_FRAME_SIZE * this.channels * 2;
this.outPCMPointer = handler._malloc(this.outPCMLength);
this.outPCM = handler.HEAPU16.subarray(
this.outPCMPointer,
this.outPCMPointer + this.outPCMLength,
);
}
/** Encode a buffer into Opus */
encode(buffer: Uint8Array, frameSize: number): Uint8Array {
this.inPCM.set(buffer);
let len = this.handler._encode(
this.inPCM.byteOffset,
buffer.length,
this.outOpusPointer,
frameSize,
);
if (len < 0) {
throw new Error("Encode error: " + OpusError[len]);
}
return this.outOpus.subarray(0, len);
}
/** Decode an opus buffer */
decode(buffer: Uint8Array): Uint16Array {
this.inOpus.set(buffer);
let len: number = this.handler._decode(
this.inOpusPointer,
buffer.length,
this.outPCM.byteOffset,
);
if (len < 0) {
throw new Error("Decode error: " + OpusError[len]);
}
return this.outPCM.subarray(0, len * this.channels * 2);
}
encoderCTL(ctl: number, arg: number): void {
let len = this.handler._encoder_ctl(ctl, arg);
if (len < 0) {
throw new Error("Encoder CTL error: " + OpusError[len]);
}
}
decoderCTL(ctl: number, arg: number): void {
let len = this.handler._decoder_ctl(ctl, arg);
if (len < 0) {
throw new Error("Decoder CTL error: " + OpusError[len]);
}
}
/** Delete the opus object */
delete(): void {
let handler = opus;
handler.OpusScriptHandler.destroy_handler(this.handler);
handler._free(this.inPCMPointer);
handler._free(this.inOpusPointer);
handler._free(this.outOpusPointer);
handler._free(this.outPCMPointer);
}
}