Spaces:
Running
Running
asigalov61
commited on
Commit
•
cf7d19c
1
Parent(s):
29090fa
Update app.py
Browse files
app.py
CHANGED
@@ -26,343 +26,103 @@ in_space = os.getenv("SYSTEM") == "spaces"
|
|
26 |
|
27 |
#==========================================================================================================
|
28 |
|
29 |
-
def
|
30 |
|
31 |
print('=' * 70)
|
32 |
print('Loading MIDI file...')
|
33 |
|
34 |
#==================================================
|
35 |
|
36 |
-
|
|
|
37 |
|
38 |
-
|
39 |
|
40 |
-
|
|
|
41 |
|
42 |
-
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
track.sort(key=lambda x: x[1])
|
47 |
-
events_matrix.extend(track)
|
48 |
-
else:
|
49 |
-
midi_ticks = s
|
50 |
|
51 |
-
|
|
|
52 |
|
53 |
-
|
|
|
54 |
|
55 |
-
mult_pitches_counts = []
|
56 |
-
|
57 |
-
for i in range(-6, 6):
|
58 |
-
|
59 |
-
events_matrix1 = []
|
60 |
-
|
61 |
-
for e in events_matrix:
|
62 |
-
|
63 |
-
ev = copy.deepcopy(e)
|
64 |
-
|
65 |
-
if e[0] == 'note':
|
66 |
-
if e[3] == 9:
|
67 |
-
ev[4] = ((e[4] % 128) + 128)
|
68 |
-
else:
|
69 |
-
ev[4] = ((e[4] % 128) + i)
|
70 |
-
|
71 |
-
events_matrix1.append(ev)
|
72 |
-
|
73 |
-
pitches_counts = [[y[0],y[1]] for y in Counter([y[4] for y in events_matrix1 if y[0] == 'note']).most_common()]
|
74 |
-
pitches_counts.sort(key=lambda x: x[0], reverse=True)
|
75 |
-
|
76 |
-
mult_pitches_counts.append(pitches_counts)
|
77 |
-
|
78 |
-
patches_list = sorted(list(set([y[3] for y in events_matrix if y[0] == 'patch_change'])))
|
79 |
-
|
80 |
-
|
81 |
-
#==================================================
|
82 |
-
|
83 |
-
ms_score = MIDI.midi2ms_score(midi)
|
84 |
-
|
85 |
-
ms_events_matrix = []
|
86 |
-
|
87 |
-
itrack1 = 1
|
88 |
-
|
89 |
-
while itrack1 < len(ms_score):
|
90 |
-
for event in ms_score[itrack1]:
|
91 |
-
if event[0] == 'note':
|
92 |
-
ms_events_matrix.append(event)
|
93 |
-
itrack1 += 1
|
94 |
-
|
95 |
-
ms_events_matrix.sort(key=lambda x: x[1])
|
96 |
-
|
97 |
-
|
98 |
-
chords = []
|
99 |
-
pe = ms_events_matrix[0]
|
100 |
-
cho = []
|
101 |
-
for e in ms_events_matrix:
|
102 |
-
if (e[1] - pe[1]) == 0:
|
103 |
-
if e[3] != 9:
|
104 |
-
if (e[4] % 12) not in cho:
|
105 |
-
cho.append(e[4] % 12)
|
106 |
-
else:
|
107 |
-
if len(cho) > 0:
|
108 |
-
chords.append(sorted(cho))
|
109 |
-
cho = []
|
110 |
-
if e[3] != 9:
|
111 |
-
if (e[4] % 12) not in cho:
|
112 |
-
cho.append(e[4] % 12)
|
113 |
-
|
114 |
-
pe = e
|
115 |
-
|
116 |
-
if len(cho) > 0:
|
117 |
-
chords.append(sorted(cho))
|
118 |
-
|
119 |
-
ms_chords_counts = sorted([[list(key), val] for key,val in Counter([tuple(c) for c in chords if len(c) > 1]).most_common()], reverse=True, key = lambda x: x[1])
|
120 |
-
|
121 |
-
times = []
|
122 |
-
pt = ms_events_matrix[0][1]
|
123 |
-
start = True
|
124 |
-
for e in ms_events_matrix:
|
125 |
-
if (e[1]-pt) != 0 or start == True:
|
126 |
-
times.append((e[1]-pt))
|
127 |
-
start = False
|
128 |
-
pt = e[1]
|
129 |
-
|
130 |
-
durs = [e[2] for e in ms_events_matrix]
|
131 |
-
vels = [e[5] for e in ms_events_matrix]
|
132 |
-
|
133 |
-
avg_time = int(sum(times) / len(times))
|
134 |
-
avg_dur = int(sum(durs) / len(durs))
|
135 |
-
|
136 |
-
mode_time = statistics.mode(times)
|
137 |
-
mode_dur = statistics.mode(durs)
|
138 |
-
|
139 |
-
median_time = int(statistics.median(times))
|
140 |
-
median_dur = int(statistics.median(durs))
|
141 |
-
|
142 |
-
#==================================================
|
143 |
-
|
144 |
-
print('=' * 70)
|
145 |
print('Done!')
|
146 |
print('=' * 70)
|
147 |
-
|
148 |
-
|
149 |
|
150 |
-
|
151 |
|
152 |
-
|
153 |
-
|
154 |
-
maximum_match_ratio_to_search_for = max_match_ratio #@param {type:"slider", min:0, max:1, step:0.01}
|
155 |
-
|
156 |
-
#@markdown MIDI pitches search options
|
157 |
-
|
158 |
-
pitches_counts_cutoff_threshold_ratio = 0 #@param {type:"slider", min:0, max:1, step:0.05}
|
159 |
-
search_transposed_pitches = False #@param {type:"boolean"}
|
160 |
-
skip_exact_matches = False #@param {type:"boolean"}
|
161 |
-
|
162 |
-
#@markdown Additional search options
|
163 |
-
|
164 |
-
add_pitches_counts_ratios = False #@param {type:"boolean"}
|
165 |
-
add_timings_ratios = False #@param {type:"boolean"}
|
166 |
-
add_durations_ratios = False #@param {type:"boolean"}
|
167 |
-
|
168 |
-
print('=' * 70)
|
169 |
-
print('MIDI Pitches Search')
|
170 |
print('=' * 70)
|
171 |
|
172 |
-
|
173 |
-
|
174 |
-
for d in progress.tqdm(meta_data):
|
175 |
-
|
176 |
-
|
177 |
-
p_counts = d[1][10][1]
|
178 |
-
p_counts.sort(reverse = True, key = lambda x: x[1])
|
179 |
-
max_p_count = p_counts[0][1]
|
180 |
-
trimmed_p_counts = [y for y in p_counts if y[1] >= (max_p_count * pitches_counts_cutoff_threshold_ratio)]
|
181 |
-
total_p_counts = sum([y[1] for y in trimmed_p_counts])
|
182 |
-
|
183 |
-
if search_transposed_pitches:
|
184 |
-
search_pitches = mult_pitches_counts
|
185 |
-
else:
|
186 |
-
search_pitches = [mult_pitches_counts[6]]
|
187 |
-
|
188 |
-
#===================================================
|
189 |
-
|
190 |
-
ratios_list = []
|
191 |
|
192 |
-
|
|
|
193 |
|
194 |
-
|
|
|
|
|
|
|
|
|
195 |
|
196 |
-
|
197 |
|
198 |
-
|
199 |
-
|
200 |
-
mode_time]
|
201 |
|
202 |
-
|
203 |
|
204 |
-
|
205 |
|
206 |
-
|
207 |
-
maxtratio = max(source_times[i], match_times[i])
|
208 |
-
mintratio = min(source_times[i], match_times[i])
|
209 |
-
times_ratios.append(mintratio / maxtratio)
|
210 |
|
211 |
-
|
212 |
|
213 |
-
|
|
|
214 |
|
215 |
-
|
216 |
|
217 |
-
|
218 |
|
219 |
-
|
220 |
|
221 |
-
|
222 |
-
|
223 |
-
|
|
|
|
|
|
|
224 |
|
225 |
-
|
|
|
|
|
|
|
226 |
|
227 |
-
|
|
|
228 |
|
229 |
-
|
230 |
-
maxtratio = max(source_durs[i], match_durs[i])
|
231 |
-
mintratio = min(source_durs[i], match_durs[i])
|
232 |
-
durs_ratios.append(mintratio / maxtratio)
|
233 |
|
234 |
-
|
235 |
|
236 |
-
|
237 |
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
m.sort(reverse = True, key = lambda x: x[1])
|
245 |
-
max_pitches_count = m[0][1]
|
246 |
-
trimmed_pitches_counts = [y for y in m if y[1] >= (max_pitches_count * pitches_counts_cutoff_threshold_ratio)]
|
247 |
-
total_pitches_counts = sum([y[1] for y in trimmed_pitches_counts])
|
248 |
-
|
249 |
-
same_pitches = set([T[0] for T in trimmed_p_counts]) & set([m[0] for m in trimmed_pitches_counts])
|
250 |
-
num_same_pitches = len(same_pitches)
|
251 |
-
|
252 |
-
if num_same_pitches == len(trimmed_pitches_counts):
|
253 |
-
same_pitches_ratio = (num_same_pitches / len(trimmed_p_counts))
|
254 |
-
else:
|
255 |
-
same_pitches_ratio = (num_same_pitches / max(len(trimmed_p_counts), len(trimmed_pitches_counts)))
|
256 |
-
|
257 |
-
if skip_exact_matches:
|
258 |
-
if same_pitches_ratio == 1:
|
259 |
-
same_pitches_ratio = 0
|
260 |
-
|
261 |
-
sprat.append(same_pitches_ratio)
|
262 |
-
|
263 |
-
#===================================================
|
264 |
-
|
265 |
-
spcrat = [0]
|
266 |
-
|
267 |
-
if add_pitches_counts_ratios:
|
268 |
-
|
269 |
-
same_trimmed_p_counts = sorted([T for T in trimmed_p_counts if T[0] in same_pitches], reverse = True)
|
270 |
-
same_trimmed_pitches_counts = sorted([T for T in trimmed_pitches_counts if T[0] in same_pitches], reverse = True)
|
271 |
-
|
272 |
-
same_trimmed_p_counts_ratios = [[s[0], s[1] / total_p_counts] for s in same_trimmed_p_counts]
|
273 |
-
same_trimmed_pitches_counts_ratios = [[s[0], s[1] / total_pitches_counts] for s in same_trimmed_pitches_counts]
|
274 |
-
|
275 |
-
same_pitches_counts_ratios = []
|
276 |
-
|
277 |
-
for i in range(len(same_trimmed_p_counts_ratios)):
|
278 |
-
mincratio = min(same_trimmed_p_counts_ratios[i][1], same_trimmed_pitches_counts_ratios[i][1])
|
279 |
-
maxcratio = max(same_trimmed_p_counts_ratios[i][1], same_trimmed_pitches_counts_ratios[i][1])
|
280 |
-
same_pitches_counts_ratios.append([same_trimmed_p_counts_ratios[i][0], mincratio / maxcratio])
|
281 |
-
|
282 |
-
same_counts_ratios = [s[1] for s in same_pitches_counts_ratios]
|
283 |
-
|
284 |
-
if len(same_counts_ratios) > 0:
|
285 |
-
avg_same_pitches_counts_ratio = sum(same_counts_ratios) / len(same_counts_ratios)
|
286 |
-
else:
|
287 |
-
avg_same_pitches_counts_ratio = 0
|
288 |
-
|
289 |
-
spcrat[0] = avg_same_pitches_counts_ratio
|
290 |
-
|
291 |
-
#===================================================
|
292 |
-
|
293 |
-
r_list = [sprat[0]]
|
294 |
-
|
295 |
-
if add_pitches_counts_ratios:
|
296 |
-
r_list.append(spcrat[0])
|
297 |
-
|
298 |
-
if add_timings_ratios:
|
299 |
-
r_list.append(atrat[0])
|
300 |
-
|
301 |
-
if add_durations_ratios:
|
302 |
-
r_list.append(adrat[0])
|
303 |
-
|
304 |
-
ratios_list.append(r_list)
|
305 |
-
|
306 |
-
#===================================================
|
307 |
-
|
308 |
-
avg_ratios_list = []
|
309 |
-
|
310 |
-
for r in ratios_list:
|
311 |
-
avg_ratios_list.append(sum(r) / len(r))
|
312 |
-
|
313 |
-
#===================================================
|
314 |
-
|
315 |
-
final_ratio = max(avg_ratios_list)
|
316 |
-
|
317 |
-
if final_ratio > maximum_match_ratio_to_search_for:
|
318 |
-
final_ratio = 0
|
319 |
-
|
320 |
-
final_ratios.append(final_ratio)
|
321 |
-
|
322 |
-
#===================================================
|
323 |
-
|
324 |
-
max_ratio = max(final_ratios)
|
325 |
-
max_ratio_index = final_ratios.index(max_ratio)
|
326 |
-
|
327 |
-
print('FOUND')
|
328 |
-
print('=' * 70)
|
329 |
-
print('Match ratio', max_ratio)
|
330 |
-
print('MIDI file name', meta_data[max_ratio_index][0])
|
331 |
-
print('=' * 70)
|
332 |
-
|
333 |
-
fn = meta_data[max_ratio_index][0]
|
334 |
-
|
335 |
-
#==========================================================================================================
|
336 |
-
|
337 |
-
md = meta_data[max_ratio_index]
|
338 |
-
|
339 |
-
mid_seq = md[1][17:-1]
|
340 |
-
mid_seq_ticks = md[1][16][1]
|
341 |
-
mdata = md[1][:16]
|
342 |
-
|
343 |
-
txt_mdata = ''
|
344 |
|
345 |
-
txt_mdata += '==============================================================' + chr(10)
|
346 |
-
txt_mdata += 'MIDI MATCH RATIO: ' + str(max_ratio) + chr(10)
|
347 |
-
txt_mdata += '==============================================================' + chr(10)
|
348 |
-
txt_mdata += 'MIDI MATCH MD5 HASH: ' + str(fn) + chr(10)
|
349 |
-
txt_mdata += '==============================================================' + chr(10)
|
350 |
-
|
351 |
-
for m in mdata:
|
352 |
-
txt_mdata += str(m[0]) + ': ' + str(m[1])
|
353 |
-
txt_mdata += chr(10)
|
354 |
-
|
355 |
-
txt_mdata += '==============================================================' + chr(10)
|
356 |
-
|
357 |
-
for m in [d for d in md[1][16:] if d[0] != 'note']:
|
358 |
-
txt_mdata += str(m)
|
359 |
-
txt_mdata += chr(10)
|
360 |
-
|
361 |
-
txt_mdata += '==============================================================' + chr(10)
|
362 |
-
txt_mdata += 'MIDI MATCH RATIO: ' + str(max_ratio) + chr(10)
|
363 |
-
txt_mdata += '==============================================================' + chr(10)
|
364 |
-
txt_mdata += 'MIDI MATCH MD5 HASH: ' + str(fn) + chr(10)
|
365 |
-
txt_mdata += '==============================================================' + chr(10)
|
366 |
|
367 |
x = []
|
368 |
y = []
|
@@ -425,9 +185,8 @@ if __name__ == "__main__":
|
|
425 |
|
426 |
gr.Markdown("# Upload MIDI")
|
427 |
|
428 |
-
|
429 |
-
|
430 |
-
input_midi = gr.File(label="Input MIDI", file_types=[".midi", ".mid", ".kar"], type="binary")
|
431 |
|
432 |
gr.Markdown("# Match results")
|
433 |
|
|
|
26 |
|
27 |
#==========================================================================================================
|
28 |
|
29 |
+
def find_midi(title, artist):
|
30 |
|
31 |
print('=' * 70)
|
32 |
print('Loading MIDI file...')
|
33 |
|
34 |
#==================================================
|
35 |
|
36 |
+
print('Searching titles...Please wait...')
|
37 |
+
random.shuffle(AUX_DATA)
|
38 |
|
39 |
+
titles_index = []
|
40 |
|
41 |
+
for A in AUX_DATA:
|
42 |
+
titles_index.append(A[0])
|
43 |
|
44 |
+
search_string = ''
|
45 |
|
46 |
+
if enter_desired_song_title != '' and enter_desired_artist != '':
|
47 |
+
search_string = enter_desired_song_title + ' --- ' + enter_desired_artist
|
|
|
|
|
|
|
|
|
48 |
|
49 |
+
else:
|
50 |
+
search_string = enter_desired_song_title + enter_desired_artist
|
51 |
|
52 |
+
search_match = process.extract(query=search_string, choices=titles_index, limit=1)
|
53 |
+
search_index = titles_index.index(search_match[0][0])
|
54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
print('Done!')
|
56 |
print('=' * 70)
|
57 |
+
print('Selected title:', AUX_DATA[search_index][0])
|
58 |
+
print('=' * 70)
|
59 |
|
60 |
+
outy = AUX_DATA[search_index][1]
|
61 |
|
62 |
+
print('Sample INTs', outy[:12])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
print('=' * 70)
|
64 |
|
65 |
+
if len(outy) != 0:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
+
song = outy
|
68 |
+
song_f = []
|
69 |
|
70 |
+
time = 0
|
71 |
+
dur = 0
|
72 |
+
vel = 90
|
73 |
+
pitch = 0
|
74 |
+
channel = 0
|
75 |
|
76 |
+
patches = [-1] * 16
|
77 |
|
78 |
+
channels = [0] * 16
|
79 |
+
channels[9] = 1
|
|
|
80 |
|
81 |
+
for ss in song:
|
82 |
|
83 |
+
if 0 <= ss < 256:
|
84 |
|
85 |
+
time += ss * 16
|
|
|
|
|
|
|
86 |
|
87 |
+
if 256 <= ss < 2304:
|
88 |
|
89 |
+
dur = ((ss-256) // 8) * 16
|
90 |
+
vel = (((ss-256) % 8)+1) * 15
|
91 |
|
92 |
+
if 2304 <= ss < 18945:
|
93 |
|
94 |
+
patch = (ss-2304) // 129
|
95 |
|
96 |
+
if patch < 128:
|
97 |
|
98 |
+
if patch not in patches:
|
99 |
+
if 0 in channels:
|
100 |
+
cha = channels.index(0)
|
101 |
+
channels[cha] = 1
|
102 |
+
else:
|
103 |
+
cha = 15
|
104 |
|
105 |
+
patches[cha] = patch
|
106 |
+
channel = patches.index(patch)
|
107 |
+
else:
|
108 |
+
channel = patches.index(patch)
|
109 |
|
110 |
+
if patch == 128:
|
111 |
+
channel = 9
|
112 |
|
113 |
+
pitch = (ss-2304) % 129
|
|
|
|
|
|
|
114 |
|
115 |
+
song_f.append(['note', time, dur, channel, pitch, vel, patch ])
|
116 |
|
117 |
+
patches = [0 if x==-1 else x for x in patches]
|
118 |
|
119 |
+
detailed_stats = TMIDIX.Tegridy_ms_SONG_to_MIDI_Converter(song_f,
|
120 |
+
output_signature = 'Giant Music Transformer',
|
121 |
+
output_file_name = '/content/Giant-Music-Transformer-Music-Composition_'+str(i),
|
122 |
+
track_name='Project Los Angeles',
|
123 |
+
list_of_MIDI_patches=patches
|
124 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
|
127 |
x = []
|
128 |
y = []
|
|
|
185 |
|
186 |
gr.Markdown("# Upload MIDI")
|
187 |
|
188 |
+
artist = gr.Textbox()
|
189 |
+
title = gr.Textbox()
|
|
|
190 |
|
191 |
gr.Markdown("# Match results")
|
192 |
|