Added voice selection, username filter and word filter from hermes

This commit is contained in:
Tom 2024-01-05 10:07:41 +00:00
parent 8845757c29
commit 9cd6725570
10 changed files with 339 additions and 180 deletions

View File

@ -1,8 +1,10 @@
using System; using System;
using TwitchChatTTS.Hermes;
public class HermesClient { public class HermesClient {
private Account account; private Account account;
private string key; private string key;
private WebHelper _web;
public string Id { get => account?.id; } public string Id { get => account?.id; }
public string Username { get => account?.username; } public string Username { get => account?.username; }
@ -15,23 +17,73 @@ public class HermesClient {
} }
key = File.ReadAllText(".token")?.Trim(); key = File.ReadAllText(".token")?.Trim();
WebHelper.AddHeader("x-api-key", key); _web = new WebHelper();
_web.AddHeader("x-api-key", key);
} }
public async Task UpdateHermesAccount() { public async Task UpdateHermesAccount() {
account = await WebHelper.GetJson<Account>("https://hermes.goblincaves.com/api/account"); ValidateKey();
account = await _web.GetJson<Account>("https://hermes.goblincaves.com/api/account");
} }
public async Task<TwitchBotToken> FetchTwitchBotToken() { public async Task<TwitchBotToken> FetchTwitchBotToken() {
if (string.IsNullOrWhiteSpace(key)) { ValidateKey();
throw new InvalidOperationException("Hermes API key not provided.");
}
var token = await WebHelper.GetJson<TwitchBotToken>("https://hermes.goblincaves.com/api/token/bot"); var token = await _web.GetJson<TwitchBotToken>("https://hermes.goblincaves.com/api/token/bot");
if (token == null) { if (token == null) {
throw new Exception("Failed to fetch Twitch API token from Hermes."); throw new Exception("Failed to fetch Twitch API token from Hermes.");
} }
return token; return token;
} }
public async Task<IEnumerable<TTSUsernameFilter>> FetchTTSUsernameFilters() {
ValidateKey();
var filters = await _web.GetJson<IEnumerable<TTSUsernameFilter>>("https://hermes.goblincaves.com/api/settings/tts/filter/users");
if (filters == null) {
throw new Exception("Failed to fetch TTS username filters from Hermes.");
}
return filters;
}
public async Task<string> FetchTTSDefaultVoice() {
ValidateKey();
var data = await _web.GetJson<TTSVoice>("https://hermes.goblincaves.com/api/settings/tts/default");
if (data == null) {
throw new Exception("Failed to fetch TTS default voice from Hermes.");
}
return data.label;
}
public async Task<IEnumerable<TTSVoice>> FetchTTSEnabledVoices() {
ValidateKey();
var voices = await _web.GetJson<IEnumerable<TTSVoice>>("https://hermes.goblincaves.com/api/settings/tts");
if (voices == null) {
throw new Exception("Failed to fetch TTS enabled voices from Hermes.");
}
return voices;
}
public async Task<IEnumerable<TTSWordFilter>> FetchTTSWordFilters() {
ValidateKey();
var filters = await _web.GetJson<IEnumerable<TTSWordFilter>>("https://hermes.goblincaves.com/api/settings/tts/filter/words");
if (filters == null) {
throw new Exception("Failed to fetch TTS word filters from Hermes.");
}
return filters;
}
private void ValidateKey() {
if (string.IsNullOrWhiteSpace(key)) {
throw new InvalidOperationException("Hermes API key not provided.");
}
}
} }

View File

@ -0,0 +1,5 @@
public class TTSUsernameFilter {
public string username { get; set; }
public string tag { get; set; }
public string userId { get; set; }
}

View File

@ -0,0 +1,6 @@
public class TTSVoice {
public string label { get; set; }
public int value { get; set; }
public string gender { get; set; }
public string language { get; set; }
}

View File

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace TwitchChatTTS.Hermes
{
public class TTSWordFilter
{
public string id { get; set; }
public string search { get; set; }
public string replace { get; set; }
public string userId { get; set; }
public bool IsRegex { get; set; }
public TTSWordFilter() {
IsRegex = true;
}
}
}

View File

