Merge branch 'sound-engine'

This commit is contained in:
Evan Pratten 2020-04-18 13:34:16 -04:00
commit 4d8888a8ad
2 changed files with 20 additions and 8 deletions

View File

@ -12,9 +12,12 @@
* // ... * // ...
* }); * });
* *
* // Play a sound * // Play a sound using channels
* globalSoundContext.playSound(globalSoundContext.channels.<channel>, sounds.<soundname>); * globalSoundContext.playSound(globalSoundContext.channels.<channel>, sounds.<soundname>);
* *
* // Just play a sound now
* globalSoundContext.playSoundNow(sounds.<soundname>);
*
* // Stop a channel * // Stop a channel
* globalSoundContext.mute(globalSoundContext.channels.<channel>); * globalSoundContext.mute(globalSoundContext.channels.<channel>);
*/ */
@ -39,10 +42,12 @@ class _SoundChannel {
*/ */
enqueue(snippet) { enqueue(snippet) {
console.log(this.sound_queue)
// If the queue is full, cut out the next sound in the queue to make room // If the queue is full, cut out the next sound in the queue to make room
if (this.sound_queue.length >= this.max_size) { // if (this.sound_queue.length > this.max_size) {
this.sound_queue.splice(1, 1); // this.sound_queue.splice(1, 1);
} // }
// Append the sound to the queue // Append the sound to the queue
this.sound_queue.push(snippet); this.sound_queue.push(snippet);
@ -66,7 +71,7 @@ class _SoundChannel {
// Spawn a clean action for that time in the future // Spawn a clean action for that time in the future
setTimeout(() => { setTimeout(() => {
this._cleanQueue(snippet); this._cleanQueue(snippet);
}, length); }, length * 1000);
} }
/** /**
@ -92,7 +97,6 @@ class _SoundChannel {
} }
// Spawn a watcher for the next sound & play it // Spawn a watcher for the next sound & play it
if (this.sound_queue.length > 0) { if (this.sound_queue.length > 0) {
this.sound_queue[0].play(); this.sound_queue[0].play();
@ -124,7 +128,7 @@ class _SoundContext {
// Define all sound channels // Define all sound channels
this.channels = { this.channels = {
bgm: new _SoundChannel(2) bgm: new _SoundChannel(3)
} }
} }
@ -138,6 +142,14 @@ class _SoundContext {
channel.enqueue(snippet); channel.enqueue(snippet);
} }
/**
* Play a sound right now
* @param {SoundSnippet} snippet
*/
playSoundNow(snippet) {
snippet.play();
}
/** /**
* Stop all sounds in a channel * Stop all sounds in a channel
* @param {*} channel * @param {*} channel

View File

@ -62,7 +62,7 @@ class SoundSnippet {
* Get the sound length in seconds * Get the sound length in seconds
*/ */
getLengthSeconds() { getLengthSeconds() {
return 0; return this.audio.duration;
} }
/** /**