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

Fix vad to return zero output for zero input #3685

Merged
merged 2 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions src/torchaudio/functional/filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -1660,6 +1660,9 @@ def vad(
flushedLen_ns = (measures_len - num_measures_to_flush) * measure_period_ns
break
# end for window
if not has_triggered:
return waveform[..., :0].view(shape[:-1] + torch.Size([0]))

res = waveform[:, pos - samplesLen_ns + flushedLen_ns :]
# unpack batch
return res.view(shape[:-1] + res.shape[-1:])
12 changes: 12 additions & 0 deletions test/torchaudio_unittest/transforms/sox_compatibility_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ def test_vad(self, filename):
result = T.Vad(sample_rate)(data)
self.assert_sox_effect(result, path, ["vad"])

@parameterized.expand(
[
(torch.zeros(32000), torch.zeros(0), 16000),
(torch.zeros(1, 32000), torch.zeros(1, 0), 32000),
(torch.zeros(2, 44100), torch.zeros(2, 0), 32000),
(torch.zeros(2, 2, 44100), torch.zeros(2, 2, 0), 32000),
]
)
def test_vad_on_zero_audio(self, inpt: torch.Tensor, expected_output: torch.Tensor, sample_rate: int):
result = T.Vad(sample_rate)(inpt)
self.assertEqual(result, expected_output)

def test_vad_warning(self):
"""vad should throw a warning if input dimension is greater than 2"""
sample_rate = 41100
Expand Down