2017-11-17 13:17:59 +00:00
|
|
|
var Class = require('../../utils/Class');
|
|
|
|
var BaseSound = require('../BaseSound');
|
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]
|
|
|
|
*
|
2017-11-15 16:37:22 +00:00
|
|
|
* @property {GainNode} muteNode
|
2017-11-15 14:42:37 +00:00
|
|
|
*/
|
2017-11-15 16:37:22 +00:00
|
|
|
this.muteNode = manager.context.createGain();
|
2017-11-15 15:02:11 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
2017-11-15 16:37:22 +00:00
|
|
|
* @property {GainNode} volumeNode
|
2017-11-15 15:02:11 +00:00
|
|
|
*/
|
2017-11-15 16:37:22 +00:00
|
|
|
this.volumeNode = manager.context.createGain();
|
|
|
|
this.muteNode.connect(this.volumeNode);
|
|
|
|
this.volumeNode.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) {
|
2017-11-16 13:19:04 +00:00
|
|
|
if (!BaseSound.prototype.play.call(this, marker, config)) {
|
|
|
|
return null;
|
2017-11-14 18:35:18 +00:00
|
|
|
}
|
2017-11-15 15:55:13 +00:00
|
|
|
this.source = this.manager.context.createBufferSource();
|
|
|
|
this.source.buffer = this.audioBuffer;
|
2017-11-15 16:37:22 +00:00
|
|
|
this.source.connect(this.muteNode);
|
2017-11-16 12:32:35 +00:00
|
|
|
this.applyConfig();
|
2017-11-15 15:55:13 +00:00
|
|
|
this.source.start();
|
2017-11-14 18:35:18 +00:00
|
|
|
return this;
|
2017-11-16 13:23:04 +00:00
|
|
|
},
|
|
|
|
pause: function () {
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
resume: function () {
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
stop: function () {
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
update: function () {
|
|
|
|
},
|
|
|
|
destroy: function () {
|
2017-11-14 15:27:22 +00:00
|
|
|
}
|
|
|
|
});
|
2017-11-15 14:58:38 +00:00
|
|
|
/**
|
2017-11-15 16:37:22 +00:00
|
|
|
* Mute setting.
|
|
|
|
* @property {boolean} mute
|
2017-11-15 14:58:38 +00:00
|
|
|
*/
|
2017-11-15 16:37:22 +00:00
|
|
|
Object.defineProperty(WebAudioSound.prototype, 'mute', {
|
2017-11-15 14:58:38 +00:00
|
|
|
get: function () {
|
2017-11-15 16:37:22 +00:00
|
|
|
return this.muteNode.gain.value === 0;
|
2017-11-15 14:58:38 +00:00
|
|
|
},
|
|
|
|
set: function (value) {
|
2017-11-16 12:09:43 +00:00
|
|
|
this.currentConfig.mute = value;
|
2017-11-15 16:37:22 +00:00
|
|
|
this.muteNode.gain.value = value ? 0 : 1;
|
2017-11-15 14:58:38 +00:00
|
|
|
}
|
|
|
|
});
|
2017-11-15 15:14:04 +00:00
|
|
|
/**
|
2017-11-15 16:37:22 +00:00
|
|
|
* Volume setting.
|
|
|
|
* @property {number} volume
|
2017-11-15 15:14:04 +00:00
|
|
|
*/
|
2017-11-15 16:37:22 +00:00
|
|
|
Object.defineProperty(WebAudioSound.prototype, 'volume', {
|
2017-11-15 15:14:04 +00:00
|
|
|
get: function () {
|
2017-11-15 16:37:22 +00:00
|
|
|
return this.volumeNode.gain.value;
|
2017-11-15 15:14:04 +00:00
|
|
|
},
|
|
|
|
set: function (value) {
|
2017-11-16 12:09:43 +00:00
|
|
|
this.currentConfig.volume = value;
|
2017-11-15 16:37:22 +00:00
|
|
|
this.volumeNode.gain.value = value;
|
2017-11-15 15:14:04 +00:00
|
|
|
}
|
|
|
|
});
|
2017-11-16 14:21:57 +00:00
|
|
|
/**
|
|
|
|
* Playback rate.
|
|
|
|
* @property {number} rate
|
|
|
|
*/
|
|
|
|
Object.defineProperty(WebAudioSound.prototype, 'rate', {
|
|
|
|
get: function () {
|
|
|
|
return this.currentConfig.rate;
|
|
|
|
},
|
|
|
|
set: function (value) {
|
|
|
|
this.currentConfig.rate = value;
|
|
|
|
if (this.source) {
|
|
|
|
this.source.playbackRate.value = value * this.manager.rate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2017-11-16 16:25:06 +00:00
|
|
|
/**
|
|
|
|
* Detuning of sound.
|
|
|
|
* @property {number} detune
|
|
|
|
*/
|
|
|
|
Object.defineProperty(WebAudioSound.prototype, 'detune', {
|
|
|
|
get: function () {
|
|
|
|
return this.currentConfig.detune;
|
|
|
|
},
|
|
|
|
set: function (value) {
|
|
|
|
this.currentConfig.detune = value;
|
|
|
|
if (this.source) {
|
|
|
|
this.source.detune.value =
|
|
|
|
Math.max(-1200, Math.min(value + this.manager.detune, 1200));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2017-11-14 15:27:22 +00:00
|
|
|
module.exports = WebAudioSound;
|