@eva/plugin-sound 1.2.8-fix.1 → 1.2.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
1
+ function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
2
2
 
3
3
  window.EVA = window.EVA || {};
4
4
  window.EVA.plugin = window.EVA.plugin || {};
@@ -1 +1 @@
1
- function _extends(){return(_extends=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var o=arguments[e];for(var n in o)Object.prototype.hasOwnProperty.call(o,n)&&(t[n]=o[n])}return t}).apply(this,arguments)}window.EVA=window.EVA||{},window.EVA.plugin=window.EVA.plugin||{};var _EVA_IIFE_sound=function(t,e){"use strict";var o=function(t,e){return(o=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var o in e)e.hasOwnProperty(o)&&(t[o]=e[o])})(t,e)};function n(t,e){function n(){this.constructor=t}o(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}function i(t,e,o,n){return new(o||(o=Promise))((function(i,r){function s(t){try{a(n.next(t))}catch(t){r(t)}}function u(t){try{a(n.throw(t))}catch(t){r(t)}}function a(t){var e;t.done?i(t.value):(e=t.value,e instanceof o?e:new o((function(t){t(e)}))).then(s,u)}a((n=n.apply(t,e||[])).next())}))}function r(t,e){var o,n,i,r,s={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return r={next:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(r[Symbol.iterator]=function(){return this}),r;function u(r){return function(u){return function(r){if(o)throw new TypeError("Generator is already executing.");for(;s;)try{if(o=1,n&&(i=2&r[0]?n.return:r[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,r[1])).done)return i;switch(n=0,i&&(r=[2&r[0],i.value]),r[0]){case 0:case 1:i=r;break;case 4:return s.label++,{value:r[1],done:!1};case 5:s.label++,n=r[1],r=[0];continue;case 7:r=s.ops.pop(),s.trys.pop();continue;default:if(!(i=s.trys,(i=i.length>0&&i[i.length-1])||6!==r[0]&&2!==r[0])){s=0;continue}if(3===r[0]&&(!i||r[1]>i[0]&&r[1]<i[3])){s.label=r[1];break}if(6===r[0]&&s.label<i[1]){s.label=i[1],i=r;break}if(i&&s.label<i[2]){s.label=i[2],s.ops.push(r);break}i[2]&&s.ops.pop(),s.trys.pop();continue}r=e.call(t,s)}catch(t){r=[6,t],n=0}finally{o=i=0}if(5&r[0])throw r[1];return{value:r[0]?r[1]:void 0,done:!0}}([r,u])}}}var s=function(t){function o(e){var o=t.call(this)||this;return o.autoPauseAndStart=!0,o.components=[],o.pausedComponents=[],o.audioBufferCache={},o.decodeAudioPromiseMap={},_extends(o,e),o}return n(o,t),Object.defineProperty(o.prototype,"muted",{get:function(){return!!this.gainNode&&0===this.gainNode.gain.value},set:function(t){this.gainNode&&this.gainNode.gain.setValueAtTime(t?0:1,0)},enumerable:!1,configurable:!0}),Object.defineProperty(o.prototype,"volume",{get:function(){return this.gainNode?this.gainNode.gain.value:1},set:function(t){!this.gainNode||"number"!=typeof t||t<0||t>1||this.gainNode.gain.setValueAtTime(t,0)},enumerable:!1,configurable:!0}),Object.defineProperty(o.prototype,"audioLocked",{get:function(){return!this.ctx||"running"!==this.ctx.state},enumerable:!1,configurable:!0}),o.prototype.resumeAll=function(){var t=this,e=function(){t.pausedComponents.forEach((function(t){t.play()})),t.pausedComponents=[]};this.ctx.resume().then(e,e)},o.prototype.pauseAll=function(){var t=this;this.components.forEach((function(e){e.playing&&(t.pausedComponents.push(e),e.pause())})),this.ctx.suspend().then()},o.prototype.stopAll=function(){this.components.forEach((function(t){t.playing&&t.stop()})),this.pausedComponents=[],this.ctx.suspend().then()},o.prototype.init=function(){this.setupAudioContext()},o.prototype.update=function(){var t,e,o=this.componentObserver.clear();try{for(var n=function(t){var e="function"==typeof Symbol&&Symbol.iterator,o=e&&t[e],n=0;if(o)return o.call(t);if(t&&"number"==typeof t.length)return{next:function(){return t&&n>=t.length&&(t=void 0),{value:t&&t[n++],done:!t}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")}(o),i=n.next();!i.done;i=n.next()){var r=i.value;this.componentChanged(r)}}catch(e){t={error:e}}finally{try{i&&!i.done&&(e=n.return)&&e.call(n)}finally{if(t)throw t.error}}},o.prototype.onResume=function(){this.autoPauseAndStart&&this.resumeAll()},o.prototype.onPause=function(){this.autoPauseAndStart&&this.pauseAll()},o.prototype.onDestroy=function(){this.components.forEach((function(t){t.onDestroy()})),this.components=[],this.ctx&&(this.gainNode.disconnect(),this.gainNode=null,this.ctx.close(),this.ctx=null)},o.prototype.componentChanged=function(t){return i(this,void 0,void 0,(function(){return r(this,(function(o){return"Sound"!==t.componentName||t.type===e.OBSERVER_TYPE.ADD&&this.add(t),[2]}))}))},o.prototype.setupAudioContext=function(){try{var t=window.AudioContext||window.webkitAudioContext;this.ctx=new t}catch(t){console.error(t),this.onError&&this.onError(t)}this.ctx&&(this.gainNode=void 0===this.ctx.createGain?this.ctx.createGainNode():this.ctx.createGain(),this.gainNode.gain.setValueAtTime(this.muted?0:this.volume,this.ctx.currentTime),this.gainNode.connect(this.ctx.destination),this.unlockAudio())},o.prototype.unlockAudio=function(){var t=this;if(this.ctx&&this.audioLocked){var e=function e(){if(t.ctx){var o=function(){document.body.removeEventListener("touchstart",e),document.body.removeEventListener("touchend",e),document.body.removeEventListener("click",e)};t.ctx.resume().then(o,o)}};document.body.addEventListener("touchstart",e),document.body.addEventListener("touchend",e),document.body.addEventListener("click",e)}},o.prototype.add=function(t){var o;return i(this,void 0,void 0,(function(){var n,i,s,u,a,c;return r(this,(function(r){switch(r.label){case 0:n=t.component,this.components.push(n),r.label=1;case 1:return r.trys.push([1,5,,6]),i=n.config,n.state="loading",[4,e.resource.getResource(i.resource)];case 2:return s=r.sent(),this.audioBufferCache[s.name]||!(null===(o=null==s?void 0:s.data)||void 0===o?void 0:o.audio)?[3,4]:(u=this.audioBufferCache,a=s.name,[4,this.decodeAudioData(s.data.audio,s.name)]);case 3:u[a]=r.sent(),r.label=4;case 4:return this.audioBufferCache[s.name]&&(n.systemContext=this.ctx,n.systemDestination=this.gainNode,n.onload(this.audioBufferCache[s.name])),[3,6];case 5:return c=r.sent(),this.onError&&this.onError(c),[3,6];case 6:return[2]}}))}))},o.prototype.decodeAudioData=function(t,e){var o=this;if(this.decodeAudioPromiseMap[e])return this.decodeAudioPromiseMap[e];var n=new Promise((function(n,i){o.ctx||i(new Error("No audio support"));var r=o.ctx.decodeAudioData(t,(function(t){o.decodeAudioPromiseMap[e]&&delete o.decodeAudioPromiseMap[e],t?n(t):i(new Error("Error decoding audio "+e))}),(function(n){o.decodeAudioPromiseMap[e]&&delete o.decodeAudioPromiseMap[e],i(new Error(n+". arrayBuffer byteLength: "+(t?t.byteLength:0)))}));r instanceof Promise&&r.catch((function(e){i(new Error("catch "+e+", arrayBuffer byteLength: "+(t?t.byteLength:0)))}))}));return this.decodeAudioPromiseMap[e]=n,n},o.systemName="SoundSystem",o=function(t,e,o,n){var i,r=arguments.length,s=r<3?e:null===n?n=Object.getOwnPropertyDescriptor(e,o):n;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(t,e,o,n);else for(var u=t.length-1;u>=0;u--)(i=t[u])&&(s=(r<3?i(s):r>3?i(e,o,s):i(e,o))||s);return r>3&&s&&Object.defineProperty(e,o,s),s}([e.decorators.componentObserver({Sound:[]})],o)}(e.System),u=function(t){function e(){var e=null!==t&&t.apply(this,arguments)||this;return e.state="unloaded",e.config={resource:"",autoplay:!1,muted:!1,volume:1,loop:!1,seek:0},e.playTime=0,e.startTime=0,e.duration=0,e.actionQueue=[],e}return n(e,t),Object.defineProperty(e.prototype,"muted",{get:function(){return!!this.gainNode&&0===this.gainNode.gain.value},set:function(t){this.gainNode&&this.gainNode.gain.setValueAtTime(t?0:this.config.volume,0)},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"volume",{get:function(){return this.gainNode?this.gainNode.gain.value:1},set:function(t){"number"!=typeof t||t<0||t>1||(this.config.volume=t,this.gainNode&&this.gainNode.gain.setValueAtTime(t,0))},enumerable:!1,configurable:!0}),e.prototype.init=function(t){t&&(_extends(this.config,t),this.config.autoplay&&this.actionQueue.push(this.play.bind(this)))},e.prototype.play=function(){var t=this;if("loaded"!==this.state&&this.actionQueue.push(this.play.bind(this)),this.destroySource(),this.createSource(),this.sourceNode){var e=this.systemContext.currentTime,o=this.config.seek,n=this.config.duration;this.sourceNode.start(0,o,n),this.startTime=e,this.playTime=e-o,this.paused=!1,this.playing=!0,this.resetConfig(),this.endedListener=function(){t.sourceNode&&(t.config.onEnd&&t.config.onEnd(),t.playing&&t.destroySource())},this.sourceNode.addEventListener("ended",this.endedListener)}},e.prototype.pause=function(){"loaded"!==this.state&&this.actionQueue.push(this.pause.bind(this)),!this.paused&&this.playing&&(this.paused=!0,this.playing=!1,this.config.seek=this.getCurrentTime(),this.destroySource())},e.prototype.stop=function(){"loaded"!==this.state&&this.actionQueue.push(this.stop.bind(this)),(this.paused||this.playing)&&(this.playing=!1,this.paused=!1,this.destroySource(),this.resetConfig())},e.prototype.onload=function(t){this.state="loaded",this.buffer=t,this.duration=this.buffer.duration,this.actionQueue.forEach((function(t){return t()})),this.actionQueue.length=0},e.prototype.onDestroy=function(){this.actionQueue.length=0,this.destroySource()},e.prototype.resetConfig=function(){this.config.seek=0},e.prototype.getCurrentTime=function(){return this.config.loop&&this.duration>0?(this.systemContext.currentTime-this.playTime)%this.duration:this.systemContext.currentTime-this.playTime},e.prototype.createSource=function(){this.systemContext&&"loaded"===this.state&&(this.sourceNode=this.systemContext.createBufferSource(),this.sourceNode.buffer=this.buffer,this.sourceNode.loop=this.config.loop,this.gainNode||(this.gainNode=this.systemContext.createGain(),this.gainNode.connect(this.systemDestination),_extends(this,this.config)),this.sourceNode.connect(this.gainNode))},e.prototype.destroySource=function(){this.sourceNode&&(this.sourceNode.removeEventListener("ended",this.endedListener),this.sourceNode.stop(),this.sourceNode.disconnect(),this.sourceNode=null,this.startTime=0,this.playTime=0,this.playing=!1)},e.componentName="Sound",e}(e.Component);return t.Sound=u,t.SoundSystem=s,Object.defineProperty(t,"__esModule",{value:!0}),t}({},EVA);window.EVA.plugin.sound=window.EVA.plugin.sound||_EVA_IIFE_sound;
1
+ function _extends(){return _extends=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var o=arguments[e];for(var n in o)Object.prototype.hasOwnProperty.call(o,n)&&(t[n]=o[n])}return t},_extends.apply(this,arguments)}window.EVA=window.EVA||{},window.EVA.plugin=window.EVA.plugin||{};var _EVA_IIFE_sound=function(t,e){"use strict";var o=function(t,e){return o=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var o in e)e.hasOwnProperty(o)&&(t[o]=e[o])},o(t,e)};function n(t,e){function n(){this.constructor=t}o(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}function i(t,e,o,n){return new(o||(o=Promise))((function(i,r){function s(t){try{a(n.next(t))}catch(t){r(t)}}function u(t){try{a(n.throw(t))}catch(t){r(t)}}function a(t){var e;t.done?i(t.value):(e=t.value,e instanceof o?e:new o((function(t){t(e)}))).then(s,u)}a((n=n.apply(t,e||[])).next())}))}function r(t,e){var o,n,i,r,s={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return r={next:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(r[Symbol.iterator]=function(){return this}),r;function u(r){return function(u){return function(r){if(o)throw new TypeError("Generator is already executing.");for(;s;)try{if(o=1,n&&(i=2&r[0]?n.return:r[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,r[1])).done)return i;switch(n=0,i&&(r=[2&r[0],i.value]),r[0]){case 0:case 1:i=r;break;case 4:return s.label++,{value:r[1],done:!1};case 5:s.label++,n=r[1],r=[0];continue;case 7:r=s.ops.pop(),s.trys.pop();continue;default:if(!(i=s.trys,(i=i.length>0&&i[i.length-1])||6!==r[0]&&2!==r[0])){s=0;continue}if(3===r[0]&&(!i||r[1]>i[0]&&r[1]<i[3])){s.label=r[1];break}if(6===r[0]&&s.label<i[1]){s.label=i[1],i=r;break}if(i&&s.label<i[2]){s.label=i[2],s.ops.push(r);break}i[2]&&s.ops.pop(),s.trys.pop();continue}r=e.call(t,s)}catch(t){r=[6,t],n=0}finally{o=i=0}if(5&r[0])throw r[1];return{value:r[0]?r[1]:void 0,done:!0}}([r,u])}}}var s=function(t){function o(e){var o=t.call(this)||this;return o.autoPauseAndStart=!0,o.components=[],o.pausedComponents=[],o.audioBufferCache={},o.decodeAudioPromiseMap={},_extends(o,e),o}return n(o,t),Object.defineProperty(o.prototype,"muted",{get:function(){return!!this.gainNode&&0===this.gainNode.gain.value},set:function(t){this.gainNode&&this.gainNode.gain.setValueAtTime(t?0:1,0)},enumerable:!1,configurable:!0}),Object.defineProperty(o.prototype,"volume",{get:function(){return this.gainNode?this.gainNode.gain.value:1},set:function(t){!this.gainNode||"number"!=typeof t||t<0||t>1||this.gainNode.gain.setValueAtTime(t,0)},enumerable:!1,configurable:!0}),Object.defineProperty(o.prototype,"audioLocked",{get:function(){return!this.ctx||"running"!==this.ctx.state},enumerable:!1,configurable:!0}),o.prototype.resumeAll=function(){var t=this,e=function(){t.pausedComponents.forEach((function(t){t.play()})),t.pausedComponents=[]};this.ctx.resume().then(e,e)},o.prototype.pauseAll=function(){var t=this;this.components.forEach((function(e){e.playing&&(t.pausedComponents.push(e),e.pause())})),this.ctx.suspend().then()},o.prototype.stopAll=function(){this.components.forEach((function(t){t.playing&&t.stop()})),this.pausedComponents=[],this.ctx.suspend().then()},o.prototype.init=function(){this.setupAudioContext()},o.prototype.update=function(){var t,e,o=this.componentObserver.clear();try{for(var n=function(t){var e="function"==typeof Symbol&&Symbol.iterator,o=e&&t[e],n=0;if(o)return o.call(t);if(t&&"number"==typeof t.length)return{next:function(){return t&&n>=t.length&&(t=void 0),{value:t&&t[n++],done:!t}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")}(o),i=n.next();!i.done;i=n.next()){var r=i.value;this.componentChanged(r)}}catch(e){t={error:e}}finally{try{i&&!i.done&&(e=n.return)&&e.call(n)}finally{if(t)throw t.error}}},o.prototype.onResume=function(){this.autoPauseAndStart&&this.resumeAll()},o.prototype.onPause=function(){this.autoPauseAndStart&&this.pauseAll()},o.prototype.onDestroy=function(){this.components.forEach((function(t){t.onDestroy()})),this.components=[],this.ctx&&(this.gainNode.disconnect(),this.gainNode=null,this.ctx.close(),this.ctx=null)},o.prototype.componentChanged=function(t){return i(this,void 0,void 0,(function(){return r(this,(function(o){return"Sound"!==t.componentName||t.type===e.OBSERVER_TYPE.ADD&&this.add(t),[2]}))}))},o.prototype.setupAudioContext=function(){try{var t=window.AudioContext||window.webkitAudioContext;this.ctx=new t}catch(t){console.error(t),this.onError&&this.onError(t)}this.ctx&&(this.gainNode=void 0===this.ctx.createGain?this.ctx.createGainNode():this.ctx.createGain(),this.gainNode.gain.setValueAtTime(this.muted?0:this.volume,this.ctx.currentTime),this.gainNode.connect(this.ctx.destination),this.unlockAudio())},o.prototype.unlockAudio=function(){var t=this;if(this.ctx&&this.audioLocked){var e=function e(){if(t.ctx){var o=function(){document.body.removeEventListener("touchstart",e),document.body.removeEventListener("touchend",e),document.body.removeEventListener("click",e)};t.ctx.resume().then(o,o)}};document.body.addEventListener("touchstart",e),document.body.addEventListener("touchend",e),document.body.addEventListener("click",e)}},o.prototype.add=function(t){var o;return i(this,void 0,void 0,(function(){var n,i,s,u,a,c;return r(this,(function(r){switch(r.label){case 0:n=t.component,this.components.push(n),r.label=1;case 1:return r.trys.push([1,5,,6]),i=n.config,n.state="loading",[4,e.resource.getResource(i.resource)];case 2:return s=r.sent(),this.audioBufferCache[s.name]||!(null===(o=null==s?void 0:s.data)||void 0===o?void 0:o.audio)?[3,4]:(u=this.audioBufferCache,a=s.name,[4,this.decodeAudioData(s.data.audio,s.name)]);case 3:u[a]=r.sent(),r.label=4;case 4:return this.audioBufferCache[s.name]&&(n.systemContext=this.ctx,n.systemDestination=this.gainNode,n.onload(this.audioBufferCache[s.name])),[3,6];case 5:return c=r.sent(),this.onError&&this.onError(c),[3,6];case 6:return[2]}}))}))},o.prototype.decodeAudioData=function(t,e){var o=this;if(this.decodeAudioPromiseMap[e])return this.decodeAudioPromiseMap[e];var n=new Promise((function(n,i){o.ctx||i(new Error("No audio support"));var r=o.ctx.decodeAudioData(t,(function(t){o.decodeAudioPromiseMap[e]&&delete o.decodeAudioPromiseMap[e],t?n(t):i(new Error("Error decoding audio "+e))}),(function(n){o.decodeAudioPromiseMap[e]&&delete o.decodeAudioPromiseMap[e],i(new Error(n+". arrayBuffer byteLength: "+(t?t.byteLength:0)))}));r instanceof Promise&&r.catch((function(e){i(new Error("catch "+e+", arrayBuffer byteLength: "+(t?t.byteLength:0)))}))}));return this.decodeAudioPromiseMap[e]=n,n},o.systemName="SoundSystem",o=function(t,e,o,n){var i,r=arguments.length,s=r<3?e:null===n?n=Object.getOwnPropertyDescriptor(e,o):n;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(t,e,o,n);else for(var u=t.length-1;u>=0;u--)(i=t[u])&&(s=(r<3?i(s):r>3?i(e,o,s):i(e,o))||s);return r>3&&s&&Object.defineProperty(e,o,s),s}([e.decorators.componentObserver({Sound:[]})],o),o}(e.System),u=s,a=function(t){function e(){var e=null!==t&&t.apply(this,arguments)||this;return e.state="unloaded",e.config={resource:"",autoplay:!1,muted:!1,volume:1,loop:!1,seek:0},e.playTime=0,e.startTime=0,e.duration=0,e.actionQueue=[],e}return n(e,t),Object.defineProperty(e.prototype,"muted",{get:function(){return!!this.gainNode&&0===this.gainNode.gain.value},set:function(t){this.gainNode&&this.gainNode.gain.setValueAtTime(t?0:this.config.volume,0)},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"volume",{get:function(){return this.gainNode?this.gainNode.gain.value:1},set:function(t){"number"!=typeof t||t<0||t>1||(this.config.volume=t,this.gainNode&&this.gainNode.gain.setValueAtTime(t,0))},enumerable:!1,configurable:!0}),e.prototype.init=function(t){t&&(_extends(this.config,t),this.config.autoplay&&this.actionQueue.push(this.play.bind(this)))},e.prototype.play=function(){var t=this;if("loaded"!==this.state&&this.actionQueue.push(this.play.bind(this)),this.destroySource(),this.createSource(),this.sourceNode){var e=this.systemContext.currentTime,o=this.config.seek,n=this.config.duration;this.sourceNode.start(0,o,n),this.startTime=e,this.playTime=e-o,this.paused=!1,this.playing=!0,this.resetConfig(),this.endedListener=function(){t.sourceNode&&(t.config.onEnd&&t.config.onEnd(),t.playing&&t.destroySource())},this.sourceNode.addEventListener("ended",this.endedListener)}},e.prototype.pause=function(){"loaded"!==this.state&&this.actionQueue.push(this.pause.bind(this)),!this.paused&&this.playing&&(this.paused=!0,this.playing=!1,this.config.seek=this.getCurrentTime(),this.destroySource())},e.prototype.stop=function(){"loaded"!==this.state&&this.actionQueue.push(this.stop.bind(this)),(this.paused||this.playing)&&(this.playing=!1,this.paused=!1,this.destroySource(),this.resetConfig())},e.prototype.onload=function(t){this.state="loaded",this.buffer=t,this.duration=this.buffer.duration,this.actionQueue.forEach((function(t){return t()})),this.actionQueue.length=0},e.prototype.onDestroy=function(){this.actionQueue.length=0,this.destroySource()},e.prototype.resetConfig=function(){this.config.seek=0},e.prototype.getCurrentTime=function(){return this.config.loop&&this.duration>0?(this.systemContext.currentTime-this.playTime)%this.duration:this.systemContext.currentTime-this.playTime},e.prototype.createSource=function(){this.systemContext&&"loaded"===this.state&&(this.sourceNode=this.systemContext.createBufferSource(),this.sourceNode.buffer=this.buffer,this.sourceNode.loop=this.config.loop,this.gainNode||(this.gainNode=this.systemContext.createGain(),this.gainNode.connect(this.systemDestination),_extends(this,this.config)),this.sourceNode.connect(this.gainNode))},e.prototype.destroySource=function(){this.sourceNode&&(this.sourceNode.removeEventListener("ended",this.endedListener),this.sourceNode.stop(),this.sourceNode.disconnect(),this.sourceNode=null,this.startTime=0,this.playTime=0,this.playing=!1)},e.componentName="Sound",e}(e.Component),c=a;return t.Sound=c,t.SoundSystem=u,Object.defineProperty(t,"__esModule",{value:!0}),t}({},EVA);window.EVA.plugin.sound=window.EVA.plugin.sound||_EVA_IIFE_sound;
@@ -0,0 +1,529 @@
1
+ function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
2
+
3
+ import { windowAlias, documentAlias } from '@eva/miniprogram-adapter';
4
+ import { __extends, __values, __awaiter, __generator, __decorate } from 'tslib';
5
+ import { OBSERVER_TYPE, resource, decorators, System, Component } from '@eva/eva.js/dist/miniprogram';
6
+
7
+ var SoundSystem = function (_super) {
8
+ __extends(SoundSystem, _super);
9
+
10
+ function SoundSystem(obj) {
11
+ var _this = _super.call(this) || this;
12
+
13
+ _this.autoPauseAndStart = true;
14
+ _this.components = [];
15
+ _this.pausedComponents = [];
16
+ _this.audioBufferCache = {};
17
+ _this.decodeAudioPromiseMap = {};
18
+
19
+ _extends(_this, obj);
20
+
21
+ return _this;
22
+ }
23
+
24
+ Object.defineProperty(SoundSystem.prototype, "muted", {
25
+ get: function get() {
26
+ return this.gainNode ? this.gainNode.gain.value === 0 : false;
27
+ },
28
+ set: function set(v) {
29
+ if (!this.gainNode) {
30
+ return;
31
+ }
32
+
33
+ this.gainNode.gain.setValueAtTime(v ? 0 : 1, 0);
34
+ },
35
+ enumerable: false,
36
+ configurable: true
37
+ });
38
+ Object.defineProperty(SoundSystem.prototype, "volume", {
39
+ get: function get() {
40
+ return this.gainNode ? this.gainNode.gain.value : 1;
41
+ },
42
+ set: function set(v) {
43
+ if (!this.gainNode || typeof v !== 'number' || v < 0 || v > 1) {
44
+ return;
45
+ }
46
+
47
+ this.gainNode.gain.setValueAtTime(v, 0);
48
+ },
49
+ enumerable: false,
50
+ configurable: true
51
+ });
52
+ Object.defineProperty(SoundSystem.prototype, "audioLocked", {
53
+ get: function get() {
54
+ if (!this.ctx) {
55
+ return true;
56
+ }
57
+
58
+ return this.ctx.state !== 'running';
59
+ },
60
+ enumerable: false,
61
+ configurable: true
62
+ });
63
+
64
+ SoundSystem.prototype.resumeAll = function () {
65
+ var _this = this;
66
+
67
+ var handleResume = function handleResume() {
68
+ _this.pausedComponents.forEach(function (component) {
69
+ component.play();
70
+ });
71
+
72
+ _this.pausedComponents = [];
73
+ };
74
+
75
+ this.ctx.resume().then(handleResume, handleResume);
76
+ };
77
+
78
+ SoundSystem.prototype.pauseAll = function () {
79
+ var _this = this;
80
+
81
+ this.components.forEach(function (component) {
82
+ if (component.playing) {
83
+ _this.pausedComponents.push(component);
84
+
85
+ component.pause();
86
+ }
87
+ });
88
+ this.ctx.suspend().then();
89
+ };
90
+
91
+ SoundSystem.prototype.stopAll = function () {
92
+ this.components.forEach(function (component) {
93
+ if (component.playing) {
94
+ component.stop();
95
+ }
96
+ });
97
+ this.pausedComponents = [];
98
+ this.ctx.suspend().then();
99
+ };
100
+
101
+ SoundSystem.prototype.init = function () {
102
+ this.setupAudioContext();
103
+ };
104
+
105
+ SoundSystem.prototype.update = function () {
106
+ var e_1, _a;
107
+
108
+ var changes = this.componentObserver.clear();
109
+
110
+ try {
111
+ for (var changes_1 = __values(changes), changes_1_1 = changes_1.next(); !changes_1_1.done; changes_1_1 = changes_1.next()) {
112
+ var changed = changes_1_1.value;
113
+ this.componentChanged(changed);
114
+ }
115
+ } catch (e_1_1) {
116
+ e_1 = {
117
+ error: e_1_1
118
+ };
119
+ } finally {
120
+ try {
121
+ if (changes_1_1 && !changes_1_1.done && (_a = changes_1.return)) _a.call(changes_1);
122
+ } finally {
123
+ if (e_1) throw e_1.error;
124
+ }
125
+ }
126
+ };
127
+
128
+ SoundSystem.prototype.onResume = function () {
129
+ if (!this.autoPauseAndStart) {
130
+ return;
131
+ }
132
+
133
+ this.resumeAll();
134
+ };
135
+
136
+ SoundSystem.prototype.onPause = function () {
137
+ if (!this.autoPauseAndStart) {
138
+ return;
139
+ }
140
+
141
+ this.pauseAll();
142
+ };
143
+
144
+ SoundSystem.prototype.onDestroy = function () {
145
+ this.components.forEach(function (component) {
146
+ component.onDestroy();
147
+ });
148
+ this.components = [];
149
+
150
+ if (this.ctx) {
151
+ this.gainNode.disconnect();
152
+ this.gainNode = null;
153
+ this.ctx.close();
154
+ this.ctx = null;
155
+ }
156
+ };
157
+
158
+ SoundSystem.prototype.componentChanged = function (changed) {
159
+ return __awaiter(this, void 0, void 0, function () {
160
+ return __generator(this, function (_a) {
161
+ if (changed.componentName !== 'Sound') return [2];
162
+
163
+ if (changed.type === OBSERVER_TYPE.ADD) {
164
+ this.add(changed);
165
+ }
166
+
167
+ return [2];
168
+ });
169
+ });
170
+ };
171
+
172
+ SoundSystem.prototype.setupAudioContext = function () {
173
+ try {
174
+ var AudioContext_1 = windowAlias.AudioContext || windowAlias.webkitAudioContext;
175
+ this.ctx = new AudioContext_1();
176
+ } catch (error) {
177
+ console.error(error);
178
+
179
+ if (this.onError) {
180
+ this.onError(error);
181
+ }
182
+ }
183
+
184
+ if (!this.ctx) {
185
+ return;
186
+ }
187
+
188
+ this.gainNode = typeof this.ctx.createGain === 'undefined' ? this.ctx.createGainNode() : this.ctx.createGain();
189
+ this.gainNode.gain.setValueAtTime(this.muted ? 0 : this.volume, this.ctx.currentTime);
190
+ this.gainNode.connect(this.ctx.destination);
191
+ this.unlockAudio();
192
+ };
193
+
194
+ SoundSystem.prototype.unlockAudio = function () {
195
+ var _this = this;
196
+
197
+ if (!this.ctx || !this.audioLocked) {
198
+ return;
199
+ }
200
+
201
+ var unlock = function unlock() {
202
+ if (_this.ctx) {
203
+ var removeListenerFn = function removeListenerFn() {
204
+ documentAlias.body.removeEventListener('touchstart', unlock);
205
+ documentAlias.body.removeEventListener('touchend', unlock);
206
+ documentAlias.body.removeEventListener('click', unlock);
207
+ };
208
+
209
+ _this.ctx.resume().then(removeListenerFn, removeListenerFn);
210
+ }
211
+ };
212
+
213
+ documentAlias.body.addEventListener('touchstart', unlock);
214
+ documentAlias.body.addEventListener('touchend', unlock);
215
+ documentAlias.body.addEventListener('click', unlock);
216
+ };
217
+
218
+ SoundSystem.prototype.add = function (changed) {
219
+ var _a;
220
+
221
+ return __awaiter(this, void 0, void 0, function () {
222
+ var component, config, audio, _b, _c, error_1;
223
+
224
+ return __generator(this, function (_d) {
225
+ switch (_d.label) {
226
+ case 0:
227
+ component = changed.component;
228
+ this.components.push(component);
229
+ _d.label = 1;
230
+
231
+ case 1:
232
+ _d.trys.push([1, 5,, 6]);
233
+
234
+ config = component.config;
235
+ component.state = 'loading';
236
+ return [4, resource.getResource(config.resource)];
237
+
238
+ case 2:
239
+ audio = _d.sent();
240
+ if (!(!this.audioBufferCache[audio.name] && ((_a = audio === null || audio === void 0 ? void 0 : audio.data) === null || _a === void 0 ? void 0 : _a.audio))) return [3, 4];
241
+ _b = this.audioBufferCache;
242
+ _c = audio.name;
243
+ return [4, this.decodeAudioData(audio.data.audio, audio.name)];
244
+
245
+ case 3:
246
+ _b[_c] = _d.sent();
247
+ _d.label = 4;
248
+
249
+ case 4:
250
+ if (this.audioBufferCache[audio.name]) {
251
+ component.systemContext = this.ctx;
252
+ component.systemDestination = this.gainNode;
253
+ component.onload(this.audioBufferCache[audio.name]);
254
+ }
255
+
256
+ return [3, 6];
257
+
258
+ case 5:
259
+ error_1 = _d.sent();
260
+
261
+ if (this.onError) {
262
+ this.onError(error_1);
263
+ }
264
+
265
+ return [3, 6];
266
+
267
+ case 6:
268
+ return [2];
269
+ }
270
+ });
271
+ });
272
+ };
273
+
274
+ SoundSystem.prototype.decodeAudioData = function (arraybuffer, name) {
275
+ var _this = this;
276
+
277
+ if (this.decodeAudioPromiseMap[name]) {
278
+ return this.decodeAudioPromiseMap[name];
279
+ }
280
+
281
+ var promise = new Promise(function (resolve, reject) {
282
+ if (!_this.ctx) {
283
+ reject(new Error('No audio support'));
284
+ }
285
+
286
+ var success = function success(decodedData) {
287
+ if (_this.decodeAudioPromiseMap[name]) {
288
+ delete _this.decodeAudioPromiseMap[name];
289
+ }
290
+
291
+ if (decodedData) {
292
+ resolve(decodedData);
293
+ } else {
294
+ reject(new Error("Error decoding audio " + name));
295
+ }
296
+ };
297
+
298
+ var error = function error(err) {
299
+ if (_this.decodeAudioPromiseMap[name]) {
300
+ delete _this.decodeAudioPromiseMap[name];
301
+ }
302
+
303
+ reject(new Error(err + ". arrayBuffer byteLength: " + (arraybuffer ? arraybuffer.byteLength : 0)));
304
+ };
305
+
306
+ var promise = _this.ctx.decodeAudioData(arraybuffer, success, error);
307
+
308
+ if (promise instanceof Promise) {
309
+ promise.catch(function (err) {
310
+ reject(new Error("catch " + err + ", arrayBuffer byteLength: " + (arraybuffer ? arraybuffer.byteLength : 0)));
311
+ });
312
+ }
313
+ });
314
+ this.decodeAudioPromiseMap[name] = promise;
315
+ return promise;
316
+ };
317
+
318
+ SoundSystem.systemName = 'SoundSystem';
319
+ SoundSystem = __decorate([decorators.componentObserver({
320
+ Sound: []
321
+ })], SoundSystem);
322
+ return SoundSystem;
323
+ }(System);
324
+
325
+ var SoundSystem$1 = SoundSystem;
326
+
327
+ var Sound = function (_super) {
328
+ __extends(Sound, _super);
329
+
330
+ function Sound() {
331
+ var _this = _super !== null && _super.apply(this, arguments) || this;
332
+
333
+ _this.state = 'unloaded';
334
+ _this.config = {
335
+ resource: '',
336
+ autoplay: false,
337
+ muted: false,
338
+ volume: 1,
339
+ loop: false,
340
+ seek: 0
341
+ };
342
+ _this.playTime = 0;
343
+ _this.startTime = 0;
344
+ _this.duration = 0;
345
+ _this.actionQueue = [];
346
+ return _this;
347
+ }
348
+
349
+ Object.defineProperty(Sound.prototype, "muted", {
350
+ get: function get() {
351
+ return this.gainNode ? this.gainNode.gain.value === 0 : false;
352
+ },
353
+ set: function set(v) {
354
+ if (!this.gainNode) {
355
+ return;
356
+ }
357
+
358
+ this.gainNode.gain.setValueAtTime(v ? 0 : this.config.volume, 0);
359
+ },
360
+ enumerable: false,
361
+ configurable: true
362
+ });
363
+ Object.defineProperty(Sound.prototype, "volume", {
364
+ get: function get() {
365
+ return this.gainNode ? this.gainNode.gain.value : 1;
366
+ },
367
+ set: function set(v) {
368
+ if (typeof v !== 'number' || v < 0 || v > 1) {
369
+ return;
370
+ }
371
+
372
+ this.config.volume = v;
373
+
374
+ if (!this.gainNode) {
375
+ return;
376
+ }
377
+
378
+ this.gainNode.gain.setValueAtTime(v, 0);
379
+ },
380
+ enumerable: false,
381
+ configurable: true
382
+ });
383
+
384
+ Sound.prototype.init = function (obj) {
385
+ if (!obj) {
386
+ return;
387
+ }
388
+
389
+ _extends(this.config, obj);
390
+
391
+ if (this.config.autoplay) {
392
+ this.actionQueue.push(this.play.bind(this));
393
+ }
394
+ };
395
+
396
+ Sound.prototype.play = function () {
397
+ var _this = this;
398
+
399
+ if (this.state !== 'loaded') {
400
+ this.actionQueue.push(this.play.bind(this));
401
+ }
402
+
403
+ this.destroySource();
404
+ this.createSource();
405
+
406
+ if (!this.sourceNode) {
407
+ return;
408
+ }
409
+
410
+ var when = this.systemContext.currentTime;
411
+ var offset = this.config.seek;
412
+ var duration = this.config.duration;
413
+ this.sourceNode.start(0, offset, duration);
414
+ this.startTime = when;
415
+ this.playTime = when - offset;
416
+ this.paused = false;
417
+ this.playing = true;
418
+ this.resetConfig();
419
+
420
+ this.endedListener = function () {
421
+ if (!_this.sourceNode) {
422
+ return;
423
+ }
424
+
425
+ if (_this.config.onEnd) {
426
+ _this.config.onEnd();
427
+ }
428
+
429
+ if (_this.playing) {
430
+ _this.destroySource();
431
+ }
432
+ };
433
+
434
+ this.sourceNode.addEventListener('ended', this.endedListener);
435
+ };
436
+
437
+ Sound.prototype.pause = function () {
438
+ if (this.state !== 'loaded') {
439
+ this.actionQueue.push(this.pause.bind(this));
440
+ }
441
+
442
+ if (this.paused || !this.playing) {
443
+ return;
444
+ }
445
+
446
+ this.paused = true;
447
+ this.playing = false;
448
+ this.config.seek = this.getCurrentTime();
449
+ this.destroySource();
450
+ };
451
+
452
+ Sound.prototype.stop = function () {
453
+ if (this.state !== 'loaded') {
454
+ this.actionQueue.push(this.stop.bind(this));
455
+ }
456
+
457
+ if (!this.paused && !this.playing) {
458
+ return;
459
+ }
460
+
461
+ this.playing = false;
462
+ this.paused = false;
463
+ this.destroySource();
464
+ this.resetConfig();
465
+ };
466
+
467
+ Sound.prototype.onload = function (buffer) {
468
+ this.state = 'loaded';
469
+ this.buffer = buffer;
470
+ this.duration = this.buffer.duration;
471
+ this.actionQueue.forEach(function (action) {
472
+ return action();
473
+ });
474
+ this.actionQueue.length = 0;
475
+ };
476
+
477
+ Sound.prototype.onDestroy = function () {
478
+ this.actionQueue.length = 0;
479
+ this.destroySource();
480
+ };
481
+
482
+ Sound.prototype.resetConfig = function () {
483
+ this.config.seek = 0;
484
+ };
485
+
486
+ Sound.prototype.getCurrentTime = function () {
487
+ if (this.config.loop && this.duration > 0) {
488
+ return (this.systemContext.currentTime - this.playTime) % this.duration;
489
+ }
490
+
491
+ return this.systemContext.currentTime - this.playTime;
492
+ };
493
+
494
+ Sound.prototype.createSource = function () {
495
+ if (!this.systemContext || this.state !== 'loaded') {
496
+ return;
497
+ }
498
+
499
+ this.sourceNode = this.systemContext.createBufferSource();
500
+ this.sourceNode.buffer = this.buffer;
501
+ this.sourceNode.loop = this.config.loop;
502
+
503
+ if (!this.gainNode) {
504
+ this.gainNode = this.systemContext.createGain();
505
+ this.gainNode.connect(this.systemDestination);
506
+
507
+ _extends(this, this.config);
508
+ }
509
+
510
+ this.sourceNode.connect(this.gainNode);
511
+ };
512
+
513
+ Sound.prototype.destroySource = function () {
514
+ if (!this.sourceNode) return;
515
+ this.sourceNode.removeEventListener('ended', this.endedListener);
516
+ this.sourceNode.stop();
517
+ this.sourceNode.disconnect();
518
+ this.sourceNode = null;
519
+ this.startTime = 0;
520
+ this.playTime = 0;
521
+ this.playing = false;
522
+ };
523
+
524
+ Sound.componentName = 'Sound';
525
+ return Sound;
526
+ }(Component);
527
+
528
+ var Sound$1 = Sound;
529
+ export { Sound$1 as Sound, SoundSystem$1 as SoundSystem };
@@ -1,4 +1,4 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("@eva/eva.js"),e=function(t,o){return(e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var o in e)e.hasOwnProperty(o)&&(t[o]=e[o])})(t,o)};
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("@eva/eva.js"),e=function(t,o){return e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var o in e)e.hasOwnProperty(o)&&(t[o]=e[o])},e(t,o)};
2
2
  /*! *****************************************************************************
3
3
  Copyright (c) Microsoft Corporation. All rights reserved.
4
4
  Licensed under the Apache License, Version 2.0 (the "License"); you may not use
@@ -12,4 +12,4 @@ MERCHANTABLITY OR NON-INFRINGEMENT.
12
12
 
13
13
  See the Apache Version 2.0 License for specific language governing permissions
14
14
  and limitations under the License.
15
- ***************************************************************************** */function o(t,o){function n(){this.constructor=t}e(t,o),t.prototype=null===o?Object.create(o):(n.prototype=o.prototype,new n)}function n(t,e,o,n){return new(o||(o=Promise))((function(i,r){function s(t){try{a(n.next(t))}catch(t){r(t)}}function u(t){try{a(n.throw(t))}catch(t){r(t)}}function a(t){t.done?i(t.value):new o((function(e){e(t.value)})).then(s,u)}a((n=n.apply(t,e||[])).next())}))}function i(t,e){var o,n,i,r,s={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return r={next:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(r[Symbol.iterator]=function(){return this}),r;function u(r){return function(u){return function(r){if(o)throw new TypeError("Generator is already executing.");for(;s;)try{if(o=1,n&&(i=2&r[0]?n.return:r[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,r[1])).done)return i;switch(n=0,i&&(r=[2&r[0],i.value]),r[0]){case 0:case 1:i=r;break;case 4:return s.label++,{value:r[1],done:!1};case 5:s.label++,n=r[1],r=[0];continue;case 7:r=s.ops.pop(),s.trys.pop();continue;default:if(!(i=s.trys,(i=i.length>0&&i[i.length-1])||6!==r[0]&&2!==r[0])){s=0;continue}if(3===r[0]&&(!i||r[1]>i[0]&&r[1]<i[3])){s.label=r[1];break}if(6===r[0]&&s.label<i[1]){s.label=i[1],i=r;break}if(i&&s.label<i[2]){s.label=i[2],s.ops.push(r);break}i[2]&&s.ops.pop(),s.trys.pop();continue}r=e.call(t,s)}catch(t){r=[6,t],n=0}finally{o=i=0}if(5&r[0])throw r[1];return{value:r[0]?r[1]:void 0,done:!0}}([r,u])}}}var r=function(e){function r(t){var o=e.call(this)||this;return o.autoPauseAndStart=!0,o.components=[],o.pausedComponents=[],o.audioBufferCache={},o.decodeAudioPromiseMap={},Object.assign(o,t),o}return o(r,e),Object.defineProperty(r.prototype,"muted",{get:function(){return!!this.gainNode&&0===this.gainNode.gain.value},set:function(t){this.gainNode&&this.gainNode.gain.setValueAtTime(t?0:1,0)},enumerable:!1,configurable:!0}),Object.defineProperty(r.prototype,"volume",{get:function(){return this.gainNode?this.gainNode.gain.value:1},set:function(t){!this.gainNode||"number"!=typeof t||t<0||t>1||this.gainNode.gain.setValueAtTime(t,0)},enumerable:!1,configurable:!0}),Object.defineProperty(r.prototype,"audioLocked",{get:function(){return!this.ctx||"running"!==this.ctx.state},enumerable:!1,configurable:!0}),r.prototype.resumeAll=function(){var t=this,e=function(){t.pausedComponents.forEach((function(t){t.play()})),t.pausedComponents=[]};this.ctx.resume().then(e,e)},r.prototype.pauseAll=function(){var t=this;this.components.forEach((function(e){e.playing&&(t.pausedComponents.push(e),e.pause())})),this.ctx.suspend().then()},r.prototype.stopAll=function(){this.components.forEach((function(t){t.playing&&t.stop()})),this.pausedComponents=[],this.ctx.suspend().then()},r.prototype.init=function(){this.setupAudioContext()},r.prototype.update=function(){var t,e,o=this.componentObserver.clear();try{for(var n=function(t){var e="function"==typeof Symbol&&t[Symbol.iterator],o=0;return e?e.call(t):{next:function(){return t&&o>=t.length&&(t=void 0),{value:t&&t[o++],done:!t}}}}(o),i=n.next();!i.done;i=n.next()){var r=i.value;this.componentChanged(r)}}catch(e){t={error:e}}finally{try{i&&!i.done&&(e=n.return)&&e.call(n)}finally{if(t)throw t.error}}},r.prototype.onResume=function(){this.autoPauseAndStart&&this.resumeAll()},r.prototype.onPause=function(){this.autoPauseAndStart&&this.pauseAll()},r.prototype.onDestroy=function(){this.components.forEach((function(t){t.onDestroy()})),this.components=[],this.ctx&&(this.gainNode.disconnect(),this.gainNode=null,this.ctx.close(),this.ctx=null)},r.prototype.componentChanged=function(e){return n(this,void 0,void 0,(function(){return i(this,(function(o){return"Sound"!==e.componentName||e.type===t.OBSERVER_TYPE.ADD&&this.add(e),[2]}))}))},r.prototype.setupAudioContext=function(){try{var t=window.AudioContext||window.webkitAudioContext;this.ctx=new t}catch(t){console.error(t),this.onError&&this.onError(t)}this.ctx&&(this.gainNode=void 0===this.ctx.createGain?this.ctx.createGainNode():this.ctx.createGain(),this.gainNode.gain.setValueAtTime(this.muted?0:this.volume,this.ctx.currentTime),this.gainNode.connect(this.ctx.destination),this.unlockAudio())},r.prototype.unlockAudio=function(){var t=this;if(this.ctx&&this.audioLocked){var e=function(){if(t.ctx){var o=function(){document.body.removeEventListener("touchstart",e),document.body.removeEventListener("touchend",e),document.body.removeEventListener("click",e)};t.ctx.resume().then(o,o)}};document.body.addEventListener("touchstart",e),document.body.addEventListener("touchend",e),document.body.addEventListener("click",e)}},r.prototype.add=function(e){var o;return n(this,void 0,void 0,(function(){var n,r,s,u,a,c;return i(this,(function(i){switch(i.label){case 0:n=e.component,this.components.push(n),i.label=1;case 1:return i.trys.push([1,5,,6]),r=n.config,n.state="loading",[4,t.resource.getResource(r.resource)];case 2:return s=i.sent(),this.audioBufferCache[s.name]||!(null===(o=null==s?void 0:s.data)||void 0===o?void 0:o.audio)?[3,4]:(u=this.audioBufferCache,a=s.name,[4,this.decodeAudioData(s.data.audio,s.name)]);case 3:u[a]=i.sent(),i.label=4;case 4:return this.audioBufferCache[s.name]&&(n.systemContext=this.ctx,n.systemDestination=this.gainNode,n.onload(this.audioBufferCache[s.name])),[3,6];case 5:return c=i.sent(),this.onError&&this.onError(c),[3,6];case 6:return[2]}}))}))},r.prototype.decodeAudioData=function(t,e){var o=this;if(this.decodeAudioPromiseMap[e])return this.decodeAudioPromiseMap[e];var n=new Promise((function(n,i){o.ctx||i(new Error("No audio support"));var r=o.ctx.decodeAudioData(t,(function(t){o.decodeAudioPromiseMap[e]&&delete o.decodeAudioPromiseMap[e],t?n(t):i(new Error("Error decoding audio "+e))}),(function(n){o.decodeAudioPromiseMap[e]&&delete o.decodeAudioPromiseMap[e],i(new Error(n+". arrayBuffer byteLength: "+(t?t.byteLength:0)))}));r instanceof Promise&&r.catch((function(e){i(new Error("catch "+e+", arrayBuffer byteLength: "+(t?t.byteLength:0)))}))}));return this.decodeAudioPromiseMap[e]=n,n},r.systemName="SoundSystem",r=function(t,e,o,n){var i,r=arguments.length,s=r<3?e:null===n?n=Object.getOwnPropertyDescriptor(e,o):n;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(t,e,o,n);else for(var u=t.length-1;u>=0;u--)(i=t[u])&&(s=(r<3?i(s):r>3?i(e,o,s):i(e,o))||s);return r>3&&s&&Object.defineProperty(e,o,s),s}([t.decorators.componentObserver({Sound:[]})],r)}(t.System),s=function(t){function e(){var e=null!==t&&t.apply(this,arguments)||this;return e.state="unloaded",e.config={resource:"",autoplay:!1,muted:!1,volume:1,loop:!1,seek:0},e.playTime=0,e.startTime=0,e.duration=0,e.actionQueue=[],e}return o(e,t),Object.defineProperty(e.prototype,"muted",{get:function(){return!!this.gainNode&&0===this.gainNode.gain.value},set:function(t){this.gainNode&&this.gainNode.gain.setValueAtTime(t?0:this.config.volume,0)},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"volume",{get:function(){return this.gainNode?this.gainNode.gain.value:1},set:function(t){"number"!=typeof t||t<0||t>1||(this.config.volume=t,this.gainNode&&this.gainNode.gain.setValueAtTime(t,0))},enumerable:!1,configurable:!0}),e.prototype.init=function(t){t&&(Object.assign(this.config,t),this.config.autoplay&&this.actionQueue.push(this.play.bind(this)))},e.prototype.play=function(){var t=this;if("loaded"!==this.state&&this.actionQueue.push(this.play.bind(this)),this.destroySource(),this.createSource(),this.sourceNode){var e=this.systemContext.currentTime,o=this.config.seek,n=this.config.duration;this.sourceNode.start(0,o,n),this.startTime=e,this.playTime=e-o,this.paused=!1,this.playing=!0,this.resetConfig(),this.endedListener=function(){t.sourceNode&&(t.config.onEnd&&t.config.onEnd(),t.playing&&t.destroySource())},this.sourceNode.addEventListener("ended",this.endedListener)}},e.prototype.pause=function(){"loaded"!==this.state&&this.actionQueue.push(this.pause.bind(this)),!this.paused&&this.playing&&(this.paused=!0,this.playing=!1,this.config.seek=this.getCurrentTime(),this.destroySource())},e.prototype.stop=function(){"loaded"!==this.state&&this.actionQueue.push(this.stop.bind(this)),(this.paused||this.playing)&&(this.playing=!1,this.paused=!1,this.destroySource(),this.resetConfig())},e.prototype.onload=function(t){this.state="loaded",this.buffer=t,this.duration=this.buffer.duration,this.actionQueue.forEach((function(t){return t()})),this.actionQueue.length=0},e.prototype.onDestroy=function(){this.actionQueue.length=0,this.destroySource()},e.prototype.resetConfig=function(){this.config.seek=0},e.prototype.getCurrentTime=function(){return this.config.loop&&this.duration>0?(this.systemContext.currentTime-this.playTime)%this.duration:this.systemContext.currentTime-this.playTime},e.prototype.createSource=function(){this.systemContext&&"loaded"===this.state&&(this.sourceNode=this.systemContext.createBufferSource(),this.sourceNode.buffer=this.buffer,this.sourceNode.loop=this.config.loop,this.gainNode||(this.gainNode=this.systemContext.createGain(),this.gainNode.connect(this.systemDestination),Object.assign(this,this.config)),this.sourceNode.connect(this.gainNode))},e.prototype.destroySource=function(){this.sourceNode&&(this.sourceNode.removeEventListener("ended",this.endedListener),this.sourceNode.stop(),this.sourceNode.disconnect(),this.sourceNode=null,this.startTime=0,this.playTime=0,this.playing=!1)},e.componentName="Sound",e}(t.Component);exports.Sound=s,exports.SoundSystem=r;
15
+ ***************************************************************************** */function o(t,o){function n(){this.constructor=t}e(t,o),t.prototype=null===o?Object.create(o):(n.prototype=o.prototype,new n)}function n(t,e,o,n){return new(o||(o=Promise))((function(i,r){function s(t){try{a(n.next(t))}catch(t){r(t)}}function u(t){try{a(n.throw(t))}catch(t){r(t)}}function a(t){t.done?i(t.value):new o((function(e){e(t.value)})).then(s,u)}a((n=n.apply(t,e||[])).next())}))}function i(t,e){var o,n,i,r,s={label:0,sent:function(){if(1&i[0])throw i[1];return i[1]},trys:[],ops:[]};return r={next:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(r[Symbol.iterator]=function(){return this}),r;function u(r){return function(u){return function(r){if(o)throw new TypeError("Generator is already executing.");for(;s;)try{if(o=1,n&&(i=2&r[0]?n.return:r[0]?n.throw||((i=n.return)&&i.call(n),0):n.next)&&!(i=i.call(n,r[1])).done)return i;switch(n=0,i&&(r=[2&r[0],i.value]),r[0]){case 0:case 1:i=r;break;case 4:return s.label++,{value:r[1],done:!1};case 5:s.label++,n=r[1],r=[0];continue;case 7:r=s.ops.pop(),s.trys.pop();continue;default:if(!(i=s.trys,(i=i.length>0&&i[i.length-1])||6!==r[0]&&2!==r[0])){s=0;continue}if(3===r[0]&&(!i||r[1]>i[0]&&r[1]<i[3])){s.label=r[1];break}if(6===r[0]&&s.label<i[1]){s.label=i[1],i=r;break}if(i&&s.label<i[2]){s.label=i[2],s.ops.push(r);break}i[2]&&s.ops.pop(),s.trys.pop();continue}r=e.call(t,s)}catch(t){r=[6,t],n=0}finally{o=i=0}if(5&r[0])throw r[1];return{value:r[0]?r[1]:void 0,done:!0}}([r,u])}}}var r=function(e){function r(t){var o=e.call(this)||this;return o.autoPauseAndStart=!0,o.components=[],o.pausedComponents=[],o.audioBufferCache={},o.decodeAudioPromiseMap={},Object.assign(o,t),o}return o(r,e),Object.defineProperty(r.prototype,"muted",{get:function(){return!!this.gainNode&&0===this.gainNode.gain.value},set:function(t){this.gainNode&&this.gainNode.gain.setValueAtTime(t?0:1,0)},enumerable:!1,configurable:!0}),Object.defineProperty(r.prototype,"volume",{get:function(){return this.gainNode?this.gainNode.gain.value:1},set:function(t){!this.gainNode||"number"!=typeof t||t<0||t>1||this.gainNode.gain.setValueAtTime(t,0)},enumerable:!1,configurable:!0}),Object.defineProperty(r.prototype,"audioLocked",{get:function(){return!this.ctx||"running"!==this.ctx.state},enumerable:!1,configurable:!0}),r.prototype.resumeAll=function(){var t=this,e=function(){t.pausedComponents.forEach((function(t){t.play()})),t.pausedComponents=[]};this.ctx.resume().then(e,e)},r.prototype.pauseAll=function(){var t=this;this.components.forEach((function(e){e.playing&&(t.pausedComponents.push(e),e.pause())})),this.ctx.suspend().then()},r.prototype.stopAll=function(){this.components.forEach((function(t){t.playing&&t.stop()})),this.pausedComponents=[],this.ctx.suspend().then()},r.prototype.init=function(){this.setupAudioContext()},r.prototype.update=function(){var t,e,o=this.componentObserver.clear();try{for(var n=function(t){var e="function"==typeof Symbol&&t[Symbol.iterator],o=0;return e?e.call(t):{next:function(){return t&&o>=t.length&&(t=void 0),{value:t&&t[o++],done:!t}}}}(o),i=n.next();!i.done;i=n.next()){var r=i.value;this.componentChanged(r)}}catch(e){t={error:e}}finally{try{i&&!i.done&&(e=n.return)&&e.call(n)}finally{if(t)throw t.error}}},r.prototype.onResume=function(){this.autoPauseAndStart&&this.resumeAll()},r.prototype.onPause=function(){this.autoPauseAndStart&&this.pauseAll()},r.prototype.onDestroy=function(){this.components.forEach((function(t){t.onDestroy()})),this.components=[],this.ctx&&(this.gainNode.disconnect(),this.gainNode=null,this.ctx.close(),this.ctx=null)},r.prototype.componentChanged=function(e){return n(this,void 0,void 0,(function(){return i(this,(function(o){return"Sound"!==e.componentName||e.type===t.OBSERVER_TYPE.ADD&&this.add(e),[2]}))}))},r.prototype.setupAudioContext=function(){try{var t=window.AudioContext||window.webkitAudioContext;this.ctx=new t}catch(t){console.error(t),this.onError&&this.onError(t)}this.ctx&&(this.gainNode=void 0===this.ctx.createGain?this.ctx.createGainNode():this.ctx.createGain(),this.gainNode.gain.setValueAtTime(this.muted?0:this.volume,this.ctx.currentTime),this.gainNode.connect(this.ctx.destination),this.unlockAudio())},r.prototype.unlockAudio=function(){var t=this;if(this.ctx&&this.audioLocked){var e=function(){if(t.ctx){var o=function(){document.body.removeEventListener("touchstart",e),document.body.removeEventListener("touchend",e),document.body.removeEventListener("click",e)};t.ctx.resume().then(o,o)}};document.body.addEventListener("touchstart",e),document.body.addEventListener("touchend",e),document.body.addEventListener("click",e)}},r.prototype.add=function(e){var o;return n(this,void 0,void 0,(function(){var n,r,s,u,a,c;return i(this,(function(i){switch(i.label){case 0:n=e.component,this.components.push(n),i.label=1;case 1:return i.trys.push([1,5,,6]),r=n.config,n.state="loading",[4,t.resource.getResource(r.resource)];case 2:return s=i.sent(),this.audioBufferCache[s.name]||!(null===(o=null==s?void 0:s.data)||void 0===o?void 0:o.audio)?[3,4]:(u=this.audioBufferCache,a=s.name,[4,this.decodeAudioData(s.data.audio,s.name)]);case 3:u[a]=i.sent(),i.label=4;case 4:return this.audioBufferCache[s.name]&&(n.systemContext=this.ctx,n.systemDestination=this.gainNode,n.onload(this.audioBufferCache[s.name])),[3,6];case 5:return c=i.sent(),this.onError&&this.onError(c),[3,6];case 6:return[2]}}))}))},r.prototype.decodeAudioData=function(t,e){var o=this;if(this.decodeAudioPromiseMap[e])return this.decodeAudioPromiseMap[e];var n=new Promise((function(n,i){o.ctx||i(new Error("No audio support"));var r=o.ctx.decodeAudioData(t,(function(t){o.decodeAudioPromiseMap[e]&&delete o.decodeAudioPromiseMap[e],t?n(t):i(new Error("Error decoding audio "+e))}),(function(n){o.decodeAudioPromiseMap[e]&&delete o.decodeAudioPromiseMap[e],i(new Error(n+". arrayBuffer byteLength: "+(t?t.byteLength:0)))}));r instanceof Promise&&r.catch((function(e){i(new Error("catch "+e+", arrayBuffer byteLength: "+(t?t.byteLength:0)))}))}));return this.decodeAudioPromiseMap[e]=n,n},r.systemName="SoundSystem",r=function(t,e,o,n){var i,r=arguments.length,s=r<3?e:null===n?n=Object.getOwnPropertyDescriptor(e,o):n;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(t,e,o,n);else for(var u=t.length-1;u>=0;u--)(i=t[u])&&(s=(r<3?i(s):r>3?i(e,o,s):i(e,o))||s);return r>3&&s&&Object.defineProperty(e,o,s),s}([t.decorators.componentObserver({Sound:[]})],r),r}(t.System),s=function(t){function e(){var e=null!==t&&t.apply(this,arguments)||this;return e.state="unloaded",e.config={resource:"",autoplay:!1,muted:!1,volume:1,loop:!1,seek:0},e.playTime=0,e.startTime=0,e.duration=0,e.actionQueue=[],e}return o(e,t),Object.defineProperty(e.prototype,"muted",{get:function(){return!!this.gainNode&&0===this.gainNode.gain.value},set:function(t){this.gainNode&&this.gainNode.gain.setValueAtTime(t?0:this.config.volume,0)},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"volume",{get:function(){return this.gainNode?this.gainNode.gain.value:1},set:function(t){"number"!=typeof t||t<0||t>1||(this.config.volume=t,this.gainNode&&this.gainNode.gain.setValueAtTime(t,0))},enumerable:!1,configurable:!0}),e.prototype.init=function(t){t&&(Object.assign(this.config,t),this.config.autoplay&&this.actionQueue.push(this.play.bind(this)))},e.prototype.play=function(){var t=this;if("loaded"!==this.state&&this.actionQueue.push(this.play.bind(this)),this.destroySource(),this.createSource(),this.sourceNode){var e=this.systemContext.currentTime,o=this.config.seek,n=this.config.duration;this.sourceNode.start(0,o,n),this.startTime=e,this.playTime=e-o,this.paused=!1,this.playing=!0,this.resetConfig(),this.endedListener=function(){t.sourceNode&&(t.config.onEnd&&t.config.onEnd(),t.playing&&t.destroySource())},this.sourceNode.addEventListener("ended",this.endedListener)}},e.prototype.pause=function(){"loaded"!==this.state&&this.actionQueue.push(this.pause.bind(this)),!this.paused&&this.playing&&(this.paused=!0,this.playing=!1,this.config.seek=this.getCurrentTime(),this.destroySource())},e.prototype.stop=function(){"loaded"!==this.state&&this.actionQueue.push(this.stop.bind(this)),(this.paused||this.playing)&&(this.playing=!1,this.paused=!1,this.destroySource(),this.resetConfig())},e.prototype.onload=function(t){this.state="loaded",this.buffer=t,this.duration=this.buffer.duration,this.actionQueue.forEach((function(t){return t()})),this.actionQueue.length=0},e.prototype.onDestroy=function(){this.actionQueue.length=0,this.destroySource()},e.prototype.resetConfig=function(){this.config.seek=0},e.prototype.getCurrentTime=function(){return this.config.loop&&this.duration>0?(this.systemContext.currentTime-this.playTime)%this.duration:this.systemContext.currentTime-this.playTime},e.prototype.createSource=function(){this.systemContext&&"loaded"===this.state&&(this.sourceNode=this.systemContext.createBufferSource(),this.sourceNode.buffer=this.buffer,this.sourceNode.loop=this.config.loop,this.gainNode||(this.gainNode=this.systemContext.createGain(),this.gainNode.connect(this.systemDestination),Object.assign(this,this.config)),this.sourceNode.connect(this.gainNode))},e.prototype.destroySource=function(){this.sourceNode&&(this.sourceNode.removeEventListener("ended",this.endedListener),this.sourceNode.stop(),this.sourceNode.disconnect(),this.sourceNode=null,this.startTime=0,this.playTime=0,this.playing=!1)},e.componentName="Sound",e}(t.Component);exports.Sound=s,exports.SoundSystem=r;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eva/plugin-sound",
3
- "version": "1.2.8-fix.1",
3
+ "version": "1.2.8",
4
4
  "description": "@eva/plugin-sound",
5
5
  "main": "index.js",
6
6
  "module": "dist/plugin-sound.esm.js",
@@ -18,7 +18,7 @@
18
18
  "license": "MIT",
19
19
  "homepage": "https://eva.js.org",
20
20
  "dependencies": {
21
- "@eva/eva.js": "1.2.8-fix.1",
21
+ "@eva/eva.js": "1.2.8",
22
22
  "eventemitter3": "^3.1.2"
23
23
  }
24
24
  }