2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2018-03-21 14:40:07 +00:00
|
|
|
* @author Pavle Goloskokovic <pgoloskokovic@gmail.com> (http://prunegames.com)
|
2020-01-15 12:07:09 +00:00
|
|
|
* @copyright 2020 Photon Storm Ltd.
|
2019-05-10 15:15:04 +00:00
|
|
|
* @license {@link https://opensource.org/licenses/MIT|MIT License}
|
2018-02-12 16:01:20 +00:00
|
|
|
*/
|
2018-03-21 14:40:07 +00:00
|
|
|
|
2017-11-09 13:37:41 +00:00
|
|
|
var Class = require('../utils/Class');
|
2018-05-04 01:38:01 +00:00
|
|
|
var Clone = require('../utils/object/Clone');
|
2018-01-12 17:09:09 +00:00
|
|
|
var EventEmitter = require('eventemitter3');
|
2019-01-17 17:20:54 +00:00
|
|
|
var Events = require('./events');
|
|
|
|
var GameEvents = require('../core/events');
|
2018-02-12 12:25:30 +00:00
|
|
|
var NOOP = require('../utils/NOOP');
|
2020-03-24 22:08:32 +00:00
|
|
|
var GetAll = require('../utils/array/GetAll');
|
|
|
|
var GetFirst = require('../utils/array/GetFirst');
|
2018-01-26 14:35:09 +00:00
|
|
|
|
2018-02-12 12:25:30 +00:00
|
|
|
/**
|
|
|
|
* @classdesc
|
2020-01-13 17:19:21 +00:00
|
|
|
* Base class for other Sound Manager classes.
|
2018-02-12 12:25:30 +00:00
|
|
|
*
|
2018-02-12 15:18:31 +00:00
|
|
|
* @class BaseSoundManager
|
2018-03-28 14:04:09 +00:00
|
|
|
* @extends Phaser.Events.EventEmitter
|
2018-10-10 09:49:13 +00:00
|
|
|
* @memberof Phaser.Sound
|
2018-02-12 12:25:30 +00:00
|
|
|
* @constructor
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {Phaser.Game} game - Reference to the current game instance.
|
2020-01-13 17:19:21 +00:00
|
|
|
*
|
|
|
|
* @see Phaser.Sound.HTML5AudioSoundManager
|
|
|
|
* @see Phaser.Sound.NoAudioSoundManager
|
|
|
|
* @see Phaser.Sound.WebAudioSoundManager
|
2018-01-06 16:38:17 +00:00
|
|
|
*/
|
2017-11-10 12:05:29 +00:00
|
|
|
var BaseSoundManager = new Class({
|
2018-03-21 14:40:07 +00:00
|
|
|
|
2018-01-12 17:09:09 +00:00
|
|
|
Extends: EventEmitter,
|
2018-03-21 14:40:07 +00:00
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
|
|
|
function BaseSoundManager (game)
|
2018-01-26 14:35:09 +00:00
|
|
|
{
|
2018-01-12 17:09:09 +00:00
|
|
|
EventEmitter.call(this);
|
2018-01-26 14:35:09 +00:00
|
|
|
|
2017-11-10 18:05:26 +00:00
|
|
|
/**
|
2017-11-13 18:39:32 +00:00
|
|
|
* Local reference to game.
|
2017-11-16 14:23:35 +00:00
|
|
|
*
|
2018-02-12 12:25:30 +00:00
|
|
|
* @name Phaser.Sound.BaseSoundManager#game
|
|
|
|
* @type {Phaser.Game}
|
2018-10-09 12:40:00 +00:00
|
|
|
* @readonly
|
2018-02-12 12:25:30 +00:00
|
|
|
* @since 3.0.0
|
2017-11-10 18:05:26 +00:00
|
|
|
*/
|
|
|
|
this.game = game;
|
2018-01-26 14:35:09 +00:00
|
|
|
|
2018-05-04 01:38:01 +00:00
|
|
|
/**
|
|
|
|
* Local reference to the JSON Cache, as used by Audio Sprites.
|
|
|
|
*
|
|
|
|
* @name Phaser.Sound.BaseSoundManager#jsonCache
|
|
|
|
* @type {Phaser.Cache.BaseCache}
|
2018-10-09 12:40:00 +00:00
|
|
|
* @readonly
|
2018-05-04 01:38:01 +00:00
|
|
|
* @since 3.7.0
|
|
|
|
*/
|
|
|
|
this.jsonCache = game.cache.json;
|
|
|
|
|
2017-11-16 15:04:07 +00:00
|
|
|
/**
|
|
|
|
* An array containing all added sounds.
|
|
|
|
*
|
2018-02-12 12:25:30 +00:00
|
|
|
* @name Phaser.Sound.BaseSoundManager#sounds
|
2018-02-18 20:10:37 +00:00
|
|
|
* @type {Phaser.Sound.BaseSound[]}
|
2018-01-07 21:08:51 +00:00
|
|
|
* @default []
|
2018-02-12 12:25:30 +00:00
|
|
|
* @private
|
|
|
|
* @since 3.0.0
|
2017-11-16 15:04:07 +00:00
|
|
|
*/
|
|
|
|
this.sounds = [];
|
2018-01-26 14:35:09 +00:00
|
|
|
|
2017-11-13 18:39:32 +00:00
|
|
|
/**
|
|
|
|
* Global mute setting.
|
2017-11-16 14:23:35 +00:00
|
|
|
*
|
2018-02-12 12:25:30 +00:00
|
|
|
* @name Phaser.Sound.BaseSoundManager#mute
|
|
|
|
* @type {boolean}
|
2018-01-07 21:08:51 +00:00
|
|
|
* @default false
|
2018-02-12 12:25:30 +00:00
|
|
|
* @since 3.0.0
|
2017-11-13 18:39:32 +00:00
|
|
|
*/
|
|
|
|
this.mute = false;
|
2018-01-26 14:35:09 +00:00
|
|
|
|
2017-11-13 18:39:32 +00:00
|
|
|
/**
|
|
|
|
* Global volume setting.
|
2017-11-16 14:23:35 +00:00
|
|
|
*
|
2018-02-12 12:25:30 +00:00
|
|
|
* @name Phaser.Sound.BaseSoundManager#volume
|
|
|
|
* @type {number}
|
2018-01-07 21:08:51 +00:00
|
|
|
* @default 1
|
2018-02-12 12:25:30 +00:00
|
|
|
* @since 3.0.0
|
2017-11-13 18:39:32 +00:00
|
|
|
*/
|
|
|
|
this.volume = 1;
|
2018-01-26 14:35:09 +00:00
|
|
|
|
2017-11-13 18:39:32 +00:00
|
|
|
/**
|
2017-11-21 17:02:24 +00:00
|
|
|
* Flag indicating if sounds should be paused when game looses focus,
|
2018-01-06 16:40:23 +00:00
|
|
|
* for instance when user switches to another tab/program/app.
|
2017-11-21 17:02:24 +00:00
|
|
|
*
|
2018-02-12 12:25:30 +00:00
|
|
|
* @name Phaser.Sound.BaseSoundManager#pauseOnBlur
|
|
|
|
* @type {boolean}
|
2018-01-07 21:08:51 +00:00
|
|
|
* @default true
|
2018-02-12 12:25:30 +00:00
|
|
|
* @since 3.0.0
|
2017-11-13 18:39:32 +00:00
|
|
|
*/
|
2017-11-21 17:02:24 +00:00
|
|
|
this.pauseOnBlur = true;
|
2018-02-22 01:26:36 +00:00
|
|
|
|
2017-11-27 16:33:37 +00:00
|
|
|
/**
|
|
|
|
* Property that actually holds the value of global playback rate.
|
|
|
|
*
|
2018-02-12 12:25:30 +00:00
|
|
|
* @name Phaser.Sound.BaseSoundManager#_rate
|
|
|
|
* @type {number}
|
2017-11-27 16:33:37 +00:00
|
|
|
* @private
|
2018-01-07 21:08:51 +00:00
|
|
|
* @default 1
|
2018-02-12 12:25:30 +00:00
|
|
|
* @since 3.0.0
|
2017-11-27 16:33:37 +00:00
|
|
|
*/
|
|
|
|
this._rate = 1;
|
2018-01-26 14:35:09 +00:00
|
|
|
|
2017-11-27 16:34:27 +00:00
|
|
|
/**
|
|
|
|
* Property that actually holds the value of global detune.
|
|
|
|
*
|
2018-02-12 12:25:30 +00:00
|
|
|
* @name Phaser.Sound.BaseSoundManager#_detune
|
|
|
|
* @type {number}
|
2017-11-27 16:34:27 +00:00
|
|
|
* @private
|
2018-01-07 21:08:51 +00:00
|
|
|
* @default 0
|
2018-02-12 12:25:30 +00:00
|
|
|
* @since 3.0.0
|
2017-11-27 16:34:27 +00:00
|
|
|
*/
|
|
|
|
this._detune = 0;
|
2018-01-26 14:35:09 +00:00
|
|
|
|
2018-01-17 17:09:00 +00:00
|
|
|
/**
|
2022-02-04 16:59:13 +00:00
|
|
|
* 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.
|
2018-01-17 17:09:00 +00:00
|
|
|
* Set to true if the audio system is currently locked awaiting user interaction.
|
|
|
|
*
|
2018-02-12 12:25:30 +00:00
|
|
|
* @name Phaser.Sound.BaseSoundManager#locked
|
|
|
|
* @type {boolean}
|
2018-10-09 12:40:00 +00:00
|
|
|
* @readonly
|
2018-02-12 12:25:30 +00:00
|
|
|
* @since 3.0.0
|
2018-01-17 17:09:00 +00:00
|
|
|
*/
|
2022-02-04 16:59:13 +00:00
|
|
|
this.locked = this.locked || false;
|
2018-01-26 14:35:09 +00:00
|
|
|
|
2018-01-17 17:10:40 +00:00
|
|
|
/**
|
|
|
|
* Flag used internally for handling when the audio system
|
2022-02-04 16:59:13 +00:00
|
|
|
* has been unlocked, if there ever was a need for it.
|
2018-01-17 17:10:40 +00:00
|
|
|
*
|
2018-02-12 12:25:30 +00:00
|
|
|
* @name Phaser.Sound.BaseSoundManager#unlocked
|
|
|
|
* @type {boolean}
|
2018-01-17 17:10:40 +00:00
|
|
|
* @default false
|
2018-02-18 16:17:04 +00:00
|
|
|
* @private
|
2018-02-12 12:25:30 +00:00
|
|
|
* @since 3.0.0
|
2018-01-17 17:10:40 +00:00
|
|
|
*/
|
|
|
|
this.unlocked = false;
|
2018-05-11 00:50:02 +00:00
|
|
|
|
2020-04-27 10:13:46 +00:00
|
|
|
game.events.on(GameEvents.BLUR, this.onGameBlur, this);
|
|
|
|
game.events.on(GameEvents.FOCUS, this.onGameFocus, this);
|
2019-01-17 17:20:54 +00:00
|
|
|
game.events.on(GameEvents.PRE_STEP, this.update, this);
|
|
|
|
game.events.once(GameEvents.DESTROY, this.destroy, this);
|
2017-11-13 18:39:32 +00:00
|
|
|
},
|
2018-01-26 14:35:09 +00:00
|
|
|
|
2018-01-06 16:42:20 +00:00
|
|
|
/**
|
|
|
|
* Adds a new sound into the sound manager.
|
|
|
|
*
|
|
|
|
* @method Phaser.Sound.BaseSoundManager#add
|
2018-02-12 12:25:30 +00:00
|
|
|
* @override
|
|
|
|
* @since 3.0.0
|
2018-02-18 16:16:19 +00:00
|
|
|
*
|
2018-01-06 16:42:20 +00:00
|
|
|
* @param {string} key - Asset key for the sound.
|
2019-05-09 11:38:12 +00:00
|
|
|
* @param {Phaser.Types.Sound.SoundConfig} [config] - An optional config object containing default sound settings.
|
2018-02-18 16:16:19 +00:00
|
|
|
*
|
2022-02-04 16:59:13 +00:00
|
|
|
* @return {Phaser.Sound.BaseSound} The new sound instance.
|
2018-01-06 16:42:20 +00:00
|
|
|
*/
|
2017-11-13 18:39:32 +00:00
|
|
|
add: NOOP,
|
2018-01-26 14:35:09 +00:00
|
|
|
|
2017-12-04 21:09:41 +00:00
|
|
|
/**
|
2018-01-06 16:44:27 +00:00
|
|
|
* Adds a new audio sprite sound into the sound manager.
|
2018-05-04 01:38:01 +00:00
|
|
|
* 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
|
2017-12-04 21:09:41 +00:00
|
|
|
*
|
2018-01-06 16:44:27 +00:00
|
|
|
* @method Phaser.Sound.BaseSoundManager#addAudioSprite
|
2018-02-12 12:25:30 +00:00
|
|
|
* @since 3.0.0
|
2018-02-18 16:16:19 +00:00
|
|
|
*
|
2018-01-06 16:44:27 +00:00
|
|
|
* @param {string} key - Asset key for the sound.
|
2019-05-09 11:38:12 +00:00
|
|
|
* @param {Phaser.Types.Sound.SoundConfig} [config] - An optional config object containing default sound settings.
|
2018-02-18 16:16:19 +00:00
|
|
|
*
|
2022-02-04 16:59:13 +00:00
|
|
|
* @return {(Phaser.Sound.HTML5AudioSound|Phaser.Sound.WebAudioSound)} The new audio sprite sound instance.
|
2017-12-04 21:09:41 +00:00
|
|
|
*/
|
2018-01-26 14:35:09 +00:00
|
|
|
addAudioSprite: function (key, config)
|
|
|
|
{
|
2018-05-04 01:38:01 +00:00
|
|
|
if (config === undefined) { config = {}; }
|
|
|
|
|
2017-11-30 21:14:20 +00:00
|
|
|
var sound = this.add(key, config);
|
2018-03-21 14:40:07 +00:00
|
|
|
|
2018-05-04 01:38:01 +00:00
|
|
|
sound.spritemap = this.jsonCache.get(key).spritemap;
|
2018-03-21 14:40:07 +00:00
|
|
|
|
2018-01-26 14:35:09 +00:00
|
|
|
for (var markerName in sound.spritemap)
|
|
|
|
{
|
|
|
|
if (!sound.spritemap.hasOwnProperty(markerName))
|
|
|
|
{
|
2017-11-30 21:14:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
2018-03-21 14:40:07 +00:00
|
|
|
|
2018-05-04 01:38:01 +00:00
|
|
|
var markerConfig = Clone(config);
|
|
|
|
|
2017-11-30 21:56:54 +00:00
|
|
|
var marker = sound.spritemap[markerName];
|
2018-03-21 14:40:07 +00:00
|
|
|
|
2018-05-04 01:38:01 +00:00
|
|
|
markerConfig.loop = (marker.hasOwnProperty('loop')) ? marker.loop : false;
|
|
|
|
|
2017-11-30 21:14:20 +00:00
|
|
|
sound.addMarker({
|
|
|
|
name: markerName,
|
|
|
|
start: marker.start,
|
|
|
|
duration: marker.end - marker.start,
|
2018-05-04 01:38:01 +00:00
|
|
|
config: markerConfig
|
2017-11-30 21:14:20 +00:00
|
|
|
});
|
|
|
|
}
|
2018-03-21 14:40:07 +00:00
|
|
|
|
2017-11-30 21:14:20 +00:00
|
|
|
return sound;
|
|
|
|
},
|
2018-01-26 14:35:09 +00:00
|
|
|
|
2020-03-24 22:08:32 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2022-02-04 16:59:13 +00:00
|
|
|
* @return {?Phaser.Sound.BaseSound} - The sound, or null.
|
2020-03-24 22:08:32 +00:00
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*
|
2022-02-04 16:59:13 +00:00
|
|
|
* @return {Phaser.Sound.BaseSound[]} - The sounds, or an empty array.
|
2020-03-24 22:08:32 +00:00
|
|
|
*/
|
|
|
|
getAll: function (key)
|
|
|
|
{
|
|
|
|
return GetAll(this.sounds, 'key', key);
|
|
|
|
},
|
|
|
|
|
2018-01-06 16:47:41 +00:00
|
|
|
/**
|
2020-01-03 21:36:00 +00:00
|
|
|
* 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.
|
2018-01-06 16:47:41 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Sound.BaseSoundManager#play
|
2019-01-17 17:20:54 +00:00
|
|
|
* @listens Phaser.Sound.Events#COMPLETE
|
2018-02-12 12:25:30 +00:00
|
|
|
* @since 3.0.0
|
2018-02-18 16:16:19 +00:00
|
|
|
*
|
2018-01-06 16:47:41 +00:00
|
|
|
* @param {string} key - Asset key for the sound.
|
2019-05-09 11:38:12 +00:00
|
|
|
* @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.
|
2018-02-18 16:16:19 +00:00
|
|
|
*
|
2018-02-12 12:25:30 +00:00
|
|
|
* @return {boolean} Whether the sound started playing successfully.
|
2018-01-06 16:47:41 +00:00
|
|
|
*/
|
2018-01-26 14:35:09 +00:00
|
|
|
play: function (key, extra)
|
|
|
|
{
|
2018-01-04 18:24:28 +00:00
|
|
|
var sound = this.add(key);
|
2018-03-21 14:40:07 +00:00
|
|
|
|
2019-01-17 17:20:54 +00:00
|
|
|
sound.once(Events.COMPLETE, sound.destroy, sound);
|
2018-03-21 14:40:07 +00:00
|
|
|
|
2018-01-26 14:35:09 +00:00
|
|
|
if (extra)
|
|
|
|
{
|
|
|
|
if (extra.name)
|
|
|
|
{
|
2018-01-04 18:27:12 +00:00
|
|
|
sound.addMarker(extra);
|
2018-03-21 14:40:07 +00:00
|
|
|
|
2018-01-20 18:51:54 +00:00
|
|
|
return sound.play(extra.name);
|
2018-01-04 18:27:12 +00:00
|
|
|
}
|
2018-01-26 14:35:09 +00:00
|
|
|
else
|
|
|
|
{
|
2018-01-20 18:51:54 +00:00
|
|
|
return sound.play(extra);
|
2018-01-04 18:27:12 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-26 14:35:09 +00:00
|
|
|
else
|
|
|
|
{
|
2018-01-20 18:51:54 +00:00
|
|
|
return sound.play();
|
2018-01-04 18:27:12 +00:00
|
|
|
}
|
2018-01-04 18:24:28 +00:00
|
|
|
},
|
2018-01-26 14:35:09 +00:00
|
|
|
|
2018-01-06 16:48:31 +00:00
|
|
|
/**
|
2020-03-24 20:56:22 +00:00
|
|
|
* 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.
|
2018-01-06 16:48:31 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Sound.BaseSoundManager#playAudioSprite
|
2019-01-17 17:20:54 +00:00
|
|
|
* @listens Phaser.Sound.Events#COMPLETE
|
2018-02-12 12:25:30 +00:00
|
|
|
* @since 3.0.0
|
2018-02-18 16:16:19 +00:00
|
|
|
*
|
2018-01-06 16:48:31 +00:00
|
|
|
* @param {string} key - Asset key for the sound.
|
|
|
|
* @param {string} spriteName - The name of the sound sprite to play.
|
2019-05-09 11:38:12 +00:00
|
|
|
* @param {Phaser.Types.Sound.SoundConfig} [config] - An optional config object containing default sound settings.
|
2018-02-18 16:16:19 +00:00
|
|
|
*
|
2018-02-12 12:25:30 +00:00
|
|
|
* @return {boolean} Whether the audio sprite sound started playing successfully.
|
2018-01-06 16:48:31 +00:00
|
|
|
*/
|
2018-01-26 14:35:09 +00:00
|
|
|
playAudioSprite: function (key, spriteName, config)
|
|
|
|
{
|
2018-01-04 18:28:34 +00:00
|
|
|
var sound = this.addAudioSprite(key);
|
2018-03-21 14:40:07 +00:00
|
|
|
|
2019-01-17 17:20:54 +00:00
|
|
|
sound.once(Events.COMPLETE, sound.destroy, sound);
|
2018-03-21 14:40:07 +00:00
|
|
|
|
2018-01-20 18:53:09 +00:00
|
|
|
return sound.play(spriteName, config);
|
2018-01-04 18:28:34 +00:00
|
|
|
},
|
2018-01-26 14:35:09 +00:00
|
|
|
|
2018-01-04 18:44:22 +00:00
|
|
|
/**
|
2018-01-06 16:49:17 +00:00
|
|
|
* Removes a sound from the sound manager.
|
|
|
|
* The removed sound is destroyed before removal.
|
2018-01-04 18:44:22 +00:00
|
|
|
*
|
2018-01-06 16:49:17 +00:00
|
|
|
* @method Phaser.Sound.BaseSoundManager#remove
|
2018-02-12 12:25:30 +00:00
|
|
|
* @since 3.0.0
|
2018-02-18 16:16:19 +00:00
|
|
|
*
|
2018-02-18 16:21:11 +00:00
|
|
|
* @param {Phaser.Sound.BaseSound} sound - The sound object to remove.
|
2018-02-18 16:16:19 +00:00
|
|
|
*
|
2018-02-12 12:25:30 +00:00
|
|
|
* @return {boolean} True if the sound was removed successfully, otherwise false.
|
2018-01-04 18:44:22 +00:00
|
|
|
*/
|
2018-01-26 14:35:09 +00:00
|
|
|
remove: function (sound)
|
|
|
|
{
|
2018-01-04 18:41:43 +00:00
|
|
|
var index = this.sounds.indexOf(sound);
|
2018-03-21 14:40:07 +00:00
|
|
|
|
2018-01-26 14:35:09 +00:00
|
|
|
if (index !== -1)
|
|
|
|
{
|
2018-01-06 16:49:42 +00:00
|
|
|
sound.destroy();
|
2018-03-21 14:40:07 +00:00
|
|
|
|
2018-01-04 18:41:43 +00:00
|
|
|
this.sounds.splice(index, 1);
|
2018-03-21 14:40:07 +00:00
|
|
|
|
2018-01-04 18:41:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-03-21 14:40:07 +00:00
|
|
|
|
2018-01-04 18:41:43 +00:00
|
|
|
return false;
|
|
|
|
},
|
2018-01-26 14:35:09 +00:00
|
|
|
|
2020-03-24 22:08:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
},
|
|
|
|
|
2018-01-04 18:44:22 +00:00
|
|
|
/**
|
2018-01-06 16:52:34 +00:00
|
|
|
* Removes all sounds from the sound manager that have an asset key matching the given value.
|
|
|
|
* The removed sounds are destroyed before removal.
|
2018-01-04 18:44:22 +00:00
|
|
|
*
|
2018-01-06 16:52:34 +00:00
|
|
|
* @method Phaser.Sound.BaseSoundManager#removeByKey
|
2018-02-12 12:25:30 +00:00
|
|
|
* @since 3.0.0
|
2018-02-18 16:16:19 +00:00
|
|
|
*
|
2018-01-06 16:52:34 +00:00
|
|
|
* @param {string} key - The key to match when removing sound objects.
|
2018-02-18 16:16:19 +00:00
|
|
|
*
|
2018-02-12 12:25:30 +00:00
|
|
|
* @return {number} The number of matching sound objects that were removed.
|
2018-01-04 18:44:22 +00:00
|
|
|
*/
|
2018-01-26 14:35:09 +00:00
|
|
|
removeByKey: function (key)
|
|
|
|
{
|
2018-01-04 18:43:41 +00:00
|
|
|
var removed = 0;
|
2018-03-21 14:40:07 +00:00
|
|
|
|
2018-01-26 14:35:09 +00:00
|
|
|
for (var i = this.sounds.length - 1; i >= 0; i--)
|
|
|
|
{
|
2018-01-06 16:55:12 +00:00
|
|
|
var sound = this.sounds[i];
|
2018-03-21 14:40:07 +00:00
|
|
|
|
2018-01-26 14:35:09 +00:00
|
|
|
if (sound.key === key)
|
|
|
|
{
|
2018-01-06 16:55:12 +00:00
|
|
|
sound.destroy();
|
2018-03-21 14:40:07 +00:00
|
|
|
|
2018-01-04 18:43:41 +00:00
|
|
|
this.sounds.splice(i, 1);
|
2018-03-21 14:40:07 +00:00
|
|
|
|
2018-01-04 18:43:41 +00:00
|
|
|
removed++;
|
|
|
|
}
|
|
|
|
}
|
2018-03-21 14:40:07 +00:00
|
|
|
|
2018-01-04 18:43:41 +00:00
|
|
|
return removed;
|
|
|
|
},
|
2018-01-26 14:35:09 +00:00
|
|
|
|
2018-01-06 16:55:34 +00:00
|
|
|
/**
|
|
|
|
* Pauses all the sounds in the game.
|
|
|
|
*
|
|
|
|
* @method Phaser.Sound.BaseSoundManager#pauseAll
|
2019-01-17 17:20:54 +00:00
|
|
|
* @fires Phaser.Sound.Events#PAUSE_ALL
|
2018-02-12 12:25:30 +00:00
|
|
|
* @since 3.0.0
|
2018-01-06 16:55:34 +00:00
|
|
|
*/
|
2018-01-26 14:35:09 +00:00
|
|
|
pauseAll: function ()
|
|
|
|
{
|
|
|
|
this.forEachActiveSound(function (sound)
|
|
|
|
{
|
2018-01-04 18:30:29 +00:00
|
|
|
sound.pause();
|
|
|
|
});
|
2018-01-26 14:35:09 +00:00
|
|
|
|
2019-01-17 17:20:54 +00:00
|
|
|
this.emit(Events.PAUSE_ALL, this);
|
2018-01-04 18:30:29 +00:00
|
|
|
},
|
2018-01-26 14:35:09 +00:00
|
|
|
|
2018-01-06 16:55:48 +00:00
|
|
|
/**
|
|
|
|
* Resumes all the sounds in the game.
|
|
|
|
*
|
|
|
|
* @method Phaser.Sound.BaseSoundManager#resumeAll
|
2019-01-17 17:20:54 +00:00
|
|
|
* @fires Phaser.Sound.Events#RESUME_ALL
|
2018-02-12 12:25:30 +00:00
|
|
|
* @since 3.0.0
|
2018-01-06 16:55:48 +00:00
|
|
|
*/
|
2018-01-26 14:35:09 +00:00
|
|
|
resumeAll: function ()
|
|
|
|
{
|
|
|
|
this.forEachActiveSound(function (sound)
|
|
|
|
{
|
2018-01-04 18:32:10 +00:00
|
|
|
sound.resume();
|
|
|
|
});
|
2018-01-26 14:35:09 +00:00
|
|
|
|
2019-01-17 17:20:54 +00:00
|
|
|
this.emit(Events.RESUME_ALL, this);
|
2018-01-04 18:32:10 +00:00
|
|
|
},
|
2018-01-26 14:35:09 +00:00
|
|
|
|
2018-01-06 16:56:05 +00:00
|
|
|
/**
|
|
|
|
* Stops all the sounds in the game.
|
|
|
|
*
|
|
|
|
* @method Phaser.Sound.BaseSoundManager#stopAll
|
2019-01-17 17:20:54 +00:00
|
|
|
* @fires Phaser.Sound.Events#STOP_ALL
|
2018-02-12 12:25:30 +00:00
|
|
|
* @since 3.0.0
|
2018-01-06 16:56:05 +00:00
|
|
|
*/
|
2018-01-26 14:35:09 +00:00
|
|
|
stopAll: function ()
|
|
|
|
{
|
|
|
|
this.forEachActiveSound(function (sound)
|
|
|
|
{
|
2018-01-04 14:59:44 +00:00
|
|
|
sound.stop();
|
|
|
|
});
|
2018-01-26 14:35:09 +00:00
|
|
|
|
2019-01-17 17:20:54 +00:00
|
|
|
this.emit(Events.STOP_ALL, this);
|
2018-01-04 14:59:44 +00:00
|
|
|
},
|
2018-01-26 14:35:09 +00:00
|
|
|
|
2022-02-04 16:59:13 +00:00
|
|
|
|
2020-03-24 22:08:32 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
},
|
|
|
|
|
2022-02-04 16:59:13 +00:00
|
|
|
/**
|
|
|
|
* 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,
|
|
|
|
|
2017-12-04 21:09:41 +00:00
|
|
|
/**
|
2018-01-07 21:09:11 +00:00
|
|
|
* Method used internally for pausing sound manager if
|
|
|
|
* Phaser.Sound.BaseSoundManager#pauseOnBlur is set to true.
|
|
|
|
*
|
2018-02-12 12:25:30 +00:00
|
|
|
* @method Phaser.Sound.BaseSoundManager#onBlur
|
2018-01-06 16:57:14 +00:00
|
|
|
* @override
|
|
|
|
* @protected
|
2018-02-12 12:25:30 +00:00
|
|
|
* @since 3.0.0
|
2017-12-04 21:09:41 +00:00
|
|
|
*/
|
2017-11-21 17:04:54 +00:00
|
|
|
onBlur: NOOP,
|
2018-01-26 14:35:09 +00:00
|
|
|
|
2017-12-04 21:09:41 +00:00
|
|
|
/**
|
2018-01-07 21:09:44 +00:00
|
|
|
* Method used internally for resuming sound manager if
|
|
|
|
* Phaser.Sound.BaseSoundManager#pauseOnBlur is set to true.
|
|
|
|
*
|
2018-02-12 12:25:30 +00:00
|
|
|
* @method Phaser.Sound.BaseSoundManager#onFocus
|
2018-01-06 16:57:14 +00:00
|
|
|
* @override
|
|
|
|
* @protected
|
2018-02-12 12:25:30 +00:00
|
|
|
* @since 3.0.0
|
2017-12-04 21:09:41 +00:00
|
|
|
*/
|
2017-11-21 17:04:54 +00:00
|
|
|
onFocus: NOOP,
|
2018-01-26 14:35:09 +00:00
|
|
|
|
2020-04-27 10:13:46 +00:00
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
}
|
2020-04-27 11:59:28 +00:00
|
|
|
},
|
2020-04-27 10:13:46 +00:00
|
|
|
|
2017-11-26 15:59:12 +00:00
|
|
|
/**
|
|
|
|
* Update method called on every game step.
|
2018-01-06 16:58:28 +00:00
|
|
|
* Removes destroyed sounds and updates every active sound in the game.
|
2017-11-26 15:59:12 +00:00
|
|
|
*
|
2018-01-06 16:58:28 +00:00
|
|
|
* @method Phaser.Sound.BaseSoundManager#update
|
2018-02-12 12:25:30 +00:00
|
|
|
* @protected
|
2019-01-17 17:20:54 +00:00
|
|
|
* @fires Phaser.Sound.Events#UNLOCKED
|
2018-02-12 12:25:30 +00:00
|
|
|
* @since 3.0.0
|
2018-02-18 16:16:19 +00:00
|
|
|
*
|
2017-11-26 15:59:12 +00:00
|
|
|
* @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.
|
|
|
|
*/
|
2018-01-26 14:35:09 +00:00
|
|
|
update: function (time, delta)
|
|
|
|
{
|
2022-02-04 16:59:13 +00:00
|
|
|
if (this.unlocked)
|
|
|
|
{
|
|
|
|
this.unlocked = false;
|
|
|
|
this.locked = false;
|
|
|
|
|
|
|
|
this.emit(Events.UNLOCKED, this);
|
|
|
|
}
|
2021-11-08 21:51:33 +00:00
|
|
|
|
2022-02-04 16:59:13 +00:00
|
|
|
for (var i = this.sounds.length - 1; i >= 0; i--)
|
2018-01-26 14:35:09 +00:00
|
|
|
{
|
2022-02-04 16:59:13 +00:00
|
|
|
if (this.sounds[i].pendingRemove)
|
2018-01-26 14:35:09 +00:00
|
|
|
{
|
2022-02-04 16:59:13 +00:00
|
|
|
this.sounds.splice(i, 1);
|
2018-01-04 18:50:06 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-21 14:40:07 +00:00
|
|
|
|
2022-02-04 16:59:13 +00:00
|
|
|
this.sounds.forEach(function (sound)
|
2018-01-26 14:35:09 +00:00
|
|
|
{
|
2022-02-04 16:59:13 +00:00
|
|
|
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;
|
2017-11-26 15:59:12 +00:00
|
|
|
},
|
2018-01-26 14:35:09 +00:00
|
|
|
|
2018-01-04 18:51:54 +00:00
|
|
|
/**
|
2018-01-07 21:10:02 +00:00
|
|
|
* Method used internally for iterating only over active sounds and skipping sounds that are marked for removal.
|
|
|
|
*
|
2018-01-06 17:10:19 +00:00
|
|
|
* @method Phaser.Sound.BaseSoundManager#forEachActiveSound
|
2018-02-12 12:25:30 +00:00
|
|
|
* @private
|
|
|
|
* @since 3.0.0
|
2018-02-18 16:16:19 +00:00
|
|
|
*
|
2019-05-09 11:38:12 +00:00
|
|
|
* @param {Phaser.Types.Sound.EachActiveSoundCallback} callback - Callback function. (manager: Phaser.Sound.BaseSoundManager, sound: Phaser.Sound.BaseSound, index: number, sounds: Phaser.Manager.BaseSound[]) => void
|
2018-03-21 14:02:10 +00:00
|
|
|
* @param {*} [scope] - Callback context.
|
2018-01-04 18:51:54 +00:00
|
|
|
*/
|
2018-03-19 21:57:46 +00:00
|
|
|
forEachActiveSound: function (callback, scope)
|
2018-01-26 14:35:09 +00:00
|
|
|
{
|
2022-02-04 16:59:13 +00:00
|
|
|
var _this = this;
|
2021-11-08 21:55:08 +00:00
|
|
|
|
2022-02-04 16:59:13 +00:00
|
|
|
this.sounds.forEach(function (sound, index)
|
2018-01-26 14:35:09 +00:00
|
|
|
{
|
2019-10-02 11:50:10 +00:00
|
|
|
if (sound && !sound.pendingRemove)
|
2018-01-26 14:35:09 +00:00
|
|
|
{
|
2022-02-04 16:59:13 +00:00
|
|
|
callback.call(scope || _this, sound, index, _this.sounds);
|
2018-01-04 18:51:54 +00:00
|
|
|
}
|
2022-02-04 16:59:13 +00:00
|
|
|
});
|
2017-11-27 16:35:09 +00:00
|
|
|
},
|
2018-02-21 22:51:05 +00:00
|
|
|
|
2018-03-21 14:40:07 +00:00
|
|
|
/**
|
|
|
|
* Sets the global playback rate at which all the sounds will be played.
|
2018-04-14 15:48:16 +00:00
|
|
|
*
|
2018-03-21 14:40:07 +00:00
|
|
|
* 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
|
2019-01-17 17:20:54 +00:00
|
|
|
* @fires Phaser.Sound.Events#GLOBAL_RATE
|
2018-03-21 14:40:07 +00:00
|
|
|
* @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.
|
|
|
|
*/
|
2018-03-22 13:22:23 +00:00
|
|
|
setRate: function (value)
|
|
|
|
{
|
2018-03-21 14:40:07 +00:00
|
|
|
this.rate = value;
|
|
|
|
|
|
|
|
return this;
|
2018-03-22 13:22:23 +00:00
|
|
|
},
|
2018-03-21 14:40:07 +00:00
|
|
|
|
2018-02-22 01:26:36 +00:00
|
|
|
/**
|
|
|
|
* 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 ()
|
2018-01-26 14:35:09 +00:00
|
|
|
{
|
2018-02-22 01:26:36 +00:00
|
|
|
return this._rate;
|
|
|
|
},
|
2018-01-26 14:35:09 +00:00
|
|
|
|
2018-02-22 01:26:36 +00:00
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
this._rate = value;
|
2018-02-21 22:51:05 +00:00
|
|
|
|
2018-02-22 01:26:36 +00:00
|
|
|
this.forEachActiveSound(function (sound)
|
|
|
|
{
|
2018-03-28 13:12:37 +00:00
|
|
|
sound.calculateRate();
|
2018-02-22 01:26:36 +00:00
|
|
|
});
|
2018-02-21 22:51:05 +00:00
|
|
|
|
2019-01-17 17:20:54 +00:00
|
|
|
this.emit(Events.GLOBAL_RATE, this, value);
|
2018-02-22 01:26:36 +00:00
|
|
|
}
|
2018-02-21 22:51:05 +00:00
|
|
|
|
2017-11-27 16:35:35 +00:00
|
|
|
},
|
2018-02-21 22:51:05 +00:00
|
|
|
|
2018-03-21 14:40:07 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2019-01-17 17:20:54 +00:00
|
|
|
* @fires Phaser.Sound.Events#GLOBAL_DETUNE
|
2018-03-21 14:40:07 +00:00
|
|
|
* @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.
|
|
|
|
*/
|
2018-03-22 13:22:23 +00:00
|
|
|
setDetune: function (value)
|
|
|
|
{
|
2018-03-21 14:40:07 +00:00
|
|
|
this.detune = value;
|
|
|
|
|
|
|
|
return this;
|
2018-03-22 13:22:23 +00:00
|
|
|
},
|
2018-03-21 14:40:07 +00:00
|
|
|
|
2018-02-22 01:26:36 +00:00
|
|
|
/**
|
|
|
|
* 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 ()
|
2018-01-26 14:35:09 +00:00
|
|
|
{
|
2018-02-22 01:26:36 +00:00
|
|
|
return this._detune;
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value)
|
|
|
|
{
|
|
|
|
this._detune = value;
|
|
|
|
|
|
|
|
this.forEachActiveSound(function (sound)
|
|
|
|
{
|
2018-03-28 13:12:37 +00:00
|
|
|
sound.calculateRate();
|
2018-02-22 01:26:36 +00:00
|
|
|
});
|
|
|
|
|
2019-01-17 17:20:54 +00:00
|
|
|
this.emit(Events.GLOBAL_DETUNE, this, value);
|
2018-02-22 01:26:36 +00:00
|
|
|
}
|
2018-01-26 14:35:09 +00:00
|
|
|
|
2017-11-27 16:35:35 +00:00
|
|
|
}
|
2018-02-21 22:51:05 +00:00
|
|
|
|
2017-11-27 16:35:35 +00:00
|
|
|
});
|
2018-02-21 22:51:05 +00:00
|
|
|
|
2017-11-10 12:05:29 +00:00
|
|
|
module.exports = BaseSoundManager;
|