Spaces:
Running
Running
asigalov61
commited on
Commit
•
8979d67
1
Parent(s):
6435eef
Update app.py
Browse files
app.py
CHANGED
@@ -23,6 +23,57 @@ in_space = os.getenv("SYSTEM") == "spaces"
|
|
23 |
|
24 |
# =================================================================================================
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
def mix_chord(chord, tones_chord, mel_patch, mel_pitch):
|
27 |
|
28 |
cho = []
|
@@ -60,7 +111,7 @@ def mix_chord(chord, tones_chord, mel_patch, mel_pitch):
|
|
60 |
|
61 |
# =================================================================================================
|
62 |
|
63 |
-
def MixMelody(input_midi):
|
64 |
print('=' * 70)
|
65 |
print('Req start time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
|
66 |
start_time = reqtime.time()
|
@@ -72,6 +123,7 @@ def MixMelody(input_midi):
|
|
72 |
|
73 |
print('-' * 70)
|
74 |
print('Input file name:', fn)
|
|
|
75 |
print('-' * 70)
|
76 |
|
77 |
#===============================================================================
|
@@ -101,9 +153,13 @@ def MixMelody(input_midi):
|
|
101 |
|
102 |
matched_songs = [a for a in all_songs if a[2] == max(32, len(src_melody))]
|
103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
|
105 |
-
new_song = random.choice(matched_songs)
|
106 |
-
|
107 |
print('Selected Monster Mono Melodies MIDI:', new_song[0])
|
108 |
print('Selected melody instrument:', TMIDIX.Number2patch[new_song[1]], '(', new_song[1], ')')
|
109 |
print('Melody notes count:', new_song[2])
|
@@ -237,6 +293,7 @@ if __name__ == "__main__":
|
|
237 |
gr.Markdown("## Upload your MIDI or select a sample example MIDI below")
|
238 |
|
239 |
input_midi = gr.File(label="Input MIDI", file_types=[".midi", ".mid", ".kar"])
|
|
|
240 |
|
241 |
run_btn = gr.Button("mix melody", variant="primary")
|
242 |
|
@@ -249,14 +306,14 @@ if __name__ == "__main__":
|
|
249 |
output_midi = gr.File(label="Output MIDI file", file_types=[".mid"])
|
250 |
|
251 |
|
252 |
-
run_event = run_btn.click(MixMelody, [input_midi],
|
253 |
[output_midi_title, output_midi_summary, output_midi, output_audio, output_plot])
|
254 |
|
255 |
gr.Examples(
|
256 |
-
[["Abracadabra-Sample-Melody.mid"],
|
257 |
-
["Sparks-Fly-Sample-Melody.mid"],
|
258 |
],
|
259 |
-
[input_midi],
|
260 |
[output_midi_title, output_midi_summary, output_midi, output_audio, output_plot],
|
261 |
MixMelody,
|
262 |
cache_examples=True,
|
|
|
23 |
|
24 |
# =================================================================================================
|
25 |
|
26 |
+
def pitches_counts(melody_score):
|
27 |
+
|
28 |
+
pitches = [p[4] for p in melody_score]
|
29 |
+
|
30 |
+
pcounts = []
|
31 |
+
|
32 |
+
count = 0
|
33 |
+
pp = pitches[0]
|
34 |
+
for p in pitches:
|
35 |
+
if p == pp:
|
36 |
+
count += 1
|
37 |
+
else:
|
38 |
+
pcounts.append(count)
|
39 |
+
count = 1
|
40 |
+
pp = p
|
41 |
+
|
42 |
+
return pcounts
|
43 |
+
|
44 |
+
=================================================================================================
|
45 |
+
|
46 |
+
def find_similar_song(songs, src_melody):
|
47 |
+
|
48 |
+
src_pcount = pitches_counts(src_melody)
|
49 |
+
|
50 |
+
ratios = []
|
51 |
+
|
52 |
+
for s in songs:
|
53 |
+
patch = s[1]
|
54 |
+
|
55 |
+
trg_melody = [e for e in s[3] if e[6] == patch]
|
56 |
+
trg_pcount = pitches_counts(trg_melody)
|
57 |
+
|
58 |
+
pcount = 0
|
59 |
+
|
60 |
+
for i, c in enumerate(src_pcount):
|
61 |
+
if c == trg_pcount[i]:
|
62 |
+
pcount += 1
|
63 |
+
else:
|
64 |
+
break
|
65 |
+
|
66 |
+
ratios.append(pcount / len(src_pcount))
|
67 |
+
|
68 |
+
max_ratio = max(ratios)
|
69 |
+
|
70 |
+
print(max_ratio)
|
71 |
+
print(ratios.count(1.0))
|
72 |
+
|
73 |
+
return songs[ratios.index(max_ratio)]
|
74 |
+
|
75 |
+
=================================================================================================
|
76 |
+
|
77 |
def mix_chord(chord, tones_chord, mel_patch, mel_pitch):
|
78 |
|
79 |
cho = []
|
|
|
111 |
|
112 |
# =================================================================================================
|
113 |
|
114 |
+
def MixMelody(input_midi, input_find_best_match):
|
115 |
print('=' * 70)
|
116 |
print('Req start time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
|
117 |
start_time = reqtime.time()
|
|
|
123 |
|
124 |
print('-' * 70)
|
125 |
print('Input file name:', fn)
|
126 |
+
print('Find best matches', input_find_best_match)
|
127 |
print('-' * 70)
|
128 |
|
129 |
#===============================================================================
|
|
|
153 |
|
154 |
matched_songs = [a for a in all_songs if a[2] == max(32, len(src_melody))]
|
155 |
|
156 |
+
random.shuffle(matched_songs)
|
157 |
+
|
158 |
+
if input_find_best_match:
|
159 |
+
new_song = find_similar_song(matched_songs, src_melody)
|
160 |
+
else:
|
161 |
+
new_song = random.choice(matched_songs)
|
162 |
|
|
|
|
|
163 |
print('Selected Monster Mono Melodies MIDI:', new_song[0])
|
164 |
print('Selected melody instrument:', TMIDIX.Number2patch[new_song[1]], '(', new_song[1], ')')
|
165 |
print('Melody notes count:', new_song[2])
|
|
|
293 |
gr.Markdown("## Upload your MIDI or select a sample example MIDI below")
|
294 |
|
295 |
input_midi = gr.File(label="Input MIDI", file_types=[".midi", ".mid", ".kar"])
|
296 |
+
input_find_best_match = gr.Checkbox(label="Find best match", value=True)
|
297 |
|
298 |
run_btn = gr.Button("mix melody", variant="primary")
|
299 |
|
|
|
306 |
output_midi = gr.File(label="Output MIDI file", file_types=[".mid"])
|
307 |
|
308 |
|
309 |
+
run_event = run_btn.click(MixMelody, [input_midi, input_find_best_match],
|
310 |
[output_midi_title, output_midi_summary, output_midi, output_audio, output_plot])
|
311 |
|
312 |
gr.Examples(
|
313 |
+
[["Abracadabra-Sample-Melody.mid", True],
|
314 |
+
["Sparks-Fly-Sample-Melody.mid", True],
|
315 |
],
|
316 |
+
[input_midi, input_find_best_match],
|
317 |
[output_midi_title, output_midi_summary, output_midi, output_audio, output_plot],
|
318 |
MixMelody,
|
319 |
cache_examples=True,
|