Make some soundplay tweaks

This commit is contained in:
Evan Pratten 2020-04-18 13:32:20 -04:00
parent 69ef6261a0
commit efad6d67c6
No known key found for this signature in database
GPG Key ID: 93AC7B3D071356D3
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>);
*
* // Just play a sound now
* globalSoundContext.playSoundNow(sounds.<soundname>);
*
* // Stop a channel
* globalSoundContext.mute(globalSoundContext.channels.<channel>);
*/
@ -39,10 +42,12 @@ class _SoundChannel {
*/
enqueue(snippet) {
console.log(this.sound_queue)
// If the queue is full, cut out the next sound in the queue to make room
if (this.sound_queue.length >= this.max_size) {
this.sound_queue.splice(1, 1);
}
// if (this.sound_queue.length > this.max_size) {
// this.sound_queue.splice(1, 1);
// }
// Append the sound to the queue
this.sound_queue.push(snippet);
@ -66,7 +71,7 @@ class _SoundChannel {
// Spawn a clean action for that time in the future
setTimeout(() => {
this._cleanQueue(snippet);
}, length);
}, length * 1000);
}
/**
@ -92,7 +97,6 @@ class _SoundChannel {
}
// Spawn a watcher for the next sound & play it
if (this.sound_queue.length > 0) {
this.sound_queue[0].play();
@ -124,7 +128,7 @@ class _SoundContext {
// Define all sound channels
this.channels = {
bgm: new _SoundChannel(2)
bgm: new _SoundChannel(3)
}
}
@ -138,6 +142,14 @@ class _SoundContext {
channel.enqueue(snippet);
}
/**
* Play a sound right now
* @param {SoundSnippet} snippet
*/
playSoundNow(snippet) {
snippet.play();
}
/**
* Stop all sounds in a channel
* @param {*} channel

View File

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