@eva/plugin-sound 2.0.0-beta.3 → 2.0.0-beta.6
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/dist/EVA.plugin.sound.js +1620 -221
- package/dist/EVA.plugin.sound.min.js +1 -1
- package/dist/plugin-sound.cjs.js +29 -232
- package/dist/plugin-sound.cjs.prod.js +3 -2
- package/dist/plugin-sound.d.ts +6 -27
- package/dist/plugin-sound.esm.js +29 -232
- package/package.json +5 -3
package/dist/EVA.plugin.sound.js
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
2
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
3
|
+
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
4
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|
5
|
+
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
1
6
|
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
|
|
2
7
|
window.EVA = window.EVA || {};
|
|
3
8
|
window.EVA.plugin = window.EVA.plugin || {};
|
|
4
|
-
var _EVA_IIFE_sound = function (exports, eva_js) {
|
|
9
|
+
var _EVA_IIFE_sound = function (exports, eva_js, pixi_js) {
|
|
5
10
|
'use strict';
|
|
6
11
|
function __decorate(decorators, target, key, desc) {
|
|
7
12
|
var c = arguments.length,
|
|
@@ -37,69 +42,1618 @@ var _EVA_IIFE_sound = function (exports, eva_js) {
|
|
|
37
42
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
38
43
|
});
|
|
39
44
|
}
|
|
40
|
-
let
|
|
41
|
-
|
|
45
|
+
let instance;
|
|
46
|
+
function setInstance(sound) {
|
|
47
|
+
instance = sound;
|
|
48
|
+
return sound;
|
|
49
|
+
}
|
|
50
|
+
function getInstance() {
|
|
51
|
+
return instance;
|
|
52
|
+
}
|
|
53
|
+
class WebAudioUtils {
|
|
54
|
+
static setParamValue(param, value) {
|
|
55
|
+
if (param.setValueAtTime) {
|
|
56
|
+
const context = getInstance().context;
|
|
57
|
+
param.setValueAtTime(value, context.audioContext.currentTime);
|
|
58
|
+
} else {
|
|
59
|
+
param.value = value;
|
|
60
|
+
}
|
|
61
|
+
return value;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
class HTMLAudioContext extends pixi_js.EventEmitter {
|
|
65
|
+
constructor() {
|
|
66
|
+
super(...arguments);
|
|
67
|
+
this.speed = 1;
|
|
68
|
+
this.muted = false;
|
|
69
|
+
this.volume = 1;
|
|
70
|
+
this.paused = false;
|
|
71
|
+
}
|
|
72
|
+
refresh() {
|
|
73
|
+
this.emit("refresh");
|
|
74
|
+
}
|
|
75
|
+
refreshPaused() {
|
|
76
|
+
this.emit("refreshPaused");
|
|
77
|
+
}
|
|
78
|
+
get filters() {
|
|
79
|
+
console.warn("HTML Audio does not support filters");
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
set filters(_filters) {
|
|
83
|
+
console.warn("HTML Audio does not support filters");
|
|
84
|
+
}
|
|
85
|
+
get audioContext() {
|
|
86
|
+
console.warn("HTML Audio does not support audioContext");
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
toggleMute() {
|
|
90
|
+
this.muted = !this.muted;
|
|
91
|
+
this.refresh();
|
|
92
|
+
return this.muted;
|
|
93
|
+
}
|
|
94
|
+
togglePause() {
|
|
95
|
+
this.paused = !this.paused;
|
|
96
|
+
this.refreshPaused();
|
|
97
|
+
return this.paused;
|
|
98
|
+
}
|
|
99
|
+
destroy() {
|
|
100
|
+
this.removeAllListeners();
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
let id$1 = 0;
|
|
104
|
+
const _HTMLAudioInstance = class extends pixi_js.EventEmitter {
|
|
105
|
+
constructor(parent) {
|
|
42
106
|
super();
|
|
43
|
-
this.
|
|
44
|
-
this.
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
this
|
|
48
|
-
|
|
107
|
+
this.id = id$1++;
|
|
108
|
+
this.init(parent);
|
|
109
|
+
}
|
|
110
|
+
set(name, value) {
|
|
111
|
+
if (this[name] === void 0) {
|
|
112
|
+
throw new Error(`Property with name ${name} does not exist.`);
|
|
113
|
+
} else {
|
|
114
|
+
switch (name) {
|
|
115
|
+
case "speed":
|
|
116
|
+
this.speed = value;
|
|
117
|
+
break;
|
|
118
|
+
case "volume":
|
|
119
|
+
this.volume = value;
|
|
120
|
+
break;
|
|
121
|
+
case "paused":
|
|
122
|
+
this.paused = value;
|
|
123
|
+
break;
|
|
124
|
+
case "loop":
|
|
125
|
+
this.loop = value;
|
|
126
|
+
break;
|
|
127
|
+
case "muted":
|
|
128
|
+
this.muted = value;
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return this;
|
|
133
|
+
}
|
|
134
|
+
get progress() {
|
|
135
|
+
const {
|
|
136
|
+
currentTime
|
|
137
|
+
} = this._source;
|
|
138
|
+
return currentTime / this._duration;
|
|
139
|
+
}
|
|
140
|
+
get paused() {
|
|
141
|
+
return this._paused;
|
|
142
|
+
}
|
|
143
|
+
set paused(paused) {
|
|
144
|
+
this._paused = paused;
|
|
145
|
+
this.refreshPaused();
|
|
146
|
+
}
|
|
147
|
+
_onPlay() {
|
|
148
|
+
this._playing = true;
|
|
149
|
+
}
|
|
150
|
+
_onPause() {
|
|
151
|
+
this._playing = false;
|
|
152
|
+
}
|
|
153
|
+
init(media) {
|
|
154
|
+
this._playing = false;
|
|
155
|
+
this._duration = media.source.duration;
|
|
156
|
+
const source = this._source = media.source.cloneNode(false);
|
|
157
|
+
source.src = media.parent.url;
|
|
158
|
+
source.onplay = this._onPlay.bind(this);
|
|
159
|
+
source.onpause = this._onPause.bind(this);
|
|
160
|
+
media.context.on("refresh", this.refresh, this);
|
|
161
|
+
media.context.on("refreshPaused", this.refreshPaused, this);
|
|
162
|
+
this._media = media;
|
|
163
|
+
}
|
|
164
|
+
_internalStop() {
|
|
165
|
+
if (this._source && this._playing) {
|
|
166
|
+
this._source.onended = null;
|
|
167
|
+
this._source.pause();
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
stop() {
|
|
171
|
+
this._internalStop();
|
|
172
|
+
if (this._source) {
|
|
173
|
+
this.emit("stop");
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
get speed() {
|
|
177
|
+
return this._speed;
|
|
178
|
+
}
|
|
179
|
+
set speed(speed) {
|
|
180
|
+
this._speed = speed;
|
|
181
|
+
this.refresh();
|
|
182
|
+
}
|
|
183
|
+
get volume() {
|
|
184
|
+
return this._volume;
|
|
185
|
+
}
|
|
186
|
+
set volume(volume) {
|
|
187
|
+
this._volume = volume;
|
|
188
|
+
this.refresh();
|
|
189
|
+
}
|
|
190
|
+
get loop() {
|
|
191
|
+
return this._loop;
|
|
192
|
+
}
|
|
193
|
+
set loop(loop) {
|
|
194
|
+
this._loop = loop;
|
|
195
|
+
this.refresh();
|
|
49
196
|
}
|
|
50
197
|
get muted() {
|
|
51
|
-
return this.
|
|
198
|
+
return this._muted;
|
|
52
199
|
}
|
|
53
|
-
set muted(
|
|
54
|
-
|
|
200
|
+
set muted(muted) {
|
|
201
|
+
this._muted = muted;
|
|
202
|
+
this.refresh();
|
|
203
|
+
}
|
|
204
|
+
get filters() {
|
|
205
|
+
console.warn("HTML Audio does not support filters");
|
|
206
|
+
return null;
|
|
207
|
+
}
|
|
208
|
+
set filters(_filters) {
|
|
209
|
+
console.warn("HTML Audio does not support filters");
|
|
210
|
+
}
|
|
211
|
+
refresh() {
|
|
212
|
+
const global = this._media.context;
|
|
213
|
+
const sound = this._media.parent;
|
|
214
|
+
this._source.loop = this._loop || sound.loop;
|
|
215
|
+
const globalVolume = global.volume * (global.muted ? 0 : 1);
|
|
216
|
+
const soundVolume = sound.volume * (sound.muted ? 0 : 1);
|
|
217
|
+
const instanceVolume = this._volume * (this._muted ? 0 : 1);
|
|
218
|
+
this._source.volume = instanceVolume * globalVolume * soundVolume;
|
|
219
|
+
this._source.playbackRate = this._speed * global.speed * sound.speed;
|
|
220
|
+
}
|
|
221
|
+
refreshPaused() {
|
|
222
|
+
const global = this._media.context;
|
|
223
|
+
const sound = this._media.parent;
|
|
224
|
+
const pausedReal = this._paused || sound.paused || global.paused;
|
|
225
|
+
if (pausedReal !== this._pausedReal) {
|
|
226
|
+
this._pausedReal = pausedReal;
|
|
227
|
+
if (pausedReal) {
|
|
228
|
+
this._internalStop();
|
|
229
|
+
this.emit("paused");
|
|
230
|
+
} else {
|
|
231
|
+
this.emit("resumed");
|
|
232
|
+
this.play({
|
|
233
|
+
start: this._source.currentTime,
|
|
234
|
+
end: this._end,
|
|
235
|
+
volume: this._volume,
|
|
236
|
+
speed: this._speed,
|
|
237
|
+
loop: this._loop
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
this.emit("pause", pausedReal);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
play(options) {
|
|
244
|
+
const {
|
|
245
|
+
start,
|
|
246
|
+
end,
|
|
247
|
+
speed,
|
|
248
|
+
loop,
|
|
249
|
+
volume,
|
|
250
|
+
muted
|
|
251
|
+
} = options;
|
|
252
|
+
if (end) {
|
|
253
|
+
console.assert(end > start, "End time is before start time");
|
|
254
|
+
}
|
|
255
|
+
this._speed = speed;
|
|
256
|
+
this._volume = volume;
|
|
257
|
+
this._loop = !!loop;
|
|
258
|
+
this._muted = muted;
|
|
259
|
+
this.refresh();
|
|
260
|
+
if (this.loop && end !== null) {
|
|
261
|
+
console.warn('Looping not support when specifying an "end" time');
|
|
262
|
+
this.loop = false;
|
|
263
|
+
}
|
|
264
|
+
this._start = start;
|
|
265
|
+
this._end = end || this._duration;
|
|
266
|
+
this._start = Math.max(0, this._start - _HTMLAudioInstance.PADDING);
|
|
267
|
+
this._end = Math.min(this._end + _HTMLAudioInstance.PADDING, this._duration);
|
|
268
|
+
this._source.onloadedmetadata = () => {
|
|
269
|
+
if (this._source) {
|
|
270
|
+
this._source.currentTime = start;
|
|
271
|
+
this._source.onloadedmetadata = null;
|
|
272
|
+
this.emit("progress", start / this._duration, this._duration);
|
|
273
|
+
pixi_js.Ticker.shared.add(this._onUpdate, this);
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
this._source.onended = this._onComplete.bind(this);
|
|
277
|
+
this._source.play();
|
|
278
|
+
this.emit("start");
|
|
279
|
+
}
|
|
280
|
+
_onUpdate() {
|
|
281
|
+
this.emit("progress", this.progress, this._duration);
|
|
282
|
+
if (this._source.currentTime >= this._end && !this._source.loop) {
|
|
283
|
+
this._onComplete();
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
_onComplete() {
|
|
287
|
+
pixi_js.Ticker.shared.remove(this._onUpdate, this);
|
|
288
|
+
this._internalStop();
|
|
289
|
+
this.emit("progress", 1, this._duration);
|
|
290
|
+
this.emit("end", this);
|
|
291
|
+
}
|
|
292
|
+
destroy() {
|
|
293
|
+
pixi_js.Ticker.shared.remove(this._onUpdate, this);
|
|
294
|
+
this.removeAllListeners();
|
|
295
|
+
const source = this._source;
|
|
296
|
+
if (source) {
|
|
297
|
+
source.onended = null;
|
|
298
|
+
source.onplay = null;
|
|
299
|
+
source.onpause = null;
|
|
300
|
+
this._internalStop();
|
|
301
|
+
}
|
|
302
|
+
this._source = null;
|
|
303
|
+
this._speed = 1;
|
|
304
|
+
this._volume = 1;
|
|
305
|
+
this._loop = false;
|
|
306
|
+
this._end = null;
|
|
307
|
+
this._start = 0;
|
|
308
|
+
this._duration = 0;
|
|
309
|
+
this._playing = false;
|
|
310
|
+
this._pausedReal = false;
|
|
311
|
+
this._paused = false;
|
|
312
|
+
this._muted = false;
|
|
313
|
+
if (this._media) {
|
|
314
|
+
this._media.context.off("refresh", this.refresh, this);
|
|
315
|
+
this._media.context.off("refreshPaused", this.refreshPaused, this);
|
|
316
|
+
this._media = null;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
toString() {
|
|
320
|
+
return `[HTMLAudioInstance id=${this.id}]`;
|
|
321
|
+
}
|
|
322
|
+
};
|
|
323
|
+
let HTMLAudioInstance = _HTMLAudioInstance;
|
|
324
|
+
HTMLAudioInstance.PADDING = 0.1;
|
|
325
|
+
class HTMLAudioMedia extends pixi_js.EventEmitter {
|
|
326
|
+
init(parent) {
|
|
327
|
+
this.parent = parent;
|
|
328
|
+
this._source = parent.options.source || new Audio();
|
|
329
|
+
if (parent.url) {
|
|
330
|
+
this._source.src = parent.url;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
create() {
|
|
334
|
+
return new HTMLAudioInstance(this);
|
|
335
|
+
}
|
|
336
|
+
get isPlayable() {
|
|
337
|
+
return !!this._source && this._source.readyState === 4;
|
|
338
|
+
}
|
|
339
|
+
get duration() {
|
|
340
|
+
return this._source.duration;
|
|
341
|
+
}
|
|
342
|
+
get context() {
|
|
343
|
+
return this.parent.context;
|
|
344
|
+
}
|
|
345
|
+
get filters() {
|
|
346
|
+
return null;
|
|
347
|
+
}
|
|
348
|
+
set filters(_filters) {
|
|
349
|
+
console.warn("HTML Audio does not support filters");
|
|
350
|
+
}
|
|
351
|
+
destroy() {
|
|
352
|
+
this.removeAllListeners();
|
|
353
|
+
this.parent = null;
|
|
354
|
+
if (this._source) {
|
|
355
|
+
this._source.src = "";
|
|
356
|
+
this._source.load();
|
|
357
|
+
this._source = null;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
get source() {
|
|
361
|
+
return this._source;
|
|
362
|
+
}
|
|
363
|
+
load(callback) {
|
|
364
|
+
const source = this._source;
|
|
365
|
+
const sound = this.parent;
|
|
366
|
+
if (source.readyState === 4) {
|
|
367
|
+
sound.isLoaded = true;
|
|
368
|
+
const instance = sound.autoPlayStart();
|
|
369
|
+
if (callback) {
|
|
370
|
+
setTimeout(() => {
|
|
371
|
+
callback(null, sound, instance);
|
|
372
|
+
}, 0);
|
|
373
|
+
}
|
|
55
374
|
return;
|
|
56
375
|
}
|
|
57
|
-
|
|
376
|
+
if (!sound.url) {
|
|
377
|
+
callback(new Error("sound.url or sound.source must be set"));
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
source.src = sound.url;
|
|
381
|
+
const onLoad = () => {
|
|
382
|
+
removeListeners();
|
|
383
|
+
sound.isLoaded = true;
|
|
384
|
+
const instance = sound.autoPlayStart();
|
|
385
|
+
if (callback) {
|
|
386
|
+
callback(null, sound, instance);
|
|
387
|
+
}
|
|
388
|
+
};
|
|
389
|
+
const onAbort = () => {
|
|
390
|
+
removeListeners();
|
|
391
|
+
if (callback) {
|
|
392
|
+
callback(new Error("Sound loading has been aborted"));
|
|
393
|
+
}
|
|
394
|
+
};
|
|
395
|
+
const onError = () => {
|
|
396
|
+
removeListeners();
|
|
397
|
+
const message = `Failed to load audio element (code: ${source.error.code})`;
|
|
398
|
+
if (callback) {
|
|
399
|
+
callback(new Error(message));
|
|
400
|
+
} else {
|
|
401
|
+
console.error(message);
|
|
402
|
+
}
|
|
403
|
+
};
|
|
404
|
+
const removeListeners = () => {
|
|
405
|
+
source.removeEventListener("canplaythrough", onLoad);
|
|
406
|
+
source.removeEventListener("load", onLoad);
|
|
407
|
+
source.removeEventListener("abort", onAbort);
|
|
408
|
+
source.removeEventListener("error", onError);
|
|
409
|
+
};
|
|
410
|
+
source.addEventListener("canplaythrough", onLoad, false);
|
|
411
|
+
source.addEventListener("load", onLoad, false);
|
|
412
|
+
source.addEventListener("abort", onAbort, false);
|
|
413
|
+
source.addEventListener("error", onError, false);
|
|
414
|
+
source.load();
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
class SoundSprite {
|
|
418
|
+
constructor(parent, options) {
|
|
419
|
+
this.parent = parent;
|
|
420
|
+
_extends(this, options);
|
|
421
|
+
this.duration = this.end - this.start;
|
|
422
|
+
console.assert(this.duration > 0, "End time must be after start time");
|
|
423
|
+
}
|
|
424
|
+
play(complete) {
|
|
425
|
+
return this.parent.play({
|
|
426
|
+
complete,
|
|
427
|
+
speed: this.speed || this.parent.speed,
|
|
428
|
+
end: this.end,
|
|
429
|
+
start: this.start,
|
|
430
|
+
loop: this.loop
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
destroy() {
|
|
434
|
+
this.parent = null;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
const extensions = ["ogg", "oga", "opus", "m4a", "mp3", "mpeg", "wav", "aiff", "wma", "mid", "caf"];
|
|
438
|
+
const mimes = ["audio/mpeg", "audio/ogg"];
|
|
439
|
+
const supported = {};
|
|
440
|
+
function validateFormats(typeOverrides) {
|
|
441
|
+
const overrides = _objectSpread({
|
|
442
|
+
m4a: "audio/mp4",
|
|
443
|
+
oga: "audio/ogg",
|
|
444
|
+
opus: 'audio/ogg; codecs="opus"',
|
|
445
|
+
caf: 'audio/x-caf; codecs="opus"'
|
|
446
|
+
}, typeOverrides || {});
|
|
447
|
+
const audio = document.createElement("audio");
|
|
448
|
+
const formats = {};
|
|
449
|
+
const no = /^no$/;
|
|
450
|
+
extensions.forEach(ext => {
|
|
451
|
+
const canByExt = audio.canPlayType(`audio/${ext}`).replace(no, "");
|
|
452
|
+
const canByType = overrides[ext] ? audio.canPlayType(overrides[ext]).replace(no, "") : "";
|
|
453
|
+
formats[ext] = !!canByExt || !!canByType;
|
|
454
|
+
});
|
|
455
|
+
_extends(supported, formats);
|
|
456
|
+
}
|
|
457
|
+
validateFormats();
|
|
458
|
+
let id = 0;
|
|
459
|
+
class WebAudioInstance extends pixi_js.EventEmitter {
|
|
460
|
+
constructor(media) {
|
|
461
|
+
super();
|
|
462
|
+
this.id = id++;
|
|
463
|
+
this._media = null;
|
|
464
|
+
this._paused = false;
|
|
465
|
+
this._muted = false;
|
|
466
|
+
this._elapsed = 0;
|
|
467
|
+
this.init(media);
|
|
468
|
+
}
|
|
469
|
+
set(name, value) {
|
|
470
|
+
if (this[name] === void 0) {
|
|
471
|
+
throw new Error(`Property with name ${name} does not exist.`);
|
|
472
|
+
} else {
|
|
473
|
+
switch (name) {
|
|
474
|
+
case "speed":
|
|
475
|
+
this.speed = value;
|
|
476
|
+
break;
|
|
477
|
+
case "volume":
|
|
478
|
+
this.volume = value;
|
|
479
|
+
break;
|
|
480
|
+
case "muted":
|
|
481
|
+
this.muted = value;
|
|
482
|
+
break;
|
|
483
|
+
case "loop":
|
|
484
|
+
this.loop = value;
|
|
485
|
+
break;
|
|
486
|
+
case "paused":
|
|
487
|
+
this.paused = value;
|
|
488
|
+
break;
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
return this;
|
|
492
|
+
}
|
|
493
|
+
stop() {
|
|
494
|
+
if (this._source) {
|
|
495
|
+
this._internalStop();
|
|
496
|
+
this.emit("stop");
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
get speed() {
|
|
500
|
+
return this._speed;
|
|
501
|
+
}
|
|
502
|
+
set speed(speed) {
|
|
503
|
+
this._speed = speed;
|
|
504
|
+
this.refresh();
|
|
505
|
+
this._update(true);
|
|
58
506
|
}
|
|
59
507
|
get volume() {
|
|
60
|
-
return this.
|
|
508
|
+
return this._volume;
|
|
61
509
|
}
|
|
62
|
-
set volume(
|
|
63
|
-
|
|
510
|
+
set volume(volume) {
|
|
511
|
+
this._volume = volume;
|
|
512
|
+
this.refresh();
|
|
513
|
+
}
|
|
514
|
+
get muted() {
|
|
515
|
+
return this._muted;
|
|
516
|
+
}
|
|
517
|
+
set muted(muted) {
|
|
518
|
+
this._muted = muted;
|
|
519
|
+
this.refresh();
|
|
520
|
+
}
|
|
521
|
+
get loop() {
|
|
522
|
+
return this._loop;
|
|
523
|
+
}
|
|
524
|
+
set loop(loop) {
|
|
525
|
+
this._loop = loop;
|
|
526
|
+
this.refresh();
|
|
527
|
+
}
|
|
528
|
+
get filters() {
|
|
529
|
+
return this._filters;
|
|
530
|
+
}
|
|
531
|
+
set filters(filters) {
|
|
532
|
+
if (this._filters) {
|
|
533
|
+
var _this$_filters;
|
|
534
|
+
(_this$_filters = this._filters) === null || _this$_filters === void 0 ? void 0 : _this$_filters.filter(filter => filter).forEach(filter => filter.disconnect());
|
|
535
|
+
this._filters = null;
|
|
536
|
+
this._source.connect(this._gain);
|
|
537
|
+
}
|
|
538
|
+
this._filters = filters !== null && filters !== void 0 && filters.length ? filters.slice(0) : null;
|
|
539
|
+
this.refresh();
|
|
540
|
+
}
|
|
541
|
+
refresh() {
|
|
542
|
+
if (!this._source) {
|
|
64
543
|
return;
|
|
65
544
|
}
|
|
66
|
-
this.
|
|
545
|
+
const global = this._media.context;
|
|
546
|
+
const sound = this._media.parent;
|
|
547
|
+
this._source.loop = this._loop || sound.loop;
|
|
548
|
+
const globalVolume = global.volume * (global.muted ? 0 : 1);
|
|
549
|
+
const soundVolume = sound.volume * (sound.muted ? 0 : 1);
|
|
550
|
+
const instanceVolume = this._volume * (this._muted ? 0 : 1);
|
|
551
|
+
WebAudioUtils.setParamValue(this._gain.gain, instanceVolume * soundVolume * globalVolume);
|
|
552
|
+
WebAudioUtils.setParamValue(this._source.playbackRate, this._speed * sound.speed * global.speed);
|
|
553
|
+
this.applyFilters();
|
|
554
|
+
}
|
|
555
|
+
applyFilters() {
|
|
556
|
+
var _this$_filters2;
|
|
557
|
+
if ((_this$_filters2 = this._filters) !== null && _this$_filters2 !== void 0 && _this$_filters2.length) {
|
|
558
|
+
this._source.disconnect();
|
|
559
|
+
let source = this._source;
|
|
560
|
+
this._filters.forEach(filter => {
|
|
561
|
+
source.connect(filter.destination);
|
|
562
|
+
source = filter;
|
|
563
|
+
});
|
|
564
|
+
source.connect(this._gain);
|
|
565
|
+
}
|
|
67
566
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
567
|
+
refreshPaused() {
|
|
568
|
+
const global = this._media.context;
|
|
569
|
+
const sound = this._media.parent;
|
|
570
|
+
const pausedReal = this._paused || sound.paused || global.paused;
|
|
571
|
+
if (pausedReal !== this._pausedReal) {
|
|
572
|
+
this._pausedReal = pausedReal;
|
|
573
|
+
if (pausedReal) {
|
|
574
|
+
this._internalStop();
|
|
575
|
+
this.emit("paused");
|
|
576
|
+
} else {
|
|
577
|
+
this.emit("resumed");
|
|
578
|
+
this.play({
|
|
579
|
+
start: this._elapsed % this._duration,
|
|
580
|
+
end: this._end,
|
|
581
|
+
speed: this._speed,
|
|
582
|
+
loop: this._loop,
|
|
583
|
+
volume: this._volume
|
|
584
|
+
});
|
|
585
|
+
}
|
|
586
|
+
this.emit("pause", pausedReal);
|
|
71
587
|
}
|
|
72
|
-
return this.ctx.state !== 'running';
|
|
73
588
|
}
|
|
74
|
-
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
589
|
+
play(options) {
|
|
590
|
+
const {
|
|
591
|
+
start,
|
|
592
|
+
end,
|
|
593
|
+
speed,
|
|
594
|
+
loop,
|
|
595
|
+
volume,
|
|
596
|
+
muted,
|
|
597
|
+
filters
|
|
598
|
+
} = options;
|
|
599
|
+
if (end) {
|
|
600
|
+
console.assert(end > start, "End time is before start time");
|
|
601
|
+
}
|
|
602
|
+
this._paused = false;
|
|
603
|
+
const {
|
|
604
|
+
source,
|
|
605
|
+
gain
|
|
606
|
+
} = this._media.nodes.cloneBufferSource();
|
|
607
|
+
this._source = source;
|
|
608
|
+
this._gain = gain;
|
|
609
|
+
this._speed = speed;
|
|
610
|
+
this._volume = volume;
|
|
611
|
+
this._loop = !!loop;
|
|
612
|
+
this._muted = muted;
|
|
613
|
+
this._filters = filters;
|
|
614
|
+
this.refresh();
|
|
615
|
+
const duration = this._source.buffer.duration;
|
|
616
|
+
this._duration = duration;
|
|
617
|
+
this._end = end;
|
|
618
|
+
this._lastUpdate = this._now();
|
|
619
|
+
this._elapsed = start;
|
|
620
|
+
this._source.onended = this._onComplete.bind(this);
|
|
621
|
+
if (this._loop) {
|
|
622
|
+
this._source.loopEnd = end;
|
|
623
|
+
this._source.loopStart = start;
|
|
624
|
+
this._source.start(0, start);
|
|
625
|
+
} else if (end) {
|
|
626
|
+
this._source.start(0, start, end - start);
|
|
627
|
+
} else {
|
|
628
|
+
this._source.start(0, start);
|
|
629
|
+
}
|
|
630
|
+
this.emit("start");
|
|
631
|
+
this._update(true);
|
|
632
|
+
this.enableTicker(true);
|
|
633
|
+
}
|
|
634
|
+
enableTicker(enabled) {
|
|
635
|
+
pixi_js.Ticker.shared.remove(this._updateListener, this);
|
|
636
|
+
if (enabled) {
|
|
637
|
+
pixi_js.Ticker.shared.add(this._updateListener, this);
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
get progress() {
|
|
641
|
+
return this._progress;
|
|
642
|
+
}
|
|
643
|
+
get paused() {
|
|
644
|
+
return this._paused;
|
|
645
|
+
}
|
|
646
|
+
set paused(paused) {
|
|
647
|
+
this._paused = paused;
|
|
648
|
+
this.refreshPaused();
|
|
649
|
+
}
|
|
650
|
+
destroy() {
|
|
651
|
+
var _this$_filters3;
|
|
652
|
+
this.removeAllListeners();
|
|
653
|
+
this._internalStop();
|
|
654
|
+
if (this._gain) {
|
|
655
|
+
this._gain.disconnect();
|
|
656
|
+
this._gain = null;
|
|
657
|
+
}
|
|
658
|
+
if (this._media) {
|
|
659
|
+
this._media.context.events.off("refresh", this.refresh, this);
|
|
660
|
+
this._media.context.events.off("refreshPaused", this.refreshPaused, this);
|
|
661
|
+
this._media = null;
|
|
662
|
+
}
|
|
663
|
+
(_this$_filters3 = this._filters) === null || _this$_filters3 === void 0 ? void 0 : _this$_filters3.forEach(filter => filter.disconnect());
|
|
664
|
+
this._filters = null;
|
|
665
|
+
this._end = null;
|
|
666
|
+
this._speed = 1;
|
|
667
|
+
this._volume = 1;
|
|
668
|
+
this._loop = false;
|
|
669
|
+
this._elapsed = 0;
|
|
670
|
+
this._duration = 0;
|
|
671
|
+
this._paused = false;
|
|
672
|
+
this._muted = false;
|
|
673
|
+
this._pausedReal = false;
|
|
674
|
+
}
|
|
675
|
+
toString() {
|
|
676
|
+
return `[WebAudioInstance id=${this.id}]`;
|
|
677
|
+
}
|
|
678
|
+
_now() {
|
|
679
|
+
return this._media.context.audioContext.currentTime;
|
|
680
|
+
}
|
|
681
|
+
_updateListener() {
|
|
682
|
+
this._update();
|
|
683
|
+
}
|
|
684
|
+
_update(force = false) {
|
|
685
|
+
if (this._source) {
|
|
686
|
+
const now = this._now();
|
|
687
|
+
const delta = now - this._lastUpdate;
|
|
688
|
+
if (delta > 0 || force) {
|
|
689
|
+
const speed = this._source.playbackRate.value;
|
|
690
|
+
this._elapsed += delta * speed;
|
|
691
|
+
this._lastUpdate = now;
|
|
692
|
+
const duration = this._duration;
|
|
693
|
+
let progress;
|
|
694
|
+
if (this._source.loopStart) {
|
|
695
|
+
const soundLength = this._source.loopEnd - this._source.loopStart;
|
|
696
|
+
progress = (this._source.loopStart + this._elapsed % soundLength) / duration;
|
|
697
|
+
} else {
|
|
698
|
+
progress = this._elapsed % duration / duration;
|
|
699
|
+
}
|
|
700
|
+
this._progress = progress;
|
|
701
|
+
this.emit("progress", this._progress, duration);
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
init(media) {
|
|
706
|
+
this._media = media;
|
|
707
|
+
media.context.events.on("refresh", this.refresh, this);
|
|
708
|
+
media.context.events.on("refreshPaused", this.refreshPaused, this);
|
|
709
|
+
}
|
|
710
|
+
_internalStop() {
|
|
711
|
+
if (this._source) {
|
|
712
|
+
this.enableTicker(false);
|
|
713
|
+
this._source.onended = null;
|
|
714
|
+
this._source.stop(0);
|
|
715
|
+
this._source.disconnect();
|
|
716
|
+
try {
|
|
717
|
+
this._source.buffer = null;
|
|
718
|
+
} catch (err) {
|
|
719
|
+
console.warn("Failed to set AudioBufferSourceNode.buffer to null:", err);
|
|
720
|
+
}
|
|
721
|
+
this._source = null;
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
_onComplete() {
|
|
725
|
+
if (this._source) {
|
|
726
|
+
this.enableTicker(false);
|
|
727
|
+
this._source.onended = null;
|
|
728
|
+
this._source.disconnect();
|
|
729
|
+
try {
|
|
730
|
+
this._source.buffer = null;
|
|
731
|
+
} catch (err) {
|
|
732
|
+
console.warn("Failed to set AudioBufferSourceNode.buffer to null:", err);
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
this._source = null;
|
|
736
|
+
this._progress = 1;
|
|
737
|
+
this.emit("progress", 1, this._duration);
|
|
738
|
+
this.emit("end", this);
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
class Filterable {
|
|
742
|
+
constructor(input, output) {
|
|
743
|
+
this._output = output;
|
|
744
|
+
this._input = input;
|
|
745
|
+
}
|
|
746
|
+
get destination() {
|
|
747
|
+
return this._input;
|
|
748
|
+
}
|
|
749
|
+
get filters() {
|
|
750
|
+
return this._filters;
|
|
751
|
+
}
|
|
752
|
+
set filters(filters) {
|
|
753
|
+
if (this._filters) {
|
|
754
|
+
this._filters.forEach(filter => {
|
|
755
|
+
if (filter) {
|
|
756
|
+
filter.disconnect();
|
|
757
|
+
}
|
|
758
|
+
});
|
|
759
|
+
this._filters = null;
|
|
760
|
+
this._input.connect(this._output);
|
|
761
|
+
}
|
|
762
|
+
if (filters && filters.length) {
|
|
763
|
+
this._filters = filters.slice(0);
|
|
764
|
+
this._input.disconnect();
|
|
765
|
+
let prevFilter = null;
|
|
766
|
+
filters.forEach(filter => {
|
|
767
|
+
if (prevFilter === null) {
|
|
768
|
+
this._input.connect(filter.destination);
|
|
769
|
+
} else {
|
|
770
|
+
prevFilter.connect(filter.destination);
|
|
771
|
+
}
|
|
772
|
+
prevFilter = filter;
|
|
78
773
|
});
|
|
79
|
-
this.
|
|
774
|
+
prevFilter.connect(this._output);
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
destroy() {
|
|
778
|
+
this.filters = null;
|
|
779
|
+
this._input = null;
|
|
780
|
+
this._output = null;
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
const _WebAudioNodes = class extends Filterable {
|
|
784
|
+
constructor(context) {
|
|
785
|
+
const audioContext = context.audioContext;
|
|
786
|
+
const bufferSource = audioContext.createBufferSource();
|
|
787
|
+
const gain = audioContext.createGain();
|
|
788
|
+
const analyser = audioContext.createAnalyser();
|
|
789
|
+
bufferSource.connect(analyser);
|
|
790
|
+
analyser.connect(gain);
|
|
791
|
+
gain.connect(context.destination);
|
|
792
|
+
super(analyser, gain);
|
|
793
|
+
this.context = context;
|
|
794
|
+
this.bufferSource = bufferSource;
|
|
795
|
+
this.gain = gain;
|
|
796
|
+
this.analyser = analyser;
|
|
797
|
+
}
|
|
798
|
+
get script() {
|
|
799
|
+
if (!this._script) {
|
|
800
|
+
this._script = this.context.audioContext.createScriptProcessor(_WebAudioNodes.BUFFER_SIZE);
|
|
801
|
+
this._script.connect(this.context.destination);
|
|
802
|
+
}
|
|
803
|
+
return this._script;
|
|
804
|
+
}
|
|
805
|
+
destroy() {
|
|
806
|
+
super.destroy();
|
|
807
|
+
this.bufferSource.disconnect();
|
|
808
|
+
if (this._script) {
|
|
809
|
+
this._script.disconnect();
|
|
810
|
+
}
|
|
811
|
+
this.gain.disconnect();
|
|
812
|
+
this.analyser.disconnect();
|
|
813
|
+
this.bufferSource = null;
|
|
814
|
+
this._script = null;
|
|
815
|
+
this.gain = null;
|
|
816
|
+
this.analyser = null;
|
|
817
|
+
this.context = null;
|
|
818
|
+
}
|
|
819
|
+
cloneBufferSource() {
|
|
820
|
+
const orig = this.bufferSource;
|
|
821
|
+
const source = this.context.audioContext.createBufferSource();
|
|
822
|
+
source.buffer = orig.buffer;
|
|
823
|
+
WebAudioUtils.setParamValue(source.playbackRate, orig.playbackRate.value);
|
|
824
|
+
source.loop = orig.loop;
|
|
825
|
+
const gain = this.context.audioContext.createGain();
|
|
826
|
+
source.connect(gain);
|
|
827
|
+
gain.connect(this.destination);
|
|
828
|
+
return {
|
|
829
|
+
source,
|
|
830
|
+
gain
|
|
80
831
|
};
|
|
81
|
-
this.ctx.resume().then(handleResume, handleResume);
|
|
82
832
|
}
|
|
83
|
-
|
|
84
|
-
this.
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
833
|
+
get bufferSize() {
|
|
834
|
+
return this.script.bufferSize;
|
|
835
|
+
}
|
|
836
|
+
};
|
|
837
|
+
let WebAudioNodes = _WebAudioNodes;
|
|
838
|
+
WebAudioNodes.BUFFER_SIZE = 0;
|
|
839
|
+
class WebAudioMedia {
|
|
840
|
+
init(parent) {
|
|
841
|
+
this.parent = parent;
|
|
842
|
+
this._nodes = new WebAudioNodes(this.context);
|
|
843
|
+
this._source = this._nodes.bufferSource;
|
|
844
|
+
this.source = parent.options.source;
|
|
845
|
+
}
|
|
846
|
+
destroy() {
|
|
847
|
+
this.parent = null;
|
|
848
|
+
this._nodes.destroy();
|
|
849
|
+
this._nodes = null;
|
|
850
|
+
try {
|
|
851
|
+
this._source.buffer = null;
|
|
852
|
+
} catch (err) {
|
|
853
|
+
console.warn("Failed to set AudioBufferSourceNode.buffer to null:", err);
|
|
854
|
+
}
|
|
855
|
+
this._source = null;
|
|
856
|
+
this.source = null;
|
|
857
|
+
}
|
|
858
|
+
create() {
|
|
859
|
+
return new WebAudioInstance(this);
|
|
860
|
+
}
|
|
861
|
+
get context() {
|
|
862
|
+
return this.parent.context;
|
|
863
|
+
}
|
|
864
|
+
get isPlayable() {
|
|
865
|
+
return !!this._source && !!this._source.buffer;
|
|
866
|
+
}
|
|
867
|
+
get filters() {
|
|
868
|
+
return this._nodes.filters;
|
|
869
|
+
}
|
|
870
|
+
set filters(filters) {
|
|
871
|
+
this._nodes.filters = filters;
|
|
872
|
+
}
|
|
873
|
+
get duration() {
|
|
874
|
+
console.assert(this.isPlayable, "Sound not yet playable, no duration");
|
|
875
|
+
return this._source.buffer.duration;
|
|
876
|
+
}
|
|
877
|
+
get buffer() {
|
|
878
|
+
return this._source.buffer;
|
|
879
|
+
}
|
|
880
|
+
set buffer(buffer) {
|
|
881
|
+
this._source.buffer = buffer;
|
|
882
|
+
}
|
|
883
|
+
get nodes() {
|
|
884
|
+
return this._nodes;
|
|
885
|
+
}
|
|
886
|
+
load(callback) {
|
|
887
|
+
if (this.source) {
|
|
888
|
+
this._decode(this.source, callback);
|
|
889
|
+
} else if (this.parent.url) {
|
|
890
|
+
this._loadUrl(callback);
|
|
891
|
+
} else if (callback) {
|
|
892
|
+
callback(new Error("sound.url or sound.source must be set"));
|
|
893
|
+
} else {
|
|
894
|
+
console.error("sound.url or sound.source must be set");
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
async _loadUrl(callback) {
|
|
898
|
+
const url = this.parent.url;
|
|
899
|
+
const response = await pixi_js.DOMAdapter.get().fetch(url);
|
|
900
|
+
this._decode(await response.arrayBuffer(), callback);
|
|
901
|
+
}
|
|
902
|
+
_decode(arrayBuffer, callback) {
|
|
903
|
+
const audioBufferReadyFn = (err, buffer) => {
|
|
904
|
+
if (err) {
|
|
905
|
+
if (callback) {
|
|
906
|
+
callback(err);
|
|
907
|
+
}
|
|
908
|
+
} else {
|
|
909
|
+
this.parent.isLoaded = true;
|
|
910
|
+
this.buffer = buffer;
|
|
911
|
+
const instance = this.parent.autoPlayStart();
|
|
912
|
+
if (callback) {
|
|
913
|
+
callback(null, this.parent, instance);
|
|
914
|
+
}
|
|
88
915
|
}
|
|
89
|
-
}
|
|
90
|
-
|
|
916
|
+
};
|
|
917
|
+
if (arrayBuffer instanceof AudioBuffer) {
|
|
918
|
+
audioBufferReadyFn(null, arrayBuffer);
|
|
919
|
+
} else {
|
|
920
|
+
const context = this.parent.context;
|
|
921
|
+
context.decode(arrayBuffer, audioBufferReadyFn);
|
|
922
|
+
}
|
|
91
923
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
924
|
+
}
|
|
925
|
+
const _Sound = class {
|
|
926
|
+
static from(source) {
|
|
927
|
+
let options = {};
|
|
928
|
+
if (typeof source === "string") {
|
|
929
|
+
options.url = source;
|
|
930
|
+
} else if (source instanceof ArrayBuffer || source instanceof AudioBuffer || source instanceof HTMLAudioElement) {
|
|
931
|
+
options.source = source;
|
|
932
|
+
} else if (Array.isArray(source)) {
|
|
933
|
+
options.url = source;
|
|
934
|
+
} else {
|
|
935
|
+
options = source;
|
|
936
|
+
}
|
|
937
|
+
options = _objectSpread({
|
|
938
|
+
autoPlay: false,
|
|
939
|
+
singleInstance: false,
|
|
940
|
+
url: null,
|
|
941
|
+
source: null,
|
|
942
|
+
preload: false,
|
|
943
|
+
volume: 1,
|
|
944
|
+
speed: 1,
|
|
945
|
+
complete: null,
|
|
946
|
+
loaded: null,
|
|
947
|
+
loop: false
|
|
948
|
+
}, options);
|
|
949
|
+
Object.freeze(options);
|
|
950
|
+
const media = getInstance().useLegacy ? new HTMLAudioMedia() : new WebAudioMedia();
|
|
951
|
+
return new _Sound(media, options);
|
|
952
|
+
}
|
|
953
|
+
constructor(media, options) {
|
|
954
|
+
this.media = media;
|
|
955
|
+
this.options = options;
|
|
956
|
+
this._instances = [];
|
|
957
|
+
this._sprites = {};
|
|
958
|
+
this.media.init(this);
|
|
959
|
+
const complete = options.complete;
|
|
960
|
+
this._autoPlayOptions = complete ? {
|
|
961
|
+
complete
|
|
962
|
+
} : null;
|
|
963
|
+
this.isLoaded = false;
|
|
964
|
+
this._preloadQueue = null;
|
|
965
|
+
this.isPlaying = false;
|
|
966
|
+
this.autoPlay = options.autoPlay;
|
|
967
|
+
this.singleInstance = options.singleInstance;
|
|
968
|
+
this.preload = options.preload || this.autoPlay;
|
|
969
|
+
this.url = Array.isArray(options.url) ? this.preferUrl(options.url) : options.url;
|
|
970
|
+
this.speed = options.speed;
|
|
971
|
+
this.volume = options.volume;
|
|
972
|
+
this.loop = options.loop;
|
|
973
|
+
if (options.sprites) {
|
|
974
|
+
this.addSprites(options.sprites);
|
|
975
|
+
}
|
|
976
|
+
if (this.preload) {
|
|
977
|
+
this._preload(options.loaded);
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
preferUrl(urls) {
|
|
981
|
+
const [file] = urls.map(url => ({
|
|
982
|
+
url,
|
|
983
|
+
ext: pixi_js.path.extname(url).slice(1)
|
|
984
|
+
})).filter(({
|
|
985
|
+
ext
|
|
986
|
+
}) => supported[ext]).sort((a, b) => extensions.indexOf(a.ext) - extensions.indexOf(b.ext));
|
|
987
|
+
if (!file) {
|
|
988
|
+
throw new Error("No supported file type found");
|
|
989
|
+
}
|
|
990
|
+
return file.url;
|
|
991
|
+
}
|
|
992
|
+
get context() {
|
|
993
|
+
return getInstance().context;
|
|
994
|
+
}
|
|
995
|
+
pause() {
|
|
996
|
+
this.isPlaying = false;
|
|
997
|
+
this.paused = true;
|
|
998
|
+
return this;
|
|
999
|
+
}
|
|
1000
|
+
resume() {
|
|
1001
|
+
this.isPlaying = this._instances.length > 0;
|
|
1002
|
+
this.paused = false;
|
|
1003
|
+
return this;
|
|
1004
|
+
}
|
|
1005
|
+
get paused() {
|
|
1006
|
+
return this._paused;
|
|
1007
|
+
}
|
|
1008
|
+
set paused(paused) {
|
|
1009
|
+
this._paused = paused;
|
|
1010
|
+
this.refreshPaused();
|
|
1011
|
+
}
|
|
1012
|
+
get speed() {
|
|
1013
|
+
return this._speed;
|
|
1014
|
+
}
|
|
1015
|
+
set speed(speed) {
|
|
1016
|
+
this._speed = speed;
|
|
1017
|
+
this.refresh();
|
|
1018
|
+
}
|
|
1019
|
+
get filters() {
|
|
1020
|
+
return this.media.filters;
|
|
1021
|
+
}
|
|
1022
|
+
set filters(filters) {
|
|
1023
|
+
this.media.filters = filters;
|
|
1024
|
+
}
|
|
1025
|
+
addSprites(source, data) {
|
|
1026
|
+
if (typeof source === "object") {
|
|
1027
|
+
const results = {};
|
|
1028
|
+
for (const alias in source) {
|
|
1029
|
+
results[alias] = this.addSprites(alias, source[alias]);
|
|
1030
|
+
}
|
|
1031
|
+
return results;
|
|
1032
|
+
}
|
|
1033
|
+
console.assert(!this._sprites[source], `Alias ${source} is already taken`);
|
|
1034
|
+
const sprite = new SoundSprite(this, data);
|
|
1035
|
+
this._sprites[source] = sprite;
|
|
1036
|
+
return sprite;
|
|
1037
|
+
}
|
|
1038
|
+
destroy() {
|
|
1039
|
+
this._removeInstances();
|
|
1040
|
+
this.removeSprites();
|
|
1041
|
+
this.media.destroy();
|
|
1042
|
+
this.media = null;
|
|
1043
|
+
this._sprites = null;
|
|
1044
|
+
this._instances = null;
|
|
1045
|
+
}
|
|
1046
|
+
removeSprites(alias) {
|
|
1047
|
+
if (!alias) {
|
|
1048
|
+
for (const name in this._sprites) {
|
|
1049
|
+
this.removeSprites(name);
|
|
96
1050
|
}
|
|
1051
|
+
} else {
|
|
1052
|
+
const sprite = this._sprites[alias];
|
|
1053
|
+
if (sprite !== void 0) {
|
|
1054
|
+
sprite.destroy();
|
|
1055
|
+
delete this._sprites[alias];
|
|
1056
|
+
}
|
|
1057
|
+
}
|
|
1058
|
+
return this;
|
|
1059
|
+
}
|
|
1060
|
+
get isPlayable() {
|
|
1061
|
+
return this.isLoaded && this.media && this.media.isPlayable;
|
|
1062
|
+
}
|
|
1063
|
+
stop() {
|
|
1064
|
+
if (!this.isPlayable) {
|
|
1065
|
+
this.autoPlay = false;
|
|
1066
|
+
this._autoPlayOptions = null;
|
|
1067
|
+
return this;
|
|
1068
|
+
}
|
|
1069
|
+
this.isPlaying = false;
|
|
1070
|
+
for (let i = this._instances.length - 1; i >= 0; i--) {
|
|
1071
|
+
this._instances[i].stop();
|
|
1072
|
+
}
|
|
1073
|
+
return this;
|
|
1074
|
+
}
|
|
1075
|
+
play(source, complete) {
|
|
1076
|
+
let options;
|
|
1077
|
+
if (typeof source === "string") {
|
|
1078
|
+
const sprite = source;
|
|
1079
|
+
options = {
|
|
1080
|
+
sprite,
|
|
1081
|
+
loop: this.loop,
|
|
1082
|
+
complete
|
|
1083
|
+
};
|
|
1084
|
+
} else if (typeof source === "function") {
|
|
1085
|
+
options = {};
|
|
1086
|
+
options.complete = source;
|
|
1087
|
+
} else {
|
|
1088
|
+
options = source;
|
|
1089
|
+
}
|
|
1090
|
+
options = _objectSpread({
|
|
1091
|
+
complete: null,
|
|
1092
|
+
loaded: null,
|
|
1093
|
+
sprite: null,
|
|
1094
|
+
end: null,
|
|
1095
|
+
start: 0,
|
|
1096
|
+
volume: 1,
|
|
1097
|
+
speed: 1,
|
|
1098
|
+
muted: false,
|
|
1099
|
+
loop: false
|
|
1100
|
+
}, options || {});
|
|
1101
|
+
if (options.sprite) {
|
|
1102
|
+
const alias = options.sprite;
|
|
1103
|
+
console.assert(!!this._sprites[alias], `Alias ${alias} is not available`);
|
|
1104
|
+
const sprite = this._sprites[alias];
|
|
1105
|
+
options.start = sprite.start + (options.start || 0);
|
|
1106
|
+
options.end = sprite.end;
|
|
1107
|
+
options.speed = sprite.speed || 1;
|
|
1108
|
+
options.loop = sprite.loop || options.loop;
|
|
1109
|
+
delete options.sprite;
|
|
1110
|
+
}
|
|
1111
|
+
if (options.offset) {
|
|
1112
|
+
options.start = options.offset;
|
|
1113
|
+
}
|
|
1114
|
+
if (!this.isLoaded) {
|
|
1115
|
+
if (this._preloadQueue) {
|
|
1116
|
+
return new Promise(resolve => {
|
|
1117
|
+
this._preloadQueue.push(() => {
|
|
1118
|
+
resolve(this.play(options));
|
|
1119
|
+
});
|
|
1120
|
+
});
|
|
1121
|
+
}
|
|
1122
|
+
this._preloadQueue = [];
|
|
1123
|
+
this.autoPlay = true;
|
|
1124
|
+
this._autoPlayOptions = options;
|
|
1125
|
+
return new Promise((resolve, reject) => {
|
|
1126
|
+
this._preload((err, sound, media) => {
|
|
1127
|
+
this._preloadQueue.forEach(resolve2 => resolve2());
|
|
1128
|
+
this._preloadQueue = null;
|
|
1129
|
+
if (err) {
|
|
1130
|
+
reject(err);
|
|
1131
|
+
} else {
|
|
1132
|
+
if (options.loaded) {
|
|
1133
|
+
options.loaded(err, sound, media);
|
|
1134
|
+
}
|
|
1135
|
+
resolve(media);
|
|
1136
|
+
}
|
|
1137
|
+
});
|
|
1138
|
+
});
|
|
1139
|
+
}
|
|
1140
|
+
if (this.singleInstance || options.singleInstance) {
|
|
1141
|
+
this._removeInstances();
|
|
1142
|
+
}
|
|
1143
|
+
const instance = this._createInstance();
|
|
1144
|
+
this._instances.push(instance);
|
|
1145
|
+
this.isPlaying = true;
|
|
1146
|
+
instance.once("end", () => {
|
|
1147
|
+
if (options.complete) {
|
|
1148
|
+
options.complete(this);
|
|
1149
|
+
}
|
|
1150
|
+
this._onComplete(instance);
|
|
97
1151
|
});
|
|
98
|
-
|
|
99
|
-
|
|
1152
|
+
instance.once("stop", () => {
|
|
1153
|
+
this._onComplete(instance);
|
|
1154
|
+
});
|
|
1155
|
+
instance.play(options);
|
|
1156
|
+
return instance;
|
|
1157
|
+
}
|
|
1158
|
+
refresh() {
|
|
1159
|
+
const len = this._instances.length;
|
|
1160
|
+
for (let i = 0; i < len; i++) {
|
|
1161
|
+
this._instances[i].refresh();
|
|
1162
|
+
}
|
|
1163
|
+
}
|
|
1164
|
+
refreshPaused() {
|
|
1165
|
+
const len = this._instances.length;
|
|
1166
|
+
for (let i = 0; i < len; i++) {
|
|
1167
|
+
this._instances[i].refreshPaused();
|
|
1168
|
+
}
|
|
1169
|
+
}
|
|
1170
|
+
get volume() {
|
|
1171
|
+
return this._volume;
|
|
1172
|
+
}
|
|
1173
|
+
set volume(volume) {
|
|
1174
|
+
this._volume = volume;
|
|
1175
|
+
this.refresh();
|
|
1176
|
+
}
|
|
1177
|
+
get muted() {
|
|
1178
|
+
return this._muted;
|
|
1179
|
+
}
|
|
1180
|
+
set muted(muted) {
|
|
1181
|
+
this._muted = muted;
|
|
1182
|
+
this.refresh();
|
|
1183
|
+
}
|
|
1184
|
+
get loop() {
|
|
1185
|
+
return this._loop;
|
|
1186
|
+
}
|
|
1187
|
+
set loop(loop) {
|
|
1188
|
+
this._loop = loop;
|
|
1189
|
+
this.refresh();
|
|
1190
|
+
}
|
|
1191
|
+
_preload(callback) {
|
|
1192
|
+
this.media.load(callback);
|
|
1193
|
+
}
|
|
1194
|
+
get instances() {
|
|
1195
|
+
return this._instances;
|
|
1196
|
+
}
|
|
1197
|
+
get sprites() {
|
|
1198
|
+
return this._sprites;
|
|
1199
|
+
}
|
|
1200
|
+
get duration() {
|
|
1201
|
+
return this.media.duration;
|
|
1202
|
+
}
|
|
1203
|
+
autoPlayStart() {
|
|
1204
|
+
let instance;
|
|
1205
|
+
if (this.autoPlay) {
|
|
1206
|
+
instance = this.play(this._autoPlayOptions);
|
|
1207
|
+
}
|
|
1208
|
+
return instance;
|
|
1209
|
+
}
|
|
1210
|
+
_removeInstances() {
|
|
1211
|
+
for (let i = this._instances.length - 1; i >= 0; i--) {
|
|
1212
|
+
this._poolInstance(this._instances[i]);
|
|
1213
|
+
}
|
|
1214
|
+
this._instances.length = 0;
|
|
1215
|
+
}
|
|
1216
|
+
_onComplete(instance) {
|
|
1217
|
+
if (this._instances) {
|
|
1218
|
+
const index = this._instances.indexOf(instance);
|
|
1219
|
+
if (index > -1) {
|
|
1220
|
+
this._instances.splice(index, 1);
|
|
1221
|
+
}
|
|
1222
|
+
this.isPlaying = this._instances.length > 0;
|
|
1223
|
+
}
|
|
1224
|
+
this._poolInstance(instance);
|
|
1225
|
+
}
|
|
1226
|
+
_createInstance() {
|
|
1227
|
+
if (_Sound._pool.length > 0) {
|
|
1228
|
+
const instance = _Sound._pool.pop();
|
|
1229
|
+
instance.init(this.media);
|
|
1230
|
+
return instance;
|
|
1231
|
+
}
|
|
1232
|
+
return this.media.create();
|
|
1233
|
+
}
|
|
1234
|
+
_poolInstance(instance) {
|
|
1235
|
+
instance.destroy();
|
|
1236
|
+
if (_Sound._pool.indexOf(instance) < 0) {
|
|
1237
|
+
_Sound._pool.push(instance);
|
|
1238
|
+
}
|
|
1239
|
+
}
|
|
1240
|
+
};
|
|
1241
|
+
let Sound$2 = _Sound;
|
|
1242
|
+
Sound$2._pool = [];
|
|
1243
|
+
class WebAudioContext extends Filterable {
|
|
1244
|
+
constructor() {
|
|
1245
|
+
const win = window;
|
|
1246
|
+
const ctx = new WebAudioContext.AudioContext();
|
|
1247
|
+
const compressor = ctx.createDynamicsCompressor();
|
|
1248
|
+
const analyser = ctx.createAnalyser();
|
|
1249
|
+
analyser.connect(compressor);
|
|
1250
|
+
compressor.connect(ctx.destination);
|
|
1251
|
+
super(analyser, compressor);
|
|
1252
|
+
this.autoPause = true;
|
|
1253
|
+
this._ctx = ctx;
|
|
1254
|
+
this._offlineCtx = new WebAudioContext.OfflineAudioContext(1, 2, win.OfflineAudioContext ? Math.max(8e3, Math.min(96e3, ctx.sampleRate)) : 44100);
|
|
1255
|
+
this.compressor = compressor;
|
|
1256
|
+
this.analyser = analyser;
|
|
1257
|
+
this.events = new pixi_js.EventEmitter();
|
|
1258
|
+
this.volume = 1;
|
|
1259
|
+
this.speed = 1;
|
|
1260
|
+
this.muted = false;
|
|
1261
|
+
this.paused = false;
|
|
1262
|
+
this._locked = ctx.state === "suspended" && ("ontouchstart" in globalThis || "onclick" in globalThis);
|
|
1263
|
+
if (this._locked) {
|
|
1264
|
+
this._unlock();
|
|
1265
|
+
this._unlock = this._unlock.bind(this);
|
|
1266
|
+
document.addEventListener("mousedown", this._unlock, true);
|
|
1267
|
+
document.addEventListener("touchstart", this._unlock, true);
|
|
1268
|
+
document.addEventListener("touchend", this._unlock, true);
|
|
1269
|
+
}
|
|
1270
|
+
this.onFocus = this.onFocus.bind(this);
|
|
1271
|
+
this.onBlur = this.onBlur.bind(this);
|
|
1272
|
+
globalThis.addEventListener("focus", this.onFocus);
|
|
1273
|
+
globalThis.addEventListener("blur", this.onBlur);
|
|
1274
|
+
}
|
|
1275
|
+
onFocus() {
|
|
1276
|
+
if (!this.autoPause) {
|
|
1277
|
+
return;
|
|
1278
|
+
}
|
|
1279
|
+
const state = this._ctx.state;
|
|
1280
|
+
if (state === "suspended" || state === "interrupted" || !this._locked) {
|
|
1281
|
+
this.paused = this._pausedOnBlur;
|
|
1282
|
+
this.refreshPaused();
|
|
1283
|
+
}
|
|
1284
|
+
}
|
|
1285
|
+
onBlur() {
|
|
1286
|
+
if (!this.autoPause) {
|
|
1287
|
+
return;
|
|
1288
|
+
}
|
|
1289
|
+
if (!this._locked) {
|
|
1290
|
+
this._pausedOnBlur = this._paused;
|
|
1291
|
+
this.paused = true;
|
|
1292
|
+
this.refreshPaused();
|
|
1293
|
+
}
|
|
1294
|
+
}
|
|
1295
|
+
_unlock() {
|
|
1296
|
+
if (!this._locked) {
|
|
1297
|
+
return;
|
|
1298
|
+
}
|
|
1299
|
+
this.playEmptySound();
|
|
1300
|
+
if (this._ctx.state === "running") {
|
|
1301
|
+
document.removeEventListener("mousedown", this._unlock, true);
|
|
1302
|
+
document.removeEventListener("touchend", this._unlock, true);
|
|
1303
|
+
document.removeEventListener("touchstart", this._unlock, true);
|
|
1304
|
+
this._locked = false;
|
|
1305
|
+
}
|
|
1306
|
+
}
|
|
1307
|
+
playEmptySound() {
|
|
1308
|
+
const source = this._ctx.createBufferSource();
|
|
1309
|
+
source.buffer = this._ctx.createBuffer(1, 1, 22050);
|
|
1310
|
+
source.connect(this._ctx.destination);
|
|
1311
|
+
source.start(0, 0, 0);
|
|
1312
|
+
if (source.context.state === "suspended") {
|
|
1313
|
+
source.context.resume();
|
|
1314
|
+
}
|
|
1315
|
+
}
|
|
1316
|
+
static get AudioContext() {
|
|
1317
|
+
const win = window;
|
|
1318
|
+
return win.AudioContext || win.webkitAudioContext || null;
|
|
1319
|
+
}
|
|
1320
|
+
static get OfflineAudioContext() {
|
|
1321
|
+
const win = window;
|
|
1322
|
+
return win.OfflineAudioContext || win.webkitOfflineAudioContext || null;
|
|
1323
|
+
}
|
|
1324
|
+
destroy() {
|
|
1325
|
+
super.destroy();
|
|
1326
|
+
const ctx = this._ctx;
|
|
1327
|
+
if (typeof ctx.close !== "undefined") {
|
|
1328
|
+
ctx.close();
|
|
1329
|
+
}
|
|
1330
|
+
globalThis.removeEventListener("focus", this.onFocus);
|
|
1331
|
+
globalThis.removeEventListener("blur", this.onBlur);
|
|
1332
|
+
this.events.removeAllListeners();
|
|
1333
|
+
this.analyser.disconnect();
|
|
1334
|
+
this.compressor.disconnect();
|
|
1335
|
+
this.analyser = null;
|
|
1336
|
+
this.compressor = null;
|
|
1337
|
+
this.events = null;
|
|
1338
|
+
this._offlineCtx = null;
|
|
1339
|
+
this._ctx = null;
|
|
1340
|
+
}
|
|
1341
|
+
get audioContext() {
|
|
1342
|
+
return this._ctx;
|
|
1343
|
+
}
|
|
1344
|
+
get offlineContext() {
|
|
1345
|
+
return this._offlineCtx;
|
|
1346
|
+
}
|
|
1347
|
+
set paused(paused) {
|
|
1348
|
+
if (paused && this._ctx.state === "running") {
|
|
1349
|
+
this._ctx.suspend();
|
|
1350
|
+
} else if (!paused && this._ctx.state === "suspended") {
|
|
1351
|
+
this._ctx.resume();
|
|
1352
|
+
}
|
|
1353
|
+
this._paused = paused;
|
|
1354
|
+
}
|
|
1355
|
+
get paused() {
|
|
1356
|
+
return this._paused;
|
|
1357
|
+
}
|
|
1358
|
+
refresh() {
|
|
1359
|
+
this.events.emit("refresh");
|
|
1360
|
+
}
|
|
1361
|
+
refreshPaused() {
|
|
1362
|
+
this.events.emit("refreshPaused");
|
|
1363
|
+
}
|
|
1364
|
+
toggleMute() {
|
|
1365
|
+
this.muted = !this.muted;
|
|
1366
|
+
this.refresh();
|
|
1367
|
+
return this.muted;
|
|
1368
|
+
}
|
|
1369
|
+
togglePause() {
|
|
1370
|
+
this.paused = !this.paused;
|
|
1371
|
+
this.refreshPaused();
|
|
1372
|
+
return this._paused;
|
|
1373
|
+
}
|
|
1374
|
+
decode(arrayBuffer, callback) {
|
|
1375
|
+
const handleError = err => {
|
|
1376
|
+
callback(new Error((err === null || err === void 0 ? void 0 : err.message) || "Unable to decode file"));
|
|
1377
|
+
};
|
|
1378
|
+
const result = this._offlineCtx.decodeAudioData(arrayBuffer, buffer => {
|
|
1379
|
+
callback(null, buffer);
|
|
1380
|
+
}, handleError);
|
|
1381
|
+
if (result) {
|
|
1382
|
+
result.catch(handleError);
|
|
1383
|
+
}
|
|
1384
|
+
}
|
|
1385
|
+
}
|
|
1386
|
+
class SoundLibrary {
|
|
1387
|
+
constructor() {
|
|
1388
|
+
this.init();
|
|
100
1389
|
}
|
|
101
1390
|
init() {
|
|
102
|
-
this.
|
|
1391
|
+
if (this.supported) {
|
|
1392
|
+
this._webAudioContext = new WebAudioContext();
|
|
1393
|
+
}
|
|
1394
|
+
this._htmlAudioContext = new HTMLAudioContext();
|
|
1395
|
+
this._sounds = {};
|
|
1396
|
+
this.useLegacy = !this.supported;
|
|
1397
|
+
return this;
|
|
1398
|
+
}
|
|
1399
|
+
get context() {
|
|
1400
|
+
return this._context;
|
|
1401
|
+
}
|
|
1402
|
+
get filtersAll() {
|
|
1403
|
+
if (!this.useLegacy) {
|
|
1404
|
+
return this._context.filters;
|
|
1405
|
+
}
|
|
1406
|
+
return [];
|
|
1407
|
+
}
|
|
1408
|
+
set filtersAll(filtersAll) {
|
|
1409
|
+
if (!this.useLegacy) {
|
|
1410
|
+
this._context.filters = filtersAll;
|
|
1411
|
+
}
|
|
1412
|
+
}
|
|
1413
|
+
get supported() {
|
|
1414
|
+
return WebAudioContext.AudioContext !== null;
|
|
1415
|
+
}
|
|
1416
|
+
add(source, sourceOptions) {
|
|
1417
|
+
if (typeof source === "object") {
|
|
1418
|
+
const results = {};
|
|
1419
|
+
for (const alias in source) {
|
|
1420
|
+
const options2 = this._getOptions(source[alias], sourceOptions);
|
|
1421
|
+
results[alias] = this.add(alias, options2);
|
|
1422
|
+
}
|
|
1423
|
+
return results;
|
|
1424
|
+
}
|
|
1425
|
+
console.assert(!this._sounds[source], `Sound with alias ${source} already exists.`);
|
|
1426
|
+
if (sourceOptions instanceof Sound$2) {
|
|
1427
|
+
this._sounds[source] = sourceOptions;
|
|
1428
|
+
return sourceOptions;
|
|
1429
|
+
}
|
|
1430
|
+
const options = this._getOptions(sourceOptions);
|
|
1431
|
+
const sound = Sound$2.from(options);
|
|
1432
|
+
this._sounds[source] = sound;
|
|
1433
|
+
return sound;
|
|
1434
|
+
}
|
|
1435
|
+
_getOptions(source, overrides) {
|
|
1436
|
+
let options;
|
|
1437
|
+
if (typeof source === "string") {
|
|
1438
|
+
options = {
|
|
1439
|
+
url: source
|
|
1440
|
+
};
|
|
1441
|
+
} else if (Array.isArray(source)) {
|
|
1442
|
+
options = {
|
|
1443
|
+
url: source
|
|
1444
|
+
};
|
|
1445
|
+
} else if (source instanceof ArrayBuffer || source instanceof AudioBuffer || source instanceof HTMLAudioElement) {
|
|
1446
|
+
options = {
|
|
1447
|
+
source
|
|
1448
|
+
};
|
|
1449
|
+
} else {
|
|
1450
|
+
options = source;
|
|
1451
|
+
}
|
|
1452
|
+
options = _objectSpread(_objectSpread({}, options), overrides || {});
|
|
1453
|
+
return options;
|
|
1454
|
+
}
|
|
1455
|
+
get useLegacy() {
|
|
1456
|
+
return this._useLegacy;
|
|
1457
|
+
}
|
|
1458
|
+
set useLegacy(legacy) {
|
|
1459
|
+
this._useLegacy = legacy;
|
|
1460
|
+
this._context = !legacy && this.supported ? this._webAudioContext : this._htmlAudioContext;
|
|
1461
|
+
}
|
|
1462
|
+
get disableAutoPause() {
|
|
1463
|
+
return !this._webAudioContext.autoPause;
|
|
1464
|
+
}
|
|
1465
|
+
set disableAutoPause(autoPause) {
|
|
1466
|
+
this._webAudioContext.autoPause = !autoPause;
|
|
1467
|
+
}
|
|
1468
|
+
remove(alias) {
|
|
1469
|
+
this.exists(alias, true);
|
|
1470
|
+
this._sounds[alias].destroy();
|
|
1471
|
+
delete this._sounds[alias];
|
|
1472
|
+
return this;
|
|
1473
|
+
}
|
|
1474
|
+
get volumeAll() {
|
|
1475
|
+
return this._context.volume;
|
|
1476
|
+
}
|
|
1477
|
+
set volumeAll(volume) {
|
|
1478
|
+
this._context.volume = volume;
|
|
1479
|
+
this._context.refresh();
|
|
1480
|
+
}
|
|
1481
|
+
get speedAll() {
|
|
1482
|
+
return this._context.speed;
|
|
1483
|
+
}
|
|
1484
|
+
set speedAll(speed) {
|
|
1485
|
+
this._context.speed = speed;
|
|
1486
|
+
this._context.refresh();
|
|
1487
|
+
}
|
|
1488
|
+
togglePauseAll() {
|
|
1489
|
+
return this._context.togglePause();
|
|
1490
|
+
}
|
|
1491
|
+
pauseAll() {
|
|
1492
|
+
this._context.paused = true;
|
|
1493
|
+
this._context.refreshPaused();
|
|
1494
|
+
return this;
|
|
1495
|
+
}
|
|
1496
|
+
resumeAll() {
|
|
1497
|
+
this._context.paused = false;
|
|
1498
|
+
this._context.refreshPaused();
|
|
1499
|
+
return this;
|
|
1500
|
+
}
|
|
1501
|
+
toggleMuteAll() {
|
|
1502
|
+
return this._context.toggleMute();
|
|
1503
|
+
}
|
|
1504
|
+
muteAll() {
|
|
1505
|
+
this._context.muted = true;
|
|
1506
|
+
this._context.refresh();
|
|
1507
|
+
return this;
|
|
1508
|
+
}
|
|
1509
|
+
unmuteAll() {
|
|
1510
|
+
this._context.muted = false;
|
|
1511
|
+
this._context.refresh();
|
|
1512
|
+
return this;
|
|
1513
|
+
}
|
|
1514
|
+
removeAll() {
|
|
1515
|
+
for (const alias in this._sounds) {
|
|
1516
|
+
this._sounds[alias].destroy();
|
|
1517
|
+
delete this._sounds[alias];
|
|
1518
|
+
}
|
|
1519
|
+
return this;
|
|
1520
|
+
}
|
|
1521
|
+
stopAll() {
|
|
1522
|
+
for (const alias in this._sounds) {
|
|
1523
|
+
this._sounds[alias].stop();
|
|
1524
|
+
}
|
|
1525
|
+
return this;
|
|
1526
|
+
}
|
|
1527
|
+
exists(alias, assert = false) {
|
|
1528
|
+
const exists = !!this._sounds[alias];
|
|
1529
|
+
if (assert) {
|
|
1530
|
+
console.assert(exists, `No sound matching alias '${alias}'.`);
|
|
1531
|
+
}
|
|
1532
|
+
return exists;
|
|
1533
|
+
}
|
|
1534
|
+
isPlaying() {
|
|
1535
|
+
for (const alias in this._sounds) {
|
|
1536
|
+
if (this._sounds[alias].isPlaying) {
|
|
1537
|
+
return true;
|
|
1538
|
+
}
|
|
1539
|
+
}
|
|
1540
|
+
return false;
|
|
1541
|
+
}
|
|
1542
|
+
find(alias) {
|
|
1543
|
+
this.exists(alias, true);
|
|
1544
|
+
return this._sounds[alias];
|
|
1545
|
+
}
|
|
1546
|
+
play(alias, options) {
|
|
1547
|
+
return this.find(alias).play(options);
|
|
1548
|
+
}
|
|
1549
|
+
stop(alias) {
|
|
1550
|
+
return this.find(alias).stop();
|
|
1551
|
+
}
|
|
1552
|
+
pause(alias) {
|
|
1553
|
+
return this.find(alias).pause();
|
|
1554
|
+
}
|
|
1555
|
+
resume(alias) {
|
|
1556
|
+
return this.find(alias).resume();
|
|
1557
|
+
}
|
|
1558
|
+
volume(alias, volume) {
|
|
1559
|
+
const sound = this.find(alias);
|
|
1560
|
+
if (volume !== void 0) {
|
|
1561
|
+
sound.volume = volume;
|
|
1562
|
+
}
|
|
1563
|
+
return sound.volume;
|
|
1564
|
+
}
|
|
1565
|
+
speed(alias, speed) {
|
|
1566
|
+
const sound = this.find(alias);
|
|
1567
|
+
if (speed !== void 0) {
|
|
1568
|
+
sound.speed = speed;
|
|
1569
|
+
}
|
|
1570
|
+
return sound.speed;
|
|
1571
|
+
}
|
|
1572
|
+
duration(alias) {
|
|
1573
|
+
return this.find(alias).duration;
|
|
1574
|
+
}
|
|
1575
|
+
close() {
|
|
1576
|
+
this.removeAll();
|
|
1577
|
+
this._sounds = null;
|
|
1578
|
+
if (this._webAudioContext) {
|
|
1579
|
+
this._webAudioContext.destroy();
|
|
1580
|
+
this._webAudioContext = null;
|
|
1581
|
+
}
|
|
1582
|
+
if (this._htmlAudioContext) {
|
|
1583
|
+
this._htmlAudioContext.destroy();
|
|
1584
|
+
this._htmlAudioContext = null;
|
|
1585
|
+
}
|
|
1586
|
+
this._context = null;
|
|
1587
|
+
return this;
|
|
1588
|
+
}
|
|
1589
|
+
}
|
|
1590
|
+
const getAlias = asset => {
|
|
1591
|
+
var _asset$alias;
|
|
1592
|
+
const src = asset.src;
|
|
1593
|
+
let alias = asset === null || asset === void 0 ? void 0 : (_asset$alias = asset.alias) === null || _asset$alias === void 0 ? void 0 : _asset$alias[0];
|
|
1594
|
+
if (!alias || asset.src === alias) {
|
|
1595
|
+
alias = pixi_js.path.basename(src, pixi_js.path.extname(src));
|
|
1596
|
+
}
|
|
1597
|
+
return alias;
|
|
1598
|
+
};
|
|
1599
|
+
const soundAsset = {
|
|
1600
|
+
extension: pixi_js.ExtensionType.Asset,
|
|
1601
|
+
detection: {
|
|
1602
|
+
test: async () => true,
|
|
1603
|
+
add: async formats => [...formats, ...extensions.filter(ext => supported[ext])],
|
|
1604
|
+
remove: async formats => formats.filter(ext => formats.includes(ext))
|
|
1605
|
+
},
|
|
1606
|
+
loader: {
|
|
1607
|
+
name: "sound",
|
|
1608
|
+
extension: {
|
|
1609
|
+
type: [pixi_js.ExtensionType.LoadParser],
|
|
1610
|
+
priority: pixi_js.LoaderParserPriority.High
|
|
1611
|
+
},
|
|
1612
|
+
test(url) {
|
|
1613
|
+
const ext = pixi_js.path.extname(url).slice(1);
|
|
1614
|
+
return !!supported[ext] || mimes.some(mime => url.startsWith(`data:${mime}`));
|
|
1615
|
+
},
|
|
1616
|
+
async load(url, asset) {
|
|
1617
|
+
const sound = await new Promise((resolve, reject) => Sound$2.from(_objectSpread(_objectSpread({}, asset.data), {}, {
|
|
1618
|
+
url,
|
|
1619
|
+
preload: true,
|
|
1620
|
+
loaded(err, sound2) {
|
|
1621
|
+
var _asset$data, _asset$data$loaded;
|
|
1622
|
+
if (err) {
|
|
1623
|
+
reject(err);
|
|
1624
|
+
} else {
|
|
1625
|
+
resolve(sound2);
|
|
1626
|
+
}
|
|
1627
|
+
(_asset$data = asset.data) === null || _asset$data === void 0 ? void 0 : (_asset$data$loaded = _asset$data.loaded) === null || _asset$data$loaded === void 0 ? void 0 : _asset$data$loaded.call(_asset$data, err, sound2);
|
|
1628
|
+
}
|
|
1629
|
+
})));
|
|
1630
|
+
getInstance().add(getAlias(asset), sound);
|
|
1631
|
+
return sound;
|
|
1632
|
+
},
|
|
1633
|
+
async unload(_sound, asset) {
|
|
1634
|
+
getInstance().remove(getAlias(asset));
|
|
1635
|
+
}
|
|
1636
|
+
}
|
|
1637
|
+
};
|
|
1638
|
+
pixi_js.extensions.add(soundAsset);
|
|
1639
|
+
const sound = setInstance(new SoundLibrary());
|
|
1640
|
+
pixi_js.extensions.add(soundAsset);
|
|
1641
|
+
let SoundSystem = class SoundSystem extends eva_js.System {
|
|
1642
|
+
constructor(obj) {
|
|
1643
|
+
super();
|
|
1644
|
+
this.autoPauseAndStart = true;
|
|
1645
|
+
this.components = [];
|
|
1646
|
+
this.audioBufferCache = {};
|
|
1647
|
+
_extends(this, obj);
|
|
1648
|
+
}
|
|
1649
|
+
resumeAll() {
|
|
1650
|
+
sound.resumeAll();
|
|
1651
|
+
}
|
|
1652
|
+
pauseAll() {
|
|
1653
|
+
sound.pauseAll();
|
|
1654
|
+
}
|
|
1655
|
+
stopAll() {
|
|
1656
|
+
sound.stopAll();
|
|
103
1657
|
}
|
|
104
1658
|
update() {
|
|
105
1659
|
const changes = this.componentObserver.clear();
|
|
@@ -124,12 +1678,7 @@ var _EVA_IIFE_sound = function (exports, eva_js) {
|
|
|
124
1678
|
component.onDestroy();
|
|
125
1679
|
});
|
|
126
1680
|
this.components = [];
|
|
127
|
-
|
|
128
|
-
this.gainNode.disconnect();
|
|
129
|
-
this.gainNode = null;
|
|
130
|
-
this.ctx.close();
|
|
131
|
-
this.ctx = null;
|
|
132
|
-
}
|
|
1681
|
+
sound.removeAll();
|
|
133
1682
|
}
|
|
134
1683
|
componentChanged(changed) {
|
|
135
1684
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -139,44 +1688,8 @@ var _EVA_IIFE_sound = function (exports, eva_js) {
|
|
|
139
1688
|
}
|
|
140
1689
|
});
|
|
141
1690
|
}
|
|
142
|
-
setupAudioContext() {
|
|
143
|
-
try {
|
|
144
|
-
const AudioContext = window.AudioContext || window.webkitAudioContext;
|
|
145
|
-
this.ctx = new AudioContext();
|
|
146
|
-
} catch (error) {
|
|
147
|
-
console.error(error);
|
|
148
|
-
if (this.onError) {
|
|
149
|
-
this.onError(error);
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
if (!this.ctx) {
|
|
153
|
-
return;
|
|
154
|
-
}
|
|
155
|
-
this.gainNode = typeof this.ctx.createGain === 'undefined' ? this.ctx.createGainNode() : this.ctx.createGain();
|
|
156
|
-
this.gainNode.gain.setValueAtTime(this.muted ? 0 : this.volume, this.ctx.currentTime);
|
|
157
|
-
this.gainNode.connect(this.ctx.destination);
|
|
158
|
-
this.unlockAudio();
|
|
159
|
-
}
|
|
160
|
-
unlockAudio() {
|
|
161
|
-
if (!this.ctx || !this.audioLocked) {
|
|
162
|
-
return;
|
|
163
|
-
}
|
|
164
|
-
const unlock = () => {
|
|
165
|
-
if (this.ctx) {
|
|
166
|
-
const removeListenerFn = () => {
|
|
167
|
-
document.body.removeEventListener('touchstart', unlock);
|
|
168
|
-
document.body.removeEventListener('touchend', unlock);
|
|
169
|
-
document.body.removeEventListener('click', unlock);
|
|
170
|
-
};
|
|
171
|
-
this.ctx.resume().then(removeListenerFn, removeListenerFn);
|
|
172
|
-
}
|
|
173
|
-
};
|
|
174
|
-
document.body.addEventListener('touchstart', unlock);
|
|
175
|
-
document.body.addEventListener('touchend', unlock);
|
|
176
|
-
document.body.addEventListener('click', unlock);
|
|
177
|
-
}
|
|
178
1691
|
add(changed) {
|
|
179
|
-
var _a;
|
|
1692
|
+
var _a, _b;
|
|
180
1693
|
return __awaiter(this, void 0, void 0, function* () {
|
|
181
1694
|
const component = changed.component;
|
|
182
1695
|
this.components.push(component);
|
|
@@ -187,11 +1700,9 @@ var _EVA_IIFE_sound = function (exports, eva_js) {
|
|
|
187
1700
|
component.state = 'loading';
|
|
188
1701
|
const audio = yield eva_js.resource.getResource(config.resource);
|
|
189
1702
|
if (!this.audioBufferCache[audio.name] && ((_a = audio === null || audio === void 0 ? void 0 : audio.data) === null || _a === void 0 ? void 0 : _a.audio)) {
|
|
190
|
-
this.audioBufferCache[audio.name] =
|
|
1703
|
+
this.audioBufferCache[audio.name] = (_b = audio === null || audio === void 0 ? void 0 : audio.data) === null || _b === void 0 ? void 0 : _b.audio;
|
|
191
1704
|
}
|
|
192
1705
|
if (this.audioBufferCache[audio.name]) {
|
|
193
|
-
component.systemContext = this.ctx;
|
|
194
|
-
component.systemDestination = this.gainNode;
|
|
195
1706
|
component.onload(this.audioBufferCache[audio.name]);
|
|
196
1707
|
}
|
|
197
1708
|
} catch (error) {
|
|
@@ -201,40 +1712,6 @@ var _EVA_IIFE_sound = function (exports, eva_js) {
|
|
|
201
1712
|
}
|
|
202
1713
|
});
|
|
203
1714
|
}
|
|
204
|
-
decodeAudioData(arraybuffer, name) {
|
|
205
|
-
if (this.decodeAudioPromiseMap[name]) {
|
|
206
|
-
return this.decodeAudioPromiseMap[name];
|
|
207
|
-
}
|
|
208
|
-
const promise = new Promise((resolve, reject) => {
|
|
209
|
-
if (!this.ctx) {
|
|
210
|
-
reject(new Error('No audio support'));
|
|
211
|
-
}
|
|
212
|
-
const success = decodedData => {
|
|
213
|
-
if (this.decodeAudioPromiseMap[name]) {
|
|
214
|
-
delete this.decodeAudioPromiseMap[name];
|
|
215
|
-
}
|
|
216
|
-
if (decodedData) {
|
|
217
|
-
resolve(decodedData);
|
|
218
|
-
} else {
|
|
219
|
-
reject(new Error(`Error decoding audio ${name}`));
|
|
220
|
-
}
|
|
221
|
-
};
|
|
222
|
-
const error = err => {
|
|
223
|
-
if (this.decodeAudioPromiseMap[name]) {
|
|
224
|
-
delete this.decodeAudioPromiseMap[name];
|
|
225
|
-
}
|
|
226
|
-
reject(new Error(`${err}. arrayBuffer byteLength: ${arraybuffer ? arraybuffer.byteLength : 0}`));
|
|
227
|
-
};
|
|
228
|
-
const promise = this.ctx.decodeAudioData(arraybuffer, success, error);
|
|
229
|
-
if (promise instanceof Promise) {
|
|
230
|
-
promise.catch(err => {
|
|
231
|
-
reject(new Error(`catch ${err}, arrayBuffer byteLength: ${arraybuffer ? arraybuffer.byteLength : 0}`));
|
|
232
|
-
});
|
|
233
|
-
}
|
|
234
|
-
});
|
|
235
|
-
this.decodeAudioPromiseMap[name] = promise;
|
|
236
|
-
return promise;
|
|
237
|
-
}
|
|
238
1715
|
};
|
|
239
1716
|
SoundSystem.systemName = 'SoundSystem';
|
|
240
1717
|
SoundSystem = __decorate([eva_js.decorators.componentObserver({
|
|
@@ -251,34 +1728,25 @@ var _EVA_IIFE_sound = function (exports, eva_js) {
|
|
|
251
1728
|
muted: false,
|
|
252
1729
|
volume: 1,
|
|
253
1730
|
loop: false,
|
|
254
|
-
seek: 0
|
|
1731
|
+
seek: 0,
|
|
1732
|
+
speed: 1
|
|
255
1733
|
};
|
|
256
|
-
this.playTime = 0;
|
|
257
|
-
this.startTime = 0;
|
|
258
|
-
this.duration = 0;
|
|
259
1734
|
this.actionQueue = [];
|
|
260
1735
|
}
|
|
1736
|
+
get playing() {
|
|
1737
|
+
return this.buffer.isPlaying;
|
|
1738
|
+
}
|
|
261
1739
|
get muted() {
|
|
262
|
-
return this.
|
|
1740
|
+
return this.buffer.muted;
|
|
263
1741
|
}
|
|
264
1742
|
set muted(v) {
|
|
265
|
-
|
|
266
|
-
return;
|
|
267
|
-
}
|
|
268
|
-
this.gainNode.gain.setValueAtTime(v ? 0 : this.config.volume, 0);
|
|
1743
|
+
this.buffer.muted = v;
|
|
269
1744
|
}
|
|
270
1745
|
get volume() {
|
|
271
|
-
return this.
|
|
1746
|
+
return this.buffer.volume;
|
|
272
1747
|
}
|
|
273
1748
|
set volume(v) {
|
|
274
|
-
|
|
275
|
-
return;
|
|
276
|
-
}
|
|
277
|
-
this.config.volume = v;
|
|
278
|
-
if (!this.gainNode) {
|
|
279
|
-
return;
|
|
280
|
-
}
|
|
281
|
-
this.gainNode.gain.setValueAtTime(v, 0);
|
|
1749
|
+
this.buffer.volume = v;
|
|
282
1750
|
}
|
|
283
1751
|
init(obj) {
|
|
284
1752
|
if (!obj) {
|
|
@@ -293,100 +1761,31 @@ var _EVA_IIFE_sound = function (exports, eva_js) {
|
|
|
293
1761
|
if (this.state !== 'loaded') {
|
|
294
1762
|
this.actionQueue.push(this.play.bind(this));
|
|
295
1763
|
}
|
|
296
|
-
this.
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
}
|
|
301
|
-
const when = this.systemContext.currentTime;
|
|
302
|
-
const offset = this.config.seek;
|
|
303
|
-
const duration = this.config.duration;
|
|
304
|
-
this.sourceNode.start(0, offset, duration);
|
|
305
|
-
this.startTime = when;
|
|
306
|
-
this.playTime = when - offset;
|
|
307
|
-
this.paused = false;
|
|
308
|
-
this.playing = true;
|
|
309
|
-
this.resetConfig();
|
|
310
|
-
this.endedListener = () => {
|
|
311
|
-
if (!this.sourceNode) {
|
|
312
|
-
return;
|
|
313
|
-
}
|
|
314
|
-
if (this.config.onEnd) {
|
|
315
|
-
this.config.onEnd();
|
|
316
|
-
}
|
|
317
|
-
if (this.playing) {
|
|
318
|
-
this.destroySource();
|
|
319
|
-
}
|
|
320
|
-
};
|
|
321
|
-
this.sourceNode.addEventListener('ended', this.endedListener);
|
|
1764
|
+
this.buffer.play();
|
|
1765
|
+
}
|
|
1766
|
+
resume() {
|
|
1767
|
+
this.buffer.resume();
|
|
322
1768
|
}
|
|
323
1769
|
pause() {
|
|
324
|
-
|
|
325
|
-
this.actionQueue.push(this.pause.bind(this));
|
|
326
|
-
}
|
|
327
|
-
if (this.paused || !this.playing) {
|
|
328
|
-
return;
|
|
329
|
-
}
|
|
330
|
-
this.paused = true;
|
|
331
|
-
this.playing = false;
|
|
332
|
-
this.config.seek = this.getCurrentTime();
|
|
333
|
-
this.destroySource();
|
|
1770
|
+
this.buffer.pause();
|
|
334
1771
|
}
|
|
335
1772
|
stop() {
|
|
336
|
-
|
|
337
|
-
this.actionQueue.push(this.stop.bind(this));
|
|
338
|
-
}
|
|
339
|
-
if (!this.paused && !this.playing) {
|
|
340
|
-
return;
|
|
341
|
-
}
|
|
342
|
-
this.playing = false;
|
|
343
|
-
this.paused = false;
|
|
344
|
-
this.destroySource();
|
|
345
|
-
this.resetConfig();
|
|
1773
|
+
this.buffer.stop();
|
|
346
1774
|
}
|
|
347
1775
|
onload(buffer) {
|
|
348
1776
|
this.state = 'loaded';
|
|
349
1777
|
this.buffer = buffer;
|
|
350
|
-
this.
|
|
1778
|
+
this.buffer.muted = this.config.muted;
|
|
1779
|
+
this.buffer.volume = this.config.volume;
|
|
1780
|
+
this.buffer.loop = this.config.loop;
|
|
1781
|
+
this.buffer.speed = this.config.speed;
|
|
351
1782
|
this.actionQueue.forEach(action => action());
|
|
352
1783
|
this.actionQueue.length = 0;
|
|
353
1784
|
}
|
|
354
1785
|
onDestroy() {
|
|
355
1786
|
this.actionQueue.length = 0;
|
|
356
|
-
this.
|
|
357
|
-
|
|
358
|
-
resetConfig() {
|
|
359
|
-
this.config.seek = 0;
|
|
360
|
-
}
|
|
361
|
-
getCurrentTime() {
|
|
362
|
-
if (this.config.loop && this.duration > 0) {
|
|
363
|
-
return (this.systemContext.currentTime - this.playTime) % this.duration;
|
|
364
|
-
}
|
|
365
|
-
return this.systemContext.currentTime - this.playTime;
|
|
366
|
-
}
|
|
367
|
-
createSource() {
|
|
368
|
-
if (!this.systemContext || this.state !== 'loaded') {
|
|
369
|
-
return;
|
|
370
|
-
}
|
|
371
|
-
this.sourceNode = this.systemContext.createBufferSource();
|
|
372
|
-
this.sourceNode.buffer = this.buffer;
|
|
373
|
-
this.sourceNode.loop = this.config.loop;
|
|
374
|
-
if (!this.gainNode) {
|
|
375
|
-
this.gainNode = this.systemContext.createGain();
|
|
376
|
-
this.gainNode.connect(this.systemDestination);
|
|
377
|
-
_extends(this, this.config);
|
|
378
|
-
}
|
|
379
|
-
this.sourceNode.connect(this.gainNode);
|
|
380
|
-
}
|
|
381
|
-
destroySource() {
|
|
382
|
-
if (!this.sourceNode) return;
|
|
383
|
-
this.sourceNode.removeEventListener('ended', this.endedListener);
|
|
384
|
-
this.sourceNode.stop();
|
|
385
|
-
this.sourceNode.disconnect();
|
|
386
|
-
this.sourceNode = null;
|
|
387
|
-
this.startTime = 0;
|
|
388
|
-
this.playTime = 0;
|
|
389
|
-
this.playing = false;
|
|
1787
|
+
this.buffer.destroy();
|
|
1788
|
+
this.buffer = null;
|
|
390
1789
|
}
|
|
391
1790
|
}
|
|
392
1791
|
Sound.componentName = 'Sound';
|
|
@@ -397,5 +1796,5 @@ var _EVA_IIFE_sound = function (exports, eva_js) {
|
|
|
397
1796
|
value: true
|
|
398
1797
|
});
|
|
399
1798
|
return exports;
|
|
400
|
-
}({}, EVA);
|
|
1799
|
+
}({}, EVA, PIXI);
|
|
401
1800
|
window.EVA.plugin.sound = window.EVA.plugin.sound || _EVA_IIFE_sound;
|