Update __main__.py

Wrong version of file uploaded.
This commit is contained in:
imonlinux
2025-11-18 11:48:04 -08:00
committed by GitHub
parent 2ccda5ac4a
commit 2fdef7d6ce
+22 -24
View File
@@ -134,9 +134,10 @@ async def main() -> None:
finally:
# --- 10. Cleanup ---
_LOGGER.debug("Shutting down...")
state.shutdown = True # Signal audio thread to stop
state.shutdown = True
audio_thread.join()
# Check for and stop the MQTT controller (set in _init_controllers)
if hasattr(state, "mqtt_controller") and state.mqtt_controller:
_LOGGER.debug("Stopping MQTT controller...")
state.mqtt_controller.stop()
@@ -403,40 +404,37 @@ def _process_audio(state: ServerState, mic, block_size: int):
has_oww = False
last_active: Optional[float] = None
is_muted_flag = False
try:
_LOGGER.debug("Opening audio input device: %s", mic.name)
with mic.recorder(samplerate=16000, channels=1, blocksize=block_size) as mic_in:
while True:
# --- THIS IS THE FIX ---
# Wait while muted. This loop will spin until unmuted.
if state.mic_muted:
_LOGGER.debug("Audio thread muted.")
while state.mic_muted:
if state.shutdown:
_LOGGER.debug("Shutdown signal received, stopping audio thread.")
return # Exit thread
time.sleep(0.1)
break
# --- Just unmuted ---
if state.mic_muted:
if not is_muted_flag:
_LOGGER.debug("Audio thread muted.")
is_muted_flag = True
time.sleep(0.1)
continue
if is_muted_flag:
_LOGGER.debug("Audio thread unmuted, clearing buffers.")
micro_features.reset()
if oww_features is not None:
oww_features.reset()
last_active = time.monotonic() # Start refractory period
last_active = time.monotonic()
# Re-open microphone to flush OS buffer
_LOGGER.debug("Re-opening microphone to flush OS buffers...")
with mic.recorder(samplerate=16000, channels=1, blocksize=block_size) as mic_in:
mic_in.flush() # Discard any stale data
_LOGGER.debug("Microphone flushed.")
# Fall through to the main recording loop
# --- END FIX ---
is_muted_flag = False
_LOGGER.debug("Opening audio input device: %s", mic.name)
with mic.recorder(samplerate=16000, channels=1, blocksize=block_size) as mic_in:
# --- This is now the main recording loop ---
while not state.mic_muted:
if state.shutdown:
_LOGGER.debug("Shutdown signal received, stopping audio thread.")
return # Exit thread
_LOGGER.debug("Flushing stale audio buffer...")
for _ in range(8):
mic_in.record(block_size)
_LOGGER.debug("Stale audio buffer cleared.")
continue
audio_chunk_array = mic_in.record(block_size).reshape(-1)
audio_chunk = (