Hands-on volume/playback control of individual sound instances

Hey there- I’m trying to determine if there’s a reasonable way to manage individual sound volumes. In particular, I have a hard requirement to be able to control volume on specific instances of a given sound.

Since it looks like multiple instances of a given sound tied to the same Sound component share their gain value, the only approach I could think of was:

  1. Build a Factory for each discrete sound that needs hands-on management
  2. Route sound requests to a central manager object
  3. Have the appropriate factory instantiate a game object wrapping a Sound component, cache the game object reference in the manager, and trigger the sound
  4. (optional) - central manager may send game object wrappers commands viz. gain, etc
  5. When the sound is completed, the clear its cached value in the manager and delete the game object attached to it

I think that in principle this should work (though I am not going to go out of my way to call it elegant- this is the closest thing I could think of to mimic Unity-style AudioSources that are distinct from the actual sounds they play)… my concern is that I’m not familiar enough with Defold to gauge if this is going to totally hose garbage collection or not.

Is instantiating/deleting ~50 Sound components and associated structure a second an obviously terrible idea?

Thanks!

2 Likes

In general it is not costly to create and delete many game objects since everything is allocated up front. Ragnar goes into a bit more detail here: Objects pooling for Factories (SOLVED)

Now, I’m not sure if instantiating sound components is more costly than other components, but I’m sure @Ragnar_Svensson, @Mathias_Westerdahl or @jakob.pogulis knows?

Edit: Since we specify maximum number of sound buffers, sources etc in game.project I assume all of this is also allocated up front, but maybe there is some additional overhead “binding” sounds to some native sound component, depending on the underlying platform?

2 Likes

Thanks for the quick response!

Based on that thread it seems like it’s worth trying at least.

2 Likes

Please do (and let us know how it goes)!

1 Like