@eva/plugin-sound 1.1.1-fix.2 → 1.1.1-fix.3

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,733 +1,519 @@
1
- function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
2
-
3
- window.EVA = window.EVA || {};
4
- window.EVA.plugin = window.EVA.plugin || {};
5
-
6
- var _EVA_IIFE_sound = function (exports, eva_js) {
7
- 'use strict';
8
-
9
- var _extendStatics = function extendStatics(d, b) {
10
- _extendStatics = Object.setPrototypeOf || {
11
- __proto__: []
12
- } instanceof Array && function (d, b) {
13
- d.__proto__ = b;
14
- } || function (d, b) {
15
- for (var p in b) {
16
- if (b.hasOwnProperty(p)) d[p] = b[p];
17
- }
18
- };
19
-
20
- return _extendStatics(d, b);
21
- };
22
-
23
- function __extends(d, b) {
24
- _extendStatics(d, b);
25
-
26
- function __() {
27
- this.constructor = d;
28
- }
29
-
30
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
31
- }
32
-
33
- function __decorate(decorators, target, key, desc) {
34
- var c = arguments.length,
35
- r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc,
36
- d;
37
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);else for (var i = decorators.length - 1; i >= 0; i--) {
38
- if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
39
- }
40
- return c > 3 && r && Object.defineProperty(target, key, r), r;
41
- }
42
-
43
- function __awaiter(thisArg, _arguments, P, generator) {
44
- function adopt(value) {
45
- return value instanceof P ? value : new P(function (resolve) {
46
- resolve(value);
47
- });
48
- }
49
-
50
- return new (P || (P = Promise))(function (resolve, reject) {
51
- function fulfilled(value) {
52
- try {
53
- step(generator.next(value));
54
- } catch (e) {
55
- reject(e);
56
- }
57
- }
58
-
59
- function rejected(value) {
60
- try {
61
- step(generator["throw"](value));
62
- } catch (e) {
63
- reject(e);
64
- }
65
- }
66
-
67
- function step(result) {
68
- result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
69
- }
70
-
71
- step((generator = generator.apply(thisArg, _arguments || [])).next());
72
- });
73
- }
74
-
75
- function __generator(thisArg, body) {
76
- var _ = {
77
- label: 0,
78
- sent: function sent() {
79
- if (t[0] & 1) throw t[1];
80
- return t[1];
81
- },
82
- trys: [],
83
- ops: []
84
- },
85
- f,
86
- y,
87
- t,
88
- g;
89
- return g = {
90
- next: verb(0),
91
- "throw": verb(1),
92
- "return": verb(2)
93
- }, typeof Symbol === "function" && (g[Symbol.iterator] = function () {
94
- return this;
95
- }), g;
96
-
97
- function verb(n) {
98
- return function (v) {
99
- return step([n, v]);
100
- };
101
- }
102
-
103
- function step(op) {
104
- if (f) throw new TypeError("Generator is already executing.");
105
-
106
- while (_) {
107
- try {
108
- 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;
109
- if (y = 0, t) op = [op[0] & 2, t.value];
110
-
111
- switch (op[0]) {
112
- case 0:
113
- case 1:
114
- t = op;
115
- break;
116
-
117
- case 4:
118
- _.label++;
119
- return {
120
- value: op[1],
121
- done: false
122
- };
123
-
124
- case 5:
125
- _.label++;
126
- y = op[1];
127
- op = [0];
128
- continue;
129
-
130
- case 7:
131
- op = _.ops.pop();
132
-
133
- _.trys.pop();
134
-
135
- continue;
136
-
137
- default:
138
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
139
- _ = 0;
140
- continue;
141
- }
142
-
143
- if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
144
- _.label = op[1];
145
- break;
146
- }
147
-
148
- if (op[0] === 6 && _.label < t[1]) {
149
- _.label = t[1];
150
- t = op;
151
- break;
152
- }
153
-
154
- if (t && _.label < t[2]) {
155
- _.label = t[2];
156
-
157
- _.ops.push(op);
158
-
159
- break;
160
- }
161
-
162
- if (t[2]) _.ops.pop();
163
-
164
- _.trys.pop();
165
-
166
- continue;
167
- }
168
-
169
- op = body.call(thisArg, _);
170
- } catch (e) {
171
- op = [6, e];
172
- y = 0;
173
- } finally {
174
- f = t = 0;
175
- }
176
- }
177
-
178
- if (op[0] & 5) throw op[1];
179
- return {
180
- value: op[0] ? op[1] : void 0,
181
- done: true
182
- };
183
- }
184
- }
185
-
186
- function __values(o) {
187
- var s = typeof Symbol === "function" && Symbol.iterator,
188
- m = s && o[s],
189
- i = 0;
190
- if (m) return m.call(o);
191
- if (o && typeof o.length === "number") return {
192
- next: function next() {
193
- if (o && i >= o.length) o = void 0;
194
- return {
195
- value: o && o[i++],
196
- done: !o
197
- };
198
- }
199
- };
200
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
201
- }
202
-
203
- var SoundSystem = function (_super) {
204
- __extends(SoundSystem, _super);
205
-
206
- function SoundSystem(obj) {
207
- var _this = _super.call(this) || this;
208
-
209
- _this.autoPauseAndStart = true;
210
- _this.components = [];
211
- _this.pausedComponents = [];
212
- _this.audioBufferCache = {};
213
- _this.decodeAudioPromiseMap = {};
214
-
215
- _extends(_this, obj);
216
-
217
- return _this;
1
+ (function (global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@eva/eva.js')) :
3
+ typeof define === 'function' && define.amd ? define(['exports', '@eva/eva.js'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.EVA = global.EVA || {}, global.EVA.plugin = global.EVA.plugin || {}, global.EVA.plugin.sound = {}), global.EVA));
5
+ }(this, (function (exports, eva_js) { 'use strict';
6
+
7
+ /*! *****************************************************************************
8
+ Copyright (c) Microsoft Corporation.
9
+
10
+ Permission to use, copy, modify, and/or distribute this software for any
11
+ purpose with or without fee is hereby granted.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
14
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
16
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
17
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
18
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
19
+ PERFORMANCE OF THIS SOFTWARE.
20
+ ***************************************************************************** */
21
+ /* global Reflect, Promise */
22
+
23
+ var extendStatics = function(d, b) {
24
+ extendStatics = Object.setPrototypeOf ||
25
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
26
+ function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
27
+ return extendStatics(d, b);
28
+ };
29
+
30
+ function __extends(d, b) {
31
+ extendStatics(d, b);
32
+ function __() { this.constructor = d; }
33
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
34
+ }
35
+
36
+ function __decorate(decorators, target, key, desc) {
37
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
38
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
39
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
40
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
41
+ }
42
+
43
+ function __awaiter(thisArg, _arguments, P, generator) {
44
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
45
+ return new (P || (P = Promise))(function (resolve, reject) {
46
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
47
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
48
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
49
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
50
+ });
51
+ }
52
+
53
+ function __generator(thisArg, body) {
54
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
55
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
56
+ function verb(n) { return function (v) { return step([n, v]); }; }
57
+ function step(op) {
58
+ if (f) throw new TypeError("Generator is already executing.");
59
+ while (_) try {
60
+ 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;
61
+ if (y = 0, t) op = [op[0] & 2, t.value];
62
+ switch (op[0]) {
63
+ case 0: case 1: t = op; break;
64
+ case 4: _.label++; return { value: op[1], done: false };
65
+ case 5: _.label++; y = op[1]; op = [0]; continue;
66
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
67
+ default:
68
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
69
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
70
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
71
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
72
+ if (t[2]) _.ops.pop();
73
+ _.trys.pop(); continue;
74
+ }
75
+ op = body.call(thisArg, _);
76
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
77
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
78
+ }
79
+ }
80
+
81
+ function __values(o) {
82
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
83
+ if (m) return m.call(o);
84
+ if (o && typeof o.length === "number") return {
85
+ next: function () {
86
+ if (o && i >= o.length) o = void 0;
87
+ return { value: o && o[i++], done: !o };
88
+ }
89
+ };
90
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
218
91
  }
219
92
 
220
- Object.defineProperty(SoundSystem.prototype, "muted", {
221
- get: function get() {
222
- return this.gainNode ? this.gainNode.gain.value === 0 : false;
223
- },
224
- set: function set(v) {
225
- if (!this.gainNode) {
226
- return;
227
- }
228
-
229
- this.gainNode.gain.setValueAtTime(v ? 0 : 1, 0);
230
- },
231
- enumerable: false,
232
- configurable: true
233
- });
234
- Object.defineProperty(SoundSystem.prototype, "volume", {
235
- get: function get() {
236
- return this.gainNode ? this.gainNode.gain.value : 1;
237
- },
238
- set: function set(v) {
239
- if (!this.gainNode || typeof v !== 'number' || v < 0 || v > 1) {
240
- return;
241
- }
242
-
243
- this.gainNode.gain.setValueAtTime(v, 0);
244
- },
245
- enumerable: false,
246
- configurable: true
247
- });
248
- Object.defineProperty(SoundSystem.prototype, "audioLocked", {
249
- get: function get() {
250
- if (!this.ctx) {
251
- return true;
252
- }
253
-
254
- return this.ctx.state !== 'running';
255
- },
256
- enumerable: false,
257
- configurable: true
258
- });
259
-
260
- SoundSystem.prototype.resumeAll = function () {
261
- var _this = this;
262
-
263
- var handleResume = function handleResume() {
264
- _this.pausedComponents.forEach(function (component) {
265
- component.play();
266
- });
267
-
268
- _this.pausedComponents = [];
269
- };
270
-
271
- this.ctx.resume().then(handleResume, handleResume);
272
- };
273
-
274
- SoundSystem.prototype.pauseAll = function () {
275
- var _this = this;
276
-
277
- this.components.forEach(function (component) {
278
- if (component.playing) {
279
- _this.pausedComponents.push(component);
280
-
281
- component.pause();
282
- }
283
- });
284
- this.ctx.suspend().then();
285
- };
286
-
287
- SoundSystem.prototype.stopAll = function () {
288
- this.components.forEach(function (component) {
289
- if (component.playing) {
290
- component.stop();
291
- }
292
- });
293
- this.pausedComponents = [];
294
- this.ctx.suspend().then();
295
- };
296
-
297
- SoundSystem.prototype.init = function () {
298
- this.setupAudioContext();
299
- };
300
-
301
- SoundSystem.prototype.update = function () {
302
- var e_1, _a;
303
-
304
- var changes = this.componentObserver.clear();
305
-
306
- try {
307
- for (var changes_1 = __values(changes), changes_1_1 = changes_1.next(); !changes_1_1.done; changes_1_1 = changes_1.next()) {
308
- var changed = changes_1_1.value;
309
- this.componentChanged(changed);
310
- }
311
- } catch (e_1_1) {
312
- e_1 = {
313
- error: e_1_1
314
- };
315
- } finally {
316
- try {
317
- if (changes_1_1 && !changes_1_1.done && (_a = changes_1.return)) _a.call(changes_1);
318
- } finally {
319
- if (e_1) throw e_1.error;
320
- }
321
- }
322
- };
323
-
324
- SoundSystem.prototype.onResume = function () {
325
- if (!this.autoPauseAndStart) {
326
- return;
327
- }
328
-
329
- this.resumeAll();
330
- };
331
-
332
- SoundSystem.prototype.onPause = function () {
333
- if (!this.autoPauseAndStart) {
334
- return;
335
- }
336
-
337
- this.pauseAll();
338
- };
339
-
340
- SoundSystem.prototype.onDestroy = function () {
341
- this.components.forEach(function (component) {
342
- component.onDestroy();
343
- });
344
- this.components = [];
345
-
346
- if (this.ctx) {
347
- this.gainNode.disconnect();
348
- this.gainNode = null;
349
- this.ctx.close();
350
- this.ctx = null;
351
- }
352
- };
353
-
354
- SoundSystem.prototype.componentChanged = function (changed) {
355
- return __awaiter(this, void 0, void 0, function () {
356
- return __generator(this, function (_a) {
357
- if (changed.componentName !== 'Sound') return [2];
358
-
359
- if (changed.type === eva_js.OBSERVER_TYPE.ADD) {
360
- this.add(changed);
361
- }
362
-
363
- return [2];
364
- });
365
- });
366
- };
367
-
368
- SoundSystem.prototype.setupAudioContext = function () {
369
- try {
370
- var AudioContext_1 = window.AudioContext || window.webkitAudioContext;
371
- this.ctx = new AudioContext_1();
372
- } catch (error) {
373
- console.error(error);
374
-
375
- if (this.onError) {
376
- this.onError(error);
377
- }
378
- }
379
-
380
- if (!this.ctx) {
381
- return;
382
- }
383
-
384
- this.gainNode = typeof this.ctx.createGain === 'undefined' ? this.ctx.createGainNode() : this.ctx.createGain();
385
- this.gainNode.gain.setValueAtTime(this.muted ? 0 : this.volume, this.ctx.currentTime);
386
- this.gainNode.connect(this.ctx.destination);
387
- this.unlockAudio();
388
- };
389
-
390
- SoundSystem.prototype.unlockAudio = function () {
391
- var _this = this;
392
-
393
- if (!this.ctx || !this.audioLocked) {
394
- return;
395
- }
396
-
397
- var unlock = function unlock() {
398
- if (_this.ctx) {
399
- var removeListenerFn = function removeListenerFn() {
400
- document.body.removeEventListener('touchstart', unlock);
401
- document.body.removeEventListener('touchend', unlock);
402
- document.body.removeEventListener('click', unlock);
403
- };
404
-
405
- _this.ctx.resume().then(removeListenerFn, removeListenerFn);
406
- }
407
- };
408
-
409
- document.body.addEventListener('touchstart', unlock);
410
- document.body.addEventListener('touchend', unlock);
411
- document.body.addEventListener('click', unlock);
412
- };
413
-
414
- SoundSystem.prototype.add = function (changed) {
415
- var _a;
416
-
417
- return __awaiter(this, void 0, void 0, function () {
418
- var component, config, audio, _b, _c, error_1;
419
-
420
- return __generator(this, function (_d) {
421
- switch (_d.label) {
422
- case 0:
423
- component = changed.component;
424
- this.components.push(component);
425
- _d.label = 1;
426
-
427
- case 1:
428
- _d.trys.push([1, 5,, 6]);
429
-
430
- config = component.config;
431
- component.state = 'loading';
432
- return [4, eva_js.resource.getResource(config.resource)];
433
-
434
- case 2:
435
- audio = _d.sent();
436
- 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];
437
- _b = this.audioBufferCache;
438
- _c = audio.name;
439
- return [4, this.decodeAudioData(audio.data.audio, audio.name)];
440
-
441
- case 3:
442
- _b[_c] = _d.sent();
443
- _d.label = 4;
444
-
445
- case 4:
446
- if (this.audioBufferCache[audio.name]) {
447
- component.systemContext = this.ctx;
448
- component.systemDestination = this.gainNode;
449
- component.onload(this.audioBufferCache[audio.name]);
450
- }
451
-
452
- return [3, 6];
453
-
454
- case 5:
455
- error_1 = _d.sent();
456
-
457
- if (this.onError) {
458
- this.onError(error_1);
459
- }
460
-
461
- return [3, 6];
462
-
463
- case 6:
464
- return [2];
465
- }
466
- });
467
- });
468
- };
469
-
470
- SoundSystem.prototype.decodeAudioData = function (arraybuffer, name) {
471
- var _this = this;
472
-
473
- if (this.decodeAudioPromiseMap[name]) {
474
- return this.decodeAudioPromiseMap[name];
475
- }
476
-
477
- var promise = new Promise(function (resolve, reject) {
478
- if (!_this.ctx) {
479
- reject(new Error('No audio support'));
480
- }
481
-
482
- var success = function success(decodedData) {
483
- if (_this.decodeAudioPromiseMap[name]) {
484
- delete _this.decodeAudioPromiseMap[name];
485
- }
486
-
487
- if (decodedData) {
488
- resolve(decodedData);
489
- } else {
490
- reject(new Error("Error decoding audio " + name));
491
- }
492
- };
493
-
494
- var error = function error(err) {
495
- if (_this.decodeAudioPromiseMap[name]) {
496
- delete _this.decodeAudioPromiseMap[name];
497
- }
498
-
499
- reject(new Error(err + ". arrayBuffer byteLength: " + (arraybuffer ? arraybuffer.byteLength : 0)));
500
- };
501
-
502
- var promise = _this.ctx.decodeAudioData(arraybuffer, success, error);
503
-
504
- if (promise instanceof Promise) {
505
- promise.catch(function (err) {
506
- reject(new Error("catch " + err + ", arrayBuffer byteLength: " + (arraybuffer ? arraybuffer.byteLength : 0)));
507
- });
508
- }
509
- });
510
- this.decodeAudioPromiseMap[name] = promise;
511
- return promise;
512
- };
513
-
514
- SoundSystem.systemName = 'SoundSystem';
515
- SoundSystem = __decorate([eva_js.decorators.componentObserver({
516
- Sound: []
517
- })], SoundSystem);
518
- return SoundSystem;
519
- }(eva_js.System);
520
-
521
- var SoundSystem$1 = SoundSystem;
522
-
523
- var Sound = function (_super) {
524
- __extends(Sound, _super);
525
-
526
- function Sound() {
527
- var _this = _super !== null && _super.apply(this, arguments) || this;
528
-
529
- _this.state = 'unloaded';
530
- _this.config = {
531
- resource: '',
532
- autoplay: false,
533
- muted: false,
534
- volume: 1,
535
- loop: false,
536
- seek: 0
537
- };
538
- _this.playTime = 0;
539
- _this.startTime = 0;
540
- _this.duration = 0;
541
- _this.actionQueue = [];
542
- return _this;
543
- }
544
-
545
- Object.defineProperty(Sound.prototype, "muted", {
546
- get: function get() {
547
- return this.gainNode ? this.gainNode.gain.value === 0 : false;
548
- },
549
- set: function set(v) {
550
- if (!this.gainNode) {
551
- return;
552
- }
553
-
554
- this.gainNode.gain.setValueAtTime(v ? 0 : this.config.volume, 0);
555
- },
556
- enumerable: false,
557
- configurable: true
558
- });
559
- Object.defineProperty(Sound.prototype, "volume", {
560
- get: function get() {
561
- return this.gainNode ? this.gainNode.gain.value : 1;
562
- },
563
- set: function set(v) {
564
- if (typeof v !== 'number' || v < 0 || v > 1) {
565
- return;
566
- }
567
-
568
- this.config.volume = v;
569
-
570
- if (!this.gainNode) {
571
- return;
572
- }
573
-
574
- this.gainNode.gain.setValueAtTime(v, 0);
575
- },
576
- enumerable: false,
577
- configurable: true
578
- });
579
-
580
- Sound.prototype.init = function (obj) {
581
- if (!obj) {
582
- return;
583
- }
584
-
585
- _extends(this.config, obj);
586
-
587
- if (this.config.autoplay) {
588
- this.actionQueue.push(this.play.bind(this));
589
- }
590
- };
591
-
592
- Sound.prototype.play = function () {
593
- var _this = this;
594
-
595
- if (this.state !== 'loaded') {
596
- this.actionQueue.push(this.play.bind(this));
597
- }
598
-
599
- this.destroySource();
600
- this.createSource();
601
-
602
- if (!this.sourceNode) {
603
- return;
604
- }
605
-
606
- var when = this.systemContext.currentTime;
607
- var offset = this.config.seek;
608
- var duration = this.config.duration;
609
- this.sourceNode.start(0, offset, duration);
610
- this.startTime = when;
611
- this.playTime = when - offset;
612
- this.paused = false;
613
- this.playing = true;
614
- this.resetConfig();
615
-
616
- this.endedListener = function () {
617
- if (!_this.sourceNode) {
618
- return;
619
- }
620
-
621
- if (_this.config.onEnd) {
622
- _this.config.onEnd();
623
- }
624
-
625
- if (_this.playing) {
626
- _this.destroySource();
627
- }
628
- };
629
-
630
- this.sourceNode.addEventListener('ended', this.endedListener);
631
- };
632
-
633
- Sound.prototype.pause = function () {
634
- if (this.state !== 'loaded') {
635
- this.actionQueue.push(this.pause.bind(this));
636
- }
637
-
638
- if (this.paused || !this.playing) {
639
- return;
640
- }
641
-
642
- this.paused = true;
643
- this.playing = false;
644
- this.config.seek = this.getCurrentTime();
645
- this.destroySource();
646
- };
647
-
648
- Sound.prototype.stop = function () {
649
- if (this.state !== 'loaded') {
650
- this.actionQueue.push(this.stop.bind(this));
651
- }
652
-
653
- if (!this.paused && !this.playing) {
654
- return;
655
- }
656
-
657
- this.playing = false;
658
- this.paused = false;
659
- this.destroySource();
660
- this.resetConfig();
661
- };
662
-
663
- Sound.prototype.onload = function (buffer) {
664
- this.state = 'loaded';
665
- this.buffer = buffer;
666
- this.duration = this.buffer.duration;
667
- this.actionQueue.forEach(function (action) {
668
- return action();
669
- });
670
- this.actionQueue.length = 0;
671
- };
672
-
673
- Sound.prototype.onDestroy = function () {
674
- this.actionQueue.length = 0;
675
- this.destroySource();
676
- };
677
-
678
- Sound.prototype.resetConfig = function () {
679
- this.config.seek = 0;
680
- };
681
-
682
- Sound.prototype.getCurrentTime = function () {
683
- if (this.config.loop && this.duration > 0) {
684
- return (this.systemContext.currentTime - this.playTime) % this.duration;
685
- }
686
-
687
- return this.systemContext.currentTime - this.playTime;
688
- };
689
-
690
- Sound.prototype.createSource = function () {
691
- if (!this.systemContext || this.state !== 'loaded') {
692
- return;
693
- }
694
-
695
- this.sourceNode = this.systemContext.createBufferSource();
696
- this.sourceNode.buffer = this.buffer;
697
- this.sourceNode.loop = this.config.loop;
698
-
699
- if (!this.gainNode) {
700
- this.gainNode = this.systemContext.createGain();
701
- this.gainNode.connect(this.systemDestination);
702
-
703
- _extends(this, this.config);
704
- }
705
-
706
- this.sourceNode.connect(this.gainNode);
707
- };
708
-
709
- Sound.prototype.destroySource = function () {
710
- if (!this.sourceNode) return;
711
- this.sourceNode.removeEventListener('ended', this.endedListener);
712
- this.sourceNode.stop();
713
- this.sourceNode.disconnect();
714
- this.sourceNode = null;
715
- this.startTime = 0;
716
- this.playTime = 0;
717
- this.playing = false;
718
- };
719
-
720
- Sound.componentName = 'Sound';
721
- return Sound;
722
- }(eva_js.Component);
723
-
724
- var Sound$1 = Sound;
725
- exports.Sound = Sound$1;
726
- exports.SoundSystem = SoundSystem$1;
727
- Object.defineProperty(exports, '__esModule', {
728
- value: true
729
- });
730
- return exports;
731
- }({}, EVA);
732
-
733
- window.EVA.plugin.sound = window.EVA.plugin.sound || _EVA_IIFE_sound;
93
+ var SoundSystem = (function (_super) {
94
+ __extends(SoundSystem, _super);
95
+ function SoundSystem(obj) {
96
+ var _this = _super.call(this) || this;
97
+ _this.autoPauseAndStart = true;
98
+ _this.components = [];
99
+ _this.pausedComponents = [];
100
+ _this.audioBufferCache = {};
101
+ _this.decodeAudioPromiseMap = {};
102
+ Object.assign(_this, obj);
103
+ return _this;
104
+ }
105
+ Object.defineProperty(SoundSystem.prototype, "muted", {
106
+ get: function () {
107
+ return this.gainNode ? this.gainNode.gain.value === 0 : false;
108
+ },
109
+ set: function (v) {
110
+ if (!this.gainNode) {
111
+ return;
112
+ }
113
+ this.gainNode.gain.setValueAtTime(v ? 0 : 1, 0);
114
+ },
115
+ enumerable: false,
116
+ configurable: true
117
+ });
118
+ Object.defineProperty(SoundSystem.prototype, "volume", {
119
+ get: function () {
120
+ return this.gainNode ? this.gainNode.gain.value : 1;
121
+ },
122
+ set: function (v) {
123
+ if (!this.gainNode || typeof v !== 'number' || v < 0 || v > 1) {
124
+ return;
125
+ }
126
+ this.gainNode.gain.setValueAtTime(v, 0);
127
+ },
128
+ enumerable: false,
129
+ configurable: true
130
+ });
131
+ Object.defineProperty(SoundSystem.prototype, "audioLocked", {
132
+ get: function () {
133
+ if (!this.ctx) {
134
+ return true;
135
+ }
136
+ return this.ctx.state !== 'running';
137
+ },
138
+ enumerable: false,
139
+ configurable: true
140
+ });
141
+ SoundSystem.prototype.resumeAll = function () {
142
+ var _this = this;
143
+ var handleResume = function () {
144
+ _this.pausedComponents.forEach(function (component) {
145
+ component.play();
146
+ });
147
+ _this.pausedComponents = [];
148
+ };
149
+ this.ctx.resume().then(handleResume, handleResume);
150
+ };
151
+ SoundSystem.prototype.pauseAll = function () {
152
+ var _this = this;
153
+ this.components.forEach(function (component) {
154
+ if (component.playing) {
155
+ _this.pausedComponents.push(component);
156
+ component.pause();
157
+ }
158
+ });
159
+ this.ctx.suspend().then();
160
+ };
161
+ SoundSystem.prototype.stopAll = function () {
162
+ this.components.forEach(function (component) {
163
+ if (component.playing) {
164
+ component.stop();
165
+ }
166
+ });
167
+ this.pausedComponents = [];
168
+ this.ctx.suspend().then();
169
+ };
170
+ SoundSystem.prototype.init = function () {
171
+ this.setupAudioContext();
172
+ };
173
+ SoundSystem.prototype.update = function () {
174
+ var e_1, _a;
175
+ var changes = this.componentObserver.clear();
176
+ try {
177
+ for (var changes_1 = __values(changes), changes_1_1 = changes_1.next(); !changes_1_1.done; changes_1_1 = changes_1.next()) {
178
+ var changed = changes_1_1.value;
179
+ this.componentChanged(changed);
180
+ }
181
+ }
182
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
183
+ finally {
184
+ try {
185
+ if (changes_1_1 && !changes_1_1.done && (_a = changes_1.return)) _a.call(changes_1);
186
+ }
187
+ finally { if (e_1) throw e_1.error; }
188
+ }
189
+ };
190
+ SoundSystem.prototype.onPlay = function () {
191
+ if (!this.autoPauseAndStart) {
192
+ return;
193
+ }
194
+ this.resumeAll();
195
+ };
196
+ SoundSystem.prototype.onPause = function () {
197
+ if (!this.autoPauseAndStart) {
198
+ return;
199
+ }
200
+ this.pauseAll();
201
+ };
202
+ SoundSystem.prototype.onDestroy = function () {
203
+ this.components.forEach(function (component) {
204
+ component.onDestroy();
205
+ });
206
+ this.components = [];
207
+ if (this.ctx) {
208
+ this.gainNode.disconnect();
209
+ this.gainNode = null;
210
+ this.ctx.close();
211
+ this.ctx = null;
212
+ }
213
+ };
214
+ SoundSystem.prototype.componentChanged = function (changed) {
215
+ return __awaiter(this, void 0, void 0, function () {
216
+ return __generator(this, function (_a) {
217
+ if (changed.componentName !== 'Sound') {
218
+ return [2];
219
+ }
220
+ if (changed.type === eva_js.OBSERVER_TYPE.ADD) {
221
+ this.add(changed);
222
+ }
223
+ return [2];
224
+ });
225
+ });
226
+ };
227
+ SoundSystem.prototype.setupAudioContext = function () {
228
+ try {
229
+ var AudioContext_1 = window.AudioContext || window.webkitAudioContext;
230
+ this.ctx = new AudioContext_1();
231
+ }
232
+ catch (error) {
233
+ console.error(error);
234
+ if (this.onError) {
235
+ this.onError(error);
236
+ }
237
+ }
238
+ if (!this.ctx) {
239
+ return;
240
+ }
241
+ this.gainNode =
242
+ typeof this.ctx.createGain === 'undefined' ? this.ctx.createGainNode() : this.ctx.createGain();
243
+ this.gainNode.gain.setValueAtTime(this.muted ? 0 : this.volume, this.ctx.currentTime);
244
+ this.gainNode.connect(this.ctx.destination);
245
+ this.unlockAudio();
246
+ };
247
+ SoundSystem.prototype.unlockAudio = function () {
248
+ var _this = this;
249
+ if (!this.ctx || !this.audioLocked) {
250
+ return;
251
+ }
252
+ var unlock = function () {
253
+ if (_this.ctx) {
254
+ var removeListenerFn = function () {
255
+ document.body.removeEventListener('touchstart', unlock);
256
+ document.body.removeEventListener('touchend', unlock);
257
+ document.body.removeEventListener('click', unlock);
258
+ };
259
+ _this.ctx.resume().then(removeListenerFn, removeListenerFn);
260
+ }
261
+ };
262
+ document.body.addEventListener('touchstart', unlock);
263
+ document.body.addEventListener('touchend', unlock);
264
+ document.body.addEventListener('click', unlock);
265
+ };
266
+ SoundSystem.prototype.add = function (changed) {
267
+ return __awaiter(this, void 0, void 0, function () {
268
+ var component, config, audio, _a, _b, error_1;
269
+ return __generator(this, function (_c) {
270
+ switch (_c.label) {
271
+ case 0:
272
+ component = changed.component;
273
+ this.components.push(component);
274
+ _c.label = 1;
275
+ case 1:
276
+ _c.trys.push([1, 5, , 6]);
277
+ config = component.config;
278
+ component.state = 'loading';
279
+ return [4, eva_js.resource.getResource(config.resource)];
280
+ case 2:
281
+ audio = _c.sent();
282
+ if (!!this.audioBufferCache[audio.name]) return [3, 4];
283
+ _a = this.audioBufferCache;
284
+ _b = audio.name;
285
+ return [4, this.decodeAudioData(audio.data.audio, audio.name)];
286
+ case 3:
287
+ _a[_b] = _c.sent();
288
+ _c.label = 4;
289
+ case 4:
290
+ if (this.audioBufferCache[audio.name]) {
291
+ component.systemContext = this.ctx;
292
+ component.systemDestination = this.gainNode;
293
+ component.onload(this.audioBufferCache[audio.name]);
294
+ }
295
+ return [3, 6];
296
+ case 5:
297
+ error_1 = _c.sent();
298
+ console.error(error_1);
299
+ if (this.onError) {
300
+ this.onError(error_1);
301
+ }
302
+ return [3, 6];
303
+ case 6: return [2];
304
+ }
305
+ });
306
+ });
307
+ };
308
+ SoundSystem.prototype.decodeAudioData = function (arraybuffer, name) {
309
+ var _this = this;
310
+ if (this.decodeAudioPromiseMap[name]) {
311
+ return this.decodeAudioPromiseMap[name];
312
+ }
313
+ var promise = new Promise(function (resolve, reject) {
314
+ if (!_this.ctx) {
315
+ reject(new Error('No audio support'));
316
+ }
317
+ var error = function (err) {
318
+ if (_this.decodeAudioPromiseMap[name]) {
319
+ delete _this.decodeAudioPromiseMap[name];
320
+ }
321
+ reject(new Error(err + ". arrayBuffer byteLength: " + (arraybuffer ? arraybuffer.byteLength : 0)));
322
+ };
323
+ var success = function (decodedData) {
324
+ if (_this.decodeAudioPromiseMap[name]) {
325
+ delete _this.decodeAudioPromiseMap[name];
326
+ }
327
+ if (decodedData) {
328
+ resolve(decodedData);
329
+ }
330
+ else {
331
+ reject(new Error("Error decoding audio " + name));
332
+ }
333
+ };
334
+ _this.ctx.decodeAudioData(arraybuffer, success, error);
335
+ });
336
+ this.decodeAudioPromiseMap[name] = promise;
337
+ return promise;
338
+ };
339
+ SoundSystem.systemName = 'SoundSystem';
340
+ SoundSystem = __decorate([
341
+ eva_js.decorators.componentObserver({
342
+ Sound: [],
343
+ })
344
+ ], SoundSystem);
345
+ return SoundSystem;
346
+ }(eva_js.System));
347
+ var SoundSystem$1 = SoundSystem;
348
+
349
+ var Sound = (function (_super) {
350
+ __extends(Sound, _super);
351
+ function Sound() {
352
+ var _this = _super !== null && _super.apply(this, arguments) || this;
353
+ _this.state = 'unloaded';
354
+ _this.config = {
355
+ resource: '',
356
+ autoplay: false,
357
+ muted: false,
358
+ volume: 1,
359
+ loop: false,
360
+ seek: 0,
361
+ };
362
+ _this.playTime = 0;
363
+ _this.startTime = 0;
364
+ _this.duration = 0;
365
+ _this.actionQueue = [];
366
+ return _this;
367
+ }
368
+ Object.defineProperty(Sound.prototype, "muted", {
369
+ get: function () {
370
+ return this.gainNode ? this.gainNode.gain.value === 0 : false;
371
+ },
372
+ set: function (v) {
373
+ if (!this.gainNode) {
374
+ return;
375
+ }
376
+ this.gainNode.gain.setValueAtTime(v ? 0 : this.config.volume, 0);
377
+ },
378
+ enumerable: false,
379
+ configurable: true
380
+ });
381
+ Object.defineProperty(Sound.prototype, "volume", {
382
+ get: function () {
383
+ return this.gainNode ? this.gainNode.gain.value : 1;
384
+ },
385
+ set: function (v) {
386
+ if (typeof v !== 'number' || v < 0 || v > 1) {
387
+ return;
388
+ }
389
+ this.config.volume = v;
390
+ if (!this.gainNode) {
391
+ return;
392
+ }
393
+ this.gainNode.gain.setValueAtTime(v, 0);
394
+ },
395
+ enumerable: false,
396
+ configurable: true
397
+ });
398
+ Sound.prototype.init = function (obj) {
399
+ if (!obj) {
400
+ return;
401
+ }
402
+ Object.assign(this.config, obj);
403
+ if (this.config.autoplay) {
404
+ this.actionQueue.push(this.play.bind(this));
405
+ }
406
+ };
407
+ Sound.prototype.play = function () {
408
+ var _this = this;
409
+ if (this.state !== 'loaded') {
410
+ this.actionQueue.push(this.play.bind(this));
411
+ }
412
+ this.destroySource();
413
+ this.createSource();
414
+ if (!this.sourceNode) {
415
+ return;
416
+ }
417
+ var when = this.systemContext.currentTime;
418
+ var offset = this.config.seek;
419
+ var duration = this.config.duration;
420
+ this.sourceNode.start(0, offset, duration);
421
+ this.startTime = when;
422
+ this.playTime = when - offset;
423
+ this.paused = false;
424
+ this.playing = true;
425
+ this.resetConfig();
426
+ this.endedListener = function () {
427
+ if (!_this.sourceNode) {
428
+ return;
429
+ }
430
+ if (_this.config.onEnd) {
431
+ _this.config.onEnd();
432
+ }
433
+ if (_this.playing) {
434
+ _this.destroySource();
435
+ }
436
+ };
437
+ this.sourceNode.addEventListener('ended', this.endedListener);
438
+ };
439
+ Sound.prototype.pause = function () {
440
+ if (this.state !== 'loaded') {
441
+ this.actionQueue.push(this.pause.bind(this));
442
+ }
443
+ if (this.paused || !this.playing) {
444
+ return;
445
+ }
446
+ this.paused = true;
447
+ this.playing = false;
448
+ this.config.seek = this.getCurrentTime();
449
+ this.destroySource();
450
+ };
451
+ Sound.prototype.stop = function () {
452
+ if (this.state !== 'loaded') {
453
+ this.actionQueue.push(this.stop.bind(this));
454
+ }
455
+ if (!this.paused && !this.playing) {
456
+ return;
457
+ }
458
+ this.playing = false;
459
+ this.paused = false;
460
+ this.destroySource();
461
+ this.resetConfig();
462
+ };
463
+ Sound.prototype.onload = function (buffer) {
464
+ this.state = 'loaded';
465
+ this.buffer = buffer;
466
+ this.duration = this.buffer.duration;
467
+ this.actionQueue.forEach(function (action) { return action(); });
468
+ this.actionQueue.length = 0;
469
+ };
470
+ Sound.prototype.onDestroy = function () {
471
+ this.actionQueue.length = 0;
472
+ this.destroySource();
473
+ };
474
+ Sound.prototype.resetConfig = function () {
475
+ this.config.seek = 0;
476
+ };
477
+ Sound.prototype.getCurrentTime = function () {
478
+ if (this.config.loop && this.duration > 0) {
479
+ return (this.systemContext.currentTime - this.playTime) % this.duration;
480
+ }
481
+ return this.systemContext.currentTime - this.playTime;
482
+ };
483
+ Sound.prototype.createSource = function () {
484
+ if (!this.systemContext || this.state !== 'loaded') {
485
+ return;
486
+ }
487
+ this.sourceNode = this.systemContext.createBufferSource();
488
+ this.sourceNode.buffer = this.buffer;
489
+ this.sourceNode.loop = this.config.loop;
490
+ if (!this.gainNode) {
491
+ this.gainNode = this.systemContext.createGain();
492
+ this.gainNode.connect(this.systemDestination);
493
+ Object.assign(this, this.config);
494
+ }
495
+ this.sourceNode.connect(this.gainNode);
496
+ };
497
+ Sound.prototype.destroySource = function () {
498
+ if (!this.sourceNode)
499
+ return;
500
+ this.sourceNode.removeEventListener('ended', this.endedListener);
501
+ this.sourceNode.stop();
502
+ this.sourceNode.disconnect();
503
+ this.sourceNode = null;
504
+ this.startTime = 0;
505
+ this.playTime = 0;
506
+ this.playing = false;
507
+ };
508
+ Sound.componentName = 'Sound';
509
+ return Sound;
510
+ }(eva_js.Component));
511
+ var Sound$1 = Sound;
512
+
513
+ exports.Sound = Sound$1;
514
+ exports.SoundSystem = SoundSystem$1;
515
+
516
+ Object.defineProperty(exports, '__esModule', { value: true });
517
+
518
+ })));
519
+ //# sourceMappingURL=EVA.plugin.sound.js.map