@ikonai/sdk 0.0.41 → 0.0.43

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.
@@ -1,130 +0,0 @@
1
- let o = null, s = null;
2
- async function a() {
3
- return s || (o || (o = (async () => {
4
- const t = (await import("./libopus-BEPFPIM_.js")).default;
5
- return s = await t({
6
- locateFile: (e) => e.endsWith(".wasm") ? new URL("../wasm/libopus.wasm", import.meta.url).href : e
7
- }), s;
8
- })()), o);
9
- }
10
- const d = 2048, l = 4002, u = 4010, m = 4028, h = 4e3, n = 2880;
11
- class _ {
12
- module = null;
13
- encoder = 0;
14
- channels;
15
- sampleRate;
16
- bitrate;
17
- application;
18
- // Pre-allocated WASM heap pointers
19
- inputPtr = 0;
20
- outputPtr = 0;
21
- ready;
22
- constructor(t) {
23
- this.channels = t.channels, this.sampleRate = t.sampleRate, this.bitrate = t.bitrate ?? 32e3, this.application = t.application ?? d, this.ready = this.initialize(t);
24
- }
25
- async initialize(t) {
26
- this.module = await a();
27
- const e = this.module._malloc(4);
28
- this.encoder = this.module._opus_encoder_create(
29
- this.sampleRate,
30
- this.channels,
31
- this.application,
32
- e
33
- );
34
- const i = new DataView(this.module.HEAPU8.buffer).getInt32(e, !0);
35
- if (this.module._free(e), i !== 0)
36
- throw new Error(`opus_encoder_create failed with error code ${i}`);
37
- this.setEncoderCtl(l, this.bitrate), t.complexity !== void 0 && this.setEncoderCtl(u, t.complexity), this.inputPtr = this.module._malloc(n * this.channels * 4), this.outputPtr = this.module._malloc(h);
38
- }
39
- setEncoderCtl(t, e) {
40
- if (!this.module || !this.encoder)
41
- return;
42
- const i = this.module._malloc(4);
43
- new DataView(this.module.HEAPU8.buffer).setInt32(i, e, !0), this.module._opus_encoder_ctl(this.encoder, t, i), this.module._free(i);
44
- }
45
- /**
46
- * Encode PCM float samples to Opus.
47
- *
48
- * Returns a view into a pre-allocated buffer. The returned Uint8Array is reused
49
- * on each call, so callers MUST copy the data if it needs to persist beyond
50
- * the next encode call.
51
- *
52
- * @param pcmFloat - Interleaved float PCM samples (-1.0 to 1.0)
53
- * @returns Opus-encoded audio data (view into internal buffer)
54
- */
55
- encode(t) {
56
- if (!this.module || !this.encoder)
57
- throw new Error("Encoder not initialized");
58
- const e = t.length / this.channels;
59
- if (e > n)
60
- throw new Error(`Frame size too large: ${e} > ${n} samples`);
61
- const i = this.inputPtr / 4;
62
- this.module.HEAPF32.set(t, i);
63
- const r = this.module._opus_encode_float(
64
- this.encoder,
65
- this.inputPtr,
66
- e,
67
- this.outputPtr,
68
- h
69
- );
70
- if (r < 0)
71
- throw new Error(`opus_encode_float failed with error code ${r}`);
72
- return new Uint8Array(this.module.HEAPU8.buffer, this.outputPtr, r);
73
- }
74
- /**
75
- * Encode and copy the result (when caller needs ownership).
76
- * Use this when you need the encoded audio to persist, e.g., when sending to another thread.
77
- */
78
- encodeAndCopy(t) {
79
- return this.encode(t).slice();
80
- }
81
- /**
82
- * Reset the encoder state.
83
- * Call this when there's a discontinuity in the audio stream.
84
- */
85
- reset() {
86
- this.module && this.encoder && this.module._opus_encoder_ctl(this.encoder, m);
87
- }
88
- /**
89
- * Set the encoder bitrate.
90
- * @param bitrate - Target bitrate in bits per second
91
- */
92
- setBitrate(t) {
93
- this.setEncoderCtl(l, t);
94
- }
95
- /**
96
- * Set the encoder complexity (0-10, higher = better quality but more CPU).
97
- * @param complexity - Complexity value (0-10)
98
- */
99
- setComplexity(t) {
100
- this.setEncoderCtl(u, t);
101
- }
102
- /**
103
- * Get the number of channels this encoder was created with.
104
- */
105
- getChannels() {
106
- return this.channels;
107
- }
108
- /**
109
- * Get the sample rate this encoder was created with.
110
- */
111
- getSampleRate() {
112
- return this.sampleRate;
113
- }
114
- /**
115
- * Release all resources.
116
- * The encoder cannot be used after calling destroy().
117
- */
118
- destroy() {
119
- 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;
120
- }
121
- }
122
- export {
123
- h as MAX_PACKET_SIZE,
124
- d as OPUS_APPLICATION_VOIP,
125
- m as OPUS_RESET_STATE,
126
- l as OPUS_SET_BITRATE_REQUEST,
127
- u as OPUS_SET_COMPLEXITY_REQUEST,
128
- _ as OpusEncoder,
129
- a as getOpusModule
130
- };
@@ -1,118 +0,0 @@
1
- let l = null, o = null;
2
- async function u() {
3
- return o || (l || (l = (async () => {
4
- const e = (await import("./libopus-BEPFPIM_.js")).default;
5
- return o = await e({
6
- locateFile: (t) => t.endsWith(".wasm") ? new URL("../wasm/libopus.wasm", import.meta.url).href : t
7
- }), o;
8
- })()), l);
9
- }
10
- const c = 4028, n = 5760, h = 4e3;
11
- class m {
12
- module = null;
13
- decoder = 0;
14
- channels;
15
- sampleRate;
16
- // Pre-allocated WASM heap pointers (allocated once, reused)
17
- inputPtr = 0;
18
- outputPtr = 0;
19
- // Pre-allocated output arrays (reused, caller must copy if needed)
20
- channelBuffers;
21
- ready;
22
- constructor(e) {
23
- this.channels = e.channels, this.sampleRate = e.sampleRate, this.channelBuffers = Array.from({ length: this.channels }, () => new Float32Array(n)), this.ready = this.initialize();
24
- }
25
- async initialize() {
26
- this.module = await u();
27
- const e = this.module._malloc(4);
28
- this.decoder = this.module._opus_decoder_create(this.sampleRate, this.channels, e);
29
- const t = new DataView(this.module.HEAPU8.buffer).getInt32(e, !0);
30
- if (this.module._free(e), t !== 0)
31
- throw new Error(`opus_decoder_create failed with error code ${t}`);
32
- this.inputPtr = this.module._malloc(h), this.outputPtr = this.module._malloc(n * this.channels * 4);
33
- }
34
- /**
35
- * Decode an Opus frame.
36
- *
37
- * Returns views into pre-allocated buffers. The channelData arrays are reused
38
- * on each call, so callers MUST copy the data if it needs to persist beyond
39
- * the next decode call.
40
- *
41
- * @param opusData - The Opus-encoded audio frame
42
- * @returns Decoded audio with channel data and sample count
43
- */
44
- decodeFrame(e) {
45
- if (!this.module || !this.decoder)
46
- throw new Error("Decoder not initialized");
47
- if (e.length > h)
48
- throw new Error(`Opus packet too large: ${e.length} > ${h}`);
49
- this.module.HEAPU8.set(e, this.inputPtr);
50
- const t = this.module._opus_decode_float(
51
- this.decoder,
52
- this.inputPtr,
53
- e.length,
54
- this.outputPtr,
55
- n,
56
- 0
57
- // decodeFec = false
58
- );
59
- if (t < 0)
60
- throw new Error(`opus_decode_float failed with error code ${t}`);
61
- const i = this.outputPtr / 4;
62
- for (let s = 0; s < this.channels; s++) {
63
- const d = this.channelBuffers[s];
64
- for (let r = 0; r < t; r++)
65
- d[r] = this.module.HEAPF32[i + r * this.channels + s];
66
- }
67
- return {
68
- channelData: this.channelBuffers,
69
- samplesDecoded: t,
70
- sampleRate: this.sampleRate
71
- };
72
- }
73
- /**
74
- * Decode a frame and copy the result (when caller needs ownership).
75
- * Use this when you need the decoded audio to persist.
76
- */
77
- decodeFrameAndCopy(e) {
78
- const t = this.decodeFrame(e);
79
- return {
80
- channelData: t.channelData.map((i) => i.slice(0, t.samplesDecoded)),
81
- samplesDecoded: t.samplesDecoded,
82
- sampleRate: t.sampleRate
83
- };
84
- }
85
- /**
86
- * Reset the decoder state.
87
- * Call this when there's a discontinuity in the audio stream.
88
- */
89
- reset() {
90
- this.module && this.decoder && this.module._opus_decoder_ctl(this.decoder, c);
91
- }
92
- /**
93
- * Get the number of channels this decoder was created with.
94
- */
95
- getChannels() {
96
- return this.channels;
97
- }
98
- /**
99
- * Get the sample rate this decoder was created with.
100
- */
101
- getSampleRate() {
102
- return this.sampleRate;
103
- }
104
- /**
105
- * Release all resources.
106
- * The decoder cannot be used after calling destroy().
107
- */
108
- destroy() {
109
- 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;
110
- }
111
- }
112
- export {
113
- n as MAX_FRAME_SIZE,
114
- h as MAX_PACKET_SIZE,
115
- c as OPUS_RESET_STATE,
116
- m as OpusDecoder,
117
- u as getOpusModule
118
- };