pcm-player.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. function PCMPlayer(option) {
  2. this.init(option);
  3. }
  4. PCMPlayer.prototype.init = function(option) {
  5. var defaults = {
  6. encoding: '16bitInt',
  7. channels: 1,
  8. sampleRate: 8000,
  9. flushingTime: 1000
  10. };
  11. this.option = Object.assign({}, defaults, option);
  12. this.samples = new Float32Array();
  13. this.flush = this.flush.bind(this);
  14. this.interval = setInterval(this.flush, this.option.flushingTime);
  15. this.maxValue = this.getMaxValue();
  16. this.typedArray = this.getTypedArray();
  17. this.createContext();
  18. };
  19. PCMPlayer.prototype.getMaxValue = function () {
  20. var encodings = {
  21. '8bitInt': 128,
  22. '16bitInt': 32768,
  23. '32bitInt': 2147483648,
  24. '32bitFloat': 1
  25. }
  26. return encodings[this.option.encoding] ? encodings[this.option.encoding] : encodings['16bitInt'];
  27. };
  28. PCMPlayer.prototype.getTypedArray = function () {
  29. var typedArrays = {
  30. '8bitInt': Int8Array,
  31. '16bitInt': Int16Array,
  32. '32bitInt': Int32Array,
  33. '32bitFloat': Float32Array
  34. }
  35. return typedArrays[this.option.encoding] ? typedArrays[this.option.encoding] : typedArrays['16bitInt'];
  36. };
  37. PCMPlayer.prototype.createContext = function() {
  38. this.audioCtx = new (window.AudioContext || window.webkitAudioContext)();
  39. this.gainNode = this.audioCtx.createGain();
  40. this.gainNode.gain.value = 1;
  41. this.gainNode.connect(this.audioCtx.destination);
  42. this.startTime = this.audioCtx.currentTime;
  43. };
  44. PCMPlayer.prototype.isTypedArray = function(data) {
  45. return (data.byteLength && data.buffer && data.buffer.constructor == ArrayBuffer);
  46. };
  47. PCMPlayer.prototype.feed = function(data) {
  48. if (!this.isTypedArray(data)) return;
  49. data = this.getFormatedValue(data);
  50. var tmp = new Float32Array(this.samples.length + data.length);
  51. tmp.set(this.samples, 0);
  52. tmp.set(data, this.samples.length);
  53. this.samples = tmp;
  54. };
  55. PCMPlayer.prototype.getFormatedValue = function(data) {
  56. var data = new this.typedArray(data.buffer),
  57. float32 = new Float32Array(data.length),
  58. i;
  59. for (i = 0; i < data.length; i++) {
  60. float32[i] = data[i] / this.maxValue;
  61. }
  62. return float32;
  63. };
  64. PCMPlayer.prototype.volume = function(volume) {
  65. this.gainNode.gain.value = volume;
  66. };
  67. PCMPlayer.prototype.destroy = function() {
  68. if (this.interval) {
  69. clearInterval(this.interval);
  70. }
  71. this.samples = null;
  72. this.audioCtx.close();
  73. this.audioCtx = null;
  74. };
  75. PCMPlayer.prototype.flush = function() {
  76. if (!this.samples.length) return;
  77. var bufferSource = this.audioCtx.createBufferSource(),
  78. length = this.samples.length / this.option.channels,
  79. audioBuffer = this.audioCtx.createBuffer(this.option.channels, length, this.option.sampleRate),
  80. audioData,
  81. channel,
  82. offset,
  83. i,
  84. decrement;
  85. for (channel = 0; channel < this.option.channels; channel++) {
  86. audioData = audioBuffer.getChannelData(channel);
  87. offset = channel;
  88. decrement = 50;
  89. for (i = 0; i < length; i++) {
  90. audioData[i] = this.samples[offset];
  91. /* fadein */
  92. if (i < 50) {
  93. audioData[i] = (audioData[i] * i) / 50;
  94. }
  95. /* fadeout*/
  96. if (i >= (length - 51)) {
  97. audioData[i] = (audioData[i] * decrement--) / 50;
  98. }
  99. offset += this.option.channels;
  100. }
  101. }
  102. if (this.startTime < this.audioCtx.currentTime) {
  103. this.startTime = this.audioCtx.currentTime;
  104. }
  105. //console.log('start vs current '+this.startTime+' vs '+this.audioCtx.currentTime+' duration: '+audioBuffer.duration);
  106. bufferSource.buffer = audioBuffer;
  107. bufferSource.connect(this.gainNode);
  108. bufferSource.start(this.startTime);
  109. this.startTime += audioBuffer.duration;
  110. this.samples = new Float32Array();
  111. };