pcm-player.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. $.alert({
  18. title: '提示',
  19. text: '开始使用云手机',
  20. onOK: () => {
  21. this.createContext();
  22. }
  23. });
  24. };
  25. PCMPlayer.prototype.getMaxValue = function () {
  26. var encodings = {
  27. '8bitInt': 128,
  28. '16bitInt': 32768,
  29. '32bitInt': 2147483648,
  30. '32bitFloat': 1
  31. }
  32. return encodings[this.option.encoding] ? encodings[this.option.encoding] : encodings['16bitInt'];
  33. };
  34. PCMPlayer.prototype.getTypedArray = function () {
  35. var typedArrays = {
  36. '8bitInt': Int8Array,
  37. '16bitInt': Int16Array,
  38. '32bitInt': Int32Array,
  39. '32bitFloat': Float32Array
  40. }
  41. return typedArrays[this.option.encoding] ? typedArrays[this.option.encoding] : typedArrays['16bitInt'];
  42. };
  43. PCMPlayer.prototype.createContext = function () {
  44. this.audioCtx = new (window.AudioContext || window.webkitAudioContext)();
  45. this.gainNode = this.audioCtx.createGain();
  46. this.gainNode.gain.value = 1;
  47. this.gainNode.connect(this.audioCtx.destination);
  48. this.startTime = this.audioCtx.currentTime;
  49. };
  50. PCMPlayer.prototype.isTypedArray = function (data) {
  51. return (data.byteLength && data.buffer && data.buffer.constructor == ArrayBuffer);
  52. };
  53. PCMPlayer.prototype.feed = function (data) {
  54. if (!this.isTypedArray(data)) return;
  55. data = this.getFormatedValue(data);
  56. var tmp = new Float32Array(this.samples.length + data.length);
  57. tmp.set(this.samples, 0);
  58. tmp.set(data, this.samples.length);
  59. this.samples = tmp;
  60. };
  61. PCMPlayer.prototype.getFormatedValue = function (data) {
  62. var data = new this.typedArray(data.buffer),
  63. float32 = new Float32Array(data.length),
  64. i;
  65. for (i = 0; i < data.length; i++) {
  66. float32[i] = data[i] / this.maxValue;
  67. }
  68. return float32;
  69. };
  70. PCMPlayer.prototype.volume = function (volume) {
  71. this.gainNode.gain.value = volume;
  72. };
  73. PCMPlayer.prototype.destroy = function () {
  74. if (this.interval) {
  75. clearInterval(this.interval);
  76. }
  77. this.samples = null;
  78. this.audioCtx.close();
  79. this.audioCtx = null;
  80. };
  81. PCMPlayer.prototype.flush = function () {
  82. if (!this.samples.length) return;
  83. var bufferSource = this.audioCtx.createBufferSource(),
  84. length = this.samples.length / this.option.channels,
  85. audioBuffer = this.audioCtx.createBuffer(this.option.channels, length, this.option.sampleRate),
  86. audioData,
  87. channel,
  88. offset,
  89. i,
  90. decrement;
  91. for (channel = 0; channel < this.option.channels; channel++) {
  92. audioData = audioBuffer.getChannelData(channel);
  93. offset = channel;
  94. decrement = 50;
  95. for (i = 0; i < length; i++) {
  96. audioData[i] = this.samples[offset];
  97. /* fadein */
  98. if (i < 50) {
  99. audioData[i] = (audioData[i] * i) / 50;
  100. }
  101. /* fadeout*/
  102. if (i >= (length - 51)) {
  103. audioData[i] = (audioData[i] * decrement--) / 50;
  104. }
  105. offset += this.option.channels;
  106. }
  107. }
  108. if (this.startTime < this.audioCtx.currentTime) {
  109. this.startTime = this.audioCtx.currentTime;
  110. }
  111. bufferSource.buffer = audioBuffer;
  112. bufferSource.connect(this.gainNode);
  113. bufferSource.start(this.startTime);
  114. this.startTime += audioBuffer.duration;
  115. this.samples = new Float32Array();
  116. };