From 547efab22847d00360fc740a72a70da9c9ebd892 Mon Sep 17 00:00:00 2001 From: rahulpscraftech360 Date: Sat, 16 Aug 2025 15:57:44 +0530 Subject: [PATCH] Increased Minimum Audio Chunks, and No more missing the beginning of actual speech (pre-buffer captures it) --- main/xiaozhi-server/core/connection.py | 7 +++++++ main/xiaozhi-server/core/handle/receiveAudioHandle.py | 11 +++++++++++ main/xiaozhi-server/core/handle/textHandle.py | 4 ++++ main/xiaozhi-server/core/providers/asr/base.py | 11 ++++++++++- 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/main/xiaozhi-server/core/connection.py b/main/xiaozhi-server/core/connection.py index 7ce99a53f..dcd786acf 100644 --- a/main/xiaozhi-server/core/connection.py +++ b/main/xiaozhi-server/core/connection.py @@ -127,6 +127,10 @@ class ConnectionHandler: # Therefore, variables related to ASR need to be defined here as private variables of the connection. self.asr_audio = [] self.asr_audio_queue = queue.Queue() + + # Pre-buffer to store audio chunks before voice detection + # This helps capture the beginning of speech that might be missed + self.audio_pre_buffer = deque(maxlen=10) # Store last 10 chunks (600ms) before voice detection # LLM related variables self.llm_finish_task = True @@ -1071,6 +1075,9 @@ class ConnectionHandler: self.client_audio_buffer = bytearray() self.client_have_voice = False self.client_voice_stop = False + # Clear pre-buffer when resetting VAD states + if hasattr(self, 'audio_pre_buffer'): + self.audio_pre_buffer.clear() self.logger.bind(tag=TAG).debug("VAD states reset.") def chat_and_close(self, text): diff --git a/main/xiaozhi-server/core/handle/receiveAudioHandle.py b/main/xiaozhi-server/core/handle/receiveAudioHandle.py index c8868d5b6..2186b68d6 100644 --- a/main/xiaozhi-server/core/handle/receiveAudioHandle.py +++ b/main/xiaozhi-server/core/handle/receiveAudioHandle.py @@ -14,6 +14,17 @@ TAG = __name__ async def handleAudioMessage(conn, audio): # Whether the current segment has someone speaking have_voice = conn.vad.is_vad(conn, audio) + + # Check if this is the initial connection period (ignore first 1 second of audio) + if have_voice and hasattr(conn, "initial_connection_handled") and not conn.initial_connection_handled: + current_time = asyncio.get_event_loop().time() + if current_time - conn.initial_connection_time < 1.0: # Ignore first 1 second + have_voice = False + conn.asr_audio.clear() # Clear any accumulated audio + return + else: + conn.initial_connection_handled = True + # If the device was just woken up, briefly ignore VAD detection if have_voice and hasattr(conn, "just_woken_up") and conn.just_woken_up: have_voice = False diff --git a/main/xiaozhi-server/core/handle/textHandle.py b/main/xiaozhi-server/core/handle/textHandle.py index 19a48f97b..1b4df24a0 100644 --- a/main/xiaozhi-server/core/handle/textHandle.py +++ b/main/xiaozhi-server/core/handle/textHandle.py @@ -38,6 +38,10 @@ async def handleTextMessage(conn, message): f"Client audio capture mode: {conn.client_listen_mode}" ) if msg_json["state"] == "start": + # Add initial connection flag to prevent false positive on first audio + if not hasattr(conn, "initial_connection_handled"): + conn.initial_connection_handled = False + conn.initial_connection_time = asyncio.get_event_loop().time() conn.client_have_voice = True conn.client_voice_stop = False elif msg_json["state"] == "stop": diff --git a/main/xiaozhi-server/core/providers/asr/base.py b/main/xiaozhi-server/core/providers/asr/base.py index 7c680e998..b2e7423cd 100644 --- a/main/xiaozhi-server/core/providers/asr/base.py +++ b/main/xiaozhi-server/core/providers/asr/base.py @@ -58,6 +58,15 @@ class ASRProviderBase(ABC): else: have_voice = conn.client_have_voice + # Always add audio to pre-buffer (rolling buffer) + conn.audio_pre_buffer.append(audio) + + # If voice is detected and we're not already collecting + if have_voice and not conn.client_have_voice: + # Add pre-buffered audio to the beginning of ASR audio + conn.asr_audio = list(conn.audio_pre_buffer) + conn.asr_audio + conn.audio_pre_buffer.clear() + conn.asr_audio.append(audio) if not have_voice and not conn.client_have_voice: conn.asr_audio = conn.asr_audio[-10:] @@ -68,7 +77,7 @@ class ASRProviderBase(ABC): conn.asr_audio.clear() conn.reset_vad_states() - if len(asr_audio_task) > 15: + if len(asr_audio_task) > 20: # Increased from 15 to 20 chunks (1.2 seconds) await self.handle_voice_stop(conn, asr_audio_task) # Handle voice stop