@@ -22,13 +22,7 @@ def __init__(self):
2222
2323 self .__audio_interface = pyaudio .PyAudio ()
2424
25- self .__input_audio_stream = self .__audio_interface .open (
26- format = pyaudio .paInt16 ,
27- channels = NUM_CHANNELS ,
28- rate = SAMPLE_RATE ,
29- input = True ,
30- frames_per_buffer = CHUNK_SIZE ,
31- )
25+ self .__input_audio_stream = None
3226
3327 self .__output_audio_stream = self .__audio_interface .open (
3428 format = pyaudio .paInt16 ,
@@ -140,14 +134,22 @@ def send_user_audio(self):
140134 print (f"Unable to receive mic audio!" )
141135 return
142136
143- while not self .__app_quit :
144- buffer = self .__input_audio_stream .read (
145- CHUNK_SIZE , exception_on_overflow = False )
146- if len (buffer ) > 0 :
147- try :
148- self .__mic_device .write_frames (buffer )
149- except Exception as e :
150- print (e )
137+ def callback (in_data , frame_count , time_info , status ):
138+ if self .__app_quit :
139+ return (None , pyaudio .paComplete )
140+ self .__mic_device .write_frames (in_data )
141+ return (in_data , pyaudio .paContinue )
142+
143+ # directly stream the sound
144+ self .__input_audio_stream = self .__audio_interface .open (
145+ format = pyaudio .paInt16 ,
146+ channels = NUM_CHANNELS ,
147+ rate = SAMPLE_RATE ,
148+ input = True ,
149+ frames_per_buffer = CHUNK_SIZE ,
150+ stream_callback = callback
151+ )
152+
151153
152154 def receive_bot_audio (self ):
153155 self .__start_event .wait ()
@@ -161,15 +163,3 @@ def receive_bot_audio(self):
161163
162164 if len (buffer ) > 0 :
163165 self .__output_audio_stream .write (buffer , CHUNK_SIZE )
164-
165- def send_app_message (self , message ):
166- """
167- Send an application message to the assistant.
168-
169- :param message: The message to send (expects a dictionary).
170- """
171- try :
172- serialized_message = json .dumps (message )
173- self .__call_client .send_app_message (serialized_message )
174- except Exception as e :
175- print (f"Failed to send app message: { e } " )
0 commit comments