audioPlayer.esm.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. function t(t, a, i) {
  2. for (var o = 0; o < i.length; o++) t.setUint8(a + o, i.charCodeAt(o));
  3. }
  4. function a(a, i, o) {
  5. var e = (function (a, i, o, e, s) {
  6. var u = a.reduce(function (t, a) {
  7. return t + a.byteLength;
  8. }, 0),
  9. n = new ArrayBuffer(44 + u),
  10. r = new DataView(n),
  11. h = o,
  12. d = 0;
  13. return (
  14. t(r, d, "RIFF"),
  15. (d += 4),
  16. r.setUint32(d, 36 + u, !0),
  17. t(r, (d += 4), "WAVE"),
  18. t(r, (d += 4), "fmt "),
  19. (d += 4),
  20. r.setUint32(d, 16, !0),
  21. (d += 4),
  22. r.setUint16(d, 1, !0),
  23. (d += 2),
  24. r.setUint16(d, h, !0),
  25. (d += 2),
  26. r.setUint32(d, i, !0),
  27. (d += 4),
  28. r.setUint32(d, h * i * (e / 8), !0),
  29. (d += 4),
  30. r.setUint16(d, h * (e / 8), !0),
  31. (d += 2),
  32. r.setUint16(d, e, !0),
  33. t(r, (d += 2), "data"),
  34. (d += 4),
  35. r.setUint32(d, u, !0),
  36. (d += 4),
  37. a.forEach(function (t) {
  38. for (var a = new DataView(t.buffer), i = 0; i < t.byteLength; )
  39. r.setUint8(d, a.getUint8(i)), d++, i++;
  40. }),
  41. r
  42. );
  43. })(a, i || 16e3, 1, o || 16);
  44. return new Blob([e], { type: "audio/wav" });
  45. }
  46. var i = (function () {
  47. function t(t) {
  48. (this.toSampleRate = 22050),
  49. (this.resumePlayDuration = 1e3),
  50. (this.fromSampleRate = 16e3),
  51. (this.isAudioDataEnded = !1),
  52. (this.status = "uninit"),
  53. (this.audioDatas = []),
  54. (this.pcmAudioDatas = []),
  55. (this.audioDataOffset = 0),
  56. // (this.processor = new Worker("".concat(t, "/processor.worker.js")));
  57. (this.processor = new Worker("/admin/processor.worker.js"));
  58. }
  59. return (
  60. (t.prototype.postMessage = function (t) {
  61. var a = t.type,
  62. i = t.data,
  63. o = t.isLastData;
  64. "uninit" !== this.status &&
  65. (this.processor.postMessage({ type: a, data: i }),
  66. (this.isAudioDataEnded = o));
  67. }),
  68. (t.prototype.playAudio = function () {
  69. var t = this;
  70. if ((clearTimeout(this.playAudioTime), this.audioContext)) {
  71. for (
  72. var a = 0, i = this.audioDataOffset;
  73. i < this.audioDatas.length;
  74. i++
  75. )
  76. a += this.audioDatas[i].length;
  77. if (!a)
  78. return void (
  79. "play" === this.status &&
  80. (this.isAudioDataEnded || this.resumePlayDuration <= 0
  81. ? this.stop()
  82. : (this.playAudioTime = setTimeout(function () {
  83. t.playAudio();
  84. }, this.resumePlayDuration)))
  85. );
  86. for (
  87. var o = this.audioContext.createBuffer(1, a, this.toSampleRate),
  88. e = o.getChannelData(0),
  89. s = this.audioDatas[this.audioDataOffset],
  90. u = 0;
  91. s;
  92. ) {
  93. if (((this.audioDataOffset += 1), o.copyToChannel))
  94. o.copyToChannel(s, 0, u), (u += s.length);
  95. else for (i = 0; i < s.length; i++) e[i] = s[i];
  96. s = this.audioDatas[this.audioDataOffset];
  97. }
  98. var n = this.audioContext.createBufferSource();
  99. (this.bufferSource = n),
  100. (n.buffer = o),
  101. n.connect(this.audioContext.destination),
  102. n.start(),
  103. (n.onended = function (a) {
  104. "play" === t.status &&
  105. (t.audioDatas.length
  106. ? t.playAudio()
  107. : t.isAudioDataEnded || t.resumePlayDuration <= 0
  108. ? t.stop()
  109. : (t.playAudioTime = setTimeout(function () {
  110. t.playAudio();
  111. }, t.resumePlayDuration)));
  112. });
  113. }
  114. }),
  115. (t.prototype.reset = function () {
  116. var t;
  117. (this.processor.onmessage = null),
  118. (this.audioDataOffset = 0),
  119. (this.audioDatas = []),
  120. (this.pcmAudioDatas = []),
  121. (this.status = "uninit"),
  122. (this.isAudioDataEnded = !1),
  123. clearTimeout(this.playAudioTime);
  124. try {
  125. null === (t = this.bufferSource) || void 0 === t || t.stop();
  126. } catch (t) {
  127. console.log(t);
  128. }
  129. }),
  130. (t.prototype.start = function (t) {
  131. var a = this,
  132. i = void 0 === t ? {} : t,
  133. o = i.autoPlay,
  134. e = void 0 === o || o,
  135. s = i.sampleRate,
  136. u = void 0 === s ? 16e3 : s,
  137. n = i.resumePlayDuration,
  138. r = void 0 === n ? 1e3 : n;
  139. this.reset(), (this.status = "init"), (this.resumePlayDuration = r);
  140. var h = u,
  141. d = Math.max(h, 22050);
  142. (d = Math.min(d, 96e3)),
  143. (this.fromSampleRate = h),
  144. (this.toSampleRate = d),
  145. this.processor.postMessage({
  146. type: "init",
  147. data: { fromSampleRate: h, toSampleRate: d },
  148. }),
  149. (this.processor.onmessage = function (t) {
  150. // console.log("processor.onmessage");
  151. // console.log(t.data);
  152. var i = t.data,
  153. o = i.audioData,
  154. s = i.pcmAudioData;
  155. a.audioDatas.push(o),
  156. a.pcmAudioDatas.push(s),
  157. 1 === a.audioDatas.length && e && "init" === a.status && a.play();
  158. });
  159. }),
  160. (t.prototype.play = function () {
  161. var t;
  162. this.audioContext ||
  163. ((this.audioContext = new (window.AudioContext ||
  164. window.webkitAudioContext)()),
  165. this.audioContext.resume()),
  166. this.audioContext &&
  167. ((this.status = "play"),
  168. null === (t = this.onPlay) || void 0 === t || t.call(this),
  169. this.playAudio());
  170. }),
  171. (t.prototype.stop = function () {
  172. var t, a;
  173. (this.audioDataOffset = 0),
  174. (this.status = "stop"),
  175. clearTimeout(this.playAudioTime);
  176. try {
  177. null === (t = this.bufferSource) || void 0 === t || t.stop(),
  178. null === (a = this.onStop) ||
  179. void 0 === a ||
  180. a.call(this, this.audioDatas);
  181. } catch (t) {
  182. console.log(t);
  183. }
  184. }),
  185. (t.prototype.getAudioDataBlob = function (t) {
  186. // console.log("processor.getAudioDataBlob");
  187. var i, o;
  188. if (null === (i = this.pcmAudioDatas) || void 0 === i ? void 0 : i.length)
  189. return "wav" === t
  190. ? a(this.pcmAudioDatas, this.fromSampleRate, 16)
  191. : ((o = this.pcmAudioDatas), new Blob(o, { type: "audio/pcm" }));
  192. }),
  193. t
  194. );
  195. })();
  196. export { i as default };