michael
michael

Reputation: 65

MediaPlayer.Volume control

I have a slider where I get a value from 0 to 100 to adjust the volume. The MediaPlayer.Volume expects a value between 0.0 and 1.0 while 0=-96db and 1=-0db. Currently I use the following code to adjust the linear values from my slider:

 float newVolume = (float)(Math.Sqrt(sliderValue) / 10);
 newVolume = MathHelper.Clamp(newVolume, 0.0001f, 1.0f);
 MediaPlayer.Volume = newVolume;

This works better then directly mapping the 0 to 100 values but in the higher half the adjustment is still rather low compared to the lower half. Are there any better solutions?

Upvotes: 1

Views: 3825

Answers (1)

Boinst
Boinst

Reputation: 3505

This works for me, where 'value' is from 0 to 100

        value = MathHelper.Clamp(value, 0f, 100f);
        var logged = Math.Log(value + 1) / Math.Log(101);
        MediaPlayer.Volume = MathHelper.Clamp((float)logged, 0.001f, 1.0f);

Upvotes: 1

Related Questions