@ -0,0 +1,200 @@
using System.Text.RegularExpressions;
using TwitchLib.Client.Events;
using TwitchChatTTS.Hermes;
public class ChatMessageHandler {
private TTSPlayer Player { get; }
public string DefaultVoice { get; set; }
public IEnumerable<TTSVoice> EnabledVoices { get; }
public Dictionary<string, TTSUsernameFilter> UsernameFilters { get; }
public IEnumerable<TTSWordFilter> WordFilters { get; }
private Regex voicesRegex;
private Regex sfxRegex;
public ChatMessageHandler(TTSPlayer player, string defaultVoice, IEnumerable<TTSVoice> enabledVoices, Dictionary<string, TTSUsernameFilter> usernameFilters, IEnumerable<TTSWordFilter> wordFilters) {
Player = player;
DefaultVoice = defaultVoice;
EnabledVoices = enabledVoices;
UsernameFilters = usernameFilters;
WordFilters = wordFilters;
voicesRegex = GenerateEnabledVoicesRegex();
sfxRegex = new Regex(@"\(([A-Za-z0-9_-]+)\)");
}
public MessageResult Handle(OnMessageReceivedArgs e) {
var m = e.ChatMessage;
var msg = e.ChatMessage.Message;
// Skip TTS messages
if ((m.IsVip || m.IsModerator || m.IsBroadcaster) && (msg.ToLower().StartsWith("!skip ") || msg.ToLower() == "!skip")) {
return MessageResult.Skip;
}
if (UsernameFilters.TryGetValue(m.Username, out TTSUsernameFilter filter) && filter.tag == "blacklisted") {
return MessageResult.Blocked;
}
// Ensure we can send it via the web.
var alphanumeric = new Regex(@"[^a-zA-Z0-9!@#$%&\^*+\-_(),+':;?.,\[\]\s\\/~`]");
msg = alphanumeric.Replace(msg, "");
// Filter highly repetitive words (like emotes) from the message.
var words = msg.Split(" ");
var wordCounter = new Dictionary<string, int>();
string filteredMsg = string.Empty;
foreach (var w in words) {
if (wordCounter.ContainsKey(w)) {
wordCounter[w]++;
} else {
wordCounter.Add(w, 1);
}
if (wordCounter[w] < 5) {
filteredMsg += w + " ";
}
}
msg = filteredMsg;
foreach (var wf in WordFilters) {
if (wf.IsRegex) {
try {
var regex = new Regex(wf.search);
msg = regex.Replace(msg, wf.replace);
continue;
} catch (Exception ex) {
wf.IsRegex = false;
}
}
msg = msg.Replace(wf.search, wf.replace);
}
int priority = 0;
if (m.IsStaff) {
priority = int.MinValue;
} else if (filter?.tag == "priority") {
priority = int.MinValue + 1;
} else if (m.IsModerator) {
priority = -100;
} else if (m.IsVip) {
priority = -10;
} else if (m.IsPartner) {
priority = -5;
} else if (m.IsHighlighted) {
priority = -1;
}
priority = (int) Math.Round(Math.Min(priority, -m.SubscribedMonthCount * (m.Badges.Any(b => b.Key == "subscriber" && b.Value == "1") ? 1.2 : 1)));
var matches = voicesRegex.Matches(msg);
int defaultEnd = matches.FirstOrDefault()?.Index ?? msg.Length;
if (defaultEnd > 0) {
HandlePartialMessage(priority, DefaultVoice, msg.Substring(0, defaultEnd).Trim(), e);
}
foreach (Match match in matches) {
var message = match.Groups[2].ToString();
if (string.IsNullOrWhiteSpace(message)) {
continue;
}
var voice = match.Groups[1].ToString();
voice = voice[0].ToString().ToUpper() + voice.Substring(1).ToLower();
HandlePartialMessage(priority, voice, message.Trim(), e);
}
return MessageResult.None;
}
private void HandlePartialMessage(int priority, string voice, string message, OnMessageReceivedArgs e) {
if (string.IsNullOrWhiteSpace(message)) {
return;
}
var m = e.ChatMessage;
var parts = sfxRegex.Split(message);
var badgesString = string.Join(", ", e.ChatMessage.Badges.Select(b => b.Key + " = " + b.Value));
if (parts.Length == 1) {
Console.WriteLine($"Voice: {voice}; Priority: {priority}; Message: {message}; Month: {m.SubscribedMonthCount}; {badgesString}");
Player.Add(new TTSMessage() {
Voice = voice,
Message = message,
Moderator = m.IsModerator,
Timestamp = DateTime.UtcNow,
Username = m.Username,
Bits = m.Bits,
Badges = m.Badges,
Priority = priority
});
return;
}
var sfxMatches = sfxRegex.Matches(message);
var sfxStart = sfxMatches.FirstOrDefault()?.Index ?? message.Length;
for (var i = 0; i < sfxMatches.Count; i++) {
var sfxMatch = sfxMatches[i];
var sfxName = sfxMatch.Groups[1]?.ToString()?.ToLower();
if (!File.Exists("sfx/" + sfxName + ".mp3")) {
parts[i * 2 + 2] = parts[i * 2] + " (" + parts[i * 2 + 1] + ")" + parts[i * 2 + 2];
continue;
}
if (!string.IsNullOrWhiteSpace(parts[i * 2])) {
Console.WriteLine($"Voice: {voice}; Priority: {priority}; Message: {parts[i * 2]}; Month: {m.SubscribedMonthCount}; {badgesString}");
Player.Add(new TTSMessage() {
Voice = voice,
Message = parts[i * 2],
Moderator = m.IsModerator,
Timestamp = DateTime.UtcNow,
Username = m.Username,
Bits = m.Bits,
Badges = m.Badges,
Priority = priority
});
}
Console.WriteLine($"Voice: {voice}; Priority: {priority}; SFX: {sfxName}; Month: {m.SubscribedMonthCount}; {badgesString}");
Player.Add(new TTSMessage() {
Voice = voice,
Message = sfxName,
File = $"sfx/{sfxName}.mp3",
Moderator = m.IsModerator,
Timestamp = DateTime.UtcNow,
Username = m.Username,
Bits = m.Bits,
Badges = m.Badges,
Priority = priority
});
}
if (!string.IsNullOrWhiteSpace(parts.Last())) {
Console.WriteLine($"Voice: {voice}; Priority: {priority}; Message: {parts.Last()}; Month: {m.SubscribedMonthCount}; {badgesString}");
Player.Add(new TTSMessage() {
Voice = voice,
Message = parts.Last(),
Moderator = m.IsModerator,
Timestamp = DateTime.UtcNow,
Username = m.Username,
Bits = m.Bits,
Badges = m.Badges,
Priority = priority
});
}
}
private Regex GenerateEnabledVoicesRegex() {
if (EnabledVoices == null || EnabledVoices.Count() <= 0) {
return null;
}
var enabledVoicesString = string.Join("|", EnabledVoices.Select(v => v.label));
return new Regex($@"\b({enabledVoicesString})\:(.*?)(?=\Z|\b(?:{enabledVoicesString})\:)", RegexOptions.IgnoreCase);
}
}

