Added error checking for alsa read errors

This commit is contained in:
Pünkösd Marcell 2020-11-12 16:10:25 +01:00
parent 5682590ced
commit d7e879f312
1 changed files with 16 additions and 1 deletions

View File

@ -1,3 +1,4 @@
import logging
from typing import Optional, Tuple
import alsaaudio
import tempfile
@ -44,10 +45,24 @@ class SlicedRecorder(Thread):
current_working_file_name, current_working_file = self._request_new_file()
current_samples_saved = 0
read_failures = 0
while self._active:
length, data = self._inp.read()
try:
length, data = self._inp.read()
except alsaaudio.ALSAAudioError as e:
logging.exception(e)
read_failures += 1
if read_failures >= 30:
logging.error("Too many ALSA Read errors. Bailing out...")
self._active = False
return
continue
if length > 0: # will be always larger than zero (except on error)
read_failures = 0
file_size_after_append = current_samples_saved + length
if file_size_after_append > self._samples_per_slice: # Appending this would cause a too big slice