From 3adc1d0efa63aa2e2dca3e136b06708589c405ac Mon Sep 17 00:00:00 2001 From: bel Date: Wed, 22 Mar 2023 19:42:13 -0600 Subject: [PATCH] k --- whisper-2023/microphone_recognition.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/whisper-2023/microphone_recognition.py b/whisper-2023/microphone_recognition.py index 29597e7..357cc96 100644 --- a/whisper-2023/microphone_recognition.py +++ b/whisper-2023/microphone_recognition.py @@ -20,6 +20,8 @@ class Recognizer(threading.Thread): def run(self): while True: got = self.q.get() + if not got: + break self.one(got) def one(self, audio): @@ -66,18 +68,29 @@ class Listener(): return mic def run(self): + try: + return self._run() + except Exception: + return None + + def _run(self): mic_timeout=int(environ.get("MIC_TIMEOUT", 5)) # obtain audio from the microphone r = sr.Recognizer() - return r.listen(self.mic(), timeout=mic_timeout, phrase_time_limit=mic_timeout) + #return r.listen(self.mic(), timeout=mic_timeout, phrase_time_limit=mic_timeout) + return r.record(self.mic(), duration=mic_timeout) q = queue.Queue(maxsize=1) l = Listener() t = Recognizer(q) t.start() -while True: - got = l.run() - q.put(got) - -t.join() +try: + while True: + got = l.run() + q.put(got) +except Exception: + pass +finally: + l._mic.__exit__(None, None, None) + t.join()