AudioWorkletGlobalScope - Web APIs 编辑

The AudioWorkletGlobalScope interface of the Web Audio API represents a global execution context for user-supplied code, which defines custom AudioWorkletProcessor-derived classes. Each 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.

Properties

currentFrame Read only
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 Read only
Returns a double that represents the ever-increasing context time of the audio block being processed. It is equal to the currentTime property of the BaseAudioContext the worklet belongs to.
sampleRate Read only
Returns a float that represents the sample rate of the associated BaseAudioContext.

Methods

registerProcessor()
Registers a class derived from the AudioWorkletProcessor interface. The class can then be used by creating an AudioWorkletNode, providing its registered name.

Examples

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 - 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)

Specifications

SpecificationStatusComment
Web Audio API
The definition of 'AudioWorkletGlobalScope' in that specification.
Working Draft

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:52 次

字数:6088

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文