Search code examples
web-audio-api

Are there any pragmatic limits to the number of nodes in Web Audio API?


Context :

  • A game which features dozens of units capable of doing many different things which make noise.
  • For more realism, the sounds are mixed together with gain nodes and panner nodes to take space into account.

This approach generates hundreds of nodes at runtime.

It's not a audio mess only because depending on the positions of the units and the camera most of them actually have a zero gain.

It works just fine on my hardware, but I would like the game to be able to run on low end hardwares.

Can I stay with a lot of nodes with a zero gain, or do I have to optimize upfront and present only nodes with a non zero gain to the web audio API to avoid performance issues ?

I can't find any information about limits in the documentation...


Solution

  • Generally, browsers do a good job when a gain node has zero gain. All nodes connected to the gain node will expend CPU of course, but all nodes after the gain node can be optimized because it is known that the output of the gain node is 0. In many cases the only processing is noting that the output of a node is 0, without doing any of the normal processing that would happen. (Things with memory like a delay node or filter are a bit more complicated.)

    Whether that's enough for you use-case is hard to tell. But in the future, you'll have a way to tell if you're using too much CPU. See the upcoming render capacity API that is being spec'ed now.