简体   繁体   English

按音量归一化来自getByteFrequencyData的音频数据

[英]Normalize audio data from getByteFrequencyData by volume

I've created a getSpectrum method using the getByteFrequencyData method on the Web Audio API's Analyser Node. 我已经在Web Audio API的分析器节点上使用getByteFrequencyData方法创建了一个getSpectrum方法。 The array of audio data returned is relative to the audio source's (either an el, or Audio() instance) volume , a value from 0 to 1. 回传音频数据阵列相对于所述音频源的(无论是一个EL,或音频()实例) volume ,从0到1的值。

Using the audio source's volume I'm trying to normalize each value received from getByteFrequencyData so that the user of getSpectrum doesn't have to worry about volume when they're visualizing the audio data. 使用音频源的音量,我试图对从getByteFrequencyData接收到的每个值进行规范化,以便getSpectrum的用户在可视化音频数据时不必担心音量。

This is the striped down version of getSpectrum 这是getSpectrum的简化版本

var audioData = new Uint8Array(analyser.binCount);
var spectrum = [];

analyser.getByteFrequencyData(audioData);

for (var i = 0; i < audioData.length; i++) {
  var value = audioData[i];

  //Where I'm trying to calculate the value independent of volume
  value = ((value / audioEl.volume) / 255);

  spectrum.push(value);
}

return spectrum;

The W3C spec references the equation used to calculate the returned value given a maxDecibels and minDecibels. W3C规范在给定maxDecibels和minDecibels的情况下引用了用于计算返回值的公式。 With my rudimentary understanding, I've tried to inverse the math so I get a normalized value, but I can't getting it working exactly right. 以我的基本理解,我试图对数学进行求逆,以便获得归一化的值,但无法使其完全正确地工作。 I'm having trouble accomplishing this with just a volume value from 0 to 1. 我很难用0到1的音量值来完成此操作。

Any incite would be greatly appreciated! 任何煽动将不胜感激! Heres a working example of the issue. 这是该问题的有效示例 Changing the volume slider will illustrate the problem. 更改音量滑块将说明问题。

Update 7/22/16: Thanks to @raymond-toy's answer I figured out how to convert the 0 to 1 volume value to decibels. 16年7月22日更新:感谢@ raymond-toy的回答,我弄清楚了如何将0到1的音量值转换为分贝。

volumeDB = Math.abs((Math.log(volume)/Math.LN10)*20);

After getting the DB, I inversed the equation in the W3C spec, 获得数据库后,我将W3C规范中的等式逆转,

value = ((audioDataValue * volumeDB) / 255) - volumeDB

Unfortunately, value somehow still ends up relative to volume . 不幸的是,相对于volumevalue仍然以某种方式结束。 Does anyone see what I'm missing? 有人看到我想念的东西吗?

getByteFrequencyData returns values in dB. getByteFrequencyData返回以dB为单位的值。 You don't want to divide these values by the audioE1.volume. 您不想将这些值除以audioE1.volume。 You want to convert (somehow!) audioE1.volume to a dB value and add (or subtract) that from values from getByteFrequencyData 你想audioE1.volume转换(!不知),以dB值,并添加(或减去)是从价值getByteFrequencyData

It might be easier to understand things if you used getFloatFrequencyData first to see what's happening. 如果首先使用getFloatFrequencyData来查看正在发生的事情,可能会更容易理解。

Apparently I was on a fool's errand. 显然我是个傻瓜。 As @raymond-toy pointed out, Spectrum values are implicitly relative to volume. 正如@ raymond-toy所指出的,Spectrum值隐式相对于体积。 Normalizing would mean losing a portion of data "off the bottom of the spectrum", which was not my goal. 规范化意味着“从频谱底部”丢失部分数据,这不是我的目标。

If anyone's curious, I ended up just dividing the audioDataValue by 255, getting a float from 0 to 1. 如果有人好奇,我最终只是将audioDataValue除以255,得到从0到1的浮点数。

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

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