Skip to content

Commit df2a6bc

Browse files
authored
Fix vad to return zero output for zero input (#3685)
When the input is zero Tensor, the result should be empty.
1 parent d4cf8d5 commit df2a6bc

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/torchaudio/functional/filtering.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,6 +1660,9 @@ def vad(
16601660
flushedLen_ns = (measures_len - num_measures_to_flush) * measure_period_ns
16611661
break
16621662
# end for window
1663+
if not has_triggered:
1664+
return waveform[..., :0].view(shape[:-1] + torch.Size([0]))
1665+
16631666
res = waveform[:, pos - samplesLen_ns + flushedLen_ns :]
16641667
# unpack batch
16651668
return res.view(shape[:-1] + res.shape[-1:])

test/torchaudio_unittest/transforms/transforms_test_impl.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,3 +478,18 @@ def test_specaugment(self, n_time_masks, time_mask_param, n_freq_masks, freq_mas
478478
self.assertTrue(diff > 0)
479479
else:
480480
self.assertTrue(diff == 0)
481+
482+
@parameterized.expand(
483+
[
484+
((32000,), (0,), 16000),
485+
((1, 32000), (1, 0), 32000),
486+
((2, 44100), (2, 0), 32000),
487+
((2, 2, 44100), (2, 2, 0), 32000),
488+
]
489+
)
490+
def test_vad_on_zero_audio(self, input_shape, output_shape, sample_rate: int):
491+
"""VAD should return zero when input is zero Tensor"""
492+
inpt = torch.zeros(input_shape, dtype=self.dtype, device=self.device)
493+
expected_output = torch.zeros(output_shape, dtype=self.dtype, device=self.device)
494+
result = T.Vad(sample_rate)(inpt)
495+
self.assertEqual(result, expected_output)

0 commit comments

Comments
 (0)