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