test.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import sys
  2. import logging
  3. from config import Config
  4. _HOTWORD_COUNT = False
  5. def _hotword_interrupt():
  6. return _HOTWORD_COUNT > 1
  7. def _hotword_callback():
  8. logging.info('Hotword callback called.')
  9. global _HOTWORD_COUNT
  10. _HOTWORD_COUNT += 1
  11. def _test_hotword_detection():
  12. logging.info('Launching hotword detector...')
  13. import snowboy.snowboydecoder
  14. detector = snowboy.snowboydecoder.HotwordDetector(Config.SNOWBOY_HOTWORD_MODEL, sensitivity=0.5)
  15. detector.start(detected_callback=_hotword_callback, interrupt_check=_hotword_interrupt)
  16. def _test_audio_in():
  17. import speech_recognition as sr
  18. with sr.Microphone() as source:
  19. recognizer = sr.Recognizer()
  20. recognizer.energy_threshold = 2000
  21. recognizer.listen(source)
  22. def _test_audio_out():
  23. import output.audio_output
  24. logging.info('Creating audio output service...')
  25. service = output.audio_output.AudioOutput()
  26. logging.info('Calling .success()...')
  27. service.success()
  28. logging.info('Calling .failure()...')
  29. service.failure()
  30. logging.info('Calling .tts("Hello World")...')
  31. service.tts('Hello World')
  32. logging.info('Done.')
  33. def run():
  34. if len(sys.argv) == 1:
  35. print '\n'.join([
  36. '1. Audio output',
  37. '2. Audio input',
  38. '3. Hotword detection',
  39. ])
  40. return
  41. choice = sys.argv[1]
  42. if choice == '1':
  43. _test_audio_out()
  44. elif choice == '2':
  45. _test_audio_in()
  46. elif choice == '3':
  47. _test_hotword_detection()
  48. if __name__ == "__main__":
  49. logging.basicConfig(stream=sys.stdout)
  50. logging.getLogger().setLevel(logging.DEBUG)
  51. run()