AudioWorkletProcessor
接口在
Web 音频 API
represents an audio processing code behind a custom
AudioWorkletNode
. It lives in the
AudioWorkletGlobalScope
and runs on the Web Audio rendering thread. In turn, an
AudioWorkletNode
based on it runs on the main thread.
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
s. The constructor of the deriving class is getting called with an options object, so you can perform a custom initialization procedures — see constructor page for details.
AudioWorkletProcessor()
AudioWorkletProcessor
对象。
port
只读
MessagePort
used for bidirectional communication between the processor and the
AudioWorkletNode
which it belongs to. The other end is available under the
port
property of the node.
AudioWorkletProcessor
interface does not define any methods of its own. However, you must provide a
process()
method, which is called in order to process the audio stream.
AudioWorkletProcessor
interface doesn't respond to any events.
To define custom audio processing code you have to derive a class from the
AudioWorkletProcessor
interface. Although not defined on the interface, the deriving class must have the
process
method. This method gets called for each block of 128 sample-frames and takes input and output arrays and calculated values of custom
AudioParam
s (if they are defined) as parameters. You can use inputs and audio parameter values to fill the outputs array, which by default holds silence.
Optionally, if you want custom
AudioParam
s on your node, you can supply a
parameterDescriptors
property as a
static getter
on the processor. The array of
AudioParamDescriptor
-based objects returned is used internally to create the
AudioParam
s during the instantiation of the
AudioWorkletNode
.
结果
AudioParam
s reside in the
参数
property of the node and can be automated using standard methods such as
linearRampToValueAtTime
. Their calculated values will be passed into the
process()
method of the processor for you to shape the node output accordingly.
An example algorithm of creating a custom audio processing mechanism is:
AudioWorkletProcessor
class (see
"Deriving classes" section
) and supply your own
process()
method in it;
AudioWorkletGlobalScope.registerProcessor()
方法;
addModule()
method on your audio context's
audioWorklet
property;
AudioWorkletNode
based on the processor. The processor will be instantiated internally by the
AudioWorkletNode
构造函数。
In the example below we create a custom
AudioWorkletNode
that outputs white noise.
First, we need to define a custom
AudioWorkletProcessor
, which will output white noise, and register it. Note that this should be done in a separate file.
// white-noise-processor.js
class WhiteNoiseProcessor extends AudioWorkletProcessor {
process (inputs, outputs, parameters) {
const output = outputs[0]
output.forEach(channel => {
for (let i = 0; i < channel.length; i++) {
channel[i] = Math.random() * 2 - 1
}
})
return true
}
}
registerProcessor('white-noise-processor', WhiteNoiseProcessor)
Next, in our main script file we'll load the processor, create an instance of
AudioWorkletNode
, passing it the name of the processor, then connect the node to an audio graph.
const audioContext = new AudioContext()
await audioContext.audioWorklet.addModule('white-noise-processor.js')
const whiteNoiseNode = new AudioWorkletNode(audioContext, 'white-noise-processor')
whiteNoiseNode.connect(audioContext.destination)
| 规范 | 状态 | 注释 |
|---|---|---|
|
Web 音频 API
The definition of 'AudioWorkletProcessor' in that specification. |
工作草案 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
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 |
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 |
port
|
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 |
完整支持
不支持
兼容性未知
AudioWorkletProcessor
AnalyserNode
AudioBuffer
AudioBufferSourceNode
AudioContext
AudioContextOptions
AudioDestinationNode
AudioListener
AudioNode
AudioNodeOptions
AudioParam
AudioProcessingEvent
AudioScheduledSourceNode
AudioWorklet
AudioWorkletGlobalScope
AudioWorkletNode
BaseAudioContext
BiquadFilterNode
ChannelMergerNode
ChannelSplitterNode
ConstantSourceNode
ConvolverNode
DelayNode
DynamicsCompressorNode
GainNode
IIRFilterNode
MediaElementAudioSourceNode
MediaStreamAudioDestinationNode
MediaStreamAudioSourceNode
OfflineAudioCompletionEvent
OfflineAudioContext
OscillatorNode
PannerNode
PeriodicWave
StereoPannerNode
WaveShaperNode