voxygen.py 4.3 KB

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