svjack commited on
Commit
4e42549
·
verified ·
1 Parent(s): 88d9a32

Create demo_app.py

Browse files
Files changed (1) hide show
  1. demo_app.py +411 -0
demo_app.py ADDED
@@ -0,0 +1,411 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import hashlib
3
+ import time
4
+ import datetime
5
+ from pytz import timezone
6
+ import copy
7
+ from collections import Counter
8
+ import random
9
+ import statistics
10
+ import gradio as gr
11
+ import TMIDIX
12
+ import TPLOTS
13
+ from midi_to_colab_audio import midi_to_colab_audio
14
+
15
+ #==========================================================================================================
16
+
17
+ def Render_MIDI(input_midi,
18
+ render_type,
19
+ soundfont_bank,
20
+ render_sample_rate,
21
+ custom_render_patch,
22
+ render_align,
23
+ render_transpose_value,
24
+ render_transpose_to_C4,
25
+ render_output_as_solo_piano,
26
+ render_remove_drums
27
+ ):
28
+
29
+ print('*' * 70)
30
+ print('Req start time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
31
+ start_time = time.time()
32
+
33
+ print('=' * 70)
34
+ print('Loading MIDI...')
35
+
36
+ fn = os.path.basename(input_midi)
37
+ fn1 = fn.split('.')[0]
38
+
39
+ fdata = open(input_midi, 'rb').read()
40
+
41
+ input_midi_md5hash = hashlib.md5(fdata).hexdigest()
42
+
43
+ print('=' * 70)
44
+ print('Requested settings:')
45
+ print('=' * 70)
46
+ print('Input MIDI file name:', fn)
47
+ print('Input MIDI md5 hash', input_midi_md5hash)
48
+ print('-' * 70)
49
+ print('Render type:', render_type)
50
+ print('Soudnfont bank', soundfont_bank)
51
+ print('Audio render sample rate', render_sample_rate)
52
+ print('Custom MIDI render patch', custom_render_patch)
53
+ print('Align to bars:', render_align)
54
+ print('Transpose value:', render_transpose_value)
55
+ print('Transpose to C4', render_transpose_to_C4)
56
+ print('Output as Solo Piano', render_output_as_solo_piano)
57
+ print('Remove drums:', render_remove_drums)
58
+ print('=' * 70)
59
+ print('Processing MIDI...Please wait...')
60
+
61
+ #=======================================================
62
+ # START PROCESSING
63
+
64
+ raw_score = TMIDIX.midi2single_track_ms_score(fdata)
65
+
66
+ escore = TMIDIX.advanced_score_processor(raw_score, return_enhanced_score_notes=True)[0]
67
+
68
+ escore = TMIDIX.augment_enhanced_score_notes(escore, timings_divider=1)
69
+
70
+ first_note_index = [e[0] for e in raw_score[1]].index('note')
71
+
72
+ cscore = TMIDIX.chordify_score([1000, escore])
73
+
74
+ meta_data = raw_score[1][:first_note_index] + [escore[0]] + [escore[-1]] + [raw_score[1][-1]]
75
+
76
+ aux_escore_notes = TMIDIX.augment_enhanced_score_notes(escore, sort_drums_last=True)
77
+ song_description = TMIDIX.escore_notes_to_text_description(aux_escore_notes)
78
+
79
+ print('Done!')
80
+ print('=' * 70)
81
+ print('Input MIDI metadata:', meta_data[:5])
82
+ print('=' * 70)
83
+ print('Input MIDI song description:', song_description)
84
+ print('=' * 70)
85
+ print('Processing...Please wait...')
86
+
87
+ output_score = copy.deepcopy(escore)
88
+
89
+ if render_type == "Extract melody":
90
+ output_score = TMIDIX.add_melody_to_enhanced_score_notes(escore, return_melody=True)
91
+ output_score = TMIDIX.recalculate_score_timings(output_score)
92
+
93
+ elif render_type == "Flip":
94
+ output_score = TMIDIX.flip_enhanced_score_notes(escore)
95
+
96
+ elif render_type == "Reverse":
97
+ output_score = TMIDIX.reverse_enhanced_score_notes(escore)
98
+
99
+ elif render_type == 'Repair Chords':
100
+ fixed_cscore = TMIDIX.advanced_check_and_fix_chords_in_chordified_score(cscore)[0]
101
+ output_score = TMIDIX.flatten(fixed_cscore)
102
+
103
+ elif render_type == "Add Drums Track":
104
+ nd_escore = [e for e in escore if e[3] != 9]
105
+ nd_escore = TMIDIX.augment_enhanced_score_notes(nd_escore)
106
+ output_score = TMIDIX.advanced_add_drums_to_escore_notes(nd_escore)
107
+
108
+ for e in output_score:
109
+ e[1] *= 16
110
+ e[2] *= 16
111
+
112
+ print('Done processing!')
113
+ print('=' * 70)
114
+
115
+ print('Repatching if needed...')
116
+ print('=' * 70)
117
+
118
+ if -1 < custom_render_patch < 128:
119
+ for e in output_score:
120
+ if e[3] != 9:
121
+ e[6] = custom_render_patch
122
+
123
+ print('Done repatching!')
124
+ print('=' * 70)
125
+
126
+ print('Sample output events', output_score[:5])
127
+ print('=' * 70)
128
+ print('Final processing...')
129
+
130
+ new_fn = fn1+'.mid'
131
+
132
+ if render_type != "Render as-is":
133
+
134
+ if render_transpose_value != 0:
135
+ output_score = TMIDIX.transpose_escore_notes(output_score, render_transpose_value)
136
+
137
+ if render_transpose_to_C4:
138
+ output_score = TMIDIX.transpose_escore_notes_to_pitch(output_score)
139
+
140
+ if render_align == "Start Times":
141
+ output_score = TMIDIX.recalculate_score_timings(output_score)
142
+ output_score = TMIDIX.align_escore_notes_to_bars(output_score)
143
+
144
+ elif render_align == "Start Times and Durations":
145
+ output_score = TMIDIX.recalculate_score_timings(output_score)
146
+ output_score = TMIDIX.align_escore_notes_to_bars(output_score, trim_durations=True)
147
+
148
+ elif render_align == "Start Times and Split Durations":
149
+ output_score = TMIDIX.recalculate_score_timings(output_score)
150
+ output_score = TMIDIX.align_escore_notes_to_bars(output_score, split_durations=True)
151
+
152
+ if render_type == "Longest Repeating Phrase":
153
+ zscore = TMIDIX.recalculate_score_timings(output_score)
154
+ lrno_score = TMIDIX.escore_notes_lrno_pattern_fast(zscore)
155
+
156
+ if lrno_score is not None:
157
+ output_score = lrno_score
158
+
159
+ else:
160
+ output_score = TMIDIX.recalculate_score_timings(TMIDIX.escore_notes_middle(output_score, 50))
161
+
162
+ if render_type == "Multi-Instrumental Summary":
163
+ zscore = TMIDIX.recalculate_score_timings(output_score)
164
+ c_escore_notes = TMIDIX.compress_patches_in_escore_notes_chords(zscore)
165
+
166
+ if len(c_escore_notes) > 128:
167
+ cmatrix = TMIDIX.escore_notes_to_image_matrix(c_escore_notes, filter_out_zero_rows=True, filter_out_duplicate_rows=True)
168
+ smatrix = TPLOTS.square_image_matrix(cmatrix, num_pca_components=max(1, min(5, len(c_escore_notes) // 128)))
169
+ output_score = TMIDIX.image_matrix_to_original_escore_notes(smatrix)
170
+
171
+ for o in output_score:
172
+ o[1] *= 250
173
+ o[2] *= 250
174
+
175
+ if render_output_as_solo_piano:
176
+ output_score = TMIDIX.solo_piano_escore_notes(output_score, keep_drums=True)
177
+
178
+ if render_remove_drums:
179
+ output_score = TMIDIX.strip_drums_from_escore_notes(output_score)
180
+
181
+ if render_type == "Solo Piano Summary":
182
+ sp_escore_notes = TMIDIX.solo_piano_escore_notes(output_score, keep_drums=False)
183
+ zscore = TMIDIX.recalculate_score_timings(sp_escore_notes)
184
+
185
+ if len(zscore) > 128:
186
+
187
+ bmatrix = TMIDIX.escore_notes_to_binary_matrix(zscore)
188
+ cmatrix = TMIDIX.compress_binary_matrix(bmatrix, only_compress_zeros=True)
189
+ smatrix = TPLOTS.square_binary_matrix(cmatrix, interpolation_order=max(1, min(5, len(zscore) // 128)))
190
+ output_score = TMIDIX.binary_matrix_to_original_escore_notes(smatrix)
191
+
192
+ for o in output_score:
193
+ o[1] *= 200
194
+ o[2] *= 200
195
+
196
+ SONG, patches, overflow_patches = TMIDIX.patch_enhanced_score_notes(output_score)
197
+
198
+ detailed_stats = TMIDIX.Tegridy_ms_SONG_to_MIDI_Converter(SONG,
199
+ output_signature = 'Advanced MIDI Renderer',
200
+ output_file_name = fn1,
201
+ track_name='Project Los Angeles',
202
+ list_of_MIDI_patches=patches
203
+ )
204
+
205
+ else:
206
+ with open(new_fn, 'wb') as f:
207
+ f.write(fdata)
208
+ f.close()
209
+
210
+ if soundfont_bank in ["Super GM",
211
+ "Orpheus GM",
212
+ "Live HQ GM",
213
+ "Nice Strings + Orchestra",
214
+ "Real Choir",
215
+ "Super Game Boy",
216
+ "Proto Square"
217
+ ]:
218
+
219
+ sf2bank = ["Super GM",
220
+ "Orpheus GM",
221
+ "Live HQ GM",
222
+ "Nice Strings + Orchestra",
223
+ "Real Choir",
224
+ "Super Game Boy",
225
+ "Proto Square"
226
+ ].index(soundfont_bank)
227
+
228
+ else:
229
+ sf2bank = 0
230
+
231
+ if render_sample_rate in ["16000", "32000", "44100"]:
232
+ srate = int(render_sample_rate)
233
+
234
+ else:
235
+ srate = 16000
236
+
237
+ print('-' * 70)
238
+ print('Generating audio with SF2 bank', sf2bank, 'and', srate, 'Hz sample rate')
239
+
240
+ audio = midi_to_colab_audio(new_fn,
241
+ soundfont_path=soundfonts[sf2bank],
242
+ sample_rate=srate,
243
+ volume_scale=10,
244
+ output_for_gradio=True
245
+ )
246
+
247
+ print('-' * 70)
248
+
249
+ new_md5_hash = hashlib.md5(open(new_fn,'rb').read()).hexdigest()
250
+
251
+ print('Done!')
252
+ print('=' * 70)
253
+
254
+ #========================================================
255
+
256
+ output_midi_md5 = str(new_md5_hash)
257
+ output_midi_title = str(fn1)
258
+ output_midi_summary = str(meta_data)
259
+ output_midi = str(new_fn)
260
+ output_audio = (srate, audio)
261
+
262
+ output_plot = TMIDIX.plot_ms_SONG(output_score, plot_title=output_midi, return_plt=True)
263
+
264
+ print('Output MIDI file name:', output_midi)
265
+ print('Output MIDI title:', output_midi_title)
266
+ print('Output MIDI hash:', output_midi_md5)
267
+ print('Output MIDI summary:', output_midi_summary[:5])
268
+ print('=' * 70)
269
+
270
+ #========================================================
271
+
272
+ print('Req end time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
273
+ print('-' * 70)
274
+ print('Req execution time:', (time.time() - start_time), 'sec')
275
+ print('*' * 70)
276
+
277
+ #========================================================
278
+
279
+ return output_midi_md5, output_midi_title, output_midi_summary, output_midi, output_audio, output_plot, song_description
280
+
281
+ #==========================================================================================================
282
+
283
+ if __name__ == "__main__":
284
+
285
+ PDT = timezone('US/Pacific')
286
+
287
+ print('=' * 70)
288
+ print('App start time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
289
+ print('=' * 70)
290
+
291
+ soundfonts = ["SGM-v2.01-YamahaGrand-Guit-Bass-v2.7.sf2",
292
+ "Orpheus_18.06.2020.sf2",
293
+ "Live HQ Natural SoundFont GM.sf2",
294
+ "Nice-Strings-PlusOrchestra-v1.6.sf2",
295
+ "KBH-Real-Choir-V2.5.sf2",
296
+ "SuperGameBoy.sf2",
297
+ "ProtoSquare.sf2"
298
+ ]
299
+
300
+ # Find all .mid files in the current directory
301
+ midi_examples = [f for f in os.listdir() if f.endswith('.mid')]
302
+
303
+ app = gr.Blocks()
304
+
305
+ with app:
306
+
307
+ gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>Advanced MIDI Renderer</h1>")
308
+ gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>Transform and render any MIDI</h1>")
309
+
310
+ gr.Markdown("![Visitors](https://api.visitorbadge.io/api/visitors?path=asigalov61.Advanced-MIDI-Renderer&style=flat)\n\n"
311
+ "This is a demo for tegridy-tools\n\n"
312
+ "Please see [tegridy-tools](https://github.com/asigalov61/tegridy-tools) GitHub repo for more information\n\n"
313
+ )
314
+
315
+ gr.Markdown("## Upload your MIDI")
316
+
317
+ input_midi = gr.File(label="Input MIDI", file_types=[".midi", ".mid", ".kar"], type="filepath")
318
+
319
+ gr.Markdown("## Select desired Sound Font bank and render sample rate")
320
+
321
+ soundfont_bank = gr.Radio(["Super GM",
322
+ "Orpheus GM",
323
+ "Live HQ GM",
324
+ "Nice Strings + Orchestra",
325
+ "Real Choir",
326
+ "Super Game Boy",
327
+ "Proto Square"
328
+ ],
329
+ label="SoundFont bank",
330
+ value="Super GM"
331
+ )
332
+
333
+ render_sample_rate = gr.Radio(["16000",
334
+ "32000",
335
+ "44100"
336
+ ],
337
+ label="MIDI audio render sample rate",
338
+ value="16000"
339
+ )
340
+
341
+ gr.Markdown("## Select desired render type")
342
+
343
+ render_type = gr.Radio(["Render as-is",
344
+ "Custom render",
345
+ "Extract melody",
346
+ "Flip",
347
+ "Reverse",
348
+ "Repair Chords",
349
+ "Longest Repeating Phrase",
350
+ "Multi-Instrumental Summary",
351
+ "Solo Piano Summary",
352
+ "Add Drums Track"
353
+ ],
354
+ label="Render type",
355
+ value="Render as-is"
356
+ )
357
+
358
+ gr.Markdown("## Select custom render options")
359
+
360
+ custom_render_patch = gr.Slider(-1, 127, value=-1, label="Custom render MIDI patch")
361
+
362
+ render_align = gr.Radio(["Do not align",
363
+ "Start Times",
364
+ "Start Times and Durations",
365
+ "Start Times and Split Durations"
366
+ ],
367
+ label="Align output to bars",
368
+ value="Do not align"
369
+ )
370
+
371
+ render_transpose_value = gr.Slider(-12, 12, value=0, step=1, label="Transpose value")
372
+ render_transpose_to_C4 = gr.Checkbox(label="Transpose to C4", value=False)
373
+
374
+
375
+ render_output_as_solo_piano = gr.Checkbox(label="Output as Solo Piano", value=False)
376
+ render_remove_drums = gr.Checkbox(label="Remove drums", value=False)
377
+
378
+ submit = gr.Button("Render MIDI", variant="primary")
379
+
380
+ gr.Markdown("## Render results")
381
+
382
+ output_midi_md5 = gr.Textbox(label="Output MIDI md5 hash")
383
+ output_midi_title = gr.Textbox(label="Output MIDI title")
384
+ output_song_description = gr.Textbox(label="Output MIDI description")
385
+ output_midi_summary = gr.Textbox(label="Output MIDI summary")
386
+ output_audio = gr.Audio(label="Output MIDI audio", format="wav", elem_id="midi_audio")
387
+ output_plot = gr.Plot(label="Output MIDI score plot")
388
+ output_midi = gr.File(label="Output MIDI file", file_types=[".mid"])
389
+
390
+ run_event = submit.click(Render_MIDI, [input_midi,
391
+ render_type,
392
+ soundfont_bank,
393
+ render_sample_rate,
394
+ custom_render_patch,
395
+ render_align,
396
+ render_transpose_value,
397
+ render_transpose_to_C4,
398
+ render_output_as_solo_piano,
399
+ render_remove_drums
400
+ ],
401
+ [output_midi_md5,
402
+ output_midi_title,
403
+ output_midi_summary,
404
+ output_midi,
405
+ output_audio,
406
+ output_plot,
407
+ output_song_description
408
+ ])
409
+
410
+ # Add examples to the app
411
+ app.launch(share=True, examples=midi_examples)