hermes-client/Chat/Speech/TTSPlayer.cs

215 lines
6.2 KiB
C#
Raw Permalink Normal View History

2023-12-30 04:27:31 -05:00
using NAudio.Wave;
namespace TwitchChatTTS.Chat.Speech
{
2024-08-12 01:44:31 -04:00
public class TTSPlayer
{
2024-08-12 01:44:31 -04:00
private readonly PriorityQueue<TTSGroupedMessage, int> _messages; // ready to play
private readonly PriorityQueue<TTSGroupedMessage, int> _buffer;
private readonly Mutex _mutex;
private readonly Mutex _mutex2;
2023-12-30 04:27:31 -05:00
2024-08-12 01:44:31 -04:00
public TTSGroupedMessage? Playing { get; set; }
public TTSPlayer()
{
2024-08-12 01:44:31 -04:00
_messages = new PriorityQueue<TTSGroupedMessage, int>(new DescendingOrder());
_buffer = new PriorityQueue<TTSGroupedMessage, int>(new DescendingOrder());
_mutex = new Mutex();
_mutex2 = new Mutex();
2023-12-30 04:27:31 -05:00
}
2024-08-12 01:44:31 -04:00
public void Add(TTSGroupedMessage message, int priority)
{
2024-08-12 01:44:31 -04:00
try
{
2024-08-12 01:44:31 -04:00
_mutex2.WaitOne();
_buffer.Enqueue(message, priority);
}
finally
{
_mutex2.ReleaseMutex();
2023-12-30 04:27:31 -05:00
}
}
2024-08-12 01:44:31 -04:00
public TTSGroupedMessage? ReceiveReady()
{
2024-08-12 01:44:31 -04:00
try
{
2024-08-12 01:44:31 -04:00
_mutex.WaitOne();
if (_messages.TryDequeue(out TTSGroupedMessage? message, out int _))
{
return message;
}
return null;
}
finally
{
_mutex.ReleaseMutex();
2023-12-30 04:27:31 -05:00
}
}
2024-08-12 01:44:31 -04:00
public TTSGroupedMessage? ReceiveBuffer()
{
2024-08-12 01:44:31 -04:00
try
{
_mutex2.WaitOne();
if (_buffer.TryDequeue(out TTSGroupedMessage? messages, out int _))
{
return messages;
}
return null;
}
finally
{
_mutex2.ReleaseMutex();
}
2023-12-30 04:27:31 -05:00
}
2024-08-12 01:44:31 -04:00
public void Ready(TTSGroupedMessage message)
{
2024-08-12 01:44:31 -04:00
try
{
_mutex.WaitOne();
_messages.Enqueue(message, message.Priority);
}
finally
{
_mutex.ReleaseMutex();
}
}
2024-08-12 01:44:31 -04:00
public void RemoveAll()
{
2024-08-12 01:44:31 -04:00
try
{
2024-08-12 01:44:31 -04:00
_mutex2.WaitOne();
_buffer.Clear();
}
2024-08-12 01:44:31 -04:00
finally
{
_mutex2.ReleaseMutex();
}
2024-08-12 01:44:31 -04:00
try
{
2024-08-12 01:44:31 -04:00
_mutex.WaitOne();
_messages.Clear();
2024-08-12 01:44:31 -04:00
}
finally
{
_mutex.ReleaseMutex();
}
}
2024-08-12 01:44:31 -04:00
public void RemoveAll(long broadcasterId, long chatterId)
{
2024-08-12 01:44:31 -04:00
try
{
2024-08-12 01:44:31 -04:00
_mutex2.WaitOne();
if (_buffer.UnorderedItems.Any(i => i.Element.RoomId == broadcasterId && i.Element.ChatterId == chatterId))
{
var list = _buffer.UnorderedItems.Where(i => i.Element.RoomId == broadcasterId && i.Element.ChatterId != chatterId).ToArray();
_buffer.Clear();
foreach (var item in list)
_buffer.Enqueue(item.Element, item.Element.Priority);
}
}
finally
{
_mutex2.ReleaseMutex();
}
try
{
_mutex.WaitOne();
if (_messages.UnorderedItems.Any(i => i.Element.RoomId == broadcasterId && i.Element.ChatterId == chatterId))
{
var list = _messages.UnorderedItems.Where(i => i.Element.RoomId == broadcasterId && i.Element.ChatterId != chatterId).ToArray();
_messages.Clear();
foreach (var item in list)
_messages.Enqueue(item.Element, item.Element.Priority);
}
}
finally
{
_mutex.ReleaseMutex();
}
}
2024-08-12 01:44:31 -04:00
public void RemoveMessage(string messageId)
{
2024-08-12 01:44:31 -04:00
try
{
2024-08-12 01:44:31 -04:00
_mutex2.WaitOne();
if (_buffer.UnorderedItems.Any(i => i.Element.MessageId == messageId))
{
var list = _buffer.UnorderedItems.Where(i => i.Element.MessageId != messageId).ToArray();
_buffer.Clear();
foreach (var item in list)
_buffer.Enqueue(item.Element, item.Element.Priority);
return;
}
}
finally
{
_mutex2.ReleaseMutex();
}
try
{
_mutex.WaitOne();
if (_messages.UnorderedItems.Any(i => i.Element.MessageId == messageId))
{
var list = _messages.UnorderedItems.Where(i => i.Element.MessageId != messageId).ToArray();
_messages.Clear();
foreach (var item in list)
_messages.Enqueue(item.Element, item.Element.Priority);
}
}
finally
{
_mutex.ReleaseMutex();
}
}
2024-08-12 01:44:31 -04:00
public bool IsEmpty()
{
2024-08-12 01:44:31 -04:00
return _messages.Count == 0;
}
private class DescendingOrder : IComparer<int>
{
public int Compare(int x, int y) => y.CompareTo(x);
}
}
2024-08-12 01:44:31 -04:00
public class TTSMessage
{
2024-08-12 01:44:31 -04:00
public string? Voice { get; set; }
public string? Message { get; set; }
public string? File { get; set; }
2023-12-30 04:27:31 -05:00
}
2024-08-12 01:44:31 -04:00
public class TTSGroupedMessage
{
2024-08-12 01:44:31 -04:00
public long RoomId { get; set; }
public long? ChatterId { get; set; }
public string? MessageId { get; set; }
2024-08-12 01:44:31 -04:00
public DateTime Timestamp { get; set; }
public int Priority { get; set; }
public IList<TTSMessage> Messages { get; set; }
public ISampleProvider? Audio { get; set; }
public TTSGroupedMessage(long broadcasterId, long? chatterId, string? messageId, IList<TTSMessage> messages, DateTime timestamp, int priority)
2024-08-12 01:44:31 -04:00
{
RoomId = broadcasterId;
ChatterId = chatterId;
MessageId = messageId;
Messages = messages;
Timestamp = timestamp;
Priority = priority;
}
}
2023-12-30 04:27:31 -05:00
}