[英]Web Audio API Stereo Volume Meter
我正在嘗試為麥克風輸入創建最基本的立體聲音量表,但無論我在哪個頻道講話,我的左右音量表都顯示相同的讀數。
這是我的代碼: https://codepen.io/9880780/pen/oNdwbLz
const meterR = document.getElementById("meterR");
const meterL = document.getElementById("meterL");
const startButtonEl = document.getElementById("startButton");
startButtonEl.onclick = async () => {
startButtonEl.disabled = true;
const stream = await navigator.mediaDevices.getUserMedia({
audio: true,
video: false
});
const context = new AudioContext();
const mediaStreamAudioSourceNode = context.createMediaStreamSource(stream);
const analyserL = context.createAnalyser();
const analyserR = context.createAnalyser();
var splitter = context.createChannelSplitter(2);
mediaStreamAudioSourceNode.connect(splitter);
splitter.connect(analyserL, 1);
splitter.connect(analyserR, 0);
const pcmDataR = new Float32Array(analyserR.fftSize);
const pcmDataL = new Float32Array(analyserL.fftSize);
const onFrame = () => {
analyserR.getFloatTimeDomainData(pcmDataR);
analyserL.getFloatTimeDomainData(pcmDataL);
let sumR = 0.0;
let sumL = 0.0;
for (const amplitude of pcmDataR) {
sumR += amplitude * amplitude;
}
for (const amplitude of pcmDataL) {
sumL += amplitude * amplitude;
}
meterR.value = Math.sqrt(sumR / pcmDataR.length);
meterL.value = Math.sqrt(sumL / pcmDataL.length);
window.requestAnimationFrame(onFrame);
};
window.requestAnimationFrame(onFrame);
};
知道我做錯了什么嗎?
已解決:您必須禁用回聲消除才能使其工作。 (為什么?)
{ "echoCancellation": false, "googAutoGainControl": false, "googNoiseSuppression": false, "googHighpassFilter": false }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.