mirror of
https://github.com/WWhiteDreamProject/wwdpublic.git
synced 2026-04-25 09:38:09 +03:00
* Revert "[Fix] TTS (#137)" This reverts commitc5bd6b70a2. * Revert "[Fix] Исправление ТТСа (#136)" This reverts commit3759acb84e. * Revert "[Port] TTS (#121)" This reverts commit0db8f3aaa4. * new TTS * new TTS * new TTS * new TTS * fix
53 lines
1.7 KiB
C#
53 lines
1.7 KiB
C#
using System.Threading.Tasks;
|
|
using Content.Server._White.Chat.Systems;
|
|
using Content.Shared._White.TTS;
|
|
|
|
namespace Content.Server._White.TTS;
|
|
|
|
// ReSharper disable once InconsistentNaming
|
|
public sealed partial class TTSSystem
|
|
{
|
|
private string _voiceId = "Announcer";
|
|
|
|
private async void OnAnnouncementSpoke(AnnouncementSpokeEvent args)
|
|
{
|
|
if (!_isEnabled ||
|
|
args.Message.Length > MaxMessageChars * 2 ||
|
|
!_prototypeManager.TryIndex<TTSVoicePrototype>(_voiceId, out var protoVoice))
|
|
{
|
|
RaiseNetworkEvent(new AnnounceTTSEvent(new byte[]{}, args.AnnouncementSound, args.AnnouncementSoundParams), args.Source);
|
|
return;
|
|
}
|
|
|
|
byte[]? soundData = null;
|
|
try
|
|
{
|
|
soundData = await GenerateTtsAnnouncement(args.Message, protoVoice.Speaker);
|
|
|
|
}
|
|
catch (Exception)
|
|
{
|
|
// skip!
|
|
}
|
|
soundData ??= new byte[] { };
|
|
RaiseNetworkEvent(new AnnounceTTSEvent(soundData, args.AnnouncementSound, args.AnnouncementSoundParams), args.Source);
|
|
}
|
|
|
|
private async Task<byte[]?> GenerateTtsAnnouncement(string text, string speaker)
|
|
{
|
|
var textSanitized = Sanitize(text);
|
|
if (textSanitized == "") return null;
|
|
if (char.IsLetter(textSanitized[^1]))
|
|
textSanitized += ".";
|
|
|
|
var textSsml = ToSsmlText(textSanitized, SoundTraits.RateFast);
|
|
|
|
var position = textSsml.LastIndexOf("Отправитель", StringComparison.InvariantCulture);
|
|
if (position != -1)
|
|
{
|
|
textSsml = textSsml[..position] + "<break time='33ms'/>" + textSsml[position..];
|
|
}
|
|
return await _ttsManager.AnnounceConvertTextToSpeech(speaker, textSsml);
|
|
}
|
|
}
|