@harmonia-audio/codec 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +526 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +425 -0
- package/dist/index.d.ts +425 -0
- package/dist/index.js +512 -0
- package/dist/index.js.map +1 -0
- package/package.json +43 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,526 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var native = require('@harmonia-audio/native');
|
|
4
|
+
var stream = require('stream');
|
|
5
|
+
|
|
6
|
+
// src/encoder.ts
|
|
7
|
+
|
|
8
|
+
// src/errors.ts
|
|
9
|
+
var HarmoniaCodecError = class extends Error {
|
|
10
|
+
/**
|
|
11
|
+
* Machine-readable error code.
|
|
12
|
+
*/
|
|
13
|
+
code;
|
|
14
|
+
/**
|
|
15
|
+
* Additional context about the error.
|
|
16
|
+
*/
|
|
17
|
+
context;
|
|
18
|
+
constructor(code, message, context = {}) {
|
|
19
|
+
super(message);
|
|
20
|
+
this.name = "HarmoniaCodecError";
|
|
21
|
+
this.code = code;
|
|
22
|
+
this.context = context;
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
// src/encoder.ts
|
|
27
|
+
var APPLICATION_MAP = {
|
|
28
|
+
voip: "Voip",
|
|
29
|
+
audio: "Audio",
|
|
30
|
+
restricted_lowdelay: "RestrictedLowDelay"
|
|
31
|
+
};
|
|
32
|
+
var OpusEncoder = class {
|
|
33
|
+
native;
|
|
34
|
+
_sampleRate;
|
|
35
|
+
_channels;
|
|
36
|
+
_application;
|
|
37
|
+
constructor(options) {
|
|
38
|
+
this._sampleRate = options.sampleRate;
|
|
39
|
+
this._channels = options.channels;
|
|
40
|
+
this._application = options.application;
|
|
41
|
+
try {
|
|
42
|
+
this.native = new native.OpusEncoder(
|
|
43
|
+
options.sampleRate,
|
|
44
|
+
options.channels,
|
|
45
|
+
APPLICATION_MAP[options.application]
|
|
46
|
+
);
|
|
47
|
+
} catch (error) {
|
|
48
|
+
throw new HarmoniaCodecError(
|
|
49
|
+
"ENCODER_INIT_FAILED",
|
|
50
|
+
`Failed to create Opus encoder: ${error instanceof Error ? error.message : String(error)}`,
|
|
51
|
+
{ sampleRate: options.sampleRate, channels: options.channels }
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
if (options.bitrate !== void 0) {
|
|
55
|
+
this.setBitrate(options.bitrate);
|
|
56
|
+
}
|
|
57
|
+
if (options.complexity !== void 0) {
|
|
58
|
+
this.setComplexity(options.complexity);
|
|
59
|
+
}
|
|
60
|
+
if (options.fec !== void 0) {
|
|
61
|
+
this.setFec(options.fec);
|
|
62
|
+
}
|
|
63
|
+
if (options.packetLossPercent !== void 0) {
|
|
64
|
+
this.setPacketLossPercent(options.packetLossPercent);
|
|
65
|
+
}
|
|
66
|
+
if (options.dtx !== void 0) {
|
|
67
|
+
this.setDtx(options.dtx);
|
|
68
|
+
}
|
|
69
|
+
if (options.vbr !== void 0) {
|
|
70
|
+
this.setVbr(options.vbr);
|
|
71
|
+
}
|
|
72
|
+
if (options.signal !== void 0) {
|
|
73
|
+
this.setSignal(options.signal);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Encode 16-bit PCM audio to Opus.
|
|
78
|
+
*
|
|
79
|
+
* @param pcm - Buffer containing interleaved 16-bit signed integer PCM samples
|
|
80
|
+
* @returns Encoded Opus packet
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```ts
|
|
84
|
+
* const opus = encoder.encode(pcmBuffer);
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
encode(pcm) {
|
|
88
|
+
try {
|
|
89
|
+
return this.native.encode(pcm);
|
|
90
|
+
} catch (error) {
|
|
91
|
+
throw new HarmoniaCodecError(
|
|
92
|
+
"ENCODE_FAILED",
|
|
93
|
+
`Opus encode failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
94
|
+
{ inputSize: pcm.length }
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Encode 32-bit float PCM audio to Opus.
|
|
100
|
+
*
|
|
101
|
+
* @param pcm - Buffer containing interleaved 32-bit float PCM samples
|
|
102
|
+
* @returns Encoded Opus packet
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```ts
|
|
106
|
+
* const opus = encoder.encodeFloat(floatPcmBuffer);
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
encodeFloat(pcm) {
|
|
110
|
+
try {
|
|
111
|
+
return this.native.encodeFloat(pcm);
|
|
112
|
+
} catch (error) {
|
|
113
|
+
throw new HarmoniaCodecError(
|
|
114
|
+
"ENCODE_FLOAT_FAILED",
|
|
115
|
+
`Opus float encode failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
116
|
+
{ inputSize: pcm.length }
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Set the encoder bitrate in bits per second.
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* ```ts
|
|
125
|
+
* encoder.setBitrate(128000);
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
128
|
+
setBitrate(bitrate) {
|
|
129
|
+
this.native.setBitrate(bitrate);
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Get the current encoder bitrate.
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* ```ts
|
|
136
|
+
* const bitrate = encoder.getBitrate();
|
|
137
|
+
* ```
|
|
138
|
+
*/
|
|
139
|
+
getBitrate() {
|
|
140
|
+
return this.native.getBitrate();
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Set encoding complexity (0-10, higher = better quality but slower).
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* ```ts
|
|
147
|
+
* encoder.setComplexity(10);
|
|
148
|
+
* ```
|
|
149
|
+
*/
|
|
150
|
+
setComplexity(complexity) {
|
|
151
|
+
this.native.setComplexity(complexity);
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Get the current encoding complexity.
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* ```ts
|
|
158
|
+
* const complexity = encoder.getComplexity();
|
|
159
|
+
* ```
|
|
160
|
+
*/
|
|
161
|
+
getComplexity() {
|
|
162
|
+
return this.native.getComplexity();
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Enable or disable forward error correction.
|
|
166
|
+
*
|
|
167
|
+
* @example
|
|
168
|
+
* ```ts
|
|
169
|
+
* encoder.setFec(true);
|
|
170
|
+
* ```
|
|
171
|
+
*/
|
|
172
|
+
setFec(enabled) {
|
|
173
|
+
this.native.setFec(enabled);
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Check if FEC is enabled.
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
179
|
+
* ```ts
|
|
180
|
+
* const fecEnabled = encoder.getFec();
|
|
181
|
+
* ```
|
|
182
|
+
*/
|
|
183
|
+
getFec() {
|
|
184
|
+
return this.native.getFec();
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Set expected packet loss percentage for FEC optimization.
|
|
188
|
+
*
|
|
189
|
+
* @example
|
|
190
|
+
* ```ts
|
|
191
|
+
* encoder.setPacketLossPercent(10);
|
|
192
|
+
* ```
|
|
193
|
+
*/
|
|
194
|
+
setPacketLossPercent(percent) {
|
|
195
|
+
this.native.setPacketLossPercent(percent);
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Enable or disable discontinuous transmission.
|
|
199
|
+
*
|
|
200
|
+
* @example
|
|
201
|
+
* ```ts
|
|
202
|
+
* encoder.setDtx(true);
|
|
203
|
+
* ```
|
|
204
|
+
*/
|
|
205
|
+
setDtx(enabled) {
|
|
206
|
+
this.native.setDtx(enabled);
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Enable or disable variable bitrate.
|
|
210
|
+
*
|
|
211
|
+
* @example
|
|
212
|
+
* ```ts
|
|
213
|
+
* encoder.setVbr(true);
|
|
214
|
+
* ```
|
|
215
|
+
*/
|
|
216
|
+
setVbr(enabled) {
|
|
217
|
+
this.native.setVbr(enabled);
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Set the signal type hint.
|
|
221
|
+
*
|
|
222
|
+
* @example
|
|
223
|
+
* ```ts
|
|
224
|
+
* encoder.setSignal('music');
|
|
225
|
+
* ```
|
|
226
|
+
*/
|
|
227
|
+
setSignal(signal) {
|
|
228
|
+
this.native.setSignal(signal);
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Reset the encoder state.
|
|
232
|
+
*
|
|
233
|
+
* @example
|
|
234
|
+
* ```ts
|
|
235
|
+
* encoder.reset();
|
|
236
|
+
* ```
|
|
237
|
+
*/
|
|
238
|
+
reset() {
|
|
239
|
+
this.native.reset();
|
|
240
|
+
}
|
|
241
|
+
/** The sample rate this encoder operates at. */
|
|
242
|
+
get sampleRate() {
|
|
243
|
+
return this._sampleRate;
|
|
244
|
+
}
|
|
245
|
+
/** The number of channels this encoder operates with. */
|
|
246
|
+
get channels() {
|
|
247
|
+
return this._channels;
|
|
248
|
+
}
|
|
249
|
+
/** The application mode this encoder was configured with. */
|
|
250
|
+
get application() {
|
|
251
|
+
return this._application;
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
var OpusDecoder = class {
|
|
255
|
+
native;
|
|
256
|
+
_sampleRate;
|
|
257
|
+
_channels;
|
|
258
|
+
constructor(options) {
|
|
259
|
+
this._sampleRate = options.sampleRate;
|
|
260
|
+
this._channels = options.channels;
|
|
261
|
+
try {
|
|
262
|
+
this.native = new native.OpusDecoder(options.sampleRate, options.channels);
|
|
263
|
+
} catch (error) {
|
|
264
|
+
throw new HarmoniaCodecError(
|
|
265
|
+
"DECODER_INIT_FAILED",
|
|
266
|
+
`Failed to create Opus decoder: ${error instanceof Error ? error.message : String(error)}`,
|
|
267
|
+
{ sampleRate: options.sampleRate, channels: options.channels }
|
|
268
|
+
);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Decode an Opus packet to 16-bit PCM.
|
|
273
|
+
*
|
|
274
|
+
* @param opusData - The Opus-encoded packet
|
|
275
|
+
* @returns Buffer containing decoded 16-bit PCM samples
|
|
276
|
+
*
|
|
277
|
+
* @example
|
|
278
|
+
* ```ts
|
|
279
|
+
* const pcm = decoder.decode(opusPacket);
|
|
280
|
+
* ```
|
|
281
|
+
*/
|
|
282
|
+
decode(opusData) {
|
|
283
|
+
try {
|
|
284
|
+
return this.native.decode(opusData);
|
|
285
|
+
} catch (error) {
|
|
286
|
+
throw new HarmoniaCodecError(
|
|
287
|
+
"DECODE_FAILED",
|
|
288
|
+
`Opus decode failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
289
|
+
{ inputSize: opusData.length }
|
|
290
|
+
);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Decode an Opus packet to 32-bit float PCM.
|
|
295
|
+
*
|
|
296
|
+
* @param opusData - The Opus-encoded packet
|
|
297
|
+
* @returns Buffer containing decoded 32-bit float PCM samples
|
|
298
|
+
*
|
|
299
|
+
* @example
|
|
300
|
+
* ```ts
|
|
301
|
+
* const pcm = decoder.decodeFloat(opusPacket);
|
|
302
|
+
* ```
|
|
303
|
+
*/
|
|
304
|
+
decodeFloat(opusData) {
|
|
305
|
+
try {
|
|
306
|
+
return this.native.decodeFloat(opusData);
|
|
307
|
+
} catch (error) {
|
|
308
|
+
throw new HarmoniaCodecError(
|
|
309
|
+
"DECODE_FLOAT_FAILED",
|
|
310
|
+
`Opus float decode failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
311
|
+
{ inputSize: opusData.length }
|
|
312
|
+
);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Decode with forward error correction from a previous packet.
|
|
317
|
+
*
|
|
318
|
+
* @param opusData - The Opus packet to attempt FEC recovery from
|
|
319
|
+
* @param frameSize - Number of samples per channel expected
|
|
320
|
+
* @returns Buffer containing recovered PCM
|
|
321
|
+
*
|
|
322
|
+
* @example
|
|
323
|
+
* ```ts
|
|
324
|
+
* const recovered = decoder.decodeFec(previousPacket, 960);
|
|
325
|
+
* ```
|
|
326
|
+
*/
|
|
327
|
+
decodeFec(opusData, frameSize) {
|
|
328
|
+
try {
|
|
329
|
+
return this.native.decodeFec(opusData, frameSize);
|
|
330
|
+
} catch (error) {
|
|
331
|
+
throw new HarmoniaCodecError(
|
|
332
|
+
"DECODE_FEC_FAILED",
|
|
333
|
+
`Opus FEC decode failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
334
|
+
{ inputSize: opusData.length, frameSize }
|
|
335
|
+
);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* Perform packet loss concealment (no input packet available).
|
|
340
|
+
*
|
|
341
|
+
* @param frameSize - Number of samples per channel to generate
|
|
342
|
+
* @returns Buffer containing concealed PCM
|
|
343
|
+
*
|
|
344
|
+
* @example
|
|
345
|
+
* ```ts
|
|
346
|
+
* const concealed = decoder.decodePlc(960);
|
|
347
|
+
* ```
|
|
348
|
+
*/
|
|
349
|
+
decodePlc(frameSize) {
|
|
350
|
+
try {
|
|
351
|
+
return this.native.decodePlc(frameSize);
|
|
352
|
+
} catch (error) {
|
|
353
|
+
throw new HarmoniaCodecError(
|
|
354
|
+
"PLC_FAILED",
|
|
355
|
+
`Opus PLC failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
356
|
+
{ frameSize }
|
|
357
|
+
);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* Reset the decoder state.
|
|
362
|
+
*
|
|
363
|
+
* @example
|
|
364
|
+
* ```ts
|
|
365
|
+
* decoder.reset();
|
|
366
|
+
* ```
|
|
367
|
+
*/
|
|
368
|
+
reset() {
|
|
369
|
+
this.native.reset();
|
|
370
|
+
}
|
|
371
|
+
/** The sample rate this decoder operates at. */
|
|
372
|
+
get sampleRate() {
|
|
373
|
+
return this._sampleRate;
|
|
374
|
+
}
|
|
375
|
+
/** The number of channels this decoder produces. */
|
|
376
|
+
get channels() {
|
|
377
|
+
return this._channels;
|
|
378
|
+
}
|
|
379
|
+
};
|
|
380
|
+
|
|
381
|
+
// src/constants.ts
|
|
382
|
+
var OPUS_SAMPLE_RATES = [8e3, 12e3, 16e3, 24e3, 48e3];
|
|
383
|
+
var OPUS_CHANNELS = [1, 2];
|
|
384
|
+
var OPUS_FRAME_DURATIONS = [2.5, 5, 10, 20, 40, 60];
|
|
385
|
+
var DISCORD_OPUS_SAMPLE_RATE = 48e3;
|
|
386
|
+
var DISCORD_OPUS_CHANNELS = 2;
|
|
387
|
+
var DISCORD_OPUS_FRAME_DURATION = 20;
|
|
388
|
+
var DISCORD_OPUS_FRAME_SIZE = 960;
|
|
389
|
+
|
|
390
|
+
// src/streams.ts
|
|
391
|
+
var OpusEncoderStream = class extends stream.Transform {
|
|
392
|
+
encoder;
|
|
393
|
+
frameSize;
|
|
394
|
+
frameSizeBytes;
|
|
395
|
+
pendingBuffer;
|
|
396
|
+
constructor(options) {
|
|
397
|
+
super({
|
|
398
|
+
readableObjectMode: true
|
|
399
|
+
});
|
|
400
|
+
this.encoder = new OpusEncoder(options);
|
|
401
|
+
this.frameSize = DISCORD_OPUS_FRAME_SIZE;
|
|
402
|
+
this.frameSizeBytes = this.frameSize * options.channels * 2;
|
|
403
|
+
this.pendingBuffer = Buffer.alloc(0);
|
|
404
|
+
}
|
|
405
|
+
_transform(chunk, _encoding, callback) {
|
|
406
|
+
this.pendingBuffer = Buffer.concat([this.pendingBuffer, chunk]);
|
|
407
|
+
while (this.pendingBuffer.length >= this.frameSizeBytes) {
|
|
408
|
+
const frame = this.pendingBuffer.subarray(0, this.frameSizeBytes);
|
|
409
|
+
this.pendingBuffer = this.pendingBuffer.subarray(this.frameSizeBytes);
|
|
410
|
+
try {
|
|
411
|
+
const encoded = this.encoder.encode(frame);
|
|
412
|
+
this.push(encoded);
|
|
413
|
+
} catch (error) {
|
|
414
|
+
callback(error instanceof Error ? error : new Error(String(error)));
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
callback();
|
|
419
|
+
}
|
|
420
|
+
_flush(callback) {
|
|
421
|
+
if (this.pendingBuffer.length > 0) {
|
|
422
|
+
const padded = Buffer.alloc(this.frameSizeBytes);
|
|
423
|
+
this.pendingBuffer.copy(padded);
|
|
424
|
+
try {
|
|
425
|
+
const encoded = this.encoder.encode(padded);
|
|
426
|
+
this.push(encoded);
|
|
427
|
+
} catch (error) {
|
|
428
|
+
callback(error instanceof Error ? error : new Error(String(error)));
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
callback();
|
|
433
|
+
}
|
|
434
|
+
};
|
|
435
|
+
var OpusDecoderStream = class extends stream.Transform {
|
|
436
|
+
decoder;
|
|
437
|
+
constructor(options) {
|
|
438
|
+
super({
|
|
439
|
+
writableObjectMode: true
|
|
440
|
+
});
|
|
441
|
+
this.decoder = new OpusDecoder(options);
|
|
442
|
+
}
|
|
443
|
+
_transform(chunk, _encoding, callback) {
|
|
444
|
+
try {
|
|
445
|
+
const decoded = this.decoder.decode(chunk);
|
|
446
|
+
this.push(decoded);
|
|
447
|
+
callback();
|
|
448
|
+
} catch (error) {
|
|
449
|
+
callback(error instanceof Error ? error : new Error(String(error)));
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
};
|
|
453
|
+
var OpusPacket = class {
|
|
454
|
+
/**
|
|
455
|
+
* Get the number of channels from an Opus packet header.
|
|
456
|
+
*
|
|
457
|
+
* @example
|
|
458
|
+
* ```ts
|
|
459
|
+
* const channels = OpusPacket.getChannels(opusPacket);
|
|
460
|
+
* ```
|
|
461
|
+
*/
|
|
462
|
+
static getChannels(packet) {
|
|
463
|
+
try {
|
|
464
|
+
return native.opusPacketGetNbChannelsNative(packet);
|
|
465
|
+
} catch (error) {
|
|
466
|
+
throw new HarmoniaCodecError(
|
|
467
|
+
"PACKET_INSPECT_FAILED",
|
|
468
|
+
`Failed to get channels: ${error instanceof Error ? error.message : String(error)}`
|
|
469
|
+
);
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
/**
|
|
473
|
+
* Get the number of frames in an Opus packet.
|
|
474
|
+
*
|
|
475
|
+
* @example
|
|
476
|
+
* ```ts
|
|
477
|
+
* const frameCount = OpusPacket.getFrameCount(opusPacket);
|
|
478
|
+
* ```
|
|
479
|
+
*/
|
|
480
|
+
static getFrameCount(packet) {
|
|
481
|
+
try {
|
|
482
|
+
return native.opusPacketGetNbFramesNative(packet);
|
|
483
|
+
} catch (error) {
|
|
484
|
+
throw new HarmoniaCodecError(
|
|
485
|
+
"PACKET_INSPECT_FAILED",
|
|
486
|
+
`Failed to get frame count: ${error instanceof Error ? error.message : String(error)}`
|
|
487
|
+
);
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
/**
|
|
491
|
+
* Get samples per frame for a given sample rate.
|
|
492
|
+
*
|
|
493
|
+
* @example
|
|
494
|
+
* ```ts
|
|
495
|
+
* const spf = OpusPacket.getSamplesPerFrame(opusPacket, 48000);
|
|
496
|
+
* ```
|
|
497
|
+
*/
|
|
498
|
+
static getSamplesPerFrame(packet, sampleRate) {
|
|
499
|
+
try {
|
|
500
|
+
return native.opusPacketGetSamplesPerFrameNative(packet, sampleRate);
|
|
501
|
+
} catch (error) {
|
|
502
|
+
throw new HarmoniaCodecError(
|
|
503
|
+
"PACKET_INSPECT_FAILED",
|
|
504
|
+
`Failed to get samples per frame: ${error instanceof Error ? error.message : String(error)}`
|
|
505
|
+
);
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
constructor() {
|
|
509
|
+
}
|
|
510
|
+
};
|
|
511
|
+
|
|
512
|
+
exports.DISCORD_OPUS_CHANNELS = DISCORD_OPUS_CHANNELS;
|
|
513
|
+
exports.DISCORD_OPUS_FRAME_DURATION = DISCORD_OPUS_FRAME_DURATION;
|
|
514
|
+
exports.DISCORD_OPUS_FRAME_SIZE = DISCORD_OPUS_FRAME_SIZE;
|
|
515
|
+
exports.DISCORD_OPUS_SAMPLE_RATE = DISCORD_OPUS_SAMPLE_RATE;
|
|
516
|
+
exports.HarmoniaCodecError = HarmoniaCodecError;
|
|
517
|
+
exports.OPUS_CHANNELS = OPUS_CHANNELS;
|
|
518
|
+
exports.OPUS_FRAME_DURATIONS = OPUS_FRAME_DURATIONS;
|
|
519
|
+
exports.OPUS_SAMPLE_RATES = OPUS_SAMPLE_RATES;
|
|
520
|
+
exports.OpusDecoder = OpusDecoder;
|
|
521
|
+
exports.OpusDecoderStream = OpusDecoderStream;
|
|
522
|
+
exports.OpusEncoder = OpusEncoder;
|
|
523
|
+
exports.OpusEncoderStream = OpusEncoderStream;
|
|
524
|
+
exports.OpusPacket = OpusPacket;
|
|
525
|
+
//# sourceMappingURL=index.cjs.map
|
|
526
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/errors.ts","../src/encoder.ts","../src/decoder.ts","../src/constants.ts","../src/streams.ts","../src/packet.ts"],"names":["NativeOpusEncoder","NativeOpusDecoder","Transform","opusPacketGetNbChannelsNative","opusPacketGetNbFramesNative","opusPacketGetSamplesPerFrameNative"],"mappings":";;;;;;;;AAcO,IAAM,kBAAA,GAAN,cAAiC,KAAA,CAAM;AAAA;AAAA;AAAA;AAAA,EAI7B,IAAA;AAAA;AAAA;AAAA;AAAA,EAKA,OAAA;AAAA,EAEhB,WAAA,CACC,IAAA,EACA,OAAA,EACA,OAAA,GAAmC,EAAC,EACnC;AACD,IAAA,KAAA,CAAM,OAAO,CAAA;AACb,IAAA,IAAA,CAAK,IAAA,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3F;AAAA,IACD;AAAA,EACD;AAAA,EAEQ,WAAA,GAAc;AAAA,EAEtB;AACD","file":"index.cjs","sourcesContent":["/**\n * Error thrown by Opus codec operations.\n *\n * @example\n * ```ts\n * try {\n * encoder.encode(buffer);\n * } catch (error) {\n * if (error instanceof HarmoniaCodecError) {\n * console.error(error.code, error.message);\n * }\n * }\n * ```\n */\nexport class HarmoniaCodecError extends Error {\n\t/**\n\t * Machine-readable error code.\n\t */\n\tpublic readonly code: string;\n\n\t/**\n\t * Additional context about the error.\n\t */\n\tpublic readonly context: Record<string, unknown>;\n\n\tconstructor(\n\t\tcode: string,\n\t\tmessage: string,\n\t\tcontext: Record<string, unknown> = {},\n\t) {\n\t\tsuper(message);\n\t\tthis.name = \"HarmoniaCodecError\";\n\t\tthis.code = code;\n\t\tthis.context = context;\n\t}\n}\n","import {\n\tOpusEncoder as NativeOpusEncoder,\n} from \"@harmonia-audio/native\";\nimport type { OpusApplication, OpusEncoderOptions, OpusSignalType } from \"./constants.js\";\nimport { HarmoniaCodecError } from \"./errors.js\";\n\nconst APPLICATION_MAP: Record<OpusApplication, string> = {\n\tvoip: \"Voip\",\n\taudio: \"Audio\",\n\trestricted_lowdelay: \"RestrictedLowDelay\",\n} as const;\n\n/**\n * High-performance Opus encoder backed by native Rust bindings.\n *\n * @example\n * ```ts\n * import { OpusEncoder } from '@harmonia/codec';\n *\n * const encoder = new OpusEncoder({\n * sampleRate: 48000,\n * channels: 2,\n * application: 'audio',\n * });\n *\n * const encoded = encoder.encode(pcmBuffer);\n * ```\n */\nexport class OpusEncoder {\n\tprivate readonly native: NativeOpusEncoder;\n\tprivate readonly _sampleRate: number;\n\tprivate readonly _channels: number;\n\tprivate readonly _application: OpusApplication;\n\n\tconstructor(options: OpusEncoderOptions) {\n\t\tthis._sampleRate = options.sampleRate;\n\t\tthis._channels = options.channels;\n\t\tthis._application = options.application;\n\n\t\ttry {\n\t\t\tthis.native = new NativeOpusEncoder(\n\t\t\t\toptions.sampleRate,\n\t\t\t\toptions.channels,\n\t\t\t\tAPPLICATION_MAP[options.application] as \"Voip\" | \"Audio\" | \"RestrictedLowDelay\",\n\t\t\t);\n\t\t} catch (error) {\n\t\t\tthrow new HarmoniaCodecError(\n\t\t\t\t\"ENCODER_INIT_FAILED\",\n\t\t\t\t`Failed to create Opus encoder: ${error instanceof Error ? error.message : String(error)}`,\n\t\t\t\t{ sampleRate: options.sampleRate, channels: options.channels },\n\t\t\t);\n\t\t}\n\n\t\tif (options.bitrate !== undefined) {\n\t\t\tthis.setBitrate(options.bitrate);\n\t\t}\n\t\tif (options.complexity !== undefined) {\n\t\t\tthis.setComplexity(options.complexity);\n\t\t}\n\t\tif (options.fec !== undefined) {\n\t\t\tthis.setFec(options.fec);\n\t\t}\n\t\tif (options.packetLossPercent !== undefined) {\n\t\t\tthis.setPacketLossPercent(options.packetLossPercent);\n\t\t}\n\t\tif (options.dtx !== undefined) {\n\t\t\tthis.setDtx(options.dtx);\n\t\t}\n\t\tif (options.vbr !== undefined) {\n\t\t\tthis.setVbr(options.vbr);\n\t\t}\n\t\tif (options.signal !== undefined) {\n\t\t\tthis.setSignal(options.signal);\n\t\t}\n\t}\n\n\t/**\n\t * Encode 16-bit PCM audio to Opus.\n\t *\n\t * @param pcm - Buffer containing interleaved 16-bit signed integer PCM samples\n\t * @returns Encoded Opus packet\n\t *\n\t * @example\n\t * ```ts\n\t * const opus = encoder.encode(pcmBuffer);\n\t * ```\n\t */\n\tencode(pcm: Buffer): Buffer {\n\t\ttry {\n\t\t\treturn this.native.encode(pcm);\n\t\t} catch (error) {\n\t\t\tthrow new HarmoniaCodecError(\n\t\t\t\t\"ENCODE_FAILED\",\n\t\t\t\t`Opus encode failed: ${error instanceof Error ? error.message : String(error)}`,\n\t\t\t\t{ inputSize: pcm.length },\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Encode 32-bit float PCM audio to Opus.\n\t *\n\t * @param pcm - Buffer containing interleaved 32-bit float PCM samples\n\t * @returns Encoded Opus packet\n\t *\n\t * @example\n\t * ```ts\n\t * const opus = encoder.encodeFloat(floatPcmBuffer);\n\t * ```\n\t */\n\tencodeFloat(pcm: Buffer): Buffer {\n\t\ttry {\n\t\t\treturn this.native.encodeFloat(pcm);\n\t\t} catch (error) {\n\t\t\tthrow new HarmoniaCodecError(\n\t\t\t\t\"ENCODE_FLOAT_FAILED\",\n\t\t\t\t`Opus float encode failed: ${error instanceof Error ? error.message : String(error)}`,\n\t\t\t\t{ inputSize: pcm.length },\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Set the encoder bitrate in bits per second.\n\t *\n\t * @example\n\t * ```ts\n\t * encoder.setBitrate(128000);\n\t * ```\n\t */\n\tsetBitrate(bitrate: number): void {\n\t\tthis.native.setBitrate(bitrate);\n\t}\n\n\t/**\n\t * Get the current encoder bitrate.\n\t *\n\t * @example\n\t * ```ts\n\t * const bitrate = encoder.getBitrate();\n\t * ```\n\t */\n\tgetBitrate(): number {\n\t\treturn this.native.getBitrate();\n\t}\n\n\t/**\n\t * Set encoding complexity (0-10, higher = better quality but slower).\n\t *\n\t * @example\n\t * ```ts\n\t * encoder.setComplexity(10);\n\t * ```\n\t */\n\tsetComplexity(complexity: number): void {\n\t\tthis.native.setComplexity(complexity);\n\t}\n\n\t/**\n\t * Get the current encoding complexity.\n\t *\n\t * @example\n\t * ```ts\n\t * const complexity = encoder.getComplexity();\n\t * ```\n\t */\n\tgetComplexity(): number {\n\t\treturn this.native.getComplexity();\n\t}\n\n\t/**\n\t * Enable or disable forward error correction.\n\t *\n\t * @example\n\t * ```ts\n\t * encoder.setFec(true);\n\t * ```\n\t */\n\tsetFec(enabled: boolean): void {\n\t\tthis.native.setFec(enabled);\n\t}\n\n\t/**\n\t * Check if FEC is enabled.\n\t *\n\t * @example\n\t * ```ts\n\t * const fecEnabled = encoder.getFec();\n\t * ```\n\t */\n\tgetFec(): boolean {\n\t\treturn this.native.getFec();\n\t}\n\n\t/**\n\t * Set expected packet loss percentage for FEC optimization.\n\t *\n\t * @example\n\t * ```ts\n\t * encoder.setPacketLossPercent(10);\n\t * ```\n\t */\n\tsetPacketLossPercent(percent: number): void {\n\t\tthis.native.setPacketLossPercent(percent);\n\t}\n\n\t/**\n\t * Enable or disable discontinuous transmission.\n\t *\n\t * @example\n\t * ```ts\n\t * encoder.setDtx(true);\n\t * ```\n\t */\n\tsetDtx(enabled: boolean): void {\n\t\tthis.native.setDtx(enabled);\n\t}\n\n\t/**\n\t * Enable or disable variable bitrate.\n\t *\n\t * @example\n\t * ```ts\n\t * encoder.setVbr(true);\n\t * ```\n\t */\n\tsetVbr(enabled: boolean): void {\n\t\tthis.native.setVbr(enabled);\n\t}\n\n\t/**\n\t * Set the signal type hint.\n\t *\n\t * @example\n\t * ```ts\n\t * encoder.setSignal('music');\n\t * ```\n\t */\n\tsetSignal(signal: OpusSignalType): void {\n\t\tthis.native.setSignal(signal);\n\t}\n\n\t/**\n\t * Reset the encoder state.\n\t *\n\t * @example\n\t * ```ts\n\t * encoder.reset();\n\t * ```\n\t */\n\treset(): void {\n\t\tthis.native.reset();\n\t}\n\n\t/** The sample rate this encoder operates at. */\n\tget sampleRate(): number {\n\t\treturn this._sampleRate;\n\t}\n\n\t/** The number of channels this encoder operates with. */\n\tget channels(): number {\n\t\treturn this._channels;\n\t}\n\n\t/** The application mode this encoder was configured with. */\n\tget application(): OpusApplication {\n\t\treturn this._application;\n\t}\n}\n","import { OpusDecoder as NativeOpusDecoder } from \"@harmonia-audio/native\";\nimport type { OpusDecoderOptions } from \"./constants.js\";\nimport { HarmoniaCodecError } from \"./errors.js\";\n\n/**\n * High-performance Opus decoder backed by native Rust bindings.\n *\n * @example\n * ```ts\n * import { OpusDecoder } from '@harmonia/codec';\n *\n * const decoder = new OpusDecoder({ sampleRate: 48000, channels: 2 });\n * const pcm = decoder.decode(opusPacket);\n * ```\n */\nexport class OpusDecoder {\n\tprivate readonly native: NativeOpusDecoder;\n\tprivate readonly _sampleRate: number;\n\tprivate readonly _channels: number;\n\n\tconstructor(options: OpusDecoderOptions) {\n\t\tthis._sampleRate = options.sampleRate;\n\t\tthis._channels = options.channels;\n\n\t\ttry {\n\t\t\tthis.native = new NativeOpusDecoder(options.sampleRate, options.channels);\n\t\t} catch (error) {\n\t\t\tthrow new HarmoniaCodecError(\n\t\t\t\t\"DECODER_INIT_FAILED\",\n\t\t\t\t`Failed to create Opus decoder: ${error instanceof Error ? error.message : String(error)}`,\n\t\t\t\t{ sampleRate: options.sampleRate, channels: options.channels },\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Decode an Opus packet to 16-bit PCM.\n\t *\n\t * @param opusData - The Opus-encoded packet\n\t * @returns Buffer containing decoded 16-bit PCM samples\n\t *\n\t * @example\n\t * ```ts\n\t * const pcm = decoder.decode(opusPacket);\n\t * ```\n\t */\n\tdecode(opusData: Buffer): Buffer {\n\t\ttry {\n\t\t\treturn this.native.decode(opusData);\n\t\t} catch (error) {\n\t\t\tthrow new HarmoniaCodecError(\n\t\t\t\t\"DECODE_FAILED\",\n\t\t\t\t`Opus decode failed: ${error instanceof Error ? error.message : String(error)}`,\n\t\t\t\t{ inputSize: opusData.length },\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Decode an Opus packet to 32-bit float PCM.\n\t *\n\t * @param opusData - The Opus-encoded packet\n\t * @returns Buffer containing decoded 32-bit float PCM samples\n\t *\n\t * @example\n\t * ```ts\n\t * const pcm = decoder.decodeFloat(opusPacket);\n\t * ```\n\t */\n\tdecodeFloat(opusData: Buffer): Buffer {\n\t\ttry {\n\t\t\treturn this.native.decodeFloat(opusData);\n\t\t} catch (error) {\n\t\t\tthrow new HarmoniaCodecError(\n\t\t\t\t\"DECODE_FLOAT_FAILED\",\n\t\t\t\t`Opus float decode failed: ${error instanceof Error ? error.message : String(error)}`,\n\t\t\t\t{ inputSize: opusData.length },\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Decode with forward error correction from a previous packet.\n\t *\n\t * @param opusData - The Opus packet to attempt FEC recovery from\n\t * @param frameSize - Number of samples per channel expected\n\t * @returns Buffer containing recovered PCM\n\t *\n\t * @example\n\t * ```ts\n\t * const recovered = decoder.decodeFec(previousPacket, 960);\n\t * ```\n\t */\n\tdecodeFec(opusData: Buffer, frameSize: number): Buffer {\n\t\ttry {\n\t\t\treturn this.native.decodeFec(opusData, frameSize);\n\t\t} catch (error) {\n\t\t\tthrow new HarmoniaCodecError(\n\t\t\t\t\"DECODE_FEC_FAILED\",\n\t\t\t\t`Opus FEC decode failed: ${error instanceof Error ? error.message : String(error)}`,\n\t\t\t\t{ inputSize: opusData.length, frameSize },\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Perform packet loss concealment (no input packet available).\n\t *\n\t * @param frameSize - Number of samples per channel to generate\n\t * @returns Buffer containing concealed PCM\n\t *\n\t * @example\n\t * ```ts\n\t * const concealed = decoder.decodePlc(960);\n\t * ```\n\t */\n\tdecodePlc(frameSize: number): Buffer {\n\t\ttry {\n\t\t\treturn this.native.decodePlc(frameSize);\n\t\t} catch (error) {\n\t\t\tthrow new HarmoniaCodecError(\n\t\t\t\t\"PLC_FAILED\",\n\t\t\t\t`Opus PLC failed: ${error instanceof Error ? error.message : String(error)}`,\n\t\t\t\t{ frameSize },\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Reset the decoder state.\n\t *\n\t * @example\n\t * ```ts\n\t * decoder.reset();\n\t * ```\n\t */\n\treset(): void {\n\t\tthis.native.reset();\n\t}\n\n\t/** The sample rate this decoder operates at. */\n\tget sampleRate(): number {\n\t\treturn this._sampleRate;\n\t}\n\n\t/** The number of channels this decoder produces. */\n\tget channels(): number {\n\t\treturn this._channels;\n\t}\n}\n","/**\n * Opus application modes.\n *\n * @example\n * ```ts\n * const encoder = new OpusEncoder({\n * sampleRate: 48000,\n * channels: 2,\n * application: 'audio',\n * });\n * ```\n */\nexport type OpusApplication = \"voip\" | \"audio\" | \"restricted_lowdelay\";\n\n/**\n * Opus signal type hints.\n *\n * @example\n * ```ts\n * encoder.setSignal('music');\n * ```\n */\nexport type OpusSignalType = \"voice\" | \"music\";\n\n/**\n * Configuration options for creating an Opus encoder.\n *\n * @example\n * ```ts\n * const options: OpusEncoderOptions = {\n * sampleRate: 48000,\n * channels: 2,\n * application: 'audio',\n * bitrate: 128000,\n * };\n * ```\n */\nexport interface OpusEncoderOptions {\n\treadonly sampleRate: (typeof OPUS_SAMPLE_RATES)[number];\n\treadonly channels: (typeof OPUS_CHANNELS)[number];\n\treadonly application: OpusApplication;\n\treadonly bitrate?: number;\n\treadonly complexity?: number;\n\treadonly fec?: boolean;\n\treadonly packetLossPercent?: number;\n\treadonly dtx?: boolean;\n\treadonly vbr?: boolean;\n\treadonly signal?: OpusSignalType;\n}\n\n/**\n * Configuration options for creating an Opus decoder.\n *\n * @example\n * ```ts\n * const decoder = new OpusDecoder({ sampleRate: 48000, channels: 2 });\n * ```\n */\nexport interface OpusDecoderOptions {\n\treadonly sampleRate: (typeof OPUS_SAMPLE_RATES)[number];\n\treadonly channels: (typeof OPUS_CHANNELS)[number];\n}\n\n/** Valid Opus sample rates. */\nexport const OPUS_SAMPLE_RATES = [8000, 12000, 16000, 24000, 48000] as const;\n\n/** Valid Opus channel counts. */\nexport const OPUS_CHANNELS = [1, 2] as const;\n\n/** Valid Opus frame durations in milliseconds. */\nexport const OPUS_FRAME_DURATIONS = [2.5, 5, 10, 20, 40, 60] as const;\n\n/** Discord's required Opus sample rate. */\nexport const DISCORD_OPUS_SAMPLE_RATE = 48000 as const;\n\n/** Discord's required Opus channel count (stereo). */\nexport const DISCORD_OPUS_CHANNELS = 2 as const;\n\n/** Discord's standard Opus frame duration in milliseconds. */\nexport const DISCORD_OPUS_FRAME_DURATION = 20 as const;\n\n/** Discord Opus frame size in samples per channel. */\nexport const DISCORD_OPUS_FRAME_SIZE = 960 as const;\n","import { Transform, type TransformCallback } from \"node:stream\";\nimport { OpusDecoder } from \"./decoder.js\";\nimport { OpusEncoder } from \"./encoder.js\";\nimport type { OpusApplication, OpusDecoderOptions, OpusEncoderOptions } from \"./constants.js\";\nimport { DISCORD_OPUS_FRAME_SIZE } from \"./constants.js\";\n\n/**\n * Transform stream that encodes PCM to Opus.\n *\n * @example\n * ```ts\n * import { OpusEncoderStream } from '@harmonia/codec';\n *\n * const stream = new OpusEncoderStream({\n * sampleRate: 48000,\n * channels: 2,\n * application: 'audio',\n * });\n *\n * pcmSource.pipe(stream).on('data', (opusPacket) => {\n * // handle encoded packet\n * });\n * ```\n */\nexport class OpusEncoderStream extends Transform {\n\tprivate readonly encoder: OpusEncoder;\n\tprivate readonly frameSize: number;\n\tprivate readonly frameSizeBytes: number;\n\tprivate pendingBuffer: Buffer;\n\n\tconstructor(options: OpusEncoderOptions) {\n\t\tsuper({\n\t\t\treadableObjectMode: true,\n\t\t});\n\t\tthis.encoder = new OpusEncoder(options);\n\t\tthis.frameSize = DISCORD_OPUS_FRAME_SIZE;\n\t\tthis.frameSizeBytes = this.frameSize * options.channels * 2;\n\t\tthis.pendingBuffer = Buffer.alloc(0);\n\t}\n\n\toverride _transform(\n\t\tchunk: Buffer,\n\t\t_encoding: BufferEncoding,\n\t\tcallback: TransformCallback,\n\t): void {\n\t\tthis.pendingBuffer = Buffer.concat([this.pendingBuffer, chunk]);\n\n\t\twhile (this.pendingBuffer.length >= this.frameSizeBytes) {\n\t\t\tconst frame = this.pendingBuffer.subarray(0, this.frameSizeBytes);\n\t\t\tthis.pendingBuffer = this.pendingBuffer.subarray(this.frameSizeBytes);\n\n\t\t\ttry {\n\t\t\t\tconst encoded = this.encoder.encode(frame);\n\t\t\t\tthis.push(encoded);\n\t\t\t} catch (error) {\n\t\t\t\tcallback(error instanceof Error ? error : new Error(String(error)));\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\n\t\tcallback();\n\t}\n\n\toverride _flush(callback: TransformCallback): void {\n\t\tif (this.pendingBuffer.length > 0) {\n\t\t\tconst padded = Buffer.alloc(this.frameSizeBytes);\n\t\t\tthis.pendingBuffer.copy(padded);\n\t\t\ttry {\n\t\t\t\tconst encoded = this.encoder.encode(padded);\n\t\t\t\tthis.push(encoded);\n\t\t\t} catch (error) {\n\t\t\t\tcallback(error instanceof Error ? error : new Error(String(error)));\n\t\t\t\treturn;\n\t\t\t}\n\t\t}\n\t\tcallback();\n\t}\n}\n\n/**\n * Transform stream that decodes Opus to PCM.\n *\n * @example\n * ```ts\n * import { OpusDecoderStream } from '@harmonia/codec';\n *\n * const stream = new OpusDecoderStream({ sampleRate: 48000, channels: 2 });\n *\n * opusSource.pipe(stream).on('data', (pcm) => {\n * // handle decoded PCM\n * });\n * ```\n */\nexport class OpusDecoderStream extends Transform {\n\tprivate readonly decoder: OpusDecoder;\n\n\tconstructor(options: OpusDecoderOptions) {\n\t\tsuper({\n\t\t\twritableObjectMode: true,\n\t\t});\n\t\tthis.decoder = new OpusDecoder(options);\n\t}\n\n\toverride _transform(\n\t\tchunk: Buffer,\n\t\t_encoding: BufferEncoding,\n\t\tcallback: TransformCallback,\n\t): void {\n\t\ttry {\n\t\t\tconst decoded = this.decoder.decode(chunk);\n\t\t\tthis.push(decoded);\n\t\t\tcallback();\n\t\t} catch (error) {\n\t\t\tcallback(error instanceof Error ? error : new Error(String(error)));\n\t\t}\n\t}\n}\n","import {\n\topusPacketGetNbChannelsNative,\n\topusPacketGetNbFramesNative,\n\topusPacketGetSamplesPerFrameNative,\n} from \"@harmonia-audio/native\";\nimport { HarmoniaCodecError } from \"./errors.js\";\n\n/**\n * Utilities for inspecting Opus packets without decoding.\n *\n * @example\n * ```ts\n * import { OpusPacket } from '@harmonia/codec';\n *\n * const channels = OpusPacket.getChannels(opusData);\n * const frames = OpusPacket.getFrameCount(opusData);\n * ```\n */\nexport class OpusPacket {\n\t/**\n\t * Get the number of channels from an Opus packet header.\n\t *\n\t * @example\n\t * ```ts\n\t * const channels = OpusPacket.getChannels(opusPacket);\n\t * ```\n\t */\n\tstatic getChannels(packet: Buffer): number {\n\t\ttry {\n\t\t\treturn opusPacketGetNbChannelsNative(packet);\n\t\t} catch (error) {\n\t\t\tthrow new HarmoniaCodecError(\n\t\t\t\t\"PACKET_INSPECT_FAILED\",\n\t\t\t\t`Failed to get channels: ${error instanceof Error ? error.message : String(error)}`,\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Get the number of frames in an Opus packet.\n\t *\n\t * @example\n\t * ```ts\n\t * const frameCount = OpusPacket.getFrameCount(opusPacket);\n\t * ```\n\t */\n\tstatic getFrameCount(packet: Buffer): number {\n\t\ttry {\n\t\t\treturn opusPacketGetNbFramesNative(packet);\n\t\t} catch (error) {\n\t\t\tthrow new HarmoniaCodecError(\n\t\t\t\t\"PACKET_INSPECT_FAILED\",\n\t\t\t\t`Failed to get frame count: ${error instanceof Error ? error.message : String(error)}`,\n\t\t\t);\n\t\t}\n\t}\n\n\t/**\n\t * Get samples per frame for a given sample rate.\n\t *\n\t * @example\n\t * ```ts\n\t * const spf = OpusPacket.getSamplesPerFrame(opusPacket, 48000);\n\t * ```\n\t */\n\tstatic getSamplesPerFrame(packet: Buffer, sampleRate: number): number {\n\t\ttry {\n\t\t\treturn opusPacketGetSamplesPerFrameNative(packet, sampleRate);\n\t\t} catch (error) {\n\t\t\tthrow new HarmoniaCodecError(\n\t\t\t\t\"PACKET_INSPECT_FAILED\",\n\t\t\t\t`Failed to get samples per frame: ${error instanceof Error ? error.message : String(error)}`,\n\t\t\t);\n\t\t}\n\t}\n\n\tprivate constructor() {\n\t\t// Static-only class\n\t}\n}\n"]}
|