File size: 15,203 Bytes
87a12d2
1f0244d
87a12d2
e3ed184
 
 
 
 
 
ce76c69
bedc750
b621c94
 
e3ed184
 
 
45454da
e3ed184
 
 
 
 
45454da
e3ed184
e06ca4c
 
38a3645
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e3ed184
f78fa12
3d26bbd
f78fa12
554bff4
 
f78fa12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87a12d2
 
e3ed184
 
e06ca4c
554bff4
 
 
 
 
 
 
b621c94
 
fa9d7e5
e3ed184
 
 
fa9d7e5
e3ed184
9c77576
 
 
 
 
 
 
554bff4
 
 
 
 
 
 
9c77576
 
554bff4
e3ed184
f994f19
e3ed184
 
 
c26d31d
e3ed184
 
 
38a3645
 
 
78b066f
433c793
 
 
 
 
 
 
 
 
9c77576
78b066f
433c793
78b066f
433c793
77b1713
433c793
 
 
 
 
 
78b066f
433c793
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78b066f
433c793
 
 
 
 
 
 
 
 
 
 
78b066f
433c793
78b066f
433c793
77b1713
433c793
 
 
 
 
 
 
 
fb0ff2b
433c793
a2ef25c
433c793
b621c94
78b066f
e3ed184
 
 
 
 
1d606db
e3ed184
 
 
6cc3f59
e3ed184
433c793
 
e3ed184
6cc3f59
e3ed184
 
433c793
fa9d7e5
433c793
 
 
fa9d7e5
433c793
 
02a1231
433c793
 
 
 
 
 
 
 
 
 
 
 
 
 
fa9d7e5
433c793
 
 
e3ed184
1f0244d
e3ed184
 
1f0244d
e3ed184
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45454da
e3ed184
6cc3f59
1f0244d
599612f
70199bd
 
0619dd4
70199bd
 
e3ed184
b8a6849
70199bd
b8a6849
 
 
 
e3ed184
b8a6849
02df4a2
b8a6849
 
02df4a2
70fdd62
e367927
b8a6849
 
 
d3eb29d
b8a6849
e3ed184
 
 
 
 
 
 
 
 
e06ca4c
b8a6849
 
 
 
 
 
 
e3ed184
 
70199bd
b8a6849
 
 
 
 
70199bd
71a3f78
b8a6849
 
 
 
 
 
70199bd
 
a3d5514
70199bd
 
e3ed184
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
#=========================================================================
# https://huggingface.co/spaces/asigalov61/Score-2-Performance-Transformer
#=========================================================================

import os
import time as reqtime
import datetime
from pytz import timezone

import copy
from itertools import groupby
import tqdm

import spaces
import gradio as gr

import torch
from x_transformer_1_23_2 import *
import random

import TMIDIX

from midi_to_colab_audio import midi_to_colab_audio

from huggingface_hub import hf_hub_download

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

print('Loading model...')

SEQ_LEN = 1802
PAD_IDX = 771
DEVICE = 'cuda' # 'cpu'

# instantiate the model

model = TransformerWrapper(
    num_tokens = PAD_IDX+1,
    max_seq_len = SEQ_LEN,
    attn_layers = Decoder(dim = 1024, 
                          depth = 8, 
                          heads = 8, 
                          rotary_pos_emb=True, 
                          attn_flash = True
                         )
    )

model = AutoregressiveWrapper(model, ignore_index = PAD_IDX)


print('=' * 70)

print('Loading model checkpoint...')

model_checkpoint = hf_hub_download(repo_id='asigalov61/Score-2-Performance-Transformer', 
                                   filename='Score_2_Performance_Transformer_Final_Small_Trained_Model_4496_steps_1.5185_loss_0.5589_acc.pth'
                                  )

model.load_state_dict(torch.load(model_checkpoint, map_location='cpu', weights_only=True))

model = torch.compile(model, mode='max-autotune')

dtype = torch.bfloat16

ctx = torch.amp.autocast(device_type=DEVICE, dtype=dtype)

print('=' * 70)
print('Done!')
print('=' * 70)

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

