26 lines
628 B
Python
26 lines
628 B
Python
import torch
|
|
import torchaudio
|
|
|
|
|
|
def load_wav(wav_path: str, sample_rate: int):
|
|
"""Load wav file and resample to target sample rate.
|
|
|
|
Args:
|
|
wav_path (str): Path to wav file.
|
|
sample_rate (int): Target sample rate.
|
|
|
|
Returns:
|
|
torch.Tensor: Waveform tensor with shape (1, T).
|
|
"""
|
|
waveform, sr = torchaudio.load(wav_path)
|
|
|
|
if sr != sample_rate:
|
|
waveform = torchaudio.functional.resample(waveform, sr, sample_rate)
|
|
|
|
if len(waveform.shape) > 1 and waveform.shape[0] > 1:
|
|
waveform = torch.mean(waveform, dim=0, keepdim=True)
|
|
|
|
return waveform
|
|
|
|
|