voxygen.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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_LANGUAGE_DEFAULT = "Michel"
  17. VOXYGEN_URL = "https://www.voxygen.fr/sites/all/modules/voxygen_voices/assets/proxy/index.php"
  18. CACHE_PATH = "/tmp/jarvis/tts/voxygen"
  19. CACHE_EXTENSION = ".tts"
  20. AUDIO_FREQUENCY = 16000
  21. AUDIO_SIZE = -16
  22. AUDIO_CHANNEL = 1
  23. AUDIO_BUFFER = 2048
  24. def say(words=None, voice=None, language=None, cache=None):
  25. create_directory()
  26. voice = get_voice(voice, language)
  27. file_path = get_file_path(words,voice,language)
  28. 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. file_path = os.path.join(CACHE_PATH, filename)
  35. return file_path
  36. def get_voice(voice=None, language=None):
  37. if language in VOXYGEN_LANGUAGES:
  38. if voice in VOXYGEN_LANGUAGES[language]:
  39. return VOXYGEN_LANGUAGES[language][voice]
  40. logging.debug("Cannot find language matching language: %s voice: %s replace by default voice: %s", language, voice, VOXYGEN_LANGUAGE_DEFAULT)
  41. return VOXYGEN_LANGUAGE_DEFAULT
  42. def get_audio(voice, text, file_path, cache):
  43. if not cache or not os.path.exists(file_path):
  44. payload = {
  45. "method": "redirect",
  46. "text": text.encode('utf8'),
  47. "voice": voice
  48. }
  49. r = requests.get(VOXYGEN_URL, params=payload, stream=True)
  50. logging.debug("Trying to get url: %s response code: %s", r.url, r.status_code)
  51. try:
  52. if r.status_code == 200:
  53. with open(file_path, "w") as sound_file:
  54. sound_file.write(r.content)
  55. except IOError as e:
  56. print("I/O error({0}): {1}".format(e.errno, e.strerror))
  57. except ValueError:
  58. print("Could not convert data to an integer.")
  59. except:
  60. print("Unexpected error:", sys.exc_info()[0])
  61. def play_audio(music_file, volume=0.8):
  62. pygame.mixer.init(AUDIO_FREQUENCY, AUDIO_SIZE, AUDIO_CHANNEL, AUDIO_BUFFER)
  63. pygame.mixer.music.set_volume(volume)
  64. clock = pygame.time.Clock()
  65. try:
  66. pygame.mixer.music.load(music_file)
  67. logging.debug("Music file %s loaded!", music_file)
  68. except pygame.error:
  69. remove_file(music_file)
  70. logging.debug("File %s not found! (%s)",music_file, pygame.get_error())
  71. return
  72. pygame.mixer.music.play()
  73. while pygame.mixer.music.get_busy():
  74. clock.tick(10)
  75. def remove_temp_file(file_path, cache):
  76. if not cache:
  77. remove_file(file_path)
  78. def remove_file(path):
  79. if os.path.exists(path):
  80. os.remove(path)
  81. def create_directory():
  82. if not os.path.exists(CACHE_PATH):
  83. os.makedirs(CACHE_PATH)
  84. def wipe_cache():
  85. shutil.rmtree(CACHE_PATH)