@eva/plugin-sound 1.2.0 → 1.2.1
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 +731 -0
- package/dist/EVA.plugin.sound.min.js +1 -0
- package/package.json +2 -2
|
@@ -0,0 +1,731 @@
|
|
|
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
|
+
this.EVA = this.EVA || {};
|
|
4
|
+
this.EVA.plugin = this.EVA.plugin || {};
|
|
5
|
+
|
|
6
|
+
this.EVA.plugin.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;
|
|
218
|
+
}
|
|
219
|
+
|
|
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);
|
|
@@ -0,0 +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}).apply(this,arguments)}this.EVA=this.EVA||{},this.EVA.plugin=this.EVA.plugin||{},this.EVA.plugin.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])})(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)}(e.System),u=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);return t.Sound=u,t.SoundSystem=s,Object.defineProperty(t,"__esModule",{value:!0}),t}({},EVA);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eva/plugin-sound",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.1",
|
|
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.
|
|
21
|
+
"@eva/eva.js": "1.2.1",
|
|
22
22
|
"eventemitter3": "^3.1.2"
|
|
23
23
|
}
|
|
24
24
|
}
|