TwitchUtils/TTSMe/Voices.cs

27 lines
1.3 KiB
C#

using TwitchLib.Client.Models;
namespace TTSMe;
public class Voices {
private static Voices? instance;
public static Voices Instance() => instance ??= new Voices();
private Dictionary<string, string> UserVoices { get; } = new();
internal readonly List<string> ValidVoices = [ "alloy", "echo", "fable", "onyx", "nova", "shimmer" ];
public bool HasVoice(string username) => UserVoices.ContainsKey(username);
public bool IsValidVoice(string voice) => ValidVoices.Contains(voice);
public bool IsVoice(string username, string voice) => HasVoice(username) && UserVoices.GetValueOrDefault(username) == voice;
public string? GetVoice(string username) => UserVoices.GetValueOrDefault(username);
public void SetVoice(string username, string voice) => UserVoices[username] = voice;
public void SetRandomVoice(string username) => UserVoices[username] = ValidVoices[new Random().Next(ValidVoices.Count)];
public void RemoveVoice(string username) => UserVoices.Remove(username);
public static async Task ProcessVoiceMessage(ChatMessage chatMessage) {
string? voice = chatMessage.GetStoredVoice();
if (voice is null) return;
if (!File.Exists(chatMessage.GetAudioFilePath())) await TTSClient.GenerateSpeech(chatMessage);
await TTSClient.PlaySpeech(chatMessage.GetAudioFilePath());
}
}