voxygen.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. CACHE_PATH = "/tmp/jarvis/tts/voxygen/"
  17. AUDIO_FREQUENCY = 16000
  18. AUDIO_SIZE = -16
  19. AUDIO_CHANNEL = 1
  20. AUDIO_BUFFER = 2048
  21. def say(words=None, voice=None, language=None, cache=None):
  22. if not os.path.exists(CACHE_PATH):
  23. os.makedirs(CACHE_PATH)
  24. sha1 = hashlib.sha1(words).hexdigest()
  25. voice = get_voice(voice, language)
  26. tempfile = CACHE_PATH + voice + "." + sha1 + ".tts"
  27. get_audio(voice, words, tempfile, cache)
  28. play_audio(tempfile)
  29. if not cache:
  30. remove_file(tempfile)
  31. def get_audio(voice, text, filepath, cache):
  32. if not cache or not os.path.exists(filepath):
  33. payload = {
  34. "method": "redirect",
  35. "text": text.encode('utf8'),
  36. "voice": voice
  37. }
  38. r = requests.get("https://www.voxygen.fr/sites/all/modules/voxygen_voices/assets/proxy/index.php", params=payload, stream=True)
  39. logging.debug("Trying to get url: %s response code: %s", r.url, r.status_code)
  40. if r.status_code == 200:
  41. with open(os.path.abspath(filepath), "wb") as sound_file:
  42. sound_file.write(r.content)
  43. def play_audio(music_file, volume=0.8):
  44. pygame.mixer.init(AUDIO_FREQUENCY, AUDIO_SIZE, AUDIO_CHANNEL, AUDIO_BUFFER)
  45. pygame.mixer.music.set_volume(volume)
  46. clock = pygame.time.Clock()
  47. try:
  48. pygame.mixer.music.load(music_file)
  49. logging.debug("Music file {} loaded!".format(music_file))
  50. except pygame.error:
  51. remove_file(music_file)
  52. logging.debug("File {} not found! ({})".format(music_file, pygame.get_error()))
  53. return
  54. pygame.mixer.music.play()
  55. while pygame.mixer.music.get_busy():
  56. clock.tick(10)
  57. def get_voice(voice=None, language=None):
  58. if language in VOXYGEN_LANGUAGES:
  59. if voice in VOXYGEN_LANGUAGES[language]:
  60. return VOXYGEN_LANGUAGES[language][voice]
  61. logging.debug("Cannot find language maching language: %s voice: %s", language, voice)
  62. return ""
  63. def remove_file(path):
  64. if os.path.exists(path):
  65. os.remove(path)
  66. def wipe_cache():
  67. shutil.rmtree(CACHE_PATH)