@eva/plugin-sound 2.0.0-beta.1 → 2.0.0-beta.3
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.
- package/dist/EVA.plugin.sound.js +180 -368
- package/dist/EVA.plugin.sound.min.js +1 -1
- package/dist/plugin-sound.cjs.js +186 -301
- package/dist/plugin-sound.cjs.prod.js +2 -2
- package/dist/plugin-sound.esm.js +187 -302
- package/package.json +2 -2
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("@eva/eva.js")
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("@eva/eva.js");
|
|
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
|
|
15
|
+
***************************************************************************** */function e(t,e,i,s){return new(i||(i=Promise))((function(o,n){function a(t){try{r(s.next(t))}catch(t){n(t)}}function h(t){try{r(s.throw(t))}catch(t){n(t)}}function r(t){t.done?o(t.value):new i((function(e){e(t.value)})).then(a,h)}r((s=s.apply(t,e||[])).next())}))}let i=class extends t.System{constructor(t){super(),this.autoPauseAndStart=!0,this.components=[],this.pausedComponents=[],this.audioBufferCache={},this.decodeAudioPromiseMap={},Object.assign(this,t)}get muted(){return!!this.gainNode&&0===this.gainNode.gain.value}set muted(t){this.gainNode&&this.gainNode.gain.setValueAtTime(t?0:1,0)}get volume(){return this.gainNode?this.gainNode.gain.value:1}set volume(t){!this.gainNode||"number"!=typeof t||t<0||t>1||this.gainNode.gain.setValueAtTime(t,0)}get audioLocked(){return!this.ctx||"running"!==this.ctx.state}resumeAll(){const t=()=>{this.pausedComponents.forEach((t=>{t.play()})),this.pausedComponents=[]};this.ctx.resume().then(t,t)}pauseAll(){this.components.forEach((t=>{t.playing&&(this.pausedComponents.push(t),t.pause())})),this.ctx.suspend().then()}stopAll(){this.components.forEach((t=>{t.playing&&t.stop()})),this.pausedComponents=[],this.ctx.suspend().then()}init(){this.setupAudioContext()}update(){const t=this.componentObserver.clear();for(const e of t)this.componentChanged(e)}onResume(){this.autoPauseAndStart&&this.resumeAll()}onPause(){this.autoPauseAndStart&&this.pauseAll()}onDestroy(){this.components.forEach((t=>{t.onDestroy()})),this.components=[],this.ctx&&(this.gainNode.disconnect(),this.gainNode=null,this.ctx.close(),this.ctx=null)}componentChanged(i){return e(this,void 0,void 0,(function*(){"Sound"===i.componentName&&i.type===t.OBSERVER_TYPE.ADD&&this.add(i)}))}setupAudioContext(){try{const 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())}unlockAudio(){if(!this.ctx||!this.audioLocked)return;const t=()=>{if(this.ctx){const e=()=>{document.body.removeEventListener("touchstart",t),document.body.removeEventListener("touchend",t),document.body.removeEventListener("click",t)};this.ctx.resume().then(e,e)}};document.body.addEventListener("touchstart",t),document.body.addEventListener("touchend",t),document.body.addEventListener("click",t)}add(i){var s;return e(this,void 0,void 0,(function*(){const e=i.component;this.components.push(e);try{const{config:i}=e;e.state="loading";const o=yield t.resource.getResource(i.resource);!this.audioBufferCache[o.name]&&(null===(s=null==o?void 0:o.data)||void 0===s?void 0:s.audio)&&(this.audioBufferCache[o.name]=yield this.decodeAudioData(o.data.audio,o.name)),this.audioBufferCache[o.name]&&(e.systemContext=this.ctx,e.systemDestination=this.gainNode,e.onload(this.audioBufferCache[o.name]))}catch(t){this.onError&&this.onError(t)}}))}decodeAudioData(t,e){if(this.decodeAudioPromiseMap[e])return this.decodeAudioPromiseMap[e];const i=new Promise(((i,s)=>{this.ctx||s(new Error("No audio support"));const o=this.ctx.decodeAudioData(t,(t=>{this.decodeAudioPromiseMap[e]&&delete this.decodeAudioPromiseMap[e],t?i(t):s(new Error(`Error decoding audio ${e}`))}),(i=>{this.decodeAudioPromiseMap[e]&&delete this.decodeAudioPromiseMap[e],s(new Error(`${i}. arrayBuffer byteLength: ${t?t.byteLength:0}`))}));o instanceof Promise&&o.catch((e=>{s(new Error(`catch ${e}, arrayBuffer byteLength: ${t?t.byteLength:0}`))}))}));return this.decodeAudioPromiseMap[e]=i,i}};i.systemName="SoundSystem",i=function(t,e,i,s){var o,n=arguments.length,a=n<3?e:null===s?s=Object.getOwnPropertyDescriptor(e,i):s;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)a=Reflect.decorate(t,e,i,s);else for(var h=t.length-1;h>=0;h--)(o=t[h])&&(a=(n<3?o(a):n>3?o(e,i,a):o(e,i))||a);return n>3&&a&&Object.defineProperty(e,i,a),a}([t.decorators.componentObserver({Sound:[]})],i);var s=i;class o extends t.Component{constructor(){super(...arguments),this.state="unloaded",this.config={resource:"",autoplay:!1,muted:!1,volume:1,loop:!1,seek:0},this.playTime=0,this.startTime=0,this.duration=0,this.actionQueue=[]}get muted(){return!!this.gainNode&&0===this.gainNode.gain.value}set muted(t){this.gainNode&&this.gainNode.gain.setValueAtTime(t?0:this.config.volume,0)}get volume(){return this.gainNode?this.gainNode.gain.value:1}set volume(t){"number"!=typeof t||t<0||t>1||(this.config.volume=t,this.gainNode&&this.gainNode.gain.setValueAtTime(t,0))}init(t){t&&(Object.assign(this.config,t),this.config.autoplay&&this.actionQueue.push(this.play.bind(this)))}play(){if("loaded"!==this.state&&this.actionQueue.push(this.play.bind(this)),this.destroySource(),this.createSource(),!this.sourceNode)return;const t=this.systemContext.currentTime,e=this.config.seek,i=this.config.duration;this.sourceNode.start(0,e,i),this.startTime=t,this.playTime=t-e,this.paused=!1,this.playing=!0,this.resetConfig(),this.endedListener=()=>{this.sourceNode&&(this.config.onEnd&&this.config.onEnd(),this.playing&&this.destroySource())},this.sourceNode.addEventListener("ended",this.endedListener)}pause(){"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())}stop(){"loaded"!==this.state&&this.actionQueue.push(this.stop.bind(this)),(this.paused||this.playing)&&(this.playing=!1,this.paused=!1,this.destroySource(),this.resetConfig())}onload(t){this.state="loaded",this.buffer=t,this.duration=this.buffer.duration,this.actionQueue.forEach((t=>t())),this.actionQueue.length=0}onDestroy(){this.actionQueue.length=0,this.destroySource()}resetConfig(){this.config.seek=0}getCurrentTime(){return this.config.loop&&this.duration>0?(this.systemContext.currentTime-this.playTime)%this.duration:this.systemContext.currentTime-this.playTime}createSource(){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))}destroySource(){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)}}o.componentName="Sound";var n=o;exports.Sound=n,exports.SoundSystem=s;
|