AudioWorkletGlobalScope
接口在
Web 音频 API
represents a global execution context for user-supplied code, which defines custom
AudioWorkletProcessor
-derived classes.
每个
BaseAudioContext
has a single
AudioWorklet
available under the
audioWorklet
property, which runs its code in a single
AudioWorkletGlobalScope
.
As the global execution context is shared across the current
BaseAudioContext
, it's possible to define any other variables and perform any actions allowed in worklets — apart from defining
AudioWorkletProcessor
-derived classes.
currentFrame
只读
Returns an integer that represents the ever-increasing current sample-frame of the audio block being processed. It is incremented by 128 (the size of a render quantum) after the processing of each audio block.
currentTime
只读
currentTime
特性为
BaseAudioContext
the worklet belongs to.
sampleRate
只读
BaseAudioContext
.
registerProcessor()
AudioWorkletProcessor
interface. The class can then be used by creating an
AudioWorkletNode
, providing its registered name.
In this example we output all global properties into the console in the constructor of a custom
AudioWorkletProcessor
.
First we need to define the processor, and register it. Note that this should be done in a separate file.
// test-processor.js
class TestProcessor extends AudioWorkletProcessor {
constructor () {
super()
// current sample-frame and time at the moment of instantiation
// to see values change, you can put these two lines in process method
console.log(currentFrame)
console.log(currentTime)
}
// the process method is required - simply output silence,
// which the outputs are already filled with
process (inputs, outputs, parameters) {
return true
}
}
// the sample rate is not going to change ever,
// because it's a read-only property of a BaseAudioContext
// and is set only during its instantiation
console.log(sampleRate)
// you can declare any variables and use them in your processors
// for example it may be an ArrayBuffer with a wavetable
const usefulVariable = 42
console.log(usefulVariable)
registerProcessor('test-processor', TestProcessor)
Next, in our main scripts file we'll load the processor, create an instance of
AudioWorkletNode
— passing the name of the processor to it — and connect the node to an audio graph. We should see the output of
console.log
calls in the console:
const audioContext = new AudioContext()
await audioContext.audioWorklet.addModule('test-processor.js')
const testNode = new AudioWorkletNode(audioContext, 'test-processor')
testNode.connect(audioContext.destination)
| 规范 | 状态 | 注释 |
|---|---|---|
|
Web 音频 API
The definition of 'AudioWorkletGlobalScope' in that specification. |
工作草案 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
AudioWorkletGlobalScope
|
Chrome 66 | Edge 79 | Firefox 76 | IE 不支持 No | Opera 53 | Safari 不支持 No | WebView Android 66 | Chrome Android 66 | Firefox Android 不支持 No | Opera Android 47 | Safari iOS 不支持 No | Samsung Internet Android 9.0 |
currentFrame
|
Chrome 66 | Edge 79 | Firefox 76 | IE 不支持 No | Opera 53 | Safari 不支持 No | WebView Android 66 | Chrome Android 66 | Firefox Android 不支持 No | Opera Android 47 | Safari iOS 不支持 No | Samsung Internet Android 9.0 |
currentTime
|
Chrome 66 | Edge 79 | Firefox 76 | IE 不支持 No | Opera 53 | Safari 不支持 No | WebView Android 66 | Chrome Android 66 | Firefox Android 不支持 No | Opera Android 47 | Safari iOS 不支持 No | Samsung Internet Android 9.0 |
registerProcessor
|
Chrome 66 | Edge 79 | Firefox 76 | IE 不支持 No | Opera 53 | Safari 不支持 No | WebView Android 66 | Chrome Android 66 | Firefox Android 不支持 No | Opera Android 47 | Safari iOS 不支持 No | Samsung Internet Android 9.0 |
sampleRate
|
Chrome 66 | Edge 79 | Firefox 76 | IE 不支持 No | Opera 53 | Safari 不支持 No | WebView Android 66 | Chrome Android 66 | Firefox Android 不支持 No | Opera Android 47 | Safari iOS 不支持 No | Samsung Internet Android 9.0 |
完整支持
不支持
AudioWorkletGlobalScope
AnalyserNode
AudioBuffer
AudioBufferSourceNode
AudioContext
AudioContextOptions
AudioDestinationNode
AudioListener
AudioNode
AudioNodeOptions
AudioParam
AudioProcessingEvent
AudioScheduledSourceNode
AudioWorklet
AudioWorkletNode
AudioWorkletProcessor
BaseAudioContext
BiquadFilterNode
ChannelMergerNode
ChannelSplitterNode
ConstantSourceNode
ConvolverNode
DelayNode
DynamicsCompressorNode
GainNode
IIRFilterNode
MediaElementAudioSourceNode
MediaStreamAudioDestinationNode
MediaStreamAudioSourceNode
OfflineAudioCompletionEvent
OfflineAudioContext
OscillatorNode
PannerNode
PeriodicWave
StereoPannerNode
WaveShaperNode