Date: 26/4/2012
Tags: audio | I've been writing a tool to normalize lots of audio files at once, as well as convert between various loss-less formats (particularly FLAC and WAV). In doing that I needed a way of converting between the raw audio sample maximum and dB. So I present to you my C functions for doing so:
double LinearToDb(int32 linear, int bitDepth)
{
uint32 MaxLinear = (1 << (bitDepth - 1)) - 1;
uint32 ab = linear >= 0 ? linear : -linear;
return log10((double)ab / MaxLinear) * 20.0;
}
int32 DbToLinear(double dB, int bitDepth)
{
uint32 MaxLinear = (1 << (bitDepth - 1)) - 1;
double d = pow(10, dB / 20);
return d * MaxLinear;
}
Another code snippit for Google to index.
|