-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTTS.cs
More file actions
84 lines (74 loc) · 1.97 KB
/
Copy pathTTS.cs
File metadata and controls
84 lines (74 loc) · 1.97 KB
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
using KokoroSharp;
using KokoroSharp.Core;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace SSC
{
public class TTS
{
private KokoroTTS processor;
private KokoroVoice voice;
private static TTS m_tts = null;
private Task SpeechTask;
private volatile Queue<string> MessageQueue = new Queue<string>();
public static TTS GetInstance()
{
if (m_tts == null)
m_tts = new TTS();
return m_tts;
}
private TTS()
{
processor = KokoroTTS.LoadModel();
voice = KokoroVoiceManager.GetVoice("af_bella");
}
public void StripMarkdownAndEnqueue(string message)
{
message = message.Replace("<3", "");
message = message.Replace(",", "");
message = message.Replace("\"", "");
message = message.Replace("*", "");
message = message.Replace("Sia", "[Sia](/ssɪɪa/)"); //This should be I think split into words and replaced as words.
//Regex reg = new Regex("[\\*].+?[\\*]");
//message = reg.Replace(message, "");
Enqueue(message);
}
public void Enqueue(string message)
{
MessageQueue.Enqueue(message);
if (SpeechTask?.IsCompleted ?? true)
{
SpeechTask = Task.Run(async () =>
{
while (MessageQueue.Count > 0)
{
var msg = MessageQueue.Dequeue();
var handle = processor.SpeakFast(msg, voice, new KokoroSharp.Processing.KokoroTTSPipelineConfig()
{
PreprocessText = true,
SecondsOfPauseBetweenProperSegments = new KokoroSharp.Processing.PauseAfterSegmentStrategy(CommaPause: 0.09f, PeriodPause: 0.4f, ExclamationMarkPause: 0.5f, OthersPause: 0.3f),
Speed = 1.1f
});
bool cancelled = false;
bool completed = false;
handle.OnSpeechCompleted += (b) =>
{
completed = true;
};
handle.OnSpeechCanceled += (b) =>
{
cancelled = true;
};
while (!completed)
{
if (cancelled)
return;
await Task.Delay(250);
}
}
});
}
}
}
}