voxygen.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import hashlib
  2. import os
  3. import shutil
  4. import pygame
  5. import requests
  6. import logging
  7. import sys
  8. from core import AudioPlayer
  9. VOXYGEN_LANGUAGES = dict(
  10. fr=dict(electra="Electra", emma="Emma", becool="Becool", agnes="Agnes", loic="Loic", fabienne="Fabienne", helene="Helene", marion="Marion", matteo="Matteo",
  11. melodine="Melodine", mendoo="Mendoo", michel="Michel", moussa="Moussa", philippe="Philippe", sorciere="Sorciere"),
  12. ar=dict(adel="Adel"),
  13. de=dict(matthias="Matthias", jylvia="Sylvia"),
  14. uk=dict(bronwen="Bronwen", elizabeth="elizabeth", judith="Judith", paul="Paul", witch="Witch"),
  15. us=dict(bruce="Bruce", jenny="Jenny"),
  16. es=dict(martha="Martha"),
  17. it=dict(sonia="Sonia"))
  18. VOXYGEN_VOICE_DEFAULT = "Michel"
  19. VOXYGEN_LANGUAGES_DEFAULT = "default"
  20. VOXYGEN_URL = "https://www.voxygen.fr/sites/all/modules/voxygen_voices/assets/proxy/index.php"
  21. VOXYGEN_CONTENT_TYPE = "audio/mpeg"
  22. VOXYGEN_TIMEOUT_SEC = 30
  23. CACHE_PATH = "/tmp/jarvis/tts/voxygen"
  24. CACHE_EXTENSION = ".tts"
  25. def say(words=None, voice=None, language=VOXYGEN_LANGUAGES_DEFAULT, cache=True):
  26. voice = get_voice(voice, language)
  27. file_path = get_file_path(words, voice, language)
  28. if get_audio(voice, words, file_path, cache):
  29. AudioPlayer.play_audio(file_path, keep_file=cache)
  30. def get_file_path(words, voice, language):
  31. md5 = hashlib.md5(words).hexdigest()
  32. filename = voice + "." + md5 + CACHE_EXTENSION
  33. cache_directory = os.path.join(CACHE_PATH, language)
  34. file_path = os.path.join(cache_directory, filename)
  35. create_directory(cache_directory)
  36. logging.debug("Cache directory %s exists and File path for audio is: %s", cache_directory, file_path)
  37. return file_path
  38. def get_voice(voice, language):
  39. if language in VOXYGEN_LANGUAGES and voice in VOXYGEN_LANGUAGES[language]:
  40. return VOXYGEN_LANGUAGES[language][voice]
  41. logging.warn("Cannot find language matching language: %s voice: %s replace by default voice: %s", language, voice, VOXYGEN_VOICE_DEFAULT)
  42. return VOXYGEN_VOICE_DEFAULT
  43. def get_audio(voice, text, file_path, cache):
  44. if not cache or not os.path.exists(file_path) or file_is_empty(file_path):
  45. payload = {
  46. "method": "redirect",
  47. "text": text.encode('utf8'),
  48. "voice": voice
  49. }
  50. r = requests.get(VOXYGEN_URL, params=payload, stream=True, timeout=VOXYGEN_TIMEOUT_SEC)
  51. content_type = r.headers['Content-Type']
  52. logging.debug("Trying to get url: %s response code: %s and content-type: %s", r.url, r.status_code, content_type)
  53. try:
  54. if r.status_code == requests.codes.ok and content_type == VOXYGEN_CONTENT_TYPE:
  55. return write_in_file(file_path, r.content)
  56. else:
  57. return False
  58. except IOError as e:
  59. logging.error("I/O error(%s): %s", e.errno, e.strerror)
  60. except ValueError:
  61. logging.error("Could not convert data to an integer.")
  62. except:
  63. logging.error("Unexpected error: %s", sys.exc_info()[0])
  64. else:
  65. return True
  66. def write_in_file(file_path, content):
  67. with open(file_path, "wb") as file_open:
  68. file_open.write(content)
  69. file_open.close()
  70. return not file_is_empty(file_path)
  71. def file_is_empty(file_path):
  72. return os.path.getsize(file_path) == 0
  73. def create_directory(cache_path):
  74. if not os.path.exists(cache_path):
  75. os.makedirs(cache_path)
  76. def wipe_cache():
  77. shutil.rmtree(CACHE_PATH)