diff --git a/main/xiaozhi-server/core/handle/sendAudioHandle.py b/main/xiaozhi-server/core/handle/sendAudioHandle.py index d159d3fff..b3a433c38 100644 --- a/main/xiaozhi-server/core/handle/sendAudioHandle.py +++ b/main/xiaozhi-server/core/handle/sendAudioHandle.py @@ -24,6 +24,13 @@ async def sendAudioMessage(conn, sentenceType, audios, text): # 发送结束消息(如果是最后一个文本) if conn.llm_finish_task and sentenceType == SentenceType.LAST: + if hasattr(conn, "audio_flow_control"): + flow_control = conn.audio_flow_control + delay = (flow_control["start_time"] + flow_control["packet_count"] * 60 / 1000) - flow_control["last_send_time"] + conn.logger.bind(tag=TAG).info(f"等待误差后发送stop指令 : {delay}") + if delay > 0: + await asyncio.sleep(delay * 20 + 0.06) + delattr(conn, "audio_flow_control") await send_tts_message(conn, "stop", None) conn.client_is_speaking = False if conn.close_after_chat: @@ -98,10 +105,11 @@ async def sendAudio(conn, audios, frame_duration=60): # 获取或初始化流控状态 if not hasattr(conn, "audio_flow_control"): + start_time = time.perf_counter() conn.audio_flow_control = { "last_send_time": 0, "packet_count": 0, - "start_time": time.perf_counter(), + "start_time": start_time, "sequence": 0, # 添加序列号 } @@ -114,6 +122,9 @@ async def sendAudio(conn, audios, frame_duration=60): delay = expected_time - current_time if delay > 0: await asyncio.sleep(delay) + else: + # 纠正误差 + flow_control["start_time"] += abs(delay) if conn.conn_from_mqtt_gateway: # 计算时间戳和序列号 diff --git a/main/xiaozhi-server/core/providers/tts/base.py b/main/xiaozhi-server/core/providers/tts/base.py index c4b323551..b331c8938 100644 --- a/main/xiaozhi-server/core/providers/tts/base.py +++ b/main/xiaozhi-server/core/providers/tts/base.py @@ -330,21 +330,11 @@ class TTSProviderBase(ABC): continue # 收到下一个文本开始或会话结束时进行上报 - if sentence_type is not SentenceType.MIDDLE: - # 重置音频流控状态(新句子开始或者结束) - if hasattr(self.conn, 'audio_flow_control'): - self.conn.audio_flow_control = { - 'last_send_time': 0, - 'packet_count': 0, - 'start_time': time.perf_counter(), - 'sequence': 0 # 添加序列号 - } - - # 上报TTS数据 - if enqueue_text is not None and enqueue_audio is not None: - enqueue_tts_report(self.conn, enqueue_text, enqueue_audio) - enqueue_audio = [] - enqueue_text = text + # 上报TTS数据 + if enqueue_text is not None and enqueue_audio is not None: + enqueue_tts_report(self.conn, enqueue_text, enqueue_audio) + enqueue_audio = [] + enqueue_text = text # 收集上报音频数据 if isinstance(audio_datas, bytes) and enqueue_audio is not None: