-
Notifications
You must be signed in to change notification settings - Fork 8
/
feature_extract.py
80 lines (61 loc) · 2.37 KB
/
feature_extract.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
from __future__ import print_function
# importing os gives us access to methods we need to manipulate paths
import os
import json
# librosa is a great all-around audio handling library in Python.
# here we'll be using it to extract features like length, loudness,
# and frequency content.
import librosa
# tqdm is a progress bar helper that will show us how quickly
# our code is running.
from tqdm import tqdm
# glob lets us find files by using wildcards, like "*"
from glob import glob
from audio_utils import normalized, \
trim_data, \
loudness_at, \
loudness_of, \
poorly_estimate_fundamental, \
average_eq_bands, \
split_into
def load_and_trim(file):
y, rate = librosa.load(file, mono=True)
y = normalized(y)
trimmed = trim_data(y)
return trimmed, rate
def features_for(file):
# Load and trim the audio file to only the portions that aren't silent.
audio, rate = load_and_trim(file)
features = {"duration": librosa.get_duration(audio, rate)}
# Let's split up the audio file into chunks
for (i, section) in enumerate(split_into(audio, 10)):
# And in each of those chunks:
# ...get the loudness for that chunk
features["loudness_%d" % i] = loudness_of(section)
# Use poorly_estimate_fundamental to figure out
# what the rough pitch is, along with the standard
# deviation - how much that pitch varies.
fundamental, fundamental_stddev = \
poorly_estimate_fundamental(section, rate)
features["fundamental_%d" % i] = fundamental
features["fundamental_stddev_%d" % i] = fundamental_stddev
# ...make a feature out of each of 25 EQ bands.
for (j, value) in enumerate(average_eq_bands(section, 99)):
features["average_eq_%d_%d" % (i, j)] = value
return features
def extract_features(data_dir="./data/"):
audio_files = glob(os.path.join(data_dir, '**', '*'))
features = {}
for file in tqdm(audio_files):
try:
features[file] = features_for(file)
except Exception as e:
sys.stderr.write("Failed to run on %s: %s\n" % (file, e))
return features
if __name__ == "__main__":
import sys
if sys.argv[-1].endswith('.py'):
features = extract_features()
else:
features = extract_features(sys.argv[-1])
print(json.dumps(features, indent=4, sort_keys=True))