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

----------------
Wimpy EQ - 0.0.3
----------------
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/44100)}function n(e){e--;for(var t=2;e>>=1;)t<<=1;return t}function r(e,t,i,n){var r=s(e);if("number"==typeof type){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):e}var a=null,o=null,h=null,d=e.prototype=new wimpy.extension.IExtension;d.af=null,d.k=null,d.x=null,d.fftSize=null,d.D=null,d.U=null,d.I=null,d.b=null,d.v=null,d.target=null,d.playing=!1,d.F=null,d.ai=null,d.E=null,d.P=null,d.X=null,d.Y=null,d.Z=null,d.J=null,d.ae=null,d.O=null,d.G=null,d.W=null,d.Q=null,d.C=null,d.A=null,d.L=null,d.width=null,d.height=null,d.target=null,d.ad=null,e.canUse=function(){var e;try{e=jbeeb.audio.CTX||new(window.AudioContext||window.webkitAudioContext)}catch(e){console.warn("Warning 100. Web Audio API not supported.")}return!!e&&(jbeeb.audio.CTX||(jbeeb.audio.CTX=e),e)},d.aj=d.init,d.init=function(t){var i=e.canUse();if(i){this.af=i,this.aj(t),this.I=this.K.bind(this),this.b=this.j.bind(this),this.ag=this.ah.bind(this),this.q=!1,this.k=this.setRenderer(t.renderer,!0);var s=r(t.scale||1,1,10);this.x=!!(s&&s>1)&&s,this.U=r(t.smoothing,.95,.3,.99),this.S=r(t.minFrequency,80,10,100),this.T=r(t.maxFrequency,7500,1e3,2e4),this.ad=r(t.bassDamp,.3,0,1);var a=r(t.fftSize,2048,32,32768);this.fftSize=n(a),this.D=this.fftSize/2,this.addEventListener("onPlayerReady",this.onPlayerReady,this)}else console.warn("Warning 101. Web Audio API not supported.")},d.register=function(e){var t=e.constructor.defaultOpts;if(t){var i=this.A||{};for(var n in t)i&&void 0!==i[n]?e[n]=i[n]:e[n]=t[n]}e.controller=this;var r=this.B||this.createCanvas();e.canvas=r.canvas,e.canvasCtx=r.ctx,e.width=this.y||r.width,e.height=this.z||r.height,this.B||(this.B=r)},d.y=null,d.z=null,d.B=null,d.createCanvas=function(e,t){var i=document.createElement("canvas"),n=i.getContext("2d");i.width=this.width,i.height=this.height,this.L?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}},d.V=function(){this.F&&(this.F.removeEventListener("loadeddata",this.v),this.F=null,this.E.disconnect(this.P),this.E=null)},d.aa=function(e){(e!=this.F||jbeeb.Browser.moz)&&(this.V(),this.F=e,e.addEventListener("loadeddata",this.v,!1),this.N(e))},e.ab=null,d.N=function(t,i){if(t!=this.F)this.aa(t);else{var n=jbeeb.Browser.moz;n&&this.E&&(this.E.disconnect(this.P),this.E=null),n||!e.ab?(this.E=this.af.createMediaElementSource(t),this.E.connect(this.P),e.ab=this.E):this.E=e.ab}},d.H=function(){var t=this.af.createAnalyser();t.fftSize=this.fftSize,t.smoothingTimeConstant=this.U,a||(o=t.minDecibels,h=t.maxDecibels,a=h-o);var i=t.frequencyBinCount;this.X=new Float32Array(i),this.ai=t;var n=e.ac||this.af.createGain();this.P=n,this.P.connect(t),e.ac||(this.ai.connect(this.af.destination),e.ac=n)},e.ac=null,d.J=!1,d.w=function(){this.J||(this.J=!0,this.K())},d.K=function(){if(this.playing){var e=this.Y;this.ai.getFloatFrequencyData(this.X),this.R(e),this.C&&!this.q&&(this.C.loop(e),requestAnimationFrame(this.I))}else this.J=!1,this.j()},d.j=function(){for(var e=this.Y,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.C&&!this.q&&(this.C.loop(e),i&&!this.playing&&requestAnimationFrame(this.b))},d.M=function(e){var t=s(e.bands)||s(e.bars)||24;this.ae=t;var i=1/this.D*44100;this.O=i/2,this.G=22050-i,this.Q=new jbeeb.utils.LogRange({linMin:0,linMax:this.ae,minVal:this.S,maxVal:this.T}),this.W=new jbeeb.utils.LogRange({linMin:0,linMax:1,minVal:-o,maxVal:-h}),this.Y=new Float32Array(this.ae);for(var n=new Float32Array(this.ae),r=Math.floor(t),a=this.ad,d=t*(r/t),l=0;l<t;l++)n[l]=l<r?a*Math.sin(l*Math.PI/(2*d))+(1-a):1;this.Z=n},d.R=function(){for(var e=this.X,n=this.Y,r=i,s=this.Q,a=this.x,o=Math.pow,h=this.ae,d=this.O,l=this.G,al=this.D,u=0;u<h;u++){for(var c=0,m=r(s.fromLin(u),d,l,al),p=r(s.fromLin(u+1),d,l,al),f=m;f<=p;f++)c+=e[f];c/=p-m+1;var g=this.Z[u]*this.W.toLin(-c);a&&(g=o(g,a)*a),g=t(g,0,1),n[u]=g}},d.trackLaunched=function(e){var t=this.C;t&&t.launch&&t.launch(e)},d.trackPlay=function(e){if(!this.playing){this.playing=!0,this.N(this.F,!0),this.af.resume(),this.w();var t=this.C;t&&t.play&&t.play(e)}},d.trackBeforePause=function(e){if(this.playing){this.af.suspend(),this.playing=!1;var t=this.C;t&&t.pause&&t.pause()}},d.trackDone=function(e){this.playing=!1;var t=this.C;t&&(t.done&&t.done(e),t.reset&&t.reset(e))},d.onPlayerReady=function(e){this.H(),this.M(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.L=i,this.width=n,this.height=r,this.A=e.rendererOpts,this.k&&(this.C=new this.k(this),this.register(this.C)),this.v=this.w.bind(this),this.player.addEventListener("mediaElementChange",this.aa,this)},d.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++,r>1e3)break}else n=0}for(var h=[],d=0;d<i.length;d++)h[d]=i[d];return h},d.getRenderer=function(){return this.C},d.setRenderer=function(e,t){var i=e;if("string"==typeof e?(e=wimpy.extension.eq.renderers[e],e||(e=wimpy.extension.eq.renderers.Peaks)):e=params.renderer,!e)throw new Error("Renderer not defined: "+i.toString());if(t)return e;this.q=!0,this.k=e,setTimeout(this.ag,100)},d.ah=function(){this.C&&(this.C.detroy&&this.C.detroy(),this.C=null),this.C=new this.k(this),this.register(this.C),this.q=!1,this.J=!0,this.K()},d.resize=function(e,t){var i=this.C;if(i){var n=i.canvas;n&&(n.width=e,n.height=t),i.width=e,i.height=t,this.y=e,this.z=t}},d.ak=d.destroy,d.destroy=function(){this.playing=!1,this.player.removeEventListener("mediaElementChange",this.aa,this),this.removeEventListener("onPlayerReady",this.onPlayerReady,this),this.V(),this.C&&(this.C.destroy&&this.C.destroy(),this.C=null),this.ak(),this.target=null,this.scale=null,this.fftSize=null,this.I=null,this.v=null,this.af=null,this.ai=null,this.E=null,this.F=null,this.k=null,this.X=null,this.Y=null},wimpy.extension.eq.GraphicEq=e,wimpyGraphicEq=e}();
