File size: 16,362 Bytes
4299f7c
26312c5
4299f7c
b934418
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe6e100
b934418
53409b8
951967e
4543735
e94f552
660b28d
 
800d0d0
660b28d
b780647
 
 
 
 
3888ab7
 
a2c6cfa
e94f552
 
a2c6cfa
29090fa
6df655f
 
410f581
8b3de7f
 
 
 
e2a8b2b
8b3de7f
9ed302a
1424ad3
 
 
4543735
8d39a27
660b28d
 
1424ad3
660b28d
4543735
 
 
2a2c4ee
4543735
 
 
 
660b28d
1424ad3
 
6df655f
4543735
 
fe6e100
f025921
57440b4
f7f27b0
91a38ff
e2a8b2b
8b3de7f
 
1424ad3
 
4543735
 
281d949
4543735
 
 
2967e03
 
 
 
8ea7c59
afd56a5
1209231
2967e03
4543735
 
 
6df655f
 
 
abb25f7
cf7d19c
8d39a27
 
410f581
6df655f
410f581
7485e9b
ffdd828
b9703ed
d57ab6a
736f5d9
ecaa203
 
24957f5
ecaa203
8a5fb8f
1b6bdce
d57ab6a
6ccde9d
8d39a27
 
 
abb25f7
8d39a27
91a38ff
abb25f7
 
2ec1a72
 
8d39a27
abb25f7
8d39a27
 
 
 
 
 
2a2c4ee
6df655f
598a5c4
57440b4
8b3de7f
f0b4cee
8b3de7f
 
 
9ed302a
 
d46a61a
9ed302a
 
 
d46a61a
9ed302a
 
 
d46a61a
9ed302a
4299f7c
410f581
4801166
 
e2a8b2b
406db87
 
 
 
7a7d25f
406db87
410f581
cf187c8
 
 
9a96351
 
 
 
 
 
 
 
cf187c8
f4a049a
 
 
1424ad3
 
f4a049a
410f581
e94f552
 
9a96351
 
 
 
 
 
 
 
 
 
 
8b3de7f
abb25f7
598a5c4
abb25f7
598a5c4
 
 
9918a6a
598a5c4
 
 
 
87798b9
598a5c4
 
34fa13e
 
 
 
 
 
 
 
67e09bb
34fa13e
 
 
 
 
 
 
 
57440b4
 
 
f7f27b0
 
 
 
 
 
3858291
 
 
2a2c4ee
 
57440b4
f7f27b0
2a2c4ee
 
 
fd58682
3858291
 
fd58682
2a2c4ee
8d39a27
6df655f
91d5e8a
fd58682
 
 
 
 
 
f7f27b0
8a1f411
9918a6a
fd58682
8d39a27
 
 
abb25f7
8d39a27
 
fd58682
 
660b28d
 
 
8d39a27
1309ddb
fd58682
 
1309ddb
fd58682
6df655f
 
3888ab7
800d0d0
 
 
 
 
 
 
b67a428
 
 
32d3f4b
 
 
 
 
3888ab7
 
8b3de7f
3888ab7
1309ddb
a04859d
 
e30e402
a04859d
99fabfd
 
a04859d
99fabfd
d0f533f
 
008d8d4
d0f533f
25f0c87
99fabfd
b67a428
 
67e09bb
 
 
4299f7c
 
 
 
b67a428
4299f7c
 
 
 
 
 
 
 
 
f7f27b0
25f0c87
 
 
 
 
 
 
410f581
e1ba81d
410f581
 
25f0c87
 
 
 
 
 
 
8b3de7f
 
4299f7c
 
 
 
 
 
 
e2a8b2b
 
 
 
 
91a38ff
1424ad3
 
 
10245a2
3adc64e
d0f533f
660b28d
a04859d
 
 
4cdbeb8
a04859d
 
c04453c
410f581
8b3de7f
 
 
 
e2a8b2b
8b3de7f
9ed302a
1424ad3
e2a8b2b
8b3de7f
04301a1
 
 
 
 
 
 
d85cb0d
8b3de7f
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
#================================================================
# https://huggingface.co/spaces/asigalov61/Advanced-MIDI-Renderer
#================================================================
# Packages:
#
#   apt install fluidsynth
#================================================================
# Requirements:
#   
#   pip install gradio
#   pip install numpy
#   pip install scipy
#   pip install matplotlib
#   pip install networkx
#   pip install scikit-learn
#================================================================
# Core modules:
#
# git clone --depth 1 https://github.com/asigalov61/tegridy-tools
#
# import TMIDIX
# import TPLOTS
# import midi_to_colab_audio
#================================================================

