@tsparticles/plugin-sounds 3.0.3 → 3.1.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.
- package/browser/Options/Classes/Sounds.js +4 -0
- package/browser/Options/Classes/SoundsEvent.js +4 -3
- package/browser/Options/Classes/SoundsIcon.js +1 -0
- package/browser/Options/Classes/SoundsIcons.js +4 -0
- package/browser/SoundsInstance.js +130 -69
- package/browser/index.js +13 -0
- package/browser/utils.js +11 -4
- package/cjs/Options/Classes/Sounds.js +4 -0
- package/cjs/Options/Classes/SoundsEvent.js +4 -3
- package/cjs/Options/Classes/SoundsIcon.js +1 -0
- package/cjs/Options/Classes/SoundsIcons.js +4 -0
- package/cjs/SoundsInstance.js +128 -67
- package/cjs/index.js +13 -0
- package/cjs/utils.js +14 -5
- package/esm/Options/Classes/Sounds.js +4 -0
- package/esm/Options/Classes/SoundsEvent.js +4 -3
- package/esm/Options/Classes/SoundsIcon.js +1 -0
- package/esm/Options/Classes/SoundsIcons.js +4 -0
- package/esm/SoundsInstance.js +130 -69
- package/esm/index.js +13 -0
- package/esm/utils.js +11 -4
- package/package.json +2 -2
- package/report.html +2 -2
- package/tsparticles.plugin.sounds.js +180 -77
- package/tsparticles.plugin.sounds.min.js +1 -1
- package/tsparticles.plugin.sounds.min.js.LICENSE.txt +1 -1
- package/types/Options/Classes/Sounds.d.ts +1 -0
- package/types/Options/Classes/SoundsEvent.d.ts +0 -5
- package/types/Options/Classes/SoundsIcon.d.ts +1 -0
- package/types/Options/Classes/SoundsIcons.d.ts +1 -0
- package/types/Options/Interfaces/ISounds.d.ts +1 -0
- package/types/Options/Interfaces/ISoundsIcon.d.ts +1 -0
- package/types/Options/Interfaces/ISoundsIcons.d.ts +1 -0
- package/types/SoundsInstance.d.ts +6 -0
- package/types/index.d.ts +1 -1
- package/types/types.d.ts +5 -5
- package/types/utils.d.ts +2 -0
- package/umd/Options/Classes/Sounds.js +4 -0
- package/umd/Options/Classes/SoundsEvent.js +4 -3
- package/umd/Options/Classes/SoundsIcon.js +1 -0
- package/umd/Options/Classes/SoundsIcons.js +4 -0
- package/umd/SoundsInstance.js +128 -67
- package/umd/index.js +14 -1
- package/umd/utils.js +14 -5
package/cjs/SoundsInstance.js
CHANGED
|
@@ -3,13 +3,19 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.SoundsInstance = void 0;
|
|
4
4
|
const engine_1 = require("@tsparticles/engine");
|
|
5
5
|
const utils_js_1 = require("./utils.js");
|
|
6
|
+
const zIndexOffset = 1, rightOffset = 1, minVolume = 0;
|
|
6
7
|
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 -
|
|
8
|
+
const img = document.createElement("img"), { clickCb, container, display, iconOptions, margin, options, pos, rightOffsets } = data, { width, path, style, svg } = iconOptions, defaultAccumulator = 0;
|
|
9
|
+
setIconStyle(img, pos.top + margin, pos.right -
|
|
10
|
+
(margin * (rightOffsets.length + rightOffset) +
|
|
11
|
+
width +
|
|
12
|
+
rightOffsets.reduce((a, b) => a + b, defaultAccumulator)), display, options.fullScreen.zIndex + zIndexOffset, width, margin, style);
|
|
9
13
|
img.src = path ?? (svg ? `data:image/svg+xml;base64,${btoa(svg)}` : "");
|
|
10
|
-
const parent = container.canvas.element?.parentNode
|
|
14
|
+
const parent = container.canvas.element?.parentNode ?? document.body;
|
|
11
15
|
parent.append(img);
|
|
12
|
-
img.addEventListener("click",
|
|
16
|
+
img.addEventListener("click", () => {
|
|
17
|
+
void clickCb();
|
|
18
|
+
});
|
|
13
19
|
return img;
|
|
14
20
|
}
|
|
15
21
|
function removeImage(image) {
|
|
@@ -18,14 +24,15 @@ function removeImage(image) {
|
|
|
18
24
|
}
|
|
19
25
|
image.remove();
|
|
20
26
|
}
|
|
21
|
-
function setIconStyle(icon, top, left, display, zIndex, width, margin) {
|
|
27
|
+
function setIconStyle(icon, top, left, display, zIndex, width, margin, style) {
|
|
22
28
|
icon.style.userSelect = "none";
|
|
23
29
|
icon.style.webkitUserSelect = "none";
|
|
24
30
|
icon.style.position = "absolute";
|
|
25
31
|
icon.style.top = `${top + margin}px`;
|
|
26
32
|
icon.style.left = `${left - margin - width}px`;
|
|
27
33
|
icon.style.display = display;
|
|
28
|
-
icon.style.zIndex = `${zIndex +
|
|
34
|
+
icon.style.zIndex = `${zIndex + zIndexOffset}`;
|
|
35
|
+
icon.style.cssText += style;
|
|
29
36
|
}
|
|
30
37
|
class SoundsInstance {
|
|
31
38
|
constructor(container, engine) {
|
|
@@ -46,12 +53,12 @@ class SoundsInstance {
|
|
|
46
53
|
}
|
|
47
54
|
for (const event of soundsOptions.events) {
|
|
48
55
|
const cb = (args) => {
|
|
49
|
-
(async () => {
|
|
56
|
+
void (async () => {
|
|
50
57
|
const filterNotValid = event.filter && !event.filter(args);
|
|
51
58
|
if (this._container !== args.container) {
|
|
52
59
|
return;
|
|
53
60
|
}
|
|
54
|
-
if (!this._container || this._container.muted || this._container.destroyed) {
|
|
61
|
+
if (!this._container || !!this._container.muted || this._container.destroyed) {
|
|
55
62
|
(0, engine_1.executeOnSingleOrMultiple)(event.event, (item) => {
|
|
56
63
|
this._engine.removeEventListener(item, cb);
|
|
57
64
|
});
|
|
@@ -60,21 +67,22 @@ class SoundsInstance {
|
|
|
60
67
|
if (filterNotValid) {
|
|
61
68
|
return;
|
|
62
69
|
}
|
|
70
|
+
const defaultNoteIndex = 0;
|
|
63
71
|
if (event.audio) {
|
|
64
72
|
this._playBuffer((0, engine_1.itemFromSingleOrMultiple)(event.audio));
|
|
65
73
|
}
|
|
66
74
|
else if (event.melodies) {
|
|
67
75
|
const melody = (0, engine_1.itemFromArray)(event.melodies);
|
|
68
76
|
if (melody.melodies.length) {
|
|
69
|
-
await Promise.allSettled(melody.melodies.map((m) => this._playNote(m.notes,
|
|
77
|
+
await Promise.allSettled(melody.melodies.map((m) => this._playNote(m.notes, defaultNoteIndex, melody.loop)));
|
|
70
78
|
}
|
|
71
79
|
else {
|
|
72
|
-
await this._playNote(melody.notes,
|
|
80
|
+
await this._playNote(melody.notes, defaultNoteIndex, melody.loop);
|
|
73
81
|
}
|
|
74
82
|
}
|
|
75
83
|
else if (event.notes) {
|
|
76
84
|
const note = (0, engine_1.itemFromArray)(event.notes);
|
|
77
|
-
await this._playNote([note],
|
|
85
|
+
await this._playNote([note], defaultNoteIndex, false);
|
|
78
86
|
}
|
|
79
87
|
})();
|
|
80
88
|
};
|
|
@@ -83,18 +91,15 @@ class SoundsInstance {
|
|
|
83
91
|
});
|
|
84
92
|
}
|
|
85
93
|
};
|
|
86
|
-
this._mute = () => {
|
|
87
|
-
const container = this._container;
|
|
88
|
-
if (!container.audioContext) {
|
|
89
|
-
return;
|
|
90
|
-
}
|
|
94
|
+
this._mute = async () => {
|
|
95
|
+
const container = this._container, audioContext = this._getAudioContext();
|
|
91
96
|
for (const source of this._audioSources) {
|
|
92
97
|
this._removeAudioSource(source);
|
|
93
98
|
}
|
|
94
99
|
if (this._gain) {
|
|
95
100
|
this._gain.disconnect();
|
|
96
101
|
}
|
|
97
|
-
|
|
102
|
+
await audioContext.close();
|
|
98
103
|
container.audioContext = undefined;
|
|
99
104
|
this._engine.dispatchEvent("soundsMuted", { container: this._container });
|
|
100
105
|
};
|
|
@@ -114,10 +119,10 @@ class SoundsInstance {
|
|
|
114
119
|
source.start();
|
|
115
120
|
};
|
|
116
121
|
this._playFrequency = async (frequency, duration) => {
|
|
117
|
-
if (!this.
|
|
122
|
+
if (!this._gain || this._container.muted) {
|
|
118
123
|
return;
|
|
119
124
|
}
|
|
120
|
-
const oscillator = this._addOscillator(
|
|
125
|
+
const audioContext = this._getAudioContext(), oscillator = this._addOscillator(audioContext);
|
|
121
126
|
oscillator.connect(this._gain);
|
|
122
127
|
oscillator.type = "sine";
|
|
123
128
|
oscillator.frequency.value = frequency;
|
|
@@ -130,14 +135,13 @@ class SoundsInstance {
|
|
|
130
135
|
});
|
|
131
136
|
};
|
|
132
137
|
this._playMuteSound = () => {
|
|
133
|
-
|
|
134
|
-
if (!container.audioContext) {
|
|
138
|
+
if (this._container.muted) {
|
|
135
139
|
return;
|
|
136
140
|
}
|
|
137
|
-
const gain =
|
|
138
|
-
gain.connect(
|
|
141
|
+
const audioContext = this._getAudioContext(), gain = audioContext.createGain();
|
|
142
|
+
gain.connect(audioContext.destination);
|
|
139
143
|
gain.gain.value = 0;
|
|
140
|
-
const oscillator =
|
|
144
|
+
const oscillator = audioContext.createOscillator();
|
|
141
145
|
oscillator.connect(gain);
|
|
142
146
|
oscillator.type = "sine";
|
|
143
147
|
oscillator.frequency.value = 1;
|
|
@@ -161,7 +165,8 @@ class SoundsInstance {
|
|
|
161
165
|
return this._playNoteValue(notes, noteIdx, idx);
|
|
162
166
|
});
|
|
163
167
|
await ((0, engine_1.isArray)(promises) ? Promise.allSettled(promises) : promises);
|
|
164
|
-
|
|
168
|
+
const indexOffset = 1;
|
|
169
|
+
let nextNoteIdx = noteIdx + indexOffset;
|
|
165
170
|
if (loop && nextNoteIdx >= notes.length) {
|
|
166
171
|
nextNoteIdx = nextNoteIdx % notes.length;
|
|
167
172
|
}
|
|
@@ -190,29 +195,31 @@ class SoundsInstance {
|
|
|
190
195
|
this._removeAudioSource = (source) => {
|
|
191
196
|
source.stop();
|
|
192
197
|
source.disconnect();
|
|
193
|
-
|
|
198
|
+
const deleteCount = 1;
|
|
199
|
+
this._audioSources.splice(this._audioSources.indexOf(source), deleteCount);
|
|
194
200
|
};
|
|
195
201
|
this._unmute = () => {
|
|
196
202
|
const container = this._container, options = container.actualOptions, soundsOptions = options.sounds;
|
|
197
203
|
if (!soundsOptions) {
|
|
198
204
|
return;
|
|
199
205
|
}
|
|
200
|
-
|
|
201
|
-
container.audioContext = new AudioContext();
|
|
202
|
-
}
|
|
203
|
-
const { audioContext } = container;
|
|
206
|
+
const audioContext = this._getAudioContext();
|
|
204
207
|
if (!this._audioSources) {
|
|
205
208
|
this._audioSources = [];
|
|
206
209
|
}
|
|
207
210
|
const gain = audioContext.createGain();
|
|
208
211
|
gain.connect(audioContext.destination);
|
|
209
|
-
gain.gain.value = soundsOptions.volume.value /
|
|
212
|
+
gain.gain.value = soundsOptions.volume.value / engine_1.percentDenominator;
|
|
210
213
|
this._gain = gain;
|
|
211
214
|
this._initEvents();
|
|
212
215
|
this._engine.dispatchEvent("soundsUnmuted", { container: this._container });
|
|
213
216
|
};
|
|
214
217
|
this._updateMuteIcons = () => {
|
|
215
|
-
const container = this._container,
|
|
218
|
+
const container = this._container, soundsOptions = container.actualOptions.sounds;
|
|
219
|
+
if (!soundsOptions?.enable || !soundsOptions.icons.enable) {
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
const muteImg = this._muteImg, unmuteImg = this._unmuteImg;
|
|
216
223
|
if (muteImg) {
|
|
217
224
|
muteImg.style.display = container.muted ? "block" : "none";
|
|
218
225
|
}
|
|
@@ -221,13 +228,13 @@ class SoundsInstance {
|
|
|
221
228
|
}
|
|
222
229
|
};
|
|
223
230
|
this._updateMuteStatus = async () => {
|
|
224
|
-
const container = this._container;
|
|
231
|
+
const container = this._container, audioContext = this._getAudioContext();
|
|
225
232
|
if (container.muted) {
|
|
226
|
-
await
|
|
227
|
-
this._mute();
|
|
233
|
+
await audioContext?.suspend();
|
|
234
|
+
await this._mute();
|
|
228
235
|
}
|
|
229
236
|
else {
|
|
230
|
-
await
|
|
237
|
+
await audioContext?.resume();
|
|
231
238
|
this._unmute();
|
|
232
239
|
this._playMuteSound();
|
|
233
240
|
}
|
|
@@ -239,12 +246,12 @@ class SoundsInstance {
|
|
|
239
246
|
}
|
|
240
247
|
(0, engine_1.clamp)(this._volume, soundsOptions.volume.min, soundsOptions.volume.max);
|
|
241
248
|
let stateChanged = false;
|
|
242
|
-
if (this._volume <=
|
|
249
|
+
if (this._volume <= minVolume && !container.muted) {
|
|
243
250
|
this._volume = 0;
|
|
244
251
|
container.muted = true;
|
|
245
252
|
stateChanged = true;
|
|
246
253
|
}
|
|
247
|
-
else if (this._volume >
|
|
254
|
+
else if (this._volume > minVolume && container.muted) {
|
|
248
255
|
container.muted = false;
|
|
249
256
|
stateChanged = true;
|
|
250
257
|
}
|
|
@@ -253,7 +260,7 @@ class SoundsInstance {
|
|
|
253
260
|
await this._updateMuteStatus();
|
|
254
261
|
}
|
|
255
262
|
if (this._gain?.gain) {
|
|
256
|
-
this._gain.gain.value = this._volume /
|
|
263
|
+
this._gain.gain.value = this._volume / engine_1.percentDenominator;
|
|
257
264
|
}
|
|
258
265
|
};
|
|
259
266
|
this._container = container;
|
|
@@ -267,6 +274,20 @@ class SoundsInstance {
|
|
|
267
274
|
if (!soundsOptions?.enable) {
|
|
268
275
|
return;
|
|
269
276
|
}
|
|
277
|
+
if (soundsOptions.autoPlay && (0, utils_js_1.isWindowMuted)()) {
|
|
278
|
+
const firstClickHandler = () => {
|
|
279
|
+
removeEventListener(engine_1.mouseDownEvent, firstClickHandler);
|
|
280
|
+
removeEventListener(engine_1.touchStartEvent, firstClickHandler);
|
|
281
|
+
(0, utils_js_1.unmuteWindow)();
|
|
282
|
+
void this.unmute();
|
|
283
|
+
};
|
|
284
|
+
const listenerOptions = {
|
|
285
|
+
capture: true,
|
|
286
|
+
once: true,
|
|
287
|
+
};
|
|
288
|
+
addEventListener(engine_1.mouseDownEvent, firstClickHandler, listenerOptions);
|
|
289
|
+
addEventListener(engine_1.touchStartEvent, firstClickHandler, listenerOptions);
|
|
290
|
+
}
|
|
270
291
|
this._volume = soundsOptions.volume.value;
|
|
271
292
|
const events = soundsOptions.events;
|
|
272
293
|
this._audioMap = new Map();
|
|
@@ -274,16 +295,25 @@ class SoundsInstance {
|
|
|
274
295
|
if (!event.audio) {
|
|
275
296
|
continue;
|
|
276
297
|
}
|
|
277
|
-
(0, engine_1.executeOnSingleOrMultiple)(event.audio, async (audio) => {
|
|
298
|
+
const promises = (0, engine_1.executeOnSingleOrMultiple)(event.audio, async (audio) => {
|
|
278
299
|
const response = await fetch(audio.source);
|
|
279
300
|
if (!response.ok) {
|
|
280
301
|
return;
|
|
281
302
|
}
|
|
282
|
-
const arrayBuffer = await response.arrayBuffer();
|
|
283
|
-
container.audioContext = new AudioContext();
|
|
284
|
-
const audioBuffer = await container.audioContext.decodeAudioData(arrayBuffer);
|
|
303
|
+
const arrayBuffer = await response.arrayBuffer(), audioContext = this._getAudioContext(), audioBuffer = await audioContext.decodeAudioData(arrayBuffer);
|
|
285
304
|
this._audioMap.set(audio.source, audioBuffer);
|
|
286
305
|
});
|
|
306
|
+
if (promises instanceof Promise) {
|
|
307
|
+
await promises;
|
|
308
|
+
}
|
|
309
|
+
else {
|
|
310
|
+
await Promise.allSettled(promises);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
async mute() {
|
|
315
|
+
if (!this._container.muted) {
|
|
316
|
+
await this.toggleMute();
|
|
287
317
|
}
|
|
288
318
|
}
|
|
289
319
|
async start() {
|
|
@@ -295,17 +325,14 @@ class SoundsInstance {
|
|
|
295
325
|
const canvas = container.canvas.element, pos = {
|
|
296
326
|
top: canvas.offsetTop,
|
|
297
327
|
right: canvas.offsetLeft + canvas.offsetWidth,
|
|
298
|
-
}, { mute, unmute, volumeDown, volumeUp } = soundsOptions.icons, margin = 10
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
this._updateMuteIcons();
|
|
302
|
-
await this._updateMuteStatus();
|
|
303
|
-
};
|
|
328
|
+
}, { mute, unmute, volumeDown, volumeUp } = soundsOptions.icons, margin = 10, toggleMute = async () => {
|
|
329
|
+
await this.toggleMute();
|
|
330
|
+
}, enableIcons = soundsOptions.icons.enable, display = enableIcons ? "block" : "none";
|
|
304
331
|
this._muteImg = initImage({
|
|
305
332
|
container,
|
|
306
333
|
options,
|
|
307
334
|
pos,
|
|
308
|
-
display
|
|
335
|
+
display,
|
|
309
336
|
iconOptions: mute,
|
|
310
337
|
margin,
|
|
311
338
|
rightOffsets: [volumeDown.width, volumeUp.width],
|
|
@@ -325,42 +352,76 @@ class SoundsInstance {
|
|
|
325
352
|
container,
|
|
326
353
|
options,
|
|
327
354
|
pos,
|
|
328
|
-
display
|
|
355
|
+
display,
|
|
329
356
|
iconOptions: volumeDown,
|
|
330
357
|
margin,
|
|
331
358
|
rightOffsets: [volumeUp.width],
|
|
332
359
|
clickCb: async () => {
|
|
333
|
-
|
|
334
|
-
this._volume = 0;
|
|
335
|
-
}
|
|
336
|
-
this._volume -= soundsOptions.volume.step;
|
|
337
|
-
await this._updateVolume();
|
|
360
|
+
await this.volumeDown();
|
|
338
361
|
},
|
|
339
362
|
});
|
|
340
363
|
this._volumeUpImg = initImage({
|
|
341
364
|
container,
|
|
342
365
|
options,
|
|
343
366
|
pos,
|
|
344
|
-
display
|
|
367
|
+
display,
|
|
345
368
|
iconOptions: volumeUp,
|
|
346
369
|
margin,
|
|
347
370
|
rightOffsets: [],
|
|
348
371
|
clickCb: async () => {
|
|
349
|
-
|
|
350
|
-
this._volume = 0;
|
|
351
|
-
}
|
|
352
|
-
this._volume += soundsOptions.volume.step;
|
|
353
|
-
await this._updateVolume();
|
|
372
|
+
await this.volumeUp();
|
|
354
373
|
},
|
|
355
374
|
});
|
|
375
|
+
if (!(0, utils_js_1.isWindowMuted)() && soundsOptions.autoPlay) {
|
|
376
|
+
await this.unmute();
|
|
377
|
+
}
|
|
356
378
|
}
|
|
357
379
|
stop() {
|
|
358
380
|
this._container.muted = true;
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
381
|
+
void (async () => {
|
|
382
|
+
await this._mute();
|
|
383
|
+
removeImage(this._muteImg);
|
|
384
|
+
removeImage(this._unmuteImg);
|
|
385
|
+
removeImage(this._volumeDownImg);
|
|
386
|
+
removeImage(this._volumeUpImg);
|
|
387
|
+
})();
|
|
388
|
+
}
|
|
389
|
+
async toggleMute() {
|
|
390
|
+
const container = this._container;
|
|
391
|
+
container.muted = !container.muted;
|
|
392
|
+
this._updateMuteIcons();
|
|
393
|
+
await this._updateMuteStatus();
|
|
394
|
+
}
|
|
395
|
+
async unmute() {
|
|
396
|
+
if (this._container.muted) {
|
|
397
|
+
await this.toggleMute();
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
async volumeDown() {
|
|
401
|
+
const container = this._container, soundsOptions = container.actualOptions.sounds;
|
|
402
|
+
if (!soundsOptions?.enable) {
|
|
403
|
+
return;
|
|
404
|
+
}
|
|
405
|
+
if (container.muted) {
|
|
406
|
+
this._volume = 0;
|
|
407
|
+
}
|
|
408
|
+
this._volume -= soundsOptions.volume.step;
|
|
409
|
+
await this._updateVolume();
|
|
410
|
+
}
|
|
411
|
+
async volumeUp() {
|
|
412
|
+
const container = this._container, soundsOptions = container.actualOptions.sounds;
|
|
413
|
+
if (!soundsOptions?.enable) {
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
this._volume += soundsOptions.volume.step;
|
|
417
|
+
await this._updateVolume();
|
|
418
|
+
}
|
|
419
|
+
_getAudioContext() {
|
|
420
|
+
const container = this._container;
|
|
421
|
+
if (!container.audioContext) {
|
|
422
|
+
container.audioContext = new AudioContext();
|
|
423
|
+
}
|
|
424
|
+
return container.audioContext;
|
|
364
425
|
}
|
|
365
426
|
}
|
|
366
427
|
exports.SoundsInstance = SoundsInstance;
|
package/cjs/index.js
CHANGED
|
@@ -1,12 +1,25 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.loadSoundsPlugin = void 0;
|
|
4
|
+
const engine_1 = require("@tsparticles/engine");
|
|
4
5
|
const Sounds_js_1 = require("./Options/Classes/Sounds.js");
|
|
5
6
|
const SoundsInstance_js_1 = require("./SoundsInstance.js");
|
|
7
|
+
const utils_js_1 = require("./utils.js");
|
|
8
|
+
const generalFirstClickHandler = () => {
|
|
9
|
+
removeEventListener(engine_1.mouseDownEvent, generalFirstClickHandler);
|
|
10
|
+
removeEventListener(engine_1.touchStartEvent, generalFirstClickHandler);
|
|
11
|
+
(0, utils_js_1.unmuteWindow)();
|
|
12
|
+
};
|
|
6
13
|
class SoundsPlugin {
|
|
7
14
|
constructor(engine) {
|
|
8
15
|
this.id = "sounds";
|
|
9
16
|
this._engine = engine;
|
|
17
|
+
const listenerOptions = {
|
|
18
|
+
capture: true,
|
|
19
|
+
once: true,
|
|
20
|
+
};
|
|
21
|
+
addEventListener(engine_1.mouseDownEvent, generalFirstClickHandler, listenerOptions);
|
|
22
|
+
addEventListener(engine_1.touchStartEvent, generalFirstClickHandler, listenerOptions);
|
|
10
23
|
}
|
|
11
24
|
getPlugin(container) {
|
|
12
25
|
return new SoundsInstance_js_1.SoundsInstance(container, this._engine);
|
package/cjs/utils.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getNoteFrequency = void 0;
|
|
3
|
+
exports.unmuteWindow = exports.isWindowMuted = exports.getNoteFrequency = void 0;
|
|
4
4
|
const notes = new Map();
|
|
5
5
|
notes.set("C", [16.35, 32.7, 65.41, 130.81, 261.63, 523.25, 1046.5, 2093.0, 4186.01]);
|
|
6
6
|
notes.set("Db", [17.32, 34.65, 69.3, 138.59, 277.18, 554.37, 1108.73, 2217.46, 4434.92]);
|
|
@@ -16,14 +16,23 @@ notes.set("Bb", [29.14, 58.27, 116.54, 233.08, 466.16, 932.33, 1864.66, 3729.31,
|
|
|
16
16
|
notes.set("B", [30.87, 61.74, 123.47, 246.94, 493.88, 987.77, 1975.53, 3951.07, 7902.13]);
|
|
17
17
|
notes.set("pause", [0]);
|
|
18
18
|
function getNoteFrequency(note) {
|
|
19
|
-
const regex = /(([A-G]b?)(\d))|pause/i, result = regex.exec(note);
|
|
20
|
-
if (!result
|
|
19
|
+
const regex = /(([A-G]b?)(\d))|pause/i, result = regex.exec(note), groupKey = 2, defaultMatchKey = 0, innerGroupKey = 3;
|
|
20
|
+
if (!result?.length) {
|
|
21
21
|
return;
|
|
22
22
|
}
|
|
23
|
-
const noteKey = result[
|
|
23
|
+
const noteKey = result[groupKey] || result[defaultMatchKey], noteItem = notes.get(noteKey);
|
|
24
24
|
if (!noteItem) {
|
|
25
25
|
return;
|
|
26
26
|
}
|
|
27
|
-
return noteItem[parseInt(result[
|
|
27
|
+
return noteItem[parseInt(result[innerGroupKey] || "0")];
|
|
28
28
|
}
|
|
29
29
|
exports.getNoteFrequency = getNoteFrequency;
|
|
30
|
+
let muted = true;
|
|
31
|
+
const isWindowMuted = () => {
|
|
32
|
+
return muted;
|
|
33
|
+
};
|
|
34
|
+
exports.isWindowMuted = isWindowMuted;
|
|
35
|
+
const unmuteWindow = () => {
|
|
36
|
+
muted = false;
|
|
37
|
+
};
|
|
38
|
+
exports.unmuteWindow = unmuteWindow;
|
|
@@ -3,6 +3,7 @@ import { SoundsIcons } from "./SoundsIcons.js";
|
|
|
3
3
|
import { SoundsVolume } from "./SoundsVolume.js";
|
|
4
4
|
export class Sounds {
|
|
5
5
|
constructor() {
|
|
6
|
+
this.autoPlay = true;
|
|
6
7
|
this.enable = false;
|
|
7
8
|
this.events = [];
|
|
8
9
|
this.icons = new SoundsIcons();
|
|
@@ -12,6 +13,9 @@ export class Sounds {
|
|
|
12
13
|
if (!data) {
|
|
13
14
|
return;
|
|
14
15
|
}
|
|
16
|
+
if (data.autoPlay !== undefined) {
|
|
17
|
+
this.autoPlay = data.autoPlay;
|
|
18
|
+
}
|
|
15
19
|
if (data.enable !== undefined) {
|
|
16
20
|
this.enable = data.enable;
|
|
17
21
|
}
|
|
@@ -41,10 +41,11 @@ export class SoundsEvent {
|
|
|
41
41
|
return tmp;
|
|
42
42
|
});
|
|
43
43
|
}
|
|
44
|
-
if (data.filter
|
|
44
|
+
if (data.filter) {
|
|
45
45
|
if (isString(data.filter)) {
|
|
46
|
-
|
|
47
|
-
|
|
46
|
+
const filterFunc = window[data.filter];
|
|
47
|
+
if (isFunction(filterFunc)) {
|
|
48
|
+
this.filter = filterFunc;
|
|
48
49
|
}
|
|
49
50
|
}
|
|
50
51
|
else {
|
|
@@ -5,6 +5,7 @@ export class SoundsIcons {
|
|
|
5
5
|
this.unmute = new SoundsIcon();
|
|
6
6
|
this.volumeDown = new SoundsIcon();
|
|
7
7
|
this.volumeUp = new SoundsIcon();
|
|
8
|
+
this.enable = false;
|
|
8
9
|
this.mute.svg = `<?xml version="1.0"?>
|
|
9
10
|
<svg baseProfile="tiny" height="24px" version="1.2" viewBox="0 0 24 24" width="24px"
|
|
10
11
|
xml:space="preserve" xmlns="http://www.w3.org/2000/svg"
|
|
@@ -46,6 +47,9 @@ export class SoundsIcons {
|
|
|
46
47
|
if (!data) {
|
|
47
48
|
return;
|
|
48
49
|
}
|
|
50
|
+
if (data.enable !== undefined) {
|
|
51
|
+
this.enable = data.enable;
|
|
52
|
+
}
|
|
49
53
|
this.mute.load(data.mute);
|
|
50
54
|
this.unmute.load(data.unmute);
|
|
51
55
|
this.volumeDown.load(data.volumeDown);
|