diff --git a/src/unmix/pipeline/s4_pitch.py b/src/unmix/pipeline/s4_pitch.py index af55216..38aecb5 100644 --- a/src/unmix/pipeline/s4_pitch.py +++ b/src/unmix/pipeline/s4_pitch.py @@ -14,6 +14,53 @@ from unmix.pipeline.runner import PipelineContext, Stage from unmix.schema.base import GeneratedBy from unmix.schema.vocals import VocalLine, VocalsDocument, VocalSyllable +# Line ends: follow strictly voiced frames (CREPE periodicity) after the last word. +# Short dropouts are always bridged; longer ones (up to the max) only if voicing resumes for a while, +# i.e. a held note with a dropout rather than a breath or consonant. +LINE_END_VOICING_THRESHOLD = 0.7 +LINE_END_MAX_GAP_SEC = 0.1 +LINE_END_RESUME_MAX_GAP_SEC = 0.3 +LINE_END_RESUME_MIN_RUN_SEC = 0.3 + + +def voiced_run_end( + is_voiced: np.ndarray, + t0: float, + hop_sec: float, + max_gap_sec: float, + cap_t: float, + resume_max_gap_sec: float = 0.0, + resume_min_run_sec: float = 0.0, +) -> float: + """End time of the voiced run starting at ``t0``. + + Unvoiced gaps up to ``max_gap_sec`` are always bridged. Gaps up to ``resume_max_gap_sec`` are + bridged if the voiced run that follows lasts at least ``resume_min_run_sec``. + """ + n = len(is_voiced) + k = int(t0 / hop_sec) if hop_sec > 0 else 0 + last = k + while k < n and k * hop_sec < cap_t: + if is_voiced[k]: + last = k + k += 1 + continue + j = k + while j < n and not is_voiced[j]: + j += 1 + if j >= n or j * hop_sec >= cap_t: + break + gap = (j - k) * hop_sec + r = j + while r < n and is_voiced[r]: + r += 1 + run = (r - j) * hop_sec + if gap <= max_gap_sec or (gap <= resume_max_gap_sec and run >= resume_min_run_sec): + k = j + else: + break + return (last + 1) * hop_sec + def hz_to_midi(hz: np.ndarray) -> np.ndarray: """Convert frequency in Hz to fractional MIDI note numbers.""" @@ -159,7 +206,7 @@ class PitchStage(Stage): """Stage 4 extracts vocal fundamental frequency and assigns median pitch per syllable.""" name = "s4_pitch" - version = "1.1.0" + version = "1.3.0" model_name = "torchcrepe-full" model_version = "1.0.0" dependencies = ["s3_lyrics"] @@ -274,10 +321,18 @@ class PitchStage(Stage): line_counter = 0 syl_counter = 0 - for line_dict in lyrics_data.get("lines", []): + all_lines = lyrics_data.get("lines", []) + is_voiced_strict = (periodicity >= LINE_END_VOICING_THRESHOLD) & (pitch_hz > 50) + + for line_pos, line_dict in enumerate(all_lines): sylls = line_dict.get("syllables", []) if not sylls: continue + next_line_start = None + for later in all_lines[line_pos + 1 :]: + if later.get("syllables"): + next_line_start = float(later["syllables"][0]["start"]) + break line_counter += 1 line_syllables_raw = [] @@ -322,6 +377,20 @@ class PitchStage(Stage): if extended_end > real_end: real_end = min(next_start_limit, extended_end) + if idx == len(sylls) - 1: + # Line end: end of the strictly voiced run after the last word (holds sustained vowels) + cap_t = next_line_start - 0.05 if next_line_start is not None else s_end + 3.0 + strict_end = voiced_run_end( + is_voiced_strict, + s_start, + hop_sec, + LINE_END_MAX_GAP_SEC, + cap_t, + resume_max_gap_sec=LINE_END_RESUME_MAX_GAP_SEC, + resume_min_run_sec=LINE_END_RESUME_MIN_RUN_SEC, + ) + real_end = max(snapped_start + 0.08, min(strict_end, cap_t)) + segs = segment_melisma( snapped_start, real_end, diff --git a/tests/test_pitch.py b/tests/test_pitch.py index 55ec083..8c55879 100644 --- a/tests/test_pitch.py +++ b/tests/test_pitch.py @@ -39,3 +39,37 @@ def test_no_correction_for_legitimate_contour(): ] corrected = correct_octave_errors(syllables) assert corrected[1]["midi"] == 64.0 + + +def test_voiced_run_end_bridges_short_gaps(): + import numpy as np + + from unmix.pipeline.s4_pitch import voiced_run_end + + hop = 0.01 + v = np.zeros(300, dtype=bool) + v[100:150] = True # 1.00-1.50 s + v[155:200] = True # gap of 0.05 s, then voiced until 2.00 s + v[260:290] = True # separate run after a 0.6 s gap + + assert voiced_run_end(v, 1.0, hop, max_gap_sec=0.1, cap_t=5.0) == pytest.approx(2.0) + assert voiced_run_end(v, 1.0, hop, max_gap_sec=0.0, cap_t=5.0) == pytest.approx(1.5) + # cap stops the run + assert voiced_run_end(v, 1.0, hop, max_gap_sec=0.1, cap_t=1.3) <= 1.31 + + +def test_voiced_run_end_bridges_longer_gap_only_if_voicing_resumes(): + import numpy as np + + from unmix.pipeline.s4_pitch import voiced_run_end + + hop = 0.01 + v = np.zeros(400, dtype=bool) + v[100:150] = True # 1.00-1.50 s + v[170:230] = True # gap 0.20 s, then a long run (0.60 s) until 2.30 s + v[250:260] = True # gap 0.20 s, then a short run (0.10 s) + + kwargs = {"resume_max_gap_sec": 0.3, "resume_min_run_sec": 0.3} + assert voiced_run_end(v, 1.0, hop, 0.1, 5.0, **kwargs) == pytest.approx(2.3) + # without the resume rule the 0.2 s gap ends the run + assert voiced_run_end(v, 1.0, hop, 0.1, 5.0) == pytest.approx(1.5)