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

AudioWorkletProcessor: AudioWorkletProcessor() constructor

· 52CE 我爱测

The **`AudioWorkletProcessor()`** constructor creates a new AudioWorkletProcessor object, which represents an underlying audio processing mechanism of an AudioWorkletNode.

Web Audio API

The AudioWorkletProcessor() constructor creates a new AudioWorkletProcessor object, which represents an underlying audio processing mechanism of an AudioWorkletNode.

[!NOTE] The AudioWorkletProcessor and classes that derive from it cannot be instantiated directly from user-supplied code. Instead, they are created only internally by the creation of an associated AudioWorkletNode.

Syntax

new AudioWorkletProcessor(options)

Parameters

  • options
    • : An object that is passed as options parameter to the AudioWorkletNode.AudioWorkletNode constructor and passed through the structured clone algorithm. Available properties are as follows:

      • numberOfInputs
        • : The value to initialize the AudioNode.numberOfInputs property to. Defaults to 1.
      • numberOfOutputs
        • : The value to initialize the AudioNode.numberOfOutputs property to. Defaults to 1.
      • outputChannelCount
        • : An array defining the number of channels for each output. For example, outputChannelCount: [n, m] specifies the number of channels in the first output to be n and the second output to be m. The array length must match numberOfOutputs.
      • parameterData
        • : An object containing the initial values of custom AudioParam objects on this node (in its AudioWorkletNode.parameters property), with key being the name of a custom parameter and value being its initial value.
      • processorOptions
        • : Any additional data that can be used for custom initialization of the underlying AudioWorkletProcessor.

      Note that there are default values for the first two properties, so even if there is no options object passed to the AudioWorkletNode.AudioWorkletNode constructor, the options object passed by the node to the AudioWorkletProcessor constructor will exist and at minimum have numberOfInputs and numberOfOutputs.

Return value

The newly constructed AudioWorkletProcessor instance.

Examples

In this example we pass custom options to the AudioWorkletNode.AudioWorkletNode constructor and observe how a structured clone of them gets passed to our AudioWorkletProcessor constructor.

First, we need to define a custom AudioWorkletProcessor and register it. Note that this should be done in a separate file.

// test-processor.js
class TestProcessor extends AudioWorkletProcessor {
  constructor(options) {
    super();
    console.log(options.numberOfInputs);
    console.log(options.processorOptions.someUsefulVariable);
  }
  process(inputs, outputs, parameters) {
    return true;
  }
}

registerProcessor("test-processor", TestProcessor);

Next, in our main script file we'll load the processor, create an instance of AudioWorkletNode passing it the name of the processor and options object.

In the options object we pass processorOptions with a Map instance under someUsefulVariable key. We don't pass numberOfInputs and see how it gets its default value.

const audioContext = new AudioContext();
await audioContext.audioWorklet.addModule("test-processor.js");
const testNode = new AudioWorkletNode(audioContext, "test-processor", {
  processorOptions: {
    someUsefulVariable: new Map([
      [1, "one"],
      [2, "two"],
    ]),
  },
});

The console output will be as follows:

> 1 // AudioWorkletNode options.numberOfInputs set to default
> Map(2) { 1 => "one", 2 => "two" } // A cloned map under someUsefulVariable

Specifications

Browser compatibility

See also

  • AudioWorkletNode interface

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

资料来源与适用范围

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

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

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