"""Voice worker end to end over a real local WebSocket, with fake STT/VAD/TTS engines. The fake VAD reads speech probability off the amplitude and the fake transcriber names a clip by its "level": each speaker sends a tone whose amplitude is the level (what the fake VAD reads) and whose pitch is level x 1000 Hz (what the fake transcriber reads, since the worker evens out loudness). """ from __future__ import annotations import asyncio import json import time import numpy as np import pytest from voice_worker.protocol import decode_tts_frame, encode_audio_frame from voice_worker.server import CLOSE_BAD_HELLO, CLOSE_BAD_TOKEN, VoiceWorker websockets = pytest.importorskip("websockets") from websockets.asyncio.client import connect # noqa: E402 TOKEN = "test-token" class AmplitudeVad: def prob(self, window): return float(np.max(np.abs(window))) class LevelTranscriber: def __init__(self): self.calls = [] def transcribe(self, clips, opts): self.calls.append((opts, len(clips))) return [f"level {np.argmax(np.abs(np.fft.rfft(c))) * 16000 / len(c) / 1000:.1f}" for c in clips] class FakeTts: """10 samples per character; optional delay per sentence; can fail on a marker.""" def __init__(self, delay: float = 0.0): self.delay = delay self.texts = [] def synthesize(self, text, rate): if self.delay: time.sleep(self.delay) if "FAIL" in text: raise RuntimeError("engine broke") self.texts.append(text) return np.full(len(text) * 10, 1000, dtype=np.int16) def pcm(level: float, ms: int, rate=16000, channels=1) -> bytes: n = rate * ms // 1000 # 20 ms frames hold whole cycles at these pitches, so frames join without a click. x = level * np.sin(2 * np.pi * level * 1000 * np.arange(n) / rate) return (np.repeat(x, channels) * 32767).astype(" MAX_SPEAKERS and all(m["text"] == "level 0.9" for m in finals)