[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:
Nick CaoandClaude authored and GitHub committed 2026-04-11 02:11:31 +08:00
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)
+61
View File
@@ -0,0 +1,61 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""Unit tests for vllm_omni.utils.audio.mel_filter_bank."""
import pytest
import torch
from vllm_omni.utils.audio import mel_filter_bank
# Parameter combinations used across the codebase.
_PARAM_SETS = [
# Qwen3-TTS talker / speaker encoder (sr=24000)
dict(sr=24000, n_fft=1024, n_mels=128, fmin=0, fmax=12000),
# CosyVoice3 whisper encoder, Qwen3-TTS 25Hz tokenizer (sr=16000, 80 mels)
dict(sr=16000, n_fft=400, n_mels=80),
# CosyVoice3 whisper encoder (sr=16000, 128 mels)
dict(sr=16000, n_fft=400, n_mels=128),
]
_parametrize_params = pytest.mark.parametrize(
"params", _PARAM_SETS, ids=lambda p: f"{p['sr']}_{p['n_fft']}_{p['n_mels']}"
)
class TestMelFilterBank:
@_parametrize_params
def test_output_shape(self, params):
fb = mel_filter_bank(**params)
n_freqs = params["n_fft"] // 2 + 1
assert fb.shape == (params["n_mels"], n_freqs)
@_parametrize_params
def test_non_negative(self, params):
fb = mel_filter_bank(**params)
assert (fb >= 0).all()
def test_dtype_is_float(self):
fb = mel_filter_bank(sr=16000, n_fft=400, n_mels=80)
assert fb.dtype == torch.float32
def test_fmax_defaults_to_nyquist(self):
"""When fmax is omitted it should equal sr / 2."""
fb_default = mel_filter_bank(sr=16000, n_fft=400, n_mels=80)
fb_explicit = mel_filter_bank(sr=16000, n_fft=400, n_mels=80, fmax=8000.0)
torch.testing.assert_close(fb_default, fb_explicit)
def test_each_mel_band_has_nonzero_energy(self):
"""Every mel band should have at least one nonzero frequency bin."""
fb = mel_filter_bank(sr=24000, n_fft=1024, n_mels=128, fmin=0, fmax=12000)
for i in range(fb.shape[0]):
assert fb[i].sum() > 0, f"mel band {i} is all zeros"
def test_higher_fmax_extends_coverage(self):
"""A higher fmax should produce nonzero weights at higher frequency bins."""
fb_low = mel_filter_bank(sr=24000, n_fft=1024, n_mels=128, fmin=0, fmax=6000)
fb_high = mel_filter_bank(sr=24000, n_fft=1024, n_mels=128, fmin=0, fmax=12000)
# The highest nonzero column should be larger for fb_high.
last_nonzero_low = (fb_low.sum(dim=0) > 0).nonzero()[-1].item()
last_nonzero_high = (fb_high.sum(dim=0) > 0).nonzero()[-1].item()
assert last_nonzero_high > last_nonzero_low