AudioWorkletGlobalScope: currentTime 属性

基线 广泛可用

此功能已得到良好建立,并且可以在许多设备和浏览器版本中使用。它从以下时间开始在浏览器中可用 2021 年 4 月.

AudioWorkletGlobalScope 接口的只读 currentTime 属性返回一个双精度浮点数,表示正在处理的音频块的不断增加的上下文时间。它等于 worklet 所属的 BaseAudioContextcurrentTime 属性。

表示时间的浮点数。

示例

AudioWorkletProcessor 可以访问特定 AudioWorkletGlobalScope 属性

js
// AudioWorkletProcessor defined in : test-processor.js
class TestProcessor extends AudioWorkletProcessor {
  constructor() {
    super();

    // Logs the current sample-frame and time at the moment of instantiation.
    // They are accessible from the AudioWorkletGlobalScope.
    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;
  }
}

// Logs the sample rate, that 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);

主脚本加载处理器,创建 AudioWorkletNode 的实例,将处理器的名称传递给它,并将节点连接到音频图。我们应该在控制台中看到 console.log() 调用的输出

js
const audioContext = new AudioContext();
await audioContext.audioWorklet.addModule("test-processor.js");
const testNode = new AudioWorkletNode(audioContext, "test-processor");
testNode.connect(audioContext.destination);

规范

规范
Web 音频 API
# dom-audioworkletglobalscope-currenttime

浏览器兼容性

BCD 表格仅在浏览器中加载

另请参阅