From 3993527c9f51cec8716db93e57b8abf5dd896129 Mon Sep 17 00:00:00 2001 From: zardamhussain Date: Tue, 1 Oct 2024 17:21:33 +0530 Subject: [PATCH] Implement non-blocking voice transmission to the daily room --- vapi_python/daily_call.py | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/vapi_python/daily_call.py b/vapi_python/daily_call.py index f4a7911..5ec0cdf 100644 --- a/vapi_python/daily_call.py +++ b/vapi_python/daily_call.py @@ -22,13 +22,7 @@ def __init__(self): self.__audio_interface = pyaudio.PyAudio() - self.__input_audio_stream = self.__audio_interface.open( - format=pyaudio.paInt16, - channels=NUM_CHANNELS, - rate=SAMPLE_RATE, - input=True, - frames_per_buffer=CHUNK_SIZE, - ) + self.__input_audio_stream = None self.__output_audio_stream = self.__audio_interface.open( format=pyaudio.paInt16, @@ -140,14 +134,22 @@ def send_user_audio(self): print(f"Unable to receive mic audio!") return - while not self.__app_quit: - buffer = self.__input_audio_stream.read( - CHUNK_SIZE, exception_on_overflow=False) - if len(buffer) > 0: - try: - self.__mic_device.write_frames(buffer) - except Exception as e: - print(e) + def callback(in_data, frame_count, time_info, status): + if self.__app_quit: + return (None, pyaudio.paComplete) + self.__mic_device.write_frames(in_data) + return (in_data, pyaudio.paContinue) + + # directly stream the sound + self.__input_audio_stream = self.__audio_interface.open( + format=pyaudio.paInt16, + channels=NUM_CHANNELS, + rate=SAMPLE_RATE, + input=True, + frames_per_buffer=CHUNK_SIZE, + stream_callback=callback + ) + def receive_bot_audio(self): self.__start_event.wait()