mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-26 17:13:54 +08:00
Merge pull request #2357 from kkroid/feature_tts-audio-send-delay
feat: 添加可配置的TTS音频发送延迟功能
This commit is contained in:
@@ -70,6 +70,12 @@ enable_stop_tts_notify: false
|
|||||||
# 说完话是否开启提示音,音效地址
|
# 说完话是否开启提示音,音效地址
|
||||||
stop_tts_notify_voice: "config/assets/tts_notify.mp3"
|
stop_tts_notify_voice: "config/assets/tts_notify.mp3"
|
||||||
|
|
||||||
|
# TTS音频发送延迟配置
|
||||||
|
# tts_audio_send_delay: 控制音频包发送间隔
|
||||||
|
# 0: 使用精确时间控制,严格匹配音频帧率(默认,运行时按音频帧率计算)
|
||||||
|
# > 0: 使用固定延迟(毫秒)发送,例如: 60
|
||||||
|
tts_audio_send_delay: 0
|
||||||
|
|
||||||
exit_commands:
|
exit_commands:
|
||||||
- "退出"
|
- "退出"
|
||||||
- "关闭"
|
- "关闭"
|
||||||
|
|||||||
@@ -90,6 +90,9 @@ async def sendAudio(conn, audios, frame_duration=60):
|
|||||||
if audios is None or len(audios) == 0:
|
if audios is None or len(audios) == 0:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# 获取发送延迟配置
|
||||||
|
send_delay = conn.config.get("tts_audio_send_delay", -1) / 1000.0
|
||||||
|
|
||||||
if isinstance(audios, bytes):
|
if isinstance(audios, bytes):
|
||||||
if conn.client_abort:
|
if conn.client_abort:
|
||||||
return
|
return
|
||||||
@@ -107,16 +110,21 @@ async def sendAudio(conn, audios, frame_duration=60):
|
|||||||
|
|
||||||
flow_control = conn.audio_flow_control
|
flow_control = conn.audio_flow_control
|
||||||
current_time = time.perf_counter()
|
current_time = time.perf_counter()
|
||||||
# 计算预期发送时间
|
|
||||||
expected_time = flow_control["start_time"] + (
|
if send_delay > 0:
|
||||||
flow_control["packet_count"] * frame_duration / 1000
|
# 使用固定延迟
|
||||||
)
|
await asyncio.sleep(send_delay)
|
||||||
delay = expected_time - current_time
|
|
||||||
if delay > 0:
|
|
||||||
await asyncio.sleep(delay)
|
|
||||||
else:
|
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:
|
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
|
conn.last_activity_time = time.time() * 1000
|
||||||
|
|
||||||
# 计算预期发送时间
|
if send_delay > 0:
|
||||||
expected_time = start_time + (play_position / 1000)
|
# 固定延迟模式
|
||||||
current_time = time.perf_counter()
|
await asyncio.sleep(send_delay)
|
||||||
delay = expected_time - current_time
|
else:
|
||||||
if delay > 0:
|
# 计算预期发送时间
|
||||||
await asyncio.sleep(delay)
|
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:
|
if conn.conn_from_mqtt_gateway:
|
||||||
# 计算时间戳和序列号(使用当前的数据包索引确保连续性)
|
# 计算时间戳和序列号(使用当前的数据包索引确保连续性)
|
||||||
|
|||||||
Reference in New Issue
Block a user