@eva/plugin-sound 2.0.0-beta.1 → 2.0.0-beta.11

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.
@@ -1,4 +1,6 @@
1
- import { decorators, System, OBSERVER_TYPE, resource, Component } from '@eva/eva.js';
1
+ import { System, decorators, OBSERVER_TYPE, resource, Component } from '@eva/eva.js';
2
+ import { soundAsset, sound } from '@pixi/sound';
3
+ import { extensions } from 'pixi.js';
2
4
 
3
5
  /*! *****************************************************************************
4
6
  Copyright (c) Microsoft Corporation. All rights reserved.
@@ -14,20 +16,6 @@ MERCHANTABLITY OR NON-INFRINGEMENT.
14
16
  See the Apache Version 2.0 License for specific language governing permissions
15
17
  and limitations under the License.
16
18
  ***************************************************************************** */
17
- /* global Reflect, Promise */
18
-
19
- var extendStatics = function(d, b) {
20
- extendStatics = Object.setPrototypeOf ||
21
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
22
- function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
23
- return extendStatics(d, b);
24
- };
25
-
26
- function __extends(d, b) {
27
- extendStatics(d, b);
28
- function __() { this.constructor = d; }
29
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
30
- }
31
19
 
32
20
  function __decorate(decorators, target, key, desc) {
33
21
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
@@ -43,357 +31,133 @@ function __awaiter(thisArg, _arguments, P, generator) {
43
31
  function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
44
32
  step((generator = generator.apply(thisArg, _arguments || [])).next());
45
33
  });
