-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport_data.py
356 lines (298 loc) · 12.8 KB
/
export_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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/. */
# ------ Copyright (C) 2021 University of Strathclyde and Author ------
# ---------------------- Author: Callum Wilson ------------------------
# --------------- e-mail: [email protected] ----------------
"""Convert and export scottish-midi files into format for training"""
import muspy
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from tqdm import tqdm, trange
import os
inst_data = ['piano']*8 + ['perc']*8 + ['organ']*8 + ['guitar']*8 + ['bass']*8 + ['strings']*8 + ['ensemble']*8 + ['brass']*8 + ['reed']*8 + ['pipe']*8 + ['synth_lead']*8 + ['synth_pad']*8 + ['synth_eff']*8 + ['ethnic']*8 + ['percussive']*8 + ['sound_eff']*8
def describe_data(mus):
"""Returns various characteristics of a music object from a single file"""
title = mus.metadata.source_filename[:-4]
mus = mus.adjust_resolution(target=24)
n_track = len(mus)
mus_piano = muspy.to_pianoroll_representation(mus)
ind_nz = np.nonzero(mus_piano)
note_min = np.min(ind_nz[1])
note_max = np.max(ind_nz[1])
mus_nz = mus_piano[ind_nz[0][0]:ind_nz[0][-1]+1,:]
inst_nos = []
inst_names = []
n_drum = 0
for track in mus:
if track.is_drum:
n_drum += 1
inst_nos.append(-1)
inst_names.append('drums')
else:
inst_nos.append(track.program+1)
inst_names.append(inst_data[track.program])
t_sig = '{}/{}'.format(mus.time_signatures[0].numerator,mus.time_signatures[0].denominator)
tempo = mus.tempos[0].qpm
return title, n_track, note_min, note_max, mus_nz.shape[0], inst_nos, inst_names, n_drum, t_sig, tempo
track_inst = ['Drums', 'Piano', 'Guitar', 'Bass', 'Strings', 'Wind', 'Accordion', 'Harp']
track_convert = {-1:0, 0:0, 1:1, 2:1, 3:1, 5:1, 6:1, 8:1,
11:7, 14:7, 15:7, 16:7,
22:6, 23:6, 24:6,
25:2, 26:2, 31:2,
33:3, 34:3, 36:3, 37:3,
41:4, 42:4, 43:3, 47:7, 49:4, 51:4,
57:5, 58:5, 59:5, 62:5, 66:5, 67:5, 68:5, 69:5, 71:5, 72:5, 73:5, 74:5, 75:5, 76:5, 77:5, 80:5,
89:7, 95:7,
106:2, 110:5, 111:4, 112:5}
def f_track_converter():
return track_inst, track_convert
def describe_tracks(mus):
"""Returns a dictionary of characteristics of each track of a music object from a single file"""
t_data = {'min_note':[], 'max_note':[], 'mean_note':[], 'n_note':[], 'poly_ratio':[], 'min_dur':[], 'max_dur':[], 'mean_dur':[], 'program':[], 'inst':[], 'new_inst':[]}
for t in mus:
t_piano = muspy.to_pianoroll_representation(muspy.Music(tracks=[t]))
ind_nz = np.nonzero(t_piano)
t_data['min_note'].append(np.min(ind_nz[1]))
t_data['max_note'].append(np.max(ind_nz[1]))
t_data['mean_note'].append(np.round(np.mean(ind_nz[1]),2))
t_data['n_note'].append(len(t.notes))
un_time, c_time = np.unique(ind_nz[0],return_counts=True)
r_poly = float(len(np.where(c_time>1)[0]))/float(len(un_time))
t_data['poly_ratio'].append(r_poly)
note_durs = [float(n.duration)/float(mus.resolution) for n in t.notes]
t_data['min_dur'].append(np.min(note_durs))
t_data['max_dur'].append(np.max(note_durs))
t_data['mean_dur'].append(np.round(np.mean(note_durs),3))
t_data['program'].append(t.program+1)
if t.is_drum:
t_data['inst'].append('drum')
t_data['new_inst'].append(0)
else:
t_data['inst'].append(inst_data[t.program])
t_data['new_inst'].append(track_convert[t.program+1])
return t_data
def balance_new_tracks(mus):
"""
Apply heuristics to tracks in mus to assign new tracks
...
Parameters
----------
mus : muspy.Music
Music to be converted
Returns
-------
new_inst_nos : list
For each track in mus, the corresponding new track to which it is assigned
n_new : list
Number of old tracks in each of the 8 new tracks
"""
new_inst_nos = []
for t in mus:
if t.is_drum:
new_inst_nos.append(0)
else:
new_inst_nos.append(track_convert[t.program+1])
new_inst_nos = np.array(new_inst_nos)
def get_n_new():
"""Number of old tracks in each new track"""
n_new = []
for i in range(8):
n_new.append(len(np.where(new_inst_nos==i)[0]))
return np.array(n_new)
n_new = get_n_new()
inst_max=2
if np.any(np.array(n_new)>inst_max):
t_info = pd.DataFrame(describe_tracks(mus))
def update_t():
t_info.new_inst = new_inst_nos
if n_new[1]>inst_max:
ind_bass = t_info.loc[(t_info.new_inst==1)&(t_info.mean_note<50)].index
new_inst_nos[ind_bass] = 3
n_new = get_n_new()
update_t()
if n_new[1]>inst_max:
ind_melody = t_info.loc[(t_info.new_inst==1)&(t_info.mean_note>60)&(t_info.n_note>100)].index
new_inst_nos[ind_melody[0]] = 4
n_new = get_n_new()
update_t()
for _ in range(3):
if n_new[1]>inst_max:
empty_inst = np.where(n_new==0)[0]
empty_inst = empty_inst[empty_inst>0]
ind_non_poly = t_info.loc[(t_info.new_inst==1)&(t_info.poly_ratio<0.2)].index
if len(empty_inst)>0 and len(ind_non_poly)>0:
new_inst_nos[ind_non_poly[0]] = empty_inst[0]
n_new = get_n_new()
update_t()
if n_new[5]>inst_max:
ind_bass = t_info.loc[(t_info.new_inst==5)&(t_info.mean_note<50)].index
new_inst_nos[ind_bass] = 3
n_new = get_n_new()
update_t()
if n_new[5]>inst_max:
ind_melody = t_info.loc[(t_info.new_inst==5)&(t_info.mean_note>60)&(t_info.n_note>100)].index
new_inst_nos[ind_melody] = 4
n_new = get_n_new()
update_t()
for _ in range(3):
if n_new[4]>inst_max:
empty_inst = np.where(n_new==0)[0]
empty_inst = empty_inst[empty_inst>0]
ind_non_poly = t_info.loc[(t_info.new_inst==4)&(t_info.poly_ratio==0)].index
if len(empty_inst)>0 and len(ind_non_poly)>0:
new_inst_nos[ind_non_poly[0]] = empty_inst[0]
n_new = get_n_new()
update_t()
if n_new[2]>inst_max:
ind_bass = t_info.loc[(t_info.new_inst==2)&(t_info.mean_note<50)].index
new_inst_nos[ind_bass] = 3
n_new = get_n_new()
update_t()
if n_new[2]>inst_max:
ind_melody = t_info.loc[(t_info.new_inst==2)&(t_info.mean_note>60)&(t_info.n_note>100)].index
if n_new[4]<2:
new_inst_nos[ind_melody] = 4
elif n_new[5]<2:
new_inst_nos[ind_melody] = 5
n_new = get_n_new()
update_t()
if n_new[2]>inst_max and n_new[1]<2:
ind_chord = t_info.loc[(t_info.new_inst==2)&(t_info.poly_ratio>0)].index
new_inst_nos[ind_chord] = 1
n_new = get_n_new()
update_t()
for _ in range(2):
if n_new[2]>inst_max:
empty_inst = np.where(n_new==0)[0]
empty_inst = empty_inst[empty_inst>0]
ind_non_poly = t_info.loc[(t_info.new_inst==2)&(t_info.poly_ratio<0.1)].index
if len(empty_inst)>0 and len(ind_non_poly)>0:
new_inst_nos[ind_non_poly[0]] = empty_inst[0]
n_new = get_n_new()
update_t()
if n_new[6]>inst_max:
ind_bass = t_info.loc[(t_info.new_inst==6)&(t_info.mean_note<50)].index
new_inst_nos[ind_bass] = 3
n_new = get_n_new()
update_t()
if n_new[4]>inst_max:
ind_melody = t_info.loc[(t_info.new_inst==4)&(t_info.mean_note>60)&(t_info.n_note>100)].index
new_inst_nos[ind_melody[0]] = 5
n_new = get_n_new()
update_t()
if n_new[7]>inst_max:
ind_chord = t_info.loc[(t_info.new_inst==7)&(t_info.poly_ratio>0)].index
new_inst_nos[ind_chord] = 1
n_new = get_n_new()
update_t()
n_new = get_n_new()
n_t = len(np.where(n_new)[0])
if n_t<3:
t_info = pd.DataFrame(describe_tracks(mus))
def update_t():
t_info.new_inst = new_inst_nos
if n_new[1]==2:
ind_melody = t_info.loc[(t_info.new_inst==1)&(t_info.poly_ratio<0.1)].index
if len(ind_melody)>0:
new_inst_nos[ind_melody[0]] = 4
else:
ind_mv = t_info.loc[(t_info.new_inst==1)].index[0]
new_inst_nos[ind_mv] = 2
n_new = get_n_new()
update_t()
if n_new[5]==2:
ind_melody = t_info.loc[(t_info.new_inst==5)&(t_info.poly_ratio<0.1)].index
new_inst_nos[ind_melody[0]] = 4
n_new = get_n_new()
update_t()
if n_new[4]==2:
ind_melody = t_info.loc[(t_info.new_inst==4)&(t_info.poly_ratio<0.1)].index
new_inst_nos[ind_melody[0]] = 5
n_new = get_n_new()
update_t()
if n_new[7]==2:
ind_melody = t_info.loc[(t_info.new_inst==7)&(t_info.poly_ratio<0.1)].index
new_inst_nos[ind_melody[0]] = 4
n_new = get_n_new()
update_t()
return new_inst_nos, n_new
def music_to_proll(mus, return_nz=True):
"""
Convert music object to pianoroll format and return pianoroll and nonzero index range
...
Parameters
----------
mus : muspy.Music
Music to be converted
return_nz : bool, optional
Whether to crop the pianoroll to nonzero content (default True)
Returns
-------
mus_piano : array
Pianoroll format music
nonzero indices : tuple
First and last location of nonzero indices in converted array before cropping
"""
mus_piano = muspy.to_pianoroll_representation(mus)
ind_nz = np.nonzero(mus_piano)
if return_nz:
mus_piano = mus_piano[ind_nz[0][0]:ind_nz[0][-1]+1,23:107]
else:
mus_piano = mus_piano[:,23:107]
return mus_piano, (ind_nz[0][0],ind_nz[0][-1]+1)
def mus_to_8track_array(mus,track_pos):
"""
Convert music object to array of shape (-1,4,96,84,8)
...
Parameters
----------
mus : muspy.Music
Music to be converted
track_pos : list of int (0-7)
Position of each mus track in the output array
Returns
-------
track_rolls : array
Array of 4 bar samples from mus in 8 tracks
"""
assert mus.resolution==24
assert len(track_pos)==len(mus)
full_proll, inds_mus = music_to_proll(mus)
mus_len = full_proll.shape[0]
track_rolls = np.zeros((mus_len, 84, 8))
for track, pos in zip(mus,track_pos):
mus_track = muspy.Music(tracks=[track])
track_proll, _ = music_to_proll(mus_track,return_nz=False)
track_proll = track_proll[inds_mus[0]:inds_mus[1],:]
if track_proll.shape[0] < mus_len:
track_proll = np.pad(track_proll, ((0,mus_len-track_proll.shape[0]), (0,0)) )
track_rolls[:,:,pos] += track_proll
rem_size = mus_len%(4*96)
if rem_size != 0:
track_rolls = track_rolls[:-rem_size,:,:]
return track_rolls.reshape((-1,4,96,84,8))
if __name__ == '__main__':
"""Import all data, determine valid files for converting, and convert valid files"""
fpath = 'scottish-midi/'
file_list = os.listdir(fpath)
all_data = []
print('Loading all midi files...')
for fname in tqdm(file_list):
mus_in = muspy.read_midi(fpath+fname)
all_data.append(describe_data(mus_in))
music_df = pd.DataFrame(data=all_data, columns=['title', 'tracks', 'min_note', 'max_note', 'duration', 'inst_nos', 'inst_names', 'n_drum', 'time_sig', 'tempo'])
mus_valid = music_df.loc[((music_df.time_sig=='2/4') | (music_df.time_sig=='4/4') | (music_df.time_sig=='2/2'))]
all_titles = mus_valid.loc[:,'title'].values
all_data_arr = np.empty((0,4,96,84,8))
print('Converting {} files to arrays...'.format(len(mus_valid)))
for title in tqdm(all_titles):
fname = title + '.mid'
mus = muspy.read_midi(fpath+fname)
mus = mus.adjust_resolution(target=24)
inst_new, _ = balance_new_tracks(mus)
mus_arr = mus_to_8track_array(mus,inst_new)
all_data_arr = np.append(all_data_arr, mus_arr.astype(bool), axis=0)
print('Saving array...')
np.save('conv-scottish-midi-data0',all_data_arr)