From 3b8bbb5c5f596a59cebaf578b7fdb4cf9aa89733 Mon Sep 17 00:00:00 2001 From: "will.zhang" Date: Tue, 14 Oct 2025 18:23:28 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=8F=AF?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E7=9A=84TTS=E9=9F=B3=E9=A2=91=E5=8F=91?= =?UTF-8?q?=E9=80=81=E5=BB=B6=E8=BF=9F=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修改 sendAudio() 函数,新增 tts_audio_send_delay 配置参数(毫秒) - 0: 使用原有逻辑 - 大于0: 使用固定延迟发送(降低首包延迟,提升响应速度) --- main/xiaozhi-server/config.yaml | 6 +++ .../core/handle/sendAudioHandle.py | 42 ++++++++++++------- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/main/xiaozhi-server/config.yaml b/main/xiaozhi-server/config.yaml index b2cc9fe6..f8caa762 100644 --- a/main/xiaozhi-server/config.yaml +++ b/main/xiaozhi-server/config.yaml @@ -70,6 +70,12 @@ enable_stop_tts_notify: false # 说完话是否开启提示音,音效地址 stop_tts_notify_voice: "config/assets/tts_notify.mp3" +# TTS音频发送延迟配置(毫秒) +# tts_audio_send_delay: 控制音频包发送间隔 +# 0: 使用精确时间控制,严格匹配音频帧率(默认,运行时按音频帧率计算) +# > 0: 使用固定延迟发送 +tts_audio_send_delay: 0 + exit_commands: - "退出" - "关闭" diff --git a/main/xiaozhi-server/core/handle/sendAudioHandle.py b/main/xiaozhi-server/core/handle/sendAudioHandle.py index 8beda577..a820ee46 100644 --- a/main/xiaozhi-server/core/handle/sendAudioHandle.py +++ b/main/xiaozhi-server/core/handle/sendAudioHandle.py @@ -90,6 +90,9 @@ async def sendAudio(conn, audios, frame_duration=60): if audios is None or len(audios) == 0: return + # 获取发送延迟配置 + send_delay = conn.config.get("tts_audio_send_delay", -1) / 1000.0 + if isinstance(audios, bytes): if conn.client_abort: return @@ -107,16 +110,21 @@ async def sendAudio(conn, audios, frame_duration=60): flow_control = conn.audio_flow_control current_time = time.perf_counter() - # 计算预期发送时间 - expected_time = flow_control["start_time"] + ( - flow_control["packet_count"] * frame_duration / 1000 - ) - delay = expected_time - current_time - if delay > 0: - await asyncio.sleep(delay) + + if send_delay > 0: + # 使用固定延迟 + await asyncio.sleep(send_delay) else: - # 纠正误差 - flow_control["start_time"] += abs(delay) + # 计算预期发送时间 + expected_time = flow_control["start_time"] + ( + flow_control["packet_count"] * 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: # 计算时间戳和序列号 @@ -164,12 +172,16 @@ async def sendAudio(conn, audios, frame_duration=60): # 重置没有声音的状态 conn.last_activity_time = time.time() * 1000 - # 计算预期发送时间 - expected_time = start_time + (play_position / 1000) - current_time = time.perf_counter() - delay = expected_time - current_time - if delay > 0: - await asyncio.sleep(delay) + if send_delay > 0: + # 固定延迟模式 + await asyncio.sleep(send_delay) + else: + # 计算预期发送时间 + expected_time = start_time + (play_position / 1000) + current_time = time.perf_counter() + delay = expected_time - current_time + if delay > 0: + await asyncio.sleep(delay) if conn.conn_from_mqtt_gateway: # 计算时间戳和序列号(使用当前的数据包索引确保连续性) From d00592020e6bd5e52f7e6cba78d3568d41eab909 Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Wed, 22 Oct 2025 11:47:54 +0800 Subject: [PATCH 2/2] Update config.yaml --- main/xiaozhi-server/config.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/main/xiaozhi-server/config.yaml b/main/xiaozhi-server/config.yaml index f8caa762..1fca578c 100644 --- a/main/xiaozhi-server/config.yaml +++ b/main/xiaozhi-server/config.yaml @@ -70,10 +70,10 @@ enable_stop_tts_notify: false # 说完话是否开启提示音,音效地址 stop_tts_notify_voice: "config/assets/tts_notify.mp3" -# TTS音频发送延迟配置(毫秒) +# TTS音频发送延迟配置 # tts_audio_send_delay: 控制音频包发送间隔 # 0: 使用精确时间控制,严格匹配音频帧率(默认,运行时按音频帧率计算) -# > 0: 使用固定延迟发送 +# > 0: 使用固定延迟(毫秒)发送,例如: 60 tts_audio_send_delay: 0 exit_commands: @@ -993,4 +993,4 @@ TTS: # sample_rate: 24000 # 采样率:16000, 8000, 24000 # volume: 50 # 音量:0-100 # speed: 50 # 语速:0-100 - # pitch: 50 # 语调:0-100 \ No newline at end of file + # pitch: 50 # 语调:0-100