Add semaphore to speak module

pull/242/head
Dylan 2023-04-06 14:02:39 +02:00
parent 0cbd2bb3d0
commit ef4a02757f
1 changed files with 5 additions and 1 deletions

View File

@ -5,7 +5,7 @@ from config import Config
cfg = Config() cfg = Config()
import gtts import gtts
import threading import threading
from threading import Lock from threading import Lock, Semaphore
# TODO: Nicer names for these ids # TODO: Nicer names for these ids
@ -17,6 +17,7 @@ tts_headers = {
} }
mutex_lock = Lock() # Ensure only one sound is played at a time mutex_lock = Lock() # Ensure only one sound is played at a time
queue_semaphore = Semaphore(1) # The amount of sounds to queue before blocking the main thread
def eleven_labs_speech(text, voice_index=0): def eleven_labs_speech(text, voice_index=0):
tts_url = "https://api.elevenlabs.io/v1/text-to-speech/{voice_id}".format( tts_url = "https://api.elevenlabs.io/v1/text-to-speech/{voice_id}".format(
@ -52,6 +53,9 @@ def say_text(text, voice_index=0):
success = eleven_labs_speech(text, voice_index) success = eleven_labs_speech(text, voice_index)
if not success: if not success:
gtts_speech(text) gtts_speech(text)
queue_semaphore.release()
queue_semaphore.acquire(True)
thread = threading.Thread(target=speak) thread = threading.Thread(target=speak)
thread.start() thread.start()