AudioWorkletGlobalScope: sampleRate 屬性
AudioWorkletGlobalScope 介面的只讀 sampleRate 屬性返回一個浮點數,表示該工作執行緒所屬的 BaseAudioContext 的取樣率。
值
表示相關取樣率的浮點數。
示例
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-samplerate |
瀏覽器相容性
載入中…