From 49cb98a60654871f6a645d2867079501b851366d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=81=A5=E8=B1=AA?= Date: Mon, 24 Mar 2025 09:28:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=9F=B3=E4=B9=90=E6=92=AD?= =?UTF-8?q?=E6=94=BE=E5=8D=A1=E9=A1=BF=E5=92=8C=E6=96=AD=E8=BF=9E=E7=9A=84?= =?UTF-8?q?bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/handle/sendAudioHandle.py | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/main/xiaozhi-server/core/handle/sendAudioHandle.py b/main/xiaozhi-server/core/handle/sendAudioHandle.py index ffb36398..c0a664eb 100644 --- a/main/xiaozhi-server/core/handle/sendAudioHandle.py +++ b/main/xiaozhi-server/core/handle/sendAudioHandle.py @@ -28,15 +28,15 @@ async def sendAudioMessage(conn, audios, text, text_index=0): # 播放音频 async def sendAudio(conn, audios): # 流控参数优化 - frame_duration = 62 # 帧时长(毫秒),增加余量 + frame_duration = 60 # 帧时长(毫秒),匹配 Opus 编码 start_time = time.perf_counter() - play_position = 0 # 已播放时长(毫秒) + play_position = 0 - # 预发送前 n 帧 - pre_buffer = min(5, len(audios)) + # 预缓冲:发送前 3 帧 + pre_buffer = min(3, len(audios)) for i in range(pre_buffer): await conn.websocket.send(audios[i]) - conn.logger.bind(tag=TAG).debug(f"预缓冲帧 {i}") + conn.logger.bind(tag=TAG).debug(f"预缓冲帧 {i}, 时间: {(time.perf_counter() - start_time) * 1000:.2f}ms") # 正常播放剩余帧 for opus_packet in audios[pre_buffer:]: @@ -50,15 +50,11 @@ async def sendAudio(conn, audios): if delay > 0: await asyncio.sleep(delay) - send_start = time.perf_counter() - await conn.websocket.send(opus_packet) + conn.logger.bind(tag=TAG).debug(f"发送帧,位置: {play_position}ms, 实际间隔: {(time.perf_counter() - current_time) * 1000:.2f}ms") - send_duration = (time.perf_counter() - send_start) * 1000 - logger.bind(tag=TAG).debug(f"发送帧,位置: {play_position}ms, 实际间隔: {(time.perf_counter() - current_time) * 1000:.2f}ms, 发送耗时: {send_duration:.2f}ms") + play_position += frame_duration - # 动态调整下次延迟,补偿发送耗时 - play_position += frame_duration # 更新播放位置 async def send_tts_message(conn, state, text=None):