Stop restarting mic after 20 attempts
@@ -33,6 +33,10 @@ from queue import Queue, Empty
import json
from copy import deepcopy
+
+MAX_MIC_RESTARTS = 20
+
+
AUDIO_DATA = 0
STREAM_START = 1
STREAM_DATA = 2
@@ -70,6 +74,7 @@ class AudioProducer(Thread):
self.stream_handler = stream_handler
def run(self):
+ restart_attempts = 0
with self.mic as source:
self.recognizer.adjust_for_ambient_noise(source)
while self.state.running:
@@ -86,11 +91,20 @@ class AudioProducer(Thread):
# input buffer overflow IOErrors due to not consuming the
# buffers quickly enough will be silently ignored.
LOG.exception('IOError Exception in AudioProducer')
+ if restart_attempts < MAX_MIC_RESTARTS:
+ restart_attempts += 1
LOG.info('Restarting the microphone...')
source.restart()
+ else:
+ LOG.error('Restarting mic doesn\'t seem to work. '
+ 'Stopping...')
+ raise
except Exception:
LOG.exception('Exception in AudioProducer')
raise
+ else:
+ # Reset restart attempt counter on sucessful audio read
+ restart_attempts = 0
finally:
if self.stream_handler is not None:
self.stream_handler.stream_stop()