def load_midi(midi_file):

    print('Loading MIDI...')

    raw_score = TMIDIX.midi2single_track_ms_score(midi_file)
    
    escore_notes = TMIDIX.advanced_score_processor(raw_score, return_enhanced_score_notes=True)
    
    if escore_notes[0]:
    
        escore_notes = TMIDIX.augment_enhanced_score_notes(escore_notes[0], timings_divider=16)
    
        pe = escore_notes[0]
    
        melody_chords = []
    
        seen = []
    
        for e in escore_notes:
    
            if e[3] != 9:
        
                #=======================================================
        
                dtime = max(0, min(255, e[1]-pe[1]))
        
                if dtime != 0:
                    seen = []
                
                # Durations
                dur = max(1, min(255, e[2]))
        
                # Pitches
                ptc = max(1, min(127, e[4]))
                
                vel = max(1, min(127, e[5]))
        
                if ptc not in seen:
        
                    melody_chords.append([dtime, dur, ptc, vel])
        
                    seen.append(ptc)
        
                pe = e
    
    print('=' * 70)
    print('Number of notes in a composition:', len(melody_chords))
    print('=' * 70)

    src_melody_chords_f = []
    melody_chords_f = []
    
    for i in range(0, len(melody_chords), 300):
        
        chunk = melody_chords[i:i+300]
        
        src = []
        src1 = []
        trg = []
        
        if len(chunk) == 300:
    
            for mm in chunk:
                src.extend([mm[0], mm[2]+256])
                src1.append([mm[0], mm[2]+256, mm[1]+384, mm[3]+640])
                trg.extend([mm[0], mm[2]+256, mm[1]+384, mm[3]+640])
    
            src_melody_chords_f.append(src1)
            melody_chords_f.append([768] + src + [769] + trg + [770])
            
    print('Done!')
    print('=' * 70)
    print('Number of composition chunks:', len(melody_chords_f))
    print('=' * 70)

    return melody_chords_f, src_melody_chords_f

# =================================================================================================
                       
