Skip to content

Commit

Permalink
Fix vad return zero output when nonzero pre_trigger_time is requested
Browse files Browse the repository at this point in the history
Differential Revision: D67532573

Pull Request resolved: #3866
  • Loading branch information
RoyJames authored Dec 20, 2024
1 parent a6b0a14 commit b6d4675
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/torchaudio/functional/filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -1662,8 +1662,8 @@ 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]))
if not has_triggered and shape[-1] >= fixed_pre_trigger_len_ns:
return waveform[..., :fixed_pre_trigger_len_ns].view(shape[:-1] + torch.Size([fixed_pre_trigger_len_ns]))

res = waveform[:, max(pos - samplesLen_ns + flushedLen_ns, 0) :]
# unpack batch
Expand Down
19 changes: 12 additions & 7 deletions test/torchaudio_unittest/transforms/transforms_test_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,15 +481,20 @@ def test_specaugment(self, n_time_masks, time_mask_param, n_freq_masks, freq_mas

@parameterized.expand(
[
((32000,), (0,), 16000),
((1, 32000), (1, 0), 32000),
((2, 44100), (2, 0), 32000),
((2, 2, 44100), (2, 2, 0), 32000),
((32000,), (0,), 16000, 0.0),
((1, 32000), (1, 0), 32000, 0.0),
((2, 44100), (2, 0), 32000, 0.0),
((2, 2, 44100), (2, 2, 0), 32000, 0.0),
((32000,), (16000,), 16000, 1.0),
((32000,), (32000,), 16000, 4.0),
((1, 32000), (1, 32000), 32000, 1.0),
((2, 44100), (2, 32000), 32000, 1.0),
((2, 2, 44100), (2, 2, 32000), 32000, 1.0),
]
)
def test_vad_on_zero_audio(self, input_shape, output_shape, sample_rate: int):
"""VAD should return zero when input is zero Tensor"""
def test_vad_on_zero_audio(self, input_shape, output_shape, sample_rate: int, pre_trigger_time: float):
"""VAD should return zero when input is zero Tensor when pre_trigger_time=0"""
inpt = torch.zeros(input_shape, dtype=self.dtype, device=self.device)
expected_output = torch.zeros(output_shape, dtype=self.dtype, device=self.device)
result = T.Vad(sample_rate)(inpt)
result = T.Vad(sample_rate, pre_trigger_time=pre_trigger_time)(inpt)
self.assertEqual(result, expected_output)

0 comments on commit b6d4675

Please sign in to comment.