phaser/v3/src/sound/WebAudioSoundManager.js

122 lines
3.5 KiB
JavaScript
Raw Normal View History

2017-11-10 11:55:32 +00:00
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) {
2017-11-10 11:55:32 +00:00
/**
* The AudioContext being used for playback.
*
* @property {AudioContext} context
2017-11-10 11:55:32 +00:00
*/
this.context = this.createAudioContext(game);
/**
* [description]
*
* @property {GainNode} masterMuteNode
*/
this.masterMuteNode = this.context.createGain();
/**
* [description]
*
* @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.
*
* @property {AudioNode} destination
*/
this.destination = this.masterMuteNode;
/**
* Property that actually holds the value of global playback rate.
*
* @property {number} _rate
* @private
*/
this._rate = 1;
/**
* Property that actually holds the value of global detune.
*
* @property {number} _detune
* @private
*/
this._detune = 0;
BaseSoundManager.call(this, game);
2017-11-10 11:55:32 +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 (window['AudioContext'] || window['webkitAudioContext'])();
},
add: function (key, config) {
var sound = new WebAudioSound(this, key, config);
this.sounds.push(sound);
return sound;
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.value = value ? 0 : 1;
}
});
/**
* Global volume setting.
* @property {number} volume
*/
Object.defineProperty(WebAudioSoundManager.prototype, 'volume', {
get: function () {
return this.masterVolumeNode.gain.value;
},
set: function (value) {
this.masterVolumeNode.gain.value = value;
}
});
/**
* Global playback rate.
* @property {number} rate
*/
Object.defineProperty(WebAudioSoundManager.prototype, 'rate', {
get: function () {
return this._rate;
},
set: function (value) {
this._rate = value;
this.sounds.forEach(function (sound) {
// invoke sound's rate setter method to update
// value based on new global rate value
sound.rate = sound.rate;
}, this);
}
});
/**
* Global detune.
* @property {number} detune
*/
Object.defineProperty(WebAudioSoundManager.prototype, 'detune', {
get: function () {
return this._detune;
},
set: function (value) {
this._detune = value;
this.sounds.forEach(function (sound) {
// invoke sound's detune setter method to update
// value based on new global detune value
sound.detune = sound.detune;
}, this);
}
});
2017-11-10 11:55:32 +00:00
module.exports = WebAudioSoundManager;