none
Converting decibels to linear 0-100 scale

    Question

  • Hello!

    I’m rewriting my app (C++, sound playing and capturing) using Windows Core Audio instead of DirectSound so that it could work in Win 7.
    I need to get and set volume levels for different connectors on soundcard (represented by IPart instances). 


    IAudioVolumeLevel *pVolume = NULL;
    pPart->Activate(CLSCTX_ALL, __uuidof(IAudioVolumeLevel), (void**)&pVolume);
    UINT nChannels = 0;
    pVolume->GetChannelCount(&nChannels);
    
    for (UINT n = 0; n < nChannels; n++)
    {
     float fMinLevelDB, fMaxLevelDB, fStepping, fLevelDB;
     pVolume->GetLevelRange(n, &fMinLevelDB, &fMaxLevelDB, &fStepping);
     pVolume->GetLevel(n, &fLevelDB);
    }
    

    GetLevelRange and GetLevel routines return volume bounds and current volume level in decibels

    E.g. for MasteVolume:
    fMaxLevelDB = 0.0 dB
    fMinLevelDB=-51.0 dB
    fStepping = 0.25 dB

    for Microphone Volume:
    fMaxLevelDB = 6.0 dB
    fMinLevelDB=-40.5 dB
    fStepping = 0.25 dB

    However, all volume controls are represented by standard 0-100 sliders in Windows dialogs.

    I.e. for Master Volume 
    fMaxLevelDB = 0.0 dB corresponds to 100
    fMinLevelDB=-51.0 dB corresponds to 0

    for Microphone Volume:
    fMaxLevelDB = 6.0 dB corresponds to 100
    fMinLevelDB=-40.5 dB corresponds to 0

    A’m not using decibels in my app, so I need to convert decibels to linear 0-100 scale and back. 
    I found some equations in the web:

    dBVal = 20 * log(linVal)
    linVal = 10 ^(dBVal/20)

    But it’s obvious, that these equations don’t work, because linear 0 is -51.0 dB for Master Volume and -40.5 dB for Microphone Volume. I should take fMaxLevelDB and fMinLevelDB into account to write universal algorithm for such conversion.

    So, the question is: How to convert decibels returned by GetLevel routine to linear 0-100 scale value shown by slider in Windows dialog?

    Thanks in advance!
    Thursday, November 04, 2010 12:26 PM

All replies

  • Hi Alies',

     

    Could you tell me your expectation of the mapping between intermediate values?  For example,

     

    For Master Volume,

    1. -25.5 dB corresponds to 50
    2. -6 dB corresponds to 50

    The latter case is derived as follows.

    For fMaxLevelDB (0.0), linVal is 10^(0.0/20) = 1.

    For fMinLevelDB (-51.0), linVal is 10^(-51.0/20) = 0.00281838293126445381910192369916.

    Hence the middle value is 0.50140919146563222690955096184958.

    Then dB of the middle value is 20*log(0.50140919146563222690955096184958) = -5.996154182736857342849128430855 -> -6.

     

    Which is your expectation or do you have any other ideas?

     

    Thanks

     


    eijit
    Thursday, November 04, 2010 4:36 PM
  • I've measured some points setting volume by slider in Windows dialog and getting volume in decibels using GetLevel routine. 
    Here's the table:


    Master Volume
    linVal    dBVal
    ----------------
    0        -51
    0,1      -30,848
    0,2      -22,477
    0,3      -17,111
    0,4      -13,152
    0,5      -10,015
    0,6      -7,415
    0,7      -4,991
    0,8      -3,26
    0,9      -1,381
    1        0


    Microphone Volume
    linVal   dBVal
    ----------------
    0        -40,5
    0,1      -13,638
    0,2      -7,817
    0,3      -4,362
    0,4      -1,897
    0,5      0,02
    0,6      1,59
    0,7      2,92
    0,8      4,072
    0,9      5,185
    1        6


    And I've found the equations for Microphone Volume:

    dBVal = fMaxLevelDB + 20 * log(linVal)
    linVal = 10 ^ ( (dBVal-fMaxLevelDB)/20 )

    It fits perfectly for Microphone Volume but it doesn't work for Master Volume.

    Friday, November 05, 2010 8:17 AM
  • If you're looking to create a  "universal" algorithm to convert from linear and logarithmic scales, that's easily done. The design issue you personally need to deal with is how you would like to use the sliderbar values for both microphone and master volume. The first thing you can ditch is the need to match or come close to windows dialog values. You can decide how the slider values scale yourself. The only design considerations you need to know are:

    -Master volume shouldn't exceed a gain of 1

    -Microphone gain is subject to two aspects. Powered microphones vs un-powered microphones. Since your gain value is 6dB in your table, it's an unpowered microphone using a gain of up to 4x the microphone signal power (3dB = double gain, -3dB = half gain). For a powered microphone you'd most likely want to cap the gain at 1, the same as master volume.

    -Slidebar values are zero or greater so you need to offset the bar value to get negative values.

    -Use the slidebar value as the desired linear gain for your microphone possibly using the midpoint of the slider as a gain of 1. This way your gain adjusts symmetrically. Calculate the power base-10 log of the value.

    -The slider value relationship between master volume and micrphone gain shouldn't relate to each other at all.

    Thursday, November 11, 2010 12:05 AM