Does a sound need to be cloned if played spatially at multiple locations?

For a scenario where we have a gun firing sound and 100 instances of a gun in different locations, do we need to clone the sound 100 times for it to be spatial to each unique gun? For example:

for (const gun of guns) {
  sound.cloneAsync({ cloneBuffer: false }).then(clonedSound => {
    clonedSound.spatial.position.copyFrom(gun.position);

    clonedSound.play();
  });
}

I also noticed that the FPS decreases rapidly over time, which may be due to creating so many clones and not cleaning them up? Would anyone know the recommended way to clean these up?

Thank you for your help!

@docEdub I’m guessing you have an answer for this one!

Yes, the spatialization node applies to instances of a sound, so you need a separate sound if you want it in a unique location.

Try sound.dispose: Babylon.js docs

Thank you so much @docEdub ! Would you recommend doing something like:

for (const gun of guns) {
  sound.cloneAsync({ cloneBuffer: false }).then(clonedSound => {
    clonedSound.spatial.position.copyFrom(gun.position);

    clonedSound.play();

    clonedSound.onEndedObservable.addOnce(() => {
        clonedSound.dispose();
    });
  });
}

Would creating an observable be the best way to find the right time to dispose the sound?

Thank you!

Yep!

1 Like