Files
wwdpublic/Content.Client/_White/TTS/TTSSystem.cs
Spatison 54086988e3 Mass clean up (#587)
* mass clean up

(cherry picked from commit 12bb873b02c1ef50e20763542b030452cc0613da)

* Revert "Centrifuge buff (#393)"

This reverts commit 2a59a18230.

(cherry picked from commit 9ee495ab4bb365e1ccd3dc627ecb55114fea6944)

* Shoving merge conflict

* fix rich traitor

* fix test

* yml

* fix test

* fix test

* ohh
2025-06-16 20:35:48 +03:00

113 lines
3.3 KiB
C#

using Content.Shared._White.CCVar;
using Content.Shared.Chat;
using Content.Shared._White.TTS;
using Content.Shared.GameTicking;
using Robust.Client.Audio;
using Robust.Client.ResourceManagement;
using Robust.Shared.Audio;
using Robust.Shared.Audio.Systems;
using Robust.Shared.Configuration;
using Robust.Shared.ContentPack;
using Robust.Shared.Utility;
namespace Content.Client._White.TTS;
/// <summary>
/// Plays TTS audio in world
/// </summary>
// ReSharper disable once InconsistentNaming
public sealed class TTSSystem : EntitySystem
{
[Dependency] private readonly IConfigurationManager _cfg = default!;
[Dependency] private readonly IResourceManager _res = default!;
[Dependency] private readonly AudioSystem _audio = default!;
private ISawmill _sawmill = default!;
private readonly MemoryContentRoot _contentRoot = new();
private ResPath _prefix;
/// <summary>
/// Reducing the volume of the TTS when whispering. Will be converted to logarithm.
/// </summary>
private const float WhisperFade = 4f;
private float _volume = 0.0f;
private ulong _fileIdx = 0;
private static ulong _shareIdx = 0;
public override void Initialize()
{
_prefix = ResPath.Root / $"TTS{_shareIdx++}";
_sawmill = Logger.GetSawmill("tts");
_res.AddRoot(_prefix, _contentRoot);
_cfg.OnValueChanged(WhiteCVars.TTSVolume, OnTtsVolumeChanged, true);
SubscribeNetworkEvent<PlayTTSEvent>(OnPlayTTS);
SubscribeLocalEvent<RoundRestartCleanupEvent>(OnRoundRestart);
}
private void OnRoundRestart(RoundRestartCleanupEvent ev)
{
_contentRoot.Clear();
}
public override void Shutdown()
{
base.Shutdown();
_cfg.UnsubValueChanged(WhiteCVars.TTSVolume, OnTtsVolumeChanged);
_contentRoot.Dispose();
}
public void RequestGlobalTTS(VoiceRequestType text, string voiceId)
{
RaiseNetworkEvent(new RequestPreviewTTSEvent(voiceId));
}
private void OnTtsVolumeChanged(float volume)
{
_volume = volume;
}
private void OnPlayTTS(PlayTTSEvent ev)
{
_sawmill.Verbose($"Play TTS audio {ev.Data.Length} bytes from {ev.SourceUid} entity");
var filePath = new ResPath($"{_fileIdx++}.ogg");
_contentRoot.AddOrUpdateFile(filePath, ev.Data);
var audioResource = new AudioResource();
audioResource.Load(IoCManager.Instance!, _prefix / filePath);
var audioParams = AudioParams.Default
.WithVolume(AdjustVolume(ev.IsWhisper))
.WithMaxDistance(AdjustDistance(ev.IsWhisper));
if (ev.SourceUid != null)
{
var sourceUid = GetEntity(ev.SourceUid.Value);
if(sourceUid.IsValid())
_audio.PlayEntity(audioResource.AudioStream, sourceUid, audioParams);
}
else
{
_audio.PlayGlobal(audioResource.AudioStream, audioParams);
}
_contentRoot.RemoveFile(filePath);
}
private float AdjustVolume(bool isWhisper)
{
var volume = SharedAudioSystem.GainToVolume(_volume);
if (isWhisper)
volume -= SharedAudioSystem.GainToVolume(WhisperFade);
return volume;
}
private float AdjustDistance(bool isWhisper)
{
return isWhisper ? SharedChatSystem.WhisperMuffledRange : SharedChatSystem.VoiceRange;
}
}