-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_json_create.py
64 lines (56 loc) · 2.3 KB
/
main_json_create.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
import os
import librosa
import math
import json
import matplotlib.pyplot as plt
import numpy as np
dataset_path = r"Data/genres_original" #location to gtzan data
json_path = r"data.json"
SAMPLE_RATE = 22050
DURATION = 30
SAMPLES_PER_TRACK = SAMPLE_RATE * DURATION
def save_mfcc(dataset_path, json_path, n_mfcc=13, n_fft=2048,
hop_length=512, num_segments=5):
# Data storage dictionary
data = {
"mapping": [],
"mfcc": [],
"labels": [],
}
samples_ps = int(SAMPLES_PER_TRACK/num_segments) # ps = per segment
expected_vects_ps = math.ceil(samples_ps/hop_length)
# loop through all the genres
for i, (dirpath, dirnames, filenames) in enumerate(os.walk(dataset_path)):
# ensuring not at root
if dirpath is not dataset_path:
# save the semantic label
dirpath_comp = dirpath.split("/")
semantic_label = dirpath_comp[-1]
data["mapping"].append(semantic_label)
print(f"Processing: {semantic_label}")
# process files for specific genre
for f in filenames:
if(f==str("jazz.00054.wav")):
# As librosa only read files <1Mb
continue
else:
# load audio file
file_path = os.path.join(dirpath, f)
signal,sr = librosa.load(file_path,sr=SAMPLE_RATE)
for s in range(num_segments):
start_sample = samples_ps * s
finish_sample = start_sample + samples_ps
mfcc = librosa.feature.mfcc(y=signal[start_sample:finish_sample],
sr=sr,
n_fft=n_fft,
n_mfcc=n_mfcc,
hop_length=hop_length)
mfcc = mfcc.T
# store mfcc if it has expected length
if len(mfcc)==expected_vects_ps:
data["mfcc"].append(mfcc.tolist())
data["labels"].append(i-1)
print(f"{file_path}, segment: {s+1}")
with open(json_path,"w") as f:
json.dump(data,f,indent=4)
save_mfcc(dataset_path, json_path, n_mfcc=13, n_fft=2048)