@ikonai/sdk-libopus 1.0.79 → 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 +5 -44
- package/lib/opus-constants.js +4 -37
- package/lib/opus-decoder.js +62 -105
- package/lib/opus-encoder.js +62 -116
- package/lib/opus-module.js +12 -15
- package/package.json +1 -1
- package/vite.config.d.ts +2 -0
- package/wasm/libopus.wasm.js +0 -4
package/index.js
CHANGED
|
@@ -1,44 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
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 };
|
package/lib/opus-constants.js
CHANGED
|
@@ -1,37 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
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 };
|
package/lib/opus-decoder.js
CHANGED
|
@@ -1,106 +1,63 @@
|
|
|
1
|
-
import { getOpusModule as
|
|
2
|
-
import { MAX_FRAME_SIZE as
|
|
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
|
-
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 };
|
package/lib/opus-encoder.js
CHANGED
|
@@ -1,117 +1,63 @@
|
|
|
1
|
-
import { getOpusModule as
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
class
|
|
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
|
-
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 };
|
package/lib/opus-module.js
CHANGED
|
@@ -1,17 +1,14 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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
|
|
12
|
-
|
|
10
|
+
function i() {
|
|
11
|
+
return n !== null;
|
|
13
12
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
a as isModuleLoaded
|
|
17
|
-
};
|
|
13
|
+
//#endregion
|
|
14
|
+
export { r as getOpusModule, i as isModuleLoaded };
|
package/package.json
CHANGED
package/vite.config.d.ts
ADDED