From f198e28d80b72b35293486455247d08d5efe07fd Mon Sep 17 00:00:00 2001 From: Michael Hansen Date: Tue, 16 Jan 2024 15:47:03 -0600 Subject: [PATCH] Better errors for mic autodetect --- installer/const.py | 2 +- installer/microphone.py | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/installer/const.py b/installer/const.py index 3697890..61318dc 100644 --- a/installer/const.py +++ b/installer/const.py @@ -16,7 +16,7 @@ LOCAL_DIR = PROGRAM_DIR / "local" SETTINGS_PATH = LOCAL_DIR / "settings.json" SERVICES_DIR = LOCAL_DIR / "services" -RECORD_SECONDS = 8 +RECORD_SECONDS = 5 RECORD_RMS_MIN = 30 TITLE = "Wyoming Satellite" diff --git a/installer/microphone.py b/installer/microphone.py index 27d39cc..de94000 100644 --- a/installer/microphone.py +++ b/installer/microphone.py @@ -31,6 +31,11 @@ def configure_microphone(settings: Settings) -> None: gauge("Speak loudly into the microphone.", RECORD_SECONDS) for device, future in futures.items(): device_rms = future.result() + if device_rms is None: + _LOGGER.warning("Failed to record from microphone %s", device) + continue + + _LOGGER.debug("Microphone %s got RMS %s (min: %s)", device, device_rms, RECORD_RMS_MIN) if device_rms < RECORD_RMS_MIN: continue @@ -43,7 +48,8 @@ def configure_microphone(settings: Settings) -> None: settings.mic.device = best_device settings.save() else: - msgbox("Audio was not detected from any microphone") + msgbox("Audio was not detected from any microphone.\n" + "If a satellite is currently running, you may need to stop it.") elif choice == "list": # arecord -L microphone_device = radiolist( @@ -90,9 +96,9 @@ def get_microphone_devices() -> List[str]: return devices -def _record_proc(device: str) -> float: +def _record_proc(device: str) -> Optional[float]: try: - audio = subprocess.check_output( + proc = subprocess.Popen( [ "arecord", "-q", @@ -109,8 +115,13 @@ def _record_proc(device: str) -> float: "-d", str(RECORD_SECONDS), ], - stderr=subprocess.DEVNULL, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, ) + audio, stderr = proc.communicate() + if proc.returncode != 0: + _LOGGER.error("Error recording from device %s: %s", device, stderr.decode("utf-8")) + return None # 16-bit mono audio_array = array.array("h", audio) @@ -120,6 +131,7 @@ def _record_proc(device: str) -> float: return rms except Exception: _LOGGER.exception("Error recording from device: %s", device) + return None return 0