diff --git a/benchmark/handshake_bench.py b/benchmark/handshake_bench.py index 2476f2ce8..7f1a3ba0b 100644 --- a/benchmark/handshake_bench.py +++ b/benchmark/handshake_bench.py @@ -245,18 +245,29 @@ def run_one(url, run_idx, args, headers_extra=None): } +def _percentile(vals, q): + """线性插值百分位(与 numpy 默认 method 一致)。""" + s = sorted(vals) + n = len(s) + if n == 0: + return None + if n == 1: + return s[0] + k = (n - 1) * q + f = int(k) + c = min(f + 1, n - 1) + if f == c: + return s[f] + return s[f] + (k - f) * (s[c] - s[f]) + + def summarize(name, vals): vals = [v for v in vals if v is not None] if not vals: print(f" {name}: no samples") return - vals_sorted = sorted(vals) - p50 = statistics.median(vals) - p95 = ( - vals_sorted[max(0, int(0.95 * (len(vals_sorted) - 1)))] - if len(vals_sorted) >= 2 - else vals_sorted[0] - ) + p50 = _percentile(vals, 0.5) + p95 = _percentile(vals, 0.95) print( f" {name}: n={len(vals)} min={min(vals):.0f} " f"p50={p50:.0f} p95={p95:.0f} max={max(vals):.0f} ms" diff --git a/main/xiaozhi-server/core/handle/helloHandle.py b/main/xiaozhi-server/core/handle/helloHandle.py index 5e593f768..d9068dcd3 100644 --- a/main/xiaozhi-server/core/handle/helloHandle.py +++ b/main/xiaozhi-server/core/handle/helloHandle.py @@ -4,6 +4,7 @@ import uuid import random import asyncio from typing import TYPE_CHECKING +from config.logger import setup_logging if TYPE_CHECKING: from core.connection import ConnectionHandler @@ -20,6 +21,7 @@ from core.providers.tools.device_mcp import ( ) TAG = __name__ +_wakeup_logger = setup_logging() WAKEUP_CONFIG = { "refresh_time": 10, @@ -173,14 +175,17 @@ async def preheat_wakeup_audio_cache(): 把 wakeup_words/{voice}.wav 在 Opus 编码结果一次性算好塞进全局 cache, 后续每条连接上来就能直接命中,省掉 wav→PCM→Opus 的数百毫秒开销。 """ - try: - file_path = "config/assets/wakeup_words_short.wav" + # 进程启动时 tts 还未初始化,无法拿到 conn.tts.voice,按常见 fallback 列表预热 + preheat_voices = ["default", "female-shaonv", "male-qn-jingying"] + fallback_path = "config/assets/wakeup_words_short.wav" + for voice in preheat_voices: + file_path = fallback_path try: - response = wakeup_words_config.get_wakeup_response("default") + response = wakeup_words_config.get_wakeup_response(voice) 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 + await audio_to_data(file_path, use_cache=True) + except Exception as e: + _wakeup_logger.bind(tag=TAG).warning( + f"预热 voice={voice} 失败: {e}" + ) diff --git a/main/xiaozhi-server/core/handle/sendAudioHandle.py b/main/xiaozhi-server/core/handle/sendAudioHandle.py index c20e57193..cdd944742 100644 --- a/main/xiaozhi-server/core/handle/sendAudioHandle.py +++ b/main/xiaozhi-server/core/handle/sendAudioHandle.py @@ -12,10 +12,8 @@ from core.providers.tts.dto.dto import SentenceType from core.utils.audioRateController import AudioRateController TAG = __name__ -# 音频帧时长(毫秒) -AUDIO_FRAME_DURATION = 60 -# 预缓冲包数量,直接发送以减少延迟 -PRE_BUFFER_COUNT = 2 +AUDIO_FRAME_DURATION = 60 # 音频帧时长(毫秒),与 Opus 编码 60ms/帧对齐 +PRE_BUFFER_COUNT = 2 # 首段发送时不入队的帧数,避免流控延迟拉高首帧到达时间 async def sendAudioMessage(conn: "ConnectionHandler", sentenceType, audios, text, sentence_id=None): diff --git a/main/xiaozhi-server/requirements.txt b/main/xiaozhi-server/requirements.txt index 055bf30fc..ae4af6cc0 100644 --- a/main/xiaozhi-server/requirements.txt +++ b/main/xiaozhi-server/requirements.txt @@ -13,6 +13,8 @@ pydub==0.25.1 funasr==1.2.7 openai==2.8.1 google-generativeai==0.8.5 +#--------- 基准测试客户端依赖(benchmark/handshake_bench.py 使用) +websocket-client==1.8.0 edge_tts==7.2.8 httpx==0.28.1 aiohttp==3.13.2