AudioWorkletProcessor() 构造函数创建新 AudioWorkletProcessor object, which represents an underlying audio processing mechanism of an AudioWorkletNode .

句法

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

var processor = new AudioWorkletProcessor(options);
				

参数

选项
An object that is passed as 选项 参数用于 AudioWorkletNode constructor and passed through the structured clone algorithm . The object is based on AudioWorkletNodeOptions dictionary. Available properties are as follows:
numberOfInputs 可选
The value to initialize the numberOfInputs property to. Defaults to 1.
numberOfOutputs 可选
The value to initialize the 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 参数 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 are no 选项 object passed to the AudioWorkletNode constructor 选项 object passed by the node to the AudioWorkletProcessor constructor will exist and at minimum have numberOfInputs and numberOfOutputs .

返回值

The newly constructed AudioWorkletProcessor 实例。

范例

In this example we pass custom options to the AudioWorkletNode constructor and observe how a structured clone of them gets passed to our AudioWorkletProcessor 构造函数。

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 选项 对象。

选项 object we pass processorOptions 采用 地图 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
					

规范

规范 状态 注释
Web 音频 API
The definition of 'AudioWorkletProcessor()' in that specification.
工作草案 初始定义。

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
AudioWorkletProcessor() 构造函数 Chrome 64 Edge 79 Firefox 76 IE 不支持 No Opera ? Safari 不支持 No WebView Android 64 Chrome Android 64 Firefox Android 不支持 No Opera Android ? Safari iOS 不支持 No Samsung Internet Android 9.0

图例

完整支持

完整支持

不支持

不支持

兼容性未知 ?

兼容性未知

另请参阅

元数据

  • 最后修改: