asigalov61 commited on
Commit
71fb6fc
1 Parent(s): b6793f7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -1
app.py CHANGED
@@ -182,6 +182,87 @@ def Harmonize_Melody(input_src_midi,
182
  print('=' * 70)
183
 
184
  #===============================================================================
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
185
 
186
  print('Rendering results...')
187
  print('=' * 70)
@@ -197,8 +278,10 @@ def Harmonize_Melody(input_src_midi,
197
  patches[2] = base_MIDI_patch_number
198
 
199
  patches[3] = melody_MIDI_patch_number
 
 
200
 
201
- for i, s in enumerate(song):
202
 
203
  if 11 < s < 141:
204
 
@@ -217,6 +300,8 @@ def Harmonize_Melody(input_src_midi,
217
  if base_MIDI_patch_number > -1:
218
  output_score.append(['note', time, dur, 2, chord[-1]+24, 120-chord[-1], base_MIDI_patch_number])
219
 
 
 
220
  fn1 = "Melody-Harmonizer-Transformer-Composition"
221
 
222
  detailed_stats = TMIDIX.Tegridy_ms_SONG_to_MIDI_Converter(output_score,
 
182
  print('=' * 70)
183
 
184
  #===============================================================================
185
+
186
+ def find_best_match(matches_indexes, previous_match_index):
187
+
188
+ msigs = []
189
+
190
+ for midx in matches_indexes:
191
+
192
+ mat = all_chords_ptcs_chunks[midx]
193
+
194
+ msig = []
195
+
196
+ for m in mat:
197
+ msig.extend([sum(m) / len(m), len(m)])
198
+
199
+ msigs.append(msig)
200
+
201
+ pmat = all_chords_ptcs_chunks[previous_match_index]
202
+
203
+ psig = []
204
+
205
+ for p in pmat:
206
+ psig.extend([sum(p) / len(p), len(p)])
207
+
208
+ dists = []
209
+
210
+ for m in msigs:
211
+ dists.append(TMIDIX.minkowski_distance(psig, m))
212
+
213
+ min_dist = min(dists)
214
+ min_dist_idx = dists.index(min_dist)
215
+
216
+ return matches_indexes[min_dist_idx]
217
+
218
+ #===============================================================================
219
+
220
+ if texture_harmonized_chords:
221
+
222
+ print('=' * 70)
223
+ print('Texturing harmonized chords...')
224
+ print('=' * 70)
225
+
226
+ chunk_length = 2
227
+
228
+ harm_chords = [TMIDIX.ALL_CHORDS_FILTERED[s-12] for s in song if 11 < s < 141]
229
+
230
+ harm_toks = [TMIDIX.ALL_CHORDS_FILTERED.index(c) for c in harm_chords] + [TMIDIX.ALL_CHORDS_FILTERED.index(harm_chords[-1])] * (chunk_length - (len(harm_chords) % chunk_length))
231
+
232
+ final_song = []
233
+
234
+ trg_chunk = np.array(harm_toks[:chunk_length])
235
+ sidxs = np.where((src_chunks == trg_chunk).all(axis=1))[0].tolist()
236
+
237
+ sidx = random.choice(sidxs)
238
+ pidx = sidx
239
+
240
+ final_song.extend(all_chords_ptcs_chunks[sidx])
241
+
242
+ for i in tqdm.tqdm(range(chunk_length, len(harm_toks), chunk_length)):
243
+
244
+ trg_chunk = np.array(harm_toks[i:i+chunk_length])
245
+
246
+ sidxs = np.where((src_chunks == trg_chunk).all(axis=1))[0].tolist()
247
+
248
+ if len(sidxs) > 0:
249
+
250
+ sidx = find_best_match(sidxs, pidx)
251
+ pidx = sidx
252
+
253
+ final_song.extend(all_chords_ptcs_chunks[sidx])
254
+
255
+ else:
256
+ print('Dead end!')
257
+ break
258
+
259
+ final_song = final_song[:len(harm_chords)]
260
+
261
+ print('=' * 70)
262
+ print(len(final_song))
263
+ print('=' * 70)
264
+ print('Done!')
265
+ print('=' * 70)
266
 
267
  print('Rendering results...')
268
  print('=' * 70)
 
278
  patches[2] = base_MIDI_patch_number
279
 
280
  patches[3] = melody_MIDI_patch_number
281
+
282
+ i = 0
283
 
284
+ for s in song:
285
 
286
  if 11 < s < 141:
287
 
 
300
  if base_MIDI_patch_number > -1:
301
  output_score.append(['note', time, dur, 2, chord[-1]+24, 120-chord[-1], base_MIDI_patch_number])
302
 
303
+ i += 1
304
+
305
  fn1 = "Melody-Harmonizer-Transformer-Composition"
306
 
307
  detailed_stats = TMIDIX.Tegridy_ms_SONG_to_MIDI_Converter(output_score,