1 line
3.3 KiB
JavaScript
1 line
3.3 KiB
JavaScript
"use strict";function e(e){for(var t=2*o+e.length,r=s.createBuffer(e.numberOfChannels,t,e.sampleRate),n=0;n<e.numberOfChannels;n++){for(var i=e.getChannelData(n),a=new Float32Array(2*o+i.length),u=0,c=0;o>c;c++)a[u]=0,u++;for(var c=0;c<i.length;c++)a[u]=i[c],u++;for(var c=0;o>c;c++)a[u]=0,u++;r.copyToChannel(a,n)}return r}function t(e){for(var t=new Float32Array(e.length),r=s.createBuffer(1,e.length,e.sampleRate),n=-o,i=0;i<e.length;i++)t[i]=n,n++;return r.getChannelData(0).set(t),r}var r=function(){function e(e,t){for(var r in t){var n=t[r];n.configurable=!0,n.value&&(n.writable=!0)}Object.defineProperties(e,t)}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}(),n=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},s=new AudioContext,o=44100,i=function(){function i(e){var t=void 0===arguments[1]?!1:arguments[1];n(this,i),this.url=e,this.reverse=t,this.scriptNode=s.createScriptProcessor(256,1,1),this.scriptNode.onaudioprocess=this.processAudio.bind(this),this._currentTime=0,this.started=!1,this.callbacks=[]}return r(i,{bufferTime:{get:function(){return o/this.buffer.sampleRate}},currentTime:{get:function(){return this._currentTime},set:function(e){this.stop(),this.createNodes(),this.sourceNode.start(0,e+this.bufferTime),this.counterNode.start(0,e+this.bufferTime),this.started=!0,this._currentTime=e,this.playbackRate=1}},playbackRate:{set:function(e){this.sourceNode.playbackRate.value=e,this.counterNode.playbackRate.value=e},get:function(){return this.sourceNode.playbackRate.value}},getSamples:{value:function(e){this.originalBufferArray?e(this.originalBufferArray,this.sampleRate):this.callbacks.push(e)}},processAudio:{value:function(e){var t=e.inputBuffer.getChannelData(0),r=t[t.length-1]||0;this._currentTime=this.duration*r/this.numSamples,this.onCurrentTime&&this.onCurrentTime(this._currentTime)}},play:{value:function(){this.started=!0,this.sourceNode.start(),this.counterNode.start()}},stop:{value:function(){this.started&&(this.sourceNode.stop(),this.counterNode.stop()),this.started=!1}},createNodes:{value:function(){this.sourceNode&&this.counterNode&&(this.sourceNode.disconnect(),this.counterNode.disconnect()),this.sourceNode=s.createBufferSource(),this.sourceNode.buffer=this.buffer,this.sourceNode.connect(s.destination),this.counterNode=s.createBufferSource(),this.counterNode.buffer=this.counterBuffer,this.counterNode.connect(this.scriptNode),this.scriptNode.connect(s.destination)}},dispose:{value:function(){this.stop(),this.scriptNode.onaudioprocess=null,this.sourceNode&&this.counterNode&&(this.sourceNode.disconnect(),this.counterNode.disconnect()),this.scriptNode.disconnect(),this.scriptNode=null,this.sourceNode=null,this.counterNode=null,this.disposed=!0}},onLoad:{value:function(r,n){var o=this;s.decodeAudioData(r.response,function(r){if(o.duration=r.length/r.sampleRate,o.sampleRate=r.sampleRate,o.numSamples=r.length,o.buffer=e(r),o.counterBuffer=t(o.buffer),o.originalBufferArray=r.getChannelData(0),o.reverse){o._currentTime=o.duration;for(var s=0;s<o.buffer.numberOfChannels;s++)Array.prototype.reverse.call(o.buffer.getChannelData(s))}o.disposed||(o.createNodes(),n())})}},load:{value:function(e){var t=new XMLHttpRequest;t.open("GET",this.url,!0),t.responseType="arraybuffer",t.onload=this.onLoad.bind(this,t,e),t.send()}}}),i}();module.exports=i; |