fix(voice_worker): even out speaker loudness and stop transcribing noise into words

- Loudness equalisation replaces peak normalisation: rumble below 60 Hz removed, level measured on
  the VAD-voiced windows only, brought to -20 dBFS (gain -20..+40 dB), soft limiter below 0.98.
- Only clearly voiced windows (VAD >= 0.5) count towards the 250 ms needed before an utterance
  reaches the model, so noise the VAD half-believes never calls Whisper.
- Hallucination gate: mean token log-probability >= -0.7 (the old len/(len+1) correction let short
  hallucinations through), compression ratio <= 2.4; no_speech_prob dropped (always 0.0 on turbo).
- With hotwords, the plain decode of the same encoder pass decides; the hint may fix a wake word's
  spelling but cannot add or drop one (it wrote the wake word on laughter and after real sentences).
- Opt-in real-model evals: voice-like babble with the hint, speech over babble, quiet and clipped
  speakers, and a JARVIS_NOISE_DIR folder of real noise recordings.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-11 10:27:38 +09:00
parent a7685e9078
commit 590cd07268
11 changed files with 460 additions and 54 deletions

View File

@@ -50,9 +50,39 @@ class InputConverter:
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)
TARGET_DB = -20.0 # loudness every speaker is brought to, dBFS RMS over the voiced windows
MAX_GAIN_DB = 40.0 # a whisper at -60 dBFS still reaches the target; pure hiss is not blown up
MIN_GAIN_DB = -20.0
HIGHPASS_HZ = 60.0 # mains hum and desk rumble carry no speech
_KNEE = 0.8 # soft limiter: peaks above this bend smoothly towards the ceiling instead of clipping
_CEILING = 0.98
_WINDOW = 512
def loudness_equalise(x: np.ndarray, voiced: np.ndarray | None = None) -> np.ndarray:
"""Bring a clip to the same loudness whoever spoke it, without clipping.
``voiced`` marks the 512-sample windows the VAD heard as speech; the level is measured on them so
silence, a click or a knock does not decide the gain. Without it the louder half of the windows is
used. Low rumble is removed first and peaks are soft-limited after the gain.
"""
x = np.asarray(x, dtype=np.float32)
if len(x) == 0:
return x
spec = np.fft.rfft(x.astype(np.float64))
spec[np.fft.rfftfreq(len(x), 1 / TARGET_RATE) < HIGHPASS_HZ] = 0
y = np.fft.irfft(spec, len(x))
n_win = -(-len(y) // _WINDOW)
frames = np.pad(y, (0, n_win * _WINDOW - len(y))).reshape(n_win, _WINDOW)
energy = (frames ** 2).mean(axis=1)
if voiced is not None and len(voiced) == n_win and voiced.any():
sel = energy[voiced]
else:
sel = energy[energy >= np.median(energy)]
level_db = 10 * np.log10(sel.mean() + 1e-20)
gain_db = float(np.clip(TARGET_DB - level_db, MIN_GAIN_DB, MAX_GAIN_DB))
y = y * 10 ** (gain_db / 20)
over = np.abs(y) > _KNEE
room = _CEILING - _KNEE
y[over] = np.sign(y[over]) * (_KNEE + room * np.tanh((np.abs(y[over]) - _KNEE) / room))
return y.astype(np.float32)