pcm-player.js 3.9 KB

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