Desynchronization of sounds after they are playing in loop for a long time

Hi, I’ve noticed that after playing sounds in a loop for 3-4 minutes some of them starts playing faster than the other. Here is a demo: https://www.babylonjs-playground.com/#HU4BCZ#3 (in this demo it takes more than 4 minutes but musicFR starts to play later and overlaps with musicSR). I don’t know if thats a issiue in Web Audio API or BABYLONJS. I would appreciate any help with that issiue.

Hi @VGFP
First things first,
As it seems all sounds play consistently and looping, (rather than switching between them)
are you sure the sound clips are equal length? To a ms?
A slight difference will eventually be heard if this is not the case

3 Likes

Hi, Thank you for your answer but this is not an issue caused by wrong length of the audiofiles. Each one of them have the same length (exactly 389092 samples with sample rate 44100) 8.8229 s.

Hi,
if this is the case,
my best bet is the sounds slowly desync due to the actual execution time of looping,
(maybe someone else knows more/better)
I suggest reducing the clips to their actual length (remove sound-less padding),
then;
sound1.play()
onEnd() { sound 2.play() }
onEnd() { sound 3.play() }
etc

Something along the lines of, https://www.babylonjs-playground.com/#HU4BCZ#4
See lines #140 & #197 & #232

1 Like

I think it doesn’t repro for me. I’ve been listening to FL, FC, FR, RS, LS, … in a loop for a while (it’s getting a bit repetitive :slight_smile:) and nothing is overlapping. What browser/OS are you on? Given that it doesn’t desync for me, it’s probably not a Babylon.js issue?

Wait, I think it’s starting to overlap…

It looks like WebAudio doesn’t do gapless looping. I couldn’t find in the WebAudio specification whether the looping parameter is supposed to be gapless or not.

In order to implement it the way you are doing it, the looping must be gapless (sometimes called seamless) in order to ensure they all restart the loop at the same time.

Also, the way these audio clips are being played is not that efficient. There are two options I can think of:

  1. Play each sound as a one-shot sound using your own logic instead of looping.
  2. Play one sound in a loop with all of the voices and move them to the right spatial location at certain points in the sound.

Both of these are more efficient and should in theory fix the desync issue.

1 Like