Skip to content

Commit

Permalink
MixedCut.truncate: fix the case when only PaddingCuts are left (#…
Browse files Browse the repository at this point in the history
…1157)

* Fix the case when only PaddingCuts are truncated

* Add regression test
  • Loading branch information
flyingleafe committed Oct 11, 2023
1 parent 9c80a1e commit afc8eff
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
5 changes: 3 additions & 2 deletions lhotse/cut/mixed.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,14 +497,15 @@ def truncate(
)
)

# Edge case: no tracks left after truncation. This can happen if we truncated an offset region.
# Edge case: no tracks with data left after truncation. This can happen if we truncated an offset region.
# In this case, return a PaddingCut of the requested duration
if len(new_tracks) == 0:
if len([t for t in new_tracks if not isinstance(t.cut, PaddingCut)]) == 0:
return PaddingCut(
id=self.id if preserve_id else str(uuid4()),
duration=duration,
sampling_rate=self.sampling_rate,
feat_value=0.0,
num_samples=compute_num_samples(duration, self.sampling_rate),
)

if len(new_tracks) == 1:
Expand Down
10 changes: 7 additions & 3 deletions test/cut/test_cut_truncate.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def gapped_mixed_cut():
id="gapped-mixed-cut",
tracks=[
MixTrack(cut=dummy_cut(0, duration=10.0)),
MixTrack(cut=dummy_cut(1, duration=10.0), offset=15.0),
MixTrack(cut=dummy_cut(1, duration=10.0).pad(12.0).pad(15.0), offset=15.0),
],
)

Expand Down Expand Up @@ -226,12 +226,16 @@ def test_truncate_mixed_cut_with_small_offset_and_duration(simple_mixed_cut):
assert truncated_cut.duration == 13.0


def test_truncate_mixed_cut_inside_gap(gapped_mixed_cut):
truncated_cut = gapped_mixed_cut.truncate(offset=11.0, duration=3.0)
@pytest.mark.parametrize("offset", [11.0, 26.0])
def test_truncate_mixed_cut_gap_or_padding(gapped_mixed_cut, offset):
print(gapped_mixed_cut.duration)
truncated_cut = gapped_mixed_cut.truncate(offset=offset, duration=3.0)
assert isinstance(truncated_cut, PaddingCut)
assert truncated_cut.start == 0.0
assert truncated_cut.duration == 3.0
assert truncated_cut.end == 3.0
audio = truncated_cut.load_audio()
assert audio is not None


def test_truncate_cut_set_offset_start(cut_set):
Expand Down

0 comments on commit afc8eff

Please sign in to comment.