From cda6e13ac807af1ff5665a24128b15feb3ee59c7 Mon Sep 17 00:00:00 2001 From: TomKranenburg Date: Sat, 23 Dec 2023 11:07:14 +0000 Subject: [PATCH] Update __init__.py Add automatic microphone reconnect loop to compensate for microphone errors --- speech_recognition/__init__.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/speech_recognition/__init__.py b/speech_recognition/__init__.py index 852eaeef..68bdf2b9 100644 --- a/speech_recognition/__init__.py +++ b/speech_recognition/__init__.py @@ -558,14 +558,20 @@ def listen_in_background(self, source, callback, phrase_time_limit=None): running = [True] def threaded_listen(): - with source as s: - while running[0]: - try: # listen for 1 second, then check again if the stop function has been called - audio = self.listen(s, 1, phrase_time_limit) - except WaitTimeoutError: # listening timed out, just try again - pass - else: - if running[0]: callback(self, audio) + while running[0]: + try: + with source as s: + while running[0]: + try: # listen for 1 second, then check again if the stop function has been called + audio = self.listen(s, 1, phrase_time_limit) + except WaitTimeoutError: # listening timed out, just try again + pass + except (OSError, AttributeError): + break + else: + if running[0]: callback(self, audio) + except OSError as Error: + time.sleep(5) def stopper(wait_for_stop=True): running[0] = False