From c3c2e7c28e565479c106d8d294005d1637df5d5a Mon Sep 17 00:00:00 2001 From: Sakura-RanChen <1908198662@qq.com> Date: Tue, 2 Sep 2025 12:00:04 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=20=E6=8F=90=E7=A4=BA=E5=B0=BE=E9=9F=B3?= =?UTF-8?q?=E6=92=AD=E6=94=BE=E5=92=8C=E5=8D=95=E6=A8=A1=E5=9D=97=E7=AC=AC?= =?UTF-8?q?=E4=B8=80=E6=AC=A1=E5=94=A4=E9=86=92=E6=97=B6=E7=AD=89=E5=BE=85?= =?UTF-8?q?tts=E5=88=9D=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xiaozhi-server/core/handle/helloHandle.py | 11 ++++++++- .../core/handle/sendAudioHandle.py | 23 ++++++++----------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/main/xiaozhi-server/core/handle/helloHandle.py b/main/xiaozhi-server/core/handle/helloHandle.py index bb307b87..5b5818e2 100644 --- a/main/xiaozhi-server/core/handle/helloHandle.py +++ b/main/xiaozhi-server/core/handle/helloHandle.py @@ -56,7 +56,16 @@ async def checkWakeupWords(conn, text): "enable_wakeup_words_response_cache" ] - if not enable_wakeup_words_response_cache or not conn.tts: + # 等待tts初始化,最多等待3秒 + start_time = time.time() + while time.time() - start_time < 3: + if conn.tts: + break + await asyncio.sleep(0.1) + else: + return False + + if not enable_wakeup_words_response_cache: return False _, filtered_text = remove_punctuation_and_length(text) diff --git a/main/xiaozhi-server/core/handle/sendAudioHandle.py b/main/xiaozhi-server/core/handle/sendAudioHandle.py index f482a1ef..bafaf16e 100644 --- a/main/xiaozhi-server/core/handle/sendAudioHandle.py +++ b/main/xiaozhi-server/core/handle/sendAudioHandle.py @@ -2,6 +2,7 @@ import json import time import asyncio from core.utils import textUtils +from core.utils.util import audio_to_data_stream from core.providers.tts.dto.dto import SentenceType TAG = __name__ @@ -30,7 +31,7 @@ async def sendAudioMessage(conn, sentenceType, audios, text): # 播放音频 -async def sendAudio(conn, audios, pre_buffer=False, frame_duration=60): +async def sendAudio(conn, audios, frame_duration=60): """ 发送单个opus包,支持流控 Args: @@ -46,11 +47,6 @@ async def sendAudio(conn, audios, pre_buffer=False, frame_duration=60): if conn.client_abort: return - # 短音频直接发送(例如:提示音) - if pre_buffer: - await conn.websocket.send(audios) - return - conn.last_activity_time = time.time() * 1000 # 获取或初始化流控状态 @@ -78,7 +74,7 @@ async def sendAudio(conn, audios, pre_buffer=False, frame_duration=60): flow_control["packet_count"] += 1 flow_control["last_send_time"] = time.perf_counter() else: - # 流控参数优化 + # 提示音和唤醒音频走普通播放 start_time = time.perf_counter() play_position = 0 @@ -124,12 +120,13 @@ async def send_tts_message(conn, state, text=None): stop_tts_notify_voice = conn.config.get( "stop_tts_notify_voice", "config/assets/tts_notify.mp3" ) - conn.tts.audio_to_opus_data_stream( - stop_tts_notify_voice, - callback=lambda audio_data: asyncio.run_coroutine_threadsafe( - sendAudio(conn, audio_data, True), conn.loop - ), - ) + # 获取音频数据 + audios = [] + def handle_audio_frame(frame_data): + audios.append(frame_data) + + audio_to_data_stream(stop_tts_notify_voice, is_opus=True, callback=handle_audio_frame) + await sendAudio(conn, audios) # 清除服务端讲话状态 conn.clearSpeakStatus()