Update app.py
Browse files
app.py
CHANGED
@@ -115,166 +115,166 @@ def Generate_Chords(input_midi, input_num_prime_chords, input_num_gen_chords, in
|
|
115 |
comp_dtimes = [max(1, min(127, b-a)) for a, b in zip(comp_times[:-1], comp_times[1:]) if b-a != 0]
|
116 |
avg_comp_dtime = max(0, min(127, int(sum(comp_dtimes) / len(comp_dtimes))))
|
117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
#=======================================================
|
119 |
-
#
|
120 |
-
|
|
|
|
|
|
|
|
|
121 |
#=======================================================
|
122 |
-
#
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
if avg_vel < 60:
|
128 |
-
TMIDIX.adjust_score_velocities(escore_notes, avg_vel * 2)
|
129 |
-
|
130 |
-
melody_chords = []
|
131 |
-
melody_chords2 = []
|
132 |
-
mel_cho = []
|
133 |
-
|
134 |
#=======================================================
|
135 |
-
#
|
136 |
-
|
137 |
-
|
138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
139 |
else:
|
140 |
-
|
141 |
-
|
142 |
-
melody_chords.extend([1929, drums_present])
|
143 |
-
mel_cho.extend([1929, drums_present])
|
144 |
-
|
145 |
-
#=======================================================
|
146 |
-
# Composition patches list
|
147 |
-
|
148 |
-
melody_chords.extend([i+1932 for i in instruments_list_without_drums])
|
149 |
-
mel_cho.extend([i+1932 for i in instruments_list_without_drums])
|
150 |
-
#=======================================================
|
151 |
-
# Composition avg pitch and dtime
|
152 |
-
|
153 |
-
mode_instruments_pitch = statistics.mode([e[4] for e in escore_notes if e[6] < 80])
|
154 |
-
|
155 |
-
melody_chords.extend([2060+mode_instruments_pitch, 2188+avg_comp_dtime])
|
156 |
-
mel_cho.extend([2060+mode_instruments_pitch, 2188+avg_comp_dtime])
|
157 |
-
|
158 |
-
melody_chords2.append(mel_cho)
|
159 |
-
|
160 |
#=======================================================
|
161 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
162 |
#=======================================================
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
c.sort(key=lambda x: x[6]) # Sorting by patch
|
171 |
-
|
172 |
-
#=======================================================
|
173 |
-
# Outro seq
|
174 |
-
|
175 |
-
#if len(cscore) > 256:
|
176 |
-
# if len(cscore) - i == 64:
|
177 |
-
# melody_chords.extend([2236])
|
178 |
-
|
179 |
-
#=======================================================
|
180 |
-
# Timings...
|
181 |
-
|
182 |
-
# Cliping all values...
|
183 |
-
delta_time = max(0, min(127, c[0][1]-pc[0][1]))
|
184 |
-
|
185 |
-
#=======================================================
|
186 |
-
# Chords...
|
187 |
-
|
188 |
-
cpitches = sorted([e[4] for e in c if e[3] != 9])
|
189 |
-
dpitches = [e[4] for e in c if e[3] == 9]
|
190 |
-
|
191 |
-
tones_chord = sorted(set([p % 12 for p in cpitches]))
|
192 |
-
|
193 |
-
if tones_chord:
|
194 |
-
|
195 |
-
if tones_chord not in TMIDIX.ALL_CHORDS_SORTED:
|
196 |
-
tones_chord_tok = 644
|
197 |
-
tones_chord_tok = TMIDIX.ALL_CHORDS_SORTED.index(TMIDIX.advanced_check_and_fix_tones_chord(tones_chord, cpitches[-1]))
|
198 |
-
|
199 |
-
else:
|
200 |
-
tones_chord_tok = TMIDIX.ALL_CHORDS_SORTED.index(tones_chord) # 321
|
201 |
-
|
202 |
-
if dpitches:
|
203 |
-
|
204 |
-
if tones_chord_tok == 644:
|
205 |
-
tones_chord_tok = 645
|
206 |
-
else:
|
207 |
-
tones_chord_tok += 321
|
208 |
-
|
209 |
-
else:
|
210 |
-
tones_chord_tok = 643 # Drums-only chord
|
211 |
-
|
212 |
#=======================================================
|
213 |
-
#
|
214 |
-
|
215 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
216 |
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
#=======================================================
|
228 |
-
# Duration
|
229 |
-
dur = max(0, min(63, int(max(0, e[2] // 4) * 2)))
|
230 |
-
|
231 |
-
# Pitch
|
232 |
-
ptc = max(1, min(127, e[4]))
|
233 |
-
|
234 |
-
# Octo-velocity
|
235 |
-
vel = max(8, min(127, (max(1, e[5] // 8) * 8)))
|
236 |
-
velocity = round(vel / 15)-1
|
237 |
-
|
238 |
-
# Patch
|
239 |
-
pat = max(0, min(128, e[6]))
|
240 |
-
|
241 |
-
if 7 < pat < 80:
|
242 |
-
ptc += 128
|
243 |
-
|
244 |
-
elif 79 < pat < 128:
|
245 |
-
ptc += 256
|
246 |
-
|
247 |
-
elif pat == 128:
|
248 |
-
ptc += 384
|
249 |
-
|
250 |
-
#=======================================================
|
251 |
-
# FINAL NOTE SEQ
|
252 |
-
|
253 |
-
# Writing final note asynchronously
|
254 |
-
|
255 |
-
dur_vel = (8 * dur) + velocity # 512
|
256 |
-
|
257 |
-
if pat != pp:
|
258 |
-
melody_chords.extend([pat+774, ptc+904, dur_vel+1416]) # 1928
|
259 |
-
mel_cho.extend([pat+774, ptc+904, dur_vel+1416])
|
260 |
-
|
261 |
-
else:
|
262 |
-
melody_chords.extend([ptc+904, dur_vel+1416])
|
263 |
-
mel_cho.extend([ptc+904, dur_vel+1416])
|
264 |
-
|
265 |
-
pp = pat
|
266 |
-
|
267 |
-
pc = c
|
268 |
-
|
269 |
-
melody_chords2.append(mel_cho)
|
270 |
-
|
271 |
#=======================================================
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
277 |
#=======================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
278 |
|
279 |
print('Done!')
|
280 |
print('=' * 70)
|
|
|
115 |
comp_dtimes = [max(1, min(127, b-a)) for a, b in zip(comp_times[:-1], comp_times[1:]) if b-a != 0]
|
116 |
avg_comp_dtime = max(0, min(127, int(sum(comp_dtimes) / len(comp_dtimes))))
|
117 |
|
118 |
+
#=======================================================
|
119 |
+
# FINAL PROCESSING
|
120 |
+
|
121 |
+
#=======================================================
|
122 |
+
# Adjusting avg velocity
|
123 |
+
|
124 |
+
vels = [e[5] for e in escore_notes]
|
125 |
+
avg_vel = int(sum(vels) / len(vels))
|
126 |
+
|
127 |
+
if avg_vel < 60:
|
128 |
+
TMIDIX.adjust_score_velocities(escore_notes, avg_vel * 2)
|
129 |
+
|
130 |
+
melody_chords = []
|
131 |
+
melody_chords2 = []
|
132 |
+
mel_cho = []
|
133 |
+
|
134 |
+
#=======================================================
|
135 |
+
# Break between compositions / Intro seq
|
136 |
+
|
137 |
+
if 128 in instruments_list:
|
138 |
+
drums_present = 1931 # Yes
|
139 |
+
else:
|
140 |
+
drums_present = 1930 # No
|
141 |
+
|
142 |
+
melody_chords.extend([1929, drums_present])
|
143 |
+
mel_cho.extend([1929, drums_present])
|
144 |
+
|
145 |
+
#=======================================================
|
146 |
+
# Composition patches list
|
147 |
+
|
148 |
+
melody_chords.extend([i+1932 for i in instruments_list_without_drums])
|
149 |
+
mel_cho.extend([i+1932 for i in instruments_list_without_drums])
|
150 |
+
#=======================================================
|
151 |
+
# Composition avg pitch and dtime
|
152 |
+
|
153 |
+
mode_instruments_pitch = statistics.mode([e[4] for e in escore_notes if e[6] < 80])
|
154 |
+
|
155 |
+
melody_chords.extend([2060+mode_instruments_pitch, 2188+avg_comp_dtime])
|
156 |
+
mel_cho.extend([2060+mode_instruments_pitch, 2188+avg_comp_dtime])
|
157 |
+
|
158 |
+
melody_chords2.append(mel_cho)
|
159 |
+
|
160 |
+
#=======================================================
|
161 |
+
# MAIN PROCESSING CYCLE
|
162 |
+
#=======================================================
|
163 |
+
|
164 |
+
cscore = TMIDIX.chordify_score([1000, escore_notes])
|
165 |
+
|
166 |
+
pc = cscore[0] # Previous chord
|
167 |
+
|
168 |
+
for i, c in enumerate(cscore):
|
169 |
+
|
170 |
+
c.sort(key=lambda x: x[6]) # Sorting by patch
|
171 |
+
|
172 |
#=======================================================
|
173 |
+
# Outro seq
|
174 |
+
|
175 |
+
#if len(cscore) > 256:
|
176 |
+
# if len(cscore) - i == 64:
|
177 |
+
# melody_chords.extend([2236])
|
178 |
+
|
179 |
#=======================================================
|
180 |
+
# Timings...
|
181 |
+
|
182 |
+
# Cliping all values...
|
183 |
+
delta_time = max(0, min(127, c[0][1]-pc[0][1]))
|
184 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
185 |
#=======================================================
|
186 |
+
# Chords...
|
187 |
+
|
188 |
+
cpitches = sorted([e[4] for e in c if e[3] != 9])
|
189 |
+
dpitches = [e[4] for e in c if e[3] == 9]
|
190 |
+
|
191 |
+
tones_chord = sorted(set([p % 12 for p in cpitches]))
|
192 |
+
|
193 |
+
if tones_chord:
|
194 |
+
|
195 |
+
if tones_chord not in TMIDIX.ALL_CHORDS_SORTED:
|
196 |
+
tones_chord_tok = 644
|
197 |
+
tones_chord_tok = TMIDIX.ALL_CHORDS_SORTED.index(TMIDIX.advanced_check_and_fix_tones_chord(tones_chord, cpitches[-1]))
|
198 |
+
|
199 |
+
else:
|
200 |
+
tones_chord_tok = TMIDIX.ALL_CHORDS_SORTED.index(tones_chord) # 321
|
201 |
+
|
202 |
+
if dpitches:
|
203 |
+
|
204 |
+
if tones_chord_tok == 644:
|
205 |
+
tones_chord_tok = 645
|
206 |
+
else:
|
207 |
+
tones_chord_tok += 321
|
208 |
+
|
209 |
else:
|
210 |
+
tones_chord_tok = 643 # Drums-only chord
|
211 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
212 |
#=======================================================
|
213 |
+
# Writing chord/time...
|
214 |
+
|
215 |
+
melody_chords.extend([tones_chord_tok, delta_time+646])
|
216 |
+
|
217 |
+
mel_cho = []
|
218 |
+
mel_cho.extend([tones_chord_tok, delta_time+646])
|
219 |
+
|
220 |
#=======================================================
|
221 |
+
# Notes...
|
222 |
+
|
223 |
+
pp = -1
|
224 |
+
|
225 |
+
for e in c:
|
226 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
227 |
#=======================================================
|
228 |
+
# Duration
|
229 |
+
dur = max(0, min(63, int(max(0, e[2] // 4) * 2)))
|
230 |
+
|
231 |
+
# Pitch
|
232 |
+
ptc = max(1, min(127, e[4]))
|
233 |
+
|
234 |
+
# Octo-velocity
|
235 |
+
vel = max(8, min(127, (max(1, e[5] // 8) * 8)))
|
236 |
+
velocity = round(vel / 15)-1
|
237 |
+
|
238 |
+
# Patch
|
239 |
+
pat = max(0, min(128, e[6]))
|
240 |
|
241 |
+
if 7 < pat < 80:
|
242 |
+
ptc += 128
|
243 |
+
|
244 |
+
elif 79 < pat < 128:
|
245 |
+
ptc += 256
|
246 |
+
|
247 |
+
elif pat == 128:
|
248 |
+
ptc += 384
|
249 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
250 |
#=======================================================
|
251 |
+
# FINAL NOTE SEQ
|
252 |
+
|
253 |
+
# Writing final note asynchronously
|
254 |
+
|
255 |
+
dur_vel = (8 * dur) + velocity # 512
|
256 |
+
|
257 |
+
if pat != pp:
|
258 |
+
melody_chords.extend([pat+774, ptc+904, dur_vel+1416]) # 1928
|
259 |
+
mel_cho.extend([pat+774, ptc+904, dur_vel+1416])
|
260 |
+
|
261 |
+
else:
|
262 |
+
melody_chords.extend([ptc+904, dur_vel+1416])
|
263 |
+
mel_cho.extend([ptc+904, dur_vel+1416])
|
264 |
+
|
265 |
+
pp = pat
|
266 |
+
|
267 |
+
pc = c
|
268 |
+
|
269 |
+
melody_chords2.append(mel_cho)
|
270 |
+
|
271 |
#=======================================================
|
272 |
+
|
273 |
+
#melody_chords.extend([2237]) # EOS
|
274 |
+
|
275 |
+
#=======================================================
|
276 |
+
# TOTAL DICTIONARY SIZE 2237+1=2238
|
277 |
+
#=======================================================
|
278 |
|
279 |
print('Done!')
|
280 |
print('=' * 70)
|