voxygen.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. try:
  41. if r.status_code == 200:
  42. with open(os.path.abspath(filepath), "w") as sound_file:
  43. sound_file.write(r.content)
  44. except IOError as e:
  45. print "I/O error({0}): {1}".format(e.errno, e.strerror)
  46. except ValueError:
  47. print "Could not convert data to an integer."
  48. except:
  49. print "Unexpected error:", sys.exc_info()[0]
  50. def play_audio(music_file, volume=0.8):
  51. pygame.mixer.init(AUDIO_FREQUENCY, AUDIO_SIZE, AUDIO_CHANNEL, AUDIO_BUFFER)
  52. pygame.mixer.music.set_volume(volume)
  53. clock = pygame.time.Clock()
  54. try:
  55. pygame.mixer.music.load(music_file)
  56. logging.debug("Music file {} loaded!".format(music_file))
  57. except pygame.error:
  58. remove_file(music_file)
  59. logging.debug("File {} not found! ({})".format(music_file, pygame.get_error()))
  60. return
  61. pygame.mixer.music.play()
  62. while pygame.mixer.music.get_busy():
  63. clock.tick(10)
  64. def get_voice(voice=None, language=None):
  65. if language in VOXYGEN_LANGUAGES:
  66. if voice in VOXYGEN_LANGUAGES[language]:
  67. return VOXYGEN_LANGUAGES[language][voice]
  68. logging.debug("Cannot find language maching language: %s voice: %s", language, voice)
  69. return ""
  70. def remove_file(path):
  71. if os.path.exists(path):
  72. os.remove(path)
  73. def wipe_cache():
  74. shutil.rmtree(CACHE_PATH)