update:优化流失前帧发送

This commit is contained in:
hrz
2025-06-08 02:33:07 +08:00
parent 61232e7dda
commit d5fc4d48b4
2 changed files with 13 additions and 9 deletions
@@ -13,15 +13,14 @@ TAG = __name__
async def handleAudioMessage(conn, audio): async def handleAudioMessage(conn, audio):
# 当前片段是否有人说话 # 当前片段是否有人说话
have_voice = conn.vad.is_vad(conn, audio) have_voice = conn.vad.is_vad(conn, audio)
# 如果设备刚刚被唤醒,短暂忽略VAD检测 # 如果设备刚刚被唤醒,短暂忽略VAD检测
if have_voice and hasattr(conn, "just_woken_up") and conn.just_woken_up: if have_voice and hasattr(conn, "just_woken_up") and conn.just_woken_up:
have_voice = False have_voice = False
# 设置一个短暂延迟后恢复VAD检测 # 设置一个短暂延迟后恢复VAD检测
conn.asr_audio.clear() conn.asr_audio.clear()
if not hasattr(conn, "vad_resume_task") or conn.vad_resume_task.done(): if not hasattr(conn, "vad_resume_task") or conn.vad_resume_task.done():
print("clear asr_audio")
conn.vad_resume_task = asyncio.create_task(resume_vad_detection(conn)) conn.vad_resume_task = asyncio.create_task(resume_vad_detection(conn))
return
if have_voice: if have_voice:
if conn.client_is_speaking: if conn.client_is_speaking:
@@ -381,6 +381,7 @@ class TTSProvider(TTSProviderBase):
"""监听TTS响应""" """监听TTS响应"""
opus_datas_cache = [] opus_datas_cache = []
is_first_sentence = True is_first_sentence = True
first_sentence_segment_count = 0 # 添加计数器
try: try:
while not self.conn.stop_event.is_set(): while not self.conn.stop_event.is_set():
try: try:
@@ -402,6 +403,7 @@ class TTSProvider(TTSProviderBase):
(SentenceType.FIRST, [], self.tts_text) (SentenceType.FIRST, [], self.tts_text)
) )
opus_datas_cache = [] opus_datas_cache = []
first_sentence_segment_count = 0 # 重置计数器
elif ( elif (
res.optional.event == EVENT_TTSResponse res.optional.event == EVENT_TTSResponse
and res.header.message_type == AUDIO_ONLY_RESPONSE and res.header.message_type == AUDIO_ONLY_RESPONSE
@@ -412,19 +414,22 @@ class TTSProvider(TTSProviderBase):
f"推送数据到队列里面帧数~~{len(opus_datas)}" f"推送数据到队列里面帧数~~{len(opus_datas)}"
) )
if is_first_sentence: if is_first_sentence:
# 第一句话直接发送 first_sentence_segment_count += 1
self.tts_audio_queue.put( if first_sentence_segment_count <= 6:
(SentenceType.MIDDLE, opus_datas, self.tts_text) self.tts_audio_queue.put(
) (SentenceType.MIDDLE, opus_datas, None)
)
else:
opus_datas_cache = opus_datas_cache + opus_datas
else: else:
# 后续句子缓存 # 后续句子缓存
opus_datas_cache = opus_datas_cache + opus_datas opus_datas_cache = opus_datas_cache + opus_datas
elif res.optional.event == EVENT_TTSSentenceEnd: elif res.optional.event == EVENT_TTSSentenceEnd:
logger.bind(tag=TAG).info(f"句子语音生成成功:{self.tts_text}") logger.bind(tag=TAG).info(f"句子语音生成成功:{self.tts_text}")
if not is_first_sentence: if not is_first_sentence or first_sentence_segment_count > 10:
# 只有非第一句话才发送缓存的数据 # 发送缓存的数据
self.tts_audio_queue.put( self.tts_audio_queue.put(
(SentenceType.MIDDLE, opus_datas_cache, self.tts_text) (SentenceType.MIDDLE, opus_datas_cache, None)
) )
# 第一句话结束后,将标志设置为False # 第一句话结束后,将标志设置为False
is_first_sentence = False is_first_sentence = False