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

[Fix] Исправление ТТСа #136

Merged
merged 2 commits into from
Dec 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
11 changes: 8 additions & 3 deletions Content.Client/Options/UI/Tabs/AudioTab.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ protected override void Dispose(bool disposing)
LobbyVolumeSlider,
InterfaceVolumeSlider,
AnnouncerVolumeSlider,
TtsVolumeSlider, // WD EDIT

LobbyMusicCheckBox,
RestartSoundsCheckBox,
Expand Down Expand Up @@ -127,7 +128,7 @@ private void OnApplyButtonPressed(BaseButton.ButtonEventArgs args)
_cfg.SetCVar(CCVars.LobbyMusicVolume, LobbyVolumeSlider.Value / 100f * ContentAudioSystem.LobbyMultiplier);
_cfg.SetCVar(CCVars.InterfaceVolume, InterfaceVolumeSlider.Value / 100f * ContentAudioSystem.InterfaceMultiplier);
_cfg.SetCVar(CCVars.AnnouncerVolume, AnnouncerVolumeSlider.Value / 100f * ContentAudioSystem.AnnouncerMultiplier);
_cfg.SetCVar(WhiteCVars.TtsVolume, TtsVolumeSlider.Value / 100f * ContentAudioSystem.TTSMultiplier); // WD EDIT
_cfg.SetCVar(WhiteCVars.TTSVolume, TtsVolumeSlider.Value / 100f * ContentAudioSystem.TTSMultiplier); // WD EDIT

_cfg.SetCVar(CCVars.MaxAmbientSources, (int)AmbienceSoundsSlider.Value);

Expand All @@ -154,7 +155,7 @@ private void Reset()
LobbyVolumeSlider.Value = _cfg.GetCVar(CCVars.LobbyMusicVolume) * 100f / ContentAudioSystem.LobbyMultiplier;
InterfaceVolumeSlider.Value = _cfg.GetCVar(CCVars.InterfaceVolume) * 100f / ContentAudioSystem.InterfaceMultiplier;
AnnouncerVolumeSlider.Value = _cfg.GetCVar(CCVars.AnnouncerVolume) * 100f / ContentAudioSystem.AnnouncerMultiplier;
TtsVolumeSlider.Value = _cfg.GetCVar(WhiteCVars.TtsVolume) * 100f / ContentAudioSystem.TTSMultiplier; // WD EDIT
TtsVolumeSlider.Value = _cfg.GetCVar(WhiteCVars.TTSVolume) * 100f / ContentAudioSystem.TTSMultiplier; // WD EDIT

AmbienceSoundsSlider.Value = _cfg.GetCVar(CCVars.MaxAmbientSources);

Expand Down Expand Up @@ -183,6 +184,8 @@ private void UpdateChanges()
Math.Abs(InterfaceVolumeSlider.Value - _cfg.GetCVar(CCVars.InterfaceVolume) * 100f / ContentAudioSystem.InterfaceMultiplier) < 0.01f;
var isAnnouncerVolumeSame =
Math.Abs(AnnouncerVolumeSlider.Value - _cfg.GetCVar(CCVars.AnnouncerVolume) * 100f / ContentAudioSystem.AnnouncerMultiplier) < 0.01f;
var isTtsVolumeSame =
Math.Abs(TtsVolumeSlider.Value - _cfg.GetCVar(WhiteCVars.TTSVolume) * 100f / ContentAudioSystem.TTSMultiplier) < 0.01f; // WD EDIT

