Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -58,6 +58,61 @@ print('=' * 70)
|
|
| 58 |
|
| 59 |
#==================================================================================
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
def Mix_Loops(input_midi,
|
| 62 |
generation_type,
|
| 63 |
melody_patch,
|
|
@@ -90,9 +145,7 @@ def Mix_Loops(input_midi,
|
|
| 90 |
|
| 91 |
#==================================================================
|
| 92 |
|
| 93 |
-
score, score_list = load_midi(input_midi.name)
|
| 94 |
|
| 95 |
-
print('Sample score events', score[:12])
|
| 96 |
|
| 97 |
#==================================================================
|
| 98 |
|
|
@@ -110,41 +163,12 @@ def Mix_Loops(input_midi,
|
|
| 110 |
print('Rendering results...')
|
| 111 |
|
| 112 |
print('=' * 70)
|
| 113 |
-
print('Sample
|
| 114 |
print('=' * 70)
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
time = 0
|
| 121 |
-
dur = 0
|
| 122 |
-
vel = 90
|
| 123 |
-
pitch = 0
|
| 124 |
-
channel = 0
|
| 125 |
-
patch = 0
|
| 126 |
-
|
| 127 |
-
channels_map = [0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 9, 12, 13, 14, 15]
|
| 128 |
-
patches_map = [40, 0, 10, 19, 24, 35, 40, 52, 56, 9, 65, 73, 0, 0, 0, 0]
|
| 129 |
-
velocities_map = [125, 80, 100, 80, 90, 100, 100, 80, 110, 110, 110, 110, 80, 80, 80, 80]
|
| 130 |
-
|
| 131 |
-
for m in final_song:
|
| 132 |
-
|
| 133 |
-
if 0 <= m < 128:
|
| 134 |
-
time += m * 32
|
| 135 |
-
|
| 136 |
-
elif 128 < m < 256:
|
| 137 |
-
dur = (m-128) * 32
|
| 138 |
-
|
| 139 |
-
elif 256 < m < 1792:
|
| 140 |
-
cha = (m-256) // 128
|
| 141 |
-
pitch = (m-256) % 128
|
| 142 |
-
|
| 143 |
-
channel = channels_map[cha]
|
| 144 |
-
patch = patches_map[channel]
|
| 145 |
-
vel = velocities_map[channel]
|
| 146 |
-
|
| 147 |
-
song_f.append(['note', time, dur, channel, pitch, vel, patch])
|
| 148 |
|
| 149 |
fn1 = "MIDI-Loops-Mixer-Composition"
|
| 150 |
|
|
@@ -173,7 +197,11 @@ def Mix_Loops(input_midi,
|
|
| 173 |
output_midi = str(new_fn)
|
| 174 |
output_audio = (16000, audio)
|
| 175 |
|
| 176 |
-
output_plot = TMIDIX.plot_ms_SONG(song_f,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
|
| 178 |
print('Output MIDI file name:', output_midi)
|
| 179 |
print('=' * 70)
|
|
|
|
| 58 |
|
| 59 |
#==================================================================================
|
| 60 |
|
| 61 |
+
def find_matches(src_array, trg_array):
|
| 62 |
+
|
| 63 |
+
matches = np.all(src_array == trg_array, axis=1)
|
| 64 |
+
matching_indices = np.where(matches)[0]
|
| 65 |
+
|
| 66 |
+
return matching_indices.tolist()
|
| 67 |
+
|
| 68 |
+
#==================================================================================
|
| 69 |
+
|
| 70 |
+
def find_closest_tuple(tuples_list, src_tuple):
|
| 71 |
+
def euclidean_distance(t1, t2):
|
| 72 |
+
return sum((a - b) ** 2 for a, b in zip(t1, t2)) ** 0.5
|
| 73 |
+
|
| 74 |
+
closest_tuple = None
|
| 75 |
+
min_distance = float('inf')
|
| 76 |
+
|
| 77 |
+
for t in tuples_list:
|
| 78 |
+
distance = euclidean_distance(t, src_tuple)
|
| 79 |
+
if distance < min_distance:
|
| 80 |
+
min_distance = distance
|
| 81 |
+
closest_tuple = t
|
| 82 |
+
|
| 83 |
+
return closest_tuple
|
| 84 |
+
|
| 85 |
+
#==================================================================================
|
| 86 |
+
|
| 87 |
+
def find_best_midx(midxs, trg_midx):
|
| 88 |
+
|
| 89 |
+
all_midxs = midxs + [trg_midx]
|
| 90 |
+
sidxs = [int(midx // 6) for midx in all_midxs]
|
| 91 |
+
|
| 92 |
+
times_durs = []
|
| 93 |
+
|
| 94 |
+
for sidx in sidxs:
|
| 95 |
+
|
| 96 |
+
score = midi_loops[sidx][2]
|
| 97 |
+
dtimes = [e[0] for e in score if e[0] != 0]
|
| 98 |
+
durs = [e[1] for e in score]
|
| 99 |
+
|
| 100 |
+
avg_dtime = int(sum(dtimes) / len(dtimes))
|
| 101 |
+
avg_dur = int(sum(durs) / len(durs))
|
| 102 |
+
|
| 103 |
+
mode_dtimes= statistics.mode(dtimes)
|
| 104 |
+
mode_durs = statistics.mode(durs)
|
| 105 |
+
|
| 106 |
+
times_durs.append([avg_dtime, avg_dur, mode_dtimes, mode_durs])
|
| 107 |
+
|
| 108 |
+
best_time_dur = find_closest_tuple(times_durs[:-1], times_durs[-1])
|
| 109 |
+
|
| 110 |
+
best_midx = midxs[times_durs.index(best_time_dur)]
|
| 111 |
+
|
| 112 |
+
return best_midx
|
| 113 |
+
|
| 114 |
+
#==================================================================================
|
| 115 |
+
|
| 116 |
def Mix_Loops(input_midi,
|
| 117 |
generation_type,
|
| 118 |
melody_patch,
|
|
|
|
| 145 |
|
| 146 |
#==================================================================
|
| 147 |
|
|
|
|
| 148 |
|
|
|
|
| 149 |
|
| 150 |
#==================================================================
|
| 151 |
|
|
|
|
| 163 |
print('Rendering results...')
|
| 164 |
|
| 165 |
print('=' * 70)
|
| 166 |
+
print('Sample MIDI events:', final_song[:3])
|
| 167 |
print('=' * 70)
|
| 168 |
|
| 169 |
+
final_song_abs = TMIDIX.delta_score_to_abs_score(final_song)
|
| 170 |
+
|
| 171 |
+
output_score, patches, overflow_patches = TMIDIX.patch_enhanced_score_notes(final_song_abs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
fn1 = "MIDI-Loops-Mixer-Composition"
|
| 174 |
|
|
|
|
| 197 |
output_midi = str(new_fn)
|
| 198 |
output_audio = (16000, audio)
|
| 199 |
|
| 200 |
+
output_plot = TMIDIX.plot_ms_SONG(song_f,
|
| 201 |
+
plot_title=output_midi,
|
| 202 |
+
timings_multiplier=16,
|
| 203 |
+
return_plt=True
|
| 204 |
+
)
|
| 205 |
|
| 206 |
print('Output MIDI file name:', output_midi)
|
| 207 |
print('=' * 70)
|