@tsparticles/plugin-sounds 3.0.0-alpha.0 → 3.0.0-beta.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 (37) hide show
  1. package/README.md +15 -11
  2. package/browser/Options/Classes/SoundsAudio.js +2 -1
  3. package/browser/Options/Classes/SoundsEvent.js +4 -3
  4. package/browser/Options/Classes/SoundsVolume.js +2 -1
  5. package/browser/SoundsInstance.js +300 -279
  6. package/browser/index.js +6 -8
  7. package/browser/utils.js +0 -1
  8. package/cjs/Options/Classes/SoundsAudio.js +2 -1
  9. package/cjs/Options/Classes/SoundsEvent.js +4 -3
  10. package/cjs/Options/Classes/SoundsVolume.js +2 -1
  11. package/cjs/SoundsInstance.js +283 -281
  12. package/cjs/index.js +6 -19
  13. package/cjs/utils.js +0 -1
  14. package/esm/Options/Classes/SoundsAudio.js +2 -1
  15. package/esm/Options/Classes/SoundsEvent.js +4 -3
  16. package/esm/Options/Classes/SoundsVolume.js +2 -1
  17. package/esm/SoundsInstance.js +300 -279
  18. package/esm/index.js +6 -8
  19. package/esm/utils.js +0 -1
  20. package/package.json +6 -5
  21. package/report.html +4 -4
  22. package/tsparticles.plugin.sounds.js +343 -305
  23. package/tsparticles.plugin.sounds.min.js +1 -1
  24. package/tsparticles.plugin.sounds.min.js.LICENSE.txt +1 -8
  25. package/types/Options/Classes/SoundsAudio.d.ts +1 -1
  26. package/types/Options/Classes/SoundsEvent.d.ts +1 -1
  27. package/types/Options/Classes/SoundsVolume.d.ts +1 -1
  28. package/types/SoundsInstance.d.ts +15 -15
  29. package/types/enums.d.ts +4 -0
  30. package/types/index.d.ts +1 -1
  31. package/types/types.d.ts +16 -0
  32. package/umd/Options/Classes/SoundsAudio.js +3 -2
  33. package/umd/Options/Classes/SoundsEvent.js +5 -4
  34. package/umd/Options/Classes/SoundsVolume.js +3 -2
  35. package/umd/SoundsInstance.js +301 -280
  36. package/umd/index.js +6 -8
  37. package/umd/utils.js +0 -1
@@ -4,15 +4,29 @@
4
4
  if (v !== undefined) module.exports = v;
5
5
  }
6
6
  else if (typeof define === "function" && define.amd) {
7
- define(["require", "exports", "@tsparticles/engine", "@tsparticles/engine", "./utils"], factory);
7
+ define(["require", "exports", "@tsparticles/engine", "./utils"], factory);
8
8
  }
