feat(voice_worker): M1 multi-speaker streaming STT and sentence TTS over a local WebSocket
- One shared faster-whisper model for every speaker, batched; finals go before partials and partial batches are capped at two so a final never waits behind a ten-speaker pass. - Per-speaker 48k stereo to 16k mono conversion, streaming Silero VAD, utterance ends on VAD silence, Discord speaking_end, missing audio, or the 28 s Whisper window. - MeloTTS in its own venv through a child process; sentences stream out one chunk each, with incremental text, cancel and failure reporting. - Loopback-only server with a token from the environment; logs carry no transcript text. - Spec in src/voice_worker/voice_worker.spec.md; unit tests with fake engines and an opt-in real-GPU test that streams ten speakers at once. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
253
tests/test_voice_worker_scheduler.py
Normal file
253
tests/test_voice_worker_scheduler.py
Normal file
@@ -0,0 +1,253 @@
|
||||
"""Voice worker: the shared-model STT scheduler (batching, finals first, partial coalescing)."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import threading
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
from voice_worker.scheduler import DecodeOptions, SttScheduler
|
||||
|
||||
FINAL = DecodeOptions(beam=2, language="ko", hotwords=())
|
||||
PARTIAL = DecodeOptions(beam=1, language="ko", hotwords=())
|
||||
|
||||
|
||||
class FakeTranscriber:
|
||||
"""Names each clip by its length; the first call can be held to build up a queue."""
|
||||
|
||||
def __init__(self, hold_first: bool = False, fail_on_beam: int | None = None):
|
||||
self.calls: list[tuple[DecodeOptions, list[int]]] = []
|
||||
self.gate = threading.Event()
|
||||
self.entered = threading.Event()
|
||||
if not hold_first:
|
||||
self.gate.set()
|
||||
self.fail_on_beam = fail_on_beam
|
||||
|
||||
def transcribe(self, clips, opts):
|
||||
self.entered.set()
|
||||
self.gate.wait(5)
|
||||
self.calls.append((opts, [len(c) for c in clips]))
|
||||
if opts.beam == self.fail_on_beam:
|
||||
raise RuntimeError("boom")
|
||||
return [f"n{len(c)}" for c in clips]
|
||||
|
||||
|
||||
def clip(n: int) -> np.ndarray:
|
||||
return np.zeros(n, dtype=np.float32)
|
||||
|
||||
|
||||
async def _run(body):
|
||||
return await asyncio.wait_for(body(), 10)
|
||||
|
||||
|
||||
def run(body):
|
||||
return asyncio.run(_run(body))
|
||||
|
||||
|
||||
async def _wait_entered(t: FakeTranscriber):
|
||||
while not t.entered.is_set():
|
||||
await asyncio.sleep(0.005)
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_finals_queued_while_busy_go_in_one_batch_before_partials():
|
||||
t = FakeTranscriber(hold_first=True)
|
||||
|
||||
async def body():
|
||||
s = SttScheduler(t, max_partial_batch=4)
|
||||
s.start()
|
||||
got = []
|
||||
done = asyncio.Event()
|
||||
|
||||
def cb(name):
|
||||
def _cb(res):
|
||||
got.append((name, res.text))
|
||||
if len(got) == 5:
|
||||
done.set()
|
||||
return _cb
|
||||
|
||||
s.submit_partial(("a", "1"), clip(10), PARTIAL, cb("p-first"))
|
||||
await _wait_entered(t)
|
||||
s.submit_partial(("a", "2"), clip(20), PARTIAL, cb("p2"))
|
||||
s.submit_final(("a", "3"), clip(30), FINAL, cb("f3"))
|
||||
s.submit_final(("a", "4"), clip(40), FINAL, cb("f4"))
|
||||
s.submit_partial(("a", "5"), clip(50), PARTIAL, cb("p5"))
|
||||
t.gate.set()
|
||||
await done.wait()
|
||||
await s.stop()
|
||||
return got
|
||||
|
||||
got = run(body)
|
||||
assert [c[1] for c in t.calls] == [[10], [30, 40], [20, 50]]
|
||||
assert [c[0].beam for c in t.calls] == [1, 2, 1]
|
||||
assert got == [("p-first", "n10"), ("f3", "n30"), ("f4", "n40"), ("p2", "n20"), ("p5", "n50")]
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_a_newer_partial_replaces_the_waiting_one_for_the_same_speaker():
|
||||
t = FakeTranscriber(hold_first=True)
|
||||
|
||||
async def body():
|
||||
s = SttScheduler(t)
|
||||
s.start()
|
||||
got = []
|
||||
done = asyncio.Event()
|
||||
s.submit_final(("a", "x"), clip(5), FINAL, lambda r: None)
|
||||
await _wait_entered(t)
|
||||
s.submit_partial(("a", "1"), clip(10), PARTIAL, lambda r: got.append(r.text))
|
||||
s.submit_partial(("a", "1"), clip(20), PARTIAL, lambda r: (got.append(r.text), done.set()))
|
||||
t.gate.set()
|
||||
await done.wait()
|
||||
await asyncio.sleep(0.05)
|
||||
await s.stop()
|
||||
return got
|
||||
|
||||
assert run(body) == ["n20"]
|
||||
assert [c[1] for c in t.calls] == [[5], [20]]
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_dropped_partials_are_never_transcribed():
|
||||
t = FakeTranscriber(hold_first=True)
|
||||
|
||||
async def body():
|
||||
s = SttScheduler(t)
|
||||
s.start()
|
||||
done = asyncio.Event()
|
||||
got = []
|
||||
s.submit_final(("a", "x"), clip(5), FINAL, lambda r: None)
|
||||
await _wait_entered(t)
|
||||
s.submit_partial(("a", "1"), clip(10), PARTIAL, lambda r: got.append(r.text))
|
||||
s.submit_partial(("b", "1"), clip(20), PARTIAL, lambda r: (got.append(r.text), done.set()))
|
||||
s.drop_partials(lambda key: key[0] == "a")
|
||||
t.gate.set()
|
||||
await done.wait()
|
||||
await s.stop()
|
||||
return got
|
||||
|
||||
assert run(body) == ["n20"]
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_different_decode_settings_are_not_mixed_in_one_batch():
|
||||
t = FakeTranscriber(hold_first=True)
|
||||
other = DecodeOptions(beam=2, language=None, hotwords=("자비스",))
|
||||
|
||||
async def body():
|
||||
s = SttScheduler(t)
|
||||
s.start()
|
||||
n = []
|
||||
done = asyncio.Event()
|
||||
|
||||
def cb(r):
|
||||
n.append(r)
|
||||
if len(n) == 4:
|
||||
done.set()
|
||||
|
||||
s.submit_final(("a", "x"), clip(1), FINAL, cb)
|
||||
await _wait_entered(t)
|
||||
s.submit_final(("a", "1"), clip(10), FINAL, cb)
|
||||
s.submit_final(("b", "1"), clip(20), other, cb)
|
||||
s.submit_final(("a", "2"), clip(30), FINAL, cb)
|
||||
t.gate.set()
|
||||
await done.wait()
|
||||
await s.stop()
|
||||
|
||||
run(body)
|
||||
assert sorted(c[1] for c in t.calls[1:]) == [[10, 30], [20]]
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_batches_are_capped():
|
||||
t = FakeTranscriber(hold_first=True)
|
||||
|
||||
async def body():
|
||||
s = SttScheduler(t, max_batch=3)
|
||||
s.start()
|
||||
n = []
|
||||
done = asyncio.Event()
|
||||
|
||||
def cb(r):
|
||||
n.append(r)
|
||||
if len(n) == 8:
|
||||
done.set()
|
||||
|
||||
s.submit_final(("a", "x"), clip(1), FINAL, cb)
|
||||
await _wait_entered(t)
|
||||
for i in range(7):
|
||||
s.submit_final(("a", str(i)), clip(10 + i), FINAL, cb)
|
||||
t.gate.set()
|
||||
await done.wait()
|
||||
await s.stop()
|
||||
|
||||
run(body)
|
||||
assert [len(c[1]) for c in t.calls] == [1, 3, 3, 1]
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_a_failing_batch_reports_the_error_and_the_scheduler_keeps_going():
|
||||
t = FakeTranscriber(fail_on_beam=2)
|
||||
|
||||
async def body():
|
||||
s = SttScheduler(t)
|
||||
s.start()
|
||||
out = []
|
||||
done = asyncio.Event()
|
||||
s.submit_final(("a", "1"), clip(10), FINAL, out.append)
|
||||
s.submit_partial(("a", "2"), clip(20), PARTIAL, lambda r: (out.append(r), done.set()))
|
||||
await done.wait()
|
||||
await s.stop()
|
||||
return out
|
||||
|
||||
out = run(body)
|
||||
assert out[0].text == "" and out[0].error
|
||||
assert out[1].text == "n20" and out[1].error is None
|
||||
assert out[0].stt_ms >= 0
|
||||
|
||||
|
||||
|
||||
class StepTranscriber:
|
||||
"""Every call waits for one ``step()``, so the test decides when each batch finishes."""
|
||||
|
||||
def __init__(self):
|
||||
self.calls: list[list[int]] = []
|
||||
self._steps = threading.Semaphore(0)
|
||||
|
||||
def step(self):
|
||||
self._steps.release()
|
||||
|
||||
def transcribe(self, clips, opts):
|
||||
self.calls.append([len(c) for c in clips])
|
||||
self._steps.acquire(timeout=5)
|
||||
return ["" for _ in clips]
|
||||
|
||||
|
||||
@pytest.mark.unit
|
||||
def test_partial_batches_are_kept_small_so_finals_can_cut_in():
|
||||
t = StepTranscriber()
|
||||
|
||||
async def until(n):
|
||||
while len(t.calls) < n:
|
||||
await asyncio.sleep(0.005)
|
||||
|
||||
async def body():
|
||||
s = SttScheduler(t, max_partial_batch=2)
|
||||
s.start()
|
||||
for i in range(5):
|
||||
s.submit_partial(("a", str(i)), clip(10 + i), PARTIAL, lambda r: None)
|
||||
await until(1) # first partial batch is running
|
||||
s.submit_final(("a", "late"), clip(99), FINAL, lambda r: None)
|
||||
t.step()
|
||||
await until(2)
|
||||
t.step()
|
||||
await until(3)
|
||||
t.step()
|
||||
await until(4)
|
||||
t.step()
|
||||
await asyncio.sleep(0.02)
|
||||
await s.stop()
|
||||
|
||||
run(body)
|
||||
assert t.calls == [[10, 11], [99], [12, 13], [14]]
|
||||
Reference in New Issue
Block a user