pico2wave.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import subprocess
  2. from tts import TTS
  3. import logging
  4. import sys
  5. logging.basicConfig()
  6. logger = logging.getLogger("jarvis")
  7. class Pico2wave(TTS):
  8. PICO2WAVE_LANGUAGES = dict(fr="fr-FR", us="en-US", uk="en-GB", de="de-DE", es="es-ES", it="it-IT")
  9. PICO2WAVE_LANGUAGES_DEFAULT = 'fr'
  10. def __init__(self, audio_player_type=None):
  11. """
  12. :param audio_player_type: MP3 or WAV
  13. """
  14. TTS.__init__(self, audio_player_type, "wav")
  15. def say(self, words=None, language=PICO2WAVE_LANGUAGES_DEFAULT, cache=False):
  16. file_path = self.cache.get_audio_file_cache_path(words, language)
  17. language = self.get_voice(language)
  18. self.get_audio(words, language, file_path)
  19. self.play_audio(file_path, cache=cache)
  20. def get_voice(self, language):
  21. logger.debug("Pico2wave asked lang: %s" % language)
  22. language = self.unify_key(language)
  23. if language in self.PICO2WAVE_LANGUAGES:
  24. return self.PICO2WAVE_LANGUAGES[language]
  25. logger.debug("Cannot find language matching language: %s replace by default voice: %s", language, self.PICO2WAVE_LANGUAGES_DEFAULT)
  26. return self.PICO2WAVE_LANGUAGES_DEFAULT
  27. @staticmethod
  28. def get_audio(words, language, file_path):
  29. subprocess.check_output(["/usr/bin/pico2wave", "-l=%s" % language, "-w=%s" % file_path, words], stderr=sys.stderr)