@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.
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
@@ -1,440 +0,0 @@
1
- (function (factory) {
2
- if (typeof module === "object" && typeof module.exports === "object") {
3
- var v = factory(require, exports);
4
- if (v !== undefined) module.exports = v;
5
- }
6
- else if (typeof define === "function" && define.amd) {
7
- define(["require", "exports", "@tsparticles/engine", "./enums.js", "./utils.js"], factory);
8
- }
9
- })(function (require, exports) {
10
- "use strict";
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.SoundsInstance = void 0;
13
- const engine_1 = require("@tsparticles/engine");
14
- const enums_js_1 = require("./enums.js");
15
- const utils_js_1 = require("./utils.js");
16
- const zIndexOffset = 1, rightOffset = 1, minVolume = 0;
17
- function initImage(data) {
18
- const img = (0, engine_1.safeDocument)().createElement("img"), { clickCb, container, display, iconOptions, margin, options, pos, rightOffsets } = data, { width, path, style, svg } = iconOptions, defaultAccumulator = 0;
19
- setIconStyle(img, pos.top + margin, pos.right -
20
- (margin * (rightOffsets.length + rightOffset) +
21
- width +
22
- rightOffsets.reduce((a, b) => a + b, defaultAccumulator)), display, options.fullScreen.zIndex + zIndexOffset, width, margin, style);
23
- img.src = path ?? (svg ? `data:image/svg+xml;base64,${btoa(svg)}` : "");
24
- const parent = container.canvas.element?.parentNode ?? (0, engine_1.safeDocument)().body;
25
- parent.append(img);
26
- img.addEventListener("click", () => {
27
- void clickCb();
28
- });
29
- return img;
30
- }
31
- function removeImage(image) {
32
- if (!image) {
33
- return;
34
- }
35
- image.remove();
36
- }
37
- function setIconStyle(icon, top, left, display, zIndex, width, margin, style) {
38
- icon.style.userSelect = "none";
39
- icon.style.position = "absolute";
40
- icon.style.top = `${(top + margin).toString()}px`;
41
- icon.style.left = `${(left - margin - width).toString()}px`;
42
- icon.style.display = display;
43
- icon.style.zIndex = (zIndex + zIndexOffset).toString();
44
- icon.style.cssText += style;
45
- }
46
- class SoundsInstance {
47
- constructor(container, engine) {
48
- this._addBuffer = audioCtx => {
49
- const buffer = audioCtx.createBufferSource();
50
- this._audioSources.push(buffer);
51
- return buffer;
52
- };
53
- this._addOscillator = audioCtx => {
54
- const oscillator = audioCtx.createOscillator();
55
- this._audioSources.push(oscillator);
56
- return oscillator;
57
- };
58
- this._initEvents = () => {
59
- const container = this._container, soundsOptions = container.actualOptions.sounds;
60
- if (!soundsOptions?.enable || !container.canvas.element) {
61
- return;
62
- }
63
- for (const event of soundsOptions.events) {
64
- const cb = (args) => {
65
- void (async () => {
66
- const filterNotValid = event.filter && !event.filter(args);
67
- if (this._container !== args.container) {
68
- return;
69
- }
70
- if (!!this._container.muted || this._container.destroyed) {
71
- (0, engine_1.executeOnSingleOrMultiple)(event.event, item => {
72
- this._engine.removeEventListener(item, cb);
73
- });
74
- return;
75
- }
76
- if (filterNotValid) {
77
- return;
78
- }
79
- const defaultNoteIndex = 0;
80
- if (event.audio) {
81
- const audio = (0, engine_1.itemFromSingleOrMultiple)(event.audio);
82
- if (!audio) {
83
- return;
84
- }
85
- this._playBuffer(audio);
86
- }
87
- else if (event.melodies) {
88
- const melody = (0, engine_1.itemFromArray)(event.melodies);
89
- if (!melody) {
90
- return;
91
- }
92
- if (melody.melodies.length) {
93
- await Promise.allSettled(melody.melodies.map(m => this._playNote(m.notes, defaultNoteIndex, melody.loop)));
94
- }
95
- else {
96
- await this._playNote(melody.notes, defaultNoteIndex, melody.loop);
97
- }
98
- }
99
- else if (event.notes) {
100
- const note = (0, engine_1.itemFromArray)(event.notes);
101
- if (!note) {
102
- return;
103
- }
104
- await this._playNote([note], defaultNoteIndex, false);
105
- }
106
- })();
107
- };
108
- (0, engine_1.executeOnSingleOrMultiple)(event.event, item => {
109
- this._engine.addEventListener(item, cb);
110
- });
111
- }
112
- };
113
- this._mute = async () => {
114
- const container = this._container, audioContext = this._getAudioContext();
115
- for (const source of this._audioSources) {
116
- this._removeAudioSource(source);
117
- }
118
- if (this._gain) {
119
- this._gain.disconnect();
120
- }
121
- await audioContext.close();
122
- container.audioContext = undefined;
123
- this._engine.dispatchEvent(enums_js_1.SoundsEventType.mute, { container: this._container });
124
- };
125
- this._playBuffer = audio => {
126
- const audioBuffer = this._audioMap.get(audio.source);
127
- if (!audioBuffer) {
128
- return;
129
- }
130
- const audioCtx = this._container.audioContext;
131
- if (!audioCtx) {
132
- return;
133
- }
134
- const source = this._addBuffer(audioCtx);
135
- source.loop = audio.loop;
136
- source.buffer = audioBuffer;
137
- source.connect(this._gain ?? audioCtx.destination);
138
- source.start();
139
- };
140
- this._playFrequency = async (frequency, duration) => {
141
- if (!this._gain || this._container.muted) {
142
- return;
143
- }
144
- const audioContext = this._getAudioContext(), oscillator = this._addOscillator(audioContext);
145
- oscillator.connect(this._gain);
146
- oscillator.type = "sine";
147
- oscillator.frequency.value = frequency;
148
- oscillator.start();
149
- return new Promise(resolve => {
150
- setTimeout(() => {
151
- this._removeAudioSource(oscillator);
152
- resolve();
153
- }, duration);
154
- });
155
- };
156
- this._playMuteSound = () => {
157
- if (this._container.muted) {
158
- return;
159
- }
160
- const audioContext = this._getAudioContext(), gain = audioContext.createGain();
161
- gain.connect(audioContext.destination);
162
- gain.gain.value = 0;
163
- const oscillator = audioContext.createOscillator();
164
- oscillator.connect(gain);
165
- oscillator.type = "sine";
166
- oscillator.frequency.value = 1;
167
- oscillator.start();
168
- setTimeout(() => {
169
- oscillator.stop();
170
- oscillator.disconnect();
171
- gain.disconnect();
172
- });
173
- };
174
- this._playNote = async (notes, noteIdx, loop) => {
175
- if (this._container.muted) {
176
- return;
177
- }
178
- const note = notes[noteIdx];
179
- if (!note) {
180
- return;
181
- }
182
- const value = note.value, promises = (0, engine_1.executeOnSingleOrMultiple)(value, async (_, idx) => {
183
- return this._playNoteValue(notes, noteIdx, idx);
184
- });
185
- await ((0, engine_1.isArray)(promises) ? Promise.allSettled(promises) : promises);
186
- const indexOffset = 1;
187
- let nextNoteIdx = noteIdx + indexOffset;
188
- if (loop && nextNoteIdx >= notes.length) {
189
- nextNoteIdx = nextNoteIdx % notes.length;
190
- }
191
- await this._playNote(notes, nextNoteIdx, loop);
192
- };
193
- this._playNoteValue = async (notes, noteIdx, valueIdx) => {
194
- const note = notes[noteIdx];
195
- if (!note) {
196
- return;
197
- }
198
- const value = (0, engine_1.itemFromSingleOrMultiple)(note.value, valueIdx, true);
199
- if (!value) {
200
- return;
201
- }
202
- try {
203
- const freq = (0, utils_js_1.getNoteFrequency)(value);
204
- if (!(0, engine_1.isNumber)(freq)) {
205
- return;
206
- }
207
- await this._playFrequency(freq, note.duration);
208
- }
209
- catch (e) {
210
- (0, engine_1.getLogger)().error(e);
211
- }
212
- };
213
- this._removeAudioSource = source => {
214
- source.stop();
215
- source.disconnect();
216
- const deleteCount = 1;
217
- this._audioSources.splice(this._audioSources.indexOf(source), deleteCount);
218
- };
219
- this._unmute = () => {
220
- const container = this._container, options = container.actualOptions, soundsOptions = options.sounds;
221
- if (!soundsOptions) {
222
- return;
223
- }
224
- const audioContext = this._getAudioContext(), gain = audioContext.createGain();
225
- gain.connect(audioContext.destination);
226
- gain.gain.value = soundsOptions.volume.value / engine_1.percentDenominator;
227
- this._gain = gain;
228
- this._initEvents();
229
- this._engine.dispatchEvent(enums_js_1.SoundsEventType.unmute, { container: this._container });
230
- };
231
- this._updateMuteIcons = () => {
232
- const container = this._container, soundsOptions = container.actualOptions.sounds;
233
- if (!soundsOptions?.enable || !soundsOptions.icons.enable) {
234
- return;
235
- }
236
- const muteImg = this._muteImg, unmuteImg = this._unmuteImg;
237
- if (muteImg) {
238
- muteImg.style.display = container.muted ? "block" : "none";
239
- }
240
- if (unmuteImg) {
241
- unmuteImg.style.display = container.muted ? "none" : "block";
242
- }
243
- };
244
- this._updateMuteStatus = async () => {
245
- const container = this._container, audioContext = this._getAudioContext();
246
- if (container.muted) {
247
- await audioContext.suspend();
248
- await this._mute();
249
- }
250
- else {
251
- await audioContext.resume();
252
- this._unmute();
253
- this._playMuteSound();
254
- }
255
- };
256
- this._updateVolume = async () => {
257
- const container = this._container, soundsOptions = container.actualOptions.sounds;
258
- if (!soundsOptions?.enable) {
259
- return;
260
- }
261
- (0, engine_1.clamp)(this._volume, soundsOptions.volume.min, soundsOptions.volume.max);
262
- let stateChanged = false;
263
- if (this._volume <= minVolume && !container.muted) {
264
- this._volume = 0;
265
- container.muted = true;
266
- stateChanged = true;
267
- }
268
- else if (this._volume > minVolume && container.muted) {
269
- container.muted = false;
270
- stateChanged = true;
271
- }
272
- if (stateChanged) {
273
- this._updateMuteIcons();
274
- await this._updateMuteStatus();
275
- }
276
- if (this._gain?.gain) {
277
- this._gain.gain.value = this._volume / engine_1.percentDenominator;
278
- }
279
- };
280
- this._container = container;
281
- this._engine = engine;
282
- this._volume = 0;
283
- this._audioSources = [];
284
- this._audioMap = new Map();
285
- }
286
- async init() {
287
- const container = this._container, options = container.actualOptions, soundsOptions = options.sounds;
288
- if (!soundsOptions?.enable) {
289
- return;
290
- }
291
- if (soundsOptions.autoPlay && (0, utils_js_1.isWindowMuted)()) {
292
- const firstClickHandler = () => {
293
- removeEventListener(engine_1.mouseDownEvent, firstClickHandler);
294
- removeEventListener(engine_1.touchStartEvent, firstClickHandler);
295
- (0, utils_js_1.unmuteWindow)();
296
- void this.unmute();
297
- };
298
- const listenerOptions = {
299
- capture: true,
300
- once: true,
301
- };
302
- addEventListener(engine_1.mouseDownEvent, firstClickHandler, listenerOptions);
303
- addEventListener(engine_1.touchStartEvent, firstClickHandler, listenerOptions);
304
- }
305
- this._volume = soundsOptions.volume.value;
306
- const events = soundsOptions.events;
307
- this._audioMap = new Map();
308
- for (const event of events) {
309
- if (!event.audio) {
310
- continue;
311
- }
312
- const promises = (0, engine_1.executeOnSingleOrMultiple)(event.audio, async (audio) => {
313
- const response = await fetch(audio.source);
314
- if (!response.ok) {
315
- return;
316
- }
317
- const arrayBuffer = await response.arrayBuffer(), audioContext = this._getAudioContext(), audioBuffer = await audioContext.decodeAudioData(arrayBuffer);
318
- this._audioMap.set(audio.source, audioBuffer);
319
- });
320
- if (promises instanceof Promise) {
321
- await promises;
322
- }
323
- else {
324
- await Promise.allSettled(promises);
325
- }
326
- }
327
- }
328
- async mute() {
329
- if (!this._container.muted) {
330
- await this.toggleMute();
331
- }
332
- }
333
- async start() {
334
- const container = this._container, options = container.actualOptions, soundsOptions = options.sounds;
335
- if (!soundsOptions?.enable || !container.canvas.element) {
336
- return;
337
- }
338
- container.muted = true;
339
- const canvas = container.canvas.element, pos = {
340
- top: canvas.offsetTop,
341
- right: canvas.offsetLeft + canvas.offsetWidth,
342
- }, { mute, unmute, volumeDown, volumeUp } = soundsOptions.icons, margin = 10, toggleMute = async () => {
343
- await this.toggleMute();
344
- }, enableIcons = soundsOptions.icons.enable, display = enableIcons ? enums_js_1.ImageDisplay.Block : enums_js_1.ImageDisplay.None;
345
- this._muteImg = initImage({
346
- container,
347
- options,
348
- pos,
349
- display,
350
- iconOptions: mute,
351
- margin,
352
- rightOffsets: [volumeDown.width, volumeUp.width],
353
- clickCb: toggleMute,
354
- });
355
- this._unmuteImg = initImage({
356
- container,
357
- options,
358
- pos,
359
- display: enums_js_1.ImageDisplay.None,
360
- iconOptions: unmute,
361
- margin,
362
- rightOffsets: [volumeDown.width, volumeUp.width],
363
- clickCb: toggleMute,
364
- });
365
- this._volumeDownImg = initImage({
366
- container,
367
- options,
368
- pos,
369
- display,
370
- iconOptions: volumeDown,
371
- margin,
372
- rightOffsets: [volumeUp.width],
373
- clickCb: async () => {
374
- await this.volumeDown();
375
- },
376
- });
377
- this._volumeUpImg = initImage({
378
- container,
379
- options,
380
- pos,
381
- display,
382
- iconOptions: volumeUp,
383
- margin,
384
- rightOffsets: [],
385
- clickCb: async () => {
386
- await this.volumeUp();
387
- },
388
- });
389
- if (!(0, utils_js_1.isWindowMuted)() && soundsOptions.autoPlay) {
390
- await this.unmute();
391
- }
392
- }
393
- stop() {
394
- this._container.muted = true;
395
- void (async () => {
396
- await this._mute();
397
- removeImage(this._muteImg);
398
- removeImage(this._unmuteImg);
399
- removeImage(this._volumeDownImg);
400
- removeImage(this._volumeUpImg);
401
- })();
402
- }
403
- async toggleMute() {
404
- const container = this._container;
405
- container.muted = !container.muted;
406
- this._updateMuteIcons();
407
- await this._updateMuteStatus();
408
- }
409
- async unmute() {
410
- if (this._container.muted) {
411
- await this.toggleMute();
412
- }
413
- }
414
- async volumeDown() {
415
- const container = this._container, soundsOptions = container.actualOptions.sounds;
416
- if (!soundsOptions?.enable) {
417
- return;
418
- }
419
- if (container.muted) {
420
- this._volume = 0;
421
- }
422
- this._volume -= soundsOptions.volume.step;
423
- await this._updateVolume();
424
- }
425
- async volumeUp() {
426
- const container = this._container, soundsOptions = container.actualOptions.sounds;
427
- if (!soundsOptions?.enable) {
428
- return;
429
- }
430
- this._volume += soundsOptions.volume.step;
431
- await this._updateVolume();
432
- }
433
- _getAudioContext() {
434
- const container = this._container;
435
- container.audioContext ??= new AudioContext();
436
- return container.audioContext;
437
- }
438
- }
439
- exports.SoundsInstance = SoundsInstance;
440
- });