text: !function(){"use strict";function e(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function t(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}function n(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var r=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),i=void 0,o=void 0,s={},u=function(){function e(){n(this,e)}return r(e,null,[{key:"getArrayBuffer",value:function(e){return new Promise(function(t,n){var r=new XMLHttpRequest;r.open("GET",e),r.responseType="arraybuffer",r.onload=function(){200===r.status||206===r.status?t(r.response):n(new Error(r.statusText))},r.onerror=function(){n(new Error("Unknown error."))},r.send()})}},{key:"loadUrl",value:function(t){var n=e.getUrlPromise(t);if(!n){var r=function(t,n){return t.catch(function(){return e.getArrayBuffer(n).then(function(t){return e.audio.OfflineContext.decodeAudioData(t)})})};n=t.reduce(r,Promise.reject()).then(function(n){return e.removeUrlPromise(t),n}).catch(function(){throw new Error("No valid audio URLs provided.")}),e.setUrlPromise(t,n)}return n}},{key:"getUrlPromise",value:function(e){for(var t in e)if(e.hasOwnProperty(t)&&s[e[t]])return s[e[t]]}},{key:"removeUrlPromise",value:function(e){e.forEach(function(e){delete s[e]})}},{key:"setUrlPromise",value:function(e,t){e.forEach(function(e){s[e]=t})}},{key:"readStorage",value:function(e){if("undefined"!=typeof localStorage)return JSON.parse(localStorage.getItem("WebAudioPlayer."+e))}},{key:"updateStorage",value:function(e,t){"undefined"!=typeof localStorage&&localStorage.setItem("WebAudioPlayer."+e,JSON.stringify(t))}},{key:"audio",get:function(){return i||(i=new l),i}},{key:"player",set:function(e){if(!(e instanceof h))throw new TypeError("Player parameter accepts the WebAudioPlayer instance only.");o=e},get:function(){return o}}]),e}(),a=function(){function e(){n(this,e),this.eventListeners={}}return r(e,[{key:"addEventListener",value:function(e,t){e in this.eventListeners||(this.eventListeners[e]=[]);for(var n=this.eventListeners[e],r=!1,i=0,o=n.length;i<o;i++)if(n[i]===t){r=!0;break}r||this.eventListeners[e].push(t)}},{key:"removeEventListener",value:function(e,t){if(e in this.eventListeners)for(var n=this.eventListeners[e],r=0,i=n.length;r<i;r++)if(n[r]===t){n.splice(r,1);break}}},{key:"dispatchEvent",value:function(e){if(e in this.eventListeners)for(var t=this.eventListeners[e],n=Array.prototype.slice.call(arguments,1),r=0,i=t.length;r<i;r++)t[r].apply(this,n)}}]),e}(),l=function e(){n(this,e),this.Context=new(window.AudioContext||window.webkitAudioContext),this.OfflineContext=new(window.OfflineAudioContext||window.webkitOfflineAudioContext)(1,2,this.Context.sampleRate),this.Analyser=this.Context.createAnalyser(),this.Gain=this.Context.createGain(),this.ScriptProcessor=this.Context.createScriptProcessor(),this.filters=[];for(var t=[60,170,310,600,1e3,3e3,6e3,12e3,14e3,16e3],r=0;r<10;r++)this.filters[r]=this.Context.createBiquadFilter(),this.filters[r].frequency.value=t[r],0===r?this.filters[0].type="lowshelf":9===r?this.filters[9].type="highshelf":this.filters[r].type="peaking",r>0&&this.filters[r-1].connect(this.filters[r]);this.Analyser.connect(this.filters[0]),this.filters[9].connect(this.Gain).connect(this.Context.destination),this.ScriptProcessor.connect(this.Context.destination)},c=function(r){function i(t){n(this,i);var r=e(this,(i.__proto__||Object.getPrototypeOf(i)).call(this));r.data={};var o=null,s=!1,a=[],l=[],c=0,f=0,h=0,d=null,v=0,p=null,y=function(){for(var e in l)if(l[e].m<=f){setTimeout(l[e].fn.bind(r),0),l.splice(e,1);break}},g=function(){s&&(f=r.getCurrentTime()-v,y(),r.dispatchEvent("playing"))};return r.load=function(){var e=this;return d||(d=u.loadUrl(t).then(function(t){return o=t,e})),d},r.play=function(){if(!o)throw new Error("Track is not loaded.");if(!s&&c<o.duration){var e=u.audio,t=u.player,n=this;s=!0,c=Math.max(c,0);var r=Math.max(o.duration-c,0);t.addEventListener("audioprocess",g),p=e.Context.createBufferSource(),p.connect(e.Analyser),p.buffer=o,p.onended=function(){this.finished=!0,p.finished&&s&&(s=!1,v=c=o.duration,n.dispatchEvent("finished"),t.removeEventListener("audioprocess",g),l=a.slice(0))},h=e.Context.currentTime,p.start(0,c,r),this.dispatchEvent("play")}return this},r.stop=function(){return s=!1,p&&p.stop(),v=c=0,u.player.removeEventListener("audioprocess",g),l=a.slice(0),this},r.pause=function(){var e=s;return s=!1,p&&p.stop(),e&&(c+=u.audio.Context.currentTime-h),u.player.removeEventListener("audioprocess",g),this},r.seek=function(e){if(e<0)throw new TypeError("Offset parameter accepts non-negative numbers only.");return v+=e-this.getCurrentTime(),s?(s=!1,p&&p.stop(),c=e,this.play()):c=e,this},r.when=function(e,t){if(e<0)throw new TypeError("Marker parameter accepts non-negative numbers only.");var n={m:e,fn:t};return l.push(n),a.push(n),this},r.getCurrentTime=function(){return s?u.audio.Context.currentTime-h+c:c},r.getPlayedTime=function(){return f},r.getDuration=function(){if(!o)throw new Error("Track is not loaded.");return o.duration},r.isPlaying=function(){return s},r}return t(i,r),i}(a),f=function(i){function o(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[];n(this,o);var r=e(this,(o.__proto__||Object.getPrototypeOf(o)).call(this));r.list=t,r.repeat=!1;var i=null;r.setCurrentByIndex=function(e){if(i&&i.isPlaying())throw new Error("Cannot change the current track while playing.");if(!this.list[e])throw new Error("Index not found.");return i=this.list[e],this},r.getCurrent=function(){return!i&&this.length&&(i=this.list[0]),i};var s=function(){i&&i!==this&&i.pause(),i=this},u=function(){var e=r.getCurrentIndex();e>=0&&r.get(e+1)&&r.load(e+1)};return r.addEventListener("trackReady",function(e){e.addEventListener("play",s),e.when(0,u),e.addEventListener("finished",function(){return r.next()})}),r}return t(o,i),r(o,[{key:"getCurrentIndex",value:function(){var e=this.getCurrent();return e?this.list.indexOf(e):-1}},{key:"get",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null,t=void 0;if(null===e)t=this.getCurrent();else if(!(t=this.list[e])&&this.repeat){var n=this.length;e>=n?t=this.list[e-n]:e<0&&(t=this.list[e+n])}return t}},{key:"load",value:function(){var e=this,t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null,n=this.get(t);return n?(n._playlistPromise||(n._playlistPromise=n.load().then(function(t){return e.dispatchEvent("trackReady",t),t})),n._playlistPromise.catch(function(r){if((t=e.list.indexOf(n))<0)throw new Error("Cannot define the next track.");return n===e.getCurrent()&&e.get(t+1)&&e.setCurrentByIndex(t+1),e.get(t+1)?e.load(t+1):new Promise(function(){})})):Promise.reject(new Error("Attempt to load nonexistent item."))}},{key:"play",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null,t=this.get(e);if(!t)throw new Error("Attempt to play nonexistent item.");return t.getCurrentTime()>=t.getDuration()&&t.stop(),t.play(),this}},{key:"pause",value:function(){var e=this.getCurrent();return e&&e.pause(),this}},{key:"previous",value:function(){var e=this.getCurrentIndex();if(e<0)throw new Error("Cannot define the previous track.");return this.get(e-1)&&this.load(e-1).then(function(e){return e.stop().play()}),this}},{key:"next",value:function(){var e=this.getCurrentIndex();if(e<0)throw new Error("Cannot define the next track.");return this.get(e+1)&&this.load(e+1).then(function(e){return e.stop().play()}),this}},{key:"isPlaying",value:function(){var e=this.getCurrent();return e&&e.isPlaying()}},{key:"shuffle",value:function(){for(var e=this.length,t=void 0,n=void 0;e;)n=Math.floor(Math.random()*e),--e,t=this.list[e],this.list[e]=this.list[n],this.list[n]=t;return this}},{key:"push",value:function(){var e;return(e=this.list).push.apply(e,arguments),this}},{key:"length",get:function(){return this.list.length}}]),o}(a),h=function(i){function o(){n(this,o);var t=e(this,(o.__proto__||Object.getPrototypeOf(o)).call(this));u.player=t,u.audio.ScriptProcessor.onaudioprocess=function(){t.dispatchEvent("audioprocess")};var r=u.readStorage("eq"),i=u.readStorage("vol");return r&&t.setEq(r),i&&t.setVolume(i),t}return t(o,i),r(o,[{key:"getAudio",value:function(){return u.audio}},{key:"createTrack",value:function(e){return new c(e)}},{key:"createPlaylist",value:function(){return new f(arguments.length>0&&void 0!==arguments[0]?arguments[0]:[])}},{key:"setVolume",value:function(e){return u.audio.Gain.gain.value=e,u.updateStorage("vol",e),this}},{key:"getVolume",value:function(){return u.audio.Gain.gain.value}},{key:"setEq",value:function(e){var t=u.audio;for(var n in e)e.hasOwnProperty(n)&&t.filters[n]&&(t.filters[n].gain.value=e[n]);return u.updateStorage("eq",this.getEq()),this}},{key:"getEq",value:function(){return u.audio.filters.map(function(e){return e.gain.value})}}]),o}(a);window.WebAudioPlayer=h}();