From ec07088c13257cba8f0bad0be1c76f5d547ac76a Mon Sep 17 00:00:00 2001 From: Sakura-RanChen <1908198662@qq.com> Date: Sat, 1 Nov 2025 17:59:16 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=B5=81=E6=8E=A7=E7=8A=B6=E6=80=81?= =?UTF-8?q?=E6=9C=AA=E9=87=8D=E7=BD=AE=20update:=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=B5=81=E5=BC=8F=E9=A2=84=E7=BC=93=E5=86=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/handle/sendAudioHandle.py | 33 ++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/main/xiaozhi-server/core/handle/sendAudioHandle.py b/main/xiaozhi-server/core/handle/sendAudioHandle.py index a820ee46..97573373 100644 --- a/main/xiaozhi-server/core/handle/sendAudioHandle.py +++ b/main/xiaozhi-server/core/handle/sendAudioHandle.py @@ -94,37 +94,40 @@ async def sendAudio(conn, audios, frame_duration=60): send_delay = conn.config.get("tts_audio_send_delay", -1) / 1000.0 if isinstance(audios, bytes): - if conn.client_abort: - return - - conn.last_activity_time = time.time() * 1000 - - # 获取或初始化流控状态 - if not hasattr(conn, "audio_flow_control"): + # 重置流控状态,第一次读取和会话发生转变时 + if not hasattr(conn, "audio_flow_control") or conn.audio_flow_control.get("sentence_id") != conn.sentence_id: conn.audio_flow_control = { "last_send_time": 0, "packet_count": 0, "start_time": time.perf_counter(), "sequence": 0, # 添加序列号 + "sentence_id": conn.sentence_id, } + if conn.client_abort: + return + + conn.last_activity_time = time.time() * 1000 + + # 预缓冲:前5个包直接发送,不做延迟 + pre_buffer_count = 5 flow_control = conn.audio_flow_control current_time = time.perf_counter() - - if send_delay > 0: + + if flow_control["packet_count"] < pre_buffer_count: + # 预缓冲阶段,直接发送不延迟 + pass + elif send_delay > 0: # 使用固定延迟 await asyncio.sleep(send_delay) else: - # 计算预期发送时间 + effective_packet = flow_control["packet_count"] - pre_buffer_count expected_time = flow_control["start_time"] + ( - flow_control["packet_count"] * frame_duration / 1000 + effective_packet * frame_duration / 1000 ) 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: # 计算时间戳和序列号 @@ -150,7 +153,7 @@ async def sendAudio(conn, audios, frame_duration=60): play_position = 0 # 执行预缓冲 - pre_buffer_frames = min(3, len(audios)) + pre_buffer_frames = min(5, len(audios)) for i in range(pre_buffer_frames): if conn.conn_from_mqtt_gateway: # 计算时间戳和序列号