forked from ImagicTheCat/libopusjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
88 lines (74 loc) · 2.75 KB
/
api.js
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
// Encoder
// create encoder
// channels: 1-2
// samplerate: 8000,12000,16000,24000,48000
// bitrate: see Opus recommended bitrates
// frame_size: frame size in milliseconds (2.5,5,10,20,40,60), 20 is recommended
// voice_optimization: true/false
function Encoder(channels, samplerate, bitrate, frame_size, voice_optimization)
{
this.enc = Module._Encoder_new.apply(null, arguments);
this.out = Module._String_new();
}
// free encoder memory
Encoder.prototype.destroy = function()
{
Module._Encoder_delete(this.enc);
Module._String_delete(this.out);
}
// add samples to the encoder buffer
// samples: Int16Array of interleaved (if multiple channels) samples
Encoder.prototype.input = function(samples)
{
var ptr = Module._malloc(samples.length*samples.BYTES_PER_ELEMENT);
var pdata = new Uint8Array(Module.HEAPU8.buffer, ptr, samples.length*samples.BYTES_PER_ELEMENT);
pdata.set(new Uint8Array(samples.buffer, samples.byteOffset, samples.length*samples.BYTES_PER_ELEMENT));
Module._Encoder_input(this.enc, ptr, samples.length);
Module._free(ptr);
}
// output the next encoded packet
// return Uint8Array (valid until the next output call) or null if there is no packet to output
Encoder.prototype.output = function()
{
var ok = Module._Encoder_output(this.enc, this.out);
if(ok)
return new Uint8Array(Module.HEAPU8.buffer, Module._String_data(this.out), Module._String_size(this.out));
}
// Decoder
// create decoder
// channels and samplerate should match the encoder options
function Decoder(channels, samplerate)
{
this.dec = Module._Decoder_new.apply(null, arguments);
this.out = Module._Int16Array_new();
}
// free decoder memory
Decoder.prototype.destroy = function()
{
Module._Decoder_delete(this.dec);
Module._Int16Array_delete(this.out);
}
// add packet to the decoder buffer
// packet: Uint8Array
Decoder.prototype.input = function(packet)
{
var ptr = Module._malloc(packet.length*packet.BYTES_PER_ELEMENT);
var pdata = new Uint8Array(Module.HEAPU8.buffer, ptr, packet.length*packet.BYTES_PER_ELEMENT);
pdata.set(new Uint8Array(packet.buffer, packet.byteOffset, packet.length*packet.BYTES_PER_ELEMENT));
Module._Decoder_input(this.dec, ptr, packet.length);
Module._free(ptr);
}
// output the next decoded samples
// return samples (interleaved if multiple channels) as Int16Array (valid until the next output call) or null if there is no output
Decoder.prototype.output = function()
{
var ok = Module._Decoder_output(this.dec, this.out);
if(ok)
return new Int16Array(Module.HEAPU8.buffer, Module._Int16Array_data(this.out), Module._Int16Array_size(this.out));
}
//export objects
Module.Encoder = Encoder;
Module.Decoder = Decoder;
//make the module global if not using nodejs
if(Module["ENVIRONMENT"] != "NODE")
libopus = Module;