phaser/v3/src/sound/WebAudioSound.js

91 lines
2.6 KiB
JavaScript
Raw Normal View History

2017-11-14 15:27:22 +00:00
var Class = require('../utils/Class');
var BaseSound = require('./BaseSound');
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) {
/**
* [description]
*
* @property {AudioBuffer} audioBuffer
*/
this.audioBuffer = manager.game.cache.audio.get(key);
if (!this.audioBuffer) {
console.error('No audio loaded in cache with key: \'' + key + '\'!');
return;
}
/**
* [description]
*
* @property {AudioBufferSourceNode} source
*/
this.source = null;
/**
* [description]
*
* @property {GainNode} volumeNode
*/
this.volumeNode = manager.context.createGain();
/**
* [description]
*
* @property {GainNode} muteNode
*/
this.muteNode = manager.context.createGain();
this.volumeNode.connect(this.muteNode);
this.muteNode.connect(manager.destination);
if (config === void 0) {
config = {};
}
config.duration = this.audioBuffer.duration;
2017-11-14 15:27:22 +00:00
BaseSound.call(this, manager, key, config);
},
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);
}
}
this.source = this.manager.context.createBufferSource();
// TODO assign config values to buffer source
this.source.buffer = this.audioBuffer;
this.source.connect(this.volumeNode);
this.source.start();
return this;
2017-11-14 15:27:22 +00:00
}
});
/**
* Volume setting.
* @property {number} volume
*/
Object.defineProperty(WebAudioSound.prototype, 'volume', {
get: function () {
return this.volumeNode.gain.value;
},
set: function (value) {
this.volumeNode.gain.value = value;
}
});
/**
* 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;