@@ -72,8 +72,7 @@ def _audio_data_generator(buff):
7272 A chunk of data that is the aggregate of all chunks of data in `buff`.
7373 The function will block until at least one data chunk is available.
7474 """
75- stop = False
76- while not stop :
75+ while True :
7776 # Use a blocking get() to ensure there's at least one chunk of data.
7877 data = [buff .get ()]
7978
@@ -84,11 +83,13 @@ def _audio_data_generator(buff):
8483 except queue .Empty :
8584 break
8685
87- # `None` in the buffer signals that the audio stream is closed. Yield
88- # the final bit of the buffer and exit the loop .
86+ # `None` in the buffer signals that we should stop generating. Put the
87+ # data back into the buffer for the next generator .
8988 if None in data :
90- stop = True
9189 data .remove (None )
90+ if data :
91+ buff .put (b'' .join (data ))
92+ break
9293
9394 yield b'' .join (data )
9495
@@ -163,7 +164,7 @@ def request_stream(data_stream, rate, interim_results=True):
163164 yield cloud_speech_pb2 .StreamingRecognizeRequest (audio_content = data )
164165
165166
166- def listen_print_loop (recognize_stream , wrap_it_up_secs , max_recog_secs = 60 ):
167+ def listen_print_loop (recognize_stream , wrap_it_up_secs , buff , max_recog_secs = 60 ):
167168 """Iterates through server responses and prints them.
168169
169170 The recognize_stream passed is a generator that will block until a response
@@ -186,6 +187,7 @@ def listen_print_loop(recognize_stream, wrap_it_up_secs, max_recog_secs=60):
186187 if resp .endpointer_type is resp .END_OF_SPEECH and (
187188 time .time () > time_to_switch ):
188189 graceful_exit = True
190+ buff .put (None )
189191 continue
190192
191193 # Display the top transcription
@@ -238,7 +240,7 @@ def main():
238240 # Now, put the transcription responses to use.
239241 try :
240242 while True :
241- listen_print_loop (recognize_stream , WRAP_IT_UP_SECS )
243+ listen_print_loop (recognize_stream , WRAP_IT_UP_SECS , buff )
242244
243245 # Discard this stream and create a new one.
244246 # Note: calling .cancel() doesn't immediately raise an RpcError
0 commit comments