forked from jsantell/dancer.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dancer.min.js
4 lines (4 loc) · 13.6 KB
/
dancer.min.js
1
2
3
4
/* dancer.js - v0.3.2 - 2012-12-07
* https://github.com/jsantell/dancer.js
* Copyright (c) 2012 Jordan Santell; Licensed MIT */
function FourierTransform(e,t){this.bufferSize=e,this.sampleRate=t,this.bandwidth=2/e*t/2,this.spectrum=new Float32Array(e/2),this.real=new Float32Array(e),this.imag=new Float32Array(e),this.peakBand=0,this.peak=0,this.getBandFrequency=function(e){return this.bandwidth*e+this.bandwidth/2},this.calculateSpectrum=function(){var t=this.spectrum,n=this.real,r=this.imag,i=2/this.bufferSize,s=Math.sqrt,o,u,a;for(var f=0,l=e/2;f<l;f++)o=n[f],u=r[f],a=i*s(o*o+u*u),a>this.peak&&(this.peakBand=f,this.peak=a),t[f]=a}}function FFT(e,t){FourierTransform.call(this,e,t),this.reverseTable=new Uint32Array(e);var n=1,r=e>>1,i;while(n<e){for(i=0;i<n;i++)this.reverseTable[i+n]=this.reverseTable[i]+r;n<<=1,r>>=1}this.sinTable=new Float32Array(e),this.cosTable=new Float32Array(e);for(i=0;i<e;i++)this.sinTable[i]=Math.sin(-Math.PI/i),this.cosTable[i]=Math.cos(-Math.PI/i)}(function(){function t(){for(var e in this.sections)this.sections[e].condition()&&this.sections[e].callback.call(this)}var e=function(){this.audioAdapter=e._getAdapter(this),this.events={},this.sections=[],this.bind("update",t)};e.version="0.3.2",e.adapters={},e.prototype={load:function(t){var n;return t instanceof HTMLElement?(this.source=t,e.isSupported()==="flash"&&(this.source={src:e._getMP3SrcFromAudio(t)})):(this.source=window.Audio?new Audio:{},this.source.src=e._makeSupportedPath(t.src,t.codecs)),this.audio=this.audioAdapter.load(this.source),this},play:function(){return this.audioAdapter.play(),this},pause:function(){return this.audioAdapter.pause(),this},setVolume:function(e){return this.audioAdapter.setVolume(e),this},createKick:function(t){return new e.Kick(this,t)},bind:function(e,t){return this.events[e]||(this.events[e]=[]),this.events[e].push(t),this},unbind:function(e){return this.events[e]&&delete this.events[e],this},trigger:function(e){var t=this;return this.events[e]&&this.events[e].forEach(function(e){e.call(t)}),this},getVolume:function(){return this.audioAdapter.getVolume()},getProgress:function(){return this.audioAdapter.getProgress()},getTime:function(){return this.audioAdapter.getTime()},getFrequency:function(e,t){var n=0;if(t!==undefined){for(var r=e;r<=t;r++)n+=this.getSpectrum()[r];return n/(t-e+1)}return this.getSpectrum()[e]},getWaveform:function(){return this.audioAdapter.getWaveform()},getSpectrum:function(){return this.audioAdapter.getSpectrum()},isLoaded:function(){return this.audioAdapter.isLoaded},isPlaying:function(){return this.audioAdapter.isPlaying},after:function(e,t){var n=this;return this.sections.push({condition:function(){return n.getTime()>e},callback:t}),this},before:function(e,t){var n=this;return this.sections.push({condition:function(){return n.getTime()<e},callback:t}),this},between:function(e,t,n){var r=this;return this.sections.push({condition:function(){return r.getTime()>e&&r.getTime()<t},callback:n}),this},onceAt:function(e,t){var n=this,r=null;return this.sections.push({condition:function(){return n.getTime()>e&&!this.called},callback:function(){t.call(this),r.called=!0},called:!1}),r=this.sections[this.sections.length-1],this}},window.Dancer=e})(),function(e){function r(){var e=!!(navigator.vendor||"").match(/Apple/),t=navigator.userAgent.match(/Version\/([^ ]*)/);return t=t?parseFloat(t[1]):0,e&&t<=6}var t={mp3:"audio/mpeg;",ogg:'audio/ogg; codecs="vorbis"',wav:'audio/wav; codecs="1"',aac:'audio/mp4; codecs="mp4a.40.2"'},n=document.createElement("audio");e.options={},e.setOptions=function(t){for(var n in t)t.hasOwnProperty(n)&&(e.options[n]=t[n])},e.isSupported=function(){return!window.Float32Array||!window.Uint32Array?null:!r()&&(window.AudioContext||window.webkitAudioContext)?"webaudio":n&&n.mozSetup?"audiodata":FlashDetect.versionAtLeast(9)?"flash":""},e.canPlay=function(r){var i=n.canPlayType;return e.isSupported()==="flash"?r.toLowerCase()==="mp3":!!n.canPlayType&&!!n.canPlayType(t[r.toLowerCase()]).replace(/no/,"")},e.addPlugin=function(t,n){e.prototype[t]===undefined&&(e.prototype[t]=n)},e._makeSupportedPath=function(t,n){if(!n)return t;for(var r=0;r<n.length;r++)if(e.canPlay(n[r]))return t+"."+n[r];return t},e._getAdapter=function(t){switch(e.isSupported()){case"webaudio":return new e.adapters.webkit(t);case"audiodata":return new e.adapters.moz(t);case"flash":return new e.adapters.flash(t);default:return null}},e._getMP3SrcFromAudio=function(e){var t=e.children;if(e.src)return e.src;for(var n=t.length;n--;)if((t[n].type||"").match(/audio\/mpeg/))return t[n].src;return null}}(window.Dancer),function(e){var t=function(t,n){n=n||{},this.dancer=t,this.frequency=n.frequency!==e?n.frequency:[0,10],this.threshold=n.threshold!==e?n.threshold:.3,this.decay=n.decay!==e?n.decay:.02,this.onKick=n.onKick,this.offKick=n.offKick,this.isOn=!1,this.currentThreshold=this.threshold;var r=this;this.dancer.bind("update",function(){r.onUpdate()})};t.prototype={on:function(){return this.isOn=!0,this},off:function(){return this.isOn=!1,this},set:function(t){t=t||{},this.frequency=t.frequency!==e?t.frequency:this.frequency,this.threshold=t.threshold!==e?t.threshold:this.threshold,this.decay=t.decay!==e?t.decay:this.decay,this.onKick=t.onKick||this.onKick,this.offKick=t.offKick||this.offKick},onUpdate:function(){if(!this.isOn)return;var e=this.maxAmplitude(this.frequency);e>=this.currentThreshold&&e>=this.threshold?(this.currentThreshold=e,this.onKick&&this.onKick.call(this.dancer,e)):(this.offKick&&this.offKick.call(this.dancer,e),this.currentThreshold-=this.decay)},maxAmplitude:function(e){var t=0,n=this.dancer.getSpectrum();if(!e.length)return e<n.length?n[~~e]:null;for(var r=e[0],i=e[1];r<=i;r++)n[r]>t&&(t=n[r]);return t}},window.Dancer.Kick=t}(),function(){function r(){this.source=this.context.createMediaElementSource(this.audio),this.source.connect(this.proc),this.source.connect(this.gain),this.gain.connect(this.context.destination),this.proc.connect(this.context.destination),this.isLoaded=!0,this.progress=1,this.dancer.trigger("loaded")}var e=2048,t=44100,n=function(e){this.dancer=e,this.audio=new Audio,this.context=window.AudioContext?new window.AudioContext:new window.webkitAudioContext};n.prototype={load:function(n){var i=this;return this.audio=n,this.isLoaded=!1,this.progress=0,this.proc=this.context.createJavaScriptNode(e/2,1,1),this.proc.onaudioprocess=function(e){i.update.call(i,e)},this.gain=this.context.createGainNode(),this.fft=new FFT(e/2,t),this.signal=new Float32Array(e/2),this.audio.readyState<3?this.audio.addEventListener("canplay",function(){r.call(i)}):r.call(i),this.audio.addEventListener("progress",function(e){e.currentTarget.duration&&e.currentTarget.seekable.length>0&&(i.progress=e.currentTarget.seekable.end(0)/e.currentTarget.duration)}),this.audio},play:function(){this.audio.play(),this.isPlaying=!0},pause:function(){this.audio.pause(),this.isPlaying=!1},setVolume:function(e){this.gain.gain.value=e},getVolume:function(){return this.gain.gain.value},getProgress:function(){return this.progress},getWaveform:function(){return this.signal},getSpectrum:function(){return this.fft.spectrum},getTime:function(){return this.audio.currentTime},update:function(t){if(!this.isPlaying||!this.isLoaded)return;var n=[],r=t.inputBuffer.numberOfChannels,i=e/r,s=function(e,t){return e[o]+t[o]},o;for(o=r;o--;)n.push(t.inputBuffer.getChannelData(o));for(o=0;o<i;o++)this.signal[o]=r>1?n.reduce(s)/r:n[0][o];this.fft.forward(this.signal),this.dancer.trigger("update")}},Dancer.adapters.webkit=n}(),function(){function t(){this.fbLength=this.audio.mozFrameBufferLength,this.channels=this.audio.mozChannels,this.rate=this.audio.mozSampleRate,this.fft=new FFT(this.fbLength/this.channels,this.rate),this.signal=new Float32Array(this.fbLength/this.channels),this.isLoaded=!0,this.progress=1,this.dancer.trigger("loaded")}var e=function(e){this.dancer=e,this.audio=new Audio};e.prototype={load:function(e){var n=this;return this.audio=e,this.isLoaded=!1,this.progress=0,this.audio.readyState<3?this.audio.addEventListener("loadedmetadata",function(){t.call(n)},!1):t.call(n),this.audio.addEventListener("MozAudioAvailable",function(e){n.update(e)},!1),this.audio.addEventListener("progress",function(e){e.currentTarget.duration&&(n.progress=e.currentTarget.seekable.end(0)/e.currentTarget.duration)},!1),this.audio},play:function(){this.audio.play(),this.isPlaying=!0},pause:function(){this.audio.pause(),this.isPlaying=!1},setVolume:function(e){this.audio.volume=e},getVolume:function(){return this.audio.volume},getProgress:function(){return this.progress},getWaveform:function(){return this.signal},getSpectrum:function(){return this.fft.spectrum},getTime:function(){return this.audio.currentTime},update:function(e){if(!this.isPlaying||!this.isLoaded)return;for(var t=0,n=this.fbLength/2;t<n;t++)this.signal[t]=(e.frameBuffer[2*t]+e.frameBuffer[2*t+1])/2;this.fft.forward(this.signal),this.dancer.trigger("update")}},Dancer.adapters.moz=e}(),function(){function o(){var e=this;r=!0,u(Dancer.options.flashJS,function(){soundManager=new SoundManager,soundManager.flashVersion=9,soundManager.flash9Options.useWaveformData=!0,soundManager.useWaveformData=!0,soundManager.useHighPerformance=!0,soundManager.useFastPolling=!0,soundManager.multiShot=!1,soundManager.debugMode=!1,soundManager.debugFlash=!1,soundManager.url=Dancer.options.flashSWF,soundManager.onready(function(){n=!0,e.load()}),soundManager.ontimeout(function(){console.error("Error loading SoundManager2.swf")}),soundManager.beginDelayedInit()})}function u(e,t){var n=document.createElement("script"),r=document.getElementsByTagName("script")[0];n.type="text/javascript",n.src=e,n.onload=t,r.parentNode.insertBefore(n,r)}var e=1024,t=44100,n=!1,r=!1,i=.93,s=function(e){this.dancer=e,this.wave_L=[],this.wave_R=[],this.spectrum=[],window.SM2_DEFER=!0};s.prototype={load:function(n){var i=this;return this.path=n?n.src:this.path,this.isLoaded=!1,this.progress=0,!window.soundManager&&!r&&o.call(this),window.soundManager&&(this.audio=soundManager.createSound({id:"dancer"+Math.random()+"",url:this.path,stream:!0,autoPlay:!1,autoLoad:!0,whileplaying:function(){i.update()},whileloading:function(){i.progress=this.bytesLoaded/this.bytesTotal},onload:function(){i.fft=new FFT(e,t),i.signal=new Float32Array(e),i.waveform=new Float32Array(e),i.isLoaded=!0,i.progress=1,i.dancer.trigger("loaded")}}),this.dancer.audio=this.audio),this.audio},play:function(){this.audio.play(),this.isPlaying=!0},pause:function(){this.audio.pause(),this.isPlaying=!1},setVolume:function(e){this.audio.setVolume(e*100)},getVolume:function(){return this.audio.volume/100},getProgress:function(){return this.progress},getWaveform:function(){return this.waveform},getSpectrum:function(){return this.fft.spectrum},getTime:function(){return this.audio.position/1e3},update:function(){if(!this.isPlaying&&!this.isLoaded)return;this.wave_L=this.audio.waveformData.left,this.wave_R=this.audio.waveformData.right;var e;for(var t=0,n=this.wave_L.length;t<n;t++)e=parseFloat(this.wave_L[t])+parseFloat(this.wave_R[t]),this.waveform[2*t]=e/2,this.waveform[t*2+1]=e/2,this.signal[2*t]=e*i,this.signal[t*2+1]=e*i;this.fft.forward(this.signal),this.dancer.trigger("update")}},Dancer.adapters.flash=s}(),FFT.prototype.forward=function(e){var t=this.bufferSize,n=this.cosTable,r=this.sinTable,i=this.reverseTable,s=this.real,o=this.imag,u=this.spectrum,a=Math.floor(Math.log(t)/Math.LN2);if(Math.pow(2,a)!==t)throw"Invalid buffer size, must be a power of 2.";if(t!==e.length)throw"Supplied buffer is not the same size as defined FFT. FFT Size: "+t+" Buffer Size: "+e.length;var f=1,l,c,h,p,d,v,m,g,y;for(y=0;y<t;y++)s[y]=e[i[y]],o[y]=0;while(f<t){l=n[f],c=r[f],h=1,p=0;for(var b=0;b<f;b++){y=b;while(y<t)d=y+f,v=h*s[d]-p*o[d],m=h*o[d]+p*s[d],s[d]=s[y]-v,o[d]=o[y]-m,s[y]+=v,o[y]+=m,y+=f<<1;g=h,h=g*l-p*c,p=g*c+p*l}f<<=1}return this.calculateSpectrum()};var FlashDetect=new function(){var e=this;e.installed=!1,e.raw="",e.major=-1,e.minor=-1,e.revision=-1,e.revisionStr="";var t=[{name:"ShockwaveFlash.ShockwaveFlash.7",version:function(e){return n(e)}},{name:"ShockwaveFlash.ShockwaveFlash.6",version:function(e){var t="6,0,21";try{e.AllowScriptAccess="always",t=n(e)}catch(r){}return t}},{name:"ShockwaveFlash.ShockwaveFlash",version:function(e){return n(e)}}],n=function(e){var t=-1;try{t=e.GetVariable("$version")}catch(n){}return t},r=function(e){var t=-1;try{t=new ActiveXObject(e)}catch(n){t={activeXError:!0}}return t},s=function(e){var t=e.split(",");return{raw:e,major:parseInt(t[0].split(" ")[1],10),minor:parseInt(t[1],10),revision:parseInt(t[2],10),revisionStr:t[2]}},o=function(e){var t=e.split(/ +/),n=t[2].split(/\./),r=t[3];return{raw:e,major:parseInt(n[0],10),minor:parseInt(n[1],10),revisionStr:r,revision:u(r)}},u=function(t){return parseInt(t.replace(/[a-zA-Z]/g,""),10)||e.revision};e.majorAtLeast=function(t){return e.major>=t},e.minorAtLeast=function(t){return e.minor>=t},e.revisionAtLeast=function(t){return e.revision>=t},e.versionAtLeast=function(t){var n=[e.major,e.minor,e.revision],r=Math.min(n.length,arguments.length);for(i=0;i<r;i++){if(n[i]>=arguments[i]){if(i+1<r&&n[i]==arguments[i])continue;return!0}return!1}},e.FlashDetect=function(){if(navigator.plugins&&navigator.plugins.length>0){var n="application/x-shockwave-flash",i=navigator.mimeTypes;if(i&&i[n]&&i[n].enabledPlugin&&i[n].enabledPlugin.description){var u=i[n].enabledPlugin.description,a=o(u);e.raw=a.raw,e.major=a.major,e.minor=a.minor,e.revisionStr=a.revisionStr,e.revision=a.revision,e.installed=!0}}else if(navigator.appVersion.indexOf("Mac")==-1&&window.execScript){var u=-1;for(var f=0;f<t.length&&u==-1;f++){var l=r(t[f].name);if(!l.activeXError){e.installed=!0,u=t[f].version(l);if(u!=-1){var a=s(u);e.raw=a.raw,e.major=a.major,e.minor=a.minor,e.revision=a.revision,e.revisionStr=a.revisionStr}}}}}()};FlashDetect.JS_RELEASE="1.0.4";