大网数据广告活动
Web 标准与前端开发

ConstantSourceNode

· 52CE 我爱测

The `ConstantSourceNode` interface—part of the Web Audio API—represents an audio source (based upon AudioScheduledSourceNode) 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

Web Audio API

The ConstantSourceNode interface—part of the Web Audio API—represents an audio source (based upon AudioScheduledSourceNode) 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 constructible AudioParam by automating the value of its ConstantSourceNode.offset or by connecting another node to it; see Controlling multiple parameters with ConstantSourceNode.

A ConstantSourceNode has no inputs and exactly one monaural (one-channel) output. The output's value is always the same as the value of the ConstantSourceNode.offset parameter.

<table class="properties"> <tbody> <tr> <th scope="row">Number of inputs</th> <td><code>0</code></td> </tr> <tr> <th scope="row">Number of outputs</th> <td><code>1</code></td> </tr> </tbody> </table>

Constructor

  • ConstantSourceNode.ConstantSourceNode
    • : Creates and returns a new ConstantSourceNode instance, optionally specifying an object which establishes initial values for the object's properties. As an alternative, you can use the BaseAudioContext.createConstantSource() factory method; see Creating an AudioNode.

Instance properties

Inherits properties from its parent interface, AudioScheduledSourceNode, and adds the following properties:

  • ConstantSourceNode.offset
    • : An AudioParam which specifies the value that this source continuously outputs. The default value is 1.0.

Instance methods

Inherits methods from its parent interface, AudioScheduledSourceNode.

[!NOTE] Some browsers' implementations of these methods are part of the AudioScheduledSourceNode interface.

  • AudioScheduledSourceNode.start
    • : Schedules a sound to playback at an exact time.
  • AudioScheduledSourceNode.stop
    • : Schedules a sound to stop playback at an exact time.

Events

Inherits events from its parent interface, AudioScheduledSourceNode.

[!NOTE] Some browsers' implementations of these events are part of the AudioScheduledSourceNode interface.

  • AudioScheduledSourceNode.ended_event
    • : Fired whenever the ConstantSourceNode data has stopped playing.

Example

In the article Controlling multiple parameters with ConstantSourceNode, a ConstantSourceNode is created to allow one slider control to change the gain on two GainNodes. 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 the ConstantSourceNode is created by calling BaseAudioContext/createConstantSource, and the gain parameters of each of the two gain nodes are connected to the ConstantSourceNode. After starting the constant source by calling its AudioScheduledSourceNode.start method. Finally, the two gain nodes are connected to the audio destination (typically speakers or headphones).

Now, whenever the value of ConstantSourceNode.offset changes, the gain on both gainNode2 and gainNode3 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, see Controlling multiple parameters with ConstantSourceNode.

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。