Spaces:
Running
on
Zero
Running
on
Zero
File size: 1,045 Bytes
37ced70 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
import os
from pathlib import Path
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchaudio
def load_audio(audiopath, sampling_rate):
"""_summary_
Args:
audiopath (_type_): audio_path
sampling_rate (_type_): sampling_rate
Returns:
_type_: _description_
"""
audio, lsr = torchaudio.load(audiopath)
# stereo to mono if needed
if audio.size(0) != 1:
audio = torch.mean(audio, dim=0, keepdim=True)
# resample
audio_resampled = torchaudio.functional.resample(audio, lsr, sampling_rate)
if torch.any(audio > 10) or not torch.any(audio < 0):
print(f"Error with {audiopath}. Max={audio.max()} min={audio.min()}")
if torch.any(audio_resampled > 10) or not torch.any(audio_resampled < 0):
print(
f"Error with {audiopath}. Max={audio_resampled.max()} min={audio_resampled.min()}"
)
# clip audio invalid values
audio.clip_(-1, 1)
audio_resampled.clip_(-1, 1)
return audio, lsr, audio_resampled
|