From 10b4427640f3e6c1f4a1778ef0526bbc0691f586 Mon Sep 17 00:00:00 2001 From: Zakaria Zbitou Date: Sun, 23 Aug 2026 19:57:11 +0200 Subject: Clamp the padded chunk start and derive the crop offset from it --- inference.py | 12 ++++++------ tests/test_inference.py | 13 ++++++------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/inference.py b/inference.py index e50d476..356bda4 100644 --- a/inference.py +++ b/inference.py @@ -167,6 +167,7 @@ def run_model( batch_starts = starts[batch_start : batch_start + chunk_batch_size] batch_chunks = [] valid_lengths = [] + batch_padded_starts = [] for start in batch_starts: end = min(start + chunk_samples, total_samples) valid_samples = end - start @@ -174,7 +175,7 @@ def run_model( # the first padded chunk begins at the file start and the last # ends at the file end, extending further left instead — # padding with silence measurably degrades the output near it. - padded_start = start - chunk_pad_samples if start else 0 + padded_start = max(0, start - chunk_pad_samples) if chunk_pad_samples and end == total_samples: padded_start = max(0, total_samples - padded_chunk_samples) chunk = audio[..., padded_start : padded_start + padded_chunk_samples] @@ -182,6 +183,7 @@ def run_model( chunk = F.pad(chunk, (0, padded_chunk_samples - chunk.shape[-1])) batch_chunks.append(chunk) valid_lengths.append(valid_samples) + batch_padded_starts.append(padded_start) chunk_batch = torch.cat(batch_chunks, dim=0) batch_output = model(chunk_batch.to(device)).detach().to("cpu") @@ -195,13 +197,11 @@ def run_model( "Chunk output is shorter than the corresponding input segment." ) - for offset, (start, valid_samples) in enumerate( - zip(batch_starts, valid_lengths) + for offset, (start, valid_samples, padded_start) in enumerate( + zip(batch_starts, valid_lengths, batch_padded_starts) ): end = start + valid_samples - valid_start = chunk_pad_samples if start else 0 - if chunk_pad_samples and end == total_samples: - valid_start = start - max(0, total_samples - padded_chunk_samples) + valid_start = start - padded_start chunk_output = batch_output[ offset : offset + 1, ..., valid_start : valid_start + valid_samples ] diff --git a/tests/test_inference.py b/tests/test_inference.py index c6360b5..f6e1380 100644 --- a/tests/test_inference.py +++ b/tests/test_inference.py @@ -297,16 +297,15 @@ class InferenceTests(unittest.TestCase): self.assertFalse(np.isfinite(output).all()) def test_chunk_padding_wider_than_the_hop_reproduces_the_input(self): - """Regression: chunk padding wider than the hop silenced whole chunks. + """ + Regression test `padded_start` was `start - chunk_pad_samples` for every chunk after - the first, which goes negative once the padding exceeds the hop. A - negative slice start is read from the end of the audio, so the slice - came back empty and `F.pad` filled the whole chunk with zeros, which - the crossfades then blended into the output. + the first, which goes negative once the padding exceeds the hop, and + makes slicing index from the end of the file - Reproducing it needs padding wider than the hop and a chunk after the - first, so the file has to hold at least two chunks: 0.5 s chunks with + The test reproduces the bug, it needs padding wider than the hop, + so the file has to hold at least two chunks: 0.5 s chunks with a 0.1 s overlap leave a 0.4 s hop, and 0.5 s of padding exceeds it. The second chunk starts at 0.4 s, giving `padded_start = -0.1 s`. """ -- cgit v1.2.3