Limit sound concurrency to 1

This commit is contained in:
System Administrator 2017-12-07 16:27:28 -08:00
parent 2c22ea925f
commit 193f66fd2d
2 changed files with 12 additions and 1 deletions

View File

@ -257,7 +257,11 @@ namespace sounds {
namespace music { namespace music {
let numSoundsPlaying = 0; let numSoundsPlaying = 0;
<<<<<<< Updated upstream
let soundsLimit = 3; let soundsLimit = 3;
=======
const soundsLimit = 1;
>>>>>>> Stashed changes
/** /**
* Plays a sound * Plays a sound

View File

@ -6,6 +6,8 @@ namespace pxsim.music {
} }
namespace pxsim.SoundMethods { namespace pxsim.SoundMethods {
let numSoundsPlaying = 0;
const soundsLimit = 1;
export function buffer(buf: RefBuffer) { export function buffer(buf: RefBuffer) {
return incr(buf) return incr(buf)
@ -20,12 +22,17 @@ namespace pxsim.SoundMethods {
} }
export function play(buf: RefBuffer, volume: number) { export function play(buf: RefBuffer, volume: number) {
if (numSoundsPlaying >= soundsLimit) {
return Promise.resolve();
}
return new Promise<void>(resolve => { return new Promise<void>(resolve => {
let url = "data:audio/wav;base64," + btoa(uint8ArrayToString(buf.data)) let url = "data:audio/wav;base64," + btoa(uint8ArrayToString(buf.data))
let audio = new Audio(url) let audio = new Audio(url)
audio.onended = () => { audio.onended = () => {
resolve() resolve();
numSoundsPlaying--;
} }
numSoundsPlaying++;
audio.play() audio.play()
}) })
} }