View File

@ -0,0 +1,5 @@
public enum MessageResult {
Skip = 1,
Blocked = 2,
None = 0
}

View File

@ -37,7 +37,6 @@ HermesClient hermes = new HermesClient();
Console.WriteLine("Fetching Hermes account details..."); Console.WriteLine("Fetching Hermes account details...");
await hermes.UpdateHermesAccount(); await hermes.UpdateHermesAccount();
Console.WriteLine("ID: " + hermes.Id);
Console.WriteLine("Username: " + hermes.Username); Console.WriteLine("Username: " + hermes.Username);
Console.WriteLine(); Console.WriteLine();
@ -45,178 +44,42 @@ Console.WriteLine("Fetching Twitch API details from Hermes...");
TwitchApiClient twitchapiclient = new TwitchApiClient(await hermes.FetchTwitchBotToken()); TwitchApiClient twitchapiclient = new TwitchApiClient(await hermes.FetchTwitchBotToken());
await twitchapiclient.Authorize(); await twitchapiclient.Authorize();
var sfxRegex = new Regex(@"\(([A-Za-z0-9_-]+)\)"); Console.WriteLine("Fetching TTS username filters...");
var voiceRegex = new Regex(@"\b(Filiz|Astrid|Tatyana|Maxim|Carmen|Ines|Cristiano|Vitoria|Ricardo|Maja|Jan|Jacek|Ewa|Ruben|Lotte|Liv|Seoyeon|Takumi|Mizuki|Giorgio|Carla|Bianca|Karl|Dora|Mathieu|Celine|Chantal|Penelope|Miguel|Mia|Enrique|Conchita|Geraint|Salli|Matthew|Kimberly|Kendra|Justin|Joey|Joanna|Ivy|Raveena|Aditi|Emma|Brian|Amy|Russell|Nicole|Vicki|Marlene|Hans|Naja|Mads|Gwyneth|Zhiyu|Tracy|Danny|Huihui|Yaoyao|Kangkang|HanHan|Zhiwei|Asaf|An|Stefanos|Filip|Ivan|Heidi|Herena|Kalpana|Hemant|Matej|Andika|Rizwan|Lado|Valluvar|Linda|Heather|Sean|Michael|Karsten|Guillaume|Pattara|Jakub|Szabolcs|Hoda|Naayf)\:(.*?)(?=\Z|\b(?:Filiz|Astrid|Tatyana|Maxim|Carmen|Ines|Cristiano|Vitoria|Ricardo|Maja|Jan|Jacek|Ewa|Ruben|Lotte|Liv|Seoyeon|Takumi|Mizuki|Giorgio|Carla|Bianca|Karl|Dora|Mathieu|Celine|Chantal|Penelope|Miguel|Mia|Enrique|Conchita|Geraint|Salli|Matthew|Kimberly|Kendra|Justin|Joey|Joanna|Ivy|Raveena|Aditi|Emma|Brian|Amy|Russell|Nicole|Vicki|Marlene|Hans|Naja|Mads|Gwyneth|Zhiyu|Tracy|Danny|Huihui|Yaoyao|Kangkang|HanHan|Zhiwei|Asaf|An|Stefanos|Filip|Ivan|Heidi|Herena|Kalpana|Hemant|Matej|Andika|Rizwan|Lado|Valluvar|Linda|Heather|Sean|Michael|Karsten|Guillaume|Pattara|Jakub|Szabolcs|Hoda|Naayf)\:)", RegexOptions.IgnoreCase); var usernameFilters = (await hermes.FetchTTSUsernameFilters())
.ToDictionary(x => x.username, x => x);
Console.WriteLine($"{usernameFilters.Where(f => f.Value.tag == "blacklisted").Count()} username(s) have been blocked.");
Console.WriteLine($"{usernameFilters.Where(f => f.Value.tag == "priority").Count()} user(s) have been prioritized.");
var enabledVoices = await hermes.FetchTTSEnabledVoices();
Console.WriteLine($"{enabledVoices.Count()} TTS voices enabled.");
var wordFilters = await hermes.FetchTTSWordFilters();
Console.WriteLine($"{wordFilters.Count()} TTS word filters.");
var defaultVoice = await hermes.FetchTTSDefaultVoice();
Console.WriteLine("Default Voice: " + defaultVoice);
TTSPlayer player = new TTSPlayer(); TTSPlayer player = new TTSPlayer();
ISampleProvider playing = null; ISampleProvider playing = null;
var handler = new ChatMessageHandler(player, defaultVoice, enabledVoices, usernameFilters, wordFilters);
var channels = File.Exists(".twitchchannels") ? File.ReadAllLines(".twitchchannels") : new string[] { hermes.Username }; var channels = File.Exists(".twitchchannels") ? File.ReadAllLines(".twitchchannels") : new string[] { hermes.Username };
Console.WriteLine("Twitch channels: " + string.Join(", ", channels));
twitchapiclient.InitializeClient(hermes, channels); twitchapiclient.InitializeClient(hermes, channels);
twitchapiclient.InitializePublisher(player, redeems); twitchapiclient.InitializePublisher(player, redeems);
void HandleMessage(int priority, string voice, string message, OnMessageReceivedArgs e, bool bot) {
var m = e.ChatMessage;
var parts = sfxRegex.Split(message);
var sfxMatches = sfxRegex.Matches(message);
var sfxStart = sfxMatches.FirstOrDefault()?.Index ?? message.Length;
var alphanumeric = new Regex(@"[^a-zA-Z0-9!@#$%&\^*+\-_(),+':;?.,\[\]\s\\/~`]");
message = alphanumeric.Replace(message, " ");
if (string.IsNullOrWhiteSpace(message)) {
return;
}
if (parts.Length == 1) {
Console.WriteLine($"Voice: {voice}; Priority: {priority}; Message: {message}; Month: {m.SubscribedMonthCount}; {string.Join(", ", e.ChatMessage.Badges.Select(b => b.Key + " = " + b.Value))}");
player.Add(new TTSMessage() {
Voice = voice,
Bot = bot,
Message = message,
Moderator = m.IsModerator,
Timestamp = DateTime.UtcNow,
Username = m.Username,
Bits = m.Bits,
Badges = m.Badges,
Priority = priority
});
return;
}
for (var i = 0; i < sfxMatches.Count; i++) {
var sfxMatch = sfxMatches[i];
var sfxName = sfxMatch.Groups[1]?.ToString()?.ToLower();
if (!File.Exists("sfx/" + sfxName + ".mp3")) {
parts[i * 2 + 2] = parts[i * 2] + " (" + parts[i * 2 + 1] + ")" + parts[i * 2 + 2];
continue;
}
if (!string.IsNullOrWhiteSpace(parts[i * 2])) {
Console.WriteLine($"Voice: {voice}; Priority: {priority}; Message: {parts[i * 2]}; Month: {m.SubscribedMonthCount}; {string.Join(", ", e.ChatMessage.Badges.Select(b => b.Key + " = " + b.Value))}");
player.Add(new TTSMessage() {
Voice = voice,
Bot = bot,
Message = parts[i * 2],
Moderator = m.IsModerator,
Timestamp = DateTime.UtcNow,
Username = m.Username,
Bits = m.Bits,
Badges = m.Badges,
Priority = priority
});
}
Console.WriteLine($"Voice: {voice}; Priority: {priority}; SFX: {sfxName}; Month: {m.SubscribedMonthCount}; {string.Join(", ", e.ChatMessage.Badges.Select(b => b.Key + " = " + b.Value))}");
player.Add(new TTSMessage() {
Voice = voice,
Bot = bot,
Message = sfxName,
File = $"sfx/{sfxName}.mp3",
Moderator = m.IsModerator,
Timestamp = DateTime.UtcNow,
Username = m.Username,
Bits = m.Bits,
Badges = m.Badges,
Priority = priority
});
}
if (!string.IsNullOrWhiteSpace(parts.Last())) {
Console.WriteLine($"Voice: {voice}; Priority: {priority}; Message: {parts.Last()}; Month: {m.SubscribedMonthCount}; {string.Join(", ", e.ChatMessage.Badges.Select(b => b.Key + " = " + b.Value))}");
player.Add(new TTSMessage() {
Voice = voice,
Bot = bot,
Message = parts.Last(),
Moderator = m.IsModerator,
Timestamp = DateTime.UtcNow,
Username = m.Username,
Bits = m.Bits,
Badges = m.Badges,
Priority = priority
});
}
}
twitchapiclient.AddOnNewMessageReceived(async Task (object? s, OnMessageReceivedArgs e) => { twitchapiclient.AddOnNewMessageReceived(async Task (object? s, OnMessageReceivedArgs e) => {
var m = e.ChatMessage; var result = handler.Handle(e);
var msg = e.ChatMessage.Message;
if ((m.IsVip || m.IsModerator || m.IsBroadcaster) && (msg.ToLower().StartsWith("!skip ") || msg.ToLower() == "!skip")) {
AudioPlaybackEngine.Instance.RemoveMixerInput(playing);
playing = null;
return;
}
string[] bots = new string[] { "nightbot", "streamelements", "own3d", "streamlabs", "soundalerts", "pokemoncommunitygame" }; switch (result) {
bool bot = bots.Any(b => b == m.Username); case MessageResult.Skip:
if (bot || m.IsBroadcaster || msg.StartsWith('!')) { AudioPlaybackEngine.Instance.RemoveMixerInput(playing);
return; playing = null;
} break;
default:
string[] bad = new string[] { "incel", "simp", "virgin", "faggot", "fagg", "fag", "nigger", "nigga", "nigg", "nig", "whore", "retard", "cock", "fuck", "bastard", "wanker", "bollocks", "motherfucker", "bitch", "bish", "bich", "asshole", "ass", "dick", "dickhead", "frigger", "shit", "slut", "turd", "twat", "nigra", "penis" }; break;
foreach (var b in bad) {
msg = new Regex($@"\b{b}\b", RegexOptions.IgnoreCase).Replace(msg, "");
}
msg = new Regex(@"%").Replace(msg, " percent ");
msg = new Regex(@"https?\:\/\/[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(?:[-a-zA-Z0-9()@:%_\+.~#?&\/\/=]*)").Replace(msg, "");
msg = new Regex(@"\bfreeze153").Replace(msg, "");
// Filter highly repetitive words (like emotes) from message.
var words = msg.Split(" ");
var wordCounter = new Dictionary<string, int>();
string filteredMsg = string.Empty;
foreach (var w in words) {
if (wordCounter.ContainsKey(w)) {
wordCounter[w]++;
} else {
wordCounter.Add(w, 1);
}
if (wordCounter[w] < 5) {
filteredMsg += w + " ";
}
}
msg = filteredMsg;
foreach (var w in words) {
if (wordCounter.ContainsKey(w)) {
wordCounter[w]++;
} else {
wordCounter.Add(w, 1);
}
}
int priority = 0;
if (m.IsStaff) {
priority = int.MinValue;
} else if (m.IsModerator) {
priority = -100;
} else if (m.IsVip) {
priority = -10;
} else if (m.IsPartner) {
priority = -5;
} else if (m.IsHighlighted) {
priority = -1;
}
priority = (int) Math.Round(Math.Min(priority, -m.SubscribedMonthCount * (m.Badges.Any(b => b.Key == "subscriber" && b.Value == "1") ? 1.2 : 1)));
var matches = voiceRegex.Matches(msg);
int defaultEnd = matches.FirstOrDefault()?.Index ?? msg.Length;
if (defaultEnd > 0) {
HandleMessage(priority, "Brian", msg.Substring(0, defaultEnd), e, bot);
}
foreach (Match match in matches) {
var message = match.Groups[2].ToString();
if (string.IsNullOrWhiteSpace(message)) {
continue;
}
var voice = match.Groups[1].ToString();
voice = voice[0].ToString().ToUpper() + voice.Substring(1).ToLower();
HandleMessage(priority, voice, message, e, bot);
} }
}); });
@ -239,8 +102,9 @@ Task.Run(async () => {
var sound = new NetworkWavSound(url); var sound = new NetworkWavSound(url);
var provider = new CachedWavProvider(sound); var provider = new CachedWavProvider(sound);
var data = AudioPlaybackEngine.Instance.ConvertSound(provider); var data = AudioPlaybackEngine.Instance.ConvertSound(provider);
var resampled = new WdlResamplingSampleProvider(data, AudioPlaybackEngine.Instance.SampleRate);
m.Audio = data; m.Audio = resampled;
player.Ready(m); player.Ready(m);
} catch (COMException e) { } catch (COMException e) {
Console.WriteLine(e.GetType().Name + ": " + e.Message + " (HResult: " + e.HResult + ")"); Console.WriteLine(e.GetType().Name + ": " + e.Message + " (HResult: " + e.HResult + ")");

View File

@ -4,16 +4,20 @@ using NAudio.Wave.SampleProviders;
public class AudioPlaybackEngine : IDisposable public class AudioPlaybackEngine : IDisposable
{ {
public static readonly AudioPlaybackEngine Instance = new AudioPlaybackEngine(22050, 1); public static readonly AudioPlaybackEngine Instance = new AudioPlaybackEngine(44100, 2);
private readonly IWavePlayer outputDevice; private readonly IWavePlayer outputDevice;
private readonly MixingSampleProvider mixer; private readonly MixingSampleProvider mixer;
public int SampleRate { get; }
private AudioPlaybackEngine(int sampleRate = 44100, int channelCount = 2) private AudioPlaybackEngine(int sampleRate = 44100, int channelCount = 2)
{ {
SampleRate = sampleRate;
outputDevice = new WaveOutEvent(); outputDevice = new WaveOutEvent();
mixer = new MixingSampleProvider(WaveFormat.CreateIeeeFloatWaveFormat(sampleRate, channelCount)); mixer = new MixingSampleProvider(WaveFormat.CreateIeeeFloatWaveFormat(sampleRate, channelCount));
mixer.ReadFully = true; mixer.ReadFully = true;
outputDevice.Init(mixer); outputDevice.Init(mixer);
outputDevice.Play(); outputDevice.Play();
} }
@ -34,7 +38,7 @@ public class AudioPlaybackEngine : IDisposable
public void PlaySound(string fileName) public void PlaySound(string fileName)
{ {
var input = new AudioFileReader(fileName); var input = new AudioFileReader(fileName);
AddMixerInput(new AutoDisposeFileReader(input)); AddMixerInput(new WdlResamplingSampleProvider(ConvertToRightChannelCount(new AutoDisposeFileReader(input)), SampleRate));
} }
public void PlaySound(NetworkWavSound sound) public void PlaySound(NetworkWavSound sound)
@ -63,7 +67,7 @@ public class AudioPlaybackEngine : IDisposable
} else { } else {
throw new ArgumentException("Unsupported source encoding while adding to mixer."); throw new ArgumentException("Unsupported source encoding while adding to mixer.");
} }
return converted; return ConvertToRightChannelCount(converted);
} }
public void AddMixerInput(ISampleProvider input) public void AddMixerInput(ISampleProvider input)

