16
16
17
17
from __future__ import division
18
18
19
+ import argparse
19
20
import contextlib
20
21
import functools
22
+ import logging
21
23
import re
22
24
import signal
23
25
import sys
33
35
import pyaudio
34
36
from six .moves import queue
35
37
36
- # Seconds to allow you to shut up
38
+ # Seconds you have to wrap up your utterance
37
39
WRAP_IT_UP_SECS = 55
38
40
39
41
# Audio recording parameters
@@ -172,9 +174,9 @@ def listen_print_loop(recognize_stream, wrap_it_up_secs, max_recog_secs=60):
172
174
the next result to overwrite it, until the response is a final one. For the
173
175
final one, print a newline to preserve the finalized transcription.
174
176
"""
175
- start_time = time . time ()
177
+ # What time should we switch to a new stream?
176
178
time_to_switch = time .time () + max_recog_secs - wrap_it_up_secs
177
- wrap_it_up = False
179
+ graceful_exit = False
178
180
num_chars_printed = 0
179
181
for resp in recognize_stream :
180
182
if resp .error .code != code_pb2 .OK :
@@ -183,12 +185,8 @@ def listen_print_loop(recognize_stream, wrap_it_up_secs, max_recog_secs=60):
183
185
if not resp .results :
184
186
if resp .endpointer_type is resp .END_OF_SPEECH and (
185
187
time .time () > time_to_switch ):
186
- wrap_it_up = True
187
- resp = next (recognize_stream )
188
- if not resp .results :
189
- return True
190
- else :
191
- continue
188
+ graceful_exit = True
189
+ continue
192
190
193
191
# Display the top transcription
194
192
result = resp .results [0 ]
@@ -213,20 +211,18 @@ def listen_print_loop(recognize_stream, wrap_it_up_secs, max_recog_secs=60):
213
211
# one of our keywords.
214
212
if re .search (r'\b(exit|quit)\b' , transcript , re .I ):
215
213
print ('Exiting..' )
216
- return False
214
+ recognize_stream . cancel ()
217
215
218
- num_chars_printed = 0
216
+ elif graceful_exit :
217
+ break
219
218
220
- if wrap_it_up :
221
- return True
219
+ num_chars_printed = 0
222
220
223
221
224
222
def main ():
225
223
service = cloud_speech_pb2 .SpeechStub (
226
224
make_channel ('speech.googleapis.com' , 443 ))
227
225
228
- keep_going = True
229
-
230
226
# For streaming audio from the microphone, there are three threads.
231
227
# First, a thread that collects audio data as it comes in
232
228
with record_audio (RATE , CHUNK ) as buff :
@@ -237,27 +233,35 @@ def main():
237
233
requests , DEADLINE_SECS )
238
234
239
235
# Exit things cleanly on interrupt
240
- def handle_interrupt (* _ ):
241
- keep_going = False
242
- recognize_stream .cancel ()
243
- signal .signal (signal .SIGINT , handle_interrupt )
236
+ signal .signal (signal .SIGINT , lambda * _ : recognize_stream .cancel ())
244
237
245
238
# Now, put the transcription responses to use.
246
- while keep_going :
247
- print ('==== Continuing... ====' )
248
- keep_going = False
249
- try :
250
- keep_going = listen_print_loop (recognize_stream , WRAP_IT_UP_SECS )
239
+ try :
240
+ while True :
241
+ listen_print_loop (recognize_stream , WRAP_IT_UP_SECS )
251
242
243
+ # Discard this stream and create a new one.
244
+ # Note: calling .cancel() doesn't immediately raise an RpcError
245
+ # - it only raises when the iterator's next() is requested
252
246
recognize_stream .cancel ()
253
- next (recognize_stream )
247
+
248
+ logging .debug ('Starting new stream' )
249
+ requests = request_stream (_audio_data_generator (buff ), RATE )
250
+ recognize_stream = service .StreamingRecognize (
251
+ requests , DEADLINE_SECS )
252
+
253
+ except grpc .RpcError , e :
254
254
# This happens because of the interrupt handler
255
- except grpc .RpcError , e :
256
- if keep_going :
257
- requests = request_stream (_audio_data_generator (buff ), RATE )
258
- recognize_stream = service .StreamingRecognize (
259
- requests , DEADLINE_SECS )
255
+ pass
260
256
261
257
262
258
if __name__ == '__main__' :
259
+ parser = argparse .ArgumentParser ()
260
+ parser .add_argument (
261
+ '-v' , '--verbose' , help = 'increase output verbosity' ,
262
+ action = 'store_true' )
263
+ args = parser .parse_args ()
264
+ if args .verbose :
265
+ logging .basicConfig (level = logging .DEBUG )
266
+
263
267
main ()
0 commit comments