index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. "use strict";
  2. var opusscript_native = require("./build/opusscript_native.js");
  3. var OpusApplication = {
  4. VOIP: 2048,
  5. AUDIO: 2049,
  6. RESTRICTED_LOWDELAY: 2051
  7. };
  8. var OpusError = {
  9. "0": "OK",
  10. "-1": "Bad argument",
  11. "-2": "Buffer too small",
  12. "-3": "Internal error",
  13. "-4": "Invalid packet",
  14. "-5": "Unimplemented",
  15. "-6": "Invalid state",
  16. "-7": "Memory allocation fail"
  17. };
  18. var VALID_SAMPLING_RATES = [8000, 12000, 16000, 24000, 48000];
  19. var MAX_FRAME_SIZE = 48000 * 60 / 1000;
  20. var MAX_PACKET_SIZE = 1276 * 3;
  21. function OpusScript(samplingRate, channels, application) {
  22. if(!~VALID_SAMPLING_RATES.indexOf(samplingRate)) {
  23. throw new RangeError(`${samplingRate} is an invalid sampling rate.`);
  24. }
  25. this.samplingRate = samplingRate;
  26. this.channels = channels || 1;
  27. this.application = application || OpusApplication.AUDIO;
  28. this.handler = new opusscript_native.OpusScriptHandler(this.samplingRate, this.channels, this.application);
  29. this.inPCMLength = MAX_FRAME_SIZE * this.channels * 2;
  30. this.inPCMPointer = opusscript_native._malloc(this.inPCMLength);
  31. this.inPCM = opusscript_native.HEAPU16.subarray(this.inPCMPointer, this.inPCMPointer + this.inPCMLength);
  32. this.inOpusPointer = opusscript_native._malloc(MAX_PACKET_SIZE);
  33. this.inOpus = opusscript_native.HEAPU8.subarray(this.inOpusPointer, this.inOpusPointer + MAX_PACKET_SIZE);
  34. this.outOpusPointer = opusscript_native._malloc(MAX_PACKET_SIZE);
  35. this.outOpus = opusscript_native.HEAPU8.subarray(this.outOpusPointer, this.outOpusPointer + MAX_PACKET_SIZE);
  36. this.outPCMLength = MAX_FRAME_SIZE * this.channels * 2;
  37. this.outPCMPointer = opusscript_native._malloc(this.outPCMLength);
  38. this.outPCM = opusscript_native.HEAPU16.subarray(this.outPCMPointer, this.outPCMPointer + this.outPCMLength);
  39. };
  40. OpusScript.prototype.encode = function encode(buffer, frameSize) {
  41. this.inPCM.set(buffer);
  42. var len = this.handler._encode(this.inPCM.byteOffset, buffer.length, this.outOpusPointer, frameSize);
  43. if(len < 0) {
  44. throw new Error("Encode error: " + OpusError["" + len]);
  45. }
  46. return new Buffer(this.outOpus.subarray(0, len));
  47. };
  48. OpusScript.prototype.decode = function decode(buffer) {
  49. this.inOpus.set(buffer);
  50. var len = this.handler._decode(this.inOpusPointer, buffer.length, this.outPCM.byteOffset);
  51. if(len < 0) {
  52. throw new Error("Decode error: " + OpusError["" + len]);
  53. }
  54. return new Buffer(this.outPCM.subarray(0, len * this.channels * 2));
  55. };
  56. OpusScript.prototype.encoderCTL = function encoderCTL(ctl, arg) {
  57. var len = this.handler._encoder_ctl(ctl, arg);
  58. if(len < 0) {
  59. throw new Error("Encoder CTL error: " + OpusError["" + len]);
  60. }
  61. };
  62. OpusScript.prototype.decoderCTL = function decoderCTL(ctl, arg) {
  63. var len = this.handler._decoder_ctl(ctl, arg);
  64. if(len < 0) {
  65. throw new Error("Decoder CTL error: " + OpusError["" + len]);
  66. }
  67. };
  68. OpusScript.prototype.delete = function del() {
  69. opusscript_native.OpusScriptHandler.destroy_handler(this.handler);
  70. opusscript_native._free(this.inPCMPointer);
  71. opusscript_native._free(this.inOpusPointer);
  72. opusscript_native._free(this.outOpusPointer);
  73. opusscript_native._free(this.outPCMPointer);
  74. };
  75. OpusScript.Application = OpusApplication;
  76. OpusScript.Error = OpusError;
  77. OpusScript.VALID_SAMPLING_RATES = VALID_SAMPLING_RATES;
  78. OpusScript.MAX_PACKET_SIZE = MAX_PACKET_SIZE;
  79. module.exports = OpusScript;