View File

@ -11,18 +11,20 @@ public class TwitchApiClient {
private TwitchBotToken token; private TwitchBotToken token;
private TwitchClient client; private TwitchClient client;
private TwitchPubSub publisher; private TwitchPubSub publisher;
private WebHelper web;
private bool initialized; private bool initialized;
public TwitchApiClient(TwitchBotToken token) { public TwitchApiClient(TwitchBotToken token) {
client = new TwitchClient(new WebSocketClient()); client = new TwitchClient(new WebSocketClient());
publisher = new TwitchPubSub(); publisher = new TwitchPubSub();
web = new WebHelper();
initialized = false; initialized = false;
this.token = token; this.token = token;
} }
public async Task<bool> Authorize() { public async Task<bool> Authorize() {
var authorize = await WebHelper.Get("https://hermes.goblincaves.com/api/account/reauthorize"); var authorize = await web.Get("https://hermes.goblincaves.com/api/account/reauthorize");
var status = (int) authorize.StatusCode; var status = (int) authorize.StatusCode;
return status == 200 || status == 201; return status == 200 || status == 201;
} }

View File

@ -3,27 +3,26 @@
using System.Net; using System.Net;
using System.Net.Http.Json; using System.Net.Http.Json;
public static class WebHelper { public class WebHelper {
private static HttpClient _client = new HttpClient(); private static HttpClient _client = new HttpClient();
public static void AddHeader(string key, string? value) { public void AddHeader(string key, string? value) {
_client.DefaultRequestHeaders.Add(key, value); _client.DefaultRequestHeaders.Add(key, value);
//ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls13;
} }
public static async Task<T?> GetJson<T>(string uri) { public async Task<T?> GetJson<T>(string uri) {
return (T) await _client.GetFromJsonAsync(uri, typeof(T)); return (T) await _client.GetFromJsonAsync(uri, typeof(T));
} }
public static async Task<HttpResponseMessage> Get(string uri) { public async Task<HttpResponseMessage> Get(string uri) {
return await _client.GetAsync(uri); return await _client.GetAsync(uri);
} }
public static async Task<HttpResponseMessage> Post<T>(string uri, T data) { public async Task<HttpResponseMessage> Post<T>(string uri, T data) {
return await _client.PostAsJsonAsync(uri, data); return await _client.PostAsJsonAsync(uri, data);
} }
public static async Task<HttpResponseMessage> Post(string uri) { public async Task<HttpResponseMessage> Post(string uri) {
return await _client.PostAsJsonAsync(uri, new object()); return await _client.PostAsJsonAsync(uri, new object());
} }
} }