@ikonai/sdk-libopus 1.0.80 → 1.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/index.js CHANGED
@@ -1,44 +1,5 @@
1
- import { OpusDecoder as S } from "./lib/opus-decoder.js";
2
- import { OpusEncoder as O } from "./lib/opus-encoder.js";
3
- import { getOpusModule as P, isModuleLoaded as A } from "./lib/opus-module.js";
4
- import { MAX_CHANNELS as R, MAX_FRAME_SIZE as D, MAX_PACKET_SIZE as N, OPUS_ALLOC_FAIL as L, OPUS_APPLICATION_AUDIO as B, OPUS_APPLICATION_RESTRICTED_LOWDELAY as C, OPUS_APPLICATION_VOIP as M, OPUS_BAD_ARG as o, OPUS_BANDWIDTH_FULLBAND as Q, OPUS_BANDWIDTH_MEDIUMBAND as e, OPUS_BANDWIDTH_NARROWBAND as r, OPUS_BANDWIDTH_SUPERWIDEBAND as G, OPUS_BANDWIDTH_WIDEBAND as W, OPUS_BUFFER_TOO_SMALL as p, OPUS_GET_BITRATE_REQUEST as F, OPUS_GET_COMPLEXITY_REQUEST as X, OPUS_GET_DTX_REQUEST as d, OPUS_GET_INBAND_FEC_REQUEST as H, OPUS_GET_PACKET_LOSS_PERC_REQUEST as V, OPUS_GET_VBR_REQUEST as t, OPUS_INTERNAL_ERROR as u, OPUS_INVALID_PACKET as K, OPUS_INVALID_STATE as f, OPUS_OK as m, OPUS_RESET_STATE as s, OPUS_SET_BITRATE_REQUEST as x, OPUS_SET_COMPLEXITY_REQUEST as Y, OPUS_SET_DTX_REQUEST as c, OPUS_SET_INBAND_FEC_REQUEST as l, OPUS_SET_PACKET_LOSS_PERC_REQUEST as Z, OPUS_SET_VBR_REQUEST as a, OPUS_SIGNAL_MUSIC as g, OPUS_SIGNAL_VOICE as i, OPUS_UNIMPLEMENTED as n } from "./lib/opus-constants.js";
5
- export {
6
- R as MAX_CHANNELS,
7
- D as MAX_FRAME_SIZE,
8
- N as MAX_PACKET_SIZE,
9
- L as OPUS_ALLOC_FAIL,
10
- B as OPUS_APPLICATION_AUDIO,
11
- C as OPUS_APPLICATION_RESTRICTED_LOWDELAY,
12
- M as OPUS_APPLICATION_VOIP,
13
- o as OPUS_BAD_ARG,
14
- Q as OPUS_BANDWIDTH_FULLBAND,
15
- e as OPUS_BANDWIDTH_MEDIUMBAND,
16
- r as OPUS_BANDWIDTH_NARROWBAND,
17
- G as OPUS_BANDWIDTH_SUPERWIDEBAND,
18
- W as OPUS_BANDWIDTH_WIDEBAND,
19
- p as OPUS_BUFFER_TOO_SMALL,
20
- F as OPUS_GET_BITRATE_REQUEST,
21
- X as OPUS_GET_COMPLEXITY_REQUEST,
22
- d as OPUS_GET_DTX_REQUEST,
23
- H as OPUS_GET_INBAND_FEC_REQUEST,
24
- V as OPUS_GET_PACKET_LOSS_PERC_REQUEST,
25
- t as OPUS_GET_VBR_REQUEST,
26
- u as OPUS_INTERNAL_ERROR,
27
- K as OPUS_INVALID_PACKET,
28
- f as OPUS_INVALID_STATE,
29
- m as OPUS_OK,
30
- s as OPUS_RESET_STATE,
31
- x as OPUS_SET_BITRATE_REQUEST,
32
- Y as OPUS_SET_COMPLEXITY_REQUEST,
33
- c as OPUS_SET_DTX_REQUEST,
34
- l as OPUS_SET_INBAND_FEC_REQUEST,
35
- Z as OPUS_SET_PACKET_LOSS_PERC_REQUEST,
36
- a as OPUS_SET_VBR_REQUEST,
37
- g as OPUS_SIGNAL_MUSIC,
38
- i as OPUS_SIGNAL_VOICE,
39
- n as OPUS_UNIMPLEMENTED,
40
- S as OpusDecoder,
41
- O as OpusEncoder,
42
- P as getOpusModule,
43
- A as isModuleLoaded
44
- };
1
+ import { getOpusModule as e, isModuleLoaded as t } from "./lib/opus-module.js";
2
+ import { MAX_CHANNELS as n, MAX_FRAME_SIZE as r, MAX_PACKET_SIZE as i, OPUS_ALLOC_FAIL as a, OPUS_APPLICATION_AUDIO as o, OPUS_APPLICATION_RESTRICTED_LOWDELAY as s, OPUS_APPLICATION_VOIP as c, OPUS_BAD_ARG as l, OPUS_BANDWIDTH_FULLBAND as u, OPUS_BANDWIDTH_MEDIUMBAND as d, OPUS_BANDWIDTH_NARROWBAND as f, OPUS_BANDWIDTH_SUPERWIDEBAND as p, OPUS_BANDWIDTH_WIDEBAND as m, OPUS_BUFFER_TOO_SMALL as h, OPUS_GET_BITRATE_REQUEST as g, OPUS_GET_COMPLEXITY_REQUEST as _, OPUS_GET_DTX_REQUEST as v, OPUS_GET_INBAND_FEC_REQUEST as y, OPUS_GET_PACKET_LOSS_PERC_REQUEST as b, OPUS_GET_VBR_REQUEST as x, OPUS_INTERNAL_ERROR as S, OPUS_INVALID_PACKET as C, OPUS_INVALID_STATE as w, OPUS_OK as T, OPUS_RESET_STATE as E, OPUS_SET_BITRATE_REQUEST as D, OPUS_SET_COMPLEXITY_REQUEST as O, OPUS_SET_DTX_REQUEST as k, OPUS_SET_INBAND_FEC_REQUEST as A, OPUS_SET_PACKET_LOSS_PERC_REQUEST as j, OPUS_SET_VBR_REQUEST as M, OPUS_SIGNAL_MUSIC as N, OPUS_SIGNAL_VOICE as P, OPUS_UNIMPLEMENTED as F } from "./lib/opus-constants.js";
3
+ import { OpusDecoder as I } from "./lib/opus-decoder.js";
4
+ import { OpusEncoder as L } from "./lib/opus-encoder.js";
5
+ export { n as MAX_CHANNELS, r as MAX_FRAME_SIZE, i as MAX_PACKET_SIZE, a as OPUS_ALLOC_FAIL, o as OPUS_APPLICATION_AUDIO, s as OPUS_APPLICATION_RESTRICTED_LOWDELAY, c as OPUS_APPLICATION_VOIP, l as OPUS_BAD_ARG, u as OPUS_BANDWIDTH_FULLBAND, d as OPUS_BANDWIDTH_MEDIUMBAND, f as OPUS_BANDWIDTH_NARROWBAND, p as OPUS_BANDWIDTH_SUPERWIDEBAND, m as OPUS_BANDWIDTH_WIDEBAND, h as OPUS_BUFFER_TOO_SMALL, g as OPUS_GET_BITRATE_REQUEST, _ as OPUS_GET_COMPLEXITY_REQUEST, v as OPUS_GET_DTX_REQUEST, y as OPUS_GET_INBAND_FEC_REQUEST, b as OPUS_GET_PACKET_LOSS_PERC_REQUEST, x as OPUS_GET_VBR_REQUEST, S as OPUS_INTERNAL_ERROR, C as OPUS_INVALID_PACKET, w as OPUS_INVALID_STATE, T as OPUS_OK, E as OPUS_RESET_STATE, D as OPUS_SET_BITRATE_REQUEST, O as OPUS_SET_COMPLEXITY_REQUEST, k as OPUS_SET_DTX_REQUEST, A as OPUS_SET_INBAND_FEC_REQUEST, j as OPUS_SET_PACKET_LOSS_PERC_REQUEST, M as OPUS_SET_VBR_REQUEST, N as OPUS_SIGNAL_MUSIC, P as OPUS_SIGNAL_VOICE, F as OPUS_UNIMPLEMENTED, I as OpusDecoder, L as OpusEncoder, e as getOpusModule, t as isModuleLoaded };
@@ -1,37 +1,4 @@
1
- const _ = 2048, E = 2049, S = 2051, T = 4002, U = 4003, O = 4010, P = 4011, A = 4006, I = 4007, o = 4016, t = 4017, c = 4012, n = 4013, s = 4014, R = 4015, D = 4028, N = 0, L = -1, B = -2, C = -3, M = -4, Q = -5, G = -6, W = -7, F = 1101, X = 1102, H = 1103, V = 1104, K = 1105, Y = 3001, e = 3002, Z = 5760, p = 4e3, r = 2;
2
- export {
3
- r as MAX_CHANNELS,
4
- Z as MAX_FRAME_SIZE,
5
- p as MAX_PACKET_SIZE,
6
- W as OPUS_ALLOC_FAIL,
7
- E as OPUS_APPLICATION_AUDIO,
8
- S as OPUS_APPLICATION_RESTRICTED_LOWDELAY,
9
- _ as OPUS_APPLICATION_VOIP,
10
- L as OPUS_BAD_ARG,
11
- K as OPUS_BANDWIDTH_FULLBAND,
12
- X as OPUS_BANDWIDTH_MEDIUMBAND,
13
- F as OPUS_BANDWIDTH_NARROWBAND,
14
- V as OPUS_BANDWIDTH_SUPERWIDEBAND,
15
- H as OPUS_BANDWIDTH_WIDEBAND,
16
- B as OPUS_BUFFER_TOO_SMALL,
17
- U as OPUS_GET_BITRATE_REQUEST,
18
- P as OPUS_GET_COMPLEXITY_REQUEST,
19
- t as OPUS_GET_DTX_REQUEST,
20
- n as OPUS_GET_INBAND_FEC_REQUEST,
21
- R as OPUS_GET_PACKET_LOSS_PERC_REQUEST,
22
- I as OPUS_GET_VBR_REQUEST,
23
- C as OPUS_INTERNAL_ERROR,
24
- M as OPUS_INVALID_PACKET,
25
- G as OPUS_INVALID_STATE,
26
- N as OPUS_OK,
27
- D as OPUS_RESET_STATE,
28
- T as OPUS_SET_BITRATE_REQUEST,
29
- O as OPUS_SET_COMPLEXITY_REQUEST,
30
- o as OPUS_SET_DTX_REQUEST,
31
- c as OPUS_SET_INBAND_FEC_REQUEST,
32
- s as OPUS_SET_PACKET_LOSS_PERC_REQUEST,
33
- A as OPUS_SET_VBR_REQUEST,
34
- e as OPUS_SIGNAL_MUSIC,
35
- Y as OPUS_SIGNAL_VOICE,
36
- Q as OPUS_UNIMPLEMENTED
37
- };
1
+ //#region src/lib/opus-constants.ts
2
+ var e = 2048, t = 2049, n = 2051, r = 4002, i = 4003, a = 4010, o = 4011, s = 4006, c = 4007, l = 4016, u = 4017, d = 4012, f = 4013, p = 4014, m = 4015, h = 4028, g = 0, _ = -1, v = -2, y = -3, b = -4, x = -5, S = -6, C = -7, w = 1101, T = 1102, E = 1103, D = 1104, O = 1105, k = 3001, A = 3002, j = 5760, M = 4e3, N = 2;
3
+ //#endregion
4
+ export { N as MAX_CHANNELS, j as MAX_FRAME_SIZE, M as MAX_PACKET_SIZE, C as OPUS_ALLOC_FAIL, t as OPUS_APPLICATION_AUDIO, n as OPUS_APPLICATION_RESTRICTED_LOWDELAY, e as OPUS_APPLICATION_VOIP, _ as OPUS_BAD_ARG, O as OPUS_BANDWIDTH_FULLBAND, T as OPUS_BANDWIDTH_MEDIUMBAND, w as OPUS_BANDWIDTH_NARROWBAND, D as OPUS_BANDWIDTH_SUPERWIDEBAND, E as OPUS_BANDWIDTH_WIDEBAND, v as OPUS_BUFFER_TOO_SMALL, i as OPUS_GET_BITRATE_REQUEST, o as OPUS_GET_COMPLEXITY_REQUEST, u as OPUS_GET_DTX_REQUEST, f as OPUS_GET_INBAND_FEC_REQUEST, m as OPUS_GET_PACKET_LOSS_PERC_REQUEST, c as OPUS_GET_VBR_REQUEST, y as OPUS_INTERNAL_ERROR, b as OPUS_INVALID_PACKET, S as OPUS_INVALID_STATE, g as OPUS_OK, h as OPUS_RESET_STATE, r as OPUS_SET_BITRATE_REQUEST, a as OPUS_SET_COMPLEXITY_REQUEST, l as OPUS_SET_DTX_REQUEST, d as OPUS_SET_INBAND_FEC_REQUEST, p as OPUS_SET_PACKET_LOSS_PERC_REQUEST, s as OPUS_SET_VBR_REQUEST, A as OPUS_SIGNAL_MUSIC, k as OPUS_SIGNAL_VOICE, x as OPUS_UNIMPLEMENTED };
@@ -1,106 +1,63 @@
1
- import { getOpusModule as d } from "./opus-module.js";
2
- import { MAX_FRAME_SIZE as i, MAX_PACKET_SIZE as h, OPUS_RESET_STATE as a } from "./opus-constants.js";
3
- class m {
4
- module = null;
5
- decoder = 0;
6
- channels;
7
- sampleRate;
8
- // Pre-allocated WASM heap pointers (allocated once, reused)
9
- inputPtr = 0;
10
- outputPtr = 0;
11
- // Pre-allocated output arrays (reused, caller must copy if needed)
12
- channelBuffers;
13
- ready;
14
- constructor(e) {
15
- this.channels = e.channels, this.sampleRate = e.sampleRate, this.channelBuffers = Array.from({ length: this.channels }, () => new Float32Array(i)), this.ready = this.initialize();
16
- }
17
- async initialize() {
18
- this.module = await d();
19
- const e = this.module._malloc(4);
20
- this.decoder = this.module._opus_decoder_create(this.sampleRate, this.channels, e);
21
- const t = new DataView(this.module.HEAPU8.buffer).getInt32(e, !0);
22
- if (this.module._free(e), t !== 0)
23
- throw new Error(`opus_decoder_create failed with error code ${t}`);
24
- this.inputPtr = this.module._malloc(h), this.outputPtr = this.module._malloc(i * this.channels * 4);
25
- }
26
- /**
27
- * Decode an Opus frame.
28
- *
29
- * Returns views into pre-allocated buffers. The channelData arrays are reused
30
- * on each call, so callers MUST copy the data if it needs to persist beyond
31
- * the next decode call.
32
- *
33
- * @param opusData - The Opus-encoded audio frame
34
- * @returns Decoded audio with channel data and sample count
35
- */
36
- decodeFrame(e) {
37
- if (!this.module || !this.decoder)
38
- throw new Error("Decoder not initialized");
39
- if (e.length > h)
40
- throw new Error(`Opus packet too large: ${e.length} > ${h}`);
41
- this.module.HEAPU8.set(e, this.inputPtr);
42
- const t = this.module._opus_decode_float(
43
- this.decoder,
44
- this.inputPtr,
45
- e.length,
46
- this.outputPtr,
47
- i,
48
- 0
49
- // decodeFec = false
50
- );
51
- if (t < 0)
52
- throw new Error(`opus_decode_float failed with error code ${t}`);
53
- const o = this.outputPtr / 4;
54
- for (let s = 0; s < this.channels; s++) {
55
- const l = this.channelBuffers[s];
56
- for (let r = 0; r < t; r++)
57
- l[r] = this.module.HEAPF32[o + r * this.channels + s];
58
- }
59
- return {
60
- channelData: this.channelBuffers,
61
- samplesDecoded: t,
62
- sampleRate: this.sampleRate
63
- };
64
- }
65
- /**
66
- * Decode a frame and copy the result (when caller needs ownership).
67
- * Use this when you need the decoded audio to persist.
68
- */
69
- decodeFrameAndCopy(e) {
70
- const t = this.decodeFrame(e);
71
- return {
72
- channelData: t.channelData.map((o) => o.slice(0, t.samplesDecoded)),
73
- samplesDecoded: t.samplesDecoded,
74
- sampleRate: t.sampleRate
75
- };
76
- }
77
- /**
78
- * Reset the decoder state.
79
- * Call this when there's a discontinuity in the audio stream.
80
- */
81
- reset() {
82
- this.module && this.decoder && this.module._opus_decoder_ctl(this.decoder, a);
83
- }
84
- /**
85
- * Get the number of channels this decoder was created with.
86
- */
87
- getChannels() {
88
- return this.channels;
89
- }
90
- /**
91
- * Get the sample rate this decoder was created with.
92
- */
93
- getSampleRate() {
94
- return this.sampleRate;
95
- }
96
- /**
97
- * Release all resources.
98
- * The decoder cannot be used after calling destroy().
99
- */
100
- destroy() {
101
- this.module && (this.decoder && this.module._opus_decoder_destroy(this.decoder), this.inputPtr && this.module._free(this.inputPtr), this.outputPtr && this.module._free(this.outputPtr)), this.decoder = 0, this.inputPtr = 0, this.outputPtr = 0, this.module = null;
102
- }
103
- }
104
- export {
105
- m as OpusDecoder
1
+ import { getOpusModule as e } from "./opus-module.js";
2
+ import { MAX_FRAME_SIZE as t, MAX_PACKET_SIZE as n, OPUS_RESET_STATE as r } from "./opus-constants.js";
3
+ //#region src/lib/opus-decoder.ts
4
+ var i = class {
5
+ module = null;
6
+ decoder = 0;
7
+ channels;
8
+ sampleRate;
9
+ inputPtr = 0;
10
+ outputPtr = 0;
11
+ channelBuffers;
12
+ ready;
13
+ constructor(e) {
14
+ this.channels = e.channels, this.sampleRate = e.sampleRate, this.channelBuffers = Array.from({ length: this.channels }, () => new Float32Array(t)), this.ready = this.initialize();
15
+ }
16
+ async initialize() {
17
+ this.module = await e();
18
+ let r = this.module._malloc(4);
19
+ this.decoder = this.module._opus_decoder_create(this.sampleRate, this.channels, r);
20
+ let i = new DataView(this.module.HEAPU8.buffer).getInt32(r, !0);
21
+ if (this.module._free(r), i !== 0) throw Error(`opus_decoder_create failed with error code ${i}`);
22
+ this.inputPtr = this.module._malloc(n), this.outputPtr = this.module._malloc(t * this.channels * 4);
23
+ }
24
+ decodeFrame(e) {
25
+ if (!this.module || !this.decoder) throw Error("Decoder not initialized");
26
+ if (e.length > 4e3) throw Error(`Opus packet too large: ${e.length} > ${n}`);
27
+ this.module.HEAPU8.set(e, this.inputPtr);
28
+ let r = this.module._opus_decode_float(this.decoder, this.inputPtr, e.length, this.outputPtr, t, 0);
29
+ if (r < 0) throw Error(`opus_decode_float failed with error code ${r}`);
30
+ let i = this.outputPtr / 4;
31
+ for (let e = 0; e < this.channels; e++) {
32
+ let t = this.channelBuffers[e];
33
+ for (let n = 0; n < r; n++) t[n] = this.module.HEAPF32[i + n * this.channels + e];
34
+ }
35
+ return {
36
+ channelData: this.channelBuffers,
37
+ samplesDecoded: r,
38
+ sampleRate: this.sampleRate
39
+ };
40
+ }
41
+ decodeFrameAndCopy(e) {
42
+ let t = this.decodeFrame(e);
43
+ return {
44
+ channelData: t.channelData.map((e) => e.slice(0, t.samplesDecoded)),
45
+ samplesDecoded: t.samplesDecoded,
46
+ sampleRate: t.sampleRate
47
+ };
48
+ }
49
+ reset() {
50
+ this.module && this.decoder && this.module._opus_decoder_ctl(this.decoder, r);
51
+ }
52
+ getChannels() {
53
+ return this.channels;
54
+ }
55
+ getSampleRate() {
56
+ return this.sampleRate;
57
+ }
58
+ destroy() {
59
+ this.module && (this.decoder && this.module._opus_decoder_destroy(this.decoder), this.inputPtr && this.module._free(this.inputPtr), this.outputPtr && this.module._free(this.outputPtr)), this.decoder = 0, this.inputPtr = 0, this.outputPtr = 0, this.module = null;
60
+ }
106
61
  };
62
+ //#endregion
63
+ export { i as OpusDecoder };
@@ -1,117 +1,63 @@
1
- import { getOpusModule as l } from "./opus-module.js";
2
- import { OPUS_APPLICATION_VOIP as u, OPUS_SET_BITRATE_REQUEST as o, OPUS_SET_COMPLEXITY_REQUEST as n, MAX_PACKET_SIZE as h, OPUS_RESET_STATE as d } from "./opus-constants.js";
3
- const r = 2880;
4
- class _ {
5
- module = null;
6
- encoder = 0;
7
- channels;
8
- sampleRate;
9
- bitrate;
10
- application;
11
- // Pre-allocated WASM heap pointers
12
- inputPtr = 0;
13
- outputPtr = 0;
14
- ready;
15
- constructor(t) {
16
- this.channels = t.channels, this.sampleRate = t.sampleRate, this.bitrate = t.bitrate ?? 32e3, this.application = t.application ?? u, this.ready = this.initialize(t);
17
- }
18
- async initialize(t) {
19
- this.module = await l();
20
- const e = this.module._malloc(4);
21
- this.encoder = this.module._opus_encoder_create(
22
- this.sampleRate,
23
- this.channels,
24
- this.application,
25
- e
26
- );
27
- const i = new DataView(this.module.HEAPU8.buffer).getInt32(e, !0);
28
- if (this.module._free(e), i !== 0)
29
- throw new Error(`opus_encoder_create failed with error code ${i}`);
30
- this.setEncoderCtl(o, this.bitrate), t.complexity !== void 0 && this.setEncoderCtl(n, t.complexity), this.inputPtr = this.module._malloc(r * this.channels * 4), this.outputPtr = this.module._malloc(h);
31
- }
32
- setEncoderCtl(t, e) {
33
- if (!this.module || !this.encoder)
34
- return;
35
- const i = this.module._malloc(4);
36
- new DataView(this.module.HEAPU8.buffer).setInt32(i, e, !0), this.module._opus_encoder_ctl(this.encoder, t, i), this.module._free(i);
37
- }
38
- /**
39
- * Encode PCM float samples to Opus.
40
- *
41
- * Returns a view into a pre-allocated buffer. The returned Uint8Array is reused
42
- * on each call, so callers MUST copy the data if it needs to persist beyond
43
- * the next encode call.
44
- *
45
- * @param pcmFloat - Interleaved float PCM samples (-1.0 to 1.0)
46
- * @returns Opus-encoded audio data (view into internal buffer)
47
- */
48
- encode(t) {
49
- if (!this.module || !this.encoder)
50
- throw new Error("Encoder not initialized");
51
- const e = t.length / this.channels;
52
- if (e > r)
53
- throw new Error(`Frame size too large: ${e} > ${r} samples`);
54
- const i = this.inputPtr / 4;
55
- this.module.HEAPF32.set(t, i);
56
- const s = this.module._opus_encode_float(
57
- this.encoder,
58
- this.inputPtr,
59
- e,
60
- this.outputPtr,
61
- h
62
- );
63
- if (s < 0)
64
- throw new Error(`opus_encode_float failed with error code ${s}`);
65
- return new Uint8Array(this.module.HEAPU8.buffer, this.outputPtr, s);
66
- }
67
- /**
68
- * Encode and copy the result (when caller needs ownership).
69
- * Use this when you need the encoded audio to persist, e.g., when sending to another thread.
70
- */
71
- encodeAndCopy(t) {
72
- return this.encode(t).slice();
73
- }
74
- /**
75
- * Reset the encoder state.
76
- * Call this when there's a discontinuity in the audio stream.
77
- */
78
- reset() {
79
- this.module && this.encoder && this.module._opus_encoder_ctl(this.encoder, d);
80
- }
81
- /**
82
- * Set the encoder bitrate.
83
- * @param bitrate - Target bitrate in bits per second
84
- */
85
- setBitrate(t) {
86
- this.setEncoderCtl(o, t);
87
- }
88
- /**
89
- * Set the encoder complexity (0-10, higher = better quality but more CPU).
90
- * @param complexity - Complexity value (0-10)
91
- */
92
- setComplexity(t) {
93
- this.setEncoderCtl(n, t);
94
- }
95
- /**
96
- * Get the number of channels this encoder was created with.
97
- */
98
- getChannels() {
99
- return this.channels;
100
- }
101
- /**
102
- * Get the sample rate this encoder was created with.
103
- */
104
- getSampleRate() {
105
- return this.sampleRate;
106
- }
107
- /**
108
- * Release all resources.
109
- * The encoder cannot be used after calling destroy().
110
- */
111
- destroy() {
112
- this.module && (this.encoder && this.module._opus_encoder_destroy(this.encoder), this.inputPtr && this.module._free(this.inputPtr), this.outputPtr && this.module._free(this.outputPtr)), this.encoder = 0, this.inputPtr = 0, this.outputPtr = 0, this.module = null;
113
- }
114
- }
115
- export {
116
- _ as OpusEncoder
1
+ import { getOpusModule as e } from "./opus-module.js";
2
+ import { MAX_PACKET_SIZE as t, OPUS_RESET_STATE as n, OPUS_SET_BITRATE_REQUEST as r, OPUS_SET_COMPLEXITY_REQUEST as i } from "./opus-constants.js";
3
+ //#region src/lib/opus-encoder.ts
4
+ var a = 2880, o = class {
5
+ module = null;
6
+ encoder = 0;
7
+ channels;
8
+ sampleRate;
9
+ bitrate;
10
+ application;
11
+ inputPtr = 0;
12
+ outputPtr = 0;
13
+ ready;
14
+ constructor(e) {
15
+ this.channels = e.channels, this.sampleRate = e.sampleRate, this.bitrate = e.bitrate ?? 32e3, this.application = e.application ?? 2048, this.ready = this.initialize(e);
16
+ }
17
+ async initialize(n) {
18
+ this.module = await e();
19
+ let o = this.module._malloc(4);
20
+ this.encoder = this.module._opus_encoder_create(this.sampleRate, this.channels, this.application, o);
21
+ let s = new DataView(this.module.HEAPU8.buffer).getInt32(o, !0);
22
+ if (this.module._free(o), s !== 0) throw Error(`opus_encoder_create failed with error code ${s}`);
23
+ this.setEncoderCtl(r, this.bitrate), n.complexity !== void 0 && this.setEncoderCtl(i, n.complexity), this.inputPtr = this.module._malloc(a * this.channels * 4), this.outputPtr = this.module._malloc(t);
24
+ }
25
+ setEncoderCtl(e, t) {
26
+ if (!this.module || !this.encoder) return;
27
+ let n = this.module._malloc(4);
28
+ new DataView(this.module.HEAPU8.buffer).setInt32(n, t, !0), this.module._opus_encoder_ctl(this.encoder, e, n), this.module._free(n);
29
+ }
30
+ encode(e) {
31
+ if (!this.module || !this.encoder) throw Error("Encoder not initialized");
32
+ let n = e.length / this.channels;
33
+ if (n > a) throw Error(`Frame size too large: ${n} > ${a} samples`);
34
+ let r = this.inputPtr / 4;
35
+ this.module.HEAPF32.set(e, r);
36
+ let i = this.module._opus_encode_float(this.encoder, this.inputPtr, n, this.outputPtr, t);
37
+ if (i < 0) throw Error(`opus_encode_float failed with error code ${i}`);
38
+ return new Uint8Array(this.module.HEAPU8.buffer, this.outputPtr, i);
39
+ }
40
+ encodeAndCopy(e) {
41
+ return this.encode(e).slice();
42
+ }
43
+ reset() {
44
+ this.module && this.encoder && this.module._opus_encoder_ctl(this.encoder, n);
45
+ }
46
+ setBitrate(e) {
47
+ this.setEncoderCtl(r, e);
48
+ }
49
+ setComplexity(e) {
50
+ this.setEncoderCtl(i, e);
51
+ }
52
+ getChannels() {
53
+ return this.channels;
54
+ }
55
+ getSampleRate() {
56
+ return this.sampleRate;
57
+ }
58
+ destroy() {
59
+ this.module && (this.encoder && this.module._opus_encoder_destroy(this.encoder), this.inputPtr && this.module._free(this.inputPtr), this.outputPtr && this.module._free(this.outputPtr)), this.encoder = 0, this.inputPtr = 0, this.outputPtr = 0, this.module = null;
60
+ }
117
61
  };
62
+ //#endregion
63
+ export { o as OpusEncoder };
@@ -1,17 +1,14 @@
1
- import o from "../wasm/libopus.wasm.js";
2
- let t = null, e = null;
3
- async function i() {
4
- return e || (t || (t = (async () => {
5
- const n = (await import("../wasm/libopus.js")).default;
6
- return e = await n({
7
- locateFile: (r) => r.endsWith(".wasm") ? o : r
8
- }), e;
9
- })()), t);
1
+ import e from "../wasm/libopus.js";
2
+ //#region src/lib/opus-module.ts
3
+ var t = null, n = null;
4
+ async function r() {
5
+ return n || (t ||= (async () => {
6
+ let t = (await import("../wasm/libopus.js")).default;
7
+ return n = await t({ locateFile: (t) => t.endsWith(".wasm") ? e : t }), n;
8
+ })(), t);
10
9
  }
11
- function a() {
12
- return e !== null;
10
+ function i() {
11
+ return n !== null;
13
12
  }
14
- export {
15
- i as getOpusModule,
16
- a as isModuleLoaded
17
- };
13
+ //#endregion
14
+ export { r as getOpusModule, i as isModuleLoaded };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ikonai/sdk-libopus",
3
- "version": "1.0.80",
3
+ "version": "1.1.0",
4
4
  "type": "module",
5
5
  "main": "./index.js",
6
6
  "types": "./index.d.ts",
@@ -0,0 +1,2 @@
1
+ declare const _default: import('vite').UserConfig;
2
+ export default _default;