Tone.js/build/Tone.min.js

11 lines
142 KiB
JavaScript
Raw Normal View History

2015-07-04 20:36:45 +00:00
!function(root){"use strict";function Main(t){Tone=t()}function Module(t){t(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-07-04 20:36:45 +00:00
Main(function(){function t(t){return void 0===t}function e(t){return"function"==typeof t}var i,s,n,o;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");return i=new AudioContext,e(AudioContext.prototype.createGain)||(AudioContext.prototype.createGain=AudioContext.prototype.createGainNode),e(AudioContext.prototype.createDelay)||(AudioContext.prototype.createDelay=AudioContext.prototype.createDelayNode),e(AudioContext.prototype.createPeriodicWave)||(AudioContext.prototype.createPeriodicWave=AudioContext.prototype.createWaveTable),e(AudioBufferSourceNode.prototype.start)||(AudioBufferSourceNode.prototype.start=AudioBufferSourceNode.prototype.noteGrainOn),e(AudioBufferSourceNode.prototype.stop)||(AudioBufferSourceNode.prototype.stop=AudioBufferSourceNode.prototype.noteOff),e(OscillatorNode.prototype.start)||(OscillatorNode.prototype.start=OscillatorNode.prototype.noteOn),e(OscillatorNode.prototype.stop)||(OscillatorNode.prototype.stop=OscillatorNode.prototype.noteOff),e(OscillatorNode.prototype.setPeriodicWave)||(OscillatorNode.prototype.setPeriodicWave=OscillatorNode.prototype.setWaveTable),AudioNode.prototype._nativeConnect=AudioNode.prototype.connect,AudioNode.prototype.connect=function(e,i,s){if(e.input)Array.isArray(e.input)?(t(s)&&(s=0),this.connect(e.input[s])):this.connect(e.input,i,s);else try{e instanceof AudioNode?this._nativeConnect(e,i,s):this._nativeConnect(e,i)}catch(n){throw new Error("error connecting to node: "+e)}},s=function(e,i){t(e)||1===e?this.input=this.context.createGain():e>1&&(this.input=new Array(e)),t(i)||1===i?this.output=this.context.createGain():i>1&&(this.output=new Array(e))},s.prototype.set=function(e,i,n){var o,r,l,a,h,u;"object"==typeof e?n=i:"string"==typeof e&&(o={},o[e]=i,e=o);for(r in e){if(i=e[r],l=this,-1!==r.indexOf(".")){for(a=r.split("."),h=0;h<a.length-1;h++)l=l[a[h]];r=a[a.length-1]}u=l[r],t(u)||(u instanceof s.Signal?u.value!==i&&(t(n)?u.value=i:u.rampTo(i,n)):u instanceof AudioParam?u.value!==i&&(u.value=i):u instanceof s?u.set(i):u!==i&&(l[r]=i))}return this},s.prototype.get=function(i){var n,o,r,l,a,h,u,c,p;for(t(i)?i=this._collectDefaults(this.constructor):"string"==typeof i&&(i=[i]),n={},o=0;o<i.length;o++){if(r=i[o],l=this,a=n,-1!==r.indexOf(".")){for(h=r.split("."),u=0;u<h.length-1;u++)c=h[u],a[c]=a[c]||{},a=a[c],l=l[c];r=h[h.length-1]}p=l[r],"object"==typeof i[r]?a[r]=p.get():p instanceof s.Signal?a[r]=p.value:p instanceof AudioParam?a[r]=p.value:p instanceof s?a[r]=p.get():e(p)||t(p)||(a[r]=p)}return n},s.prototype._collectDefaults=function(e){var i,s,n=[];if(t(e.defaults)||(n=Object.keys(e.defaults)),!t(e._super))for(i=this._collectDefaults(e._super),s=0;s<i.length;s++)-1===n.indexOf(i[s])&&n.push(i[s]);return n},s.prototype.setPreset=function(t){return!this.isUndef(this.preset)&&this.preset.hasOwnProperty(t)&&this.set(this.preset[t]),this},s.prototype.toString=function(){var t,i,n;for(t in s)if(i=t[0].match(/^[A-Z]$/),n=s[t]===this.constructor,e(s[t])&&i&&n)return t;return"Tone"},s.context=i,s.prototype.context=s.context,s.prototype.bufferSize=2048,s.prototype.bufferTime=s.prototype.bufferSize/s.context.sampleRate,s.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},n=null,s.prototype.noGC=function(){return this.output.connect(n),this},AudioNode.prototype.noGC=function(){return this.connect(n),this},s.prototype.connect=function(t,e,i){return Array.isArray(this.output)?(e=this.defaultArg(e,0),this.output[e].connect(t,0,i)):this.output.connect(t,e,i),this},s.prototype.disconnect=function(t){return Array.isArray(this.output)?(t=this.defaultArg(t,0),this.output[t].disconnect()):this.output.disconnect(),this},s.prototype.connectSeries=function(){var t,e,i;if(arguments.length>1)for(t=ar
var t,e;for(t=0;t<this._nodes.length;t++)e=this._nodes[t],this.isFunction(e.dispose)?e.dispose():this.isFunction(e.disconnect)&&e.disconnect(),e=null,this._nodes[t]=null;this._nodes=null},t.Expr.prototype.dispose=function(){t.prototype.dispose.call(this),this._disposeNodes()},t.Expr}),Module(function(t){return t.EqualPowerGain=function(){this._eqPower=this.input=this.output=new t.WaveShaper(function(t){return Math.abs(t)<.001?0:this.equalPowerScale(t)}.bind(this),4096)},t.extend(t.EqualPowerGain,t.SignalBase),t.EqualPowerGain.prototype.dispose=function(){return t.prototype.dispose.call(this),this._eqPower.dispose(),this._eqPower=null,this},t.EqualPowerGain}),Module(function(t){return t.CrossFade=function(e){t.call(this,2,1),this.a=this.input[0]=this.context.createGain(),this.b=this.input[1]=this.context.createGain(),this.fade=new t.Signal(this.defaultArg(e,.5),t.Type.NormalRange),this._equalPowerA=new t.EqualPowerGain,this._equalPowerB=new t.EqualPowerGain,this._invert=new t.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")},t.extend(t.CrossFade),t.CrossFade.prototype.dispose=function(){return t.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},t.CrossFade}),Module(function(t){return t.Filter=function(){t.call(this);var e=this.optionsObject(arguments,["frequency","type","rolloff"],t.Filter.defaults);this._filters=[],this.frequency=new t.Signal(e.frequency,t.Type.Frequency),this.detune=new t.Signal(0,t.Type.Cents),this.gain=new t.Signal({value:e.gain,units:t.Type.Decibels,convert:!1}),this.Q=new t.Signal(e.Q),this._type=e.type,this._rolloff=e.rolloff,this.rolloff=e.rolloff,this._readOnly(["detune","frequency","gain","Q"])},t.extend(t.Filter),t.Filter.defaults={type:"lowpass",frequency:350,rolloff:-12,Q:1,gain:0},Object.defineProperty(t.Filter.prototype,"type",{get:function(){return this._type},set:function(t){var e,i=["lowpass","highpass","bandpass","lowshelf","highshelf","notch","allpass","peaking"];if(-1===i.indexOf(t))throw new TypeError("Tone.Filter does not have filter type "+t);for(this._type=t,e=0;e<this._filters.length;e++)this._filters[e].type=t}}),Object.defineProperty(t.Filter.prototype,"rolloff",{get:function(){return this._rolloff},set:function(t){var e,i,s,n,o=[-12,-24,-48],r=o.indexOf(t);if(-1===r)throw new RangeError("Filter rolloff can only be -12, -24, or -48");for(r++,this._rolloff=t,this.input.disconnect(),e=0;e<this._filters.length;e++)this._filters[e].disconnect(),this._filters[e]=null;for(this._filters=new Array(r),i=0;r>i;i++)s=this.context.createBiquadFilter(),s.type=this._type,this.frequency.connect(s.frequency),this.detune.connect(s.detune),this.Q.connect(s.Q),this.gain.connect(s.gain),this._filters[i]=s;n=[this.input].concat(this._filters).concat([this.output]),this.connectSeries.apply(this,n)}}),t.Filter.prototype.dispose=function(){t.prototype.dispose.call(this);for(var e=0;e<this._filters.length;e++)this._filters[e].disconnect(),this._filters[e]=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},t.Filter}),Module(function(t){return t.MultibandSplit=function(){var e=this.optionsObject(arguments,["lowFrequency","highFrequency"],t.MultibandSplit.defaults);this.input=this.context.createGain(),this.output=new Array(3),this.low=this.output[0]=new t.Filter(0,"lowpass"),this._lowMidFilter=new t.Filter(0,"highpass"),this.mid=this.output[1]=new t.Filter(0,"lowpass"),this.high=this.output[2]=new t.Filter(0,"highpass"),this.lowFrequency=new t.Signal(e.lowFrequency,t.Type.Frequency),this.highFrequency=new t.Signal(e.highFrequency
this},t.Limiter}),Module(function(t){return t.LowpassCombFilter=function(){t.call(this);var e=this.optionsObject(arguments,["delayTime","resonance","dampening"],t.LowpassCombFilter.defaults);this._delay=this.input=this.context.createDelay(1),this.delayTime=new t.Signal(e.delayTime,t.Type.Time),this._lowpass=this.output=this.context.createBiquadFilter(),this._lowpass.Q.value=0,this._lowpass.type="lowpass",this.dampening=new t.Signal(this._lowpass.frequency,t.Type.Frequency),this.dampening.value=e.dampening,this._feedback=this.context.createGain(),this.resonance=new t.Signal(e.resonance,t.Type.NormalRange),this._delay.chain(this._lowpass,this._feedback,this._delay),this.delayTime.connect(this._delay.delayTime),this.resonance.connect(this._feedback.gain),this.dampening.connect(this._lowpass.frequency),this._readOnly(["dampening","resonance","delayTime"])},t.extend(t.LowpassCombFilter),t.LowpassCombFilter.defaults={delayTime:.1,resonance:.5,dampening:3e3},t.LowpassCombFilter.prototype.dispose=function(){return t.prototype.dispose.call(this),this._writable(["dampening","resonance","delayTime"]),this.dampening.dispose(),this.dampening=null,this.resonance.dispose(),this.resonance=null,this._delay.disconnect(),this._delay=null,this._lowpass.disconnect(),this._lowpass=null,this._feedback.disconnect(),this._feedback=null,this.delayTime.dispose(),this.delayTime=null,this},t.LowpassCombFilter}),Module(function(t){return t.Merge=function(){t.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)},t.extend(t.Merge),t.Merge.prototype.dispose=function(){return t.prototype.dispose.call(this),this.left.disconnect(),this.left=null,this.right.disconnect(),this.right=null,this._merger.disconnect(),this._merger=null,this},t.Merge}),Module(function(t){return t.Meter=function(e,i,s){t.call(this),this._channels=this.defaultArg(e,1),this._smoothing=this.defaultArg(i,.8),this._clipMemory=1e3*this.defaultArg(s,.5),this._volume=new Array(this._channels),this._values=new Array(this._channels);for(var n=0;n<this._channels;n++)this._volume[n]=0,this._values[n]=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)},t.extend(t.Meter),t.Meter.prototype._onprocess=function(t){var e,i,s,n,o,r,l,a,h,u=this._jsNode.bufferSize,c=this._smoothing;for(e=0;e<this._channels;e++){for(i=t.inputBuffer.getChannelData(e),s=0,n=0,r=!1,l=0;u>l;l++)o=i[l],!r&&o>.95&&(r=!0,this._lastClip=Date.now()),n+=o,s+=o*o;a=n/u,h=Math.sqrt(s/u),this._volume[e]=Math.max(h,this._volume[e]*c),this._values[e]=a}},t.Meter.prototype.getLevel=function(t){t=this.defaultArg(t,0);var e=this._volume[t];return 1e-5>e?0:e},t.Meter.prototype.getValue=function(t){return t=this.defaultArg(t,0),this._values[t]},t.Meter.prototype.getDb=function(t){return this.gainToDb(this.getLevel(t))},t.Meter.prototype.isClipped=function(){return Date.now()-this._lastClip<this._clipMemory},t.Meter.prototype.dispose=function(){return t.prototype.dispose.call(this),this._jsNode.disconnect(),this._jsNode.onaudioprocess=null,this._volume=null,this._values=null,this},t.Meter}),Module(function(t){return t.Split=function(){t.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)},t.extend(t.Split),t.Split.prototype.dispose=function(){return t.prototype.dispose.call(this),this._splitter.disconnect(),this.left.disconnect(),this.right.disconnect(),this.left=null,this.right=null,this._splitter=null,this},t.Split}),Module(function(t){t.MidSideSplit=function(){t.call(this,0,2),this._split=this.input=new t.Split,this.mid=this.output[0]=new t.Expr("($0 + $
this.harmonicity.dispose(),this.harmonicity=null,this.vibratoAmount.dispose(),this.vibratoAmount=null,this.vibratoRate=null,this},t.DuoSynth}),Module(function(t){return t.FMSynth=function(e){e=this.defaultArg(e,t.FMSynth.defaults),t.Monophonic.call(this,e),this.carrier=new t.MonoSynth(e.carrier),this.carrier.volume.value=-10,this.modulator=new t.MonoSynth(e.modulator),this.modulator.volume.value=-10,this.frequency=new t.Signal(440,t.Type.Frequency),this.harmonicity=new t.Multiply(e.harmonicity),this.harmonicity.units=t.Type.Positive,this.modulationIndex=new t.Multiply(e.modulationIndex),this.modulationIndex.units=t.Type.Positive,this._modulationNode=this.context.createGain(),this.frequency.connect(this.carrier.frequency),this.frequency.chain(this.harmonicity,this.modulator.frequency),this.frequency.chain(this.modulationIndex,this._modulationNode),this.modulator.connect(this._modulationNode.gain),this._modulationNode.gain.value=0,this._modulationNode.connect(this.carrier.frequency),this.carrier.connect(this.output),this._readOnly(["carrier","modulator","frequency","harmonicity","modulationIndex"])},t.extend(t.FMSynth,t.Monophonic),t.FMSynth.defaults={harmonicity:3,modulationIndex:10,carrier:{volume:-10,portamento:0,oscillator:{type:"sine"},envelope:{attack:.01,decay:0,sustain:1,release:.5},filterEnvelope:{attack:.01,decay:0,sustain:1,release:.5,min:2e4,max:2e4}},modulator:{volume:-10,portamento:0,oscillator:{type:"triangle"},envelope:{attack:.01,decay:0,sustain:1,release:.5},filterEnvelope:{attack:.01,decay:0,sustain:1,release:.5,min:2e4,max:2e4}}},t.FMSynth.prototype._triggerEnvelopeAttack=function(t,e){return t=this.toSeconds(t),this.carrier.envelope.triggerAttack(t,e),this.modulator.envelope.triggerAttack(t),this.carrier.filterEnvelope.triggerAttack(t),this.modulator.filterEnvelope.triggerAttack(t),this},t.FMSynth.prototype._triggerEnvelopeRelease=function(t){return this.carrier.triggerRelease(t),this.modulator.triggerRelease(t),this},t.FMSynth.prototype.dispose=function(){return t.Monophonic.prototype.dispose.call(this),this._writable(["carrier","modulator","frequency","harmonicity","modulationIndex"]),this.carrier.dispose(),this.carrier=null,this.modulator.dispose(),this.modulator=null,this.frequency.dispose(),this.frequency=null,this.modulationIndex.dispose(),this.modulationIndex=null,this.harmonicity.dispose(),this.harmonicity=null,this._modulationNode.disconnect(),this._modulationNode=null,this},t.FMSynth}),Module(function(t){t.Noise=function(){var e=this.optionsObject(arguments,["type"],t.Noise.defaults);t.Source.call(this,e),this._source=null,this._buffer=null,this.type=e.type},t.extend(t.Noise,t.Source),t.Noise.defaults={type:"white"},Object.defineProperty(t.Noise.prototype,"type",{get:function(){return this._buffer===s?"white":this._buffer===i?"brown":this._buffer===e?"pink":void 0},set:function(n){if(this.type!==n){switch(n){case"white":this._buffer=s;break;case"pink":this._buffer=e;break;case"brown":this._buffer=i;break;default:this._buffer=s}if(this.state===t.State.Started){var o=this.now()+this.bufferTime;this._source.onended=void 0,this._stop(o),this._start(o)}}}}),t.Noise.prototype._start=function(t){this._source=this.context.createBufferSource(),this._source.buffer=this._buffer,this._source.loop=!0,this.connectSeries(this._source,this.output),this._source.start(this.toSeconds(t)),this._source.onended=this.onended},t.Noise.prototype._stop=function(t){this._source&&this._source.stop(this.toSeconds(t))},t.Noise.prototype.dispose=function(){return t.Source.prototype.dispose.call(this),null!==this._source&&(this._source.disconnect(),this._source=null),this._buffer=null,this};var e=null,i=null,s=null;return t._initAudioContext(function(t){var n=t.sampleRate,o=4*n;e=function(){var e,i,s,r,l,a,h,u,c,p,d,f=t.createBuffer(2,o,n);for(e=0;e<f.numberOfChannels;e++)for(i=f.getChannelData(e),s=r=l=a=h=u=c=0,p=0;o>p;p++)d=2*Math.random()-1,s=.99886*s+.0555179*d,r=.99332*r+.0750759*d,l=.969*l+.153852*d,a=.8665*a+.3104856*d,h=.55*h+.5329522*d,u=-.7616*u-.016898*d,i[p]=s+r+l+a+h+u+c+.5362*d,i[p]*=.11,c=.115926*d;return f
t.Convolver.prototype.load=function(t,e){return this._buffer.load(t,function(t){this.buffer=t,e&&e()}.bind(this)),this},t.Convolver.prototype.dispose=function(){return t.Effect.prototype.dispose.call(this),this._convolver.disconnect(),this._convolver=null,this._buffer.dispose(),this._buffer=null,this},t.Convolver}),Module(function(t){return t.Distortion=function(){var e=this.optionsObject(arguments,["distortion"],t.Distortion.defaults);t.Effect.call(this,e),this._shaper=new t.WaveShaper(4096),this._distortion=e.distortion,this.connectEffect(this._shaper),this.distortion=e.distortion,this.oversample=e.oversample},t.extend(t.Distortion,t.Effect),t.Distortion.defaults={distortion:.4,oversample:"none"},Object.defineProperty(t.Distortion.prototype,"distortion",{get:function(){return this._distortion},set:function(t){var e,i;this._distortion=t,e=100*t,i=Math.PI/180,this._shaper.setMap(function(t){return Math.abs(t)<.001?0:(3+e)*t*20*i/(Math.PI+e*Math.abs(t))})}}),Object.defineProperty(t.Distortion.prototype,"oversample",{get:function(){return this._shaper.oversample},set:function(t){this._shaper.oversample=t}}),t.Distortion.prototype.dispose=function(){return t.Effect.prototype.dispose.call(this),this._shaper.dispose(),this._shaper=null,this},t.Distortion}),Module(function(t){return t.FeedbackDelay=function(){var e=this.optionsObject(arguments,["delayTime","feedback"],t.FeedbackDelay.defaults);t.FeedbackEffect.call(this,e),this.delayTime=new t.Signal(e.delayTime,t.Type.Time),this._delayNode=this.context.createDelay(4),this.connectEffect(this._delayNode),this.delayTime.connect(this._delayNode.delayTime),this._readOnly(["delayTime"])},t.extend(t.FeedbackDelay,t.FeedbackEffect),t.FeedbackDelay.defaults={delayTime:.25},t.FeedbackDelay.prototype.dispose=function(){return t.FeedbackEffect.prototype.dispose.call(this),this.delayTime.dispose(),this._delayNode.disconnect(),this._delayNode=null,this._writable(["delayTime"]),this.delayTime=null,this},t.FeedbackDelay}),Module(function(t){var e=[1557/44100,1617/44100,1491/44100,1422/44100,1277/44100,1356/44100,1188/44100,1116/44100],i=[225,556,441,341];return t.Freeverb=function(){var s,n,o,r,l,a,h=this.optionsObject(arguments,["roomSize","dampening"],t.Freeverb.defaults);for(t.StereoEffect.call(this,h),this.roomSize=new t.Signal(h.roomSize,t.Type.NormalRange),this.dampening=new t.Signal(h.dampening,t.Type.Frequency),this._combFilters=[],this._allpassFiltersL=[],this._allpassFiltersR=[],s=0;s<i.length;s++)n=this.context.createBiquadFilter(),n.type="allpass",n.frequency.value=i[s],this._allpassFiltersL.push(n);for(o=0;o<i.length;o++)r=this.context.createBiquadFilter(),r.type="allpass",r.frequency.value=i[o],this._allpassFiltersR.push(r);for(l=0;l<e.length;l++)a=new t.LowpassCombFilter(e[l]),l<e.length/2?this.effectSendL.chain(a,this._allpassFiltersL[0]):this.effectSendR.chain(a,this._allpassFiltersR[0]),this.roomSize.connect(a.resonance),this.dampening.connect(a.dampening),this._combFilters.push(a);this.connectSeries.apply(this,this._allpassFiltersL),this.connectSeries.apply(this,this._allpassFiltersR),this._allpassFiltersL[this._allpassFiltersL.length-1].connect(this.effectReturnL),this._allpassFiltersR[this._allpassFiltersR.length-1].connect(this.effectReturnR),this._readOnly(["roomSize","dampening"])},t.extend(t.Freeverb,t.StereoEffect),t.Freeverb.defaults={roomSize:.7,dampening:3e3},t.Freeverb.prototype.dispose=function(){var e,i,s;for(t.StereoEffect.prototype.dispose.call(this),e=0;e<this._allpassFiltersL.length;e++)this._allpassFiltersL[e].disconnect(),this._allpassFiltersL[e]=null;for(this._allpassFiltersL=null,i=0;i<this._allpassFiltersR.length;i++)this._allpassFiltersR[i].disconnect(),this._allpassFiltersR[i]=null;for(this._allpassFiltersR=null,s=0;s<this._combFilters.length;s++)this._combFilters[s].dispose(),this._combFilters[s]=null;return this._combFilters=null,this._writable(["roomSize","dampening"]),this.roomSize.dispose(),this.roomSize=null,this.dampening.dispose(),this.dampening=null,this},t.Freeverb}),Module(function(t){var e=[.06748,.06404,.08212,.09004],i=[.773,.802,.753