forked from QUANGLEA/song-genre-predictor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocessing.py
57 lines (49 loc) · 2.06 KB
/
preprocessing.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
import os
import librosa
import math
import json
import numpy as np
import matplotlib.pyplot as plt
DATASET_PATH = 'wav_with_labels'
JSON_PATH = "data.json"
SAMPLE_RATE = 22050
DURATION = 30
SAMPLES_PER_TRACK = SAMPLE_RATE * DURATION
os.mkdir("spectrogram")
spectrogram_path = "spectrogram/"
def save_mfcc(dataset_path, json_path, n_mfcc=13, n_fft=2048, hop_length=512, num_segments=5):
#dictionary to store data
data = {
"mapping": [],
"mfcc": [], #input
"labels": [] #output/expected label
}
num_samples_per_segment=int(SAMPLES_PER_TRACK/num_segments)
expected_num_mfcc_vectors_per_segment=math.ceil(num_samples_per_segment/hop_length)
for i, (dirpath, dirnames, filenames) in enumerate(os.walk(dataset_path)):
genre = ""
### Make sure we are at genre level
if dirpath is not dataset_path:
# Save the semantic label(classical,blues,etc)
dirpath_components = dirpath.split("/") # If dirpath = wav_with_labels/blues, then this gives us=[“wav_with_labels”,”blues”]
semantic_label=dirpath_components[-1]
data["mapping"].append(semantic_label)
genre = semantic_label
### Create subdirectories to save spectrograms
if not genre: continue
if not os.path.exists(spectrogram_path + genre):
os.mkdir(spectrogram_path + genre)
# Process files for a genre - we will save only one spectrogram for now
for fname in filenames:
# Load audio file
file_path = os.path.join(dirpath, fname)
y, sr = librosa.load(file_path,sr=SAMPLE_RATE)
D = librosa.stft(y) # STFT of y
S_db = librosa.amplitude_to_db(np.abs(D), ref=np.max)
fig, ax = plt.subplots()
img = librosa.display.specshow(S_db, x_axis='time', y_axis='linear', ax=ax)
ax.set(title=fname)
fig.colorbar(img, ax=ax, format="%+2.f dB")
plt.savefig("spectrogram/" + genre + "/" + fname + ".png" )
break
save_mfcc(DATASET_PATH, JSON_PATH)