-
Notifications
You must be signed in to change notification settings - Fork 1
/
Notification.cs
496 lines (445 loc) · 18.3 KB
/
Notification.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
using System;
using System.Collections.Generic;
using System.IO;
using NAudio.Wave;
using Serilog;
namespace AbevBot
{
public class Notification
{
private static readonly TimeSpan MinimumNotificationTime = TimeSpan.FromSeconds(3);
private static readonly TimeSpan MaximumNotificationTime = TimeSpan.FromSeconds(120);
public bool Started { get; private set; }
public DateTime StartTime { get; private set; }
public bool Finished { get; private set; }
public DateTime FinishTime { get; private set; }
public bool NotPausable { get; set; }
public bool NotReplayable { get; set; }
public DateTime CreationTime { get; }
public string TextToDisplay { get; init; }
public Notifications.TextPosition TextToDisplayPosition { get; init; } = Notifications.TextPosition.TOP;
public double TextToDisplaySize { get; init; }
public string TextToRead { get; init; }
public string SoundPath { get; init; }
public string VideoPath { get; init; }
private bool VideoEnded;
private bool VideoStarted;
private bool VideoPaused;
private bool TextDisplayed;
private bool TextCleared;
private bool SoundPlayed;
private bool TTSPlayed;
private bool AudioEnded;
private bool AudioStarted;
private bool AudioPaused;
public ChannelRedemption Redemption { get; init; }
private bool KeysPressed, Keys2Pressed;
public NotificationType Type { get; init; }
public string SubType { get; init; }
public Action ExtraActionAtStartup { get; set; }
public VideoParameters VideoParams;
public DateTime StartAfter;
public string Sender { get; init; } = string.Empty;
public NotificationControl Control { get; private set; }
public Notification()
{
CreationTime = DateTime.Now;
}
public Notification(NotificationsConfig config, string[] data, ChannelRedemption redemption = null)
{
CreationTime = DateTime.Now;
Sender = data[0];
TextToDisplay = string.Format(config.TextToDisplay, data).Replace("\\n", Environment.NewLine);
TextToDisplayPosition = config.TextPosition;
TextToDisplaySize = config.TextSize;
if (config.Type == NotificationType.CHEER)
{
// For bits check the minimum amount
if (int.TryParse(data[4], out int bits))
{
if (bits >= config.MinimumBits) TextToRead = string.Format(config.TextToSpeech, data).Replace("\\n", Environment.NewLine).Replace("#", ""); // Remove '#' symbols - they are not allowed in TTS request messages
else TextToRead = string.Empty;
}
}
else { TextToRead = string.Format(config.TextToSpeech, data).Replace("\\n", Environment.NewLine).Replace("#", ""); } // Remove '#' symbols - they are not allowed in TTS request messages
SoundPath = config.SoundToPlay;
VideoPath = config.VideoToPlay;
VideoParams = config.VideoParams;
Redemption = redemption;
Type = config.Type;
MainWindow.I.Dispatcher.Invoke(new Action(() =>
{
Control = new(this);
}));
}
/// <summary> Initializes required things and starts the notification </summary>
public void Start()
{
if (Started) return;
Reset();
Started = true;
if (!VideoEnded)
{
Server.VideoEndedCounter = 0;
Server.VideoEnded = false;
}
List<ISampleProvider> sounds = new();
if (!TTSPlayed)
{
// There is TTS to play, find all of the voices in the message and split the message to be read by different voices
var sampleSounds = Notifications.GetSampleSounds();
List<string> text = new();
int index;
text.AddRange(TextToRead.Split(':'));
if (text.Count == 0) { Log.Warning("Notification has nothing to read: {text}", TextToRead); } // Nothing to read? Do nothing
else if (text.Count == 1) { NoIdeaForTheName(text[^1], ref sampleSounds, ref sounds, "StreamElements", "Brian"); } // Just text, read with default voice
else
{
// There is at least one attempt to change the voice
string voice, maybeVoice;
while (text.Count > 1)
{
// Find a space before voice name
index = text[^2].LastIndexOf(" ");
if (index <= 0) { maybeVoice = text[^2].Trim(); } // Whole text[^2] is voice name, try to get it
else { maybeVoice = text[^2].Substring(index).Trim(); } // Part of text[^2] is voice name - extract it
voice = StreamElements.GetVoice(maybeVoice);
if (voice?.Length > 0)
{
NoIdeaForTheName(text[^1].Trim(), ref sampleSounds, ref sounds, "StreamElements", voice);
text.RemoveAt(text.Count - 1);
if (index <= 0) { text.RemoveAt(text.Count - 1); }
else { text[^1] = text[^1].Substring(0, index); }
continue;
}
voice = TikTok.GetVoice(maybeVoice);
if (voice?.Length > 0)
{
NoIdeaForTheName(text[^1].Trim(), ref sampleSounds, ref sounds, "TikTok", voice);
text.RemoveAt(text.Count - 1);
if (index <= 0) { text.RemoveAt(text.Count - 1); }
else { text[^1] = text[^1].Substring(0, index); }
continue;
}
// The voice is not found, join [^2] with [^1] and ':' symbol, and remove last text that was merged
text[^2] = string.Join(':', text[^2], text[^1]);
text.RemoveAt(text.Count - 1);
}
if (text.Count == 1)
{
// A text to read is left, so no voice is found for it, read it with default voice
// It may be that there was a voice change after a voice change - try to find the voice with remaining text
if (text[0].Trim().Length > 0)
{
maybeVoice = text[0].Trim();
voice = StreamElements.GetVoice(maybeVoice);
if (voice?.Length == 0) { voice = TikTok.GetVoice(maybeVoice); }
if (voice is null || voice?.Length == 0) { NoIdeaForTheName(text[0].Trim(), ref sampleSounds, ref sounds, "StreamElements", "Brian"); }
else { } // The remaining part was also a voice change so, do nothing? May add null to sounds but what's the point of it?
}
}
else if (text.Count != 0)
{
// Something bad happened
Log.Warning("Something bad happened with TTS generation");
}
}
}
if (!SoundPlayed) { Audio.AddToSampleProviderList(SoundPath, ref sounds, 0); }
// Create sound to play from samples
if (sounds.Count > 0)
{
Server.AudioEndedCounter = 0;
Server.AudioEnded = false;
Server.CurrentAudio = Audio.GetSoundData(sounds);
AudioEnded = false;
}
else
{
Server.CurrentAudio = null;
AudioEnded = true;
}
if (ExtraActionAtStartup != null) ExtraActionAtStartup();
StartTime = DateTime.Now;
}
/// <summary> Update status of playing notification. </summary>
/// <returns> <value>true</value> if notification ended. </returns>
public bool Update()
{
if (!Started) return false;
if (DateTime.Now - StartTime > MaximumNotificationTime)
{
Log.Warning("Maximum notification time reached, something went wrong, to not block other notificaitons force closing this one!");
Stop();
return true;
}
// Press keys
if (Redemption != null)
{
if (!KeysPressed)
{
KeysPressed = true;
if (Redemption.KeysToPressType == KeyActionType.PRESS)
{
for (int i = 0; i < Redemption.KeysToPress.Count; i++) Simulation.Keyboard.Press(Redemption.KeysToPress[i]);
for (int i = Redemption.KeysToPress.Count - 1; i >= 0; i--) Simulation.Keyboard.Release(Redemption.KeysToPress[i]);
}
else if (Redemption.KeysToPressType == KeyActionType.TYPE)
{
for (int i = 0; i < Redemption.KeysToPress.Count; i++) Simulation.Keyboard.Type(Redemption.KeysToPress[i]);
}
}
if (!Keys2Pressed)
{
if (DateTime.Now - StartTime >= Redemption.TimeToPressSecondAction)
{
Keys2Pressed = true;
// Press keys
if (Redemption.KeysToPressAfterTimeType == KeyActionType.PRESS)
{
for (int i = 0; i < Redemption.KeysToPressAfterTime.Count; i++) Simulation.Keyboard.Press(Redemption.KeysToPressAfterTime[i]);
for (int i = Redemption.KeysToPressAfterTime.Count - 1; i >= 0; i--) Simulation.Keyboard.Release(Redemption.KeysToPressAfterTime[i]);
}
else if (Redemption.KeysToPressAfterTimeType == KeyActionType.TYPE)
{
for (int i = 0; i < Redemption.KeysToPressAfterTime.Count; i++) Simulation.Keyboard.Type(Redemption.KeysToPressAfterTime[i]);
}
}
}
}
if (!VideoEnded)
{
if (!VideoStarted && !Notifications.SkipNotification)
{
// Start the video
VideoStarted = true;
Server.PlayVideo(VideoPath,
VideoParams != null ? (float)VideoParams.Left : -1f,
VideoParams != null ? (float)VideoParams.Top : -1f,
VideoParams != null ? (float)VideoParams.Width : -1f,
VideoParams != null ? (float)VideoParams.Height : -1f,
Config.VolumeVideo);
}
else if (Notifications.SkipNotification)
{
Server.ClearVideo();
VideoEnded = true;
}
else if (Notifications.NotificationsPaused && !NotPausable)
{
if (!VideoPaused)
{
VideoPaused = true;
Server.Pause();
}
}
else if (VideoPaused)
{
VideoPaused = false;
Server.Resume();
}
else if (Server.VideoEnded) { VideoEnded = true; }
}
// Display text
if (!TextDisplayed && !TextCleared && (!Notifications.NotificationsPaused || NotPausable) && !Notifications.SkipNotification)
{
TextDisplayed = true;
Server.DisplayText(TextToDisplay, TextToDisplayPosition, TextToDisplaySize);
}
else if (TextDisplayed && Notifications.NotificationsPaused && !NotPausable)
{
TextDisplayed = false;
Server.ClearText();
}
if (!VideoEnded) return false;
// Clear displayed text
if (!TextCleared && (DateTime.Now - StartTime >= MinimumNotificationTime))
{
TextCleared = true;
Server.ClearText();
}
// Play the audio
if (!AudioEnded)
{
if (Server.AudioEnded)
{
// Server is reporting that audio has finished
AudioEnded = true;
}
else if (Notifications.SkipNotification)
{
// Skip notification active - stop current audio and clear the queue
Server.ClearAudio();
}
else if (Notifications.NotificationsPaused && !NotPausable && !AudioPaused)
{
// Pause notification active and the sound is playing - pause it
AudioPaused = true;
Server.Pause();
}
else if (!Notifications.NotificationsPaused && AudioPaused)
{
// Pause notification not active and the sound is not playing - play it
AudioPaused = false;
Server.Resume();
}
else if (!AudioStarted)
{
// Play the audio
AudioStarted = true;
Server.PlayAudio(Config.VolumeAudio);
}
if (!AudioEnded) { return false; }
}
// Check if keys was pressed
if (!KeysPressed || !Keys2Pressed) return false;
// The notification is over, clear after it
if (!Notifications.SkipNotification && (DateTime.Now - StartTime < MinimumNotificationTime)) return false;
Stop(); // Just to be sure that everything is cleared out
return true; // return true when notification has ended
}
/// <summary> Stops the notification. </summary>
public void Stop()
{
if (!Started) return;
Server.ClearAll();
FinishTime = DateTime.Now;
Finished = true;
}
// FIXME: Figure out good method name :D
// It searches for sound samples in provided text,
// splits the text to parts that have to be read, and parts that should play sound sample
// It also inserts new sounds at the beginning of provided sounds array
// GettoTTSoAndoInsertoSamplesoAndoMergoWithoProvidedoSoundeso??
private void NoIdeaForTheName(string _text, ref Dictionary<string, FileInfo> sampleSounds, ref List<ISampleProvider> sounds, string supplier, string voice)
{
string text = _text;
string maybeSample;
int index = -1, nextIndex;
bool maybeUpSample, maybeDownSample;
List<ISampleProvider> newAudio = new();
while (text.Length > 0)
{
index = text.IndexOf('-', index + 1);
maybeUpSample = false;
maybeDownSample = false;
if (index >= 0)
{
nextIndex = text.IndexOf(" ", index); // Space index (end of word after '-' symbol)
if (nextIndex > index || nextIndex == -1)
{
// We got indexes between a word that starts with '-' symbol is placed (-1 means to the end of the text)
maybeSample = text.Substring(index + 1, nextIndex > 0 ? (nextIndex - index - 1) : (text.Length - index - 1));
maybeSample = maybeSample.Trim().ToLower();
// Skip symbols: !, ., ,, :, etc. - characters between 33 and 47 in ASCII
for (int i = 0; i < maybeSample.Length; i++)
{
var c = maybeSample[i];
if (c >= 33 && c <= 47)
{
// Found first symbol
maybeSample = maybeSample[..i];
break;
}
}
// Check if the sample exist in samples list
if (sampleSounds.TryGetValue(maybeSample, out var sample))
{
// Add text before the sample to be read
if (supplier.Equals("StreamElements")) { Audio.AddToSampleProviderList(StreamElements.GetTTS(text[..index].Trim(), voice), ref newAudio); }
else if (supplier.Equals("TikTok")) { Audio.AddToSampleProviderList(TikTok.GetTTS(text[..index].Trim(), voice), ref newAudio); }
else { Log.Warning("TTS supplier {supplier} not recognized!", supplier); }
// Add sample sound
Audio.AddToSampleProviderList(sample, ref newAudio);
// Remove already parsed text
if (nextIndex == -1) { text = string.Empty; } // Already reached the end
else { text = text[nextIndex..]; }
index = -1;
}
else
{
// Maybe it was -upsample or -downsample?
if (maybeSample.StartsWith("up"))
{
maybeUpSample = true;
maybeSample = maybeSample[2..]; // 2 == "up".Length
}
else if (maybeSample.StartsWith("down"))
{
maybeDownSample = true;
maybeSample = maybeSample[4..]; // 2 == "down".Length
}
if (maybeUpSample || maybeDownSample)
{
// Check again if sample without "up" / "down" prefix exist
if (sampleSounds.ContainsKey(maybeSample.Trim().ToLower()))
{
// Add text before the sample to be read
if (supplier.Equals("StreamElements")) { Audio.AddToSampleProviderList(StreamElements.GetTTS(text[..index].Trim(), voice), ref newAudio); }
else if (supplier.Equals("TikTok")) { Audio.AddToSampleProviderList(TikTok.GetTTS(text[..index].Trim(), voice), ref newAudio); }
else { Log.Warning("TTS supplier {supplier} not recognized!", supplier); }
// Add sample sound
if (maybeUpSample) { Audio.AddToSampleProviderList(Audio.GetUpOrDownSample(sampleSounds[maybeSample], false), ref newAudio); }
else if (maybeDownSample) { Audio.AddToSampleProviderList(Audio.GetUpOrDownSample(sampleSounds[maybeSample], true), ref newAudio); }
// Remove already parsed text
if (nextIndex == -1) { text = string.Empty; } // Already reached the end
else { text = text[nextIndex..]; }
index = -1;
}
}
}
}
}
else
{
// No sample found, add text to be read, clear the remainder of the text
if (supplier.Equals("StreamElements")) { Audio.AddToSampleProviderList(StreamElements.GetTTS(text.Trim(), voice), ref newAudio); }
else if (supplier.Equals("TikTok")) { Audio.AddToSampleProviderList(TikTok.GetTTS(text.Trim(), voice), ref newAudio); }
else { Log.Warning("TTS supplier {supplier} not recognized!", supplier); }
text = string.Empty;
}
}
// Insert new audio to sounds list
for (int i = newAudio.Count - 1; i >= 0; i--)
{
if (newAudio[i] is null) { Log.Warning("Some TTS request returned null audio player!"); }
else { sounds.Insert(0, newAudio[i]); }
}
}
public void UpdateControl()
{
if (Control is null)
{
MainWindow.I.Dispatcher.Invoke(new Action(() =>
{
Control = new(this);
}));
}
Control.Update();
}
// Sets the notification as dummy notification - already started and finished.
public void Dummy()
{
Finished = Started = true;
FinishTime = StartTime = DateTime.Now;
NotReplayable = true;
}
// Resets the notification as it never played.
public void Reset()
{
Finished = Started = false;
TextDisplayed = TextToDisplay is null || TextToDisplay.Length == 0;
TextCleared = TextDisplayed;
VideoEnded = VideoPath is null || VideoPath.Length == 0;
SoundPlayed = SoundPath is null || SoundPath.Length == 0;
TTSPlayed = TextToRead is null || TextToRead.Length == 0;
KeysPressed = Redemption is null || Redemption.KeysToPress.Count == 0;
Keys2Pressed = Redemption is null || Redemption.KeysToPressAfterTime.Count == 0;
VideoStarted = false;
VideoPaused = false;
AudioStarted = false;
AudioPaused = false;
NotPausable = false;
}
}
}