Tone.js/build/Tone.min.js

11 lines
92 KiB
JavaScript
Raw Normal View History

/**
* Tone.js
*
* @author Yotam Mann
*
* @license http://opensource.org/licenses/MIT MIT License 2014
*/
2014-09-06 19:57:22 +00:00
!function(t){"function"!=typeof define&&"function"!=typeof t.Tone&&(t.define=function(){var e=arguments[arguments.length-1],i=arguments[0];"Tone/core/Tone"===i?t.Tone=e():"function"==typeof e&&e(t.Tone)})}(this),define("Tone/core/Tone",[],function(){function t(t){return void 0===t}var e;if(t(window.AudioContext)&&(window.AudioContext=window.webkitAudioContext),t(window.OfflineAudioContext)&&(window.OfflineAudioContext=window.webkitOfflineAudioContext),t(AudioContext))throw new Error("Web Audio is not supported in this browser");e=new AudioContext,"function"!=typeof AudioContext.prototype.createGain&&(AudioContext.prototype.createGain=AudioContext.prototype.createGainNode),"function"!=typeof AudioContext.prototype.createDelay&&(AudioContext.prototype.createDelay=AudioContext.prototype.createDelayNode),"function"!=typeof AudioContext.prototype.createPeriodicWave&&(AudioContext.prototype.createPeriodicWave=AudioContext.prototype.createWaveTable),"function"!=typeof AudioBufferSourceNode.prototype.start&&(AudioBufferSourceNode.prototype.start=AudioBufferSourceNode.prototype.noteGrainOn),"function"!=typeof AudioBufferSourceNode.prototype.stop&&(AudioBufferSourceNode.prototype.stop=AudioBufferSourceNode.prototype.noteOff),"function"!=typeof OscillatorNode.prototype.start&&(OscillatorNode.prototype.start=OscillatorNode.prototype.noteOn),"function"!=typeof OscillatorNode.prototype.stop&&(OscillatorNode.prototype.stop=OscillatorNode.prototype.noteOff),"function"!=typeof OscillatorNode.prototype.setPeriodicWave&&(OscillatorNode.prototype.setPeriodicWave=OscillatorNode.prototype.setWaveTable),AudioNode.prototype._nativeConnect=AudioNode.prototype.connect,AudioNode.prototype.connect=function(e,i,n){if(e.input)Array.isArray(e.input)?(t(n)&&(n=0),this.connect(e.input[n])):this.connect(e.input);else try{e instanceof AudioNode?this._nativeConnect(e,i,n):this._nativeConnect(e,i)}catch(o){throw new Error("error connecting to node: "+e)}};var i=function(){this.input=this.context.createGain(),this.output=this.context.createGain()};i.context=e,i.prototype.context=i.context,i.prototype.bufferSize=2048,i.prototype.connect=function(t,e,i){Array.isArray(this.output)?(e=this.defaultArg(e,0),this.output[e].connect(t,0,i)):this.output.connect(t,e,i)},i.prototype.disconnect=function(){this.output.disconnect()},i.prototype.chain=function(){if(arguments.length>1)for(var t=arguments[0],e=1;e<arguments.length;e++){var i=arguments[e];t.connect(i),t=i}},i.prototype.fan=function(){var t=arguments[0];if(arguments.length>1)for(var e=1;e<arguments.length;e++){var i=arguments[e];t.connect(i)}},i.prototype.defaultArg=function(e,i){if("object"==typeof e&&"object"==typeof i){var n={};for(var o in e)n[o]=this.defaultArg(e[o],e[o]);for(var s in i)n[s]=this.defaultArg(e[s],i[s]);return n}return t(e)?i:e},i.prototype.optionsObject=function(t,e,i){var n={};if(1===t.length&&"object"==typeof t[0])n=t[0];else for(var o=0;o<e.length;o++)n[e[o]]=t[o];return this.isUndef(i)?n:this.defaultArg(n,i)},i.prototype.isUndef=t,i.prototype.equalPowerScale=function(t){var e=.5*Math.PI;return Math.sin(t*e)},i.prototype.logScale=function(t){return Math.max(this.normalize(this.gainToDb(t),-100,0),0)},i.prototype.expScale=function(t){return this.dbToGain(this.interpolate(t,-100,0))},i.prototype.dbToGain=function(t){return Math.pow(2,t/6)},i.prototype.gainToDb=function(t){return 20*(Math.log(t)/Math.LN10)},i.prototype.interpolate=function(t,e,i){return t*(i-e)+e},i.prototype.normalize=function(t,e,i){if(e>i){var n=i;i=e,e=n}else if(e==i)return 0;return(t-e)/(i-e)},i.prototype.dispose=function(){this.isUndef(this.input)||(this.input instanceof AudioNode&&this.input.disconnect(),this.input=null),this.isUndef(this.output)||(this.output instanceof AudioNode&&this.output.disconnect(),this.output=null)};var n=null;i.prototype.noGC=function(){this.output.connect(n)},AudioNode.prototype.noGC=function(){this.connect(n)},i.prototype.now=function(){return this.context.currentTime},i.prototype.samplesToSeconds=function(t){return t/this.context.sampleRate},i.prototype.toSamples=function(t){var e=this.
for(var i=0;i<SyncedSources.length;i++){var n=SyncedSources[i].source;n.stop(e)}}},Transport.prototype.pause=function(t){if(this.state===TransportState.STARTED){this.state=TransportState.PAUSED;var e=this.toSeconds(t);this._clock.stop(e);for(var i=0;i<SyncedSources.length;i++){var n=SyncedSources[i].source;n.pause(e)}}},Transport.prototype.setBpm=function(t,e){var i=this.notationToSeconds(tatum.toString()+"n",t,transportTimeSignature)/4;this._clock.setRate(i,e)},Transport.prototype.getBpm=function(){var t=this._clock.getRate();return 60*(t/tatum)},Transport.prototype.setTimeSignature=function(t,e){e=this.defaultArg(e,4),transportTimeSignature=t/(e/4)},Transport.prototype.getTimeSignature=function(){return transportTimeSignature},Transport.prototype.setLoopStart=function(t){loopStart=this.toTicks(t)},Transport.prototype.setLoopEnd=function(t){loopEnd=this.toTicks(t)},Transport.prototype.setLoopPoints=function(t,e){this.setLoopStart(t),this.setLoopEnd(e)},Transport.prototype.syncSource=function(t,e){SyncedSources.push({source:t,delay:this.toSeconds(this.defaultArg(e,0))})},Transport.prototype.unsyncSource=function(t){for(var e=0;e<SyncedSources.length;e++)SyncedSources[e].source===t&&SyncedSources.splice(e,1)},Transport.prototype.syncSignal=function(){};var TimelineEventIDCounter=0,TimelineEvent=function(t,e,i,n){this.startTicks=n,this.tickTime=i,this.callback=t,this.context=e,this.id=TimelineEventIDCounter++};return TimelineEvent.prototype.doCallback=function(t){this.callback.call(this.context,t)},TimelineEvent.prototype.callbackTick=function(){return this.startTicks+this.tickTime},TimelineEvent.prototype.testInterval=function(t){return(t-this.startTicks)%this.tickTime===0},Transport.prototype.dispose=function(){this._clock.dispose(),this._clock=null},Tone.prototype.isNotation=function(){var t=new RegExp(/[0-9]+[mnt]$/i);return function(e){return t.test(e)}}(),Tone.prototype.isTicks=function(){var t=new RegExp(/[0-9]+[i]$/i);return function(e){return t.test(e)}}(),Tone.prototype.isTransportTime=function(){var t=new RegExp(/^\d+(\.\d+)?:\d+(\.\d+)?(:\d+(\.\d+)?)?$/);return function(e){return t.test(e)}}(),Tone.prototype.isFrequency=function(){var t=new RegExp(/[0-9]+hz$/i);return function(e){return t.test(e)}}(),Tone.prototype.notationToSeconds=function(t,e,i){e=this.defaultArg(e,Tone.Transport.getBpm()),i=this.defaultArg(i,transportTimeSignature);var n=60/e,o=parseInt(t,10),s=0;0===o&&(s=0);var r=t.slice(-1);return s="t"===r?4/o*2/3:"n"===r?4/o:"m"===r?o*i:0,n*s},Tone.prototype.transportTimeToSeconds=function(t,e,i){e=this.defaultArg(e,Tone.Transport.getBpm()),i=this.defaultArg(i,transportTimeSignature);var n=0,o=0,s=0,r=t.split(":");2===r.length?(n=parseFloat(r[0]),o=parseFloat(r[1])):1===r.length?o=parseFloat(r[0]):3===r.length&&(n=parseFloat(r[0]),o=parseFloat(r[1]),s=parseFloat(r[2]));var a=n*i+o+s/4;return a*this.notationToSeconds("4n")},Tone.prototype.ticksToSeconds=function(t,e,i){t=parseInt(t);var n=this.notationToSeconds("4n",e,i);return 4*n/(tatum/t)},Tone.prototype.toTransportTime=function(t,e,i){var n=this.toSeconds(t,e,i);e=this.defaultArg(e,Tone.Transport.getBpm()),i=this.defaultArg(i,transportTimeSignature);var o=this.notationToSeconds("4n"),s=n/o,r=Math.floor(s/i),a=Math.floor(s%1*4);s=Math.floor(s)%i;var c=[r,s,a];return c.join(":")},Tone.prototype.toFrequency=function(t,e){return this.isFrequency(t)?parseFloat(t):this.isNotation(t)||this.isTransportTime(t)?this.secondsToFrequency(this.toSeconds(t,e)):t},Tone.prototype.toSeconds=function(time,now){if(now=this.defaultArg(now,this.now()),"number"==typeof time)return time;if("string"==typeof time){var plusTime=0;"+"===time.charAt(0)&&(plusTime=now,time=time.slice(1));var components=time.split(/[\(\)\-\+\/\*]/);if(components.length>1){for(var oringalTime=time,i=0;i<components.length;i++){var symb=components[i];if(""!==symb){var val=this.toSeconds(symb.trim());time=time.replace(symb,val)}}try{time=eval(time)}catch(e){console.log("problem evaluating Tone.Time: "+oringalTime),time=0}}else time=this.isNotation(time)?this.notationToSeconds(time):this.isTransportT
},t.extend(t.FeedbackDelay,t.FeedbackEffect),t.FeedbackDelay.defaults={delayTime:.25},t.FeedbackDelay.prototype.setDelayTime=function(t,e){e?this.delayTime.linearRampToValueNow(this.toSeconds(t),e):this.delayTime.setValue(this.toSeconds(t))},t.FeedbackDelay.prototype.set=function(e){this.isUndef(e.delayTime)||this.setDelayTime(e.delayTime),t.FeedbackEffect.prototype.set.call(this,e)},t.FeedbackDelay.prototype.dispose=function(){t.FeedbackEffect.prototype.dispose.call(this),this.delayTime.dispose(),this._delayNode.disconnect(),this._delayNode=null,this.delayTime=null},t.FeedbackDelay}),define("Tone/effect/StereoFeedbackEffect",["Tone/core/Tone","Tone/effect/StereoEffect","Tone/effect/FeedbackEffect"],function(t){return t.StereoFeedbackEffect=function(){var e=this.optionsObject(arguments,["feedback"],t.FeedbackEffect.defaults);t.StereoEffect.call(this,e),this.feedback=new t.Signal(e.feedback),this._half=new t.Multiply(.5),this._feedbackL=this.context.createGain(),this._feedbackR=this.context.createGain(),this.chain(this.effectReturnL,this._feedbackL,this.effectSendL),this.chain(this.effectReturnR,this._feedbackR,this.effectSendR),this.feedback.connect(this._half),this.fan(this._half,this._feedbackL.gain,this._feedbackR.gain)},t.extend(t.StereoFeedbackEffect,t.FeedbackEffect),t.StereoFeedbackEffect.prototype.dispose=function(){t.StereoEffect.prototype.dispose.call(this),this.feedback.dispose(),this._half.dispose(),this._feedbackL.disconnect(),this._feedbackR.disconnect(),this.feedback=null,this._feedbackL=null,this._feedbackR=null,this._half=null},t.StereoFeedbackEffect}),define("Tone/effect/Phaser",["Tone/core/Tone","Tone/component/LFO","Tone/effect/FeedbackEffect","Tone/component/Filter","Tone/effect/StereoFeedbackEffect"],function(t){return t.Phaser=function(){var e=this.optionsObject(arguments,["rate","depth","baseFrequency"],t.Phaser.defaults);t.StereoFeedbackEffect.call(this,e),this._lfoL=new t.LFO(e.rate,0,1),this._lfoR=new t.LFO(e.rate,0,1),this._lfoR.setPhase(180),this._baseFrequency=e.baseFrequency,this._depth=e.depth,this._filtersL=this._makeFilters(e.stages,this._lfoL,e.Q),this._filtersR=this._makeFilters(e.stages,this._lfoR,e.Q),this.effectSendL.connect(this._filtersL[0]),this.effectSendR.connect(this._filtersR[0]),this._filtersL[e.stages-1].connect(this.effectReturnL),this._filtersR[e.stages-1].connect(this.effectReturnR),this.effectSendL.connect(this.effectReturnL),this.effectSendR.connect(this.effectReturnR),this._lfoL.frequency.connect(this._lfoR.frequency),this.setBaseFrequency(e.baseFrequency),this.setDepth(e.depth),this.setRate(e.rate),this._lfoL.start(),this._lfoR.start()},t.extend(t.Phaser,t.StereoFeedbackEffect),t.Phaser.defaults={rate:.5,depth:1,stages:4,Q:6,baseFrequency:400,feedback:.6},t.Phaser.prototype._makeFilters=function(t,e,i){for(var n=new Array(t),o=0;t>o;o++){var s=this.context.createBiquadFilter();s.type="allpass",s.Q.value=i,e.connect(s.frequency),n[o]=s}return this.chain.apply(this,n),n},t.Phaser.prototype.setDepth=function(t){this._depth=t;var e=this._baseFrequency+this._baseFrequency*t;this._lfoL.setMax(e),this._lfoR.setMax(e)},t.Phaser.prototype.setBaseFrequency=function(t){this._baseFrequency=t,this._lfoL.setMin(t),this._lfoR.setMin(t),this.setDepth(this._depth)},t.Phaser.prototype.setRate=function(t){this._lfoL.setFrequency(t)},t.Phaser.prototype.set=function(e){this.isUndef(e.rate)||this.setRate(e.rate),this.isUndef(e.baseFrequency)||this.setBaseFrequency(e.baseFrequency),this.isUndef(e.depth)||this.setDepth(e.depth),t.StereoFeedbackEffect.prototype.set.call(this,e)},t.Phaser.prototype.dispose=function(){t.StereoFeedbackEffect.prototype.dispose.call(this),this._lfoL.dispose(),this._lfoR.dispose();for(var e=0;e<this._filtersL.length;e++)this._filtersL[e].disconnect(),this._filtersL[e]=null;for(var e=0;e<this._filtersR.length;e++)this._filtersR[e].disconnect(),this._filtersR[e]=null;this._filtersL=null,this._filtersR=null,this._lfoL=null,this._lfoR=null},t.Phaser}),define("Tone/effect/PingPongDelay",["Tone/core/Tone","Tone/effect/StereoXFeedbackEffect","Tone/signal/Signal","Tone/s