asigalov61 commited on
Commit
e618966
1 Parent(s): 2c5eabf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -83
app.py CHANGED
@@ -66,7 +66,7 @@ def GenerateDrums(input_midi, input_num_tokens):
66
 
67
  print('-' * 70)
68
  print('Input file name:', fn)
69
- print('Req num tok:', input_num_tokens)
70
  print('-' * 70)
71
 
72
  #===============================================================================
@@ -101,99 +101,115 @@ def GenerateDrums(input_midi, input_num_tokens):
101
 
102
  #===============================================================================
103
 
104
- print('Selected Improv sequence:')
105
- print(start_tokens)
106
- print('-' * 70)
107
-
108
- output_signature = 'Allegro Music Transformer'
109
- output_file_name = 'Allegro-Music-Transformer-Music-Composition'
110
- track_name = 'Project Los Angeles'
111
- list_of_MIDI_patches = [0, 24, 32, 40, 42, 46, 56, 71, 73, 0, 53, 19, 0, 0, 0, 0]
112
- number_of_ticks_per_quarter = 500
113
- text_encoding = 'ISO-8859-1'
114
-
115
- output_header = [number_of_ticks_per_quarter,
116
- [['track_name', 0, bytes(output_signature, text_encoding)]]]
117
-
118
- patch_list = [['patch_change', 0, 0, list_of_MIDI_patches[0]],
119
- ['patch_change', 0, 1, list_of_MIDI_patches[1]],
120
- ['patch_change', 0, 2, list_of_MIDI_patches[2]],
121
- ['patch_change', 0, 3, list_of_MIDI_patches[3]],
122
- ['patch_change', 0, 4, list_of_MIDI_patches[4]],
123
- ['patch_change', 0, 5, list_of_MIDI_patches[5]],
124
- ['patch_change', 0, 6, list_of_MIDI_patches[6]],
125
- ['patch_change', 0, 7, list_of_MIDI_patches[7]],
126
- ['patch_change', 0, 8, list_of_MIDI_patches[8]],
127
- ['patch_change', 0, 9, list_of_MIDI_patches[9]],
128
- ['patch_change', 0, 10, list_of_MIDI_patches[10]],
129
- ['patch_change', 0, 11, list_of_MIDI_patches[11]],
130
- ['patch_change', 0, 12, list_of_MIDI_patches[12]],
131
- ['patch_change', 0, 13, list_of_MIDI_patches[13]],
132
- ['patch_change', 0, 14, list_of_MIDI_patches[14]],
133
- ['patch_change', 0, 15, list_of_MIDI_patches[15]],
134
- ['track_name', 0, bytes(track_name, text_encoding)]]
135
-
136
- output = output_header + [patch_list]
137
-
138
- yield output, None, None, [create_msg("visualizer_clear", None)]
139
-
140
- outy = start_tokens
141
-
142
- ctime = 0
143
- dur = 0
144
- vel = 90
145
- pitch = 0
146
- channel = 0
147
-
148
- for i in range(max(1, min(512, num_tok))):
149
-
150
- inp = torch.LongTensor([outy]).cpu()
151
-
152
- with ctx:
153
-
154
- out = model.module.generate(inp,
155
- 1,
156
- temperature=0.9,
157
- return_prime=False,
158
- verbose=False)
159
-
160
- out0 = out[0].tolist()
161
- outy.extend(out0)
162
-
163
- ss1 = out0[0]
164
-
165
- if 0 < ss1 < 256:
166
- ctime += ss1 * 8
167
 
