ShellGui.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. from dialog import Dialog
  2. import locale
  3. from core import ConfigurationManager
  4. from core import OrderListener
  5. from neurons import Say
  6. class ShellGui:
  7. def __init__(self):
  8. # get settings
  9. self.conf = ConfigurationManager().get_settings()
  10. locale.setlocale(locale.LC_ALL, '')
  11. self.d = Dialog(dialog="dialog")
  12. self.d.set_background_title("Jarvis shell UI")
  13. self.show_main_menu()
  14. def show_main_menu(self):
  15. """
  16. Main menu of the shell UI.
  17. Provide a list of action the user can select to test his settings
  18. :return:
  19. """
  20. code, tag = self.d.menu("Test your JARVIS settings from this menu",
  21. choices=[("TTS", "Text to Speech"),
  22. ("STT", "Speech to text")])
  23. if code == self.d.OK:
  24. if tag == "STT":
  25. self.show_stt_test_menu()
  26. if tag == "TTS":
  27. self.show_tts_test_menu()
  28. def show_stt_test_menu(self):
  29. # we get STT from settings
  30. stt_list = ConfigurationManager.get_stt_list()
  31. print stt_list
  32. choices = self._get_choices_tuple_from_list(stt_list)
  33. code, tag = self.d.menu("Select the STT to test:",
  34. choices=choices)
  35. if code == self.d.CANCEL:
  36. self.show_main_menu()
  37. if code == self.d.OK:
  38. print tag
  39. self.d.infobox("Please talk now")
  40. order_listener = OrderListener(callback=self.callback_stt, stt=str(tag))
  41. order_listener.load_stt_plugin()
  42. def show_tts_test_menu(self, sentence_to_test=None):
  43. """
  44. A menu for testing text to speech
  45. :return:
  46. """
  47. continue_bool = True
  48. # if we don't have yet a sentence to test, we ask the user to type one
  49. if sentence_to_test is None:
  50. # First, we ask the user to type a sentence that will be passed in the TTS
  51. code, sentence_to_test = self.d.inputbox("Please type the sentence you want to test", height=20, width=50)
  52. if code == self.d.CANCEL:
  53. self.show_main_menu()
  54. continue_bool = False
  55. if code == self.d.OK:
  56. continue_bool = True
  57. if continue_bool:
  58. # we get TTS from settings
  59. tts_list = ConfigurationManager.get_tts_list()
  60. # create a list of tuple that can be used by the dialog menu
  61. choices = self._get_choices_tuple_from_list(tts_list)
  62. code, tag = self.d.menu("Sentence to test: %s" % sentence_to_test,
  63. choices=choices)
  64. if code == self.d.CANCEL:
  65. self.show_tts_test_menu()
  66. if code == self.d.OK:
  67. self._run_tts_test(tag, sentence_to_test)
  68. # then go back to this menu with the same sentence
  69. self.show_tts_test_menu(sentence_to_test=sentence_to_test)
  70. def _run_tts_test(self, tag, sentence_to_test):
  71. """
  72. Call the TTS
  73. :param tag:
  74. :param sentence_to_test:
  75. :return:
  76. """
  77. Say(message=sentence_to_test, tts=tag)
  78. @staticmethod
  79. def _get_choices_tuple_from_list(list_to_convert):
  80. """
  81. Return a list of tup that can be used in Dialog menu
  82. :param stt_list:
  83. :return:
  84. """
  85. # create a list of tuple that can be used by the dialog menu
  86. choices = list()
  87. for el in list_to_convert:
  88. try:
  89. for name, settings in el.iteritems():
  90. print name
  91. print settings
  92. tup = (str(name), str(settings))
  93. choices.append(tup)
  94. except AttributeError:
  95. # sometime there is no settings for the STT key
  96. tup = (str(el), str("No settings"))
  97. choices.append(tup)
  98. return choices
  99. def callback_stt(self, audio):
  100. """
  101. Callback function called after the STT has finish his job
  102. :param audio: Text from the translated audio
  103. """
  104. code = self.d.msgbox("The STT engine think you said:\n %s" % audio, width=50)
  105. if code == self.d.OK:
  106. self.show_stt_test_menu()