forked from annahung31/MidiNet-by-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_data.py
655 lines (535 loc) · 22.2 KB
/
get_data.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
import xml.etree.ElementTree as ET
import os
from os.path import join, splitext
import numpy as np
from argparse import ArgumentParser
def get_sample(cur_song, cur_dur, n_ratio, dim_pitch, dim_bar):
'''
MidiNet paper: https://arxiv.org/pdf/1703.10847.pdf
get_sample() converts a list of the form [path, midi notes 1:n] to MULTIPLE sample arrays X described below
h = 128, number of midi notes we consider
w = 128, number of timesteps in a bar
X \in (h x w) = (1,128, 128)
after calculating the matrix X for bar, we concatenate on the first layer (_, 1, 128, 128) to stack bars from the same song together
Can check for values generated outside range of two octaves selected in the binary matrix for model collapse
'''
cur_bar = np.zeros((1, dim_pitch, dim_bar), dtype=int) # (1,128,128)
idx = 1 # start at first midi note value - not path
sd = 0 # standard duration? HELP
ed = 0 # extended duration? HELP
song_sample = []
while idx < len(cur_song):
cur_pitch = cur_song[idx]-1 # store previous midi note value
# ed + current midi note value * n_ratio (?)
ed = int(ed + cur_dur[idx]*n_ratio)
if ed < dim_bar:
# if 'ed'<128 set all values (~, previous note value, ) that are less than ed in current bar to 1
cur_bar[0, cur_pitch, sd:ed] = 1
sd = ed
idx = idx + 1
elif ed >= dim_bar:
cur_bar[0, cur_pitch, sd:] = 1
song_sample.append(cur_bar) # add to list of ndarrays
# reset for next bar
cur_bar = np.zeros((1, dim_pitch, dim_bar), dtype=int)
sd = 0
ed = 0
# sample = np.asarray(sample)
# print('sample shape: {}\t cur_bar shape: {}'.format(sample.shape, cur_bar.shape))
return song_sample
def build_matrix(note_list_all_c, dur_list_all_c, key_list_all_c):
data_x = []
prev_x = []
y = []
zero_counter = 0
for i in range(len(note_list_all_c)):
song = note_list_all_c[i]
dur = dur_list_all_c[i]
key = key_list_all_c[i]
song_sample = get_sample(song, dur, 4, 128, 128)
np_sample = np.asarray(song_sample)
if len(np_sample) == 0:
zero_counter += 1
if len(np_sample) != 0:
np_sample = np_sample[0]
np_sample = np_sample.reshape(1, 1, 128, 128)
if np.sum(np_sample) != 0:
place = np_sample.shape[3]
new = []
for j in range(0, place, 16):
new.append(np_sample[0][:, :, j:j+16])
# (2,1,128,128) will become (16,1,128,16)
new = np.asarray(new)
new_prev = np.zeros(new.shape, dtype=int)
new_prev[1:, :, :, :] = new[0:new.shape[0]-1, :, :, :]
data_x.append(new)
prev_x.append(new_prev)
for j in range(8):
y.append(key)
y = np.vstack(y)
data_x = np.vstack(data_x)
prev_x = np.vstack(prev_x)
return data_x, prev_x, y, zero_counter
# def build_matrix(note_list_all_c, dur_list_all_c, key_list_all_c):
# data_x, prev_x, y = ([], ) * 3
# zero_counter = 0
# for i in range(len(note_list_all_c)): # iterating over number of songs
# song = note_list_all_c[i]
# dur = dur_list_all_c[i]
# key = key_list_all_c[i]
# # sample = get_sample(cur_song=song, cur_dur=dur, key=key, n_ratio=4, dim_pitch=128, dim_bar=128) # returns (num_bars, 1, 128, 128)
# song_sample = get_sample(cur_song=song, cur_dur=dur, n_ratio=4, dim_pitch=128, dim_bar=128)
# sample = np.asarray(song_sample)
# if len(sample) == 0: # might be empty from get_sample
# zero_counter += 1
# if len(sample) != 0:
# sample = sample[0] # why would you overwrite the sample you got above?
# sample = sample.reshape(1, 1, 128, 128)
# if np.sum(sample) != 0:
# place = sample.shape[3] # 128
# new = []
# for i in range(0, place, 16): # [0, 16, 32, 48, 64, 80, 96, 112]
# new.append(sample[0][:, :, i:i+16])
# # above line splits last dimension into blocks of 16 - puts it into dimension 0
# # THIS DOES NOTHING WITH THE FIRST DIMENSION OF SAMPLES ???? FIX
# new = np.asarray(new) # (2,1,128,128) will become (16,1,128,16)
# new_prev = np.zeros(new.shape, dtype=int)
# new_prev[1:, :, :, :] = new[0:new.shape[0]-1, :, :, :]
# data_x.append(new)
# prev_x.append(new_prev)
# y.append(key)
# data_x = np.vstack(data_x)
# prev_x = np.vstack(prev_x)
# y = np.vstack(y)
# return data_x, prev_x, y, zero_counter
def check_melody_range(note_list_all, dur_list_all, key_list_all):
in_range = 0
# THESE NAMES ARE TERRIBLE - FIX!!!
note_list_all_c = []
dur_list_all_c = []
key_list_all_c = []
for i in range(len(note_list_all)):
song = note_list_all[i]
if len(song[1:]) == 0: # NO NOTES IN SONG - only path
pass
elif min(song[1:]) >= 60 and max(song[1:]) <= 83:
in_range += 1
note_list_all_c.append(song)
dur_list_all_c.append(dur_list_all[i])
key_list_all_c.append(key_list_all[i])
np.save('dur_list_all_c.npy', dur_list_all_c)
np.save('note_list_all_c.npy', note_list_all_c)
np.save('key_list_all_c.npy', key_list_all_c)
return in_range, note_list_all_c, dur_list_all_c, key_list_all_c
def one_hot(length, pos):
oh = np.zeros(length)
oh[pos] = 1
return oh
def transform_note(c_key_list, d_key_list, e_key_list, f_key_list, g_key_list, a_key_list, b_key_list):
# HUGE PROBLEM - THIS GENERALIZES THE SCALE TO ONLY NATURAL NOTES - NO SHARPS OR FLATS
scale = [48, 50, 52, 53, 55, 57, 59, 60, 62, 64, 65, 67, 69,
71, 72, 74, 76, 77, 79, 81, 83, 84, 86, 88, 89, 91, 93]
transfor_list_C1 = scale[0:7]
transfor_list_C2 = scale[7:14]
transfor_list_C3 = scale[14:21]
transfor_list_D1 = scale[1:8]
transfor_list_D2 = scale[8:15]
transfor_list_D3 = scale[15:22]
transfor_list_E1 = scale[2:9]
transfor_list_E2 = scale[9:16]
transfor_list_E3 = scale[16:23]
transfor_list_F1 = scale[3:10]
transfor_list_F2 = scale[10:17]
transfor_list_F3 = scale[17:24]
transfor_list_G1 = scale[4:11]
transfor_list_G2 = scale[11:18]
transfor_list_G3 = scale[18:25]
transfor_list_A1 = scale[5:12]
transfor_list_A2 = scale[12:19]
transfor_list_A3 = scale[19:26]
transfor_list_B1 = scale[6:13]
transfor_list_B2 = scale[13:20]
transfor_list_B3 = scale[20:27]
note_c, note_d, note_e, note_f, note_g, note_a, note_b = ([], ) * 7
dur_c, dur_d, dur_e, dur_f, dur_g, dur_a, dur_b = ([], ) * 7
key_c, key_d, key_e, key_f, key_g, key_a, key_b = ([], ) * 7
for file_ in c_key_list:
note_list = [file_]
dur_list = [file_]
try:
chorus_file = ET.parse(file_)
root = chorus_file.getroot()
for item in root.iter(tag='note'):
note = item[4].text
dur = item[3].text
octave = item[5].text
dur = float(dur)
dur_list.append(dur)
try:
note = int(note)
if octave == '-1':
# PROBLEM - THIS GENERALIZES ALL NOTES IN 'C' to the major scale only
h_idx = transfor_list_C1[note-1]
elif octave == '0':
h_idx = transfor_list_C2[note-1]
elif octave == '1':
h_idx = transfor_list_C3[note-1]
note_list.append(h_idx)
except:
if len(note_list) == 1:
note = 0
note_list.append(note)
else:
note = note_list[-1]
note_list.append(note)
if note_list[1] == 0:
note_list[1] = note_list[2]
dur_list[1] = dur_list[2]
note_c.append(note_list)
dur_c.append(dur_list)
key_c.append(one_hot(13, 0)) # encoded 1-12 = key, 13=maj/min
except:
pass
# print('c key but no melody/notes :{}'.format(file_))
for file_ in d_key_list:
note_list = [file_]
dur_list = [file_]
try:
chorus_file = ET.parse(file_)
root = chorus_file.getroot()
for item in root.iter(tag='note'):
note = item[4].text
dur = item[3].text
octave = item[5].text
dur = float(dur)
dur_list.append(dur)
try:
note = int(note)
if octave == '-1':
h_idx = transfor_list_D1[note-1]
elif octave == '0':
h_idx = transfor_list_D2[note-1]
elif octave == '1':
h_idx = transfor_list_D3[note-1]
note_list.append(h_idx)
except:
if len(note_list) == 1:
note = 0
note_list.append(note)
else:
note = note_list[-1]
note_list.append(note)
if note_list[1] == 0:
note_list[1] = note_list[2]
dur_list[1] = dur_list[2]
note_d.append(note_list)
dur_d.append(dur_list)
key_d.append(one_hot(13, 2)) # encoded 1-12 = key, 13=maj/min
except:
pass
# print('d key but no melody/notes :{}'.format(file_))
for file_ in e_key_list:
note_list = [file_]
dur_list = [file_]
try:
chorus_file = ET.parse(file_)
root = chorus_file.getroot()
for item in root.iter(tag='note'):
note = item[4].text
dur = item[3].text
octave = item[5].text
dur = float(dur)
dur_list.append(dur)
try:
note = int(note)
if octave == '-1':
h_idx = transfor_list_E1[note-1]
elif octave == '0':
h_idx = transfor_list_E2[note-1]
elif octave == '1':
h_idx = transfor_list_E3[note-1]
note_list.append(h_idx)
except:
if len(note_list) == 1:
note = 0
note_list.append(note)
else:
note = note_list[-1]
note_list.append(note)
if note_list[1] == 0:
note_list[1] = note_list[2]
dur_list[1] = dur_list[2]
note_e.append(note_list)
dur_e.append(dur_list)
key_e.append(one_hot(13, 4)) # encoded 1-12 = key, 13=maj/min
except:
pass
# print('e key but no melody/notes :{}'.format(file_))
for file_ in f_key_list:
note_list = [file_]
dur_list = [file_]
try:
chorus_file = ET.parse(file_)
root = chorus_file.getroot()
for item in root.iter(tag='note'):
note = item[4].text
dur = item[3].text
octave = item[5].text
dur = float(dur)
dur_list.append(dur)
try:
note = int(note)
if octave == '-1':
h_idx = transfor_list_F1[note-1]
elif octave == '0':
h_idx = transfor_list_F2[note-1]
elif octave == '1':
h_idx = transfor_list_F3[note-1]
note_list.append(h_idx)
except:
if len(note_list) == 1:
note = 0
note_list.append(note)
else:
note = note_list[-1]
note_list.append(note)
if note_list[1] == 0:
note_list[1] = note_list[2]
dur_list[1] = dur_list[2]
note_f.append(note_list)
dur_f.append(dur_list)
key_f.append(one_hot(13, 5))
except:
pass
# print('f key but no melody/notes :{}'.format(file_))
for file_ in g_key_list:
note_list = [file_]
dur_list = [file_]
try:
chorus_file = ET.parse(file_)
root = chorus_file.getroot()
for item in root.iter(tag='note'):
note = item[4].text
dur = item[3].text
octave = item[5].text
dur = float(dur)
dur_list.append(dur)
try:
note = int(note)
if octave == '-1':
h_idx = transfor_list_G1[note-1]
elif octave == '0':
h_idx = transfor_list_G2[note-1]
elif octave == '1':
h_idx = transfor_list_G3[note-1]
note_list.append(h_idx)
except:
if len(note_list) == 1:
note = 0
note_list.append(note)
else:
note = note_list[-1]
note_list.append(note)
if note_list[1] == 0:
note_list[1] = note_list[2]
dur_list[1] = dur_list[2]
note_g.append(note_list)
dur_g.append(dur_list)
key_g.append(one_hot(13, 7))
except:
pass
# print('g key but no melody/notes :{}'.format(file_))
for file_ in a_key_list:
note_list = [file_]
dur_list = [file_]
try:
chorus_file = ET.parse(file_)
root = chorus_file.getroot()
for item in root.iter(tag='note'):
note = item[4].text
dur = item[3].text
octave = item[5].text
dur = float(dur)
dur_list.append(dur)
try:
note = int(note)
if octave == '-1':
h_idx = transfor_list_A1[note-1]
elif octave == '0':
h_idx = transfor_list_A2[note-1]
elif octave == '1':
h_idx = transfor_list_A3[note-1]
note_list.append(h_idx)
except:
if len(note_list) == 1:
note = 0
note_list.append(note)
else:
note = note_list[-1]
note_list.append(note)
if note_list[1] == 0:
note_list[1] = note_list[2]
dur_list[1] = dur_list[2]
note_a.append(note_list)
dur_a.append(dur_list)
key_a.append(one_hot(13, 9))
except:
pass
# print('a key but no melody/notes :{}'.format(file_))
for file_ in b_key_list:
note_list = [file_]
dur_list = [file_]
try:
chorus_file = ET.parse(file_)
root = chorus_file.getroot()
for item in root.iter(tag='note'):
note = item[4].text
dur = item[3].text
octave = item[5].text
dur = float(dur)
dur_list.append(dur)
try:
note = int(note)
if octave == '-1':
h_idx = transfor_list_A1[note-1]
elif octave == '0':
h_idx = transfor_list_A2[note-1]
elif octave == '1':
h_idx = transfor_list_A3[note-1]
note_list.append(h_idx)
except:
if len(note_list) == 1:
note = 0
note_list.append(note)
else:
note = note_list[-1]
note_list.append(note)
if note_list[1] == 0:
note_list[1] = note_list[2]
dur_list[1] = dur_list[2]
note_b.append(note_list)
dur_b.append(dur_list)
key_b.append(one_hot(13, 11))
except:
pass
# print('b key but no melody/notes :{}'.format(file_))
note_list_all = note_c + note_d + note_e + note_f + note_g + note_a + note_b
dur_list_all = dur_c + dur_d + dur_e + dur_f + dur_g + dur_a + dur_b
key_list_all = key_c + key_d + key_e + key_f + key_g + key_a + key_b
return note_list_all, dur_list_all, key_list_all
def get_key(list_of_four_beat):
key_list = []
c_key_list = []
d_key_list = []
e_key_list = []
f_key_list = []
g_key_list = []
a_key_list = []
b_key_list = []
for file_ in list_of_four_beat:
try:
chorus_file = ET.parse(file_)
root = chorus_file.getroot()
# this only gives the letter key - need to find major and minor
key = root.findall('.//key')
key_list.append(key[0].text)
if key[0].text == 'C':
c_key_list.append(file_)
if key[0].text == 'D':
d_key_list.append(file_)
if key[0].text == 'E':
e_key_list.append(file_)
if key[0].text == 'F':
f_key_list.append(file_)
if key[0].text == 'G':
g_key_list.append(file_)
if key[0].text == 'A':
a_key_list.append(file_)
if key[0].text == 'B':
b_key_list.append(file_)
except:
print('Unable to open {}... skipping'.format(file_))
return c_key_list, d_key_list, e_key_list, f_key_list, g_key_list, a_key_list, b_key_list
def beats_(list_):
list_of_four_beat = []
for file_ in list_:
try:
chorus_file = ET.parse(file_)
root = chorus_file.getroot()
beats = root.findall('.//beats_in_measure')
num = beats[0].text
if num == '4':
list_of_four_beat.append(file_)
except:
print('Unable to open {}... skipping'.format(file_))
return list_of_four_beat
def check_chord_type(list_file):
list_ = []
for file_ in list_file:
try:
chorus_file = ET.parse(file_)
root = chorus_file.getroot()
check_list = []
counter = 0
None_counter = 0
for item in root.iter(tag='fb'):
check_list.append(item.text)
counter += 1
if item.text == None:
None_counter += 1
for item in root.iter(tag='borrowed'):
check_list.append(item.text)
counter += 1
if item.text == None:
None_counter += 1
if counter == None_counter:
list_.append(file_)
except:
print('Unable to open {}... skipping'.format(file_))
return list_
def get_listfile(dataset_path):
list_file = []
for root, dirs, files in os.walk(dataset_path):
for f in files:
if splitext(f)[0] == 'chorus':
fp = join(root, f)
list_file.append(fp)
return list_file
def main(args):
# first time run need to include both --get_data and --get_matrix in order to generate the matrix (or run w --get_data only)
if args.get_data:
a = 'data'
list_file = get_listfile(a)
list_ = check_chord_type(list_file)
list_of_four_beat = beats_(list_)
c_key_list, d_key_list, e_key_list, f_key_list, g_key_list, a_key_list, b_key_list = get_key(
list_of_four_beat)
note_list_all, dur_list_all, key_list_all = transform_note(
c_key_list, d_key_list, e_key_list, f_key_list, g_key_list, a_key_list, b_key_list)
in_range, note_list_all_c, dur_list_all_c, key_list_all_c = check_melody_range(
note_list_all, dur_list_all, key_list_all)
print('Total normal chord: {}'.format(len(list_)))
print('Total in four: {}'.format(len(list_of_four_beat)))
print('Melodies in range: {}'.format(len(note_list_all)))
if args.get_matrix:
note_list_all_c = np.load('note_list_all_c.npy')
dur_list_all_c = np.load('dur_list_all_c.npy')
key_list_all_c = np.load('key_list_all_c.npy')
data_x, prev_x, y, zero_counter = build_matrix(
note_list_all_c, dur_list_all_c, key_list_all_c)
np.save('data_x.npy', data_x)
np.save('prev_x.npy', prev_x)
np.save('y.npy', y)
print('Final tab num: {}'.format(len(note_list_all_c)))
print('Songs not long enough: {}'.format(zero_counter))
print('Sample shape: {}, prev sample shape: {}, label shape: {}'.format(
data_x.shape, prev_x.shape, y.shape))
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument('--get_data', action='store_true')
parser.add_argument('--get_matrix', action='store_true')
args = parser.parse_args()
# trick to allow pickle
np_load_old = np.load
np.load = lambda *a, **k: np_load_old(*a, allow_pickle=True, **k)
main(args)