From d116a8cd9442c4c273aba835cb1115dc8a38326f Mon Sep 17 00:00:00 2001 From: Xinyuan Tong <115166877+JustinTong0323@users.noreply.github.com> Date: Mon, 9 Mar 2026 11:24:20 +0000 Subject: [PATCH] [Bugfix] Fix load_audio: mono before resample + use torchaudio (#20054) --- python/sglang/srt/utils/common.py | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/python/sglang/srt/utils/common.py b/python/sglang/srt/utils/common.py index 6bba0ae3a..7bbecb3df 100644 --- a/python/sglang/srt/utils/common.py +++ b/python/sglang/srt/utils/common.py @@ -848,7 +848,8 @@ def load_audio( # Use soundfile here, since librosa use it under the hood, # and librosa will not support audio loading in the future import soundfile as sf - from scipy.signal import resample + import torch + import torchaudio if sr is None: sr = 16000 @@ -875,15 +876,27 @@ def load_audio( else: raise ValueError(f"Invalid audio format: {audio_file}") - # Resample audio if the original sample rate is different from the desired sample rate - if original_sr != sr: - num_samples = int(len(audio) * float(sr) / original_sr) - audio = resample(audio, num_samples) - - # Convert to mono if requested and audio is stereo + # Convert to mono first (before resampling) to reduce computation if mono and len(audio.shape) > 1: audio = np.mean(audio, axis=1) + # Resample audio if the original sample rate is different from the desired sample rate + if original_sr != sr: + audio_tensor = torch.from_numpy(audio).float() + if audio_tensor.dim() == 1: + audio_tensor = audio_tensor.unsqueeze(0) + else: + audio_tensor = audio_tensor.T # (time, channel) -> (channel, time) + + audio_tensor = torchaudio.functional.resample( + audio_tensor, orig_freq=original_sr, new_freq=sr + ) + + if audio_tensor.shape[0] == 1: + audio = audio_tensor.squeeze(0).numpy() + else: + audio = audio_tensor.T.numpy() # (channel, time) -> (time, channel) + return audio