@eva/plugin-sound 2.0.0-beta.2 → 2.0.0-beta.21
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 +1656 -223
- package/dist/EVA.plugin.sound.min.js +1 -1
- package/dist/plugin-sound.cjs.js +54 -227
- package/dist/plugin-sound.cjs.prod.js +3 -2
- package/dist/plugin-sound.d.ts +9 -28
- package/dist/plugin-sound.esm.js +54 -227
- package/package.json +5 -3
package/dist/plugin-sound.esm.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { System, decorators, OBSERVER_TYPE, resource, Component } from '@eva/eva.js';
|
|
2
|
+
import { sound, utils, soundAsset } from '@ali/pixi-sound';
|
|
3
|
+
import { extensions } from 'pixi.js';
|
|
2
4
|
|
|
3
5
|
/*! *****************************************************************************
|
|
4
6
|
Copyright (c) Microsoft Corporation. All rights reserved.
|
|
@@ -31,69 +33,31 @@ function __awaiter(thisArg, _arguments, P, generator) {
|
|
|
31
33
|
});
|
|
32
34
|
}
|
|
33
35
|
|
|
36
|
+
sound.disableAutoPause = true;
|
|
37
|
+
utils.extensions.push('aac');
|
|
38
|
+
utils.validateFormats({
|
|
39
|
+
aac: 'audio/aac',
|
|
40
|
+
});
|
|
41
|
+
extensions.add(soundAsset);
|
|
34
42
|
let SoundSystem = class SoundSystem extends System {
|
|
35
43
|
constructor(obj) {
|
|
36
44
|
super();
|
|
37
45
|
this.autoPauseAndStart = true;
|
|
38
46
|
this.components = [];
|
|
39
|
-
this.pausedComponents = [];
|
|
40
47
|
this.audioBufferCache = {};
|
|
41
|
-
this.decodeAudioPromiseMap = {};
|
|
42
48
|
Object.assign(this, obj);
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
return this.gainNode ? this.gainNode.gain.value === 0 : false;
|
|
46
|
-
}
|
|
47
|
-
set muted(v) {
|
|
48
|
-
if (!this.gainNode) {
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
|
-
this.gainNode.gain.setValueAtTime(v ? 0 : 1, 0);
|
|
52
|
-
}
|
|
53
|
-
get volume() {
|
|
54
|
-
return this.gainNode ? this.gainNode.gain.value : 1;
|
|
55
|
-
}
|
|
56
|
-
set volume(v) {
|
|
57
|
-
if (!this.gainNode || typeof v !== 'number' || v < 0 || v > 1) {
|
|
58
|
-
return;
|
|
59
|
-
}
|
|
60
|
-
this.gainNode.gain.setValueAtTime(v, 0);
|
|
61
|
-
}
|
|
62
|
-
get audioLocked() {
|
|
63
|
-
if (!this.ctx) {
|
|
64
|
-
return true;
|
|
49
|
+
if (obj === null || obj === void 0 ? void 0 : obj.useLegacy) {
|
|
50
|
+
sound.useLegacy = true;
|
|
65
51
|
}
|
|
66
|
-
return this.ctx.state !== 'running';
|
|
67
52
|
}
|
|
68
53
|
resumeAll() {
|
|
69
|
-
|
|
70
|
-
this.pausedComponents.forEach(component => {
|
|
71
|
-
component.play();
|
|
72
|
-
});
|
|
73
|
-
this.pausedComponents = [];
|
|
74
|
-
};
|
|
75
|
-
this.ctx.resume().then(handleResume, handleResume);
|
|
54
|
+
sound.resumeAll();
|
|
76
55
|
}
|
|
77
56
|
pauseAll() {
|
|
78
|
-
|
|
79
|
-
if (component.playing) {
|
|
80
|
-
this.pausedComponents.push(component);
|
|
81
|
-
component.pause();
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
this.ctx.suspend().then();
|
|
57
|
+
sound.pauseAll();
|
|
85
58
|
}
|
|
86
59
|
stopAll() {
|
|
87
|
-
|
|
88
|
-
if (component.playing) {
|
|
89
|
-
component.stop();
|
|
90
|
-
}
|
|
91
|
-
});
|
|
92
|
-
this.pausedComponents = [];
|
|
93
|
-
this.ctx.suspend().then();
|
|
94
|
-
}
|
|
95
|
-
init() {
|
|
96
|
-
this.setupAudioContext();
|
|
60
|
+
sound.stopAll();
|
|
97
61
|
}
|
|
98
62
|
update() {
|
|
99
63
|
const changes = this.componentObserver.clear();
|
|
@@ -118,12 +82,7 @@ let SoundSystem = class SoundSystem extends System {
|
|
|
118
82
|
component.onDestroy();
|
|
119
83
|
});
|
|
120
84
|
this.components = [];
|
|
121
|
-
|
|
122
|
-
this.gainNode.disconnect();
|
|
123
|
-
this.gainNode = null;
|
|
124
|
-
this.ctx.close();
|
|
125
|
-
this.ctx = null;
|
|
126
|
-
}
|
|
85
|
+
sound.removeAll();
|
|
127
86
|
}
|
|
128
87
|
componentChanged(changed) {
|
|
129
88
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -134,46 +93,8 @@ let SoundSystem = class SoundSystem extends System {
|
|
|
134
93
|
}
|
|
135
94
|
});
|
|
136
95
|
}
|
|
137
|
-
setupAudioContext() {
|
|
138
|
-
try {
|
|
139
|
-
const AudioContext = window.AudioContext || window.webkitAudioContext;
|
|
140
|
-
this.ctx = new AudioContext();
|
|
141
|
-
}
|
|
142
|
-
catch (error) {
|
|
143
|
-
console.error(error);
|
|
144
|
-
if (this.onError) {
|
|
145
|
-
this.onError(error);
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
if (!this.ctx) {
|
|
149
|
-
return;
|
|
150
|
-
}
|
|
151
|
-
this.gainNode =
|
|
152
|
-
typeof this.ctx.createGain === 'undefined' ? this.ctx.createGainNode() : this.ctx.createGain();
|
|
153
|
-
this.gainNode.gain.setValueAtTime(this.muted ? 0 : this.volume, this.ctx.currentTime);
|
|
154
|
-
this.gainNode.connect(this.ctx.destination);
|
|
155
|
-
this.unlockAudio();
|
|
156
|
-
}
|
|
157
|
-
unlockAudio() {
|
|
158
|
-
if (!this.ctx || !this.audioLocked) {
|
|
159
|
-
return;
|
|
160
|
-
}
|
|
161
|
-
const unlock = () => {
|
|
162
|
-
if (this.ctx) {
|
|
163
|
-
const removeListenerFn = () => {
|
|
164
|
-
document.body.removeEventListener('touchstart', unlock);
|
|
165
|
-
document.body.removeEventListener('touchend', unlock);
|
|
166
|
-
document.body.removeEventListener('click', unlock);
|
|
167
|
-
};
|
|
168
|
-
this.ctx.resume().then(removeListenerFn, removeListenerFn);
|
|
169
|
-
}
|
|
170
|
-
};
|
|
171
|
-
document.body.addEventListener('touchstart', unlock);
|
|
172
|
-
document.body.addEventListener('touchend', unlock);
|
|
173
|
-
document.body.addEventListener('click', unlock);
|
|
174
|
-
}
|
|
175
96
|
add(changed) {
|
|
176
|
-
var _a;
|
|
97
|
+
var _a, _b;
|
|
177
98
|
return __awaiter(this, void 0, void 0, function* () {
|
|
178
99
|
const component = changed.component;
|
|
179
100
|
this.components.push(component);
|
|
@@ -182,11 +103,9 @@ let SoundSystem = class SoundSystem extends System {
|
|
|
182
103
|
component.state = 'loading';
|
|
183
104
|
const audio = yield resource.getResource(config.resource);
|
|
184
105
|
if (!this.audioBufferCache[audio.name] && ((_a = audio === null || audio === void 0 ? void 0 : audio.data) === null || _a === void 0 ? void 0 : _a.audio)) {
|
|
185
|
-
this.audioBufferCache[audio.name] =
|
|
106
|
+
this.audioBufferCache[audio.name] = (_b = audio === null || audio === void 0 ? void 0 : audio.data) === null || _b === void 0 ? void 0 : _b.audio;
|
|
186
107
|
}
|
|
187
108
|
if (this.audioBufferCache[audio.name]) {
|
|
188
|
-
component.systemContext = this.ctx;
|
|
189
|
-
component.systemDestination = this.gainNode;
|
|
190
109
|
component.onload(this.audioBufferCache[audio.name]);
|
|
191
110
|
}
|
|
192
111
|
}
|
|
@@ -197,41 +116,6 @@ let SoundSystem = class SoundSystem extends System {
|
|
|
197
116
|
}
|
|
198
117
|
});
|
|
199
118
|
}
|
|
200
|
-
decodeAudioData(arraybuffer, name) {
|
|
201
|
-
if (this.decodeAudioPromiseMap[name]) {
|
|
202
|
-
return this.decodeAudioPromiseMap[name];
|
|
203
|
-
}
|
|
204
|
-
const promise = new Promise((resolve, reject) => {
|
|
205
|
-
if (!this.ctx) {
|
|
206
|
-
reject(new Error('No audio support'));
|
|
207
|
-
}
|
|
208
|
-
const success = (decodedData) => {
|
|
209
|
-
if (this.decodeAudioPromiseMap[name]) {
|
|
210
|
-
delete this.decodeAudioPromiseMap[name];
|
|
211
|
-
}
|
|
212
|
-
if (decodedData) {
|
|
213
|
-
resolve(decodedData);
|
|
214
|
-
}
|
|
215
|
-
else {
|
|
216
|
-
reject(new Error(`Error decoding audio ${name}`));
|
|
217
|
-
}
|
|
218
|
-
};
|
|
219
|
-
const error = (err) => {
|
|
220
|
-
if (this.decodeAudioPromiseMap[name]) {
|
|
221
|
-
delete this.decodeAudioPromiseMap[name];
|
|
222
|
-
}
|
|
223
|
-
reject(new Error(`${err}. arrayBuffer byteLength: ${arraybuffer ? arraybuffer.byteLength : 0}`));
|
|
224
|
-
};
|
|
225
|
-
const promise = this.ctx.decodeAudioData(arraybuffer, success, error);
|
|
226
|
-
if (promise instanceof Promise) {
|
|
227
|
-
promise.catch(err => {
|
|
228
|
-
reject(new Error(`catch ${err}, arrayBuffer byteLength: ${arraybuffer ? arraybuffer.byteLength : 0}`));
|
|
229
|
-
});
|
|
230
|
-
}
|
|
231
|
-
});
|
|
232
|
-
this.decodeAudioPromiseMap[name] = promise;
|
|
233
|
-
return promise;
|
|
234
|
-
}
|
|
235
119
|
};
|
|
236
120
|
SoundSystem.systemName = 'SoundSystem';
|
|
237
121
|
SoundSystem = __decorate([
|
|
@@ -252,33 +136,34 @@ class Sound extends Component {
|
|
|
252
136
|
volume: 1,
|
|
253
137
|
loop: false,
|
|
254
138
|
seek: 0,
|
|
139
|
+
speed: 1,
|
|
255
140
|
};
|
|
256
|
-
this.playTime = 0;
|
|
257
|
-
this.startTime = 0;
|
|
258
|
-
this.duration = 0;
|
|
259
141
|
this.actionQueue = [];
|
|
142
|
+
this.startTime = 0;
|
|
143
|
+
}
|
|
144
|
+
get systemContext() {
|
|
145
|
+
return sound.context.audioContext;
|
|
146
|
+
}
|
|
147
|
+
get playing() {
|
|
148
|
+
if (!this.buffer)
|
|
149
|
+
return false;
|
|
150
|
+
return this.buffer.isPlaying;
|
|
260
151
|
}
|
|
261
152
|
get muted() {
|
|
262
|
-
|
|
153
|
+
var _a;
|
|
154
|
+
return ((_a = this.buffer) === null || _a === void 0 ? void 0 : _a.muted) || false;
|
|
263
155
|
}
|
|
264
156
|
set muted(v) {
|
|
265
|
-
if (
|
|
266
|
-
|
|
267
|
-
}
|
|
268
|
-
this.gainNode.gain.setValueAtTime(v ? 0 : this.config.volume, 0);
|
|
157
|
+
if (this.buffer)
|
|
158
|
+
this.buffer.muted = v;
|
|
269
159
|
}
|
|
270
160
|
get volume() {
|
|
271
|
-
|
|
161
|
+
var _a;
|
|
162
|
+
return ((_a = this.buffer) === null || _a === void 0 ? void 0 : _a.volume) || 0;
|
|
272
163
|
}
|
|
273
164
|
set volume(v) {
|
|
274
|
-
if (
|
|
275
|
-
|
|
276
|
-
}
|
|
277
|
-
this.config.volume = v;
|
|
278
|
-
if (!this.gainNode) {
|
|
279
|
-
return;
|
|
280
|
-
}
|
|
281
|
-
this.gainNode.gain.setValueAtTime(v, 0);
|
|
165
|
+
if (this.buffer)
|
|
166
|
+
this.buffer.volume = v;
|
|
282
167
|
}
|
|
283
168
|
init(obj) {
|
|
284
169
|
if (!obj) {
|
|
@@ -293,101 +178,43 @@ class Sound extends Component {
|
|
|
293
178
|
if (this.state !== 'loaded') {
|
|
294
179
|
this.actionQueue.push(this.play.bind(this));
|
|
295
180
|
}
|
|
296
|
-
this.
|
|
297
|
-
this.createSource();
|
|
298
|
-
if (!this.sourceNode) {
|
|
181
|
+
if (!this.buffer)
|
|
299
182
|
return;
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
this.
|
|
305
|
-
|
|
306
|
-
this.
|
|
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);
|
|
183
|
+
this.startTime = this.systemContext.currentTime;
|
|
184
|
+
this.buffer.play();
|
|
185
|
+
}
|
|
186
|
+
resume() {
|
|
187
|
+
if (!this.buffer)
|
|
188
|
+
return;
|
|
189
|
+
this.buffer.resume();
|
|
322
190
|
}
|
|
323
191
|
pause() {
|
|
324
|
-
if (this.
|
|
325
|
-
this.actionQueue.push(this.pause.bind(this));
|
|
326
|
-
}
|
|
327
|
-
if (this.paused || !this.playing) {
|
|
192
|
+
if (!this.buffer)
|
|
328
193
|
return;
|
|
329
|
-
|
|
330
|
-
this.paused = true;
|
|
331
|
-
this.playing = false;
|
|
332
|
-
this.config.seek = this.getCurrentTime();
|
|
333
|
-
this.destroySource();
|
|
194
|
+
this.buffer.pause();
|
|
334
195
|
}
|
|
335
196
|
stop() {
|
|
336
|
-
if (this.
|
|
337
|
-
this.actionQueue.push(this.stop.bind(this));
|
|
338
|
-
}
|
|
339
|
-
if (!this.paused && !this.playing) {
|
|
197
|
+
if (!this.buffer)
|
|
340
198
|
return;
|
|
341
|
-
|
|
342
|
-
this.playing = false;
|
|
343
|
-
this.paused = false;
|
|
344
|
-
this.destroySource();
|
|
345
|
-
this.resetConfig();
|
|
199
|
+
this.buffer.stop();
|
|
346
200
|
}
|
|
347
201
|
onload(buffer) {
|
|
348
202
|
this.state = 'loaded';
|
|
349
203
|
this.buffer = buffer;
|
|
350
|
-
this.
|
|
204
|
+
this.buffer.muted = this.config.muted;
|
|
205
|
+
this.buffer.volume = this.config.volume;
|
|
206
|
+
this.buffer.loop = this.config.loop;
|
|
207
|
+
this.buffer.speed = this.config.speed;
|
|
351
208
|
this.actionQueue.forEach(action => action());
|
|
352
209
|
this.actionQueue.length = 0;
|
|
353
210
|
}
|
|
354
211
|
onDestroy() {
|
|
355
212
|
this.actionQueue.length = 0;
|
|
356
|
-
this.destroySource();
|
|
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
|
-
Object.assign(this, this.config);
|
|
378
|
-
}
|
|
379
|
-
this.sourceNode.connect(this.gainNode);
|
|
380
|
-
}
|
|
381
|
-
destroySource() {
|
|
382
|
-
if (!this.sourceNode)
|
|
383
|
-
return;
|
|
384
|
-
this.sourceNode.removeEventListener('ended', this.endedListener);
|
|
385
|
-
this.sourceNode.stop();
|
|
386
|
-
this.sourceNode.disconnect();
|
|
387
|
-
this.sourceNode = null;
|
|
388
213
|
this.startTime = 0;
|
|
389
|
-
this.
|
|
390
|
-
|
|
214
|
+
if (this.buffer) {
|
|
215
|
+
this.buffer.destroy();
|
|
216
|
+
this.buffer = null;
|
|
217
|
+
}
|
|
391
218
|
}
|
|
392
219
|
}
|
|
393
220
|
Sound.componentName = 'Sound';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eva/plugin-sound",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
3
|
+
"version": "2.0.0-beta.21",
|
|
4
4
|
"description": "@eva/plugin-sound",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"module": "dist/plugin-sound.esm.js",
|
|
@@ -18,7 +18,9 @@
|
|
|
18
18
|
"license": "MIT",
|
|
19
19
|
"homepage": "https://eva.js.org",
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@eva/eva.js": "2.0.0-beta.
|
|
22
|
-
"eventemitter3": "^3.1.2"
|
|
21
|
+
"@eva/eva.js": "2.0.0-beta.21",
|
|
22
|
+
"eventemitter3": "^3.1.2",
|
|
23
|
+
"pixi.js": "^8.8.1",
|
|
24
|
+
"@ali/pixi-sound": "^6.0.3"
|
|
23
25
|
}
|
|
24
26
|
}
|