pico2wave.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. # the mother class TTSModule needs to know what we will generate to check the cache et generate the file path
  13. self.set_words(words)
  14. if not self.cache:
  15. # no cache, we need to generate the file
  16. self._generate_audio_file()
  17. else:
  18. # we check if the file already exist. If not we generate it with the TTS engine
  19. if not self.is_file_already_in_cache():
  20. self._generate_audio_file()
  21. # then play the generated audio file
  22. self.play_audio()
  23. def _generate_audio_file(self):
  24. pico2wave_exec_path = ["/usr/bin/pico2wave"]
  25. # pico2wave needs that the file path ends with .wav
  26. tmp_path = self.file_path+".wav"
  27. pico2wave_options = ["-l=%s" % self.language, "-w=%s" % tmp_path]
  28. final_command = list()
  29. final_command.extend(pico2wave_exec_path)
  30. final_command.extend(pico2wave_options)
  31. final_command.append(self.words)
  32. logger.debug("Pico2wave command: %s" % final_command)
  33. # generate the file with pico2wav
  34. subprocess.call(final_command, stderr=sys.stderr)
  35. # remove the extension .wav
  36. os.rename(tmp_path, self.file_path)