diff --git a/src/docgen/align.py b/src/docgen/align.py index 48be636..79b434c 100644 --- a/src/docgen/align.py +++ b/src/docgen/align.py @@ -129,6 +129,19 @@ def probe_duration(audio_path: Path) -> float: return _finite_probe_seconds(audio_path, out.stdout.strip()) +def _require_silence_settings(noise_db: float, min_silence_sec: float) -> None: + """Silence settings must be finite, and the minimum gap must be positive.""" + if not math.isfinite(noise_db): + raise AlignmentError( + f"silence noise must be a finite number of dB, not {noise_db!r}" + ) + if not math.isfinite(min_silence_sec) or min_silence_sec <= 0: + raise AlignmentError( + "min silence must be a finite number of seconds greater than zero, " + f"not {min_silence_sec!r}" + ) + + def detect_speech_intervals( audio_path: Path, duration: float, @@ -137,6 +150,7 @@ def detect_speech_intervals( min_silence_sec: float = DEFAULT_MIN_SILENCE_SEC, ) -> list[tuple[float, float]]: """Run ffmpeg silencedetect and return speech (non-silent) intervals.""" + _require_silence_settings(noise_db, min_silence_sec) cmd = [ "ffmpeg", "-hide_banner", "-nostats", "-i", str(audio_path), diff --git a/tests/test_align.py b/tests/test_align.py index 648c7f9..ef48d4f 100644 --- a/tests/test_align.py +++ b/tests/test_align.py @@ -5,7 +5,9 @@ import pytest from docgen.align import ( + AlignmentError, build_local_timing, + detect_speech_intervals, parse_silencedetect_output, reconcile_intervals_to_sentences, split_sentences, @@ -191,3 +193,17 @@ class _Proc: monkeypatch.setattr("docgen.align.subprocess.run", lambda *_a, **_k: _Proc()) assert detect_speech_intervals(Path("/tmp/x.mp3"), 10.0) == [(0.0, 10.0)] + + +def test_silence_settings_must_be_finite(monkeypatch) -> None: + from pathlib import Path + + def _boom(*_a, **_k): + raise AssertionError("ffmpeg should not run") + + monkeypatch.setattr("docgen.align.subprocess.run", _boom) + audio = Path("/tmp/x.mp3") + with pytest.raises(AlignmentError, match="min silence"): + detect_speech_intervals(audio, 10.0, min_silence_sec=float("inf")) + with pytest.raises(AlignmentError, match="silence noise"): + detect_speech_intervals(audio, 10.0, noise_db=float("nan"))