voxygen.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import logging
  2. import requests
  3. from core import FileManager
  4. from core.TTS.TTSModule import TTSModule, MissingTTSParameter
  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. """
  19. :param words: The sentence to say
  20. """
  21. self.generate_and_play(words, self._generate_audio_file)
  22. def _generate_audio_file(self):
  23. """
  24. Generic method used as a Callback in TTSModule
  25. - must provided the audio file and write it on the disk
  26. .. raises:: FailToLoadSoundFile
  27. """
  28. payload = self.get_payload(self.voice, self.words)
  29. # getting the mp3
  30. r = requests.get(TTS_URL, params=payload, stream=True, timeout=TTS_TIMEOUT_SEC)
  31. content_type = r.headers['Content-Type']
  32. logger.debug("Voxygen : Trying to get url: %s response code: %s and content-type: %s",
  33. r.url,
  34. r.status_code,
  35. content_type)
  36. if r.status_code == requests.codes.ok and content_type == TTS_CONTENT_TYPE:
  37. FileManager.write_in_file(self.file_path, r.content)
  38. else:
  39. logger.debug("Unable to get a valid audio file. Returned code: %s" % r.status_code)
  40. @staticmethod
  41. def get_payload(voice, words):
  42. """
  43. Generic method used load the payload used to access the remote api
  44. :return: Payload to use to access the remote api
  45. """
  46. return {
  47. "method": "redirect",
  48. "text": words,
  49. "voice": voice
  50. }