If I wanted to reduce a WAV file's amplitude by 25%, I would write something like this:
如果我想将WAV文件的幅度降低25%,我会写这样的东西:
for (int i = 0; i < data.Length; i++)
{
data[i] *= 0.75;
}
A lot of the articles I read on audio techniques, however, discuss amplitude in terms of decibels. I understand the logarithmic nature of decibel units in principle, but not so much in terms of actual code.
然而,我读到的很多关于音频技术的文章都以分贝的形式讨论了振幅。我原则上理解分贝单元的对数性质,但在实际代码方面却没有那么多。
My question is: if I wanted to attenuate the volume of a WAV file by, say, 20 decibels, how would I do this in code like my above example?
我的问题是:如果我想减少一个WAV文件的音量,比如20分贝,我会如何像上面的例子一样在代码中这样做?
Update: formula (based on Nils Pipenbrinck's answer) for attenuating by a given number of decibels (entered as a positive number e.g. 10, 20 etc.):
更新:公式(基于Nils Pipenbrinck的答案)用于衰减给定数量的分贝(输入为正数,例如10,20等):
public void AttenuateAudio(float[] data, int decibels)
{
float gain = (float)Math.Pow(10, (double)-decibels / 20.0);
for (int i = 0; i < data.Length; i++)
{
data[i] *= gain;
}
}
So, if I want to attenuate by 20 decibels, the gain factor is .1.
因此,如果我想衰减20分贝,增益因子是.1。
4 个解决方案
#1
I think you want to convert from decibel to gain.
我想你想从分贝转换为增益。
The equations for audio are:
音频方程是:
decibel to gain:
分贝获得:
gain = 10 ^ (attenuation in db / 20)
or in C:
或在C:
gain = powf(10, attenuation / 20.0f);
The equations to convert from gain to db are:
从增益转换为db的公式为:
attenuation_in_db = 20 * log10 (gain)
#2
If you just want to adust some audio, I've had good results with the normalize package from nongnu.org
. If you want to study how it's done, the source code is freely available. I've also used wavnorm
, whose home page seems to be out at the moment.
如果您只想调整一些音频,我使用nongnu.org的normalize包得到了很好的结果。如果你想研究它是如何完成的,源代码是免费提供的。我也使用了wavnorm,其主页似乎已经出现了。
#3
One thing to consider: .WAV files have MANY different formats. The code above only works for WAVE_FORMAT_FLOAT. If you're dealing with PCM files, then your samples are going to be 8, 16, 24 or 32 bit integers (8 bit PCM uses unsigned integers from 0..255, 24 bit PCM can be packed or unpacked (packed == 3 byte values packed next to each other, unpacked == 3 byte values in a 4 byte package).
需要考虑的一件事:.WAV文件有许多不同的格式。上面的代码仅适用于WAVE_FORMAT_FLOAT。如果您正在处理PCM文件,那么您的样本将是8,16,24或32位整数(8位PCM使用0..255的无符号整数,24位PCM可以打包或解压缩(打包== 3个字节值彼此相邻打包,解包= = 4字节包中的3个字节值)。
And then there's the issue of alternate encodings - For instance in Win7, all the windows sounds are actually MP3 files in a WAV container.
然后是备用编码的问题 - 例如在Win7中,所有的窗口声音实际上都是WAV容器中的MP3文件。
It's unfortunately not as simple as it sounds :(.
遗憾的是,它不像听起来那么简单:(。
#4
Oops I misunderstood the question… You can see my python implementations of converting from dB to a float (which you can use as a multiplier on the amplitude like you show above) and vice-versa
哎呀我误解了这个问题......你可以看到我的python实现从dB转换为浮点数(你可以像上面所示的那样用作幅度的乘数),反之亦然
https://github.com/jiaaro/pydub/blob/master/pydub/utils.py
In a nutshell it's:
简而言之,它是:
10 ^ (db_gain / 10)
so to reduce the volume by 6 dB you would multiply the amplitude of each sample by:
因此,要将音量减小6 dB,您可以将每个样本的幅度乘以:
10 ^ (-6 / 10) == 10 ^ (-0.6) == 0.2512
#1
I think you want to convert from decibel to gain.
我想你想从分贝转换为增益。
The equations for audio are:
音频方程是:
decibel to gain:
分贝获得:
gain = 10 ^ (attenuation in db / 20)
or in C:
或在C:
gain = powf(10, attenuation / 20.0f);
The equations to convert from gain to db are:
从增益转换为db的公式为:
attenuation_in_db = 20 * log10 (gain)
#2
If you just want to adust some audio, I've had good results with the normalize package from nongnu.org
. If you want to study how it's done, the source code is freely available. I've also used wavnorm
, whose home page seems to be out at the moment.
如果您只想调整一些音频,我使用nongnu.org的normalize包得到了很好的结果。如果你想研究它是如何完成的,源代码是免费提供的。我也使用了wavnorm,其主页似乎已经出现了。
#3
One thing to consider: .WAV files have MANY different formats. The code above only works for WAVE_FORMAT_FLOAT. If you're dealing with PCM files, then your samples are going to be 8, 16, 24 or 32 bit integers (8 bit PCM uses unsigned integers from 0..255, 24 bit PCM can be packed or unpacked (packed == 3 byte values packed next to each other, unpacked == 3 byte values in a 4 byte package).
需要考虑的一件事:.WAV文件有许多不同的格式。上面的代码仅适用于WAVE_FORMAT_FLOAT。如果您正在处理PCM文件,那么您的样本将是8,16,24或32位整数(8位PCM使用0..255的无符号整数,24位PCM可以打包或解压缩(打包== 3个字节值彼此相邻打包,解包= = 4字节包中的3个字节值)。
And then there's the issue of alternate encodings - For instance in Win7, all the windows sounds are actually MP3 files in a WAV container.
然后是备用编码的问题 - 例如在Win7中,所有的窗口声音实际上都是WAV容器中的MP3文件。
It's unfortunately not as simple as it sounds :(.
遗憾的是,它不像听起来那么简单:(。
#4
Oops I misunderstood the question… You can see my python implementations of converting from dB to a float (which you can use as a multiplier on the amplitude like you show above) and vice-versa
哎呀我误解了这个问题......你可以看到我的python实现从dB转换为浮点数(你可以像上面所示的那样用作幅度的乘数),反之亦然
https://github.com/jiaaro/pydub/blob/master/pydub/utils.py
In a nutshell it's:
简而言之,它是:
10 ^ (db_gain / 10)
so to reduce the volume by 6 dB you would multiply the amplitude of each sample by:
因此,要将音量减小6 dB,您可以将每个样本的幅度乘以:
10 ^ (-6 / 10) == 10 ^ (-0.6) == 0.2512