You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

6 lines
106 KiB
JavaScript

/*! p5.sound.min.js v0.3.2 2016-11-01 */
!function(t,e){"function"==typeof define&&define.amd?define("p5.sound",["p5"],function(t){e(t)}):e("object"==typeof exports?require("../p5"):t.p5)}(this,function(p5){var sndcore;sndcore=function(){"use strict";!function(t,e,i){function o(t){t&&(t.setTargetAtTime||(t.setTargetAtTime=t.setTargetValueAtTime))}return e=e||{},window.hasOwnProperty("webkitAudioContext")&&!window.hasOwnProperty("AudioContext")&&(window.AudioContext=webkitAudioContext,"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.createScriptProcessor&&(AudioContext.prototype.createScriptProcessor=AudioContext.prototype.createJavaScriptNode),"function"!=typeof AudioContext.prototype.createPeriodicWave&&(AudioContext.prototype.createPeriodicWave=AudioContext.prototype.createWaveTable),AudioContext.prototype.internal_createGain=AudioContext.prototype.createGain,AudioContext.prototype.createGain=function(){var t=this.internal_createGain();return o(t.gain),t},AudioContext.prototype.internal_createDelay=AudioContext.prototype.createDelay,AudioContext.prototype.createDelay=function(t){var e=t?this.internal_createDelay(t):this.internal_createDelay();return o(e.delayTime),e},AudioContext.prototype.internal_createBufferSource=AudioContext.prototype.createBufferSource,AudioContext.prototype.createBufferSource=function(){var t=this.internal_createBufferSource();return t.start?(t.internal_start=t.start,t.start=function(e,i,o){"undefined"!=typeof o?t.internal_start(e||0,i,o):t.internal_start(e||0,i||0)}):t.start=function(t,e,i){e||i?this.noteGrainOn(t||0,e,i):this.noteOn(t||0)},t.stop?(t.internal_stop=t.stop,t.stop=function(e){t.internal_stop(e||0)}):t.stop=function(t){this.noteOff(t||0)},o(t.playbackRate),t},AudioContext.prototype.internal_createDynamicsCompressor=AudioContext.prototype.createDynamicsCompressor,AudioContext.prototype.createDynamicsCompressor=function(){var t=this.internal_createDynamicsCompressor();return o(t.threshold),o(t.knee),o(t.ratio),o(t.reduction),o(t.attack),o(t.release),t},AudioContext.prototype.internal_createBiquadFilter=AudioContext.prototype.createBiquadFilter,AudioContext.prototype.createBiquadFilter=function(){var t=this.internal_createBiquadFilter();return o(t.frequency),o(t.detune),o(t.Q),o(t.gain),t},"function"!=typeof AudioContext.prototype.createOscillator&&(AudioContext.prototype.internal_createOscillator=AudioContext.prototype.createOscillator,AudioContext.prototype.createOscillator=function(){var t=this.internal_createOscillator();return t.start?(t.internal_start=t.start,t.start=function(e){t.internal_start(e||0)}):t.start=function(t){this.noteOn(t||0)},t.stop?(t.internal_stop=t.stop,t.stop=function(e){t.internal_stop(e||0)}):t.stop=function(t){this.noteOff(t||0)},t.setPeriodicWave||(t.setPeriodicWave=t.setWaveTable),o(t.frequency),o(t.detune),t})),window.hasOwnProperty("webkitOfflineAudioContext")&&!window.hasOwnProperty("OfflineAudioContext")&&(window.OfflineAudioContext=webkitOfflineAudioContext),e}(window);var t=new window.AudioContext;p5.prototype.getAudioContext=function(){return t},navigator.getUserMedia=navigator.getUserMedia||navigator.webkitGetUserMedia||navigator.mozGetUserMedia||navigator.msGetUserMedia;var e=document.createElement("audio");p5.prototype.isSupported=function(){return!!e.canPlayType};var i=function(){return!!e.canPlayType&&e.canPlayType('audio/ogg; codecs="vorbis"')},o=function(){return!!e.canPlayType&&e.canPlayType("audio/mpeg;")},n=function(){return!!e.canPlayType&&e.canPlayType('audio/wav; codecs="1"')},r=function(){return!!e.canPlayType&&(e.canPlayType("audio/x-m4a;")||e.canPlayType("audio/aac;"))},s=function(){return!!e.canPlayType&&e.canPlayType("audio/x-aiff;")};p5.prototype.isFileSupported=function(t){switch(t.toLowerCase()){case"mp3":return o();case"wav":return n();case"ogg":return i();case"mp4":return r();case"aiff":return s();default:return!1}};
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,o){if(e.input)Array.isArray(e.input)?(t(o)&&(o=0),this.connect(e.input[o])):this.connect(e.input,i,o);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 o=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))};o.prototype.set=function(e,i,n){if(this.isObject(e))n=i;else if(this.isString(e)){var r={};r[e]=i,e=r}for(var s in e){i=e[s];var a=this;if(-1!==s.indexOf(".")){for(var u=s.split("."),p=0;p<u.length-1;p++)a=a[u[p]];s=u[u.length-1]}var c=a[s];t(c)||(o.Signal&&c instanceof o.Signal||o.Param&&c instanceof o.Param?c.value!==i&&(t(n)?c.value=i:c.rampTo(i,n)):c instanceof AudioParam?c.value!==i&&(c.value=i):c instanceof o?c.set(i):c!==i&&(a[s]=i))}return this},o.prototype.get=function(i){t(i)?i=this._collectDefaults(this.constructor):this.isString(i)&&(i=[i]);for(var n={},r=0;r<i.length;r++){var s=i[r],a=this,u=n;if(-1!==s.indexOf(".")){for(var p=s.split("."),c=0;c<p.length-1;c++){var h=p[c];u[h]=u[h]||{},u=u[h],a=a[h]}s=p[p.length-1]}var l=a[s];this.isObject(i[s])?u[s]=l.get():o.Signal&&l instanceof o.Signal?u[s]=l.value:o.Param&&l instanceof o.Param?u[s]=l.value:l instanceof AudioParam?u[s]=l.value:l instanceof o?u[s]=l.get():e(l)||t(l)||(u[s]=l)}return n},o.prototype._collectDefaults=function(e){var i=[];if(t(e.defaults)||(i=Object.keys(e.defaults)),!t(e._super))for(var o=this._collectDefaults(e._super),n=0;n<o.length;n++)-1===i.indexOf(o[n])&&i.push(o[n]);return i},o.prototype.toString=function(){for(var t in o){var i=t[0].match(/^[A-Z]$/),n=o[t]===this.constructor;if(e(o[t])&&i&&n)return t}return"Tone"},o.context=i,o.prototype.context=o.context,o.prototype.bufferSize=2048,o.prototype.blockTime=128/o.context.sampleRate,o.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};var n=null;o.prototype.noGC=function(){return this.output.connect(n),this},AudioNode.prototype.noGC=function(){return this.connect(n),this},o.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},o.prototype.disconnect=function(t){return Array.isArray(this.output)?(t=this.defaultArg(t,0),this.output[t].disconnect()):this.output.disconnect(),this},o.prototype.connectSeries=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}return this},o.prototype.connectParallel=function(){var t=arguments[0];if(arguments.length>1)for(var e=1;e<arguments.length;e++){var i=arguments[e];t.connect(i)}return this},o.prototype.chain=function(){if(arguments.length>0)for(var t=this,e=0;e<arguments.length;e++){var i=arguments[e];t.connect(i),t=i}return this},o.prototype.fan=function(){if(arguments.length>0)for(var t=0;t<arguments.length;t++)this.connect(arguments[t]);return this},AudioNode.prototype.chain=o.prototype.chain,AudioNode.prototype.fan=o.prototype.fan,o.prototype.defaultArg=function(e,i){if(this.isObject(e)&&this.isObject(i)){var o={};for(var n in e)o[n]=this.defaultArg(i[n],e[n]);for(var r in i)o[r]=this.defaultArg(e[r],i[r]);return o}return t(e)?i:e},o.prototype.optionsObject=function(t,e,i){var o={};if(1===t.length&&this.isObject(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)},o.prototype.isUndef=t,o.prototype.
i=this.toSeconds(i),this._events.addEvent({type:t.TimelineSignal.Type.Linear,value:e,time:i}),this._param.linearRampToValueAtTime(e,i),this},t.TimelineSignal.prototype.exponentialRampToValueAtTime=function(e,i){return e=this._fromUnits(e),e=Math.max(this._minOutput,e),i=this.toSeconds(i),this._events.addEvent({type:t.TimelineSignal.Type.Exponential,value:e,time:i}),this._param.exponentialRampToValueAtTime(e,i),this},t.TimelineSignal.prototype.setTargetAtTime=function(e,i,o){return e=this._fromUnits(e),e=Math.max(this._minOutput,e),o=Math.max(this._minOutput,o),i=this.toSeconds(i),this._events.addEvent({type:t.TimelineSignal.Type.Target,value:e,time:i,constant:o}),this._param.setTargetAtTime(e,i,o),this},t.TimelineSignal.prototype.cancelScheduledValues=function(t){return this._events.cancel(t),this._param.cancelScheduledValues(this.toSeconds(t)),this},t.TimelineSignal.prototype.setRampPoint=function(e){e=this.toSeconds(e);var i=this.getValueAtTime(e),o=this._searchAfter(e);return o&&(this.cancelScheduledValues(e),o.type===t.TimelineSignal.Type.Linear?this.linearRampToValueAtTime(i,e):o.type===t.TimelineSignal.Type.Exponential&&this.exponentialRampToValueAtTime(i,e)),this.setValueAtTime(i,e),this},t.TimelineSignal.prototype.linearRampToValueBetween=function(t,e,i){return this.setRampPoint(e),this.linearRampToValueAtTime(t,i),this},t.TimelineSignal.prototype.exponentialRampToValueBetween=function(t,e,i){return this.setRampPoint(e),this.exponentialRampToValueAtTime(t,i),this},t.TimelineSignal.prototype._searchBefore=function(t){return this._events.getEvent(t)},t.TimelineSignal.prototype._searchAfter=function(t){return this._events.getEventAfter(t)},t.TimelineSignal.prototype.getValueAtTime=function(e){var i=this._searchAfter(e),o=this._searchBefore(e),n=this._initial;if(null===o)n=this._initial;else if(o.type===t.TimelineSignal.Type.Target){var r,s=this._events.getEventBefore(o.time);r=null===s?this._initial:s.value,n=this._exponentialApproach(o.time,r,o.value,o.constant,e)}else n=null===i?o.value:i.type===t.TimelineSignal.Type.Linear?this._linearInterpolate(o.time,o.value,i.time,i.value,e):i.type===t.TimelineSignal.Type.Exponential?this._exponentialInterpolate(o.time,o.value,i.time,i.value,e):o.value;return n},t.TimelineSignal.prototype.connect=t.SignalBase.prototype.connect,t.TimelineSignal.prototype._exponentialApproach=function(t,e,i,o,n){return i+(e-i)*Math.exp(-(n-t)/o)},t.TimelineSignal.prototype._linearInterpolate=function(t,e,i,o,n){return e+(o-e)*((n-t)/(i-t))},t.TimelineSignal.prototype._exponentialInterpolate=function(t,e,i,o,n){return e=Math.max(this._minOutput,e),e*Math.pow(o/e,(n-t)/(i-t))},t.TimelineSignal.prototype.dispose=function(){t.Signal.prototype.dispose.call(this),t.Param.prototype.dispose.call(this),this._events.dispose(),this._events=null},t.TimelineSignal}(Tone_core_Tone,Tone_signal_Signal);var env;env=function(){"use strict";var t=master,e=Tone_signal_Add,i=Tone_signal_Multiply,o=Tone_signal_Scale,n=Tone_signal_TimelineSignal,r=Tone_core_Tone;r.setContext(t.audiocontext),p5.Env=function(e,i,o,r,s,a){t.audiocontext.currentTime;this.aTime=e||.1,this.aLevel=i||1,this.dTime=o||.5,this.dLevel=r||0,this.rTime=s||0,this.rLevel=a||0,this._rampHighPercentage=.98,this._rampLowPercentage=.02,this.output=t.audiocontext.createGain(),this.control=new n,this._init(),this.control.connect(this.output),this.connection=null,this.mathOps=[this.control],this.isExponential=!1,this.sourceToClear=null,this.wasTriggered=!1,t.soundArray.push(this)},p5.Env.prototype._init=function(){var e=t.audiocontext.currentTime,i=e;this.control.setTargetAtTime(1e-5,i,.001),this._setRampAD(this.aTime,this.dTime)},p5.Env.prototype.set=function(t,e,i,o,n,r){this.aTime=t,this.aLevel=e,this.dTime=i||0,this.dLevel=o||0,this.rTime=n||0,this.rLevel=r||0,this._setRampAD(t,i)},p5.Env.prototype.setADSR=function(t,e,i,o){this.aTime=t,this.dTime=e||0,this.sPercent=i||0,this.dLevel="undefined"!=typeof i?i*(this.aLevel-this.rLevel)+this.rLevel:0,this.rTime=o||0,this._setRampAD(t,e)},p5.Env.prototype.setRange=function(t,e){this.aLevel=t||1,this.rLevel
cancelAnimationFrame(this._loopID),t.TimelineState.prototype.dispose.call(this),this._writable("frequency"),this.frequency.dispose(),this.frequency=null,this._boundLoop=t.noOp,this._nextTick=1/0,this.callback=null,this._state.dispose(),this._state=null},t.Clock}(Tone_core_Tone,Tone_signal_TimelineSignal);var metro;metro=function(){"use strict";var t=master,e=Tone_core_Clock;t.audiocontext;p5.Metro=function(){this.clock=new e({callback:this.ontick.bind(this)}),this.syncedParts=[],this.bpm=120,this._init(),this.tickCallback=function(){}};var i=0,o=0;p5.Metro.prototype.ontick=function(e){var n=e-i,r=e-t.audiocontext.currentTime;if(!(-.02>=n-o)){i=e;for(var s in this.syncedParts){var a=this.syncedParts[s];if(!a.isPlaying)return;a.incrementStep(r);for(var u in a.phrases){var p=a.phrases[u],c=p.sequence,h=this.metroTicks%c.length;0!==c[h]&&(this.metroTicks<c.length||!p.looping)&&p.callback(r,c[h])}}this.metroTicks+=1,this.tickCallback(r)}},p5.Metro.prototype.setBPM=function(e,i){var n=60/(e*this.tatums),r=t.audiocontext.currentTime;o=n;var i=i||0;this.clock.frequency.setValueAtTime(this.clock.frequency.value,r),this.clock.frequency.linearRampToValueAtTime(e,r+i),this.bpm=e},p5.Metro.prototype.getBPM=function(t){return this.clock.getRate()/this.tatums*60},p5.Metro.prototype._init=function(){this.metroTicks=0},p5.Metro.prototype.resetSync=function(t){this.syncedParts=[t]},p5.Metro.prototype.pushSync=function(t){this.syncedParts.push(t)},p5.Metro.prototype.start=function(e){var i=e||0,o=t.audiocontext.currentTime;this.clock.start(o+i),this.setBPM(this.bpm)},p5.Metro.prototype.stop=function(e){var i=e||0,o=t.audiocontext.currentTime;this.clock._oscillator&&this.clock.stop(o+i)},p5.Metro.prototype.beatLength=function(t){this.tatums=1/t/4}}(master,Tone_core_Clock);var looper;looper=function(){"use strict";function t(t){t.currentPart++,t.currentPart>=t.parts.length?(t.scoreStep=0,t.onended()):(t.scoreStep=0,t.parts[t.currentPart-1].stop(),t.parts[t.currentPart].start())}var e=master,i=120;p5.prototype.setBPM=function(t,o){i=t;for(var n in e.parts)e.parts[n].setBPM(i,o)},p5.Phrase=function(t,e,i){this.phraseStep=0,this.name=t,this.callback=e,this.sequence=i},p5.Part=function(t,o){this.length=t||0,this.partStep=0,this.phrases=[],this.isPlaying=!1,this.noLoop(),this.tatums=o||.0625,this.metro=new p5.Metro,this.metro._init(),this.metro.beatLength(this.tatums),this.metro.setBPM(i),e.parts.push(this),this.callback=function(){}},p5.Part.prototype.setBPM=function(t,e){this.metro.setBPM(t,e)},p5.Part.prototype.getBPM=function(){return this.metro.getBPM()},p5.Part.prototype.start=function(t){if(!this.isPlaying){this.isPlaying=!0,this.metro.resetSync(this);var e=t||0;this.metro.start(e)}},p5.Part.prototype.loop=function(t){this.looping=!0,this.onended=function(){this.partStep=0};var e=t||0;this.start(e)},p5.Part.prototype.noLoop=function(){this.looping=!1,this.onended=function(){this.stop()}},p5.Part.prototype.stop=function(t){this.partStep=0,this.pause(t)},p5.Part.prototype.pause=function(t){this.isPlaying=!1;var e=t||0;this.metro.stop(e)},p5.Part.prototype.addPhrase=function(t,e,i){var o;if(3===arguments.length)o=new p5.Phrase(t,e,i);else{if(!(arguments[0]instanceof p5.Phrase))throw"invalid input. addPhrase accepts name, callback, array or a p5.Phrase";o=arguments[0]}this.phrases.push(o),o.sequence.length>this.length&&(this.length=o.sequence.length)},p5.Part.prototype.removePhrase=function(t){for(var e in this.phrases)this.phrases[e].name===t&&this.phrases.splice(e,1)},p5.Part.prototype.getPhrase=function(t){for(var e in this.phrases)if(this.phrases[e].name===t)return this.phrases[e]},p5.Part.prototype.replaceSequence=function(t,e){for(var i in this.phrases)this.phrases[i].name===t&&(this.phrases[i].sequence=e)},p5.Part.prototype.incrementStep=function(t){this.partStep<this.length-1?(this.callback(t),this.partStep+=1):this.looping||this.partStep!=this.length-1||(console.log("done"),this.onended())},p5.Part.prototype.onStep=function(t){this.callback=t},p5.Score=function(){this.parts=[],this.currentPart=0;var e=this;for(var i in arguments)this