var isAmbientSoundsSame = (int)AmbienceSoundsSlider.Value == _cfg.GetCVar(CCVars.MaxAmbientSources);
var isLobbySame = LobbyMusicCheckBox.Pressed == _cfg.GetCVar(CCVars.LobbyMusicEnabled);
Expand All @@ -193,7 +196,7 @@ private void UpdateChanges()
var isEverythingSame = isMasterVolumeSame && isMidiVolumeSame && isAmbientVolumeSame
&& isAmbientMusicVolumeSame && isAmbientSoundsSame && isLobbySame && isRestartSoundsSame && isEventSame
&& isAnnouncerDisableMultipleSoundsSame && isAdminSoundsSame && isLobbyVolumeSame
&& isInterfaceVolumeSame && isAnnouncerVolumeSame;
&& isInterfaceVolumeSame && isAnnouncerVolumeSame && isTtsVolumeSame; // WD EDIT
ApplyButton.Disabled = isEverythingSame;
ResetButton.Disabled = isEverythingSame;
MasterVolumeLabel.Text =
Expand All @@ -210,6 +213,8 @@ private void UpdateChanges()
Loc.GetString("ui-options-volume-percent", ("volume", InterfaceVolumeSlider.Value / 100));
AnnouncerVolumeLabel.Text =
Loc.GetString("ui-options-volume-percent", ("volume", AnnouncerVolumeSlider.Value / 100));
TtsVolumeLabel.Text =
Loc.GetString("ui-options-volume-percent", ("volume", TtsVolumeSlider.Value / 100)); // WD EDIT
AmbienceSoundsLabel.Text = ((int)AmbienceSoundsSlider.Value).ToString();
}
}
Expand Down
4 changes: 2 additions & 2 deletions Content.Client/_White/TTS/TTSSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ public sealed class TTSSystem : EntitySystem

public override void Initialize()
{
_cfg.OnValueChanged(WhiteCVars.TtsVolume, OnTtsVolumeChanged, true);
_cfg.OnValueChanged(WhiteCVars.TTSVolume, OnTtsVolumeChanged, true);

SubscribeNetworkEvent<PlayTTSEvent>(OnPlayTTS);
}

public override void Shutdown()
{
base.Shutdown();
_cfg.UnsubValueChanged(WhiteCVars.TtsVolume, OnTtsVolumeChanged);
_cfg.UnsubValueChanged(WhiteCVars.TTSVolume, OnTtsVolumeChanged);
ClearQueues();
}

Expand Down
37 changes: 15 additions & 22 deletions Content.Server/_White/TTS/TTSManager.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Linq;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading;
Expand Down Expand Up @@ -51,14 +52,21 @@ public void Initialize()
/// <returns>OGG audio bytes</returns>
public async Task<byte[]?> ConvertTextToSpeech(string speaker, string text, string pitch, string rate, string? effect = null)
{
var url = _cfg.GetCVar(WhiteCVars.TtsApiUrl);
var url = _cfg.GetCVar(WhiteCVars.TTSApiUrl);
if (string.IsNullOrWhiteSpace(url))
{
_sawmill.Log(LogLevel.Error, nameof(TTSManager), "TTS Api url not specified");
return null;
}

var maxCacheSize = _cfg.GetCVar(WhiteCVars.TtsMaxCacheSize);
var token = _cfg.GetCVar(WhiteCVars.TTSApiToken);
if (string.IsNullOrWhiteSpace(token))
{
_sawmill.Log(LogLevel.Error, nameof(TTSManager), "TTS Api token not specified");
return null;
}

var maxCacheSize = _cfg.GetCVar(WhiteCVars.TTSMaxCache);
WantedCount.Inc();
var cacheKey = GenerateCacheKey(speaker, text);
if (_cache.TryGetValue(cacheKey, out var data))
Expand All @@ -70,20 +78,19 @@ public void Initialize()

var body = new GenerateVoiceRequest
{
ApiToken = token,
Text = text,
Speaker = speaker,
Pitch = pitch,
Rate = rate,
Effect = effect
};

var request = CreateRequestLink(url, body);

var reqTime = DateTime.UtcNow;
try
{
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var response = await _httpClient.GetAsync(request, cts.Token);
var response = await _httpClient.PostAsJsonAsync(url, body, cts.Token);
if (!response.IsSuccessStatusCode)
throw new Exception($"TTS request returned bad status code: {response.StatusCode}");

Expand Down Expand Up @@ -116,23 +123,6 @@ public void Initialize()
}
}

private static string CreateRequestLink(string url, GenerateVoiceRequest body)
{
var uriBuilder = new UriBuilder(url);
var query = HttpUtility.ParseQueryString(uriBuilder.Query);
query["speaker"] = body.Speaker;
query["text"] = body.Text;
query["pitch"] = body.Pitch;
query["rate"] = body.Rate;
query["file"] = "1";
query["ext"] = "ogg";
if (body.Effect != null)
query["effect"] = body.Effect;

uriBuilder.Query = query.ToString();
return uriBuilder.ToString();
}

