Skip to content

Commit

Permalink
New TTS
Browse files Browse the repository at this point in the history
  • Loading branch information
KashRas committed Aug 14, 2024
1 parent 814eb78 commit f99e147
Show file tree
Hide file tree
Showing 18 changed files with 583 additions and 4,859 deletions.
1 change: 0 additions & 1 deletion Content.Client/Credits/CreditsWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ void AddSection(string title, string path, bool markup = false)
AddSection(Loc.GetString("credits-window-contributors-section-title"), "GitHub.txt");
AddSection(Loc.GetString("credits-window-devADT-section-title"), "ADTeam.txt"); // ADT team
AddSection(Loc.GetString("credits-window-codebases-section-title"), "SpaceStation13.txt");
AddSection(Loc.GetString("credits-window-tts-title"), "TTS.txt"); // Corvax-TTS
AddSection(Loc.GetString("credits-window-original-remake-team-section-title"), "OriginalRemake.txt");
AddSection(Loc.GetString("credits-window-special-thanks-section-title"), "SpecialThanks.txt", true);

Expand Down
75 changes: 11 additions & 64 deletions Content.Server/Corvax/TTS/TTSManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using Content.Shared.Corvax.CCCVars;
using Prometheus;
using Robust.Shared.Configuration;
Expand All @@ -18,9 +19,9 @@ public sealed class TTSManager
private static readonly Histogram RequestTimings = Metrics.CreateHistogram(
"tts_req_timings",
"Timings of TTS API requests",
new HistogramConfiguration()
new HistogramConfiguration
{
LabelNames = new[] {"type"},
LabelNames = ["type"],
Buckets = Histogram.ExponentialBuckets(.1, 1.5, 10),
});

Expand All @@ -41,7 +42,6 @@ public sealed class TTSManager
private readonly List<string> _cacheKeysSeq = new();
private int _maxCachedCount = 200;
private string _apiUrl = string.Empty;
private string _apiToken = string.Empty;

public void Initialize()
{
Expand All @@ -52,7 +52,11 @@ public void Initialize()
ResetCache();
}, true);
_cfg.OnValueChanged(CCCVars.TTSApiUrl, v => _apiUrl = v, true);
_cfg.OnValueChanged(CCCVars.TTSApiToken, v => _apiToken = v, true);
_cfg.OnValueChanged(CCCVars.TTSApiToken,
v =>
{
_httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", v);
}, true);
}

/// <summary>
Expand All @@ -74,19 +78,12 @@ public void Initialize()

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

var body = new GenerateVoiceRequest
{
ApiToken = _apiToken,
Text = text,
Speaker = speaker,
};

var reqTime = DateTime.UtcNow;
try
{
var timeout = _cfg.GetCVar(CCCVars.TTSApiTimeout);
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(timeout));
var response = await _httpClient.PostAsJsonAsync(_apiUrl, body, cts.Token);
var response = await _httpClient.GetAsync($"{_apiUrl}?speaker={speaker}&text={HttpUtility.UrlEncode(text)}&ext=ogg", cts.Token);
if (!response.IsSuccessStatusCode)
{
if (response.StatusCode == HttpStatusCode.TooManyRequests)
Expand All @@ -99,8 +96,7 @@ public void Initialize()
return null;
}

var json = await response.Content.ReadFromJsonAsync<GenerateVoiceResponse>(cancellationToken: cts.Token);
var soundData = Convert.FromBase64String(json.Results.First().Audio);
var soundData = await response.Content.ReadAsByteArrayAsync(cancellationToken: cts.Token);

_cache.Add(cacheKey, soundData);
_cacheKeysSeq.Add(cacheKey);
Expand Down Expand Up @@ -139,58 +135,9 @@ public void ResetCache()
private string GenerateCacheKey(string speaker, string text)
{
var key = $"{speaker}/{text}";
byte[] keyData = Encoding.UTF8.GetBytes(key);
var keyData = Encoding.UTF8.GetBytes(key);
var sha256 = System.Security.Cryptography.SHA256.Create();
var bytes = sha256.ComputeHash(keyData);
return Convert.ToHexString(bytes);
}

private struct GenerateVoiceRequest
{
public GenerateVoiceRequest()
{
}

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

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

[JsonPropertyName("speaker")]
public string Speaker { get; set; } = "";

[JsonPropertyName("ssml")]
public bool SSML { get; private set; } = true;

[JsonPropertyName("word_ts")]
public bool WordTS { get; private set; } = false;

[JsonPropertyName("put_accent")]
public bool PutAccent { get; private set; } = true;

[JsonPropertyName("put_yo")]
public bool PutYo { get; private set; } = false;

[JsonPropertyName("sample_rate")]
public int SampleRate { get; private set; } = 24000;

[JsonPropertyName("format")]
public string Format { get; private set; } = "ogg";
}

private struct GenerateVoiceResponse
{
[JsonPropertyName("results")]
public List<VoiceResult> Results { get; set; }

[JsonPropertyName("original_sha1")]
public string Hash { get; set; }
}

private struct VoiceResult
{
[JsonPropertyName("audio")]
public string Audio { get; set; }
}
}
23 changes: 0 additions & 23 deletions Content.Server/Corvax/TTS/TTSSystem.SSML.cs

This file was deleted.

19 changes: 4 additions & 15 deletions Content.Server/Corvax/TTS/TTSSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,23 +153,12 @@ private async void HandleWhisper(EntityUid uid, string message, string obfMessag
if (char.IsLetter(textSanitized[^1]))
textSanitized += ".";

var ssmlTraits = SoundTraits.RateFast;
if (isWhisper)
ssmlTraits = SoundTraits.PitchVerylow;
var textSsml = ToSsmlText(textSanitized, ssmlTraits);

return await _ttsManager.ConvertTextToSpeech(speaker, textSsml);
return await _ttsManager.ConvertTextToSpeech(speaker, textSanitized);
}
}

public sealed class TransformSpeakerVoiceEvent : EntityEventArgs
public sealed class TransformSpeakerVoiceEvent(EntityUid sender, string voiceId) : EntityEventArgs
{
public EntityUid Sender;
public string VoiceId;

public TransformSpeakerVoiceEvent(EntityUid sender, string voiceId)
{
Sender = sender;
VoiceId = voiceId;
}
public EntityUid Sender = sender;
public string VoiceId = voiceId;
}
9 changes: 4 additions & 5 deletions Content.Shared/Humanoid/SharedHumanoidAppearanceSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Content.Shared.Humanoid.Markings;
using Content.Shared.Humanoid.Prototypes;
using Content.Shared.Corvax.TTS;
using Content.Shared.Decals;
using Content.Shared.IdentityManagement;
using Content.Shared.Preferences;
using Robust.Shared;
Expand Down Expand Up @@ -42,12 +41,12 @@ public abstract class SharedHumanoidAppearanceSystem : EntitySystem
[ValidatePrototypeId<SpeciesPrototype>]
public const string DefaultSpecies = "Human";
// Corvax-TTS-Start
public const string DefaultVoice = "Aidar";
public const string DefaultVoice = "johnny";
public static readonly Dictionary<Sex, string> DefaultSexVoice = new()
{
{Sex.Male, "Aidar"},
{Sex.Female, "Kseniya"},
{Sex.Unsexed, "Baya"},
{Sex.Male, "johnny"},
{Sex.Female, "v_female"},
{Sex.Unsexed, "serana"},
};
// Corvax-TTS-End

Expand Down
1 change: 0 additions & 1 deletion Resources/Credits/TTS.txt

This file was deleted.

Loading

0 comments on commit f99e147

Please sign in to comment.