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