Javascript AnalyzerNode.getFloatFrequencyData()返回负值

Javascript AnalyzerNode.getFloatFrequencyData()返回负值,javascript,web-audio-api,Javascript,Web Audio Api,我正在尝试使用Web音频API,使用AnalyzerNode.getFloatFrequencyData()获取麦克风输入的音量 该规范规定,“数组中的每个项表示特定频率的分贝值”,但它只返回负值,尽管它们看起来确实对声音级别做出了反应-哨声将返回-23左右的值,静音将返回-80左右(dataArray中的值也都是负数,所以我认为这与我如何将它们相加无关。)相同的代码给出了我期望的值(正数)(使用AnalyzerNode.getByeFrequencyData()但返回的分贝值已在0-255之间

我正在尝试使用Web音频API,使用AnalyzerNode.getFloatFrequencyData()获取麦克风输入的音量

该规范规定,“数组中的每个项表示特定频率的分贝值”,但它只返回负值,尽管它们看起来确实对声音级别做出了反应-哨声将返回-23左右的值,静音将返回-80左右(dataArray中的值也都是负数,所以我认为这与我如何将它们相加无关。)相同的代码给出了我期望的值(正数)(使用
AnalyzerNode.getByeFrequencyData()
但返回的分贝值已在0-255之间标准化,因此更难相加以确定总音量

为什么我没有得到我期望的值?和/或这可能不是一个获得麦克风输入音量的好方法吗

function getVolume(analyser) {
  analyser.fftSize = 32; 
  let bufferLength = analyser.frequencyBinCount;
  let dataArray = new Float32Array(bufferLength);
  analyser.getFloatFrequencyData(dataArray);
  let totalAntilogAmplitude = 0;
  for (let i = 0; i < bufferLength; i++) {
    let thisAmp = dataArray[i]; // amplitude of current bin
      let thisAmpAntilog = Math.pow(10, (thisAmp / 10)) // antilog amplitude for adding
      totalAntilogAmplitude = totalAntilogAmplitude + thisAmpAntilog; 
  }
  let amplitude = 10 * Math.log10(totalAntilogAmplitude);
  return amplitude;
}
功能获取体积(分析仪){
Analyzer.fftSize=32;
让bufferLength=Analyzer.frequencyBinCount;
让dataArray=新的Float32Array(bufferLength);
Analyzer.getFloatFrequencyData(数据阵列);
设总反对数振幅=0;
for(设i=0;i
您的代码看起来是正确的。但是如果没有示例,很难判断它是否产生了您期望的值。此外,由于您只是(基本上)计算变换系数所有值的总和,因此您刚刚完成了一个更昂贵的时域信号平方和

另一种方法是对信号进行平方处理,稍微过滤以消除变化,并在不同的时间获得输出值。类似于以下内容,
s
是具有您感兴趣的信号的节点

let g = new GainNode(context, {gain: 0});
s.connect(g);
s.connect(g.gain);
// Output of g is now the square of s

let f = new BiquadFilterNode(context, {frequency: 10});
// May want to adjust the frequency some to other value for your needs.
// I arbitrarily chose 10 Hz.
g.connect(f).connect(analyser)

// Now get the time-domain value from the analyser and just take the first 
// value from the signal.  This is the energy of the signal and represents 
// the volume.

您的代码看起来是正确的。但是如果没有示例,很难判断它是否产生了您期望的值。此外,由于您只是计算(基本上)变换系数所有值的总和,因此您刚刚完成了一个更昂贵的时域信号平方和

另一种方法是对信号进行平方处理,稍微过滤以消除变化,并在不同的时间获得输出值。类似于以下内容,
s
是具有您感兴趣的信号的节点

let g = new GainNode(context, {gain: 0});
s.connect(g);
s.connect(g.gain);
// Output of g is now the square of s

let f = new BiquadFilterNode(context, {frequency: 10});
// May want to adjust the frequency some to other value for your needs.
// I arbitrarily chose 10 Hz.
g.connect(f).connect(analyser)

// Now get the time-domain value from the analyser and just take the first 
// value from the signal.  This is the energy of the signal and represents 
// the volume.

我也遇到过同样的问题,我想知道是否由于某种原因,这些值都向下移动到负数,也许你可以将它们全部向上移动到最低的值,对我来说,沉默似乎解决了-891.048828125的值。我也遇到过同样的问题,我想知道是否出于某种原因,这些值都向下移动到负数,也许你可以d将它们全部向上移动一个最小值,对我来说,沉默似乎与-891.048828125的值一致。