9
9
  })(function (require, exports) {
10
10
  "use strict";
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.SoundsInstance = void 0;
13
13
  const engine_1 = require("@tsparticles/engine");
14
- const engine_2 = require("@tsparticles/engine");
15
14
  const utils_1 = require("./utils");
15
+ function initImage(data) {
16
+ const img = document.createElement("img"), { clickCb, container, display, iconOptions, margin, options, pos, rightOffsets } = data, { width, path, svg } = iconOptions;
17
+ setIconStyle(img, pos.top + margin, pos.right - (margin * (rightOffsets.length + 1) + width + rightOffsets.reduce((a, b) => a + b, 0)), display, options.fullScreen.zIndex + 1, width, margin);
18
+ img.src = path ?? (svg ? `data:image/svg+xml;base64,${btoa(svg)}` : "");
19
+ const parent = container.canvas.element?.parentNode || document.body;
20
+ parent.append(img);
21
+ img.addEventListener("click", clickCb);
22
+ return img;
23
+ }
24
+ function removeImage(image) {
25
+ if (!image) {
26
+ return;
27
+ }
28
+ image.remove();
29
+ }
16
30
  function setIconStyle(icon, top, left, display, zIndex, width, margin) {
17
31
  icon.style.userSelect = "none";
18
32
  icon.style.webkitUserSelect = "none";
@@ -24,6 +38,228 @@
24
38
  }
25
39
  class SoundsInstance {
26
40
  constructor(container, engine) {
41
+ this._addBuffer = (audioCtx) => {
42
+ const buffer = audioCtx.createBufferSource();
43
+ this._audioSources.push(buffer);
44
+ return buffer;
45
+ };
46
+ this._addOscillator = (audioCtx) => {
47
+ const oscillator = audioCtx.createOscillator();
48
+ this._audioSources.push(oscillator);
49
+ return oscillator;
50
+ };
51
+ this._initEvents = () => {
52
+ const container = this._container, soundsOptions = container.actualOptions.sounds;
53
+ if (!soundsOptions?.enable || !container.canvas.element) {
54
+ return;
55
+ }
56
+ for (const event of soundsOptions.events) {
57
+ const cb = async (args) => {
58
+ if (this._container !== args.container) {
59
+ return;
60
+ }
61
+ if (!this._container || this._container.muted || this._container.destroyed) {
62
+ (0, engine_1.executeOnSingleOrMultiple)(event.event, (item) => {
63
+ this._engine.removeEventListener(item, cb);
64
+ });
65
+ return;
66
+ }
67
+ if (event.filter && !event.filter(args)) {
68
+ return;
69
+ }
70
+ if (event.audio) {
71
+ this._playBuffer((0, engine_1.itemFromSingleOrMultiple)(event.audio));
72
+ }
73
+ else if (event.melodies) {
74
+ const melody = (0, engine_1.itemFromArray)(event.melodies);
75
+ if (melody.melodies.length) {
76
+ await Promise.allSettled(melody.melodies.map((m) => this._playNote(m.notes, 0, melody.loop)));
77
+ }
78
+ else {
79
+ await this._playNote(melody.notes, 0, melody.loop);
80
+ }
81
+ }
82
+ else if (event.notes) {
83
+ const note = (0, engine_1.itemFromArray)(event.notes);
84
+ await this._playNote([note], 0, false);
85
+ }
86
+ };
87
+ (0, engine_1.executeOnSingleOrMultiple)(event.event, (item) => {
88
+ this._engine.addEventListener(item, cb);
89
+ });
90
+ }
91
+ };
92
+ this._mute = () => {
93
+ const container = this._container;
94
+ if (!container.audioContext) {
95
+ return;
96
+ }
97
+ for (const source of this._audioSources) {
98
+ this._removeAudioSource(source);
99
+ }
100
+ if (this._gain) {
101
+ this._gain.disconnect();
102
+ }
103
+ container.audioContext.close();
104
+ container.audioContext = undefined;
105
+ this._engine.dispatchEvent("soundsMuted", { container: this._container });
106
+ };
107
+ this._playBuffer = (audio) => {
108
+ const audioBuffer = this._audioMap.get(audio.source);
109
+ if (!audioBuffer) {
110
+ return;
111
+ }
112
+ const audioCtx = this._container.audioContext;
113
+ if (!audioCtx) {
114
+ return;
115
+ }
116
+ const source = this._addBuffer(audioCtx);
117
+ source.loop = audio.loop;
118
+ source.buffer = audioBuffer;
119
+ source.connect(this._gain ?? audioCtx.destination);
120
+ source.start();
121
+ };
122
+ this._playFrequency = async (frequency, duration) => {
123
+ if (!this._container.audioContext || !this._gain) {
124
+ return;
125
+ }
126
+ const oscillator = this._addOscillator(this._container.audioContext);
127
+ oscillator.connect(this._gain);
128
+ oscillator.type = "sine";
129
+ oscillator.frequency.value = frequency;
130
+ oscillator.start();
131
+ return new Promise((resolve) => {
132
+ setTimeout(() => {
133
+ this._removeAudioSource(oscillator);
134
+ resolve();
135
+ }, duration);
136
+ });
137
+ };
138
+ this._playMuteSound = () => {
139
+ const container = this._container;
140
+ if (!container.audioContext) {
141
+ return;
142
+ }
143
+ const gain = container.audioContext.createGain();
144
+ gain.connect(container.audioContext.destination);
145
+ gain.gain.value = 0;
146
+ const oscillator = container.audioContext.createOscillator();
147
+ oscillator.connect(gain);
148
+ oscillator.type = "sine";
149
+ oscillator.frequency.value = 1;
150
+ oscillator.start();
151
+ setTimeout(() => {
152
+ oscillator.stop();
153
+ oscillator.disconnect();
154
+ gain.disconnect();
155
+ });
156
+ };
157
+ this._playNote = async (notes, noteIdx, loop) => {
158
+ if (this._container.muted) {
159
+ return;
160
+ }
161
+ const note = notes[noteIdx];
162
+ if (!note) {
163
+ return;
164
+ }
165
+ const value = note.value;
166
+ const promises = (0, engine_1.executeOnSingleOrMultiple)(value, async (_, idx) => {
167
+ return this._playNoteValue(notes, noteIdx, idx);
168
+ });
169
+ await ((0, engine_1.isArray)(promises) ? Promise.allSettled(promises) : promises);
170
+ let nextNoteIdx = noteIdx + 1;
171
+ if (loop && nextNoteIdx >= notes.length) {
172
+ nextNoteIdx = nextNoteIdx % notes.length;
173
+ }
174
+ if (this._container.muted) {
175
+ return;
176
+ }
177
+ await this._playNote(notes, nextNoteIdx, loop);
178
+ };
179
+ this._playNoteValue = async (notes, noteIdx, valueIdx) => {
180
+ const note = notes[noteIdx];
181
+ if (!note) {
182
+ return;
183
+ }
184
+ const value = (0, engine_1.itemFromSingleOrMultiple)(note.value, valueIdx, true);
185
+ try {
186
+ const freq = (0, utils_1.getNoteFrequency)(value);
187
+ if (!(0, engine_1.isNumber)(freq)) {
188
+ return;
189
+ }
190
+ await this._playFrequency(freq, note.duration);
191
+ }
192
+ catch (e) {
193
+ (0, engine_1.getLogger)().error(e);
194
+ }
195
+ };
196
+ this._removeAudioSource = (source) => {
197
+ source.stop();
198
+ source.disconnect();
199
+ this._audioSources.splice(this._audioSources.indexOf(source), 1);
200
+ };
201
+ this._unmute = () => {
202
+ const container = this._container, options = container.actualOptions, soundsOptions = options.sounds;
203
+ if (!soundsOptions) {
204
+ return;
205
+ }
206
+ if (!container.audioContext) {
207
+ container.audioContext = new AudioContext();
208
+ }
209
+ const { audioContext } = container;
210
+ if (!this._audioSources) {
211
+ this._audioSources = [];
212
+ }
213
+ const gain = audioContext.createGain();
214
+ gain.connect(audioContext.destination);
215
+ gain.gain.value = soundsOptions.volume.value / 100;
216
+ this._gain = gain;
217
+ this._initEvents();
218
+ this._engine.dispatchEvent("soundsUnmuted", { container: this._container });
219
+ };
220
+ this._updateMuteIcons = () => {
221
+ const container = this._container, muteImg = this._muteImg, unmuteImg = this._unmuteImg;
222
+ if (muteImg) {
223
+ muteImg.style.display = container.muted ? "block" : "none";
224
+ }
225
+ if (unmuteImg) {
226
+ unmuteImg.style.display = container.muted ? "none" : "block";
227
+ }
228
+ };
229
+ this._updateMuteStatus = () => {
230
+ const container = this._container;
231
+ if (container.muted) {
232
+ this._mute();
233
+ }
234
+ else {
235
+ this._unmute();
236
+ this._playMuteSound();
237
+ }
238
+ };
239
+ this._updateVolume = () => {
240
+ const container = this._container, soundsOptions = container.actualOptions.sounds;
241
+ if (!soundsOptions?.enable) {
242
+ return;
243
+ }
244
+ (0, engine_1.clamp)(this._volume, soundsOptions.volume.min, soundsOptions.volume.max);
245
+ let stateChanged = false;
246
+ if (this._volume <= 0 && !container.muted) {
247
+ this._volume = 0;
248
+ container.muted = true;
249
+ stateChanged = true;
250
+ }
251
+ else if (this._volume > 0 && container.muted) {
252
+ container.muted = false;
253
+ stateChanged = true;
254
+ }
255
+ if (stateChanged) {
256
+ this._updateMuteIcons();
257
+ this._updateMuteStatus();
258
+ }
259
+ if (this._gain?.gain) {
260
+ this._gain.gain.value = this._volume / 100;
261
+ }
262
+ };
27
263
  this._container = container;
28
264
  this._engine = engine;
29
265
  this._volume = 0;
@@ -32,7 +268,7 @@
32
268
  }
33
269
  async init() {
34
270
  const container = this._container, options = container.actualOptions, soundsOptions = options.sounds;
35
- if (!(soundsOptions === null || soundsOptions === void 0 ? void 0 : soundsOptions.enable)) {
271
+ if (!soundsOptions?.enable) {
36
272
  return;
37
273
  }
38
274
  this._volume = soundsOptions.volume.value;
@@ -42,7 +278,7 @@
42
278
  if (!event.audio) {
43
279
  continue;
44
280
  }
45
- (0, engine_2.executeOnSingleOrMultiple)(event.audio, async (audio) => {
281
+ (0, engine_1.executeOnSingleOrMultiple)(event.audio, async (audio) => {
46
282
  const response = await fetch(audio.source);
47
283
  if (!response.ok) {
48
284
  return;
@@ -55,295 +291,80 @@
55
291
  }
56
292
  }
57
293
  async start() {
58
- var _a, _b, _c, _d;
59
294
  const container = this._container, options = container.actualOptions, soundsOptions = options.sounds;
60
- if (!(soundsOptions === null || soundsOptions === void 0 ? void 0 : soundsOptions.enable) || !container.canvas.element) {
295
+ if (!soundsOptions?.enable || !container.canvas.element) {
61
296
  return;
62
297
  }
63
298
  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);
299
+ const canvas = container.canvas.element, pos = {
300
+ top: canvas.offsetTop,
301
+ right: canvas.offsetLeft + canvas.offsetWidth,
302
+ }, { mute, unmute, volumeDown, volumeUp } = soundsOptions.icons, margin = 10;
85
303
  const toggleMute = () => {
86
304
  container.muted = !container.muted;
87
305
  this._updateMuteIcons();
88
306
  this._updateMuteStatus();
89
307
  };
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);
308
+ this._muteImg = initImage({
309
+ container,
310
+ options,
311
+ pos,
312
+ display: "block",
313
+ iconOptions: mute,
314
+ margin,
315
+ rightOffsets: [volumeDown.width, volumeUp.width],
316
+ clickCb: toggleMute,
221
317
  });
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();
318
+ this._unmuteImg = initImage({
319
+ container,
320
+ options,
321
+ pos,
322
+ display: "none",
323
+ iconOptions: unmute,
324
+ margin,
325
+ rightOffsets: [volumeDown.width, volumeUp.width],
326
+ clickCb: toggleMute,
240
327
  });
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);
328
+ this._volumeDownImg = initImage({
329
+ container,
330
+ options,
331
+ pos,
332
+ display: "block",
333
+ iconOptions: volumeDown,
334
+ margin,
335
+ rightOffsets: [volumeUp.width],
336
+ clickCb: () => {
337
+ if (container.muted) {
338
+ this._volume = 0;
339
+ }
340
+ this._volume -= soundsOptions.volume.step;
341
+ this._updateVolume();
342
+ },
343
+ });
344
+ this._volumeUpImg = initImage({
345
+ container,
346
+ options,
347
+ pos,
348
+ display: "block",
349
+ iconOptions: volumeUp,
350
+ margin,
351
+ rightOffsets: [],
352
+ clickCb: () => {
353
+ if (container.muted) {
354
+ this._volume = 0;
355
+ }
356
+ this._volume += soundsOptions.volume.step;
357
+ this._updateVolume();
358
+ },
253
359
  });
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
360
  }
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
- }
361
+ stop() {
362
+ this._container.muted = true;
363
+ this._mute();
364
+ removeImage(this._muteImg);
365
+ removeImage(this._unmuteImg);
366
+ removeImage(this._volumeDownImg);
367
+ removeImage(this._volumeUpImg);
347
368
  }
348
369
  }
349
370
  exports.SoundsInstance = SoundsInstance;
package/umd/index.js CHANGED
@@ -21,23 +21,21 @@
21
21
  return new SoundsInstance_1.SoundsInstance(container, this._engine);
22
22
  }
23
23
  loadOptions(options, source) {
24
- if (!this.needsPlugin(source)) {
24
+ if (!this.needsPlugin(options) && !this.needsPlugin(source)) {
25
25
  return;
26
26
  }
27
27
  let soundsOptions = options.sounds;
28
- if ((soundsOptions === null || soundsOptions === void 0 ? void 0 : soundsOptions.load) === undefined) {
28
+ if (soundsOptions?.load === undefined) {
29
29
  options.sounds = soundsOptions = new Sounds_1.Sounds();
30
30
  }
31
- soundsOptions.load(source === null || source === void 0 ? void 0 : source.sounds);
31
+ soundsOptions.load(source?.sounds);
32
32
  }
33
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;
34
+ return options?.sounds?.enable ?? false;
36
35
  }
37
36
  }
38
- async function loadSoundsPlugin(engine) {
39
- const plugin = new SoundsPlugin(engine);
40
- await engine.addPlugin(plugin);
37
+ async function loadSoundsPlugin(engine, refresh = true) {
38
+ await engine.addPlugin(new SoundsPlugin(engine), refresh);
41
39
  }
42
40
  exports.loadSoundsPlugin = loadSoundsPlugin;
43
41
  });
package/umd/utils.js CHANGED
@@ -29,7 +29,6 @@
29
29
  if (!result || !result.length) {
30
30
  return;
31
31
  }
32
- console.log(result);
33
32
  const noteKey = result[2] || result[0], noteItem = notes.get(noteKey);
34
33
  if (!noteItem) {
35
34
  return;