mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-23 03:19:41 +00:00
feat(performance-tester): interactive device simulator TUI with local audio
- device_simulator.py: audio E2E engine (mic capture -> Opus 60ms frames, downlink decode/playback), half-duplex mutex, digital injection of corpus wavs, headless scenarios (baseline/swallowhunt/zombie/idle incl. half-open connection reproduction), JSONL event log and uplink/downlink PCM dumps - device_simulator_tui.py: Textual TUI front-end with full per-turn transcript, L1-L5 latency panel, WS ping probe and key bindings (wake/interrupt/mic/probe/corpus injection/volume) - phrases.txt + prewarm_corpus.py + speak.ps1: corpus and local synthesis (edge-tts with Windows SAPI fallback) - docs/device-simulator-tui.md (bilingual) + cross references in device-protocol-simulator.md and performance_tester.md
This commit is contained in:
parent
7f477e3b29
commit
1e675874d0
10 changed files with 1259 additions and 2 deletions
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
`performance_tester/performance_tester_device.py` 用真实设备协议(`hello` → `listen` → `opus` 音频帧 → `listen stop`)连接 xiaozhi-server,用于回归验证「设备说话后无反应」这类端到端问题:服务端到底有没有完成 ASR、有没有触发工具/大模型、有没有下发 TTS。
|
||||
|
||||
它不替代固件测试:不涉及麦克风/扬声器声学链路,也不验证固件看门狗与重连逻辑。
|
||||
它不替代固件测试:不涉及麦克风/扬声器声学链路,也不验证固件看门狗与重连逻辑。需要真实麦克风/扬声器交互式多轮自测时,用同族的交互式模拟器 `performance_tester/device_simulator_tui.py`(见 `docs/device-simulator-tui.md`)。
|
||||
|
||||
## 快速开始
|
||||
|
||||
|
|
@ -74,7 +74,7 @@ python performance_tester.py
|
|||
|
||||
`performance_tester/performance_tester_device.py` connects to xiaozhi-server using the real device protocol (`hello` → `listen` → Opus frames → `listen stop`) to reproduce and verify end-to-end "device says something, server never responds" issues: did ASR produce text, did the tool/LLM run, did TTS audio come back?
|
||||
|
||||
It is **not** a firmware or acoustic test: microphone/speaker paths, watchdog and reconnect behavior are out of scope.
|
||||
It is **not** a firmware or acoustic test: microphone/speaker paths, watchdog and reconnect behavior are out of scope. For interactive multi-turn self-testing with a real microphone/speaker, use the TUI sibling `performance_tester/device_simulator_tui.py` (see `docs/device-simulator-tui.md`).
|
||||
|
||||
## Quick start
|
||||
|
||||
|
|
|
|||
135
docs/device-simulator-tui.md
Normal file
135
docs/device-simulator-tui.md
Normal file
|
|
@ -0,0 +1,135 @@
|
|||
# 设备模拟器 TUI(交互式多轮自测)
|
||||
|
||||
`performance_tester/device_simulator_tui.py` 是 `performance_tester_device.py`(无头单轮)之外的**交互式**设备模拟器:Textual TUI 界面,走同一套协议 v1(`hello` → `listen` → Opus 60ms 帧),但带**本机真实音频**——麦克风 16k 采集、扬声器 48k 播放,支持多轮对话、打断、语料注入与逐轮时延面板。
|
||||
|
||||
引擎 `performance_tester/device_simulator.py` 与 TUI 共用,也可无头运行(见下)。
|
||||
|
||||
| 文件 | 作用 |
|
||||
|---|---|
|
||||
| `device_simulator.py` | 引擎:协议会话、Opus 编解码(PyAV libopus)、半双工互斥、数字注入、无头场景(baseline/swallowhunt/zombie/idle) |
|
||||
| `device_simulator_tui.py` | Textual TUI 前端:逐轮回显 + L1-L5 时延面板 + 键位交互 |
|
||||
| `phrases.txt` | 注入语料(每行一条,`#` 注释,`1-9` 映射前 9 条,`e` 键重载) |
|
||||
| `prewarm_corpus.py` + `speak.ps1` | 语料预合成缓存(Windows SAPI 16k wav;装有 `edge-tts` 时优先用之) |
|
||||
| `start_tui.cmd` / `start_tui.sh` | 双击/脚本启动器 |
|
||||
|
||||
## 快速开始
|
||||
|
||||
```bash
|
||||
cd main/xiaozhi-server/performance_tester
|
||||
pip install numpy sounddevice websockets av textual # edge-tts 可选
|
||||
|
||||
# 预合成语料缓存(可选,仅 Windows;TUI 中按 1-9 也可实时合成)
|
||||
python prewarm_corpus.py
|
||||
|
||||
# 启动 TUI(在支持 ANSI 的控制台运行)
|
||||
python device_simulator_tui.py --device-id AA:BB:CC:DD:EE:FF
|
||||
# Windows 双击 start_tui.cmd;Linux/macOS ./start_tui.sh
|
||||
```
|
||||
|
||||
`--device-id` 须为服务端已登记设备;认证参数与 `performance_tester_device.py` 相同(`--token` / `--secret` / 环境变量 `DEVICE_SIM_TOKEN`)。
|
||||
|
||||
## 键位
|
||||
|
||||
| 键 | 动作 |
|
||||
|---|---|
|
||||
| `w` | 唤醒(建连 + `listen start`),之后直接对麦克风说话(auto 模式) |
|
||||
| `space` | 打断:发 `{"type":"abort"}` + 本地立即停止播放/清队列(实证:`listen/state=abort` 服务端静默忽略,必须用 `type=abort`) |
|
||||
| `m` | 麦克风开关 |
|
||||
| `p` | 网络探针(WS ping RTT,10s 周期) |
|
||||
| `e` | 重载 phrases.txt |
|
||||
| `1-9` | 数字注入第 N 条语料(本地合成缓存 wav 直灌识别通道) |
|
||||
| `+/-` | 播放音量 |
|
||||
| `q` | 退出 |
|
||||
|
||||
## 内建行为
|
||||
|
||||
- **半双工互斥**:TTS 播放期间上行自动静默、播完清积压再放开——与真机半双工一致,从源头杜绝"扬声器回声被识别"(`--duplex` 可关闭,回声实验用)。
|
||||
- **数字注入**:wav PCM 直灌麦克风队列(60ms 节拍、丢帧计数),绕过扬声器↔麦克风声学耦合差异,同一文件可反复注入得到确定性回归。
|
||||
- **取证落盘**:`tui_*.jsonl` 全事件(含 ping RTT、1Hz RMS);`rec/mic_*.pcm`(16k,实际发往服务端的内容)与 `rec/spk_*.pcm`(48k,扬声器实际播放内容)。
|
||||
- **时延面板**:L1 唤醒→建连 / L2 说完→STT / L3 STT→首包 / L5 端到端(近 5 轮均值)。
|
||||
|
||||
## 引擎无头用法(回归脚本友好)
|
||||
|
||||
```bash
|
||||
cd main/xiaozhi-server/performance_tester
|
||||
# 多轮注入对话(--plays 不给时自动使用 phrases.txt 对应的预合成缓存)
|
||||
python device_simulator.py --scenario baseline --device-id AA:BB:CC:DD:EE:FF \
|
||||
--plays "cache_xxxxxxxxxx.wav@1.0" --out log_baseline.jsonl
|
||||
# 半开连接复现(需管理员权限,仅 Windows;从 --url 自动解析入口 IP)
|
||||
python device_simulator.py --scenario zombie --device-id ... --out log_zombie.jsonl
|
||||
```
|
||||
|
||||
场景:`baseline` 多轮注入 / `swallowhunt` 短句密集轮次 / `zombie` 半开连接与会话不收敛复现 / `idle` 静音保持观察。
|
||||
|
||||
## 限制
|
||||
|
||||
- 依赖本机音频设备与显示终端(TUI 需支持 ANSI);服务器/容器内请用无头的 `performance_tester_device.py`。
|
||||
- `zombie` 场景需要 Windows 管理员权限(netsh 防火墙)。
|
||||
- 语料合成的 SAPI 兜底仅 Windows;`edge-tts` 受网络影响时用 `prewarm_corpus.py` 预合成。
|
||||
|
||||
---
|
||||
|
||||
# Device Simulator TUI (English)
|
||||
|
||||
`performance_tester/device_simulator_tui.py` is the **interactive** counterpart of `performance_tester_device.py` (headless, single turn): a Textual TUI speaking the same protocol v1 (`hello` → `listen` → Opus 60 ms frames) with **real local audio** — 16 k microphone capture, 48 k speaker playback, multi-turn conversation, interrupt, corpus injection and a per-turn latency panel.
|
||||
|
||||
The engine `performance_tester/device_simulator.py` is shared with the TUI and can also run headless (see below).
|
||||
|
||||
| File | Purpose |
|
||||
|---|---|
|
||||
| `device_simulator.py` | Engine: protocol session, Opus codec (PyAV libopus), half-duplex mutex, digital injection, headless scenarios (baseline/swallowhunt/zombie/idle) |
|
||||
| `device_simulator_tui.py` | Textual TUI front-end: full per-turn transcript + L1-L5 latency panel + key bindings |
|
||||
| `phrases.txt` | Injection corpus (one phrase per line, `#` comments; keys `1-9` map to the first 9, `e` reloads) |
|
||||
| `prewarm_corpus.py` + `speak.ps1` | Pre-synthesize corpus cache (Windows SAPI 16 kHz wav; uses `edge-tts` when available) |
|
||||
| `start_tui.cmd` / `start_tui.sh` | Launchers (double-click / shell script) |
|
||||
|
||||
## Quick start
|
||||
|
||||
```bash
|
||||
cd main/xiaozhi-server/performance_tester
|
||||
pip install numpy sounddevice websockets av textual # edge-tts optional
|
||||
|
||||
python prewarm_corpus.py # optional, Windows only
|
||||
|
||||
python device_simulator_tui.py --device-id AA:BB:CC:DD:EE:FF
|
||||
# Windows: double-click start_tui.cmd; Linux/macOS: ./start_tui.sh
|
||||
```
|
||||
|
||||
`--device-id` must be registered on the server; auth options are the same as `performance_tester_device.py` (`--token` / `--secret` / `DEVICE_SIM_TOKEN`).
|
||||
|
||||
## Key bindings
|
||||
|
||||
| Key | Action |
|
||||
|---|---|
|
||||
| `w` | Wake (connect + `listen start`); then just speak to the microphone (auto mode) |
|
||||
| `space` | Interrupt: sends `{"type":"abort"}` and stops local playback immediately (verified: `listen/state=abort` is silently ignored by the server — `type=abort` is required) |
|
||||
| `m` | Microphone on/off |
|
||||
| `p` | Network probe (WS ping RTT every 10 s) |
|
||||
| `e` | Reload phrases.txt |
|
||||
| `1-9` | Digitally inject corpus phrase N (locally synthesized cache wav fed straight into the recognition channel) |
|
||||
| `+/-` | Playback volume |
|
||||
| `q` | Quit |
|
||||
|
||||
## Built-in behavior
|
||||
|
||||
- **Half-duplex mutex**: uplink is muted automatically while TTS plays and un-muted with a backlog flush after it ends — mirrors real-device half duplex and prevents "speaker echo being recognized" (`--duplex` disables it for echo experiments).
|
||||
- **Digital injection**: wav PCM is fed straight into the microphone queue (60 ms pacing, audited drop counter), bypassing speaker↔microphone acoustic coupling differences; the same file can be re-injected for deterministic regression.
|
||||
- **Forensics**: `tui_*.jsonl` full event log (ping RTT, 1 Hz RMS); `rec/mic_*.pcm` (16 k, what was actually sent) and `rec/spk_*.pcm` (48 k, what was actually played).
|
||||
- **Latency panel**: L1 wake→connect / L2 speech-end→STT / L3 STT→first packet / L5 end-to-end (mean of last 5 turns).
|
||||
|
||||
## Headless engine usage (regression-friendly)
|
||||
|
||||
```bash
|
||||
cd main/xiaozhi-server/performance_tester
|
||||
python device_simulator.py --scenario baseline --device-id AA:BB:CC:DD:EE:FF \
|
||||
--plays "cache_xxxxxxxxxx.wav@1.0" --out log_baseline.jsonl
|
||||
python device_simulator.py --scenario zombie --device-id ... --out log_zombie.jsonl # admin required
|
||||
```
|
||||
|
||||
Scenarios: `baseline` multi-turn injection / `swallowhunt` dense short utterances / `zombie` half-open connection & session non-convergence / `idle` silence holding.
|
||||
|
||||
## Limitations
|
||||
|
||||
- Needs a local audio device and an ANSI-capable terminal; inside servers/containers use the headless `performance_tester_device.py` instead.
|
||||
- The `zombie` scenario requires Windows admin rights (netsh firewall).
|
||||
- The SAPI corpus-synthesis fallback is Windows-only; pre-synthesize with `prewarm_corpus.py` when `edge-tts` is unreachable.
|
||||
|
|
@ -74,3 +74,5 @@ python performance_tester.py
|
|||
## 设备协议 E2E 模拟器
|
||||
|
||||
`performance_tester_device.py` 不测供应器延迟,而是按真实设备协议(`hello` → `listen` → opus 帧 → `listen stop`)连接服务端,验证 ASR → 工具/大模型 → TTS 全链路是否响应,适合复现「设备说话后无反应」类问题。用法、认证与限制见 [device-protocol-simulator.md](device-protocol-simulator.md)。
|
||||
|
||||
需要真实麦克风/扬声器的多轮交互自测(打断、语料注入、时延面板),用交互式 TUI `device_simulator_tui.py`(引擎 `device_simulator.py`,也可无头跑回归场景),见 [device-simulator-tui.md](device-simulator-tui.md)。
|
||||
|
|
|
|||
708
main/xiaozhi-server/performance_tester/device_simulator.py
Normal file
708
main/xiaozhi-server/performance_tester/device_simulator.py
Normal file
|
|
@ -0,0 +1,708 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""device_simulator.py — xiaozhi 设备协议 v1 音频 E2E 模拟器引擎(无头场景;device_simulator_tui.py 复用本引擎)
|
||||
|
||||
本机采集/回放真实音频:麦克风 16k 采集 → Opus 60ms 帧实时上行;下行解码 48k 播放。
|
||||
支持「数字注入」(wav PCM 直灌识别通道,绕过扬声器↔麦克风声学耦合差异,确定性回归)
|
||||
与「半双工互斥」(TTS 播放期自动静默上行,防回声,与真机行为一致)。
|
||||
逐事件 JSONL 日志;上行(实发 16k)/ 下行(实播 48k)PCM 落盘 rec/ 供取证。
|
||||
|
||||
用法(在 performance_tester 目录下):
|
||||
python device_simulator.py --scenario baseline --device-id AA:BB:CC:DD:EE:FF \
|
||||
--plays "cache_xxx.wav@1.0,cache_yyy.wav@1.5" --out log_baseline.jsonl
|
||||
python device_simulator.py --scenario idle --device-id ... --out log_idle.jsonl # 静音观察
|
||||
认证: --token <设备token>(或环境变量 DEVICE_SIM_TOKEN);或 --secret <auth_key> 现签
|
||||
场景: baseline=多轮注入对话; swallowhunt=短句密集轮次; zombie=半开连接(需管理员netsh); idle=静音保持
|
||||
"""
|
||||
import os
|
||||
for _k in ("HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "WSS_PROXY", "WS_PROXY", "http_proxy", "https_proxy", "all_proxy"):
|
||||
os.environ.pop(_k, None) # 强制直连(与设备一致),禁止 env 代理劫持
|
||||
|
||||
import argparse
|
||||
import asyncio
|
||||
import json
|
||||
import queue
|
||||
import socket
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
||||
import urllib.parse
|
||||
import wave as wavemod
|
||||
import numpy as np
|
||||
import sounddevice as sd
|
||||
import websockets
|
||||
|
||||
SAMPLE_RATE = 16000
|
||||
FRAME_SAMPLES = 960 # 60ms @16k
|
||||
FW_IS_TIMEOUT_S = 120 # 固件 protocol.cc IsTimeout 看门狗
|
||||
|
||||
LOG = None
|
||||
T0 = time.monotonic()
|
||||
|
||||
|
||||
def ev(kind, **kw):
|
||||
rec = {"t_wall": time.strftime("%H:%M:%S") + f".{int(time.time()*1000)%1000:03d}",
|
||||
"t_mono": round(time.monotonic() - T0, 3), "ev": kind}
|
||||
rec.update(kw)
|
||||
if LOG:
|
||||
LOG.write(json.dumps(rec, ensure_ascii=False) + "\n")
|
||||
LOG.flush()
|
||||
print(json.dumps(rec, ensure_ascii=False), flush=True)
|
||||
|
||||
|
||||
def resolve_token(args) -> str:
|
||||
"""认证三选一: --token / 环境变量 DEVICE_SIM_TOKEN / --secret 现签(需能导入服务端 core.auth)"""
|
||||
if getattr(args, "token", None):
|
||||
return args.token
|
||||
if os.environ.get("DEVICE_SIM_TOKEN"):
|
||||
return os.environ["DEVICE_SIM_TOKEN"]
|
||||
secret = getattr(args, "secret", None)
|
||||
if secret:
|
||||
_SERVER_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
if _SERVER_ROOT not in sys.path:
|
||||
sys.path.insert(0, _SERVER_ROOT)
|
||||
from core.auth import AuthManager
|
||||
return AuthManager(secret).generate_token(args.client_id, args.device_id)
|
||||
return ""
|
||||
|
||||
|
||||
class OpusEncoder:
|
||||
def __init__(self):
|
||||
import av
|
||||
# 关键: PyAV libopus 实际运行于 48k(声明 16k 被忽略, 实测音频 3× 变调)。
|
||||
# 显式按 48k 编码: 麦克风 16k 先 ×3 重采样再喂包, 服务端解码即得正确音高/时长。
|
||||
self.c = av.CodecContext.create("libopus", "w")
|
||||
self.c.sample_rate = 48000
|
||||
self.c.layout = "mono"
|
||||
self.c.format = av.AudioFormat("s16")
|
||||
self.c.options = {"application": "voip"}
|
||||
self.c.open()
|
||||
ev("enc_open", frame_size=self.c.frame_size, note="48k native")
|
||||
|
||||
def encode(self, pcm_int16_16k: np.ndarray):
|
||||
"""输入 16k int16(60ms=960 样本) → ×3 重采样 → 48k 编码"""
|
||||
import av
|
||||
n = len(pcm_int16_16k)
|
||||
up = np.interp(
|
||||
np.linspace(0, n - 1, n * 3), np.arange(n), pcm_int16_16k.astype(np.float32)
|
||||
).astype(np.int16)
|
||||
frame = av.AudioFrame.from_ndarray(up.reshape(1, -1), format="s16", layout="mono")
|
||||
frame.sample_rate = 48000
|
||||
frame.pts = None
|
||||
return [bytes(p) for p in self.c.encode(frame)]
|
||||
|
||||
|
||||
class OpusDecoder:
|
||||
def __init__(self, sr: int):
|
||||
import av
|
||||
self.c = av.CodecContext.create("libopus", "r")
|
||||
self.c.sample_rate = 48000 # libopus 解码输出固定 48k(仅本地播放用;服务端自行解码不受影响)
|
||||
self.c.layout = "mono"
|
||||
self.c.open()
|
||||
self.out_sr = 48000
|
||||
|
||||
def decode(self, packet: bytes) -> np.ndarray:
|
||||
import av
|
||||
pkt = av.Packet(packet)
|
||||
frames = self.c.decode(pkt)
|
||||
out = []
|
||||
for f in frames:
|
||||
arr = f.to_ndarray()
|
||||
if arr.dtype != np.int16:
|
||||
arr = (np.clip(arr, -1, 1) * 32767).astype(np.int16)
|
||||
out.append(arr.reshape(-1))
|
||||
return np.concatenate(out) if out else np.zeros(0, dtype=np.int16)
|
||||
|
||||
|
||||
def rms(i16: np.ndarray) -> int:
|
||||
if len(i16) == 0:
|
||||
return 0
|
||||
return int(np.sqrt(np.mean(i16.astype(np.int64) ** 2)))
|
||||
|
||||
|
||||
class Sim:
|
||||
def __init__(self, scenario: str, plays, out_path: str, dur: float,
|
||||
url: str, device_id: str, client_id: str, token: str = ""):
|
||||
self.scenario = scenario
|
||||
self.plays = plays
|
||||
self.dur = dur
|
||||
self.url = url
|
||||
self.device_id = device_id
|
||||
self.client_id = client_id
|
||||
self.token = token
|
||||
self.session_id = None
|
||||
self.downlink_sr = 16000
|
||||
self.seq = 0
|
||||
self.mic_on = True
|
||||
self.mic_q = asyncio.Queue(maxsize=200)
|
||||
self.loop = None
|
||||
self.enc = None
|
||||
self.dec = None
|
||||
self.playback_active = False
|
||||
self.inject = True # 数字注入(默认开; TUI 人工模式可关, 走真实麦克风)
|
||||
self.last_incoming = time.monotonic()
|
||||
self.turn_marker = {"speak_end": None}
|
||||
self.stt_waiters = []
|
||||
self.first_audio_ts = None
|
||||
self.tts_done = asyncio.Event()
|
||||
self.stt_event = asyncio.Event()
|
||||
# TUI/复用安全: 通道属性先置空(open_channel 时创建)
|
||||
self.asr_ws = None
|
||||
self.ws = None
|
||||
self.mic_stream = None
|
||||
self.spk_stream = None
|
||||
self.pump_task = None
|
||||
self.probe_task = None
|
||||
# 播放与半双工(F批): 接收/播放解耦 + 播放期上行静默(互斥防回声, 与真机半双工一致)
|
||||
self.play_q = queue.Queue(maxsize=2000)
|
||||
self._player_thread = None
|
||||
self._spk_gen = 0 # 播放代次: abort 后旧队列项作废
|
||||
self.tts_playing = False
|
||||
self.half_duplex = True # --duplex 可关(回声实验用); 默认互斥
|
||||
self.last_voice_mono = None # 真麦克风时延锚: 最近一块实音(rms>=600)时刻
|
||||
self.last_stt_mono = None
|
||||
self.rec_mic = None # 上行 PCM 落盘(发往服务端的帧, 16k s16le)
|
||||
self.rec_spk = None # 下行 PCM 落盘(解码后播放帧, 48k s16le)
|
||||
|
||||
def _set_mic_state(self, on: bool, reason: str):
|
||||
"""上行静默开关; 放开时清空积压帧, 防播放期回声/静音块灌入下一轮识别"""
|
||||
if self.mic_on == on:
|
||||
return
|
||||
self.mic_on = on
|
||||
if on:
|
||||
dropped = 0
|
||||
while True:
|
||||
try:
|
||||
self.mic_q.get_nowait()
|
||||
dropped += 1
|
||||
except Exception:
|
||||
break
|
||||
ev("mic_unmute", reason=reason, dropped_backlog=dropped)
|
||||
else:
|
||||
ev("mic_mute", reason=reason)
|
||||
|
||||
# ---------- 音频 ----------
|
||||
def mic_callback(self, indata, frames, t, status):
|
||||
if status:
|
||||
ev("mic_status", status=str(status))
|
||||
if self.mic_on:
|
||||
if int(np.sqrt(np.mean(indata.astype(np.int64) ** 2))) >= 600:
|
||||
self.last_voice_mono = time.monotonic() # 采集侧锚(免受 pump 滞后污染)
|
||||
try:
|
||||
self.mic_q.put_nowait(indata.copy())
|
||||
except asyncio.QueueFull:
|
||||
self._drops = getattr(self, "_drops", 0) + 1
|
||||
if self._drops == 1 or self._drops % 100 == 0:
|
||||
ev("mic_drop", total=self._drops)
|
||||
|
||||
def play_wav(self, path):
|
||||
with wavemod.open(path, "rb") as w:
|
||||
sr, ch, sw, n = w.getframerate(), w.getnchannels(), w.getsampwidth(), w.getnframes()
|
||||
raw = w.readframes(n)
|
||||
data = np.frombuffer(raw, dtype=np.int16)
|
||||
if ch > 1:
|
||||
data = data.reshape(-1, ch).mean(axis=1).astype(np.int16)
|
||||
ev("speak_start", file=os.path.basename(path), sr=sr, dur_ms=int(n / sr * 1000), rms=rms(data))
|
||||
# 数字注入: wav PCM 直灌麦克风队列(绕过扬声器↔麦克风声学耦合差异, 确定性回归)
|
||||
if self.inject:
|
||||
import threading
|
||||
def _feed():
|
||||
self.mic_on = False # 注入期间静默真实麦克风(单源, 防交叠稀释)
|
||||
while not self.mic_q.empty(): # 清空积压的真实麦克风静音块, 防语音块排队迟到
|
||||
try:
|
||||
self.mic_q.get_nowait()
|
||||
except Exception:
|
||||
break
|
||||
step = 960
|
||||
drops = 0
|
||||
for i in range(0, len(data), step):
|
||||
try:
|
||||
self.mic_q.put_nowait(data[i:i + step].reshape(-1, 1).copy())
|
||||
except Exception:
|
||||
drops += 1 # 丢帧可审计(不再静默)
|
||||
if drops % 100 == 1:
|
||||
ev("inject_drop", total=drops)
|
||||
time.sleep(0.06)
|
||||
time.sleep(0.3)
|
||||
if not self.tts_playing: # 回复已在播则保持静默(半双工), 由 tts stop 统一放开
|
||||
self.mic_on = True
|
||||
threading.Thread(target=_feed, daemon=True).start()
|
||||
else:
|
||||
sd.play(data, samplerate=sr)
|
||||
sd.wait()
|
||||
self.turn_marker["speak_end"] = time.monotonic()
|
||||
ev("speak_end", file=os.path.basename(path))
|
||||
|
||||
async def mic_pump(self):
|
||||
# 节拍在消费侧 —— 第 k 块应发时刻对齐单调钟(60ms/块), 落后>1s 追平
|
||||
ev("pump_start")
|
||||
try:
|
||||
await self._mic_pump_inner()
|
||||
except asyncio.CancelledError:
|
||||
ev("pump_cancelled")
|
||||
raise
|
||||
except Exception as e:
|
||||
import traceback as _tb
|
||||
ev("pump_fatal", err=repr(e), tb=_tb.format_exc()[-500:])
|
||||
|
||||
async def _mic_pump_inner(self):
|
||||
t_origin = time.monotonic()
|
||||
block_idx = 0
|
||||
last_consume = time.monotonic()
|
||||
while True:
|
||||
block = await self.mic_q.get()
|
||||
now = time.monotonic()
|
||||
if now - last_consume > 0.5:
|
||||
# 产量断档(半双工静音/注入间隙)后重锚节拍, 防把静音时长误判为滞后而突发连发
|
||||
t_origin = now - block_idx * 0.06
|
||||
ev("pump_reanchor", gap_s=round(now - last_consume, 2), qsize=self.mic_q.qsize())
|
||||
last_consume = now
|
||||
n = len(block)
|
||||
for i in range(0, n, FRAME_SAMPLES):
|
||||
chunk = block[i:i + FRAME_SAMPLES].reshape(-1)
|
||||
if len(chunk) < FRAME_SAMPLES:
|
||||
continue
|
||||
block_idx += 1
|
||||
due = t_origin + block_idx * 0.06
|
||||
lag = time.monotonic() - due
|
||||
if lag > 1.0:
|
||||
ev("pump_catchup", lag_s=round(lag, 2), qsize=self.mic_q.qsize())
|
||||
t_origin = time.monotonic() - block_idx * 0.06 # 重锚, 放弃历史滞后
|
||||
elif lag < -0.005:
|
||||
await asyncio.sleep(-lag) # 超前则睡到点
|
||||
r = rms(chunk)
|
||||
if self.rec_mic:
|
||||
self.rec_mic.write(chunk.astype(np.int16).tobytes())
|
||||
try:
|
||||
pkts = self.enc.encode(chunk)
|
||||
for pkt in pkts:
|
||||
self.seq += 1
|
||||
await self.ws.send(pkt)
|
||||
if block_idx % 17 == 1:
|
||||
ev("frame_tx", seq=block_idx, rms=r, qsize=self.mic_q.qsize())
|
||||
except websockets.ConnectionClosed:
|
||||
ev("pump_exit_closed", seq=block_idx)
|
||||
return
|
||||
|
||||
async def audio_rx(self, data: bytes):
|
||||
if not self.tts_playing:
|
||||
self.tts_playing = True
|
||||
if self.half_duplex:
|
||||
self._set_mic_state(False, reason="tts_playback") # 半双工: 播放即静默上行
|
||||
if self.first_audio_ts is None:
|
||||
self.first_audio_ts = time.monotonic()
|
||||
lat_stt = (round(self.first_audio_ts - self.last_stt_mono, 3)
|
||||
if self.last_stt_mono else None)
|
||||
ev("tts_first_audio", latency_from_speak_end=(
|
||||
round(self.first_audio_ts - self.turn_marker["speak_end"], 3)
|
||||
if self.turn_marker["speak_end"] else None),
|
||||
latency_from_stt=lat_stt)
|
||||
pcm = self.dec.decode(data)
|
||||
if self.rec_spk:
|
||||
self.rec_spk.write(pcm.tobytes())
|
||||
try:
|
||||
self.play_q.put_nowait((self._spk_gen, pcm))
|
||||
except queue.Full:
|
||||
ev("play_q_full", dropped=True)
|
||||
|
||||
def _player_loop(self):
|
||||
"""单线程顺序写扬声器: 接收循环不再被阻塞写背压(曾实测文本消息延迟 8s)"""
|
||||
while True:
|
||||
item = self.play_q.get()
|
||||
if item is None:
|
||||
return
|
||||
gen, pcm = item
|
||||
if gen != self._spk_gen or len(pcm) == 0:
|
||||
continue
|
||||
if getattr(self, "_spk_started", False) is False:
|
||||
self._spk_started = True
|
||||
ev("tts_play_start", latency_from_speak_end=(
|
||||
round(time.monotonic() - self.turn_marker["speak_end"], 3)
|
||||
if self.turn_marker["speak_end"] else None))
|
||||
g = getattr(self, "playback_gain", 1.0)
|
||||
if g != 1.0:
|
||||
a = np.frombuffer(pcm, dtype=np.int16).astype(np.int32)
|
||||
pcm = np.clip(a * g, -32768, 32767).astype(np.int16).tobytes()
|
||||
try:
|
||||
self.spk_stream.write(pcm)
|
||||
except Exception as e:
|
||||
if getattr(self, "_spk_err_last", "") != str(e): # 连续同错去重(停流期间刷屏)
|
||||
ev("spk_err", err=str(e))
|
||||
self._spk_err_last = str(e)
|
||||
else:
|
||||
self._spk_err_last = ""
|
||||
|
||||
# ---------- 协议 ----------
|
||||
def hello_msg(self):
|
||||
return json.dumps({"type": "hello", "version": 1, "features": {"mcp": True},
|
||||
"transport": "websocket",
|
||||
"audio_params": {"format": "opus", "sample_rate": SAMPLE_RATE,
|
||||
"channels": 1, "frame_duration": 60}}, ensure_ascii=False)
|
||||
|
||||
async def handle_text(self, raw: str):
|
||||
try:
|
||||
msg = json.loads(raw)
|
||||
except Exception:
|
||||
ev("text_rx_unparsed", raw=raw[:200])
|
||||
return
|
||||
t = msg.get("type")
|
||||
if t == "hello":
|
||||
self.session_id = msg.get("session_id")
|
||||
ap = msg.get("audio_params") or {}
|
||||
self.downlink_sr = int(ap.get("sample_rate", 16000))
|
||||
self.dec = OpusDecoder(self.downlink_sr)
|
||||
ev("hello_rx", session_id=self.session_id, downlink_sr=self.downlink_sr)
|
||||
elif t == "mcp":
|
||||
await self.reply_mcp(msg)
|
||||
elif t == "tts":
|
||||
st = msg.get("state")
|
||||
if st == "stop":
|
||||
self.tts_playing = False
|
||||
if self.half_duplex:
|
||||
self._set_mic_state(True, reason="tts_stop")
|
||||
self.tts_done.set()
|
||||
ev("tts_state", state=st, **{k: v for k, v in msg.items() if k in ("session_id", "text")})
|
||||
elif t == "stt":
|
||||
lat = None
|
||||
now = time.monotonic()
|
||||
if self.last_voice_mono and 0 <= now - self.last_voice_mono < 60:
|
||||
lat = round(now - self.last_voice_mono, 3) # 真麦克风: 最近实音→识别到达
|
||||
elif self.turn_marker["speak_end"]:
|
||||
lat = round(now - self.turn_marker["speak_end"], 3)
|
||||
self.last_stt_mono = now
|
||||
ev("stt_rx", text=msg.get("text"), stt_latency_from_speak_end=lat)
|
||||
self.stt_event.set()
|
||||
else:
|
||||
ev("text_rx", msg=json.dumps(msg, ensure_ascii=False)[:300])
|
||||
|
||||
async def reply_mcp(self, msg):
|
||||
payload = msg.get("payload") or {}
|
||||
rid = payload.get("id")
|
||||
method = payload.get("method", "")
|
||||
if rid is None:
|
||||
return
|
||||
if method == "initialize":
|
||||
result = {"protocolVersion": "2024-11-05", "capabilities": {"tools": {}},
|
||||
"serverInfo": {"name": "esp32-s3", "version": "2.5.2"}}
|
||||
elif method == "tools/list":
|
||||
result = {"tools": []}
|
||||
else:
|
||||
result = {}
|
||||
out = json.dumps({"session_id": self.session_id, "type": "mcp",
|
||||
"payload": {"jsonrpc": "2.0", "id": rid, "result": result}},
|
||||
ensure_ascii=False)
|
||||
await self.ws.send(out)
|
||||
ev("mcp_reply", method=method, id=rid)
|
||||
|
||||
async def listen(self, state, mode="auto"):
|
||||
await self.ws.send(json.dumps({"session_id": self.session_id, "type": "listen",
|
||||
"state": state, "mode": mode}, ensure_ascii=False))
|
||||
ev("listen_tx", state=state, mode=mode)
|
||||
|
||||
# ---------- 场景 ----------
|
||||
def _entry_ip(self) -> str:
|
||||
"""从 --url 解析服务端入口 IPv4(zombie 场景防火墙阻断用)"""
|
||||
host = urllib.parse.urlparse(self.url).hostname or ""
|
||||
return socket.gethostbyname(host)
|
||||
|
||||
async def open_channel(self):
|
||||
old_pump = getattr(self, "pump_task", None)
|
||||
if old_pump:
|
||||
old_pump.cancel()
|
||||
old_probe = getattr(self, "probe_task", None)
|
||||
if old_probe:
|
||||
old_probe.cancel()
|
||||
for attr in ("mic_stream", "spk_stream"):
|
||||
s = getattr(self, attr, None)
|
||||
if s:
|
||||
try:
|
||||
s.stop(); s.close()
|
||||
except Exception:
|
||||
pass
|
||||
ev("connect_start", url=self.url)
|
||||
headers = {"Device-Id": self.device_id, "Client-Id": self.client_id,
|
||||
"Protocol-Version": "1"}
|
||||
if self.token:
|
||||
headers["Authorization"] = "Bearer " + self.token
|
||||
use_ping = self.scenario != "zombie" # zombie 场景忠实固件:无任何 ping
|
||||
self.ws = await websockets.connect(
|
||||
self.url, additional_headers=headers,
|
||||
max_size=None,
|
||||
ping_interval=20 if use_ping else None,
|
||||
ping_timeout=15 if use_ping else None,
|
||||
close_timeout=5)
|
||||
ev("connect_ok", client_ping=use_ping)
|
||||
await self.ws.send(self.hello_msg())
|
||||
ev("hello_tx")
|
||||
self.enc = OpusEncoder()
|
||||
self.mic_stream = sd.InputStream(samplerate=SAMPLE_RATE, channels=1, dtype="int16",
|
||||
blocksize=FRAME_SAMPLES, callback=self.mic_callback)
|
||||
self.spk_stream = sd.OutputStream(samplerate=48000, channels=1, dtype="int16")
|
||||
self.spk_stream.start()
|
||||
self.mic_stream.start()
|
||||
ev("audio_devices", mic=sd.query_devices(kind="input")["name"],
|
||||
spk=sd.query_devices(kind="output")["name"])
|
||||
# 端侧取证落盘: 上行(实际发往服务端, 16k) + 下行(解码后播放帧, 48k)
|
||||
for f in (self.rec_mic, self.rec_spk): # 先关旧句柄(重开频道场景)
|
||||
if f:
|
||||
try:
|
||||
f.close()
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
os.makedirs("rec", exist_ok=True)
|
||||
ts = time.strftime("%Y%m%d_%H%M%S")
|
||||
self.rec_mic = open(f"rec/mic_{ts}.pcm", "wb")
|
||||
self.rec_spk = open(f"rec/spk_{ts}.pcm", "wb")
|
||||
ev("rec_start", mic_dump=f"rec/mic_{ts}.pcm", spk_dump=f"rec/spk_{ts}.pcm",
|
||||
mic_sr=16000, spk_sr=48000)
|
||||
except Exception as e:
|
||||
ev("rec_start_err", err=str(e))
|
||||
old_player = self._player_thread
|
||||
if old_player is not None:
|
||||
self.play_q.put(None) # 让旧播放线程退出, 防双线程竞争队列
|
||||
self._player_thread = threading.Thread(target=self._player_loop, daemon=True)
|
||||
self._player_thread.start()
|
||||
self.pump_task = asyncio.create_task(self.mic_pump())
|
||||
if self.scenario != "zombie":
|
||||
self.probe_task = asyncio.create_task(self.ping_probe())
|
||||
|
||||
async def ping_probe(self):
|
||||
"""每 10s 主动 ping 一次测路径 RTT;pong 停 = 路径死,pong 活但无数据 = 服务端楔死"""
|
||||
while True:
|
||||
await asyncio.sleep(10)
|
||||
try:
|
||||
t0 = time.monotonic()
|
||||
await asyncio.wait_for(self.ws.ping(), timeout=8)
|
||||
ev("ping_rtt_ms", ms=round((time.monotonic() - t0) * 1000))
|
||||
except Exception as e:
|
||||
ev("ping_fail", err=repr(e))
|
||||
|
||||
async def recv_loop(self):
|
||||
try:
|
||||
async for raw in self.ws:
|
||||
self.last_incoming = time.monotonic()
|
||||
if isinstance(raw, str):
|
||||
await self.handle_text(raw)
|
||||
else:
|
||||
await self.audio_rx(raw)
|
||||
self.seq_rx = getattr(self, "seq_rx", 0) + 1
|
||||
if self.seq_rx % 50 == 1:
|
||||
ev("frame_rx", seq=self.seq_rx, bytes=len(raw))
|
||||
except websockets.ConnectionClosed as e:
|
||||
ev("ws_closed", code=getattr(e, "code", None), reason=str(getattr(e, "reason", "")))
|
||||
except Exception as e:
|
||||
ev("ws_error", err=repr(e))
|
||||
|
||||
async def abort(self, reason="user"):
|
||||
"""打断: 发 abort(type=abort, 服务端 AbortTextMessageHandler 才会路由) + 本地即停 + 释放上行静默
|
||||
(实证: type=listen/state=abort 服务端静默忽略, 抓包验证)"""
|
||||
if self.ws:
|
||||
try:
|
||||
await self.ws.send(json.dumps(
|
||||
{"session_id": self.session_id, "type": "abort"}))
|
||||
except Exception as e:
|
||||
ev("abort_send_err", err=str(e))
|
||||
self._spk_gen += 1
|
||||
dropped = 0
|
||||
while True:
|
||||
try:
|
||||
self.play_q.get_nowait()
|
||||
dropped += 1
|
||||
except Exception:
|
||||
break
|
||||
try:
|
||||
self.spk_stream.stop() # 丢弃设备缓冲中在发声帧, 立即静音
|
||||
self.spk_stream.start()
|
||||
except Exception as e:
|
||||
ev("spk_flush_err", err=str(e))
|
||||
self.tts_playing = False
|
||||
self._spk_started = False
|
||||
if self.half_duplex:
|
||||
self._set_mic_state(True, reason="abort")
|
||||
ev("abort_tx", reason=reason, dropped_playq=dropped)
|
||||
|
||||
async def do_turn(self, wav_path, settle=0.8):
|
||||
"""一轮对话:静置→播放语音(麦克风采集回灌)→等 stt→等 tts 播完"""
|
||||
self.turn_marker["speak_end"] = None
|
||||
self.first_audio_ts = None
|
||||
self.tts_done.clear()
|
||||
self.stt_event.clear()
|
||||
await asyncio.sleep(settle)
|
||||
await asyncio.get_event_loop().run_in_executor(None, self.play_wav, wav_path)
|
||||
try:
|
||||
await asyncio.wait_for(self.stt_event.wait(), timeout=15)
|
||||
except asyncio.TimeoutError:
|
||||
ev("stt_wait_timeout")
|
||||
try:
|
||||
await asyncio.wait_for(self.tts_done.wait(), timeout=60)
|
||||
except asyncio.TimeoutError:
|
||||
ev("tts_wait_timeout")
|
||||
await asyncio.sleep(0.5)
|
||||
|
||||
async def run_idle(self, seconds: float = 150.0):
|
||||
"""静音观察: 建连+listen 后保持静音 N 秒(验证静音空识别不触发兜底)"""
|
||||
await self.open_channel()
|
||||
recv = asyncio.create_task(self.recv_loop())
|
||||
await asyncio.sleep(0.5)
|
||||
await self.listen("start")
|
||||
t0 = time.monotonic()
|
||||
while time.monotonic() - t0 < seconds:
|
||||
await asyncio.sleep(10)
|
||||
ev("idle_tick", since_start_s=round(time.monotonic() - t0, 1))
|
||||
await self.listen("stop")
|
||||
await asyncio.sleep(1)
|
||||
await self.ws.close()
|
||||
ev("close_tx")
|
||||
recv.cancel()
|
||||
|
||||
async def run_baseline(self):
|
||||
await self.open_channel()
|
||||
recv = asyncio.create_task(self.recv_loop())
|
||||
await asyncio.sleep(0.5)
|
||||
await self.listen("start")
|
||||
for p in self.plays:
|
||||
wav, delay = (p.split("@") + ["1.0"])[:2]
|
||||
await asyncio.sleep(float(delay))
|
||||
await self.do_turn(wav)
|
||||
ev("baseline_done", turns=len(self.plays))
|
||||
await self.listen("stop")
|
||||
await asyncio.sleep(1)
|
||||
await self.ws.close()
|
||||
ev("close_tx")
|
||||
recv.cancel()
|
||||
|
||||
async def run_swallowhunt(self):
|
||||
# 与 baseline 同构,但轮次多、句子短,逐轮标记编号便于与服务端"重放 N 帧"对照
|
||||
await self.run_baseline()
|
||||
|
||||
async def run_zombie(self):
|
||||
"""半开连接模拟: 防火墙吞 FIN 后关旧连接→服务端无感知→新连接并存(复现会话不收敛)。
|
||||
需要管理员权限执行 netsh; 仅 Windows。"""
|
||||
import subprocess
|
||||
await self.open_channel()
|
||||
recv = asyncio.create_task(self.recv_loop())
|
||||
await asyncio.sleep(0.5)
|
||||
await self.listen("start")
|
||||
await self.do_turn(self.plays[0].split("@")[0])
|
||||
ev("zombie_phase", phase="turn_done_going_idle")
|
||||
# 进入"设备空闲":停发音频(真机空闲不推流),保持 WS 打开
|
||||
self.mic_on = False
|
||||
block_rule = 'xiaozhi-sim-block'
|
||||
block_ip = self._entry_ip()
|
||||
subprocess.run(["netsh", "advfirewall", "firewall", "delete", "rule", "name=" + block_rule],
|
||||
capture_output=True)
|
||||
r = subprocess.run(["netsh", "advfirewall", "firewall", "add", "rule", "name=" + block_rule,
|
||||
"dir=out", "action=block", f"remoteip={block_ip}",
|
||||
"protocol=tcp", "remoteport=443"], capture_output=True, text=True)
|
||||
ev("firewall_block_on", rc=r.returncode, remoteip=block_ip)
|
||||
t_block = time.monotonic()
|
||||
for m in (30, 60, 90, 120):
|
||||
await asyncio.sleep(m - (time.monotonic() - t_block) if time.monotonic() - t_block < m else 0)
|
||||
ev("zombie_tick", since_block_s=round(time.monotonic() - t_block, 1),
|
||||
since_incoming_s=round(time.monotonic() - self.last_incoming, 1))
|
||||
ev("fw_is_timeout_fires", note=f"固件 IsTimeout({FW_IS_TIMEOUT_S}s) 等效点:频道判死")
|
||||
# 固件语义:websocket_.reset() —— 关旧 socket(FIN 走被阻断路径=丢失),随后新开连接
|
||||
try:
|
||||
await self.ws.close()
|
||||
ev("old_socket_close_tx", note="FIN 被防火墙吞掉,服务端将无感知")
|
||||
except Exception as e:
|
||||
ev("old_socket_close_err", err=repr(e))
|
||||
await asyncio.sleep(3)
|
||||
ev("zombie_phase", phase="reopen_channel")
|
||||
self.mic_on = True
|
||||
await self.open_channel() # 新连接(新 hello/新 session)
|
||||
recv.cancel()
|
||||
recv = asyncio.create_task(self.recv_loop())
|
||||
await asyncio.sleep(0.5)
|
||||
await self.listen("start")
|
||||
await self.do_turn(self.plays[-1].split("@")[0])
|
||||
ev("zombie_phase", phase="post_reopen_turn_done")
|
||||
r = subprocess.run(["netsh", "advfirewall", "firewall", "delete", "rule", "name=" + block_rule],
|
||||
capture_output=True)
|
||||
ev("firewall_block_off", rc=r.returncode)
|
||||
# 保留 20s 观测旧僵尸是否被服务端回收
|
||||
await asyncio.sleep(20)
|
||||
ev("zombie_done")
|
||||
try:
|
||||
await self.ws.close()
|
||||
except Exception:
|
||||
pass
|
||||
recv.cancel()
|
||||
|
||||
|
||||
def phrase_cache_names(limit: int = 4) -> list:
|
||||
"""从 phrases.txt 读语料, 返回前 limit 条对应的预合成缓存文件名(存在才返回)"""
|
||||
import hashlib
|
||||
pf = os.path.join(os.path.dirname(os.path.abspath(__file__)), "phrases.txt")
|
||||
try:
|
||||
phrases = [ln.strip() for ln in open(pf, encoding="utf-8")
|
||||
if ln.strip() and not ln.strip().startswith("#")]
|
||||
except OSError:
|
||||
return []
|
||||
names = []
|
||||
for text in phrases:
|
||||
cache = f"cache_{hashlib.md5(text.encode()).hexdigest()[:10]}.wav"
|
||||
if os.path.exists(cache):
|
||||
names.append(cache)
|
||||
if len(names) >= limit:
|
||||
break
|
||||
return names
|
||||
|
||||
|
||||
def add_common_args(ap: argparse.ArgumentParser):
|
||||
ap.add_argument("--url", default=os.environ.get("DEVICE_SIM_URL", "ws://127.0.0.1:8000/xiaozhi/v1/"))
|
||||
ap.add_argument("--device-id", default=os.environ.get("DEVICE_SIM_ID", ""),
|
||||
help="设备 Device-Id(如 AA:BB:CC:DD:EE:FF)")
|
||||
ap.add_argument("--client-id", default=os.environ.get("DEVICE_SIM_CLIENT_ID") or None)
|
||||
ap.add_argument("--token", default="", help="设备 token(服务端开启认证时)")
|
||||
ap.add_argument("--secret", default="", help="服务端 auth_key,现签 token(服务端本机调试方便)")
|
||||
ap.add_argument("--noinject", action="store_true", help="禁用数字注入(走真实声学路径)")
|
||||
ap.add_argument("--duplex", action="store_true", help="关闭半双工互斥(播放期不静默上行, 回声实验用)")
|
||||
|
||||
|
||||
async def main():
|
||||
ap = argparse.ArgumentParser()
|
||||
add_common_args(ap)
|
||||
ap.add_argument("--scenario", required=True, choices=["baseline", "swallowhunt", "zombie", "idle"])
|
||||
ap.add_argument("--plays", default="")
|
||||
ap.add_argument("--out", required=True)
|
||||
ap.add_argument("--dur", type=float, default=600)
|
||||
args = ap.parse_args()
|
||||
if not args.device_id:
|
||||
ap.error("缺少 --device-id(或环境变量 DEVICE_SIM_ID)")
|
||||
args.client_id = args.client_id or __import__("uuid").uuid4().__str__()
|
||||
token = resolve_token(args)
|
||||
|
||||
global LOG
|
||||
LOG = open(args.out, "a", encoding="utf-8")
|
||||
plays = [p for p in args.plays.split(",") if p]
|
||||
if not plays and args.scenario in ("baseline", "swallowhunt", "zombie"):
|
||||
plays = phrase_cache_names(limit=6 if args.scenario == "swallowhunt" else 1)
|
||||
if not plays:
|
||||
ap.error("未提供 --plays,且 phrases.txt 对应的预合成缓存不存在(先运行 prewarm_corpus.py)")
|
||||
sim = Sim(args.scenario, plays, args.out, args.dur,
|
||||
url=args.url, device_id=args.device_id, client_id=args.client_id, token=token)
|
||||
if args.noinject:
|
||||
sim.inject = False
|
||||
if args.duplex:
|
||||
sim.half_duplex = False
|
||||
fn = {"baseline": sim.run_baseline, "swallowhunt": sim.run_swallowhunt,
|
||||
"zombie": sim.run_zombie, "idle": sim.run_idle}[args.scenario]
|
||||
try:
|
||||
await asyncio.wait_for(fn(), timeout=args.dur)
|
||||
except asyncio.TimeoutError:
|
||||
ev("sim_timeout", dur=args.dur)
|
||||
except Exception as e:
|
||||
ev("sim_fatal", err=repr(e))
|
||||
finally:
|
||||
try:
|
||||
sim.mic_stream.stop(); sim.mic_stream.close()
|
||||
sim.spk_stream.stop(); sim.spk_stream.close()
|
||||
except Exception:
|
||||
pass
|
||||
ev("sim_exit")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
365
main/xiaozhi-server/performance_tester/device_simulator_tui.py
Normal file
365
main/xiaozhi-server/performance_tester/device_simulator_tui.py
Normal file
|
|
@ -0,0 +1,365 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""device_simulator_tui.py — xiaozhi 设备模拟器交互前端(Textual TUI)
|
||||
|
||||
复用 device_simulator.py 的引擎(Sim/Opus/日志),逐轮全量回显输入输出
|
||||
+ L1-L5 时延面板 + 网络质量探针(WS ping RTT)+ 键位交互(唤醒/打断/音量/语料注入)。
|
||||
|
||||
用法(在 performance_tester 目录下):
|
||||
python device_simulator_tui.py --device-id AA:BB:CC:DD:EE:FF [--url ...] [--token ...|--secret ...]
|
||||
键位: w 唤醒 · space 打断 · m 麦克风 · p 探针 · e 重载语料 · 1-9 注入语料 · +/- 音量 · q 退出
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import time
|
||||
import wave as wavemod
|
||||
import numpy as np
|
||||
|
||||
from textual.app import App, ComposeResult
|
||||
from textual.containers import Vertical
|
||||
from textual.widgets import Header, Footer, RichLog, Static
|
||||
|
||||
import device_simulator as core
|
||||
from device_simulator import Sim, FRAME_SAMPLES, add_common_args, resolve_token
|
||||
|
||||
_BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
PHRASES_FILE = os.path.join(_BASE_DIR, "phrases.txt") # 每行一条, # 注释
|
||||
|
||||
|
||||
class SimTuiApp(App):
|
||||
TITLE = "xiaozhi device simulator"
|
||||
SUB_TITLE = "device protocol v1 · E2E"
|
||||
|
||||
CSS = """
|
||||
Screen { layout: vertical; }
|
||||
#conv { height: 40%; border: round $accent; }
|
||||
#lat { height: auto; border: round $success; padding: 0 1; }
|
||||
#meter{ height: auto; border: round $warning; padding: 0 1; }
|
||||
#evt { height: 1fr; border: round $primary; }
|
||||
"""
|
||||
BINDINGS = [
|
||||
("w", "wake", "唤醒"),
|
||||
("space", "abort", "打断"),
|
||||
("m", "mic", "麦克风"),
|
||||
("p", "probe", "探针"),
|
||||
("e", "reload", "重载语料"),
|
||||
("q", "quit", "退出"),
|
||||
]
|
||||
|
||||
def __init__(self, args):
|
||||
super().__init__()
|
||||
self.args = args
|
||||
self.sim = None
|
||||
self.phrases = []
|
||||
self.gain = 1.0
|
||||
self.probe_on = False
|
||||
self.probe_task = None
|
||||
self.state = "idle"
|
||||
self.turn_lat = [] # [(L2, L3, L5)] 近 5 轮
|
||||
self.cur = {} # 本轮时延锚点
|
||||
self._evt_poll = None
|
||||
self._evq = []
|
||||
self._evq_lock = __import__("threading").Lock()
|
||||
|
||||
# ---------- 布局 ----------
|
||||
def compose(self) -> ComposeResult:
|
||||
yield Header(show_clock=True)
|
||||
yield RichLog(id="conv", markup=True, highlight=False, wrap=True)
|
||||
yield Static("时延 本轮/近5轮均值 — 尚无数据", id="lat")
|
||||
yield Static("麦克风 ▁▁▁ 扬声器 ▁", id="meter")
|
||||
yield RichLog(id="evt", markup=False, wrap=True)
|
||||
yield Footer()
|
||||
|
||||
# ---------- 事件路由 ----------
|
||||
def _route_ev(self, rec: dict):
|
||||
"""线程安全事件入口: 入队, UI 定时器排空(规避跨线程/同线程差异)"""
|
||||
with self._evq_lock:
|
||||
self._evq.append(rec)
|
||||
|
||||
def _handle_ev(self, rec: dict):
|
||||
kind = rec.get("ev", "")
|
||||
t = rec.get("t_wall", "")
|
||||
evt_log = self.query_one("#evt", RichLog)
|
||||
conv = self.query_one("#conv", RichLog)
|
||||
# 对话回显
|
||||
if kind == "stt_rx":
|
||||
conv.write(f"[b cyan]{t} 我[/] {rec.get('text','')}")
|
||||
self.cur["stt"] = rec.get("t_mono")
|
||||
l2_real = rec.get("stt_latency_from_speak_end")
|
||||
if l2_real is not None:
|
||||
self.cur["l2_real"] = l2_real # 真麦克风: 引擎按最近实音锚计算
|
||||
self.state = "listening"
|
||||
elif kind == "mic_mute":
|
||||
self._mic_muted = rec.get("reason", "tts")
|
||||
elif kind == "mic_unmute":
|
||||
self._mic_muted = None
|
||||
elif kind == "tts_state" and rec.get("state") == "sentence_start":
|
||||
conv.write(f"[b green]{t} 小智[/] {rec.get('text','')}")
|
||||
self.state = "speaking"
|
||||
elif kind == "tts_state" and rec.get("state") == "stop":
|
||||
self.state = "listening"
|
||||
elif kind == "speak_start":
|
||||
conv.write(f"[b yellow]{t} 我(注入)[/] {rec.get('file','')} ({rec.get('dur_ms',0)}ms)")
|
||||
self.cur["speak_start"] = rec.get("t_mono")
|
||||
self.cur["speak_end"] = None
|
||||
elif kind == "speak_end":
|
||||
self.cur["speak_end"] = rec.get("t_mono")
|
||||
# 状态机
|
||||
elif kind == "connect_ok":
|
||||
self.state = "connecting"
|
||||
elif kind == "hello_rx":
|
||||
self.state = "listening"
|
||||
self.cur["l1_done"] = rec.get("t_mono")
|
||||
elif kind == "listen_tx" and rec.get("state") == "start":
|
||||
if self.state == "idle":
|
||||
self.state = "connecting"
|
||||
elif kind == "ws_closed":
|
||||
self.state = "idle"
|
||||
elif kind in ("tts_first_audio", "tts_play_start"):
|
||||
self.cur.setdefault(kind, rec.get("t_mono"))
|
||||
# 时延计算
|
||||
if kind in ("stt_rx", "tts_first_audio", "tts_play_start", "hello_rx", "speak_end"):
|
||||
self._update_latency()
|
||||
# 探针
|
||||
if kind == "ping_rtt_ms":
|
||||
self._last_rtt = rec.get("ms")
|
||||
# 事件尾栏(全量)
|
||||
evt_log.write(f"{t} {json.dumps(rec, ensure_ascii=False)[:180]}")
|
||||
self._refresh_meter(rec)
|
||||
|
||||
def _update_latency(self):
|
||||
cur = self.cur
|
||||
l2 = l3 = l5 = None
|
||||
se, stt, fa, ps = cur.get("speak_end"), cur.get("stt"), cur.get("tts_first_audio"), cur.get("tts_play_start")
|
||||
l2 = cur.get("l2_real") # 引擎锚(真麦克风/注入均有效)
|
||||
if stt and fa and fa > stt:
|
||||
l3 = round(fa - stt, 2)
|
||||
if se and ps and ps > se:
|
||||
l5 = round(ps - se, 2)
|
||||
if l2 is not None and fa:
|
||||
row = (l2, l3, l5)
|
||||
if not self.turn_lat or self.turn_lat[-1] != row:
|
||||
self.turn_lat.append(row)
|
||||
self.turn_lat = self.turn_lat[-5:]
|
||||
lat = self.query_one("#lat", Static)
|
||||
if self.turn_lat:
|
||||
n = len(self.turn_lat)
|
||||
m2 = round(sum(r[0] or 0 for r in self.turn_lat) / n, 2)
|
||||
m3 = round(sum(r[1] or 0 for r in self.turn_lat) / n, 2)
|
||||
m5 = round(sum(r[2] or 0 for r in self.turn_lat) / n, 2)
|
||||
last = self.turn_lat[-1]
|
||||
lat.update(
|
||||
f"L1 唤醒→建连 {self._l1}s L2 说完→STT {last[0]}/{m2}s "
|
||||
f"L3 STT→首包 {last[1]}/{m3}s L5 端到端 {last[2]}/{m5}s "
|
||||
f"(近{n}轮均值)")
|
||||
self.sub_title = f"device · 状态 {self.state} · 会话 {(self.sim.session_id or '-')[:8]} · 音量 {int(self.gain*70)}"
|
||||
|
||||
_l1 = 0.0
|
||||
|
||||
def _refresh_meter(self, rec):
|
||||
meter = self.query_one("#meter", Static)
|
||||
mic_bar = "▇" * min(10, (rec.get("rms", 0) or 0) // 400) if rec.get("ev") == "frame_tx" else None
|
||||
if mic_bar is not None:
|
||||
self._mic_level = mic_bar
|
||||
playing = "▶ 播放中" if rec.get("ev") == "tts_first_audio" else self._spk_state
|
||||
mic_label = "🔇静默(播放中)" if getattr(self, "_mic_muted", None) else getattr(self, "_mic_level", "▁▁▁")
|
||||
meter.update(f"麦克风 {mic_label} 扬声器 {playing} "
|
||||
f"RTT {getattr(self,'_last_rtt','-')}ms 探针 {'开' if self.probe_on else '关'}")
|
||||
|
||||
_mic_level = "▁▁▁"
|
||||
_spk_state = "▁"
|
||||
_mic_muted = None
|
||||
|
||||
# ---------- 动作 ----------
|
||||
def on_key(self, event):
|
||||
if event.character.isdigit() and event.character != "0":
|
||||
self.run_worker(self._phrase_n(int(event.character)), exclusive=False)
|
||||
elif event.character == "+":
|
||||
self.run_worker(self.action_vol_up(), exclusive=False)
|
||||
elif event.character == "-":
|
||||
self.run_worker(self.action_vol_dn(), exclusive=False)
|
||||
|
||||
async def action_wake(self):
|
||||
if self.sim is None:
|
||||
self.sim = Sim("tui", [], "", 86400,
|
||||
url=self.args.url, device_id=self.args.device_id,
|
||||
client_id=self.args.client_id, token=self.token)
|
||||
if self.args.noinject:
|
||||
self.sim.inject = False
|
||||
if self.args.duplex:
|
||||
self.sim.half_duplex = False
|
||||
if self.sim.asr_ws is None:
|
||||
self._l1 = 0.0
|
||||
t0 = time.monotonic()
|
||||
await self.sim.open_channel()
|
||||
self._l1 = round(time.monotonic() - t0, 2)
|
||||
# 引擎 recv_loop 是独立任务(headless 场景里手动创建), TUI 侧同样必须启动
|
||||
self.sim.recv_task = asyncio.create_task(self.sim.recv_loop())
|
||||
await self.sim.listen("start")
|
||||
else:
|
||||
self.notify("频道已打开")
|
||||
|
||||
def _route_ev_bridge(self, rec):
|
||||
# 引擎 ev() 被替换为本桥: 写 JSONL + 路由 UI
|
||||
if core.LOG:
|
||||
core.LOG.write(json.dumps(rec, ensure_ascii=False) + "\n")
|
||||
core.LOG.flush()
|
||||
self._route_ev(rec)
|
||||
|
||||
async def action_abort(self):
|
||||
if self.sim and self.sim.asr_ws and self.sim.session_id:
|
||||
await self.sim.abort(reason="tui_space")
|
||||
self.state = "listening"
|
||||
else:
|
||||
self.notify("频道未打开")
|
||||
|
||||
async def action_mic(self):
|
||||
if self.sim:
|
||||
self.sim.mic_on = not self.sim.mic_on
|
||||
self.notify(f"麦克风 {'开' if self.sim.mic_on else '关'}")
|
||||
|
||||
async def action_probe(self):
|
||||
self.probe_on = not self.probe_on
|
||||
if self.probe_on and self.sim and self.sim.asr_ws:
|
||||
self.sim.probe_task = asyncio.create_task(self.sim.ping_probe())
|
||||
self.notify("探针 P1 开(WS ping RTT)")
|
||||
elif not self.probe_on and getattr(self.sim, "probe_task", None):
|
||||
self.sim.probe_task.cancel()
|
||||
self.sim.probe_task = None
|
||||
self.notify("探针关")
|
||||
|
||||
async def action_reload(self):
|
||||
self._load_phrases()
|
||||
self.notify(f"语料重载: {len(self.phrases)} 条")
|
||||
|
||||
def _load_phrases(self):
|
||||
try:
|
||||
with open(PHRASES_FILE, encoding="utf-8") as f:
|
||||
self.phrases = [ln.strip() for ln in f if ln.strip() and not ln.strip().startswith("#")]
|
||||
except FileNotFoundError:
|
||||
self.phrases = []
|
||||
self.notify("phrases.txt 不存在")
|
||||
|
||||
async def _inject_phrase(self, text: str):
|
||||
"""语料文本 → 本地合成(缓存) → 数字注入; 合成链: edge-tts → PowerShell SAPI → 报错"""
|
||||
import hashlib, subprocess
|
||||
cache = os.path.join(_BASE_DIR, f"cache_{hashlib.md5(text.encode()).hexdigest()[:10]}.wav")
|
||||
if not os.path.exists(cache):
|
||||
ok = False
|
||||
try:
|
||||
import edge_tts
|
||||
await edge_tts.Communicate(text, "zh-CN-XiaoxiaoNeural").save(cache)
|
||||
ok = os.path.getsize(cache) > 1000
|
||||
except Exception:
|
||||
ok = False
|
||||
if not ok:
|
||||
try:
|
||||
r = subprocess.run(
|
||||
["powershell", "-ExecutionPolicy", "Bypass", "-File",
|
||||
os.path.join(_BASE_DIR, "speak.ps1"), text, os.path.abspath(cache)],
|
||||
capture_output=True, timeout=30)
|
||||
ok = os.path.exists(cache) and os.path.getsize(cache) > 1000
|
||||
except Exception as e:
|
||||
self.notify(f"合成失败: {e}")
|
||||
return
|
||||
if not ok:
|
||||
self.notify("合成失败(edge-tts+SAPI 均不可用)")
|
||||
return
|
||||
self.cur = {"speak_start": time.monotonic()}
|
||||
await self.sim.do_turn(cache)
|
||||
|
||||
async def _phrase_n(self, n: int):
|
||||
if not self.phrases:
|
||||
self._load_phrases()
|
||||
if 1 <= n <= len(self.phrases):
|
||||
await self._inject_phrase(self.phrases[n - 1])
|
||||
|
||||
async def action_vol_up(self):
|
||||
self.gain = min(2.0, self.gain + 0.2)
|
||||
if self.sim:
|
||||
self.sim.playback_gain = self.gain
|
||||
self.notify(f"播放增益 {int(self.gain*50)}%")
|
||||
|
||||
async def action_vol_dn(self):
|
||||
self.gain = max(0.2, self.gain - 0.2)
|
||||
if self.sim:
|
||||
self.sim.playback_gain = self.gain
|
||||
self.notify(f"播放增益 {int(self.gain*50)}%")
|
||||
|
||||
# ---------- 生命周期 ----------
|
||||
def on_mount(self):
|
||||
self._load_phrases()
|
||||
conv = self.query_one("#conv", RichLog)
|
||||
conv.write("[b]键位[/] w唤醒 · space打断(本地即停+清队列) · m麦克风 · p探针 · e重载语料 · q退出 · 1-9注入语料 · +/- 音量")
|
||||
conv.write("[b]半双工[/] 播放期间上行自动静默(互斥防回声, 与真机一致); 上下行 PCM 落盘 rec/ 供取证")
|
||||
conv.write(f"[b]语料[/] {len(self.phrases)} 条(phrases.txt); 人工对话直接对麦克风说话(auto 模式)")
|
||||
self._evt_poll = self.set_interval(0.2, self._poll_events)
|
||||
|
||||
def _poll_events(self):
|
||||
while True:
|
||||
with self._evq_lock:
|
||||
if not self._evq:
|
||||
return
|
||||
rec = self._evq.pop(0)
|
||||
self._handle_ev(rec)
|
||||
|
||||
async def action_quit(self):
|
||||
try:
|
||||
if self.sim and self.sim.asr_ws:
|
||||
await self.sim.ws.close()
|
||||
except Exception:
|
||||
pass
|
||||
self.exit()
|
||||
|
||||
|
||||
def install_bridge(app):
|
||||
"""引擎 ev() 桥接到 TUI(签名与引擎一致: (kind, **kw) → 记录构建+JSONL+UI 路由)"""
|
||||
log_path = os.path.join(
|
||||
_BASE_DIR, "tui_" + time.strftime("%Y%m%d_%H%M%S") + ".jsonl")
|
||||
log_fh = open(log_path, "a", encoding="utf-8")
|
||||
|
||||
def bridge(kind, **kw):
|
||||
rec = {"t_wall": time.strftime("%H:%M:%S") + f".{int(time.time()*1000)%1000:03d}",
|
||||
"t_mono": round(time.monotonic() - core.T0, 3), "ev": kind}
|
||||
rec.update(kw)
|
||||
try:
|
||||
log_fh.write(json.dumps(rec, ensure_ascii=False) + "\n")
|
||||
log_fh.flush()
|
||||
except Exception:
|
||||
pass
|
||||
app._route_ev(rec)
|
||||
|
||||
core.ev = bridge
|
||||
|
||||
|
||||
def main():
|
||||
import argparse
|
||||
ap = argparse.ArgumentParser()
|
||||
add_common_args(ap)
|
||||
args = ap.parse_args()
|
||||
if not args.device_id:
|
||||
ap.error("缺少 --device-id(或环境变量 DEVICE_SIM_ID)")
|
||||
args.client_id = args.client_id or __import__("uuid").uuid4().__str__()
|
||||
args.token = resolve_token(args)
|
||||
|
||||
app = SimTuiApp(args)
|
||||
install_bridge(app)
|
||||
try:
|
||||
app.run()
|
||||
except Exception:
|
||||
# 启动/运行异常落盘, 留证据通道
|
||||
import traceback as _tb
|
||||
p = os.path.join(_BASE_DIR, f"tui_error_{time.strftime('%Y%m%d_%H%M%S')}.log")
|
||||
try:
|
||||
with open(p, "w", encoding="utf-8") as f:
|
||||
f.write(_tb.format_exc())
|
||||
except Exception:
|
||||
pass
|
||||
raise
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
10
main/xiaozhi-server/performance_tester/phrases.txt
Normal file
10
main/xiaozhi-server/performance_tester/phrases.txt
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
# 测试语料 — 每行一条, # 开头为注释; TUI 中 1-9 映射前 9 条, e 键重载本文件
|
||||
给我科普一下AI的知识吧
|
||||
今天深圳的天气怎么样?
|
||||
帮我讲一个关于太空探索的小故事,要带一点悬念的
|
||||
现在几点了
|
||||
把音量调到五十
|
||||
你是谁
|
||||
讲个笑话吧
|
||||
明天需要带伞吗
|
||||
放一首歌吧
|
||||
20
main/xiaozhi-server/performance_tester/prewarm_corpus.py
Normal file
20
main/xiaozhi-server/performance_tester/prewarm_corpus.py
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
# prewarm_corpus.py — 按 device_simulator_tui 的 md5 约定预合成全部语料(SAPI)
|
||||
import hashlib
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
||||
|
||||
phrases = [ln.strip() for ln in open("phrases.txt", encoding="utf-8")
|
||||
if ln.strip() and not ln.strip().startswith("#")]
|
||||
for idx, text in enumerate(phrases, 1):
|
||||
cache = f"cache_{hashlib.md5(text.encode()).hexdigest()[:10]}.wav"
|
||||
if os.path.exists(cache) and os.path.getsize(cache) > 1000:
|
||||
print(f"[{idx}] cached: {text}")
|
||||
continue
|
||||
r = subprocess.run(
|
||||
["powershell", "-ExecutionPolicy", "Bypass", "-File", "speak.ps1", text,
|
||||
os.path.abspath(cache)], capture_output=True, timeout=40)
|
||||
size = os.path.getsize(cache) if os.path.exists(cache) else 0
|
||||
print(f"[{idx}] {size}B: {text}")
|
||||
print("done")
|
||||
8
main/xiaozhi-server/performance_tester/speak.ps1
Normal file
8
main/xiaozhi-server/performance_tester/speak.ps1
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
param([Parameter(Mandatory=$true)][string]$Text, [Parameter(Mandatory=$true)][string]$Out)
|
||||
Add-Type -AssemblyName System.Speech
|
||||
$s = New-Object System.Speech.Synthesis.SpeechSynthesizer
|
||||
$s.SetOutputToWaveFile($Out, (New-Object System.Speech.AudioFormat.SpeechAudioFormatInfo(16000, [System.Speech.AudioFormat.AudioBitsPerSample]::Sixteen, [System.Speech.AudioFormat.AudioChannel]::Mono)))
|
||||
$s.Rate = 0
|
||||
$s.Speak($Text)
|
||||
$s.Dispose()
|
||||
Write-Output ("OK " + (Get-Item $Out).Length)
|
||||
5
main/xiaozhi-server/performance_tester/start_tui.cmd
Normal file
5
main/xiaozhi-server/performance_tester/start_tui.cmd
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
@echo off
|
||||
rem device-simulator-tui launcher - double click; q to quit; errors go to tui_error_*.log
|
||||
cd /d "%~dp0"
|
||||
python -X utf8 device_simulator_tui.py %*
|
||||
pause
|
||||
4
main/xiaozhi-server/performance_tester/start_tui.sh
Normal file
4
main/xiaozhi-server/performance_tester/start_tui.sh
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
#!/usr/bin/env bash
|
||||
# device-simulator-tui launcher — ./start_tui.sh
|
||||
cd "$(dirname "$0")"
|
||||
python3 -X utf8 device_simulator_tui.py "$@"
|
||||
Loading…
Reference in a new issue