From 0e278fffcb300d44e7f04da9bdf9ffa63b613432 Mon Sep 17 00:00:00 2001 From: bel Date: Wed, 29 Mar 2023 07:45:55 -0600 Subject: [PATCH] finally close reader --- whisper-2023/hotwords.py | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/whisper-2023/hotwords.py b/whisper-2023/hotwords.py index d845a6a..342f79b 100644 --- a/whisper-2023/hotwords.py +++ b/whisper-2023/hotwords.py @@ -70,19 +70,24 @@ class Reader(threading.Thread): def run(self): log("Reader.run: start") - idx = [ - idx for idx,v in enumerate( - sr.Microphone.list_microphone_names(), - ) if v in self.name.split(",") - ][0] - with sr.Microphone(device_index=idx) as mic: - while not self.should_stop(): - try: - self.outq.put(self._run(mic)) - except Exception as e: - if not "timed out" in str(e): - log("Reader.run: error:", e) - self.outq.put(None) + try: + idx = [ + idx for idx,v in enumerate( + sr.Microphone.list_microphone_names(), + ) if v in self.name.split(",") + ][0] + with sr.Microphone(device_index=idx) as mic: + while not self.should_stop(): + try: + self.outq.put(self._run(mic)) + except Exception as e: + if not "timed out" in str(e): + log("Reader.run: error:", e) + except Exception as e: + log("Reader.run panic:", e) + log("microphones:", sr.Microphone.list_microphone_names()) + finally: + self.outq.put(None) log("Reader.run: stop") def should_stop(self):