Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Playing Multiple Announcement Sounds at Once #740

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 42 additions & 11 deletions Content.Client/Announcements/Systems/AnnouncerSystem.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Linq;
using Content.Client.Audio;
using Content.Shared.Announcements.Events;
using Content.Shared.Announcements.Systems;
Expand All @@ -18,8 +19,8 @@ public sealed class AnnouncerSystem : SharedAnnouncerSystem
[Dependency] private readonly IResourceCache _cache = default!;
[Dependency] private readonly IAudioManager _audioManager = default!;

private IAudioSource? AnnouncerSource { get; set; }
private float AnnouncerVolume { get; set; }
public List<IAudioSource> AnnouncerSources { get; } = new();
public float AnnouncerVolume { get; private set; }


public override void Initialize()
Expand All @@ -28,26 +29,42 @@ public override void Initialize()

AnnouncerVolume = _config.GetCVar(CCVars.AnnouncerVolume) * 100f / ContentAudioSystem.AnnouncerMultiplier;

SubscribeNetworkEvent<AnnouncementSendEvent>(OnAnnouncementReceived);
_config.OnValueChanged(CCVars.AnnouncerVolume, OnAnnouncerVolumeChanged);
_config.OnValueChanged(CCVars.AnnouncerDisableMultipleSounds, OnAnnouncerDisableMultipleSounds);

SubscribeNetworkEvent<AnnouncementSendEvent>(OnAnnouncementReceived);
}

public override void Shutdown()
{
base.Shutdown();

_config.UnsubValueChanged(CCVars.AnnouncerVolume, OnAnnouncerVolumeChanged);
_config.UnsubValueChanged(CCVars.AnnouncerDisableMultipleSounds, OnAnnouncerDisableMultipleSounds);
}


private void OnAnnouncerVolumeChanged(float value)
{
AnnouncerVolume = value;

if (AnnouncerSource != null)
AnnouncerSource.Gain = AnnouncerVolume;
foreach (var source in AnnouncerSources)
source.Gain = AnnouncerVolume;
}

private void OnAnnouncerDisableMultipleSounds(bool value)
{
if (!value)
return;

foreach (var audioSource in AnnouncerSources.ToList())
{
audioSource.Dispose();
AnnouncerSources.Remove(audioSource);
}
}


private void OnAnnouncementReceived(AnnouncementSendEvent ev)
{
if (!ev.Recipients.Contains(_player.LocalSession!.UserId)
Expand All @@ -56,14 +73,28 @@ private void OnAnnouncementReceived(AnnouncementSendEvent ev)
return;

var source = _audioManager.CreateAudioSource(resource);
if (source != null)
if (source == null)
return;

source.Gain = AnnouncerVolume * SharedAudioSystem.VolumeToGain(ev.AudioParams.Volume);
source.Global = true;

if (_config.GetCVar(CCVars.AnnouncerDisableMultipleSounds))
{
foreach (var audioSource in AnnouncerSources.ToList())
{
audioSource.Dispose();
AnnouncerSources.Remove(audioSource);
}
}

foreach (var audioSource in AnnouncerSources.ToList().Where(audioSource => !audioSource.Playing))
{
source.Gain = AnnouncerVolume * SharedAudioSystem.VolumeToGain(ev.AudioParams.Volume);
source.Global = true;
audioSource.Dispose();
AnnouncerSources.Remove(audioSource);
}

AnnouncerSource?.Dispose();
AnnouncerSource = source;
AnnouncerSource?.StartPlaying();
AnnouncerSources.Add(source);
source.StartPlaying();
}
}
3 changes: 3 additions & 0 deletions Content.Client/Options/UI/Tabs/AudioTab.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@
<CheckBox Name="LobbyMusicCheckBox" Text="{Loc 'ui-options-lobby-music'}" />
<CheckBox Name="RestartSoundsCheckBox" Text="{Loc 'ui-options-restart-sounds'}" />
<CheckBox Name="EventMusicCheckBox" Text="{Loc 'ui-options-event-music'}" />
<CheckBox Name="AnnouncerDisableMultipleSoundsCheckBox"
Text="{Loc 'ui-options-announcer-disable-multiple-sounds'}"
ToolTip="{Loc 'ui-options-announcer-disable-multiple-sounds-tooltip'}" />
<CheckBox Name="AdminSoundsCheckBox" Text="{Loc 'ui-options-admin-sounds'}" />
</BoxContainer>
</BoxContainer>
Expand Down
150 changes: 73 additions & 77 deletions Content.Client/Options/UI/Tabs/AudioTab.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,97 +30,89 @@ public AudioTab()

