Files

3 lines
12 KiB
JavaScript

var gdjs;(function(h){const d=new h.Logger("Audio manager"),f=["audio"],_={preload:!0,onplayerror:(l,e)=>d.error("Can't play an audio file: "+e),onloaderror:(l,e)=>d.error("Error while loading an audio file: "+e)},c=l=>l>1?1:l<0?0:l,u=(l,e)=>{if(l instanceof Error&&l.message&&typeof l.message=="string"&&l.message.startsWith("Maximum call stack size exceeded"))console.warn(`An error occurred when call method "${e}":`,l);else throw l};class m{constructor(e,t,s,i,a){this._id=null;this._oncePlay=[];this._onPlay=[];this._howl=e,this._initialVolume=c(t),this._loop=s,this._rate=i,this._audioResourceName=a}isActualId(){return!!this._howl._soundById(this._id)}isLoaded(){return this._howl.state()==="loaded"}play(){try{if(this.isLoaded()){const e=this._howl.play(this._id===null?"__default":this._id);this._id=e,this._howl.volume(this._initialVolume,e),this._howl.loop(this._loop,e),this._howl.rate(h.HowlerSoundManager.clampRate(this._rate),e),this._onPlay.forEach(t=>{this.on("play",t),t(e)}),this._oncePlay.forEach(t=>t(e)),this._onPlay=[],this._oncePlay=[]}else this._howl.once("load",()=>this.play())}catch(e){u(e,"play")}return this}pause(){try{this._id!==null&&this.isActualId()&&this._howl.pause(this._id)}catch(e){u(e,"pause")}return this}stop(){try{this._id!==null&&this.isActualId()&&this._howl.stop(this._id)}catch(e){u(e,"stop")}return this}playing(){return(this._id!==null?this._howl.playing(this._id):!0)||!this.isLoaded()}paused(){return!this.playing()}stopped(){return this.paused()&&this.getSeek()===0}getRate(){return this._rate}setRate(e){try{this._rate=e,this._id!==null&&this.isActualId()&&(e=h.HowlerSoundManager.clampRate(e),this._howl.rate(e,this._id))}catch(t){u(t,"rate")}return this}getLoop(){return this._loop}setLoop(e){try{this._loop=e,this._id!==null&&this.isActualId()&&this._howl.loop(e,this._id)}catch(t){u(t,"loop")}return this}getVolume(){try{return this._id===null||!this.isActualId()?this._initialVolume:this._howl.volume(this._id)}catch(e){u(e,"getVolume")}return this._initialVolume}setVolume(e){try{this._initialVolume=c(e),this._id!==null&&this.isActualId()&&this._howl.volume(this._initialVolume,this._id)}catch(t){u(t,"volume")}return this}getMute(){return this._id===null||!this.isActualId()?!1:this._howl.mute(this._id)}setMute(e){try{this._id!==null&&this.isActualId()&&this._howl.mute(e,this._id)}catch(t){u(t,"mute")}return this}getSeek(){return this._id===null||!this.isActualId()?0:this._howl.seek(this._id)}setSeek(e){try{this._id!==null&&this.isActualId()&&this._howl.seek(e,this._id)}catch(t){u(t,"seek")}return this}getSpatialPosition(e){return this._id===null||!this.isActualId()?0:this._howl.pos(this._id)[e==="x"?0:e==="y"?1:2]}setSpatialPosition(e,t,s){return this._id!==null&&this.isActualId()&&this._howl.pos(e,t,s,this._id),this}fade(e,t,s){try{this._id!==null&&this.isActualId()&&this._howl.fade(c(e),c(t),s,this._id)}catch(i){u(i,"fade")}return this}on(e,t){return e==="play"?this._id===null?this._onPlay.push(t):this._howl.on(e,t,this._id):this._id===null?this.once("play",()=>this.on(e,t)):this._howl.on(e,t,this._id),this}once(e,t){return e==="play"?this._id===null?this._oncePlay.push(t):this.playing()?t(this._id):this._howl.once(e,t,this._id):this._id===null?this.once("play",()=>this.once(e,t)):this._howl.once(e,t,this._id),this}off(e,t){return this._id!==null&&this._howl.off(e,t,this._id),this}getNetworkSyncData(){if(this.paused()||!this.isLoaded()||this.stopped())return;const e=this.getSeek(),t=typeof e!="number"?0:e,s=this.isLoaded()?this.getVolume():this._initialVolume;return{resourceName:this._audioResourceName,loop:this._loop,volume:s,rate:this._rate,seek:t}}}h.HowlerSound=m;class p{constructor(e){this._loadedMusics=new h.ResourceCache;this._loadedSounds=new h.ResourceCache;this._availableResources={};this._globalVolume=100;this._sounds={};this._cachedSpatialPosition={};this._musics={};this._freeSounds=[];this._freeMusics=[];this._muteEverythingReasons=new Set;this._pausedSounds=[];this._paused=!1;this._getAudioResource=e=>{const t=this._resourceLoader.getResource(e);return t&&this.getResourceKinds().includes(t.kind)?t:{file:e,kind:"audio",metadata:"",name:e}};this._resourceLoader=e,h.registerRuntimeScenePostEventsCallback(this._clearCachedSpatialPosition.bind(this));const t=this;document.addEventListener("deviceready",function(){document.addEventListener("pause",function(){t.pauseAllActiveSounds()},!1),document.addEventListener("resume",function(){t.resumeAllActiveSounds()},!1)})}pauseAllActiveSounds(){const e=this._freeSounds.concat(this._freeMusics);for(let t in this._sounds)this._sounds.hasOwnProperty(t)&&e.push(this._sounds[t]);for(let t in this._musics)this._musics.hasOwnProperty(t)&&e.push(this._musics[t]);for(let t=0;t<e.length;t++){const s=e[t];!s.paused()&&!s.stopped()&&(s.pause(),this._pausedSounds.push(s))}this._paused=!0}resumeAllActiveSounds(){const e=Howler.ctx;e&&e.resume().catch(()=>{});try{for(let t=0;t<this._pausedSounds.length;t++){const s=this._pausedSounds[t];s.stopped()||s.play()}}catch(t){const s=t;if(s.message&&typeof s.message=="string"&&s.message.startsWith("Maximum call stack size exceeded"))console.warn("An error occurred when resuming paused sounds while the game was in background:",s);else throw s}this._pausedSounds.length=0,this._paused=!1}getResourceKinds(){return f}static clampRate(e){return e>4?4:e<.5?.5:e}_getSoundUrlsFromResource(e){return[this._resourceLoader.getFullUrl(e.file)]}_getDefaultSoundUrl(e){return this._resourceLoader.getFullUrl(e.file)}_preloadAudioFile(e,t){const s=e.file;return new Promise((i,a)=>{const o=t?this._loadedMusics:this._loadedSounds;o[s]=new Howl(Object.assign({},_,{src:this._getSoundUrlsFromResource(e),onload:i,onloaderror:(n,r)=>a(r),html5:t,xhr:{withCredentials:this._resourceLoader.checkIfCredentialsRequired(s)},volume:0}))})}_storeSoundInArray(e,t){for(let s=0,i=e.length;s<i;++s)if(!e[s]||e[s].stopped())return e[s]=t,t;return e.push(t),t}createHowlerSound(e,t,s,i,a){const o=t?this._loadedMusics:this._loadedSounds,n=this._getAudioResource(e);let r=o.get(n);return r||(r=new Howl(Object.assign({src:this._getSoundUrlsFromResource(n),html5:t,xhr:{withCredentials:this._resourceLoader.checkIfCredentialsRequired(n.file)},volume:0},_)),o.set(n,r)),new h.HowlerSound(r,s,i,a,e)}loadAudio(e,t){const s=t?this._loadedMusics:this._loadedSounds,i=this._getAudioResource(e);s.get(i)||s.set(i,new Howl(Object.assign({src:this._getSoundUrlsFromResource(i),html5:t,xhr:{withCredentials:this._resourceLoader.checkIfCredentialsRequired(i.file)},volume:0},_)))}unloadAudio(e,t){const s=t?this._loadedMusics:this._loadedSounds,i=this._getAudioResource(e),a=s.get(i);if(!a)return;function o(n){for(let r in n)n[r]&&n[r]._howl===a&&(n[r].stop(),delete n[r])}o(this._freeMusics),o(this._freeSounds),o(Object.values(this._musics)),o(Object.values(this._sounds)),o(this._pausedSounds),a.unload(),s.delete(i)}unloadAll(){Howler.unload(),this._freeSounds.length=0,this._freeMusics.length=0,this._sounds={},this._musics={},this._pausedSounds.length=0,this._loadedMusics.clear(),this._loadedSounds.clear()}playSound(e,t,s,i,a){const o=this.createHowlerSound(e,!1,s/100,t,i);this._storeSoundInArray(this._freeSounds,o),o.once("play",()=>{this._paused&&(o.pause(),this._pausedSounds.push(o))}),o.play(),a&&o.setSeek(a)}playSoundOnChannel(e,t,s,i,a,o){this._sounds[t]&&this._sounds[t].stop();const n=this.createHowlerSound(e,!1,i/100,s,a),r=this._cachedSpatialPosition[t];r&&n.once("play",()=>{n.setSpatialPosition(...r)}),this._sounds[t]=n,n.once("play",()=>{this._paused&&(n.pause(),this._pausedSounds.push(n))}),n.play(),o&&n.setSeek(o)}getSoundOnChannel(e){return this._sounds[e]||null}playMusic(e,t,s,i,a){const o=this.createHowlerSound(e,!0,s/100,t,i);this._storeSoundInArray(this._freeMusics,o),o.once("play",()=>{this._paused&&(o.pause(),this._pausedSounds.push(o))}),o.play(),a&&o.setSeek(a)}playMusicOnChannel(e,t,s,i,a,o){this._musics[t]&&this._musics[t].stop();const n=this.createHowlerSound(e,!0,i/100,s,a);this._musics[t]=n,n.once("play",()=>{this._paused&&(n.pause(),this._pausedSounds.push(n))}),n.play(),o&&n.setSeek(o)}getMusicOnChannel(e){return this._musics[e]||null}setSoundSpatialPositionOnChannel(e,t,s,i){const a=this.getSoundOnChannel(e);a&&!a.paused()?a.setSpatialPosition(t,s,i):this._cachedSpatialPosition[e]=[t,s,i]}_clearCachedSpatialPosition(){this._cachedSpatialPosition={}}muteEverything(e){this._muteEverythingReasons.add(e),this._updateGlobalVolume()}unmuteEverything(e){this._muteEverythingReasons.delete(e),this._updateGlobalVolume()}setGlobalVolume(e){this._globalVolume=e,this._globalVolume>100&&(this._globalVolume=100),this._globalVolume<0&&(this._globalVolume=0),this._updateGlobalVolume()}getGlobalVolume(){return this._globalVolume}_updateGlobalVolume(){this._muteEverythingReasons.size>0?Howler.volume(0):Howler.volume(this._globalVolume/100)}clearAll(){Howler.stop(),this._freeSounds.length=0,this._freeMusics.length=0,this._sounds={},this._musics={},this._pausedSounds.length=0}async processResource(e){}async loadResource(e){const t=this._resourceLoader.getResource(e);if(!t){d.warn('Unable to find audio for resource "'+e+'".');return}if(t.file){if(this._availableResources[t.name])return;this._availableResources[t.name]=t}if(t.preloadAsMusic)try{await this._preloadAudioFile(t,!0)}catch(s){throw delete this._availableResources[t.name],d.warn("There was an error while preloading an audio file: "+s),s}if(t.preloadAsSound)try{await this._preloadAudioFile(t,!1)}catch(s){throw delete this._availableResources[t.name],d.warn("There was an error while preloading an audio file: "+s),s}else if(t.preloadInCache||!t.preloadAsMusic)try{const s=t.file;await new Promise((i,a)=>{const o=new XMLHttpRequest;o.withCredentials=this._resourceLoader.checkIfCredentialsRequired(s),o.addEventListener("load",()=>{o.status>=200&&o.status<300?i(void 0):a(`HTTP error while preloading audio file in cache. Status is ${o.status}.`)}),o.addEventListener("error",n=>a("XHR error: "+s)),o.addEventListener("abort",n=>a("XHR abort: "+s)),o.open("GET",this._getDefaultSoundUrl(t)),o.send()})}catch(s){throw delete this._availableResources[t.name],d.warn("There was an error while preloading an audio file: "+s),s}}getNetworkSyncData(){const e=[];this._freeMusics.forEach(a=>{const o=a.getNetworkSyncData();o&&e.push(o)});const t=[];this._freeSounds.forEach(a=>{const o=a.getNetworkSyncData();o&&t.push(o)});const s={};Object.entries(this._musics).forEach(([a,o])=>{const n=o.getNetworkSyncData();if(n){const r=parseInt(a,10);s[r]=n}});const i={};return Object.entries(this._sounds).forEach(([a,o])=>{const n=o.getNetworkSyncData();if(n){const r=parseInt(a,10);i[r]=n}}),{globalVolume:this._globalVolume,cachedSpatialPosition:this._cachedSpatialPosition,freeMusics:e,freeSounds:t,musics:s,sounds:i}}updateFromNetworkSyncData(e){this.clearAll(),this._globalVolume=e.globalVolume,this._cachedSpatialPosition=e.cachedSpatialPosition;for(let t=0;t<e.freeSounds.length;t++){const s=e.freeSounds[t];this.playSound(s.resourceName,s.loop,s.volume*100,s.rate,s.seek)}for(let t=0;t<e.freeMusics.length;t++){const s=e.freeMusics[t];this.playMusic(s.resourceName,s.loop,s.volume*100,s.rate,s.seek)}for(const[t,s]of Object.entries(e.sounds)){const i=parseInt(t,10);this.playSoundOnChannel(s.resourceName,i,s.loop,s.volume*100,s.rate,s.seek)}for(const[t,s]of Object.entries(e.musics)){const i=parseInt(t,10);this.playMusicOnChannel(s.resourceName,i,s.loop,s.volume*100,s.rate,s.seek)}}dispose(){this.unloadAll()}unloadResource(e){this._loadedMusics.getFromName(e.name)&&this.unloadAudio(e.name,!0),this._loadedSounds.getFromName(e.name)&&this.unloadAudio(e.name,!1)}}h.HowlerSoundManager=p,h.SoundManager=p})(gdjs||(gdjs={}));
//# sourceMappingURL=howler-sound-manager.js.map