@drincs/pixi-vn
Version:
Pixi'VN is a npm package that provides various features for creating visual novels.
2 lines • 11 kB
JavaScript
;var core=require('@drincs/pixi-vn/core'),h=require('@drincs/pixi-vn/pixi.js'),sound=require('@pixi/sound');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var h__default=/*#__PURE__*/_interopDefault(h);var O=Object.defineProperty;var P=(o,t,e)=>t in o?O(o,t,{enumerable:true,configurable:true,writable:true,value:e}):o[t]=e;var m=(o,t,e)=>P(o,typeof t!="symbol"?t+"":t,e);var C="general";var c;(s=>(s.log=(a,...r)=>console.log(`[Pixi\u2019VN] ${a}`,...r),s.warn=(a,...r)=>console.warn(`[Pixi\u2019VN] ${a}`,...r),s.error=(a,...r)=>console.error(`[Pixi\u2019VN] ${a}`,...r),s.info=(a,...r)=>console.info(`[Pixi\u2019VN] ${a}`,...r)))(c||(c={}));function S(o){try{if(typeof o>"u")return o;let t=JSON.stringify(o);return JSON.parse(t)}catch(t){throw c.error("Error creating exportable element",o,t),new core.PixiError("not_json_serializable","Error creating exportable element")}}function E(o,t){return o===void 0&&t===void 0?1:t===void 0?o:o===void 0?t:o*t}var i=class{constructor(){}};m(i,"mediaInstances",{}),m(i,"channels",{}),m(i,"delayTimeoutInstances",[]);function M(o,t,e){return new Proxy(t,{get(n,s,a){switch(s){case "volume":case "muted":{let r=i.mediaInstances[o];return r?r.options[s]:Reflect.get(n,s,a)}default:return Reflect.get(n,s,a)}},set(n,s,a,r){if(o in i.mediaInstances)switch(s){case "volume":return i.mediaInstances[o].options[s]=a,a=E(a,e.channelOptions.volume),Reflect.set(n,s,a,r);case "muted":return i.mediaInstances[o].options[s]=a,e.channelOptions.muted?Reflect.set(n,s,true,r):Reflect.set(n,s,a,r);case "loop":case "delay":case "end":case "filters":case "singleInstance":case "speed":case "sprite":case "start":i.mediaInstances[o].options[s]=a;default:return Reflect.set(n,s,a,r)}return Reflect.set(n,s,a,r)}})}var y=class{constructor(t,e={}){this.alias=t;this.channelOptions=e;}async play(t,e,n){let s,a;if(typeof e=="string"?(s=t,a=e):(s=t,a=t,n=e),s in i.mediaInstances){let l=i.mediaInstances[s];l.instance.stop(),n={...l.options,...n};}let r=M(s,await sound.sound.play(a,{...n??{},filters:[...this.channelOptions.filters||[],...n?.filters||[]],muted:!!this.channelOptions.muted||!!n?.muted,volume:E(n?.volume,this.channelOptions.volume)}),this);if(n?.delay){r.paused=true;let l=setTimeout(()=>{r.paused=false,i.delayTimeoutInstances=i.delayTimeoutInstances.filter(I=>I[0]!==l);},n.delay*1e3);i.delayTimeoutInstances.push([l,s]);}return i.mediaInstances[s]={channelAlias:this.alias,soundAlias:a,instance:r,stepCounter:core.GameUnifier.stepCounter,options:{volume:n?.volume??1,muted:n?.muted??false,loop:n?.loop??false,...n??{}}},r.on("end",()=>{delete i.mediaInstances[s];}),r}updateMediaVolume(){for(let t in i.mediaInstances){let e=i.mediaInstances[t];if(e.channelAlias===this.alias){let n=e.options.volume??1;e.instance.volume=n;}}}get volume(){return this.channelOptions.volume??1}set volume(t){this.channelOptions.volume=t,this.updateMediaVolume();}updateMediaMuted(){for(let t in i.mediaInstances){let e=i.mediaInstances[t];if(e.channelAlias===this.alias){let n=e.options.muted??false;e.instance.muted=n;}}}get muted(){return this.channelOptions.muted??false}set muted(t){this.channelOptions.muted=t,this.updateMediaMuted();}toggleMuteAll(){return this.muted=!this.muted,this.muted}get mediaInstances(){return Object.values(i.mediaInstances).reduce((t,e)=>(e.channelAlias===this.alias&&t.push(e.instance),t),[])}get background(){return this.channelOptions.background||false}stopAll(){for(let t in i.mediaInstances){let e=i.mediaInstances[t];e.channelAlias===this.alias&&(e.instance.stop(),delete i.mediaInstances[t]);}return this}pauseAll(){for(let t in i.mediaInstances){let e=i.mediaInstances[t];e.channelAlias===this.alias&&!e.instance.paused&&(e.instance.paused=true);}return this}resumeAll(){for(let t in i.mediaInstances){let e=i.mediaInstances[t];e.channelAlias===this.alias&&e.instance.paused&&(e.instance.paused=false);}return this}};function A(o){let t=[];for(let e of o)e.type==="TelephoneFilter"?t.push(new sound.filters.TelephoneFilter):e.type==="StreamFilter"?t.push(new sound.filters.StreamFilter):e.type==="StereoFilter"?t.push(new sound.filters.StereoFilter(e.pan)):e.type==="ReverbFilter"?t.push(new sound.filters.ReverbFilter(e.seconds,e.decay,e.reverse)):e.type==="MonoFilter"?t.push(new sound.filters.MonoFilter):e.type==="EqualizerFilter"?t.push(new sound.filters.EqualizerFilter(e.f32,e.f64,e.f125,e.f250,e.f500,e.f1k,e.f2k,e.f4k,e.f8k,e.f16k)):e.type==="DistortionFilter"?t.push(new sound.filters.DistortionFilter(e.amount)):c.error("Unknown sound filter type");return t}function _(o){if(!o)return;let t=[];for(let e of o)e instanceof sound.filters.TelephoneFilter?t.push({type:"TelephoneFilter"}):e instanceof sound.filters.StreamFilter?t.push({type:"StreamFilter"}):e instanceof sound.filters.StereoFilter?t.push({type:"StereoFilter",pan:e.pan}):e instanceof sound.filters.ReverbFilter?t.push({type:"ReverbFilter",seconds:e.seconds,decay:e.decay,reverse:e.reverse}):e instanceof sound.filters.MonoFilter?t.push({type:"MonoFilter"}):e instanceof sound.filters.EqualizerFilter?t.push({type:"EqualizerFilter",f32:e.f32,f64:e.f64,f125:e.f125,f250:e.f250,f500:e.f500,f1k:e.f1k,f2k:e.f2k,f4k:e.f4k,f8k:e.f8k,f16k:e.f16k}):e instanceof sound.filters.DistortionFilter?t.push({type:"DistortionFilter",amount:e.amount}):c.error("Unknown sound filter type");return t}var g=class{constructor(){m(this,"_defaultChannelAlias",C);}get context(){return sound.sound.context}get filtersAll(){return sound.sound.filtersAll}set filtersAll(t){sound.sound.filtersAll=t.filter(e=>!(e instanceof sound.filters.Filter));}get supported(){return sound.sound.supported}get defaultChannelAlias(){return this._defaultChannelAlias}set defaultChannelAlias(t){this._defaultChannelAlias=t;}add(t,e){return sound.sound.add(t,e)}async edit(t,e){let n=sound.sound.find(t);if(!n&&(await this.load(t),n=sound.sound.find(t),!n)){c.error(`Sound with alias ${t} not found after loading.`);return}n.options=e;}get useLegacy(){return sound.sound.useLegacy}set useLegacy(t){sound.sound.useLegacy=t;}get disableAutoPause(){return sound.sound.disableAutoPause}set disableAutoPause(t){sound.sound.disableAutoPause=t;}get volumeAll(){return sound.sound.volumeAll}set volumeAll(t){sound.sound.volumeAll=t;}get speedAll(){return sound.sound.speedAll}set speedAll(t){sound.sound.speedAll=t;}pauseAll(){for(let t in i.mediaInstances){let e=i.mediaInstances[t];e.instance.paused||(e.instance.paused=true);}return this}resumeAll(){for(let t in i.mediaInstances){let e=i.mediaInstances[t];e.instance.paused&&(e.instance.paused=false);}return this}toggleMuteAll(){return sound.sound.toggleMuteAll()}muteAll(){return sound.sound.muteAll(),this}unmuteAll(){return sound.sound.unmuteAll(),this}stopAll(){return i.mediaInstances={},sound.sound.stopAll(),this}isPlaying(){return sound.sound.isPlaying()}async play(t,e,n){let s,a;typeof e=="string"?(s=t,a=e):(s=t,a=t,n=e);let{channel:r=this.defaultChannelAlias,...l}=n??{};return await this.findChannel(r).play(s,a,l)}find(t){return i.mediaInstances[t]?.instance}stop(t){let e=this.find(t);e?(e.stop(),delete i.mediaInstances[t]):c.warn(`No media instance found with alias ${t} to stop.`);}pause(t){let e=this.find(t);if(!e){c.warn(`No media instance found with alias ${t} to pause.`);return}return e.paused=true,e}resume(t){let e=this.find(t);if(!e){c.warn(`No media instance found with alias ${t} to resume.`);return}return e.paused=false,e}duration(t){return sound.sound.duration(t)}async load(t){await h__default.default.Assets.load(t),typeof t=="string"&&(t=[t]),t.forEach(e=>{let n=h__default.default.Assets.get(e);sound.sound.exists(e)||sound.sound.add(e,n);});}backgroundLoad(t){let e=h__default.default.Assets.backgroundLoad(t);return e.then(()=>{typeof t=="string"&&(t=[t]),t.forEach(n=>{let s=h__default.default.Assets.get(n);sound.sound.exists(n)||sound.sound.add(n,s);});}),e}backgroundLoadBundle(t){let e=h__default.default.Assets.backgroundLoadBundle(t);return e.then(async()=>{try{let n=await h__default.default.Assets.loadBundle(t);for(let s in n){let a=n[s];a instanceof sound.Sound&&(sound.sound.exists(s)||sound.sound.add(s,a));}}catch(n){c.error("Error loading sound bundle",n);}}),e}clear(){this.stopAll();}addChannel(t,e={}){if(typeof t!="string"){t.forEach(s=>{let a={...e,filters:e.filters?[...e.filters]:e.filters};this.addChannel(s,a);});return}if(i.channels[t]){c.warn(`Channel with alias ${t} already exists.`);return}let n=new y(t,e);return i.channels[t]=n,n}findChannel(t){let e=i.channels[t];return e||this.addChannel(t)}get channels(){return Object.values(i.channels)}export(){let t=Object.entries(i.mediaInstances).reduce((e,[n,s])=>(e[n]={channelAlias:s.channelAlias,soundAlias:s.soundAlias,stepCounter:s.stepCounter,options:{...s.options,filters:_(s.options.filters)},paused:s.instance.paused},e),{});return {mediaInstances:S(t),filters:S(_(this.filtersAll))}}async restore(t){try{if(t.hasOwnProperty("soundsPlaying")){let e=t.soundsPlaying;if(e){let n=Object.keys(e).map(async s=>{await this.load(s),await this.play(s);});await Promise.all(n);}}if(t.hasOwnProperty("mediaInstances")){let e=t.mediaInstances;if(e){let n=new Set,s=Object.values(e).map(async({soundAlias:r,channelAlias:l})=>(n.add(l),await this.load(r)));await Promise.all(s),this.channels.forEach(r=>{(!r.background||!n.has(r.alias))&&r.stopAll();});let a=Object.keys(e).map(async r=>{let l=e[r],I=this.findChannel(l.channelAlias);if(I.background)if(l.stepCounter===core.GameUnifier.stepCounter||!this.find(r)){let d=await I.play(r,l.soundAlias,{...l.options,filters:A(l.options.filters||[])});l.paused&&(d.paused=l.paused);}else {let d=this.find(r);d&&(i.mediaInstances[r].options={...l.options,filters:A(l.options.filters||[])},d.paused!==l.paused&&(d.paused=l.paused),d.loop!==(l.options.loop||!1)&&(d.loop=l.options.loop||!1),d.volume!==(l.options.volume??1)&&(d.volume=l.options.volume??1),d.muted!==(l.options.muted||!1)&&(d.muted=l.options.muted||!1),d.speed!==(l.options.speed??1)&&(d.speed=l.options.speed??1));}else {let d=await I.play(r,l.soundAlias,{...l.options,filters:A(l.options.filters||[])});l.paused&&(d.paused=l.paused);}});await Promise.all(a);}}if(t.hasOwnProperty("filters")){let e=t.filters;e&&(this.filtersAll=A(e));}}catch(e){c.error("Error importing data",e);}}};var w={DistortionFilter:sound.filters.DistortionFilter,EqualizerFilter:sound.filters.EqualizerFilter,MonoFilter:sound.filters.MonoFilter,ReverbFilter:sound.filters.ReverbFilter,StereoFilter:sound.filters.StereoFilter,StreamFilter:sound.filters.StreamFilter,TelephoneFilter:sound.filters.TelephoneFilter};var Ce=new g;core.GameUnifier.addOnPreContinue(async()=>{try{i.delayTimeoutInstances.forEach(o=>{clearTimeout(o[0]);let t=i.mediaInstances[o[1]];t&&(t.instance.paused=!1);}),i.delayTimeoutInstances=[],Object.values(i.channels).forEach(o=>{o.background||o.stopAll();});}catch{}});exports.SoundManagerStatic=i;exports.filters=w;exports.sound=Ce;//# sourceMappingURL=sound.cjs.map
//# sourceMappingURL=sound.cjs.map