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

Add option to disable sending TTS events to client #16

Merged
merged 1 commit into from
Jun 4, 2024
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
7 changes: 7 additions & 0 deletions Content.Client/_Sunrise/TTS/TTSSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public override void Initialize()
_cfg.OnValueChanged(SunriseCCVars.TTSVolume, OnTtsVolumeChanged, true);
_cfg.OnValueChanged(SunriseCCVars.TTSRadioVolume, OnTtsRadioVolumeChanged, true);
_cfg.OnValueChanged(SunriseCCVars.TTSAnnounceVolume, OnTtsAnnounceVolumeChanged, true);
_cfg.OnValueChanged(SunriseCCVars.TTSClientEnabled, OnTtsClientOptionChanged, true);
SubscribeNetworkEvent<PlayTTSEvent>(OnPlayTTS);
SubscribeNetworkEvent<AnnounceTtsEvent>(OnAnnounceTTSPlay);
}
Expand All @@ -53,6 +54,7 @@ public override void Shutdown()
_cfg.UnsubValueChanged(SunriseCCVars.TTSVolume, OnTtsVolumeChanged);
_cfg.UnsubValueChanged(SunriseCCVars.TTSRadioVolume, OnTtsRadioVolumeChanged);
_cfg.UnsubValueChanged(SunriseCCVars.TTSAnnounceVolume, OnTtsAnnounceVolumeChanged);
_cfg.UnsubValueChanged(SunriseCCVars.TTSClientEnabled, OnTtsClientOptionChanged);
_contentRoot.Dispose();
}

Expand All @@ -71,6 +73,11 @@ private void OnTtsAnnounceVolumeChanged(float volume)
_volumeAnnounce = volume;
}

private void OnTtsClientOptionChanged(bool option)
{
RaiseNetworkEvent(new ClientOptionTTSEvent(option));
}

private void OnAnnounceTTSPlay(AnnounceTtsEvent ev)
{
if (_volumeAnnounce == 0)
Expand Down
21 changes: 17 additions & 4 deletions Content.Server/_Sunrise/TTS/TTSSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public sealed partial class TTSSystem : EntitySystem
private const int MaxMessageChars = 100 * 2; // same as SingleBubbleCharLimit * 2
private bool _isEnabled = false;
private string _voiceId = "Hanson";
private List<ICommonSession> _ignoredRecipients = new();
private string _nukieVoiceId = "Sentrybot";
public const float WhisperVoiceVolumeModifier = 0.6f; // how far whisper goes in world units
public const int WhisperVoiceRange = 4; // how far whisper goes in world units
Expand All @@ -56,6 +57,7 @@ public override void Initialize()
SubscribeLocalEvent<AnnouncementSpokeEvent>(OnAnnouncementSpoke);

SubscribeNetworkEvent<RequestPreviewTTSEvent>(OnRequestPreviewTTS);
SubscribeNetworkEvent<ClientOptionTTSEvent>(OnClientOptionTTS);
}

private async void OnRequestPreviewTTS(RequestPreviewTTSEvent ev, EntitySessionEventArgs args)
Expand All @@ -72,6 +74,14 @@ private async void OnRequestPreviewTTS(RequestPreviewTTSEvent ev, EntitySessionE
RaiseNetworkEvent(new PlayTTSEvent(soundData), Filter.SinglePlayer(args.SenderSession));
}

private async void OnClientOptionTTS(ClientOptionTTSEvent ev, EntitySessionEventArgs args)
{
if (ev.Enabled)
_ignoredRecipients.Remove(args.SenderSession);
else
_ignoredRecipients.Add(args.SenderSession);
}

private void OnRadioReceiveEvent(RadioSpokeEvent args)
{
if (!_isEnabled || args.Message.Length > MaxMessageChars)
Expand Down Expand Up @@ -112,13 +122,13 @@ private async void OnAnnouncementSpoke(AnnouncementSpokeEvent args)
args.Message.Length > MaxMessageChars * 2 ||
!GetVoicePrototype(args.Nukie ? _nukieVoiceId : _voiceId, out var protoVoice))
{
RaiseNetworkEvent(new AnnounceTtsEvent(new byte[] { }), args.Source);
RaiseNetworkEvent(new AnnounceTtsEvent(new byte[] { }), args.Source.RemovePlayers(_ignoredRecipients));
return;
}

var soundData = await GenerateTTS(args.Message, protoVoice.Speaker, isAnnounce: true);
soundData ??= new byte[] { };
RaiseNetworkEvent(new AnnounceTtsEvent(soundData), args.Source);
RaiseNetworkEvent(new AnnounceTtsEvent(soundData), args.Source.RemovePlayers(_ignoredRecipients));
}

private async void OnEntitySpoke(EntityUid uid, TTSComponent component, EntitySpokeEvent args)
Expand Down Expand Up @@ -151,7 +161,7 @@ private async void HandleSay(EntityUid uid, string message, string speaker)
{
var soundData = await GenerateTTS(message, speaker);
if (soundData is null) return;
RaiseNetworkEvent(new PlayTTSEvent(soundData, GetNetEntity(uid)), Filter.Pvs(uid));
RaiseNetworkEvent(new PlayTTSEvent(soundData, GetNetEntity(uid)), Filter.Pvs(uid).RemovePlayers(_ignoredRecipients));
}

private async void HandleWhisper(EntityUid uid, string message, string speaker, bool isRadio)
Expand All @@ -171,6 +181,9 @@ private async void HandleWhisper(EntityUid uid, string message, string speaker,
if (!session.AttachedEntity.HasValue)
continue;

if (_ignoredRecipients.Contains(session))
return;

var xform = xformQuery.GetComponent(session.AttachedEntity.Value);
var distance = (sourcePos - _xforms.GetWorldPosition(xform, xformQuery)).LengthSquared();

Expand All @@ -194,7 +207,7 @@ private async void HandleRadio(EntityUid[] uids, string message, string speaker)

foreach (var uid in uids)
{
RaiseNetworkEvent(new PlayTTSEvent(soundData, GetNetEntity(uid), true), Filter.Entities(uid));
RaiseNetworkEvent(new PlayTTSEvent(soundData, GetNetEntity(uid), true), Filter.Entities(uid).RemovePlayers(_ignoredRecipients));
}
}

Expand Down
7 changes: 7 additions & 0 deletions Content.Shared/_Sunrise/SunriseCCVars/SunriseCCVars.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ public sealed class SunriseCCVars
public static readonly CVarDef<int> TTSApiTimeout =
CVarDef.Create("tts.api_timeout", 5, CVar.SERVERONLY | CVar.ARCHIVE);

/// <summary>
/// Option to disable TTS events for client
/// </summary>
public static readonly CVarDef<bool> TTSClientEnabled =
CVarDef.Create("tts.client_enabled", false, CVar.CLIENTONLY | CVar.ARCHIVE);


/// <summary>
/// Default volume setting of TTS sound
/// </summary>
Expand Down
13 changes: 13 additions & 0 deletions Content.Shared/_Sunrise/TTS/ClientOptionTTSEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Robust.Shared.Serialization;

namespace Content.Shared._Sunrise.TTS;

[Serializable, NetSerializable]
public sealed class ClientOptionTTSEvent : EntityEventArgs
{
public bool Enabled { get; }
public ClientOptionTTSEvent(bool enabled)
{
Enabled = enabled;
}
}
Loading