@tsparticles/plugin-sounds 4.0.0-beta.1 → 4.0.0-beta.2
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/414.min.js +2 -2
- package/browser/SoundsPlugin.js +1 -0
- package/browser/index.js +2 -1
- package/cjs/SoundsPlugin.js +1 -0
- package/cjs/index.js +2 -1
- package/dist_browser_SoundsPluginInstance_js.js +1 -1
- package/dist_browser_SoundsPlugin_js.js +2 -2
- package/esm/SoundsPlugin.js +1 -0
- package/esm/index.js +2 -1
- package/package.json +3 -4
- package/report.html +1 -1
- package/tsparticles.plugin.sounds.js +2 -2
- package/tsparticles.plugin.sounds.min.js +1 -1
- package/umd/Options/Classes/Sounds.js +0 -54
- package/umd/Options/Classes/SoundsAudio.js +0 -39
- package/umd/Options/Classes/SoundsEvent.js +0 -75
- package/umd/Options/Classes/SoundsIcon.js +0 -44
- package/umd/Options/Classes/SoundsIcons.js +0 -78
- package/umd/Options/Classes/SoundsMelody.js +0 -48
- package/umd/Options/Classes/SoundsNote.js +0 -34
- package/umd/Options/Classes/SoundsVolume.js +0 -49
- package/umd/Options/Interfaces/ISounds.js +0 -12
- package/umd/Options/Interfaces/ISoundsAudio.js +0 -12
- package/umd/Options/Interfaces/ISoundsEvent.js +0 -12
- package/umd/Options/Interfaces/ISoundsIcon.js +0 -12
- package/umd/Options/Interfaces/ISoundsIcons.js +0 -12
- package/umd/Options/Interfaces/ISoundsMelody.js +0 -12
- package/umd/Options/Interfaces/ISoundsNote.js +0 -12
- package/umd/Options/Interfaces/ISoundsVolume.js +0 -12
- package/umd/SoundsPlugin.js +0 -86
- package/umd/SoundsPluginInstance.js +0 -451
- package/umd/constants.js +0 -14
- package/umd/enums.js +0 -23
- package/umd/index.js +0 -54
- package/umd/types.js +0 -12
- package/umd/utils.js +0 -48
|
@@ -1,12 +0,0 @@
|
|
|
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
|
-
});
|
|
@@ -1,12 +0,0 @@
|
|
|
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
|
-
});
|
|
@@ -1,12 +0,0 @@
|
|
|
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
|
-
});
|
|
@@ -1,12 +0,0 @@
|
|
|
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
|
-
});
|
|
@@ -1,12 +0,0 @@
|
|
|
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
|
-
});
|
|
@@ -1,12 +0,0 @@
|
|
|
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
|
-
});
|
|
@@ -1,12 +0,0 @@
|
|
|
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/SoundsPlugin.js
DELETED
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
2
|
-
if (k2 === undefined) k2 = k;
|
|
3
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
4
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
5
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
6
|
-
}
|
|
7
|
-
Object.defineProperty(o, k2, desc);
|
|
8
|
-
}) : (function(o, m, k, k2) {
|
|
9
|
-
if (k2 === undefined) k2 = k;
|
|
10
|
-
o[k2] = m[k];
|
|
11
|
-
}));
|
|
12
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
13
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
14
|
-
}) : function(o, v) {
|
|
15
|
-
o["default"] = v;
|
|
16
|
-
});
|
|
17
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
18
|
-
var ownKeys = function(o) {
|
|
19
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
20
|
-
var ar = [];
|
|
21
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
22
|
-
return ar;
|
|
23
|
-
};
|
|
24
|
-
return ownKeys(o);
|
|
25
|
-
};
|
|
26
|
-
return function (mod) {
|
|
27
|
-
if (mod && mod.__esModule) return mod;
|
|
28
|
-
var result = {};
|
|
29
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
30
|
-
__setModuleDefault(result, mod);
|
|
31
|
-
return result;
|
|
32
|
-
};
|
|
33
|
-
})();
|
|
34
|
-
(function (factory) {
|
|
35
|
-
if (typeof module === "object" && typeof module.exports === "object") {
|
|
36
|
-
var v = factory(require, exports);
|
|
37
|
-
if (v !== undefined) module.exports = v;
|
|
38
|
-
}
|
|
39
|
-
else if (typeof define === "function" && define.amd) {
|
|
40
|
-
define(["require", "exports", "./constants.js", "./Options/Classes/Sounds.js", "./utils.js"], factory);
|
|
41
|
-
}
|
|
42
|
-
})(function (require, exports) {
|
|
43
|
-
"use strict";
|
|
44
|
-
var __syncRequire = typeof module === "object" && typeof module.exports === "object";
|
|
45
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
46
|
-
exports.SoundsPlugin = void 0;
|
|
47
|
-
const constants_js_1 = require("./constants.js");
|
|
48
|
-
const Sounds_js_1 = require("./Options/Classes/Sounds.js");
|
|
49
|
-
const utils_js_1 = require("./utils.js");
|
|
50
|
-
const generalFirstClickHandler = () => {
|
|
51
|
-
removeEventListener(constants_js_1.mouseDownEvent, generalFirstClickHandler);
|
|
52
|
-
removeEventListener(constants_js_1.touchStartEvent, generalFirstClickHandler);
|
|
53
|
-
(0, utils_js_1.unmuteWindow)();
|
|
54
|
-
};
|
|
55
|
-
class SoundsPlugin {
|
|
56
|
-
id = "sounds";
|
|
57
|
-
_engine;
|
|
58
|
-
constructor(engine) {
|
|
59
|
-
this._engine = engine;
|
|
60
|
-
const listenerOptions = {
|
|
61
|
-
capture: true,
|
|
62
|
-
once: true,
|
|
63
|
-
};
|
|
64
|
-
addEventListener(constants_js_1.mouseDownEvent, generalFirstClickHandler, listenerOptions);
|
|
65
|
-
addEventListener(constants_js_1.touchStartEvent, generalFirstClickHandler, listenerOptions);
|
|
66
|
-
}
|
|
67
|
-
async getPlugin(container) {
|
|
68
|
-
const { SoundsPluginInstance } = await (__syncRequire ? Promise.resolve().then(() => __importStar(require("./SoundsPluginInstance.js"))) : new Promise((resolve_1, reject_1) => { require(["./SoundsPluginInstance.js"], resolve_1, reject_1); }).then(__importStar));
|
|
69
|
-
return new SoundsPluginInstance(container, this._engine);
|
|
70
|
-
}
|
|
71
|
-
loadOptions(_container, options, source) {
|
|
72
|
-
if (!this.needsPlugin(options) && !this.needsPlugin(source)) {
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
let soundsOptions = options.sounds;
|
|
76
|
-
if (soundsOptions?.load === undefined) {
|
|
77
|
-
options.sounds = soundsOptions = new Sounds_js_1.Sounds();
|
|
78
|
-
}
|
|
79
|
-
soundsOptions.load(source?.sounds);
|
|
80
|
-
}
|
|
81
|
-
needsPlugin(options) {
|
|
82
|
-
return options?.sounds?.enable ?? false;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
exports.SoundsPlugin = SoundsPlugin;
|
|
86
|
-
});
|
|
@@ -1,451 +0,0 @@
|
|
|
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", "./enums.js", "./utils.js", "./constants.js"], factory);
|
|
8
|
-
}
|
|
9
|
-
})(function (require, exports) {
|
|
10
|
-
"use strict";
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.SoundsPluginInstance = void 0;
|
|
13
|
-
const engine_1 = require("@tsparticles/engine");
|
|
14
|
-
const enums_js_1 = require("./enums.js");
|
|
15
|
-
const utils_js_1 = require("./utils.js");
|
|
16
|
-
const constants_js_1 = require("./constants.js");
|
|
17
|
-
const zIndexOffset = 1, rightOffset = 1, minVolume = 0;
|
|
18
|
-
function initImage(data) {
|
|
19
|
-
const img = (0, engine_1.safeDocument)().createElement("img"), { clickCb, container, display, iconOptions, margin, options, pos, rightOffsets } = data, { width, path, style, svg } = iconOptions, defaultAccumulator = 0;
|
|
20
|
-
setIconStyle(img, pos.top + margin, pos.right -
|
|
21
|
-
(margin * (rightOffsets.length + rightOffset) + width + rightOffsets.reduce((a, b) => a + b, defaultAccumulator)), display, options.fullScreen.zIndex + zIndexOffset, width, margin, style);
|
|
22
|
-
img.src = path ?? (svg ? `data:image/svg+xml;base64,${btoa(svg)}` : "");
|
|
23
|
-
const parent = container.canvas.element?.parentNode ?? (0, engine_1.safeDocument)().body;
|
|
24
|
-
parent.append(img);
|
|
25
|
-
img.addEventListener("click", () => {
|
|
26
|
-
void clickCb();
|
|
27
|
-
});
|
|
28
|
-
return img;
|
|
29
|
-
}
|
|
30
|
-
function removeImage(image) {
|
|
31
|
-
if (!image) {
|
|
32
|
-
return;
|
|
33
|
-
}
|
|
34
|
-
image.remove();
|
|
35
|
-
}
|
|
36
|
-
function setIconStyle(icon, top, left, display, zIndex, width, margin, style) {
|
|
37
|
-
icon.style.userSelect = "none";
|
|
38
|
-
icon.style.position = "absolute";
|
|
39
|
-
icon.style.top = `${(top + margin).toString()}px`;
|
|
40
|
-
icon.style.left = `${(left - margin - width).toString()}px`;
|
|
41
|
-
icon.style.display = display;
|
|
42
|
-
icon.style.zIndex = (zIndex + zIndexOffset).toString();
|
|
43
|
-
icon.style.cssText += style;
|
|
44
|
-
}
|
|
45
|
-
class SoundsPluginInstance {
|
|
46
|
-
_audioMap;
|
|
47
|
-
_audioSources;
|
|
48
|
-
_container;
|
|
49
|
-
_engine;
|
|
50
|
-
_gain;
|
|
51
|
-
_muteImg;
|
|
52
|
-
_unmuteImg;
|
|
53
|
-
_volume;
|
|
54
|
-
_volumeDownImg;
|
|
55
|
-
_volumeUpImg;
|
|
56
|
-
constructor(container, engine) {
|
|
57
|
-
this._container = container;
|
|
58
|
-
this._engine = engine;
|
|
59
|
-
this._volume = 0;
|
|
60
|
-
this._audioSources = [];
|
|
61
|
-
this._audioMap = new Map();
|
|
62
|
-
}
|
|
63
|
-
async init() {
|
|
64
|
-
const container = this._container, options = container.actualOptions, soundsOptions = options.sounds;
|
|
65
|
-
if (!soundsOptions?.enable) {
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
if (soundsOptions.autoPlay && (0, utils_js_1.isWindowMuted)()) {
|
|
69
|
-
const firstClickHandler = () => {
|
|
70
|
-
removeEventListener(constants_js_1.mouseDownEvent, firstClickHandler);
|
|
71
|
-
removeEventListener(constants_js_1.touchStartEvent, firstClickHandler);
|
|
72
|
-
(0, utils_js_1.unmuteWindow)();
|
|
73
|
-
void this.unmute();
|
|
74
|
-
}, listenerOptions = {
|
|
75
|
-
capture: true,
|
|
76
|
-
once: true,
|
|
77
|
-
};
|
|
78
|
-
addEventListener(constants_js_1.mouseDownEvent, firstClickHandler, listenerOptions);
|
|
79
|
-
addEventListener(constants_js_1.touchStartEvent, firstClickHandler, listenerOptions);
|
|
80
|
-
}
|
|
81
|
-
this._volume = soundsOptions.volume.value;
|
|
82
|
-
const events = soundsOptions.events;
|
|
83
|
-
this._audioMap = new Map();
|
|
84
|
-
for (const event of events) {
|
|
85
|
-
if (!event.audio) {
|
|
86
|
-
continue;
|
|
87
|
-
}
|
|
88
|
-
const promises = (0, engine_1.executeOnSingleOrMultiple)(event.audio, async (audio) => {
|
|
89
|
-
const response = await fetch(audio.source);
|
|
90
|
-
if (!response.ok) {
|
|
91
|
-
return;
|
|
92
|
-
}
|
|
93
|
-
const arrayBuffer = await response.arrayBuffer(), audioContext = this._getAudioContext(), audioBuffer = await audioContext.decodeAudioData(arrayBuffer);
|
|
94
|
-
this._audioMap.set(audio.source, audioBuffer);
|
|
95
|
-
});
|
|
96
|
-
if (promises instanceof Promise) {
|
|
97
|
-
await promises;
|
|
98
|
-
}
|
|
99
|
-
else {
|
|
100
|
-
await Promise.allSettled(promises);
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
async mute() {
|
|
105
|
-
if (!this._container.muted) {
|
|
106
|
-
await this.toggleMute();
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
async start() {
|
|
110
|
-
const container = this._container, options = container.actualOptions, soundsOptions = options.sounds;
|
|
111
|
-
if (!soundsOptions?.enable || !container.canvas.element) {
|
|
112
|
-
return;
|
|
113
|
-
}
|
|
114
|
-
container.muted = true;
|
|
115
|
-
const canvas = container.canvas.element, pos = {
|
|
116
|
-
top: canvas.offsetTop,
|
|
117
|
-
right: canvas.offsetLeft + canvas.offsetWidth,
|
|
118
|
-
}, { mute, unmute, volumeDown, volumeUp } = soundsOptions.icons, margin = 10, toggleMute = async () => {
|
|
119
|
-
await this.toggleMute();
|
|
120
|
-
}, enableIcons = soundsOptions.icons.enable, display = enableIcons ? enums_js_1.ImageDisplay.Block : enums_js_1.ImageDisplay.None;
|
|
121
|
-
this._muteImg = initImage({
|
|
122
|
-
container,
|
|
123
|
-
options,
|
|
124
|
-
pos,
|
|
125
|
-
display,
|
|
126
|
-
iconOptions: mute,
|
|
127
|
-
margin,
|
|
128
|
-
rightOffsets: [volumeDown.width, volumeUp.width],
|
|
129
|
-
clickCb: toggleMute,
|
|
130
|
-
});
|
|
131
|
-
this._unmuteImg = initImage({
|
|
132
|
-
container,
|
|
133
|
-
options,
|
|
134
|
-
pos,
|
|
135
|
-
display: enums_js_1.ImageDisplay.None,
|
|
136
|
-
iconOptions: unmute,
|
|
137
|
-
margin,
|
|
138
|
-
rightOffsets: [volumeDown.width, volumeUp.width],
|
|
139
|
-
clickCb: toggleMute,
|
|
140
|
-
});
|
|
141
|
-
this._volumeDownImg = initImage({
|
|
142
|
-
container,
|
|
143
|
-
options,
|
|
144
|
-
pos,
|
|
145
|
-
display,
|
|
146
|
-
iconOptions: volumeDown,
|
|
147
|
-
margin,
|
|
148
|
-
rightOffsets: [volumeUp.width],
|
|
149
|
-
clickCb: async () => {
|
|
150
|
-
await this.volumeDown();
|
|
151
|
-
},
|
|
152
|
-
});
|
|
153
|
-
this._volumeUpImg = initImage({
|
|
154
|
-
container,
|
|
155
|
-
options,
|
|
156
|
-
pos,
|
|
157
|
-
display,
|
|
158
|
-
iconOptions: volumeUp,
|
|
159
|
-
margin,
|
|
160
|
-
rightOffsets: [],
|
|
161
|
-
clickCb: async () => {
|
|
162
|
-
await this.volumeUp();
|
|
163
|
-
},
|
|
164
|
-
});
|
|
165
|
-
if (!(0, utils_js_1.isWindowMuted)() && soundsOptions.autoPlay) {
|
|
166
|
-
await this.unmute();
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
stop() {
|
|
170
|
-
this._container.muted = true;
|
|
171
|
-
void (async () => {
|
|
172
|
-
await this._mute();
|
|
173
|
-
removeImage(this._muteImg);
|
|
174
|
-
removeImage(this._unmuteImg);
|
|
175
|
-
removeImage(this._volumeDownImg);
|
|
176
|
-
removeImage(this._volumeUpImg);
|
|
177
|
-
})();
|
|
178
|
-
}
|
|
179
|
-
async toggleMute() {
|
|
180
|
-
const container = this._container;
|
|
181
|
-
container.muted = !container.muted;
|
|
182
|
-
this._updateMuteIcons();
|
|
183
|
-
await this._updateMuteStatus();
|
|
184
|
-
}
|
|
185
|
-
async unmute() {
|
|
186
|
-
if (this._container.muted) {
|
|
187
|
-
await this.toggleMute();
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
async volumeDown() {
|
|
191
|
-
const container = this._container, soundsOptions = container.actualOptions.sounds;
|
|
192
|
-
if (!soundsOptions?.enable) {
|
|
193
|
-
return;
|
|
194
|
-
}
|
|
195
|
-
if (container.muted) {
|
|
196
|
-
this._volume = 0;
|
|
197
|
-
}
|
|
198
|
-
this._volume -= soundsOptions.volume.step;
|
|
199
|
-
await this._updateVolume();
|
|
200
|
-
}
|
|
201
|
-
async volumeUp() {
|
|
202
|
-
const container = this._container, soundsOptions = container.actualOptions.sounds;
|
|
203
|
-
if (!soundsOptions?.enable) {
|
|
204
|
-
return;
|
|
205
|
-
}
|
|
206
|
-
this._volume += soundsOptions.volume.step;
|
|
207
|
-
await this._updateVolume();
|
|
208
|
-
}
|
|
209
|
-
_addBuffer = audioCtx => {
|
|
210
|
-
const buffer = audioCtx.createBufferSource();
|
|
211
|
-
this._audioSources.push(buffer);
|
|
212
|
-
return buffer;
|
|
213
|
-
};
|
|
214
|
-
_addOscillator = audioCtx => {
|
|
215
|
-
const oscillator = audioCtx.createOscillator();
|
|
216
|
-
this._audioSources.push(oscillator);
|
|
217
|
-
return oscillator;
|
|
218
|
-
};
|
|
219
|
-
_getAudioContext() {
|
|
220
|
-
const container = this._container;
|
|
221
|
-
container.audioContext ??= new AudioContext();
|
|
222
|
-
return container.audioContext;
|
|
223
|
-
}
|
|
224
|
-
_initEvents = () => {
|
|
225
|
-
const container = this._container, soundsOptions = container.actualOptions.sounds;
|
|
226
|
-
if (!soundsOptions?.enable || !container.canvas.element) {
|
|
227
|
-
return;
|
|
228
|
-
}
|
|
229
|
-
for (const event of soundsOptions.events) {
|
|
230
|
-
const cb = (args) => {
|
|
231
|
-
if (!args) {
|
|
232
|
-
return;
|
|
233
|
-
}
|
|
234
|
-
void (async () => {
|
|
235
|
-
const filterNotValid = event.filter && !event.filter(args);
|
|
236
|
-
if (this._container !== args.container) {
|
|
237
|
-
return;
|
|
238
|
-
}
|
|
239
|
-
if (!!this._container.muted || this._container.destroyed) {
|
|
240
|
-
(0, engine_1.executeOnSingleOrMultiple)(event.event, item => {
|
|
241
|
-
this._engine.removeEventListener(item, cb);
|
|
242
|
-
});
|
|
243
|
-
return;
|
|
244
|
-
}
|
|
245
|
-
if (filterNotValid) {
|
|
246
|
-
return;
|
|
247
|
-
}
|
|
248
|
-
const defaultNoteIndex = 0;
|
|
249
|
-
if (event.audio) {
|
|
250
|
-
const audio = (0, engine_1.itemFromSingleOrMultiple)(event.audio);
|
|
251
|
-
if (!audio) {
|
|
252
|
-
return;
|
|
253
|
-
}
|
|
254
|
-
this._playBuffer(audio);
|
|
255
|
-
}
|
|
256
|
-
else if (event.melodies) {
|
|
257
|
-
const melody = (0, engine_1.itemFromArray)(event.melodies);
|
|
258
|
-
if (!melody) {
|
|
259
|
-
return;
|
|
260
|
-
}
|
|
261
|
-
if (melody.melodies.length) {
|
|
262
|
-
await Promise.allSettled(melody.melodies.map(m => this._playNote(m.notes, defaultNoteIndex, melody.loop)));
|
|
263
|
-
}
|
|
264
|
-
else {
|
|
265
|
-
await this._playNote(melody.notes, defaultNoteIndex, melody.loop);
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
else if (event.notes) {
|
|
269
|
-
const note = (0, engine_1.itemFromArray)(event.notes);
|
|
270
|
-
if (!note) {
|
|
271
|
-
return;
|
|
272
|
-
}
|
|
273
|
-
await this._playNote([note], defaultNoteIndex, false);
|
|
274
|
-
}
|
|
275
|
-
})();
|
|
276
|
-
};
|
|
277
|
-
(0, engine_1.executeOnSingleOrMultiple)(event.event, item => {
|
|
278
|
-
this._engine.addEventListener(item, cb);
|
|
279
|
-
});
|
|
280
|
-
}
|
|
281
|
-
};
|
|
282
|
-
_mute = async () => {
|
|
283
|
-
const container = this._container, audioContext = this._getAudioContext();
|
|
284
|
-
for (const source of this._audioSources) {
|
|
285
|
-
this._removeAudioSource(source);
|
|
286
|
-
}
|
|
287
|
-
if (this._gain) {
|
|
288
|
-
this._gain.disconnect();
|
|
289
|
-
}
|
|
290
|
-
await audioContext.close();
|
|
291
|
-
container.audioContext = undefined;
|
|
292
|
-
this._container.dispatchEvent(enums_js_1.SoundsEventType.mute);
|
|
293
|
-
};
|
|
294
|
-
_playBuffer = audio => {
|
|
295
|
-
const audioBuffer = this._audioMap.get(audio.source);
|
|
296
|
-
if (!audioBuffer) {
|
|
297
|
-
return;
|
|
298
|
-
}
|
|
299
|
-
const audioCtx = this._container.audioContext;
|
|
300
|
-
if (!audioCtx) {
|
|
301
|
-
return;
|
|
302
|
-
}
|
|
303
|
-
const source = this._addBuffer(audioCtx);
|
|
304
|
-
source.loop = audio.loop;
|
|
305
|
-
source.buffer = audioBuffer;
|
|
306
|
-
source.connect(this._gain ?? audioCtx.destination);
|
|
307
|
-
source.start();
|
|
308
|
-
};
|
|
309
|
-
_playFrequency = async (frequency, duration) => {
|
|
310
|
-
if (!this._gain || this._container.muted) {
|
|
311
|
-
return;
|
|
312
|
-
}
|
|
313
|
-
const audioContext = this._getAudioContext(), oscillator = this._addOscillator(audioContext);
|
|
314
|
-
oscillator.connect(this._gain);
|
|
315
|
-
oscillator.type = "sine";
|
|
316
|
-
oscillator.frequency.value = frequency;
|
|
317
|
-
oscillator.start();
|
|
318
|
-
return new Promise(resolve => {
|
|
319
|
-
setTimeout(() => {
|
|
320
|
-
this._removeAudioSource(oscillator);
|
|
321
|
-
resolve();
|
|
322
|
-
}, duration);
|
|
323
|
-
});
|
|
324
|
-
};
|
|
325
|
-
_playMuteSound = () => {
|
|
326
|
-
if (this._container.muted) {
|
|
327
|
-
return;
|
|
328
|
-
}
|
|
329
|
-
const audioContext = this._getAudioContext(), gain = audioContext.createGain();
|
|
330
|
-
gain.connect(audioContext.destination);
|
|
331
|
-
gain.gain.value = 0;
|
|
332
|
-
const oscillator = audioContext.createOscillator();
|
|
333
|
-
oscillator.connect(gain);
|
|
334
|
-
oscillator.type = "sine";
|
|
335
|
-
oscillator.frequency.value = 1;
|
|
336
|
-
oscillator.start();
|
|
337
|
-
setTimeout(() => {
|
|
338
|
-
oscillator.stop();
|
|
339
|
-
oscillator.disconnect();
|
|
340
|
-
gain.disconnect();
|
|
341
|
-
});
|
|
342
|
-
};
|
|
343
|
-
_playNote = async (notes, noteIdx, loop) => {
|
|
344
|
-
if (this._container.muted) {
|
|
345
|
-
return;
|
|
346
|
-
}
|
|
347
|
-
const note = notes[noteIdx];
|
|
348
|
-
if (!note) {
|
|
349
|
-
return;
|
|
350
|
-
}
|
|
351
|
-
const value = note.value, promises = (0, engine_1.executeOnSingleOrMultiple)(value, async (_, idx) => {
|
|
352
|
-
return this._playNoteValue(notes, noteIdx, idx);
|
|
353
|
-
});
|
|
354
|
-
await ((0, engine_1.isArray)(promises) ? Promise.allSettled(promises) : promises);
|
|
355
|
-
const indexOffset = 1;
|
|
356
|
-
let nextNoteIdx = noteIdx + indexOffset;
|
|
357
|
-
if (loop && nextNoteIdx >= notes.length) {
|
|
358
|
-
nextNoteIdx = nextNoteIdx % notes.length;
|
|
359
|
-
}
|
|
360
|
-
await this._playNote(notes, nextNoteIdx, loop);
|
|
361
|
-
};
|
|
362
|
-
_playNoteValue = async (notes, noteIdx, valueIdx) => {
|
|
363
|
-
const note = notes[noteIdx];
|
|
364
|
-
if (!note) {
|
|
365
|
-
return;
|
|
366
|
-
}
|
|
367
|
-
const value = (0, engine_1.itemFromSingleOrMultiple)(note.value, valueIdx, true);
|
|
368
|
-
if (!value) {
|
|
369
|
-
return;
|
|
370
|
-
}
|
|
371
|
-
try {
|
|
372
|
-
const freq = (0, utils_js_1.getNoteFrequency)(value);
|
|
373
|
-
if (!(0, engine_1.isNumber)(freq)) {
|
|
374
|
-
return;
|
|
375
|
-
}
|
|
376
|
-
await this._playFrequency(freq, note.duration);
|
|
377
|
-
}
|
|
378
|
-
catch (e) {
|
|
379
|
-
(0, engine_1.getLogger)().error(e);
|
|
380
|
-
}
|
|
381
|
-
};
|
|
382
|
-
_removeAudioSource = source => {
|
|
383
|
-
source.stop();
|
|
384
|
-
source.disconnect();
|
|
385
|
-
const deleteCount = 1;
|
|
386
|
-
this._audioSources.splice(this._audioSources.indexOf(source), deleteCount);
|
|
387
|
-
};
|
|
388
|
-
_unmute = () => {
|
|
389
|
-
const container = this._container, options = container.actualOptions, soundsOptions = options.sounds;
|
|
390
|
-
if (!soundsOptions) {
|
|
391
|
-
return;
|
|
392
|
-
}
|
|
393
|
-
const audioContext = this._getAudioContext(), gain = audioContext.createGain();
|
|
394
|
-
gain.connect(audioContext.destination);
|
|
395
|
-
gain.gain.value = soundsOptions.volume.value / engine_1.percentDenominator;
|
|
396
|
-
this._gain = gain;
|
|
397
|
-
this._initEvents();
|
|
398
|
-
this._container.dispatchEvent(enums_js_1.SoundsEventType.unmute);
|
|
399
|
-
};
|
|
400
|
-
_updateMuteIcons = () => {
|
|
401
|
-
const container = this._container, soundsOptions = container.actualOptions.sounds;
|
|
402
|
-
if (!soundsOptions?.enable || !soundsOptions.icons.enable) {
|
|
403
|
-
return;
|
|
404
|
-
}
|
|
405
|
-
const muteImg = this._muteImg, unmuteImg = this._unmuteImg;
|
|
406
|
-
if (muteImg) {
|
|
407
|
-
muteImg.style.display = container.muted ? "block" : "none";
|
|
408
|
-
}
|
|
409
|
-
if (unmuteImg) {
|
|
410
|
-
unmuteImg.style.display = container.muted ? "none" : "block";
|
|
411
|
-
}
|
|
412
|
-
};
|
|
413
|
-
_updateMuteStatus = async () => {
|
|
414
|
-
const container = this._container, audioContext = this._getAudioContext();
|
|
415
|
-
if (container.muted) {
|
|
416
|
-
await audioContext.suspend();
|
|
417
|
-
await this._mute();
|
|
418
|
-
}
|
|
419
|
-
else {
|
|
420
|
-
await audioContext.resume();
|
|
421
|
-
this._unmute();
|
|
422
|
-
this._playMuteSound();
|
|
423
|
-
}
|
|
424
|
-
};
|
|
425
|
-
_updateVolume = async () => {
|
|
426
|
-
const container = this._container, soundsOptions = container.actualOptions.sounds;
|
|
427
|
-
if (!soundsOptions?.enable) {
|
|
428
|
-
return;
|
|
429
|
-
}
|
|
430
|
-
(0, engine_1.clamp)(this._volume, soundsOptions.volume.min, soundsOptions.volume.max);
|
|
431
|
-
let stateChanged = false;
|
|
432
|
-
if (this._volume <= minVolume && !container.muted) {
|
|
433
|
-
this._volume = 0;
|
|
434
|
-
container.muted = true;
|
|
435
|
-
stateChanged = true;
|
|
436
|
-
}
|
|
437
|
-
else if (this._volume > minVolume && container.muted) {
|
|
438
|
-
container.muted = false;
|
|
439
|
-
stateChanged = true;
|
|
440
|
-
}
|
|
441
|
-
if (stateChanged) {
|
|
442
|
-
this._updateMuteIcons();
|
|
443
|
-
await this._updateMuteStatus();
|
|
444
|
-
}
|
|
445
|
-
if (this._gain?.gain) {
|
|
446
|
-
this._gain.gain.value = this._volume / engine_1.percentDenominator;
|
|
447
|
-
}
|
|
448
|
-
};
|
|
449
|
-
}
|
|
450
|
-
exports.SoundsPluginInstance = SoundsPluginInstance;
|
|
451
|
-
});
|
package/umd/constants.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
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.touchStartEvent = exports.mouseDownEvent = void 0;
|
|
13
|
-
exports.mouseDownEvent = "pointerdown", exports.touchStartEvent = "touchstart";
|
|
14
|
-
});
|
package/umd/enums.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
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.ImageDisplay = exports.SoundsEventType = void 0;
|
|
13
|
-
var SoundsEventType;
|
|
14
|
-
(function (SoundsEventType) {
|
|
15
|
-
SoundsEventType["mute"] = "soundsMuted";
|
|
16
|
-
SoundsEventType["unmute"] = "soundsUnmuted";
|
|
17
|
-
})(SoundsEventType || (exports.SoundsEventType = SoundsEventType = {}));
|
|
18
|
-
var ImageDisplay;
|
|
19
|
-
(function (ImageDisplay) {
|
|
20
|
-
ImageDisplay["Block"] = "block";
|
|
21
|
-
ImageDisplay["None"] = "none";
|
|
22
|
-
})(ImageDisplay || (exports.ImageDisplay = ImageDisplay = {}));
|
|
23
|
-
});
|