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

TTS optimization #1873

Merged
merged 4 commits into from
Feb 16, 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
32 changes: 20 additions & 12 deletions Content.Client/Corvax/TTS/TTSSystem.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using Content.Shared.Corvax.CCCVars;
using Content.Shared.Chat;
using Content.Shared.Corvax.CCCVars;
using Content.Shared.Corvax.TTS;
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.Player;
using Robust.Shared.Utility;

namespace Content.Client.Corvax.TTS;
Expand All @@ -17,8 +18,8 @@ namespace Content.Client.Corvax.TTS;
public sealed class TTSSystem : EntitySystem
{
[Dependency] private readonly IConfigurationManager _cfg = default!;
[Dependency] private readonly IResourceCache _resourceCache = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
[Dependency] private readonly IResourceManager _res = default!;
[Dependency] private readonly AudioSystem _audio = default!;

private ISawmill _sawmill = default!;
private readonly MemoryContentRoot _contentRoot = new();
Expand All @@ -40,7 +41,7 @@ public sealed class TTSSystem : EntitySystem
public override void Initialize()
{
_sawmill = Logger.GetSawmill("tts");
_resourceCache.AddRoot(Prefix, _contentRoot);
_res.AddRoot(Prefix, _contentRoot);
_cfg.OnValueChanged(CCCVars.TTSVolume, OnTtsVolumeChanged, true);
SubscribeNetworkEvent<PlayTTSEvent>(OnPlayTTS);
}
Expand All @@ -64,24 +65,26 @@ private void OnTtsVolumeChanged(float volume)

private void OnPlayTTS(PlayTTSEvent ev)
{
_sawmill.Debug($"Play TTS audio {ev.Data.Length} bytes from {ev.SourceUid} entity");

var volume = AdjustVolume(ev.IsWhisper);
_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 audioParams = AudioParams.Default.WithVolume(volume);
var soundPath = new SoundPathSpecifier(Prefix / filePath, audioParams);
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);
_audio.PlayEntity(soundPath, Filter.Local(), sourceUid, false, audioParams);
_audio.PlayEntity(audioResource.AudioStream, sourceUid, audioParams);
}
else
{
_audio.PlayGlobal(soundPath, Filter.Local(), false);
_audio.PlayGlobal(audioResource.AudioStream, audioParams);
}

_contentRoot.RemoveFile(filePath);
Expand All @@ -98,4 +101,9 @@ private float AdjustVolume(bool isWhisper)

return volume;
}

private float AdjustDistance(bool isWhisper)
{
return isWhisper ? SharedChatSystem.WhisperMuffledRange : SharedChatSystem.VoiceRange;
}
}
8 changes: 5 additions & 3 deletions Content.Server/Chat/Systems/ChatSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ public sealed partial class ChatSystem : SharedChatSystem
[Dependency] private readonly SharedInteractionSystem _interactionSystem = default!;
[Dependency] private readonly ReplacementAccentSystem _wordreplacement = default!;

public const int VoiceRange = 10; // how far voice goes in world units
public const int WhisperClearRange = 2; // how far whisper goes while still being understandable, in world units
public const int WhisperMuffledRange = 5; // how far whisper goes at all, in world units
// Corvax-TTS-Start: Moved from Server to Shared
// public const int VoiceRange = 10; // how far voice goes in world units
// public const int WhisperClearRange = 2; // how far whisper goes while still being understandable, in world units
// public const int WhisperMuffledRange = 5; // how far whisper goes at all, in world units
// Corvax-TTS-End
public const string DefaultAnnouncementSound = "/Audio/Corvax/Announcements/announce.ogg"; // Corvax-Announcements
public const string CentComAnnouncementSound = "/Audio/Corvax/Announcements/centcomm.ogg"; // Corvax-Announcements

Expand Down
4 changes: 2 additions & 2 deletions Content.Server/Corvax/TTS/TTSManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ public void Initialize()
if (_cache.TryGetValue(cacheKey, out var data))
{
ReusedCount.Inc();
_sawmill.Debug($"Use cached sound for '{text}' speech by '{speaker}' speaker");
_sawmill.Verbose($"Use cached sound for '{text}' speech by '{speaker}' speaker");
return data;
}

_sawmill.Debug($"Generate new audio for '{text}' speech by '{speaker}' speaker");
_sawmill.Verbose($"Generate new audio for '{text}' speech by '{speaker}' speaker");

var body = new GenerateVoiceRequest
{
Expand Down
5 changes: 5 additions & 0 deletions Content.Shared/Chat/SharedChatSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public abstract class SharedChatSystem : EntitySystem
public const char AdminPrefix = ']';
public const char WhisperPrefix = ',';
public const char DefaultChannelKey = 'р'; // Corvax-Localization
// Corvax-TTS-Start: Moved from Server to Shared
public const int VoiceRange = 10; // how far voice goes in world units
public const int WhisperClearRange = 2; // how far whisper goes while still being understandable, in world units
public const int WhisperMuffledRange = 5; // how far whisper goes at all, in world units
// Corvax-TTS-End

[ValidatePrototypeId<RadioChannelPrototype>]
public const string CommonChannel = "Common";
Expand Down
Loading