How to get access to all sounds in the scene in AudioEngine V2?

In V1 all sounds could be accessed through scene.sounds, but in V2 that object is null.
The audioEngine._nodes seems like containing the references, but it’s considered private. It also allows duplicates by name that don’t have any sort of unique id (as far as I can see).

Just did some experiments here for reference: https://playground.babylonjs.com/#GHJP10#2

scene.mainSoundTrack.soundCollection.forEach(sound => {
  console.log(sound)
});

Scene.sounds and Scene.mainSoundTrack are for the old audio engine. The new V2 audio engine does not use them.

There is currently no public property exposing the sounds list for the new audio engine but I will add one for you if you like!

That would be amazing if you could help us add the property :grinning_face_with_smiling_eyes:

Done!

3 Likes

Thank you so much, @docEdub !

Would you consider using a Set to track sounds instead of an Array? After this change, the cost of Sound.dispose()went from O(1) to O(n).

For my application, where I’m creating and disposing several sounds per frame, keepingSound.dispose()as O(1) would be a huge help.

To accommodate both performance and the new property, could we return a Set of sounds? Or if the returned property needs to be an Array, perhaps a ReadonlyArray could be generated from an internal Set?

Thank you so much for your help, docEdub!!

1 Like

Yep, it’s changed it to a Set internally now.

See Improve performance of audio engine V2 add/remove sound by docEdub · Pull Request #17397 · BabylonJS/Babylon.js · GitHub.

1 Like