Keep audio loop running when Porcupine key is missing

Without this fix, listener_loop exits early on Porcupine init failure,
which starves the sound classifier ring buffer. Now the audio loop
continues for YAMNet classification even without wake word detection.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Alex
2026-04-11 16:56:45 -05:00
parent 73b6793c02
commit f9a25eb5d8

View File

@@ -234,6 +234,7 @@ def listener_loop():
global state, dual_stream global state, dual_stream
logger.info("Initializing Porcupine...") logger.info("Initializing Porcupine...")
porcupine = None
try: try:
porcupine = pvporcupine.create( porcupine = pvporcupine.create(
access_key=PORCUPINE_ACCESS_KEY, access_key=PORCUPINE_ACCESS_KEY,
@@ -241,8 +242,7 @@ def listener_loop():
) )
except Exception as e: except Exception as e:
logger.error(f"Failed to init Porcupine: {e}") logger.error(f"Failed to init Porcupine: {e}")
state.error = str(e) logger.warning("Wake word detection disabled — audio loop continues for classification")
return
vad = webrtcvad.Vad(VAD_AGGRESSIVENESS) vad = webrtcvad.Vad(VAD_AGGRESSIVENESS)
@@ -264,9 +264,6 @@ def listener_loop():
state.active_side = side state.active_side = side
# Convert bytes to int16 array for Porcupine
pcm = struct.unpack_from("h" * 512, frame_data)
# Feed sound classifier ring buffer # Feed sound classifier ring buffer
if sound_ring_buffer is not None: if sound_ring_buffer is not None:
sound_ring_buffer.append(frame_data) sound_ring_buffer.append(frame_data)
@@ -275,7 +272,10 @@ def listener_loop():
if enrollment_buffer is not None: if enrollment_buffer is not None:
enrollment_buffer.append(frame_data) enrollment_buffer.append(frame_data)
# Check for wake word # Check for wake word (skip if Porcupine not available)
keyword_index = -1
if porcupine:
pcm = struct.unpack_from("h" * 512, frame_data)
keyword_index = porcupine.process(pcm) keyword_index = porcupine.process(pcm)
if keyword_index >= 0 and not is_recording: if keyword_index >= 0 and not is_recording:
@@ -345,6 +345,7 @@ def listener_loop():
logger.error(f"Listener error: {e}") logger.error(f"Listener error: {e}")
state.error = str(e) state.error = str(e)
finally: finally:
if porcupine:
porcupine.delete() porcupine.delete()
state.listening = False state.listening = False
leds_off() leds_off()