Tone.js/build/Tone.min.js

11 lines
143 KiB
JavaScript
Raw Normal View History

2015-05-13 04:28:11 +00:00
!function(root){"use strict";function ToneCore(a){Tone=a()}function ToneModule(a){a(Tone)}var Tone;/**
2014-12-04 03:43:45 +00:00
* Tone.js
* @author Yotam Mann
2015-03-03 17:15:37 +00:00
* @license http://opensource.org/licenses/MIT MIT License
* @copyright 2014-2015 Yotam Mann
2014-12-04 03:43:45 +00:00
*/
2015-05-13 04:28:11 +00:00
ToneCore(function(){function a(a){return void 0===a}function b(a){return"function"==typeof a}var c,d,e,f;if(a(window.AudioContext)&&(window.AudioContext=window.webkitAudioContext),a(window.OfflineAudioContext)&&(window.OfflineAudioContext=window.webkitOfflineAudioContext),a(AudioContext))throw new Error("Web Audio is not supported in this browser");return c=new AudioContext,b(AudioContext.prototype.createGain)||(AudioContext.prototype.createGain=AudioContext.prototype.createGainNode),b(AudioContext.prototype.createDelay)||(AudioContext.prototype.createDelay=AudioContext.prototype.createDelayNode),b(AudioContext.prototype.createPeriodicWave)||(AudioContext.prototype.createPeriodicWave=AudioContext.prototype.createWaveTable),b(AudioBufferSourceNode.prototype.start)||(AudioBufferSourceNode.prototype.start=AudioBufferSourceNode.prototype.noteGrainOn),b(AudioBufferSourceNode.prototype.stop)||(AudioBufferSourceNode.prototype.stop=AudioBufferSourceNode.prototype.noteOff),b(OscillatorNode.prototype.start)||(OscillatorNode.prototype.start=OscillatorNode.prototype.noteOn),b(OscillatorNode.prototype.stop)||(OscillatorNode.prototype.stop=OscillatorNode.prototype.noteOff),b(OscillatorNode.prototype.setPeriodicWave)||(OscillatorNode.prototype.setPeriodicWave=OscillatorNode.prototype.setWaveTable),AudioNode.prototype._nativeConnect=AudioNode.prototype.connect,AudioNode.prototype.connect=function(b,c,d){if(b.input)Array.isArray(b.input)?(a(d)&&(d=0),this.connect(b.input[d])):this.connect(b.input,c,d);else try{b instanceof AudioNode?this._nativeConnect(b,c,d):this._nativeConnect(b,c)}catch(e){throw new Error("error connecting to node: "+b)}},d=function(b,c){a(b)||1===b?this.input=this.context.createGain():b>1&&(this.input=new Array(b)),a(c)||1===c?this.output=this.context.createGain():c>1&&(this.output=new Array(b))},d.prototype.set=function(b,c,e){var f,g,h;"object"==typeof b?e=c:"string"==typeof b&&(f={},f[b]=c,b=f);for(g in b)h=this[g],a(h)||(c=b[g],h instanceof d.Signal?h.value!==c&&(a(e)?h.value=c:h.rampTo(c,e)):h instanceof AudioParam?h.value!==c&&(h.value=c):h instanceof d?h.set(c):h!==c&&(this[g]=c));return this},d.prototype.get=function(c){var e,f,g,h,i,j;if(a(c))c=this._collectDefaults(this.constructor);else if("string"==typeof c)e={},e[c]=0,c=e;else if(Array.isArray(c)){for(f={},g=0;g<c.length;g++)f[c[g]]=0;c=f}h={};for(i in c)j=this[i],"object"==typeof c[i]?h[i]=j.get(c[i]):j instanceof d.Signal?h[i]=j.value:j instanceof AudioParam?h[i]=j.value:j instanceof d?h[i]=j.get():b(j)||a(j)||(h[i]=j);return h},d.prototype._collectDefaults=function(b){var c,d,e={};if(a(b.defaults)||(e=b.defaults),!a(b._super)){c=this._collectDefaults(b._super);for(d in c)e[d]=c[d]}return e},d.prototype.setPreset=function(a){return!this.isUndef(this.preset)&&this.preset.hasOwnProperty(a)&&this.set(this.preset[a]),this},d.prototype.toString=function(){var a,c,e;for(a in d)if(c=a[0].match(/^[A-Z]$/),e=d[a]===this.constructor,b(d[a])&&c&&e)return a;return"Tone"},d.context=c,d.prototype.context=d.context,d.prototype.bufferSize=2048,d.prototype.bufferTime=d.prototype.bufferSize/d.context.sampleRate,d.prototype.dispose=function(){return 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),this},e=null,d.prototype.noGC=function(){return this.output.connect(e),this},AudioNode.prototype.noGC=function(){return this.connect(e),this},d.prototype.connect=function(a,b,c){return Array.isArray(this.output)?(b=this.defaultArg(b,0),this.output[b].connect(a,0,c)):this.output.connect(a,b,c),this},d.prototype.disconnect=function(a){return Array.isArray(this.output)?(a=this.defaultArg(a,0),this.output[a].disconnect()):this.output.disconnect(),this},d.prototype.connectSeries=function(){var a,b,c;if(arguments.length>1)for(a=arguments[0],b=1;b<arguments.length;b++)c=arguments[b],a.connect(c),a=c;return this},d.prototype.connectParallel=function(){var a,b,c=arguments[0];if(arguments.length>1)for(a=1;a<arguments.length;a++)b=ar
},a.EqualPowerGain}),ToneModule(function(a){return a.CrossFade=function(b){a.call(this,2,1),this.a=this.input[0]=this.context.createGain(),this.b=this.input[1]=this.context.createGain(),this.fade=new a.Signal(this.defaultArg(b,.5),a.Signal.Units.Normal),this._equalPowerA=new a.EqualPowerGain,this._equalPowerB=new a.EqualPowerGain,this._invert=new a.Expr("1 - $0"),this.a.connect(this.output),this.b.connect(this.output),this.fade.chain(this._equalPowerB,this.b.gain),this.fade.chain(this._invert,this._equalPowerA,this.a.gain),this._readOnly("fade")},a.extend(a.CrossFade),a.CrossFade.prototype.dispose=function(){return a.prototype.dispose.call(this),this._writable("fade"),this._equalPowerA.dispose(),this._equalPowerA=null,this._equalPowerB.dispose(),this._equalPowerB=null,this.fade.dispose(),this.fade=null,this._invert.dispose(),this._invert=null,this.a.disconnect(),this.a=null,this.b.disconnect(),this.b=null,this},a.CrossFade}),ToneModule(function(a){return a.Filter=function(){a.call(this);var b=this.optionsObject(arguments,["frequency","type","rolloff"],a.Filter.defaults);this._filters=[],this.frequency=new a.Signal(b.frequency,a.Signal.Units.Frequency),this.detune=new a.Signal(0),this.gain=new a.Signal(b.gain,a.Signal.Units.Decibels),this.Q=new a.Signal(b.Q),this._type=b.type,this._rolloff=b.rolloff,this.rolloff=b.rolloff,this._readOnly(["detune","frequency","gain","Q"])},a.extend(a.Filter),a.Filter.defaults={type:"lowpass",frequency:350,rolloff:-12,Q:1,gain:0},Object.defineProperty(a.Filter.prototype,"type",{get:function(){return this._type},set:function(a){var b,c=["lowpass","highpass","bandpass","lowshelf","highshelf","notch","allpass","peaking"];if(-1===c.indexOf(a))throw new TypeError("Tone.Filter does not have filter type "+a);for(this._type=a,b=0;b<this._filters.length;b++)this._filters[b].type=a}}),Object.defineProperty(a.Filter.prototype,"rolloff",{get:function(){return this._rolloff},set:function(a){var b,c,d,e,f=Math.log(a/-12)/Math.LN2+1;if(f%1!==0)throw new RangeError("Filter rolloff can only be -12, -24, or -48");for(this._rolloff=a,this.input.disconnect(),b=0;b<this._filters.length;b++)this._filters[b].disconnect(),this._filters[b]=null;for(this._filters=new Array(f),c=0;f>c;c++)d=this.context.createBiquadFilter(),d.type=this._type,this.frequency.connect(d.frequency),this.detune.connect(d.detune),this.Q.connect(d.Q),this.gain.connect(d.gain),this._filters[c]=d;e=[this.input].concat(this._filters).concat([this.output]),this.connectSeries.apply(this,e)}}),a.Filter.prototype.dispose=function(){a.prototype.dispose.call(this);for(var b=0;b<this._filters.length;b++)this._filters[b].disconnect(),this._filters[b]=null;return this._filters=null,this._writable(["detune","frequency","gain","Q"]),this.frequency.dispose(),this.Q.dispose(),this.frequency=null,this.Q=null,this.detune.dispose(),this.detune=null,this.gain.dispose(),this.gain=null,this},a.Filter}),ToneModule(function(a){return a.MultibandSplit=function(){var b=this.optionsObject(arguments,["lowFrequency","highFrequency"],a.MultibandSplit.defaults);this.input=this.context.createGain(),this.output=new Array(3),this.low=this.output[0]=new a.Filter(0,"lowpass"),this._lowMidFilter=new a.Filter(0,"highpass"),this.mid=this.output[1]=new a.Filter(0,"lowpass"),this.high=this.output[2]=new a.Filter(0,"highpass"),this.lowFrequency=new a.Signal(b.lowFrequency),this.highFrequency=new a.Signal(b.highFrequency),this.input.fan(this.low,this.high),this.input.chain(this._lowMidFilter,this.mid),this.lowFrequency.connect(this.low.frequency),this.lowFrequency.connect(this._lowMidFilter.frequency),this.highFrequency.connect(this.mid.frequency),this.highFrequency.connect(this.high.frequency),this._readOnly(["high","mid","low","highFrequency","lowFrequency"])},a.extend(a.MultibandSplit),a.MultibandSplit.defaults={lowFrequency:400,highFrequency:2500},a.MultibandSplit.prototype.dispose=function(){return a.prototype.dispose.call(this),this._writable(["high","mid","low","highFrequency","lowFrequency"]),this.low.dispose(),this._lowMidFilter.dispose(),this.mid.dispose(),this.high.dispos
},a.LowpassCombFilter}),ToneModule(function(a){return a.Merge=function(){a.call(this,2,0),this.left=this.input[0]=this.context.createGain(),this.right=this.input[1]=this.context.createGain(),this._merger=this.output=this.context.createChannelMerger(2),this.left.connect(this._merger,0,0),this.right.connect(this._merger,0,1)},a.extend(a.Merge),a.Merge.prototype.dispose=function(){return a.prototype.dispose.call(this),this.left.disconnect(),this.left=null,this.right.disconnect(),this.right=null,this._merger.disconnect(),this._merger=null,this},a.Merge}),ToneModule(function(a){return a.Meter=function(b,c,d){a.call(this),this._channels=this.defaultArg(b,1),this._smoothing=this.defaultArg(c,.8),this._clipMemory=1e3*this.defaultArg(d,.5),this._volume=new Array(this._channels),this._values=new Array(this._channels);for(var e=0;e<this._channels;e++)this._volume[e]=0,this._values[e]=0;this._lastClip=0,this._jsNode=this.context.createScriptProcessor(this.bufferSize,this._channels,1),this._jsNode.onaudioprocess=this._onprocess.bind(this),this._jsNode.noGC(),this.input.connect(this.output),this.input.connect(this._jsNode)},a.extend(a.Meter),a.Meter.prototype._onprocess=function(a){var b,c,d,e,f,g,h,i,j,k=this._jsNode.bufferSize,l=this._smoothing;for(b=0;b<this._channels;b++){for(c=a.inputBuffer.getChannelData(b),d=0,e=0,g=!1,h=0;k>h;h++)f=c[h],!g&&f>.95&&(g=!0,this._lastClip=Date.now()),e+=f,d+=f*f;i=e/k,j=Math.sqrt(d/k),this._volume[b]=Math.max(j,this._volume[b]*l),this._values[b]=i}},a.Meter.prototype.getLevel=function(a){a=this.defaultArg(a,0);var b=this._volume[a];return 1e-5>b?0:b},a.Meter.prototype.getValue=function(a){return a=this.defaultArg(a,0),this._values[a]},a.Meter.prototype.getDb=function(a){return this.gainToDb(this.getLevel(a))},a.Meter.prototype.isClipped=function(){return Date.now()-this._lastClip<this._clipMemory},a.Meter.prototype.dispose=function(){return a.prototype.dispose.call(this),this._jsNode.disconnect(),this._jsNode.onaudioprocess=null,this._volume=null,this._values=null,this},a.Meter}),ToneModule(function(a){return a.Split=function(){a.call(this,0,2),this._splitter=this.input=this.context.createChannelSplitter(2),this.left=this.output[0]=this.context.createGain(),this.right=this.output[1]=this.context.createGain(),this._splitter.connect(this.left,0,0),this._splitter.connect(this.right,1,0)},a.extend(a.Split),a.Split.prototype.dispose=function(){return a.prototype.dispose.call(this),this._splitter.disconnect(),this.left.disconnect(),this.right.disconnect(),this.left=null,this.right=null,this._splitter=null,this},a.Split}),ToneModule(function(a){a.MidSideSplit=function(){a.call(this,0,2),this._split=this.input=new a.Split,this.mid=this.output[0]=new a.Expr("($0 + $1) * $2"),this.side=this.output[1]=new a.Expr("($0 - $1) * $2"),this._split.connect(this.mid,0,0),this._split.connect(this.mid,1,1),this._split.connect(this.side,0,0),this._split.connect(this.side,1,1),b.connect(this.mid,0,2),b.connect(this.side,0,2)},a.extend(a.MidSideSplit);var b=null;return a._initAudioContext(function(){b=new a.Signal(1/Math.sqrt(2))}),a.MidSideSplit.prototype.dispose=function(){return a.prototype.dispose.call(this),this.mid.dispose(),this.mid=null,this.side.dispose(),this.side=null,this._split.dispose(),this._split=null,this},a.MidSideSplit}),ToneModule(function(a){a.MidSideMerge=function(){a.call(this,2,0),this.mid=this.input[0]=this.context.createGain(),this._left=new a.Expr("($0 + $1) * $2"),this.side=this.input[1]=this.context.createGain(),this._right=new a.Expr("($0 - $1) * $2"),this._merge=this.output=new a.Merge,this.mid.connect(this._left,0,0),this.side.connect(this._left,0,1),this.mid.connect(this._right,0,0),this.side.connect(this._right,0,1),this._left.connect(this._merge,0,0),this._right.connect(this._merge,0,1),b.connect(this._left,0,2),b.connect(this._right,0,2)},a.extend(a.MidSideMerge);var b=null;return a._initAudioContext(function(){b=new a.Signal(1/Math.sqrt(2))}),a.MidSideMerge.prototype.dispose=function(){return a.prototype.dispose.call(this),this.mid.disconnect(),this.mid=null,this.side.disconnect(),th
},a.extend(a.Freeverb,a.StereoEffect),a.Freeverb.defaults={roomSize:.7,dampening:3e3},a.Freeverb.prototype.dispose=function(){var b,c,d;for(a.StereoEffect.prototype.dispose.call(this),b=0;b<this._allpassFiltersL.length;b++)this._allpassFiltersL[b].disconnect(),this._allpassFiltersL[b]=null;for(this._allpassFiltersL=null,c=0;c<this._allpassFiltersR.length;c++)this._allpassFiltersR[c].disconnect(),this._allpassFiltersR[c]=null;for(this._allpassFiltersR=null,d=0;d<this._combFilters.length;d++)this._combFilters[d].dispose(),this._combFilters[d]=null;return this._combFilters=null,this._writable(["roomSize","dampening"]),this.roomSize.dispose(),this.roomSize=null,this.dampening.dispose(),this.dampening=null,this},a.Freeverb}),ToneModule(function(a){var b=[.06748,.06404,.08212,.09004],c=[.773,.802,.753,.733],d=[347,113,37];return a.JCReverb=function(){var e,f,g,h,i=this.optionsObject(arguments,["roomSize"],a.JCReverb.defaults);for(a.StereoEffect.call(this,i),this.roomSize=new a.Signal(i.roomSize,a.Signal.Units.Normal),this._scaleRoomSize=new a.Scale(-.733,.197),this._allpassFilters=[],this._feedbackCombFilters=[],e=0;e<d.length;e++)f=this.context.createBiquadFilter(),f.type="allpass",f.frequency.value=d[e],this._allpassFilters.push(f);for(g=0;g<b.length;g++)h=new a.FeedbackCombFilter(b[g],.1),this._scaleRoomSize.connect(h.resonance),h.resonance.value=c[g],this._allpassFilters[this._allpassFilters.length-1].connect(h),h.connect(g<b.length/2?this.effectReturnL:this.effectReturnR),this._feedbackCombFilters.push(h);this.roomSize.connect(this._scaleRoomSize),this.connectSeries.apply(this,this._allpassFilters),this.effectSendL.connect(this._allpassFilters[0]),this.effectSendR.connect(this._allpassFilters[0]),this._readOnly(["roomSize"])},a.extend(a.JCReverb,a.StereoEffect),a.JCReverb.defaults={roomSize:.5},a.JCReverb.prototype.dispose=function(){var b,c;for(a.StereoEffect.prototype.dispose.call(this),b=0;b<this._allpassFilters.length;b++)this._allpassFilters[b].disconnect(),this._allpassFilters[b]=null;for(this._allpassFilters=null,c=0;c<this._feedbackCombFilters.length;c++)this._feedbackCombFilters[c].dispose(),this._feedbackCombFilters[c]=null;return this._feedbackCombFilters=null,this._writable(["roomSize"]),this.roomSize.dispose(),this.roomSize=null,this._scaleRoomSize.dispose(),this._scaleRoomSize=null,this},a.JCReverb}),ToneModule(function(a){return a.MidSideEffect=function(){a.Effect.call(this),this._midSideSplit=new a.MidSideSplit,this._midSideMerge=new a.MidSideMerge,this.midSend=this._midSideSplit.mid,this.sideSend=this._midSideSplit.side,this.midReturn=this._midSideMerge.mid,this.sideReturn=this._midSideMerge.side,this.effectSend.connect(this._midSideSplit),this._midSideMerge.connect(this.effectReturn)},a.extend(a.MidSideEffect,a.Effect),a.MidSideEffect.prototype.dispose=function(){return a.Effect.prototype.dispose.call(this),this._midSideSplit.dispose(),this._midSideSplit=null,this._midSideMerge.dispose(),this._midSideMerge=null,this.midSend=null,this.sideSend=null,this.midReturn=null,this.sideReturn=null,this},a.MidSideEffect}),ToneModule(function(a){return a.Phaser=function(){var b=this.optionsObject(arguments,["frequency","depth","baseFrequency"],a.Phaser.defaults);a.StereoEffect.call(this,b),this._lfoL=new a.LFO(b.frequency,0,1),this._lfoR=new a.LFO(b.frequency,0,1),this._lfoR.phase=180,this._baseFrequency=b.baseFrequency,this._depth=b.depth,this._filtersL=this._makeFilters(b.stages,this._lfoL,b.Q),this._filtersR=this._makeFilters(b.stages,this._lfoR,b.Q),this.frequency=this._lfoL.frequency,this.frequency.value=b.frequency,this.effectSendL.connect(this._filtersL[0]),this.effectSendR.connect(this._filtersR[0]),this._filtersL[b.stages-1].connect(this.effectReturnL),this._filtersR[b.stages-1].connect(this.effectReturnR),this.effectSendL.connect(this.effectReturnL),this.effectSendR.connect(this.effectReturnR),this._lfoL.frequency.connect(this._lfoR.frequency),this.baseFrequency=b.baseFrequency,this.depth=b.depth,this._lfoL.start(),this._lfoR.start(),this._readOnly(["frequency"])},a.extend(a.Phaser,a.StereoEffect),a.Phase
var c=1/a;return this._lfcf.delayTime.setValueAtTime(c,b),this._noise.start(b),this._noise.stop(b+c*this.attackNoise),this},a.PluckSynth.prototype.dispose=function(){return a.Instrument.prototype.dispose.call(this),this._noise.dispose(),this._lfcf.dispose(),this._noise=null,this._lfcf=null,this._writable(["resonance","dampening"]),this.dampening=null,this.resonance=null,this},a.PluckSynth}),ToneModule(function(a){return a.PolySynth=function(){var b,c,d;for(a.Instrument.call(this),b=this.optionsObject(arguments,["polyphony","voice"],a.PolySynth.defaults),this.voices=new Array(b.polyphony),this._freeVoices=[],this._activeVoices={},c=0;c<b.polyphony;c++)d=new b.voice(arguments[2],arguments[3]),this.voices[c]=d,d.connect(this.output);this._freeVoices=this.voices.slice(0)},a.extend(a.PolySynth,a.Instrument),a.PolySynth.defaults={polyphony:4,voice:a.MonoSynth},a.PolySynth.prototype.triggerAttack=function(a,b,c){var d,e,f,g;for(Array.isArray(a)||(a=[a]),d=0;d<a.length;d++)e=a[d],f=JSON.stringify(e),this._activeVoices[f]?this._activeVoices[f].triggerAttack(e,b,c):this._freeVoices.length>0&&(g=this._freeVoices.shift(),g.triggerAttack(e,b,c),this._activeVoices[f]=g);return this},a.PolySynth.prototype.triggerAttackRelease=function(a,b,c,d){return c=this.toSeconds(c),this.triggerAttack(a,c,d),this.triggerRelease(a,c+this.toSeconds(b)),this},a.PolySynth.prototype.triggerRelease=function(a,b){var c,d,e;for(Array.isArray(a)||(a=[a]),c=0;c<a.length;c++)d=JSON.stringify(a[c]),e=this._activeVoices[d],e&&(e.triggerRelease(b),this._freeVoices.push(e),delete this._activeVoices[d],e=null);return this},a.PolySynth.prototype.set=function(a,b,c){for(var d=0;d<this.voices.length;d++)this.voices[d].set(a,b,c);return this},a.PolySynth.prototype.get=function(a){return this.voices[0].get(a)},a.PolySynth.prototype.setPreset=function(a){for(var b=0;b<this.voices.length;b++)this.voices[b].setPreset(a);return this},a.PolySynth.prototype.dispose=function(){a.Instrument.prototype.dispose.call(this);for(var b=0;b<this.voices.length;b++)this.voices[b].dispose(),this.voices[b]=null;return this.voices=null,this._activeVoices=null,this._freeVoices=null,this},a.PolySynth}),ToneModule(function(a){return a.Player=function(){var b=this.optionsObject(arguments,["url","onload"],a.Player.defaults);a.Source.call(this,b),this._source=null,this.autostart=b.autostart,this._buffer=new a.Buffer({url:b.url,onload:this._onload.bind(this,b.onload),reverse:b.reverse}),this._loop=b.loop,this._loopStart=b.loopStart,this._loopEnd=b.loopEnd,this._playbackRate=b.playbackRate,this.retrigger=b.retrigger},a.extend(a.Player,a.Source),a.Player.defaults={onload:function(){},playbackRate:1,loop:!1,autostart:!1,loopStart:0,loopEnd:0,retrigger:!1,reverse:!1},a.Player.prototype.load=function(a,b){return this._buffer.load(a,this._onload.bind(this,b)),this},a.Player.prototype._onload=function(a){a(this),this.autostart&&this.start()},a.Player.prototype._start=function(a,b,c){if(!this._buffer.loaded)throw Error("tried to start Player before the buffer was loaded");return b=this._loop?this.defaultArg(b,this._loopStart):this.defaultArg(b,0),b=this.toSeconds(b),c=this.defaultArg(c,this._buffer.duration-b),a=this.toSeconds(a),c=this.toSeconds(c),this._source=this.context.createBufferSource(),this._source.buffer=this._buffer.get(),this._loop?(this._source.loop=this._loop,this._source.loopStart=this.toSeconds(this._loopStart),this._source.loopEnd=this.toSeconds(this._loopEnd),c=65536):this._nextStop=a+c,this._source.playbackRate.value=this._playbackRate,this._source.onended=this.onended,this._source.connect(this.output),this._source.start(a,b,c),this},a.Player.prototype._stop=function(a){return this._source&&(this._source.stop(this.toSeconds(a)),this._source=null),this},a.Player.prototype.setLoopPoints=function(a,b){return this.loopStart=a,this.loopEnd=b,this},Object.defineProperty(a.Player.prototype,"loopStart",{get:function(){return this._loopStart},set:function(a){this._loopStart=a,this._source&&(this._source.loopStart=this.toSeconds(a))}}),Object.defineProperty(a.Player.prototype,"loopEnd",{get:fu