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