AnalyserNode: fftSize 屬性
AnalyserNode 介面的 fftSize 屬性是一個無符號長整型值,表示在執行 快速傅立葉變換 (FFT) 以獲取頻域資料時使用的樣本視窗大小。
值
一個無符號整數,表示 FFT 的樣本視窗大小。值越高,頻域的細節越多,但幅度域的細節越少。
必須是介於 25 和 215 之間的 2 的冪,即:32、64、128、256、512、1024、2048、4096、8192、16384 和 32768。預設為 2048。
異常
IndexSizeErrorDOMException-
如果設定的值不是 2 的冪或超出允許範圍,則會丟擲異常。
示例
以下示例展示瞭如何使用 AudioContext 建立一個 AnalyserNode,然後使用 requestAnimationFrame 和 <canvas> 元素反覆收集時域資料,並繪製當前音訊輸入的“示波器風格”輸出。有關更完整的應用示例/資訊,請檢視我們的 Voice-change-O-matic 演示(相關程式碼請參見 app.js 的 108-193 行)。
js
const audioCtx = new AudioContext();
const analyser = audioCtx.createAnalyser();
// …
analyser.fftSize = 2048;
const bufferLength = analyser.frequencyBinCount;
const dataArray = new Uint8Array(bufferLength);
analyser.getByteTimeDomainData(dataArray);
// draw an oscilloscope of the current audio source
function draw() {
drawVisual = requestAnimationFrame(draw);
analyser.getByteTimeDomainData(dataArray);
canvasCtx.fillStyle = "rgb(200 200 200)";
canvasCtx.fillRect(0, 0, WIDTH, HEIGHT);
canvasCtx.lineWidth = 2;
canvasCtx.strokeStyle = "rgb(0 0 0)";
canvasCtx.beginPath();
const sliceWidth = (WIDTH * 1.0) / bufferLength;
let x = 0;
for (let i = 0; i < bufferLength; i++) {
const v = dataArray[i] / 128.0;
const y = (v * HEIGHT) / 2;
if (i === 0) {
canvasCtx.moveTo(x, y);
} else {
canvasCtx.lineTo(x, y);
}
x += sliceWidth;
}
canvasCtx.lineTo(canvas.width, canvas.height / 2);
canvasCtx.stroke();
}
draw();
規範
| 規範 |
|---|
| Web Audio API # dom-analysernode-fftsize |
瀏覽器相容性
載入中…