@@ -17,7 +17,7 @@ public class TwitchChatTts : IDisposable {
17
17
/// <summary>
18
18
/// The configuration for the twitch chat.
19
19
/// </summary>
20
- private readonly TwitchChatConfiguration ? chatConfig ;
20
+ public TwitchChatConfiguration ? ChatConfig { get ; }
21
21
22
22
/// <summary>
23
23
/// The collection of sounds to play.
@@ -59,6 +59,8 @@ public class TwitchChatTts : IDisposable {
59
59
/// </summary>
60
60
private ManualResetEvent ? ttsSoundOutputSignal ;
61
61
62
+ private int messageToSkip ;
63
+
62
64
/// <summary>
63
65
/// Initializes a new instance of the <see cref="TwitchChatTts" /> class.
64
66
/// </summary>
@@ -69,8 +71,7 @@ public TwitchChatTts(TwitchChatConfiguration? config) {
69
71
this . soundThread . Name = "TwitchChatTts Thread" ;
70
72
this . soundThread . IsBackground = true ;
71
73
this . soundThread . Start ( ) ;
72
- this . chatConfig = config ;
73
- GlobalKeyboardListener . Instance . Callback += this . KeyboardPressCallback ;
74
+ this . ChatConfig = config ;
74
75
}
75
76
76
77
/// <summary>
@@ -92,21 +93,23 @@ public void Dispose() {
92
93
this . ttsSoundOutput = null ;
93
94
}
94
95
95
- if ( null == this . chatConfig ) {
96
+ if ( null == this . ChatConfig ) {
96
97
return ;
97
98
}
98
99
99
- var user = Configuration . Instance . GetTwitchAccount ( this . chatConfig . AccountUsername ) ;
100
+ var user = Configuration . Instance . GetTwitchAccount ( this . ChatConfig . AccountUsername ) ;
100
101
if ( null == user ) {
101
102
return ;
102
103
}
103
104
104
105
var twitchManager = TwitchChatManager . Instance ;
105
- twitchManager . RemoveTwitchChannel ( user , this . chatConfig . TwitchChannel , this . Client_OnMessageReceived ) ;
106
+ twitchManager . RemoveTwitchChannel ( user , this . ChatConfig . TwitchChannel , this . Client_OnMessageReceived ) ;
106
107
if ( this . soundThread . Join ( 5000 ) ) {
107
108
this . soundThread . Interrupt ( ) ;
108
109
}
109
110
}
111
+
112
+ public string ? CurrentUsername { get ; set ; }
110
113
111
114
/// <summary>
112
115
/// The main thread used to play sound asynchronously.
@@ -119,8 +122,14 @@ private void PlaySoundsThread() {
119
122
return ;
120
123
}
121
124
125
+ if ( messageToSkip > 0 ) {
126
+ -- messageToSkip ;
127
+ Console . WriteLine ( $ "Skipping: { e . ChatMessage . Username } says { e . ChatMessage . Message } ") ;
128
+ continue ;
129
+ }
130
+
122
131
Console . WriteLine ( $ "Running: { e . ChatMessage . Username } says { e . ChatMessage . Message } ") ;
123
- if ( null == this . chatConfig ) {
132
+ if ( null == this . ChatConfig ) {
124
133
continue ;
125
134
}
126
135
@@ -154,8 +163,8 @@ private void PlaySoundsThread() {
154
163
using ( var stream = new MemoryStream ( ) ) {
155
164
// Setup the microsoft TTS object according to the settings.
156
165
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 ;
159
168
synth . Speak ( chatMessage ) ;
160
169
161
170
// Now that we filled the stream, seek to the beginning so we can play it.
@@ -173,8 +182,8 @@ private void PlaySoundsThread() {
173
182
this . ttsSoundOutput = new WaveOutEvent ( ) ;
174
183
this . ttsSoundOutputSignal = new ManualResetEvent ( false ) ;
175
184
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 ;
178
187
179
188
this . ttsSoundOutput . Init ( reader ) ;
180
189
@@ -191,8 +200,10 @@ private void PlaySoundsThread() {
191
200
}
192
201
193
202
// Wait for the play to finish, we will get signaled.
203
+ CurrentUsername = e . ChatMessage . Username ;
194
204
var signal = this . ttsSoundOutputSignal ;
195
205
signal ? . WaitOne ( ) ;
206
+ CurrentUsername = null ;
196
207
} finally {
197
208
// Finally dispose of everything safely in the lock.
198
209
lock ( this . ttsSoundOutputLock )
@@ -215,18 +226,18 @@ private void PlaySoundsThread() {
215
226
/// Connects to the chat to listen for messages to read in text to speech.
216
227
/// </summary>
217
228
public void Connect ( ) {
218
- if ( null == this . chatConfig ) {
229
+ if ( null == this . ChatConfig ) {
219
230
return ;
220
231
}
221
232
222
233
var config = Configuration . Instance ;
223
- var user = config . GetTwitchAccount ( this . chatConfig . AccountUsername ) ;
234
+ var user = config . GetTwitchAccount ( this . ChatConfig . AccountUsername ) ;
224
235
if ( null == user ) {
225
236
return ;
226
237
}
227
238
228
239
var twitchManager = TwitchChatManager . Instance ;
229
- twitchManager . AddTwitchChannel ( user , this . chatConfig . TwitchChannel , this . Client_OnMessageReceived ) ;
240
+ twitchManager . AddTwitchChannel ( user , this . ChatConfig . TwitchChannel , this . Client_OnMessageReceived ) ;
230
241
}
231
242
232
243
/// <summary>
@@ -261,18 +272,13 @@ private void Client_OnMessageReceived(TwitchClient twitchClient, OnMessageReceiv
261
272
}
262
273
}
263
274
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 ( ) ;
276
282
}
277
283
}
278
284
}
0 commit comments