@eva/plugin-sound 1.1.1-fix.0 → 1.1.1-fix.1

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