2023-12-30 04:27:31 -05:00
|
|
|
using NAudio.Wave;
|
|
|
|
|
|
|
|
public class TTSPlayer {
|
|
|
|
private PriorityQueue<TTSMessage, int> _messages; // ready to play
|
|
|
|
private PriorityQueue<TTSMessage, int> _buffer;
|
|
|
|
private Mutex _mutex;
|
|
|
|
private Mutex _mutex2;
|
|
|
|
|
2024-03-15 08:27:35 -04:00
|
|
|
public ISampleProvider? Playing { get; set; }
|
|
|
|
|
2023-12-30 04:27:31 -05:00
|
|
|
public TTSPlayer() {
|
|
|
|
_messages = new PriorityQueue<TTSMessage, int>();
|
|
|
|
_buffer = new PriorityQueue<TTSMessage, int>();
|
|
|
|
_mutex = new Mutex();
|
|
|
|
_mutex2 = new Mutex();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Add(TTSMessage message) {
|
|
|
|
try {
|
|
|
|
_mutex2.WaitOne();
|
|
|
|
_buffer.Enqueue(message, message.Priority);
|
|
|
|
} finally {
|
|
|
|
_mutex2.ReleaseMutex();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-12 14:05:27 -04:00
|
|
|
public TTSMessage? ReceiveReady() {
|
2023-12-30 04:27:31 -05:00
|
|
|
try {
|
|
|
|
_mutex.WaitOne();
|
2024-03-12 14:05:27 -04:00
|
|
|
if (_messages.TryDequeue(out TTSMessage? message, out int _)) {
|
2023-12-30 04:27:31 -05:00
|
|
|
return message;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
} finally {
|
|
|
|
_mutex.ReleaseMutex();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-12 14:05:27 -04:00
|
|
|
public TTSMessage? ReceiveBuffer() {
|
2023-12-30 04:27:31 -05:00
|
|
|
try {
|
|
|
|
_mutex2.WaitOne();
|
2024-03-12 14:05:27 -04:00
|
|
|
if (_buffer.TryDequeue(out TTSMessage? message, out int _)) {
|
2023-12-30 04:27:31 -05:00
|
|
|
return message;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
} finally {
|
|
|
|
_mutex2.ReleaseMutex();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Ready(TTSMessage message) {
|
|
|
|
try {
|
|
|
|
_mutex.WaitOne();
|
|
|
|
_messages.Enqueue(message, message.Priority);
|
|
|
|
} finally {
|
|
|
|
_mutex.ReleaseMutex();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-12 14:05:27 -04:00
|
|
|
public void RemoveAll() {
|
|
|
|
try {
|
|
|
|
_mutex2.WaitOne();
|
|
|
|
_buffer.Clear();
|
|
|
|
} finally {
|
|
|
|
_mutex2.ReleaseMutex();
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
_mutex.WaitOne();
|
|
|
|
_messages.Clear();
|
|
|
|
} finally {
|
|
|
|
_mutex.ReleaseMutex();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-30 04:27:31 -05:00
|
|
|
public bool IsEmpty() {
|
|
|
|
return _messages.Count == 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class TTSMessage {
|
2024-03-12 14:05:27 -04:00
|
|
|
public string? Voice { get; set; }
|
|
|
|
public string? Channel { get; set; }
|
|
|
|
public string? Username { 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 bool Moderator { get; set; }
|
|
|
|
public bool Bot { get; set; }
|
2024-03-12 14:05:27 -04:00
|
|
|
public IEnumerable<KeyValuePair<string, string>>? Badges { get; set; }
|
2023-12-30 04:27:31 -05:00
|
|
|
public int Bits { get; set; }
|
|
|
|
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
|
|
|
}
|