If I wanted to reduce a WAV file's amplitude by 25%, I would write something like this:
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?
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.):
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.
dB SPL (Sound Pressure Level) (20dB = 10x)A 20 dB difference in SPL represents a ratio of ten-to-one in sound pressure. Thus, a 40dB SPL would be a sound pressure level that is 100 times greater than the sound pressure level of the quietest sound that normal human hearing can detect.
The standard definition of a decibel indicates that it's roughly the smallest amount of volume change that a person can subjectively perceive. That means variations of up to 1 dB ought to be pretty much imperceptible, while those at or beyond 1 dB are noticeable.
Near silence is expressed as 0 dB but a sound measured at 10 dB is actually 10 times louder. If a sound is 20 dB, that's 100 times louder than near silence. This is important to understand, as it helps you understand just how loud something really is when looking at dB charts.
A skilled listener is supposed to be able to hear changes of 1dB or more. Less skilled listeners need more like 2 or 3 dB before they are sure there is a difference. (This is measuring source signal amplitude, by the way, not acoustic power.)
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:
gain = powf(10, attenuation / 20.0f);
The equations to convert from gain to db are:
attenuation_in_db = 20 * log10 (gain)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With