projectlosangeles commited on
Commit
ab74d2b
·
verified ·
1 Parent(s): 8c92137

Upload 8 files

Browse files
Abracadabra-Sample-Melody.mid ADDED
Binary file (1.33 kB). View file
 
README.md CHANGED
@@ -1,15 +1,23 @@
1
  ---
2
  title: Harmonic Melody MIDI Mixer
3
- emoji: 🌍
4
- colorFrom: pink
5
- colorTo: gray
6
  sdk: gradio
7
- sdk_version: 6.20.0
8
- python_version: '3.13'
9
  app_file: app.py
10
  pinned: false
11
  license: apache-2.0
12
  short_description: Harmonize and mix any MIDI melody
 
 
 
 
 
 
 
 
 
13
  ---
14
 
15
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
  title: Harmonic Melody MIDI Mixer
3
+ emoji: 🔥
4
+ colorFrom: red
5
+ colorTo: blue
6
  sdk: gradio
7
+ sdk_version: 6.19.0
 
8
  app_file: app.py
9
  pinned: false
10
  license: apache-2.0
11
  short_description: Harmonize and mix any MIDI melody
12
+ tags:
13
+ - midi
14
+ - music
15
+ - melody
16
+ - music algorithm
17
+ - MIDI
18
+ - harmonizer
19
+ - mixer
20
+ - harmony
21
  ---
22
 
23
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
Sparks-Fly-Sample-Melody.mid ADDED
Binary file (1.48 kB). View file
 
TMIDIX.py ADDED
The diff for this file is too large to render. See raw diff
 
