@tsparticles/plugin-sounds 4.0.0-alpha.2 → 4.0.0-alpha.21
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/249.min.js +1 -0
- package/807.min.js +33 -0
- package/browser/Options/Classes/Sounds.js +5 -0
- package/browser/Options/Classes/SoundsAudio.js +2 -0
- package/browser/Options/Classes/SoundsEvent.js +5 -0
- package/browser/Options/Classes/SoundsIcon.js +5 -0
- package/browser/Options/Classes/SoundsIcons.js +5 -0
- package/browser/Options/Classes/SoundsMelody.js +3 -0
- package/browser/Options/Classes/SoundsNote.js +2 -0
- package/browser/Options/Classes/SoundsVolume.js +4 -0
- package/browser/SoundsPlugin.js +7 -6
- package/browser/SoundsPluginInstance.js +437 -0
- package/browser/constants.js +1 -0
- package/browser/index.js +3 -3
- package/cjs/Options/Classes/Sounds.js +5 -0
- package/cjs/Options/Classes/SoundsAudio.js +2 -0
- package/cjs/Options/Classes/SoundsEvent.js +5 -0
- package/cjs/Options/Classes/SoundsIcon.js +5 -0
- package/cjs/Options/Classes/SoundsIcons.js +5 -0
- package/cjs/Options/Classes/SoundsMelody.js +3 -0
- package/cjs/Options/Classes/SoundsNote.js +2 -0
- package/cjs/Options/Classes/SoundsVolume.js +4 -0
- package/cjs/SoundsPlugin.js +7 -6
- package/cjs/SoundsPluginInstance.js +437 -0
- package/cjs/constants.js +1 -0
- package/cjs/index.js +3 -3
- package/dist_browser_SoundsPluginInstance_js.js +40 -0
- package/dist_browser_SoundsPlugin_js.js +16 -26
- package/esm/Options/Classes/Sounds.js +5 -0
- package/esm/Options/Classes/SoundsAudio.js +2 -0
- package/esm/Options/Classes/SoundsEvent.js +5 -0
- package/esm/Options/Classes/SoundsIcon.js +5 -0
- package/esm/Options/Classes/SoundsIcons.js +5 -0
- package/esm/Options/Classes/SoundsMelody.js +3 -0
- package/esm/Options/Classes/SoundsNote.js +2 -0
- package/esm/Options/Classes/SoundsVolume.js +4 -0
- package/esm/SoundsPlugin.js +7 -6
- package/esm/SoundsPluginInstance.js +437 -0
- package/esm/constants.js +1 -0
- package/esm/index.js +3 -3
- package/package.json +2 -2
- package/report.html +3 -3
- package/tsparticles.plugin.sounds.js +11 -11
- package/tsparticles.plugin.sounds.min.js +2 -2
- package/types/SoundsPlugin.d.ts +4 -5
- package/types/{SoundsInstance.d.ts → SoundsPluginInstance.d.ts} +1 -1
- package/types/constants.d.ts +1 -0
- package/types/index.d.ts +1 -1
- package/umd/Options/Classes/Sounds.js +5 -0
- package/umd/Options/Classes/SoundsAudio.js +2 -0
- package/umd/Options/Classes/SoundsEvent.js +5 -0
- package/umd/Options/Classes/SoundsIcon.js +5 -0
- package/umd/Options/Classes/SoundsIcons.js +5 -0
- package/umd/Options/Classes/SoundsMelody.js +3 -0
- package/umd/Options/Classes/SoundsNote.js +2 -0
- package/umd/Options/Classes/SoundsVolume.js +4 -0
- package/umd/SoundsPlugin.js +46 -11
- package/umd/SoundsPluginInstance.js +451 -0
- package/umd/constants.js +14 -0
- package/umd/index.js +3 -3
- package/902.min.js +0 -2
- package/902.min.js.LICENSE.txt +0 -1
- package/browser/SoundsInstance.js +0 -426
- package/cjs/SoundsInstance.js +0 -426
- package/esm/SoundsInstance.js +0 -426
- package/tsparticles.plugin.sounds.min.js.LICENSE.txt +0 -1
- package/umd/SoundsInstance.js +0 -440
package/249.min.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";(this.webpackChunk_tsparticles_plugin_sounds=this.webpackChunk_tsparticles_plugin_sounds||[]).push([[249],{249(t,e,i){i.d(e,{SoundsPluginInstance:()=>m});var n,s,a,o,u=i(303);(n=a||(a={})).mute="soundsMuted",n.unmute="soundsUnmuted",(s=o||(o={})).Block="block",s.None="none";var l=i(946),r=i(794);function c(t){var e,i,n,s,a,o,l,r;let c=(0,u.safeDocument)().createElement("img"),{clickCb:d,container:m,display:h,iconOptions:_,margin:p,options:g,pos:f,rightOffsets:v}=t,{width:y,path:w,style:O,svg:S}=_;return e=c,i=f.top+p,n=f.right-(p*(v.length+1)+y+v.reduce((t,e)=>t+e,0)),s=h,a=g.fullScreen.zIndex+1,o=y,l=p,r=O,e.style.userSelect="none",e.style.position="absolute",e.style.top=`${(i+l).toString()}px`,e.style.left=`${(n-l-o).toString()}px`,e.style.display=s,e.style.zIndex=(a+1).toString(),e.style.cssText+=r,c.src=w??(S?`data:image/svg+xml;base64,${btoa(S)}`:""),(m.canvas.element?.parentNode??(0,u.safeDocument)().body).append(c),c.addEventListener("click",()=>{d()}),c}function d(t){t&&t.remove()}class m{_audioMap;_audioSources;_container;_engine;_gain;_muteImg;_unmuteImg;_volume;_volumeDownImg;_volumeUpImg;constructor(t,e){this._container=t,this._engine=e,this._volume=0,this._audioSources=[],this._audioMap=new Map}async init(){let t=this._container.actualOptions.sounds;if(!t?.enable)return;if(t.autoPlay&&(0,l.E3)()){let t=()=>{removeEventListener(r.m,t),removeEventListener(r.s,t),(0,l.XK)(),this.unmute()},e={capture:!0,once:!0};addEventListener(r.m,t,e),addEventListener(r.s,t,e)}this._volume=t.volume.value;let e=t.events;for(let t of(this._audioMap=new Map,e)){if(!t.audio)continue;let e=(0,u.executeOnSingleOrMultiple)(t.audio,async t=>{let e=await fetch(t.source);if(!e.ok)return;let 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(){let t=this._container,e=t.actualOptions,i=e.sounds;if(!i?.enable||!t.canvas.element)return;t.muted=!0;let n=t.canvas.element,s={top:n.offsetTop,right:n.offsetLeft+n.offsetWidth},{mute:a,unmute:u,volumeDown:r,volumeUp:d}=i.icons,m=async()=>{await this.toggleMute()},h=i.icons.enable?o.Block:o.None;this._muteImg=c({container:t,options:e,pos:s,display:h,iconOptions:a,margin:10,rightOffsets:[r.width,d.width],clickCb:m}),this._unmuteImg=c({container:t,options:e,pos:s,display:o.None,iconOptions:u,margin:10,rightOffsets:[r.width,d.width],clickCb:m}),this._volumeDownImg=c({container:t,options:e,pos:s,display:h,iconOptions:r,margin:10,rightOffsets:[d.width],clickCb:async()=>{await this.volumeDown()}}),this._volumeUpImg=c({container:t,options:e,pos:s,display:h,iconOptions:d,margin:10,rightOffsets:[],clickCb:async()=>{await this.volumeUp()}}),!(0,l.E3)()&&i.autoPlay&&await this.unmute()}stop(){this._container.muted=!0,(async()=>{await this._mute(),d(this._muteImg),d(this._unmuteImg),d(this._volumeDownImg),d(this._volumeUpImg)})()}async toggleMute(){let t=this._container;t.muted=!t.muted,this._updateMuteIcons(),await this._updateMuteStatus()}async unmute(){this._container.muted&&await this.toggleMute()}async volumeDown(){let t=this._container,e=t.actualOptions.sounds;e?.enable&&(t.muted&&(this._volume=0),this._volume-=e.volume.step,await this._updateVolume())}async volumeUp(){let t=this._container.actualOptions.sounds;t?.enable&&(this._volume+=t.volume.step,await this._updateVolume())}_addBuffer=t=>{let e=t.createBufferSource();return this._audioSources.push(e),e};_addOscillator=t=>{let e=t.createOscillator();return this._audioSources.push(e),e};_getAudioContext(){let t=this._container;return t.audioContext??=new AudioContext,t.audioContext}_initEvents=()=>{let t=this._container,e=t.actualOptions.sounds;if(e?.enable&&t.canvas.element)for(let t of e.events){let e=i=>{i&&(async()=>{let n=t.filter&&!t.filter(i);if(this._container===i.container){if(this._container.muted||this._container.destroyed)return(0,u.executeOnSingleOrMultiple)(t.event,t=>{this._engine.removeEventListener(t,e)});if(!n){if(t.audio){let e=(0,u.itemFromSingleOrMultiple)(t.audio);if(!e)return;this._playBuffer(e)}else if(t.melodies){let e=(0,u.itemFromArray)(t.melodies);if(!e)return;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){let e=(0,u.itemFromArray)(t.notes);if(!e)return;await this._playNote([e],0,!1)}}}})()};(0,u.executeOnSingleOrMultiple)(t.event,t=>{this._engine.addEventListener(t,e)})}};_mute=async()=>{let t=this._container,e=this._getAudioContext();for(let t of this._audioSources)this._removeAudioSource(t);this._gain&&this._gain.disconnect(),await e.close(),t.audioContext=void 0,this._engine.dispatchEvent(a.mute,{container:this._container})};_playBuffer=t=>{let e=this._audioMap.get(t.source);if(!e)return;let i=this._container.audioContext;if(!i)return;let n=this._addBuffer(i);n.loop=t.loop,n.buffer=e,n.connect(this._gain??i.destination),n.start()};_playFrequency=async(t,e)=>{if(!this._gain||this._container.muted)return;let 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)})};_playMuteSound=()=>{if(this._container.muted)return;let t=this._getAudioContext(),e=t.createGain();e.connect(t.destination),e.gain.value=0;let i=t.createOscillator();i.connect(e),i.type="sine",i.frequency.value=1,i.start(),setTimeout(()=>{i.stop(),i.disconnect(),e.disconnect()})};_playNote=async(t,e,i)=>{if(this._container.muted)return;let n=t[e];if(!n)return;let s=n.value,a=(0,u.executeOnSingleOrMultiple)(s,async(i,n)=>this._playNoteValue(t,e,n));await ((0,u.isArray)(a)?Promise.allSettled(a):a);let o=e+1;i&&o>=t.length&&(o%=t.length),await this._playNote(t,o,i)};_playNoteValue=async(t,e,i)=>{let n=t[e];if(!n)return;let s=(0,u.itemFromSingleOrMultiple)(n.value,i,!0);if(s)try{let t=(0,l.jE)(s);if(!(0,u.isNumber)(t))return;await this._playFrequency(t,n.duration)}catch(t){(0,u.getLogger)().error(t)}};_removeAudioSource=t=>{t.stop(),t.disconnect(),this._audioSources.splice(this._audioSources.indexOf(t),1)};_unmute=()=>{let t=this._container.actualOptions.sounds;if(!t)return;let e=this._getAudioContext(),i=e.createGain();i.connect(e.destination),i.gain.value=t.volume.value/u.percentDenominator,this._gain=i,this._initEvents(),this._engine.dispatchEvent(a.unmute,{container:this._container})};_updateMuteIcons=()=>{let t=this._container,e=t.actualOptions.sounds;if(!e?.enable||!e.icons.enable)return;let i=this._muteImg,n=this._unmuteImg;i&&(i.style.display=t.muted?"block":"none"),n&&(n.style.display=t.muted?"none":"block")};_updateMuteStatus=async()=>{let t=this._container,e=this._getAudioContext();t.muted?(await e.suspend(),await this._mute()):(await e.resume(),this._unmute(),this._playMuteSound())};_updateVolume=async()=>{let t=this._container,e=t.actualOptions.sounds;if(!e?.enable)return;(0,u.clamp)(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/u.percentDenominator)}}}}]);
|
package/807.min.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";(this.webpackChunk_tsparticles_plugin_sounds=this.webpackChunk_tsparticles_plugin_sounds||[]).push([[807],{807(e,t,s){s.d(t,{SoundsPlugin:()=>m});var i=s(794),l=s(303);class o{loop;source;constructor(){this.loop=!1,this.source=""}load(e){(0,l.isNull)(e)||((0,l.isObject)(e)?(void 0!==e.loop&&(this.loop=e.loop),void 0!==e.source&&(this.source=e.source)):this.source=e)}}class n{duration;value;constructor(){this.duration=500,this.value=[]}load(e){(0,l.isNull)(e)||(void 0!==e.duration&&(this.duration=e.duration),void 0!==e.value&&(this.value=e.value))}}class a{loop;melodies;notes;constructor(){this.loop=!1,this.melodies=[],this.notes=[]}load(e){(0,l.isNull)(e)||(void 0!==e.loop&&(this.loop=e.loop),void 0!==e.melodies&&(this.melodies=e.melodies.map(e=>{let t=new a;return t.load(e),t})),void 0!==e.notes&&(this.notes=e.notes.map(e=>{let t=new n;return t.load(e),t})))}}class c{audio;event;filter;melodies;notes;constructor(){this.event=[],this.notes=[]}load(e){if(!(0,l.isNull)(e)&&(void 0!==e.event&&(this.event=e.event),void 0!==e.audio&&((0,l.isArray)(e.audio)?this.audio=e.audio.map(e=>{let t=new o;return t.load(e),t}):(this.audio=new o,this.audio.load(e.audio))),void 0!==e.notes&&(this.notes=e.notes.map(e=>{let t=new n;return t.load(e),t})),void 0!==e.melodies&&(this.melodies=e.melodies.map(e=>{let t=new a;return t.load(e),t})),e.filter))if((0,l.isString)(e.filter)){let t=globalThis[e.filter];(0,l.isFunction)(t)&&(this.filter=t)}else this.filter=e.filter}}class h{height;path;style;svg;width;constructor(){this.width=24,this.height=24,this.style=""}load(e){(0,l.isNull)(e)||(void 0!==e.path&&(this.path=e.path),void 0!==e.svg&&(this.svg=e.svg),void 0!==e.width&&(this.width=e.width),void 0!==e.height&&(this.height=e.height))}}class u{enable;mute;unmute;volumeDown;volumeUp;constructor(){this.mute=new h,this.unmute=new h,this.volumeDown=new h,this.volumeUp=new h,this.enable=!1,this.mute.svg=`<?xml version="1.0"?>
|
|
2
|
+
<svg baseProfile="tiny" height="24px" version="1.2" viewBox="0 0 24 24" width="24px"
|
|
3
|
+
xml:space="preserve" xmlns="http://www.w3.org/2000/svg"
|
|
4
|
+
xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
5
|
+
<g id="Layer_1">
|
|
6
|
+
<path fill="#fff" d="M19.707,5.293c-0.391-0.391-1.023-0.391-1.414,0l-1.551,1.551c-0.345-0.688-0.987-1.02-1.604-1.02c-0.449,0-0.905,0.152-1.356,0.453l-2.672,1.781C10.357,8.561,8.904,9,8,9c-1.654,0-3,1.346-3,3v2c0,1.237,0.754,2.302,1.826,2.76l-1.533,1.533c-0.391,0.391-0.391,1.023,0,1.414C5.488,19.902,5.744,20,6,20s0.512-0.098,0.707-0.293l2.527-2.527c0.697,0.174,1.416,0.455,1.875,0.762l2.672,1.781c0.451,0.301,0.907,0.453,1.356,0.453C16.035,20.176,17,19.495,17,18V9.414l2.707-2.707C20.098,6.316,20.098,5.684,19.707,5.293z M14.891,7.941c0.038-0.025,0.073-0.046,0.104-0.062C14.998,7.914,15,7.954,15,8v1.293l-2,2V9.202L14.891,7.941z M7,12c0-0.552,0.448-1,1-1c1.211,0,2.907-0.495,4-1.146v2.439l-2.83,2.83C8.757,15.046,8.356,15,8,15c-0.552,0-1-0.448-1-1V12z M10.301,15.406L12,13.707v2.439C11.519,15.859,10.925,15.604,10.301,15.406z M14.994,18.12c-0.03-0.016-0.065-0.036-0.104-0.062L13,16.798v-4.091l2-2V18C15,18.046,14.998,18.086,14.994,18.12z"/>
|
|
7
|
+
</g>
|
|
8
|
+
</svg>`,this.unmute.svg=`<?xml version="1.0"?>
|
|
9
|
+
<svg baseProfile="tiny" height="24px" version="1.2" viewBox="0 0 24 24" width="24px"
|
|
10
|
+
xml:space="preserve" xmlns="http://www.w3.org/2000/svg"
|
|
11
|
+
xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
12
|
+
<g id="Layer_1">
|
|
13
|
+
<path fill="#fff" d="M17.138,5.824c-0.449,0-0.905,0.152-1.356,0.453l-2.672,1.781C12.357,8.561,10.904,9,10,9c-1.654,0-3,1.346-3,3v2c0,1.654,1.346,3,3,3c0.904,0,2.357,0.439,3.109,0.941l2.672,1.781c0.451,0.301,0.907,0.453,1.356,0.453C18.035,20.176,19,19.495,19,18V8C19,6.505,18.035,5.824,17.138,5.824z M14,16.146C12.907,15.495,11.211,15,10,15c-0.552,0-1-0.448-1-1v-2c0-0.552,0.448-1,1-1c1.211,0,2.907-0.495,4-1.146V16.146z M17,18c0,0.046-0.002,0.086-0.006,0.12c-0.03-0.016-0.065-0.036-0.104-0.062L15,16.798V9.202l1.891-1.261c0.038-0.025,0.073-0.046,0.104-0.062C16.998,7.914,17,7.954,17,8V18z"/>
|
|
14
|
+
</g>
|
|
15
|
+
</svg>`,this.volumeDown.svg=`<?xml version="1.0"?>
|
|
16
|
+
<svg baseProfile="tiny" height="24px" version="1.2" viewBox="0 0 24 24" width="24px"
|
|
17
|
+
xml:space="preserve" xmlns="http://www.w3.org/2000/svg"
|
|
18
|
+
xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
19
|
+
<g id="Layer_1">
|
|
20
|
+
<path fill="#fff" d="M15.138,5.824c-0.449,0-0.905,0.152-1.356,0.453l-2.672,1.781C10.357,8.561,8.904,9,8,9c-1.654,0-3,1.346-3,3v2c0,1.654,1.346,3,3,3c0.904,0,2.357,0.439,3.109,0.941l2.672,1.781c0.451,0.301,0.907,0.453,1.356,0.453C16.035,20.176,17,19.495,17,18V8C17,6.505,16.035,5.824,15.138,5.824z M8,15c-0.552,0-1-0.448-1-1v-2c0-0.552,0.448-1,1-1c1.211,0,2.907-0.495,4-1.146v6.293C10.907,15.495,9.211,15,8,15z M15,18c0,0.046-0.002,0.086-0.006,0.12c-0.03-0.016-0.065-0.036-0.104-0.062L13,16.798V9.202l1.891-1.261c0.038-0.025,0.073-0.046,0.104-0.062C14.998,7.914,15,7.954,15,8V18z"/>
|
|
21
|
+
<path fill="#fff" d="M18.292,10.294c-0.39,0.391-0.39,1.023,0.002,1.414c0.345,0.345,0.535,0.803,0.535,1.291c0,0.489-0.19,0.948-0.536,1.294c-0.391,0.39-0.391,1.023,0,1.414C18.488,15.902,18.744,16,19,16s0.512-0.098,0.707-0.293c0.724-0.723,1.122-1.685,1.122-2.708s-0.398-1.984-1.123-2.707C19.317,9.903,18.683,9.901,18.292,10.294z"/>
|
|
22
|
+
</g>
|
|
23
|
+
</svg>`,this.volumeUp.svg=`<?xml version="1.0"?>
|
|
24
|
+
<svg baseProfile="tiny" height="24px" version="1.2" viewBox="0 0 24 24" width="24px"
|
|
25
|
+
xml:space="preserve" xmlns="http://www.w3.org/2000/svg"
|
|
26
|
+
xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
27
|
+
<g id="Layer_1">
|
|
28
|
+
<path fill="#fff" d="M16.706,10.292c-0.389-0.389-1.023-0.391-1.414,0.002c-0.39,0.391-0.39,1.023,0.002,1.414c0.345,0.345,0.535,0.803,0.535,1.291c0,0.489-0.19,0.948-0.536,1.294c-0.391,0.39-0.391,1.023,0,1.414C15.488,15.902,15.744,16,16,16s0.512-0.098,0.707-0.293c0.724-0.723,1.122-1.685,1.122-2.708S17.431,11.015,16.706,10.292z"/>
|
|
29
|
+
<path fill="#fff" d="M18.706,8.292c-0.391-0.389-1.023-0.39-1.414,0.002c-0.39,0.391-0.39,1.024,0.002,1.414c0.879,0.877,1.363,2.044,1.364,3.287c0.001,1.246-0.484,2.417-1.365,3.298c-0.391,0.391-0.391,1.023,0,1.414C17.488,17.902,17.744,18,18,18s0.512-0.098,0.707-0.293c1.259-1.259,1.952-2.933,1.951-4.713C20.657,11.217,19.964,9.547,18.706,8.292z"/>
|
|
30
|
+
<path fill="#fff" d="M20.706,6.292c-0.391-0.389-1.023-0.39-1.414,0.002c-0.39,0.391-0.39,1.024,0.002,1.414c1.412,1.409,2.191,3.285,2.192,5.284c0.002,2.002-0.777,3.885-2.193,5.301c-0.391,0.391-0.391,1.023,0,1.414C19.488,19.902,19.744,20,20,20s0.512-0.098,0.707-0.293c1.794-1.794,2.781-4.18,2.779-6.717C23.485,10.457,22.497,8.078,20.706,6.292z"/>
|
|
31
|
+
<path fill="#fff" d="M12.138,5.824c-0.449,0-0.905,0.152-1.356,0.453L8.109,8.059C7.357,8.561,5.904,9,5,9c-1.654,0-3,1.346-3,3v2c0,1.654,1.346,3,3,3c0.904,0,2.357,0.439,3.109,0.941l2.672,1.781c0.451,0.301,0.907,0.453,1.356,0.453C13.035,20.176,14,19.495,14,18V8C14,6.505,13.035,5.824,12.138,5.824z M5,15c-0.552,0-1-0.448-1-1v-2c0-0.552,0.448-1,1-1c1.211,0,2.907-0.495,4-1.146v6.293C7.907,15.495,6.211,15,5,15z M12,18c0,0.046-0.002,0.086-0.006,0.12c-0.03-0.016-0.065-0.036-0.104-0.062L10,16.798V9.202l1.891-1.261c0.038-0.025,0.073-0.046,0.104-0.062C11.998,7.914,12,7.954,12,8V18z"/>
|
|
32
|
+
</g>
|
|
33
|
+
</svg>`}load(e){(0,l.isNull)(e)||(void 0!==e.enable&&(this.enable=e.enable),this.mute.load(e.mute),this.unmute.load(e.unmute),this.volumeDown.load(e.volumeDown),this.volumeUp.load(e.volumeUp))}}class v{max;min;step;value;constructor(){this.value=100,this.max=100,this.min=0,this.step=10}load(e){(0,l.isNull)(e)||((0,l.isObject)(e)?(void 0!==e.max&&(this.max=e.max),void 0!==e.min&&(this.min=e.min),void 0!==e.step&&(this.step=e.step),void 0!==e.value&&(this.value=e.value)):this.value=e)}}class d{autoPlay;enable;events;icons;volume;constructor(){this.autoPlay=!0,this.enable=!1,this.events=[],this.icons=new u,this.volume=new v}load(e){(0,l.isNull)(e)||(void 0!==e.autoPlay&&(this.autoPlay=e.autoPlay),void 0!==e.enable&&(this.enable=e.enable),void 0!==e.events&&(this.events=e.events.map(e=>{let t=new c;return t.load(e),t})),this.icons.load(e.icons),void 0!==e.volume&&this.volume.load(e.volume))}}var r=s(946);let p=()=>{removeEventListener(i.m,p),removeEventListener(i.s,p),(0,r.XK)()};class m{id="sounds";_engine;constructor(e){this._engine=e;const t={capture:!0,once:!0};addEventListener(i.m,p,t),addEventListener(i.s,p,t)}async getPlugin(e){let{SoundsPluginInstance:t}=await s.e(249).then(s.bind(s,249));return new t(e,this._engine)}loadOptions(e,t,s){if(!this.needsPlugin(t)&&!this.needsPlugin(s))return;let i=t.sounds;i?.load===void 0&&(t.sounds=i=new d),i.load(s?.sounds)}needsPlugin(e){return e?.sounds?.enable??!1}}},794(e,t,s){s.d(t,{m:()=>i,s:()=>l});let i="pointerdown",l="touchstart"},946(e,t,s){s.d(t,{E3:()=>n,XK:()=>a,jE:()=>l});let i=new Map;function l(e){let t=/(([A-G]b?)(\d))|pause/i.exec(e);if(!t?.length)return;let s=t[2]??t[0],l=i.get(s);if(l)return l[parseInt(t[3]??"0")]}i.set("C",[16.35,32.7,65.41,130.81,261.63,523.25,1046.5,2093,4186.01]),i.set("Db",[17.32,34.65,69.3,138.59,277.18,554.37,1108.73,2217.46,4434.92]),i.set("D",[18.35,36.71,73.42,146.83,293.66,587.33,1174.66,2349.32,4698.63]),i.set("Eb",[19.45,38.89,77.78,155.56,311.13,622.25,1244.51,2489.02,4978.03]),i.set("E",[20.6,41.2,82.41,164.81,329.63,659.25,1318.51,2637.02,5274.04]),i.set("F",[21.83,43.65,87.31,174.61,349.23,698.46,1396.91,2793.83,5587.65]),i.set("Gb",[23.12,46.25,92.5,185,369.99,739.99,1479.98,2959.96,5919.91]),i.set("G",[24.5,49,98,196,392,783.99,1567.98,3135.96,6271.93]),i.set("Ab",[25.96,51.91,103.83,207.65,415.3,830.61,1661.22,3322.44,6644.88]),i.set("A",[27.5,55,110,220,440,880,1760,3520,7040]),i.set("Bb",[29.14,58.27,116.54,233.08,466.16,932.33,1864.66,3729.31,7458.62]),i.set("B",[30.87,61.74,123.47,246.94,493.88,987.77,1975.53,3951.07,7902.13]),i.set("pause",[0]);let o=!0,n=()=>o,a=()=>{o=!1}}}]);
|
|
@@ -3,6 +3,11 @@ import { SoundsEvent } from "./SoundsEvent.js";
|
|
|
3
3
|
import { SoundsIcons } from "./SoundsIcons.js";
|
|
4
4
|
import { SoundsVolume } from "./SoundsVolume.js";
|
|
5
5
|
export class Sounds {
|
|
6
|
+
autoPlay;
|
|
7
|
+
enable;
|
|
8
|
+
events;
|
|
9
|
+
icons;
|
|
10
|
+
volume;
|
|
6
11
|
constructor() {
|
|
7
12
|
this.autoPlay = true;
|
|
8
13
|
this.enable = false;
|
|
@@ -3,6 +3,11 @@ import { SoundsAudio } from "./SoundsAudio.js";
|
|
|
3
3
|
import { SoundsMelody } from "./SoundsMelody.js";
|
|
4
4
|
import { SoundsNote } from "./SoundsNote.js";
|
|
5
5
|
export class SoundsEvent {
|
|
6
|
+
audio;
|
|
7
|
+
event;
|
|
8
|
+
filter;
|
|
9
|
+
melodies;
|
|
10
|
+
notes;
|
|
6
11
|
constructor() {
|
|
7
12
|
this.event = [];
|
|
8
13
|
this.notes = [];
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { isNull } from "@tsparticles/engine";
|
|
2
2
|
import { SoundsIcon } from "./SoundsIcon.js";
|
|
3
3
|
export class SoundsIcons {
|
|
4
|
+
enable;
|
|
5
|
+
mute;
|
|
6
|
+
unmute;
|
|
7
|
+
volumeDown;
|
|
8
|
+
volumeUp;
|
|
4
9
|
constructor() {
|
|
5
10
|
this.mute = new SoundsIcon();
|
|
6
11
|
this.unmute = new SoundsIcon();
|
package/browser/SoundsPlugin.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { mouseDownEvent, touchStartEvent
|
|
1
|
+
import { mouseDownEvent, touchStartEvent } from "./constants.js";
|
|
2
2
|
import { Sounds } from "./Options/Classes/Sounds.js";
|
|
3
|
-
import { SoundsInstance } from "./SoundsInstance.js";
|
|
4
3
|
import { unmuteWindow } from "./utils.js";
|
|
5
4
|
const generalFirstClickHandler = () => {
|
|
6
5
|
removeEventListener(mouseDownEvent, generalFirstClickHandler);
|
|
@@ -8,8 +7,9 @@ const generalFirstClickHandler = () => {
|
|
|
8
7
|
unmuteWindow();
|
|
9
8
|
};
|
|
10
9
|
export class SoundsPlugin {
|
|
10
|
+
id = "sounds";
|
|
11
|
+
_engine;
|
|
11
12
|
constructor(engine) {
|
|
12
|
-
this.id = "sounds";
|
|
13
13
|
this._engine = engine;
|
|
14
14
|
const listenerOptions = {
|
|
15
15
|
capture: true,
|
|
@@ -18,10 +18,11 @@ export class SoundsPlugin {
|
|
|
18
18
|
addEventListener(mouseDownEvent, generalFirstClickHandler, listenerOptions);
|
|
19
19
|
addEventListener(touchStartEvent, generalFirstClickHandler, listenerOptions);
|
|
20
20
|
}
|
|
21
|
-
getPlugin(container) {
|
|
22
|
-
|
|
21
|
+
async getPlugin(container) {
|
|
22
|
+
const { SoundsPluginInstance } = await import("./SoundsPluginInstance.js");
|
|
23
|
+
return new SoundsPluginInstance(container, this._engine);
|
|
23
24
|
}
|
|
24
|
-
loadOptions(options, source) {
|
|
25
|
+
loadOptions(_container, options, source) {
|
|
25
26
|
if (!this.needsPlugin(options) && !this.needsPlugin(source)) {
|
|
26
27
|
return;
|
|
27
28
|
}
|
|
@@ -0,0 +1,437 @@
|
|
|
1
|
+
import { clamp, executeOnSingleOrMultiple, getLogger, isArray, isNumber, itemFromArray, itemFromSingleOrMultiple, percentDenominator, safeDocument, } from "@tsparticles/engine";
|
|
2
|
+
import { ImageDisplay, SoundsEventType } from "./enums.js";
|
|
3
|
+
import { getNoteFrequency, isWindowMuted, unmuteWindow } from "./utils.js";
|
|
4
|
+
import { mouseDownEvent, touchStartEvent } from "./constants.js";
|
|
5
|
+
const zIndexOffset = 1, rightOffset = 1, minVolume = 0;
|
|
6
|
+
function initImage(data) {
|
|
7
|
+
const img = safeDocument().createElement("img"), { clickCb, container, display, iconOptions, margin, options, pos, rightOffsets } = data, { width, path, style, svg } = iconOptions, defaultAccumulator = 0;
|
|
8
|
+
setIconStyle(img, pos.top + margin, pos.right -
|
|
9
|
+
(margin * (rightOffsets.length + rightOffset) + width + rightOffsets.reduce((a, b) => a + b, defaultAccumulator)), display, options.fullScreen.zIndex + zIndexOffset, width, margin, style);
|
|
10
|
+
img.src = path ?? (svg ? `data:image/svg+xml;base64,${btoa(svg)}` : "");
|
|
11
|
+
const parent = container.canvas.element?.parentNode ?? safeDocument().body;
|
|
12
|
+
parent.append(img);
|
|
13
|
+
img.addEventListener("click", () => {
|
|
14
|
+
void clickCb();
|
|
15
|
+
});
|
|
16
|
+
return img;
|
|
17
|
+
}
|
|
18
|
+
function removeImage(image) {
|
|
19
|
+
if (!image) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
image.remove();
|
|
23
|
+
}
|
|
24
|
+
function setIconStyle(icon, top, left, display, zIndex, width, margin, style) {
|
|
25
|
+
icon.style.userSelect = "none";
|
|
26
|
+
icon.style.position = "absolute";
|
|
27
|
+
icon.style.top = `${(top + margin).toString()}px`;
|
|
28
|
+
icon.style.left = `${(left - margin - width).toString()}px`;
|
|
29
|
+
icon.style.display = display;
|
|
30
|
+
icon.style.zIndex = (zIndex + zIndexOffset).toString();
|
|
31
|
+
icon.style.cssText += style;
|
|
32
|
+
}
|
|
33
|
+
export class SoundsPluginInstance {
|
|
34
|
+
_audioMap;
|
|
35
|
+
_audioSources;
|
|
36
|
+
_container;
|
|
37
|
+
_engine;
|
|
38
|
+
_gain;
|
|
39
|
+
_muteImg;
|
|
40
|
+
_unmuteImg;
|
|
41
|
+
_volume;
|
|
42
|
+
_volumeDownImg;
|
|
43
|
+
_volumeUpImg;
|
|
44
|
+
constructor(container, engine) {
|
|
45
|
+
this._container = container;
|
|
46
|
+
this._engine = engine;
|
|
47
|
+
this._volume = 0;
|
|
48
|
+
this._audioSources = [];
|
|
49
|
+
this._audioMap = new Map();
|
|
50
|
+
}
|
|
51
|
+
async init() {
|
|
52
|
+
const container = this._container, options = container.actualOptions, soundsOptions = options.sounds;
|
|
53
|
+
if (!soundsOptions?.enable) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
if (soundsOptions.autoPlay && isWindowMuted()) {
|
|
57
|
+
const firstClickHandler = () => {
|
|
58
|
+
removeEventListener(mouseDownEvent, firstClickHandler);
|
|
59
|
+
removeEventListener(touchStartEvent, firstClickHandler);
|
|
60
|
+
unmuteWindow();
|
|
61
|
+
void this.unmute();
|
|
62
|
+
}, listenerOptions = {
|
|
63
|
+
capture: true,
|
|
64
|
+
once: true,
|
|
65
|
+
};
|
|
66
|
+
addEventListener(mouseDownEvent, firstClickHandler, listenerOptions);
|
|
67
|
+
addEventListener(touchStartEvent, firstClickHandler, listenerOptions);
|
|
68
|
+
}
|
|
69
|
+
this._volume = soundsOptions.volume.value;
|
|
70
|
+
const events = soundsOptions.events;
|
|
71
|
+
this._audioMap = new Map();
|
|
72
|
+
for (const event of events) {
|
|
73
|
+
if (!event.audio) {
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
const promises = executeOnSingleOrMultiple(event.audio, async (audio) => {
|
|
77
|
+
const response = await fetch(audio.source);
|
|
78
|
+
if (!response.ok) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
const arrayBuffer = await response.arrayBuffer(), audioContext = this._getAudioContext(), audioBuffer = await audioContext.decodeAudioData(arrayBuffer);
|
|
82
|
+
this._audioMap.set(audio.source, audioBuffer);
|
|
83
|
+
});
|
|
84
|
+
if (promises instanceof Promise) {
|
|
85
|
+
await promises;
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
await Promise.allSettled(promises);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
async mute() {
|
|
93
|
+
if (!this._container.muted) {
|
|
94
|
+
await this.toggleMute();
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
async start() {
|
|
98
|
+
const container = this._container, options = container.actualOptions, soundsOptions = options.sounds;
|
|
99
|
+
if (!soundsOptions?.enable || !container.canvas.element) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
container.muted = true;
|
|
103
|
+
const canvas = container.canvas.element, pos = {
|
|
104
|
+
top: canvas.offsetTop,
|
|
105
|
+
right: canvas.offsetLeft + canvas.offsetWidth,
|
|
106
|
+
}, { mute, unmute, volumeDown, volumeUp } = soundsOptions.icons, margin = 10, toggleMute = async () => {
|
|
107
|
+
await this.toggleMute();
|
|
108
|
+
}, enableIcons = soundsOptions.icons.enable, display = enableIcons ? ImageDisplay.Block : ImageDisplay.None;
|
|
109
|
+
this._muteImg = initImage({
|
|
110
|
+
container,
|
|
111
|
+
options,
|
|
112
|
+
pos,
|
|
113
|
+
display,
|
|
114
|
+
iconOptions: mute,
|
|
115
|
+
margin,
|
|
116
|
+
rightOffsets: [volumeDown.width, volumeUp.width],
|
|
117
|
+
clickCb: toggleMute,
|
|
118
|
+
});
|
|
119
|
+
this._unmuteImg = initImage({
|
|
120
|
+
container,
|
|
121
|
+
options,
|
|
122
|
+
pos,
|
|
123
|
+
display: ImageDisplay.None,
|
|
124
|
+
iconOptions: unmute,
|
|
125
|
+
margin,
|
|
126
|
+
rightOffsets: [volumeDown.width, volumeUp.width],
|
|
127
|
+
clickCb: toggleMute,
|
|
128
|
+
});
|
|
129
|
+
this._volumeDownImg = initImage({
|
|
130
|
+
container,
|
|
131
|
+
options,
|
|
132
|
+
pos,
|
|
133
|
+
display,
|
|
134
|
+
iconOptions: volumeDown,
|
|
135
|
+
margin,
|
|
136
|
+
rightOffsets: [volumeUp.width],
|
|
137
|
+
clickCb: async () => {
|
|
138
|
+
await this.volumeDown();
|
|
139
|
+
},
|
|
140
|
+
});
|
|
141
|
+
this._volumeUpImg = initImage({
|
|
142
|
+
container,
|
|
143
|
+
options,
|
|
144
|
+
pos,
|
|
145
|
+
display,
|
|
146
|
+
iconOptions: volumeUp,
|
|
147
|
+
margin,
|
|
148
|
+
rightOffsets: [],
|
|
149
|
+
clickCb: async () => {
|
|
150
|
+
await this.volumeUp();
|
|
151
|
+
},
|
|
152
|
+
});
|
|
153
|
+
if (!isWindowMuted() && soundsOptions.autoPlay) {
|
|
154
|
+
await this.unmute();
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
stop() {
|
|
158
|
+
this._container.muted = true;
|
|
159
|
+
void (async () => {
|
|
160
|
+
await this._mute();
|
|
161
|
+
removeImage(this._muteImg);
|
|
162
|
+
removeImage(this._unmuteImg);
|
|
163
|
+
removeImage(this._volumeDownImg);
|
|
164
|
+
removeImage(this._volumeUpImg);
|
|
165
|
+
})();
|
|
166
|
+
}
|
|
167
|
+
async toggleMute() {
|
|
168
|
+
const container = this._container;
|
|
169
|
+
container.muted = !container.muted;
|
|
170
|
+
this._updateMuteIcons();
|
|
171
|
+
await this._updateMuteStatus();
|
|
172
|
+
}
|
|
173
|
+
async unmute() {
|
|
174
|
+
if (this._container.muted) {
|
|
175
|
+
await this.toggleMute();
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
async volumeDown() {
|
|
179
|
+
const container = this._container, soundsOptions = container.actualOptions.sounds;
|
|
180
|
+
if (!soundsOptions?.enable) {
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
if (container.muted) {
|
|
184
|
+
this._volume = 0;
|
|
185
|
+
}
|
|
186
|
+
this._volume -= soundsOptions.volume.step;
|
|
187
|
+
await this._updateVolume();
|
|
188
|
+
}
|
|
189
|
+
async volumeUp() {
|
|
190
|
+
const container = this._container, soundsOptions = container.actualOptions.sounds;
|
|
191
|
+
if (!soundsOptions?.enable) {
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
this._volume += soundsOptions.volume.step;
|
|
195
|
+
await this._updateVolume();
|
|
196
|
+
}
|
|
197
|
+
_addBuffer = audioCtx => {
|
|
198
|
+
const buffer = audioCtx.createBufferSource();
|
|
199
|
+
this._audioSources.push(buffer);
|
|
200
|
+
return buffer;
|
|
201
|
+
};
|
|
202
|
+
_addOscillator = audioCtx => {
|
|
203
|
+
const oscillator = audioCtx.createOscillator();
|
|
204
|
+
this._audioSources.push(oscillator);
|
|
205
|
+
return oscillator;
|
|
206
|
+
};
|
|
207
|
+
_getAudioContext() {
|
|
208
|
+
const container = this._container;
|
|
209
|
+
container.audioContext ??= new AudioContext();
|
|
210
|
+
return container.audioContext;
|
|
211
|
+
}
|
|
212
|
+
_initEvents = () => {
|
|
213
|
+
const container = this._container, soundsOptions = container.actualOptions.sounds;
|
|
214
|
+
if (!soundsOptions?.enable || !container.canvas.element) {
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
for (const event of soundsOptions.events) {
|
|
218
|
+
const cb = (args) => {
|
|
219
|
+
if (!args) {
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
void (async () => {
|
|
223
|
+
const filterNotValid = event.filter && !event.filter(args);
|
|
224
|
+
if (this._container !== args.container) {
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
if (!!this._container.muted || this._container.destroyed) {
|
|
228
|
+
executeOnSingleOrMultiple(event.event, item => {
|
|
229
|
+
this._engine.removeEventListener(item, cb);
|
|
230
|
+
});
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
if (filterNotValid) {
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
const defaultNoteIndex = 0;
|
|
237
|
+
if (event.audio) {
|
|
238
|
+
const audio = itemFromSingleOrMultiple(event.audio);
|
|
239
|
+
if (!audio) {
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
this._playBuffer(audio);
|
|
243
|
+
}
|
|
244
|
+
else if (event.melodies) {
|
|
245
|
+
const melody = itemFromArray(event.melodies);
|
|
246
|
+
if (!melody) {
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
if (melody.melodies.length) {
|
|
250
|
+
await Promise.allSettled(melody.melodies.map(m => this._playNote(m.notes, defaultNoteIndex, melody.loop)));
|
|
251
|
+
}
|
|
252
|
+
else {
|
|
253
|
+
await this._playNote(melody.notes, defaultNoteIndex, melody.loop);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
else if (event.notes) {
|
|
257
|
+
const note = itemFromArray(event.notes);
|
|
258
|
+
if (!note) {
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
await this._playNote([note], defaultNoteIndex, false);
|
|
262
|
+
}
|
|
263
|
+
})();
|
|
264
|
+
};
|
|
265
|
+
executeOnSingleOrMultiple(event.event, item => {
|
|
266
|
+
this._engine.addEventListener(item, cb);
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
_mute = async () => {
|
|
271
|
+
const container = this._container, audioContext = this._getAudioContext();
|
|
272
|
+
for (const source of this._audioSources) {
|
|
273
|
+
this._removeAudioSource(source);
|
|
274
|
+
}
|
|
275
|
+
if (this._gain) {
|
|
276
|
+
this._gain.disconnect();
|
|
277
|
+
}
|
|
278
|
+
await audioContext.close();
|
|
279
|
+
container.audioContext = undefined;
|
|
280
|
+
this._engine.dispatchEvent(SoundsEventType.mute, { container: this._container });
|
|
281
|
+
};
|
|
282
|
+
_playBuffer = audio => {
|
|
283
|
+
const audioBuffer = this._audioMap.get(audio.source);
|
|
284
|
+
if (!audioBuffer) {
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
const audioCtx = this._container.audioContext;
|
|
288
|
+
if (!audioCtx) {
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
const source = this._addBuffer(audioCtx);
|
|
292
|
+
source.loop = audio.loop;
|
|
293
|
+
source.buffer = audioBuffer;
|
|
294
|
+
source.connect(this._gain ?? audioCtx.destination);
|
|
295
|
+
source.start();
|
|
296
|
+
};
|
|
297
|
+
_playFrequency = async (frequency, duration) => {
|
|
298
|
+
if (!this._gain || this._container.muted) {
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
const audioContext = this._getAudioContext(), oscillator = this._addOscillator(audioContext);
|
|
302
|
+
oscillator.connect(this._gain);
|
|
303
|
+
oscillator.type = "sine";
|
|
304
|
+
oscillator.frequency.value = frequency;
|
|
305
|
+
oscillator.start();
|
|
306
|
+
return new Promise(resolve => {
|
|
307
|
+
setTimeout(() => {
|
|
308
|
+
this._removeAudioSource(oscillator);
|
|
309
|
+
resolve();
|
|
310
|
+
}, duration);
|
|
311
|
+
});
|
|
312
|
+
};
|
|
313
|
+
_playMuteSound = () => {
|
|
314
|
+
if (this._container.muted) {
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
const audioContext = this._getAudioContext(), gain = audioContext.createGain();
|
|
318
|
+
gain.connect(audioContext.destination);
|
|
319
|
+
gain.gain.value = 0;
|
|
320
|
+
const oscillator = audioContext.createOscillator();
|
|
321
|
+
oscillator.connect(gain);
|
|
322
|
+
oscillator.type = "sine";
|
|
323
|
+
oscillator.frequency.value = 1;
|
|
324
|
+
oscillator.start();
|
|
325
|
+
setTimeout(() => {
|
|
326
|
+
oscillator.stop();
|
|
327
|
+
oscillator.disconnect();
|
|
328
|
+
gain.disconnect();
|
|
329
|
+
});
|
|
330
|
+
};
|
|
331
|
+
_playNote = async (notes, noteIdx, loop) => {
|
|
332
|
+
if (this._container.muted) {
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
const note = notes[noteIdx];
|
|
336
|
+
if (!note) {
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
const value = note.value, promises = executeOnSingleOrMultiple(value, async (_, idx) => {
|
|
340
|
+
return this._playNoteValue(notes, noteIdx, idx);
|
|
341
|
+
});
|
|
342
|
+
await (isArray(promises) ? Promise.allSettled(promises) : promises);
|
|
343
|
+
const indexOffset = 1;
|
|
344
|
+
let nextNoteIdx = noteIdx + indexOffset;
|
|
345
|
+
if (loop && nextNoteIdx >= notes.length) {
|
|
346
|
+
nextNoteIdx = nextNoteIdx % notes.length;
|
|
347
|
+
}
|
|
348
|
+
await this._playNote(notes, nextNoteIdx, loop);
|
|
349
|
+
};
|
|
350
|
+
_playNoteValue = async (notes, noteIdx, valueIdx) => {
|
|
351
|
+
const note = notes[noteIdx];
|
|
352
|
+
if (!note) {
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
const value = itemFromSingleOrMultiple(note.value, valueIdx, true);
|
|
356
|
+
if (!value) {
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
try {
|
|
360
|
+
const freq = getNoteFrequency(value);
|
|
361
|
+
if (!isNumber(freq)) {
|
|
362
|
+
return;
|
|
363
|
+
}
|
|
364
|
+
await this._playFrequency(freq, note.duration);
|
|
365
|
+
}
|
|
366
|
+
catch (e) {
|
|
367
|
+
getLogger().error(e);
|
|
368
|
+
}
|
|
369
|
+
};
|
|
370
|
+
_removeAudioSource = source => {
|
|
371
|
+
source.stop();
|
|
372
|
+
source.disconnect();
|
|
373
|
+
const deleteCount = 1;
|
|
374
|
+
this._audioSources.splice(this._audioSources.indexOf(source), deleteCount);
|
|
375
|
+
};
|
|
376
|
+
_unmute = () => {
|
|
377
|
+
const container = this._container, options = container.actualOptions, soundsOptions = options.sounds;
|
|
378
|
+
if (!soundsOptions) {
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
const audioContext = this._getAudioContext(), gain = audioContext.createGain();
|
|
382
|
+
gain.connect(audioContext.destination);
|
|
383
|
+
gain.gain.value = soundsOptions.volume.value / percentDenominator;
|
|
384
|
+
this._gain = gain;
|
|
385
|
+
this._initEvents();
|
|
386
|
+
this._engine.dispatchEvent(SoundsEventType.unmute, { container: this._container });
|
|
387
|
+
};
|
|
388
|
+
_updateMuteIcons = () => {
|
|
389
|
+
const container = this._container, soundsOptions = container.actualOptions.sounds;
|
|
390
|
+
if (!soundsOptions?.enable || !soundsOptions.icons.enable) {
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
const muteImg = this._muteImg, unmuteImg = this._unmuteImg;
|
|
394
|
+
if (muteImg) {
|
|
395
|
+
muteImg.style.display = container.muted ? "block" : "none";
|
|
396
|
+
}
|
|
397
|
+
if (unmuteImg) {
|
|
398
|
+
unmuteImg.style.display = container.muted ? "none" : "block";
|
|
399
|
+
}
|
|
400
|
+
};
|
|
401
|
+
_updateMuteStatus = async () => {
|
|
402
|
+
const container = this._container, audioContext = this._getAudioContext();
|
|
403
|
+
if (container.muted) {
|
|
404
|
+
await audioContext.suspend();
|
|
405
|
+
await this._mute();
|
|
406
|
+
}
|
|
407
|
+
else {
|
|
408
|
+
await audioContext.resume();
|
|
409
|
+
this._unmute();
|
|
410
|
+
this._playMuteSound();
|
|
411
|
+
}
|
|
412
|
+
};
|
|
413
|
+
_updateVolume = async () => {
|
|
414
|
+
const container = this._container, soundsOptions = container.actualOptions.sounds;
|
|
415
|
+
if (!soundsOptions?.enable) {
|
|
416
|
+
return;
|
|
417
|
+
}
|
|
418
|
+
clamp(this._volume, soundsOptions.volume.min, soundsOptions.volume.max);
|
|
419
|
+
let stateChanged = false;
|
|
420
|
+
if (this._volume <= minVolume && !container.muted) {
|
|
421
|
+
this._volume = 0;
|
|
422
|
+
container.muted = true;
|
|
423
|
+
stateChanged = true;
|
|
424
|
+
}
|
|
425
|
+
else if (this._volume > minVolume && container.muted) {
|
|
426
|
+
container.muted = false;
|
|
427
|
+
stateChanged = true;
|
|
428
|
+
}
|
|
429
|
+
if (stateChanged) {
|
|
430
|
+
this._updateMuteIcons();
|
|
431
|
+
await this._updateMuteStatus();
|
|
432
|
+
}
|
|
433
|
+
if (this._gain?.gain) {
|
|
434
|
+
this._gain.gain.value = this._volume / percentDenominator;
|
|
435
|
+
}
|
|
436
|
+
};
|
|
437
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const mouseDownEvent = "pointerdown", touchStartEvent = "touchstart";
|
package/browser/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export function loadSoundsPlugin(engine) {
|
|
2
|
-
engine.checkVersion("4.0.0-alpha.
|
|
3
|
-
engine.register(async (e) => {
|
|
1
|
+
export async function loadSoundsPlugin(engine) {
|
|
2
|
+
engine.checkVersion("4.0.0-alpha.21");
|
|
3
|
+
await engine.register(async (e) => {
|
|
4
4
|
const { SoundsPlugin } = await import("./SoundsPlugin.js");
|
|
5
5
|
e.addPlugin(new SoundsPlugin(engine));
|
|
6
6
|
});
|
|
@@ -3,6 +3,11 @@ import { SoundsEvent } from "./SoundsEvent.js";
|
|
|
3
3
|
import { SoundsIcons } from "./SoundsIcons.js";
|
|
4
4
|
import { SoundsVolume } from "./SoundsVolume.js";
|
|
5
5
|
export class Sounds {
|
|
6
|
+
autoPlay;
|
|
7
|
+
enable;
|
|
8
|
+
events;
|
|
9
|
+
icons;
|
|
10
|
+
volume;
|
|
6
11
|
constructor() {
|
|
7
12
|
this.autoPlay = true;
|
|
8
13
|
this.enable = false;
|