/*
                         
 _ _ _ _                  _____ _____ 
| | | |_|_____ ___ _ _   |   __|     |
| | | | |     | . | | |  |   __|  |  |
|_____|_|_|_|_|  _|_  |  |_____|__  _|
              |_| |___|           |__|

----------------
Wimpy EQ - 0.0.6
----------------
A real graphic EQ for wimpy player.

Copyright (c) Michael Gieson.
www.wimpyplayer.com

Examples, info and usage:
http://www.wimpyplayer.com/examples/graphic-eq/


*/
;this.wimpy=this.wimpy||{},this.wimpy.extension=this.wimpy.extension||{},this.wimpy.extension.eq=this.wimpy.extension.eq||{},this.jbeeb=this.jbeeb||{},this.jbeeb.audio=this.jbeeb.audio||{},function(){function e(e){this.init(e)}function t(e,t,i){return e<t?e=t:e>i&&(e=i),e}function i(e,t,i,n){return e<t?0:e>i?n-1:Math.round(n*e/a)}function n(e){e--;for(var t=2;e>>=1;)t<<=1;return t}function r(e,t,i,n){if(void 0===e)return t;var r=s(e);if(r&&"number"==typeof r){if(r<i)return i;if(r>n)return n}else r=t;return r}function s(e){var t=typeof e;return"number"==t?e:"string"==t?Number(e):null}var a=44100,o=null,h=null,d=null,l=e.prototype=new wimpy.extension.IExtension;l._audioCtx=null,l._usingRenderer=null,l._scale=null,l.fftSize=null,l._nyquist=null,l._fftSmoothing=null,l._loop_bound=null,l._windDown_bound=null,l._startRenderer_bound=null,l.target=null,l.playing=!1,l._mediaElem=null,l._analyzerNode=null,l._mediaSourceNode=null,l._gainNode=null,l._dataSourceFloat=null,l._dataBands=null,l._dampTable=null,l._loopStarted=null,l._bands=null,l._halfBandWidth=null,l._maxBandWidth=null,l._dbLog=null,l._frequencyRangeLog=null,l._renderer=null,l._rendererOpts=null,l._isJbeeb=null,l.width=null,l.height=null,l.target=null,l._bassDamp=null,e.canUse=function(){var e;try{e=jbeeb.audio.CTX||new(window.AudioContext||window.webkitAudioContext)}catch(e){console.warn("wimpy.GraphicEQ - Warning 100. Web Audio API not supported.")}return!!e&&(jbeeb.audio.CTX||(jbeeb.audio.CTX=e),e)},l._GraphicEq_init=l.init,l.init=function(t){var i=e.canUse();if(i){this._audioCtx=i,this._GraphicEq_init(t),this._loop_bound=this._loop.bind(this),this._windDown_bound=this._windDown.bind(this),this._applyRenderer_bound=this._applyRenderer.bind(this),this._suspendForRenderReplace=!1,this._usingRenderer=this.setRenderer(t.renderer,!0);var s=r(t.scale||1,1,1,10);this._scale=!!(s&&s>1)&&s,this._fftSmoothing=r(t.smoothing,.95,.3,.99),this._freqMin=r(t.minFrequency,80,10,100),this._freqMax=r(t.maxFrequency,7500,1e3,2e4),this._bassDamp=r(t.bassDamp,.3,0,1);var a=r(t.fftSize,2048,32,32768);this.fftSize=n(a),this._nyquist=this.fftSize/2,this.addEventListener("onPlayerReady",this.onPlayerReady,this)}else console.warn("wimpy.GraphicEQ - Warning 101. Web Audio API not supported.")},l.register=function(e){var t=e.constructor.defaultOpts;if(t){var i=this._rendererOpts||{};for(var n in t)e[n]=i&&void 0!==i[n]?i[n]:t[n]}e.controller=this;var r=this._rendererCanvasStash||this.createCanvas();e.canvas=r.canvas,e.canvasCtx=r.ctx,e.width=this._resizeW||r.width,e.height=this._resizeH||r.height,this._rendererCanvasStash||(this._rendererCanvasStash=r)},l._resizeW=null,l._resizeH=null,l._rendererCanvasStash=null,l.createCanvas=function(e,t){var i=document.createElement("canvas"),n=i.getContext("2d");i.width=this.width,i.height=this.height,this._isJbeeb?this.target.element.appendChild(i):this.target.appendChild(i);var r=i.style;return r.position="relative",r.left=0,r.top=0,{canvas:i,ctx:n,width:this.width,height:this.height}},l._destroyMediaElem=function(){this._mediaElem&&(this._mediaElem.removeEventListener("loadeddata",this._startRenderer_bound),this._mediaElem=null,this._mediaSourceNode.disconnect(this._gainNode),this._mediaSourceNode=null)},l._connectMediaElement=function(e){(e!=this._mediaElem||jbeeb.Browser.moz)&&(this._destroyMediaElem(),this._mediaElem=e,e.addEventListener("loadeddata",this._startRenderer_bound,!1),this._hookMediaSourceNode(e))},e._commonMediaSourceNode=null,l._hookMediaSourceNode=function(t,i){if(t!=this._mediaElem)this._connectMediaElement(t);else{var n=jbeeb.Browser.moz;n&&this._mediaSourceNode&&(this._mediaSourceNode.disconnect(this._gainNode),this._mediaSourceNode=null),n||!e._commonMediaSourceNode?(this._mediaSourceNode=this._audioCtx.createMediaElementSource(t),this._mediaSourceNode.connect(this._gainNode),e._commonMediaSourceNode=this._mediaSourceNode):this._mediaSourceNode=e._commonMediaSourceNode}},l._mapAudioNodes=function(){var t=this._audioCtx.createAnalyser();t.fftSize=this.fftSize,t.smoothingTimeConstant=this._fftSmoothing,o||(h=t.minDecibels,d=t.maxDecibels,o=d-h);var i=t.frequencyBinCount;this._dataSourceFloat=new Float32Array(i),this._analyzerNode=t;var n=e._commonGain||this._audioCtx.createGain();this._gainNode=n,this._gainNode.connect(t),e._commonGain||(this._analyzerNode.connect(this._audioCtx.destination),e._commonGain=n)},e._commonGain=null,l._loopStarted=!1,l._startRenderer=function(){this._loopStarted||(this._loopStarted=!0,this._loop())},l._loop=function(){if(this.playing){var e=this._dataBands;this._analyzerNode.getFloatFrequencyData(this._dataSourceFloat),this._freqencyWeighting(e),this._renderer&&!this._suspendForRenderReplace&&(this._renderer.loop(e),requestAnimationFrame(this._loop_bound))}else this._loopStarted=!1,this._windDown()},l._windDown=function(){for(var e=this._dataBands,t=e.length,i=!1,n=0;n<t;n++){var r=e[n];r<=0?r=0:(i=r,r-=.05),e[n]=r}this._renderer&&!this._suspendForRenderReplace&&(this._renderer.loop(e),i&&!this.playing&&requestAnimationFrame(this._windDown_bound))},l._initSpectrum=function(e){var t=s(e.bands)||s(e.bars)||24;this._bands=t;var i=1/this._nyquist*a;this._halfBandWidth=i/2,this._maxBandWidth=a/2-i,this._frequencyRangeLog=new jbeeb.utils.LogRange({linMin:0,linMax:this._bands,minVal:this._freqMin,maxVal:this._freqMax}),this._dbLog=new jbeeb.utils.LogRange({linMin:0,linMax:1,minVal:-h,maxVal:-d}),this._dataBands=new Float32Array(this._bands);for(var n=new Float32Array(this._bands),r=Math.floor(t),o=this._bassDamp,l=t*(r/t),_=0;_<t;_++)n[_]=_<r?o*Math.sin(_*Math.PI/(2*l))+(1-o):1;this._dampTable=n},l._freqencyWeighting=function(){for(var e=this._dataSourceFloat,n=this._dataBands,r=i,s=this._frequencyRangeLog,a=this._scale,o=Math.pow,h=this._bands,d=this._halfBandWidth,l=this._maxBandWidth,_=this._nyquist,u=0;u<h;u++){for(var c=0,m=r(s.fromLin(u),d,l,_),p=r(s.fromLin(u+1),d,l,_),f=m;f<=p;f++)c+=e[f];c/=p-m+1;var g=this._dampTable[u]*this._dbLog.toLin(-c);a&&(g=o(g,a)*a),g=t(g,0,1),n[u]=g}},l.trackLaunched=function(e){var t=this._renderer;t&&t.launch&&t.launch(e)},l.trackPlay=function(e){if(!this.playing){this.playing=!0,this._hookMediaSourceNode(this._mediaElem,!0),this._audioCtx.resume(),this._startRenderer();var t=this._renderer;t&&t.play&&t.play(e)}},l.trackBeforePause=function(e){if(this.playing){this._audioCtx.suspend(),this.playing=!1;var t=this._renderer;t&&t.pause&&t.pause()}},l.trackStop=function(e){this.playing=!1;var t=this._renderer;t&&(t.done&&t.done(e),t.reset&&t.reset(e))},l.onPlayerReady=function(e){this._mapAudioNodes(),this._initSpectrum(e);var t=e.target;if("string"==typeof t&&(t=this.player.getSkinElement(t)||document.getElementById(t)),!t)throw new Error("Target not defined. Ensure the DIV id attribute matches the 'target' parameter.");this.target=t;var i,n=e.width||null,r=e.height||null;t.parent&&(i=!0,n=t.width||null,r=t.height||null,t=t.element);var n=e.width||null,r=e.height||null;i||(n=t.clientWidth,r=t.clientHeight),this._isJbeeb=i,this.width=n,this.height=r,this._rendererOpts=e.rendererOpts,this._usingRenderer&&(this._renderer=new this._usingRenderer(this),this.register(this._renderer)),this._startRenderer_bound=this._startRenderer.bind(this),this.player.addEventListener("mediaElementChange",this._connectMediaElement,this)},l.tweenGradient=function(e,t){var i=e.slice(),n=0;if(i.length<2)throw new Error("Color array doesn't have enough colors");for(var r=0;t+1>i.length;){var s=i[n],a=i[n+1];if(a){var o=jbeeb.Utils.hexBetween(s,a);if(i.splice(n+1,0,o),n+=2,++r>1e3)break}else n=0}for(var h=[],d=0;d<i.length;d++)h[d]=i[d];return h},l.getRenderer=function(){return this._renderer},l.setRenderer=function(e,t){var i=e;if("string"==typeof e?(e=wimpy.extension.eq.renderers[e])||(e=wimpy.extension.eq.renderers.Peaks):e=params.renderer,!e)throw new Error("Renderer not defined: "+i.toString());if(t)return e;this._suspendForRenderReplace=!0,this._usingRenderer=e,setTimeout(this._applyRenderer_bound,100)},l._applyRenderer=function(){this._renderer&&(this._renderer.detroy&&this._renderer.detroy(),this._renderer=null),this._renderer=new this._usingRenderer(this),this.register(this._renderer),this._suspendForRenderReplace=!1,this._loopStarted=!0,this._loop()},l.resize=function(e,t){var i=this._renderer;if(i){var n=i.canvas;n&&(n.width=e,n.height=t),i.width=e,i.height=t,this._resizeW=e,this._resizeH=t}},l._GraphicEq_destroy=l.destroy,l.destroy=function(){this.playing=!1,this.player.removeEventListener("mediaElementChange",this._connectMediaElement,this),this.removeEventListener("onPlayerReady",this.onPlayerReady,this),this._destroyMediaElem(),this._renderer&&(this._renderer.destroy&&this._renderer.destroy(),this._renderer=null),this._GraphicEq_destroy(),this.target=null,this.scale=null,this.fftSize=null,this._loop_bound=null,this._startRenderer_bound=null,this._audioCtx=null,this._analyzerNode=null,this._mediaSourceNode=null,this._mediaElem=null,this._usingRenderer=null,this._dataSourceFloat=null,this._dataBands=null},wimpy.extension.eq.GraphicEq=e,wimpyGraphicEq=e}();
