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

@@ -5,7 +5,7 @@ from __future__ import annotations
import numpy as np
import pytest
from voice_worker.audio import InputConverter, peak_normalise
from voice_worker.audio import InputConverter, loudness_equalise
from voice_worker.protocol import (KIND_AUDIO, KIND_TTS, ProtocolError, decode_audio_frame, encode_audio_frame,
encode_tts_frame, decode_tts_frame)
@@ -104,19 +104,70 @@ def test_odd_byte_counts_are_carried_to_the_next_frame():
assert len(a) + len(b) == 10
def _db(x: np.ndarray) -> float:
return 20 * np.log10(np.sqrt(np.mean(np.asarray(x, dtype=np.float64) ** 2)) + 1e-12)
def _voice(seconds: float, db: float) -> np.ndarray:
"""A stand-in for a voice: a 220 Hz tone with harmonics, at ``db`` dBFS RMS."""
t = np.arange(int(16000 * seconds)) / 16000
x = sum(np.sin(2 * np.pi * 220 * k * t) / k for k in (1, 2, 3, 4))
return (x * 10 ** (db / 20) / np.sqrt(np.mean(x ** 2))).astype(np.float32)
def _mask(n_samples: int, voiced: slice) -> np.ndarray:
m = np.zeros(-(-n_samples // 512), dtype=bool)
m[voiced] = True
return m
@pytest.mark.unit
def test_quiet_clip_is_raised_to_the_target_peak():
x = np.full(100, 0.2, dtype=np.float32)
assert np.isclose(np.max(np.abs(peak_normalise(x))), 0.9)
@pytest.mark.parametrize("db", [-60, -45, -30, -10, -3])
def test_every_speaker_level_comes_out_at_the_same_loudness(db):
x = _voice(2.0, db)
y = loudness_equalise(x)
assert y.dtype == np.float32 and len(y) == len(x)
assert abs(_db(y) - (-20)) < 1.0
@pytest.mark.unit
def test_loudness_is_measured_on_the_voice_not_the_silence_around_it():
x = np.concatenate([np.zeros(16000, dtype=np.float32), _voice(1.0, -40)])
y = loudness_equalise(x, _mask(len(x), slice(32, None)))
assert abs(_db(y[16000:]) - (-20)) < 1.0
@pytest.mark.unit
def test_a_loud_click_neither_keeps_a_quiet_voice_quiet_nor_clips():
x = _voice(2.0, -50)
burst = np.random.default_rng(3).normal(0, 1, 200) * np.exp(-np.arange(200) / 30)
x[8000:8200] += (0.95 * burst / np.max(np.abs(burst))).astype(np.float32) # a mouse click, far louder
voiced = _mask(len(x), slice(None))
voiced[8000 // 512] = False
y = loudness_equalise(x, voiced)
assert abs(_db(y[:7000]) - (-20)) < 1.0
assert np.max(np.abs(y)) < 1.0
@pytest.mark.unit
def test_clipped_shouting_is_brought_down_without_new_clipping():
x = np.clip(_voice(2.0, -3) * 4, -1, 1)
y = loudness_equalise(x)
assert abs(_db(y) - (-20)) < 1.5
assert np.max(np.abs(y)) < 1.0
@pytest.mark.unit
def test_near_silence_is_not_boosted_past_the_gain_cap():
x = np.full(100, 0.001, dtype=np.float32)
assert np.isclose(np.max(np.abs(peak_normalise(x))), 0.01)
x = _voice(1.0, -90)
assert abs(_db(loudness_equalise(x)) - (-90 + 40)) < 1.0
@pytest.mark.unit
def test_loud_clip_is_brought_down_to_the_target_peak():
x = np.array([0.0, 1.0, -0.5], dtype=np.float32)
assert np.isclose(np.max(np.abs(peak_normalise(x))), 0.9)
def test_low_rumble_is_removed_before_levelling():
t = np.arange(32000) / 16000
rumble = (0.3 * np.sin(2 * np.pi * 30 * t)).astype(np.float32)
y = loudness_equalise(_voice(2.0, -40) + rumble)
spec = np.abs(np.fft.rfft(y))
freqs = np.fft.rfftfreq(len(y), 1 / 16000)
assert spec[np.argmin(np.abs(freqs - 30))] < 0.01 * spec[np.argmin(np.abs(freqs - 220))]