diff --git a/src/rspeech/gcloud.py b/src/rspeech/gcloud.py index 6f1b453..34848b1 100644 --- a/src/rspeech/gcloud.py +++ b/src/rspeech/gcloud.py @@ -39,7 +39,7 @@ from rspeech.play import play_audio import google.oauth2.credentials import uuid import pathlib -from rspeech.play import play_audio +from rspeech.play import play_audio_async # Chars to be ignored in speech IGNORE_CHARS = ["*", "#", "`","'",'"',"\\","/","---"] @@ -104,7 +104,8 @@ async def tts(text:str ,google_project:str="lisa-448004", language_code:str="nl- file = pathlib.Path(str(uuid.uuid4()) + ".mp3") with file.open("wb") as audio_file: audio_file.write(base64.b64decode(audio_content.encode('latin1'))) - play_audio(file) + async for tik in play_audio(file) + await asyncio.sleep(0.1) file.unlink() return diff --git a/src/rspeech/play.py b/src/rspeech/play.py index 8d75518..42eae76 100644 --- a/src/rspeech/play.py +++ b/src/rspeech/play.py @@ -30,16 +30,18 @@ import os import subprocess import sys import pygame - +import time async def play_audio_async(filename) pygame.mixer.init() pygame.mixer.music.load(filename) + time_start = time.time() pygame.mixer.music.play() + while pygame.mixer.music.get_busy(): - await asyncio.sleep(0.1) - yield + duration = time.time() - time_start + yield duration def play_audio(filename):