hermes-client/Chat/Speech/TTSPlayer.cs

198 lines
5.0 KiB
C#
Raw Normal View History

2023-12-30 04:27:31 -05:00
using NAudio.Wave;
using TwitchChatTTS.Twitch.Socket.Messages;
2023-12-30 04:27:31 -05:00
public class TTSPlayer
{
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
public TTSMessage? Playing { get; set; }
public TTSPlayer()
{
_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();
}
public void Add(TTSMessage message)
{
try
{
2023-12-30 04:27:31 -05:00
_mutex2.WaitOne();
_buffer.Enqueue(message, message.Priority);
}
finally
{
2023-12-30 04:27:31 -05:00
_mutex2.ReleaseMutex();
}
}
public TTSMessage? ReceiveReady()
{
try
{
2023-12-30 04:27:31 -05:00
_mutex.WaitOne();
if (_messages.TryDequeue(out TTSMessage? message, out int _))
{
2023-12-30 04:27:31 -05:00
return message;
}
return null;
}
finally
{
2023-12-30 04:27:31 -05:00
_mutex.ReleaseMutex();
}
}
public TTSMessage? ReceiveBuffer()
{
try
{
2023-12-30 04:27:31 -05:00
_mutex2.WaitOne();
if (_buffer.TryDequeue(out TTSMessage? message, out int _))
{
2023-12-30 04:27:31 -05:00
return message;
}
return null;
}
finally
{
2023-12-30 04:27:31 -05:00
_mutex2.ReleaseMutex();
}
}
public void Ready(TTSMessage message)
{
try
{
2023-12-30 04:27:31 -05:00
_mutex.WaitOne();
_messages.Enqueue(message, message.Priority);
}
finally
{
2023-12-30 04:27:31 -05:00
_mutex.ReleaseMutex();
}
}
public void RemoveAll()
{
try
{
_mutex2.WaitOne();
_buffer.Clear();
}
finally
{
_mutex2.ReleaseMutex();
}
try
{
_mutex.WaitOne();
_messages.Clear();
}
finally
{
_mutex.ReleaseMutex();
}
}
public void RemoveAll(long broadcasterId, long chatterId)
{
try
{
_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();
}
}
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();
}
}
public bool IsEmpty()
{
2023-12-30 04:27:31 -05:00
return _messages.Count == 0;
}
private class DescendingOrder : IComparer<int>
{
public int Compare(int x, int y) => y.CompareTo(x);
}
2023-12-30 04:27:31 -05:00
}
public class TTSMessage
{
public string? Voice { get; set; }
public long RoomId { get; set; }
public long ChatterId { get; set; }
public string MessageId { get; set; }
public string? Message { get; set; }
public string? File { get; set; }
2023-12-30 04:27:31 -05:00
public DateTime Timestamp { get; set; }
public IEnumerable<TwitchBadge> Badges { get; set; }
2023-12-30 04:27:31 -05:00
public int Priority { get; set; }
public ISampleProvider? Audio { get; set; }
2023-12-30 04:27:31 -05:00
}