Added hermes websocket support. Added chat command support. Added selectable voice command via websocket. Added websocket heartbeat management.

This commit is contained in:
Tom
2024-03-15 12:27:35 +00:00
parent b5cc6b5706
commit d4004d6230
53 changed files with 1227 additions and 461 deletions

210
TTS.cs
View File

@@ -2,11 +2,12 @@ using System.Runtime.InteropServices;
using System.Web;
using CommonSocketLibrary.Abstract;
using CommonSocketLibrary.Common;
using HermesSocketLibrary.Socket.Data;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NAudio.Wave;
using NAudio.Wave.SampleProviders;
using TwitchChatTTS.Hermes.Socket;
using TwitchLib.Client.Events;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
@@ -15,32 +16,54 @@ namespace TwitchChatTTS
{
public class TTS : IHostedService
{
private ILogger Logger { get; }
private Configuration Configuration { get; }
private TTSPlayer Player { get; }
private IServiceProvider ServiceProvider { get; }
private ISampleProvider? Playing { get; set; }
private readonly ILogger _logger;
private readonly Configuration _configuration;
private readonly TTSPlayer _player;
private readonly IServiceProvider _serviceProvider;
public TTS(ILogger<TTS> logger, Configuration configuration, TTSPlayer player, IServiceProvider serviceProvider) {
Logger = logger;
Configuration = configuration;
Player = player;
ServiceProvider = serviceProvider;
_logger = logger;
_configuration = configuration;
_player = player;
_serviceProvider = serviceProvider;
}
public async Task StartAsync(CancellationToken cancellationToken) {
Console.Title = "TTS - Twitch Chat";
var user = _serviceProvider.GetRequiredService<User>();
var hermes = await InitializeHermes();
var hermesAccount = await hermes.FetchHermesAccountDetails();
user.HermesUserId = hermesAccount.Id;
user.TwitchUsername = hermesAccount.Username;
var twitchBotToken = await hermes.FetchTwitchBotToken();
user.TwitchUserId = long.Parse(twitchBotToken.BroadcasterId);
_logger.LogInformation($"Username: {user.TwitchUsername} (id: {user.TwitchUserId})");
user.DefaultTTSVoice = await hermes.FetchTTSDefaultVoice();
_logger.LogInformation("Default Voice: " + user.DefaultTTSVoice);
var wordFilters = await hermes.FetchTTSWordFilters();
user.RegexFilters = wordFilters.ToList();
_logger.LogInformation($"{user.RegexFilters.Count()} TTS word filters.");
var usernameFilters = await hermes.FetchTTSUsernameFilters();
user.ChatterFilters = usernameFilters.ToDictionary(e => e.Username, e => e);
_logger.LogInformation($"{user.ChatterFilters.Where(f => f.Value.Tag == "blacklisted").Count()} username(s) have been blocked.");
_logger.LogInformation($"{user.ChatterFilters.Where(f => f.Value.Tag == "priority").Count()} user(s) have been prioritized.");
var twitchapiclient = await InitializeTwitchApiClient(user.TwitchUsername);
await InitializeHermesWebsocket(user);
await InitializeSevenTv();
await InitializeObs();
try {
var hermes = await InitializeHermes();
var twitchapiclient = await InitializeTwitchApiClient(hermes);
AudioPlaybackEngine.Instance.AddOnMixerInputEnded((object? s, SampleProviderEventArgs e) => {
if (e.SampleProvider == Playing) {
Playing = null;
if (e.SampleProvider == _player.Playing) {
_player.Playing = null;
}
});
@@ -48,11 +71,11 @@ namespace TwitchChatTTS
while (true) {
try {
if (cancellationToken.IsCancellationRequested) {
Logger.LogWarning("TTS Buffer - Cancellation token was canceled.");
_logger.LogWarning("TTS Buffer - Cancellation token was canceled.");
return;
}
var m = Player.ReceiveBuffer();
var m = _player.ReceiveBuffer();
if (m == null) {
await Task.Delay(200);
continue;
@@ -63,14 +86,14 @@ namespace TwitchChatTTS
var provider = new CachedWavProvider(sound);
var data = AudioPlaybackEngine.Instance.ConvertSound(provider);
var resampled = new WdlResamplingSampleProvider(data, AudioPlaybackEngine.Instance.SampleRate);
Logger.LogDebug("Fetched TTS audio data.");
_logger.LogDebug("Fetched TTS audio data.");
m.Audio = resampled;
Player.Ready(m);
_player.Ready(m);
} catch (COMException e) {
Logger.LogError(e, "Failed to send request for TTS (HResult: " + e.HResult + ").");
_logger.LogError(e, "Failed to send request for TTS (HResult: " + e.HResult + ").");
} catch (Exception e) {
Logger.LogError(e, "Failed to send request for TTS.");
_logger.LogError(e, "Failed to send request for TTS.");
}
}
});
@@ -79,40 +102,40 @@ namespace TwitchChatTTS
while (true) {
try {
if (cancellationToken.IsCancellationRequested) {
Logger.LogWarning("TTS Queue - Cancellation token was canceled.");
_logger.LogWarning("TTS Queue - Cancellation token was canceled.");
return;
}
while (Player.IsEmpty() || Playing != null) {
while (_player.IsEmpty() || _player.Playing != null) {
await Task.Delay(200);
continue;
}
var m = Player.ReceiveReady();
var m = _player.ReceiveReady();
if (m == null) {
continue;
}
if (!string.IsNullOrWhiteSpace(m.File) && File.Exists(m.File)) {
Logger.LogInformation("Playing message: " + m.File);
_logger.LogInformation("Playing message: " + m.File);
AudioPlaybackEngine.Instance.PlaySound(m.File);
continue;
}
Logger.LogInformation("Playing message: " + m.Message);
Playing = m.Audio;
_logger.LogInformation("Playing message: " + m.Message);
_player.Playing = m.Audio;
if (m.Audio != null)
AudioPlaybackEngine.Instance.AddMixerInput(m.Audio);
} catch (Exception e) {
Logger.LogError(e, "Failed to play a TTS audio message");
_logger.LogError(e, "Failed to play a TTS audio message");
}
}
});
StartSavingEmoteCounter();
Logger.LogInformation("Twitch API client connecting...");
_logger.LogInformation("Twitch API client connecting...");
await twitchapiclient.Connect();
} catch (Exception e) {
Logger.LogError(e, "Failed to initialize.");
_logger.LogError(e, "Failed to initialize.");
}
Console.ReadLine();
}
@@ -120,75 +143,100 @@ namespace TwitchChatTTS
public async Task StopAsync(CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
Logger.LogWarning("Application has stopped due to cancellation token.");
_logger.LogWarning("Application has stopped due to cancellation token.");
else
Logger.LogWarning("Application has stopped.");
_logger.LogWarning("Application has stopped.");
}
private async Task InitializeHermesWebsocket(User user) {
if (_configuration.Hermes?.Token == null) {
_logger.LogDebug("No api token given to hermes. Skipping hermes websockets.");
return;
}
try {
_logger.LogInformation("Initializing hermes websocket client.");
var hermesClient = _serviceProvider.GetRequiredKeyedService<SocketClient<WebSocketMessage>>("hermes") as HermesSocketClient;
var url = "wss://hermes-ws.goblincaves.com";
_logger.LogDebug($"Attempting to connect to {url}");
await hermesClient.ConnectAsync(url);
await hermesClient.Send(1, new HermesLoginMessage() {
ApiKey = _configuration.Hermes.Token
});
while (hermesClient.UserId == null)
await Task.Delay(TimeSpan.FromMilliseconds(200));
await hermesClient.Send(3, new RequestMessage() {
Type = "get_tts_voices",
Data = null
});
var token = _serviceProvider.GetRequiredService<TwitchBotToken>();
await hermesClient.Send(3, new RequestMessage() {
Type = "get_tts_users",
Data = new Dictionary<string, string>() { { "@broadcaster", token.BroadcasterId } }
});
} catch (Exception) {
_logger.LogWarning("Connecting to hermes failed. Skipping hermes websockets.");
}
}
private async Task InitializeSevenTv() {
Logger.LogInformation("Initializing 7tv client.");
var sevenClient = ServiceProvider.GetRequiredKeyedService<SocketClient<WebSocketMessage>>("7tv");
if (Configuration.Seven is not null && !string.IsNullOrWhiteSpace(Configuration.Seven.Url)) {
var base_url = "@" + string.Join(",", Configuration.Seven.InitialSubscriptions.Select(sub => sub.Type + "<" + string.Join(",", sub.Condition?.Select(e => e.Key + "=" + e.Value) ?? new string[0]) + ">"));
Logger.LogDebug($"Attempting to connect to {Configuration.Seven.Protocol?.Trim() ?? "wss"}://{Configuration.Seven.Url.Trim()}{base_url}");
await sevenClient.ConnectAsync($"{Configuration.Seven.Protocol?.Trim() ?? "wss"}://{Configuration.Seven.Url.Trim()}{base_url}");
if (_configuration.Seven?.UserId == null) {
_logger.LogDebug("No user id given to 7tv. Skipping 7tv websockets.");
return;
}
try {
_logger.LogInformation("Initializing 7tv websocket client.");
var sevenClient = _serviceProvider.GetRequiredKeyedService<SocketClient<WebSocketMessage>>("7tv");
//var base_url = "@" + string.Join(",", Configuration.Seven.InitialSubscriptions.Select(sub => sub.Type + "<" + string.Join(",", sub.Condition?.Select(e => e.Key + "=" + e.Value) ?? new string[0]) + ">"));
var url = $"{SevenApiClient.WEBSOCKET_URL}@emote_set.*<object_id={_configuration.Seven.UserId.Trim()}>";
_logger.LogDebug($"Attempting to connect to {url}");
await sevenClient.ConnectAsync($"{url}");
} catch (Exception) {
_logger.LogWarning("Connecting to 7tv failed. Skipping 7tv websockets.");
}
}
private async Task InitializeObs() {
Logger.LogInformation("Initializing obs client.");
var obsClient = ServiceProvider.GetRequiredKeyedService<SocketClient<WebSocketMessage>>("obs");
if (Configuration.Obs is not null && !string.IsNullOrWhiteSpace(Configuration.Obs.Host) && Configuration.Obs.Port.HasValue && Configuration.Obs.Port.Value >= 0) {
Logger.LogDebug($"Attempting to connect to ws://{Configuration.Obs.Host.Trim()}:{Configuration.Obs.Port}");
await obsClient.ConnectAsync($"ws://{Configuration.Obs.Host.Trim()}:{Configuration.Obs.Port}");
await Task.Delay(500);
if (_configuration.Obs == null || string.IsNullOrWhiteSpace(_configuration.Obs.Host) || !_configuration.Obs.Port.HasValue || _configuration.Obs.Port.Value < 0) {
_logger.LogDebug("Lacking obs connection info. Skipping obs websockets.");
return;
}
try {
_logger.LogInformation("Initializing obs websocket client.");
var obsClient = _serviceProvider.GetRequiredKeyedService<SocketClient<WebSocketMessage>>("obs");
var url = $"ws://{_configuration.Obs.Host.Trim()}:{_configuration.Obs.Port}";
_logger.LogDebug($"Attempting to connect to {url}");
await obsClient.ConnectAsync(url);
} catch (Exception) {
_logger.LogWarning("Connecting to obs failed. Skipping obs websockets.");
}
}
private async Task<HermesClient> InitializeHermes() {
// Fetch id and username based on api key given.
Logger.LogInformation("Initializing hermes client.");
var hermes = ServiceProvider.GetRequiredService<HermesClient>();
_logger.LogInformation("Initializing hermes client.");
var hermes = _serviceProvider.GetRequiredService<HermesClient>();
await hermes.FetchHermesAccountDetails();
if (hermes.Username == null)
throw new Exception("Username fetched from Hermes is invalid.");
Logger.LogInformation("Username: " + hermes.Username);
return hermes;
}
private async Task<TwitchApiClient> InitializeTwitchApiClient(HermesClient hermes) {
Logger.LogInformation("Initializing twitch client.");
var twitchapiclient = ServiceProvider.GetRequiredService<TwitchApiClient>();
private async Task<TwitchApiClient> InitializeTwitchApiClient(string username) {
_logger.LogInformation("Initializing twitch client.");
var twitchapiclient = _serviceProvider.GetRequiredService<TwitchApiClient>();
await twitchapiclient.Authorize();
var channels = Configuration.Twitch?.Channels ?? [hermes.Username];
Logger.LogInformation("Twitch channels: " + string.Join(", ", channels));
twitchapiclient.InitializeClient(hermes, channels);
var channels = _configuration.Twitch.Channels ?? [username];
_logger.LogInformation("Twitch channels: " + string.Join(", ", channels));
twitchapiclient.InitializeClient(username, channels);
twitchapiclient.InitializePublisher();
var handler = ServiceProvider.GetRequiredService<ChatMessageHandler>();
var handler = _serviceProvider.GetRequiredService<ChatMessageHandler>();
twitchapiclient.AddOnNewMessageReceived(async Task (object? s, OnMessageReceivedArgs e) => {
var result = handler.Handle(e);
switch (result) {
case MessageResult.Skip:
if (Playing != null) {
AudioPlaybackEngine.Instance.RemoveMixerInput(Playing);
Playing = null;
}
break;
case MessageResult.SkipAll:
Player.RemoveAll();
if (Playing != null) {
AudioPlaybackEngine.Instance.RemoveMixerInput(Playing);
Playing = null;
}
break;
default:
break;
}
var result = await handler.Handle(e);
});
return twitchapiclient;
@@ -204,13 +252,13 @@ namespace TwitchChatTTS
.WithNamingConvention(HyphenatedNamingConvention.Instance)
.Build();
var chathandler = ServiceProvider.GetRequiredService<ChatMessageHandler>();
using (TextWriter writer = File.CreateText(Configuration.Emotes.CounterFilePath.Trim()))
var chathandler = _serviceProvider.GetRequiredService<ChatMessageHandler>();
using (TextWriter writer = File.CreateText(_configuration.Emotes.CounterFilePath.Trim()))
{
await writer.WriteAsync(serializer.Serialize(chathandler.EmoteCounter));
await writer.WriteAsync(serializer.Serialize(chathandler._emoteCounter));
}
} catch (Exception e) {
Logger.LogError(e, "Failed to save the emote counter.");
_logger.LogError(e, "Failed to save the emote counter.");
}
}
});