-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft.py
166 lines (135 loc) · 4.54 KB
/
ft.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import pydub
import librosa
import math
import csv
import json
import numpy as np
def cut_song(song):
start = 0
end = len(song)
song_pieces = []
while start + 100000 < end:
song_pieces.append(song[start:start+100000])
start += 100000
return song_pieces
def prepare_song(song_path):
list_matrices = []
dur = pydub.utils.mediainfo(song_path)["duration"]
y, sr = librosa.load(song_path, duration = math.floor(float(dur)))
song_pieces = cut_song(y)
for song_piece in song_pieces:
melspect = librosa.feature.melspectrogram(y = song_piece)
list_matrices.append(melspect)
return list_matrices
with open('n_data.csv', mode ='r')as file:
with open('n_fulldata.csv', 'w') as f:
csvFile = csv.reader(file)
writer = csv.writer(f)
for line in csvFile:
path = "normal_songs/" + line[0]
ft = prepare_song(path)
line.append(ft)
writer.writerow(line)
with open('lk_data.csv', mode ='r')as file:
with open('lk_fulldata.csv', 'w') as f:
csvFile = csv.reader(file)
writer = csv.writer(f)
for line in csvFile:
path = "lk_songs/" + line[0]
ft = prepare_song(path)
line.append(ft)
writer.writerow(line)
with open('data/n_data.csv', mode ='r')as file:
with open('n_fulldata_exp.csv', 'w') as f:
csvFile = csv.reader(file)
writer = csv.writer(f)
for line in csvFile:
path = "normal_songs/" + line[0]
ft = prepare_song(path)
json_arr = json.dumps(ft)
line.append(json_arr)
writer.writerow(line)
# n_test
n_test_feature = []
n_test_labels = []
with open('n_test.csv', mode ='r')as file:
csvFile = csv.reader(file)
for line in csvFile:
path = "normal_songs/" + line[0]
ft = np.array(prepare_song(path))
ft.resize(46, 128, 196)
n_test_feature.append(ft)
n_test_labels.append(int(line[2]))
n_test_feature = np.array(n_test_feature)
n_test_labels= np.array(n_test_labels)
np.savez('n_test_np.npz', data=n_test_feature, labels=n_test_labels)
# n_train
n_train_feature = []
n_train_labels = []
with open('n_train.csv', mode ='r')as file:
csvFile = csv.reader(file)
for line in csvFile:
path = "normal_songs/" + line[0]
ft = np.array(prepare_song(path))
ft.resize(46, 128, 196)
n_train_feature.append(ft)
n_train_labels.append(int(line[2]))
n_train_feature = np.array(n_train_feature)
n_train_labels= np.array(n_train_labels)
np.savez('n_train_np.npz', data=n_train_feature, labels=n_train_labels)
# n_validation
n_validation_feature = []
n_validation_labels = []
with open('n_validation.csv', mode ='r')as file:
csvFile = csv.reader(file)
for line in csvFile:
path = "normal_songs/" + line[0]
ft = np.array(prepare_song(path))
ft.resize(46, 128, 196)
n_validation_feature.append(ft)
n_validation_labels.append(int(line[2]))
n_validation_feature = np.array(n_validation_feature)
n_validation_labels= np.array(n_validation_labels)
np.savez('n_validation_np.npz', data=n_validation_feature, labels=n_validation_labels)
#lk_test
lk_test_feature = []
lk_test_labels = []
with open('lk_test.csv', mode ='r')as file:
csvFile = csv.reader(file)
for line in csvFile:
path = "lk_songs/" + line[0]
ft = np.array(prepare_song(path))
ft.resize(46, 128, 196)
lk_test_feature.append(ft)
lk_test_labels.append(int(line[2]))
lk_test_feature = np.array(lk_test_feature)
lk_test_labels= np.array(lk_test_labels)
np.savez('lk_test_np.npz', data=lk_test_feature, labels=lk_test_labels)
#lk_train
lk_train_feature = []
lk_train_labels = []
with open('lk_train.csv', mode ='r')as file:
csvFile = csv.reader(file)
for line in csvFile:
path = "lk_songs/" + line[0]
ft = np.array(prepare_song(path))
ft.resize(46, 128, 196)
lk_train_feature.append(ft)
lk_train_labels.append(int(line[2]))
lk_train_feature = np.array(lk_train_feature)
lk_train_labels= np.array(lk_train_labels)
np.savez('lk_train_np.npz', data=lk_train_feature, labels=lk_train_labels)
# lk_validation
lk_validation_feature = []
lk_validation_labels = []
with open('lk_validation.csv', mode ='r')as file:
csvFile = csv.reader(file)
for line in csvFile:
path = "lk_songs/" + line[0]
ft = np.array(prepare_song(path))
ft.resize(46, 128, 196)
lk_validation_feature.append(ft)
lk_validation_labels.append(int(line[2]))
lk_validation_feature = np.array(lk_validation_feature)
lk_validation_labels= np.array(lk_validation_labels)
np.savez('lk_validation_np.npz', data=lk_validation_feature, labels=lk_validation_labels)