BlueGit

blue-star danique-bot

Files

danique-bot / node_modules / cjopus / lib / Opus.js

Opus.js

Import template
timfa Tim · pushed by timfa · 4 years ago
c788ad20 History
Opus.js 4.2 KiB Download Raw
1var Module = require('./CJOpus.js');
2var Opus = {};
3
4Opus.APPLICATIONS = {
5 VOIP: 2048,
6 AUDIO: 2049,
7 RESTRICTED_LOWDELAY:2051
8};
9Opus.ERRORS = {
10 OK: 0,
11 BAD_ARG: -1,
12 BUFFER_TOO_SMALL: -2,
13 INTERNAL_ERROR: -3,
14 INVALID_PACKET: -4,
15 UNIMPLEMENTED: -5,
16 INVALID_STATE: -6,
17 ALLOC_FAIL: -7
18};
19Opus.SAMPLE_RATES = [8000, 12000, 16000, 24000, 48000];
20Opus.CHANNELS = [1, 2];
21Opus.OpusEncoder = OpusEncoder;
22
23var APPLICATION_CODES = Object.keys(Opus.APPLICATIONS).map(function(a) { return Opus.APPLICATIONS[a]; });
24var ERROR_CODES = Object.keys(Opus.ERRORS).map(function(e) { return Opus.ERRORS[e]; });
25
26function OpusEncoder(sample_rate, channels, application) {
27 if (Opus.CHANNELS.indexOf(channels) < 0) channels = 2;
28 if (Opus.SAMPLE_RATES.indexOf(sample_rate) < 0) sample_rate = 48000;
29 if (APPLICATION_CODES.indexOf(application) < 0) application = Opus.APPLICATIONS.AUDIO;
30
31 Object.defineProperty(this, 'encoder', {
32 value: Module._create_encoder_and_decoder(sample_rate, channels, application)
33 });
34
35 var encoderError = Module._get_encoder_error(this.encoder);
36 var decoderError = Module._get_decoder_error(this.encoder);
37
38 if (encoderError || decoderError) {
39 throw new OpusError( encoderError || decoderError );
40 }
41
42 Object.defineProperties(this, {
43 channels: {value: channels},
44
45 //Is this correct?
46 encodeFrameSize: {value: sample_rate / 50},
47
48 // --Encoding--
49 pcmOffset: {value: Module._get_in_pcm_offset(this.encoder)},
50 encodedOffset: {value: Module._get_encoded_offset(this.encoder)},
51
52 // --Decoding--
53 opusOffset: {value: Module._get_in_opus_offset(this.encoder)},
54 decodedOffset: {value: Module._get_decoded_little_endian_offset(this.encoder)}
55 });
56
57 this.destroyed = false;
58}
59
60OpusEncoder.prototype.encode = function( PCM ) {
61 if ( this.destroyed ) {
62 throw new Error( "Attempting to encode using a destroyed decoder" );
63 }
64 if ( !isExpectedType(PCM) ) {
65 throw new TypeError( Object.prototype.toString.call(PCM) + " is not a valid type (Buffer, TypedArray, Array)");
66 }
67
68 Module.HEAPU8.set( PCM, this.pcmOffset );
69
70 var length = Module._encode(this.encoder, PCM.length, this.encodeFrameSize);
71
72 if (length < 0) throw new OpusError(length);
73
74 return Module.HEAPU8.slice( this.encodedOffset, this.encodedOffset + length );
75}
76
77OpusEncoder.prototype.decode = function( OPUS ) {
78 if ( this.destroyed ) {
79 throw new Error( "Attempting to decode using a destroyed decoder" );
80 }
81 if ( !isExpectedType(OPUS) ) {
82 throw new TypeError( Object.prototype.toString.call(OPUS) + " is not a valid type (Buffer, TypedArray, Array)");
83 }
84
85 Module.HEAPU8.set( OPUS, this.opusOffset );
86
87 var frameSize = Module._decode( this.encoder, OPUS.length );
88
89 if (frameSize < 0) throw new OpusError(frameSize);
90
91 return Module.HEAP16.slice( this.decodedOffset / 2, (this.decodedOffset /2) + (frameSize * 2 * this.channels) );
92}
93
94OpusEncoder.prototype.encodeUnsafe = function( PCM ) {
95 Module.HEAPU8.set( PCM, this.pcmOffset );
96 return Module.HEAPU8.subarray(
97 this.encodedOffset,
98 this.encodedOffset + Module._encode(
99 this.encoder,
100 PCM.length,
101 this.encodeFrameSize
102 )
103 );
104}
105
106OpusEncoder.prototype.decodeUnsafe = function( OPUS ) {
107 Module.HEAPU8.set( OPUS, this.opusOffset );
108 return Module.HEAP16.subarray(
109 this.decodedOffset / 2,
110 (this.decodedOffset / 2) +
111 (Module._decode(
112 this.encoder,
113 OPUS.length
114 ) * 2 * this.channels)
115 );
116}
117
118OpusEncoder.prototype.destroy = function() {
119 if ( this.destroyed ) {
120 throw new Error( "Decoder already destroyed" );
121 }
122
123 Module._destroy_encoder(this.encoder);
124 this.destroyed = true;
125}
126
127function OpusError(code) {
128 var error = new Error(Object.keys(Opus.ERRORS)[ERROR_CODES.indexOf(code)]);
129 error.code = code;
130 return error;
131}
132
133function isExpectedType(t) {
134 return t instanceof Buffer || ArrayBuffer.isView(t) || Array.isArray(t);
135}
136
137module.exports = Opus;