Fixed command permissions. Moved to using Twitch's EventSub via websockets. Cleaned some code up. Added detection for subscription messages (no TTS), message deletion, full or partial chat clear. Removes messages from TTS queue if applicable. Added command aliases for static parameters. Word filters use compiled regex if possible. Fixed TTS voice deletion.
This commit is contained in:
@ -2,24 +2,23 @@ using NAudio.Wave;
|
||||
using NAudio.Extras;
|
||||
using NAudio.Wave.SampleProviders;
|
||||
|
||||
public class AudioPlaybackEngine : IDisposable
|
||||
public sealed class AudioPlaybackEngine : IDisposable
|
||||
{
|
||||
public static readonly AudioPlaybackEngine Instance = new AudioPlaybackEngine(44100, 2);
|
||||
|
||||
private readonly IWavePlayer outputDevice;
|
||||
private readonly MixingSampleProvider mixer;
|
||||
public int SampleRate { get; }
|
||||
|
||||
private readonly IWavePlayer _outputDevice;
|
||||
private readonly MixingSampleProvider _mixer;
|
||||
|
||||
private AudioPlaybackEngine(int sampleRate = 44100, int channelCount = 2)
|
||||
public AudioPlaybackEngine(int sampleRate = 44100, int channelCount = 2)
|
||||
{
|
||||
SampleRate = sampleRate;
|
||||
outputDevice = new WaveOutEvent();
|
||||
_outputDevice = new WaveOutEvent();
|
||||
|
||||
mixer = new MixingSampleProvider(WaveFormat.CreateIeeeFloatWaveFormat(sampleRate, channelCount));
|
||||
mixer.ReadFully = true;
|
||||
_mixer = new MixingSampleProvider(WaveFormat.CreateIeeeFloatWaveFormat(sampleRate, channelCount));
|
||||
_mixer.ReadFully = true;
|
||||
|
||||
outputDevice.Init(mixer);
|
||||
outputDevice.Play();
|
||||
_outputDevice.Init(_mixer);
|
||||
_outputDevice.Play();
|
||||
}
|
||||
|
||||
private ISampleProvider ConvertToRightChannelCount(ISampleProvider? input)
|
||||
@ -27,11 +26,11 @@ public class AudioPlaybackEngine : IDisposable
|
||||
if (input == null)
|
||||
throw new NullReferenceException(nameof(input));
|
||||
|
||||
if (input.WaveFormat.Channels == mixer.WaveFormat.Channels)
|
||||
if (input.WaveFormat.Channels == _mixer.WaveFormat.Channels)
|
||||
return input;
|
||||
if (input.WaveFormat.Channels == 1 && mixer.WaveFormat.Channels == 2)
|
||||
if (input.WaveFormat.Channels == 1 && _mixer.WaveFormat.Channels == 2)
|
||||
return new MonoToStereoSampleProvider(input);
|
||||
if (input.WaveFormat.Channels == 2 && mixer.WaveFormat.Channels == 1)
|
||||
if (input.WaveFormat.Channels == 2 && _mixer.WaveFormat.Channels == 1)
|
||||
return new StereoToMonoSampleProvider(input);
|
||||
throw new NotImplementedException("Not yet implemented this channel count conversion");
|
||||
}
|
||||
@ -89,26 +88,26 @@ public class AudioPlaybackEngine : IDisposable
|
||||
|
||||
public void AddMixerInput(ISampleProvider input)
|
||||
{
|
||||
mixer.AddMixerInput(input);
|
||||
_mixer.AddMixerInput(input);
|
||||
}
|
||||
|
||||
public void AddMixerInput(IWaveProvider input)
|
||||
{
|
||||
mixer.AddMixerInput(input);
|
||||
_mixer.AddMixerInput(input);
|
||||
}
|
||||
|
||||
public void RemoveMixerInput(ISampleProvider sound)
|
||||
{
|
||||
mixer.RemoveMixerInput(sound);
|
||||
_mixer.RemoveMixerInput(sound);
|
||||
}
|
||||
|
||||
public void AddOnMixerInputEnded(EventHandler<SampleProviderEventArgs> e)
|
||||
{
|
||||
mixer.MixerInputEnded += e;
|
||||
_mixer.MixerInputEnded += e;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
outputDevice.Dispose();
|
||||
_outputDevice.Dispose();
|
||||
}
|
||||
}
|
@ -1,4 +1,5 @@
|
||||
using NAudio.Wave;
|
||||
using TwitchChatTTS.Twitch.Socket.Messages;
|
||||
|
||||
public class TTSPlayer
|
||||
{
|
||||
@ -7,7 +8,7 @@ public class TTSPlayer
|
||||
private readonly Mutex _mutex;
|
||||
private readonly Mutex _mutex2;
|
||||
|
||||
public ISampleProvider? Playing { get; set; }
|
||||
public TTSMessage? Playing { get; set; }
|
||||
|
||||
public TTSPlayer()
|
||||
{
|
||||
@ -100,12 +101,80 @@ public class TTSPlayer
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsEmpty()
|
||||
{
|
||||
return _messages.Count == 0;
|
||||
}
|
||||
|
||||
private class DescendingOrder : IComparer<int> {
|
||||
private class DescendingOrder : IComparer<int>
|
||||
{
|
||||
public int Compare(int x, int y) => y.CompareTo(x);
|
||||
}
|
||||
}
|
||||
@ -113,15 +182,12 @@ public class TTSPlayer
|
||||
public class TTSMessage
|
||||
{
|
||||
public string? Voice { get; set; }
|
||||
public string? Channel { get; set; }
|
||||
public string? Username { get; set; }
|
||||
public long ChatterId { get; set; }
|
||||
public string MessageId { get; set; }
|
||||
public string? Message { get; set; }
|
||||
public string? File { get; set; }
|
||||
public DateTime Timestamp { get; set; }
|
||||
public bool Moderator { get; set; }
|
||||
public bool Bot { get; set; }
|
||||
public IEnumerable<KeyValuePair<string, string>>? Badges { get; set; }
|
||||
public int Bits { get; set; }
|
||||
public IEnumerable<TwitchBadge> Badges { get; set; }
|
||||
public int Priority { get; set; }
|
||||
public ISampleProvider? Audio { get; set; }
|
||||
}
|
Reference in New Issue
Block a user