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:
javis-bot
2026-11-11 10:00:06 +09:00
parent b4926d5e19
commit a7685e9078
26 changed files with 2660 additions and 1 deletions

58
src/voice_worker/audio.py Normal file
View File

@@ -0,0 +1,58 @@
"""Incoming PCM to 16 kHz mono float32, and clip normalisation before transcription."""
from __future__ import annotations
import numpy as np
TARGET_RATE = 16000
_TAPS = 97 # windowed-sinc low-pass for the 48 kHz -> 16 kHz decimation
_CUTOFF_HZ = 7200 # below the new 8 kHz Nyquist
def _lowpass(rate: int) -> np.ndarray:
n = np.arange(_TAPS) - (_TAPS - 1) / 2
fc = _CUTOFF_HZ / rate
h = 2 * fc * np.sinc(2 * fc * n) * np.blackman(_TAPS)
return (h / h.sum()).astype(np.float64)
class InputConverter:
"""Streaming converter for one speaker. Keeps filter history and odd bytes between frames."""
def __init__(self, rate: int, channels: int):
if rate not in (16000, 48000):
raise ValueError("audio rate must be 16000 or 48000")
if channels not in (1, 2):
raise ValueError("audio channels must be 1 or 2")
self.channels = channels
self.factor = rate // TARGET_RATE
self._carry = b""
if self.factor > 1:
self._h = _lowpass(rate)
self._hist = np.zeros(_TAPS - 1, dtype=np.float64)
self._phase = 0 # input samples to skip before the next kept output
def push(self, pcm: bytes) -> np.ndarray:
data = self._carry + pcm
frame_bytes = 2 * self.channels
usable = len(data) - len(data) % frame_bytes
self._carry = data[usable:]
x = np.frombuffer(data[:usable], dtype="<i2").astype(np.float64) / 32768.0
if self.channels == 2:
x = x.reshape(-1, 2).mean(axis=1)
if self.factor == 1:
return x.astype(np.float32)
buf = np.concatenate([self._hist, x])
self._hist = buf[-(_TAPS - 1):]
y = np.convolve(buf, self._h, mode="valid") # one output per new input sample
out = y[self._phase::self.factor]
self._phase = (self._phase - len(y)) % self.factor
return out.astype(np.float32)
def peak_normalise(x: np.ndarray, target: float = 0.9, max_gain: float = 10.0) -> np.ndarray:
peak = float(np.max(np.abs(x))) if len(x) else 0.0
if peak <= 0:
return x.astype(np.float32)
gain = min(target / peak, max_gain)
return (x * gain).astype(np.float32)