voxygen.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import sha
  2. import os
  3. import pygame
  4. import requests
  5. VOXYGEN_LANGUAGES = {
  6. "fr" : {"electra":"Electra","emma":"Emma","becool":"Becool","agnes":"Agnes","loic":"Loic","fabienne":"Fabienne","helene":"Helene","marion":"Marion","matteo":"Matteo","melodine":"Melodine","mendoo":"Mendoo","michel":"Michel","moussa":"Moussa","philippe":"Philippe","sorciere":"Sorciere"},
  7. "ar" : {"adel":"Adel"},
  8. "de" : {"matthias":"Matthias","jylvia":"Sylvia"},
  9. "uk" : {"bronwen":"Bronwen","elizabeth":"elizabeth","judith":"Judith","paul":"Paul","witch":"Witch"},
  10. "us" : {"bruce":"Bruce","jenny":"Jenny"},
  11. "es" : {"martha":"Martha"},
  12. "it" : {"sonia":"Sonia"}
  13. }
  14. def say(words=None, voice=None, language=None, cache=None):
  15. path = "/tmp/jarvis/tts/voxygen/"
  16. if not os.path.exists(path):
  17. os.makedirs(path)
  18. sha1 = sha.new(words).hexdigest()
  19. tempfile = path+voice+"."+sha1+".tts"
  20. get_audio(voice,words,tempfile,cache)
  21. play_audio(tempfile)
  22. if not cache:
  23. os.remove(tempfile)
  24. def get_audio(voice, text, filepath,cache):
  25. if not cache or not os.path.exists(filepath):
  26. payload ={
  27. "method" : "redirect",
  28. "text" : text.encode('utf8'),
  29. "voice" : voice
  30. }
  31. r = requests.get("https://www.voxygen.fr/sites/all/modules/voxygen_voices/assets/proxy/index.php", params=payload, stream=True)
  32. logging.debug("Trying to get url: %s response code: %s",r.url,r.status_code)
  33. if r.status_code==200:
  34. with open(os.path.abspath(filepath), "wb") as sound_file:
  35. sound_file.write(r.content)
  36. def play_audio(music_file, volume=0.8):
  37. pygame.mixer.init(16000, -16, 1, 2048)
  38. pygame.mixer.music.set_volume(volume)
  39. clock = pygame.time.Clock()
  40. try:
  41. pygame.mixer.music.load(music_file)
  42. logging.debug("Music file {} loaded!".format(music_file))
  43. except pygame.error:
  44. os.remove(music_file)
  45. logging.debug("File {} not found! ({})".format(music_file, pygame.get_error()))
  46. return
  47. pygame.mixer.music.play()
  48. while pygame.mixer.music.get_busy():
  49. clock.tick(10)
  50. def get_voice(voice=None, language=None):
  51. if language in VOXYGEN_LANGUAGES:
  52. if voice in VOXYGEN_LANGUAGES[language]:
  53. return VOXYGEN_LANGUAGES[language][voice]
  54. logging.debug("Cannot find language maching language: %s voice: %s",language,voice)
  55. return ""