|
@@ -16,7 +16,11 @@ VOXYGEN_LANGUAGES = dict(
|
|
|
es=dict(martha="Martha"),
|
|
|
it=dict(sonia="Sonia"))
|
|
|
|
|
|
-CACHE_PATH = "/tmp/jarvis/tts/voxygen/"
|
|
|
+VOXYGEN_LANGUAGE_DEFAULT = "Michel"
|
|
|
+VOXYGEN_URL = "https://www.voxygen.fr/sites/all/modules/voxygen_voices/assets/proxy/index.php"
|
|
|
+
|
|
|
+CACHE_PATH = "/tmp/jarvis/tts/voxygen"
|
|
|
+CACHE_EXTENSION = ".tts"
|
|
|
AUDIO_FREQUENCY = 16000
|
|
|
AUDIO_SIZE = -16
|
|
|
AUDIO_CHANNEL = 1
|
|
@@ -24,44 +28,56 @@ AUDIO_BUFFER = 2048
|
|
|
|
|
|
|
|
|
def say(words=None, voice=None, language=None, cache=None):
|
|
|
- if not os.path.exists(CACHE_PATH):
|
|
|
- os.makedirs(CACHE_PATH)
|
|
|
-
|
|
|
- sha1 = hashlib.sha1(words).hexdigest()
|
|
|
+ create_directory()
|
|
|
|
|
|
voice = get_voice(voice, language)
|
|
|
|
|
|
- tempfile = CACHE_PATH + voice + "." + sha1 + ".tts"
|
|
|
+ file_path = get_file_path(words,voice,language)
|
|
|
|
|
|
- get_audio(voice, words, tempfile, cache)
|
|
|
+ get_audio(voice, words, file_path, cache)
|
|
|
|
|
|
- play_audio(tempfile)
|
|
|
+ play_audio(file_path)
|
|
|
|
|
|
- if not cache:
|
|
|
- remove_file(tempfile)
|
|
|
+ remove_temp_file(file_path, cache)
|
|
|
+
|
|
|
+
|
|
|
+def get_file_path(words, voice, language):
|
|
|
+ sha1 = hashlib.sha1(words).hexdigest()
|
|
|
+ filename = voice + "." + sha1 + CACHE_EXTENSION
|
|
|
+ file_path = os.path.join(CACHE_PATH, filename)
|
|
|
+ return file_path
|
|
|
+
|
|
|
+
|
|
|
+def get_voice(voice=None, language=None):
|
|
|
+ if language in VOXYGEN_LANGUAGES:
|
|
|
+ if voice in VOXYGEN_LANGUAGES[language]:
|
|
|
+ return VOXYGEN_LANGUAGES[language][voice]
|
|
|
|
|
|
+ logging.debug("Cannot find language matching language: %s voice: %s replace by default voice: %s", language, voice, VOXYGEN_LANGUAGE_DEFAULT)
|
|
|
+ return VOXYGEN_LANGUAGE_DEFAULT
|
|
|
|
|
|
-def get_audio(voice, text, filepath, cache):
|
|
|
- if not cache or not os.path.exists(filepath):
|
|
|
+
|
|
|
+def get_audio(voice, text, file_path, cache):
|
|
|
+ if not cache or not os.path.exists(file_path):
|
|
|
payload = {
|
|
|
"method": "redirect",
|
|
|
"text": text.encode('utf8'),
|
|
|
"voice": voice
|
|
|
}
|
|
|
|
|
|
- r = requests.get("https://www.voxygen.fr/sites/all/modules/voxygen_voices/assets/proxy/index.php", params=payload, stream=True)
|
|
|
+ r = requests.get(VOXYGEN_URL, params=payload, stream=True)
|
|
|
logging.debug("Trying to get url: %s response code: %s", r.url, r.status_code)
|
|
|
|
|
|
try:
|
|
|
if r.status_code == 200:
|
|
|
- with open(os.path.abspath(filepath), "w") as sound_file:
|
|
|
+ with open(file_path, "w") as sound_file:
|
|
|
sound_file.write(r.content)
|
|
|
except IOError as e:
|
|
|
- print "I/O error({0}): {1}".format(e.errno, e.strerror)
|
|
|
+ print("I/O error({0}): {1}".format(e.errno, e.strerror))
|
|
|
except ValueError:
|
|
|
- print "Could not convert data to an integer."
|
|
|
+ print("Could not convert data to an integer.")
|
|
|
except:
|
|
|
- print "Unexpected error:", sys.exc_info()[0]
|
|
|
+ print("Unexpected error:", sys.exc_info()[0])
|
|
|
|
|
|
|
|
|
def play_audio(music_file, volume=0.8):
|
|
@@ -70,23 +86,19 @@ def play_audio(music_file, volume=0.8):
|
|
|
clock = pygame.time.Clock()
|
|
|
try:
|
|
|
pygame.mixer.music.load(music_file)
|
|
|
- logging.debug("Music file {} loaded!".format(music_file))
|
|
|
+ logging.debug("Music file %s loaded!", music_file)
|
|
|
except pygame.error:
|
|
|
remove_file(music_file)
|
|
|
- logging.debug("File {} not found! ({})".format(music_file, pygame.get_error()))
|
|
|
+ logging.debug("File %s not found! (%s)",music_file, pygame.get_error())
|
|
|
return
|
|
|
pygame.mixer.music.play()
|
|
|
while pygame.mixer.music.get_busy():
|
|
|
clock.tick(10)
|
|
|
|
|
|
|
|
|
-def get_voice(voice=None, language=None):
|
|
|
- if language in VOXYGEN_LANGUAGES:
|
|
|
- if voice in VOXYGEN_LANGUAGES[language]:
|
|
|
- return VOXYGEN_LANGUAGES[language][voice]
|
|
|
-
|
|
|
- logging.debug("Cannot find language maching language: %s voice: %s", language, voice)
|
|
|
- return ""
|
|
|
+def remove_temp_file(file_path, cache):
|
|
|
+ if not cache:
|
|
|
+ remove_file(file_path)
|
|
|
|
|
|
|
|
|
def remove_file(path):
|
|
@@ -94,6 +106,11 @@ def remove_file(path):
|
|
|
os.remove(path)
|
|
|
|
|
|
|
|
|
+def create_directory():
|
|
|
+ if not os.path.exists(CACHE_PATH):
|
|
|
+ os.makedirs(CACHE_PATH)
|
|
|
+
|
|
|
+
|
|
|
def wipe_cache():
|
|
|
shutil.rmtree(CACHE_PATH)
|
|
|
|