AnalyserNode: maxDecibels 屬性
AnalyserNode 介面的 maxDecibels 屬性是一個雙精度浮點數值,表示 FFT 分析資料縮放範圍內的最大功率值,用於轉換為無符號位元組值——基本上,這指定了在使用 getByteFrequencyData() 時結果範圍的最大值。
值
一個雙精度浮點數,表示 FFT 分析資料縮放的最大 分貝 值,其中 0 dB 是最響亮的聲音,-10 dB 是其十分之一,以此類推。預設值為 -30 dB。
從 getByteFrequencyData() 獲取資料時,任何幅度等於或大於 maxDecibels 的頻率都將返回 255。
異常
IndexSizeErrorDOMException-
如果設定的值小於或等於
AnalyserNode.minDecibels,則會丟擲錯誤。
示例
下面的示例展示瞭如何使用 AudioContext 建立一個 AnalyserNode,然後使用 requestAnimationFrame 和 <canvas> 來反覆收集頻率資料,並繪製一個“Winamp 條形圖風格”的當前音訊輸入的輸出。有關更完整的實際示例/資訊,請檢視我們的 Voice-change-O-matic 演示(請參閱 app.js 的 108-193 行中的相關程式碼)。
js
const audioCtx = new AudioContext();
const analyser = audioCtx.createAnalyser();
analyser.minDecibels = -90;
analyser.maxDecibels = -10;
// …
analyser.fftSize = 256;
const bufferLength = analyser.frequencyBinCount;
console.log(bufferLength);
const dataArray = new Uint8Array(bufferLength);
canvasCtx.clearRect(0, 0, WIDTH, HEIGHT);
function draw() {
drawVisual = requestAnimationFrame(draw);
analyser.getByteFrequencyData(dataArray);
canvasCtx.fillStyle = "rgb(0 0 0)";
canvasCtx.fillRect(0, 0, WIDTH, HEIGHT);
const barWidth = (WIDTH / bufferLength) * 2.5;
let barHeight;
let x = 0;
for (let i = 0; i < bufferLength; i++) {
barHeight = dataArray[i];
canvasCtx.fillStyle = `rgb(${barHeight + 100} 50 50)`;
canvasCtx.fillRect(x, HEIGHT - barHeight / 2, barWidth, barHeight / 2);
x += barWidth + 1;
}
}
draw();
規範
| 規範 |
|---|
| Web Audio API # dom-analysernode-maxdecibels |
瀏覽器相容性
載入中…