From 44587985dd998af4de9c9fc5ec56fc20010b3fad Mon Sep 17 00:00:00 2001 From: 3030332422 <3030332422@qq.com> Date: Wed, 22 Apr 2026 11:42:22 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=E5=94=A4=E9=86=92?= =?UTF-8?q?=E8=AF=8D=E9=9F=B3=E9=A2=91=E9=87=87=E9=9B=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test/wakeword_runtime/core/microphone.py | 41 ++++--------------- 1 file changed, 8 insertions(+), 33 deletions(-) diff --git a/main/xiaozhi-server/test/wakeword_runtime/core/microphone.py b/main/xiaozhi-server/test/wakeword_runtime/core/microphone.py index 5b15c29d..b6bbbbcf 100644 --- a/main/xiaozhi-server/test/wakeword_runtime/core/microphone.py +++ b/main/xiaozhi-server/test/wakeword_runtime/core/microphone.py @@ -1,6 +1,4 @@ import logging -import threading -from collections import deque from typing import Protocol import numpy as np @@ -16,7 +14,6 @@ class AudioListener(Protocol): class MicrophoneListener: - _MAX_QUEUE_SIZE = 10 def __init__(self, config: RuntimeConfig) -> None: self.config = config @@ -26,11 +23,8 @@ class MicrophoneListener: self._sample_rate = self.config.audio.sample_rate self._channels = self.config.audio.channels self._device = self.config.audio.input_device - self._block_duration_ms = 100 + self._block_duration_ms = 30 self._block_size = int(self._sample_rate * (self._block_duration_ms / 1000)) - self._audio_queue: deque = deque(maxlen=self._MAX_QUEUE_SIZE) - self._queue_event = threading.Event() - self._consumer_thread = None def add_audio_listener(self, listener: AudioListener) -> None: if listener not in self._listeners: @@ -48,13 +42,6 @@ class MicrophoneListener: "Missing dependency: sounddevice. Install runtime dependencies before starting microphone listener." ) from exc - self._running = True - self._audio_queue.clear() - self._consumer_thread = threading.Thread( - target=self._consume_loop, daemon=True, name="mic-consumer" - ) - self._consumer_thread.start() - self._stream = sd.InputStream( device=self._device, samplerate=self._sample_rate, @@ -65,6 +52,7 @@ class MicrophoneListener: latency="low", ) self._stream.start() + self._running = True logger.info("microphone listener started") logger.info("microphone sample rate: %s", self._sample_rate) @@ -77,10 +65,6 @@ class MicrophoneListener: return self._running = False - self._queue_event.set() - if self._consumer_thread is not None: - self._consumer_thread.join(timeout=2) - self._consumer_thread = None if self._stream is not None: try: self._stream.stop() @@ -91,7 +75,7 @@ class MicrophoneListener: def _input_callback(self, indata, frames, time_info, status) -> None: _ = frames, time_info - if status: + if status and "overflow" not in str(status).lower(): logger.warning("microphone status: %s", status) audio = np.copy(indata) @@ -100,17 +84,8 @@ class MicrophoneListener: else: audio = audio.reshape(-1) - self._audio_queue.append(audio) - self._queue_event.set() - - def _consume_loop(self) -> None: - while self._running: - self._queue_event.wait(timeout=0.5) - self._queue_event.clear() - while self._audio_queue: - audio = self._audio_queue.popleft() - for listener in list(self._listeners): - try: - listener.on_audio_data(audio) - except Exception: - logger.exception("audio listener callback failed") + for listener in list(self._listeners): + try: + listener.on_audio_data(audio) + except Exception: + logger.exception("audio listener callback failed")