diff --git a/main/xiaozhi-server/core/connection.py b/main/xiaozhi-server/core/connection.py index 86c88617a..dceecdb1e 100644 --- a/main/xiaozhi-server/core/connection.py +++ b/main/xiaozhi-server/core/connection.py @@ -914,6 +914,10 @@ class ConnectionHandler: """ self.logger.bind(tag=TAG).info(f"大模型收到用户消息: {query}") + # 记录 LLM 开始处理时间 + llm_start_time = time.time() * 1000 + llm_first_token_time = None + # 检查 TTS 是否已初始化 if self.tts is None: self.logger.bind(tag=TAG).error("TTS 未初始化,无法处理聊天请求") @@ -1016,6 +1020,21 @@ class ConnectionHandler: else: content = response + # 记录首个 token 时间(首字延迟) + if llm_first_token_time is None and content is not None and len(content) > 0: + llm_first_token_time = time.time() * 1000 + first_token_delay = llm_first_token_time - llm_start_time + + # 计算从用户说完到首 token 的延迟 + e2e_first_token = 0 + if hasattr(self, '_latency_voice_end_time'): + e2e_first_token = llm_first_token_time - self._latency_voice_end_time + + self.logger.bind(tag=TAG).info( + f"🤖 [延迟追踪] LLM首token: {first_token_delay:.0f}ms | " + f"用户说完→首token: {e2e_first_token:.0f}ms" + ) + # 在llm回复中获取情绪表情,一轮对话只在开头获取一次 # if emotion_flag and content is not None and content.strip(): # asyncio.run_coroutine_threadsafe( @@ -1083,6 +1102,20 @@ class ConnectionHandler: ).result() self._handle_function_result(result, function_call_data, depth=depth) + # 记录 LLM 完成时间 + llm_end_time = time.time() * 1000 + llm_total_delay = llm_end_time - llm_start_time + + # 计算从用户说完到 LLM 完成的延迟 + e2e_llm_complete = 0 + if hasattr(self, '_latency_voice_end_time'): + e2e_llm_complete = llm_end_time - self._latency_voice_end_time + + self.logger.bind(tag=TAG).info( + f"🤖 [延迟追踪] LLM完成: {llm_total_delay:.0f}ms | " + f"用户说完→LLM完成: {e2e_llm_complete:.0f}ms" + ) + # 存储对话内容 if len(response_message) > 0: text_buff = "".join(response_message) diff --git a/main/xiaozhi-server/core/handle/receiveAudioHandle.py b/main/xiaozhi-server/core/handle/receiveAudioHandle.py index 1039f3b64..8bbe8e22d 100644 --- a/main/xiaozhi-server/core/handle/receiveAudioHandle.py +++ b/main/xiaozhi-server/core/handle/receiveAudioHandle.py @@ -512,6 +512,7 @@ async def handleAudioMessage(conn, audio): # conn._continuous_voice_count = 0 if have_voice: if conn.client_is_speaking and conn.client_listen_mode != "manual": + logger.bind(tag=TAG).info("检测到语音,触发打断") await handleAbortMessage(conn) # 设备长时间空闲检测,用于say goodbye diff --git a/main/xiaozhi-server/core/handle/sendAudioHandle.py b/main/xiaozhi-server/core/handle/sendAudioHandle.py index a820ee46a..c0c8c5e76 100644 --- a/main/xiaozhi-server/core/handle/sendAudioHandle.py +++ b/main/xiaozhi-server/core/handle/sendAudioHandle.py @@ -10,9 +10,20 @@ TAG = __name__ async def sendAudioMessage(conn, sentenceType, audios, text): if conn.tts.tts_audio_first_sentence: - conn.logger.bind(tag=TAG).info(f"发送第一段语音: {text}") conn.tts.tts_audio_first_sentence = False await send_tts_message(conn, "start", None) + + # 记录首句 TTS 播放时间(端到端延迟的终点) + first_audio_time = time.time() * 1000 + + # 计算端到端延迟 + if hasattr(conn, '_latency_voice_end_time'): + e2e_total_delay = first_audio_time - conn._latency_voice_end_time + conn.logger.bind(tag=TAG).info( + f"🔊 [延迟追踪] 首句TTS开始播放 | " + f"⏱️ 端到端总延迟: {e2e_total_delay:.0f}ms (用户说完→首句播放) | " + f"文本: {text if text else '(无文本)'}" + ) if sentenceType == SentenceType.FIRST: await send_tts_message(conn, "sentence_start", text) diff --git a/main/xiaozhi-server/core/providers/asr/base.py b/main/xiaozhi-server/core/providers/asr/base.py index 2562f5895..f4d731588 100644 --- a/main/xiaozhi-server/core/providers/asr/base.py +++ b/main/xiaozhi-server/core/providers/asr/base.py @@ -101,7 +101,17 @@ class ASRProviderBase(ABC): self.speech_to_text(asr_audio_task, conn.session_id, conn.audio_format) ) end_time = time.monotonic() - logger.bind(tag=TAG).info(f"ASR耗时: {end_time - start_time:.3f}s") + asr_elapsed_ms = (end_time - start_time) * 1000 + + # 计算从用户说完到 ASR 完成的延迟 + e2e_asr_delay = 0 + if hasattr(conn, '_latency_voice_end_time'): + e2e_asr_delay = time.time() * 1000 - conn._latency_voice_end_time + + logger.bind(tag=TAG).info( + f"🎙️ [延迟追踪] ASR完成: {asr_elapsed_ms:.0f}ms | " + f"用户说完→ASR完成: {e2e_asr_delay:.0f}ms" + ) return result finally: loop.close() diff --git a/main/xiaozhi-server/core/providers/tts/fish_dual_stream.py b/main/xiaozhi-server/core/providers/tts/fish_dual_stream.py index 267fdccc0..7b6297aa1 100644 --- a/main/xiaozhi-server/core/providers/tts/fish_dual_stream.py +++ b/main/xiaozhi-server/core/providers/tts/fish_dual_stream.py @@ -158,6 +158,9 @@ class TTSProvider(TTSProviderBase): if message.sentence_type == SentenceType.FIRST: # ========== New dialogue round starts ========== self.conn.client_abort = False + self.tts_audio_first_sentence = True + self._first_sent = False + self._session_text_buffer = [] elif self.conn.client_abort: # ========== Interruption ========== diff --git a/main/xiaozhi-server/core/providers/vad/silero.py b/main/xiaozhi-server/core/providers/vad/silero.py index 9270e4bef..74abc212c 100644 --- a/main/xiaozhi-server/core/providers/vad/silero.py +++ b/main/xiaozhi-server/core/providers/vad/silero.py @@ -22,19 +22,19 @@ class VADProvider(VADProviderBase): self.decoder = opuslib_next.Decoder(16000, 1) # 处理空字符串的情况 - threshold = config.get("threshold", "0.5") - threshold_low = config.get("threshold_low", "0.2") - min_silence_duration_ms = config.get("min_silence_duration_ms", "1000") + threshold = config.get("threshold", "0.6") + threshold_low = config.get("threshold_low", "0.3") + min_silence_duration_ms = config.get("min_silence_duration_ms", "200") self.vad_threshold = float(threshold) if threshold else 0.5 - self.vad_threshold_low = float(threshold_low) if threshold_low else 0.2 + self.vad_threshold_low = float(threshold_low) if threshold_low else 0.3 self.silence_threshold_ms = ( - int(min_silence_duration_ms) if min_silence_duration_ms else 1000 + int(min_silence_duration_ms) if min_silence_duration_ms else 200 ) # 至少要多少帧才算有语音 - self.frame_window_threshold = 3 + self.frame_window_threshold = 5 def is_vad(self, conn, opus_packet): try: @@ -74,12 +74,23 @@ class VADProvider(VADProviderBase): conn.client_voice_window.count(True) >= self.frame_window_threshold ) + # 检测语音开始(边缘检测) + if not conn.client_have_voice and client_have_voice: + conn._latency_voice_start_time = time.time() * 1000 + logger.bind(tag=TAG).info(f"🎤 [延迟追踪] 用户开始说话") + # 如果之前有声音,但本次没有声音,且与上次有声音的时间差已经超过了静默阈值,则认为已经说完一句话 if conn.client_have_voice and not client_have_voice: stop_duration = time.time() * 1000 - conn.last_activity_time if stop_duration >= self.silence_threshold_ms: if conn.client_listen_mode != "manual": conn.client_voice_stop = True + # 记录用户说完的时间(端到端延迟的起点) + conn._latency_voice_end_time = time.time() * 1000 + voice_duration = conn._latency_voice_end_time - conn._latency_voice_start_time + logger.bind(tag=TAG).info( + f"🎤 [延迟追踪] 用户说完,语音时长: {voice_duration:.0f}ms" + ) if client_have_voice: conn.client_have_voice = True conn.last_activity_time = time.time() * 1000 diff --git a/main/xiaozhi-server/custom_config.yaml b/main/xiaozhi-server/custom_config.yaml index cbad3d93c..94053b62b 100644 --- a/main/xiaozhi-server/custom_config.yaml +++ b/main/xiaozhi-server/custom_config.yaml @@ -476,7 +476,7 @@ ASR: VAD: SileroVAD: type: silero - threshold: 0.5 + threshold: 0.6 threshold_low: 0.3 model_dir: models/snakers4_silero-vad min_silence_duration_ms: 200 # 如果说话停顿比较长,可以把这个值设置大一些