espeak.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. from kalliope.core.TTS.TTSModule import TTSModule, MissingTTSParameter
  2. import logging
  3. import sys
  4. import subprocess
  5. logging.basicConfig()
  6. logger = logging.getLogger("kalliope")
  7. class Espeak(TTSModule):
  8. def __init__(self, **kwargs):
  9. super(Espeak, self).__init__(language="any", **kwargs)
  10. # set parameter from what we receive from the settings
  11. self.variant = kwargs.get('variant', None)
  12. self.speed = str(kwargs.get('speed', '160'))
  13. self.amplitude = str(kwargs.get('amplitude', '100'))
  14. self.pitch = str(kwargs.get('pitch', '50'))
  15. self.espeak_exec_path = kwargs.get('path', r'/usr/bin/espeak')
  16. if self.voice == 'default' or self.voice is None:
  17. raise MissingTTSParameter("voice parameter is required by the eSpeak TTS")
  18. # if voice = default, don't add voice option to espeak
  19. if self.variant is None:
  20. self.voice_and_variant = self.voice
  21. else:
  22. self.voice_and_variant = self.voice + '+' + self.variant
  23. def say(self, words):
  24. """
  25. :param words: The sentence to say
  26. """
  27. self.generate_and_play(words, self._generate_audio_file)
  28. def _generate_audio_file(self):
  29. """
  30. Generic method used as a Callback in TTSModule
  31. - must provided the audio file and write it on the disk
  32. .. raises:: FailToLoadSoundFile
  33. """
  34. options = {
  35. 'v': '-v' + self.voice_and_variant,
  36. 's': '-s' + self.speed,
  37. 'a': '-a' + self.amplitude,
  38. 'p': '-p' + self.pitch,
  39. 'w': '-w' + self.file_path
  40. }
  41. final_command = [self.espeak_exec_path, options['v'], options['s'], options['a'],
  42. options['p'], options['w'], self.words]
  43. # generate the file with eSpeak
  44. subprocess.call(final_command, stderr=sys.stderr)