@@ -17,7 +17,7 @@ public class TwitchChatTts : IDisposable {
1717 /// <summary>
1818 /// The configuration for the twitch chat.
1919 /// </summary>
20- private readonly TwitchChatConfiguration ? chatConfig ;
20+ public TwitchChatConfiguration ? ChatConfig { get ; }
2121
2222 /// <summary>
2323 /// The collection of sounds to play.
@@ -59,6 +59,8 @@ public class TwitchChatTts : IDisposable {
5959 /// </summary>
6060 private ManualResetEvent ? ttsSoundOutputSignal ;
6161
62+ private int messageToSkip ;
63+
6264 /// <summary>
6365 /// Initializes a new instance of the <see cref="TwitchChatTts" /> class.
6466 /// </summary>
@@ -69,8 +71,7 @@ public TwitchChatTts(TwitchChatConfiguration? config) {
6971 this . soundThread . Name = "TwitchChatTts Thread" ;
7072 this . soundThread . IsBackground = true ;
7173 this . soundThread . Start ( ) ;
72- this . chatConfig = config ;
73- GlobalKeyboardListener . Instance . Callback += this . KeyboardPressCallback ;
74+ this . ChatConfig = config ;
7475 }
7576
7677 /// <summary>
@@ -92,21 +93,23 @@ public void Dispose() {
9293 this . ttsSoundOutput = null ;
9394 }
9495
95- if ( null == this . chatConfig ) {
96+ if ( null == this . ChatConfig ) {
9697 return ;
9798 }
9899
99- var user = Configuration . Instance . GetTwitchAccount ( this . chatConfig . AccountUsername ) ;
100+ var user = Configuration . Instance . GetTwitchAccount ( this . ChatConfig . AccountUsername ) ;
100101 if ( null == user ) {
101102 return ;
102103 }
103104
104105 var twitchManager = TwitchChatManager . Instance ;
105- twitchManager . RemoveTwitchChannel ( user , this . chatConfig . TwitchChannel , this . Client_OnMessageReceived ) ;
106+ twitchManager . RemoveTwitchChannel ( user , this . ChatConfig . TwitchChannel , this . Client_OnMessageReceived ) ;
106107 if ( this . soundThread . Join ( 5000 ) ) {
107108 this . soundThread . Interrupt ( ) ;
108109 }
109110 }
111+
112+ public string ? CurrentUsername { get ; set ; }
110113
111114 /// <summary>
112115 /// The main thread used to play sound asynchronously.
@@ -119,8 +122,14 @@ private void PlaySoundsThread() {
119122 return ;
120123 }
121124
125+ if ( messageToSkip > 0 ) {
126+ -- messageToSkip ;
127+ Console . WriteLine ( $ "Skipping: { e . ChatMessage . Username } says { e . ChatMessage . Message } ") ;
128+ continue ;
129+ }
130+
122131 Console . WriteLine ( $ "Running: { e . ChatMessage . Username } says { e . ChatMessage . Message } ") ;
123- if ( null == this . chatConfig ) {
132+ if ( null == this . ChatConfig ) {
124133 continue ;
125134 }
126135
@@ -154,8 +163,8 @@ private void PlaySoundsThread() {
154163 using ( var stream = new MemoryStream ( ) ) {
155164 // Setup the microsoft TTS object according to the settings.
156165 synth . SetOutputToWaveStream ( stream ) ;
157- synth . SelectVoice ( this . chatConfig . TtsVoice ) ;
158- synth . Volume = ( int ) this . chatConfig . TtsVolume ;
166+ synth . SelectVoice ( this . ChatConfig . TtsVoice ) ;
167+ synth . Volume = ( int ) this . ChatConfig . TtsVolume ;
159168 synth . Speak ( chatMessage ) ;
160169
161170 // Now that we filled the stream, seek to the beginning so we can play it.
@@ -173,8 +182,8 @@ private void PlaySoundsThread() {
173182 this . ttsSoundOutput = new WaveOutEvent ( ) ;
174183 this . ttsSoundOutputSignal = new ManualResetEvent ( false ) ;
175184
176- this . ttsSoundOutput . DeviceNumber = NAudioUtilities . GetOutputDeviceIndex ( this . chatConfig . OutputDevice ) ;
177- this . ttsSoundOutput . Volume = this . chatConfig . TtsVolume / 100.0f ;
185+ this . ttsSoundOutput . DeviceNumber = NAudioUtilities . GetOutputDeviceIndex ( this . ChatConfig . OutputDevice ) ;
186+ this . ttsSoundOutput . Volume = this . ChatConfig . TtsVolume / 100.0f ;
178187
179188 this . ttsSoundOutput . Init ( reader ) ;
180189
@@ -191,8 +200,10 @@ private void PlaySoundsThread() {
191200 }
192201
193202 // Wait for the play to finish, we will get signaled.
203+ CurrentUsername = e . ChatMessage . Username ;
194204 var signal = this . ttsSoundOutputSignal ;
195205 signal ? . WaitOne ( ) ;
206+ CurrentUsername = null ;
196207 } finally {
197208 // Finally dispose of everything safely in the lock.
198209 lock ( this . ttsSoundOutputLock )
@@ -215,18 +226,18 @@ private void PlaySoundsThread() {
215226 /// Connects to the chat to listen for messages to read in text to speech.
216227 /// </summary>
217228 public void Connect ( ) {
218- if ( null == this . chatConfig ) {
229+ if ( null == this . ChatConfig ) {
219230 return ;
220231 }
221232
222233 var config = Configuration . Instance ;
223- var user = config . GetTwitchAccount ( this . chatConfig . AccountUsername ) ;
234+ var user = config . GetTwitchAccount ( this . ChatConfig . AccountUsername ) ;
224235 if ( null == user ) {
225236 return ;
226237 }
227238
228239 var twitchManager = TwitchChatManager . Instance ;
229- twitchManager . AddTwitchChannel ( user , this . chatConfig . TwitchChannel , this . Client_OnMessageReceived ) ;
240+ twitchManager . AddTwitchChannel ( user , this . ChatConfig . TwitchChannel , this . Client_OnMessageReceived ) ;
230241 }
231242
232243 /// <summary>
@@ -261,18 +272,13 @@ private void Client_OnMessageReceived(TwitchClient twitchClient, OnMessageReceiv
261272 }
262273 }
263274
264- /// <summary>
265- /// Handles key press anywhere in the OS.
266- /// </summary>
267- /// <param name="keyboard">The key that was pressed in string format.</param>
268- /// <remarks>
269- /// See:
270- /// <see href="https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes?redirectedfrom=MSDN" />
271- /// </remarks>
272- private void KeyboardPressCallback ( string keyboard ) {
273- if ( "123" . Equals ( keyboard , StringComparison . InvariantCultureIgnoreCase ) ) {
274- this . ttsSoundOutput ? . Stop ( ) ;
275- }
275+ public void SkipCurrentTts ( ) {
276+ this . ttsSoundOutput ? . Stop ( ) ;
277+ }
278+
279+ public void SkipAllTts ( ) {
280+ messageToSkip = this . soundsToPlay . Count ;
281+ this . ttsSoundOutput ? . Stop ( ) ;
276282 }
277283 }
278284}
0 commit comments