UNPKG

7.15 kBJavaScriptView Raw
1/*! For license information please see 192.min.js.LICENSE.txt */
2(this.webpackChunk_tsparticles_fireworks=this.webpackChunk_tsparticles_fireworks||[]).push([[192],{5192:(t,e,i)=>{i.d(e,{SoundsInstance:()=>c});var n=i(5540),s=i(7434);const o=1;function a(t){const e=document.createElement("img"),{clickCb:i,container:n,display:s,iconOptions:a,margin:u,options:c,pos:r,rightOffsets:l}=t,{width:h,path:d,style:_,svg:m}=a;!function(t,e,i,n,s,a,u,c){t.style.userSelect="none",t.style.webkitUserSelect="none",t.style.position="absolute",t.style.top=`${e+u}px`,t.style.left=i-u-a+"px",t.style.display=n,t.style.zIndex=`${s+o}`,t.style.cssText+=c}(e,r.top+u,r.right-(u*(l.length+1)+h+l.reduce(((t,e)=>t+e),0)),s,c.fullScreen.zIndex+o,h,u,_),e.src=d??(m?`data:image/svg+xml;base64,${btoa(m)}`:"");return(n.canvas.element?.parentNode??document.body).append(e),e.addEventListener("click",(()=>{i()})),e}function u(t){t&&t.remove()}class c{constructor(t,e){this._addBuffer=t=>{const e=t.createBufferSource();return this._audioSources.push(e),e},this._addOscillator=t=>{const e=t.createOscillator();return this._audioSources.push(e),e},this._initEvents=()=>{const t=this._container,e=t.actualOptions.sounds;if(e?.enable&&t.canvas.element)for(const t of e.events){const e=i=>{(async()=>{const s=t.filter&&!t.filter(i);if(this._container!==i.container)return;if(!this._container||this._container.muted||this._container.destroyed)return void(0,n.wJ)(t.event,(t=>{this._engine.removeEventListener(t,e)}));if(s)return;if(t.audio)this._playBuffer((0,n.TA)(t.audio));else if(t.melodies){const e=(0,n.Vh)(t.melodies);e.melodies.length?await Promise.allSettled(e.melodies.map((t=>this._playNote(t.notes,0,e.loop)))):await this._playNote(e.notes,0,e.loop)}else if(t.notes){const e=(0,n.Vh)(t.notes);await this._playNote([e],0,!1)}})()};(0,n.wJ)(t.event,(t=>{this._engine.addEventListener(t,e)}))}},this._mute=async()=>{const t=this._container,e=this._getAudioContext();for(const t of this._audioSources)this._removeAudioSource(t);this._gain&&this._gain.disconnect(),await e.close(),t.audioContext=void 0,this._engine.dispatchEvent("soundsMuted",{container:this._container})},this._playBuffer=t=>{const e=this._audioMap.get(t.source);if(!e)return;const i=this._container.audioContext;if(!i)return;const n=this._addBuffer(i);n.loop=t.loop,n.buffer=e,n.connect(this._gain??i.destination),n.start()},this._playFrequency=async(t,e)=>{if(!this._gain||this._container.muted)return;const i=this._getAudioContext(),n=this._addOscillator(i);return n.connect(this._gain),n.type="sine",n.frequency.value=t,n.start(),new Promise((t=>{setTimeout((()=>{this._removeAudioSource(n),t()}),e)}))},this._playMuteSound=()=>{if(this._container.muted)return;const t=this._getAudioContext(),e=t.createGain();e.connect(t.destination),e.gain.value=0;const i=t.createOscillator();i.connect(e),i.type="sine",i.frequency.value=1,i.start(),setTimeout((()=>{i.stop(),i.disconnect(),e.disconnect()}))},this._playNote=async(t,e,i)=>{if(this._container.muted)return;const s=t[e];if(!s)return;const o=s.value,a=(0,n.wJ)(o,(async(i,n)=>this._playNoteValue(t,e,n)));await((0,n.cy)(a)?Promise.allSettled(a):a);let u=e+1;i&&u>=t.length&&(u%=t.length),this._container.muted||await this._playNote(t,u,i)},this._playNoteValue=async(t,e,i)=>{const o=t[e];if(!o)return;const a=(0,n.TA)(o.value,i,!0);try{const t=(0,s.jE)(a);if(!(0,n.Et)(t))return;await this._playFrequency(t,o.duration)}catch(t){(0,n.tZ)().error(t)}},this._removeAudioSource=t=>{t.stop(),t.disconnect();this._audioSources.splice(this._audioSources.indexOf(t),1)},this._unmute=()=>{const t=this._container.actualOptions.sounds;if(!t)return;const e=this._getAudioContext();this._audioSources||(this._audioSources=[]);const i=e.createGain();i.connect(e.destination),i.gain.value=t.volume.value/n.a5,this._gain=i,this._initEvents(),this._engine.dispatchEvent("soundsUnmuted",{container:this._container})},this._updateMuteIcons=()=>{const t=this._container,e=t.actualOptions.sounds;if(!e?.enable||!e.icons.enable)return;const i=this._muteImg,n=this._unmuteImg;i&&(i.style.display=t.muted?"block":"none"),n&&(n.style.display=t.muted?"none":"block")},this._updateMuteStatus=async()=>{const t=this._container,e=this._getAudioContext();t.muted?(await(e?.suspend()),await this._mute()):(await(e?.resume()),this._unmute(),this._playMuteSound())},this._updateVolume=async()=>{const t=this._container,e=t.actualOptions.sounds;if(!e?.enable)return;(0,n.qE)(this._volume,e.volume.min,e.volume.max);let i=!1;this._volume<=0&&!t.muted?(this._volume=0,t.muted=!0,i=!0):this._volume>0&&t.muted&&(t.muted=!1,i=!0),i&&(this._updateMuteIcons(),await this._updateMuteStatus()),this._gain?.gain&&(this._gain.gain.value=this._volume/n.a5)},this._container=t,this._engine=e,this._volume=0,this._audioSources=[],this._audioMap=new Map}async init(){const t=this._container.actualOptions.sounds;if(!t?.enable)return;if(t.autoPlay&&(0,s.E3)()){const t=()=>{removeEventListener(n.ms,t),removeEventListener(n.s7,t),(0,s.XK)(),this.unmute()},e={capture:!0,once:!0};addEventListener(n.ms,t,e),addEventListener(n.s7,t,e)}this._volume=t.volume.value;const e=t.events;this._audioMap=new Map;for(const t of e){if(!t.audio)continue;const e=(0,n.wJ)(t.audio,(async t=>{const e=await fetch(t.source);if(!e.ok)return;const i=await e.arrayBuffer(),n=this._getAudioContext(),s=await n.decodeAudioData(i);this._audioMap.set(t.source,s)}));e instanceof Promise?await e:await Promise.allSettled(e)}}async mute(){this._container.muted||await this.toggleMute()}async start(){const t=this._container,e=t.actualOptions,i=e.sounds;if(!i?.enable||!t.canvas.element)return;t.muted=!0;const n=t.canvas.element,o={top:n.offsetTop,right:n.offsetLeft+n.offsetWidth},{mute:u,unmute:c,volumeDown:r,volumeUp:l}=i.icons,h=async()=>{await this.toggleMute()},d=i.icons.enable?"block":"none";this._muteImg=a({container:t,options:e,pos:o,display:d,iconOptions:u,margin:10,rightOffsets:[r.width,l.width],clickCb:h}),this._unmuteImg=a({container:t,options:e,pos:o,display:"none",iconOptions:c,margin:10,rightOffsets:[r.width,l.width],clickCb:h}),this._volumeDownImg=a({container:t,options:e,pos:o,display:d,iconOptions:r,margin:10,rightOffsets:[l.width],clickCb:async()=>{await this.volumeDown()}}),this._volumeUpImg=a({container:t,options:e,pos:o,display:d,iconOptions:l,margin:10,rightOffsets:[],clickCb:async()=>{await this.volumeUp()}}),!(0,s.E3)()&&i.autoPlay&&await this.unmute()}stop(){this._container.muted=!0,(async()=>{await this._mute(),u(this._muteImg),u(this._unmuteImg),u(this._volumeDownImg),u(this._volumeUpImg)})()}async toggleMute(){const t=this._container;t.muted=!t.muted,this._updateMuteIcons(),await this._updateMuteStatus()}async unmute(){this._container.muted&&await this.toggleMute()}async volumeDown(){const t=this._container,e=t.actualOptions.sounds;e?.enable&&(t.muted&&(this._volume=0),this._volume-=e.volume.step,await this._updateVolume())}async volumeUp(){const t=this._container.actualOptions.sounds;t?.enable&&(this._volume+=t.volume.step,await this._updateVolume())}_getAudioContext(){const t=this._container;return t.audioContext||(t.audioContext=new AudioContext),t.audioContext}}}}]);
\No newline at end of file