2023-12-30 04:27:31 -05:00
|
|
|
using NAudio.Wave;
|
2024-08-04 19:46:10 -04:00
|
|
|
using TwitchChatTTS.Twitch.Socket.Messages;
|
2023-12-30 04:27:31 -05:00
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
public class TTSPlayer
|
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
private readonly PriorityQueue<TTSMessage, int> _messages; // ready to play
|
|
|
|
private readonly PriorityQueue<TTSMessage, int> _buffer;
|
|
|
|
private readonly Mutex _mutex;
|
|
|
|
private readonly Mutex _mutex2;
|
2023-12-30 04:27:31 -05:00
|
|
|
|
2024-08-04 19:46:10 -04:00
|
|
|
public TTSMessage? Playing { get; set; }
|
2024-03-15 08:27:35 -04:00
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
public TTSPlayer()
|
|
|
|
{
|
2024-07-12 13:36:09 -04:00
|
|
|
_messages = new PriorityQueue<TTSMessage, int>(new DescendingOrder());
|
|
|
|
_buffer = new PriorityQueue<TTSMessage, int>(new DescendingOrder());
|
2023-12-30 04:27:31 -05:00
|
|
|
_mutex = new Mutex();
|
|
|
|
_mutex2 = new Mutex();
|
|
|
|
}
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
public void Add(TTSMessage message)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2023-12-30 04:27:31 -05:00
|
|
|
_mutex2.WaitOne();
|
|
|
|
_buffer.Enqueue(message, message.Priority);
|
2024-06-16 20:19:31 -04:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
2023-12-30 04:27:31 -05:00
|
|
|
_mutex2.ReleaseMutex();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
public TTSMessage? ReceiveReady()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2023-12-30 04:27:31 -05:00
|
|
|
_mutex.WaitOne();
|
2024-06-16 20:19:31 -04:00
|
|
|
if (_messages.TryDequeue(out TTSMessage? message, out int _))
|
|
|
|
{
|
2023-12-30 04:27:31 -05:00
|
|
|
return message;
|
|
|
|
}
|
|
|
|
return null;
|
2024-06-16 20:19:31 -04:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
2023-12-30 04:27:31 -05:00
|
|
|
_mutex.ReleaseMutex();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
public TTSMessage? ReceiveBuffer()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2023-12-30 04:27:31 -05:00
|
|
|
_mutex2.WaitOne();
|
2024-06-16 20:19:31 -04:00
|
|
|
if (_buffer.TryDequeue(out TTSMessage? message, out int _))
|
|
|
|
{
|
2023-12-30 04:27:31 -05:00
|
|
|
return message;
|
|
|
|
}
|
|
|
|
return null;
|
2024-06-16 20:19:31 -04:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
2023-12-30 04:27:31 -05:00
|
|
|
_mutex2.ReleaseMutex();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
public void Ready(TTSMessage message)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2023-12-30 04:27:31 -05:00
|
|
|
_mutex.WaitOne();
|
|
|
|
_messages.Enqueue(message, message.Priority);
|
2024-06-16 20:19:31 -04:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
2023-12-30 04:27:31 -05:00
|
|
|
_mutex.ReleaseMutex();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
public void RemoveAll()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2024-03-12 14:05:27 -04:00
|
|
|
_mutex2.WaitOne();
|
|
|
|
_buffer.Clear();
|
2024-06-16 20:19:31 -04:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
2024-03-12 14:05:27 -04:00
|
|
|
_mutex2.ReleaseMutex();
|
|
|
|
}
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
try
|
|
|
|
{
|
2024-03-12 14:05:27 -04:00
|
|
|
_mutex.WaitOne();
|
|
|
|
_messages.Clear();
|
2024-06-16 20:19:31 -04:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
2024-03-12 14:05:27 -04:00
|
|
|
_mutex.ReleaseMutex();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-04 19:46:10 -04:00
|
|
|
public void RemoveAll(long chatterId)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
_mutex2.WaitOne();
|
|
|
|
if (_buffer.UnorderedItems.Any(i => i.Element.ChatterId == chatterId)) {
|
|
|
|
var list = _buffer.UnorderedItems.Where(i => 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.ChatterId == chatterId)) {
|
|
|
|
var list = _messages.UnorderedItems.Where(i => i.Element.ChatterId != chatterId).ToArray();
|
|
|
|
_messages.Clear();
|
|
|
|
foreach (var item in list)
|
|
|
|
_messages.Enqueue(item.Element, item.Element.Priority);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
_mutex.ReleaseMutex();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void RemoveMessage(string messageId)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
_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-06-16 20:19:31 -04:00
|
|
|
public bool IsEmpty()
|
|
|
|
{
|
2023-12-30 04:27:31 -05:00
|
|
|
return _messages.Count == 0;
|
|
|
|
}
|
2024-07-12 13:36:09 -04:00
|
|
|
|
2024-08-04 19:46:10 -04:00
|
|
|
private class DescendingOrder : IComparer<int>
|
|
|
|
{
|
2024-07-12 13:36:09 -04:00
|
|
|
public int Compare(int x, int y) => y.CompareTo(x);
|
|
|
|
}
|
2023-12-30 04:27:31 -05:00
|
|
|
}
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
public class TTSMessage
|
|
|
|
{
|
2024-03-12 14:05:27 -04:00
|
|
|
public string? Voice { get; set; }
|
2024-08-04 19:46:10 -04:00
|
|
|
public long ChatterId { get; set; }
|
|
|
|
public string MessageId { get; set; }
|
2024-03-12 14:05:27 -04:00
|
|
|
public string? Message { get; set; }
|
|
|
|
public string? File { get; set; }
|
2023-12-30 04:27:31 -05:00
|
|
|
public DateTime Timestamp { get; set; }
|
2024-08-04 19:46:10 -04:00
|
|
|
public IEnumerable<TwitchBadge> Badges { get; set; }
|
2023-12-30 04:27:31 -05:00
|
|
|
public int Priority { get; set; }
|
2024-03-12 14:05:27 -04:00
|
|
|
public ISampleProvider? Audio { get; set; }
|
2023-12-30 04:27:31 -05:00
|
|
|
}
|