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()