Files
wwdpublic/Content.Client/_White/TTS/TTSSystem.cs
Spatison 3b0dadc0d4 [Fix] TTS (#139)
* Revert "[Fix] TTS (#137)"

This reverts commit c5bd6b70a2.

* Revert "[Fix] Исправление ТТСа (#136)"

This reverts commit 3759acb84e.

* Revert "[Port] TTS (#121)"

This reverts commit 0db8f3aaa4.

* new TTS

* new TTS

* new TTS

* new TTS

* fix
2024-12-06 08:49:32 +02:00

120 lines
3.5 KiB
C#

using Content.Shared._White;
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;
/// <summary>
/// The volume at which the TTS sound will not be heard.
/// </summary>
private const float MinimalVolume = -10f;
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 = Math.Max(MinimalVolume, SharedAudioSystem.GainToVolume(_volume));
if (isWhisper)
{
volume -= SharedAudioSystem.GainToVolume(WhisperFade);
}
return volume;
}
private float AdjustDistance(bool isWhisper)
{
return isWhisper ? SharedChatSystem.WhisperMuffledRange : SharedChatSystem.VoiceRange;
}
}