@tsparticles/plugin-sounds 3.0.0-alpha.0

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 (112) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +70 -0
  3. package/browser/Options/Classes/Sounds.js +30 -0
  4. package/browser/Options/Classes/SoundsAudio.js +22 -0
  5. package/browser/Options/Classes/SoundsEvent.js +54 -0
  6. package/browser/Options/Classes/SoundsIcon.js +23 -0
  7. package/browser/Options/Classes/SoundsIcons.js +54 -0
  8. package/browser/Options/Classes/SoundsMelody.js +30 -0
  9. package/browser/Options/Classes/SoundsNote.js +17 -0
  10. package/browser/Options/Classes/SoundsVolume.js +30 -0
  11. package/browser/Options/Interfaces/ISounds.js +1 -0
  12. package/browser/Options/Interfaces/ISoundsAudio.js +1 -0
  13. package/browser/Options/Interfaces/ISoundsEvent.js +1 -0
  14. package/browser/Options/Interfaces/ISoundsIcon.js +1 -0
  15. package/browser/Options/Interfaces/ISoundsIcons.js +1 -0
  16. package/browser/Options/Interfaces/ISoundsMelody.js +1 -0
  17. package/browser/Options/Interfaces/ISoundsNote.js +1 -0
  18. package/browser/Options/Interfaces/ISoundsVolume.js +1 -0
  19. package/browser/SoundsInstance.js +336 -0
  20. package/browser/enums.js +1 -0
  21. package/browser/index.js +29 -0
  22. package/browser/types.js +1 -0
  23. package/browser/utils.js +26 -0
  24. package/cjs/Options/Classes/Sounds.js +34 -0
  25. package/cjs/Options/Classes/SoundsAudio.js +26 -0
  26. package/cjs/Options/Classes/SoundsEvent.js +58 -0
  27. package/cjs/Options/Classes/SoundsIcon.js +27 -0
  28. package/cjs/Options/Classes/SoundsIcons.js +58 -0
  29. package/cjs/Options/Classes/SoundsMelody.js +34 -0
  30. package/cjs/Options/Classes/SoundsNote.js +21 -0
  31. package/cjs/Options/Classes/SoundsVolume.js +34 -0
  32. package/cjs/Options/Interfaces/ISounds.js +2 -0
  33. package/cjs/Options/Interfaces/ISoundsAudio.js +2 -0
  34. package/cjs/Options/Interfaces/ISoundsEvent.js +2 -0
  35. package/cjs/Options/Interfaces/ISoundsIcon.js +2 -0
  36. package/cjs/Options/Interfaces/ISoundsIcons.js +2 -0
  37. package/cjs/Options/Interfaces/ISoundsMelody.js +2 -0
  38. package/cjs/Options/Interfaces/ISoundsNote.js +2 -0
  39. package/cjs/Options/Interfaces/ISoundsVolume.js +2 -0
  40. package/cjs/SoundsInstance.js +359 -0
  41. package/cjs/enums.js +2 -0
  42. package/cjs/index.js +44 -0
  43. package/cjs/types.js +2 -0
  44. package/cjs/utils.js +30 -0
  45. package/esm/Options/Classes/Sounds.js +30 -0
  46. package/esm/Options/Classes/SoundsAudio.js +22 -0
  47. package/esm/Options/Classes/SoundsEvent.js +54 -0
  48. package/esm/Options/Classes/SoundsIcon.js +23 -0
  49. package/esm/Options/Classes/SoundsIcons.js +54 -0
  50. package/esm/Options/Classes/SoundsMelody.js +30 -0
  51. package/esm/Options/Classes/SoundsNote.js +17 -0
  52. package/esm/Options/Classes/SoundsVolume.js +30 -0
  53. package/esm/Options/Interfaces/ISounds.js +1 -0
  54. package/esm/Options/Interfaces/ISoundsAudio.js +1 -0
  55. package/esm/Options/Interfaces/ISoundsEvent.js +1 -0
  56. package/esm/Options/Interfaces/ISoundsIcon.js +1 -0
  57. package/esm/Options/Interfaces/ISoundsIcons.js +1 -0
  58. package/esm/Options/Interfaces/ISoundsMelody.js +1 -0
  59. package/esm/Options/Interfaces/ISoundsNote.js +1 -0
  60. package/esm/Options/Interfaces/ISoundsVolume.js +1 -0
  61. package/esm/SoundsInstance.js +336 -0
  62. package/esm/enums.js +1 -0
  63. package/esm/index.js +29 -0
  64. package/esm/types.js +1 -0
  65. package/esm/utils.js +26 -0
  66. package/package.json +81 -0
  67. package/report.html +39 -0
  68. package/tsparticles.plugin.sounds.js +780 -0
  69. package/tsparticles.plugin.sounds.min.js +2 -0
  70. package/tsparticles.plugin.sounds.min.js.LICENSE.txt +8 -0
  71. package/types/Options/Classes/Sounds.d.ts +13 -0
  72. package/types/Options/Classes/SoundsAudio.d.ts +8 -0
  73. package/types/Options/Classes/SoundsEvent.d.ts +20 -0
  74. package/types/Options/Classes/SoundsIcon.d.ts +10 -0
  75. package/types/Options/Classes/SoundsIcons.d.ts +11 -0
  76. package/types/Options/Classes/SoundsMelody.d.ts +10 -0
  77. package/types/Options/Classes/SoundsNote.d.ts +8 -0
  78. package/types/Options/Classes/SoundsVolume.d.ts +10 -0
  79. package/types/Options/Interfaces/ISounds.d.ts +9 -0
  80. package/types/Options/Interfaces/ISoundsAudio.d.ts +4 -0
  81. package/types/Options/Interfaces/ISoundsEvent.d.ts +12 -0
  82. package/types/Options/Interfaces/ISoundsIcon.d.ts +6 -0
  83. package/types/Options/Interfaces/ISoundsIcons.d.ts +7 -0
  84. package/types/Options/Interfaces/ISoundsMelody.d.ts +6 -0
  85. package/types/Options/Interfaces/ISoundsNote.d.ts +5 -0
  86. package/types/Options/Interfaces/ISoundsVolume.d.ts +6 -0
  87. package/types/SoundsInstance.d.ts +32 -0
  88. package/types/enums.d.ts +4 -0
  89. package/types/index.d.ts +2 -0
  90. package/types/types.d.ts +15 -0
  91. package/types/utils.d.ts +1 -0
  92. package/umd/Options/Classes/Sounds.js +44 -0
  93. package/umd/Options/Classes/SoundsAudio.js +36 -0
  94. package/umd/Options/Classes/SoundsEvent.js +68 -0
  95. package/umd/Options/Classes/SoundsIcon.js +37 -0
  96. package/umd/Options/Classes/SoundsIcons.js +68 -0
  97. package/umd/Options/Classes/SoundsMelody.js +44 -0
  98. package/umd/Options/Classes/SoundsNote.js +31 -0
  99. package/umd/Options/Classes/SoundsVolume.js +44 -0
  100. package/umd/Options/Interfaces/ISounds.js +12 -0
  101. package/umd/Options/Interfaces/ISoundsAudio.js +12 -0
  102. package/umd/Options/Interfaces/ISoundsEvent.js +12 -0
  103. package/umd/Options/Interfaces/ISoundsIcon.js +12 -0
  104. package/umd/Options/Interfaces/ISoundsIcons.js +12 -0
  105. package/umd/Options/Interfaces/ISoundsMelody.js +12 -0
  106. package/umd/Options/Interfaces/ISoundsNote.js +12 -0
  107. package/umd/Options/Interfaces/ISoundsVolume.js +12 -0
  108. package/umd/SoundsInstance.js +350 -0
  109. package/umd/enums.js +12 -0
  110. package/umd/index.js +43 -0
  111. package/umd/types.js +12 -0
  112. package/umd/utils.js +40 -0
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SoundsVolume = void 0;
4
+ class SoundsVolume {
5
+ constructor() {
6
+ this.value = 100;
7
+ this.max = 100;
8
+ this.min = 0;
9
+ this.step = 10;
10
+ }
11
+ load(data) {
12
+ if (data === undefined) {
13
+ return;
14
+ }
15
+ if (typeof data === "object") {
16
+ if (data.max !== undefined) {
17
+ this.max = data.max;
18
+ }
19
+ if (data.min !== undefined) {
20
+ this.min = data.min;
21
+ }
22
+ if (data.step !== undefined) {
23
+ this.step = data.step;
24
+ }
25
+ if (data.value !== undefined) {
26
+ this.value = data.value;
27
+ }
28
+ }
29
+ else {
30
+ this.value = data;
31
+ }
32
+ }
33
+ }
34
+ exports.SoundsVolume = SoundsVolume;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,359 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.SoundsInstance = void 0;
13
+ const engine_1 = require("@tsparticles/engine");
14
+ const engine_2 = require("@tsparticles/engine");
15
+ const utils_1 = require("./utils");
16
+ function setIconStyle(icon, top, left, display, zIndex, width, margin) {
17
+ icon.style.userSelect = "none";
18
+ icon.style.webkitUserSelect = "none";
19
+ icon.style.position = "absolute";
20
+ icon.style.top = `${top + margin}px`;
21
+ icon.style.left = `${left - margin - width}px`;
22
+ icon.style.display = display;
23
+ icon.style.zIndex = `${zIndex + 1}`;
24
+ }
25
+ class SoundsInstance {
26
+ constructor(container, engine) {
27
+ this._container = container;
28
+ this._engine = engine;
29
+ this._volume = 0;
30
+ this._audioSources = [];
31
+ this._audioMap = new Map();
32
+ }
33
+ init() {
34
+ return __awaiter(this, void 0, void 0, function* () {
35
+ const container = this._container, options = container.actualOptions, soundsOptions = options.sounds;
36
+ if (!(soundsOptions === null || soundsOptions === void 0 ? void 0 : soundsOptions.enable)) {
37
+ return;
38
+ }
39
+ this._volume = soundsOptions.volume.value;
40
+ const events = soundsOptions.events;
41
+ this._audioMap = new Map();
42
+ for (const event of events) {
43
+ if (!event.audio) {
44
+ continue;
45
+ }
46
+ (0, engine_2.executeOnSingleOrMultiple)(event.audio, (audio) => __awaiter(this, void 0, void 0, function* () {
47
+ const response = yield fetch(audio.source);
48
+ if (!response.ok) {
49
+ return;
50
+ }
51
+ const arrayBuffer = yield response.arrayBuffer();
52
+ container.audioContext = new AudioContext();
53
+ const audioBuffer = yield container.audioContext.decodeAudioData(arrayBuffer);
54
+ this._audioMap.set(audio.source, audioBuffer);
55
+ }));
56
+ }
57
+ });
58
+ }
59
+ start() {
60
+ var _a, _b, _c, _d;
61
+ return __awaiter(this, void 0, void 0, function* () {
62
+ const container = this._container, options = container.actualOptions, soundsOptions = options.sounds;
63
+ if (!(soundsOptions === null || soundsOptions === void 0 ? void 0 : soundsOptions.enable) || !container.canvas.element) {
64
+ return;
65
+ }
66
+ container.muted = true;
67
+ this._muteImg = document.createElement("img");
68
+ this._unmuteImg = document.createElement("img");
69
+ this._volumeDownImg = document.createElement("img");
70
+ this._volumeUpImg = document.createElement("img");
71
+ const muteImg = this._muteImg, unmuteImg = this._unmuteImg, volumeDownImg = this._volumeDownImg, volumeUpImg = this._volumeUpImg, containerTop = container.canvas.element.offsetTop, containerRight = container.canvas.element.offsetLeft + container.canvas.element.offsetWidth, iconsOptions = soundsOptions.icons, muteOptions = iconsOptions.mute, unmuteOptions = iconsOptions.unmute, volumeDownOptions = iconsOptions.volumeDown, volumeUpOptions = iconsOptions.volumeUp, margin = 10;
72
+ setIconStyle(muteImg, containerTop + margin, containerRight - margin * 3 - muteOptions.width - volumeDownOptions.width - volumeUpOptions.width, "block", options.fullScreen.zIndex + 1, muteOptions.width, margin);
73
+ setIconStyle(unmuteImg, containerTop + margin, containerRight - margin * 3 - unmuteOptions.width - volumeDownOptions.width - volumeUpOptions.width, "none", options.fullScreen.zIndex + 1, unmuteOptions.width, margin);
74
+ setIconStyle(volumeDownImg, containerTop + margin, containerRight - margin * 2 - volumeDownOptions.width - volumeUpOptions.width, "block", options.fullScreen.zIndex + 1, volumeDownOptions.width, margin);
75
+ setIconStyle(volumeUpImg, containerTop + margin, containerRight - margin - volumeUpOptions.width, "block", options.fullScreen.zIndex + 1, volumeUpOptions.width, margin);
76
+ muteImg.src = (_a = muteOptions.path) !== null && _a !== void 0 ? _a : (muteOptions.svg ? `data:image/svg+xml;base64,${btoa(muteOptions.svg)}` : "");
77
+ unmuteImg.src =
78
+ (_b = unmuteOptions.path) !== null && _b !== void 0 ? _b : (unmuteOptions.svg ? `data:image/svg+xml;base64,${btoa(unmuteOptions.svg)}` : "");
79
+ volumeDownImg.src =
80
+ (_c = volumeDownOptions.path) !== null && _c !== void 0 ? _c : (volumeDownOptions.svg ? `data:image/svg+xml;base64,${btoa(volumeDownOptions.svg)}` : "");
81
+ volumeUpImg.src =
82
+ (_d = volumeUpOptions.path) !== null && _d !== void 0 ? _d : (volumeUpOptions.svg ? `data:image/svg+xml;base64,${btoa(volumeUpOptions.svg)}` : "");
83
+ const parent = container.canvas.element.parentNode || document.body;
84
+ parent.append(muteImg);
85
+ parent.append(unmuteImg);
86
+ parent.append(volumeDownImg);
87
+ parent.append(volumeUpImg);
88
+ const toggleMute = () => {
89
+ container.muted = !container.muted;
90
+ this._updateMuteIcons();
91
+ this._updateMuteStatus();
92
+ };
93
+ const volumeDown = () => {
94
+ if (container.muted) {
95
+ this._volume = 0;
96
+ }
97
+ this._volume -= soundsOptions.volume.step;
98
+ this._updateVolume();
99
+ };
100
+ const volumeUp = () => {
101
+ if (container.muted) {
102
+ this._volume = 0;
103
+ }
104
+ this._volume += soundsOptions.volume.step;
105
+ this._updateVolume();
106
+ };
107
+ muteImg.addEventListener("click", toggleMute);
108
+ unmuteImg.addEventListener("click", toggleMute);
109
+ volumeDownImg.addEventListener("click", volumeDown);
110
+ volumeUpImg.addEventListener("click", volumeUp);
111
+ });
112
+ }
113
+ stop() {
114
+ this._container.muted = true;
115
+ this._mute();
116
+ if (this._muteImg) {
117
+ this._muteImg.remove();
118
+ }
119
+ if (this._unmuteImg) {
120
+ this._unmuteImg.remove();
121
+ }
122
+ if (this._volumeDownImg) {
123
+ this._volumeDownImg.remove();
124
+ }
125
+ if (this._volumeUpImg) {
126
+ this._volumeUpImg.remove();
127
+ }
128
+ }
129
+ _addBuffer(audioCtx) {
130
+ const buffer = audioCtx.createBufferSource();
131
+ this._audioSources.push(buffer);
132
+ return buffer;
133
+ }
134
+ _addOscillator(audioCtx) {
135
+ const oscillator = audioCtx.createOscillator();
136
+ this._audioSources.push(oscillator);
137
+ return oscillator;
138
+ }
139
+ _initEvents() {
140
+ const container = this._container, soundsOptions = container.actualOptions.sounds;
141
+ if (!(soundsOptions === null || soundsOptions === void 0 ? void 0 : soundsOptions.enable) || !container.canvas.element) {
142
+ return;
143
+ }
144
+ for (const event of soundsOptions.events) {
145
+ const cb = (args) => __awaiter(this, void 0, void 0, function* () {
146
+ if (this._container !== args.container) {
147
+ return;
148
+ }
149
+ if (!this._container || this._container.muted || this._container.destroyed) {
150
+ (0, engine_2.executeOnSingleOrMultiple)(event.event, (item) => {
151
+ this._engine.removeEventListener(item, cb);
152
+ });
153
+ return;
154
+ }
155
+ if (event.filter && !event.filter(args)) {
156
+ return;
157
+ }
158
+ if (event.audio) {
159
+ this._playBuffer((0, engine_1.itemFromSingleOrMultiple)(event.audio));
160
+ }
161
+ else if (event.melodies) {
162
+ const melody = (0, engine_1.itemFromArray)(event.melodies);
163
+ if (melody.melodies.length) {
164
+ yield Promise.allSettled(melody.melodies.map((m) => this._playNote(m.notes, 0, melody.loop)));
165
+ }
166
+ else {
167
+ yield this._playNote(melody.notes, 0, melody.loop);
168
+ }
169
+ }
170
+ else if (event.notes) {
171
+ const note = (0, engine_1.itemFromArray)(event.notes);
172
+ yield this._playNote([note], 0, false);
173
+ }
174
+ });
175
+ (0, engine_2.executeOnSingleOrMultiple)(event.event, (item) => {
176
+ this._engine.addEventListener(item, cb);
177
+ });
178
+ }
179
+ }
180
+ _mute() {
181
+ const container = this._container;
182
+ if (!container.audioContext) {
183
+ return;
184
+ }
185
+ for (const source of this._audioSources) {
186
+ this._removeAudioSource(source);
187
+ }
188
+ if (this._gain) {
189
+ this._gain.disconnect();
190
+ }
191
+ container.audioContext.close();
192
+ container.audioContext = undefined;
193
+ this._engine.dispatchEvent("soundsMuted", { container: this._container });
194
+ }
195
+ _playBuffer(audio) {
196
+ var _a;
197
+ const audioBuffer = this._audioMap.get(audio.source);
198
+ if (!audioBuffer) {
199
+ return;
200
+ }
201
+ const audioCtx = this._container.audioContext;
202
+ if (!audioCtx) {
203
+ return;
204
+ }
205
+ const source = this._addBuffer(audioCtx);
206
+ source.loop = audio.loop;
207
+ source.buffer = audioBuffer;
208
+ source.connect((_a = this._gain) !== null && _a !== void 0 ? _a : audioCtx.destination);
209
+ source.start();
210
+ }
211
+ _playFrequency(frequency, duration) {
212
+ return __awaiter(this, void 0, void 0, function* () {
213
+ if (!this._container.audioContext || !this._gain) {
214
+ return;
215
+ }
216
+ const oscillator = this._addOscillator(this._container.audioContext);
217
+ oscillator.connect(this._gain);
218
+ oscillator.type = "sine";
219
+ oscillator.frequency.value = frequency;
220
+ oscillator.start();
221
+ return new Promise((resolve) => {
222
+ setTimeout(() => {
223
+ this._removeAudioSource(oscillator);
224
+ resolve();
225
+ }, duration);
226
+ });
227
+ });
228
+ }
229
+ _playMuteSound() {
230
+ const container = this._container;
231
+ if (!container.audioContext) {
232
+ return;
233
+ }
234
+ const gain = container.audioContext.createGain();
235
+ gain.connect(container.audioContext.destination);
236
+ gain.gain.value = 0;
237
+ const oscillator = container.audioContext.createOscillator();
238
+ oscillator.connect(gain);
239
+ oscillator.type = "sine";
240
+ oscillator.frequency.value = 1;
241
+ oscillator.start();
242
+ setTimeout(() => {
243
+ oscillator.stop();
244
+ oscillator.disconnect();
245
+ gain.disconnect();
246
+ });
247
+ }
248
+ _playNote(notes, noteIdx, loop) {
249
+ return __awaiter(this, void 0, void 0, function* () {
250
+ if (this._container.muted) {
251
+ return;
252
+ }
253
+ const note = notes[noteIdx];
254
+ if (!note) {
255
+ return;
256
+ }
257
+ const value = note.value;
258
+ const promises = (0, engine_2.executeOnSingleOrMultiple)(value, (_, idx) => __awaiter(this, void 0, void 0, function* () {
259
+ return this._playNoteValue(notes, noteIdx, idx);
260
+ }));
261
+ yield (promises instanceof Array ? Promise.allSettled(promises) : promises);
262
+ let nextNoteIdx = noteIdx + 1;
263
+ if (loop && nextNoteIdx >= notes.length) {
264
+ nextNoteIdx = nextNoteIdx % notes.length;
265
+ }
266
+ if (this._container.muted) {
267
+ return;
268
+ }
269
+ yield this._playNote(notes, nextNoteIdx, loop);
270
+ });
271
+ }
272
+ _playNoteValue(notes, noteIdx, valueIdx) {
273
+ return __awaiter(this, void 0, void 0, function* () {
274
+ const note = notes[noteIdx];
275
+ if (!note) {
276
+ return;
277
+ }
278
+ const value = (0, engine_1.itemFromSingleOrMultiple)(note.value, valueIdx, true);
279
+ try {
280
+ const freq = (0, utils_1.getNoteFrequency)(value);
281
+ if (typeof freq !== "number") {
282
+ return;
283
+ }
284
+ yield this._playFrequency(freq, note.duration);
285
+ }
286
+ catch (e) {
287
+ console.error(e);
288
+ }
289
+ });
290
+ }
291
+ _removeAudioSource(source) {
292
+ source.stop();
293
+ source.disconnect();
294
+ this._audioSources.splice(this._audioSources.indexOf(source), 1);
295
+ }
296
+ _unmute() {
297
+ const container = this._container, options = container.actualOptions, soundsOptions = options.sounds;
298
+ if (!soundsOptions) {
299
+ return;
300
+ }
301
+ if (!container.audioContext) {
302
+ container.audioContext = new AudioContext();
303
+ }
304
+ if (!this._audioSources) {
305
+ this._audioSources = [];
306
+ }
307
+ const gain = container.audioContext.createGain();
308
+ gain.connect(container.audioContext.destination);
309
+ gain.gain.value = soundsOptions.volume.value / 100;
310
+ this._gain = gain;
311
+ this._initEvents();
312
+ this._engine.dispatchEvent("soundsUnmuted", { container: this._container });
313
+ }
314
+ _updateMuteIcons() {
315
+ const container = this._container, muteImg = this._muteImg, unmuteImg = this._unmuteImg;
316
+ if (muteImg) {
317
+ muteImg.style.display = container.muted ? "block" : "none";
318
+ }
319
+ if (unmuteImg) {
320
+ unmuteImg.style.display = container.muted ? "none" : "block";
321
+ }
322
+ }
323
+ _updateMuteStatus() {
324
+ const container = this._container;
325
+ if (container.muted) {
326
+ this._mute();
327
+ }
328
+ else {
329
+ this._unmute();
330
+ this._playMuteSound();
331
+ }
332
+ }
333
+ _updateVolume() {
334
+ var _a;
335
+ const container = this._container, soundsOptions = container.actualOptions.sounds;
336
+ if (!(soundsOptions === null || soundsOptions === void 0 ? void 0 : soundsOptions.enable)) {
337
+ return;
338
+ }
339
+ (0, engine_1.clamp)(this._volume, soundsOptions.volume.min, soundsOptions.volume.max);
340
+ let stateChanged = false;
341
+ if (this._volume <= 0 && !container.muted) {
342
+ this._volume = 0;
343
+ container.muted = true;
344
+ stateChanged = true;
345
+ }
346
+ else if (this._volume > 0 && container.muted) {
347
+ container.muted = false;
348
+ stateChanged = true;
349
+ }
350
+ if (stateChanged) {
351
+ this._updateMuteIcons();
352
+ this._updateMuteStatus();
353
+ }
354
+ if ((_a = this._gain) === null || _a === void 0 ? void 0 : _a.gain) {
355
+ this._gain.gain.value = this._volume / 100;
356
+ }
357
+ }
358
+ }
359
+ exports.SoundsInstance = SoundsInstance;
package/cjs/enums.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/cjs/index.js ADDED
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.loadSoundsPlugin = void 0;
13
+ const Sounds_1 = require("./Options/Classes/Sounds");
14
+ const SoundsInstance_1 = require("./SoundsInstance");
15
+ class SoundsPlugin {
16
+ constructor(engine) {
17
+ this.id = "sounds";
18
+ this._engine = engine;
19
+ }
20
+ getPlugin(container) {
21
+ return new SoundsInstance_1.SoundsInstance(container, this._engine);
22
+ }
23
+ loadOptions(options, source) {
24
+ if (!this.needsPlugin(source)) {
25
+ return;
26
+ }
27
+ let soundsOptions = options.sounds;
28
+ if ((soundsOptions === null || soundsOptions === void 0 ? void 0 : soundsOptions.load) === undefined) {
29
+ options.sounds = soundsOptions = new Sounds_1.Sounds();
30
+ }
31
+ soundsOptions.load(source === null || source === void 0 ? void 0 : source.sounds);
32
+ }
33
+ needsPlugin(options) {
34
+ var _a, _b;
35
+ return (_b = (_a = options === null || options === void 0 ? void 0 : options.sounds) === null || _a === void 0 ? void 0 : _a.enable) !== null && _b !== void 0 ? _b : false;
36
+ }
37
+ }
38
+ function loadSoundsPlugin(engine) {
39
+ return __awaiter(this, void 0, void 0, function* () {
40
+ const plugin = new SoundsPlugin(engine);
41
+ yield engine.addPlugin(plugin);
42
+ });
43
+ }
44
+ exports.loadSoundsPlugin = loadSoundsPlugin;
package/cjs/types.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/cjs/utils.js ADDED
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getNoteFrequency = void 0;
4
+ const notes = new Map();
5
+ notes.set("C", [16.35, 32.7, 65.41, 130.81, 261.63, 523.25, 1046.5, 2093.0, 4186.01]);
6
+ notes.set("Db", [17.32, 34.65, 69.3, 138.59, 277.18, 554.37, 1108.73, 2217.46, 4434.92]);
7
+ notes.set("D", [18.35, 36.71, 73.42, 146.83, 293.66, 587.33, 1174.66, 2349.32, 4698.63]);
8
+ notes.set("Eb", [19.45, 38.89, 77.78, 155.56, 311.13, 622.25, 1244.51, 2489.02, 4978.03]);
9
+ notes.set("E", [20.6, 41.2, 82.41, 164.81, 329.63, 659.25, 1318.51, 2637.02, 5274.04]);
10
+ notes.set("F", [21.83, 43.65, 87.31, 174.61, 349.23, 698.46, 1396.91, 2793.83, 5587.65]);
11
+ notes.set("Gb", [23.12, 46.25, 92.5, 185.0, 369.99, 739.99, 1479.98, 2959.96, 5919.91]);
12
+ notes.set("G", [24.5, 49.0, 98.0, 196.0, 392.0, 783.99, 1567.98, 3135.96, 6271.93]);
13
+ notes.set("Ab", [25.96, 51.91, 103.83, 207.65, 415.3, 830.61, 1661.22, 3322.44, 6644.88]);
14
+ notes.set("A", [27.5, 55.0, 110.0, 220.0, 440.0, 880.0, 1760.0, 3520.0, 7040.0]);
15
+ notes.set("Bb", [29.14, 58.27, 116.54, 233.08, 466.16, 932.33, 1864.66, 3729.31, 7458.62]);
16
+ notes.set("B", [30.87, 61.74, 123.47, 246.94, 493.88, 987.77, 1975.53, 3951.07, 7902.13]);
17
+ notes.set("pause", [0]);
18
+ function getNoteFrequency(note) {
19
+ const regex = /(([A-G]b?)(\d))|pause/i, result = regex.exec(note);
20
+ if (!result || !result.length) {
21
+ return;
22
+ }
23
+ console.log(result);
24
+ const noteKey = result[2] || result[0], noteItem = notes.get(noteKey);
25
+ if (!noteItem) {
26
+ return;
27
+ }
28
+ return noteItem[parseInt(result[3] || "0")];
29
+ }
30
+ exports.getNoteFrequency = getNoteFrequency;
@@ -0,0 +1,30 @@
1
+ import { SoundsEvent } from "./SoundsEvent";
2
+ import { SoundsIcons } from "./SoundsIcons";
3
+ import { SoundsVolume } from "./SoundsVolume";
4
+ export class Sounds {
5
+ constructor() {
6
+ this.enable = false;
7
+ this.events = [];
8
+ this.icons = new SoundsIcons();
9
+ this.volume = new SoundsVolume();
10
+ }
11
+ load(data) {
12
+ if (!data) {
13
+ return;
14
+ }
15
+ if (data.enable !== undefined) {
16
+ this.enable = data.enable;
17
+ }
18
+ if (data.events !== undefined) {
19
+ this.events = data.events.map((t) => {
20
+ const event = new SoundsEvent();
21
+ event.load(t);
22
+ return event;
23
+ });
24
+ }
25
+ this.icons.load(data.icons);
26
+ if (data.volume !== undefined) {
27
+ this.volume.load(data.volume);
28
+ }
29
+ }
30
+ }
@@ -0,0 +1,22 @@
1
+ export class SoundsAudio {
2
+ constructor() {
3
+ this.loop = false;
4
+ this.source = "";
5
+ }
6
+ load(data) {
7
+ if (data === undefined) {
8
+ return;
9
+ }
10
+ if (typeof data === "object") {
11
+ if (data.loop !== undefined) {
12
+ this.loop = data.loop;
13
+ }
14
+ if (data.source !== undefined) {
15
+ this.source = data.source;
16
+ }
17
+ }
18
+ else {
19
+ this.source = data;
20
+ }
21
+ }
22
+ }
@@ -0,0 +1,54 @@
1
+ import { SoundsAudio } from "./SoundsAudio";
2
+ import { SoundsMelody } from "./SoundsMelody";
3
+ import { SoundsNote } from "./SoundsNote";
4
+ export class SoundsEvent {
5
+ constructor() {
6
+ this.event = [];
7
+ this.notes = [];
8
+ }
9
+ load(data) {
10
+ if (!data) {
11
+ return;
12
+ }
13
+ if (data.event !== undefined) {
14
+ this.event = data.event;
15
+ }
16
+ if (data.audio !== undefined) {
17
+ if (data.audio instanceof Array) {
18
+ this.audio = data.audio.map((s) => {
19
+ const tmp = new SoundsAudio();
20
+ tmp.load(s);
21
+ return tmp;
22
+ });
23
+ }
24
+ else {
25
+ this.audio = new SoundsAudio();
26
+ this.audio.load(data.audio);
27
+ }
28
+ }
29
+ if (data.notes !== undefined) {
30
+ this.notes = data.notes.map((t) => {
31
+ const tmp = new SoundsNote();
32
+ tmp.load(t);
33
+ return tmp;
34
+ });
35
+ }
36
+ if (data.melodies !== undefined) {
37
+ this.melodies = data.melodies.map((t) => {
38
+ const tmp = new SoundsMelody();
39
+ tmp.load(t);
40
+ return tmp;
41
+ });
42
+ }
43
+ if (data.filter !== undefined) {
44
+ if (typeof data.filter === "string") {
45
+ if (typeof window[data.filter] === "function") {
46
+ this.filter = window[data.filter];
47
+ }
48
+ }
49
+ else {
50
+ this.filter = data.filter;
51
+ }
52
+ }
53
+ }
54
+ }
@@ -0,0 +1,23 @@
1
+ export class SoundsIcon {
2
+ constructor() {
3
+ this.width = 24;
4
+ this.height = 24;
5
+ }
6
+ load(data) {
7
+ if (!data) {
8
+ return;
9
+ }
10
+ if (data.path !== undefined) {
11
+ this.path = data.path;
12
+ }
13
+ if (data.svg !== undefined) {
14
+ this.svg = data.svg;
15
+ }
16
+ if (data.width !== undefined) {
17
+ this.width = data.width;
18
+ }
19
+ if (data.height !== undefined) {
20
+ this.height = data.height;
21
+ }
22
+ }
23
+ }