From d96c4b7736a1fcbc152c3fce071fb9bc39de529d Mon Sep 17 00:00:00 2001 From: Harold Date: Fri, 19 Dec 2025 18:20:26 +0800 Subject: [PATCH 1/3] feat: silero --- main/xiaozhi-server/core/providers/vad/base.py | 2 +- main/xiaozhi-server/custom_config.yaml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/main/xiaozhi-server/core/providers/vad/base.py b/main/xiaozhi-server/core/providers/vad/base.py index 79515ba9f..b584d8b64 100644 --- a/main/xiaozhi-server/core/providers/vad/base.py +++ b/main/xiaozhi-server/core/providers/vad/base.py @@ -317,7 +317,7 @@ class VADStream(ABC): asr_input_queue.put_nowait(asr_message) logger.bind(tag=TAG).info( - f"ASR LAST message sent: total_speech={event.speech_duration:.2f}s, " + f"ASR LAST message sent: total_speech={event.speech_duration:.0f}ms, " f"audio={asr_message.audio_duration_ms:.0f}ms" ) diff --git a/main/xiaozhi-server/custom_config.yaml b/main/xiaozhi-server/custom_config.yaml index 8375511db..288b5cfe3 100644 --- a/main/xiaozhi-server/custom_config.yaml +++ b/main/xiaozhi-server/custom_config.yaml @@ -567,8 +567,8 @@ TTS: # ============================= Module Selection ================================ # =============================================================================== selected_module: - VAD: FsmnVAD - # VAD: SileroVAD + # VAD: FsmnVAD + VAD: SileroVAD TurnDetection: TenTurnDetection # Set to TenTurnDetection to enable ASR: GroqASR # ASR: ByteplusStreamASR From c437630f74ebf536f1eb9a801d3d79a555e73a07 Mon Sep 17 00:00:00 2001 From: Harold Date: Fri, 19 Dec 2025 18:31:45 +0800 Subject: [PATCH 2/3] feat: rollback --- main/xiaozhi-server/core/providers/asr/base.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/main/xiaozhi-server/core/providers/asr/base.py b/main/xiaozhi-server/core/providers/asr/base.py index 616a4ada4..effc6195c 100644 --- a/main/xiaozhi-server/core/providers/asr/base.py +++ b/main/xiaozhi-server/core/providers/asr/base.py @@ -259,10 +259,10 @@ class ASRProviderBase(ABC): conn.asr_text_buffer = raw_text # Turn Detection: let turn detection handle end of turn - if conn.turn_detection: - # Turn detection will wait for endpoint delay, then call on_end_of_turn - conn.turn_detection.check_end_of_turn(conn) - return + # if conn.turn_detection: + # # Turn detection will wait for endpoint delay, then call on_end_of_turn + # conn.turn_detection.check_end_of_turn(conn) + # return # # No Turn Detection: process immediately # await conn.on_end_of_turn() From 6f6cea860b567ae36f710e61accb03f1f705fb97 Mon Sep 17 00:00:00 2001 From: Harold Date: Sun, 21 Dec 2025 13:55:45 +0800 Subject: [PATCH 3/3] feat: non-streaming interruption doesn't process text length logic --- main/xiaozhi-server/core/connection.py | 65 ++++++++++++------- .../xiaozhi-server/core/providers/asr/base.py | 12 ++-- main/xiaozhi-server/custom_config.yaml | 2 +- 3 files changed, 50 insertions(+), 29 deletions(-) diff --git a/main/xiaozhi-server/core/connection.py b/main/xiaozhi-server/core/connection.py index 9fc827c01..1b8bc4675 100644 --- a/main/xiaozhi-server/core/connection.py +++ b/main/xiaozhi-server/core/connection.py @@ -1535,7 +1535,8 @@ class ConnectionHandler: 2. TTS must be speaking (client_is_speaking = True) 3. Not in manual listen mode 4. Speech duration >= min_interrupt_speech_duration_ms - 5. ASR text buffer length >= min_interrupt_text_length + 5. For streaming ASR: text buffer length >= min_interrupt_text_length + For non-streaming ASR: skip text check (not available during speech) Args: speech_duration_ms: Current speech duration in milliseconds @@ -1547,32 +1548,50 @@ class ConnectionHandler: if self.client_listen_mode == "manual": return - # Check if meeting interruption thresholds - words = tokenize.split_words( - self.asr_text_buffer, - ignore_punctuation=True, - split_character=True, - retain_format=False, - ) - asr_text_len = len(words) + # Check speech duration threshold speech_ok = speech_duration_ms >= self.min_interrupt_speech_duration_ms - text_ok = asr_text_len >= self.min_interrupt_text_length + if not speech_ok: + return - if speech_ok and text_ok: - self.logger.bind(tag=TAG).info( - f"Interrupt triggered: speech={speech_duration_ms:.0f}ms >= {self.min_interrupt_speech_duration_ms:.0f}ms, " + # Check text length threshold (only for streaming ASR) + # Non-streaming ASR doesn't have real-time text during speech + from core.providers.asr.dto import InterfaceType + is_streaming_asr = ( + self.asr is not None + and hasattr(self.asr, 'interface_type') + and self.asr.interface_type == InterfaceType.STREAM + ) + + if is_streaming_asr: + words = tokenize.split_words( + self.asr_text_buffer, + ignore_punctuation=True, + split_character=True, + retain_format=False, + ) + asr_text_len = len(words) + text_ok = asr_text_len >= self.min_interrupt_text_length + if not text_ok: + return + log_msg = ( + f"Interrupt triggered (streaming): speech={speech_duration_ms:.0f}ms, " f"text_len={asr_text_len} >= {self.min_interrupt_text_length}" ) - # Trigger interrupt - self.client_abort = True - self.clear_queues() - # Send stop message to client - async def send_stop_message(): - await self.websocket.send( - json.dumps({"type": "tts", "state": "stop", "session_id": self.session_id}) - ) - asyncio.create_task(send_stop_message()) - self.clearSpeakStatus() + else: + log_msg = f"Interrupt triggered (non-streaming): speech={speech_duration_ms:.0f}ms >= {self.min_interrupt_speech_duration_ms:.0f}ms" + + self.logger.bind(tag=TAG).info(log_msg) + + # Trigger interrupt + self.client_abort = True + self.clear_queues() + # Send stop message to client + async def send_stop_message(): + await self.websocket.send( + json.dumps({"type": "tts", "state": "stop", "session_id": self.session_id}) + ) + asyncio.create_task(send_stop_message()) + self.clearSpeakStatus() def chat_and_close(self, text): """Chat with the user and then close the connection""" diff --git a/main/xiaozhi-server/core/providers/asr/base.py b/main/xiaozhi-server/core/providers/asr/base.py index effc6195c..722b60bd3 100644 --- a/main/xiaozhi-server/core/providers/asr/base.py +++ b/main/xiaozhi-server/core/providers/asr/base.py @@ -14,7 +14,7 @@ from config.logger import setup_logging from typing import Optional, Tuple, List from core.handle.reportHandle import enqueue_asr_report from core.utils.util import remove_punctuation_and_length -from .dto import ASRMessageType, ASRInputMessage +from .dto import ASRMessageType, ASRInputMessage, InterfaceType from queue import Queue, Empty TAG = __name__ @@ -26,6 +26,8 @@ class ASRProviderBase(ABC): def __init__(self): self.asr_input_queue = Queue[ASRInputMessage]() self.asr_input_audio_format = "pcm" + # Default to non-streaming, subclasses can override + self.interface_type = InterfaceType.NON_STREAM # 打开音频通道 async def open_audio_channels(self, conn): @@ -259,10 +261,10 @@ class ASRProviderBase(ABC): conn.asr_text_buffer = raw_text # Turn Detection: let turn detection handle end of turn - # if conn.turn_detection: - # # Turn detection will wait for endpoint delay, then call on_end_of_turn - # conn.turn_detection.check_end_of_turn(conn) - # return + if conn.turn_detection: + # Turn detection will wait for endpoint delay, then call on_end_of_turn + conn.turn_detection.check_end_of_turn(conn) + return # # No Turn Detection: process immediately # await conn.on_end_of_turn() diff --git a/main/xiaozhi-server/custom_config.yaml b/main/xiaozhi-server/custom_config.yaml index 288b5cfe3..15e6a469e 100644 --- a/main/xiaozhi-server/custom_config.yaml +++ b/main/xiaozhi-server/custom_config.yaml @@ -569,7 +569,7 @@ TTS: selected_module: # VAD: FsmnVAD VAD: SileroVAD - TurnDetection: TenTurnDetection # Set to TenTurnDetection to enable + # TurnDetection: TenTurnDetection # Set to TenTurnDetection to enable ASR: GroqASR # ASR: ByteplusStreamASR # LLM: GroqLLM