AudioWorkletGlobalScope: currentFrame 屬性

Baseline 已廣泛支援

此特性已得到良好支援,可在多種裝置和瀏覽器版本上使用。自 2021 年 4 月起,所有瀏覽器均已支援此特性。

AudioWorkletGlobalScope 介面的只讀屬性 currentFrame 返回一個整數,表示正在處理的音訊塊不斷增加的當前取樣幀。在每個音訊塊處理完成後,它會增加 128(渲染量的大小)。

一個整數。

示例

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 Audio API
# dom-audioworkletglobalscope-currentframe

瀏覽器相容性

另見