02-25-2011, 08:56 PM
AnthonyZ Wrote:Dean, could you describe the conversion that you're using to generate % as opposed to dB? I would interested to see how you scale it.
In this case, I'm trying to be a little clever and avoid any rounding issues. Since the actual range is -80.5 to +18, that's a 98.5 dB range, so it's almost 100. So what I did was adjust it so that +18 comes out to 100% and then it goes down from there. If it's -79dB or less, I just set it to 0%, i.e. I cheat on the lowest 1.5dB. No one is going to care down there if the volume isn't 100% correct since you can't hear it anyway.
This allows me to stick basically to a fairly one to one mapping of dBs to percentages. In most cases you'd do like this:
percent = (card)((curval / float(maxval - minval)) * 100.0);
So you take the current value, divide it by the maximum range (as a floating point to force the calculation to floating point), then multiply that times 100 to turn it from a 0.0 to 1.0 value to a 0 to 100 percent value and convert that to a card value to store as a percent. The max-min you can pre-calculate of course. To go the other way it's Just:
dB = ((percent / 100.0) * (maxval - minval)) + minval;
So you just take the current percent, convert it back to a 0.0 to 1.0 value, and multiply times the range to figure out where in the range you are, then add that to the minimum value.
There are possibilities for a little rounding to happen. It's kind of unavoidable sometimes, but generally most devices have a range that's not too far from 100dB so it shouldn't be too bad in most cases.
Dean Roddey
Explorans limites defectum
Explorans limites defectum