@spaces.GPU
def Convert_Score_to_Performance(input_midi,
                                 input_conv_type,
                                 input_number_prime_notes,
                                 input_number_conv_notes,
                                 input_model_dur_top_k,
                                 input_model_dur_temperature,
                                 input_model_vel_temperature
                                ):

    #===============================================================================
    
    print('=' * 70)
    print('Req start time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
    start_time = reqtime.time()
    print('=' * 70)

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

    print('=' * 70)
    print('Requested settings:')
    print('=' * 70)
    print('Input MIDI file name:', fn)
    print('Conversion type:', input_conv_type)
    print('Number of prime notes:', input_number_prime_notes)
    print('Number of notes to convert:', input_number_conv_notes)
    print('Model durations sampling top value:', input_model_dur_top_k)
    print('Model durations temperature:', input_model_dur_temperature)
    print('Model velocities temperature:', input_model_vel_temperature)
    
    print('=' * 70)

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

    melody_chords_f, src_melody_chords_f = load_midi(input_midi.name)

    #==================================================================
    
    print('Sample output events', melody_chords_f[0][:16])
    print('=' * 70)
    print('Generating...')

    model.to(DEVICE)
    model.eval()

    #==================================================================
    
    composition_chunk_idx = 0 # Composition chunk idx to generate durations and velocities for. Each chunk is 300 notes
    
    num_prime_notes = input_number_prime_notes # Priming improves the results but it is not necessary and you can set it to zero
    dur_top_k = input_model_dur_top_k # Use k == 1 if src composition is score and k > 1 if src composition is performance
    
    dur_temperature = input_model_dur_temperature # For best results, durations temperature should be more than 1.0 but less than velocities temperature
    vel_temperature = input_model_vel_temperature # For best results, velocities temperature must be larger than 1.3 and larger than durations temperature
    
    #==================================================================
    
    song_chunk = src_melody_chords_f[composition_chunk_idx]
    
    song = [768]
    
    for m in song_chunk:
        song.extend(m[:2])
    
    song.append(769)
    
    for i in tqdm.tqdm(range(len(song_chunk))):
    
        song.extend(song_chunk[i][:2])
    
        # Durations
    
        if i < num_prime_notes:
            song.append(song_chunk[i][2])
    
        else:
    
            x = torch.LongTensor(song).cuda()
    
            y = 0 
    
            while not 384 < y < 640:
            
                with ctx:
                    out = model.generate(x,
                                         1,
                                         temperature=dur_temperature,
                                         filter_logits_fn=top_k,
                                         filter_kwargs={'k': dur_top_k},
                                         return_prime=False,
                                         verbose=False)
                
                y = out.tolist()[0][0]
        
            song.append(y)
    
    
        # Velocities
        
        if i < num_prime_notes:
            song.append(song_chunk[i][3])
    
        else:
    
            x = torch.LongTensor(song).cuda()
            
            y = 0 
    
            while not 640 < y < 768:
                    
                with ctx:
                    out = model.generate(x,
                                         1,
                                         temperature=vel_temperature,
                                         #filter_logits_fn=top_k,
                                         #filter_kwargs={'k': 10},
                                         return_prime=False,
                                         verbose=False)
                
                y = out.tolist()[0][0]
        
            song.append(y)

            
    print('=' * 70)
    print('Done!')
    print('=' * 70)
    
    #===============================================================================
    
    print('Rendering results...')
    
    print('=' * 70)
    print('Sample INTs', song[:15])
    print('=' * 70)

    song_f = []
    
    if len(song) != 0:
    
        time = 0
        dur = 0
        vel = 90
        pitch = 60
        channel = 0
        patch = 0
        
        patches = [0] * 16
        
        for ss in song[602:]:
        
            if 0 <= ss < 256:
        
                time += ss * 16
        
            if 256 <= ss < 384:
        
                pitch = ss-256
        
            if 384 <= ss < 640:
        
                dur = (ss-384) * 16
        
            if 640 <= ss < 768:
                
                vel = (ss-640)
            
                song_f.append(['note', time, dur, channel, pitch, vel, patch])

    fn1 = "Score-2-Performance-Transformer-Composition"
    
    detailed_stats = TMIDIX.Tegridy_ms_SONG_to_MIDI_Converter(song_f,
                                                              output_signature = 'Score 2 Performance Transformer',
                                                              output_file_name = fn1,
                                                              track_name='Project Los Angeles',
                                                              list_of_MIDI_patches=patches
                                                              )
    
    new_fn = fn1+'.mid'
            
    
    audio = midi_to_colab_audio(new_fn, 
                        soundfont_path=soundfont,
                        sample_rate=16000,
                        volume_scale=10,
                        output_for_gradio=True
                        )
    
    print('Done!')
    print('=' * 70)

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

    output_midi_title = str(fn1)
    output_midi_summary = str(song_f[:3])
    output_midi = str(new_fn)
    output_audio = (16000, audio)
    
    output_plot = TMIDIX.plot_ms_SONG(song_f, plot_title=output_midi, return_plt=True)

    print('Output MIDI file name:', output_midi)
    print('Output MIDI title:', output_midi_title)
    print('Output MIDI summary:', output_midi_summary)
    print('=' * 70) 
    

    #========================================================
    
    print('-' * 70)
    print('Req end time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
    print('-' * 70)
    print('Req execution time:', (reqtime.time() - start_time), 'sec')

    return 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)

    soundfont = "SGM-v2.01-YamahaGrand-Guit-Bass-v2.7.sf2"

    app = gr.Blocks()
    
    with app:
        
        gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>Score 2 Performance Transformer</h1>")
        gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>Convert any MIDI score to a nice performance</h1>")

        gr.Markdown("## Upload your MIDI or select a sample example MIDI below")
        gr.Markdown("### PLEASE NOTE that the score MIDI MUST HAVE at least 300 notes for this demo to work")
        
        input_midi = gr.File(label="Input MIDI", file_types=[".midi", ".mid", ".kar"])
        
        gr.Markdown("## Select conversion type")

        input_conv_type = gr.Radio(["Durations and Velocities", "Durations", "Velocities"], 
                                   value="Durations and Velocities", 
                                   label="Conversion type"
                                  )

        gr.Markdown("## Conversion options")

        input_number_prime_notes = gr.Slider(0, 512, value=0, step=8, label="Number of prime notes")
        input_number_conv_notes = gr.Slider(0, 3072, value=1024, step=16, label="Number of notes to convert")

        gr.Markdown("## Model options")

        input_model_dur_top_k = gr.Slider(1, 100, value=1, step=1, label="Model sampling top k value for durations")
        input_model_dur_temperature = gr.Slider(0.5, 1.5, value=1.1, step=0.05, label="Model temperature for durations")
        input_model_vel_temperature = gr.Slider(0.5, 1.5, value=1.5, step=0.05, label="Model temperature for velocities")
        
        run_btn = gr.Button("convert", variant="primary")

        gr.Markdown("## Generation results")

        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 = run_btn.click(Convert_Score_to_Performance, [input_midi,
                                                                 input_conv_type,
                                                                 input_number_prime_notes,
                                                                 input_number_conv_notes,
                                                                 input_model_dur_top_k,
                                                                 input_model_dur_temperature,
                                                                 input_model_vel_temperature
                                                                ],
                                  [output_midi_title, output_midi_summary, output_midi, output_audio, output_plot])

        gr.Examples(
            [["asap_midi_score_21.mid", "Durations and Velocities", 8, 600, 1, 1.1, 1.5],
             ["asap_midi_score_45.mid", "Durations and Velocities", 8, 600, 1, 1.1, 1.5],
             ["asap_midi_score_69.mid", "Durations and Velocities", 8, 600, 1, 1.1, 1.5],
             ["asap_midi_score_118.mid", "Durations and Velocities", 8, 600, 1, 1.1, 1.5],
             ["asap_midi_score_167.mid", "Durations and Velocities", 8, 600, 1, 1.1, 1.5],
            ],
            [input_midi,
             input_conv_type,
             input_number_prime_notes,
             input_number_conv_notes,
             input_model_dur_top_k,
             input_model_dur_temperature,
             input_model_vel_temperature
            ],
            [output_midi_title, output_midi_summary, output_midi, output_audio, output_plot],
            Convert_Score_to_Performance
        )
        
        app.queue().launch()