Make audio sliders use gain not volume (#21984)
This commit is contained in:
@@ -101,7 +101,7 @@ public sealed class AmbientSoundSystem : SharedAmbientSoundSystem
|
||||
_cfg.OnValueChanged(CCVars.AmbientCooldown, SetCooldown, true);
|
||||
_cfg.OnValueChanged(CCVars.MaxAmbientSources, SetAmbientCount, true);
|
||||
_cfg.OnValueChanged(CCVars.AmbientRange, SetAmbientRange, true);
|
||||
_cfg.OnValueChanged(CCVars.AmbienceVolume, SetAmbienceVolume, true);
|
||||
_cfg.OnValueChanged(CCVars.AmbienceVolume, SetAmbienceGain, true);
|
||||
SubscribeLocalEvent<AmbientSoundComponent, ComponentShutdown>(OnShutdown);
|
||||
}
|
||||
|
||||
@@ -116,9 +116,9 @@ public sealed class AmbientSoundSystem : SharedAmbientSoundSystem
|
||||
_playingCount.Remove(sound.Path);
|
||||
}
|
||||
|
||||
private void SetAmbienceVolume(float value)
|
||||
private void SetAmbienceGain(float value)
|
||||
{
|
||||
_ambienceVolume = value;
|
||||
_ambienceVolume = SharedAudioSystem.GainToVolume(value);
|
||||
|
||||
foreach (var (comp, values) in _playingSounds)
|
||||
{
|
||||
@@ -141,7 +141,7 @@ public sealed class AmbientSoundSystem : SharedAmbientSoundSystem
|
||||
_cfg.UnsubValueChanged(CCVars.AmbientCooldown, SetCooldown);
|
||||
_cfg.UnsubValueChanged(CCVars.MaxAmbientSources, SetAmbientCount);
|
||||
_cfg.UnsubValueChanged(CCVars.AmbientRange, SetAmbientRange);
|
||||
_cfg.UnsubValueChanged(CCVars.AmbienceVolume, SetAmbienceVolume);
|
||||
_cfg.UnsubValueChanged(CCVars.AmbienceVolume, SetAmbienceGain);
|
||||
}
|
||||
|
||||
private int PlayingCount(string countSound)
|
||||
|
||||
@@ -119,7 +119,7 @@ public sealed class BackgroundAudioSystem : EntitySystem
|
||||
}
|
||||
|
||||
_lobbyStream = _audio.PlayGlobal(file, Filter.Local(), false,
|
||||
_lobbyParams.WithVolume(_lobbyParams.Volume + _configManager.GetCVar(CCVars.LobbyMusicVolume)))?.Entity;
|
||||
_lobbyParams.WithVolume(_lobbyParams.Volume + SharedAudioSystem.GainToVolume(_configManager.GetCVar(CCVars.LobbyMusicVolume))))?.Entity;
|
||||
}
|
||||
|
||||
private void EndLobbyMusic()
|
||||
|
||||
@@ -75,7 +75,7 @@ public sealed partial class ContentAudioSystem
|
||||
|
||||
private void AmbienceCVarChanged(float obj)
|
||||
{
|
||||
_volumeSlider = obj;
|
||||
_volumeSlider = SharedAudioSystem.GainToVolume(obj);
|
||||
|
||||
if (_ambientMusicStream != null && _musicProto != null)
|
||||
{
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using Content.Shared.Audio;
|
||||
using Content.Shared.CCVar;
|
||||
using Robust.Client.GameObjects;
|
||||
using Robust.Shared;
|
||||
using Robust.Shared.Audio;
|
||||
using AudioComponent = Robust.Shared.Audio.Components.AudioComponent;
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ using Robust.Client.UserInterface;
|
||||
using Robust.Client.UserInterface.Controls;
|
||||
using Robust.Client.UserInterface.XAML;
|
||||
using Robust.Shared;
|
||||
using Robust.Shared.Audio.Systems;
|
||||
using Robust.Shared.Configuration;
|
||||
using Range = Robust.Client.UserInterface.Controls.Range;
|
||||
|
||||
@@ -16,14 +17,14 @@ namespace Content.Client.Options.UI.Tabs
|
||||
public sealed partial class AudioTab : Control
|
||||
{
|
||||
[Dependency] private readonly IConfigurationManager _cfg = default!;
|
||||
private readonly AudioSystem _audio;
|
||||
private readonly IAudioManager _audio;
|
||||
|
||||
public AudioTab()
|
||||
{
|
||||
RobustXamlLoader.Load(this);
|
||||
IoCManager.InjectDependencies(this);
|
||||
|
||||
_audio = IoCManager.Resolve<IEntityManager>().System<AudioSystem>();
|
||||
_audio = IoCManager.Resolve<IAudioManager>();
|
||||
LobbyMusicCheckBox.Pressed = _cfg.GetCVar(CCVars.LobbyMusicEnabled);
|
||||
RestartSoundsCheckBox.Pressed = _cfg.GetCVar(CCVars.RestartSoundsEnabled);
|
||||
EventMusicCheckBox.Pressed = _cfg.GetCVar(CCVars.EventMusicEnabled);
|
||||
@@ -82,7 +83,7 @@ namespace Content.Client.Options.UI.Tabs
|
||||
|
||||
private void OnMasterVolumeSliderChanged(Range range)
|
||||
{
|
||||
_audio.SetMasterVolume(MasterVolumeSlider.Value / 100);
|
||||
_audio.SetMasterGain(MasterVolumeSlider.Value / 100f);
|
||||
UpdateChanges();
|
||||
}
|
||||
|
||||
@@ -111,15 +112,16 @@ namespace Content.Client.Options.UI.Tabs
|
||||
|
||||
private void OnApplyButtonPressed(BaseButton.ButtonEventArgs args)
|
||||
{
|
||||
_cfg.SetCVar(CVars.AudioMasterVolume, LV100ToDB(MasterVolumeSlider.Value, CCVars.MasterMultiplier));
|
||||
_cfg.SetCVar(CVars.AudioMasterVolume, MasterVolumeSlider.Value / 100f);
|
||||
// Want the CVar updated values to have the multiplier applied
|
||||
// For the UI we just display 0-100 still elsewhere
|
||||
_cfg.SetCVar(CVars.MidiVolume, LV100ToDB(MidiVolumeSlider.Value, CCVars.MidiMultiplier));
|
||||
_cfg.SetCVar(CCVars.AmbienceVolume, LV100ToDB(AmbienceVolumeSlider.Value, CCVars.AmbienceMultiplier));
|
||||
_cfg.SetCVar(CCVars.AmbientMusicVolume, LV100ToDB(AmbientMusicVolumeSlider.Value, CCVars.AmbientMusicMultiplier));
|
||||
_cfg.SetCVar(CVars.MidiVolume, MidiVolumeSlider.Value / 100f);
|
||||
_cfg.SetCVar(CCVars.AmbienceVolume, AmbienceVolumeSlider.Value / 100f);
|
||||
_cfg.SetCVar(CCVars.AmbientMusicVolume, AmbientMusicVolumeSlider.Value / 100f);
|
||||
_cfg.SetCVar(CCVars.LobbyMusicVolume, LobbyVolumeSlider.Value / 100f);
|
||||
|
||||
_cfg.SetCVar(CCVars.LobbyMusicVolume, LV100ToDB(LobbyVolumeSlider.Value));
|
||||
_cfg.SetCVar(CCVars.MaxAmbientSources, (int)AmbienceSoundsSlider.Value);
|
||||
|
||||
_cfg.SetCVar(CCVars.LobbyMusicEnabled, LobbyMusicCheckBox.Pressed);
|
||||
_cfg.SetCVar(CCVars.RestartSoundsEnabled, RestartSoundsCheckBox.Pressed);
|
||||
_cfg.SetCVar(CCVars.EventMusicEnabled, EventMusicCheckBox.Pressed);
|
||||
@@ -135,13 +137,14 @@ namespace Content.Client.Options.UI.Tabs
|
||||
|
||||
private void Reset()
|
||||
{
|
||||
MasterVolumeSlider.Value = DBToLV100(_cfg.GetCVar(CVars.AudioMasterVolume), CCVars.MasterMultiplier);
|
||||
MidiVolumeSlider.Value = DBToLV100(_cfg.GetCVar(CVars.MidiVolume), CCVars.MidiMultiplier);
|
||||
AmbienceVolumeSlider.Value = DBToLV100(_cfg.GetCVar(CCVars.AmbienceVolume), CCVars.AmbienceMultiplier);
|
||||
AmbientMusicVolumeSlider.Value =
|
||||
DBToLV100(_cfg.GetCVar(CCVars.AmbientMusicVolume), CCVars.AmbientMusicMultiplier);
|
||||
LobbyVolumeSlider.Value = DBToLV100(_cfg.GetCVar(CCVars.LobbyMusicVolume));
|
||||
MasterVolumeSlider.Value = _cfg.GetCVar(CVars.AudioMasterVolume) * 100f;
|
||||
MidiVolumeSlider.Value = _cfg.GetCVar(CVars.MidiVolume) * 100f;
|
||||
AmbienceVolumeSlider.Value = _cfg.GetCVar(CCVars.AmbienceVolume) * 100f;
|
||||
AmbientMusicVolumeSlider.Value = _cfg.GetCVar(CCVars.AmbientMusicVolume) * 100f;
|
||||
LobbyVolumeSlider.Value = _cfg.GetCVar(CCVars.LobbyMusicVolume) * 100f;
|
||||
|
||||
AmbienceSoundsSlider.Value = _cfg.GetCVar(CCVars.MaxAmbientSources);
|
||||
|
||||
LobbyMusicCheckBox.Pressed = _cfg.GetCVar(CCVars.LobbyMusicEnabled);
|
||||
RestartSoundsCheckBox.Pressed = _cfg.GetCVar(CCVars.RestartSoundsEnabled);
|
||||
EventMusicCheckBox.Pressed = _cfg.GetCVar(CCVars.EventMusicEnabled);
|
||||
@@ -149,33 +152,25 @@ namespace Content.Client.Options.UI.Tabs
|
||||
UpdateChanges();
|
||||
}
|
||||
|
||||
// Note: Rather than moving these functions somewhere, instead switch MidiManager to using linear units rather than dB
|
||||
// Do be sure to rename the setting though
|
||||
private float DBToLV100(float db, float multiplier = 1f)
|
||||
private float GetGain(float value)
|
||||
{
|
||||
var beri = (float) (Math.Pow(10, db / 10) * 100 / multiplier);
|
||||
return beri;
|
||||
}
|
||||
|
||||
private float LV100ToDB(float lv100, float multiplier = 1f)
|
||||
{
|
||||
// Saving negative infinity doesn't work, so use -10000000 instead (MidiManager does it)
|
||||
var weh = MathF.Max(-10000000, (float) (Math.Log(lv100 * multiplier / 100, 10) * 10));
|
||||
return weh;
|
||||
return value;
|
||||
}
|
||||
|
||||
private void UpdateChanges()
|
||||
{
|
||||
// y'all need jesus.
|
||||
var isMasterVolumeSame =
|
||||
Math.Abs(MasterVolumeSlider.Value - DBToLV100(_cfg.GetCVar(CVars.AudioMasterVolume), CCVars.MasterMultiplier)) < 0.01f;
|
||||
Math.Abs(MasterVolumeSlider.Value - _cfg.GetCVar(CVars.AudioMasterVolume) * 100f) < 0.01f;
|
||||
var isMidiVolumeSame =
|
||||
Math.Abs(MidiVolumeSlider.Value - DBToLV100(_cfg.GetCVar(CVars.MidiVolume), CCVars.MidiMultiplier)) < 0.01f;
|
||||
Math.Abs(MidiVolumeSlider.Value - _cfg.GetCVar(CVars.MidiVolume) * 100f) < 0.01f;
|
||||
var isAmbientVolumeSame =
|
||||
Math.Abs(AmbienceVolumeSlider.Value - DBToLV100(_cfg.GetCVar(CCVars.AmbienceVolume), CCVars.AmbienceMultiplier)) < 0.01f;
|
||||
Math.Abs(AmbienceVolumeSlider.Value - _cfg.GetCVar(CCVars.AmbienceVolume) * 100f) < 0.01f;
|
||||
var isAmbientMusicVolumeSame =
|
||||
Math.Abs(AmbientMusicVolumeSlider.Value - DBToLV100(_cfg.GetCVar(CCVars.AmbientMusicVolume), CCVars.AmbientMusicMultiplier)) < 0.01f;
|
||||
Math.Abs(AmbientMusicVolumeSlider.Value - _cfg.GetCVar(CCVars.AmbientMusicVolume) * 100f) < 0.01f;
|
||||
var isLobbyVolumeSame =
|
||||
Math.Abs(LobbyVolumeSlider.Value - DBToLV100(_cfg.GetCVar(CCVars.LobbyMusicVolume))) < 0.01f;
|
||||
Math.Abs(LobbyVolumeSlider.Value - _cfg.GetCVar(CCVars.LobbyMusicVolume) * 100f) < 0.01f;
|
||||
|
||||
var isAmbientSoundsSame = (int)AmbienceSoundsSlider.Value == _cfg.GetCVar(CCVars.MaxAmbientSources);
|
||||
var isLobbySame = LobbyMusicCheckBox.Pressed == _cfg.GetCVar(CCVars.LobbyMusicEnabled);
|
||||
var isRestartSoundsSame = RestartSoundsCheckBox.Pressed == _cfg.GetCVar(CCVars.RestartSoundsEnabled);
|
||||
|
||||
@@ -67,28 +67,19 @@ namespace Content.Shared.CCVar
|
||||
/// Ambience volume.
|
||||
/// </summary>
|
||||
public static readonly CVarDef<float> AmbienceVolume =
|
||||
CVarDef.Create("ambience.volume", 0.0f, CVar.ARCHIVE | CVar.CLIENTONLY);
|
||||
|
||||
public const float MasterMultiplier = 2f;
|
||||
|
||||
// Midi is on engine so deal
|
||||
public const float MidiMultiplier = 3f;
|
||||
|
||||
public const float AmbienceMultiplier = 2f;
|
||||
CVarDef.Create("ambience.volume", 0.50f, CVar.ARCHIVE | CVar.CLIENTONLY);
|
||||
|
||||
/// <summary>
|
||||
/// Ambience music volume.
|
||||
/// </summary>
|
||||
public static readonly CVarDef<float> AmbientMusicVolume =
|
||||
CVarDef.Create("ambience.music_volume", 0.0f, CVar.ARCHIVE | CVar.CLIENTONLY);
|
||||
|
||||
public const float AmbientMusicMultiplier = 2f;
|
||||
CVarDef.Create("ambience.music_volume", 0.50f, CVar.ARCHIVE | CVar.CLIENTONLY);
|
||||
|
||||
/// <summary>
|
||||
/// Lobby / round end music volume.
|
||||
/// </summary>
|
||||
public static readonly CVarDef<float> LobbyMusicVolume =
|
||||
CVarDef.Create("ambience.lobby_music_volume", 0.0f, CVar.ARCHIVE | CVar.CLIENTONLY);
|
||||
CVarDef.Create("ambience.lobby_music_volume", 0.50f, CVar.ARCHIVE | CVar.CLIENTONLY);
|
||||
|
||||
/*
|
||||
* Status
|
||||
|
||||
Reference in New Issue
Block a user