import os.path import time as reqtime import datetime from pytz import timezone import torch import spaces import gradio as gr from x_transformer_1_23_2 import * import random import tqdm from midi_to_colab_audio import midi_to_colab_audio import TMIDIX import matplotlib.pyplot as plt in_space = os.getenv("SYSTEM") == "spaces" # ================================================================================================= @spaces.GPU def InpaintPitches(input_midi, input_num_of_notes, input_patch_number): print('=' * 70) print('Req start time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT))) start_time = reqtime.time() print('Loading model...') SEQ_LEN = 8192 # Models seq len PAD_IDX = 19463 # Models pad index DEVICE = 'cuda' # 'cuda' # instantiate the model model = TransformerWrapper( num_tokens = PAD_IDX+1, max_seq_len = SEQ_LEN, attn_layers = Decoder(dim = 1024, depth = 32, heads = 32, attn_flash = True) ) model = AutoregressiveWrapper(model, ignore_index = PAD_IDX) model.to(DEVICE) print('=' * 70) print('Loading model checkpoint...') model.load_state_dict( torch.load('Giant_Music_Transformer_Large_Trained_Model_36074_steps_0.3067_loss_0.927_acc.pth', map_location=DEVICE)) print('=' * 70) model.eval() if DEVICE == 'cpu': dtype = torch.bfloat16 else: dtype = torch.bfloat16 ctx = torch.amp.autocast(device_type=DEVICE, dtype=dtype) print('Done!') print('=' * 70) fn = os.path.basename(input_midi.name) fn1 = fn.split('.')[0] input_num_of_notes = max(8, min(2048, input_num_of_notes)) print('-' * 70) print('Input file name:', fn) print('Req num of notes:', input_num_of_notes) print('Req patch number:', input_patch_number) print('-' * 70) #=============================================================================== raw_score = TMIDIX.midi2single_track_ms_score(input_midi.name) #=============================================================================== # Enhanced score notes events_matrix1 = TMIDIX.advanced_score_processor(raw_score, return_enhanced_score_notes=True)[0] #======================================================= # PRE-PROCESSING # checking number of instruments in a composition instruments_list_without_drums = list(set([y[3] for y in events_matrix1 if y[3] != 9])) instruments_list = list(set([y[3] for y in events_matrix1])) if len(events_matrix1) > 0 and len(instruments_list_without_drums) > 0: #====================================== events_matrix2 = [] # Recalculating timings for e in events_matrix1: # Original timings e[1] = int(e[1] / 16) e[2] = int(e[2] / 16) #=================================== # ORIGINAL COMPOSITION #=================================== # Sorting by patch, pitch, then by start-time events_matrix1.sort(key=lambda x: x[6]) events_matrix1.sort(key=lambda x: x[4], reverse=True) events_matrix1.sort(key=lambda x: x[1]) #======================================================= # FINAL PROCESSING melody_chords = [] melody_chords2 = [] # Break between compositions / Intro seq if 9 in instruments_list: drums_present = 19331 # Yes else: drums_present = 19330 # No if events_matrix1[0][3] != 9: pat = events_matrix1[0][6] else: pat = 128 melody_chords.extend([19461, drums_present, 19332+pat]) # Intro seq #======================================================= # MAIN PROCESSING CYCLE #======================================================= abs_time = 0 pbar_time = 0 pe = events_matrix1[0] chords_counter = 1 comp_chords_len = len(list(set([y[1] for y in events_matrix1]))) for e in events_matrix1: #======================================================= # Timings... # Cliping all values... delta_time = max(0, min(255, e[1]-pe[1])) # Durations and channels dur = max(0, min(255, e[2])) cha = max(0, min(15, e[3])) # Patches if cha == 9: # Drums patch will be == 128 pat = 128 else: pat = e[6] # Pitches ptc = max(1, min(127, e[4])) # Velocities # Calculating octo-velocity vel = max(8, min(127, e[5])) velocity = round(vel / 15)-1 #======================================================= # FINAL NOTE SEQ # Writing final note asynchronously dur_vel = (8 * dur) + velocity pat_ptc = (129 * pat) + ptc melody_chords.extend([delta_time, dur_vel+256, pat_ptc+2304]) melody_chords2.append([delta_time, dur_vel+256, pat_ptc+2304]) pe = e #================================================================== print('=' * 70) print('Number of tokens:', len(melody_chords)) print('Number of notes:', len(melody_chords2)) print('Sample output events', melody_chords[:5]) print('=' * 70) print('Generating...') output = [] max_chords_limit = 8 temperature=0.9 num_memory_tokens=4096 output = [] idx = 0 for c in chords[:input_num_tokens]: output.append(c) if input_conditioning_type == 'Chords-Times' or input_conditioning_type == 'Chords-Times-Durations': output.append(times[idx]) if input_conditioning_type == 'Chords-Times-Durations': output.append(durs[idx]) x = torch.tensor([output] * 1, dtype=torch.long, device=DEVICE) o = 0 ncount = 0 while o < 384 and ncount < max_chords_limit: with ctx: out = model.generate(x[-num_memory_tokens:], 1, temperature=temperature, return_prime=False, verbose=False) o = out.tolist()[0][0] if 256 <= o < 384: ncount += 1 if o < 384: x = torch.cat((x, out), 1) outy = x.tolist()[0][len(output):] output.extend(outy) idx += 1 if idx == len(chords[:input_num_tokens])-1: break print('=' * 70) print('Done!') print('=' * 70) #=============================================================================== print('Rendering results...') print('=' * 70) print('Sample INTs', output[:12]) print('=' * 70) out1 = output if len(out1) != 0: song = out1 song_f = [] time = 0 dur = 0 vel = 90 pitch = 0 channel = 0 patches = [0] * 16 channel = 0 for ss in song: if 0 <= ss < 128: time += ss * 32 if 128 <= ss < 256: dur = (ss-128) * 32 if 256 <= ss < 384: pitch = (ss-256) vel = max(40, pitch) song_f.append(['note', time, dur, channel, pitch, vel, 0]) fn1 = "Chords-Progressions-Transformer-Composition" detailed_stats = TMIDIX.Tegridy_ms_SONG_to_MIDI_Converter(song_f, output_signature = 'Chords Progressions 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("