pcm-player.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. document.documentElement.addEventListener('mousedown', () => {
  44. console.log(this.audioCtx.state);
  45. if (this.audioCtx.state !== 'running') {
  46. this.audioCtx.resume();
  47. console.log(this.audioCtx.resume());
  48. }
  49. });
  50. };
  51. PCMPlayer.prototype.isTypedArray = function (data) {
  52. return (data.byteLength && data.buffer && data.buffer.constructor == ArrayBuffer);
  53. };
  54. PCMPlayer.prototype.feed = function (data) {
  55. if (!this.isTypedArray(data)) return;
  56. data = this.getFormatedValue(data);
  57. var tmp = new Float32Array(this.samples.length + data.length);
  58. tmp.set(this.samples, 0);
  59. tmp.set(data, this.samples.length);
  60. this.samples = tmp;
  61. };
  62. PCMPlayer.prototype.getFormatedValue = function (data) {
  63. var data = new this.typedArray(data.buffer),
  64. float32 = new Float32Array(data.length),
  65. i;
  66. for (i = 0; i < data.length; i++) {
  67. float32[i] = data[i] / this.maxValue;
  68. }
  69. return float32;
  70. };
  71. PCMPlayer.prototype.volume = function (volume) {
  72. this.gainNode.gain.value = volume;
  73. };
  74. PCMPlayer.prototype.destroy = function () {
  75. if (this.interval) {
  76. clearInterval(this.interval);
  77. }
  78. this.samples = null;
  79. this.audioCtx.close();
  80. this.audioCtx = null;
  81. };
  82. PCMPlayer.prototype.flush = function () {
  83. if (!this.samples.length) return;
  84. var bufferSource = this.audioCtx.createBufferSource(),
  85. length = this.samples.length / this.option.channels,
  86. audioBuffer = this.audioCtx.createBuffer(this.option.channels, length, this.option.sampleRate),
  87. audioData,
  88. channel,
  89. offset,
  90. i,
  91. decrement;
  92. for (channel = 0; channel < this.option.channels; channel++) {
  93. audioData = audioBuffer.getChannelData(channel);
  94. offset = channel;
  95. decrement = 50;
  96. for (i = 0; i < length; i++) {
  97. audioData[i] = this.samples[offset];
  98. /* fadein */
  99. if (i < 50) {
  100. audioData[i] = (audioData[i] * i) / 50;
  101. }
  102. /* fadeout*/
  103. if (i >= (length - 51)) {
  104. audioData[i] = (audioData[i] * decrement--) / 50;
  105. }
  106. offset += this.option.channels;
  107. }
  108. }
  109. if (this.startTime < this.audioCtx.currentTime) {
  110. this.startTime = this.audioCtx.currentTime;
  111. }
  112. bufferSource.buffer = audioBuffer;
  113. bufferSource.connect(this.gainNode);
  114. bufferSource.start(this.startTime);
  115. this.startTime += audioBuffer.duration;
  116. this.samples = new Float32Array();
  117. };