ShellGui.py 4.5 KB

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