临时修复丢字现象

This commit is contained in:
Chingfeng Li 2025-09-12 15:44:03 +08:00
parent a9b0775469
commit 25b6e8f905
2 changed files with 17 additions and 16 deletions

View file

@ -24,6 +24,13 @@ async def sendAudioMessage(conn, sentenceType, audios, text):
# 发送结束消息(如果是最后一个文本) # 发送结束消息(如果是最后一个文本)
if conn.llm_finish_task and sentenceType == SentenceType.LAST: if conn.llm_finish_task and sentenceType == SentenceType.LAST:
if hasattr(conn, "audio_flow_control"):
flow_control = conn.audio_flow_control
delay = (flow_control["start_time"] + flow_control["packet_count"] * 60 / 1000) - flow_control["last_send_time"]
conn.logger.bind(tag=TAG).info(f"等待误差后发送stop指令 : {delay}")
if delay > 0:
await asyncio.sleep(delay * 20 + 0.06)
delattr(conn, "audio_flow_control")
await send_tts_message(conn, "stop", None) await send_tts_message(conn, "stop", None)
conn.client_is_speaking = False conn.client_is_speaking = False
if conn.close_after_chat: if conn.close_after_chat:
@ -98,10 +105,11 @@ async def sendAudio(conn, audios, frame_duration=60):
# 获取或初始化流控状态 # 获取或初始化流控状态
if not hasattr(conn, "audio_flow_control"): if not hasattr(conn, "audio_flow_control"):
start_time = time.perf_counter()
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": start_time,
"sequence": 0, # 添加序列号 "sequence": 0, # 添加序列号
} }
@ -114,6 +122,9 @@ async def sendAudio(conn, audios, frame_duration=60):
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:
# 计算时间戳和序列号 # 计算时间戳和序列号

View file

@ -330,21 +330,11 @@ class TTSProviderBase(ABC):
continue continue
# 收到下一个文本开始或会话结束时进行上报 # 收到下一个文本开始或会话结束时进行上报
if sentence_type is not SentenceType.MIDDLE: # 上报TTS数据
# 重置音频流控状态(新句子开始或者结束) if enqueue_text is not None and enqueue_audio is not None:
if hasattr(self.conn, 'audio_flow_control'): enqueue_tts_report(self.conn, enqueue_text, enqueue_audio)
self.conn.audio_flow_control = { enqueue_audio = []
'last_send_time': 0, enqueue_text = text
'packet_count': 0,
'start_time': time.perf_counter(),
'sequence': 0 # 添加序列号
}
# 上报TTS数据
if enqueue_text is not None and enqueue_audio is not None:
enqueue_tts_report(self.conn, enqueue_text, enqueue_audio)
enqueue_audio = []
enqueue_text = text
# 收集上报音频数据 # 收集上报音频数据
if isinstance(audio_datas, bytes) and enqueue_audio is not None: if isinstance(audio_datas, bytes) and enqueue_audio is not None: