简体   繁体   English

Web 音频 API 立体声音量计

[英]Web Audio API Stereo Volume Meter

I'm trying to create the most basic stereo volume meter for mic input but my right and left meters are showing the same readings regardless of which channel I am speaking into.我正在尝试为麦克风输入创建最基本的立体声音量表,但无论我在哪个频道讲话,我的左右音量表都显示相同的读数。

Here's my code in action: https://codepen.io/9880780/pen/oNdwbLz这是我的代码: 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);
};

Any idea what I'm doing wrong?知道我做错了什么吗?

Solved: You must disable echo cancellation for it to work.已解决:您必须禁用回声消除才能使其工作。 (Why?) (为什么?)

{ "echoCancellation": false, "googAutoGainControl": false, "googNoiseSuppression": false, "googHighpassFilter": false }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM