Files
javis_bot/src/voice_worker/vad.py
javis-bot a7685e9078 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>
2026-11-11 10:00:06 +09:00

46 lines
1.5 KiB
Python

"""Streaming Silero VAD (the v6 model bundled with faster-whisper), one state per speaker."""
from __future__ import annotations
import os
import numpy as np
from .segmenter import WINDOW
_CONTEXT = 64
class SileroVad:
"""Owns the ONNX session; ``stream()`` gives each speaker its own recurrent state."""
def __init__(self, path: str | None = None):
import onnxruntime
if path is None:
from faster_whisper.utils import get_assets_path
path = os.path.join(get_assets_path(), "silero_vad_v6.onnx")
opts = onnxruntime.SessionOptions()
opts.inter_op_num_threads = 1
opts.intra_op_num_threads = 1
opts.enable_cpu_mem_arena = False
opts.log_severity_level = 4
self.session = onnxruntime.InferenceSession(path, providers=["CPUExecutionProvider"], sess_options=opts)
def stream(self) -> "SileroStream":
return SileroStream(self.session)
class SileroStream:
def __init__(self, session):
self.session = session
self.h = np.zeros((1, 1, 128), dtype=np.float32)
self.c = np.zeros((1, 1, 128), dtype=np.float32)
self.context = np.zeros(_CONTEXT, dtype=np.float32)
def prob(self, window: np.ndarray) -> float:
assert len(window) == WINDOW
x = np.concatenate([self.context, window.astype(np.float32, copy=False)])[None, :]
out, self.h, self.c = self.session.run(None, {"input": x, "h": self.h, "c": self.c})
self.context = x[0, -_CONTEXT:]
return float(np.asarray(out).reshape(-1)[0])