asigalov61 commited on
Commit
7d39313
1 Parent(s): f7bac0d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -142
app.py CHANGED
@@ -6,7 +6,6 @@ import time as reqtime
6
  import datetime
7
  from pytz import timezone
8
 
9
- from itertools import groupby
10
  import copy
11
 
12
  import gradio as gr
@@ -14,119 +13,12 @@ import gradio as gr
14
  import random
15
 
16
  from midi_to_colab_audio import midi_to_colab_audio
17
- import TMIDIX
18
-
19
- import matplotlib.pyplot as plt
20
-
21
- # =================================================================================================
22
-
23
- def pitches_counts(melody_score):
24
-
25
- pitches = [p[4] for p in melody_score]
26
-
27
- pcounts = []
28
-
29
- count = 0
30
- pp = -1
31
-
32
- for p in pitches:
33
- if p == pp:
34
- count += 1
35
- pcounts.append(count)
36
- else:
37
- count = 0
38
- pcounts.append(count)
39
- pp = p
40
-
41
- return pcounts
42
-
43
- # =================================================================================================
44
-
45
- def find_similar_song(songs, src_melody):
46
-
47
- src_pcount = pitches_counts(src_melody)
48
-
49
- ratios = []
50
-
51
- for s in songs:
52
- patch = s[1]
53
-
54
- trg_melody = [e for e in s[3] if e[6] == patch]
55
- trg_pcount = pitches_counts(trg_melody)
56
-
57
- pcount = 0
58
-
59
- for i, c in enumerate(src_pcount):
60
- if c == trg_pcount[i]:
61
- pcount += 1
62
-
63
- ratios.append(pcount / len(src_pcount))
64
-
65
- max_ratio = max(ratios)
66
-
67
- return songs[ratios.index(max_ratio)], max_ratio, ratios.count(max_ratio)
68
-
69
- # =================================================================================================
70
 
