asigalov61 commited on
Commit
433c793
·
verified ·
1 Parent(s): 554bff4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -118
app.py CHANGED
@@ -184,7 +184,7 @@ def Convert_Score_to_Performance(input_midi,
184
 
185
  #==================================================================
186
 
187
- melody_chords, src_melody_chords = load_midi(input_midi)
188
 
189
  #==================================================================
190
 
@@ -196,106 +196,83 @@ def Convert_Score_to_Performance(input_midi,
196
  model.eval()
197
 
198
  #==================================================================
199
-
200
- def generate_continuation(num_prime_tokens, num_gen_tokens):
201
-
202
- x = torch.tensor(TMIDIX.flatten(prime_toks)[:num_prime_tokens], dtype=torch.long, device=DEVICE)
203
-
204
- with ctx:
205
- out = model.generate(x,
206
- num_gen_tokens,
207
- filter_logits_fn=top_p,
208
- filter_kwargs={'thres': input_model_top_p},
209
- temperature=input_model_temperature,
210
- return_prime=True,
211
- verbose=True)
212
-
213
- y = out.tolist()[0]
214
-
215
- return y
216
-
217
  #==================================================================
218
-
219
- def generate_tokens(seq, max_num_ptcs=5, max_tries=10):
220
 
221
- input = copy.deepcopy(seq)
222
-
223
- pcount = 0
224
- y = 545
225
- tries = 0
226
 
227
- gen_tokens = []
228
-
229
- seen = False
230
 
231
- if 256 < input[-1] < 544:
232
- seen = True
233
-
234
- while pcount < max_num_ptcs and y > 255 and tries < max_tries:
 
 
235
 
236
- x = torch.tensor(input[-input_num_memory_tokens:], dtype=torch.long, device=DEVICE)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
237
 
238
- with ctx:
239
- out = model.generate(x,
240
- 1,
241
- filter_logits_fn=top_p,
242
- filter_kwargs={'thres': input_model_top_p},
243
- return_prime=False,
244
- verbose=False)
 
 
 
 
245
 
246
- y = out[0].tolist()[0]
247
 
248
- if 256 < y < 544:
249
- if not seen:
250
- input.append(y)
251
- gen_tokens.append(y)
252
- seen = True
253
-
254
- else:
255
- tries += 1
256
 
257
- if y > 544 and seen:
258
- if pcount < max_num_ptcs and y not in gen_tokens:
259
- input.append(y)
260
- gen_tokens.append(y)
261
- pcount += 1
262
-
263
- else:
264
- tries += 1
265
 
266
- return gen_tokens
267
-
268
- #==================================================================
269
-
270
- song = []
271
-
272
- if input_gen_type == 'Freestyle':
273
 
274
- output = generate_continuation(input_number_prime_tokens, input_number_gen_tokens)
275
- song.extend(output)
276
 
277
- else:
278
-
279
- for i in range(input_number_prime_chords):
280
- song.extend(prime_toks[i])
281
-
282
- for i in tqdm.tqdm(range(input_number_prime_chords, input_number_prime_chords+input_number_gen_chords)):
283
-
284
- song.extend(score_toks[i])
285
-
286
- if control_toks[i]:
287
- for ct in control_toks[i]:
288
-
289
- if input_use_original_durations:
290
- song.append(ct[0])
291
-
292
- if input_match_original_pitches_counts:
293
- out_seq = generate_tokens(song, ct[1])
294
-
295
- else:
296
- out_seq = generate_tokens(song)
297
-
298
- song.extend(out_seq)
299
 
300
  print('=' * 70)
301
  print('Done!')
@@ -308,42 +285,39 @@ def Convert_Score_to_Performance(input_midi,
308
  print('=' * 70)
309
  print('Sample INTs', song[:15])
310
  print('=' * 70)
 
 
311
 
312
  if len(song) != 0:
313
 
314
- song_f = []
315
-
316
  time = 0
317
- dur = 32
318
- channel = 0
319
- pitch = 60
320
  vel = 90
 
 
 
321
 
322
- patches = [0, 10, 19, 24, 35, 40, 52, 56, 65, 9, 0, 0, 0, 0, 0, 0]
323
- velocities = [80, 100, 90, 100, 110, 100, 100, 100, 100, 110]
324
-
325
- for ss in song:
326
-
327
- if 0 <= ss < 128:
328
-
329
- time += ss * 32
330
-
331
- if 128 < ss < 256:
332
-
333
- song_f.append(['note', time, 32, 9, ss-128, velocities[9], 128])
 
 
 
 
 
334
 
335
- if 256 < ss < 544:
336
-
337
- dur = ((ss-256) % 32) * 4 * 32
338
- channel = (ss-256) // 32
339
-
340
- if 544 < ss < 672:
341
-
342
- patch = channel * 8
343
-
344
- pitch = ss-544
345
-
346
- song_f.append(['note', time, dur, channel, pitch, velocities[channel], patch])
347
 
348
  fn1 = "Score-2-Performance-Transformer-Composition"
349
 
 
184
 
185
  #==================================================================
186
 
187
+ melody_chords_f, src_melody_chords_f = load_midi(input_midi)
188
 
189
  #==================================================================
190
 
 
196
  model.eval()
197
 
198
  #==================================================================
199
+
200
+ composition_chunk_idx = 0 # Composition chunk idx to generate durations and velocities for. Each chunk is 300 notes
201
+
202
+ num_prime_notes = input_number_prime_notes # Priming improves the results but it is not necessary and you can set it to zero
203
+ dur_top_k = input_model_dur_top_k # Use k == 1 if src composition is score and k > 1 if src composition is performance
204
+
205
+ dur_temperature = input_model_dur_temperature # For best results, durations temperature should be more than 1.0 but less than velocities temperature
206
+ vel_temperature = input_model_vel_temperature # For best results, velocities temperature must be larger than 1.3 and larger than durations temperature
207
+
 
 
 
 
 
 
 
 
 
208
  #==================================================================
 
 
209
 
210
+ song_chunk = src_melody_chords_f[composition_chunk_idx]
 
 
 
 
211
 
212
+ song = [768]
 
 
213
 
214
+ for m in song_chunk:
215
+ song.extend(m[:2])
216
+
217
+ song.append(769)
218
+
219
+ for i in tqdm.tqdm(range(len(song_chunk))):
220
 
221
+ song.extend(song_chunk[i][:2])
222
+
223
+ # Durations
224
+
225
+ if i < num_prime_notes:
226
+ song.append(song_chunk[i][2])
227
+
228
+ else:
229
+
230
+ x = torch.LongTensor(song).cuda()
231
+
232
+ y = 0
233
+
234
+ while not 384 < y < 640:
235
+
236
+ with ctx:
237
+ out = model.generate(x,
238
+ 1,
239
+ temperature=dur_temperature,
240
+ filter_logits_fn=top_k,
241
+ filter_kwargs={'k': dur_top_k},
242
+ return_prime=False,
243
+ verbose=False)
244
+
245
+ y = out.tolist()[0][0]
246
 
247
+ song.append(y)
248
+
249
+
250
+ # Velocities
251
+
252
+ if i < num_prime_notes:
253
+ song.append(song_chunk[i][3])
254
+
255
+ else:
256
+
257
+ x = torch.LongTensor(song).cuda()
258
 
259
+ y = 0
260
 
261
+ while not 640 < y < 768:
 
 
 
 
 
 
 
262
 
263
+ with ctx:
264
+ out = model.generate(x,
265
+ 1,
266
+ temperature=vel_temperature,
267
+ #filter_logits_fn=top_k,
268
+ #filter_kwargs={'k': 10},
269
+ return_prime=False,
270
+ verbose=False)
271
 
272
+ y = out.tolist()[0][0]
 
 
 
 
 
 
273
 
274
+ song.append(y)
 
275
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
276
 
277
  print('=' * 70)
278
  print('Done!')
 
285
  print('=' * 70)
286
  print('Sample INTs', song[:15])
287
  print('=' * 70)
288
+
289
+ song_f = []
290
 
291
  if len(song) != 0:
292
 
 
 
293
  time = 0
294
+ dur = 0
 
 
295
  vel = 90
296
+ pitch = 60
297
+ channel = 0
298
+ patch = 0
299
 
300
+ patches = [0] * 16
301
+
302
+ for ss in song1:
303
+
304
+ if 0 <= ss < 256:
305
+
306
+ time += ss * 16
307
+
308
+ if 256 <= ss < 384:
309
+
310
+ pitch = ss-256
311
+
312
+ if 384 <= ss < 640:
313
+
314
+ dur = (ss-384) * 16
315
+
316
+ if 640 <= ss < 768:
317
 
318
+ vel = (ss-640)
319
+
320
+ song_f.append(['note', time, dur, channel, pitch, vel, patch])
 
 
 
 
 
 
 
 
 
321
 
322
  fn1 = "Score-2-Performance-Transformer-Composition"
323