pcm-player.js 3.9 KB

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