import os
import hashlib

import time
import datetime
from pytz import timezone

import copy
from collections import Counter
import random
import statistics

import gradio as gr

import TMIDIX
import TPLOTS

from midi_to_colab_audio import midi_to_colab_audio

#==========================================================================================================

def Render_MIDI(input_midi, 
                render_type, 
                soundfont_bank, 
                render_sample_rate, 
                custom_render_patch,
                render_align,
                render_transpose_value,
                render_transpose_to_C4,
                render_output_as_solo_piano,
                render_remove_drums 
                ):
    
    print('*' * 70)
    print('Req start time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
    start_time = time.time()
    
    print('=' * 70)
    print('Loading MIDI...')

    fn = os.path.basename(input_midi)
    fn1 = fn.split('.')[0]

    fdata = open(input_midi, 'rb').read()

    input_midi_md5hash = hashlib.md5(fdata).hexdigest()
    
    print('=' * 70)
    print('Requested settings:')
    print('=' * 70)
    print('Input MIDI file name:', fn)
    print('Input MIDI md5 hash', input_midi_md5hash)
    print('-' * 70)
    print('Render type:', render_type)
    print('Soudnfont bank', soundfont_bank)
    print('Audio render sample rate', render_sample_rate)
    print('Custom MIDI render patch', custom_render_patch)
    print('Align to bars:', render_align)
    print('Transpose value:', render_transpose_value)
    print('Transpose to C4', render_transpose_to_C4)
    print('Output as Solo Piano', render_output_as_solo_piano)
    print('Remove drums:', render_remove_drums)
    print('=' * 70)
    print('Processing MIDI...Please wait...')
    
    #=======================================================
    # START PROCESSING

    raw_score = TMIDIX.midi2single_track_ms_score(fdata)
    
    escore = TMIDIX.advanced_score_processor(raw_score, return_enhanced_score_notes=True)[0]
    
    escore = TMIDIX.augment_enhanced_score_notes(escore, timings_divider=1)

    first_note_index = [e[0] for e in raw_score[1]].index('note')
    
    cscore = TMIDIX.chordify_score([1000, escore])

    meta_data = raw_score[1][:first_note_index] + [escore[0]] + [escore[-1]] + [raw_score[1][-1]]
    
    print('Done!')
    print('=' * 70)
    print('Input MIDI metadata:', meta_data[:5])
    print('=' * 70)
    print('Processing...Please wait...')

    output_score = copy.deepcopy(escore)

    if render_type == "Extract melody":
        output_score = TMIDIX.add_melody_to_enhanced_score_notes(escore, return_melody=True)
        output_score = TMIDIX.recalculate_score_timings(output_score)

    elif render_type == "Flip":
        output_score = TMIDIX.flip_enhanced_score_notes(escore)
        
    elif render_type == "Reverse":
        output_score = TMIDIX.reverse_enhanced_score_notes(escore)
        
    elif render_type == 'Repair Chords':
        fixed_cscore = TMIDIX.advanced_check_and_fix_chords_in_chordified_score(cscore)[0]
        output_score = TMIDIX.flatten(fixed_cscore)

    print('Done processing!')
    print('=' * 70)
    
    print('Repatching if needed...')
    print('=' * 70)

    if -1 < custom_render_patch < 128:
        for e in output_score:
            if e[3] != 9:
                e[6] = custom_render_patch
        
    print('Done repatching!')
    print('=' * 70)
    
    print('Sample output events', output_score[:5])
    print('=' * 70)
    print('Final processing...')
    
    new_fn = fn1+'.mid'

    if render_type != "Render as-is":

        if render_transpose_value != 0:
            output_score = TMIDIX.transpose_escore_notes(output_score, render_transpose_value)

        if render_transpose_to_C4:
            output_score = TMIDIX.transpose_escore_notes_to_pitch(output_score)

        if render_align == "Start Times":
            output_score = TMIDIX.recalculate_score_timings(output_score)
            output_score = TMIDIX.align_escore_notes_to_bars(output_score)
    
        elif render_align == "Start Times and Durations":
            output_score = TMIDIX.recalculate_score_timings(output_score)
            output_score = TMIDIX.align_escore_notes_to_bars(output_score, trim_durations=True)
    
        elif render_align == "Start Times and Split Durations":
            output_score = TMIDIX.recalculate_score_timings(output_score)
            output_score = TMIDIX.align_escore_notes_to_bars(output_score, split_durations=True)

        if render_type == "Longest Repeating Phrase":
            zscore = TMIDIX.recalculate_score_timings(output_score)
            lrno_score = TMIDIX.escore_notes_lrno_pattern(zscore)

            if lrno_score is not None:
                output_score = TMIDIX.recalculate_score_timings(lrno_score)

            else:
                output_score = TMIDIX.recalculate_score_timings(TMIDIX.escore_notes_middle(output_score, 50))

        if render_type == "Multi-Instrumental Summary":
            zscore = TMIDIX.recalculate_score_timings(output_score)
            c_escore_notes = TMIDIX.compress_patches_in_escore_notes_chords(zscore)
            
            if len(c_escore_notes) > 128:
                cmatrix = TMIDIX.escore_notes_to_image_matrix(c_escore_notes, filter_out_zero_rows=True, filter_out_duplicate_rows=True)
                smatrix = TPLOTS.square_image_matrix(cmatrix)
                output_score = TMIDIX.image_matrix_to_original_escore_notes(smatrix)
                
                for o in output_score:
                    o[1] *= 250
                    o[2] *= 250            

        if render_output_as_solo_piano:
            output_score = TMIDIX.solo_piano_escore_notes(output_score, keep_drums=True)
            
        if render_remove_drums:
            output_score = TMIDIX.strip_drums_from_escore_notes(output_score)
            
        if render_type == "Solo Piano Summary":
            sp_escore_notes = TMIDIX.solo_piano_escore_notes(output_score, keep_drums=False)
            zscore = TMIDIX.recalculate_score_timings(sp_escore_notes)

            if len(zscore) > 128:
                
                bmatrix = TMIDIX.escore_notes_to_binary_matrix(zscore)
                cmatrix = TMIDIX.compress_binary_matrix(bmatrix, only_compress_zeros=True)
                smatrix = TPLOTS.square_binary_matrix(cmatrix)
                output_score = TMIDIX.binary_matrix_to_original_escore_notes(smatrix)
    
                for o in output_score:
                    o[1] *= 200
                    o[2] *= 200
            
        SONG, patches, overflow_patches = TMIDIX.patch_enhanced_score_notes(output_score)
                    
        detailed_stats = TMIDIX.Tegridy_ms_SONG_to_MIDI_Converter(SONG,
                                                                  output_signature = 'Advanced MIDI Renderer',
                                                                  output_file_name = fn1,
                                                                  track_name='Project Los Angeles',
                                                                  list_of_MIDI_patches=patches
                                                                  )

    else:
        with open(new_fn, 'wb') as f:
            f.write(fdata)
            f.close()
            
    if soundfont_bank in ["Super GM",
                            "Orpheus GM",
                            "Live HQ GM",
                            "Nice Strings + Orchestra", 
                            "Real Choir", 
                            "Super Game Boy", 
                            "Proto Square"
                            ]:
        
        sf2bank = ["Super GM",
                    "Orpheus GM",
                    "Live HQ GM",
                    "Nice Strings + Orchestra", 
                    "Real Choir", 
                    "Super Game Boy", 
                    "Proto Square"
                    ].index(soundfont_bank)
    
    else:
        sf2bank = 0

    if render_sample_rate in ["16000", "32000", "44100"]:
        srate = int(render_sample_rate)
    
    else:
        srate = 16000

    print('-' * 70)
    print('Generating audio with SF2 bank', sf2bank, 'and', srate, 'Hz sample rate')
    
    audio = midi_to_colab_audio(new_fn, 
                        soundfont_path=soundfonts[sf2bank],
                        sample_rate=srate,
                        volume_scale=10,
                        output_for_gradio=True
                        )

    print('-' * 70)

    new_md5_hash = hashlib.md5(open(new_fn,'rb').read()).hexdigest()
    
    print('Done!')
    print('=' * 70)

    #========================================================

    output_midi_md5 = str(new_md5_hash)
    output_midi_title = str(fn1)
    output_midi_summary = str(meta_data)
    output_midi = str(new_fn)
    output_audio = (srate, audio)
    
    output_plot = TMIDIX.plot_ms_SONG(output_score, plot_title=output_midi, return_plt=True)

    print('Output MIDI file name:', output_midi)
    print('Output MIDI title:', output_midi_title)
    print('Output MIDI hash:', output_midi_md5)
    print('Output MIDI summary:', output_midi_summary[:5])
    print('=' * 70) 
    
    #========================================================

    print('Req end time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
    print('-' * 70)
    print('Req execution time:', (time.time() - start_time), 'sec')
    print('*' * 70)
    
    #========================================================
    
    return output_midi_md5, output_midi_title, output_midi_summary, output_midi, output_audio, output_plot
    
#==========================================================================================================

if __name__ == "__main__":

    PDT = timezone('US/Pacific')
    
    print('=' * 70)
    print('App start time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
    print('=' * 70)

    soundfonts = ["SGM-v2.01-YamahaGrand-Guit-Bass-v2.7.sf2",
                  "Orpheus_18.06.2020.sf2",
                  "Live HQ Natural SoundFont GM.sf2",
                  "Nice-Strings-PlusOrchestra-v1.6.sf2", 
                  "KBH-Real-Choir-V2.5.sf2", 
                  "SuperGameBoy.sf2", 
                  "ProtoSquare.sf2"
                 ]

    app = gr.Blocks()
    
    with app:
        
        gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>Advanced MIDI Renderer</h1>")
        gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>Transform and render any MIDI</h1>")
        
        gr.Markdown("![Visitors](https://api.visitorbadge.io/api/visitors?path=asigalov61.Advanced-MIDI-Renderer&style=flat)\n\n"
                    "This is a demo for tegridy-tools\n\n"
                    "Please see [tegridy-tools](https://github.com/asigalov61/tegridy-tools) GitHub repo for more information\n\n"
                   )
        
        gr.Markdown("## Upload your MIDI")
        
        input_midi = gr.File(label="Input MIDI", file_types=[".midi", ".mid", ".kar"], type="filepath")

        gr.Markdown("## Select desired Sound Font bank and render sample rate")

        soundfont_bank = gr.Radio(["Super GM",
                                   "Orpheus GM",
                                   "Live HQ GM",
                                   "Nice Strings + Orchestra", 
                                   "Real Choir", 
                                   "Super Game Boy", 
                                   "Proto Square"
                                  ], 
                                  label="SoundFont bank", 
                                  value="Super GM"
                                 )

        render_sample_rate = gr.Radio(["16000", 
                                       "32000", 
                                       "44100"
                                      ], 
                                      label="MIDI audio render sample rate", 
                                      value="16000"
                                     )

        gr.Markdown("## Select desired render type")

        render_type = gr.Radio(["Render as-is", 
                                "Custom render", 
                                "Extract melody", 
                                "Flip", 
                                "Reverse", 
                                "Repair Chords",
                                "Longest Repeating Phrase",
                                "Multi-Instrumental Summary",
                                "Solo Piano Summary"
                               ], 
                               label="Render type", 
                               value="Render as-is"
                              )

        gr.Markdown("## Select custom render options")

        custom_render_patch = gr.Slider(-1, 127, value=-1, label="Custom render MIDI patch")
        
        render_align = gr.Radio(["Do not align", 
                                 "Start Times", 
                                 "Start Times and Durations", 
                                 "Start Times and Split Durations"
                                ], 
                                label="Align output to bars", 
                                value="Do not align"
                               )        
        
        render_transpose_value = gr.Slider(-12, 12, value=0, step=1, label="Transpose value")
        render_transpose_to_C4 = gr.Checkbox(label="Transpose to C4", value=False)


        render_output_as_solo_piano = gr.Checkbox(label="Output as Solo Piano", value=False)
        render_remove_drums = gr.Checkbox(label="Remove drums", value=False)
        
        submit = gr.Button("Render MIDI", variant="primary")

        gr.Markdown("## Render results")
        
        output_midi_md5 = gr.Textbox(label="Output MIDI md5 hash")
        output_midi_title = gr.Textbox(label="Output MIDI title")
        output_midi_summary = gr.Textbox(label="Output MIDI summary")
        output_audio = gr.Audio(label="Output MIDI audio", format="wav", elem_id="midi_audio")
        output_plot = gr.Plot(label="Output MIDI score plot")
        output_midi = gr.File(label="Output MIDI file", file_types=[".mid"])
        
        run_event = submit.click(Render_MIDI, [input_midi, 
                                               render_type, 
                                               soundfont_bank, 
                                               render_sample_rate, 
                                               custom_render_patch,
                                               render_align,
                                               render_transpose_value,
                                               render_transpose_to_C4,
                                               render_output_as_solo_piano,
                                               render_remove_drums                                              
                                              ],
                                                [output_midi_md5, 
                                                 output_midi_title, 
                                                 output_midi_summary, 
                                                 output_midi, 
                                                 output_audio, 
                                                 output_plot
                                                ])
        
    app.queue().launch()