Tone.js/build/Tone.min.js

11 lines
94 KiB
JavaScript
Raw Normal View History

/**
* Tone.js
*
* @author Yotam Mann
*
* @license http://opensource.org/licenses/MIT MIT License 2014
*/
2014-09-12 00:37:40 +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,o){if(e.input)Array.isArray(e.input)?(t(o)&&(o=0),this.connect(e.input[o])):this.connect(e.input);else try{e instanceof AudioNode?this._nativeConnect(e,i,o):this._nativeConnect(e,i)}catch(n){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 o={};for(var n in e)o[n]=this.defaultArg(e[n],e[n]);for(var s in i)o[s]=this.defaultArg(e[s],i[s]);return o}return t(e)?i:e},i.prototype.optionsObject=function(t,e,i){var o={};if(1===t.length&&"object"==typeof t[0])o=t[0];else for(var n=0;n<e.length;n++)o[e[n]]=t[n];return this.isUndef(i)?o:this.defaultArg(o,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 o=i;i=e,e=o}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 o=null;i.prototype.noGC=function(){this.output.connect(o)},AudioNode.prototype.noGC=function(){this.connect(o)},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.
o.pause(e)}}},Tone.Transport.prototype.setBpm=function(t,e){var i=this.notationToSeconds(tatum.toString()+"n",t,transportTimeSignature)/4;this._clock.setRate(i,e)},Tone.Transport.prototype.getBpm=function(){var t=this._clock.getRate();return 60*(t/tatum)},Tone.Transport.prototype.setTimeSignature=function(t,e){e=this.defaultArg(e,4),transportTimeSignature=t/(e/4)},Tone.Transport.prototype.getTimeSignature=function(){return transportTimeSignature},Tone.Transport.prototype.setLoopStart=function(t){loopStart=this.toTicks(t)},Tone.Transport.prototype.setLoopEnd=function(t){loopEnd=this.toTicks(t)},Tone.Transport.prototype.setLoopPoints=function(t,e){this.setLoopStart(t),this.setLoopEnd(e)},Tone.Transport.prototype.syncSource=function(t,e){SyncedSources.push({source:t,delay:this.toSeconds(this.defaultArg(e,0))})},Tone.Transport.prototype.unsyncSource=function(t){for(var e=0;e<SyncedSources.length;e++)SyncedSources[e].source===t&&SyncedSources.splice(e,1)},Tone.Transport.prototype.syncSignal=function(t){t.sync(this._clock._controlSignal)},Tone.Transport.prototype.dispose=function(){this._clock.dispose(),this._clock=null};var TimelineEventIDCounter=0,TimelineEvent=function(t,e,i,o){this.startTicks=o,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},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 o=60/e,n=parseInt(t,10),s=0;0===n&&(s=0);var r=t.slice(-1);return s="t"===r?4/n*2/3:"n"===r?4/n:"m"===r?n*i:0,o*s},Tone.prototype.transportTimeToSeconds=function(t,e,i){e=this.defaultArg(e,Tone.Transport.getBpm()),i=this.defaultArg(i,transportTimeSignature);var o=0,n=0,s=0,r=t.split(":");2===r.length?(o=parseFloat(r[0]),n=parseFloat(r[1])):1===r.length?n=parseFloat(r[0]):3===r.length&&(o=parseFloat(r[0]),n=parseFloat(r[1]),s=parseFloat(r[2]));var a=o*i+n+s/4;return a*this.notationToSeconds("4n")},Tone.prototype.ticksToSeconds=function(t,e,i){t=parseInt(t);var o=this.notationToSeconds("4n",e,i);return 4*o/(tatum/t)},Tone.prototype.toTransportTime=function(t,e,i){var o=this.toSeconds(t,e,i);e=this.defaultArg(e,Tone.Transport.getBpm()),i=this.defaultArg(i,transportTimeSignature);var n=this.notationToSeconds("4n"),s=o/n,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.isTransportTime(time)?this.transportTimeToSeconds(time):this.isFrequency(time)?this.frequencyToSeconds(time):parseFloat(time);return time+plusTime}return now},Tone.Transport=new Tone.Transport,Tone.Transport.setBpm(120),Tone._init
var e=this._delayTime*t;this._lfoL.setMin(this._delayTime-e),this._lfoL.setMax(this._delayTime+e),this._lfoR.setMin(this._delayTime-e),this._lfoR.setMax(this._delayTime+e)},t.Chorus.prototype.setDelayTime=function(t){this._delayTime=t/1e3,this.setDepth(this._depth)},t.Chorus.prototype.setRate=function(t){this._lfoL.setFrequency(t)},t.Chorus.prototype.setType=function(t){this._lfoL.setType(t),this._lfoR.setType(t)},t.Chorus.prototype.set=function(e){this.isUndef(e.rate)||this.setRate(e.rate),this.isUndef(e.delayTime)||this.setDelayTime(e.delayTime),this.isUndef(e.depth)||this.setDepth(e.depth),this.isUndef(e.type)||this.setType(e.type),t.FeedbackEffect.prototype.set.call(this,e)},t.Chorus.prototype.dispose=function(){t.StereoXFeedbackEffect.prototype.dispose.call(this),this._lfoL.dispose(),this._lfoR.dispose(),this._delayNodeL.disconnect(),this._delayNodeR.disconnect(),this._lfoL=null,this._lfoR=null,this._delayNodeL=null,this._delayNodeR=null},t.Chorus}),define("Tone/effect/FeedbackDelay",["Tone/core/Tone","Tone/effect/FeedbackEffect","Tone/signal/Signal"],function(t){return t.FeedbackDelay=function(){var e=this.optionsObject(arguments,["delayTime"],t.FeedbackDelay.defaults);t.FeedbackEffect.call(this,e),this.delayTime=new t.Signal,this._delayNode=this.context.createDelay(4),this.connectEffect(this._delayNode),this.delayTime.connect(this._delayNode.delayTime),this.setDelayTime(e.delayTime)},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.P