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