168
- if 256 <= ss1 < 1280:
169
- dur = ((ss1 - 256) // 8) * 32
170
- vel = (((ss1 - 256) % 8) + 1) * 15
 
 
 
 
 
 
171
 
172
- if 1280 <= ss1 < 2816:
173
- channel = (ss1 - 1280) // 128
174
- pitch = (ss1 - 1280) % 128
175
- event = ['note', ctime, dur, channel, pitch, vel]
176
- output[-1].append(event)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177
 
178
- yield output, None, None, [create_msg("visualizer_append", event), create_msg("progress", [i + 1, num_tok])]
179
 
180
- midi_data = TMIDIX.score2midi(output, text_encoding)
 
 
 
 
 
181
 
182
- with open(f"Allegro-Music-Transformer-Music-Composition.mid", 'wb') as f:
183
- f.write(midi_data)
 
 
 
184
 
185
- audio = synthesis(TMIDIX.score2opus(output), 'SGM-v2.01-YamahaGrand-Guit-Bass-v2.7.sf2')
186
 
187
- print('Sample INTs', outy[:16])
188
- print('-' * 70)
189
- print('Last generated MIDI event', output[2][-1])
190
  print('-' * 70)
191
  print('Req end time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
192
  print('-' * 70)
193
  print('Req execution time:', (time.time() - start_time), 'sec')
194
 
195
- yield output, "Allegro-Music-Transformer-Music-Composition.mid", (44100, audio), [
196
- create_msg("visualizer_end", None)]
197
 
198
  # =================================================================================================
199
 
@@ -273,6 +289,6 @@ if __name__ == "__main__":
273
 
274
 
275
  run_event = run_btn.click(GenerateDrums, [input_midi, input_num_tokens],
276
- [output_midi_title, output_midi_summary, output_audio, output_plot, output_midi])
277
 
278
  app.queue(concurrency_count=1).launch(server_port=opt.port, share=opt.share, inbrowser=True)
 
66
 
67
  print('-' * 70)
68
  print('Input file name:', fn)
69
+ print('Req num toks:', input_num_tokens)
70
  print('-' * 70)
71
 
72
  #===============================================================================
 
101
 
102
  #===============================================================================
103
 
104
+ print('=' * 70)
105
+
106
+ print('Sample output events', escore_notes[:5])
107
+ print('=' * 70)
108
+ print('Generating...')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
 
110
+ for c in comp_times[:input_num_tokens]:
111
+ output.append(c)
112
+
113
+ out = generate_drums(output,
114
+ temperature=0.9,
115
+ max_drums_limit=8,
116
+ num_memory_tokens=4096
117
+ )
118
+ output.extend(out)
119
 
120
+ print('=' * 70)
121
+ print('Done!')
122
+ print('=' * 70)
123
+
124
+ #===============================================================================
125
+ print('Rendering results...')
126
+
127
+ print('=' * 70)
128
+ print('Sample INTs', output[:12])
129
+ print('=' * 70)
130
+
131
+ if len(output) != 0:
132
+
133
+ song = output
134
+ song_f = []
135
+
136
+ time = 0
137
+ dtime = 0
138
+ ntime = 0
139
+ dur = 32
140
+ vel = 90
141
+ vels = [100, 120]
142
+ pitch = 0
143
+ channel = 0
144
+
145
+ idx = 0
146
+
147
+ for ss in song:
148
+
149
+ if 0 <= ss < 128:
150
+
151
+ dtime = time
152
+
153
+ time += cscore[idx][0][0] * 32
154
+
155
+ for c in cscore[idx]:
156
+ song_f.append(['note', time, c[1] * 32, c[2], c[3], c[4], c[5]])
157
+
158
+ idx += 1
159
+
160
+ if 128 <= ss < 256:
161
+
162
+ dtime += (ss-128) * 32
163
+
164
+ if 256 <= ss < 384:
165
+
166
+ pitch = (ss-256)
167
+
168
+ song_f.append(['note', dtime, dur, 9, pitch, vels[pitch % 2], 128 ])
169
+
170
+ detailed_stats = TMIDIX.Tegridy_ms_SONG_to_MIDI_Converter(song_f,
171
+ output_signature = 'Ultimate Drums Transformer',
172
+ output_file_name = '/content/Ultimate-Drums-Transformer-Composition',
173
+ track_name='Project Los Angeles',
174
+ list_of_MIDI_patches=patches
175
+ )
176
+
177
+ new_fn = fn1+'.mid'
178
+
179
+
180
+ audio = midi_to_colab_audio(new_fn,
181
+ soundfont_path=soundfont,
182
+ sample_rate=16000,
183
+ volume_scale=10,
184
+ output_for_gradio=True
185
+ )
186
+
187
+ print('Done!')
188
+ print('=' * 70)
189
 
190
+ #========================================================
191
 
192
+ output_midi_title = str(fn1)
193
+ output_midi_summary = str('')
194
+ output_midi = str(fn1)
195
+ output_audio = (16000, audio)
196
+
197
+ output_plot = TMIDIX.plot_ms_SONG(output_score, plot_title=output_midi, return_plt=True)
198
 
199
+ print('Output MIDI file name:', output_midi)
200
+ print('Output MIDI title:', output_midi_title)
201
+ print('Output MIDI summary:', '')
202
+ print('=' * 70)
203
+
204
 
205
+ #========================================================
206
 
 
 
 
207
  print('-' * 70)
208
  print('Req end time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
209
  print('-' * 70)
210
  print('Req execution time:', (time.time() - start_time), 'sec')
211
 
212
+ yield output_midi_title, output_midi_summary, output_midi, output_audio, output_plot
 
213
 
214
  # =================================================================================================
215
 
 
289
 
290
 
291
  run_event = run_btn.click(GenerateDrums, [input_midi, input_num_tokens],
292
+ [output_midi_title, output_midi_summary, output_midi, output_audio, output_plot])
293
 
294
  app.queue(concurrency_count=1).launch(server_port=opt.port, share=opt.share, inbrowser=True)