diff --git a/libs/music/sounds.ts b/libs/music/sounds.ts index 44c7cef0..8fe1a906 100644 --- a/libs/music/sounds.ts +++ b/libs/music/sounds.ts @@ -257,7 +257,7 @@ namespace sounds { namespace music { let numSoundsPlaying = 0; - let soundsLimit = 3; + const soundsLimit = 1; /** * Plays a sound diff --git a/sim/state/sounds.ts b/sim/state/sounds.ts index 4aa21738..a43aacde 100644 --- a/sim/state/sounds.ts +++ b/sim/state/sounds.ts @@ -6,6 +6,8 @@ namespace pxsim.music { } namespace pxsim.SoundMethods { + let numSoundsPlaying = 0; + const soundsLimit = 1; export function buffer(buf: RefBuffer) { return incr(buf) @@ -20,12 +22,17 @@ namespace pxsim.SoundMethods { } export function play(buf: RefBuffer, volume: number) { + if (!buf || numSoundsPlaying >= soundsLimit) { + return Promise.resolve(); + } return new Promise(resolve => { let url = "data:audio/wav;base64," + btoa(uint8ArrayToString(buf.data)) let audio = new Audio(url) audio.onended = () => { - resolve() + resolve(); + numSoundsPlaying--; } + numSoundsPlaying++; audio.play() }) }