[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

+3 -3
View File
@@ -2,7 +2,7 @@ import numpy as np
async def extract_audio_from_video_async(video_url: str) -> tuple[np.ndarray, int | float]:
"""Extract audio from a video URL using librosa.
"""Extract audio from a video URL using vllm's load_audio.
Returns a (audio_array, sample_rate) tuple compatible with audio format.
All blocking I/O operations are run in a thread pool.
@@ -26,9 +26,9 @@ async def extract_audio_from_video_async(video_url: str) -> tuple[np.ndarray, in
return temp_file.name
def _load_audio_sync(file_path: str) -> tuple[np.ndarray, int | float]:
import librosa
from vllm.multimodal.media.audio import load_audio
return librosa.load(file_path, sr=16000)
return load_audio(file_path, sr=16000)
def _cleanup_file_sync(file_path: str) -> None:
try:
@@ -1,6 +1,8 @@
from io import BytesIO
import numpy as np
import torch
import torchaudio
from vllm.logger import init_logger
from vllm_omni.entrypoints.openai.protocol.audio import AudioResponse, CreateAudio
@@ -10,11 +12,6 @@ try:
except ImportError:
soundfile = None
try:
import librosa
except ImportError:
librosa = None
logger = init_logger(__name__)
@@ -74,20 +71,53 @@ class AudioMixin:
return AudioResponse(audio_data=audio_data, media_type=media_type)
def _apply_speed_adjustment(self, audio_tensor: np.ndarray, speed: float, sample_rate: int):
"""Apply speed adjustment to the audio tensor while preserving pitch."""
"""Apply speed adjustment to the audio tensor while preserving pitch.
Uses torchaudio's phase vocoder (Spectrogram → TimeStretch →
InverseSpectrogram) to stretch/compress audio in time without
changing pitch.
"""
if speed == 1.0:
return audio_tensor, sample_rate
if librosa is None:
raise ImportError("librosa is required for speed adjustment. Please install it with: pip install librosa")
try:
# librosa.effects.time_stretch requires a float audio tensor.
if not np.issubdtype(audio_tensor.dtype, np.floating):
audio_tensor = audio_tensor.astype(np.float32)
stretched_audio = librosa.effects.time_stretch(y=audio_tensor, rate=speed)
return stretched_audio, sample_rate
# Stereo numpy arrays use channels-last (T, C);
# torch expects channels-first (C, T).
channels_last = audio_tensor.ndim == 2
if channels_last:
waveform = torch.from_numpy(audio_tensor.T)
else:
waveform = torch.from_numpy(audio_tensor).unsqueeze(0)
# Match librosa.stft defaults: n_fft=2048, hop_length=n_fft//4
n_fft = 2048
hop_length = n_fft // 4
to_spec = torchaudio.transforms.Spectrogram(
n_fft=n_fft,
hop_length=hop_length,
power=None,
)
stretch = torchaudio.transforms.TimeStretch(
n_freq=n_fft // 2 + 1,
hop_length=hop_length,
)
to_wave = torchaudio.transforms.InverseSpectrogram(
n_fft=n_fft,
hop_length=hop_length,
)
spec = to_spec(waveform)
stretched = stretch(spec, speed)
expected_length = int(audio_tensor.shape[0] / speed)
result = to_wave(stretched, length=expected_length)
result = result.squeeze(0).numpy()
if channels_last:
result = result.T
return result, sample_rate
except Exception as e:
logger.error(f"An error occurred during speed adjustment: {e}")
raise ValueError("Failed to apply speed adjustment.") from e