-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
320 lines (296 loc) · 13.3 KB
/
Copy pathProgram.cs
File metadata and controls
320 lines (296 loc) · 13.3 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
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
using System.Speech.Synthesis;
using System.Runtime.InteropServices;
// Simple option record
record Options(
string? FilePath,
string? Voice,
int? Rate,
int? Volume,
string? SavePath,
bool ListOnly,
bool Help,
bool UseAzure,
string? AzureRegion,
string? AzureKey,
string? AzureFormat,
string? SsmlPath
);
internal class Program
{
private static void Main(string[] args)
{
var options = ParseArgs(args);
if (options.Help)
{
PrintHelp();
return;
}
if (options.UseAzure)
{
RunAzureAsync(options).GetAwaiter().GetResult();
return;
}
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Console.Error.WriteLine("Windows-only mode selected (System.Speech) but OS is not Windows. Use --azure instead.");
return;
}
using var synth = new SpeechSynthesizer();
var voices = synth.GetInstalledVoices().ToList();
if (!voices.Any())
{
Console.Error.WriteLine("No installed voices were found.");
return;
}
if (options.ListOnly)
{
ListVoices(voices);
return;
}
// Select voice: precedence order -> explicit --voice -> interactive
if (!TrySelectVoice(synth, voices, options.Voice))
{
// Interactive selection if no valid voice specified
ListVoices(voices);
Console.Write("Select a voice index: ");
var input = Console.ReadLine();
if (!TrySelectVoice(synth, voices, input))
{
Console.WriteLine("Defaulting to first voice: {0}", voices[0].VoiceInfo.Name);
synth.SelectVoice(voices[0].VoiceInfo.Name);
}
}
// Rate and Volume
if (options.Rate is int rate)
{
synth.Rate = Math.Clamp(rate, -10, 10);
}
if (options.Volume is int volume)
{
synth.Volume = Math.Clamp(volume, 0, 100);
}
// Determine text source
var fileToRead = options.FilePath ?? Path.Combine(Directory.GetCurrentDirectory(), "README.md");
if (!File.Exists(fileToRead))
{
Console.Error.WriteLine($"File not found: {fileToRead}");
return;
}
string text;
try
{
text = File.ReadAllText(fileToRead);
}
catch (Exception ex)
{
Console.Error.WriteLine($"Failed to read file: {ex.Message}");
return;
}
// If saving to wav requested
if (!string.IsNullOrWhiteSpace(options.SavePath))
{
try
{
var savePath = Path.GetFullPath(options.SavePath);
var dir = Path.GetDirectoryName(savePath);
if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir)) Directory.CreateDirectory(dir);
// First pass: write to wave file only
synth.SetOutputToWaveFile(savePath);
synth.Speak(text);
synth.Speak("Audio has been written to the specified wave file.");
synth.SetOutputToDefaultAudioDevice();
Console.WriteLine($"Saved audio to {savePath}");
}
catch (Exception ex)
{
Console.Error.WriteLine($"Failed to save WAV file: {ex.Message}");
}
}
// Always speak to default device (second pass) unless user explicitly disabled? (Not implemented – always speak.)
synth.SetOutputToDefaultAudioDevice();
synth.Speak(text);
synth.Speak("Sample complete.");
}
private static bool TrySelectVoice(SpeechSynthesizer synth, List<InstalledVoice> voices, string? selector)
{
if (string.IsNullOrWhiteSpace(selector)) return false;
selector = selector.Trim();
// Index?
if (int.TryParse(selector, out var idx))
{
if (idx >= 0 && idx < voices.Count)
{
synth.SelectVoice(voices[idx].VoiceInfo.Name);
return true;
}
return false;
}
// Name match (case-insensitive contains or equals)
var match = voices.FirstOrDefault(v => v.VoiceInfo.Name.Equals(selector, StringComparison.OrdinalIgnoreCase))
?? voices.FirstOrDefault(v => v.VoiceInfo.Name.Contains(selector, StringComparison.OrdinalIgnoreCase));
if (match != null)
{
synth.SelectVoice(match.VoiceInfo.Name);
return true;
}
return false;
}
private static void ListVoices(List<InstalledVoice> voices)
{
for (int i = 0; i < voices.Count; i++)
{
var info = voices[i].VoiceInfo;
Console.WriteLine($"[{i}] {info.Name} | Culture={info.Culture} | Gender={info.Gender} | Age={info.Age}");
}
}
private static Options ParseArgs(string[] args)
{
string? file = null; string? voice = null; int? rate = null; int? volume = null; string? save = null; bool list = false; bool help = false;
bool useAzure = false; string? region = Environment.GetEnvironmentVariable("AZURE_SPEECH_REGION"); string? key = Environment.GetEnvironmentVariable("AZURE_SPEECH_KEY"); string? format = null; string? ssml = null;
for (int i = 0; i < args.Length; i++)
{
var a = args[i];
switch (a)
{
case "--file":
case "-f":
if (i + 1 < args.Length) file = args[++i];
break;
case "--voice":
case "-v":
if (i + 1 < args.Length) voice = args[++i];
break;
case "--rate":
case "-r":
if (i + 1 < args.Length && int.TryParse(args[i + 1], out var r)) { rate = r; i++; }
break;
case "--volume":
case "--vol":
if (i + 1 < args.Length && int.TryParse(args[i + 1], out var vol)) { volume = vol; i++; }
break;
case "--save":
case "-s":
if (i + 1 < args.Length) save = args[++i];
break;
case "--list":
case "-l":
list = true;
break;
case "--help":
case "-h":
case "-?":
help = true;
break;
case "--azure":
useAzure = true;
break;
case "--azure-region":
if (i + 1 < args.Length) region = args[++i];
break;
case "--azure-key":
if (i + 1 < args.Length) key = args[++i];
break;
case "--audio-format":
if (i + 1 < args.Length) format = args[++i];
break;
case "--ssml":
if (i + 1 < args.Length) ssml = args[++i];
break;
}
}
return new Options(file, voice, rate, volume, save, list, help, useAzure, region, key, format, ssml);
}
private static void PrintHelp()
{
Console.WriteLine(@"Usage: speak [options]
Options:
-f, --file <path> File to read (default: README.md in current directory)
-v, --voice <name|idx> Voice name (full/partial, case-insensitive) or index
-r, --rate <int> Speaking rate (-10..10)
--volume <0-100> Output volume percentage (0..100)
-s, --save <wav> Save spoken text to specified WAV file (in addition to live playback)
-l, --list List installed voices and exit
-h, --help Show this help and exit
Examples:
speak --list
speak --voice 1 --file intro.txt
speak --voice 1 --rate 2 --volume 80
speak --voice ""Microsoft Zira"" --rate 2 --volume 80 --save out/readme.wav
Azure (cross-platform) examples:
speak --azure --file README.md --voice en-US-AriaNeural
speak --azure --ssml aria.ssml --audio-format riff-24khz-16bit-mono-pcm
Environment variables (fallback): AZURE_SPEECH_REGION, AZURE_SPEECH_KEY");
}
// Azure Speech integration
private static async Task RunAzureAsync(Options options)
{
// Lazy load to avoid needing Microsoft.CognitiveServices.Speech on non-Azure path if trimmed
var region = options.AzureRegion ?? Environment.GetEnvironmentVariable("AZURE_SPEECH_REGION");
var key = options.AzureKey ?? Environment.GetEnvironmentVariable("AZURE_SPEECH_KEY");
if (string.IsNullOrWhiteSpace(region) || string.IsNullOrWhiteSpace(key))
{
Console.Error.WriteLine("Azure Speech requires region and key (--azure-region/--azure-key or env vars AZURE_SPEECH_REGION/AZURE_SPEECH_KEY).");
return;
}
string? text = null;
if (!string.IsNullOrEmpty(options.SsmlPath))
{
if (!File.Exists(options.SsmlPath)) { Console.Error.WriteLine("SSML file not found: " + options.SsmlPath); return; }
text = await File.ReadAllTextAsync(options.SsmlPath);
}
else
{
var file = options.FilePath ?? Path.Combine(Directory.GetCurrentDirectory(), "README.md");
if (!File.Exists(file)) { Console.Error.WriteLine("File not found: " + file); return; }
text = await File.ReadAllTextAsync(file);
}
if (string.IsNullOrWhiteSpace(text)) { Console.Error.WriteLine("No input text."); return; }
try
{
var config = Microsoft.CognitiveServices.Speech.SpeechConfig.FromSubscription(key, region);
if (!string.IsNullOrWhiteSpace(options.Voice)) config.SpeechSynthesisVoiceName = options.Voice;
if (options.Rate is int r) config.SetProperty("SpeechSynthesis:Rate", r.ToString());
if (options.Volume is int v) config.SetProperty("SpeechSynthesis:Volume", v.ToString());
if (!string.IsNullOrWhiteSpace(options.AzureFormat)) config.SetSpeechSynthesisOutputFormat(ParseFormat(options.AzureFormat));
using var result = await SynthesizeAsync(config, text, isSsml: !string.IsNullOrEmpty(options.SsmlPath));
if (result.Reason == Microsoft.CognitiveServices.Speech.ResultReason.SynthesizingAudioCompleted)
{
Console.WriteLine("Synthesis completed. Audio length: {0} bytes", result.AudioData.Length);
if (!string.IsNullOrWhiteSpace(options.SavePath))
{
var path = Path.GetFullPath(options.SavePath!);
var dir = Path.GetDirectoryName(path);
if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir)) Directory.CreateDirectory(dir);
await File.WriteAllBytesAsync(path, result.AudioData);
Console.WriteLine("Saved: " + path);
}
}
else if (result.Reason == Microsoft.CognitiveServices.Speech.ResultReason.Canceled)
{
var details = Microsoft.CognitiveServices.Speech.CancellationDetails.FromResult(result);
Console.Error.WriteLine("Synthesis canceled: " + details.Reason + " | " + details.ErrorDetails);
}
}
catch (Exception ex)
{
Console.Error.WriteLine("Azure synthesis failed: " + ex.Message);
}
}
private static async Task<Microsoft.CognitiveServices.Speech.SpeechSynthesisResult> SynthesizeAsync(Microsoft.CognitiveServices.Speech.SpeechConfig config, string content, bool isSsml)
{
using var synthesizer = new Microsoft.CognitiveServices.Speech.SpeechSynthesizer(config, null);
return isSsml ? await synthesizer.SpeakSsmlAsync(content) : await synthesizer.SpeakTextAsync(content);
}
private static Microsoft.CognitiveServices.Speech.SpeechSynthesisOutputFormat ParseFormat(string format)
{
// Map a few common shorthand values; fall back to default if not matched.
return format.ToLowerInvariant() switch
{
"raw-16khz" => Microsoft.CognitiveServices.Speech.SpeechSynthesisOutputFormat.Raw16Khz16BitMonoPcm,
"raw-24khz" => Microsoft.CognitiveServices.Speech.SpeechSynthesisOutputFormat.Raw24Khz16BitMonoPcm,
"riff-24khz-16bit-mono-pcm" => Microsoft.CognitiveServices.Speech.SpeechSynthesisOutputFormat.Riff24Khz16BitMonoPcm,
"riff-16khz-16bit-mono-pcm" => Microsoft.CognitiveServices.Speech.SpeechSynthesisOutputFormat.Riff16Khz16BitMonoPcm,
_ => Microsoft.CognitiveServices.Speech.SpeechSynthesisOutputFormat.Riff24Khz16BitMonoPcm
};
}
}