Skip to content

Commit

Permalink
Implement non-blocking voice transmission to the daily room
Browse files Browse the repository at this point in the history
  • Loading branch information
zardamhussain committed Oct 1, 2024
1 parent ba16f2b commit 0f987a2
Showing 1 changed file with 17 additions and 27 deletions.
44 changes: 17 additions & 27 deletions vapi_python/daily_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand All @@ -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}")

0 comments on commit 0f987a2

Please sign in to comment.