-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathboombox.min.js
30 lines (30 loc) · 27.7 KB
/
boombox.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* Browser sound library which blended HTMLVideo and HTMLAudio and WebAudio
*
* The MIT License (MIT)
*
* Copyright (c) 2014-2015 CyberAgent, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*
* @license MIT
* @author Kei Funagayama <[email protected]>
*/
!function(a){"use strict";var b=function(){},c=!("function"!=typeof define||!define.amd),d="-",e="-",f=function(a){return{prefix:a.substring(0,a.indexOf(d)),suffix:a.substring(a.indexOf(d)+1),name:a}},g=3,h=function(){function b(a){this.prefix=a||e,this.prefix="["+this.prefix+"]"}var c=Array.prototype,d=c.slice;return b.prototype.trace=function(){1>=g&&a.console&&(a.console.trace?a.console.trace("[TRACE]",this.prefix,d.call(arguments).join(" ")):a.console.debug?a.console.debug("[TRACE]",this.prefix,d.call(arguments).join(" ")):a.console.log("[TRACE]",this.prefix,d.call(arguments).join(" ")))},b.prototype.debug=function(){2>=g&&a.console&&(a.console.debug?a.console.debug("[DEBUG]",this.prefix,d.call(arguments).join(" ")):a.console.log("[DEBUG]",this.prefix,d.call(arguments).join(" ")))},b.prototype.info=function(){3>=g&&a.console&&a.console.info("[INFO]",this.prefix,d.call(arguments).join(" "))},b.prototype.warn=function(){4>=g&&a.console&&a.console.warn("[WARN]",this.prefix,d.call(arguments).join(" "))},b.prototype.error=function(){5>=g&&a.console&&a.console.error("[ERROR]",this.prefix,d.call(arguments).join(" "))},b}(),i=function(){function b(){this.VERSION="1.0.9",this.LOOP_NOT=0,this.LOOP_ORIGINAL=1,this.LOOP_NATIVE=2,this.POWER_OFF=!1,this.POWER_ON=!0,this.ERROR_MEDIA_TYPE=0,this.ERROR_HIT_FILTER=1,this.THRESHOLD=.2,this.setuped=!1,this.AudioContext=a.AudioContext||a.webkitAudioContext,this.support={mimes:[],webaudio:{use:!!this.AudioContext},htmlaudio:{use:!1},htmlvideo:{use:!1}},this.support.webaudio.use&&(this.WEB_AUDIO_CONTEXT=new this.AudioContext,this.WEB_AUDIO_CONTEXT.createGain||(this.WEB_AUDIO_CONTEXT.createGain=this.WEB_AUDIO_CONTEXT.createGainNode));try{(new a.Audio).canPlayType?this.support.htmlaudio.use=!0:this.support.htmlaudio.use=!1}catch(b){this.support.htmlaudio.use=!1}try{document.createElement("video").canPlayType?this.support.htmlvideo.use=!0:this.support.htmlvideo.use=!1}catch(b){this.support.htmlvideo.use=!1}this.pool={},this.waits=[],this.visibility={hidden:void 0,visibilityChange:void 0},this.state={power:this.POWER_ON},this.filter={}}return b.prototype.isWebAudio=function(){return this.support.webaudio.use},b.prototype.isHTMLAudio=function(){return this.support.htmlaudio.use},b.prototype.isHTMLVideo=function(){return this.support.htmlvideo.use},b.prototype.isPlayback=function(){var a=!1;for(var b in this.pool)if(this.pool[b].isPlayback()){a=!0;break}return a},b.prototype.setup=function(a){return a=a||{},"undefined"!=typeof a.threshold&&(this.THRESHOLD=a.threshold),"undefined"!=typeof a.loglevel&&(g=a.loglevel),this.logger=new h("Boombox "),this.setuped?(this.logger.warn('"setup" already, are running.'),this):(a.webaudio&&"undefined"!=typeof a.webaudio.use&&(this.support.webaudio.use=a.webaudio.use,this.logger.info("options.webaudio.use:",this.support.webaudio.use)),a.htmlaudio&&"undefined"!=typeof a.htmlaudio.use&&(this.support.htmlaudio.use=a.htmlaudio.use,this.logger.info("options.htmlaudio.use: ",this.support.htmlaudio.use)),a.htmlvideo&&"undefined"!=typeof a.htmlvideo.use&&(this.support.htmlvideo.use=a.htmlvideo.use,this.logger.info("options htmlvideo",this.support.htmlvideo)),this._browserControl(),this.logger.debug(this.support.webaudio.use?"WebAudio use support.":"WebAudio use not support."),this.logger.debug(this.support.htmlaudio.use?"HTMLAudio use support.":"HTMLAudio use not support."),this.logger.debug(this.support.htmlvideo.use?"HTMLVideo use support.":"HTMLVideo use not support."),this.setuped=!0,this)},b.prototype.get=function(a){return this.pool[a]},b.prototype.load=function(a,b,c,d){if("function"==typeof arguments[2]&&(d=c,c=null),!this.setuped)return d&&d(new Error("setup incomplete boombox. run: boombox.setup(options)")),this;if(this.pool[a])return this.logger.trace("audio pool cache hit!!",a),d&&d(null,this.pool[a]);var e=this.useMediaType(b.src);if(e&&(b.media=e.media,b.src=e.path),c&&this.isHTMLVideo()){var f=new j.HTMLVideo(a);return e||(f.state.error=this.ERROR_MEDIA_TYPE),"undefined"!=typeof f.state.error||this.runfilter(f,b)?d&&d(new Error(f.state.error),f):(f.load(b,d),this.setPool(a,f,j.HTMLVideo),this)}if(this.isWebAudio()){this.logger.debug("use web audio");var g=new j.WebAudio(a);if(e||(g.state.error=this.ERROR_MEDIA_TYPE),"undefined"!=typeof g.state.error||this.runfilter(g,b))return d&&d(new Error(g.state.error),g);g.load(b,d),this.setPool(a,g,j.WebAudio)}else if(this.isHTMLAudio()){this.logger.debug("use html audio");var h=new j.HTMLAudio(a);if(e||(h.state.error=this.ERROR_MEDIA_TYPE),"undefined"!=typeof h.state.error||this.runfilter(h,b))return d&&d(new Error(h.state.error),h);h.load(b,d),this.setPool(a,h,j.HTMLAudio)}else d&&d(new Error("This environment does not support HTMLAudio, both WebAudio both."),this);return this},b.prototype.remove=function(a){return this.pool[a]&&(this.logger.trace("Remove Audio that is pooled. name",a),this.pool[a].dispose&&this.pool[a].dispose(),this.pool[a]=void 0,delete this.pool[a]),this},b.prototype.setPool=function(a,b,c){if(b.isParentSprite()){for(var e in this.pool)~e.indexOf(a+d)&&delete this.pool[e];for(var f in b.sprite.options){var g=a+d+f,h=new c(g,b);this.pool[h.name]=h}}return this.remove(a),this.pool[a]=b,this},b.prototype.runfilter=function(a,b){for(var c,d=b.filter||[],e=0;e<d.length;e++){var f=d[e],g=this.filter[f];if(!g)return void this.logger.warn("filter not found. name:",f);if(this.logger.debug("filter run. name:",f),g(f,a,b)){c=f;break}}return c?(a.state.error=this.ERROR_HIT_FILTER,this.logger.warn("Hit the filter of",c),!0):!1},b.prototype.useMediaType=function(b){for(var c=0;c<b.length;c++){var d=b[c];if((new a.Audio).canPlayType(d.media))return d;this.logger.warn("skip audio type.",d.media)}return void 0},b.prototype.pause=function(){var a=this;this.logger.trace("pause");for(var b in this.pool){var c=this.pool[b];c.pause(),a.waits.push(b)}return this},b.prototype.resume=function(){this.logger.trace("resume");var a=this.waits.shift();return a&&this.pool[a]&&this.pool[a].resume(),0<this.waits.length&&this.resume(),this},b.prototype.power=function(a){this.logger.trace("power:",this.name,"flag:",a);for(var b in this.pool){var c=this.pool[b];c.power(a)}return this.state.power=a,this},b.prototype.volume=function(a){this.logger.trace("volume:",this.name,"volume:",a);for(var b in this.pool){var c=this.pool[b];c.volume(a)}return this},b.prototype.onVisibilityChange=function(a){this.logger.trace("onVisibilityChange"),document[this.visibility.hidden]?this.pause():this.resume()},b.prototype.onFocus=function(a){this.logger.trace("onFocus"),this.resume()},b.prototype.onBlur=function(a){this.logger.trace("onBlur"),this.pause()},b.prototype.onPageShow=function(a){this.logger.trace("onPageShow"),this.resume()},b.prototype.onPageHide=function(a){this.logger.trace("onPageHide"),this.pause()},b.prototype._browserControl=function(){var a=this;return"undefined"!=typeof document.hidden?(this.visibility.hidden="hidden",this.visibility.visibilityChange="visibilitychange"):"undefined"!=typeof document.webkitHidden?(this.visibility.hidden="webkitHidden",this.visibility.visibilityChange="webkitvisibilitychange"):"undefined"!=typeof document.mozHidden?(this.visibility.hidden="mozHidden",this.visibility.visibilityChange="mozvisibilitychange"):"undefined"!=typeof document.msHidden&&(this.visibility.hidden="msHidden",this.visibility.visibilityChange="msvisibilitychange"),this.visibility.hidden&&document.addEventListener(this.visibility.visibilityChange,function(b){a.onVisibilityChange(b)},!1),"undefined"!=typeof window.addEventListener?(window.addEventListener("focus",function(b){a.onFocus(b)},!1),window.addEventListener("blur",function(b){a.onBlur(b)},!1)):(window.attachEvent("onfocusin",function(b){a.onFocus(b)},!1),window.attachEvent("onfocusout",function(b){a.onBlur(b)},!1)),window.onpageshow=function(b){a.onPageShow(b)},window.onpagehide=function(b){a.onPageHide(b)},this},b.prototype.addFilter=function(a,b){return this.filter[a]=b,this},b.prototype.dispose=function(){for(var a in this.pool){var b=this.pool[a];b.dispose&&b.dispose()}delete this.VERSION,delete this.setuped,delete this.support.mimes,delete this.support.webaudio.use,this.WEB_AUDIO_CONTEXT&&delete this.WEB_AUDIO_CONTEXT,delete this.support.webaudio,delete this.support.htmlaudio.use,delete this.support.htmlaudio,delete this.support.htmlvideo.use,delete this.support.htmlvideo,delete this.support,delete this.pool,delete this.waits,delete this.visibility,delete this.state.power,delete this.state,delete this.filter},b}(),j=new i,k=function(){function c(b,c){if(this.logger=new h("HTMLAudio"),this.name=b,this._timer={},this.sprite=void 0,c){var d=f(b),e=c.sprite.options[d.suffix];this.parent=c,this.state=this.parent.state,this.$el=this.parent.$el,this.sprite=new n(void 0,e)}else this.state={time:{playback:void 0,pause:void 0},loop:j.LOOP_NOT,power:j.POWER_ON,loaded:!1,error:void 0},this.$el=new a.Audio}return c.prototype.load=function(a,c){var d=c||b;if(this.parent)return d(null,this),this;a=a||{preload:"auto",autoplay:!1,loop:!1,muted:!1,controls:!1};var e=a.timeout||15e3;delete a.timeout,a.spritemap&&(this.sprite=new n(a.spritemap),delete a.spritemap);for(var f in a){var g=a[f];this.logger.trace("HTMLAudioElement attribute:",f,g),this.$el[f]=g}var h=this,i="canplay",j=window.navigator.userAgent.match(/(iPhone\sOS)\s([\d_]+)/);return j&&0<j.length&&(i="suspend"),this.logger.trace("hook event name:",i),this.$el.addEventListener(i,function k(a){return h.logger.trace("processEvent "+a.target.id+" : "+a.type,"event"),h.state.loaded=!0,h.$el.removeEventListener(i,k,!1),d(null,h)}),this.$el.addEventListener("ended",function(a){h._onEnded(a)},!1),setTimeout(function(){h.$el&&4!==h.$el.readyState&&(h.$el.src="",d(new Error("load of html audio file has timed out. timeout:"+e),h),d=function(){})},e),this.$el.load(),this},c.prototype.isUse=function(){return this.state.power===j.POWER_OFF||j.state.power===j.POWER_OFF?!1:this.state.loaded&&"undefined"==typeof this.state.error?!0:!1},c.prototype.isPlayback=function(){return!!this.state.time.playback},c.prototype.isStop=function(){return!this.state.time.playback},c.prototype.isPause=function(){return!!this.state.time.pause},c.prototype.isLoop=function(){return 0<this.state.loop},c.prototype.isParentSprite=function(){return!(this.parent||!this.sprite||this.sprite.current)},c.prototype.isSprite=function(){return!!(this.parent&&this.sprite&&this.sprite.current)},c.prototype.clearTimerAll=function(){for(var a in this._timer){{this._timer[a]}this.clearTimer(a)}return this},c.prototype.clearTimer=function(a){var b=this._timer[a];return b&&(this.logger.debug("remove setTimetout:",b),clearTimeout(b),delete this._timer[a]),b},c.prototype.setTimer=function(a,b){return this._timer[a]&&this.logger.warn("Access that is not expected:",a,b),this._timer[a]=b,this._timer[a]},c.prototype.play=function(a){if(!this.isUse())return this.logger.debug("skip play:",this.name,"state can not be used"),this;if(this.isPlayback())return this.logger.debug("skip play:",this.name,"is playing"),this;var c=this,d="play",e=b;if(this.state.time.playback=Date.now(),a&&this.state.time.pause){if(this.setCurrentTime(this.state.time.pause),this.isSprite()){var f=this.state.time.pause;e=function(){var a=Math.ceil(1e3*(c.sprite.current.end-f));c.setTimer("play",setTimeout(function(){c.stop(),c._onEnded()},a))}}this.state.time.pause=void 0,d="resume:"}else if(this.setCurrentTime(0),this.isSprite()){var g=this.sprite.current.start;this.setCurrentTime(g),e=function(){var a=Math.ceil(1e3*c.sprite.current.term);c.setTimer("play",setTimeout(function(){c.stop(),c._onEnded()},a))}}return this.logger.debug(d,this.name),e(),this.state.time.name=this.name,this.$el.play(),this},c.prototype.stop=function(){return this.state.loaded&&"undefined"==typeof this.state.error?this.state.time.name&&this.state.time.name!==this.name?(this.logger.debug("skip stop: It is used in other sources",this.name,this.state.time.name),this):(this.logger.debug("stop:",this.name),this.clearTimer("play"),this.$el.pause(),this.setCurrentTime(0),this.state.time.playback=void 0,this.state.time.name=void 0,this):(this.logger.debug("skip stop:",this.name,"state can not be used"),this)},c.prototype.pause=function(){return this.isUse()?this.state.time.name&&this.state.time.name!==this.name?(this.logger.debug("skip pause: It is used in other sources",this.name,this.state.time.name),this):(this.logger.debug("pause:",this.name),this.clearTimer("play"),this.$el.pause(),this.state.time.pause=this.$el.currentTime,this.state.time.playback=void 0,this):(this.logger.debug("skip pause:",this.name,"state can not be used"),this)},c.prototype.resume=function(){return this.isUse()?this.state.time.name&&this.state.time.name!==this.name?(this.logger.debug("skip resume: It is used in other sources",this.name,this.state.time.name),this):(this.state.time.pause&&this.play(!0),this):(this.logger.debug("skip resume:",this.name,"state can not be used"),this)},c.prototype.replay=function(){return this.isUse()?(this.logger.debug("replay:",this.name),this.clearTimer("play"),this.pause(),this.setCurrentTime(0),this.play(),this):(this.logger.debug("skip replay:",this.name,"state can not be used"),this)},c.prototype.volume=function(a){this.logger.trace("volume:",this.name,"volume:",a),this.$el.volume=Math.max(0,Math.min(1,a))},c.prototype._onEnded=function(a){this.isDisposed()||(this.logger.trace("onended fire! name:",this.name),this.state.time.playback=void 0,this.state.time.name=void 0,this.onEnded(a),this.isDisposed()||this.state.loop===j.LOOP_ORIGINAL&&"undefined"==typeof this.state.time.pause&&(this.logger.trace("onended original loop play.",this.name),this.play()))},c.prototype.onEnded=b,c.prototype.setLoop=function(a){if(!this.isUse())return this;if(this.state.loop=a,a===j.LOOP_NOT)this.$el.loop=j.LOOP_NOT;else if(a===j.LOOP_NATIVE){if(this.isSprite())return this.logger.warn("audiosprite does not support the native. please use the original loop."),this;this.$el&&(this.$el.loop=a)}return this},c.prototype.power=function(a){return this.logger.trace("power:",this.name,"flag:",a),a===j.POWER_OFF&&this.stop(),this.state.power=a,this},c.prototype.setCurrentTime=function(a){try{this.$el.currentTime=a}catch(b){this.logger.error("Set currentTime.",b.message)}return this},c.prototype.isDisposed=function(){return m.prototype.isDisposed.apply(this,arguments)},c.prototype.dispose=function(){delete this.name,delete this.state.time.playback,delete this.state.time.pause,delete this.state.time.name,delete this.state.time,delete this.state.loop,delete this.state.power,delete this.state.loaded,delete this.state.error,delete this.state,this.$el.src=void 0,delete this.$el,this.clearTimerAll(),delete this._timer,delete this.parent,this.sprite&&this.sprite.dispose&&this.sprite.dispose(),delete this.sprite},c}(),l=function(){function a(a,b){if(this.logger=new h("HTMLVideo"),this.name=a,this._timer={},this.sprite=void 0,b){var c=f(a),d=b.sprite.options[c.suffix];this.parent=b,this.state=this.parent.state,this.$el=this.parent.$el,this.sprite=new n(void 0,d)}else this.state={time:{playback:void 0,pause:void 0},loop:j.LOOP_NOT,power:j.POWER_ON,loaded:!1,error:void 0},this.$el=document.createElement("video")}return a.prototype.load=function(a,c){var d=this,e=c||b;if(this.parent)return e(null,this),this;a=a||{preload:"auto",autoplay:!1,loop:!1,muted:!1,controls:!1};var f=a.timeout||15e3;delete a.timeout,a.spritemap&&(this.sprite=new n(a.spritemap),delete a.spritemap);for(var g in a){var h=a[g];d.logger.trace("HTMLVideoElement attribute:",g,h),d.$el[g]=h}var i="canplay",j=window.navigator.userAgent.match(/(iPhone\sOS)\s([\d_]+)/);return j&&0<j.length?i="suspend":window.navigator.userAgent.match(/(Android)\s+(4)([\d.]+)/)?i="loadeddata":window.navigator.userAgent.match(/(Android)\s+(2)([\d.]+)/)&&(i="stalled"),d.logger.trace("hook event name:",i),this.$el.addEventListener(i,function k(a){return d.logger.trace("processEvent "+a.target.id+" : "+a.type,"event"),d.state.loaded=!0,d.$el.removeEventListener(i,k,!1),e(null,d)}),this.$el.addEventListener("ended",function(a){d._onEnded(a)},!1),setTimeout(function(){d.$el&&4!==d.$el.readyState&&(d.$el.src="",e(new Error("load of html video file has timed out. timeout:"+f),d),e=function(){})},f),this.$el.load(),this},a.prototype.isUse=function(){return j.HTMLAudio.prototype.isUse.apply(this,arguments)},a.prototype.isPlayback=function(){return j.HTMLAudio.prototype.isPlayback.apply(this,arguments)},a.prototype.isStop=function(){return j.HTMLAudio.prototype.isStop.apply(this,arguments)},a.prototype.isPause=function(){return j.HTMLAudio.prototype.isPause.apply(this,arguments)},a.prototype.isLoop=function(){return j.HTMLAudio.prototype.isLoop.apply(this,arguments)},a.prototype.isParentSprite=function(){return j.HTMLAudio.prototype.isParentSprite.apply(this,arguments)},a.prototype.isSprite=function(){return j.HTMLAudio.prototype.isSprite.apply(this,arguments)},a.prototype.clearTimerAll=function(){return j.HTMLAudio.prototype.clearTimerAll.apply(this,arguments)},a.prototype.clearTimer=function(a){return j.HTMLAudio.prototype.clearTimer.apply(this,arguments)},a.prototype.setTimer=function(a,b){return j.HTMLAudio.prototype.setTimer.apply(this,arguments)},a.prototype.play=function(a){if(!this.isUse())return this.logger.debug("skip play:",this.name,"state can not be used"),this;if(this.isPlayback())return this.logger.debug("skip play:",this.name,"is playing"),this;var c=this,d="play",e=b;if(this.state.time.playback=Date.now(),a&&this.state.time.pause){if(this.setCurrentTime(this.state.time.pause),this.isSprite()){var f=this.state.time.pause;e=function(){var a=Math.ceil(1e3*(c.sprite.current.end-f));c.setTimer("play",setTimeout(function(){c.stop(),c._onEnded()},a))}}this.state.time.pause=void 0,d="resume:"}else if(this.setCurrentTime(0),this.isSprite()){var g=this.sprite.current.start;this.setCurrentTime(g),e=function(){var a=Math.ceil(1e3*c.sprite.current.term);c.setTimer("play",setTimeout(function(){c.stop(),c._onEnded()},a))}}return this.logger.debug(d,this.name),e(),this.state.time.name=this.name,this.$el.play(),this},a.prototype.stop=function(){return j.HTMLAudio.prototype.stop.apply(this,arguments)},a.prototype.pause=function(){return j.HTMLAudio.prototype.pause.apply(this,arguments)},a.prototype.resume=function(){return j.HTMLAudio.prototype.resume.apply(this,arguments)},a.prototype.replay=function(){return j.HTMLAudio.prototype.replay.apply(this,arguments)},a.prototype.volume=function(a){return j.HTMLAudio.prototype.volume.apply(this,arguments)},a.prototype._onEnded=function(a){return j.HTMLAudio.prototype._onEnded.apply(this,arguments)},a.prototype.onEnded=b,a.prototype.setLoop=function(a){return j.HTMLAudio.prototype.setLoop.apply(this,arguments)},a.prototype.power=function(a){return j.HTMLAudio.prototype.power.apply(this,arguments)},a.prototype.setCurrentTime=function(a){return j.HTMLAudio.prototype.setCurrentTime.apply(this,arguments)},a.prototype.isDisposed=function(){return j.HTMLAudio.prototype.isDisposed.apply(this,arguments)},a.prototype.dispose=function(){return j.HTMLAudio.prototype.dispose.apply(this,arguments)},a}(),m=function(){function a(a,b){if(this.logger=new h("WebAudio"),this.name=a,this._timer={},this.sprite=void 0,this.buffer=void 0,this.source=void 0,this.ctx=j.WEB_AUDIO_CONTEXT,this.gainNode=this.ctx.createGain(),this.state={time:{playback:void 0,pause:void 0,progress:0},loop:j.LOOP_NOT,power:j.POWER_ON,loaded:!1,error:void 0},b){var c=f(a);this.parent=b,this.state.loaded=this.parent.state.loaded,this.state.error=this.parent.state.error;var d=b.sprite.options[c.suffix];this.sprite=new n(void 0,d)}}return a.prototype.load=function(a,c){var e=this;a=a||{};var f=c||b;if(this.parent)return f(null,this),this;a.spritemap&&(this.sprite=new n(a.spritemap),delete a.spritemap);for(var g in a){var h=a[g];this.logger.trace("WebAudio attribute:",g,h)}var i=new XMLHttpRequest;i.onload=function(b){return"2"!==b.target.status.toString().charAt(0)?(e.logger.error("fail to load resource: ",a.url),f(new Error("fail to load resource"),e)):void e.ctx.decodeAudioData(i.response,function(b){if(!b)return e.logger.error("error decode file data: ",a.url),f(new Error("error decode file data"),e);if(e.buffer=b,e.state.loaded=!0,e.isParentSprite())for(var c in j.pool)~c.indexOf(e.name+d)&&(j.pool[c].buffer=b,j.pool[c].state.loaded=e.state.loaded);return f(null,e)},function(){return f(new Error("fail to decode file data"),e)})};var k=a.timeout||15e3;return setTimeout(function(){4!==i.readyState&&(i.abort(),f(new Error("load of web audio file has timed out. timeout:"+k),e),f=b)},k),i.open("GET",a.src,!0),i.responseType="arraybuffer",i.send(),this},a.prototype.isUse=function(){return j.HTMLAudio.prototype.isUse.apply(this,arguments)},a.prototype.isPlayback=function(){return!(!this.source||!this.state.time.playback||this.state.time.pause||1!==this.source.playbackState&&2!==this.source.playbackState)},a.prototype.isStop=function(){return!this.source},a.prototype.isPause=function(){return j.HTMLAudio.prototype.isPause.apply(this,arguments)},a.prototype.isLoop=function(){return j.HTMLAudio.prototype.isLoop.apply(this,arguments)},a.prototype.isParentSprite=function(){return j.HTMLAudio.prototype.isParentSprite.apply(this,arguments)},a.prototype.isSprite=function(){return j.HTMLAudio.prototype.isSprite.apply(this,arguments)},a.prototype.clearTimerAll=function(){return j.HTMLAudio.prototype.clearTimerAll.apply(this,arguments)},a.prototype.clearTimer=function(a){return j.HTMLAudio.prototype.clearTimer.apply(this,arguments)},a.prototype.setTimer=function(a,b){return j.HTMLAudio.prototype.setTimer.apply(this,arguments)},a.prototype.play=function(a){var c=this;if(!this.isUse())return this.logger.debug("skip play:",this.name,"state can not be used"),this;if(this.isPlayback())return this.logger.debug("skip play:",this.name,"is playing"),this;a||this.sourceDispose(),this.source=this.ctx.createBufferSource(),this.state.loop===j.LOOP_NATIVE&&(this.source.loop=this.state.loop),this.source.buffer=this.buffer,this.source.connect(this.gainNode),this.gainNode.connect(this.ctx.destination);var d="play",e=b,f=0;if(this.state.time.playback=Date.now(),a&&this.state.time.pause){if(f=this.state.time.pause/1e3,this.isSprite()){var g=this.state.time.pause/1e3;f=this.sprite.current.start+g;var h=Math.ceil(1e3*(c.sprite.current.term-g));e=function(){c.setTimer("play",setTimeout(function(){c.stop(),c._onEnded()},h))}}this.state.time.pause=void 0}else this.isSprite()&&(f=this.sprite.current.start,e=function(){var a=Math.ceil(1e3*c.sprite.current.term);c.setTimer("play",setTimeout(function(){c.stop(),c._onEnded()},a))});this.logger.debug(d,this.name,"offset:",f),e();var i=this.buffer.duration-f;if(!this.isSprite())if(this.source.hasOwnProperty("onended"))this.source.onended=function(a){c._onEnded(a)};else{var h=Math.ceil(1e3*i);this.setTimer("play",setTimeout(function(){c.stop(),c._onEnded()},h))}return this.source.start?(this.logger.debug("use source.start()",this.name,f,i),this.source.start(0,f,this.buffer.duration)):(this.isSprite()&&(i=c.sprite.current.term),this.logger.debug("use source.noteGrainOn()",this.name,f,i),this.source.noteGrainOn(0,f,i)),this},a.prototype.stop=function(){return this.state.loaded&&"undefined"==typeof this.state.error?(this.logger.debug("stop:",this.name),this.clearTimer("play"),this.source&&(this.source.stop?(this.logger.debug("stop: use source.stop()",this.name),this.source.stop(0)):(this.logger.debug("stop: use source.noteOff()",this.name),this.source.noteOff(0))),this.sourceDispose(),this):(this.logger.debug("skip stop:",this.name,"state can not be used"),this)},a.prototype.pause=function(){if(!this.isUse())return this.logger.debug("skip pause:",this.name,"state can not be used"),this;if(!this.source)return this.logger.debug("skip pause, Not playing."),this;if(this.state.time.pause)return this.logger.debug("skip pause, It is already paused."),this;var a=Date.now(),b=a-this.state.time.playback;return this.state.time.pause=this.state.time.progress+b,this.state.time.progress+=b,this.logger.trace("state.time.pause:",this.state.time.pause,"now:",a,"state.time.playback",this.state.time.playback),this.logger.debug("pause:",this.name),this.clearTimer("play"),this.source&&(this.source.stop?(this.logger.debug("pause: use source.stop()",this.name),this.source.stop(0)):(this.logger.debug("pause: use source.noteOff()",this.name),this.source.noteOff(0))),this},a.prototype.resume=function(){return this.isUse()?(this.state.time.pause&&this.play(!0),this):(this.logger.debug("skip resume:",this.name,"state can not be used"),this)},a.prototype.replay=function(){return this.isUse()?(this.logger.debug("replay:",this.name),this.clearTimer("play"),this.sourceDispose(),this.play(),this):(this.logger.debug("skip replay:",this.name,"state can not be used"),this)},a.prototype.volume=function(a){this.logger.trace("volume:",this.name,"volume:",a),this.gainNode.gain.value=a},a.prototype._onEnded=function(a){if(!this.isDisposed()){var b=this,c=Date.now();if(b.source&&Math.abs((c-b.state.time.playback+b.state.time.progress)/1e3-b.source.buffer.duration)>=j.THRESHOLD)return void b.logger.debug("skip if sounds is not ended",b.name);this.logger.trace("onended fire!",this.name),this.state.time.playback=void 0,this.onEnded(a),this.isDisposed()||(this.state.loop&&"undefined"==typeof this.state.time.pause?(this.logger.trace("onended loop play."),this.play()):this.sourceDispose())}},a.prototype.onEnded=b,a.prototype.setLoop=function(a){return this.isUse()?(this.state.loop=a,a===j.LOOP_NOT?this.source&&(this.source.loop=j.LOOP_NOT):a===j.LOOP_NATIVE&&this.source&&(this.source.loop=a),this):this},a.prototype.power=function(a){return j.HTMLAudio.prototype.power.apply(this,arguments)},a.prototype.sourceDispose=function(){this.logger.trace("source dispose",this.name),this.source&&this.source.disconnect(),this.source=void 0,this.state.time.playback=void 0,this.state.time.pause=void 0,this.state.time.progress=0},a.prototype.isDisposed=function(){return this.logger.trace("check dispose flag",!!this.state),!this.state},a.prototype.dispose=function(){this.logger.trace("WebAudio dispose",this.name),delete this.buffer,this.sourceDispose(),delete this.source,this.clearTimerAll(),delete this._timer,delete this.state.time,delete this.state.loop,delete this.state.power,delete this.state.loaded,delete this.state.error,delete this.state,this.parent=null,delete this.parent,this.sprite&&this.sprite.dispose&&this.sprite.dispose(),delete this.sprite,delete this.name,this.gainNode&&this.gainNode.disconnect&&delete this.gainNode,this.ctx=null},a}(),n=function(){function a(a,b){if(this.logger=new h("Sprite "),this.current=b,this.options=a,!b)for(var c in this.options)this.options[c].term=this.options[c].end-this.options[c].start}return a.prototype.dispose=function(){this.options=null,delete this.options,delete this.current},a}();j.HTMLAudio=k,j.HTMLVideo=l,j.WebAudio=m,c?define([],function(){return j}):a.boombox=j}(window);
//# sourceMappingURL=boombox.min.map