/
BaseSoundManager.js
708 lines (615 loc) · 18.9 KB
/
BaseSoundManager.js
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
696
697
698
699
700
701
702
703
704
705
706
707
708
/**
* @author Richard Davey <rich@photonstorm.com>
* @author Pavle Goloskokovic <pgoloskokovic@gmail.com> (http://prunegames.com)
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Class = require('../utils/Class');
var Clone = require('../utils/object/Clone');
var EventEmitter = require('eventemitter3');
var Events = require('./events');
var GameEvents = require('../core/events');
var NOOP = require('../utils/NOOP');
var GetAll = require('../utils/array/GetAll');
var GetFirst = require('../utils/array/GetFirst');
/**
* @classdesc
* Base class for other Sound Manager classes.
*
* @class BaseSoundManager
* @extends Phaser.Events.EventEmitter
* @memberof Phaser.Sound
* @constructor
* @since 3.0.0
*
* @param {Phaser.Game} game - Reference to the current game instance.
*
* @see Phaser.Sound.HTML5AudioSoundManager
* @see Phaser.Sound.NoAudioSoundManager
* @see Phaser.Sound.WebAudioSoundManager
*/
var BaseSoundManager = new Class({
Extends: EventEmitter,
initialize:
function BaseSoundManager (game)
{
EventEmitter.call(this);
/**
* Local reference to game.
*
* @name Phaser.Sound.BaseSoundManager#game
* @type {Phaser.Game}
* @readonly
* @since 3.0.0
*/
this.game = game;
/**
* Local reference to the JSON Cache, as used by Audio Sprites.
*
* @name Phaser.Sound.BaseSoundManager#jsonCache
* @type {Phaser.Cache.BaseCache}
* @readonly
* @since 3.7.0
*/
this.jsonCache = game.cache.json;
/**
* An array containing all added sounds.
*
* @name Phaser.Sound.BaseSoundManager#sounds
* @type {Phaser.Sound.BaseSound[]}
* @default []
* @private
* @since 3.0.0
*/
this.sounds = [];
/**
* Global mute setting.
*
* @name Phaser.Sound.BaseSoundManager#mute
* @type {boolean}
* @default false
* @since 3.0.0
*/
this.mute = false;
/**
* Global volume setting.
*
* @name Phaser.Sound.BaseSoundManager#volume
* @type {number}
* @default 1
* @since 3.0.0
*/
this.volume = 1;
/**
* Flag indicating if sounds should be paused when game looses focus,
* for instance when user switches to another tab/program/app.
*
* @name Phaser.Sound.BaseSoundManager#pauseOnBlur
* @type {boolean}
* @default true
* @since 3.0.0
*/
this.pauseOnBlur = true;
/**
* Property that actually holds the value of global playback rate.
*
* @name Phaser.Sound.BaseSoundManager#_rate
* @type {number}
* @private
* @default 1
* @since 3.0.0
*/
this._rate = 1;
/**
* Property that actually holds the value of global detune.
*
* @name Phaser.Sound.BaseSoundManager#_detune
* @type {number}
* @private
* @default 0
* @since 3.0.0
*/
this._detune = 0;
/**
* Mobile devices require sounds to be triggered from an explicit user action,
* such as a tap, before any sound can be loaded/played on a web page.
* Set to true if the audio system is currently locked awaiting user interaction.
*
* @name Phaser.Sound.BaseSoundManager#locked
* @type {boolean}
* @readonly
* @since 3.0.0
*/
this.locked = this.locked || false;
/**
* Flag used internally for handling when the audio system
* has been unlocked, if there ever was a need for it.
*
* @name Phaser.Sound.BaseSoundManager#unlocked
* @type {boolean}
* @default false
* @private
* @since 3.0.0
*/
this.unlocked = false;
game.events.on(GameEvents.BLUR, this.onGameBlur, this);
game.events.on(GameEvents.FOCUS, this.onGameFocus, this);
game.events.on(GameEvents.PRE_STEP, this.update, this);
game.events.once(GameEvents.DESTROY, this.destroy, this);
},
/**
* Adds a new sound into the sound manager.
*
* @method Phaser.Sound.BaseSoundManager#add
* @override
* @since 3.0.0
*
* @param {string} key - Asset key for the sound.
* @param {Phaser.Types.Sound.SoundConfig} [config] - An optional config object containing default sound settings.
*
* @return {Phaser.Sound.BaseSound} The new sound instance.
*/
add: NOOP,
/**
* Adds a new audio sprite sound into the sound manager.
* Audio Sprites are a combination of audio files and a JSON configuration.
* The JSON follows the format of that created by https://github.com/tonistiigi/audiosprite
*
* @method Phaser.Sound.BaseSoundManager#addAudioSprite
* @since 3.0.0
*
* @param {string} key - Asset key for the sound.
* @param {Phaser.Types.Sound.SoundConfig} [config] - An optional config object containing default sound settings.
*
* @return {(Phaser.Sound.HTML5AudioSound|Phaser.Sound.WebAudioSound)} The new audio sprite sound instance.
*/
addAudioSprite: function (key, config)
{
if (config === undefined) { config = {}; }
var sound = this.add(key, config);
sound.spritemap = this.jsonCache.get(key).spritemap;
for (var markerName in sound.spritemap)
{
if (!sound.spritemap.hasOwnProperty(markerName))
{
continue;
}
var markerConfig = Clone(config);
var marker = sound.spritemap[markerName];
markerConfig.loop = (marker.hasOwnProperty('loop')) ? marker.loop : false;
sound.addMarker({
name: markerName,
start: marker.start,
duration: marker.end - marker.start,
config: markerConfig
});
}
return sound;
},
/**
* Gets the first sound in the manager matching the given key, if any.
*
* @method Phaser.Sound.BaseSoundManager#get
* @since 3.23.0
*
* @param {string} key - Sound asset key.
*
* @return {?Phaser.Sound.BaseSound} - The sound, or null.
*/
get: function (key)
{
return GetFirst(this.sounds, 'key', key);
},
/**
* Gets any sounds in the manager matching the given key.
*
* @method Phaser.Sound.BaseSoundManager#getAll
* @since 3.23.0
*
* @param {string} key - Sound asset key.
*
* @return {Phaser.Sound.BaseSound[]} - The sounds, or an empty array.
*/
getAll: function (key)
{
return GetAll(this.sounds, 'key', key);
},
/**
* Adds a new sound to the sound manager and plays it.
* The sound will be automatically removed (destroyed) once playback ends.
* This lets you play a new sound on the fly without the need to keep a reference to it.
*
* @method Phaser.Sound.BaseSoundManager#play
* @listens Phaser.Sound.Events#COMPLETE
* @since 3.0.0
*
* @param {string} key - Asset key for the sound.
* @param {(Phaser.Types.Sound.SoundConfig|Phaser.Types.Sound.SoundMarker)} [extra] - An optional additional object containing settings to be applied to the sound. It could be either config or marker object.
*
* @return {boolean} Whether the sound started playing successfully.
*/
play: function (key, extra)
{
var sound = this.add(key);
sound.once(Events.COMPLETE, sound.destroy, sound);
if (extra)
{
if (extra.name)
{
sound.addMarker(extra);
return sound.play(extra.name);
}
else
{
return sound.play(extra);
}
}
else
{
return sound.play();
}
},
/**
* Adds a new audio sprite sound to the sound manager and plays it.
* The sprite will be automatically removed (destroyed) once playback ends.
* This lets you play a new sound on the fly without the need to keep a reference to it.
*
* @method Phaser.Sound.BaseSoundManager#playAudioSprite
* @listens Phaser.Sound.Events#COMPLETE
* @since 3.0.0
*
* @param {string} key - Asset key for the sound.
* @param {string} spriteName - The name of the sound sprite to play.
* @param {Phaser.Types.Sound.SoundConfig} [config] - An optional config object containing default sound settings.
*
* @return {boolean} Whether the audio sprite sound started playing successfully.
*/
playAudioSprite: function (key, spriteName, config)
{
var sound = this.addAudioSprite(key);
sound.once(Events.COMPLETE, sound.destroy, sound);
return sound.play(spriteName, config);
},
/**
* Removes a sound from the sound manager.
* The removed sound is destroyed before removal.
*
* @method Phaser.Sound.BaseSoundManager#remove
* @since 3.0.0
*
* @param {Phaser.Sound.BaseSound} sound - The sound object to remove.
*
* @return {boolean} True if the sound was removed successfully, otherwise false.
*/
remove: function (sound)
{
var index = this.sounds.indexOf(sound);
if (index !== -1)
{
sound.destroy();
this.sounds.splice(index, 1);
return true;
}
return false;
},
/**
* Removes all sounds from the manager, destroying the sounds.
*
* @method Phaser.Sound.BaseSoundManager#removeAll
* @since 3.23.0
*/
removeAll: function ()
{
this.sounds.forEach(function (sound)
{
sound.destroy();
});
this.sounds.length = 0;
},
/**
* Removes all sounds from the sound manager that have an asset key matching the given value.
* The removed sounds are destroyed before removal.
*
* @method Phaser.Sound.BaseSoundManager#removeByKey
* @since 3.0.0
*
* @param {string} key - The key to match when removing sound objects.
*
* @return {number} The number of matching sound objects that were removed.
*/
removeByKey: function (key)
{
var removed = 0;
for (var i = this.sounds.length - 1; i >= 0; i--)
{
var sound = this.sounds[i];
if (sound.key === key)
{
sound.destroy();
this.sounds.splice(i, 1);
removed++;
}
}
return removed;
},
/**
* Pauses all the sounds in the game.
*
* @method Phaser.Sound.BaseSoundManager#pauseAll
* @fires Phaser.Sound.Events#PAUSE_ALL
* @since 3.0.0
*/
pauseAll: function ()
{
this.forEachActiveSound(function (sound)
{
sound.pause();
});
this.emit(Events.PAUSE_ALL, this);
},
/**
* Resumes all the sounds in the game.
*
* @method Phaser.Sound.BaseSoundManager#resumeAll
* @fires Phaser.Sound.Events#RESUME_ALL
* @since 3.0.0
*/
resumeAll: function ()
{
this.forEachActiveSound(function (sound)
{
sound.resume();
});
this.emit(Events.RESUME_ALL, this);
},
/**
* Stops all the sounds in the game.
*
* @method Phaser.Sound.BaseSoundManager#stopAll
* @fires Phaser.Sound.Events#STOP_ALL
* @since 3.0.0
*/
stopAll: function ()
{
this.forEachActiveSound(function (sound)
{
sound.stop();
});
this.emit(Events.STOP_ALL, this);
},
/**
* Stops any sounds matching the given key.
*
* @method Phaser.Sound.BaseSoundManager#stopByKey
* @since 3.23.0
*
* @param {string} key - Sound asset key.
*
* @return {number} - How many sounds were stopped.
*/
stopByKey: function (key)
{
var stopped = 0;
this.getAll(key).forEach(function (sound)
{
if (sound.stop()) { stopped++; }
});
return stopped;
},
/**
* Method used internally for unlocking audio playback on devices that
* require user interaction before any sound can be played on a web page.
*
* Read more about how this issue is handled here in [this article](https://medium.com/@pgoloskokovic/unlocking-web-audio-the-smarter-way-8858218c0e09).
*
* @method Phaser.Sound.BaseSoundManager#unlock
* @override
* @protected
* @since 3.0.0
*/
unlock: NOOP,
/**
* Method used internally for pausing sound manager if
* Phaser.Sound.BaseSoundManager#pauseOnBlur is set to true.
*
* @method Phaser.Sound.BaseSoundManager#onBlur
* @override
* @protected
* @since 3.0.0
*/
onBlur: NOOP,
/**
* Method used internally for resuming sound manager if
* Phaser.Sound.BaseSoundManager#pauseOnBlur is set to true.
*
* @method Phaser.Sound.BaseSoundManager#onFocus
* @override
* @protected
* @since 3.0.0
*/
onFocus: NOOP,
/**
* Internal handler for Phaser.Core.Events#BLUR.
*
* @method Phaser.Sound.BaseSoundManager#onGameBlur
* @private
* @since 3.23.0
*/
onGameBlur: function ()
{
if (this.pauseOnBlur)
{
this.onBlur();
}
},
/**
* Internal handler for Phaser.Core.Events#FOCUS.
*
* @method Phaser.Sound.BaseSoundManager#onGameFocus
* @private
* @since 3.23.0
*/
onGameFocus: function ()
{
if (this.pauseOnBlur)
{
this.onFocus();
}
},
/**
* Update method called on every game step.
* Removes destroyed sounds and updates every active sound in the game.
*
* @method Phaser.Sound.BaseSoundManager#update
* @protected
* @fires Phaser.Sound.Events#UNLOCKED
* @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.
*/
update: function (time, delta)
{
if (this.unlocked)
{
this.unlocked = false;
this.locked = false;
this.emit(Events.UNLOCKED, this);
}
for (var i = this.sounds.length - 1; i >= 0; i--)
{
if (this.sounds[i].pendingRemove)
{
this.sounds.splice(i, 1);
}
}
this.sounds.forEach(function (sound)
{
sound.update(time, delta);
});
},
/**
* Destroys all the sounds in the game and all associated events.
*
* @method Phaser.Sound.BaseSoundManager#destroy
* @since 3.0.0
*/
destroy: function ()
{
this.game.events.off(GameEvents.BLUR, this.onGameBlur, this);
this.game.events.off(GameEvents.FOCUS, this.onGameFocus, this);
this.game.events.off(GameEvents.PRE_STEP, this.update, this);
this.removeAllListeners();
this.removeAll();
this.sounds.length = 0;
this.sounds = null;
this.game = null;
},
/**
* Method used internally for iterating only over active sounds and skipping sounds that are marked for removal.
*
* @method Phaser.Sound.BaseSoundManager#forEachActiveSound
* @private
* @since 3.0.0
*
* @param {Phaser.Types.Sound.EachActiveSoundCallback} callback - Callback function. (manager: Phaser.Sound.BaseSoundManager, sound: Phaser.Sound.BaseSound, index: number, sounds: Phaser.Manager.BaseSound[]) => void
* @param {*} [scope] - Callback context.
*/
forEachActiveSound: function (callback, scope)
{
var _this = this;
this.sounds.forEach(function (sound, index)
{
if (sound && !sound.pendingRemove)
{
callback.call(scope || _this, sound, index, _this.sounds);
}
});
},
/**
* Sets the global playback rate at which all the sounds will be played.
*
* For example, a value of 1.0 plays the audio at full speed, 0.5 plays the audio at half speed
* and 2.0 doubles the audios playback speed.
*
* @method Phaser.Sound.BaseSoundManager#setRate
* @fires Phaser.Sound.Events#GLOBAL_RATE
* @since 3.3.0
*
* @param {number} value - Global playback rate at which all the sounds will be played.
*
* @return {Phaser.Sound.BaseSoundManager} This Sound Manager.
*/
setRate: function (value)
{
this.rate = value;
return this;
},
/**
* Global playback rate at which all the sounds will be played.
* Value of 1.0 plays the audio at full speed, 0.5 plays the audio at half speed
* and 2.0 doubles the audio's playback speed.
*
* @name Phaser.Sound.BaseSoundManager#rate
* @type {number}
* @default 1
* @since 3.0.0
*/
rate: {
get: function ()
{
return this._rate;
},
set: function (value)
{
this._rate = value;
this.forEachActiveSound(function (sound)
{
sound.calculateRate();
});
this.emit(Events.GLOBAL_RATE, this, value);
}
},
/**
* Sets the global detuning of all sounds in [cents](https://en.wikipedia.org/wiki/Cent_%28music%29).
* The range of the value is -1200 to 1200, but we recommend setting it to [50](https://en.wikipedia.org/wiki/50_Cent).
*
* @method Phaser.Sound.BaseSoundManager#setDetune
* @fires Phaser.Sound.Events#GLOBAL_DETUNE
* @since 3.3.0
*
* @param {number} value - The range of the value is -1200 to 1200, but we recommend setting it to [50](https://en.wikipedia.org/wiki/50_Cent).
*
* @return {Phaser.Sound.BaseSoundManager} This Sound Manager.
*/
setDetune: function (value)
{
this.detune = value;
return this;
},
/**
* Global detuning of all sounds in [cents](https://en.wikipedia.org/wiki/Cent_%28music%29).
* The range of the value is -1200 to 1200, but we recommend setting it to [50](https://en.wikipedia.org/wiki/50_Cent).
*
* @name Phaser.Sound.BaseSoundManager#detune
* @type {number}
* @default 0
* @since 3.0.0
*/
detune: {
get: function ()
{
return this._detune;
},
set: function (value)
{
this._detune = value;
this.forEachActiveSound(function (sound)
{
sound.calculateRate();
});
this.emit(Events.GLOBAL_DETUNE, this, value);
}
}
});
module.exports = BaseSoundManager;