Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MixedCut.truncate: fix the case when only PaddingCuts are left #1157

Merged
merged 2 commits into from
Oct 11, 2023
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
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
Loading