This commit is contained in:
Richard Davey 2017-11-23 01:46:02 +00:00
commit 9516e6b188
3 changed files with 47 additions and 41 deletions

View file

@ -24,15 +24,7 @@ var BaseSound = new Class({
*
* @property {ISoundConfig} config
*/
this.config = {
/**
* A value representing the duration, in seconds.
* It could be total sound duration or a marker duration.
*
* @type {number}
*/
duration: 0
};
this.config = {};
/**
* Reference to the currently used config.
* It could be default config or marker config.
@ -90,6 +82,21 @@ var BaseSound = new Class({
*/
this.pan = 0;
this.config = Extend(this.config, config);
/**
* A value representing the duration, in seconds.
* It could be total sound duration or a marker duration.
*
* @readonly
* @property {number} duration
*/
this.duration = 0;
/**
* Duration of the entire sound.
*
* @readonly
* @property {number}
*/
this.totalDuration = 0;
/**
* Flag indicating if sound is currently playing.
*
@ -109,13 +116,12 @@ var BaseSound = new Class({
*/
this.markers = {};
/**
* Name of the currently played marker.
* If no marker is played, but instead the whole sound
* the value is an empty string - ''.
* Currently playing marker.
* 'null' if whole sound is playing.
*
* @property {string} currentMarker
* @property {ISoundMarker} currentMarker
*/
this.currentMarker = '';
this.currentMarker = null;
/**
* [description]
*
@ -137,26 +143,28 @@ var BaseSound = new Class({
removeMarker: function (markerName) {
return false;
},
play: function (marker, config) {
if (marker === void 0) { marker = ''; }
if (typeof marker === 'object') {
config = marker;
marker = '';
play: function (markerName, config) {
if (markerName === void 0) { markerName = ''; }
if (typeof markerName === 'object') {
config = markerName;
markerName = '';
}
if (typeof marker !== 'string') {
if (typeof markerName !== 'string') {
console.error('Sound marker name has to be a string!');
return null;
}
if (!marker) {
if (!markerName) {
this.currentConfig = this.config;
this.duration = this.totalDuration;
}
else {
if (!this.markers[marker]) {
console.error('No marker with name \'' + marker + '\' found for sound \'' + this.key + '\'!');
if (!this.markers[markerName]) {
console.error('No marker with name \'' + markerName + '\' found for sound \'' + this.key + '\'!');
return null;
}
this.currentMarker = marker;
this.currentConfig = this.markers[marker].config;
this.currentMarker = this.markers[markerName];
this.currentConfig = this.currentMarker.config;
this.duration = this.currentMarker.duration;
}
this.currentConfig = Extend(this.currentConfig, config);
this.isPlaying = true;

View file

@ -6,6 +6,7 @@ var BaseSound = require('../BaseSound');
var WebAudioSound = new Class({
Extends: BaseSound,
initialize: function WebAudioSound(manager, key, config) {
if (config === void 0) { config = {}; }
/**
* [description]
*
@ -48,22 +49,19 @@ var WebAudioSound = new Class({
* @property {number} pausedTime
*/
this.pausedTime = 0;
// TODO add duration and total duration
this.muteNode.connect(this.volumeNode);
this.volumeNode.connect(manager.destination);
if (config === void 0) {
config = {};
}
config.duration = this.audioBuffer.duration;
BaseSound.call(this, manager, key, config);
this.duration = this.audioBuffer.duration;
this.totalDuration = this.audioBuffer.duration;
},
play: function (marker, config) {
if (!BaseSound.prototype.play.call(this, marker, config)) {
play: function (markerName, config) {
if (!BaseSound.prototype.play.call(this, markerName, config)) {
return null;
}
this.stopAndRemoveBufferSource();
// TODO include config offset and marker start
this.createAndStartBufferSource(0, this.currentConfig.duration);
this.createAndStartBufferSource(0, this.duration);
this.startTime = this.manager.context.currentTime;
this.pausedTime = 0;
return this;
@ -81,7 +79,7 @@ var WebAudioSound = new Class({
return false;
}
var offset = this.pausedTime; // TODO include marker start time
var duration = this.currentConfig.duration - this.pausedTime;
var duration = this.duration - this.pausedTime;
this.createAndStartBufferSource(offset, duration);
this.startTime = this.manager.context.currentTime - this.pausedTime;
this.pausedTime = 0;
@ -103,6 +101,7 @@ var WebAudioSound = new Class({
* @param {number} offset
* @param {number} duration
*/
// TODO add when param
createAndStartBufferSource: function (offset, duration) {
this.source = this.manager.context.createBufferSource();
this.source.buffer = this.audioBuffer;
@ -137,7 +136,7 @@ Object.defineProperty(WebAudioSound.prototype, 'mute', {
},
set: function (value) {
this.currentConfig.mute = value;
this.muteNode.gain.value = value ? 0 : 1;
this.muteNode.gain.setValueAtTime(value ? 0 : 1, 0);
}
});
/**
@ -150,7 +149,7 @@ Object.defineProperty(WebAudioSound.prototype, 'volume', {
},
set: function (value) {
this.currentConfig.volume = value;
this.volumeNode.gain.value = value;
this.volumeNode.gain.setValueAtTime(value, 0);
}
});
/**
@ -164,7 +163,7 @@ Object.defineProperty(WebAudioSound.prototype, 'rate', {
set: function (value) {
this.currentConfig.rate = value;
if (this.source) {
this.source.playbackRate.value = value * this.manager.rate;
this.source.playbackRate.setValueAtTime(value * this.manager.rate, 0);
}
}
});
@ -179,8 +178,7 @@ Object.defineProperty(WebAudioSound.prototype, 'detune', {
set: function (value) {
this.currentConfig.detune = value;
if (this.source && this.source.detune) {
this.source.detune.value =
Math.max(-1200, Math.min(value + this.manager.detune, 1200));
this.source.detune.setValueAtTime(Math.max(-1200, Math.min(value + this.manager.detune, 1200)), 0);
}
}
});

View file

@ -95,7 +95,7 @@ Object.defineProperty(WebAudioSoundManager.prototype, 'mute', {
return this.masterMuteNode.gain.value === 0;
},
set: function (value) {
this.masterMuteNode.gain.value = value ? 0 : 1;
this.masterMuteNode.gain.setValueAtTime(value ? 0 : 1, 0);
}
});
/**
@ -107,7 +107,7 @@ Object.defineProperty(WebAudioSoundManager.prototype, 'volume', {
return this.masterVolumeNode.gain.value;
},
set: function (value) {
this.masterVolumeNode.gain.value = value;
this.masterVolumeNode.gain.setValueAtTime(value, 0);
}
});
/**