How do I attenuate a WAV file by a given decibel value?
audio, wav
Solution
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)
Problem
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.