pico2wave.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import os
  2. import subprocess
  3. from core.TTS.TTSModule import TTSModule
  4. import logging
  5. import sys
  6. logging.basicConfig()
  7. logger = logging.getLogger("kalliope")
  8. class Pico2wave(TTSModule):
  9. def __init__(self, **kwargs):
  10. super(Pico2wave, self).__init__(**kwargs)
  11. def say(self, words):
  12. """
  13. :param words: The sentence to say
  14. """
  15. self.generate_and_play(words, self._generate_audio_file)
  16. def _generate_audio_file(self):
  17. """
  18. Generic method used as a Callback in TTSModule
  19. - must provided the audio file and write it on the disk
  20. .. raises:: FailToLoadSoundFile
  21. """
  22. pico2wave_exec_path = ["/usr/bin/pico2wave"]
  23. # pico2wave needs that the file path ends with .wav
  24. tmp_path = self.file_path+".wav"
  25. pico2wave_options = ["-l=%s" % self.language, "-w=%s" % tmp_path]
  26. final_command = list()
  27. final_command.extend(pico2wave_exec_path)
  28. final_command.extend(pico2wave_options)
  29. final_command.append(self.words)
  30. logger.debug("Pico2wave command: %s" % final_command)
  31. # generate the file with pico2wav
  32. subprocess.call(final_command, stderr=sys.stderr)
  33. # remove the extension .wav
  34. os.rename(tmp_path, self.file_path)