-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess.py
76 lines (55 loc) · 2.82 KB
/
preprocess.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
import os
import json
import librosa
import math
DATASET_PATH = "./archive/Data/genres_original"
JSON_PATH = "DATA.json"
SAMPLE_RATE = 22050
DURATION = 30 # in seconds
SAMPLES_PER_TRACK = 22050 * DURATION
def save_mfcc(dataset_path, json_path, n_mfcc=13, n_fft=2048, hop_length=512, num_segments=5):
# dict to store data
data = {
"mapping": [],
"mfcc": [],
"labels": []
}
num_samples_per_segment = SAMPLES_PER_TRACK // num_segments
expected_num_mfcc_vectors_per_segment = math.ceil(num_samples_per_segment / hop_length) # 1.2 => 2
# loop through all genres
for i, (dirpath, dirnames, filenames) in enumerate(os.walk(dataset_path)):
# ensure we are not at root directory
if dirpath is not dataset_path:
# save semantic label
dirpath_components = dirpath.split("\\") # genre\\blues => ['genre', 'blues']
sematic_label = dirpath_components[-1] # blues
data["mapping"].append(sematic_label)
print("Processing {}".format(sematic_label))
# loop through all files in genre
for f in filenames:
# load audio file
file_path = os.path.join(dirpath, f)
try:
signal, sr = librosa.load(file_path, sr=SAMPLE_RATE)
# process segments extracting mfcc and storing data
for s in range(num_segments):
start_sample = num_samples_per_segment * s # s=0 => 0
finish_sample = start_sample + num_samples_per_segment # s=0 => num_samples_per_segment
mfcc = librosa.feature.mfcc(signal[start_sample:finish_sample],
sr=sr,
n_fft=n_fft,
n_mfcc=n_mfcc,
hop_length=hop_length)
mfcc = mfcc.T
# store mfcc for segment if it has expected length
if len(mfcc) == expected_num_mfcc_vectors_per_segment:
data["mfcc"].append(mfcc.tolist())
data["labels"].append(i-1)
except Exception as e:
print("Error processing file {}".format(file_path))
print(e)
# save data to json file
with open(json_path,"w+", encoding="utf-8") as fp:
json.dump(data, fp, indent=4)
if __name__ == "__main__":
save_mfcc(DATASET_PATH, JSON_PATH, num_segments=10)