mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-28 01:53:53 +08:00
@@ -94,37 +94,40 @@ async def sendAudio(conn, audios, frame_duration=60):
|
|||||||
send_delay = conn.config.get("tts_audio_send_delay", -1) / 1000.0
|
send_delay = conn.config.get("tts_audio_send_delay", -1) / 1000.0
|
||||||
|
|
||||||
if isinstance(audios, bytes):
|
if isinstance(audios, bytes):
|
||||||
if conn.client_abort:
|
# 重置流控状态,第一次读取和会话发生转变时
|
||||||
return
|
if not hasattr(conn, "audio_flow_control") or conn.audio_flow_control.get("sentence_id") != conn.sentence_id:
|
||||||
|
|
||||||
conn.last_activity_time = time.time() * 1000
|
|
||||||
|
|
||||||
# 获取或初始化流控状态
|
|
||||||
if not hasattr(conn, "audio_flow_control"):
|
|
||||||
conn.audio_flow_control = {
|
conn.audio_flow_control = {
|
||||||
"last_send_time": 0,
|
"last_send_time": 0,
|
||||||
"packet_count": 0,
|
"packet_count": 0,
|
||||||
"start_time": time.perf_counter(),
|
"start_time": time.perf_counter(),
|
||||||
"sequence": 0, # 添加序列号
|
"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
|
flow_control = conn.audio_flow_control
|
||||||
current_time = time.perf_counter()
|
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)
|
await asyncio.sleep(send_delay)
|
||||||
else:
|
else:
|
||||||
# 计算预期发送时间
|
effective_packet = flow_control["packet_count"] - pre_buffer_count
|
||||||
expected_time = flow_control["start_time"] + (
|
expected_time = flow_control["start_time"] + (
|
||||||
flow_control["packet_count"] * frame_duration / 1000
|
effective_packet * frame_duration / 1000
|
||||||
)
|
)
|
||||||
delay = expected_time - current_time
|
delay = expected_time - current_time
|
||||||
if delay > 0:
|
if delay > 0:
|
||||||
await asyncio.sleep(delay)
|
await asyncio.sleep(delay)
|
||||||
else:
|
|
||||||
# 纠正误差
|
|
||||||
flow_control["start_time"] += abs(delay)
|
|
||||||
|
|
||||||
if conn.conn_from_mqtt_gateway:
|
if conn.conn_from_mqtt_gateway:
|
||||||
# 计算时间戳和序列号
|
# 计算时间戳和序列号
|
||||||
@@ -150,7 +153,7 @@ async def sendAudio(conn, audios, frame_duration=60):
|
|||||||
play_position = 0
|
play_position = 0
|
||||||
|
|
||||||
# 执行预缓冲
|
# 执行预缓冲
|
||||||
pre_buffer_frames = min(3, len(audios))
|
pre_buffer_frames = min(5, len(audios))
|
||||||
for i in range(pre_buffer_frames):
|
for i in range(pre_buffer_frames):
|
||||||
if conn.conn_from_mqtt_gateway:
|
if conn.conn_from_mqtt_gateway:
|
||||||
# 计算时间戳和序列号
|
# 计算时间戳和序列号
|
||||||
|
|||||||
Reference in New Issue
Block a user