2017-11-10 11:55:32 +00:00
|
|
|
var Class = require('../utils/Class');
|
2017-11-10 12:05:29 +00:00
|
|
|
var BaseSoundManager = require('./BaseSoundManager');
|
2017-11-14 16:35:44 +00:00
|
|
|
var WebAudioSound = require('./WebAudioSound');
|
2017-11-10 12:05:29 +00:00
|
|
|
// Phaser.Loader.WebAudioSoundManager
|
2017-11-10 11:55:32 +00:00
|
|
|
var WebAudioSoundManager = new Class({
|
2017-11-10 12:05:29 +00:00
|
|
|
Extends: BaseSoundManager,
|
2017-11-14 15:49:01 +00:00
|
|
|
initialize: function WebAudioSoundManager(game) {
|
2017-11-10 11:55:32 +00:00
|
|
|
/**
|
2017-11-15 13:38:45 +00:00
|
|
|
* The AudioContext being used for playback.
|
|
|
|
*
|
|
|
|
* @property {AudioContext} context
|
2017-11-10 11:55:32 +00:00
|
|
|
*/
|
2017-11-15 14:11:37 +00:00
|
|
|
this.context = this.createAudioContext(game);
|
2017-11-15 16:49:23 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {GainNode} masterMuteNode
|
|
|
|
*/
|
|
|
|
this.masterMuteNode = this.context.createGain();
|
2017-11-15 13:38:45 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {GainNode} masterVolumeNode
|
|
|
|
*/
|
|
|
|
this.masterVolumeNode = this.context.createGain();
|
2017-11-15 16:49:23 +00:00
|
|
|
this.masterMuteNode.connect(this.masterVolumeNode);
|
|
|
|
this.masterVolumeNode.connect(this.context.destination);
|
2017-11-15 13:46:12 +00:00
|
|
|
/**
|
|
|
|
* Destination node for connecting individual sounds to.
|
|
|
|
*
|
|
|
|
* @property {AudioNode} destination
|
|
|
|
*/
|
2017-11-15 16:49:23 +00:00
|
|
|
this.destination = this.masterMuteNode;
|
2017-11-16 14:44:56 +00:00
|
|
|
/**
|
|
|
|
* Property that actually holds the value of global playback rate.
|
|
|
|
*
|
|
|
|
* @property {number} _rate
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._rate = 1;
|
2017-11-16 16:23:26 +00:00
|
|
|
/**
|
|
|
|
* Property that actually holds the value of global detune.
|
|
|
|
*
|
|
|
|
* @property {number} _detune
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._detune = 0;
|
2017-11-15 14:11:37 +00:00
|
|
|
BaseSoundManager.call(this, game);
|
2017-11-10 11:55:32 +00:00
|
|
|
},
|
2017-11-15 14:11:37 +00:00
|
|
|
createAudioContext: function (game) {
|
|
|
|
var audioConfig = game.config.audio;
|
2017-11-14 15:49:01 +00:00
|
|
|
if (audioConfig && audioConfig.context) {
|
2017-11-10 18:05:26 +00:00
|
|
|
return audioConfig.context;
|
2017-11-10 11:55:32 +00:00
|
|
|
}
|
|
|
|
return new (window['AudioContext'] || window['webkitAudioContext'])();
|
2017-11-14 16:35:44 +00:00
|
|
|
},
|
|
|
|
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
|
|
|
}
|
|
|
|
});
|
2017-11-15 14:11:37 +00:00
|
|
|
/**
|
2017-11-15 16:49:23 +00:00
|
|
|
* Global mute setting.
|
|
|
|
* @property {boolean} mute
|
2017-11-15 14:11:37 +00:00
|
|
|
*/
|
2017-11-15 16:49:23 +00:00
|
|
|
Object.defineProperty(WebAudioSoundManager.prototype, 'mute', {
|
2017-11-15 14:11:37 +00:00
|
|
|
get: function () {
|
2017-11-15 16:49:23 +00:00
|
|
|
return this.masterMuteNode.gain.value === 0;
|
2017-11-15 14:11:37 +00:00
|
|
|
},
|
|
|
|
set: function (value) {
|
2017-11-15 16:49:23 +00:00
|
|
|
this.masterMuteNode.gain.value = value ? 0 : 1;
|
2017-11-15 14:11:37 +00:00
|
|
|
}
|
|
|
|
});
|
2017-11-15 14:31:24 +00:00
|
|
|
/**
|
2017-11-15 16:49:23 +00:00
|
|
|
* Global volume setting.
|
|
|
|
* @property {number} volume
|
2017-11-15 14:31:24 +00:00
|
|
|
*/
|
2017-11-15 16:49:23 +00:00
|
|
|
Object.defineProperty(WebAudioSoundManager.prototype, 'volume', {
|
2017-11-15 14:31:24 +00:00
|
|
|
get: function () {
|
2017-11-15 16:49:23 +00:00
|
|
|
return this.masterVolumeNode.gain.value;
|
2017-11-15 14:31:24 +00:00
|
|
|
},
|
|
|
|
set: function (value) {
|
2017-11-15 16:49:23 +00:00
|
|
|
this.masterVolumeNode.gain.value = value;
|
2017-11-15 14:31:24 +00:00
|
|
|
}
|
|
|
|
});
|
2017-11-16 14:44:56 +00:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
});
|
2017-11-16 16:24:20 +00:00
|
|
|
/**
|
|
|
|
* 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;
|