@tsparticles/plugin-sounds 4.0.0-alpha.2 → 4.0.0-alpha.20

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.
Files changed (67) hide show
  1. package/249.min.js +1 -0
  2. package/807.min.js +33 -0
  3. package/browser/Options/Classes/Sounds.js +5 -0
  4. package/browser/Options/Classes/SoundsAudio.js +2 -0
  5. package/browser/Options/Classes/SoundsEvent.js +5 -0
  6. package/browser/Options/Classes/SoundsIcon.js +5 -0
  7. package/browser/Options/Classes/SoundsIcons.js +5 -0
  8. package/browser/Options/Classes/SoundsMelody.js +3 -0
  9. package/browser/Options/Classes/SoundsNote.js +2 -0
  10. package/browser/Options/Classes/SoundsVolume.js +4 -0
  11. package/browser/SoundsPlugin.js +7 -6
  12. package/browser/SoundsPluginInstance.js +437 -0
  13. package/browser/constants.js +1 -0
  14. package/browser/index.js +3 -3
  15. package/cjs/Options/Classes/Sounds.js +5 -0
  16. package/cjs/Options/Classes/SoundsAudio.js +2 -0
  17. package/cjs/Options/Classes/SoundsEvent.js +5 -0
  18. package/cjs/Options/Classes/SoundsIcon.js +5 -0
  19. package/cjs/Options/Classes/SoundsIcons.js +5 -0
  20. package/cjs/Options/Classes/SoundsMelody.js +3 -0
  21. package/cjs/Options/Classes/SoundsNote.js +2 -0
  22. package/cjs/Options/Classes/SoundsVolume.js +4 -0
  23. package/cjs/SoundsPlugin.js +7 -6
  24. package/cjs/SoundsPluginInstance.js +437 -0
  25. package/cjs/constants.js +1 -0
  26. package/cjs/index.js +3 -3
  27. package/dist_browser_SoundsPluginInstance_js.js +40 -0
  28. package/dist_browser_SoundsPlugin_js.js +16 -26
  29. package/esm/Options/Classes/Sounds.js +5 -0
  30. package/esm/Options/Classes/SoundsAudio.js +2 -0
  31. package/esm/Options/Classes/SoundsEvent.js +5 -0
  32. package/esm/Options/Classes/SoundsIcon.js +5 -0
  33. package/esm/Options/Classes/SoundsIcons.js +5 -0
  34. package/esm/Options/Classes/SoundsMelody.js +3 -0
  35. package/esm/Options/Classes/SoundsNote.js +2 -0
  36. package/esm/Options/Classes/SoundsVolume.js +4 -0
  37. package/esm/SoundsPlugin.js +7 -6
  38. package/esm/SoundsPluginInstance.js +437 -0
  39. package/esm/constants.js +1 -0
  40. package/esm/index.js +3 -3
  41. package/package.json +2 -2
  42. package/report.html +3 -3
  43. package/tsparticles.plugin.sounds.js +11 -11
  44. package/tsparticles.plugin.sounds.min.js +2 -2
  45. package/types/SoundsPlugin.d.ts +4 -5
  46. package/types/{SoundsInstance.d.ts → SoundsPluginInstance.d.ts} +1 -1
  47. package/types/constants.d.ts +1 -0
  48. package/types/index.d.ts +1 -1
  49. package/umd/Options/Classes/Sounds.js +5 -0
  50. package/umd/Options/Classes/SoundsAudio.js +2 -0
  51. package/umd/Options/Classes/SoundsEvent.js +5 -0
  52. package/umd/Options/Classes/SoundsIcon.js +5 -0
  53. package/umd/Options/Classes/SoundsIcons.js +5 -0
  54. package/umd/Options/Classes/SoundsMelody.js +3 -0
  55. package/umd/Options/Classes/SoundsNote.js +2 -0
  56. package/umd/Options/Classes/SoundsVolume.js +4 -0
  57. package/umd/SoundsPlugin.js +46 -11
  58. package/umd/SoundsPluginInstance.js +451 -0
  59. package/umd/constants.js +14 -0
  60. package/umd/index.js +3 -3
  61. package/902.min.js +0 -2
  62. package/902.min.js.LICENSE.txt +0 -1
  63. package/browser/SoundsInstance.js +0 -426
  64. package/cjs/SoundsInstance.js +0 -426
  65. package/esm/SoundsInstance.js +0 -426
  66. package/tsparticles.plugin.sounds.min.js.LICENSE.txt +0 -1
  67. package/umd/SoundsInstance.js +0 -440
@@ -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/esm/index.js CHANGED
@@ -1,6 +1,6 @@
1
- export function loadSoundsPlugin(engine) {
2
- engine.checkVersion("4.0.0-alpha.2");
3
- engine.register(async (e) => {
1
+ export async function loadSoundsPlugin(engine) {
2
+ engine.checkVersion("4.0.0-alpha.20");
3
+ await engine.register(async (e) => {
4
4
  const { SoundsPlugin } = await import("./SoundsPlugin.js");
5
5
  e.addPlugin(new SoundsPlugin(engine));
6
6
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsparticles/plugin-sounds",
3
- "version": "4.0.0-alpha.2",
3
+ "version": "4.0.0-alpha.20",
4
4
  "description": "tsParticles sounds plugin",
5
5
  "homepage": "https://particles.js.org",
6
6
  "repository": {
@@ -86,7 +86,7 @@
86
86
  "./package.json": "./package.json"
87
87
  },
88
88
  "dependencies": {
89
- "@tsparticles/engine": "4.0.0-alpha.2"
89
+ "@tsparticles/engine": "4.0.0-alpha.20"
90
90
  },
91
91
  "publishConfig": {
92
92
  "access": "public"