ShellGui.py 4.4 KB

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