Program.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Speech.Synthesis;
  7. namespace WinTTS {
  8. class Program {
  9. static void Main(string[] args) {
  10. Dictionary<string, string> arguments = new Dictionary<string, string>();
  11. arguments.Add("general", "");
  12. foreach (string arg in args) {
  13. if (arg.StartsWith("-") || arg.StartsWith("/")) {
  14. string key = arg.Substring(1);
  15. if(key.StartsWith("-"))
  16. key = key.Substring(1);
  17. if (key.Contains('=')) {
  18. string[] s = key.Split('=');
  19. arguments.Add(s[0], s[1]);
  20. } else {
  21. arguments.Add(key, "");
  22. }
  23. } else {
  24. arguments["general"] += arg;
  25. }
  26. }
  27. SpeechSynthesizer speech = new SpeechSynthesizer();
  28. speech.SpeakCompleted += Speech_SpeakCompleted;
  29. string savePath = "";
  30. string lang = "";
  31. bool bUseSsml = false;
  32. string age = "";
  33. string gender = "";
  34. if (arguments.ContainsKey("voice"))
  35. speech.SelectVoice(arguments["voice"]);
  36. if (arguments.ContainsKey("v"))
  37. speech.SelectVoice(arguments["v"]);
  38. if (arguments.ContainsKey("path"))
  39. savePath = arguments["path"];
  40. if (arguments.ContainsKey("p"))
  41. savePath = arguments["p"];
  42. if (arguments.ContainsKey("lang"))
  43. lang = arguments["lang"];
  44. if (arguments.ContainsKey("l"))
  45. lang = arguments["l"];
  46. if(arguments.ContainsKey("age"))
  47. age = arguments["age"];
  48. if (arguments.ContainsKey("a"))
  49. age = arguments["a"];
  50. if (arguments.ContainsKey("g")) {
  51. if (arguments["g"].ToLower() == "f")
  52. gender = "female";
  53. else if (arguments["g"].ToLower() == "m")
  54. gender = "male";
  55. }
  56. bUseSsml = arguments.ContainsKey("ssml") || arguments.ContainsKey("s");
  57. string text = arguments["general"];
  58. if (bUseSsml) {
  59. if(gender.Length>0 || age.Length>0) {
  60. text = "<voice" + (age.Length > 0 ? " age=\"" + age + "\"":"") + (gender.Length > 0 ? " gender=\"" + gender + "\"":"") + "><p>" + text + "</p></voice>";
  61. }
  62. if(text.IndexOf("<speak")==-1) {
  63. 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>";
  64. }
  65. if(!text.StartsWith("<?xml")) {
  66. text = "<?xml version=\"1.0\"?>" + text;
  67. }
  68. 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\">");
  69. } else {
  70. text = System.Text.RegularExpressions.Regex.Replace(text, "<.+?>", "");
  71. }
  72. System.Diagnostics.Debug.WriteLine(text);
  73. if (savePath != "")
  74. //speech.SetOutputToWaveFile(savePath, new System.Speech.AudioFormat.SpeechAudioFormatInfo(300, System.Speech.AudioFormat.AudioBitsPerSample.Sixteen, System.Speech.AudioFormat.AudioChannel.Mono));
  75. speech.SetOutputToWaveFile(savePath);
  76. if (bUseSsml) {
  77. speech.SpeakSsml(text);
  78. } else {
  79. speech.Speak(text);
  80. }
  81. }
  82. // <?xml version="1.0"?>
  83. // <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">
  84. // <voice age="15">
  85. // Hallo, Welt! Das ist ein Debug Text.
  86. // </voice>
  87. // </speak>
  88. private static void Speech_SpeakCompleted(object sender, SpeakCompletedEventArgs e) {
  89. Environment.Exit(1);
  90. }
  91. }
  92. }