@eva/plugin-sound 2.0.0-beta.6 → 2.0.0-beta.7
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
CHANGED
|
@@ -1732,21 +1732,28 @@ var _EVA_IIFE_sound = function (exports, eva_js, pixi_js) {
|
|
|
1732
1732
|
speed: 1
|
|
1733
1733
|
};
|
|
1734
1734
|
this.actionQueue = [];
|
|
1735
|
+
this.startTime = 0;
|
|
1736
|
+
}
|
|
1737
|
+
get systemContext() {
|
|
1738
|
+
return sound.context;
|
|
1735
1739
|
}
|
|
1736
1740
|
get playing() {
|
|
1741
|
+
if (!this.buffer) return false;
|
|
1737
1742
|
return this.buffer.isPlaying;
|
|
1738
1743
|
}
|
|
1739
1744
|
get muted() {
|
|
1740
|
-
|
|
1745
|
+
var _a;
|
|
1746
|
+
return ((_a = this.buffer) === null || _a === void 0 ? void 0 : _a.muted) || false;
|
|
1741
1747
|
}
|
|
1742
1748
|
set muted(v) {
|
|
1743
|
-
this.buffer.muted = v;
|
|
1749
|
+
if (this.buffer) this.buffer.muted = v;
|
|
1744
1750
|
}
|
|
1745
1751
|
get volume() {
|
|
1746
|
-
|
|
1752
|
+
var _a;
|
|
1753
|
+
return ((_a = this.buffer) === null || _a === void 0 ? void 0 : _a.volume) || 0;
|
|
1747
1754
|
}
|
|
1748
1755
|
set volume(v) {
|
|
1749
|
-
this.buffer.volume = v;
|
|
1756
|
+
if (this.buffer) this.buffer.volume = v;
|
|
1750
1757
|
}
|
|
1751
1758
|
init(obj) {
|
|
1752
1759
|
if (!obj) {
|
|
@@ -1761,15 +1768,20 @@ var _EVA_IIFE_sound = function (exports, eva_js, pixi_js) {
|
|
|
1761
1768
|
if (this.state !== 'loaded') {
|
|
1762
1769
|
this.actionQueue.push(this.play.bind(this));
|
|
1763
1770
|
}
|
|
1771
|
+
if (!this.buffer) return;
|
|
1772
|
+
this.startTime = this.systemContext.currentTime;
|
|
1764
1773
|
this.buffer.play();
|
|
1765
1774
|
}
|
|
1766
1775
|
resume() {
|
|
1776
|
+
if (!this.buffer) return;
|
|
1767
1777
|
this.buffer.resume();
|
|
1768
1778
|
}
|
|
1769
1779
|
pause() {
|
|
1780
|
+
if (!this.buffer) return;
|
|
1770
1781
|
this.buffer.pause();
|
|
1771
1782
|
}
|
|
1772
1783
|
stop() {
|
|
1784
|
+
if (!this.buffer) return;
|
|
1773
1785
|
this.buffer.stop();
|
|
1774
1786
|
}
|
|
1775
1787
|
onload(buffer) {
|
|
@@ -1784,8 +1796,11 @@ var _EVA_IIFE_sound = function (exports, eva_js, pixi_js) {
|
|
|
1784
1796
|
}
|
|
1785
1797
|
onDestroy() {
|
|
1786
1798
|
this.actionQueue.length = 0;
|
|
1787
|
-
this.
|
|
1788
|
-
this.buffer
|
|
1799
|
+
this.startTime = 0;
|
|
1800
|
+
if (this.buffer) {
|
|
1801
|
+
this.buffer.destroy();
|
|
1802
|
+
this.buffer = null;
|
|
1803
|
+
}
|
|
1789
1804
|
}
|
|
1790
1805
|
}
|
|
1791
1806
|
Sound.componentName = 'Sound';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
function ownKeys(t,e){var s=Object.keys(t);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(t);e&&(i=i.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),s.push.apply(s,i)}return s}function _objectSpread(t){for(var e=1;e<arguments.length;e++){var s=null!=arguments[e]?arguments[e]:{};e%2?ownKeys(Object(s),!0).forEach((function(e){_defineProperty(t,e,s[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(s)):ownKeys(Object(s)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(s,e))}))}return t}function _defineProperty(t,e,s){return(e=_toPropertyKey(e))in t?Object.defineProperty(t,e,{value:s,enumerable:!0,configurable:!0,writable:!0}):t[e]=s,t}function _toPropertyKey(t){var e=_toPrimitive(t,"string");return"symbol"==typeof e?e:e+""}function _toPrimitive(t,e){if("object"!=typeof t||!t)return t;var s=t[Symbol.toPrimitive];if(void 0!==s){var i=s.call(t,e||"default");if("object"!=typeof i)return i;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}function _extends(){return _extends=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var s=arguments[e];for(var i in s)({}).hasOwnProperty.call(s,i)&&(t[i]=s[i])}return t},_extends.apply(null,arguments)}window.EVA=window.EVA||{},window.EVA.plugin=window.EVA.plugin||{};var _EVA_IIFE_sound=function(t,e,s){"use strict";function i(t,e,s,i){return new(s||(s=Promise))((function(o,r){function n(t){try{h(i.next(t))}catch(t){r(t)}}function u(t){try{h(i.throw(t))}catch(t){r(t)}}function h(t){var e;t.done?o(t.value):(e=t.value,e instanceof s?e:new s((function(t){t(e)}))).then(n,u)}h((i=i.apply(t,e||[])).next())}))}let o;function r(){return o}class n{static setParamValue(t,e){if(t.setValueAtTime){const s=r().context;t.setValueAtTime(e,s.audioContext.currentTime)}else t.value=e;return e}}class u extends s.EventEmitter{constructor(){super(...arguments),this.speed=1,this.muted=!1,this.volume=1,this.paused=!1}refresh(){this.emit("refresh")}refreshPaused(){this.emit("refreshPaused")}get filters(){return console.warn("HTML Audio does not support filters"),null}set filters(t){console.warn("HTML Audio does not support filters")}get audioContext(){return console.warn("HTML Audio does not support audioContext"),null}toggleMute(){return this.muted=!this.muted,this.refresh(),this.muted}togglePause(){return this.paused=!this.paused,this.refreshPaused(),this.paused}destroy(){this.removeAllListeners()}}let h=0;const a=class extends s.EventEmitter{constructor(t){super(),this.id=h++,this.init(t)}set(t,e){if(void 0===this[t])throw new Error(`Property with name ${t} does not exist.`);switch(t){case"speed":this.speed=e;break;case"volume":this.volume=e;break;case"paused":this.paused=e;break;case"loop":this.loop=e;break;case"muted":this.muted=e}return this}get progress(){const{currentTime:t}=this._source;return t/this._duration}get paused(){return this._paused}set paused(t){this._paused=t,this.refreshPaused()}_onPlay(){this._playing=!0}_onPause(){this._playing=!1}init(t){this._playing=!1,this._duration=t.source.duration;const e=this._source=t.source.cloneNode(!1);e.src=t.parent.url,e.onplay=this._onPlay.bind(this),e.onpause=this._onPause.bind(this),t.context.on("refresh",this.refresh,this),t.context.on("refreshPaused",this.refreshPaused,this),this._media=t}_internalStop(){this._source&&this._playing&&(this._source.onended=null,this._source.pause())}stop(){this._internalStop(),this._source&&this.emit("stop")}get speed(){return this._speed}set speed(t){this._speed=t,this.refresh()}get volume(){return this._volume}set volume(t){this._volume=t,this.refresh()}get loop(){return this._loop}set loop(t){this._loop=t,this.refresh()}get muted(){return this._muted}set muted(t){this._muted=t,this.refresh()}get filters(){return console.warn("HTML Audio does not support filters"),null}set filters(t){console.warn("HTML Audio does not support filters")}refresh(){const t=this._media.context,e=this._media.parent;this._source.loop=this._loop||e.loop;const s=t.volume*(t.muted?0:1),i=e.volume*(e.muted?0:1),o=this._volume*(this._muted?0:1);this._source.volume=o*s*i,this._source.playbackRate=this._speed*t.speed*e.speed}refreshPaused(){const t=this._media.context,e=this._media.parent,s=this._paused||e.paused||t.paused;s!==this._pausedReal&&(this._pausedReal=s,s?(this._internalStop(),this.emit("paused")):(this.emit("resumed"),this.play({start:this._source.currentTime,end:this._end,volume:this._volume,speed:this._speed,loop:this._loop})),this.emit("pause",s))}play(t){const{start:e,end:i,speed:o,loop:r,volume:n,muted:u}=t;i&&console.assert(i>e,"End time is before start time"),this._speed=o,this._volume=n,this._loop=!!r,this._muted=u,this.refresh(),this.loop&&null!==i&&(console.warn('Looping not support when specifying an "end" time'),this.loop=!1),this._start=e,this._end=i||this._duration,this._start=Math.max(0,this._start-a.PADDING),this._end=Math.min(this._end+a.PADDING,this._duration),this._source.onloadedmetadata=()=>{this._source&&(this._source.currentTime=e,this._source.onloadedmetadata=null,this.emit("progress",e/this._duration,this._duration),s.Ticker.shared.add(this._onUpdate,this))},this._source.onended=this._onComplete.bind(this),this._source.play(),this.emit("start")}_onUpdate(){this.emit("progress",this.progress,this._duration),this._source.currentTime>=this._end&&!this._source.loop&&this._onComplete()}_onComplete(){s.Ticker.shared.remove(this._onUpdate,this),this._internalStop(),this.emit("progress",1,this._duration),this.emit("end",this)}destroy(){s.Ticker.shared.remove(this._onUpdate,this),this.removeAllListeners();const t=this._source;t&&(t.onended=null,t.onplay=null,t.onpause=null,this._internalStop()),this._source=null,this._speed=1,this._volume=1,this._loop=!1,this._end=null,this._start=0,this._duration=0,this._playing=!1,this._pausedReal=!1,this._paused=!1,this._muted=!1,this._media&&(this._media.context.off("refresh",this.refresh,this),this._media.context.off("refreshPaused",this.refreshPaused,this),this._media=null)}toString(){return`[HTMLAudioInstance id=${this.id}]`}};let l=a;l.PADDING=.1;class d extends s.EventEmitter{init(t){this.parent=t,this._source=t.options.source||new Audio,t.url&&(this._source.src=t.url)}create(){return new l(this)}get isPlayable(){return!!this._source&&4===this._source.readyState}get duration(){return this._source.duration}get context(){return this.parent.context}get filters(){return null}set filters(t){console.warn("HTML Audio does not support filters")}destroy(){this.removeAllListeners(),this.parent=null,this._source&&(this._source.src="",this._source.load(),this._source=null)}get source(){return this._source}load(t){const e=this._source,s=this.parent;if(4===e.readyState){s.isLoaded=!0;const e=s.autoPlayStart();return void(t&&setTimeout((()=>{t(null,s,e)}),0))}if(!s.url)return void t(new Error("sound.url or sound.source must be set"));e.src=s.url;const i=()=>{n(),s.isLoaded=!0;const e=s.autoPlayStart();t&&t(null,s,e)},o=()=>{n(),t&&t(new Error("Sound loading has been aborted"))},r=()=>{n();const s=`Failed to load audio element (code: ${e.error.code})`;t?t(new Error(s)):console.error(s)},n=()=>{e.removeEventListener("canplaythrough",i),e.removeEventListener("load",i),e.removeEventListener("abort",o),e.removeEventListener("error",r)};e.addEventListener("canplaythrough",i,!1),e.addEventListener("load",i,!1),e.addEventListener("abort",o,!1),e.addEventListener("error",r,!1),e.load()}}class c{constructor(t,e){this.parent=t,_extends(this,e),this.duration=this.end-this.start,console.assert(this.duration>0,"End time must be after start time")}play(t){return this.parent.play({complete:t,speed:this.speed||this.parent.speed,end:this.end,start:this.start,loop:this.loop})}destroy(){this.parent=null}}const p=["ogg","oga","opus","m4a","mp3","mpeg","wav","aiff","wma","mid","caf"],_=["audio/mpeg","audio/ogg"],f={};!function(t){const e=_objectSpread({m4a:"audio/mp4",oga:"audio/ogg",opus:'audio/ogg; codecs="opus"',caf:'audio/x-caf; codecs="opus"'},t||{}),s=document.createElement("audio"),i={},o=/^no$/;p.forEach((t=>{const r=s.canPlayType(`audio/${t}`).replace(o,""),n=e[t]?s.canPlayType(e[t]).replace(o,""):"";i[t]=!!r||!!n})),_extends(f,i)}();let m=0;class g extends s.EventEmitter{constructor(t){super(),this.id=m++,this._media=null,this._paused=!1,this._muted=!1,this._elapsed=0,this.init(t)}set(t,e){if(void 0===this[t])throw new Error(`Property with name ${t} does not exist.`);switch(t){case"speed":this.speed=e;break;case"volume":this.volume=e;break;case"muted":this.muted=e;break;case"loop":this.loop=e;break;case"paused":this.paused=e}return this}stop(){this._source&&(this._internalStop(),this.emit("stop"))}get speed(){return this._speed}set speed(t){this._speed=t,this.refresh(),this._update(!0)}get volume(){return this._volume}set volume(t){this._volume=t,this.refresh()}get muted(){return this._muted}set muted(t){this._muted=t,this.refresh()}get loop(){return this._loop}set loop(t){this._loop=t,this.refresh()}get filters(){return this._filters}set filters(t){var e;this._filters&&(null===(e=this._filters)||void 0===e||e.filter((t=>t)).forEach((t=>t.disconnect())),this._filters=null,this._source.connect(this._gain));this._filters=null!=t&&t.length?t.slice(0):null,this.refresh()}refresh(){if(!this._source)return;const t=this._media.context,e=this._media.parent;this._source.loop=this._loop||e.loop;const s=t.volume*(t.muted?0:1),i=e.volume*(e.muted?0:1),o=this._volume*(this._muted?0:1);n.setParamValue(this._gain.gain,o*i*s),n.setParamValue(this._source.playbackRate,this._speed*e.speed*t.speed),this.applyFilters()}applyFilters(){var t;if(null!==(t=this._filters)&&void 0!==t&&t.length){this._source.disconnect();let t=this._source;this._filters.forEach((e=>{t.connect(e.destination),t=e})),t.connect(this._gain)}}refreshPaused(){const t=this._media.context,e=this._media.parent,s=this._paused||e.paused||t.paused;s!==this._pausedReal&&(this._pausedReal=s,s?(this._internalStop(),this.emit("paused")):(this.emit("resumed"),this.play({start:this._elapsed%this._duration,end:this._end,speed:this._speed,loop:this._loop,volume:this._volume})),this.emit("pause",s))}play(t){const{start:e,end:s,speed:i,loop:o,volume:r,muted:n,filters:u}=t;s&&console.assert(s>e,"End time is before start time"),this._paused=!1;const{source:h,gain:a}=this._media.nodes.cloneBufferSource();this._source=h,this._gain=a,this._speed=i,this._volume=r,this._loop=!!o,this._muted=n,this._filters=u,this.refresh();const l=this._source.buffer.duration;this._duration=l,this._end=s,this._lastUpdate=this._now(),this._elapsed=e,this._source.onended=this._onComplete.bind(this),this._loop?(this._source.loopEnd=s,this._source.loopStart=e,this._source.start(0,e)):s?this._source.start(0,e,s-e):this._source.start(0,e),this.emit("start"),this._update(!0),this.enableTicker(!0)}enableTicker(t){s.Ticker.shared.remove(this._updateListener,this),t&&s.Ticker.shared.add(this._updateListener,this)}get progress(){return this._progress}get paused(){return this._paused}set paused(t){this._paused=t,this.refreshPaused()}destroy(){var t;this.removeAllListeners(),this._internalStop(),this._gain&&(this._gain.disconnect(),this._gain=null),this._media&&(this._media.context.events.off("refresh",this.refresh,this),this._media.context.events.off("refreshPaused",this.refreshPaused,this),this._media=null),null===(t=this._filters)||void 0===t||t.forEach((t=>t.disconnect())),this._filters=null,this._end=null,this._speed=1,this._volume=1,this._loop=!1,this._elapsed=0,this._duration=0,this._paused=!1,this._muted=!1,this._pausedReal=!1}toString(){return`[WebAudioInstance id=${this.id}]`}_now(){return this._media.context.audioContext.currentTime}_updateListener(){this._update()}_update(t=!1){if(this._source){const e=this._now(),s=e-this._lastUpdate;if(s>0||t){const t=this._source.playbackRate.value;this._elapsed+=s*t,this._lastUpdate=e;const i=this._duration;let o;if(this._source.loopStart){const t=this._source.loopEnd-this._source.loopStart;o=(this._source.loopStart+this._elapsed%t)/i}else o=this._elapsed%i/i;this._progress=o,this.emit("progress",this._progress,i)}}}init(t){this._media=t,t.context.events.on("refresh",this.refresh,this),t.context.events.on("refreshPaused",this.refreshPaused,this)}_internalStop(){if(this._source){this.enableTicker(!1),this._source.onended=null,this._source.stop(0),this._source.disconnect();try{this._source.buffer=null}catch(t){console.warn("Failed to set AudioBufferSourceNode.buffer to null:",t)}this._source=null}}_onComplete(){if(this._source){this.enableTicker(!1),this._source.onended=null,this._source.disconnect();try{this._source.buffer=null}catch(t){console.warn("Failed to set AudioBufferSourceNode.buffer to null:",t)}}this._source=null,this._progress=1,this.emit("progress",1,this._duration),this.emit("end",this)}}class y{constructor(t,e){this._output=e,this._input=t}get destination(){return this._input}get filters(){return this._filters}set filters(t){if(this._filters&&(this._filters.forEach((t=>{t&&t.disconnect()})),this._filters=null,this._input.connect(this._output)),t&&t.length){this._filters=t.slice(0),this._input.disconnect();let e=null;t.forEach((t=>{null===e?this._input.connect(t.destination):e.connect(t.destination),e=t})),e.connect(this._output)}}destroy(){this.filters=null,this._input=null,this._output=null}}const v=class extends y{constructor(t){const e=t.audioContext,s=e.createBufferSource(),i=e.createGain(),o=e.createAnalyser();s.connect(o),o.connect(i),i.connect(t.destination),super(o,i),this.context=t,this.bufferSource=s,this.gain=i,this.analyser=o}get script(){return this._script||(this._script=this.context.audioContext.createScriptProcessor(v.BUFFER_SIZE),this._script.connect(this.context.destination)),this._script}destroy(){super.destroy(),this.bufferSource.disconnect(),this._script&&this._script.disconnect(),this.gain.disconnect(),this.analyser.disconnect(),this.bufferSource=null,this._script=null,this.gain=null,this.analyser=null,this.context=null}cloneBufferSource(){const t=this.bufferSource,e=this.context.audioContext.createBufferSource();e.buffer=t.buffer,n.setParamValue(e.playbackRate,t.playbackRate.value),e.loop=t.loop;const s=this.context.audioContext.createGain();return e.connect(s),s.connect(this.destination),{source:e,gain:s}}get bufferSize(){return this.script.bufferSize}};let b=v;b.BUFFER_SIZE=0;class x{init(t){this.parent=t,this._nodes=new b(this.context),this._source=this._nodes.bufferSource,this.source=t.options.source}destroy(){this.parent=null,this._nodes.destroy(),this._nodes=null;try{this._source.buffer=null}catch(t){console.warn("Failed to set AudioBufferSourceNode.buffer to null:",t)}this._source=null,this.source=null}create(){return new g(this)}get context(){return this.parent.context}get isPlayable(){return!!this._source&&!!this._source.buffer}get filters(){return this._nodes.filters}set filters(t){this._nodes.filters=t}get duration(){return console.assert(this.isPlayable,"Sound not yet playable, no duration"),this._source.buffer.duration}get buffer(){return this._source.buffer}set buffer(t){this._source.buffer=t}get nodes(){return this._nodes}load(t){this.source?this._decode(this.source,t):this.parent.url?this._loadUrl(t):t?t(new Error("sound.url or sound.source must be set")):console.error("sound.url or sound.source must be set")}async _loadUrl(t){const e=this.parent.url,i=await s.DOMAdapter.get().fetch(e);this._decode(await i.arrayBuffer(),t)}_decode(t,e){const s=(t,s)=>{if(t)e&&e(t);else{this.parent.isLoaded=!0,this.buffer=s;const t=this.parent.autoPlayStart();e&&e(null,this.parent,t)}};if(t instanceof AudioBuffer)s(null,t);else{this.parent.context.decode(t,s)}}}const P=class{static from(t){let e={};"string"==typeof t?e.url=t:t instanceof ArrayBuffer||t instanceof AudioBuffer||t instanceof HTMLAudioElement?e.source=t:Array.isArray(t)?e.url=t:e=t,e=_objectSpread({autoPlay:!1,singleInstance:!1,url:null,source:null,preload:!1,volume:1,speed:1,complete:null,loaded:null,loop:!1},e),Object.freeze(e);const s=r().useLegacy?new d:new x;return new P(s,e)}constructor(t,e){this.media=t,this.options=e,this._instances=[],this._sprites={},this.media.init(this);const s=e.complete;this._autoPlayOptions=s?{complete:s}:null,this.isLoaded=!1,this._preloadQueue=null,this.isPlaying=!1,this.autoPlay=e.autoPlay,this.singleInstance=e.singleInstance,this.preload=e.preload||this.autoPlay,this.url=Array.isArray(e.url)?this.preferUrl(e.url):e.url,this.speed=e.speed,this.volume=e.volume,this.loop=e.loop,e.sprites&&this.addSprites(e.sprites),this.preload&&this._preload(e.loaded)}preferUrl(t){const[e]=t.map((t=>({url:t,ext:s.path.extname(t).slice(1)}))).filter((({ext:t})=>f[t])).sort(((t,e)=>p.indexOf(t.ext)-p.indexOf(e.ext)));if(!e)throw new Error("No supported file type found");return e.url}get context(){return r().context}pause(){return this.isPlaying=!1,this.paused=!0,this}resume(){return this.isPlaying=this._instances.length>0,this.paused=!1,this}get paused(){return this._paused}set paused(t){this._paused=t,this.refreshPaused()}get speed(){return this._speed}set speed(t){this._speed=t,this.refresh()}get filters(){return this.media.filters}set filters(t){this.media.filters=t}addSprites(t,e){if("object"==typeof t){const e={};for(const s in t)e[s]=this.addSprites(s,t[s]);return e}console.assert(!this._sprites[t],`Alias ${t} is already taken`);const s=new c(this,e);return this._sprites[t]=s,s}destroy(){this._removeInstances(),this.removeSprites(),this.media.destroy(),this.media=null,this._sprites=null,this._instances=null}removeSprites(t){if(t){const e=this._sprites[t];void 0!==e&&(e.destroy(),delete this._sprites[t])}else for(const t in this._sprites)this.removeSprites(t);return this}get isPlayable(){return this.isLoaded&&this.media&&this.media.isPlayable}stop(){if(!this.isPlayable)return this.autoPlay=!1,this._autoPlayOptions=null,this;this.isPlaying=!1;for(let t=this._instances.length-1;t>=0;t--)this._instances[t].stop();return this}play(t,e){let s;if("string"==typeof t){s={sprite:t,loop:this.loop,complete:e}}else"function"==typeof t?(s={},s.complete=t):s=t;if(s=_objectSpread({complete:null,loaded:null,sprite:null,end:null,start:0,volume:1,speed:1,muted:!1,loop:!1},s||{}),s.sprite){const t=s.sprite;console.assert(!!this._sprites[t],`Alias ${t} is not available`);const e=this._sprites[t];s.start=e.start+(s.start||0),s.end=e.end,s.speed=e.speed||1,s.loop=e.loop||s.loop,delete s.sprite}if(s.offset&&(s.start=s.offset),!this.isLoaded)return this._preloadQueue?new Promise((t=>{this._preloadQueue.push((()=>{t(this.play(s))}))})):(this._preloadQueue=[],this.autoPlay=!0,this._autoPlayOptions=s,new Promise(((t,e)=>{this._preload(((i,o,r)=>{this._preloadQueue.forEach((t=>t())),this._preloadQueue=null,i?e(i):(s.loaded&&s.loaded(i,o,r),t(r))}))})));(this.singleInstance||s.singleInstance)&&this._removeInstances();const i=this._createInstance();return this._instances.push(i),this.isPlaying=!0,i.once("end",(()=>{s.complete&&s.complete(this),this._onComplete(i)})),i.once("stop",(()=>{this._onComplete(i)})),i.play(s),i}refresh(){const t=this._instances.length;for(let e=0;e<t;e++)this._instances[e].refresh()}refreshPaused(){const t=this._instances.length;for(let e=0;e<t;e++)this._instances[e].refreshPaused()}get volume(){return this._volume}set volume(t){this._volume=t,this.refresh()}get muted(){return this._muted}set muted(t){this._muted=t,this.refresh()}get loop(){return this._loop}set loop(t){this._loop=t,this.refresh()}_preload(t){this.media.load(t)}get instances(){return this._instances}get sprites(){return this._sprites}get duration(){return this.media.duration}autoPlayStart(){let t;return this.autoPlay&&(t=this.play(this._autoPlayOptions)),t}_removeInstances(){for(let t=this._instances.length-1;t>=0;t--)this._poolInstance(this._instances[t]);this._instances.length=0}_onComplete(t){if(this._instances){const e=this._instances.indexOf(t);e>-1&&this._instances.splice(e,1),this.isPlaying=this._instances.length>0}this._poolInstance(t)}_createInstance(){if(P._pool.length>0){const t=P._pool.pop();return t.init(this.media),t}return this.media.create()}_poolInstance(t){t.destroy(),P._pool.indexOf(t)<0&&P._pool.push(t)}};let A=P;A._pool=[];class w extends y{constructor(){const t=window,e=new w.AudioContext,i=e.createDynamicsCompressor(),o=e.createAnalyser();o.connect(i),i.connect(e.destination),super(o,i),this.autoPause=!0,this._ctx=e,this._offlineCtx=new w.OfflineAudioContext(1,2,t.OfflineAudioContext?Math.max(8e3,Math.min(96e3,e.sampleRate)):44100),this.compressor=i,this.analyser=o,this.events=new s.EventEmitter,this.volume=1,this.speed=1,this.muted=!1,this.paused=!1,this._locked="suspended"===e.state&&("ontouchstart"in globalThis||"onclick"in globalThis),this._locked&&(this._unlock(),this._unlock=this._unlock.bind(this),document.addEventListener("mousedown",this._unlock,!0),document.addEventListener("touchstart",this._unlock,!0),document.addEventListener("touchend",this._unlock,!0)),this.onFocus=this.onFocus.bind(this),this.onBlur=this.onBlur.bind(this),globalThis.addEventListener("focus",this.onFocus),globalThis.addEventListener("blur",this.onBlur)}onFocus(){if(!this.autoPause)return;const t=this._ctx.state;"suspended"!==t&&"interrupted"!==t&&this._locked||(this.paused=this._pausedOnBlur,this.refreshPaused())}onBlur(){this.autoPause&&(this._locked||(this._pausedOnBlur=this._paused,this.paused=!0,this.refreshPaused()))}_unlock(){this._locked&&(this.playEmptySound(),"running"===this._ctx.state&&(document.removeEventListener("mousedown",this._unlock,!0),document.removeEventListener("touchend",this._unlock,!0),document.removeEventListener("touchstart",this._unlock,!0),this._locked=!1))}playEmptySound(){const t=this._ctx.createBufferSource();t.buffer=this._ctx.createBuffer(1,1,22050),t.connect(this._ctx.destination),t.start(0,0,0),"suspended"===t.context.state&&t.context.resume()}static get AudioContext(){const t=window;return t.AudioContext||t.webkitAudioContext||null}static get OfflineAudioContext(){const t=window;return t.OfflineAudioContext||t.webkitOfflineAudioContext||null}destroy(){super.destroy();const t=this._ctx;void 0!==t.close&&t.close(),globalThis.removeEventListener("focus",this.onFocus),globalThis.removeEventListener("blur",this.onBlur),this.events.removeAllListeners(),this.analyser.disconnect(),this.compressor.disconnect(),this.analyser=null,this.compressor=null,this.events=null,this._offlineCtx=null,this._ctx=null}get audioContext(){return this._ctx}get offlineContext(){return this._offlineCtx}set paused(t){t&&"running"===this._ctx.state?this._ctx.suspend():t||"suspended"!==this._ctx.state||this._ctx.resume(),this._paused=t}get paused(){return this._paused}refresh(){this.events.emit("refresh")}refreshPaused(){this.events.emit("refreshPaused")}toggleMute(){return this.muted=!this.muted,this.refresh(),this.muted}togglePause(){return this.paused=!this.paused,this.refreshPaused(),this._paused}decode(t,e){const s=t=>{e(new Error((null==t?void 0:t.message)||"Unable to decode file"))},i=this._offlineCtx.decodeAudioData(t,(t=>{e(null,t)}),s);i&&i.catch(s)}}const E=t=>{var e;const i=t.src;let o=null==t||null===(e=t.alias)||void 0===e?void 0:e[0];return o&&t.src!==o||(o=s.path.basename(i,s.path.extname(i))),o},S={extension:s.ExtensionType.Asset,detection:{test:async()=>!0,add:async t=>[...t,...p.filter((t=>f[t]))],remove:async t=>t.filter((e=>t.includes(e)))},loader:{name:"sound",extension:{type:[s.ExtensionType.LoadParser],priority:s.LoaderParserPriority.High},test(t){const e=s.path.extname(t).slice(1);return!!f[e]||_.some((e=>t.startsWith(`data:${e}`)))},async load(t,e){const s=await new Promise(((s,i)=>A.from(_objectSpread(_objectSpread({},e.data),{},{url:t,preload:!0,loaded(t,o){var r,n;t?i(t):s(o),null===(r=e.data)||void 0===r||null===(n=r.loaded)||void 0===n||n.call(r,t,o)}}))));return r().add(E(e),s),s},async unload(t,e){r().remove(E(e))}}};s.extensions.add(S);const C=function(t){return o=t,t}(new class{constructor(){this.init()}init(){return this.supported&&(this._webAudioContext=new w),this._htmlAudioContext=new u,this._sounds={},this.useLegacy=!this.supported,this}get context(){return this._context}get filtersAll(){return this.useLegacy?[]:this._context.filters}set filtersAll(t){this.useLegacy||(this._context.filters=t)}get supported(){return null!==w.AudioContext}add(t,e){if("object"==typeof t){const s={};for(const i in t){const o=this._getOptions(t[i],e);s[i]=this.add(i,o)}return s}if(console.assert(!this._sounds[t],`Sound with alias ${t} already exists.`),e instanceof A)return this._sounds[t]=e,e;const s=this._getOptions(e),i=A.from(s);return this._sounds[t]=i,i}_getOptions(t,e){let s;return s="string"==typeof t||Array.isArray(t)?{url:t}:t instanceof ArrayBuffer||t instanceof AudioBuffer||t instanceof HTMLAudioElement?{source:t}:t,s=_objectSpread(_objectSpread({},s),e||{}),s}get useLegacy(){return this._useLegacy}set useLegacy(t){this._useLegacy=t,this._context=!t&&this.supported?this._webAudioContext:this._htmlAudioContext}get disableAutoPause(){return!this._webAudioContext.autoPause}set disableAutoPause(t){this._webAudioContext.autoPause=!t}remove(t){return this.exists(t,!0),this._sounds[t].destroy(),delete this._sounds[t],this}get volumeAll(){return this._context.volume}set volumeAll(t){this._context.volume=t,this._context.refresh()}get speedAll(){return this._context.speed}set speedAll(t){this._context.speed=t,this._context.refresh()}togglePauseAll(){return this._context.togglePause()}pauseAll(){return this._context.paused=!0,this._context.refreshPaused(),this}resumeAll(){return this._context.paused=!1,this._context.refreshPaused(),this}toggleMuteAll(){return this._context.toggleMute()}muteAll(){return this._context.muted=!0,this._context.refresh(),this}unmuteAll(){return this._context.muted=!1,this._context.refresh(),this}removeAll(){for(const t in this._sounds)this._sounds[t].destroy(),delete this._sounds[t];return this}stopAll(){for(const t in this._sounds)this._sounds[t].stop();return this}exists(t,e=!1){const s=!!this._sounds[t];return e&&console.assert(s,`No sound matching alias '${t}'.`),s}isPlaying(){for(const t in this._sounds)if(this._sounds[t].isPlaying)return!0;return!1}find(t){return this.exists(t,!0),this._sounds[t]}play(t,e){return this.find(t).play(e)}stop(t){return this.find(t).stop()}pause(t){return this.find(t).pause()}resume(t){return this.find(t).resume()}volume(t,e){const s=this.find(t);return void 0!==e&&(s.volume=e),s.volume}speed(t,e){const s=this.find(t);return void 0!==e&&(s.speed=e),s.speed}duration(t){return this.find(t).duration}close(){return this.removeAll(),this._sounds=null,this._webAudioContext&&(this._webAudioContext.destroy(),this._webAudioContext=null),this._htmlAudioContext&&(this._htmlAudioContext.destroy(),this._htmlAudioContext=null),this._context=null,this}});s.extensions.add(S);let L=class extends e.System{constructor(t){super(),this.autoPauseAndStart=!0,this.components=[],this.audioBufferCache={},_extends(this,t)}resumeAll(){C.resumeAll()}pauseAll(){C.pauseAll()}stopAll(){C.stopAll()}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=[],C.removeAll()}componentChanged(t){return i(this,void 0,void 0,(function*(){"Sound"===t.componentName&&t.type===e.OBSERVER_TYPE.ADD&&this.add(t)}))}add(t){var s,o;return i(this,void 0,void 0,(function*(){const i=t.component;this.components.push(i);try{const{config:t}=i;i.state="loading";const r=yield e.resource.getResource(t.resource);!this.audioBufferCache[r.name]&&(null===(s=null==r?void 0:r.data)||void 0===s?void 0:s.audio)&&(this.audioBufferCache[r.name]=null===(o=null==r?void 0:r.data)||void 0===o?void 0:o.audio),this.audioBufferCache[r.name]&&i.onload(this.audioBufferCache[r.name])}catch(t){this.onError&&this.onError(t)}}))}};L.systemName="SoundSystem",L=function(t,e,s,i){var o,r=arguments.length,n=r<3?e:null===i?i=Object.getOwnPropertyDescriptor(e,s):i;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)n=Reflect.decorate(t,e,s,i);else for(var u=t.length-1;u>=0;u--)(o=t[u])&&(n=(r<3?o(n):r>3?o(e,s,n):o(e,s))||n);return r>3&&n&&Object.defineProperty(e,s,n),n}([e.decorators.componentObserver({Sound:[]})],L);var O=L;class k extends e.Component{constructor(){super(...arguments),this.state="unloaded",this.config={resource:"",autoplay:!1,muted:!1,volume:1,loop:!1,seek:0,speed:1},this.actionQueue=[]}get playing(){return this.buffer.isPlaying}get muted(){return this.buffer.muted}set muted(t){this.buffer.muted=t}get volume(){return this.buffer.volume}set volume(t){this.buffer.volume=t}init(t){t&&(_extends(this.config,t),this.config.autoplay&&this.actionQueue.push(this.play.bind(this)))}play(){"loaded"!==this.state&&this.actionQueue.push(this.play.bind(this)),this.buffer.play()}resume(){this.buffer.resume()}pause(){this.buffer.pause()}stop(){this.buffer.stop()}onload(t){this.state="loaded",this.buffer=t,this.buffer.muted=this.config.muted,this.buffer.volume=this.config.volume,this.buffer.loop=this.config.loop,this.buffer.speed=this.config.speed,this.actionQueue.forEach((t=>t())),this.actionQueue.length=0}onDestroy(){this.actionQueue.length=0,this.buffer.destroy(),this.buffer=null}}k.componentName="Sound";var T=k;return t.Sound=T,t.SoundSystem=O,Object.defineProperty(t,"__esModule",{value:!0}),t}({},EVA,PIXI);window.EVA.plugin.sound=window.EVA.plugin.sound||_EVA_IIFE_sound;
|
|
1
|
+
function ownKeys(t,e){var s=Object.keys(t);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(t);e&&(i=i.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),s.push.apply(s,i)}return s}function _objectSpread(t){for(var e=1;e<arguments.length;e++){var s=null!=arguments[e]?arguments[e]:{};e%2?ownKeys(Object(s),!0).forEach((function(e){_defineProperty(t,e,s[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(s)):ownKeys(Object(s)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(s,e))}))}return t}function _defineProperty(t,e,s){return(e=_toPropertyKey(e))in t?Object.defineProperty(t,e,{value:s,enumerable:!0,configurable:!0,writable:!0}):t[e]=s,t}function _toPropertyKey(t){var e=_toPrimitive(t,"string");return"symbol"==typeof e?e:e+""}function _toPrimitive(t,e){if("object"!=typeof t||!t)return t;var s=t[Symbol.toPrimitive];if(void 0!==s){var i=s.call(t,e||"default");if("object"!=typeof i)return i;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}function _extends(){return _extends=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var s=arguments[e];for(var i in s)({}).hasOwnProperty.call(s,i)&&(t[i]=s[i])}return t},_extends.apply(null,arguments)}window.EVA=window.EVA||{},window.EVA.plugin=window.EVA.plugin||{};var _EVA_IIFE_sound=function(t,e,s){"use strict";function i(t,e,s,i){return new(s||(s=Promise))((function(o,r){function n(t){try{h(i.next(t))}catch(t){r(t)}}function u(t){try{h(i.throw(t))}catch(t){r(t)}}function h(t){var e;t.done?o(t.value):(e=t.value,e instanceof s?e:new s((function(t){t(e)}))).then(n,u)}h((i=i.apply(t,e||[])).next())}))}let o;function r(){return o}class n{static setParamValue(t,e){if(t.setValueAtTime){const s=r().context;t.setValueAtTime(e,s.audioContext.currentTime)}else t.value=e;return e}}class u extends s.EventEmitter{constructor(){super(...arguments),this.speed=1,this.muted=!1,this.volume=1,this.paused=!1}refresh(){this.emit("refresh")}refreshPaused(){this.emit("refreshPaused")}get filters(){return console.warn("HTML Audio does not support filters"),null}set filters(t){console.warn("HTML Audio does not support filters")}get audioContext(){return console.warn("HTML Audio does not support audioContext"),null}toggleMute(){return this.muted=!this.muted,this.refresh(),this.muted}togglePause(){return this.paused=!this.paused,this.refreshPaused(),this.paused}destroy(){this.removeAllListeners()}}let h=0;const a=class extends s.EventEmitter{constructor(t){super(),this.id=h++,this.init(t)}set(t,e){if(void 0===this[t])throw new Error(`Property with name ${t} does not exist.`);switch(t){case"speed":this.speed=e;break;case"volume":this.volume=e;break;case"paused":this.paused=e;break;case"loop":this.loop=e;break;case"muted":this.muted=e}return this}get progress(){const{currentTime:t}=this._source;return t/this._duration}get paused(){return this._paused}set paused(t){this._paused=t,this.refreshPaused()}_onPlay(){this._playing=!0}_onPause(){this._playing=!1}init(t){this._playing=!1,this._duration=t.source.duration;const e=this._source=t.source.cloneNode(!1);e.src=t.parent.url,e.onplay=this._onPlay.bind(this),e.onpause=this._onPause.bind(this),t.context.on("refresh",this.refresh,this),t.context.on("refreshPaused",this.refreshPaused,this),this._media=t}_internalStop(){this._source&&this._playing&&(this._source.onended=null,this._source.pause())}stop(){this._internalStop(),this._source&&this.emit("stop")}get speed(){return this._speed}set speed(t){this._speed=t,this.refresh()}get volume(){return this._volume}set volume(t){this._volume=t,this.refresh()}get loop(){return this._loop}set loop(t){this._loop=t,this.refresh()}get muted(){return this._muted}set muted(t){this._muted=t,this.refresh()}get filters(){return console.warn("HTML Audio does not support filters"),null}set filters(t){console.warn("HTML Audio does not support filters")}refresh(){const t=this._media.context,e=this._media.parent;this._source.loop=this._loop||e.loop;const s=t.volume*(t.muted?0:1),i=e.volume*(e.muted?0:1),o=this._volume*(this._muted?0:1);this._source.volume=o*s*i,this._source.playbackRate=this._speed*t.speed*e.speed}refreshPaused(){const t=this._media.context,e=this._media.parent,s=this._paused||e.paused||t.paused;s!==this._pausedReal&&(this._pausedReal=s,s?(this._internalStop(),this.emit("paused")):(this.emit("resumed"),this.play({start:this._source.currentTime,end:this._end,volume:this._volume,speed:this._speed,loop:this._loop})),this.emit("pause",s))}play(t){const{start:e,end:i,speed:o,loop:r,volume:n,muted:u}=t;i&&console.assert(i>e,"End time is before start time"),this._speed=o,this._volume=n,this._loop=!!r,this._muted=u,this.refresh(),this.loop&&null!==i&&(console.warn('Looping not support when specifying an "end" time'),this.loop=!1),this._start=e,this._end=i||this._duration,this._start=Math.max(0,this._start-a.PADDING),this._end=Math.min(this._end+a.PADDING,this._duration),this._source.onloadedmetadata=()=>{this._source&&(this._source.currentTime=e,this._source.onloadedmetadata=null,this.emit("progress",e/this._duration,this._duration),s.Ticker.shared.add(this._onUpdate,this))},this._source.onended=this._onComplete.bind(this),this._source.play(),this.emit("start")}_onUpdate(){this.emit("progress",this.progress,this._duration),this._source.currentTime>=this._end&&!this._source.loop&&this._onComplete()}_onComplete(){s.Ticker.shared.remove(this._onUpdate,this),this._internalStop(),this.emit("progress",1,this._duration),this.emit("end",this)}destroy(){s.Ticker.shared.remove(this._onUpdate,this),this.removeAllListeners();const t=this._source;t&&(t.onended=null,t.onplay=null,t.onpause=null,this._internalStop()),this._source=null,this._speed=1,this._volume=1,this._loop=!1,this._end=null,this._start=0,this._duration=0,this._playing=!1,this._pausedReal=!1,this._paused=!1,this._muted=!1,this._media&&(this._media.context.off("refresh",this.refresh,this),this._media.context.off("refreshPaused",this.refreshPaused,this),this._media=null)}toString(){return`[HTMLAudioInstance id=${this.id}]`}};let l=a;l.PADDING=.1;class d extends s.EventEmitter{init(t){this.parent=t,this._source=t.options.source||new Audio,t.url&&(this._source.src=t.url)}create(){return new l(this)}get isPlayable(){return!!this._source&&4===this._source.readyState}get duration(){return this._source.duration}get context(){return this.parent.context}get filters(){return null}set filters(t){console.warn("HTML Audio does not support filters")}destroy(){this.removeAllListeners(),this.parent=null,this._source&&(this._source.src="",this._source.load(),this._source=null)}get source(){return this._source}load(t){const e=this._source,s=this.parent;if(4===e.readyState){s.isLoaded=!0;const e=s.autoPlayStart();return void(t&&setTimeout((()=>{t(null,s,e)}),0))}if(!s.url)return void t(new Error("sound.url or sound.source must be set"));e.src=s.url;const i=()=>{n(),s.isLoaded=!0;const e=s.autoPlayStart();t&&t(null,s,e)},o=()=>{n(),t&&t(new Error("Sound loading has been aborted"))},r=()=>{n();const s=`Failed to load audio element (code: ${e.error.code})`;t?t(new Error(s)):console.error(s)},n=()=>{e.removeEventListener("canplaythrough",i),e.removeEventListener("load",i),e.removeEventListener("abort",o),e.removeEventListener("error",r)};e.addEventListener("canplaythrough",i,!1),e.addEventListener("load",i,!1),e.addEventListener("abort",o,!1),e.addEventListener("error",r,!1),e.load()}}class c{constructor(t,e){this.parent=t,_extends(this,e),this.duration=this.end-this.start,console.assert(this.duration>0,"End time must be after start time")}play(t){return this.parent.play({complete:t,speed:this.speed||this.parent.speed,end:this.end,start:this.start,loop:this.loop})}destroy(){this.parent=null}}const p=["ogg","oga","opus","m4a","mp3","mpeg","wav","aiff","wma","mid","caf"],_=["audio/mpeg","audio/ogg"],f={};!function(t){const e=_objectSpread({m4a:"audio/mp4",oga:"audio/ogg",opus:'audio/ogg; codecs="opus"',caf:'audio/x-caf; codecs="opus"'},t||{}),s=document.createElement("audio"),i={},o=/^no$/;p.forEach((t=>{const r=s.canPlayType(`audio/${t}`).replace(o,""),n=e[t]?s.canPlayType(e[t]).replace(o,""):"";i[t]=!!r||!!n})),_extends(f,i)}();let m=0;class g extends s.EventEmitter{constructor(t){super(),this.id=m++,this._media=null,this._paused=!1,this._muted=!1,this._elapsed=0,this.init(t)}set(t,e){if(void 0===this[t])throw new Error(`Property with name ${t} does not exist.`);switch(t){case"speed":this.speed=e;break;case"volume":this.volume=e;break;case"muted":this.muted=e;break;case"loop":this.loop=e;break;case"paused":this.paused=e}return this}stop(){this._source&&(this._internalStop(),this.emit("stop"))}get speed(){return this._speed}set speed(t){this._speed=t,this.refresh(),this._update(!0)}get volume(){return this._volume}set volume(t){this._volume=t,this.refresh()}get muted(){return this._muted}set muted(t){this._muted=t,this.refresh()}get loop(){return this._loop}set loop(t){this._loop=t,this.refresh()}get filters(){return this._filters}set filters(t){var e;this._filters&&(null===(e=this._filters)||void 0===e||e.filter((t=>t)).forEach((t=>t.disconnect())),this._filters=null,this._source.connect(this._gain));this._filters=null!=t&&t.length?t.slice(0):null,this.refresh()}refresh(){if(!this._source)return;const t=this._media.context,e=this._media.parent;this._source.loop=this._loop||e.loop;const s=t.volume*(t.muted?0:1),i=e.volume*(e.muted?0:1),o=this._volume*(this._muted?0:1);n.setParamValue(this._gain.gain,o*i*s),n.setParamValue(this._source.playbackRate,this._speed*e.speed*t.speed),this.applyFilters()}applyFilters(){var t;if(null!==(t=this._filters)&&void 0!==t&&t.length){this._source.disconnect();let t=this._source;this._filters.forEach((e=>{t.connect(e.destination),t=e})),t.connect(this._gain)}}refreshPaused(){const t=this._media.context,e=this._media.parent,s=this._paused||e.paused||t.paused;s!==this._pausedReal&&(this._pausedReal=s,s?(this._internalStop(),this.emit("paused")):(this.emit("resumed"),this.play({start:this._elapsed%this._duration,end:this._end,speed:this._speed,loop:this._loop,volume:this._volume})),this.emit("pause",s))}play(t){const{start:e,end:s,speed:i,loop:o,volume:r,muted:n,filters:u}=t;s&&console.assert(s>e,"End time is before start time"),this._paused=!1;const{source:h,gain:a}=this._media.nodes.cloneBufferSource();this._source=h,this._gain=a,this._speed=i,this._volume=r,this._loop=!!o,this._muted=n,this._filters=u,this.refresh();const l=this._source.buffer.duration;this._duration=l,this._end=s,this._lastUpdate=this._now(),this._elapsed=e,this._source.onended=this._onComplete.bind(this),this._loop?(this._source.loopEnd=s,this._source.loopStart=e,this._source.start(0,e)):s?this._source.start(0,e,s-e):this._source.start(0,e),this.emit("start"),this._update(!0),this.enableTicker(!0)}enableTicker(t){s.Ticker.shared.remove(this._updateListener,this),t&&s.Ticker.shared.add(this._updateListener,this)}get progress(){return this._progress}get paused(){return this._paused}set paused(t){this._paused=t,this.refreshPaused()}destroy(){var t;this.removeAllListeners(),this._internalStop(),this._gain&&(this._gain.disconnect(),this._gain=null),this._media&&(this._media.context.events.off("refresh",this.refresh,this),this._media.context.events.off("refreshPaused",this.refreshPaused,this),this._media=null),null===(t=this._filters)||void 0===t||t.forEach((t=>t.disconnect())),this._filters=null,this._end=null,this._speed=1,this._volume=1,this._loop=!1,this._elapsed=0,this._duration=0,this._paused=!1,this._muted=!1,this._pausedReal=!1}toString(){return`[WebAudioInstance id=${this.id}]`}_now(){return this._media.context.audioContext.currentTime}_updateListener(){this._update()}_update(t=!1){if(this._source){const e=this._now(),s=e-this._lastUpdate;if(s>0||t){const t=this._source.playbackRate.value;this._elapsed+=s*t,this._lastUpdate=e;const i=this._duration;let o;if(this._source.loopStart){const t=this._source.loopEnd-this._source.loopStart;o=(this._source.loopStart+this._elapsed%t)/i}else o=this._elapsed%i/i;this._progress=o,this.emit("progress",this._progress,i)}}}init(t){this._media=t,t.context.events.on("refresh",this.refresh,this),t.context.events.on("refreshPaused",this.refreshPaused,this)}_internalStop(){if(this._source){this.enableTicker(!1),this._source.onended=null,this._source.stop(0),this._source.disconnect();try{this._source.buffer=null}catch(t){console.warn("Failed to set AudioBufferSourceNode.buffer to null:",t)}this._source=null}}_onComplete(){if(this._source){this.enableTicker(!1),this._source.onended=null,this._source.disconnect();try{this._source.buffer=null}catch(t){console.warn("Failed to set AudioBufferSourceNode.buffer to null:",t)}}this._source=null,this._progress=1,this.emit("progress",1,this._duration),this.emit("end",this)}}class y{constructor(t,e){this._output=e,this._input=t}get destination(){return this._input}get filters(){return this._filters}set filters(t){if(this._filters&&(this._filters.forEach((t=>{t&&t.disconnect()})),this._filters=null,this._input.connect(this._output)),t&&t.length){this._filters=t.slice(0),this._input.disconnect();let e=null;t.forEach((t=>{null===e?this._input.connect(t.destination):e.connect(t.destination),e=t})),e.connect(this._output)}}destroy(){this.filters=null,this._input=null,this._output=null}}const v=class extends y{constructor(t){const e=t.audioContext,s=e.createBufferSource(),i=e.createGain(),o=e.createAnalyser();s.connect(o),o.connect(i),i.connect(t.destination),super(o,i),this.context=t,this.bufferSource=s,this.gain=i,this.analyser=o}get script(){return this._script||(this._script=this.context.audioContext.createScriptProcessor(v.BUFFER_SIZE),this._script.connect(this.context.destination)),this._script}destroy(){super.destroy(),this.bufferSource.disconnect(),this._script&&this._script.disconnect(),this.gain.disconnect(),this.analyser.disconnect(),this.bufferSource=null,this._script=null,this.gain=null,this.analyser=null,this.context=null}cloneBufferSource(){const t=this.bufferSource,e=this.context.audioContext.createBufferSource();e.buffer=t.buffer,n.setParamValue(e.playbackRate,t.playbackRate.value),e.loop=t.loop;const s=this.context.audioContext.createGain();return e.connect(s),s.connect(this.destination),{source:e,gain:s}}get bufferSize(){return this.script.bufferSize}};let b=v;b.BUFFER_SIZE=0;class x{init(t){this.parent=t,this._nodes=new b(this.context),this._source=this._nodes.bufferSource,this.source=t.options.source}destroy(){this.parent=null,this._nodes.destroy(),this._nodes=null;try{this._source.buffer=null}catch(t){console.warn("Failed to set AudioBufferSourceNode.buffer to null:",t)}this._source=null,this.source=null}create(){return new g(this)}get context(){return this.parent.context}get isPlayable(){return!!this._source&&!!this._source.buffer}get filters(){return this._nodes.filters}set filters(t){this._nodes.filters=t}get duration(){return console.assert(this.isPlayable,"Sound not yet playable, no duration"),this._source.buffer.duration}get buffer(){return this._source.buffer}set buffer(t){this._source.buffer=t}get nodes(){return this._nodes}load(t){this.source?this._decode(this.source,t):this.parent.url?this._loadUrl(t):t?t(new Error("sound.url or sound.source must be set")):console.error("sound.url or sound.source must be set")}async _loadUrl(t){const e=this.parent.url,i=await s.DOMAdapter.get().fetch(e);this._decode(await i.arrayBuffer(),t)}_decode(t,e){const s=(t,s)=>{if(t)e&&e(t);else{this.parent.isLoaded=!0,this.buffer=s;const t=this.parent.autoPlayStart();e&&e(null,this.parent,t)}};if(t instanceof AudioBuffer)s(null,t);else{this.parent.context.decode(t,s)}}}const P=class{static from(t){let e={};"string"==typeof t?e.url=t:t instanceof ArrayBuffer||t instanceof AudioBuffer||t instanceof HTMLAudioElement?e.source=t:Array.isArray(t)?e.url=t:e=t,e=_objectSpread({autoPlay:!1,singleInstance:!1,url:null,source:null,preload:!1,volume:1,speed:1,complete:null,loaded:null,loop:!1},e),Object.freeze(e);const s=r().useLegacy?new d:new x;return new P(s,e)}constructor(t,e){this.media=t,this.options=e,this._instances=[],this._sprites={},this.media.init(this);const s=e.complete;this._autoPlayOptions=s?{complete:s}:null,this.isLoaded=!1,this._preloadQueue=null,this.isPlaying=!1,this.autoPlay=e.autoPlay,this.singleInstance=e.singleInstance,this.preload=e.preload||this.autoPlay,this.url=Array.isArray(e.url)?this.preferUrl(e.url):e.url,this.speed=e.speed,this.volume=e.volume,this.loop=e.loop,e.sprites&&this.addSprites(e.sprites),this.preload&&this._preload(e.loaded)}preferUrl(t){const[e]=t.map((t=>({url:t,ext:s.path.extname(t).slice(1)}))).filter((({ext:t})=>f[t])).sort(((t,e)=>p.indexOf(t.ext)-p.indexOf(e.ext)));if(!e)throw new Error("No supported file type found");return e.url}get context(){return r().context}pause(){return this.isPlaying=!1,this.paused=!0,this}resume(){return this.isPlaying=this._instances.length>0,this.paused=!1,this}get paused(){return this._paused}set paused(t){this._paused=t,this.refreshPaused()}get speed(){return this._speed}set speed(t){this._speed=t,this.refresh()}get filters(){return this.media.filters}set filters(t){this.media.filters=t}addSprites(t,e){if("object"==typeof t){const e={};for(const s in t)e[s]=this.addSprites(s,t[s]);return e}console.assert(!this._sprites[t],`Alias ${t} is already taken`);const s=new c(this,e);return this._sprites[t]=s,s}destroy(){this._removeInstances(),this.removeSprites(),this.media.destroy(),this.media=null,this._sprites=null,this._instances=null}removeSprites(t){if(t){const e=this._sprites[t];void 0!==e&&(e.destroy(),delete this._sprites[t])}else for(const t in this._sprites)this.removeSprites(t);return this}get isPlayable(){return this.isLoaded&&this.media&&this.media.isPlayable}stop(){if(!this.isPlayable)return this.autoPlay=!1,this._autoPlayOptions=null,this;this.isPlaying=!1;for(let t=this._instances.length-1;t>=0;t--)this._instances[t].stop();return this}play(t,e){let s;if("string"==typeof t){s={sprite:t,loop:this.loop,complete:e}}else"function"==typeof t?(s={},s.complete=t):s=t;if(s=_objectSpread({complete:null,loaded:null,sprite:null,end:null,start:0,volume:1,speed:1,muted:!1,loop:!1},s||{}),s.sprite){const t=s.sprite;console.assert(!!this._sprites[t],`Alias ${t} is not available`);const e=this._sprites[t];s.start=e.start+(s.start||0),s.end=e.end,s.speed=e.speed||1,s.loop=e.loop||s.loop,delete s.sprite}if(s.offset&&(s.start=s.offset),!this.isLoaded)return this._preloadQueue?new Promise((t=>{this._preloadQueue.push((()=>{t(this.play(s))}))})):(this._preloadQueue=[],this.autoPlay=!0,this._autoPlayOptions=s,new Promise(((t,e)=>{this._preload(((i,o,r)=>{this._preloadQueue.forEach((t=>t())),this._preloadQueue=null,i?e(i):(s.loaded&&s.loaded(i,o,r),t(r))}))})));(this.singleInstance||s.singleInstance)&&this._removeInstances();const i=this._createInstance();return this._instances.push(i),this.isPlaying=!0,i.once("end",(()=>{s.complete&&s.complete(this),this._onComplete(i)})),i.once("stop",(()=>{this._onComplete(i)})),i.play(s),i}refresh(){const t=this._instances.length;for(let e=0;e<t;e++)this._instances[e].refresh()}refreshPaused(){const t=this._instances.length;for(let e=0;e<t;e++)this._instances[e].refreshPaused()}get volume(){return this._volume}set volume(t){this._volume=t,this.refresh()}get muted(){return this._muted}set muted(t){this._muted=t,this.refresh()}get loop(){return this._loop}set loop(t){this._loop=t,this.refresh()}_preload(t){this.media.load(t)}get instances(){return this._instances}get sprites(){return this._sprites}get duration(){return this.media.duration}autoPlayStart(){let t;return this.autoPlay&&(t=this.play(this._autoPlayOptions)),t}_removeInstances(){for(let t=this._instances.length-1;t>=0;t--)this._poolInstance(this._instances[t]);this._instances.length=0}_onComplete(t){if(this._instances){const e=this._instances.indexOf(t);e>-1&&this._instances.splice(e,1),this.isPlaying=this._instances.length>0}this._poolInstance(t)}_createInstance(){if(P._pool.length>0){const t=P._pool.pop();return t.init(this.media),t}return this.media.create()}_poolInstance(t){t.destroy(),P._pool.indexOf(t)<0&&P._pool.push(t)}};let A=P;A._pool=[];class w extends y{constructor(){const t=window,e=new w.AudioContext,i=e.createDynamicsCompressor(),o=e.createAnalyser();o.connect(i),i.connect(e.destination),super(o,i),this.autoPause=!0,this._ctx=e,this._offlineCtx=new w.OfflineAudioContext(1,2,t.OfflineAudioContext?Math.max(8e3,Math.min(96e3,e.sampleRate)):44100),this.compressor=i,this.analyser=o,this.events=new s.EventEmitter,this.volume=1,this.speed=1,this.muted=!1,this.paused=!1,this._locked="suspended"===e.state&&("ontouchstart"in globalThis||"onclick"in globalThis),this._locked&&(this._unlock(),this._unlock=this._unlock.bind(this),document.addEventListener("mousedown",this._unlock,!0),document.addEventListener("touchstart",this._unlock,!0),document.addEventListener("touchend",this._unlock,!0)),this.onFocus=this.onFocus.bind(this),this.onBlur=this.onBlur.bind(this),globalThis.addEventListener("focus",this.onFocus),globalThis.addEventListener("blur",this.onBlur)}onFocus(){if(!this.autoPause)return;const t=this._ctx.state;"suspended"!==t&&"interrupted"!==t&&this._locked||(this.paused=this._pausedOnBlur,this.refreshPaused())}onBlur(){this.autoPause&&(this._locked||(this._pausedOnBlur=this._paused,this.paused=!0,this.refreshPaused()))}_unlock(){this._locked&&(this.playEmptySound(),"running"===this._ctx.state&&(document.removeEventListener("mousedown",this._unlock,!0),document.removeEventListener("touchend",this._unlock,!0),document.removeEventListener("touchstart",this._unlock,!0),this._locked=!1))}playEmptySound(){const t=this._ctx.createBufferSource();t.buffer=this._ctx.createBuffer(1,1,22050),t.connect(this._ctx.destination),t.start(0,0,0),"suspended"===t.context.state&&t.context.resume()}static get AudioContext(){const t=window;return t.AudioContext||t.webkitAudioContext||null}static get OfflineAudioContext(){const t=window;return t.OfflineAudioContext||t.webkitOfflineAudioContext||null}destroy(){super.destroy();const t=this._ctx;void 0!==t.close&&t.close(),globalThis.removeEventListener("focus",this.onFocus),globalThis.removeEventListener("blur",this.onBlur),this.events.removeAllListeners(),this.analyser.disconnect(),this.compressor.disconnect(),this.analyser=null,this.compressor=null,this.events=null,this._offlineCtx=null,this._ctx=null}get audioContext(){return this._ctx}get offlineContext(){return this._offlineCtx}set paused(t){t&&"running"===this._ctx.state?this._ctx.suspend():t||"suspended"!==this._ctx.state||this._ctx.resume(),this._paused=t}get paused(){return this._paused}refresh(){this.events.emit("refresh")}refreshPaused(){this.events.emit("refreshPaused")}toggleMute(){return this.muted=!this.muted,this.refresh(),this.muted}togglePause(){return this.paused=!this.paused,this.refreshPaused(),this._paused}decode(t,e){const s=t=>{e(new Error((null==t?void 0:t.message)||"Unable to decode file"))},i=this._offlineCtx.decodeAudioData(t,(t=>{e(null,t)}),s);i&&i.catch(s)}}const E=t=>{var e;const i=t.src;let o=null==t||null===(e=t.alias)||void 0===e?void 0:e[0];return o&&t.src!==o||(o=s.path.basename(i,s.path.extname(i))),o},S={extension:s.ExtensionType.Asset,detection:{test:async()=>!0,add:async t=>[...t,...p.filter((t=>f[t]))],remove:async t=>t.filter((e=>t.includes(e)))},loader:{name:"sound",extension:{type:[s.ExtensionType.LoadParser],priority:s.LoaderParserPriority.High},test(t){const e=s.path.extname(t).slice(1);return!!f[e]||_.some((e=>t.startsWith(`data:${e}`)))},async load(t,e){const s=await new Promise(((s,i)=>A.from(_objectSpread(_objectSpread({},e.data),{},{url:t,preload:!0,loaded(t,o){var r,n;t?i(t):s(o),null===(r=e.data)||void 0===r||null===(n=r.loaded)||void 0===n||n.call(r,t,o)}}))));return r().add(E(e),s),s},async unload(t,e){r().remove(E(e))}}};s.extensions.add(S);const C=function(t){return o=t,t}(new class{constructor(){this.init()}init(){return this.supported&&(this._webAudioContext=new w),this._htmlAudioContext=new u,this._sounds={},this.useLegacy=!this.supported,this}get context(){return this._context}get filtersAll(){return this.useLegacy?[]:this._context.filters}set filtersAll(t){this.useLegacy||(this._context.filters=t)}get supported(){return null!==w.AudioContext}add(t,e){if("object"==typeof t){const s={};for(const i in t){const o=this._getOptions(t[i],e);s[i]=this.add(i,o)}return s}if(console.assert(!this._sounds[t],`Sound with alias ${t} already exists.`),e instanceof A)return this._sounds[t]=e,e;const s=this._getOptions(e),i=A.from(s);return this._sounds[t]=i,i}_getOptions(t,e){let s;return s="string"==typeof t||Array.isArray(t)?{url:t}:t instanceof ArrayBuffer||t instanceof AudioBuffer||t instanceof HTMLAudioElement?{source:t}:t,s=_objectSpread(_objectSpread({},s),e||{}),s}get useLegacy(){return this._useLegacy}set useLegacy(t){this._useLegacy=t,this._context=!t&&this.supported?this._webAudioContext:this._htmlAudioContext}get disableAutoPause(){return!this._webAudioContext.autoPause}set disableAutoPause(t){this._webAudioContext.autoPause=!t}remove(t){return this.exists(t,!0),this._sounds[t].destroy(),delete this._sounds[t],this}get volumeAll(){return this._context.volume}set volumeAll(t){this._context.volume=t,this._context.refresh()}get speedAll(){return this._context.speed}set speedAll(t){this._context.speed=t,this._context.refresh()}togglePauseAll(){return this._context.togglePause()}pauseAll(){return this._context.paused=!0,this._context.refreshPaused(),this}resumeAll(){return this._context.paused=!1,this._context.refreshPaused(),this}toggleMuteAll(){return this._context.toggleMute()}muteAll(){return this._context.muted=!0,this._context.refresh(),this}unmuteAll(){return this._context.muted=!1,this._context.refresh(),this}removeAll(){for(const t in this._sounds)this._sounds[t].destroy(),delete this._sounds[t];return this}stopAll(){for(const t in this._sounds)this._sounds[t].stop();return this}exists(t,e=!1){const s=!!this._sounds[t];return e&&console.assert(s,`No sound matching alias '${t}'.`),s}isPlaying(){for(const t in this._sounds)if(this._sounds[t].isPlaying)return!0;return!1}find(t){return this.exists(t,!0),this._sounds[t]}play(t,e){return this.find(t).play(e)}stop(t){return this.find(t).stop()}pause(t){return this.find(t).pause()}resume(t){return this.find(t).resume()}volume(t,e){const s=this.find(t);return void 0!==e&&(s.volume=e),s.volume}speed(t,e){const s=this.find(t);return void 0!==e&&(s.speed=e),s.speed}duration(t){return this.find(t).duration}close(){return this.removeAll(),this._sounds=null,this._webAudioContext&&(this._webAudioContext.destroy(),this._webAudioContext=null),this._htmlAudioContext&&(this._htmlAudioContext.destroy(),this._htmlAudioContext=null),this._context=null,this}});s.extensions.add(S);let L=class extends e.System{constructor(t){super(),this.autoPauseAndStart=!0,this.components=[],this.audioBufferCache={},_extends(this,t)}resumeAll(){C.resumeAll()}pauseAll(){C.pauseAll()}stopAll(){C.stopAll()}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=[],C.removeAll()}componentChanged(t){return i(this,void 0,void 0,(function*(){"Sound"===t.componentName&&t.type===e.OBSERVER_TYPE.ADD&&this.add(t)}))}add(t){var s,o;return i(this,void 0,void 0,(function*(){const i=t.component;this.components.push(i);try{const{config:t}=i;i.state="loading";const r=yield e.resource.getResource(t.resource);!this.audioBufferCache[r.name]&&(null===(s=null==r?void 0:r.data)||void 0===s?void 0:s.audio)&&(this.audioBufferCache[r.name]=null===(o=null==r?void 0:r.data)||void 0===o?void 0:o.audio),this.audioBufferCache[r.name]&&i.onload(this.audioBufferCache[r.name])}catch(t){this.onError&&this.onError(t)}}))}};L.systemName="SoundSystem",L=function(t,e,s,i){var o,r=arguments.length,n=r<3?e:null===i?i=Object.getOwnPropertyDescriptor(e,s):i;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)n=Reflect.decorate(t,e,s,i);else for(var u=t.length-1;u>=0;u--)(o=t[u])&&(n=(r<3?o(n):r>3?o(e,s,n):o(e,s))||n);return r>3&&n&&Object.defineProperty(e,s,n),n}([e.decorators.componentObserver({Sound:[]})],L);var O=L;class k extends e.Component{constructor(){super(...arguments),this.state="unloaded",this.config={resource:"",autoplay:!1,muted:!1,volume:1,loop:!1,seek:0,speed:1},this.actionQueue=[],this.startTime=0}get systemContext(){return C.context}get playing(){return!!this.buffer&&this.buffer.isPlaying}get muted(){var t;return(null===(t=this.buffer)||void 0===t?void 0:t.muted)||!1}set muted(t){this.buffer&&(this.buffer.muted=t)}get volume(){var t;return(null===(t=this.buffer)||void 0===t?void 0:t.volume)||0}set volume(t){this.buffer&&(this.buffer.volume=t)}init(t){t&&(_extends(this.config,t),this.config.autoplay&&this.actionQueue.push(this.play.bind(this)))}play(){"loaded"!==this.state&&this.actionQueue.push(this.play.bind(this)),this.buffer&&(this.startTime=this.systemContext.currentTime,this.buffer.play())}resume(){this.buffer&&this.buffer.resume()}pause(){this.buffer&&this.buffer.pause()}stop(){this.buffer&&this.buffer.stop()}onload(t){this.state="loaded",this.buffer=t,this.buffer.muted=this.config.muted,this.buffer.volume=this.config.volume,this.buffer.loop=this.config.loop,this.buffer.speed=this.config.speed,this.actionQueue.forEach((t=>t())),this.actionQueue.length=0}onDestroy(){this.actionQueue.length=0,this.startTime=0,this.buffer&&(this.buffer.destroy(),this.buffer=null)}}k.componentName="Sound";var T=k;return t.Sound=T,t.SoundSystem=O,Object.defineProperty(t,"__esModule",{value:!0}),t}({},EVA,PIXI);window.EVA.plugin.sound=window.EVA.plugin.sound||_EVA_IIFE_sound;
|
package/dist/plugin-sound.cjs.js
CHANGED
|
@@ -135,21 +135,31 @@ class Sound extends eva_js.Component {
|
|
|
135
135
|
speed: 1,
|
|
136
136
|
};
|
|
137
137
|
this.actionQueue = [];
|
|
138
|
+
this.startTime = 0;
|
|
139
|
+
}
|
|
140
|
+
get systemContext() {
|
|
141
|
+
return sound.sound.context;
|
|
138
142
|
}
|
|
139
143
|
get playing() {
|
|
144
|
+
if (!this.buffer)
|
|
145
|
+
return false;
|
|
140
146
|
return this.buffer.isPlaying;
|
|
141
147
|
}
|
|
142
148
|
get muted() {
|
|
143
|
-
|
|
149
|
+
var _a;
|
|
150
|
+
return ((_a = this.buffer) === null || _a === void 0 ? void 0 : _a.muted) || false;
|
|
144
151
|
}
|
|
145
152
|
set muted(v) {
|
|
146
|
-
this.buffer
|
|
153
|
+
if (this.buffer)
|
|
154
|
+
this.buffer.muted = v;
|
|
147
155
|
}
|
|
148
156
|
get volume() {
|
|
149
|
-
|
|
157
|
+
var _a;
|
|
158
|
+
return ((_a = this.buffer) === null || _a === void 0 ? void 0 : _a.volume) || 0;
|
|
150
159
|
}
|
|
151
160
|
set volume(v) {
|
|
152
|
-
this.buffer
|
|
161
|
+
if (this.buffer)
|
|
162
|
+
this.buffer.volume = v;
|
|
153
163
|
}
|
|
154
164
|
init(obj) {
|
|
155
165
|
if (!obj) {
|
|
@@ -164,15 +174,24 @@ class Sound extends eva_js.Component {
|
|
|
164
174
|
if (this.state !== 'loaded') {
|
|
165
175
|
this.actionQueue.push(this.play.bind(this));
|
|
166
176
|
}
|
|
177
|
+
if (!this.buffer)
|
|
178
|
+
return;
|
|
179
|
+
this.startTime = this.systemContext.currentTime;
|
|
167
180
|
this.buffer.play();
|
|
168
181
|
}
|
|
169
182
|
resume() {
|
|
183
|
+
if (!this.buffer)
|
|
184
|
+
return;
|
|
170
185
|
this.buffer.resume();
|
|
171
186
|
}
|
|
172
187
|
pause() {
|
|
188
|
+
if (!this.buffer)
|
|
189
|
+
return;
|
|
173
190
|
this.buffer.pause();
|
|
174
191
|
}
|
|
175
192
|
stop() {
|
|
193
|
+
if (!this.buffer)
|
|
194
|
+
return;
|
|
176
195
|
this.buffer.stop();
|
|
177
196
|
}
|
|
178
197
|
onload(buffer) {
|
|
@@ -187,8 +206,11 @@ class Sound extends eva_js.Component {
|
|
|
187
206
|
}
|
|
188
207
|
onDestroy() {
|
|
189
208
|
this.actionQueue.length = 0;
|
|
190
|
-
this.
|
|
191
|
-
this.buffer
|
|
209
|
+
this.startTime = 0;
|
|
210
|
+
if (this.buffer) {
|
|
211
|
+
this.buffer.destroy();
|
|
212
|
+
this.buffer = null;
|
|
213
|
+
}
|
|
192
214
|
}
|
|
193
215
|
}
|
|
194
216
|
Sound.componentName = 'Sound';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@eva/eva.js"),t=require("@pixi/sound");function
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@eva/eva.js"),t=require("@pixi/sound");function s(e,t,s,o){return new(s||(s=Promise))((function(i,u){function n(e){try{a(o.next(e))}catch(e){u(e)}}function r(e){try{a(o.throw(e))}catch(e){u(e)}}function a(e){e.done?i(e.value):new s((function(t){t(e.value)})).then(n,r)}a((o=o.apply(e,t||[])).next())}))}require("pixi.js").extensions.add(t.soundAsset);let o=class extends e.System{constructor(e){super(),this.autoPauseAndStart=!0,this.components=[],this.audioBufferCache={},Object.assign(this,e)}resumeAll(){t.sound.resumeAll()}pauseAll(){t.sound.pauseAll()}stopAll(){t.sound.stopAll()}update(){const e=this.componentObserver.clear();for(const t of e)this.componentChanged(t)}onResume(){this.autoPauseAndStart&&this.resumeAll()}onPause(){this.autoPauseAndStart&&this.pauseAll()}onDestroy(){this.components.forEach((e=>{e.onDestroy()})),this.components=[],t.sound.removeAll()}componentChanged(t){return s(this,void 0,void 0,(function*(){"Sound"===t.componentName&&t.type===e.OBSERVER_TYPE.ADD&&this.add(t)}))}add(t){var o,i;return s(this,void 0,void 0,(function*(){const s=t.component;this.components.push(s);try{const{config:t}=s;s.state="loading";const u=yield e.resource.getResource(t.resource);!this.audioBufferCache[u.name]&&(null===(o=null==u?void 0:u.data)||void 0===o?void 0:o.audio)&&(this.audioBufferCache[u.name]=null===(i=null==u?void 0:u.data)||void 0===i?void 0:i.audio),this.audioBufferCache[u.name]&&s.onload(this.audioBufferCache[u.name])}catch(e){this.onError&&this.onError(e)}}))}};o.systemName="SoundSystem",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
|
|
@@ -13,4 +13,4 @@ MERCHANTABLITY OR NON-INFRINGEMENT.
|
|
|
13
13
|
See the Apache Version 2.0 License for specific language governing permissions
|
|
14
14
|
and limitations under the License.
|
|
15
15
|
***************************************************************************** */
|
|
16
|
-
function(e,t,o
|
|
16
|
+
function(e,t,s,o){var i,u=arguments.length,n=u<3?t:null===o?o=Object.getOwnPropertyDescriptor(t,s):o;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)n=Reflect.decorate(e,t,s,o);else for(var r=e.length-1;r>=0;r--)(i=e[r])&&(n=(u<3?i(n):u>3?i(t,s,n):i(t,s))||n);return u>3&&n&&Object.defineProperty(t,s,n),n}([e.decorators.componentObserver({Sound:[]})],o);var i=o;class u extends e.Component{constructor(){super(...arguments),this.state="unloaded",this.config={resource:"",autoplay:!1,muted:!1,volume:1,loop:!1,seek:0,speed:1},this.actionQueue=[],this.startTime=0}get systemContext(){return t.sound.context}get playing(){return!!this.buffer&&this.buffer.isPlaying}get muted(){var e;return(null===(e=this.buffer)||void 0===e?void 0:e.muted)||!1}set muted(e){this.buffer&&(this.buffer.muted=e)}get volume(){var e;return(null===(e=this.buffer)||void 0===e?void 0:e.volume)||0}set volume(e){this.buffer&&(this.buffer.volume=e)}init(e){e&&(Object.assign(this.config,e),this.config.autoplay&&this.actionQueue.push(this.play.bind(this)))}play(){"loaded"!==this.state&&this.actionQueue.push(this.play.bind(this)),this.buffer&&(this.startTime=this.systemContext.currentTime,this.buffer.play())}resume(){this.buffer&&this.buffer.resume()}pause(){this.buffer&&this.buffer.pause()}stop(){this.buffer&&this.buffer.stop()}onload(e){this.state="loaded",this.buffer=e,this.buffer.muted=this.config.muted,this.buffer.volume=this.config.volume,this.buffer.loop=this.config.loop,this.buffer.speed=this.config.speed,this.actionQueue.forEach((e=>e())),this.actionQueue.length=0}onDestroy(){this.actionQueue.length=0,this.startTime=0,this.buffer&&(this.buffer.destroy(),this.buffer=null)}}u.componentName="Sound";var n=u;exports.Sound=n,exports.SoundSystem=i;
|
package/dist/plugin-sound.d.ts
CHANGED
|
@@ -5,12 +5,13 @@ import { System } from '@eva/eva.js';
|
|
|
5
5
|
|
|
6
6
|
export declare class Sound extends Component<SoundParams> {
|
|
7
7
|
static componentName: string;
|
|
8
|
-
systemContext: AudioContext;
|
|
8
|
+
get systemContext(): AudioContext;
|
|
9
9
|
systemDestination: GainNode;
|
|
10
10
|
get playing(): boolean;
|
|
11
11
|
state: 'unloaded' | 'loading' | 'loaded';
|
|
12
12
|
config: SoundParams;
|
|
13
13
|
private actionQueue;
|
|
14
|
+
startTime: number;
|
|
14
15
|
private buffer;
|
|
15
16
|
get muted(): boolean;
|
|
16
17
|
set muted(v: boolean);
|
package/dist/plugin-sound.esm.js
CHANGED
|
@@ -131,21 +131,31 @@ class Sound extends Component {
|
|
|
131
131
|
speed: 1,
|
|
132
132
|
};
|
|
133
133
|
this.actionQueue = [];
|
|
134
|
+
this.startTime = 0;
|
|
135
|
+
}
|
|
136
|
+
get systemContext() {
|
|
137
|
+
return sound.context;
|
|
134
138
|
}
|
|
135
139
|
get playing() {
|
|
140
|
+
if (!this.buffer)
|
|
141
|
+
return false;
|
|
136
142
|
return this.buffer.isPlaying;
|
|
137
143
|
}
|
|
138
144
|
get muted() {
|
|
139
|
-
|
|
145
|
+
var _a;
|
|
146
|
+
return ((_a = this.buffer) === null || _a === void 0 ? void 0 : _a.muted) || false;
|
|
140
147
|
}
|
|
141
148
|
set muted(v) {
|
|
142
|
-
this.buffer
|
|
149
|
+
if (this.buffer)
|
|
150
|
+
this.buffer.muted = v;
|
|
143
151
|
}
|
|
144
152
|
get volume() {
|
|
145
|
-
|
|
153
|
+
var _a;
|
|
154
|
+
return ((_a = this.buffer) === null || _a === void 0 ? void 0 : _a.volume) || 0;
|
|
146
155
|
}
|
|
147
156
|
set volume(v) {
|
|
148
|
-
this.buffer
|
|
157
|
+
if (this.buffer)
|
|
158
|
+
this.buffer.volume = v;
|
|
149
159
|
}
|
|
150
160
|
init(obj) {
|
|
151
161
|
if (!obj) {
|
|
@@ -160,15 +170,24 @@ class Sound extends Component {
|
|
|
160
170
|
if (this.state !== 'loaded') {
|
|
161
171
|
this.actionQueue.push(this.play.bind(this));
|
|
162
172
|
}
|
|
173
|
+
if (!this.buffer)
|
|
174
|
+
return;
|
|
175
|
+
this.startTime = this.systemContext.currentTime;
|
|
163
176
|
this.buffer.play();
|
|
164
177
|
}
|
|
165
178
|
resume() {
|
|
179
|
+
if (!this.buffer)
|
|
180
|
+
return;
|
|
166
181
|
this.buffer.resume();
|
|
167
182
|
}
|
|
168
183
|
pause() {
|
|
184
|
+
if (!this.buffer)
|
|
185
|
+
return;
|
|
169
186
|
this.buffer.pause();
|
|
170
187
|
}
|
|
171
188
|
stop() {
|
|
189
|
+
if (!this.buffer)
|
|
190
|
+
return;
|
|
172
191
|
this.buffer.stop();
|
|
173
192
|
}
|
|
174
193
|
onload(buffer) {
|
|
@@ -183,8 +202,11 @@ class Sound extends Component {
|
|
|
183
202
|
}
|
|
184
203
|
onDestroy() {
|
|
185
204
|
this.actionQueue.length = 0;
|
|
186
|
-
this.
|
|
187
|
-
this.buffer
|
|
205
|
+
this.startTime = 0;
|
|
206
|
+
if (this.buffer) {
|
|
207
|
+
this.buffer.destroy();
|
|
208
|
+
this.buffer = null;
|
|
209
|
+
}
|
|
188
210
|
}
|
|
189
211
|
}
|
|
190
212
|
Sound.componentName = 'Sound';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eva/plugin-sound",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
3
|
+
"version": "2.0.0-beta.7",
|
|
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": "2.0.0-beta.
|
|
21
|
+
"@eva/eva.js": "2.0.0-beta.7",
|
|
22
22
|
"eventemitter3": "^3.1.2",
|
|
23
23
|
"pixi.js": "^8.6.5",
|
|
24
24
|
"@pixi/sound": "^6.0.1"
|