voxygen.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. CACHE_PATH = "/tmp/jarvis/tts/voxygen"
  20. CACHE_EXTENSION = ".tts"
  21. AUDIO_FREQUENCY = 16000
  22. AUDIO_SIZE = -16
  23. AUDIO_CHANNEL = 1
  24. AUDIO_BUFFER = 2048
  25. def say(words=None, voice=None, language=VOXYGEN_LANGUAGES_DEFAULT, cache=None):
  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. play_audio(file_path)
  30. remove_temp_file(file_path, cache)
  31. def get_file_path(words, voice, language):
  32. sha1 = hashlib.sha1(words).hexdigest()
  33. filename = voice + "." + sha1 + CACHE_EXTENSION
  34. cache_directory = os.path.join(CACHE_PATH, language)
  35. file_path = os.path.join(cache_directory, filename)
  36. create_directory(cache_directory)
  37. logging.debug("Cache directory %s exists and File path for audio is: %s", cache_directory, file_path)
  38. return file_path
  39. def get_voice(voice, language):
  40. if language in VOXYGEN_LANGUAGES and voice in VOXYGEN_LANGUAGES[language]:
  41. return VOXYGEN_LANGUAGES[language][voice]
  42. logging.debug("Cannot find language matching language: %s voice: %s replace by default voice: %s", language, voice, VOXYGEN_VOICE_DEFAULT)
  43. return VOXYGEN_VOICE_DEFAULT
  44. def get_audio(voice, text, file_path, cache):
  45. if not cache or not os.path.exists(file_path) or file_is_empty(file_path):
  46. payload = {
  47. "method": "redirect",
  48. "text": text.encode('utf8'),
  49. "voice": voice
  50. }
  51. r = requests.get(VOXYGEN_URL, params=payload, stream=True)
  52. logging.debug("Trying to get url: %s response code: %s", r.url, r.status_code)
  53. try:
  54. if r.status_code == 200:
  55. write_in_file(file_path, r.content)
  56. return True
  57. else:
  58. return False
  59. except IOError as e:
  60. print("I/O error(%s): %s", e.errno, e.strerror)
  61. except ValueError:
  62. print("Could not convert data to an integer.")
  63. except:
  64. print("Unexpected error: %s", sys.exc_info()[0])
  65. else:
  66. return True
  67. def play_audio(music_file, volume=0.8):
  68. try:
  69. init_player_audio(music_file, volume)
  70. logging.debug("Music file %s loaded!", music_file)
  71. except pygame.error:
  72. remove_file(music_file)
  73. logging.debug("File %s not found! (%s)", music_file, pygame.get_error())
  74. return
  75. start_player_audio()
  76. def init_player_audio(music_file, volume):
  77. pygame.mixer.init(AUDIO_FREQUENCY, AUDIO_SIZE, AUDIO_CHANNEL, AUDIO_BUFFER)
  78. pygame.mixer.music.set_volume(volume)
  79. pygame.mixer.music.load(music_file)
  80. def start_player_audio():
  81. pygame.mixer.music.play()
  82. clock = pygame.time.Clock()
  83. while pygame.mixer.music.get_busy():
  84. clock.tick(10)
  85. return
  86. def remove_temp_file(file_path, cache):
  87. if not cache:
  88. remove_file(file_path)
  89. def remove_file(file_path):
  90. if os.path.exists(file_path):
  91. os.remove(file_path)
  92. def write_in_file(file_path, content):
  93. with open(file_path, "w") as file_open:
  94. file_open.write(content)
  95. file_open.close()
  96. def file_is_empty(file_path):
  97. return os.path.getsize(file_path) == 0
  98. def create_directory(cache_path):
  99. if not os.path.exists(cache_path):
  100. os.makedirs(cache_path)
  101. def wipe_cache():
  102. shutil.rmtree(CACHE_PATH)