mp2-wasm.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. JSMpeg.Decoder.MP2AudioWASM = (function(){ "use strict";
  2. // Based on kjmp2 by Martin J. Fiedler
  3. // http://keyj.emphy.de/kjmp2/
  4. var MP2WASM = function(options) {
  5. JSMpeg.Decoder.Base.call(this, options);
  6. this.onDecodeCallback = options.onAudioDecode;
  7. this.module = options.wasmModule;
  8. this.bufferSize = options.audioBufferSize || 128*1024;
  9. this.bufferMode = options.streaming
  10. ? JSMpeg.BitBuffer.MODE.EVICT
  11. : JSMpeg.BitBuffer.MODE.EXPAND;
  12. this.sampleRate = 0;
  13. };
  14. MP2WASM.prototype = Object.create(JSMpeg.Decoder.Base.prototype);
  15. MP2WASM.prototype.constructor = MP2WASM;
  16. MP2WASM.prototype.initializeWasmDecoder = function() {
  17. if (!this.module.instance) {
  18. console.warn('JSMpeg: WASM module not compiled yet');
  19. return;
  20. }
  21. this.instance = this.module.instance;
  22. this.functions = this.module.instance.exports;
  23. this.decoder = this.functions._mp2_decoder_create(this.bufferSize, this.bufferMode);
  24. };
  25. MP2WASM.prototype.destroy = function() {
  26. if (!this.decoder) {
  27. return;
  28. }
  29. this.functions._mp2_decoder_destroy(this.decoder);
  30. };
  31. MP2WASM.prototype.bufferGetIndex = function() {
  32. if (!this.decoder) {
  33. return;
  34. }
  35. return this.functions._mp2_decoder_get_index(this.decoder);
  36. };
  37. MP2WASM.prototype.bufferSetIndex = function(index) {
  38. if (!this.decoder) {
  39. return;
  40. }
  41. this.functions._mp2_decoder_set_index(this.decoder, index);
  42. };
  43. MP2WASM.prototype.bufferWrite = function(buffers) {
  44. if (!this.decoder) {
  45. this.initializeWasmDecoder();
  46. }
  47. var totalLength = 0;
  48. for (var i = 0; i < buffers.length; i++) {
  49. totalLength += buffers[i].length;
  50. }
  51. var ptr = this.functions._mp2_decoder_get_write_ptr(this.decoder, totalLength);
  52. for (var i = 0; i < buffers.length; i++) {
  53. this.instance.heapU8.set(buffers[i], ptr);
  54. ptr += buffers[i].length;
  55. }
  56. this.functions._mp2_decoder_did_write(this.decoder, totalLength);
  57. return totalLength;
  58. };
  59. MP2WASM.prototype.decode = function() {
  60. var startTime = JSMpeg.Now();
  61. if (!this.decoder) {
  62. return false;
  63. }
  64. var decodedBytes = this.functions._mp2_decoder_decode(this.decoder);
  65. if (decodedBytes === 0) {
  66. return false;
  67. }
  68. if (!this.sampleRate) {
  69. this.sampleRate = this.functions._mp2_decoder_get_sample_rate(this.decoder);
  70. }
  71. if (this.destination) {
  72. // Create a Float32 View into the modules output channel data
  73. var leftPtr = this.functions._mp2_decoder_get_left_channel_ptr(this.decoder),
  74. rightPtr = this.functions._mp2_decoder_get_right_channel_ptr(this.decoder);
  75. var leftOffset = leftPtr / Float32Array.BYTES_PER_ELEMENT,
  76. rightOffset = rightPtr / Float32Array.BYTES_PER_ELEMENT;
  77. var left = this.instance.heapF32.subarray(leftOffset, leftOffset + MP2WASM.SAMPLES_PER_FRAME),
  78. right = this.instance.heapF32.subarray(rightOffset, rightOffset + MP2WASM.SAMPLES_PER_FRAME);
  79. this.destination.play(this.sampleRate, left, right);
  80. }
  81. this.advanceDecodedTime(MP2WASM.SAMPLES_PER_FRAME / this.sampleRate);
  82. var elapsedTime = JSMpeg.Now() - startTime;
  83. if (this.onDecodeCallback) {
  84. this.onDecodeCallback(this, elapsedTime);
  85. }
  86. return true;
  87. };
  88. MP2WASM.prototype.getCurrentTime = function() {
  89. var enqueuedTime = this.destination ? this.destination.enqueuedTime : 0;
  90. return this.decodedTime - enqueuedTime;
  91. };
  92. MP2WASM.SAMPLES_PER_FRAME = 1152;
  93. return MP2WASM;
  94. })();