46
- }
47
-
48
- function __generator(thisArg, body) {
49
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
50
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
51
- function verb(n) { return function (v) { return step([n, v]); }; }
52
- function step(op) {
53
- if (f) throw new TypeError("Generator is already executing.");
54
- while (_) try {
55
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
56
- if (y = 0, t) op = [op[0] & 2, t.value];
57
- switch (op[0]) {
58
- case 0: case 1: t = op; break;
59
- case 4: _.label++; return { value: op[1], done: false };
60
- case 5: _.label++; y = op[1]; op = [0]; continue;
61
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
62
- default:
63
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
64
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
65
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
66
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
67
- if (t[2]) _.ops.pop();
68
- _.trys.pop(); continue;
69
- }
70
- op = body.call(thisArg, _);
71
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
72
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
73
- }
74
- }
75
-
76
- function __values(o) {
77
- var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0;
78
- if (m) return m.call(o);
79
- return {
80
- next: function () {
81
- if (o && i >= o.length) o = void 0;
82
- return { value: o && o[i++], done: !o };
83
- }
84
- };
85
34
  }
86
35
 
87
- var SoundSystem = (function (_super) {
88
- __extends(SoundSystem, _super);
89
- function SoundSystem(obj) {
90
- var _this = _super.call(this) || this;
91
- _this.autoPauseAndStart = true;
92
- _this.components = [];
93
- _this.pausedComponents = [];
94
- _this.audioBufferCache = {};
95
- _this.decodeAudioPromiseMap = {};
96
- Object.assign(_this, obj);
97
- return _this;
36
+ extensions.add(soundAsset);
37
+ let SoundSystem = class SoundSystem extends System {
38
+ constructor(obj) {
39
+ super();
40
+ this.autoPauseAndStart = true;
41
+ this.components = [];
42
+ this.audioBufferCache = {};
43
+ Object.assign(this, obj);
98
44
  }
99
- Object.defineProperty(SoundSystem.prototype, "muted", {
100
- get: function () {
101
- return this.gainNode ? this.gainNode.gain.value === 0 : false;
102
- },
103
- set: function (v) {
104
- if (!this.gainNode) {
105
- return;
106
- }
107
- this.gainNode.gain.setValueAtTime(v ? 0 : 1, 0);
108
- },
109
- enumerable: false,
110
- configurable: true
111
- });
112
- Object.defineProperty(SoundSystem.prototype, "volume", {
113
- get: function () {
114
- return this.gainNode ? this.gainNode.gain.value : 1;
115
- },
116
- set: function (v) {
117
- if (!this.gainNode || typeof v !== 'number' || v < 0 || v > 1) {
118
- return;
119
- }
120
- this.gainNode.gain.setValueAtTime(v, 0);
121
- },
122
- enumerable: false,
123
- configurable: true
124
- });
125
- Object.defineProperty(SoundSystem.prototype, "audioLocked", {
126
- get: function () {
127
- if (!this.ctx) {
128
- return true;
129
- }
130
- return this.ctx.state !== 'running';
131
- },
132
- enumerable: false,
133
- configurable: true
134
- });
135
- SoundSystem.prototype.resumeAll = function () {
136
- var _this = this;
137
- var handleResume = function () {
138
- _this.pausedComponents.forEach(function (component) {
139
- component.play();
140
- });
141
- _this.pausedComponents = [];
142
- };
143
- this.ctx.resume().then(handleResume, handleResume);
144
- };
145
- SoundSystem.prototype.pauseAll = function () {
146
- var _this = this;
147
- this.components.forEach(function (component) {
148
- if (component.playing) {
149
- _this.pausedComponents.push(component);
150
- component.pause();
151
- }
152
- });
153
- this.ctx.suspend().then();
154
- };
155
- SoundSystem.prototype.stopAll = function () {
156
- this.components.forEach(function (component) {
157
- if (component.playing) {
158
- component.stop();
159
- }
160
- });
161
- this.pausedComponents = [];
162
- this.ctx.suspend().then();
163
- };
164
- SoundSystem.prototype.init = function () {
165
- this.setupAudioContext();
166
- };
167
- SoundSystem.prototype.update = function () {
168
- var e_1, _a;
169
- var changes = this.componentObserver.clear();
170
- try {
171
- for (var changes_1 = __values(changes), changes_1_1 = changes_1.next(); !changes_1_1.done; changes_1_1 = changes_1.next()) {
172
- var changed = changes_1_1.value;
173
- this.componentChanged(changed);
174
- }
175
- }
176
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
177
- finally {
178
- try {
179
- if (changes_1_1 && !changes_1_1.done && (_a = changes_1.return)) _a.call(changes_1);
180
- }
181
- finally { if (e_1) throw e_1.error; }
45
+ resumeAll() {
46
+ sound.resumeAll();
47
+ }
48
+ pauseAll() {
49
+ sound.pauseAll();
50
+ }
51
+ stopAll() {
52
+ sound.stopAll();
53
+ }
54
+ update() {
55
+ const changes = this.componentObserver.clear();
56
+ for (const changed of changes) {
57
+ this.componentChanged(changed);
182
58
  }
183
- };
184
- SoundSystem.prototype.onResume = function () {
59
+ }
60
+ onResume() {
185
61
  if (!this.autoPauseAndStart) {
186
62
  return;
187
63
  }
188
64
  this.resumeAll();
189
- };
190
- SoundSystem.prototype.onPause = function () {
65
+ }
66
+ onPause() {
191
67
  if (!this.autoPauseAndStart) {
192
68
  return;
193
69
  }
194
70
  this.pauseAll();
195
- };
196
- SoundSystem.prototype.onDestroy = function () {
197
- this.components.forEach(function (component) {
71
+ }
72
+ onDestroy() {
73
+ this.components.forEach(component => {
198
74
  component.onDestroy();
199
75
  });
200
76
  this.components = [];
201
- if (this.ctx) {
202
- this.gainNode.disconnect();
203
- this.gainNode = null;
204
- this.ctx.close();
205
- this.ctx = null;
206
- }
207
- };
208
- SoundSystem.prototype.componentChanged = function (changed) {
209
- return __awaiter(this, void 0, void 0, function () {
210
- return __generator(this, function (_a) {
211
- if (changed.componentName !== 'Sound')
212
- return [2];
213
- if (changed.type === OBSERVER_TYPE.ADD) {
214
- this.add(changed);
215
- }
216
- return [2];
217
- });
218
- });
219
- };
220
- SoundSystem.prototype.setupAudioContext = function () {
221
- try {
222
- var AudioContext_1 = window.AudioContext || window.webkitAudioContext;
223
- this.ctx = new AudioContext_1();
224
- }
225
- catch (error) {
226
- console.error(error);
227
- if (this.onError) {
228
- this.onError(error);
229
- }
230
- }
231
- if (!this.ctx) {
232
- return;
233
- }
234
- this.gainNode =
235
- typeof this.ctx.createGain === 'undefined' ? this.ctx.createGainNode() : this.ctx.createGain();
236
- this.gainNode.gain.setValueAtTime(this.muted ? 0 : this.volume, this.ctx.currentTime);
237
- this.gainNode.connect(this.ctx.destination);
238
- this.unlockAudio();
239
- };
240
- SoundSystem.prototype.unlockAudio = function () {
241
- var _this = this;
242
- if (!this.ctx || !this.audioLocked) {
243
- return;
244
- }
245
- var unlock = function () {
246
- if (_this.ctx) {
247
- var removeListenerFn = function () {
248
- document.body.removeEventListener('touchstart', unlock);
249
- document.body.removeEventListener('touchend', unlock);
250
- document.body.removeEventListener('click', unlock);
251
- };
252
- _this.ctx.resume().then(removeListenerFn, removeListenerFn);
77
+ sound.removeAll();
78
+ }
79
+ componentChanged(changed) {
80
+ return __awaiter(this, void 0, void 0, function* () {
81
+ if (changed.componentName !== 'Sound')
82
+ return;
83
+ if (changed.type === OBSERVER_TYPE.ADD) {
84
+ this.add(changed);
253
85
  }
254
- };
255
- document.body.addEventListener('touchstart', unlock);
256
- document.body.addEventListener('touchend', unlock);
257
- document.body.addEventListener('click', unlock);
258
- };
259
- SoundSystem.prototype.add = function (changed) {
260
- var _a;
261
- return __awaiter(this, void 0, void 0, function () {
262
- var component, config, audio, _b, _c, error_1;
263
- return __generator(this, function (_d) {
264
- switch (_d.label) {
265
- case 0:
266
- component = changed.component;
267
- this.components.push(component);
268
- _d.label = 1;
269
- case 1:
270
- _d.trys.push([1, 5, , 6]);
271
- config = component.config;
272
- component.state = 'loading';
273
- return [4, resource.getResource(config.resource)];
274
- case 2:
275
- audio = _d.sent();
276
- if (!(!this.audioBufferCache[audio.name] && ((_a = audio === null || audio === void 0 ? void 0 : audio.data) === null || _a === void 0 ? void 0 : _a.audio))) return [3, 4];
277
- _b = this.audioBufferCache;
278
- _c = audio.name;
279
- return [4, this.decodeAudioData(audio.data.audio, audio.name)];
280
- case 3:
281
- _b[_c] = _d.sent();
282
- _d.label = 4;
283
- case 4:
284
- if (this.audioBufferCache[audio.name]) {
285
- component.systemContext = this.ctx;
286
- component.systemDestination = this.gainNode;
287
- component.onload(this.audioBufferCache[audio.name]);
288
- }
289
- return [3, 6];
290
- case 5:
291
- error_1 = _d.sent();
292
- if (this.onError) {
293
- this.onError(error_1);
294
- }
295
- return [3, 6];
296
- case 6: return [2];
297
- }
298
- });
299
86
  });
300
- };
301
- SoundSystem.prototype.decodeAudioData = function (arraybuffer, name) {
302
- var _this = this;
303
- if (this.decodeAudioPromiseMap[name]) {
304
- return this.decodeAudioPromiseMap[name];
305
- }
306
- var promise = new Promise(function (resolve, reject) {
307
- if (!_this.ctx) {
308
- reject(new Error('No audio support'));
309
- }
310
- var success = function (decodedData) {
311
- if (_this.decodeAudioPromiseMap[name]) {
312
- delete _this.decodeAudioPromiseMap[name];
313
- }
314
- if (decodedData) {
315
- resolve(decodedData);
87
+ }
88
+ add(changed) {
89
+ var _a, _b;
90
+ return __awaiter(this, void 0, void 0, function* () {
91
+ const component = changed.component;
92
+ this.components.push(component);
93
+ try {
94
+ const { config } = component;
95
+ component.state = 'loading';
96
+ const audio = yield resource.getResource(config.resource);
97
+ if (!this.audioBufferCache[audio.name] && ((_a = audio === null || audio === void 0 ? void 0 : audio.data) === null || _a === void 0 ? void 0 : _a.audio)) {
98
+ this.audioBufferCache[audio.name] = (_b = audio === null || audio === void 0 ? void 0 : audio.data) === null || _b === void 0 ? void 0 : _b.audio;
316
99
  }
317
- else {
318
- reject(new Error("Error decoding audio " + name));
100
+ if (this.audioBufferCache[audio.name]) {
101
+ component.onload(this.audioBufferCache[audio.name]);
319
102
  }
320
- };
321
- var error = function (err) {
322
- if (_this.decodeAudioPromiseMap[name]) {
323
- delete _this.decodeAudioPromiseMap[name];
103
+ }
104
+ catch (error) {
105
+ if (this.onError) {
106
+ this.onError(error);
324
107
  }
325
- reject(new Error(err + ". arrayBuffer byteLength: " + (arraybuffer ? arraybuffer.byteLength : 0)));
326
- };
327
- var promise = _this.ctx.decodeAudioData(arraybuffer, success, error);
328
- if (promise instanceof Promise) {
329
- promise.catch(function (err) {
330
- reject(new Error("catch " + err + ", arrayBuffer byteLength: " + (arraybuffer ? arraybuffer.byteLength : 0)));
331
- });
332
108
  }
333
109
  });
334
- this.decodeAudioPromiseMap[name] = promise;
335
- return promise;
336
- };
337
- SoundSystem.systemName = 'SoundSystem';
338
- SoundSystem = __decorate([
339
- decorators.componentObserver({
340
- Sound: [],
341
- })
342
- ], SoundSystem);
343
- return SoundSystem;
344
- }(System));
110
+ }
111
+ };
112
+ SoundSystem.systemName = 'SoundSystem';
113
+ SoundSystem = __decorate([
114
+ decorators.componentObserver({
115
+ Sound: [],
116
+ })
117
+ ], SoundSystem);
345
118
  var SoundSystem$1 = SoundSystem;
346
119
 
347
- var Sound = (function (_super) {
348
- __extends(Sound, _super);
349
- function Sound() {
350
- var _this = _super !== null && _super.apply(this, arguments) || this;
351
- _this.state = 'unloaded';
352
- _this.config = {
120
+ class Sound extends Component {
121
+ constructor() {
122
+ super(...arguments);
123
+ this.state = 'unloaded';
124
+ this.config = {
353
125
  resource: '',
354
126
  autoplay: false,
355
127
  muted: false,
356
128
  volume: 1,
357
129
  loop: false,
358
130
  seek: 0,
131
+ speed: 1,
359
132
  };
360
- _this.playTime = 0;
361
- _this.startTime = 0;
362
- _this.duration = 0;
363
- _this.actionQueue = [];
364
- return _this;
133
+ this.actionQueue = [];
134
+ this.startTime = 0;
365
135
  }
366
- Object.defineProperty(Sound.prototype, "muted", {
367
- get: function () {
368
- return this.gainNode ? this.gainNode.gain.value === 0 : false;
369
- },
370
- set: function (v) {
371
- if (!this.gainNode) {
372
- return;
373
- }
374
- this.gainNode.gain.setValueAtTime(v ? 0 : this.config.volume, 0);
375
- },
376
- enumerable: false,
377
- configurable: true
378
- });
379
- Object.defineProperty(Sound.prototype, "volume", {
380
- get: function () {
381
- return this.gainNode ? this.gainNode.gain.value : 1;
382
- },
383
- set: function (v) {
384
- if (typeof v !== 'number' || v < 0 || v > 1) {
385
- return;
386
- }
387
- this.config.volume = v;
388
- if (!this.gainNode) {
389
- return;
390
- }
391
- this.gainNode.gain.setValueAtTime(v, 0);
392
- },
393
- enumerable: false,
394
- configurable: true
395
- });
396
- Sound.prototype.init = function (obj) {
136
+ get systemContext() {
137
+ return sound.context;
138
+ }
139
+ get playing() {
140
+ if (!this.buffer)
141
+ return false;
142
+ return this.buffer.isPlaying;
143
+ }
144
+ get muted() {
145
+ var _a;
146
+ return ((_a = this.buffer) === null || _a === void 0 ? void 0 : _a.muted) || false;
147
+ }
148
+ set muted(v) {
149
+ if (this.buffer)
150
+ this.buffer.muted = v;
151
+ }
152
+ get volume() {
153
+ var _a;
154
+ return ((_a = this.buffer) === null || _a === void 0 ? void 0 : _a.volume) || 0;
155
+ }
156
+ set volume(v) {
157
+ if (this.buffer)
158
+ this.buffer.volume = v;
159
+ }
160
+ init(obj) {
397
161
  if (!obj) {
398
162
  return;
399
163
  }
@@ -401,111 +165,51 @@ var Sound = (function (_super) {
401
165
  if (this.config.autoplay) {
402
166
  this.actionQueue.push(this.play.bind(this));
403
167
  }
404
- };
405
- Sound.prototype.play = function () {
406
- var _this = this;
168
+ }
169
+ play() {
407
170
  if (this.state !== 'loaded') {
408
171
  this.actionQueue.push(this.play.bind(this));
409
172
  }
410
- this.destroySource();
411
- this.createSource();
412
- if (!this.sourceNode) {
173
+ if (!this.buffer)
413
174
  return;
414
- }
415
- var when = this.systemContext.currentTime;
416
- var offset = this.config.seek;
417
- var duration = this.config.duration;
418
- this.sourceNode.start(0, offset, duration);
419
- this.startTime = when;
420
- this.playTime = when - offset;
421
- this.paused = false;
422
- this.playing = true;
423
- this.resetConfig();
424
- this.endedListener = function () {
425
- if (!_this.sourceNode) {
426
- return;
427
- }
428
- if (_this.config.onEnd) {
429
- _this.config.onEnd();
430
- }
431
- if (_this.playing) {
432
- _this.destroySource();
433
- }
434
- };
435
- this.sourceNode.addEventListener('ended', this.endedListener);
436
- };
437
- Sound.prototype.pause = function () {
438
- if (this.state !== 'loaded') {
439
- this.actionQueue.push(this.pause.bind(this));
440
- }
441
- if (this.paused || !this.playing) {
175
+ this.startTime = this.systemContext.currentTime;
176
+ this.buffer.play();
177
+ }
178
+ resume() {
179
+ if (!this.buffer)
442
180
  return;
443
- }
444
- this.paused = true;
445
- this.playing = false;
446
- this.config.seek = this.getCurrentTime();
447
- this.destroySource();
448
- };
449
- Sound.prototype.stop = function () {
450
- if (this.state !== 'loaded') {
451
- this.actionQueue.push(this.stop.bind(this));
452
- }
453
- if (!this.paused && !this.playing) {
181
+ this.buffer.resume();
182
+ }
183
+ pause() {
184
+ if (!this.buffer)
454
185
  return;
455
- }
456
- this.playing = false;
457
- this.paused = false;
458
- this.destroySource();
459
- this.resetConfig();
460
- };
461
- Sound.prototype.onload = function (buffer) {
186
+ this.buffer.pause();
187
+ }
188
+ stop() {
189
+ if (!this.buffer)
190
+ return;
191
+ this.buffer.stop();
192
+ }
193
+ onload(buffer) {
462
194
  this.state = 'loaded';
463
195
  this.buffer = buffer;
464
- this.duration = this.buffer.duration;
465
- this.actionQueue.forEach(function (action) { return action(); });
196
+ this.buffer.muted = this.config.muted;
197
+ this.buffer.volume = this.config.volume;
198
+ this.buffer.loop = this.config.loop;
199
+ this.buffer.speed = this.config.speed;
200
+ this.actionQueue.forEach(action => action());
466
201
  this.actionQueue.length = 0;
467
- };
468
- Sound.prototype.onDestroy = function () {
202
+ }
203
+ onDestroy() {
469
204
  this.actionQueue.length = 0;
470
- this.destroySource();
471
- };
472
- Sound.prototype.resetConfig = function () {
473
- this.config.seek = 0;
474
- };
475
- Sound.prototype.getCurrentTime = function () {
476
- if (this.config.loop && this.duration > 0) {
477
- return (this.systemContext.currentTime - this.playTime) % this.duration;
478
- }
479
- return this.systemContext.currentTime - this.playTime;
480
- };
481
- Sound.prototype.createSource = function () {
482
- if (!this.systemContext || this.state !== 'loaded') {
483
- return;
484
- }
485
- this.sourceNode = this.systemContext.createBufferSource();
486
- this.sourceNode.buffer = this.buffer;
487
- this.sourceNode.loop = this.config.loop;
488
- if (!this.gainNode) {
489
- this.gainNode = this.systemContext.createGain();
490
- this.gainNode.connect(this.systemDestination);
491
- Object.assign(this, this.config);
492
- }
493
- this.sourceNode.connect(this.gainNode);
494
- };
495
- Sound.prototype.destroySource = function () {
496
- if (!this.sourceNode)
497
- return;
498
- this.sourceNode.removeEventListener('ended', this.endedListener);
499
- this.sourceNode.stop();
500
- this.sourceNode.disconnect();
501
- this.sourceNode = null;
502
205
  this.startTime = 0;
503
- this.playTime = 0;
504
- this.playing = false;
505
- };
506
- Sound.componentName = 'Sound';
507
- return Sound;
508
- }(Component));
206
+ if (this.buffer) {
207
+ this.buffer.destroy();
208
+ this.buffer = null;
209
+ }
210
+ }
211
+ }
212
+ Sound.componentName = 'Sound';
509
213
  var Sound$1 = Sound;
510
214
 
511
215
  export { Sound$1 as Sound, SoundSystem$1 as SoundSystem };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eva/plugin-sound",
3
- "version": "2.0.0-beta.1",
3
+ "version": "2.0.0-beta.11",
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.1",
22
- "eventemitter3": "^3.1.2"
21
+ "@eva/eva.js": "2.0.0-beta.11",
22
+ "eventemitter3": "^3.1.2",
23
+ "pixi.js": "^8.6.5",
24
+ "@pixi/sound": "^6.0.1"
23
25
  }
24
26
  }