Skip to content

Commit d35fa56

Browse files
author
Jerjou Cheng
committed
Reduce amount of audio lost between utterances
1 parent b28aa8c commit d35fa56

2 files changed

Lines changed: 21 additions & 14 deletions

File tree

speech/grpc/transcribe_streaming_minute.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

speech/grpc/transcribe_streaming_utterances.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ def _audio_data_generator(buff):
6666
A chunk of data that is the aggregate of all chunks of data in `buff`.
6767
The function will block until at least one data chunk is available.
6868
"""
69-
stop = False
70-
while not stop:
69+
while True:
7170
# Use a blocking get() to ensure there's at least one chunk of data.
7271
data = [buff.get()]
7372

@@ -78,11 +77,13 @@ def _audio_data_generator(buff):
7877
except queue.Empty:
7978
break
8079

81-
# `None` in the buffer signals that the audio stream is closed. Yield
82-
# the final bit of the buffer and exit the loop.
80+
# `None` in the buffer signals that we should stop generating. Put the
81+
# data back into the buffer for the next generator.
8382
if None in data:
84-
stop = True
8583
data.remove(None)
84+
if data:
85+
buff.put(b''.join(data))
86+
break
8687

8788
yield b''.join(data)
8889

@@ -158,7 +159,7 @@ def request_stream(data_stream, rate, interim_results=True):
158159
yield cloud_speech_pb2.StreamingRecognizeRequest(audio_content=data)
159160

160161

161-
def listen_print_loop(recognize_stream):
162+
def listen_print_loop(recognize_stream, buff):
162163
"""Iterates through server responses and prints them.
163164
164165
The recognize_stream passed is a generator that will block until a response
@@ -175,6 +176,10 @@ def listen_print_loop(recognize_stream):
175176
raise RuntimeError('Server error: ' + resp.error.message)
176177

177178
if not resp.results:
179+
if resp.endpointer_type is resp.END_OF_UTTERANCE:
180+
# Signal the audio generator to stop generating, and leave the
181+
# buffer to fill.
182+
buff.put(None)
178183
continue
179184

180185
# Display the top transcription
@@ -226,7 +231,7 @@ def main():
226231
# Now, put the transcription responses to use.
227232
try:
228233
while True:
229-
listen_print_loop(recognize_stream)
234+
listen_print_loop(recognize_stream, buff)
230235

231236
# Discard this stream and create a new one.
232237
# Note: calling .cancel() doesn't immediately raise an RpcError

0 commit comments

Comments
 (0)