-
Notifications
You must be signed in to change notification settings - Fork 0
/
crotchet.min.js
1 lines (1 loc) · 9.14 KB
/
crotchet.min.js
1
function onBufferLoaded(e,t,n){var r=new Array;var i=CROTCHET.context.destination;for(var s=0;s<e.length;s++){var o=CROTCHET.context.createBufferSource();o.buffer=e[s];t.node=o;if(n!=null&&n!=undefined)t.connectNode(n);else o.connect(i);r.push(t)}if(n!=null&&n!=undefined){n.setOut(i)}for(var s=0;s<r.length;s++){r[s].loaded=true;console.log("Loaded "+"play$ound."+t.name);var u=new Event("play$ound."+t.name);dispatchEvent(u)}}var CROTCHET={REVISION:"1"};navigator.getUserMedia=navigator.getUserMedia||navigator.webkitGetUserMedia||navigator.mozGetUserMedia||navigator.msGetUserMedia;window.requestAnimFrame=function(){return window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.oRequestAnimationFrame||window.msRequestAnimationFrame||function(e){window.setTimeout(e,1e3/60)}}();CROTCHET.update=function(){};CROTCHET.mainGain=null;CROTCHET.setGlobalVolume=function(e){if(e>=0)CROTCHET.mainGain.gain.value=e/100;else console.log("Error on setGlobalVolume, invalid value")};CROTCHET.connections=new Array;CROTCHET.root=0;CROTCHET.mediaNodes=new Array;CROTCHET.initContext=function(){var e=window.AudioContext||window.webkitAudioContext||window.mozAudioContext||window.oAudioContext||window.msAudioContext;if(e){CROTCHET.context=new e;CROTCHET.root=new CROTCHET.Node(null,null)}else alert("Your browser cannot use Audio API so this site will not work correctly for you.")};CROTCHET.Node=function(e,t){this.node=e;this.connections=new Array;this.connectedTo=t};CROTCHET.Node.prototype={connectNode:function(e){this.node.connect(e.node);this.connections.push(e)},setOut:function(e){if(this.connections!=null&&this.connections.length>0){this.connections[0].setOut(e)}else{if(CROTCHET.mainGain==null){CROTCHET.mainGain=CROTCHET.context.createGain()}this.node.connect(CROTCHET.mainGain);CROTCHET.mainGain.connect(e)}}};CROTCHET.MediaNode=function(e,t){this.base=CROTCHET.Node;this.base(e,t);this.loaded=false;this.name=null;this.played=false;this.changeSpeed=function(e){if(e>0)this.node.playbackRate.value=e/100;else console.log("Speed cannot be negative set a value greater than 0")};this.setLoopable=function(e,t,n){this.node.loop=false;if(t!=undefined&&t!=null&&n!=undefined&&n!=null&&t>=0&&n>=0){this.node.loopStart=t;this.node.loopEnd=n}};this.onEnd=function(e){this.node.onended=e}};CROTCHET.MediaNode.prototype=new CROTCHET.Node;CROTCHET.AnalyserNode=function(e,t){this.base=CROTCHET.Node;this.base(e,t);this.setFftSize=function(e){if(e!=undefined&&e!=null){if(e%2==0)this.node.fftSize=e;else console.log("Error in fftSize in setFftSize(fftSize)")}};this.getByteSpectrum=function(e,t){if(e!=undefined&&e!=null&&t!=undefined&&t!=null){this.node.maxDecibels=t;this.node.minDecibels=e}var n=new Uint8Array(this.node.frequencyBinCount);this.node.getByteFrequencyData(n);return n};this.getFloatSpectrum=function(){var e=new Float32Array(this.node.frequencyBinCount);this.node.getFloatFrequencyData(e);return e};this.getFloatWaveform=function(){var e=new Float32Array(this.node.frequencyBinCount);this.node.getFloatTimeDomainData(e);return e};this.getByteWaveform=function(e,t){if(e!=undefined&&e!=null&&t!=undefined&&t!=null){this.node.maxDecibels=t;this.node.minDecibels=e}var n=new Uint8Array(this.node.frequencyBinCount);this.node.getByteTimeDomainData(n);return n}};CROTCHET.AnalyserNode.prototype=new CROTCHET.Node;CROTCHET.createAnalyser=function(e){var t=CROTCHET.context.createAnalyser();if(e!=undefined&&e!=null){if(e%2==0)t.fftSize=e;else console.log("Error in fftSize in createAnalyser(fftSize)")}var n=new CROTCHET.AnalyserNode(t,null);return n};CROTCHET.FilterNode=function(e,t){this.base=CROTCHET.Node;this.base(e,t);this.setQ=function(e){if(e>0&&e<=1e3)this.node.Q.value=e};this.setGain=function(e){if(e>=-40&&e<=40)this.filter.gain.value=e};this.setType=function(e){switch(e.toUpperCase()){case"NOTCH":this.node.type="notch";break;case"LOWPASS":this.node.type="lowpass";break;case"HIGHPASS":this.node.type="highpass";break;case"BANDPASS":this.node.type="bandpass";break;case"ALLPASS":this.node.type="allpass";break;case"HIGHSELF":this.node.type="highself";break;case"LOWSHELF":this.node.type="lowshelf";break;case"PEAKING":this.node.type="peaking";break}}};CROTCHET.FilterNode.prototype=new CROTCHET.Node;CROTCHET.createFilter=function(e,t,n,r){var i=CROTCHET.context.createBiquadFilter();switch(e.toUpperCase()){case"NOTCH":i.type=i.NOTCH;break;case"LOWPASS":i.type=i.LOWPASS;break;case"HIGHPASS":i.type=i.HIGHPASS;break;case"BANDPASS":i.type=i.BANDPASS;break;case"ALLPASS":i.type=i.ALLPASS;break;case"HIGHSELF":i.type=i.HIGHSELF;break;case"LOWSHELF":i.type=i.LOWSHELF;break;case"PEAKING":i.type=i.PEAKING;break}i.frequency.value=t;i.Q.value=n;i.gain.value=r;var s=new CROTCHET.FilterNode(i,null);return s};CROTCHET.OscillatorNode=function(e,t){this.base=CROTCHET.Node;this.base(e,t);this.connected=false;this.start=function(e){if(!this.connected){this.setOut(CROTCHET.context.destination)}if(e!=undefined&&e!=null)this.node.start(e);else this.node.start(0)};this.stop=function(e){if(e!=undefined&&e!=null)this.node.stop(e);else this.node.stop(0)};this.setFrequency=function(e){this.node.frequency.value=e};this.setType=function(e){switch(e.toLowerCase()){case"square":this.node.type="square";break;case"line":this.node.type="line";break;case"triangle":this.node.type="triangle";break;case"sawtooth":this.node.type="sawtooth";break;case"custom":this.node.type="custom";break}if(e.toUpperCase()=="CUSTOM"){}};this.detune=function(e){this.node.detune.value=e}};CROTCHET.OscillatorNode.prototype=new CROTCHET.Node;CROTCHET.createOscillator=function(e,t,n){var r=CROTCHET.context.createOscillator();r.frequency.value=e;switch(t.toUpperCase()){case"SQUARE":r.type=r.SQUARE;break;case"SINE":r.type=r.SQUARE;break;case"TRIANGLE":r.type=r.SQUARE;break;case"SAWTOOTH":r.type=r.SAWTOOTH;break;case"CUSTOM":r.type=r.CUSTOM;break}r.type=t;if(t=="custom"){}var i=new CROTCHET.OscillatorNode(r,null);return i};CROTCHET.GainNode=function(e,t){this.base=CROTCHET.Node;this.base(e,t);this.setGain=function(e){this.node.gain.value=e/100}};CROTCHET.GainNode.prototype=new CROTCHET.Node;CROTCHET.createGain=function(e){var t=CROTCHET.context.createGain();if(e!=undefined&&e!=null)t.gain.value=e/100;else t.gain.value=1;var n=new CROTCHET.GainNode(t,null);return n};CROTCHET.microphoneObject=new function(){};CROTCHET.microphoneObject.initMicro=function(){navigator.getUserMedia({audio:true},this.onStream.bind(this),this.onStreamError.bind(this))};CROTCHET.microphoneObject.addConnections=function(e){if(typeof e==Array)this.conns=e;else{this.conns=new Array;this.conns.push(e)}};CROTCHET.microphoneObject.start=function(e){navigator.getUserMedia({audio:true},this.onStream.bind(this),this.onStreamError.bind(this));this.addConnections(e)};CROTCHET.microphoneObject.onStream=function(e){var t=CROTCHET.context.createMediaStreamSource(e);CROTCHET.start(t,this)};CROTCHET.microphoneObject.onStreamError=function(e){console.error("Error getting microphone",e)};CROTCHET.startMicrophone=function(e){var t=CROTCHET.microphoneObject;if(e!=null&&e!=undefined)t.addConnections(e);t.initMicro()};CROTCHET.start=function(e,t){var n=new CROTCHET.Node(e,CROTCHET.root);if(t.conns!=null&&t.conns!=undefined){for(var r=0;r<t.conns.length;r++){n.connectNode(t.conns[r])}}var i=CROTCHET.context.destination;n.setOut(i)};CROTCHET.BufferLoader=function(e,t,n,r,i){if(t.length==n.length){this.context=e;this.urlList=t;this.onload=r;this.conn=i;this.bufferList=new Array;this.loadCount=0;this.nameList=n}else alert("Error in media names and URLs")};CROTCHET.BufferLoader.prototype.loadBuffer=function(e,t,n){var r=new XMLHttpRequest;r.open("GET",e,true);r.responseType="arraybuffer";var i=new CROTCHET.MediaNode(null,null);i.name=t;CROTCHET.mediaNodes.push(i);var s=this;r.onload=function(){s.context.decodeAudioData(r.response,function(t){if(!t){alert("error decoding file data: "+e);return}s.bufferList[n]=t;if(++s.loadCount==s.urlList.length)s.onload(s.bufferList,i,s.conn)},function(e){console.error("decodeAudioData error",e)})};r.onerror=function(){alert("BufferLoader: XHR error in file "+e)};r.send()};CROTCHET.loadSounds=function(e,t,n){bufferLoader=new CROTCHET.BufferLoader(CROTCHET.context,e,t,onBufferLoaded,n);bufferLoader.load()};CROTCHET.BufferLoader.prototype.load=function(){for(var e=0;e<this.urlList.length;++e)this.loadBuffer(this.urlList[e],this.nameList[e],e)};CROTCHET.findMedia=function(e){for(var t=0;t<CROTCHET.mediaNodes.length;t++){if(CROTCHET.mediaNodes[t].name==e){return CROTCHET.mediaNodes[t]}}console.log("Error finding sound: "+e);return undefined};CROTCHET.play=function(e){var t=CROTCHET.findMedia(e);if(t.loaded){if(t.played==false){t.node.start();t.played=true}else{var n=CROTCHET.context.createBufferSource();var r=t.connections[0];var i=t.node.buffer;n.buffer=i;t.node.stop(0);t.node=n;t.node.connect(r.node);t.node.start();t.played=true}}else{addEventListener("play$ound."+e,function(){CROTCHET.play(e)})}};CROTCHET.stop=function(e){var t=CROTCHET.findMedia(e);if(t.played){var n=CROTCHET.context.createBufferSource();var r=t.connections[0];var i=t.node.buffer;n.buffer=i;t.node.stop(0);t.node=n;t.node.connect(r.node);t.played=false}};CROTCHET.setUpdate=function(e){CROTCHET.update=e};CROTCHET.frame=function(){CROTCHET.update();requestAnimationFrame(CROTCHET.frame)};CROTCHET.initContext()