Convert a linear scale to DirectX units and back

Started by
0 comments, last by Anthony Serrano 8 years, 1 month ago

I'm an audio noob and this question was answered back in '05 here, but I was hoping to understand what the numbers mean. For example why is 2000 used and why do both functions have .5 added to them? Here are the functions JohnBolton posted followed by his explanation:


    int ConvertLinearToDirectX( int value, int max )
    {
        if ( value == 0 )
        {
           return -10000;
        }
        else
        {
            return (int)floorf( 2000.0f * log10f( (float)(value) / (float)max ) + 0.5f );
        }
    }

    int ConvertDirectXToLinear( int value, int max )
    {
        if ( value == -10000 )
        {
            return 0;
        }
        else
        {
            return (int)floorf( powf( 10.0f, (float)value / 2000.0f ) * (float)max ) + 0.5f );
        }
    } 

JohnBolton, on 07 Aug 2005 - 09:34 AM, said:

DirectX uses a modified decibel scale (millibels?), in which the units are in 1/100ths of a decibel (dB). The decibel scale is a little complicated -- for one, it is logarithmic and not linear. Without going into the details, adding 6.02 dB (20*log(2)) makes the sound twice as loud and adding -6.02 dB (20*log(0.5)) makes the sound half as loud. -6.02 dB is -602 in the DirectX system, so setting the volume to -602 will make it half as loud. Setting it to -1204 will make it 1/4 as loud, -1806 will make it 1/8 as loud, and so on.

Advertisement

The 2000 is a conversion factor: *1000 to convert from DirectX units (mB) to Bels, and *2 to account for the fact that sound pressure is proportional to the square of amplitude.

The 0.5 is a bias factor, added in to allow the use of floor() to round off.

This topic is closed to new replies.

Advertisement