voxygen.py 4.3 KB

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