feat: wire Opus decode + VAD turn detection to firmware-compatible reply

Device streams continuous 20ms Opus frames with no turn markers. The loop
now decodes Opus, feeds the VAD, fires one turn on end-of-speech, and
replies with the firmware JSON contract (tts.state / stt.text) + Opus audio.

- app/audio/opus.py: RealOpus/Opus16kEncoder/PassthroughOpus; fix add_dll_directory
  to use absolute paths (WinError 87)
- app/xiaozhi/websocket.py: DeviceLoop.run() = decode -> VAD -> run_turn -> reply
- app/config.py: AudioConfig (opus kind + VAD thresholds)
- config.yaml: audio.opus=real, VAD 5/8/3000 frames
- tests/test_app.py: utterance = loud burst + silence; asserts stt + audio + tts stop
- requirements.txt: opuslib>=3

21/21 tests; live handshake verified via zhi.moreminimore.com on venv python.
This commit is contained in:
kunthawat committed 2026-10-03 20:35:38 +07:00
1 parent 3bfeff29bd
commit 261b0f3e91
7 files changed
+293 -40

No files matched your search

+13
View File
@@ -56,6 +56,18 @@ class ProtocolConfig(BaseModel):
frame_ms: int = 20 # typical Xiaozhi Opus frame size
class AudioConfig(BaseModel):
"""Device audio path: the device streams continuous 20 ms Opus frames
(no turn markers), so the bridge decodes Opus, accumulates through a
VAD, and fires a turn on end-of-speech. ``passthrough`` treats frames as
raw PCM (synthetic clients / tests without a native codec)."""
opus: Literal["passthrough", "real"] = "passthrough"
vad_min_speech_frames: int = 5 # 100 ms of speech before an utterance counts
vad_min_silence_frames: int = 8 # 160 ms of silence ends the utterance
vad_max_utterance_frames: int = 3000 # 60 s safety flush
class OtaConfig(BaseModel):
"""OTA endpoint data (GET/POST /ota) — xiaozhi device onboarding.
@@ -95,6 +107,7 @@ class Config(BaseModel):
security: SecurityConfig = Field(default_factory=SecurityConfig)
ota: Optional[OtaConfig] = None
gpu: GpuConfig = Field(default_factory=GpuConfig)
audio: AudioConfig = Field(default_factory=AudioConfig)
@classmethod
def from_dict(cls, data: dict) -> "Config":