-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeneratecsv.py
72 lines (59 loc) · 3.04 KB
/
generatecsv.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
"""
generatecsv.py
Generate the csv file as needed (useful for creating datasets)
"""
import librosa
import librosa.display
import matplotlib.pyplot as plt
import pathlib, os, csv
import numpy as np
genres = 'blues classical country disco hiphop jazz metal pop reggae rock'.split()
# Generate csv files for the datasets
header = 'filename imgname melname label rms spectral_centroid spectral_bandwidth spectral_flatness spectral_rolloff zero_crossing_rate'
header = header.split()
file = open('metadata_train.csv', 'w', newline='')
with file:
writer = csv.writer(file)
writer.writerow(header)
for g in genres:
for filename in os.listdir(f'dataset/{g}'):
songname = f'dataset/{g}/{filename}'
# The first 8 3-sec sections of the song will be put in the training set
for i in range(8):
imgname = f'dataimg/{g}/{filename[:-3].replace(".", "")}_{i}.png'
melname = f'datamel/{g}/{filename[:-3].replace(".", "")}_{i}.png'
y, sr = librosa.load(songname, sr=None, mono=True, offset=3*i, duration=3)
rms = librosa.feature.rms(y=y)
spec_cent = librosa.feature.spectral_centroid(y=y, sr=sr)
spec_bw = librosa.feature.spectral_bandwidth(y=y, sr=sr)
spec_fl = librosa.feature.spectral_flatness(y=y)
spec_ro = librosa.feature.spectral_rolloff(y=y, sr=sr)
zcr = librosa.feature.zero_crossing_rate(y)
to_append = f'{songname} {imgname} {melname} {g} {np.mean(rms)} {np.mean(spec_cent)} {np.mean(spec_bw)} {np.mean(spec_fl)} {np.mean(spec_ro)} {np.mean(zcr)}'
file = open('metadata_train.csv', 'a', newline='')
with file:
writer = csv.writer(file)
writer.writerow(to_append.split())
file = open('metadata_test.csv', 'w', newline='')
with file:
writer = csv.writer(file)
writer.writerow(header)
for g in genres:
for filename in os.listdir(f'dataset/{g}'):
songname = f'dataset/{g}/{filename}'
# The last 2 3-sec sections of the song will be put in the test set
for i in range(8, 10):
imgname = f'dataimg/{g}/{filename[:-3].replace(".", "")}_{i}.png'
melname = f'datamel/{g}/{filename[:-3].replace(".", "")}_{i}.png'
y, sr = librosa.load(songname, sr=None, mono=True, offset=3*i, duration=3)
rms = librosa.feature.rms(y=y)
spec_cent = librosa.feature.spectral_centroid(y=y, sr=sr)
spec_bw = librosa.feature.spectral_bandwidth(y=y, sr=sr)
spec_fl = librosa.feature.spectral_flatness(y=y)
spec_ro = librosa.feature.spectral_rolloff(y=y, sr=sr)
zcr = librosa.feature.zero_crossing_rate(y)
to_append = f'{songname} {imgname} {melname} {g} {np.mean(rms)} {np.mean(spec_cent)} {np.mean(spec_bw)} {np.mean(spec_fl)} {np.mean(spec_ro)} {np.mean(zcr)}'
file = open('metadata_test.csv', 'a', newline='')
with file:
writer = csv.writer(file)
writer.writerow(to_append.split())