asigalov61 commited on
Commit
2fdf5af
·
verified ·
1 Parent(s): 505138e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -173
app.py CHANGED
@@ -65,197 +65,50 @@ def GenerateGroove():
65
 
66
  print('Done!')
67
  print('=' * 70)
68
-
69
- fn = os.path.basename(input_midi.name)
70
- fn1 = fn.split('.')[0]
71
-
72
- input_num_tokens = max(4, min(128, input_num_tokens))
73
-
74
- print('-' * 70)
75
- print('Input file name:', fn)
76
- print('Req num toks:', input_num_tokens)
77
- print('Conditioning type:', input_conditioning_type)
78
- print('Strip notes:', input_strip_notes)
79
- print('-' * 70)
80
-
81
- #===============================================================================
82
- raw_score = TMIDIX.midi2single_track_ms_score(input_midi.name)
83
 
84
- #===============================================================================
85
- # Enhanced score notes
86
-
87
- escore_notes = TMIDIX.advanced_score_processor(raw_score, return_enhanced_score_notes=True)[0]
 
88
 
89
- no_drums_escore_notes = [e for e in escore_notes if e[6] < 80]
90
 
91
- if len(no_drums_escore_notes) > 0:
 
 
92
 
93
- #=======================================================
94
- # PRE-PROCESSING
95
-
96
- #===============================================================================
97
- # Augmented enhanced score notes
98
-
99
- no_drums_escore_notes = TMIDIX.augment_enhanced_score_notes(no_drums_escore_notes)
100
-
101
- cscore = TMIDIX.chordify_score([1000, no_drums_escore_notes])
102
-
103
- clean_cscore = []
104
-
105
- for c in cscore:
106
- pitches = []
107
- cho = []
108
- for cc in c:
109
- if cc[4] not in pitches:
110
- cho.append(cc)
111
- pitches.append(cc[4])
112
-
113
- clean_cscore.append(cho)
114
-
115
- #=======================================================
116
- # FINAL PROCESSING
117
-
118
- melody_chords = []
119
- chords = []
120
- times = [0]
121
- durs = []
122
-
123
- #=======================================================
124
- # MAIN PROCESSING CYCLE
125
- #=======================================================
126
-
127
- pe = clean_cscore[0][0]
128
-
129
- first_chord = True
130
-
131
- for c in clean_cscore:
132
-
133
- # Chords
134
-
135
- c.sort(key=lambda x: x[4], reverse=True)
136
-
137
- tones_chord = sorted(set([cc[4] % 12 for cc in c]))
138
-
139
- try:
140
- chord_token = TMIDIX.ALL_CHORDS_SORTED.index(tones_chord)
141
- except:
142
- checked_tones_chord = TMIDIX.check_and_fix_tones_chord(tones_chord)
143
- chord_token = TMIDIX.ALL_CHORDS_SORTED.index(checked_tones_chord)
144
-
145
- melody_chords.extend([chord_token+384])
146
-
147
- if input_strip_notes:
148
- if len(tones_chord) > 1:
149
- chords.extend([chord_token+384])
150
-
151
- else:
152
- chords.extend([chord_token+384])
153
-
154
- if first_chord:
155
- melody_chords.extend([0])
156
- first_chord = False
157
-
158
- for e in c:
159
-
160
- #=======================================================
161
- # Timings...
162
-
163
- time = e[1]-pe[1]
164
-
165
- dur = e[2]
166
-
167
- if time != 0 and time % 2 != 0:
168
- time += 1
169
- if dur % 2 != 0:
170
- dur += 1
171
-
172
- delta_time = int(max(0, min(255, time)) / 2)
173
-
174
- # Durations
175
-
176
- dur = int(max(0, min(255, dur)) / 2)
177
-
178
- # Pitches
179
-
180
- ptc = max(1, min(127, e[4]))
181
-
182
- #=======================================================
183
- # FINAL NOTE SEQ
184
-
185
- # Writing final note asynchronously
186
-
187
- if delta_time != 0:
188
- melody_chords.extend([delta_time, dur+128, ptc+256])
189
- if input_strip_notes:
190
- if len(c) > 1:
191
- times.append(delta_time)
192
- durs.append(dur+128)
193
- else:
194
- times.append(delta_time)
195
- durs.append(dur+128)
196
- else:
197
- melody_chords.extend([dur+128, ptc+256])
198
-
199
- pe = e
200
-
201
  #==================================================================
202
 
203
  print('=' * 70)
204
 
205
- print('Sample output events', melody_chords[:5])
206
  print('=' * 70)
207
  print('Generating...')
208
 
209
- output = []
 
 
210
 
211
- max_chords_limit = 8
212
- temperature=0.9
213
- num_memory_tokens=4096
214
 
215
- output = []
216
 
217
- idx = 0
218
 
219
- for c in chords[:input_num_tokens]:
220
-
221
- output.append(c)
222
 
223
- if input_conditioning_type == 'Chords-Times' or input_conditioning_type == 'Chords-Times-Durations':
224
- output.append(times[idx])
225
 
226
- if input_conditioning_type == 'Chords-Times-Durations':
227
- output.append(durs[idx])
228
-
229
- x = torch.tensor([output] * 1, dtype=torch.long, device='cuda')
230
-
231
- o = 0
232
-
233
- ncount = 0
234
-
235
- while o < 384 and ncount < max_chords_limit:
236
- with ctx:
237
- out = model.generate(x[-num_memory_tokens:],
238
- 1,
239
- temperature=temperature,
240
- return_prime=False,
241
- verbose=False)
242
-
243
- o = out.tolist()[0][0]
244
-
245
- if 256 <= o < 384:
246
- ncount += 1
247
-
248
- if o < 384:
249
- x = torch.cat((x, out), 1)
250
-
251
- outy = x.tolist()[0][len(output):]
252
-
253
- output.extend(outy)
254
 
255
- idx += 1
256
-
257
- if idx == len(chords[:input_num_tokens])-1:
258
- break
259
 
260
  print('=' * 70)
261
  print('Done!')
 
65
 
66
  print('Done!')
67
  print('=' * 70)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
 
69
+ print('Loading Google Magenta Groove processed MIDIs...')
70
+ all_scores = TMIDIX.Tegridy_Any_Pickle_File_Reader('Google_Magenta_Groove_8675_Select_Processed_MIDIs')
71
+
72
+ print('Done!')
73
+ print('=' * 70)
74
 
75
+ print('=' * 70)
76
 
77
+ drums_score_idx = random.randint(0, len(all_scores))
78
+ drums_score_fn = all_scores[drums_score_idx][0]
79
+ drums_score = all_scores[drums_score_idx][1]
80
 
81
+ print('Drums score index', drums_score_idx)
82
+ print('Drums score name', drums_score_fn)
83
+ print('Drums score length', len(drums_score))
84
+ print('=' * 70)
85
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
  #==================================================================
87
 
88
  print('=' * 70)
89
 
90
+ print('Sample input events', drums_score[:5])
91
  print('=' * 70)
92
  print('Generating...')
93
 
94
+ num_prime_chords = 7
95
+
96
+ outy = []
97
 
98
+ for d in drums_score[:num_prime_chords]:
 
 
99
 
100
+ outy.extend(d)
101
 
102
+ for i in tqdm.tqdm(range(num_prime_chords, len(drums_score))):
103
 
104
+ outy.extend(drums_score[i])
 
 
105
 
106
+ if i == num_prime_chords:
107
+ outy.append(256+8)
108
 
109
+ out = generate_chords(outy)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
 
111
+ outy.extend(out)
 
 
 
112
 
113
  print('=' * 70)
114
  print('Done!')