diff --git a/vapi_python/daily_call.py b/vapi_python/daily_call.py index f4a7911..421d440 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() @@ -161,15 +163,3 @@ def receive_bot_audio(self): if len(buffer) > 0: self.__output_audio_stream.write(buffer, CHUNK_SIZE) - - def send_app_message(self, message): - """ - Send an application message to the assistant. - - :param message: The message to send (expects a dictionary). - """ - try: - serialized_message = json.dumps(message) - self.__call_client.send_app_message(serialized_message) - except Exception as e: - print(f"Failed to send app message: {e}")