Datasets:
Download tests/test_vad.py from mavleo96/turn-detection-data: direct link, hf CLI and curl.
- Browser
- Download file 3.17 kB
-
https://huggingface.co/datasets/mavleo96/turn-detection-data/resolve/main/tests/test_vad.py
- Command line
-
hf download hf://datasets/mavleo96/turn-detection-data/tests/test_vad.py
-
curl -L -o test_vad.py https://huggingface.co/datasets/mavleo96/turn-detection-data/resolve/main/tests/test_vad.py
3.17 kB
| import numpy as np | |
| import pytest | |
| from scripts.vad import VadConfig, gate, state_machine | |
| CFG = VadConfig(sr=16000) | |
| FS = CFG.frame / CFG.sr # 32 ms; 200 ms -> 7 frames | |
| QUIET = -10.0 - CFG.gate_db - 5 # 5 dB under the gate threshold for speech at -10 dB | |
| def seq(*parts): | |
| return np.concatenate([np.full(n, p) for p, n in parts]) | |
| def test_start_and_stop(): | |
| segs = state_machine(seq((0.0, 10), (0.9, 30), (0.1, 20)), CFG) | |
| assert len(segs) == 1 | |
| s = segs[0] | |
| assert s.start == pytest.approx(10 * FS) | |
| assert s.end == pytest.approx(40 * FS) | |
| assert s.fire == pytest.approx(47 * FS) | |
| def test_short_burst_does_not_start(): | |
| assert state_machine(seq((0.0, 10), (0.9, 6), (0.0, 20)), CFG) == [] | |
| def test_short_dip_does_not_stop(): | |
| segs = state_machine(seq((0.9, 20), (0.1, 6), (0.9, 20), (0.1, 10)), CFG) | |
| assert len(segs) == 1 and segs[0].end == pytest.approx(46 * FS) | |
| def test_hysteresis(): | |
| # 0.6 is below start threshold (no start) but above stop threshold (keeps speaking) | |
| assert state_machine(seq((0.6, 50)), CFG) == [] | |
| segs = state_machine(seq((0.9, 10), (0.6, 50), (0.1, 10)), CFG) | |
| assert len(segs) == 1 and segs[0].end == pytest.approx(60 * FS) | |
| def test_open_segment_at_end_is_not_emitted(): | |
| assert state_machine(seq((0.9, 20), (0.1, 3)), CFG) == [] | |
| def test_gate_zeroes_quiet_frames(): | |
| probs = seq((0.9, 20), (0.9, 20)) | |
| db = seq((-10.0, 20), (QUIET, 20)) # second half below the gate threshold | |
| gated = gate(probs, db, CFG) | |
| assert (gated[:20] == 0.9).all() and (gated[20:] == 0).all() | |
| def test_gate_inactive_before_any_speech_and_forgets_after_window(): | |
| window = round(CFG.level_window_s / FS) | |
| probs = seq((0.9, 10), (0.0, window), (0.9, 10)) | |
| db = seq((-10.0, 10), (-60.0, window), (-30.0, 10)) | |
| gated = gate(probs, db, CFG) | |
| assert (gated[-10:] == 0.9).all() # loud speech left the window, quieter speech passes | |
| assert gate(seq((0.3, 5)), seq((-80.0, 5)), CFG).tolist() == [0.3] * 5 | |
| def test_gate_bleed_after_long_own_speech_is_gated(): | |
| # 10 s loud own speech, then 15 s of quiet bleed that Silero is confident about | |
| own, bleed = round(10 / FS), round(15 / FS) | |
| probs = seq((0.95, own), (0.95, bleed)) | |
| db = seq((-10.0, own), (QUIET, bleed)) | |
| assert (gate(probs, db, CFG)[own:] == 0).all() | |
| def test_gate_single_click_does_not_suppress_speech(): | |
| speech, after = round(5 / FS), round(3 / FS) | |
| probs = seq((0.95, speech), (0.95, 1), (0.8, after)) | |
| db = seq((-20.0, speech), (10.0, 1), (-22.0, after)) # one +10 dB click, then normal speech | |
| assert (gate(probs, db, CFG)[speech + 1 :] == 0.8).all() | |
| def test_gate_is_causal(): | |
| rng = np.random.default_rng(0) | |
| probs, db = rng.random(3000), rng.normal(-30, 10, 3000) | |
| t = 1500 | |
| probs2, db2 = probs.copy(), db.copy() | |
| probs2[t + 1 :], db2[t + 1 :] = rng.random(1499), rng.normal(0, 20, 1499) | |
| assert (gate(probs, db, CFG)[: t + 1] == gate(probs2, db2, CFG)[: t + 1]).all() | |
| def test_frame_is_32_ms_at_both_rates(): | |
| assert VadConfig(sr=8000).frame == 256 and VadConfig(sr=16000).frame == 512 | |
| with pytest.raises(AssertionError): | |
| VadConfig(sr=22050) | |