2024-03-12 14:05:27 -04:00
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
using System.Web;
|
|
|
|
using CommonSocketLibrary.Abstract;
|
|
|
|
using CommonSocketLibrary.Common;
|
2024-03-15 08:27:35 -04:00
|
|
|
using HermesSocketLibrary.Socket.Data;
|
2024-03-12 14:05:27 -04:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using NAudio.Wave.SampleProviders;
|
2024-03-15 08:27:35 -04:00
|
|
|
using TwitchChatTTS.Hermes.Socket;
|
2024-03-12 14:05:27 -04:00
|
|
|
using TwitchLib.Client.Events;
|
|
|
|
using YamlDotNet.Serialization;
|
|
|
|
using YamlDotNet.Serialization.NamingConventions;
|
|
|
|
|
|
|
|
namespace TwitchChatTTS
|
|
|
|
{
|
|
|
|
public class TTS : IHostedService
|
|
|
|
{
|
2024-03-15 08:27:35 -04:00
|
|
|
private readonly ILogger _logger;
|
|
|
|
private readonly Configuration _configuration;
|
|
|
|
private readonly TTSPlayer _player;
|
|
|
|
private readonly IServiceProvider _serviceProvider;
|
2024-03-12 14:05:27 -04:00
|
|
|
|
|
|
|
public TTS(ILogger<TTS> logger, Configuration configuration, TTSPlayer player, IServiceProvider serviceProvider) {
|
2024-03-15 08:27:35 -04:00
|
|
|
_logger = logger;
|
|
|
|
_configuration = configuration;
|
|
|
|
_player = player;
|
|
|
|
_serviceProvider = serviceProvider;
|
2024-03-12 14:05:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task StartAsync(CancellationToken cancellationToken) {
|
|
|
|
Console.Title = "TTS - Twitch Chat";
|
|
|
|
|
2024-03-15 08:27:35 -04:00
|
|
|
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);
|
2024-03-12 14:05:27 -04:00
|
|
|
await InitializeSevenTv();
|
|
|
|
await InitializeObs();
|
|
|
|
|
|
|
|
try {
|
|
|
|
AudioPlaybackEngine.Instance.AddOnMixerInputEnded((object? s, SampleProviderEventArgs e) => {
|
2024-03-15 08:27:35 -04:00
|
|
|
if (e.SampleProvider == _player.Playing) {
|
|
|
|
_player.Playing = null;
|
2024-03-12 14:05:27 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Task.Run(async () => {
|
|
|
|
while (true) {
|
|
|
|
try {
|
|
|
|
if (cancellationToken.IsCancellationRequested) {
|
2024-03-15 08:27:35 -04:00
|
|
|
_logger.LogWarning("TTS Buffer - Cancellation token was canceled.");
|
2024-03-12 14:05:27 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-03-15 08:27:35 -04:00
|
|
|
var m = _player.ReceiveBuffer();
|
2024-03-12 14:05:27 -04:00
|
|
|
if (m == null) {
|
|
|
|
await Task.Delay(200);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
string url = $"https://api.streamelements.com/kappa/v2/speech?voice={m.Voice}&text={HttpUtility.UrlEncode(m.Message)}";
|
|
|
|
var sound = new NetworkWavSound(url);
|
|
|
|
var provider = new CachedWavProvider(sound);
|
|
|
|
var data = AudioPlaybackEngine.Instance.ConvertSound(provider);
|
|
|
|
var resampled = new WdlResamplingSampleProvider(data, AudioPlaybackEngine.Instance.SampleRate);
|
2024-03-15 08:27:35 -04:00
|
|
|
_logger.LogDebug("Fetched TTS audio data.");
|
2024-03-12 14:05:27 -04:00
|
|
|
|
|
|
|
m.Audio = resampled;
|
2024-03-15 08:27:35 -04:00
|
|
|
_player.Ready(m);
|
2024-03-12 14:05:27 -04:00
|
|
|
} catch (COMException e) {
|
2024-03-15 08:27:35 -04:00
|
|
|
_logger.LogError(e, "Failed to send request for TTS (HResult: " + e.HResult + ").");
|
2024-03-12 14:05:27 -04:00
|
|
|
} catch (Exception e) {
|
2024-03-15 08:27:35 -04:00
|
|
|
_logger.LogError(e, "Failed to send request for TTS.");
|
2024-03-12 14:05:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Task.Run(async () => {
|
|
|
|
while (true) {
|
|
|
|
try {
|
|
|
|
if (cancellationToken.IsCancellationRequested) {
|
2024-03-15 08:27:35 -04:00
|
|
|
_logger.LogWarning("TTS Queue - Cancellation token was canceled.");
|
2024-03-12 14:05:27 -04:00
|
|
|
return;
|
|
|
|
}
|
2024-03-15 08:27:35 -04:00
|
|
|
while (_player.IsEmpty() || _player.Playing != null) {
|
2024-03-12 14:05:27 -04:00
|
|
|
await Task.Delay(200);
|
|
|
|
continue;
|
|
|
|
}
|
2024-03-15 08:27:35 -04:00
|
|
|
var m = _player.ReceiveReady();
|
2024-03-12 14:05:27 -04:00
|
|
|
if (m == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(m.File) && File.Exists(m.File)) {
|
2024-03-15 08:27:35 -04:00
|
|
|
_logger.LogInformation("Playing message: " + m.File);
|
2024-03-12 14:05:27 -04:00
|
|
|
AudioPlaybackEngine.Instance.PlaySound(m.File);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-03-15 08:27:35 -04:00
|
|
|
_logger.LogInformation("Playing message: " + m.Message);
|
|
|
|
_player.Playing = m.Audio;
|
2024-03-12 14:05:27 -04:00
|
|
|
if (m.Audio != null)
|
|
|
|
AudioPlaybackEngine.Instance.AddMixerInput(m.Audio);
|
|
|
|
} catch (Exception e) {
|
2024-03-15 08:27:35 -04:00
|
|
|
_logger.LogError(e, "Failed to play a TTS audio message");
|
2024-03-12 14:05:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
StartSavingEmoteCounter();
|
|
|
|
|
2024-03-15 08:27:35 -04:00
|
|
|
_logger.LogInformation("Twitch API client connecting...");
|
2024-03-12 14:05:27 -04:00
|
|
|
await twitchapiclient.Connect();
|
|
|
|
} catch (Exception e) {
|
2024-03-15 08:27:35 -04:00
|
|
|
_logger.LogError(e, "Failed to initialize.");
|
2024-03-12 14:05:27 -04:00
|
|
|
}
|
|
|
|
Console.ReadLine();
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task StopAsync(CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
if (cancellationToken.IsCancellationRequested)
|
2024-03-15 08:27:35 -04:00
|
|
|
_logger.LogWarning("Application has stopped due to cancellation token.");
|
2024-03-12 14:05:27 -04:00
|
|
|
else
|
2024-03-15 08:27:35 -04:00
|
|
|
_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.");
|
|
|
|
}
|
2024-03-12 14:05:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private async Task InitializeSevenTv() {
|
2024-03-15 08:27:35 -04:00
|
|
|
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.");
|
2024-03-12 14:05:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task InitializeObs() {
|
2024-03-15 08:27:35 -04:00
|
|
|
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.");
|
2024-03-12 14:05:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task<HermesClient> InitializeHermes() {
|
|
|
|
// Fetch id and username based on api key given.
|
2024-03-15 08:27:35 -04:00
|
|
|
_logger.LogInformation("Initializing hermes client.");
|
|
|
|
var hermes = _serviceProvider.GetRequiredService<HermesClient>();
|
2024-03-12 14:05:27 -04:00
|
|
|
await hermes.FetchHermesAccountDetails();
|
|
|
|
return hermes;
|
|
|
|
}
|
|
|
|
|
2024-03-15 08:27:35 -04:00
|
|
|
private async Task<TwitchApiClient> InitializeTwitchApiClient(string username) {
|
|
|
|
_logger.LogInformation("Initializing twitch client.");
|
|
|
|
var twitchapiclient = _serviceProvider.GetRequiredService<TwitchApiClient>();
|
2024-03-12 14:05:27 -04:00
|
|
|
await twitchapiclient.Authorize();
|
|
|
|
|
2024-03-15 08:27:35 -04:00
|
|
|
var channels = _configuration.Twitch.Channels ?? [username];
|
|
|
|
_logger.LogInformation("Twitch channels: " + string.Join(", ", channels));
|
|
|
|
twitchapiclient.InitializeClient(username, channels);
|
2024-03-12 14:05:27 -04:00
|
|
|
twitchapiclient.InitializePublisher();
|
|
|
|
|
2024-03-15 08:27:35 -04:00
|
|
|
var handler = _serviceProvider.GetRequiredService<ChatMessageHandler>();
|
2024-03-12 14:05:27 -04:00
|
|
|
twitchapiclient.AddOnNewMessageReceived(async Task (object? s, OnMessageReceivedArgs e) => {
|
2024-03-15 08:27:35 -04:00
|
|
|
var result = await handler.Handle(e);
|
2024-03-12 14:05:27 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
return twitchapiclient;
|
|
|
|
}
|
|
|
|
|
|
|
|
private async Task StartSavingEmoteCounter() {
|
|
|
|
Task.Run(async () => {
|
|
|
|
while (true) {
|
|
|
|
try {
|
|
|
|
await Task.Delay(TimeSpan.FromSeconds(300));
|
|
|
|
|
|
|
|
var serializer = new SerializerBuilder()
|
|
|
|
.WithNamingConvention(HyphenatedNamingConvention.Instance)
|
|
|
|
.Build();
|
|
|
|
|
2024-03-15 08:27:35 -04:00
|
|
|
var chathandler = _serviceProvider.GetRequiredService<ChatMessageHandler>();
|
|
|
|
using (TextWriter writer = File.CreateText(_configuration.Emotes.CounterFilePath.Trim()))
|
2024-03-12 14:05:27 -04:00
|
|
|
{
|
2024-03-15 08:27:35 -04:00
|
|
|
await writer.WriteAsync(serializer.Serialize(chathandler._emoteCounter));
|
2024-03-12 14:05:27 -04:00
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
2024-03-15 08:27:35 -04:00
|
|
|
_logger.LogError(e, "Failed to save the emote counter.");
|
2024-03-12 14:05:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|