app.py ADDED
@@ -0,0 +1,481 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ############################################################################
2
+ # https://huggingface.co/spaces/projectlosangeles/Harmonic-Melody-MIDI-Mixer
3
+ ############################################################################
4
+
5
+ import os
6
+ os.environ["GRADIO_HOT_RELOAD"] = "0"
7
+
8
+ import os.path
9
+
10
+ import time as reqtime
11
+ import datetime
12
+ from pytz import timezone
13
+
14
+ from itertools import groupby
15
+ import copy
16
+
17
+ import gradio as gr
18
+
19
+ import random
20
+
21
+ from midi_to_colab_audio import midi_to_colab_audio
22
+ import TMIDIX
23
+
24
+ import matplotlib.pyplot as plt
25
+
26
+ # =================================================================================================
27
+
28
+ def pitches_counts(melody_score):
29
+
30
+ pitches = [p[4] for p in melody_score]
31
+
32
+ pcounts = []
33
+
34
+ count = 0
35
+ pp = -1
36
+
37
+ for p in pitches:
38
+ if p == pp:
39
+ count += 1
40
+ pcounts.append(count)
41
+ else:
42
+ count = 0
43
+ pcounts.append(count)
44
+ pp = p
45
+
46
+ return pcounts
47
+
48
+ # =================================================================================================
49
+
50
+ def find_similar_song(songs, src_melody):
51
+
52
+ src_pcount = pitches_counts(src_melody)
53
+
54
+ ratios = []
55
+
56
+ for s in songs:
57
+ patch = s[1]
58
+
59
+ trg_melody = [e for e in s[3] if e[6] == patch]
60
+ trg_pcount = pitches_counts(trg_melody)
61
+
62
+ pcount = 0
63
+
64
+ for i, c in enumerate(src_pcount):
65
+ if c == trg_pcount[i]:
66
+ pcount += 1
67
+
68
+ ratios.append(pcount / len(src_pcount))
69
+
70
+ max_ratio = max(ratios)
71
+
72
+ return songs[ratios.index(max_ratio)], max_ratio, ratios.count(max_ratio)
73
+
74
+ # =================================================================================================
75
+
76
+ def mix_chord(chord, tones_chord, mel_patch, mel_pitch, next_note_dtime):
77
+
78
+ cho = []
79
+
80
+ for k, g in groupby(sorted(chord, key=lambda x: x[6]), lambda x: x[6]):
81
+
82
+ if k != 128:
83
+ if k == mel_patch:
84
+
85
+ cg = list(g)
86
+
87
+ c = copy.deepcopy(cg[0])
88
+
89
+ if cg[0][2] > next_note_dtime:
90
+ c[2] = next_note_dtime
91
+
92
+ c[4] = mel_pitch
93
+ c[5] = 105 + (mel_pitch % 12)
94
+
95
+ cho.append(c)
96
+
97
+ else:
98
+ cg = list(g)
99
+
100
+ tclen = len(tones_chord)
101
+
102
+ if len(cg) > tclen:
103
+ tchord = tones_chord + [random.choice(tones_chord) for _ in range(len(cg)-tclen)]
104
+
105
+ else:
106
+ tchord = tones_chord
107
+
108
+ seen = []
109
+
110
+ for i, cc in enumerate(cg):
111
+
112
+ if [cc[4], cc[6]] not in seen:
113
+
114
+ c = copy.deepcopy(cc)
115
+
116
+ if cc[2] > next_note_dtime:
117
+ c[2] = next_note_dtime
118
+
119
+ c[4] = ((c[4] // 12) * 12) + tchord[i]
120
+ c[5] += c[4] % 12
121
+
122
+ cho.append(c)
123
+
124
+ seen.append([cc[4], cc[6]])
125
+
126
+ else:
127
+ cho.extend(list(g))
128
+
129
+ return cho
130
+
131
+ # =================================================================================================
132
+
133
+ def Mix_Melody(input_midi,
134
+ input_find_best_match,
135
+ input_adjust_melody_notes_durations,
136
+ input_adjust_accompaniment_notes_durations,
137
+ input_output_as_solo_piano,
138
+ input_remove_drums,
139
+ input_output_tempo,
140
+ input_transform,
141
+ input_transpose_to_C4,
142
+ input_transpose_value
143
+ ):
144
+
145
+ print('=' * 70)
146
+ print('Req start time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
147
+ start_time = reqtime.time()
148
+
149
+ print('=' * 70)
150
+
151
+ fn = os.path.basename(input_midi.name)
152
+ fn1 = fn.split('.')[0]
153
+
154
+ print('-' * 70)
155
+ print('Requested settings:')
156
+ print('-' * 70)
157
+ print('Input file name:', fn)
158
+ print('Find best matches', input_find_best_match)
159
+ print('Adjust melody notes durations:', input_adjust_melody_notes_durations)
160
+ print('Adjust accompaniment notes durations:', input_adjust_accompaniment_notes_durations)
161
+ print('Output as Solo Piano:', input_output_as_solo_piano)
162
+ print('Remove drums:', input_remove_drums)
163
+ print('Output tempo:', input_output_tempo)
164
+ print('Transform:', input_transform)
165
+ print('Transpose to C4:', input_transpose_to_C4)
166
+ print('Transpose value:', input_transpose_value)
167
+ print('-' * 70)
168
+
169
+ #===============================================================================
170
+ raw_score = TMIDIX.midi2single_track_ms_score(input_midi.name)
171
+
172
+ #===============================================================================
173
+ # Enhanced score notes
174
+
175
+ raw_escore = TMIDIX.advanced_score_processor(raw_score, return_enhanced_score_notes=True)[0]
176
+
177
+ if len(raw_escore) > 0:
178
+
179
+ #===============================================================================
180
+ # Augmented enhanced score notes
181
+
182
+ src_escore = TMIDIX.recalculate_score_timings(TMIDIX.augment_enhanced_score_notes([e for e in raw_escore if e[6] < 80]))
183
+
184
+ src_cscore = TMIDIX.chordify_score([1000, src_escore])
185
+
186
+ src_melody = [c[0] for c in src_cscore][:256]
187
+
188
+ if input_transform == 'Flip Melody':
189
+ src_melody = TMIDIX.flip_enhanced_score_notes(src_melody)
190
+
191
+ elif input_transform == 'Reverse Melody':
192
+ src_melody = TMIDIX.reverse_enhanced_score_notes(src_melody)
193
+
194
+ mel_avg_time = TMIDIX.escore_notes_averages(src_melody)[0]
195
+
196
+ src_melody_pitches = [p[4] for p in src_melody]
197
+
198
+ src_harm_tones_chords = TMIDIX.harmonize_enhanced_melody_score_notes(src_melody)
199
+
200
+ #===============================================================================
201
+
202
+ matched_songs = [a for a in all_songs if a[2] == max(32, len(src_melody))]
203
+
204
+ random.shuffle(matched_songs)
205
+
206
+ max_match_ratio = -1
207
+ max_match_ratios_count = len(matched_songs)
208
+
209
+ if input_find_best_match:
210
+ new_song, max_match_ratio, max_match_ratios_count = find_similar_song(matched_songs, src_melody)
211
+ else:
212
+ new_song = random.choice(matched_songs)
213
+
214
+ print('Selected Monster Mono Melodies MIDI:', new_song[0])
215
+ print('Selected melody match ratio:', max_match_ratio)
216
+ print('Selected melody instrument:', TMIDIX.Number2patch[new_song[1]], '(', new_song[1], ')')
217
+ print('Melody notes count:', new_song[2])
218
+ print('Matched melodies pool count', max_match_ratios_count)
219
+
220
+ MIDI_Summary = 'Selected Monster Mono Melodies MIDI: ' + str(new_song[0]) + '\n'
221
+ MIDI_Summary += 'Selected melody match ratio: ' + str(max_match_ratio) + '\n'
222
+ MIDI_Summary += 'Selected melody instrument: ' + str(TMIDIX.Number2patch[new_song[1]]) + ' (' + str(new_song[1]) + ')' + '\n'
223
+ MIDI_Summary += 'Melody notes count: ' + str(new_song[2]) + '\n'
224
+ MIDI_Summary += 'Matched melodies pool count: ' + str(max_match_ratios_count)
225
+
226
+ fn1 += '_' + str(new_song[0]) + '_' + str(TMIDIX.Number2patch[new_song[1]]) + '_' + str(new_song[1]) + '_' + str(new_song[2])
227
+
228
+ trg_patch = new_song[1]
229
+
230
+ trg_song = copy.deepcopy(new_song[3])
231
+
232
+ mix_avg_time = TMIDIX.escore_notes_averages(trg_song)[0]
233
+ mix_mel_avg_time = TMIDIX.escore_notes_averages([e for e in trg_song if e[6] == trg_patch])[0]
234
+
235
+ TMIDIX.adjust_score_velocities(trg_song, 95)
236
+
237
+ cscore = TMIDIX.chordify_score([1000, trg_song])
238
+
239
+ print('=' * 70)
240
+ print('Done loading source and target MIDIs...!')
241
+ print('=' * 70)
242
+ print('Mixing...')
243
+
244
+ mixed_song = []
245
+
246
+ midx = 0
247
+ next_note_dtime = 255
248
+
249
+ for i, c in enumerate(cscore):
250
+ cho = copy.deepcopy(c)
251
+
252
+ patches = sorted(set([e[6] for e in c]))
253
+
254
+ if trg_patch in patches:
255
+
256
+ if input_adjust_melody_notes_durations:
257
+ if midx < len(src_melody)-1:
258
+ next_note_dtime = src_melody[midx+1][1] - src_melody[midx][1]
259
+ else:
260
+ next_note_dtime = 255
261
+
262
+ mixed_song.extend(mix_chord(c, src_harm_tones_chords[midx], trg_patch, src_melody_pitches[midx], next_note_dtime))
263
+
264
+ midx += 1
265
+
266
+ else:
267
+ if input_adjust_accompaniment_notes_durations:
268
+ if i < len(cscore)-1:
269
+ next_note_dtime = cscore[i+1][0][1] - cscore[i][0][1]
270
+ else:
271
+ next_note_dtime = 255
272
+
273
+ mixed_song.extend(mix_chord(cho, src_harm_tones_chords[midx], trg_patch, src_melody_pitches[midx], next_note_dtime))
274
+
275
+ if midx == len(src_melody):
276
+ break
277
+
278
+ print('=' * 70)
279
+ print('Done!')
280
+ print('=' * 70)
281
+
282
+ #===============================================================================
283
+
284
+ if input_output_as_solo_piano:
285
+
286
+ csong = TMIDIX.chordify_score([1000, mixed_song])
287
+
288
+ mixed_song = []
289
+
290
+ for c in csong:
291
+
292
+ pitches = [e[4] for e in c if e[6] == trg_patch]
293
+
294
+ for cc in c:
295
+
296
+ ccc = copy.deepcopy(cc)
297
+
298
+ if cc[3] != 9:
299
+ if cc[6] == trg_patch:
300
+ ccc[3] = 3
301
+ ccc[6] = 0
302
+ mixed_song.append(ccc)
303
+
304
+ else:
305
+ if cc[4] not in pitches:
306
+ ccc[3] = 0
307
+ ccc[6] = 0
308
+ mixed_song.append(ccc)
309
+ pitches.append(cc[4])
310
+
311
+ else:
312
+ mixed_song.append(ccc)
313
+
314
+ if input_remove_drums:
315
+ mixed_song = [e for e in mixed_song if e[3] != 9]
316
+
317
+ if input_output_tempo == 'Mix':
318
+
319
+ time_k = mel_avg_time / mix_avg_time
320
+
321
+ mixed_song = TMIDIX.adjust_escore_notes_timings(mixed_song, time_k)
322
+
323
+ elif input_output_tempo == 'Source Melody':
324
+
325
+ time_k = mel_avg_time / mix_mel_avg_time
326
+
327
+ mixed_song = TMIDIX.adjust_escore_notes_timings(mixed_song, time_k)
328
+
329
+ if input_transform == 'Flip Mix':
330
+ mixed_song = TMIDIX.flip_enhanced_score_notes(mixed_song)
331
+
332
+ elif input_transform == 'Reverse Mix':
333
+ mixed_song = TMIDIX.reverse_enhanced_score_notes(mixed_song)
334
+
335
+ if input_transpose_value != 0:
336
+ mixed_song = TMIDIX.transpose_escore_notes(mixed_song, input_transpose_value)
337
+
338
+ if input_transpose_to_C4:
339
+ mixed_song = TMIDIX.transpose_escore_notes_to_pitch(mixed_song)
340
+
341
+ #===============================================================================
342
+ print('Rendering results...')
343
+
344
+ print('=' * 70)
345
+ print('Sample INTs', mixed_song[:5])
346
+ print('=' * 70)
347
+
348
+ output_score, patches, overflow_patches = TMIDIX.patch_enhanced_score_notes(mixed_song)
349
+
350
+ detailed_stats = TMIDIX.Tegridy_ms_SONG_to_MIDI_Converter(output_score,
351
+ output_signature = 'Harmonic Melody MIDI Mixer',
352
+ output_file_name = fn1,
353
+ track_name='Project Los Angeles',
354
+ list_of_MIDI_patches=patches,
355
+ timings_multiplier=16
356
+ )
357
+
358
+ new_fn = fn1+'.mid'
359
+
360
+
361
+ audio = midi_to_colab_audio(new_fn,
362
+ soundfont_path=soundfont,
363
+ sample_rate=16000,
364
+ output_for_gradio=True
365
+ )
366
+
367
+ print('Done!')
368
+ print('=' * 70)
369
+
370
+ #========================================================
371
+
372
+ output_midi_title = str(fn1)
373
+ output_midi_summary = str(MIDI_Summary)
374
+ output_midi = str(new_fn)
375
+ output_audio = (16000, audio)
376
+
377
+ for o in output_score:
378
+ o[1] *= 16
379
+ o[2] *= 16
380
+
381
+ output_plot = TMIDIX.plot_ms_SONG(output_score, plot_title=output_midi_title, return_plt=True)
382
+
383
+ print('Output MIDI file name:', output_midi)
384
+ print('Output MIDI title:', output_midi_title)
385
+ print('Output MIDI summary:', MIDI_Summary)
386
+ print('=' * 70)
387
+
388
+
389
+ #========================================================
390
+
391
+ print('-' * 70)
392
+ print('Req end time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
393
+ print('-' * 70)
394
+ print('Req execution time:', (reqtime.time() - start_time), 'sec')
395
+
396
+ return output_midi_title, output_midi_summary, output_midi, output_audio, output_plot
397
+
398
+ # =================================================================================================
399
+
400
+ PDT = timezone('US/Pacific')
401
+
402
+ print('=' * 70)
403
+ print('App start time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
404
+ print('=' * 70)
405
+
406
+ soundfont = "SGM-v2.01-YamahaGrand-Guit-Bass-v2.7.sf2"
407
+
408
+ all_songs = TMIDIX.Tegridy_Any_Pickle_File_Reader('Monster_Mono_Melodies_MIDI_Dataset_65536_32_256')
409
+ print('=' * 70)
410
+
411
+ demo = gr.Blocks()
412
+ with demo:
413
+ gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>Harmonic Melody MIDI Mixer</h1>")
414
+ gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>Harmonize and mix any MIDI melody</h1>")
415
+ gr.Markdown(
416
+ "![Visitors](https://api.visitorbadge.io/api/visitors?path=asigalov61.Harmonic-Melody-MIDI-Mixer&style=flat)\n\n"
417
+ "This is a demo for TMIDIX Python module from tegridy-tools and Monster Mono Melodies MIDI Dataset\n\n"
418
+ "Check out [tegridy-tools](https://github.com/asigalov61/tegridy-tools) on GitHub!\n\n"
419
+ "Check out [Monster-MIDI-Dataset](https://github.com/asigalov61/Monster-MIDI-Dataset) on GitHub!\n\n"
420
+ )
421
+ gr.Markdown("## Upload your MIDI or select a sample example MIDI below")
422
+
423
+ input_midi = gr.File(label="Input MIDI", file_types=[".midi", ".mid", ".kar"])
424
+
425
+ gr.Markdown("## Select mixing options")
426
+
427
+ input_find_best_match = gr.Checkbox(label="Find best match", value=False)
428
+ input_adjust_melody_notes_durations = gr.Checkbox(label="Adjust melody notes durations", value=False)
429
+ input_adjust_accompaniment_notes_durations = gr.Checkbox(label="Adjust accompaniment notes durations", value=False)
430
+ input_output_as_solo_piano = gr.Checkbox(label="Output as Solo Piano", value=False)
431
+ input_remove_drums = gr.Checkbox(label="Remove drums from output", value=False)
432
+ input_output_tempo = gr.Radio(["Mix Melody", "Source Melody", "Mix"], value="Mix Melody", label="Output tempo")
433
+ input_transform = gr.Radio(["As-is", "Flip Melody", "Reverse Melody", "Flip Mix", "Reverse Mix"], value="As-is", label="Transform")
434
+ input_transpose_value = gr.Slider(-12, 12, value=0, step=1, label="Transpose value")
435
+ input_transpose_to_C4 = gr.Checkbox(label="Transpose to C4", value=False)
436
+
437
+ run_btn = gr.Button("mix melody", variant="primary")
438
+
439
+ gr.Markdown("## Output results")
440
+
441
+ output_midi_title = gr.Textbox(label="Output MIDI title")
442
+ output_midi_summary = gr.Textbox(label="Output MIDI summary")
443
+ output_audio = gr.Audio(label="Output MIDI audio", format="mp3", elem_id="midi_audio")
444
+ output_plot = gr.Plot(label="Output MIDI score plot")
445
+ output_midi = gr.File(label="Output MIDI file", file_types=[".mid"])
446
+
447
+
448
+ run_event = run_btn.click(Mix_Melody, [input_midi,
449
+ input_find_best_match,
450
+ input_adjust_melody_notes_durations,
451
+ input_adjust_accompaniment_notes_durations,
452
+ input_output_as_solo_piano,
453
+ input_remove_drums,
454
+ input_output_tempo,
455
+ input_transform,
456
+ input_transpose_to_C4,
457
+ input_transpose_value
458
+ ],
459
+ [output_midi_title, output_midi_summary, output_midi, output_audio, output_plot])
460
+
461
+ gr.Examples(
462
+ [["Abracadabra-Sample-Melody.mid", True, True, True, False, False, "Mix Melody", "As-is", False, 0],
463
+ ["Sparks-Fly-Sample-Melody.mid", True, True, True, False, False, "Mix Melody", "As-is", False, 0],
464
+ ],
465
+ [input_midi,
466
+ input_find_best_match,
467
+ input_adjust_melody_notes_durations,
468
+ input_adjust_accompaniment_notes_durations,
469
+ input_output_as_solo_piano,
470
+ input_remove_drums,
471
+ input_output_tempo,
472
+ input_transform,
473
+ input_transpose_to_C4,
474
+ input_transpose_value
475
+ ],
476
+ [output_midi_title, output_midi_summary, output_midi, output_audio, output_plot],
477
+ Mix_Melody,
478
+ cache_examples=True,
479
+ )
480
+
481
+ demo.launch()
midi_to_colab_audio.py ADDED
The diff for this file is too large to render. See raw diff
 
packages.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ fluidsynth
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio
2
+ matplotlib