- 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>
216 lines
6.4 KiB
Python
216 lines
6.4 KiB
Python
"""Voice worker: per-speaker utterance segmentation (start, partial cadence, the four end reasons).
|
|
|
|
The fake VAD reads the speech probability straight off the audio: a window of constant amplitude
|
|
0.9 is "speech at 0.9". That keeps the tests about the segmentation rules, not about Silero.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import numpy as np
|
|
import pytest
|
|
|
|
from voice_worker.segmenter import Final, Partial, Segmenter, SpeechStart
|
|
from voice_worker.settings import SttSettings
|
|
|
|
SR = 16000
|
|
FRAME_MS = 20
|
|
|
|
|
|
class AmplitudeVad:
|
|
def prob(self, window: np.ndarray) -> float:
|
|
return float(np.max(np.abs(window)))
|
|
|
|
|
|
class Feeder:
|
|
def __init__(self, settings: SttSettings | None = None):
|
|
self.settings = settings or SttSettings()
|
|
self.seg = Segmenter(AmplitudeVad(), self.settings)
|
|
self.now = 100.0
|
|
self.events: list = []
|
|
|
|
def feed(self, level: float, ms: int) -> list:
|
|
got = []
|
|
for _ in range(ms // FRAME_MS):
|
|
self.now += FRAME_MS / 1000
|
|
got += self.seg.push(np.full(SR * FRAME_MS // 1000, level, dtype=np.float32), self.now)
|
|
self.events += got
|
|
return got
|
|
|
|
def of(self, cls) -> list:
|
|
return [e for e in self.events if isinstance(e, cls)]
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_silence_produces_nothing():
|
|
f = Feeder()
|
|
f.feed(0.0, 3000)
|
|
assert f.events == []
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_speech_then_silence_ends_with_one_final():
|
|
f = Feeder()
|
|
f.feed(0.0, 500)
|
|
f.feed(0.9, 1000)
|
|
f.feed(0.0, 500)
|
|
starts, finals = f.of(SpeechStart), f.of(Final)
|
|
assert [s.utt for s in starts] == [1]
|
|
assert len(finals) == 1
|
|
fin = finals[0]
|
|
assert fin.utt == 1 and fin.reason == "silence"
|
|
# Pre-roll (<= 200 ms) + 1 s speech + trailing silence capped at 200 ms.
|
|
assert 1.0 <= len(fin.audio) / SR <= 1.45
|
|
assert fin.audio.dtype == np.float32
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_final_waits_for_the_configured_silence():
|
|
f = Feeder(SttSettings(end_silence_ms=600))
|
|
f.feed(0.9, 800)
|
|
f.feed(0.0, 400)
|
|
assert f.of(Final) == []
|
|
f.feed(0.0, 300)
|
|
assert len(f.of(Final)) == 1
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_changed_settings_apply_to_the_running_utterance():
|
|
f = Feeder(SttSettings(end_silence_ms=1500))
|
|
f.feed(0.9, 800)
|
|
f.settings.update({"end_silence_ms": 200})
|
|
f.feed(0.0, 300)
|
|
assert [e.reason for e in f.of(Final)] == ["silence"]
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_a_short_blip_is_dropped_without_any_message():
|
|
f = Feeder()
|
|
f.feed(0.9, 120)
|
|
f.feed(0.0, 1000)
|
|
assert f.events == []
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_hysteresis_keeps_speech_going_but_never_starts_it():
|
|
f = Feeder()
|
|
f.feed(0.4, 1000)
|
|
assert f.events == []
|
|
f.feed(0.9, 400)
|
|
f.feed(0.4, 1000) # below the start threshold but above the keep threshold
|
|
assert f.of(Final) == []
|
|
f.feed(0.0, 400)
|
|
assert len(f.of(Final)) == 1
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_weak_speech_like_noise_never_builds_up_to_an_utterance():
|
|
"""A breath of real speech probability followed by a long stretch of 'maybe speech' (fans,
|
|
keyboard, background rumble that the VAD scores around 0.4) is not enough to call the model."""
|
|
f = Feeder()
|
|
for _ in range(3):
|
|
f.feed(0.9, 40)
|
|
f.feed(0.4, 1000)
|
|
f.feed(0.0, 500)
|
|
assert f.events == []
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_finals_mark_which_windows_were_voiced():
|
|
f = Feeder()
|
|
f.feed(0.0, 500)
|
|
f.feed(0.9, 640)
|
|
f.feed(0.0, 500)
|
|
fin = f.of(Final)[0]
|
|
assert len(fin.voiced) == -(-len(fin.audio) // 512)
|
|
assert 20 <= fin.voiced.sum() <= 21 # 640 ms of 32 ms windows, not aligned to the frames
|
|
assert not fin.voiced[0] and not fin.voiced[-1] # pre-roll and tail are not voiced
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_speaking_end_closes_the_utterance_at_once():
|
|
f = Feeder()
|
|
f.feed(0.9, 700)
|
|
events = f.seg.speaking_end(f.now)
|
|
assert [type(e) for e in events] == [Final]
|
|
assert events[0].reason == "speaking_end"
|
|
assert events[0].decided_at == f.now
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_speaking_end_after_a_blip_sends_nothing():
|
|
f = Feeder()
|
|
f.feed(0.9, 100)
|
|
assert f.seg.speaking_end(f.now) == []
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_audio_that_stops_arriving_ends_as_idle():
|
|
f = Feeder()
|
|
f.feed(0.9, 700)
|
|
assert f.seg.tick(f.now + 0.2) == []
|
|
events = f.seg.tick(f.now + 0.31)
|
|
assert [e.reason for e in events] == ["idle"]
|
|
assert f.seg.tick(f.now + 1.0) == []
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_long_speech_is_cut_at_the_whisper_window_and_continues_as_a_new_utterance():
|
|
f = Feeder()
|
|
f.feed(0.9, 29_000)
|
|
finals = f.of(Final)
|
|
assert [e.reason for e in finals] == ["max_length"]
|
|
assert len(finals[0].audio) / SR <= 28.0
|
|
assert [s.utt for s in f.of(SpeechStart)] == [1, 2]
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_partials_follow_the_interval_and_grow():
|
|
f = Feeder(SttSettings(partial_interval_ms=400))
|
|
f.feed(0.9, 2000)
|
|
partials = f.of(Partial)
|
|
assert 3 <= len(partials) <= 5
|
|
lengths = [len(p.audio) for p in partials]
|
|
assert lengths == sorted(lengths) and len(set(lengths)) == len(lengths)
|
|
assert all(p.utt == 1 for p in partials)
|
|
f.feed(0.0, 400)
|
|
n = len(f.of(Partial))
|
|
f.feed(0.0, 1000)
|
|
assert len(f.of(Partial)) == n # nothing after the final
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_no_partials_before_speech_start():
|
|
f = Feeder(SttSettings(partial_interval_ms=100))
|
|
f.feed(0.9, 200)
|
|
assert f.of(Partial) == []
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_utterance_numbers_count_up_per_speaker():
|
|
f = Feeder()
|
|
for _ in range(3):
|
|
f.feed(0.9, 500)
|
|
f.feed(0.0, 400)
|
|
assert [e.utt for e in f.of(Final)] == [1, 2, 3]
|
|
|
|
|
|
@pytest.mark.unit
|
|
@pytest.mark.parametrize("bad", [{"end_silence_ms": 50}, {"end_silence_ms": 5000}, {"final_beam": 0},
|
|
{"final_beam": 9}, {"partials": "yes"}, {"hotwords": "자비스"},
|
|
{"language": 3}, {"partial_interval_ms": 10}, {"nope": 1}])
|
|
def test_bad_settings_are_refused_and_change_nothing(bad):
|
|
s = SttSettings()
|
|
before = s.as_dict()
|
|
with pytest.raises(ValueError):
|
|
s.update(bad)
|
|
assert s.as_dict() == before
|
|
|
|
|
|
@pytest.mark.unit
|
|
def test_settings_accept_null_language_and_hotword_lists():
|
|
s = SttSettings()
|
|
s.update({"language": None, "hotwords": ["자비스", "Jarvis"], "partials": False})
|
|
assert s.as_dict()["language"] is None
|
|
assert s.hotwords == ("자비스", "Jarvis")
|
|
assert s.partials is False
|