Tone.js/build/Tone.min.js

12 lines
105 KiB
JavaScript
Raw Normal View History

/**
* Tone.js
*
* @author Yotam Mann
*
* @license http://opensource.org/licenses/MIT MIT License 2014
*/
2014-09-24 01:57:32 +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.
},t.Clock.prototype.dispose=function(){this._jsNode.disconnect(),this._controlSignal.dispose(),this._oscillator&&(this._oscillator.onended(),this._oscillator.disconnect()),this._jsNode.onaudioprocess=function(){},this._jsNode=null,this._controlSignal=null,this._oscillator=null},t.Clock}),define("Tone/core/Transport",["Tone/core/Tone","Tone/core/Clock","Tone/signal/Signal"],function(Tone){Tone.Transport=function(){this._clock=new Tone.Clock(1,this._processTick.bind(this)),this.loop=!1,this.state=TransportState.STOPPED},Tone.extend(Tone.Transport);var timelineTicks=0,transportTicks=0,tatum=12,transportTimeSignature=4,loopStart=0,loopEnd=4*tatum,intervals=[],timeouts=[],transportTimeline=[],timelineProgress=0,SyncedSources=[],TransportState={STARTED:"started",PAUSED:"paused",STOPPED:"stopped"};Tone.Transport.prototype._processTick=function(t){this.state===TransportState.STARTED&&(processIntervals(t),processTimeouts(t),processTimeline(t),transportTicks+=1,timelineTicks+=1,this.loop&&timelineTicks===loopEnd&&this._setTicks(loopStart))},Tone.Transport.prototype._setTicks=function(t){timelineTicks=t;for(var e=0;e<transportTimeline.length;e++){var i=transportTimeline[e];if(i.callbackTick()>=t){timelineProgress=e;break}}};var processIntervals=function(t){for(var e=0,i=intervals.length;i>e;e++){var n=intervals[e];n.testInterval(transportTicks)&&n.doCallback(t)}},processTimeouts=function(t){for(var e=0,i=0,n=timeouts.length;n>i;i++){var o=timeouts[i],s=o.callbackTick();if(transportTicks>=s)o.doCallback(t),e++;else if(s>transportTicks)break}timeouts.splice(0,e)},processTimeline=function(t){for(var e=timelineProgress,i=transportTimeline.length;i>e;e++){var n=transportTimeline[e],o=n.callbackTick();if(o===timelineTicks)timelineProgress=e,n.doCallback(t);else if(o>timelineTicks)break}};Tone.Transport.prototype.setInterval=function(t,e,i){var n=this.toTicks(e),o=new TimelineEvent(t,i,n,transportTicks);return intervals.push(o),o.id},Tone.Transport.prototype.clearInterval=function(t){for(var e=0;e<intervals.length;e++){var i=intervals[e];if(i.id===t)return intervals.splice(e,1),!0}return!1},Tone.Transport.prototype.clearIntervals=function(){intervals=[]},Tone.Transport.prototype.setTimeout=function(t,e,i){for(var n=this.toTicks(e),o=new TimelineEvent(t,i,n+transportTicks,0),s=0,r=timeouts.length;r>s;s++){var a=timeouts[s];if(a.callbackTick()>o.callbackTick())return timeouts.splice(s,0,o),o.id}return timeouts.push(o),o.id},Tone.Transport.prototype.clearTimeout=function(t){for(var e=0;e<timeouts.length;e++){var i=timeouts[e];if(i.id===t)return timeouts.splice(e,1),!0}return!1},Tone.Transport.prototype.clearTimeouts=function(){timeouts=[]},Tone.Transport.prototype.setTimeline=function(t,e,i){for(var n=this.toTicks(e),o=new TimelineEvent(t,i,n,0),s=timelineProgress,r=transportTimeline.length;r>s;s++){var a=transportTimeline[s];if(a.callbackTick()>o.callbackTick())return transportTimeline.splice(s,0,o),o.id}return transportTimeline.push(o),o.id},Tone.Transport.prototype.clearTimeline=function(t){for(var e=0;e<transportTimeline.length;e++){var i=transportTimeline[e];if(i.id===t)return transportTimeline.splice(e,1),!0}return!1},Tone.Transport.prototype.clearTimelines=function(){timelineProgress=0,transportTimeline=[]},Tone.Transport.prototype.toTicks=function(t){var e=this.toSeconds(t),i=this.notationToSeconds("4n"),n=e/i,o=n*tatum;return Math.round(o)},Tone.Transport.prototype.getTransportTime=function(){var t=timelineTicks/tatum,e=Math.floor(t/transportTimeSignature),i=Math.floor(t%1*4);t=Math.floor(t)%transportTimeSignature;var n=[e,t,i];return n.join(":")},Tone.Transport.prototype.setTransportTime=function(t){var e=this.toTicks(t);this._setTicks(e)},Tone.Transport.prototype.start=function(t){if(this.state===TransportState.STOPPED||this.state===TransportState.PAUSED){this.state=TransportState.STARTED;var e=this.toSeconds(t);this._clock.start(e);for(var i=0;i<SyncedSources.length;i++){var n=SyncedSources[i].source,o=SyncedSources[i].delay;n.start(e+o)}}},Tone.Transport.prototype.stop=function(t){if(this.state===TransportState.STARTED||this.st
};return t.extend(e),e.prototype._makeCurve=function(t){for(var e=Math.pow(2,18),i=new Float32Array(e),n=0;n<i.length;n++)i[n]=n===e-1?-t:0===n?t:0;this._operator.curve=i},e.prototype.connect=function(t){this._operator.connect(t),this.input.connect(t)},e.prototype.dispose=function(){t.prototype.dispose.call(this),this._div.dispose(),this._operator.disconnect(),this._div=null,this._operator=null},t.Modulo}),define("Tone/effect/BitCrusher",["Tone/core/Tone","Tone/effect/Effect","Tone/signal/Modulo","Tone/signal/Negate","Tone/signal/Add"],function(t){return t.BitCrusher=function(){var e=this.optionsObject(arguments,["bits"],t.BitCrusher.defaults);t.Effect.call(this,e),this._modulo=new t.Modulo(1,e.bits),this._neg=new t.Negate,this._sub=this.context.createGain();var i=Math.pow(2,e.bits-1);this._scale=new t.Scale(-1,1,0,i),this._invScale=new t.Scale(0,i,-1,1),this.effectSend.connect(this._scale),this._scale.connect(this._invScale),this.chain(this._scale,this._modulo,this._neg,this._invScale,this.effectReturn)},t.extend(t.BitCrusher,t.Effect),t.BitCrusher.defaults={bits:4},t.BitCrusher.prototype.setBits=function(t){t=Math.min(t,8);var e=Math.pow(2,t-1);this._scale.setOutputMax(e),this._invScale.setInputMax(e)},t.BitCrusher.prototype.set=function(e){this.isUndef(e.bits)||this.setBits(e.bits),t.Effect.prototype.set.call(this,e)},t.BitCrusher.prototype.dispose=function(){t.Effect.prototype.dispose.call(this),this._modulo.dispose(),this._neg.dispose(),this._sub.disconnect(),this._scale.dispose(),this._invScale.dispose(),this._modulo=null,this._neg=null,this._sub=null,this._scale=null,this._invScale=null},t.BitCrusher}),define("Tone/effect/StereoEffect",["Tone/core/Tone","Tone/effect/Effect","Tone/component/Split","Tone/component/Merge","Tone/component/Mono"],function(t){return t.StereoEffect=function(){t.call(this);var e=this.optionsObject(arguments,["dry"],t.Effect.defaults);this.dryWet=new t.DryWet,this._mono=new t.Mono,this._split=new t.Split,this.effectSendL=this._split.left,this.effectSendR=this._split.right,this._merge=new t.Merge,this.effectReturnL=this._merge.left,this.effectReturnR=this._merge.right,this.input.connect(this._mono),this._mono.connect(this._split),this._mono.connect(this.dryWet.dry),this._merge.connect(this.dryWet.wet),this.dryWet.connect(this.output),this.setDry(e.dry)},t.extend(t.StereoEffect,t.Effect),t.StereoEffect.prototype.dispose=function(){t.prototype.dispose.call(this),this.dryWet.dispose(),this._mono.dispose(),this._split.dispose(),this._merge.dispose(),this._mono=null,this._split=null,this._merge=null,this.effectSendL=null,this.effectSendR=null,this.effectReturnL=null,this.effectReturnR=null,this.dryWet=null},t.StereoEffect}),define("Tone/effect/FeedbackEffect",["Tone/core/Tone","Tone/effect/Effect","Tone/signal/Signal","Tone/signal/Multiply"],function(t){return t.FeedbackEffect=function(){var e=this.optionsObject(arguments,["feedback"]);e=this.defaultArg(e,t.FeedbackEffect.defaults),t.Effect.call(this,e),this.feedback=new t.Signal(e.feedback),this._half=new t.Multiply(.5),this._feedbackGain=this.context.createGain(),this.chain(this.effectReturn,this._feedbackGain,this.effectSend),this.chain(this.feedback,this._half,this._feedbackGain.gain)},t.extend(t.FeedbackEffect,t.Effect),t.FeedbackEffect.defaults={feedback:.25},t.FeedbackEffect.prototype.setFeedback=function(t,e){e?this.feedback.linearRampToValueNow(t,e):this.feedback.setValue(t)},t.FeedbackEffect.prototype.set=function(e){this.isUndef(e.feedback)||this.setFeedback(e.feedback),t.Effect.prototype.set.call(this,e)},t.FeedbackEffect.prototype.dispose=function(){t.Effect.prototype.dispose.call(this),this.feedback.dispose(),this._half.dispose(),this._feedbackGain.disconnect(),this.feedback=null,this._feedbackGain=null,this._half=null},t.FeedbackEffect}),define("Tone/effect/StereoXFeedbackEffect",["Tone/core/Tone","Tone/effect/StereoEffect","Tone/effect/FeedbackEffect"],function(t){return t.StereoXFeedbackEffect=function(){var e=this.optionsObject(arguments,["feedback"],t.FeedbackEffect.defaults);t.StereoEffect.call(this,e),this.feedback=new t.Si
},t.MultiSampler}),define("Tone/source/Noise",["Tone/core/Tone","Tone/source/Source"],function(t){t.Noise=function(e){t.Source.call(this),this._source=null,this._buffer=null,this.onended=function(){},this.setType(this.defaultArg(e,"white"))},t.extend(t.Noise,t.Source),t.Noise.prototype.setType=function(o,s){switch(o){case"white":this._buffer=n;break;case"pink":this._buffer=e;break;case"brown":this._buffer=i;break;default:this._buffer=n}this.state===t.Source.State.STARTED&&(s=this.toSeconds(s),this._source.onended=void 0,this._stop(s),this._start(s))},t.Noise.prototype._start=function(t){this._source=this.context.createBufferSource(),this._source.buffer=this._buffer,this._source.loop=!0,this.chain(this._source,this.output),this._source.start(this.toSeconds(t)),this._source.onended=this._onended.bind(this)},t.Noise.prototype.start=function(e){this.state===t.Source.State.STOPPED&&(this.state=t.Source.State.STARTED,this._start(e))},t.Noise.prototype._stop=function(t){this._source.stop(this.toSeconds(t))},t.Noise.prototype.stop=function(e){this.state===t.Source.State.STARTED&&this._buffer&&this._source&&(e||(this.state=t.Source.State.STOPPED),this._stop(e))},t.Noise.prototype._onended=function(){this.state=t.Source.State.STOPPED,this.onended()},t.Noise.prototype.dispose=function(){t.Source.prototype.dispose.call(this),null!==this._source&&(this._source.disconnect(),this._source=null),this._buffer=null};var e=null,i=null,n=null;return t._initAudioContext(function(t){var o=t.sampleRate,s=4*o;e=function(){for(var e=t.createBuffer(2,s,o),i=0;i<e.numberOfChannels;i++){var n,r,a,c,l,h,u,p=e.getChannelData(i);n=r=a=c=l=h=u=0;for(var f=0;s>f;f++){var d=2*Math.random()-1;n=.99886*n+.0555179*d,r=.99332*r+.0750759*d,a=.969*a+.153852*d,c=.8665*c+.3104856*d,l=.55*l+.5329522*d,h=-.7616*h-.016898*d,p[f]=n+r+a+c+l+h+u+.5362*d,p[f]*=.11,u=.115926*d}}return e}(),i=function(){for(var e=t.createBuffer(2,s,o),i=0;i<e.numberOfChannels;i++)for(var n=e.getChannelData(i),r=0,a=0;s>a;a++){var c=2*Math.random()-1;n[a]=(r+.02*c)/1.02,r=n[a],n[a]*=3.5}return e}(),n=function(){for(var e=t.createBuffer(2,s,o),i=0;i<e.numberOfChannels;i++)for(var n=e.getChannelData(i),r=0;s>r;r++)n[r]=2*Math.random()-1;return e}()}),t.Noise}),define("Tone/instrument/PluckSynth",["Tone/core/Tone","Tone/instrument/Instrument","Tone/source/Noise","Tone/component/LowpassCombFilter"],function(t){return t.PluckSynth=function(){t.Instrument.call(this),this._noise=new t.Noise("pink"),this.attackNoise=1,this._lfcf=new t.LowpassCombFilter(1/440),this.resonance=this._lfcf.resonance,this.dampening=this._lfcf.dampening,this._noise.connect(this._lfcf),this._lfcf.connect(this.output)},t.extend(t.PluckSynth,t.Instrument),t.PluckSynth.prototype.triggerAttack=function(t,e){"string"==typeof t&&(t=this.noteToFrequency(t)),e=this.toSeconds(e);var i=1/t;this._lfcf.setDelayTime(i,e),this._noise.start(e),this._noise.stop(e+i*this.attackNoise)},t.PluckSynth.prototype.dispose=function(){t.Instrument.prototype.dispose.call(this),this._noise.dispose(),this._lfcf.dispose(),this._noise=null,this._lfcf=null,this.dampening=null,this.resonance=null},t.PluckSynth}),define("Tone/instrument/PolySynth",["Tone/core/Tone","Tone/instrument/MonoSynth","Tone/source/Source"],function(t){return t.PolySynth=function(){t.Instrument.call(this);var e=this.optionsObject(arguments,["polyphony","voice","voiceOptions"],t.PolySynth.defaults);this._voices=new Array(e.polyphony),this._freeVoices=[],this._activeVoices={};for(var i=0;i<e.polyphony;i++){var n=new e.voice(e.voiceOptions);this._voices[i]=n,n.connect(this.output)}this._freeVoices=this._voices.slice(0)},t.extend(t.PolySynth,t.Instrument),t.PolySynth.defaults={polyphony:4,voice:t.MonoSynth,voiceOptions:{portamento:0}},t.PolySynth.prototype.triggerAttack=function(t,e,i){var n=JSON.stringify(t);if(this._activeVoices[n])this._activeVoices[n].triggerAttack(t,e,i);else if(this._freeVoices.length>0){var o=this._freeVoices.shift();o.triggerAttack(t,e,i),this._activeVoices[n]=o}},t.PolySynth.prototype.triggerAttackRelease=function(t,e,i,n){i=this.toSeconds(i),this.triggerAtta