Asked by:
Converting decibels to linear 0100 scale

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 dBfMinLevelDB=51.0 dBfStepping = 0.25 dB
for Microphone Volume:fMaxLevelDB = 6.0 dBfMinLevelDB=40.5 dBfStepping = 0.25 dB
However, all volume controls are represented by standard 0100 sliders in Windows dialogs.
I.e. for Master VolumefMaxLevelDB = 0.0 dB corresponds to 100fMinLevelDB=51.0 dB corresponds to 0
for Microphone Volume:fMaxLevelDB = 6.0 dB corresponds to 100fMinLevelDB=40.5 dB corresponds to 0
A’m not using decibels in my app, so I need to convert decibels to linear 0100 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 0100 scale value shown by slider in Windows dialog?
Thanks in advance!
Question
All replies

Hi Alies',
Could you tell me your expectation of the mapping between intermediate values? For example,
For Master Volume,
 25.5 dB corresponds to 50
 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 
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 VolumelinVal dBVal0 510,1 30,8480,2 22,4770,3 17,1110,4 13,1520,5 10,0150,6 7,4150,7 4,9910,8 3,260,9 1,3811 0
Microphone VolumelinVal dBVal0 40,50,1 13,6380,2 7,8170,3 4,3620,4 1,8970,5 0,020,6 1,590,7 2,920,8 4,0720,9 5,1851 6
And I've found the equations for Microphone Volume:
dBVal = fMaxLevelDB + 20 * log(linVal)linVal = 10 ^ ( (dBValfMaxLevelDB)/20 )
It fits perfectly for Microphone Volume but it doesn't work for Master Volume.

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 unpowered 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 base10 log of the value.
The slider value relationship between master volume and micrphone gain shouldn't relate to each other at all.