What is the best way to destroy an audionode

Viewed 4314

I'm building something which requires the creation and destruction of potentially a lot of audio nodes. As far as I am aware, in order to destroy an audio node it is necessary to:

  1. set all references of the node to null or something else
  2. call the stop method if applicable
  3. disconnect all connections in and out

The first two are simple enough but I'm trying to figure out if it's possible to list all the node's connections and also need to know if anything else is missing from my list. I also need to know how I can detect the existence of an unreferenced audio node

2 Answers

Here's the specifications on the lifetime of an AudioNode:

An AudioNode will live as long as there are any references to it. There are several types of references:

  1. A normal JavaScript reference obeying normal garbage collection rules.
  2. A playing reference for both AudioBufferSourceNodes and OscillatorNodes. These nodes maintain a playing reference to themselves while they are currently playing.
  3. A connection reference which occurs if another AudioNode is connected to it.
  4. A tail-time reference which an AudioNode maintains on itself as long as it has any internal processing state which has not yet been emitted. For example, a ConvolverNode has a tail which continues to play even after receiving silent input (think about clapping your hands in a large concert hall and continuing to hear the sound reverberate throughout the hall). Some AudioNodes have this property. Please see details for specific nodes.

Any AudioNodes which are connected in a cycle and are directly or indirectly connected to the AudioDestinationNode of the AudioContext will stay alive as long as the AudioContext is alive.

So, from my understanding:

  • a playing AudioNode just needs to be stopped.
  • others are best to be disconnected.
  • it doesn't hurt to disconnect them all.

If you simply call audioNode.disconnect() with no parameters, it will disconnect all the inputs to the node as well as all the outputs from it.

Related