public void ResetCache()
{
_cache.Clear();
Expand All @@ -149,6 +139,9 @@ private string GenerateCacheKey(string speaker, string text)

private record GenerateVoiceRequest
{
[JsonPropertyName("api_token")]
public string ApiToken { get; set; } = "";

[JsonPropertyName("text")]
public string Text { get; set; } = default!;

Expand Down
6 changes: 3 additions & 3 deletions Content.Server/_White/TTS/TTSSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public sealed partial class TTSSystem : EntitySystem

public override void Initialize()
{
_cfg.OnValueChanged(WhiteCVars.TtsEnabled, v => _isEnabled = v, true);
_cfg.OnValueChanged(WhiteCVars.TtsApiUrl, url => _apiUrl = url, true);
_cfg.OnValueChanged(WhiteCVars.TTSEnabled, v => _isEnabled = v, true);
_cfg.OnValueChanged(WhiteCVars.TTSApiUrl, url => _apiUrl = url, true);

SubscribeLocalEvent<TTSComponent, EntitySpokeEvent>(OnEntitySpoke);

Expand Down Expand Up @@ -171,7 +171,7 @@ private void OnRoundRestartCleanup(RoundRestartCleanupEvent ev)

private async void OnRequestTTS(MsgRequestTTS ev)
{
var url = _cfg.GetCVar(WhiteCVars.TtsApiUrl);
var url = _cfg.GetCVar(WhiteCVars.TTSApiUrl);
if (string.IsNullOrWhiteSpace(url))
return;

Expand Down
26 changes: 22 additions & 4 deletions Content.Shared/_White/CVars.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,41 @@ public static readonly CVarDef<string>
/// <summary>
/// if the TTS system enabled or not.
/// </summary>
public static readonly CVarDef<bool> TtsEnabled = CVarDef.Create("tts.enabled", true, CVar.SERVERONLY);
// ReSharper disable once InconsistentNaming
public static readonly CVarDef<bool> TTSEnabled = CVarDef.Create("tts.enabled", true, CVar.SERVERONLY);

/// <summary>
/// URL of the TTS server API.
/// </summary>
public static readonly CVarDef<string> TtsApiUrl = CVarDef.Create("tts.api_url", "", CVar.SERVERONLY);
// ReSharper disable once InconsistentNaming
public static readonly CVarDef<string> TTSApiUrl = CVarDef.Create("tts.api_url", "", CVar.SERVERONLY);

/// <summary>
/// Auth token of the TTS server API.
/// </summary>
// ReSharper disable once InconsistentNaming
public static readonly CVarDef<string> TTSApiToken =
CVarDef.Create("tts.api_token", "", CVar.SERVERONLY | CVar.CONFIDENTIAL);

/// <summary>
/// The volume of TTS playback.
/// </summary>
public static readonly CVarDef<float> TtsVolume = CVarDef.Create("tts.volume", 0f, CVar.CLIENTONLY | CVar.ARCHIVE);
// ReSharper disable once InconsistentNaming
public static readonly CVarDef<float> TTSVolume = CVarDef.Create("tts.volume", 0f, CVar.CLIENTONLY | CVar.ARCHIVE);

/// <summary>
/// TTS Cache.
/// </summary>
public static readonly CVarDef<int> TtsMaxCacheSize =
// ReSharper disable once InconsistentNaming
public static readonly CVarDef<int> TTSMaxCache =
CVarDef.Create("tts.max_cash_size", 200, CVar.SERVERONLY | CVar.ARCHIVE);

/// <summary>
/// Amount of seconds before timeout for API
/// </summary>
// ReSharper disable once InconsistentNaming
public static readonly CVarDef<int> TTSApiTimeout =
CVarDef.Create("tts.api_timeout", 5, CVar.SERVERONLY | CVar.ARCHIVE);

#endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ tts-voice-name-asa = Ася
tts-voice-name-valentain = Валентин
tts-voice-name-chelsea = Челси
tts-voice-name-anna = Анна
tts-voice-name-jeanne = Джон
tts-voice-name-jeanne = Жанна
tts-voice-name-jasmin = Жасмин
tts-voice-name-juelette = Джульета
tts-voice-name-arina = Арина
Expand Down
Loading