forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTML5AudioSound.js
More file actions
695 lines (659 loc) · 19.9 KB
/
Copy pathHTML5AudioSound.js
File metadata and controls
695 lines (659 loc) · 19.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2018 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var Class = require('../../utils/Class');
var BaseSound = require('../BaseSound');
/**
* @classdesc
* HTML5 Audio implementation of the sound.
*
* @class HTML5AudioSound
* @extends Phaser.Sound.BaseSound
* @memberOf Phaser.Sound
* @constructor
* @author Pavle Goloskokovic <pgoloskokovic@gmail.com> (http://prunegames.com)
* @since 3.0.0
*
* @param {Phaser.Sound.HTML5AudioSoundManager} manager - Reference to the current sound manager instance.
* @param {string} key - Asset key for the sound.
* @param {SoundConfig} [config={}] - An optional config object containing default sound settings.
*/
var HTML5AudioSound = new Class({
Extends: BaseSound,
initialize: function HTML5AudioSound (manager, key, config)
{
if (config === void 0) { config = {}; }
/**
* An array containing all HTML5 Audio tags that could be used for individual
* sound's playback. Number of instances depends on the config value passed
* to the Loader#audio method call, default is 1.
*
* @name Phaser.Sound.HTML5AudioSound#tags
* @type {HTMLAudioElement[]}
* @private
* @since 3.0.0
*/
this.tags = manager.game.cache.audio.get(key);
if (!this.tags)
{
// eslint-disable-next-line no-console
console.error('No audio loaded in cache with key: \'' + key + '\'!');
return;
}
/**
* Reference to an HTML5 Audio tag used for playing sound.
*
* @name Phaser.Sound.HTML5AudioSound#audio
* @type {HTMLAudioElement}
* @private
* @default null
* @since 3.0.0
*/
this.audio = null;
/**
* Timestamp as generated by the Request Animation Frame or SetTimeout
* representing the time at which the delayed sound playback should start.
* Set to 0 if sound playback is not delayed.
*
* @name Phaser.Sound.HTML5AudioSound#startTime
* @type {number}
* @private
* @default 0
* @since 3.0.0
*/
this.startTime = 0;
/**
* Audio tag's playback position recorded on previous
* update method call. Set to 0 if sound is not playing.
*
* @name Phaser.Sound.HTML5AudioSound#previousTime
* @type {number}
* @private
* @default 0
* @since 3.0.0
*/
this.previousTime = 0;
this.duration = this.tags[0].duration;
this.totalDuration = this.tags[0].duration;
BaseSound.call(this, manager, key, config);
},
/**
* Play this sound, or a marked section of it.
* It always plays the sound from the start. If you want to start playback from a specific time
* you can set 'seek' setting of the config object, provided to this call, to that value.
*
* @method Phaser.Sound.HTML5AudioSound#play
* @since 3.0.0
*
* @param {string} [markerName=''] - If you want to play a marker then provide the marker name here, otherwise omit it to play the full sound.
* @param {SoundConfig} [config] - Optional sound config object to be applied to this marker or entire sound if no marker name is provided. It gets memorized for future plays of current section of the sound.
*
* @return {boolean} Whether the sound started playing successfully.
*/
play: function (markerName, config)
{
if (this.manager.isLocked(this, 'play', [ markerName, config ]))
{
return false;
}
if (!BaseSound.prototype.play.call(this, markerName, config))
{
return false;
}
// \/\/\/ isPlaying = true, isPaused = false \/\/\/
if (!this.pickAndPlayAudioTag())
{
return false;
}
/**
* @event Phaser.Sound.HTML5AudioSound#play
* @param {Phaser.Sound.HTML5AudioSound} sound - Reference to the sound that emitted event.
*/
this.emit('play', this);
return true;
},
/**
* Pauses the sound.
*
* @method Phaser.Sound.HTML5AudioSound#pause
* @since 3.0.0
*
* @return {boolean} Whether the sound was paused successfully.
*/
pause: function ()
{
if (this.manager.isLocked(this, 'pause'))
{
return false;
}
if (this.startTime > 0)
{
return false;
}
if (!BaseSound.prototype.pause.call(this))
{
return false;
}
// \/\/\/ isPlaying = false, isPaused = true \/\/\/
this.currentConfig.seek = this.audio.currentTime
- (this.currentMarker ? this.currentMarker.start : 0);
this.stopAndReleaseAudioTag();
/**
* @event Phaser.Sound.HTML5AudioSound#pause
* @param {Phaser.Sound.HTML5AudioSound} sound - Reference to the sound that emitted event.
*/
this.emit('pause', this);
return true;
},
/**
* Resumes the sound.
*
* @method Phaser.Sound.HTML5AudioSound#resume
* @since 3.0.0
*
* @return {boolean} Whether the sound was resumed successfully.
*/
resume: function ()
{
if (this.manager.isLocked(this, 'resume'))
{
return false;
}
if (this.startTime > 0)
{
return false;
}
if (!BaseSound.prototype.resume.call(this))
{
return false;
}
// \/\/\/ isPlaying = true, isPaused = false \/\/\/
if (!this.pickAndPlayAudioTag())
{
return false;
}
/**
* @event Phaser.Sound.HTML5AudioSound#resume
* @param {Phaser.Sound.HTML5AudioSound} sound - Reference to the sound that emitted event.
*/
this.emit('resume', this);
return true;
},
/**
* Stop playing this sound.
*
* @method Phaser.Sound.HTML5AudioSound#stop
* @since 3.0.0
*
* @return {boolean} Whether the sound was stopped successfully.
*/
stop: function ()
{
if (this.manager.isLocked(this, 'stop'))
{
return false;
}
if (!BaseSound.prototype.stop.call(this))
{
return false;
}
// \/\/\/ isPlaying = false, isPaused = false \/\/\/
this.stopAndReleaseAudioTag();
/**
* @event Phaser.Sound.HTML5AudioSound#stop
* @param {Phaser.Sound.HTML5AudioSound} sound - Reference to the sound that emitted event.
*/
this.emit('stop', this);
return true;
},
/**
* Used internally to do what the name says.
*
* @method Phaser.Sound.HTML5AudioSound#pickAndPlayAudioTag
* @private
* @since 3.0.0
*
* @return {boolean} Whether the sound was assigned an audio tag successfully.
*/
pickAndPlayAudioTag: function ()
{
if (!this.pickAudioTag())
{
this.reset();
return false;
}
var seek = this.currentConfig.seek;
var delay = this.currentConfig.delay;
var offset = (this.currentMarker ? this.currentMarker.start : 0) + seek;
this.previousTime = offset;
this.audio.currentTime = offset;
this.applyConfig();
if (delay === 0)
{
this.startTime = 0;
if (this.audio.paused)
{
this.playCatchPromise();
}
}
else
{
this.startTime = window.performance.now() + delay * 1000;
if (!this.audio.paused)
{
this.audio.pause();
}
}
this.resetConfig();
return true;
},
/**
* This method performs the audio tag pooling logic. It first looks for
* unused audio tag to assign to this sound object. If there are no unused
* audio tags, based on HTML5AudioSoundManager#override property value, it
* looks for sound with most advanced playback and hijacks its audio tag or
* does nothing.
*
* @method Phaser.Sound.HTML5AudioSound#pickAudioTag
* @private
* @since 3.0.0
*
* @return {boolean} Whether the sound was assigned an audio tag successfully.
*/
pickAudioTag: function ()
{
if (this.audio)
{
return true;
}
for (var i = 0; i < this.tags.length; i++)
{
var audio = this.tags[i];
if (audio.dataset.used === 'false')
{
audio.dataset.used = 'true';
this.audio = audio;
return true;
}
}
if (!this.manager.override)
{
return false;
}
var otherSounds = [];
this.manager.forEachActiveSound(function (sound)
{
if (sound.key === this.key && sound.audio)
{
otherSounds.push(sound);
}
}, this);
otherSounds.sort(function (a1, a2)
{
if (a1.loop === a2.loop)
{
// sort by progress
return (a2.seek / a2.duration) - (a1.seek / a1.duration);
}
return a1.loop ? 1 : -1;
});
var selectedSound = otherSounds[0];
this.audio = selectedSound.audio;
selectedSound.reset();
selectedSound.audio = null;
selectedSound.startTime = 0;
selectedSound.previousTime = 0;
return true;
},
/**
* Method used for playing audio tag and catching possible exceptions
* thrown from rejected Promise returned from play method call.
*
* @method Phaser.Sound.HTML5AudioSound#playCatchPromise
* @private
* @since 3.0.0
*/
playCatchPromise: function ()
{
var playPromise = this.audio.play();
if (playPromise)
{
// eslint-disable-next-line no-unused-vars
playPromise.catch(function (reason)
{
console.warn(reason);
});
}
},
/**
* Used internally to do what the name says.
*
* @method Phaser.Sound.HTML5AudioSound#stopAndReleaseAudioTag
* @private
* @since 3.0.0
*/
stopAndReleaseAudioTag: function ()
{
this.audio.pause();
this.audio.dataset.used = 'false';
this.audio = null;
this.startTime = 0;
this.previousTime = 0;
},
/**
* Method used internally to reset sound state, usually when stopping sound
* or when hijacking audio tag from another sound.
*
* @method Phaser.Sound.HTML5AudioSound#reset
* @private
* @since 3.0.0
*/
reset: function ()
{
BaseSound.prototype.stop.call(this);
},
/**
* Method used internally by sound manager for pausing sound if
* Phaser.Sound.HTML5AudioSoundManager#pauseOnBlur is set to true.
*
* @method Phaser.Sound.HTML5AudioSoundManager#onBlur
* @private
* @since 3.0.0
*/
onBlur: function ()
{
this.isPlaying = false;
this.isPaused = true;
this.currentConfig.seek = this.audio.currentTime -
(this.currentMarker ? this.currentMarker.start : 0);
this.currentConfig.delay = Math.max(0, (this.startTime - window.performance.now()) / 1000);
this.stopAndReleaseAudioTag();
},
/**
* Method used internally by sound manager for resuming sound if
* Phaser.Sound.HTML5AudioSoundManager#pauseOnBlur is set to true.
*
* @method Phaser.Sound.HTML5AudioSound#onFocus
* @private
* @since 3.0.0
*/
onFocus: function ()
{
this.isPlaying = true;
this.isPaused = false;
this.pickAndPlayAudioTag();
},
/**
* Update method called automatically by sound manager on every game step.
*
* @method Phaser.Sound.HTML5AudioSound#update
* @protected
* @since 3.0.0
*
* @param {number} time - The current timestamp as generated by the Request Animation Frame or SetTimeout.
* @param {number} delta - The delta time elapsed since the last frame.
*/
// eslint-disable-next-line no-unused-vars
update: function (time, delta)
{
if (!this.isPlaying)
{
return;
}
// handling delayed playback
if (this.startTime > 0)
{
if (this.startTime < time - this.manager.audioPlayDelay)
{
this.audio.currentTime += Math.max(0, time - this.startTime) / 1000;
this.startTime = 0;
this.previousTime = this.audio.currentTime;
this.playCatchPromise();
}
return;
}
// handle looping and ending
var startTime = this.currentMarker ? this.currentMarker.start : 0;
var endTime = startTime + this.duration;
var currentTime = this.audio.currentTime;
if (this.currentConfig.loop)
{
if (currentTime >= endTime - this.manager.loopEndOffset)
{
this.audio.currentTime = startTime + Math.max(0, currentTime - endTime);
currentTime = this.audio.currentTime;
}
else if (currentTime < startTime)
{
this.audio.currentTime += startTime;
currentTime = this.audio.currentTime;
}
if (currentTime < this.previousTime)
{
/**
* @event Phaser.Sound.HTML5AudioSound#looped
* @param {Phaser.Sound.HTML5AudioSound} sound - Reference to the sound that emitted event.
*/
this.emit('looped', this);
}
}
else if (currentTime >= endTime)
{
this.reset();
this.stopAndReleaseAudioTag();
/**
* @event Phaser.Sound.HTML5AudioSound#ended
* @param {Phaser.Sound.HTML5AudioSound} sound - Reference to the sound that emitted event.
*/
this.emit('ended', this);
return;
}
this.previousTime = currentTime;
},
/**
* Calls Phaser.Sound.BaseSound#destroy method
* and cleans up all HTML5 Audio related stuff.
*
* @method Phaser.Sound.HTML5AudioSound#destroy
* @since 3.0.0
*/
destroy: function ()
{
BaseSound.prototype.destroy.call(this);
this.tags = null;
if (this.audio)
{
this.stopAndReleaseAudioTag();
}
},
/**
* Method used internally to determine mute setting of the sound.
*
* @method Phaser.Sound.HTML5AudioSound#setMute
* @private
* @since 3.0.0
*/
setMute: function ()
{
if (this.audio)
{
this.audio.muted = this.currentConfig.mute || this.manager.mute;
}
},
/**
* Method used internally to calculate total volume of the sound.
*
* @method Phaser.Sound.HTML5AudioSound#setVolume
* @private
* @since 3.0.0
*/
setVolume: function ()
{
if (this.audio)
{
this.audio.volume = this.currentConfig.volume * this.manager.volume;
}
},
/**
* Method used internally to calculate total playback rate of the sound.
*
* @method Phaser.Sound.HTML5AudioSound#setRate
* @protected
* @since 3.0.0
*/
setRate: function ()
{
BaseSound.prototype.setRate.call(this);
if (this.audio)
{
this.audio.playbackRate = this.totalRate;
}
}
});
Object.defineProperty(HTML5AudioSound.prototype, 'mute', {
get: function ()
{
return this.currentConfig.mute;
},
set: function (value)
{
this.currentConfig.mute = value;
if (this.manager.isLocked(this, 'mute', value))
{
return;
}
this.setMute();
/**
* @event Phaser.Sound.HTML5AudioSound#mute
* @param {Phaser.Sound.HTML5AudioSound} sound - Reference to the sound that emitted event.
* @param {boolean} value - An updated value of Phaser.Sound.HTML5AudioSound#mute property.
*/
this.emit('mute', this, value);
}
});
Object.defineProperty(HTML5AudioSound.prototype, 'volume', {
get: function ()
{
return this.currentConfig.volume;
},
set: function (value)
{
this.currentConfig.volume = value;
if (this.manager.isLocked(this, 'volume', value))
{
return;
}
this.setVolume();
/**
* @event Phaser.Sound.HTML5AudioSound#volume
* @param {Phaser.Sound.HTML5AudioSound} sound - Reference to the sound that emitted event.
* @param {number} value - An updated value of Phaser.Sound.HTML5AudioSound#volume property.
*/
this.emit('volume', this, value);
}
});
Object.defineProperty(HTML5AudioSound.prototype, 'rate', {
get: function ()
{
return Object.getOwnPropertyDescriptor(BaseSound.prototype, 'rate').get.call(this);
},
set: function (value)
{
this.currentConfig.rate = value;
if (this.manager.isLocked(this, 'rate', value))
{
return;
}
Object.getOwnPropertyDescriptor(BaseSound.prototype, 'rate').set.call(this, value);
}
});
Object.defineProperty(HTML5AudioSound.prototype, 'detune', {
get: function ()
{
return Object.getOwnPropertyDescriptor(BaseSound.prototype, 'detune').get.call(this);
},
set: function (value)
{
this.currentConfig.detune = value;
if (this.manager.isLocked(this, 'detune', value))
{
return;
}
Object.getOwnPropertyDescriptor(BaseSound.prototype, 'detune').set.call(this, value);
}
});
Object.defineProperty(HTML5AudioSound.prototype, 'seek', {
get: function ()
{
if (this.isPlaying)
{
return this.audio.currentTime -
(this.currentMarker ? this.currentMarker.start : 0);
}
else if (this.isPaused)
{
return this.currentConfig.seek;
}
else
{
return 0;
}
},
set: function (value)
{
if (this.manager.isLocked(this, 'seek', value))
{
return;
}
if (this.startTime > 0)
{
return;
}
if (this.isPlaying || this.isPaused)
{
value = Math.min(Math.max(0, value), this.duration);
if (this.isPlaying)
{
this.previousTime = value;
this.audio.currentTime = value;
}
else if (this.isPaused)
{
this.currentConfig.seek = value;
}
/**
* @event Phaser.Sound.HTML5AudioSound#seek
* @param {Phaser.Sound.HTML5AudioSound} sound - Reference to the sound that emitted event.
* @param {number} value - An updated value of Phaser.Sound.HTML5AudioSound#seek property.
*/
this.emit('seek', this, value);
}
}
});
Object.defineProperty(HTML5AudioSound.prototype, 'loop', {
get: function ()
{
return this.currentConfig.loop;
},
set: function (value)
{
this.currentConfig.loop = value;
if (this.manager.isLocked(this, 'loop', value))
{
return;
}
if (this.audio)
{
this.audio.loop = value;
}
/**
* @event Phaser.Sound.HTML5AudioSound#loop
* @param {Phaser.Sound.HTML5AudioSound} sound - Reference to the sound that emitted event.
* @param {boolean} value - An updated value of Phaser.Sound.HTML5AudioSound#loop property.
*/
this.emit('loop', this, value);
}
});
module.exports = HTML5AudioSound;