Directsound logarithm volume to linear volume slider

青春壹個敷衍的年華 提交于 2019-12-06 16:44:48
Nico Schertler

There is a rule of thumb to determine the perceived loudness:

A difference of 10 dB (doubleValue) results in a sound twice / half as loud as the original source.

With that in mind we can create a formula that maps the attenuation to the sound pressure level.

But at first we have to calculate the actual attenuation (as a fraction). DirectSound can attenuate a sound by 100 dB, which is an attenuation of 1/2^(100/doubleValue). This is the value for the minimum trackbar value. The maximum value is 1 (no change). So overall:

doubleValue = 10;
minimumAttenuation = 1/2^(100/doubleValue)
attenuation = minimumAttenuation + trkBalance.Value / 100 * (1 - minimumAttenuation);

Now we have a value within valid range. Now we need to find the sound pressure level for this attenuation.

And we know that the loudness doubles every 10 db (doubleValue):

attenuation = 2^(db/doubleValue) //ln
ln(attenuation) = db / doubleValue * ln(2)
db = doubleValue * ln(attenuation) / ln(2)

And since DirectSound takes hundreths dB, you can use

foreGroundSound.Volume = db * 100;

Those are just some theoretical thoughts based on wikipedia information. It might or might not work. Just try it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!