| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Speech.Synthesis;
- namespace WinTTS {
- class Program {
- static void Main(string[] args) {
- Dictionary<string, string> arguments = new Dictionary<string, string>();
- arguments.Add("general", "");
- foreach (string arg in args) {
- if (arg.StartsWith("-") || arg.StartsWith("/")) {
- string key = arg.Substring(1);
- if(key.StartsWith("-"))
- key = key.Substring(1);
- if (key.Contains('=')) {
- string[] s = key.Split('=');
- arguments.Add(s[0], s[1]);
- } else {
- arguments.Add(key, "");
- }
- } else {
- arguments["general"] += arg;
- }
- }
- SpeechSynthesizer speech = new SpeechSynthesizer();
- speech.SpeakCompleted += Speech_SpeakCompleted;
- string savePath = "";
- string lang = "";
- bool bUseSsml = false;
- string age = "";
- string gender = "";
- if (arguments.ContainsKey("voice"))
- speech.SelectVoice(arguments["voice"]);
- if (arguments.ContainsKey("v"))
- speech.SelectVoice(arguments["v"]);
-
- if (arguments.ContainsKey("path"))
- savePath = arguments["path"];
- if (arguments.ContainsKey("p"))
- savePath = arguments["p"];
- if (arguments.ContainsKey("lang"))
- lang = arguments["lang"];
- if (arguments.ContainsKey("l"))
- lang = arguments["l"];
- if(arguments.ContainsKey("age"))
- age = arguments["age"];
- if (arguments.ContainsKey("a"))
- age = arguments["a"];
- if (arguments.ContainsKey("g")) {
- if (arguments["g"].ToLower() == "f")
- gender = "female";
- else if (arguments["g"].ToLower() == "m")
- gender = "male";
- }
- bUseSsml = arguments.ContainsKey("ssml") || arguments.ContainsKey("s");
- string text = arguments["general"];
- if (bUseSsml) {
- if(gender.Length>0 || age.Length>0) {
- text = "<voice" + (age.Length > 0 ? " age=\"" + age + "\"":"") + (gender.Length > 0 ? " gender=\"" + gender + "\"":"") + "><p>" + text + "</p></voice>";
- }
- if(text.IndexOf("<speak")==-1) {
- text = "<speak xmlns=\"http://www.w3.org/2001/10/synthesis\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" version=\"1.0\" xml:lang=\"de-DE\">" + text + "</speak>";
- }
- if(!text.StartsWith("<?xml")) {
- text = "<?xml version=\"1.0\"?>" + text;
- }
- text = text.Replace("<speak>", "<speak xmlns=\"http://www.w3.org/2001/10/synthesis\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" version=\"1.0\" xml:lang=\"de-DE\">");
- } else {
- text = System.Text.RegularExpressions.Regex.Replace(text, "<.+?>", "");
- }
- System.Diagnostics.Debug.WriteLine(text);
- if (savePath != "")
- //speech.SetOutputToWaveFile(savePath, new System.Speech.AudioFormat.SpeechAudioFormatInfo(300, System.Speech.AudioFormat.AudioBitsPerSample.Sixteen, System.Speech.AudioFormat.AudioChannel.Mono));
- speech.SetOutputToWaveFile(savePath);
- if (bUseSsml) {
- speech.SpeakSsml(text);
- } else {
- speech.Speak(text);
- }
- }
- // <?xml version="1.0"?>
- // <speak xmlns="http://www.w3.org/2001/10/synthesis" xmlns:dc="http://purl.org/dc/elements/1.1/" version="1.0" xml:lang="de-DE">
- // <voice age="15">
- // Hallo, Welt! Das ist ein Debug Text.
- // </voice>
- // </speak>
- private static void Speech_SpeakCompleted(object sender, SpeakCompletedEventArgs e) {
- Environment.Exit(1);
- }
- }
- }
|