This commit is contained in:
retoor 2025-01-18 15:40:31 +01:00
parent 71cc3955f1
commit 2c319b5492
2 changed files with 8 additions and 5 deletions

View File

@ -39,7 +39,7 @@ from rspeech.play import play_audio
import google.oauth2.credentials import google.oauth2.credentials
import uuid import uuid
import pathlib import pathlib
from rspeech.play import play_audio from rspeech.play import play_audio_async
# Chars to be ignored in speech # Chars to be ignored in speech
IGNORE_CHARS = ["*", "#", "`","'",'"',"\\","/","---"] 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") file = pathlib.Path(str(uuid.uuid4()) + ".mp3")
with file.open("wb") as audio_file: with file.open("wb") as audio_file:
audio_file.write(base64.b64decode(audio_content.encode('latin1'))) 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() file.unlink()
return return

View File

@ -30,16 +30,18 @@ import os
import subprocess import subprocess
import sys import sys
import pygame import pygame
import time
async def play_audio_async(filename) async def play_audio_async(filename)
pygame.mixer.init() pygame.mixer.init()
pygame.mixer.music.load(filename) pygame.mixer.music.load(filename)
time_start = time.time()
pygame.mixer.music.play() pygame.mixer.music.play()
while pygame.mixer.music.get_busy(): while pygame.mixer.music.get_busy():
await asyncio.sleep(0.1) duration = time.time() - time_start
yield yield duration
def play_audio(filename): def play_audio(filename):