Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/docgen/align.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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),
Expand Down
16 changes: 16 additions & 0 deletions tests/test_align.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"))
Loading