71
- def mix_chord(chord, tones_chord, mel_patch, mel_pitch, next_note_dtime):
72
-
73
- cho = []
74
-
75
- for k, g in groupby(sorted(chord, key=lambda x: x[6]), lambda x: x[6]):
76
-
77
- if k != 128:
78
- if k == mel_patch:
79
-
80
- cg = list(g)
81
-
82
- c = copy.deepcopy(cg[0])
83
-
84
- if cg[0][2] > next_note_dtime:
85
- c[2] = next_note_dtime
86
-
87
- c[4] = mel_pitch
88
- c[5] = 105 + (mel_pitch % 12)
89
-
90
- cho.append(c)
91
-
92
- else:
93
- cg = list(g)
94
-
95
- tclen = len(tones_chord)
96
-
97
- if len(cg) > tclen:
98
- tchord = tones_chord + [random.choice(tones_chord) for _ in range(len(cg)-tclen)]
99
-
100
- else:
101
- tchord = tones_chord
102
-
103
- seen = []
104
-
105
- for i, cc in enumerate(cg):
106
-
107
- if [cc[4], cc[6]] not in seen:
108
-
109
- c = copy.deepcopy(cc)
110
-
111
- if cc[2] > next_note_dtime:
112
- c[2] = next_note_dtime
113
-
114
- c[4] = ((c[4] // 12) * 12) + tchord[i]
115
- c[5] += c[4] % 12
116
-
117
- cho.append(c)
118
-
119
- seen.append([cc[4], cc[6]])
120
-
121
- else:
122
- cho.extend(list(g))
123
-
124
- return cho
125
 
126
  # =================================================================================================
127
 
128
- def Mix_Melody(input_midi,
129
- input_find_best_match,
130
  input_adjust_melody_notes_durations,
131
  input_adjust_accompaniment_notes_durations,
132
  input_output_as_solo_piano,
@@ -403,22 +295,20 @@ if __name__ == "__main__":
403
 
404
  soundfont = "SGM-v2.01-YamahaGrand-Guit-Bass-v2.7.sf2"
405
 
406
- all_songs = TMIDIX.Tegridy_Any_Pickle_File_Reader('Monster_Mono_Melodies_MIDI_Dataset_65536_32_256')
407
  print('=' * 70)
408
 
409
  app = gr.Blocks()
 
410
  with app:
411
- gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>Harmonic Melody MIDI Mixer</h1>")
412
- gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>Harmonize and mix any MIDI melody</h1>")
413
  gr.Markdown(
414
- "![Visitors](https://api.visitorbadge.io/api/visitors?path=asigalov61.Harmonic-Melody-MIDI-Mixer&style=flat)\n\n"
415
- "This is a demo for TMIDIX Python module from tegridy-tools and Monster Mono Melodies MIDI Dataset\n\n"
416
  "Check out [tegridy-tools](https://github.com/asigalov61/tegridy-tools) on GitHub!\n\n"
417
- "Check out [Monster-MIDI-Dataset](https://github.com/asigalov61/Monster-MIDI-Dataset) on GitHub!\n\n"
418
  )
419
- gr.Markdown("## Upload your MIDI or select a sample example MIDI below")
420
-
421
- input_midi = gr.File(label="Input MIDI", file_types=[".midi", ".mid", ".kar"])
422
 
423
  gr.Markdown("## Select mixing options")
424
 
@@ -432,7 +322,7 @@ if __name__ == "__main__":
432
  input_transpose_value = gr.Slider(-12, 12, value=0, step=1, label="Transpose value")
433
  input_transpose_to_C4 = gr.Checkbox(label="Transpose to C4", value=False)
434
 
435
- run_btn = gr.Button("mix melody", variant="primary")
436
 
437
  gr.Markdown("## Output results")
438
 
@@ -443,8 +333,7 @@ if __name__ == "__main__":
443
  output_midi = gr.File(label="Output MIDI file", file_types=[".mid"])
444
 
445
 
446
- run_event = run_btn.click(Mix_Melody, [input_midi,
447
- input_find_best_match,
448
  input_adjust_melody_notes_durations,
449
  input_adjust_accompaniment_notes_durations,
450
  input_output_as_solo_piano,
@@ -456,24 +345,4 @@ if __name__ == "__main__":
456
  ],
457
  [output_midi_title, output_midi_summary, output_midi, output_audio, output_plot])
458
 
459
- gr.Examples(
460
- [["Abracadabra-Sample-Melody.mid", True, True, True, False, False, "Mix Melody", "As-is", False, 0],
461
- ["Sparks-Fly-Sample-Melody.mid", True, True, True, False, False, "Mix Melody", "As-is", False, 0],
462
- ],
463
- [input_midi,
464
- input_find_best_match,
465
- input_adjust_melody_notes_durations,
466
- input_adjust_accompaniment_notes_durations,
467
- input_output_as_solo_piano,
468
- input_remove_drums,
469
- input_output_tempo,
470
- input_transform,
471
- input_transpose_to_C4,
472
- input_transpose_value
473
- ],
474
- [output_midi_title, output_midi_summary, output_midi, output_audio, output_plot],
475
- Mix_Melody,
476
- cache_examples=True,
477
- )
478
-
479
  app.queue().launch()
 
6
  import datetime
7
  from pytz import timezone
8
 
 
9
  import copy
10
 
11
  import gradio as gr
 
13
  import random
14
 
15
  from midi_to_colab_audio import midi_to_colab_audio
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
+ import TMIDIX
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
  # =================================================================================================
20
 
21
+ def Mix_Chords(input_find_best_match,
 
22
  input_adjust_melody_notes_durations,
23
  input_adjust_accompaniment_notes_durations,
24
  input_output_as_solo_piano,
 
295
 
296
  soundfont = "SGM-v2.01-YamahaGrand-Guit-Bass-v2.7.sf2"
297
 
298
+ all_chords_chunks = TMIDIX.Tegridy_Any_Pickle_File_Reader('Los_Angeles_Chords_Songs_MIDI_Dataset_79898_128_256')
299
  print('=' * 70)
300
 
301
  app = gr.Blocks()
302
+
303
  with app:
304
+ gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>MIDI Chords Mixer</h1>")
305
+ gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>Mix chords from one MIDI to another MIDI</h1>")
306
  gr.Markdown(
307
+ "![Visitors](https://api.visitorbadge.io/api/visitors?path=asigalov61.MIDI-Chords-Mixer&style=flat)\n\n"
308
+ "This is a demo for TMIDIX Python module from tegridy-tools and Los Angeles MIDI Dataset\n\n"
309
  "Check out [tegridy-tools](https://github.com/asigalov61/tegridy-tools) on GitHub!\n\n"
310
+ "Check out [Los Angeles MIDI Dataset](https://github.com/asigalov61/Los-Angeles-MIDI-Dataset) on GitHub!\n\n"
311
  )
 
 
 
312
 
313
  gr.Markdown("## Select mixing options")
314
 
 
322
  input_transpose_value = gr.Slider(-12, 12, value=0, step=1, label="Transpose value")
323
  input_transpose_to_C4 = gr.Checkbox(label="Transpose to C4", value=False)
324
 
325
+ run_btn = gr.Button("mix chords", variant="primary")
326
 
327
  gr.Markdown("## Output results")
328
 
 
333
  output_midi = gr.File(label="Output MIDI file", file_types=[".mid"])
334
 
335
 
336
+ run_event = run_btn.click(Mix_Chords, [input_find_best_match,
 
337
  input_adjust_melody_notes_durations,
338
  input_adjust_accompaniment_notes_durations,
339
  input_output_as_solo_piano,
 
345
  ],
346
  [output_midi_title, output_midi_summary, output_midi, output_audio, output_plot])
347
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
348
  app.queue().launch()