Audio Dataset
This commit is contained in:
@ -15,6 +15,9 @@ class Speed(object):
|
||||
# noinspection PyTypeChecker
|
||||
self.max_amount = min(max(0, max_amount), 1)
|
||||
|
||||
def __repr__(self):
|
||||
return f'{self.__class__.__name__}({self.__dict__})'
|
||||
|
||||
def __call__(self, x):
|
||||
if self.speed_min == 1 and self.speed_max == 1:
|
||||
return x
|
||||
|
@ -37,6 +37,9 @@ class MFCC(object):
|
||||
def __init__(self, **kwargs):
|
||||
self.__dict__.update(kwargs)
|
||||
|
||||
def __repr__(self):
|
||||
return f'{self.__class__.__name__}({self.__dict__})'
|
||||
|
||||
def __call__(self, y):
|
||||
mfcc = librosa.feature.mfcc(y, **self.__dict__)
|
||||
return mfcc
|
||||
@ -47,6 +50,9 @@ class NormalizeLocal(object):
|
||||
self.cache: np.ndarray
|
||||
pass
|
||||
|
||||
def __repr__(self):
|
||||
return f'{self.__class__.__name__}({self.__dict__})'
|
||||
|
||||
def __call__(self, x: np.ndarray):
|
||||
mean = x.mean()
|
||||
std = x.std() + 0.0001
|
||||
@ -65,6 +71,9 @@ class NormalizeMelband(object):
|
||||
self.cache: np.ndarray
|
||||
pass
|
||||
|
||||
def __repr__(self):
|
||||
return f'{self.__class__.__name__}({self.__dict__})'
|
||||
|
||||
def __call__(self, x: np.ndarray):
|
||||
mean = x.mean(-1).unsqueeze(-1)
|
||||
std = x.std(-1).unsqueeze(-1)
|
||||
@ -98,6 +107,9 @@ class PowerToDB(object):
|
||||
def __init__(self, running_max=False):
|
||||
self.running_max = 0 if running_max else None
|
||||
|
||||
def __repr__(self):
|
||||
return f'{self.__class__.__name__}({self.__dict__})'
|
||||
|
||||
def __call__(self, x):
|
||||
if self.running_max is not None:
|
||||
self.running_max = max(np.max(x), self.running_max)
|
||||
@ -109,6 +121,9 @@ class LowPass(object):
|
||||
def __init__(self, sr=16000):
|
||||
self.sr = sr
|
||||
|
||||
def __repr__(self):
|
||||
return f'{self.__class__.__name__}({self.__dict__})'
|
||||
|
||||
def __call__(self, x):
|
||||
return butter_lowpass_filter(x, 1000, 1)
|
||||
|
||||
@ -117,6 +132,9 @@ class MelToImage(object):
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def __repr__(self):
|
||||
return f'{self.__class__.__name__}({self.__dict__})'
|
||||
|
||||
def __call__(self, x):
|
||||
# Source to Solution: https://stackoverflow.com/a/57204349
|
||||
mels = np.log(x + 1e-9) # add small number to avoid log(0)
|
||||
|
30
audio_toolset/mel_dataset.py
Normal file
30
audio_toolset/mel_dataset.py
Normal file
@ -0,0 +1,30 @@
|
||||
from pathlib import Path
|
||||
|
||||
import numpy as np
|
||||
from torch.utils.data import Dataset
|
||||
|
||||
|
||||
class TorchMelDataset(Dataset):
|
||||
def __init__(self, identifier, mel_path, segment_len, hop_len, label, padding=0, transform=None):
|
||||
self.padding = padding
|
||||
self.path = next(iter(Path(mel_path).glob(f'{identifier}_*')))
|
||||
self.segment_len = segment_len
|
||||
self.m, self.n = str(self.path).split('_')[-2:] # get spectrogram dimensions
|
||||
self.n = int(self.n.split('.', 1)[0]) # remove .npy
|
||||
self.m, self.n = (int(i) for i in (self.m, self.n))
|
||||
self.offsets = list(range(0, self.n - segment_len, hop_len))
|
||||
self.label = label
|
||||
self.transform = transform
|
||||
|
||||
def __getitem__(self, item):
|
||||
start = self.offsets[item]
|
||||
mel_spec = np.load(str(self.path), allow_pickle=True)
|
||||
if self.padding > 0:
|
||||
mel_spec = np.pad(mel_spec, pad_width=[(0, 0), (self.padding // 2, self.padding // 2)], mode='mean')
|
||||
snippet = mel_spec[:, start: start + self.segment_len]
|
||||
if self.transform:
|
||||
snippet = self.transform(snippet)
|
||||
return snippet, self.label
|
||||
|
||||
def __len__(self):
|
||||
return len(self.offsets)
|
@ -8,6 +8,9 @@ class Normalize(object):
|
||||
def __init__(self, min_db_level: Union[int, float]):
|
||||
self.min_db_level = min_db_level
|
||||
|
||||
def __repr__(self):
|
||||
return f'{self.__class__.__name__}({self.__dict__})'
|
||||
|
||||
def __call__(self, s: np.ndarray) -> np.ndarray:
|
||||
return np.clip((s - self.min_db_level) / -self.min_db_level, 0, 1)
|
||||
|
||||
@ -17,5 +20,8 @@ class DeNormalize(object):
|
||||
def __init__(self, min_db_level: Union[int, float]):
|
||||
self.min_db_level = min_db_level
|
||||
|
||||
def __repr__(self):
|
||||
return f'{self.__class__.__name__}({self.__dict__})'
|
||||
|
||||
def __call__(self, s: np.ndarray) -> np.ndarray:
|
||||
return (np.clip(s, 0, 1) * -self.min_db_level) + self.min_db_level
|
||||
|
Reference in New Issue
Block a user