ConstantSourceNode
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since April 2021.
TheConstantSourceNode interface—part of the Web Audio API—represents an audio source (based uponAudioScheduledSourceNode) whose output is single unchanging value. This makes it useful for cases in which you need a constant value coming in from an audio source. In addition, it can be used like a constructibleAudioParam by automating the value of itsoffset or by connecting another node to it; seeControlling multiple parameters with ConstantSourceNode.
AConstantSourceNode has no inputs and exactly one monaural (one-channel) output. The output's value is always the same as the value of theoffset parameter.
| Number of inputs | 0 |
|---|---|
| Number of outputs | 1 |
In this article
Constructor
ConstantSourceNode()Creates and returns a new
ConstantSourceNodeinstance, optionally specifying an object which establishes initial values for the object's properties. As an alternative, you can use theBaseAudioContext.createConstantSource()factory method; seeCreating an AudioNode.
Instance properties
Inherits properties from its parent interface,AudioScheduledSourceNode, and adds the following properties:
offsetAn
AudioParamwhich specifies the value that this source continuously outputs. The default value is 1.0.
Events
Inherits events from its parent interface,AudioScheduledSourceNode.
Note:Some browsers' implementations of these events are part of theAudioScheduledSourceNode interface.
endedFired whenever the
ConstantSourceNodedata has stopped playing.
Instance methods
Inherits methods from its parent interface,AudioScheduledSourceNode.
Note:Some browsers' implementations of these methods are part of theAudioScheduledSourceNode interface.
Example
In the articleControlling multiple parameters with ConstantSourceNode, aConstantSourceNode is created to allow one slider control to change the gain on twoGainNodes. The three nodes are set up like this:
gainNode2 = context.createGain();gainNode3 = context.createGain();gainNode2.gain.value = gainNode3.gain.value = 0.5;volumeSliderControl.value = gainNode2.gain.value;constantNode = context.createConstantSource();constantNode.connect(gainNode2.gain);constantNode.connect(gainNode3.gain);constantNode.start();gainNode2.connect(context.destination);gainNode3.connect(context.destination);This code starts by creating the gain nodes and setting them and the volume control that will adjust their value all to 0.5. Then theConstantSourceNode is created by callingAudioContext.createConstantSource(), and the gain parameters of each of the two gain nodes are connected to theConstantSourceNode. After starting the constant source by calling itsstart() method. Finally, the two gain nodes are connected to the audio destination (typically speakers or headphones).
Now, whenever the value ofconstantNode.offset changes, the gain on bothgainNode2 andgainNode3 will change to have that same value.
To see this example in action, as well as to read the rest of the code from which these snippets were derived, seeControlling multiple parameters with ConstantSourceNode.
Specifications
| Specification |
|---|
| Web Audio API> # ConstantSourceNode> |