SpeechExecute.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Speech.text;
  5. using System.Diagnostics;
  6. namespace Speech {
  7. public static class SpeechExecute {
  8. public static SpeechCache Cache = new SpeechCache();
  9. public static tts.ISpeechSystem SpeechSystem;
  10. public static play.IPlaySystem PlaySystem;
  11. public static bool isGenerating = false;
  12. public static bool isPlaying = false;
  13. private static SpeechCacheEntry entry;
  14. public static string voice;
  15. public static int freq;
  16. public static string device;
  17. public static void Init() {
  18. Cache.Load("cache/speech.cah");
  19. }
  20. public static void Say(string s, bool bRaw = false) {
  21. Say(s, eMood.None, false);
  22. }
  23. public static bool Say(string s, eMood Mood , bool bRaw = false) {
  24. string formated = SpeechFormat.Format(s);
  25. string pronounced = formated;
  26. System.Diagnostics.Debug.WriteLine("Say('"+s+"', "+Mood+")");
  27. if (isPlaying || isGenerating) {
  28. System.Diagnostics.Debug.WriteLine("- Failed");
  29. if (isGenerating) System.Diagnostics.Debug.WriteLine(" - Already generating");
  30. if (isPlaying) System.Diagnostics.Debug.WriteLine(" - Is already playing");
  31. return false;
  32. }
  33. SpeechExecute.isGenerating = true;
  34. SpeechExecute.isPlaying = true;
  35. entry = Cache.find(formated);
  36. if (entry == null) {
  37. if (!bRaw) {
  38. pronounced = SpeechFormat.Pronounce(pronounced);
  39. }
  40. System.Diagnostics.Debug.WriteLine("Generate TTS");
  41. entry = Cache.Add(formated, Mood);
  42. entry.LastAccess = DateTime.Now;
  43. System.Diagnostics.Debug.WriteLine("- Cache FileName: " + entry.File);
  44. Process p = SpeechSystem.GenerateTTS(entry.File, voice, "de-DE", pronounced);
  45. Console.WriteLine("Generate TTS: '" + pronounced + "'");
  46. //p.Exited += Generate_Exited;
  47. p.Start();
  48. p.WaitForExit();
  49. } else {
  50. System.Diagnostics.Debug.WriteLine("- Cache");
  51. entry.LastAccess = DateTime.Now;
  52. }
  53. SpeechExecute.isGenerating = false;
  54. Cache.Save("cache/speech.cah");
  55. Play();
  56. return true;
  57. }
  58. private static void Play() {
  59. Process p = PlaySystem.Play(entry.File, device, freq);
  60. p.Start();
  61. p.WaitForExit();
  62. SpeechExecute.isPlaying = false;
  63. }
  64. }
  65. }