-
Notifications
You must be signed in to change notification settings - Fork 3
/
audio.py
81 lines (66 loc) · 2.73 KB
/
audio.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import librosa
import numpy as np
import pickle
from pathlib import Path
import os
class AudioFeature:
def __init__(self, src_path, fold, label):
self.src_path = src_path
self.fold = fold
self.label = label
self.y, self.sr = librosa.load(self.src_path, mono=True)
self.features = None
def _concat_features(self, feature):
"""
Whenever a self._extract_xxx() method is called in this class,
this function concatenates to the self.features feature vector
"""
self.features = np.hstack(
[self.features, feature] if self.features is not None else feature
)
def _extract_mfcc(self, n_mfcc=25):
mfcc = librosa.feature.mfcc(self.y, sr=self.sr, n_mfcc=n_mfcc)
mfcc_mean = mfcc.mean(axis=1).T
mfcc_std = mfcc.std(axis=1).T
mfcc_feature = np.hstack([mfcc_mean, mfcc_std])
self._concat_features(mfcc_feature)
def _extract_spectral_contrast(self, n_bands=3):
spec_con = librosa.feature.spectral_contrast(
y=self.y, sr=self.sr, n_bands=n_bands
)
spec_con_mean = spec_con.mean(axis=1).T
spec_con_std = spec_con.std(axis=1).T
spec_con_feature = np.hstack([spec_con_mean, spec_con_std])
self._concat_features(spec_con_feature)
def _extract_chroma_stft(self):
stft = np.abs(librosa.stft(self.y))
chroma_stft = librosa.feature.chroma_stft(S=stft, sr=self.sr)
chroma_mean = chroma_stft.mean(axis=1).T
chroma_std = chroma_stft.std(axis=1).T
chroma_feature = np.hstack([chroma_mean, chroma_std])
self._concat_features(chroma_feature)
def extract_features(self, *feature_list, save_local=True):
"""
Specify a list of features to extract, and a feature vector will be
built for you for a given Audio sample.
By default the extracted feature and class attributes will be saved in
a local directory. This can be turned off with save_local=False.
"""
extract_fn = dict(
mfcc=self._extract_mfcc,
spectral=self._extract_spectral_contrast,
chroma=self._extract_chroma_stft,
)
for feature in feature_list:
extract_fn[feature]()
if save_local:
self._save_local()
def _save_local(self, clean_source=True):
out_name = self.src_path.split("/")[-1]
out_name = out_name.replace(".wav", "")
filename = f"{Path.home()}/projects/urban_sound_classification/data/fold{self.fold}/{out_name}.pkl"
os.makedirs(os.path.dirname(filename), exist_ok=True)
with open(filename, "wb") as f:
pickle.dump(self, f)
if clean_source:
self.y = None