Spaces:
Running on Zero
Running on Zero
File size: 1,767 Bytes
83e59db | 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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | import numpy as np
import torch
import torchaudio
def load_audio(path, dtype="float64"):
try:
waveform, samplerate = torchaudio.load(path, channels_first=False)
waveform = np.asanyarray(waveform.squeeze().numpy(), dtype=dtype)
return waveform, samplerate
except Exception:
# in case torchaudio fails, try soundfile
try:
import soundfile as sf
return sf.read(path, dtype=dtype)
except Exception:
# some files are not readable by soundfile, try madmom
try:
import madmom
return madmom.io.load_audio_file(str(path), dtype=dtype)
except Exception:
raise RuntimeError(f'Could not load audio from "{path}".')
class LogMelSpect(torch.nn.Module):
def __init__(
self,
sample_rate=22050,
n_fft=1024,
hop_length=441,
f_min=30,
f_max=11000,
n_mels=128,
mel_scale="slaney",
normalized="frame_length",
power=1,
log_multiplier=1000,
device="cpu",
):
super().__init__()
self.spect_class = torchaudio.transforms.MelSpectrogram(
sample_rate=sample_rate,
n_fft=n_fft,
hop_length=hop_length,
f_min=f_min,
f_max=f_max,
n_mels=n_mels,
mel_scale=mel_scale,
normalized=normalized,
power=power,
).to(device)
self.log_multiplier = log_multiplier
def forward(self, x):
"""Input is a waveform as a monodimensional array of shape T,
output is a 2D log mel spectrogram of shape (F,128)."""
return torch.log1p(self.log_multiplier * self.spect_class(x).T)
|