-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
399 lines (336 loc) · 14.5 KB
/
Program.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
using Google.Cloud.TextToSpeech.V1Beta1;
using System.Globalization;
using System.Media;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Versioning;
using System.Speech.Synthesis;
using System.Text;
using System.Text.Json.Nodes;
namespace TextToSpeekListener
{
[SupportedOSPlatform("windows")]
public class TextToSpeekListener
{
// Default sample rate for creating a WAV file with google
public static readonly int INPUT_SAMPLE_RATE = 16000;
private static string GetText(JsonObject job)
{
string text = "Hello World";
var a = job["text"];
if (a != null)
{
text = a.ToString();
}
return text;
}
private static int GetProvider(JsonObject job)
{
int provider = 0;
var a = job["provider"];
if (a != null)
{
provider = Int16.Parse(a.ToString());
}
return provider;
}
private static int GetVolumeMicrosoft(JsonObject job)
{
int volume = 100;
var a = job["volume"];
if (a != null)
{
volume = Int16.Parse(a.ToString());
volume = Math.Min(100, volume);
volume = Math.Max(0, volume);
}
return volume;
}
private static int GetVolumeGoogle(JsonObject job)
{
// Google uses volume increase in DB in [-96,10] with default 0 DB.
int volume = 0;
var a = job["volume"];
if (a != null)
{
volume = Int16.Parse(a.ToString());
volume = Math.Min(10, volume);
volume = Math.Max(-96, volume);
}
return volume;
}
private static string? GetVoice(JsonObject job)
{
string? voice = null;
var a = job["voice"];
if (a != null)
{
voice = a.ToString();
}
return voice;
}
private static string? GetCulture(JsonObject job)
{
string? culture = null;
var a = job["culture"];
if (a != null)
{
culture = a.ToString();
}
return culture;
}
private static VoiceGender GetGender(JsonObject job)
{
var a = job["gender"];
if (a != null)
{
string gender = a.ToString();
if (gender.Trim().ToLower() == "male")
{
return VoiceGender.Male;
}
else if (gender.Trim().ToLower() == "neutral")
{
return VoiceGender.Neutral;
}
else
{
return VoiceGender.Female;
}
}
else
{
return VoiceGender.Female;
}
}
private static void StartListener(int listenPort = 11042, string listenHost= "127.0.0.1", bool googleAvail = false)
{
IPAddress ipaddress = IPAddress.Parse("127.0.0.1");
UdpClient listener = new(listenPort);
//IPEndPoint groupEP = new(IPAddress.Any, listenPort);
IPEndPoint groupEP = new(ipaddress, listenPort);
Console.WriteLine();
Console.WriteLine("------------------------------------------");
Console.WriteLine($"Starting listener on port {listenPort}");
Console.WriteLine("------------------------------------------");
Console.WriteLine();
// Create Speech synthesizer instance
SpeechSynthesizer synth = new();
try
{
while (true)
{
//
Console.Write("Waiting for broadcast...");
byte[] bytes = listener.Receive(ref groupEP);
// Get message as text (we assume UTF8 instead of ASCII encoding, which works better for special characters)
string? jsonString = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
// Check if this comes from MOOSE (string is hard coded as SOCKET.DataType.TTS="moose_text2speech")
bool isMoose = jsonString.Contains("moose_text2speech");
if (isMoose)
{
Console.WriteLine();
Console.WriteLine($"Received broadcast from {groupEP} :");
Console.WriteLine($"{jsonString}");
// Create a json object
var jsonObject = JsonNode.Parse(jsonString).AsObject();
//string server = "Unknown";
string? text = GetText(jsonObject);
int provider = GetProvider(jsonObject);
string? voice = GetVoice(jsonObject);
string? culture = GetCulture(jsonObject);
VoiceGender gender = GetGender(jsonObject);
// Debug output
Console.WriteLine($"Text to speech: {text}");
if (googleAvail && provider == 1)
{
// Create a google TTS client instance
var client = TextToSpeechClient.Create();
// The input to be synthesized, can be provided as text or SSML.
var input = new SynthesisInput
{
//Text = text
Ssml = text
};
VoiceSelectionParams? voiceSelection = null;
if (!string.IsNullOrEmpty(voice))
{
voiceSelection = new VoiceSelectionParams()
{
Name = voice,
// csharp_style_prefer_range_operator = false
LanguageCode = voice.Substring(0, 5),
};
}
else
{
if (culture == null)
{ culture = "en-US"; }
SsmlVoiceGender sgender = SsmlVoiceGender.Unspecified;
switch (gender)
{
case VoiceGender.Male:
sgender = SsmlVoiceGender.Male;
break;
case VoiceGender.Neutral:
sgender = SsmlVoiceGender.Neutral;
break;
case VoiceGender.Female:
sgender = SsmlVoiceGender.Female;
break;
default:
sgender = SsmlVoiceGender.Male;
break;
}
voiceSelection = new VoiceSelectionParams
{
LanguageCode = culture,
SsmlGender = sgender, // This does not seem to work correctly
};
}
// Get volume
int volume = GetVolumeGoogle(jsonObject);
// Specify the type of audio file that is created. We do a wav file and play it.
var audioConfig = new AudioConfig
{
//AudioEncoding = AudioEncoding.Mp3
AudioEncoding = AudioEncoding.Linear16, // wav file
SampleRateHertz = INPUT_SAMPLE_RATE,
VolumeGainDb = volume
};
// Perform the text-to-speech request.
var response = client.SynthesizeSpeech(input, voiceSelection, audioConfig);
// Create temmp file name
var tempFile = Path.GetTempFileName();
// Write the response to the output file.
using (var output = File.Create(tempFile))
{
response.AudioContent.WriteTo(output);
}
// Debug message
Console.WriteLine($"Speaking Google: voice={voice}, culture={culture}, gender={gender}, volumeGain={volume} DB");
//Console.WriteLine($"Audio content written to file \"{tempFile}\"");
// Play sound file
//SoundPlayer soundfile = new SoundPlayer(tempFile);
SoundPlayer soundfile = new(tempFile);
soundfile.PlaySync(); // PlaySync means that once sound start then no other activity if form will occur untill sound goes to finish
// Delete temp file
File.Delete(tempFile);
}
else
{
// Get volume
int volume = GetVolumeMicrosoft(jsonObject);
// Select voice
if (voice == null || voice.Length == 0)
{
// No explicit voice ==> select by hints (culture and/or gender)
if (culture == null)
{
//Console.WriteLine($"Selecting voice from hint gender={gender}");
// Get voice with given gender (no culture)
synth.SelectVoiceByHints(gender, VoiceAge.Adult);
}
else
{
// Get voice by given gender and culture
synth.SelectVoiceByHints(gender, VoiceAge.Adult, 0, new CultureInfo(culture, false));
}
}
else
{
// Voice has been given explicitly
try
{
synth.SelectVoice(voice);
}
catch (Exception ex)
{
Console.WriteLine($"Could not get specific voice {voice}: {ex.Message}");
}
}
// Set volume
synth.Volume = volume;
// Debug message
Console.WriteLine($"Speaking Microsoft: voice={voice}, culture={culture}, gender={gender}, volume={volume}");
// Speak text
synth.Speak(text);
}
// Write line
Console.WriteLine();
}
}
}
catch (SocketException e)
{
Console.WriteLine(e);
}
finally
{
listener.Close();
}
}
private static void ShowVoicesSystem()
{
// Initialize a new instance of the SpeechSynthesizer.
using (SpeechSynthesizer synth = new())
{
// Output information about all of the installed voices.
Console.WriteLine("Installed voices Microsoft:");
foreach (InstalledVoice voice in synth.GetInstalledVoices())
{
VoiceInfo info = voice.VoiceInfo;
Console.WriteLine($"- {info.Name}, Culture: {info.Culture}, Gender: {info.Gender}, Age: {info.Age}");
}
Console.WriteLine();
}
}
private static bool ShowVoicesGoogle()
{
try
{
var client = TextToSpeechClient.Create();
var response = client.ListVoices("en-US");
Console.WriteLine("Installed voices Google (only first 10 en-US):");
int N = 0;
foreach (var voice in response.Voices)
{
Console.WriteLine($"- {voice.Name}, Gender={voice.SsmlGender}; Culture(s): {string.Join(", ", voice.LanguageCodes)}");
N++;
if (N >= 10) break;
}
Console.WriteLine();
return true;
}
catch (Exception ex)
{
Console.WriteLine($"Error with Google Text to Speech: {ex.Message}");
Console.WriteLine();
return false;
}
}
public static void Main()
{
// Greetings
Console.WriteLine("Text-To-Speech Listener");
Console.WriteLine("=======================");
Console.WriteLine();
// Show installed system voices
ShowVoicesSystem();
// Show insalled google voices. This also checks if google is available at all
bool googleAvail = ShowVoicesGoogle();
// User input of port
Console.Write("Listen on port (hit enter for default 11042): ");
string? port = Console.ReadLine();
int listenPort = 11042;
if (port is not null && port.Length > 0)
{
listenPort = Int16.Parse(port);
}
Console.Write("Listen on host (hit enter for default \"127.0.0.1\"): ");
string? host = Console.ReadLine();
// Start UDP listener
StartListener(listenPort, host, googleAvail);
}
}
}