2017-11-09 13:37:41 +00:00
|
|
|
var Class = require('../utils/Class');
|
2017-11-13 18:39:32 +00:00
|
|
|
var NOOP = require('../utils/NOOP');
|
|
|
|
var EventDispatcher = require('../events/EventDispatcher');
|
2018-01-03 20:28:45 +00:00
|
|
|
var SoundValueEvent = require('./SoundValueEvent');
|
2017-11-13 18:39:32 +00:00
|
|
|
// Phaser.Sound.BaseSoundManager
|
2017-11-10 12:05:29 +00:00
|
|
|
var BaseSoundManager = new Class({
|
2017-11-13 18:39:32 +00:00
|
|
|
initialize: function BaseSoundManager(game) {
|
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
|
|
|
*
|
2017-11-13 18:39:32 +00:00
|
|
|
* @property {Phaser.Game} game
|
2017-11-10 18:05:26 +00:00
|
|
|
*/
|
|
|
|
this.game = game;
|
2018-01-03 20:55:51 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {Phaser.Events.EventDispatcher} events
|
|
|
|
*/
|
|
|
|
this.events = new EventDispatcher();
|
2017-11-16 15:04:07 +00:00
|
|
|
/**
|
|
|
|
* An array containing all added sounds.
|
|
|
|
*
|
2017-12-04 21:09:41 +00:00
|
|
|
* @private
|
2017-11-16 15:04:07 +00:00
|
|
|
* @property {Array} sounds
|
|
|
|
*/
|
|
|
|
this.sounds = [];
|
2017-11-13 18:39:32 +00:00
|
|
|
/**
|
|
|
|
* Global mute setting.
|
2017-11-16 14:23:35 +00:00
|
|
|
*
|
2017-11-13 18:39:32 +00:00
|
|
|
* @property {boolean} mute
|
|
|
|
*/
|
|
|
|
this.mute = false;
|
|
|
|
/**
|
|
|
|
* Global volume setting.
|
2017-11-16 14:23:35 +00:00
|
|
|
*
|
2017-11-13 18:39:32 +00:00
|
|
|
* @property {number} volume
|
|
|
|
*/
|
|
|
|
this.volume = 1;
|
|
|
|
/**
|
2018-01-04 18:24:28 +00:00
|
|
|
* Global playback rate at which all the sounds will be played.
|
2017-11-13 18:39:32 +00:00
|
|
|
* 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.
|
2017-11-16 14:23:35 +00:00
|
|
|
*
|
2017-11-13 18:39:32 +00:00
|
|
|
* @property {number} rate
|
|
|
|
*/
|
|
|
|
this.rate = 1;
|
2017-11-16 16:21:49 +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).
|
|
|
|
*
|
|
|
|
* @property {number} detune
|
|
|
|
*/
|
|
|
|
this.detune = 0;
|
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,
|
|
|
|
* for instance when user switches tabs or to another program/app.
|
|
|
|
*
|
|
|
|
* @property {boolean} pauseOnBlur
|
2017-11-13 18:39:32 +00:00
|
|
|
*/
|
2017-11-21 17:02:24 +00:00
|
|
|
this.pauseOnBlur = true;
|
2017-11-21 17:06:18 +00:00
|
|
|
game.events.on('ON_BLUR', function () {
|
|
|
|
if (this.pauseOnBlur) {
|
|
|
|
this.onBlur();
|
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
game.events.on('ON_FOCUS', function () {
|
|
|
|
if (this.pauseOnBlur) {
|
|
|
|
this.onFocus();
|
|
|
|
}
|
|
|
|
}.bind(this));
|
2017-11-27 16:33:37 +00:00
|
|
|
/**
|
|
|
|
* Property that actually holds the value of global playback rate.
|
|
|
|
*
|
|
|
|
* @property {number} _rate
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._rate = 1;
|
2017-11-27 16:34:27 +00:00
|
|
|
/**
|
|
|
|
* Property that actually holds the value of global detune.
|
|
|
|
*
|
|
|
|
* @property {number} _detune
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._detune = 0;
|
2017-11-13 18:39:32 +00:00
|
|
|
},
|
|
|
|
add: NOOP,
|
2017-12-04 21:09:41 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @param {string} key
|
|
|
|
* @param {ISoundConfig} config
|
|
|
|
* @returns {IAudioSpriteSound}
|
|
|
|
*/
|
2017-11-30 21:14:20 +00:00
|
|
|
addAudioSprite: function (key, config) {
|
|
|
|
var sound = this.add(key, config);
|
|
|
|
/**
|
|
|
|
* Local reference to 'spritemap' object form json file generated by audiosprite tool.
|
|
|
|
*
|
|
|
|
* @property {object} spritemap
|
|
|
|
*/
|
|
|
|
sound.spritemap = this.game.cache.json.get(key).spritemap;
|
|
|
|
for (var markerName in sound.spritemap) {
|
2017-11-30 21:56:54 +00:00
|
|
|
if (!sound.spritemap.hasOwnProperty(markerName)) {
|
2017-11-30 21:14:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
2017-11-30 21:56:54 +00:00
|
|
|
var marker = sound.spritemap[markerName];
|
2017-11-30 21:14:20 +00:00
|
|
|
sound.addMarker({
|
|
|
|
name: markerName,
|
|
|
|
start: marker.start,
|
|
|
|
duration: marker.end - marker.start,
|
|
|
|
config: config
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return sound;
|
|
|
|
},
|
2018-01-04 18:27:12 +00:00
|
|
|
play: function (key, extra) {
|
2018-01-04 18:24:28 +00:00
|
|
|
var sound = this.add(key);
|
|
|
|
sound.events.once('SOUND_ENDED', sound.destroy.bind(sound));
|
2018-01-04 18:27:12 +00:00
|
|
|
if (extra) {
|
|
|
|
if (extra.name) {
|
|
|
|
sound.addMarker(extra);
|
|
|
|
sound.play(extra.name);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sound.play(extra);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sound.play();
|
|
|
|
}
|
2018-01-04 18:24:28 +00:00
|
|
|
},
|
2018-01-04 18:28:34 +00:00
|
|
|
playAudioSprite: function (key, spriteName, config) {
|
|
|
|
var sound = this.addAudioSprite(key);
|
|
|
|
sound.events.once('SOUND_ENDED', sound.destroy.bind(sound));
|
|
|
|
sound.play(spriteName, config);
|
|
|
|
},
|
2017-11-13 18:39:32 +00:00
|
|
|
remove: NOOP,
|
|
|
|
removeByKey: NOOP,
|
2018-01-04 18:30:29 +00:00
|
|
|
pauseAll: function () {
|
|
|
|
this.sounds.forEach(function (sound) {
|
|
|
|
sound.pause();
|
|
|
|
});
|
|
|
|
},
|
2018-01-04 18:32:10 +00:00
|
|
|
resumeAll: function () {
|
|
|
|
this.sounds.forEach(function (sound) {
|
|
|
|
sound.resume();
|
|
|
|
});
|
|
|
|
},
|
2018-01-04 14:59:44 +00:00
|
|
|
stopAll: function () {
|
|
|
|
this.sounds.forEach(function (sound) {
|
|
|
|
sound.stop();
|
|
|
|
});
|
|
|
|
},
|
2017-12-04 21:09:41 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2017-11-21 17:04:54 +00:00
|
|
|
onBlur: NOOP,
|
2017-12-04 21:09:41 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2017-11-21 17:04:54 +00:00
|
|
|
onFocus: NOOP,
|
2017-11-26 15:59:12 +00:00
|
|
|
/**
|
|
|
|
* Update method called on every game step.
|
|
|
|
*
|
2017-12-04 21:09:41 +00:00
|
|
|
* @private
|
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.
|
|
|
|
*/
|
|
|
|
update: function (time, delta) {
|
2017-12-10 12:18:03 +00:00
|
|
|
// TODO remove pending sounds
|
2017-11-26 15:59:12 +00:00
|
|
|
this.sounds.forEach(function (sound) {
|
|
|
|
sound.update(time, delta);
|
|
|
|
});
|
|
|
|
},
|
2017-11-13 18:39:32 +00:00
|
|
|
destroy: NOOP
|
2017-11-10 18:05:26 +00:00
|
|
|
});
|
2017-11-27 16:35:09 +00:00
|
|
|
/**
|
|
|
|
* Global playback rate.
|
|
|
|
* @property {number} rate
|
|
|
|
*/
|
|
|
|
Object.defineProperty(BaseSoundManager.prototype, 'rate', {
|
|
|
|
get: function () {
|
|
|
|
return this._rate;
|
|
|
|
},
|
|
|
|
set: function (value) {
|
|
|
|
this._rate = value;
|
|
|
|
this.sounds.forEach(function (sound) {
|
|
|
|
sound.setRate();
|
|
|
|
}, this);
|
2018-01-03 20:28:45 +00:00
|
|
|
this.events.dispatch(new SoundValueEvent(this, 'SOUND_RATE', value));
|
2017-11-27 16:35:09 +00:00
|
|
|
}
|
|
|
|
});
|
2017-11-27 16:35:35 +00:00
|
|
|
/**
|
|
|
|
* Global detune.
|
|
|
|
* @property {number} detune
|
|
|
|
*/
|
|
|
|
Object.defineProperty(BaseSoundManager.prototype, 'detune', {
|
|
|
|
get: function () {
|
|
|
|
return this._detune;
|
|
|
|
},
|
|
|
|
set: function (value) {
|
|
|
|
this._detune = value;
|
|
|
|
this.sounds.forEach(function (sound) {
|
|
|
|
sound.setRate();
|
|
|
|
}, this);
|
2018-01-03 20:29:00 +00:00
|
|
|
this.events.dispatch(new SoundValueEvent(this, 'SOUND_DETUNE', value));
|
2017-11-27 16:35:35 +00:00
|
|
|
}
|
|
|
|
});
|
2017-11-10 12:05:29 +00:00
|
|
|
module.exports = BaseSoundManager;
|