[Refactor] Remove dependency on librosa (#2273)
Signed-off-by: Nick Cao <ncao@redhat.com> Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
687405c5f2
commit
2bc183f6f0
46 files changed
+229
-323
No files matched your search
@@ -63,14 +63,11 @@ class TestAudioMixin:
|
||||
adjusted_tensor = mock_speed.call_args[0][0]
|
||||
assert len(adjusted_tensor) == 24000
|
||||
|
||||
def test_speed_adjustment(self, audio_mixin, mocker: MockerFixture):
|
||||
mock_time_stretch = mocker.patch("librosa.effects.time_stretch")
|
||||
mock_time_stretch.return_value = np.zeros(12000)
|
||||
def test_speed_adjustment(self, audio_mixin):
|
||||
audio_tensor = np.random.rand(24000).astype(np.float32)
|
||||
|
||||
adjusted_audio, _ = audio_mixin._apply_speed_adjustment(audio_tensor, speed=2.0, sample_rate=24000)
|
||||
|
||||
mock_time_stretch.assert_called_with(y=audio_tensor, rate=2.0)
|
||||
assert adjusted_audio.shape == (12000,)
|
||||
|
||||
def test_unsupported_format_fallback(self, audio_mixin, caplog, mocker: MockerFixture):
|
||||
@@ -117,30 +114,22 @@ class TestAudioMixin:
|
||||
assert np.array_equal(output_tensor, stereo_tensor)
|
||||
|
||||
def test_speed_adjustment_bypass(self, audio_mixin, mocker: MockerFixture):
|
||||
"""Test that speed=1.0 bypasses the expensive librosa time stretching."""
|
||||
"""Test that speed=1.0 bypasses the expensive torchaudio time stretching."""
|
||||
audio_tensor = np.random.rand(24000).astype(np.float32)
|
||||
|
||||
mock_time_stretch = mocker.patch("librosa.effects.time_stretch")
|
||||
# speed=1.0 should return immediately without calling librosa
|
||||
mock_time_stretch = mocker.patch("torchaudio.transforms.TimeStretch")
|
||||
# speed=1.0 should return immediately without calling torchaudio
|
||||
result, _ = audio_mixin._apply_speed_adjustment(audio_tensor, speed=1.0, sample_rate=24000)
|
||||
|
||||
mock_time_stretch.assert_not_called()
|
||||
assert np.array_equal(result, audio_tensor)
|
||||
|
||||
def test_speed_adjustment_stereo_handling(self, audio_mixin, mocker: MockerFixture):
|
||||
"""Test that speed adjustment is attempted on stereo inputs."""
|
||||
mock_time_stretch = mocker.patch("librosa.effects.time_stretch")
|
||||
def test_speed_adjustment_stereo_handling(self, audio_mixin):
|
||||
"""Test that speed adjustment handles stereo (channels-last) input."""
|
||||
stereo_tensor = np.random.rand(24000, 2).astype(np.float32)
|
||||
# Mock return value representing a sped-up version (half length)
|
||||
mock_time_stretch.return_value = np.zeros((12000, 2), dtype=np.float32)
|
||||
|
||||
result, _ = audio_mixin._apply_speed_adjustment(stereo_tensor, speed=2.0, sample_rate=24000)
|
||||
|
||||
mock_time_stretch.assert_called_once()
|
||||
# Ensure the stereo tensor was passed to librosa
|
||||
call_args = mock_time_stretch.call_args
|
||||
assert np.array_equal(call_args.kwargs["y"], stereo_tensor)
|
||||
assert call_args.kwargs["rate"] == 2.0
|
||||
assert result.shape == (12000, 2)
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user