Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -111,80 +111,28 @@ print('=' * 70)
|
|
111 |
def load_midi(input_midi):
|
112 |
|
113 |
raw_score = TMIDIX.midi2single_track_ms_score(input_midi.name)
|
114 |
-
|
115 |
-
escore_notes = TMIDIX.advanced_score_processor(raw_score, return_enhanced_score_notes=True)
|
116 |
-
|
117 |
-
escore_notes = TMIDIX.augment_enhanced_score_notes(escore_notes[0], timings_divider=16)
|
118 |
|
119 |
-
|
120 |
-
|
121 |
-
#=======================================================
|
122 |
-
# FINAL PROCESSING
|
123 |
-
#=======================================================
|
124 |
|
125 |
-
|
126 |
-
|
127 |
-
# Break between compositions / Intro seq
|
128 |
|
129 |
-
|
130 |
-
drums_present = 19331 # Yes
|
131 |
-
else:
|
132 |
-
drums_present = 19330 # No
|
133 |
-
|
134 |
-
pat = escore_notes[0][6]
|
135 |
|
136 |
-
|
137 |
|
138 |
-
|
139 |
-
# MAIN PROCESSING CYCLE
|
140 |
-
#=======================================================
|
141 |
|
142 |
-
|
|
|
143 |
|
144 |
-
|
|
|
145 |
|
146 |
-
|
147 |
-
# Timings...
|
148 |
-
|
149 |
-
# Cliping all values...
|
150 |
-
delta_time = max(0, min(255, e[1]-pe[1]))
|
151 |
-
|
152 |
-
# Durations and channels
|
153 |
-
|
154 |
-
dur = max(0, min(255, e[2]))
|
155 |
-
cha = max(0, min(15, e[3]))
|
156 |
-
|
157 |
-
# Patches
|
158 |
-
if cha == 9: # Drums patch will be == 128
|
159 |
-
pat = 128
|
160 |
-
|
161 |
-
else:
|
162 |
-
pat = e[6]
|
163 |
-
|
164 |
-
# Pitches
|
165 |
-
|
166 |
-
ptc = max(1, min(127, e[4]))
|
167 |
-
|
168 |
-
# Velocities
|
169 |
-
|
170 |
-
# Calculating octo-velocity
|
171 |
-
vel = max(8, min(127, e[5]))
|
172 |
-
velocity = round(vel / 15)-1
|
173 |
-
|
174 |
-
#=======================================================
|
175 |
-
# FINAL NOTE SEQ
|
176 |
-
#=======================================================
|
177 |
-
|
178 |
-
# Writing final note asynchronously
|
179 |
-
|
180 |
-
dur_vel = (8 * dur) + velocity
|
181 |
-
pat_ptc = (129 * pat) + ptc
|
182 |
-
|
183 |
-
melody_chords.extend([delta_time, dur_vel+256, pat_ptc+2304])
|
184 |
-
|
185 |
-
pe = e
|
186 |
|
187 |
-
return
|
188 |
|
189 |
#==================================================================================
|
190 |
|
@@ -198,50 +146,25 @@ def save_midi(tokens, batch_number=None):
|
|
198 |
vel = 90
|
199 |
pitch = 0
|
200 |
channel = 0
|
201 |
-
|
202 |
-
|
203 |
-
patches[
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
patch = (ss-2304) // 129
|
222 |
-
|
223 |
-
if patch < 128:
|
224 |
-
|
225 |
-
if patch not in patches:
|
226 |
-
if 0 in channels:
|
227 |
-
cha = channels.index(0)
|
228 |
-
channels[cha] = 1
|
229 |
-
else:
|
230 |
-
cha = 15
|
231 |
-
|
232 |
-
patches[cha] = patch
|
233 |
-
channel = patches.index(patch)
|
234 |
-
else:
|
235 |
-
channel = patches.index(patch)
|
236 |
-
|
237 |
-
if patch == 128:
|
238 |
-
channel = 9
|
239 |
-
|
240 |
-
pitch = (ss-2304) % 129
|
241 |
-
|
242 |
-
song_f.append(['note', time, dur, channel, pitch, vel, patch ])
|
243 |
-
|
244 |
-
patches = [0 if x==-1 else x for x in patches]
|
245 |
|
246 |
if batch_number == None:
|
247 |
fname = 'Monster-Piano-Transformer-Music-Composition'
|
@@ -273,20 +196,11 @@ def generate_music(prime,
|
|
273 |
):
|
274 |
|
275 |
if not prime:
|
276 |
-
inputs = [
|
277 |
|
278 |
else:
|
279 |
inputs = prime[-num_mem_tokens:]
|
280 |
-
|
281 |
-
if gen_outro == 'Force':
|
282 |
-
inputs.extend([18945])
|
283 |
-
|
284 |
-
if gen_drums:
|
285 |
-
drums = [36, 38]
|
286 |
-
drum_pitch = random.choice(drums)
|
287 |
-
inputs.extend([0, ((8*8)+6)+256, ((128*129)+drum_pitch)+2304])
|
288 |
|
289 |
-
# torch.cuda.empty_cache()
|
290 |
model.cuda()
|
291 |
model.eval()
|
292 |
|
@@ -300,27 +214,18 @@ def generate_music(prime,
|
|
300 |
with torch.inference_mode():
|
301 |
out = model.generate(inp,
|
302 |
num_gen_tokens,
|
303 |
-
filter_logits_fn=top_p,
|
304 |
-
filter_kwargs={'thres': model_sampling_top_p},
|
305 |
temperature=model_temperature,
|
306 |
return_prime=False,
|
307 |
verbose=False)
|
308 |
|
309 |
output = out.tolist()
|
310 |
|
311 |
-
output_batches = []
|
312 |
-
|
313 |
-
if gen_outro == 'Disable':
|
314 |
-
for o in output:
|
315 |
-
output_batches.append([t for t in o if not 18944 < t < 19330])
|
316 |
-
|
317 |
-
else:
|
318 |
-
output_batches = output
|
319 |
-
|
320 |
print('Done!')
|
321 |
print('=' * 70)
|
322 |
|
323 |
-
return
|
324 |
|
325 |
#==================================================================================
|
326 |
|
|
|
111 |
def load_midi(input_midi):
|
112 |
|
113 |
raw_score = TMIDIX.midi2single_track_ms_score(input_midi.name)
|
|
|
|
|
|
|
|
|
114 |
|
115 |
+
escore_notes = TMIDIX.advanced_score_processor(raw_score, return_enhanced_score_notes=True)[0]
|
116 |
+
escore_notes = TMIDIX.augment_enhanced_score_notes(escore_notes, timings_divider=32)
|
|
|
|
|
|
|
117 |
|
118 |
+
sp_escore_notes = TMIDIX.solo_piano_escore_notes(escore_notes, keep_drums=False)
|
119 |
+
zscore = TMIDIX.recalculate_score_timings(sp_escore_notes)
|
|
|
120 |
|
121 |
+
cscore = TMIDIX.chordify_score([1000, zscore])
|
|
|
|
|
|
|
|
|
|
|
122 |
|
123 |
+
score = []
|
124 |
|
125 |
+
pc = cscore[0]
|
|
|
|
|
126 |
|
127 |
+
for c in cscore:
|
128 |
+
score.append(max(0, min(127, c[0][1]-pc[0][1])))
|
129 |
|
130 |
+
for n in c:
|
131 |
+
score.extend([max(1, min(127, n[2]))+128, max(1, min(127, n[4]))+256, max(1, min(127, n[5]))+384])
|
132 |
|
133 |
+
pc = c
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
|
135 |
+
return score
|
136 |
|
137 |
#==================================================================================
|
138 |
|
|
|
146 |
vel = 90
|
147 |
pitch = 0
|
148 |
channel = 0
|
149 |
+
patch = 0
|
150 |
+
|
151 |
+
patches = [0] * 16
|
152 |
+
|
153 |
+
for m in song:
|
154 |
+
|
155 |
+
if 0 <= m < 128:
|
156 |
+
time += m * 32
|
157 |
+
|
158 |
+
elif 128 < m < 256:
|
159 |
+
dur = (m-128) * 32
|
160 |
+
|
161 |
+
elif 256 < m < 384:
|
162 |
+
pitch = (m-256)
|
163 |
+
|
164 |
+
elif 384 < m < 512:
|
165 |
+
vel = (m-384)
|
166 |
+
|
167 |
+
song_f.append(['note', time, dur, 0, pitch, vel, 0])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
168 |
|
169 |
if batch_number == None:
|
170 |
fname = 'Monster-Piano-Transformer-Music-Composition'
|
|
|
196 |
):
|
197 |
|
198 |
if not prime:
|
199 |
+
inputs = [0]
|
200 |
|
201 |
else:
|
202 |
inputs = prime[-num_mem_tokens:]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
203 |
|
|
|
204 |
model.cuda()
|
205 |
model.eval()
|
206 |
|
|
|
214 |
with torch.inference_mode():
|
215 |
out = model.generate(inp,
|
216 |
num_gen_tokens,
|
217 |
+
#filter_logits_fn=top_p,
|
218 |
+
#filter_kwargs={'thres': model_sampling_top_p},
|
219 |
temperature=model_temperature,
|
220 |
return_prime=False,
|
221 |
verbose=False)
|
222 |
|
223 |
output = out.tolist()
|
224 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
225 |
print('Done!')
|
226 |
print('=' * 70)
|
227 |
|
228 |
+
return output
|
229 |
|
230 |
#==================================================================================
|
231 |
|