Skip to content

Commit 645c03f

Browse files
committed
Added setRate and setDetune methods
1 parent 8e7944a commit 645c03f

2 files changed

Lines changed: 106 additions & 24 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ A special mention must go to @orblazer for their outstanding assistance in helpi
3232
* Groups will now listen for a `destroy` event from any Game Object added to them, and if received will automatically remove that GameObject from the Group. Fix #3418 (thanks @hadikcz)
3333
* MatterGameObject is a new function, available via the Matter Factory in `this.matter.add.gameObject`, that will inject a Matter JS Body into any Game Object, such as a Text or TileSprite object.
3434
* Matter.SetBody and SetExistingBody will now set the origin of the Game Object to be the Matter JS sprite.xOffset and yOffset values, which will auto-center the Game Object to the origin of the body, regardless of shape.
35+
* SoundManager.setRate is a chainable method to allow you to set the global playback rate of all sounds in the SoundManager.
36+
* SoundManager.setDetune is a chainable method to allow you to set the global detuning of all sounds in the SoundManager.
3537

3638
### Bug Fixes
3739

src/sound/BaseSoundManager.js

Lines changed: 104 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
/**
22
* @author Richard Davey <rich@photonstorm.com>
3+
* @author Pavle Goloskokovic <pgoloskokovic@gmail.com> (http://prunegames.com)
34
* @copyright 2018 Photon Storm Ltd.
45
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
56
*/
7+
68
var Class = require('../utils/Class');
79
var EventEmitter = require('eventemitter3');
810
var NOOP = require('../utils/NOOP');
@@ -20,21 +22,26 @@ var NOOP = require('../utils/NOOP');
2022
* @classdesc
2123
* The sound manager is responsible for playing back audio via Web Audio API or HTML Audio tag as fallback.
2224
* The audio file type and the encoding of those files are extremely important.
25+
*
2326
* Not all browsers can play all audio formats.
27+
*
2428
* There is a good guide to what's supported [here](https://developer.mozilla.org/en-US/Apps/Fundamentals/Audio_and_video_delivery/Cross-browser_audio_basics#Audio_Codec_Support).
2529
*
2630
* @class BaseSoundManager
2731
* @extends EventEmitter
2832
* @memberOf Phaser.Sound
2933
* @constructor
30-
* @author Pavle Goloskokovic <pgoloskokovic@gmail.com> (http://prunegames.com)
3134
* @since 3.0.0
3235
*
3336
* @param {Phaser.Game} game - Reference to the current game instance.
3437
*/
3538
var BaseSoundManager = new Class({
39+
3640
Extends: EventEmitter,
37-
initialize: function BaseSoundManager (game)
41+
42+
initialize:
43+
44+
function BaseSoundManager (game)
3845
{
3946
EventEmitter.call(this);
4047

@@ -196,21 +203,26 @@ var BaseSoundManager = new Class({
196203
addAudioSprite: function (key, config)
197204
{
198205
var sound = this.add(key, config);
206+
199207
sound.spritemap = this.game.cache.json.get(key).spritemap;
208+
200209
for (var markerName in sound.spritemap)
201210
{
202211
if (!sound.spritemap.hasOwnProperty(markerName))
203212
{
204213
continue;
205214
}
215+
206216
var marker = sound.spritemap[markerName];
217+
207218
sound.addMarker({
208219
name: markerName,
209220
start: marker.start,
210221
duration: marker.end - marker.start,
211222
config: config
212223
});
213224
}
225+
214226
return sound;
215227
},
216228

@@ -229,12 +241,15 @@ var BaseSoundManager = new Class({
229241
play: function (key, extra)
230242
{
231243
var sound = this.add(key);
244+
232245
sound.once('ended', sound.destroy, sound);
246+
233247
if (extra)
234248
{
235249
if (extra.name)
236250
{
237251
sound.addMarker(extra);
252+
238253
return sound.play(extra.name);
239254
}
240255
else
@@ -264,7 +279,9 @@ var BaseSoundManager = new Class({
264279
playAudioSprite: function (key, spriteName, config)
265280
{
266281
var sound = this.addAudioSprite(key);
282+
267283
sound.once('ended', sound.destroy, sound);
284+
268285
return sound.play(spriteName, config);
269286
},
270287

@@ -282,12 +299,16 @@ var BaseSoundManager = new Class({
282299
remove: function (sound)
283300
{
284301
var index = this.sounds.indexOf(sound);
302+
285303
if (index !== -1)
286304
{
287305
sound.destroy();
306+
288307
this.sounds.splice(index, 1);
308+
289309
return true;
290310
}
311+
291312
return false;
292313
},
293314

@@ -305,23 +326,34 @@ var BaseSoundManager = new Class({
305326
removeByKey: function (key)
306327
{
307328
var removed = 0;
329+
308330
for (var i = this.sounds.length - 1; i >= 0; i--)
309331
{
310332
var sound = this.sounds[i];
333+
311334
if (sound.key === key)
312335
{
313336
sound.destroy();
337+
314338
this.sounds.splice(i, 1);
339+
315340
removed++;
316341
}
317342
}
343+
318344
return removed;
319345
},
320346

347+
/**
348+
* @event Phaser.Sound.BaseSoundManager#pauseall
349+
* @param {Phaser.Sound.BaseSoundManager} soundManager - Reference to the sound manager that emitted event.
350+
*/
351+
321352
/**
322353
* Pauses all the sounds in the game.
323354
*
324355
* @method Phaser.Sound.BaseSoundManager#pauseAll
356+
* @fires Phaser.Sound.BaseSoundManager#pauseall
325357
* @since 3.0.0
326358
*/
327359
pauseAll: function ()
@@ -331,17 +363,19 @@ var BaseSoundManager = new Class({
331363
sound.pause();
332364
});
333365

334-
/**
335-
* @event Phaser.Sound.BaseSoundManager#pauseall
336-
* @param {Phaser.Sound.BaseSoundManager} soundManager - Reference to the sound manager that emitted event.
337-
*/
338366
this.emit('pauseall', this);
339367
},
340368

369+
/**
370+
* @event Phaser.Sound.BaseSoundManager#resumeall
371+
* @param {Phaser.Sound.BaseSoundManager} soundManager - Reference to the sound manager that emitted event.
372+
*/
373+
341374
/**
342375
* Resumes all the sounds in the game.
343376
*
344377
* @method Phaser.Sound.BaseSoundManager#resumeAll
378+
* @fires Phaser.Sound.BaseSoundManager#resumeall
345379
* @since 3.0.0
346380
*/
347381
resumeAll: function ()
@@ -351,17 +385,19 @@ var BaseSoundManager = new Class({
351385
sound.resume();
352386
});
353387

354-
/**
355-
* @event Phaser.Sound.BaseSoundManager#resumeall
356-
* @param {Phaser.Sound.BaseSoundManager} soundManager - Reference to the sound manager that emitted event.
357-
*/
358388
this.emit('resumeall', this);
359389
},
360390

391+
/**
392+
* @event Phaser.Sound.BaseSoundManager#stopall
393+
* @param {Phaser.Sound.BaseSoundManager} soundManager - Reference to the sound manager that emitted event.
394+
*/
395+
361396
/**
362397
* Stops all the sounds in the game.
363398
*
364399
* @method Phaser.Sound.BaseSoundManager#stopAll
400+
* @fires Phaser.Sound.BaseSoundManager#stopall
365401
* @since 3.0.0
366402
*/
367403
stopAll: function ()
@@ -371,10 +407,6 @@ var BaseSoundManager = new Class({
371407
sound.stop();
372408
});
373409

374-
/**
375-
* @event Phaser.Sound.BaseSoundManager#stopall
376-
* @param {Phaser.Sound.BaseSoundManager} soundManager - Reference to the sound manager that emitted event.
377-
*/
378410
this.emit('stopall', this);
379411
},
380412

@@ -437,13 +469,15 @@ var BaseSoundManager = new Class({
437469
*/
438470
this.emit('unlocked', this);
439471
}
472+
440473
for (var i = this.sounds.length - 1; i >= 0; i--)
441474
{
442475
if (this.sounds[i].pendingRemove)
443476
{
444477
this.sounds.splice(i, 1);
445478
}
446479
}
480+
447481
this.sounds.forEach(function (sound)
448482
{
449483
sound.update(time, delta);
@@ -459,12 +493,15 @@ var BaseSoundManager = new Class({
459493
destroy: function ()
460494
{
461495
this.removeAllListeners();
496+
462497
this.forEachActiveSound(function (sound)
463498
{
464499
sound.destroy();
465500
});
501+
466502
this.sounds.length = 0;
467503
this.sounds = null;
504+
468505
this.game = null;
469506
},
470507

@@ -481,6 +518,7 @@ var BaseSoundManager = new Class({
481518
forEachActiveSound: function (callback, scope)
482519
{
483520
var _this = this;
521+
484522
this.sounds.forEach(function (sound, index)
485523
{
486524
if (!sound.pendingRemove)
@@ -490,6 +528,33 @@ var BaseSoundManager = new Class({
490528
});
491529
},
492530

531+
/**
532+
* @event Phaser.Sound.BaseSoundManager#rate
533+
* @param {Phaser.Sound.BaseSoundManager} soundManager - Reference to the sound manager that emitted event.
534+
* @param {number} value - An updated value of Phaser.Sound.BaseSoundManager#rate property.
535+
*/
536+
537+
/**
538+
* Sets the global playback rate at which all the sounds will be played.
539+
*
540+
* For example, a value of 1.0 plays the audio at full speed, 0.5 plays the audio at half speed
541+
* and 2.0 doubles the audios playback speed.
542+
*
543+
* @method Phaser.Sound.BaseSoundManager#setRate
544+
* @fires Phaser.Sound.BaseSoundManager#rate
545+
* @since 3.3.0
546+
*
547+
* @param {number} value - Global playback rate at which all the sounds will be played.
548+
*
549+
* @return {Phaser.Sound.BaseSoundManager} This Sound Manager.
550+
*/
551+
setRate: function (value)
552+
{
553+
this.rate = value;
554+
555+
return this;
556+
},
557+
493558
/**
494559
* Global playback rate at which all the sounds will be played.
495560
* Value of 1.0 plays the audio at full speed, 0.5 plays the audio at half speed
@@ -516,16 +581,36 @@ var BaseSoundManager = new Class({
516581
sound.setRate();
517582
});
518583

519-
/**
520-
* @event Phaser.Sound.BaseSoundManager#rate
521-
* @param {Phaser.Sound.BaseSoundManager} soundManager - Reference to the sound manager that emitted event.
522-
* @param {number} value - An updated value of Phaser.Sound.BaseSoundManager#rate property.
523-
*/
524584
this.emit('rate', this, value);
525585
}
526586

527587
},
528588

589+
/**
590+
* Sets the global detuning of all sounds in [cents](https://en.wikipedia.org/wiki/Cent_%28music%29).
591+
* The range of the value is -1200 to 1200, but we recommend setting it to [50](https://en.wikipedia.org/wiki/50_Cent).
592+
*
593+
* @method Phaser.Sound.BaseSoundManager#setDetune
594+
* @fires Phaser.Sound.BaseSoundManager#detune
595+
* @since 3.3.0
596+
*
597+
* @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).
598+
*
599+
* @return {Phaser.Sound.BaseSoundManager} This Sound Manager.
600+
*/
601+
setDetune: function (value)
602+
{
603+
this.detune = value;
604+
605+
return this;
606+
},
607+
608+
/**
609+
* @event Phaser.Sound.BaseSoundManager#detune
610+
* @param {Phaser.Sound.BaseSoundManager} soundManager - Reference to the sound manager that emitted event.
611+
* @param {number} value - An updated value of Phaser.Sound.BaseSoundManager#detune property.
612+
*/
613+
529614
/**
530615
* Global detuning of all sounds in [cents](https://en.wikipedia.org/wiki/Cent_%28music%29).
531616
* The range of the value is -1200 to 1200, but we recommend setting it to [50](https://en.wikipedia.org/wiki/50_Cent).
@@ -551,11 +636,6 @@ var BaseSoundManager = new Class({
551636
sound.setRate();
552637
});
553638

554-
/**
555-
* @event Phaser.Sound.BaseSoundManager#detune
556-
* @param {Phaser.Sound.BaseSoundManager} soundManager - Reference to the sound manager that emitted event.
557-
* @param {number} value - An updated value of Phaser.Sound.BaseSoundManager#detune property.
558-
*/
559639
this.emit('detune', this, value);
560640
}
561641

0 commit comments

Comments
 (0)