-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmfcc_data_extraction.py
More file actions
60 lines (49 loc) · 2.17 KB
/
mfcc_data_extraction.py
File metadata and controls
60 lines (49 loc) · 2.17 KB
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
from operator import index
import os
import librosa
import math
import json
DATASET_PATH = "Data/genres_original"
JSON_PATH = "data.json"
sample_rate = 22050
duration = 30 # mesaured in seconds
samples_per_track = sample_rate * duration
def extract_mfcc(dataset_path, json_path, n_mfcc=13, hop_length=512, n_fft=2048, num_segments=5):
# dictionary to store the data
data = {
"mapping": [],
"mfcc": [],
"labels": []
}
num_samples_per_segment = int(samples_per_track / num_segments)
expected_num_mfcc_vectors_per_segment = math.ceil(
num_samples_per_segment / hop_length)
# loop through all the genres
for i, (dirpath, dirnames, filenames) in enumerate(os.walk(dataset_path)):
if dirpath is not dataset_path:
dirpath_components = dirpath.split("/")
semantic_label = dirpath_components[-1]
data["mapping"].append(semantic_label)
print("\nProcessing {}".format(semantic_label))
# looop through the audio files
for f in filenames:
file_path = os.path.join(dirpath, f)
signal, sr = librosa.load(file_path, sr=sample_rate)
# process mfcc for each window segment
for s in range(num_segments):
start_sample = num_samples_per_segment * s
finish_sample = start_sample + num_samples_per_segment
mfcc = librosa.feature.mfcc(signal[start_sample:finish_sample],
sr=sr,
n_mfcc=n_mfcc,
hop_length=hop_length,
n_fft=n_fft)
mfcc = mfcc.T
if len(mfcc) == expected_num_mfcc_vectors_per_segment:
data["mfcc"].append(mfcc.tolist())
data["labels"].append(i-1)
print("{}, segment:{}".format(file_path, s))
with open(json_path, "w") as fp:
json.dump(data, fp, indent=4)
if __name__ == "__main__":
extract_mfcc(DATASET_PATH, JSON_PATH, num_segments=10)