voxygen.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import logging
  2. import requests
  3. from core import FileManager
  4. from core.TTS.TTSModule import TTSModule, MissingTTSParameter, FailToLoadSoundFile
  5. logging.basicConfig()
  6. logger = logging.getLogger("kalliope")
  7. TTS_URL = "https://www.voxygen.fr/sites/all/modules/voxygen_voices/assets/proxy/index.php"
  8. TTS_TIMEOUT_SEC = 30
  9. TTS_CONTENT_TYPE = "audio/mpeg"
  10. class Voxygen(TTSModule):
  11. def __init__(self, **kwargs):
  12. # voxygen does'nt need a language. The name of the voice correspond to a lang
  13. super(Voxygen, self).__init__(language="any", **kwargs)
  14. self.voice = kwargs.get('voice', None)
  15. if self.voice is None:
  16. raise MissingTTSParameter("voice parameter is required by the Voxygen TTS")
  17. def say(self, words):
  18. self.generate_and_play(words, self._generate_audio_file)
  19. def _generate_audio_file(self):
  20. payload = self.get_payload(self.voice, self.words)
  21. # getting the mp3
  22. r = requests.get(TTS_URL, params=payload, stream=True, timeout=TTS_TIMEOUT_SEC)
  23. content_type = r.headers['Content-Type']
  24. logger.debug("Voxygen : Trying to get url: %s response code: %s and content-type: %s",
  25. r.url,
  26. r.status_code,
  27. content_type)
  28. if r.status_code == requests.codes.ok and content_type == TTS_CONTENT_TYPE:
  29. FileManager.write_in_file(self.file_path, r.content)
  30. else:
  31. logger.debug("Unable to get a valid audio file. Returned code: %s" % r.status_code)
  32. @staticmethod
  33. def get_payload(voice, words):
  34. return {
  35. "method": "redirect",
  36. "text": words,
  37. "voice": voice
  38. }