phaser/src/sound/webaudio/WebAudioSoundManager.js

170 lines
5.7 KiB
JavaScript
Raw Normal View History

var Class = require('../../utils/Class');
var BaseSoundManager = require('../BaseSoundManager');
var WebAudioSound = require('./WebAudioSound');
2018-01-06 18:14:17 +00:00
/*!
* @author Pavle Goloskokovic <pgoloskokovic@gmail.com> (http://prunegames.com)
*/
2017-11-10 11:55:32 +00:00
var WebAudioSoundManager = new Class({
Extends: BaseSoundManager,
2018-01-06 18:14:17 +00:00
/**
* Web Audio API implementation of the sound manager.
*
* @class Phaser.Sound.WebAudioSoundManager
* @constructor
* @param {Phaser.Game} game - Reference to the current game instance.
*/
initialize: function WebAudioSoundManager(game) {
2017-11-10 11:55:32 +00:00
/**
* The AudioContext being used for playback.
*
* @private
* @property {AudioContext} context
2017-11-10 11:55:32 +00:00
*/
this.context = this.createAudioContext(game);
/**
2018-01-06 18:14:43 +00:00
* Gain node responsible for controlling global muting.
*
* @private
* @property {GainNode} masterMuteNode
*/
this.masterMuteNode = this.context.createGain();
/**
2018-01-06 18:14:43 +00:00
* Gain node responsible for controlling global volume.
*
* @private
* @property {GainNode} masterVolumeNode
*/
this.masterVolumeNode = this.context.createGain();
this.masterMuteNode.connect(this.masterVolumeNode);
this.masterVolumeNode.connect(this.context.destination);
/**
* Destination node for connecting individual sounds to.
*
* @private
* @property {AudioNode} destination
*/
this.destination = this.masterMuteNode;
this.locked = this.context.state === 'suspended' && 'ontouchstart' in window;
BaseSoundManager.call(this, game);
2017-11-10 11:55:32 +00:00
},
2017-11-26 15:19:56 +00:00
/**
2018-01-06 18:17:19 +00:00
* Method responsible for instantiating and returning AudioContext instance.
* If an instance of an AudioContext class was provided trough the game config,
* that instance will be returned instead. This can come in handy if you are reloading
* a Phaser game on a page that never properly refreshes (such as in an SPA project)
* and you want to reuse already instantiated AudioContext.
*
2017-11-26 15:19:56 +00:00
* @private
2018-01-06 18:17:19 +00:00
* @method Phaser.Sound.WebAudioSoundManager#createAudioContext
* @param {Phaser.Game} game - Reference to the current game instance.
* @returns {AudioContext} The AudioContext instance to be used for playback.
2017-11-26 15:19:56 +00:00
*/
createAudioContext: function (game) {
var audioConfig = game.config.audio;
if (audioConfig && audioConfig.context) {
return audioConfig.context;
2017-11-10 11:55:32 +00:00
}
return new AudioContext();
},
2018-01-06 18:18:00 +00:00
/**
* Adds a new sound into the sound manager.
*
* @method Phaser.Sound.WebAudioSoundManager#add
* @param {string} key - Asset key for the sound.
* @param {ISoundConfig} [config] - An optional config object containing default sound settings.
* @returns {Phaser.Sound.WebAudioSound} The new sound instance.
*/
add: function (key, config) {
var sound = new WebAudioSound(this, key, config);
this.sounds.push(sound);
return sound;
},
2017-11-26 15:19:56 +00:00
/**
2018-01-06 18:18:48 +00:00
* Unlocks Web Audio API on iOS devices on the initial touch event.
*
2018-01-06 18:18:48 +00:00
* Read more about how this issue is handled here in [this article](TODO add link).
*
2017-11-26 15:19:56 +00:00
* @private
2018-01-06 18:18:48 +00:00
* @method Phaser.Sound.WebAudioSoundManager#unlock
2017-11-26 15:19:56 +00:00
*/
unlock: function () {
var _this = this;
var unlock = function () {
_this.context.resume().then(function () {
document.body.removeEventListener('touchstart', unlock);
document.body.removeEventListener('touchend', unlock);
_this.unlocked = true;
});
};
document.body.addEventListener('touchstart', unlock, false);
document.body.addEventListener('touchend', unlock, false);
},
2018-01-06 18:19:37 +00:00
/**
2018-01-07 21:12:04 +00:00
* Method used internally for pausing sound manager if
* Phaser.Sound.WebAudioSoundManager#pauseOnBlur is set to true.
*
2018-01-06 18:19:37 +00:00
* @protected
* @method Phaser.Sound.WebAudioSoundManager#onBlur
*/
onBlur: function () {
this.context.suspend();
},
2018-01-06 18:19:48 +00:00
/**
2018-01-07 21:12:17 +00:00
* Method used internally for resuming sound manager if
* Phaser.Sound.WebAudioSoundManager#pauseOnBlur is set to true.
*
2018-01-06 18:19:48 +00:00
* @protected
* @method Phaser.Sound.WebAudioSoundManager#onFocus
*/
onFocus: function () {
this.context.resume();
},
2018-01-07 19:20:32 +00:00
/**
* Calls Phaser.Sound.BaseSoundManager#destroy method
* and cleans up all Web Audio API related stuff.
*
* @method Phaser.Sound.WebAudioSoundManager#destroy
*/
destroy: function () {
BaseSoundManager.prototype.destroy.call(this);
this.destination = null;
this.masterVolumeNode.disconnect();
this.masterVolumeNode = null;
this.masterMuteNode.disconnect();
this.masterMuteNode = null;
this.context.suspend();
this.context = null;
2017-11-10 11:55:32 +00:00
}
});
/**
* Global mute setting.
2018-01-06 18:20:13 +00:00
*
* @name Phaser.Sound.WebAudioSoundManager#mute
* @property {boolean} mute
*/
Object.defineProperty(WebAudioSoundManager.prototype, 'mute', {
get: function () {
return this.masterMuteNode.gain.value === 0;
},
set: function (value) {
this.masterMuteNode.gain.setValueAtTime(value ? 0 : 1, 0);
this.emit('mute', this, value);
}
});
/**
* Global volume setting.
2018-01-06 18:20:28 +00:00
*
* @name Phaser.Sound.WebAudioSoundManager#volume
* @property {number} volume
*/
Object.defineProperty(WebAudioSoundManager.prototype, 'volume', {
get: function () {
return this.masterVolumeNode.gain.value;
},
set: function (value) {
this.masterVolumeNode.gain.setValueAtTime(value, 0);
this.emit('volume', this, value);
}
});
2017-11-10 11:55:32 +00:00
module.exports = WebAudioSoundManager;