phaser/v3/src/sound/webaudio/WebAudioSoundManager.js

116 lines
3.6 KiB
JavaScript
Raw Normal View History

var Class = require('../../utils/Class');
var BaseSoundManager = require('../BaseSoundManager');
var WebAudioSound = require('./WebAudioSound');
// Phaser.Loader.WebAudioSoundManager
2017-11-10 11:55:32 +00:00
var WebAudioSoundManager = new Class({
Extends: BaseSoundManager,
initialize: function WebAudioSoundManager(game) {
/**
* Flag indicating if Web Audio implementation is webkit or standards based.
* There are certain cases where they have to be handled differently.
*
* https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API/Porting_webkitAudioContext_code_to_standards_based_AudioContext
*
* @private
* @property {boolean} webkit
*/
this.webkit = !!window['webkitAudioContext'] && !window['AudioContext'];
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);
/**
* [description]
*
* @private
* @property {GainNode} masterMuteNode
*/
this.masterMuteNode = this.context.createGain();
/**
* [description]
*
* @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.unlock();
BaseSoundManager.call(this, game);
2017-11-10 11:55:32 +00:00
},
2017-11-26 15:19:56 +00:00
/**
* @private
* @param game
* @returns {AudioContext}
*/
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 (window['AudioContext'] || window['webkitAudioContext'])();
},
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
/**
* @private
*/
unlock: function () {
var _this = this;
if (this.context.state === 'suspended') {
var unlock_1 = function () {
_this.context.resume().then(function () {
document.body.removeEventListener('touchstart', unlock_1);
document.body.removeEventListener('touchend', unlock_1);
});
};
document.body.addEventListener('touchstart', unlock_1, false);
document.body.addEventListener('touchend', unlock_1, false);
}
},
onBlur: function () {
this.context.suspend();
},
onFocus: function () {
this.context.resume();
2017-11-10 11:55:32 +00:00
}
});
/**
* Global mute setting.
* @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);
}
});
/**
* Global volume setting.
* @property {number} volume
*/
Object.defineProperty(WebAudioSoundManager.prototype, 'volume', {
get: function () {
return this.masterVolumeNode.gain.value;
},
set: function (value) {
this.masterVolumeNode.gain.setValueAtTime(value, 0);
}
});
2017-11-10 11:55:32 +00:00
module.exports = WebAudioSoundManager;