Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I attenuate a WAV file by a given decibel value?

Tags:

audio

wav

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.

like image 927
MusiGenesis Avatar asked Jul 19 '09 02:07

MusiGenesis


People also ask

How much difference is 20dB?

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.

Can you hear a 1 dB difference?

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.

What is 20dB?

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.

Can you hear a 2 dB difference?

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.)


2 Answers

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)
like image 86
Nils Pipenbrinck Avatar answered Oct 16 '22 10:10

Nils Pipenbrinck


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.

like image 29
Norman Ramsey Avatar answered Oct 16 '22 08:10

Norman Ramsey