Fixing an issue with rounding causing certain volume levels to not show the same on the steam deck as they did in the volume mixer

This commit is contained in:
dlprows 2023-09-10 22:06:38 -06:00
parent 0d056215bc
commit b57ea24b11
5 changed files with 32 additions and 20 deletions

View File

@ -65,18 +65,14 @@ public class ActiveAudioSessionWrapper : IAudioSession
{ {
//if you have more than one volume. they will all get set based on the first volume control //if you have more than one volume. they will all get set based on the first volume control
var level = Volume.FirstOrDefault()?.MasterVolume ?? 0; var level = Volume.FirstOrDefault()?.MasterVolume ?? 0;
level = VolumeHelpers.GetAdjustedVolume(level, step, ticks);
level += (0.01f * step) * ticks;
level = Math.Max(level, 0);
level = Math.Min(level, 1);
Volume.ForEach(x => x.MasterVolume = level); Volume.ForEach(x => x.MasterVolume = level);
} }
public int GetVolumeLevel() public int GetVolumeLevel()
{ {
var level = Volume.FirstOrDefault()?.MasterVolume ?? 0; var level = Volume.FirstOrDefault()?.MasterVolume ?? 0;
return (int)(level * 100); return VolumeHelpers.GetVolumePercentage(level);
} }
} }

View File

@ -24,15 +24,10 @@ internal class SystemSoundsAudioSession : IAudioSession
public void IncrementVolumeLevel(int step, int ticks) public void IncrementVolumeLevel(int step, int ticks)
{ {
var level = _volumeControl.MasterVolume; var level = VolumeHelpers.GetAdjustedVolume(_volumeControl.MasterVolume, step, ticks);
level += (0.01f * step) * ticks;
level = Math.Max(level, 0);
level = Math.Min(level, 1);
_volumeControl.MasterVolume = level; _volumeControl.MasterVolume = level;
} }
public int GetVolumeLevel() => (int)(_volumeControl.MasterVolume * 100); public int GetVolumeLevel() => VolumeHelpers.GetVolumePercentage(_volumeControl.MasterVolume);
} }

View File

@ -24,15 +24,10 @@ internal class SystemVolumeAudioSession : IAudioSession
public void IncrementVolumeLevel(int step, int ticks) public void IncrementVolumeLevel(int step, int ticks)
{ {
var level = _volumeControl.MasterVolumeLevelScalar; var level = VolumeHelpers.GetAdjustedVolume(_volumeControl.MasterVolumeLevelScalar, step, ticks);
level += (0.01f * step) * ticks;
level = Math.Max(level, 0);
level = Math.Min(level, 1);
_volumeControl.MasterVolumeLevelScalar = level; _volumeControl.MasterVolumeLevelScalar = level;
} }
public int GetVolumeLevel() => (int)(_volumeControl.MasterVolumeLevelScalar * 100); public int GetVolumeLevel() => VolumeHelpers.GetVolumePercentage(_volumeControl.MasterVolumeLevelScalar);
} }

View File

@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FocusVolumeControl.AudioSessions
{
internal class VolumeHelpers
{
public static float GetAdjustedVolume(float startingVolume, int step, int ticks)
{
var level = startingVolume;
level += 0.01f * step * ticks;
level = Math.Max(level, 0);
level = Math.Min(level, 1);
return level;
}
public static int GetVolumePercentage(float volume) => (int)Math.Round(volume * 100);
}
}

View File

@ -56,6 +56,7 @@
<ItemGroup> <ItemGroup>
<Compile Include="AudioSessions\ActiveAudioSessionWrapper.cs" /> <Compile Include="AudioSessions\ActiveAudioSessionWrapper.cs" />
<Compile Include="AudioHelper.cs" /> <Compile Include="AudioHelper.cs" />
<Compile Include="AudioSessions\VolumeHelpers.cs" />
<Compile Include="AudioSessions\SystemSoundsAudioSession.cs" /> <Compile Include="AudioSessions\SystemSoundsAudioSession.cs" />
<Compile Include="AudioSessions\SystemVolumeAudioSession.cs" /> <Compile Include="AudioSessions\SystemVolumeAudioSession.cs" />
<Compile Include="DialAction.cs" /> <Compile Include="DialAction.cs" />