does the web audio api clear out a source node after it finishes playing?

3.3k Views Asked by At

when a user clicks on a button a sound is played , and if he clicks it again a new instance of that same sound is played .

i do that by connecting a new source node to the audio Context on every click.

now say that user clicks the button for 1 hour , does every source node that has finished playing gets deleted or it stays connected to the audio Context ?

2

There are 2 best solutions below

7
On BEST ANSWER

Here are the relevant specs:

The following behaviors provide a normative description of the conditions under which an AudioNode is alive, meaning that it MUST be retained in the graph by an implementation. Where these conditions do not apply, AudioNodes MAY be released by an implementation.

There are several types of references:

  1. A normal reference obeying normal garbage collection rules.

  2. A playing reference for AudioBufferSourceNodes, MediaElementAudioSourceNodes, MediaStreamAudioSourceNodes 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 one or more of its inputs. Connections to a node’s AudioParams do not imply a connection reference.

  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.

  5. MediaStreams keep a MediaStreamAudioSourceNode alive as long as the underlying MediaStreamTrack that is playing through the MediaStreamAudioSourceNode has not ended (as per [mediacapture-streams]).

  6. HTMLMediaElements keep their associated MediaElementAudioSourceNode alive as long as the HTMLMediaElement is in a state where audio could ever be played in the future.

So in the case of for instance, an AudioBufferSourceNode, since it has no input(3), no tail-time(4), is not linked to an external MediaStream(5) or MediaElement(6), if you don't keep any reference to the node in your js code(1), and that the Node has finished playing(2), then it can be removed from the graph, and then Garbage Collected.


Also note that most source nodes have a very little fingerprint anyway.

If once again we take AudioBufferSourceNodes as an example, then you have to understand that they don't duplicate the AudioBuffer, they just reference it.
So creating thousands of AudioBufferSourceNodes from the same AudioBuffer is fine.

However creating thousands of AudioBuffers from the same ArrayBuffer, or thousands of ArrayBuffers from the same file, or both is not fine at all, so be sure that when you are handling the click event all you do is create a new AudioBufferSourceNode, from a preexisting AudioBuffer.

1
On

The BufferSourceNode won't be deleted if it's still connected to the audio context. You can check this by using the Firefox Web Audio dev tools, or installing the Audion Chrome extension.

You can add an onended function to the BufferSourceNode to disconnect it after it's used. Then, as long as nothing else has a reference to that buffer source node, it'll get cleaned up by the garbage collector.

Source: https://developer.mozilla.org/en-US/docs/Web/API/AudioScheduledSourceNode/onended