29 lines
827 B
Python
29 lines
827 B
Python
"""Patch songrate's load_audio to use soundfile instead of torchaudio"""
|
|
import soundfile as sf
|
|
import numpy as np
|
|
import torch
|
|
|
|
def load_audio_patched(filepath, sr=22050):
|
|
"""使用 soundfile 加载音频,避免 torchcodec 依赖"""
|
|
# 读取音频
|
|
data, orig_sr = sf.read(filepath)
|
|
|
|
# 转单声道
|
|
if len(data.shape) > 1:
|
|
data = np.mean(data, axis=1)
|
|
|
|
# 重采样 (使用 librosa)
|
|
if orig_sr != sr:
|
|
import librosa
|
|
data = librosa.resample(data, orig_sr=orig_sr, target_sr=sr)
|
|
|
|
# 转 tensor 并移到 GPU
|
|
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
|
waveform = torch.from_numpy(data).float().to(device)
|
|
|
|
return waveform, sr
|
|
|
|
# Monkey patch
|
|
import songrate.analyzers
|
|
songrate.analyzers.load_audio = load_audio_patched
|