Upload mel_spectrogram.py with huggingface_hub
Browse files- mel_spectrogram.py +40 -0
mel_spectrogram.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from torchaudio import transforms as T
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
|
| 5 |
+
MEAN, STD = 0.5347, 0.0772 # Xeno-Canto stats
|
| 6 |
+
SR = 16000
|
| 7 |
+
NFFT = 1024
|
| 8 |
+
HOPLEN = 320
|
| 9 |
+
NMELS = 128
|
| 10 |
+
FMIN = 50
|
| 11 |
+
FMAX = 8000
|
| 12 |
+
|
| 13 |
+
class Normalization(torch.nn.Module):
|
| 14 |
+
def __init__(self):
|
| 15 |
+
super().__init__()
|
| 16 |
+
|
| 17 |
+
def forward(self, x):
|
| 18 |
+
return (x - x.min()) / (x.max() - x.min())
|
| 19 |
+
|
| 20 |
+
class Standardization(torch.nn.Module):
|
| 21 |
+
def __init__(self, mean, std):
|
| 22 |
+
super().__init__()
|
| 23 |
+
|
| 24 |
+
self.mean = mean
|
| 25 |
+
self.std = std
|
| 26 |
+
|
| 27 |
+
def forward(self, x):
|
| 28 |
+
return (x - self.mean) / self.std
|
| 29 |
+
|
| 30 |
+
class MelSpectrogramProcessor:
|
| 31 |
+
def __init__(self, sample_rate=SR, n_mels=NMELS, n_fft=NFFT, hop_length=HOPLEN, f_min=FMIN, f_max=FMAX, device='cpu'):
|
| 32 |
+
self.transform = nn.Sequential(
|
| 33 |
+
T.MelSpectrogram(sample_rate=sample_rate, n_mels=n_mels, n_fft=n_fft, hop_length=hop_length, f_min=f_min, f_max=f_max),
|
| 34 |
+
T.AmplitudeToDB(),
|
| 35 |
+
Normalization(),
|
| 36 |
+
Standardization(mean=MEAN, std=STD),
|
| 37 |
+
).to(device)
|
| 38 |
+
|
| 39 |
+
def process(self, waveform):
|
| 40 |
+
return self.transform(waveform)
|