-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspectogram_model.py
More file actions
71 lines (51 loc) · 2.55 KB
/
Copy pathspectogram_model.py
File metadata and controls
71 lines (51 loc) · 2.55 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
61
62
63
64
65
66
67
68
69
70
import matplotlib.pyplot as plt
import os
import librosa.display
import numpy as np
import multiprocessing as mp
import time
GENRES = ['Blues', 'Classical', 'Country', 'Electronic', 'Hip-Hop', 'Jazz', 'Metal', 'Pop', 'Reggae', 'Rock']
WAV_DIR = './new_wav/' # TODO: change based on .wavs we want
def generate_mel_spectrograms():
for genre in GENRES:
for song in os.listdir(WAV_DIR + genre):
song_name = song[:-4]
samples, sample_rate = librosa.load(WAV_DIR + genre + '/' + song, sr=None)
sgram = librosa.stft(samples)
# use the mel-scale instead of raw frequency
sgram_mag, _ = librosa.magphase(sgram)
mel_scale_sgram = librosa.feature.melspectrogram(S=sgram_mag, sr=sample_rate)
# use the decibel scale to get the final Mel Spectrogram
mel_sgram = librosa.amplitude_to_db(mel_scale_sgram, ref=np.min)
librosa.display.specshow(mel_sgram, sr=sample_rate, x_axis='time', y_axis='mel')
# for saving image
plt.axis('off')
plt.savefig('melspecs/' + genre + '/' + song_name + '.png', bbox_inches='tight', pad_inches=0)
print("mel spectrogram saved for " + genre + ' / ' + song_name)
def generate_mel_spectrograms_for_one_genre(genre: str):
start = time.time()
song_num = 1
for song in os.listdir(WAV_DIR + genre):
song_name = song[:-4]
samples, sample_rate = librosa.load(WAV_DIR + genre + '/' + song, sr=None)
sgram = librosa.stft(samples)
# use the mel-scale instead of raw frequency
sgram_mag, _ = librosa.magphase(sgram)
mel_scale_sgram = librosa.feature.melspectrogram(S=sgram_mag, sr=sample_rate)
# use the decibel scale to get the final Mel Spectrogram
mel_sgram = librosa.amplitude_to_db(mel_scale_sgram, ref=np.min)
librosa.display.specshow(mel_sgram, sr=sample_rate, x_axis='time', y_axis='mel')
# for saving image
plt.axis('off')
plt.savefig('melspecs/' + genre + '/' + song_name + '.png', bbox_inches='tight', pad_inches=0)
print(str(song_num) + "\tmel spectrogram saved for " + genre + ' /\t' + song_name, flush=True)
song_num += 1
end = time.time()
message = "~~ thread generating mel spectrograms for " + genre + " took " + str((end-start)/60) + " minutes"
print()
print(message, flush=True)
print()
if __name__ == '__main__':
for genre in GENRES:
p = mp.Process(target=generate_mel_spectrograms_for_one_genre, args=(genre,))
p.start()