ApplyButton.OnPressed += OnApplyButtonPressed;
ResetButton.OnPressed += OnResetButtonPressed;
MasterVolumeSlider.OnValueChanged += OnMasterVolumeSliderChanged;
MidiVolumeSlider.OnValueChanged += OnMidiVolumeSliderChanged;
AmbientMusicVolumeSlider.OnValueChanged += OnAmbientMusicVolumeSliderChanged;
AmbienceVolumeSlider.OnValueChanged += OnAmbienceVolumeSliderChanged;
AmbienceSoundsSlider.OnValueChanged += OnAmbienceSoundsSliderChanged;
LobbyVolumeSlider.OnValueChanged += OnLobbyVolumeSliderChanged;
InterfaceVolumeSlider.OnValueChanged += OnInterfaceVolumeSliderChanged;
AnnouncerVolumeSlider.OnValueChanged += OnAnnouncerVolumeSliderChanged;
LobbyMusicCheckBox.OnToggled += OnLobbyMusicCheckToggled;
RestartSoundsCheckBox.OnToggled += OnRestartSoundsCheckToggled;
EventMusicCheckBox.OnToggled += OnEventMusicCheckToggled;
AdminSoundsCheckBox.OnToggled += OnAdminSoundsCheckToggled;

AttachUpdateChangesHandler(
MasterVolumeSlider,
MidiVolumeSlider,
AmbientMusicVolumeSlider,
AmbienceVolumeSlider,
AmbienceSoundsSlider,
LobbyVolumeSlider,
InterfaceVolumeSlider,
AnnouncerVolumeSlider,

LobbyMusicCheckBox,
RestartSoundsCheckBox,
EventMusicCheckBox,
AnnouncerDisableMultipleSoundsCheckBox,
AdminSoundsCheckBox
);

AmbienceSoundsSlider.MinValue = _cfg.GetCVar(CCVars.MinMaxAmbientSourcesConfigured);
AmbienceSoundsSlider.MaxValue = _cfg.GetCVar(CCVars.MaxMaxAmbientSourcesConfigured);

Reset();
return;

void AttachUpdateChangesHandler(params Control[] controls)
{
foreach (var control in controls)
{
switch (control)
{
case Slider slider:
slider.OnValueChanged += _ => UpdateChanges();
break;
case CheckBox checkBox:
checkBox.OnToggled += _ => UpdateChanges();
break;
}
}
}
}

protected override void Dispose(bool disposing)
{
ApplyButton.OnPressed -= OnApplyButtonPressed;
ResetButton.OnPressed -= OnResetButtonPressed;
MasterVolumeSlider.OnValueChanged -= OnMasterVolumeSliderChanged;
MidiVolumeSlider.OnValueChanged -= OnMidiVolumeSliderChanged;
AmbientMusicVolumeSlider.OnValueChanged -= OnAmbientMusicVolumeSliderChanged;
AmbienceVolumeSlider.OnValueChanged -= OnAmbienceVolumeSliderChanged;
LobbyVolumeSlider.OnValueChanged -= OnLobbyVolumeSliderChanged;
InterfaceVolumeSlider.OnValueChanged -= OnInterfaceVolumeSliderChanged;
AnnouncerVolumeSlider.OnValueChanged -= OnAnnouncerVolumeSliderChanged;
base.Dispose(disposing);
}

private void OnLobbyVolumeSliderChanged(Range obj)
{
UpdateChanges();
}

private void OnInterfaceVolumeSliderChanged(Range obj)
{
UpdateChanges();
}

private void OnAmbientMusicVolumeSliderChanged(Range obj)
{
UpdateChanges();
}

private void OnAmbienceVolumeSliderChanged(Range obj)
{
UpdateChanges();
}

private void OnAmbienceSoundsSliderChanged(Range obj)
{
UpdateChanges();
}

private void OnMasterVolumeSliderChanged(Range range)
{
_audio.SetMasterGain(MasterVolumeSlider.Value / 100f * ContentAudioSystem.MasterVolumeMultiplier);
UpdateChanges();
}

private void OnMidiVolumeSliderChanged(Range range)
{
UpdateChanges();
}

private void OnAnnouncerVolumeSliderChanged(Range range)
{
UpdateChanges();
}
DetachUpdateChangesHandler(
MasterVolumeSlider,
MidiVolumeSlider,
AmbientMusicVolumeSlider,
AmbienceVolumeSlider,
AmbienceSoundsSlider,
LobbyVolumeSlider,
InterfaceVolumeSlider,
AnnouncerVolumeSlider,

LobbyMusicCheckBox,
RestartSoundsCheckBox,
EventMusicCheckBox,
AnnouncerDisableMultipleSoundsCheckBox,
AdminSoundsCheckBox
);

private void OnLobbyMusicCheckToggled(BaseButton.ButtonEventArgs args)
{
UpdateChanges();
}
private void OnRestartSoundsCheckToggled(BaseButton.ButtonEventArgs args)
{
UpdateChanges();
}
private void OnEventMusicCheckToggled(BaseButton.ButtonEventArgs args)
{
UpdateChanges();
base.Dispose(disposing);
return;

void DetachUpdateChangesHandler(params Control[] controls)
{
foreach (var control in controls)
{
switch (control)
{
case Slider slider:
slider.OnValueChanged -= _ => UpdateChanges();
break;
case CheckBox checkBox:
checkBox.OnToggled -= _ => UpdateChanges();
break;
}
}
}
}

private void OnAdminSoundsCheckToggled(BaseButton.ButtonEventArgs args)
{
UpdateChanges();
}

