From f9f2e2d7ea480100a5e549e53c755528530f2f7f Mon Sep 17 00:00:00 2001 From: Moishe Lettvin Date: Wed, 17 Jan 2024 19:25:01 -0500 Subject: [PATCH] stop_when_done --- src/dailyai/services/ai_services.py | 3 +-- src/dailyai/services/daily_transport_service.py | 4 ++++ src/samples/theoretical-to-real/01-say-one-thing.py | 13 +++++++------ .../theoretical-to-real/02-llm-say-one-thing.py | 3 +-- .../theoretical-to-real/04-utterance-and-speech.py | 3 +-- .../theoretical-to-real/05-sync-speech-and-text.py | 3 +-- 6 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/dailyai/services/ai_services.py b/src/dailyai/services/ai_services.py index 4c5604c0d..83c1c7099 100644 --- a/src/dailyai/services/ai_services.py +++ b/src/dailyai/services/ai_services.py @@ -163,8 +163,7 @@ class TTSService(AIService): # Convenience function to send the audio for a sentence to the given queue async def say(self, sentence, queue: asyncio.Queue): - async for audio_chunk in self.run_tts(sentence): - await queue.put(QueueFrame(FrameType.AUDIO, audio_chunk)) + await self.run_to_queue(queue, [QueueFrame(FrameType.SENTENCE, sentence)]) class ImageGenService(AIService): diff --git a/src/dailyai/services/daily_transport_service.py b/src/dailyai/services/daily_transport_service.py index be34b56a1..5349036e5 100644 --- a/src/dailyai/services/daily_transport_service.py +++ b/src/dailyai/services/daily_transport_service.py @@ -209,6 +209,10 @@ class DailyTransportService(EventHandler): def wait_for_send_queue_to_empty(self): self.threadsafe_send_queue.join() + def stop_when_done(self): + self.wait_for_send_queue_to_empty() + self.stop() + async def run(self) -> None: self.configure_daily() diff --git a/src/samples/theoretical-to-real/01-say-one-thing.py b/src/samples/theoretical-to-real/01-say-one-thing.py index 80ba91a32..9a95bbd9b 100644 --- a/src/samples/theoretical-to-real/01-say-one-thing.py +++ b/src/samples/theoretical-to-real/01-say-one-thing.py @@ -5,6 +5,7 @@ from typing import AsyncGenerator from dailyai.queue_frame import QueueFrame, FrameType from dailyai.services.daily_transport_service import DailyTransportService from dailyai.services.azure_ai_services import AzureTTSService +from dailyai.services.elevenlabs_ai_service import ElevenLabsTTSService async def main(room_url): # create a transport service object using environment variables for @@ -23,9 +24,7 @@ async def main(room_url): meeting_duration_minutes, ) transport.mic_enabled = True - - # similarly, create a tts service - tts = AzureTTSService() + tts = ElevenLabsTTSService(voice_id="ErXwobaYiN019PkySvjV") # Register an event handler so we can play the audio when the participant joins. @transport.event_handler("on_participant_joined") @@ -33,11 +32,13 @@ async def main(room_url): if participant["info"]["isLocal"]: return - await tts.say("Hello there, " + participant["info"]["userName"] + "!", transport.send_queue) + await tts.say( + "Hello there, " + participant["info"]["userName"] + "!", + transport.send_queue, + ) # wait for the output queue to be empty, then leave the meeting - transport.wait_for_send_queue_to_empty() - transport.stop() + transport.stop_when_done() await transport.run() diff --git a/src/samples/theoretical-to-real/02-llm-say-one-thing.py b/src/samples/theoretical-to-real/02-llm-say-one-thing.py index 301b2762c..425eacf2f 100644 --- a/src/samples/theoretical-to-real/02-llm-say-one-thing.py +++ b/src/samples/theoretical-to-real/02-llm-say-one-thing.py @@ -38,8 +38,7 @@ async def main(room_url): async def on_first_other_participant_joined(transport): await tts_task - transport.wait_for_send_queue_to_empty() - transport.stop() + transport.stop_when_done() await transport.run() diff --git a/src/samples/theoretical-to-real/04-utterance-and-speech.py b/src/samples/theoretical-to-real/04-utterance-and-speech.py index 0d72905b4..bf831c29b 100644 --- a/src/samples/theoretical-to-real/04-utterance-and-speech.py +++ b/src/samples/theoretical-to-real/04-utterance-and-speech.py @@ -63,8 +63,7 @@ async def main(room_url:str): await asyncio.gather(llm_response_task, buffer_to_send_queue()) - transport.wait_for_send_queue_to_empty() - transport.stop() + transport.stop_when_done() await transport.run() diff --git a/src/samples/theoretical-to-real/05-sync-speech-and-text.py b/src/samples/theoretical-to-real/05-sync-speech-and-text.py index 89c0230e3..a839d8385 100644 --- a/src/samples/theoretical-to-real/05-sync-speech-and-text.py +++ b/src/samples/theoretical-to-real/05-sync-speech-and-text.py @@ -115,8 +115,7 @@ async def main(room_url): await transport.send_queue.put(QueueFrame(FrameType.AUDIO, audio)) # wait for the output queue to be empty, then leave the meeting - transport.wait_for_send_queue_to_empty() - transport.stop() + transport.stop_when_done() month_tasks = [asyncio.create_task(get_month_data(month)) for month in months]