2017-11-14 15:27:22 +00:00
|
|
|
var Class = require('../utils/Class');
|
|
|
|
var BaseSound = require('./BaseSound');
|
2017-11-14 18:35:18 +00:00
|
|
|
var Extend = require('../utils/object/Extend');
|
2017-11-14 15:27:22 +00:00
|
|
|
// Phaser.Sound.WebAudioSound
|
|
|
|
var WebAudioSound = new Class({
|
|
|
|
Extends: BaseSound,
|
|
|
|
initialize: function WebAudioSound(manager, key, config) {
|
2017-11-14 18:30:51 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {AudioBuffer} audioBuffer
|
|
|
|
*/
|
2017-11-14 19:09:44 +00:00
|
|
|
this.audioBuffer = manager.game.cache.audio.get(key);
|
2017-11-14 18:30:51 +00:00
|
|
|
if (!this.audioBuffer) {
|
|
|
|
console.error('No audio loaded in cache with key: \'' + key + '\'!');
|
|
|
|
return;
|
|
|
|
}
|
2017-11-15 15:55:13 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {AudioBufferSourceNode} source
|
|
|
|
*/
|
|
|
|
this.source = null;
|
2017-11-15 14:42:37 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {GainNode} volumeNode
|
|
|
|
*/
|
|
|
|
this.volumeNode = manager.context.createGain();
|
2017-11-15 15:02:11 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @property {GainNode} muteNode
|
|
|
|
*/
|
|
|
|
this.muteNode = manager.context.createGain();
|
2017-11-15 15:03:40 +00:00
|
|
|
this.volumeNode.connect(this.muteNode);
|
|
|
|
this.muteNode.connect(manager.destination);
|
2017-11-14 19:09:44 +00:00
|
|
|
if (config === void 0) {
|
|
|
|
config = {};
|
|
|
|
}
|
2017-11-14 18:30:51 +00:00
|
|
|
config.duration = this.audioBuffer.duration;
|
2017-11-14 15:27:22 +00:00
|
|
|
BaseSound.call(this, manager, key, config);
|
2017-11-14 18:35:18 +00:00
|
|
|
},
|
|
|
|
play: function (marker, config) {
|
|
|
|
if (marker === void 0) { marker = ''; }
|
|
|
|
if (typeof marker !== 'string') {
|
|
|
|
console.error('Sound marker has to be a string!');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (config) {
|
|
|
|
if (!marker) {
|
|
|
|
this.config = Extend(this.config, config);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.markers[marker].config = Extend(this.markers[marker].config, config);
|
|
|
|
}
|
|
|
|
}
|
2017-11-15 15:55:13 +00:00
|
|
|
this.source = this.manager.context.createBufferSource();
|
2017-11-14 18:35:18 +00:00
|
|
|
// TODO assign config values to buffer source
|
2017-11-15 15:55:13 +00:00
|
|
|
this.source.buffer = this.audioBuffer;
|
|
|
|
this.source.connect(this.volumeNode);
|
|
|
|
this.source.start();
|
2017-11-14 18:35:18 +00:00
|
|
|
return this;
|
2017-11-14 15:27:22 +00:00
|
|
|
}
|
|
|
|
});
|
2017-11-15 14:58:38 +00:00
|
|
|
/**
|
2017-11-15 15:14:04 +00:00
|
|
|
* Volume setting.
|
2017-11-15 14:58:38 +00:00
|
|
|
* @property {number} volume
|
|
|
|
*/
|
|
|
|
Object.defineProperty(WebAudioSound.prototype, 'volume', {
|
|
|
|
get: function () {
|
|
|
|
return this.volumeNode.gain.value;
|
|
|
|
},
|
|
|
|
set: function (value) {
|
|
|
|
this.volumeNode.gain.value = value;
|
|
|
|
}
|
|
|
|
});
|
2017-11-15 15:14:04 +00:00
|
|
|
/**
|
|
|
|
* Mute setting.
|
|
|
|
* @property {boolean} mute
|
|
|
|
*/
|
|
|
|
Object.defineProperty(WebAudioSound.prototype, 'mute', {
|
|
|
|
get: function () {
|
|
|
|
return this.muteNode.gain.value === 0;
|
|
|
|
},
|
|
|
|
set: function (value) {
|
|
|
|
this.muteNode.gain.value = value ? 0 : 1;
|
|
|
|
}
|
|
|
|
});
|
2017-11-14 15:27:22 +00:00
|
|
|
module.exports = WebAudioSound;
|