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
|
2017-11-17 16:07:04 +00:00
|
|
|
// TODO support webkitAudioContext implementation differences
|
2017-11-17 16:02:11 +00:00
|
|
|
// https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API/Porting_webkitAudioContext_code_to_standards_based_AudioContext
|
2017-11-14 15:27:22 +00:00
|
|
|
var WebAudioSound = new Class({
|
|
|
|
Extends: BaseSound,
|
|
|
|
initialize: function WebAudioSound(manager, key, config) {
|
2017-11-22 16:58:05 +00:00
|
|
|
if (config === void 0) { 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();
|
2017-11-17 13:57:57 +00:00
|
|
|
/**
|
|
|
|
* The time the previous playback started at based on
|
|
|
|
* BaseAudioContext.currentTime value.
|
|
|
|
*
|
|
|
|
* @property {number} startTime
|
|
|
|
*/
|
|
|
|
this.startTime = 0;
|
2017-11-17 14:33:42 +00:00
|
|
|
/**
|
|
|
|
* Relative time when sound was paused.
|
|
|
|
* Corresponds to the seek value at the time when pause() method was called on this sound.
|
|
|
|
*
|
|
|
|
* @property {number} pausedTime
|
|
|
|
*/
|
|
|
|
this.pausedTime = 0;
|
2017-11-26 16:03:36 +00:00
|
|
|
/**
|
|
|
|
* Used for keeping track when sound source playback has ended
|
|
|
|
* so it's state can be updated accordingly.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @property {boolean} hasEnded
|
|
|
|
*/
|
|
|
|
this.hasEnded = false;
|
2017-11-15 16:37:22 +00:00
|
|
|
this.muteNode.connect(this.volumeNode);
|
|
|
|
this.volumeNode.connect(manager.destination);
|
2017-11-14 15:27:22 +00:00
|
|
|
BaseSound.call(this, manager, key, config);
|
2017-11-22 16:54:00 +00:00
|
|
|
this.duration = this.audioBuffer.duration;
|
|
|
|
this.totalDuration = this.audioBuffer.duration;
|
2017-11-14 18:35:18 +00:00
|
|
|
},
|
2017-11-22 17:06:21 +00:00
|
|
|
play: function (markerName, config) {
|
|
|
|
if (!BaseSound.prototype.play.call(this, markerName, config)) {
|
2017-11-16 13:19:04 +00:00
|
|
|
return null;
|
2017-11-14 18:35:18 +00:00
|
|
|
}
|
2017-11-17 17:38:23 +00:00
|
|
|
this.stopAndRemoveBufferSource();
|
2017-11-23 12:55:27 +00:00
|
|
|
// TODO include config seek
|
2017-11-23 12:53:57 +00:00
|
|
|
var offset = this.currentMarker ? this.currentMarker.start : 0;
|
|
|
|
var duration = this.duration;
|
|
|
|
this.createAndStartBufferSource(offset, duration);
|
2017-11-17 14:29:49 +00:00
|
|
|
this.startTime = this.manager.context.currentTime;
|
2017-11-17 14:42:22 +00:00
|
|
|
this.pausedTime = 0;
|
2017-11-14 18:35:18 +00:00
|
|
|
return this;
|
2017-11-16 13:23:04 +00:00
|
|
|
},
|
|
|
|
pause: function () {
|
2017-11-17 16:17:06 +00:00
|
|
|
if (!BaseSound.prototype.pause.call(this)) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-11-17 16:07:04 +00:00
|
|
|
this.stopAndRemoveBufferSource();
|
2017-11-17 14:42:22 +00:00
|
|
|
this.pausedTime = this.manager.context.currentTime - this.startTime;
|
2017-11-17 16:17:06 +00:00
|
|
|
return true;
|
2017-11-16 13:23:04 +00:00
|
|
|
},
|
|
|
|
resume: function () {
|
2017-11-17 16:17:06 +00:00
|
|
|
if (!BaseSound.prototype.resume.call(this)) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-11-23 12:55:08 +00:00
|
|
|
var offset = (this.currentMarker ? this.currentMarker.start : 0) + this.pausedTime;
|
2017-11-22 16:56:55 +00:00
|
|
|
var duration = this.duration - this.pausedTime;
|
2017-11-17 16:01:12 +00:00
|
|
|
this.createAndStartBufferSource(offset, duration);
|
2017-11-17 14:42:22 +00:00
|
|
|
this.startTime = this.manager.context.currentTime - this.pausedTime;
|
|
|
|
this.pausedTime = 0;
|
2017-11-17 16:17:06 +00:00
|
|
|
return true;
|
2017-11-16 13:23:04 +00:00
|
|
|
},
|
|
|
|
stop: function () {
|
2017-11-17 16:17:06 +00:00
|
|
|
if (!BaseSound.prototype.stop.call(this)) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-11-17 16:07:04 +00:00
|
|
|
this.stopAndRemoveBufferSource();
|
2017-11-17 14:05:17 +00:00
|
|
|
this.startTime = 0;
|
2017-11-17 14:42:22 +00:00
|
|
|
this.pausedTime = 0;
|
2017-11-17 16:17:06 +00:00
|
|
|
return true;
|
2017-11-16 13:23:04 +00:00
|
|
|
},
|
2017-11-17 16:01:12 +00:00
|
|
|
/**
|
|
|
|
* Used internally to do what the name says.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {number} offset
|
|
|
|
* @param {number} duration
|
|
|
|
*/
|
2017-11-22 17:07:08 +00:00
|
|
|
// TODO add when param
|
2017-11-17 16:01:12 +00:00
|
|
|
createAndStartBufferSource: function (offset, duration) {
|
|
|
|
this.source = this.manager.context.createBufferSource();
|
|
|
|
this.source.buffer = this.audioBuffer;
|
|
|
|
this.source.connect(this.muteNode);
|
|
|
|
this.applyConfig();
|
2017-11-26 15:24:49 +00:00
|
|
|
this.source.onended = function (ev) {
|
2017-11-26 15:35:13 +00:00
|
|
|
if (ev.target === this.source) {
|
2017-11-26 15:37:03 +00:00
|
|
|
// sound ended
|
2017-11-26 16:07:05 +00:00
|
|
|
this.hasEnded = true;
|
2017-11-26 15:27:39 +00:00
|
|
|
}
|
2017-11-26 15:37:03 +00:00
|
|
|
// else was stopped
|
2017-11-26 15:24:49 +00:00
|
|
|
}.bind(this);
|
2017-11-17 16:01:12 +00:00
|
|
|
this.source.start(0, offset, duration);
|
|
|
|
},
|
2017-11-17 16:07:04 +00:00
|
|
|
/**
|
|
|
|
* Used internally to do what the name says.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
stopAndRemoveBufferSource: function () {
|
2017-11-17 17:38:23 +00:00
|
|
|
if (this.source) {
|
2017-11-26 15:35:13 +00:00
|
|
|
this.source.stop();
|
2017-11-17 17:38:23 +00:00
|
|
|
this.source = null;
|
|
|
|
}
|
2017-11-17 16:07:04 +00:00
|
|
|
},
|
2017-11-16 13:23:04 +00:00
|
|
|
update: function () {
|
2017-11-26 16:05:24 +00:00
|
|
|
if (this.hasEnded) {
|
|
|
|
this.stop();
|
|
|
|
}
|
2017-11-16 13:23:04 +00:00
|
|
|
},
|
|
|
|
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-21 18:47:27 +00:00
|
|
|
this.muteNode.gain.setValueAtTime(value ? 0 : 1, 0);
|
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-21 18:47:27 +00:00
|
|
|
this.volumeNode.gain.setValueAtTime(value, 0);
|
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) {
|
2017-11-21 18:47:27 +00:00
|
|
|
this.source.playbackRate.setValueAtTime(value * this.manager.rate, 0);
|
2017-11-16 14:21:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
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;
|
2017-11-20 13:54:33 +00:00
|
|
|
if (this.source && this.source.detune) {
|
2017-11-21 18:47:27 +00:00
|
|
|
this.source.detune.setValueAtTime(Math.max(-1200, Math.min(value + this.manager.detune, 1200)), 0);
|
2017-11-16 16:25:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2017-11-14 15:27:22 +00:00
|
|
|
module.exports = WebAudioSound;
|