private void OnApplyButtonPressed(BaseButton.ButtonEventArgs args)
{
Expand All @@ -139,6 +131,7 @@ private void OnApplyButtonPressed(BaseButton.ButtonEventArgs args)
_cfg.SetCVar(CCVars.LobbyMusicEnabled, LobbyMusicCheckBox.Pressed);
_cfg.SetCVar(CCVars.RestartSoundsEnabled, RestartSoundsCheckBox.Pressed);
_cfg.SetCVar(CCVars.EventMusicEnabled, EventMusicCheckBox.Pressed);
_cfg.SetCVar(CCVars.AnnouncerDisableMultipleSounds, AnnouncerDisableMultipleSoundsCheckBox.Pressed);
_cfg.SetCVar(CCVars.AdminSoundsEnabled, AdminSoundsCheckBox.Pressed);
_cfg.SaveToFile();
UpdateChanges();
Expand All @@ -164,6 +157,7 @@ private void Reset()
LobbyMusicCheckBox.Pressed = _cfg.GetCVar(CCVars.LobbyMusicEnabled);
RestartSoundsCheckBox.Pressed = _cfg.GetCVar(CCVars.RestartSoundsEnabled);
EventMusicCheckBox.Pressed = _cfg.GetCVar(CCVars.EventMusicEnabled);
AnnouncerDisableMultipleSoundsCheckBox.Pressed = _cfg.GetCVar(CCVars.AnnouncerDisableMultipleSounds);
AdminSoundsCheckBox.Pressed = _cfg.GetCVar(CCVars.AdminSoundsEnabled);
UpdateChanges();
}
Expand All @@ -190,10 +184,12 @@ private void UpdateChanges()
var isLobbySame = LobbyMusicCheckBox.Pressed == _cfg.GetCVar(CCVars.LobbyMusicEnabled);
var isRestartSoundsSame = RestartSoundsCheckBox.Pressed == _cfg.GetCVar(CCVars.RestartSoundsEnabled);
var isEventSame = EventMusicCheckBox.Pressed == _cfg.GetCVar(CCVars.EventMusicEnabled);
var isAnnouncerDisableMultipleSoundsSame = AnnouncerDisableMultipleSoundsCheckBox.Pressed == _cfg.GetCVar(CCVars.AnnouncerDisableMultipleSounds);
var isAdminSoundsSame = AdminSoundsCheckBox.Pressed == _cfg.GetCVar(CCVars.AdminSoundsEnabled);
var isEverythingSame = isMasterVolumeSame && isMidiVolumeSame && isAmbientVolumeSame
&& isAmbientMusicVolumeSame && isAmbientSoundsSame && isLobbySame && isRestartSoundsSame && isEventSame
&& isAdminSoundsSame && isLobbyVolumeSame && isInterfaceVolumeSame && isAnnouncerVolumeSame;
&& isAnnouncerDisableMultipleSoundsSame && isAdminSoundsSame && isLobbyVolumeSame
&& isInterfaceVolumeSame && isAnnouncerVolumeSame;
ApplyButton.Disabled = isEverythingSame;
ResetButton.Disabled = isEverythingSame;
MasterVolumeLabel.Text =
Expand Down
10 changes: 8 additions & 2 deletions Content.Shared/CCVar/CCVars.cs
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,12 @@ public static readonly CVarDef<bool>
public static readonly CVarDef<float> AnnouncerVolume =
CVarDef.Create("announcer.volume", 0.5f, CVar.ARCHIVE | CVar.CLIENTONLY);

/// <summary>
/// Disables multiple announcement sounds from playing at once
/// </summary>
public static readonly CVarDef<bool> AnnouncerDisableMultipleSounds =
CVarDef.Create("announcer.disable_multiple_sounds", false, CVar.ARCHIVE | CVar.CLIENTONLY);


/*
* Queue
Expand Down Expand Up @@ -2291,7 +2297,7 @@ public static readonly CVarDef<float>
/// </summary>
public static readonly CVarDef<float> StationGoalsChance =
CVarDef.Create("game.station_goals_chance", 0.1f, CVar.SERVERONLY);


#region CPR System
/// <summary>
Expand Down Expand Up @@ -2338,7 +2344,7 @@ public static readonly CVarDef<float>
/// </summary>
public static readonly CVarDef<float> CPRAirlossReductionMultiplier =
CVarDef.Create("cpr.airloss_reduction_multiplier", 1f, CVar.REPLICATED | CVar.SERVER);

#endregion

#region Contests System
Expand Down
2 changes: 2 additions & 0 deletions Resources/Locale/en-US/escape-menu/ui/options-menu.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ ui-options-announcer-volume = Announcer volume:
ui-options-lobby-music = Lobby & Round-end Music
ui-options-restart-sounds = Round Restart Sounds
ui-options-event-music = Event Music
ui-options-announcer-disable-multiple-sounds = Disable Overlapping Announcer Sounds
ui-options-announcer-disable-multiple-sounds-tooltip = Some announcements will not sound right, this setting isn't recommended
ui-options-admin-sounds = Play Admin Sounds
ui-options-volume-label = Volume
ui-options-volume-percent = { TOSTRING($volume, "P0") }
Expand Down
Loading