asigalov61 commited on
Commit
67d849b
·
verified ·
1 Parent(s): d12db4a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -138
app.py CHANGED
@@ -1,16 +1,16 @@
1
- # https://huggingface.co/spaces/asigalov61/Harmonic-Melody-MIDI-Mixer
2
-
3
- import os.path
4
 
5
  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
13
 
 
14
  import random
15
 
16
  from midi_to_colab_audio import midi_to_colab_audio
@@ -20,122 +20,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,
133
- input_remove_drums,
134
- input_output_tempo,
135
- input_transform,
136
- input_transpose_to_C4,
137
- input_transpose_value
138
- ):
139
 
140
  print('=' * 70)
141
  print('Req start time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
@@ -402,20 +299,20 @@ if __name__ == "__main__":
402
  print('=' * 70)
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"])
@@ -443,18 +340,18 @@ 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,
451
- input_remove_drums,
452
- input_output_tempo,
453
- input_transform,
454
- input_transpose_to_C4,
455
- input_transpose_value
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],
@@ -472,7 +369,7 @@ if __name__ == "__main__":
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
 
 
1
+ #=========================================================================
2
+ # https://huggingface.co/spaces/asigalov61/Parsons-Code-Melody-Transformer
3
+ #=========================================================================
4
 
5
  import time as reqtime
6
  import datetime
7
  from pytz import timezone
8
 
 
9
  import copy
10
 
11
  import gradio as gr
12
 
13
+ from x_transformer_1_23_2 import *
14
  import random
15
 
16
  from midi_to_colab_audio import midi_to_colab_audio
 
20
 
21
  # =================================================================================================
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  # =================================================================================================
24
 
25
+ def Generate_Melody(input_midi,
26
+ input_find_best_match,
27
+ input_adjust_melody_notes_durations,
28
+ input_adjust_accompaniment_notes_durations,
29
+ input_output_as_solo_piano,
30
+ input_remove_drums,
31
+ input_output_tempo,
32
+ input_transform,
33
+ input_transpose_to_C4,
34
+ input_transpose_value
35
+ ):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  print('=' * 70)
38
  print('Req start time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
 
299
  print('=' * 70)
300
 
301
  soundfont = "SGM-v2.01-YamahaGrand-Guit-Bass-v2.7.sf2"
302
+
 
 
 
303
  app = gr.Blocks()
304
+
305
  with app:
306
+
307
+ gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>Parsons Code Melody Transformer</h1>")
308
+ gr.Markdown("<h1 style='text-align: center; margin-bottom: 1rem'>Generate unique melodies from Parsons codes</h1>")
309
  gr.Markdown(
310
+ "![Visitors](https://api.visitorbadge.io/api/visitors?path=asigalov61.Parsons-Code-Melody-Transformer&style=flat)\n\n"
311
  "This is a demo for TMIDIX Python module from tegridy-tools and Monster Mono Melodies MIDI Dataset\n\n"
312
  "Check out [tegridy-tools](https://github.com/asigalov61/tegridy-tools) on GitHub!\n\n"
313
  "Check out [Monster-MIDI-Dataset](https://github.com/asigalov61/Monster-MIDI-Dataset) on GitHub!\n\n"
314
  )
315
+
316
  gr.Markdown("## Upload your MIDI or select a sample example MIDI below")
317
 
318
  input_midi = gr.File(label="Input MIDI", file_types=[".midi", ".mid", ".kar"])
 
340
  output_midi = gr.File(label="Output MIDI file", file_types=[".mid"])
341
 
342
 
343
+ run_event = run_btn.click(Generate_Melody, [input_midi,
344
+ input_find_best_match,
345
+ input_adjust_melody_notes_durations,
346
+ input_adjust_accompaniment_notes_durations,
347
+ input_output_as_solo_piano,
348
+ input_remove_drums,
349
+ input_output_tempo,
350
+ input_transform,
351
+ input_transpose_to_C4,
352
+ input_transpose_value
353
+ ],
354
+ [output_midi_title, output_midi_summary, output_midi, output_audio, output_plot])
355
 
356
  gr.Examples(
357
  [["Abracadabra-Sample-Melody.mid", True, True, True, False, False, "Mix Melody", "As-is", False, 0],
 
369
  input_transpose_value
370
  ],
371
  [output_midi_title, output_midi_summary, output_midi, output_audio, output_plot],
372
+ Generate_Melody,
373
  cache_examples=True,
374
  )
375