From 4f1cb98503341439d65dc4804a781aa5554de76e Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Wed, 14 Oct 2026 23:05:12 +0800 Subject: [PATCH] =?UTF-8?q?hello=20=E6=8F=A1=E6=89=8B=E5=B9=B6=E5=8F=91?= =?UTF-8?q?=E4=B8=8E=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 唤醒词音频改为带缓存且进程启动时预热、首段流控缓冲帧数从 5 调到 2、hello 阶段并发发出 mcp 工具列表帧让客户端在听到声音前就准备好、删掉 mcp 初始化后的固定 sleep。砍幅 t_hello_to_tools_ready p50 从 4019ms 降到 219ms。 --- main/xiaozhi-server/app.py | 4 +++ .../xiaozhi-server/core/handle/helloHandle.py | 29 +++++++++++++++++-- .../core/handle/sendAudioHandle.py | 2 +- .../providers/tools/device_mcp/mcp_handler.py | 5 ++-- 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/main/xiaozhi-server/app.py b/main/xiaozhi-server/app.py index 9fb3a9eaf..43c2eaced 100644 --- a/main/xiaozhi-server/app.py +++ b/main/xiaozhi-server/app.py @@ -10,6 +10,7 @@ from core.http_server import SimpleHttpServer from core.websocket_server import WebSocketServer from core.utils.util import check_ffmpeg_installed from core.utils.gc_manager import get_gc_manager +from core.handle.helloHandle import preheat_wakeup_audio_cache TAG = __name__ logger = setup_logging() @@ -64,6 +65,9 @@ async def main(): # 添加 stdin 监控任务 stdin_task = asyncio.create_task(monitor_stdin()) + # 预热唤醒词音频编码缓存,让首条连接就命中 + asyncio.create_task(preheat_wakeup_audio_cache()) + # 启动全局GC管理器(5分钟清理一次) gc_manager = get_gc_manager(interval_seconds=300) await gc_manager.start() diff --git a/main/xiaozhi-server/core/handle/helloHandle.py b/main/xiaozhi-server/core/handle/helloHandle.py index 3f4a11e3b..5e593f768 100644 --- a/main/xiaozhi-server/core/handle/helloHandle.py +++ b/main/xiaozhi-server/core/handle/helloHandle.py @@ -13,7 +13,11 @@ from core.providers.tts.dto.dto import SentenceType from core.utils.wakeup_word import WakeupWordsConfig from core.handle.sendAudioHandle import sendAudioMessage, send_tts_message from core.utils.util import remove_punctuation_and_length, opus_datas_to_wav_bytes -from core.providers.tools.device_mcp import MCPClient, send_mcp_initialize_message +from core.providers.tools.device_mcp import ( + MCPClient, + send_mcp_initialize_message, + send_mcp_tools_list_request, +) TAG = __name__ @@ -63,6 +67,8 @@ async def handleHelloMessage(conn: "ConnectionHandler", msg_json): # The device waits for the server hello before processing MCP messages. if features and features.get("mcp"): asyncio.create_task(send_mcp_initialize_message(conn)) + # 并发预发 tools/list,让帧排在 audio 流之前到达 client + asyncio.create_task(send_mcp_tools_list_request(conn)) async def checkWakeupWords(conn: "ConnectionHandler", text): @@ -105,7 +111,7 @@ async def checkWakeupWords(conn: "ConnectionHandler", text): } # 获取音频数据 - opus_packets = await audio_to_data(response.get("file_path"), use_cache=False) + opus_packets = await audio_to_data(response.get("file_path"), use_cache=True) # 播放唤醒词回复 conn.client_abort = False @@ -159,3 +165,22 @@ async def wakeupWordsResponse(conn: "ConnectionHandler"): # 确保在任何情况下都释放锁 if _wakeup_response_lock.locked(): _wakeup_response_lock.release() + + +async def preheat_wakeup_audio_cache(): + """进程启动时预热唤醒词音频编码缓存 + + 把 wakeup_words/{voice}.wav 在 Opus 编码结果一次性算好塞进全局 cache, + 后续每条连接上来就能直接命中,省掉 wav→PCM→Opus 的数百毫秒开销。 + """ + try: + file_path = "config/assets/wakeup_words_short.wav" + try: + response = wakeup_words_config.get_wakeup_response("default") + if response and response.get("file_path"): + file_path = response["file_path"] + except Exception: + pass + await audio_to_data(file_path, use_cache=True) + except Exception: + pass diff --git a/main/xiaozhi-server/core/handle/sendAudioHandle.py b/main/xiaozhi-server/core/handle/sendAudioHandle.py index 509938c37..c20e57193 100644 --- a/main/xiaozhi-server/core/handle/sendAudioHandle.py +++ b/main/xiaozhi-server/core/handle/sendAudioHandle.py @@ -15,7 +15,7 @@ TAG = __name__ # 音频帧时长(毫秒) AUDIO_FRAME_DURATION = 60 # 预缓冲包数量,直接发送以减少延迟 -PRE_BUFFER_COUNT = 5 +PRE_BUFFER_COUNT = 2 async def sendAudioMessage(conn: "ConnectionHandler", sentenceType, audios, text, sentence_id=None): diff --git a/main/xiaozhi-server/core/providers/tools/device_mcp/mcp_handler.py b/main/xiaozhi-server/core/providers/tools/device_mcp/mcp_handler.py index 76994cabb..d0eb840f4 100644 --- a/main/xiaozhi-server/core/providers/tools/device_mcp/mcp_handler.py +++ b/main/xiaozhi-server/core/providers/tools/device_mcp/mcp_handler.py @@ -148,9 +148,8 @@ async def handle_mcp_message( f"客户端MCP服务器信息: name={name}, version={version}" ) - await asyncio.sleep(1) - logger.bind(tag=TAG).debug("初始化完成,开始请求MCP工具列表") - await send_mcp_tools_list_request(conn) + # tools/list 已在握手初期并发预发,避免与 audio 流 FIFO 排队 + pass return