asigalov61 commited on
Commit
52b276e
1 Parent(s): 48c9cae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -202
app.py CHANGED
@@ -2,8 +2,6 @@
2
  # https://huggingface.co/spaces/asigalov61/MIDI-Chords-Mixer
3
  # =================================================================================================
4
 
5
- import os.path
6
-
7
  import time as reqtime
8
  import datetime
9
  from pytz import timezone
@@ -37,13 +35,9 @@ def Mix_Chords(input_find_best_match,
37
 
38
  print('=' * 70)
39
 
40
- fn = os.path.basename(input_midi.name)
41
- fn1 = fn.split('.')[0]
42
-
43
  print('-' * 70)
44
  print('Requested settings:')
45
  print('-' * 70)
46
- print('Input file name:', fn)
47
  print('Find best matches', input_find_best_match)
48
  print('Adjust melody notes durations:', input_adjust_melody_notes_durations)
49
  print('Adjust accompaniment notes durations:', input_adjust_accompaniment_notes_durations)
@@ -55,235 +49,134 @@ def Mix_Chords(input_find_best_match,
55
  print('Transpose value:', input_transpose_value)
56
  print('-' * 70)
57
 
58
- #===============================================================================
59
- raw_score = TMIDIX.midi2single_track_ms_score(input_midi.name)
60
-
61
- #===============================================================================
62
- # Enhanced score notes
63
-
64
- raw_escore = TMIDIX.advanced_score_processor(raw_score, return_enhanced_score_notes=True)[0]
65
-
66
- if len(raw_escore) > 0:
67
-
68
- #===============================================================================
69
- # Augmented enhanced score notes
70
-
71
- src_escore = TMIDIX.recalculate_score_timings(TMIDIX.augment_enhanced_score_notes([e for e in raw_escore if e[6] < 80]))
72
 
73
- src_cscore = TMIDIX.chordify_score([1000, src_escore])
74
 
75
- src_melody = [c[0] for c in src_cscore][:256]
 
 
 
76
 
77
- if input_transform == 'Flip Melody':
78
- src_melody = TMIDIX.flip_enhanced_score_notes(src_melody)
79
-
80
- elif input_transform == 'Reverse Melody':
81
- src_melody = TMIDIX.reverse_enhanced_score_notes(src_melody)
82
 
83
- mel_avg_time = TMIDIX.escore_notes_averages(src_melody)[0]
84
-
85
- src_melody_pitches = [p[4] for p in src_melody]
86
-
87
- src_harm_tones_chords = TMIDIX.harmonize_enhanced_melody_score_notes(src_melody)
88
-
89
- #===============================================================================
90
-
91
- matched_songs = [a for a in all_songs if a[2] == max(32, len(src_melody))]
92
-
93
- random.shuffle(matched_songs)
94
 
95
- max_match_ratio = -1
96
- max_match_ratios_count = len(matched_songs)
 
97
 
98
- if input_find_best_match:
99
- new_song, max_match_ratio, max_match_ratios_count = find_similar_song(matched_songs, src_melody)
100
- else:
101
- new_song = random.choice(matched_songs)
102
-
103
- print('Selected Monster Mono Melodies MIDI:', new_song[0])
104
- print('Selected melody match ratio:', max_match_ratio)
105
- print('Selected melody instrument:', TMIDIX.Number2patch[new_song[1]], '(', new_song[1], ')')
106
- print('Melody notes count:', new_song[2])
107
- print('Matched melodies pool count', max_match_ratios_count)
108
-
109
- MIDI_Summary = 'Selected Monster Mono Melodies MIDI: ' + str(new_song[0]) + '\n'
110
- MIDI_Summary += 'Selected melody match ratio: ' + str(max_match_ratio) + '\n'
111
- MIDI_Summary += 'Selected melody instrument: ' + str(TMIDIX.Number2patch[new_song[1]]) + ' (' + str(new_song[1]) + ')' + '\n'
112
- MIDI_Summary += 'Melody notes count: ' + str(new_song[2]) + '\n'
113
- MIDI_Summary += 'Matched melodies pool count: ' + str(max_match_ratios_count)
114
 
115
- fn1 += '_' + str(new_song[0]) + '_' + str(TMIDIX.Number2patch[new_song[1]]) + '_' + str(new_song[1]) + '_' + str(new_song[2])
116
-
117
- trg_patch = new_song[1]
118
-
119
- trg_song = copy.deepcopy(new_song[3])
120
-
121
- mix_avg_time = TMIDIX.escore_notes_averages(trg_song)[0]
122
- mix_mel_avg_time = TMIDIX.escore_notes_averages([e for e in trg_song if e[6] == trg_patch])[0]
123
-
124
- TMIDIX.adjust_score_velocities(trg_song, 95)
125
-
126
- cscore = TMIDIX.chordify_score([1000, trg_song])
127
 
128
- print('=' * 70)
129
- print('Done loading source and target MIDIs...!')
130
- print('=' * 70)
131
- print('Mixing...')
132
 
133
  mixed_song = []
134
-
135
- midx = 0
136
- next_note_dtime = 255
137
-
138
- for i, c in enumerate(cscore):
139
- cho = copy.deepcopy(c)
140
 
141
- patches = sorted(set([e[6] for e in c]))
142
 
143
- if trg_patch in patches:
144
-
145
- if input_adjust_melody_notes_durations:
146
- if midx < len(src_melody)-1:
147
- next_note_dtime = src_melody[midx+1][1] - src_melody[midx][1]
148
- else:
149
- next_note_dtime = 255
150
-
151
- mixed_song.extend(mix_chord(c, src_harm_tones_chords[midx], trg_patch, src_melody_pitches[midx], next_note_dtime))
152
 
153
- midx += 1
154
-
155
- else:
156
- if input_adjust_accompaniment_notes_durations:
157
- if i < len(cscore)-1:
158
- next_note_dtime = cscore[i+1][0][1] - cscore[i][0][1]
 
 
159
  else:
160
- next_note_dtime = 255
 
 
 
 
 
 
 
161
 
162
- mixed_song.extend(mix_chord(cho, src_harm_tones_chords[midx], trg_patch, src_melody_pitches[midx], next_note_dtime))
 
163
 
164
- if midx == len(src_melody):
165
- break
166
 
167
- print('=' * 70)
168
- print('Done!')
169
- print('=' * 70)
170
 
171
- #===============================================================================
172
 
173
- if input_output_as_solo_piano:
174
-
175
- csong = TMIDIX.chordify_score([1000, mixed_song])
176
 
177
- mixed_song = []
178
 
179
- for c in csong:
180
-
181
- pitches = [e[4] for e in c if e[6] == trg_patch]
182
-
183
- for cc in c:
184
-
185
- ccc = copy.deepcopy(cc)
186
-
187
- if cc[3] != 9:
188
- if cc[6] == trg_patch:
189
- ccc[3] = 3
190
- ccc[6] = 0
191
- mixed_song.append(ccc)
192
-
193
- else:
194
- if cc[4] not in pitches:
195
- ccc[3] = 0
196
- ccc[6] = 0
197
- mixed_song.append(ccc)
198
- pitches.append(cc[4])
199
-
200
- else:
201
- mixed_song.append(ccc)
202
-
203
- if input_remove_drums:
204
- mixed_song = [e for e in mixed_song if e[3] != 9]
205
 
206
- if input_output_tempo == 'Mix':
 
 
 
 
207
 
208
- time_k = mel_avg_time / mix_avg_time
 
 
209
 
210
- mixed_song = TMIDIX.adjust_escore_notes_timings(mixed_song, time_k)
211
 
212
- elif input_output_tempo == 'Source Melody':
 
 
 
 
 
 
 
 
213
 
214
- time_k = mel_avg_time / mix_mel_avg_time
215
 
216
- mixed_song = TMIDIX.adjust_escore_notes_timings(mixed_song, time_k)
 
 
 
 
 
 
 
 
217
 
218
- if input_transform == 'Flip Mix':
219
- mixed_song = TMIDIX.flip_enhanced_score_notes(mixed_song)
220
-
221
- elif input_transform == 'Reverse Mix':
222
- mixed_song = TMIDIX.reverse_enhanced_score_notes(mixed_song)
223
 
224
- if input_transpose_value != 0:
225
- mixed_song = TMIDIX.transpose_escore_notes(mixed_song, input_transpose_value)
 
 
226
 
227
- if input_transpose_to_C4:
228
- mixed_song = TMIDIX.transpose_escore_notes_to_pitch(mixed_song)
229
-
230
- #===============================================================================
231
- print('Rendering results...')
232
-
233
- print('=' * 70)
234
- print('Sample INTs', mixed_song[:5])
235
- print('=' * 70)
236
-
237
- output_score, patches, overflow_patches = TMIDIX.patch_enhanced_score_notes(mixed_song)
238
-
239
- detailed_stats = TMIDIX.Tegridy_ms_SONG_to_MIDI_Converter(output_score,
240
- output_signature = 'Harmonic Melody MIDI Mixer',
241
- output_file_name = fn1,
242
- track_name='Project Los Angeles',
243
- list_of_MIDI_patches=patches,
244
- timings_multiplier=16
245
- )
246
-
247
- new_fn = fn1+'.mid'
248
-
249
-
250
- audio = midi_to_colab_audio(new_fn,
251
- soundfont_path=soundfont,
252
- sample_rate=16000,
253
- volume_scale=10,
254
- output_for_gradio=True
255
- )
256
-
257
- print('Done!')
258
- print('=' * 70)
259
-
260
- #========================================================
261
 
262
- output_midi_title = str(fn1)
263
- output_midi_summary = str(MIDI_Summary)
264
- output_midi = str(new_fn)
265
- output_audio = (16000, audio)
266
-
267
- for o in output_score:
268
- o[1] *= 16
269
- o[2] *= 16
270
-
271
- output_plot = TMIDIX.plot_ms_SONG(output_score, plot_title=output_midi_title, return_plt=True)
272
-
273
- print('Output MIDI file name:', output_midi)
274
- print('Output MIDI title:', output_midi_title)
275
- print('Output MIDI summary:', MIDI_Summary)
276
- print('=' * 70)
277
-
278
 
279
- #========================================================
280
-
281
- print('-' * 70)
282
- print('Req end time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
283
- print('-' * 70)
284
- print('Req execution time:', (reqtime.time() - start_time), 'sec')
285
 
286
- return output_midi_title, output_midi_summary, output_midi, output_audio, output_plot
 
 
 
 
 
287
 
288
  # =================================================================================================
289
 
 
2
  # https://huggingface.co/spaces/asigalov61/MIDI-Chords-Mixer
3
  # =================================================================================================
4
 
 
 
5
  import time as reqtime
6
  import datetime
7
  from pytz import timezone
 
35
 
36
  print('=' * 70)
37
 
 
 
 
38
  print('-' * 70)
39
  print('Requested settings:')
40
  print('-' * 70)
 
41
  print('Find best matches', input_find_best_match)
42
  print('Adjust melody notes durations:', input_adjust_melody_notes_durations)
43
  print('Adjust accompaniment notes durations:', input_adjust_accompaniment_notes_durations)
 
49
  print('Transpose value:', input_transpose_value)
50
  print('-' * 70)
51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
 
53
 
54
+ print('=' * 70)
55
+ print('Done loading source and target MIDIs...!')
56
+ print('=' * 70)
57
+ print('Mixing...')
58
 
 
 
 
 
 
59
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
+ print('=' * 70)
62
+ print('Done!')
63
+ print('=' * 70)
64
 
65
+ #===============================================================================
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
+ if input_output_as_solo_piano:
 
 
 
 
 
 
 
 
 
 
 
68
 
69
+ csong = TMIDIX.chordify_score([1000, mixed_song])
 
 
 
70
 
71
  mixed_song = []
72
+
73
+ for c in csong:
 
 
 
 
74
 
75
+ pitches = [e[4] for e in c if e[6] == trg_patch]
76
 
77
+ for cc in c:
 
 
 
 
 
 
 
 
78
 
79
+ ccc = copy.deepcopy(cc)
80
+
81
+ if cc[3] != 9:
82
+ if cc[6] == trg_patch:
83
+ ccc[3] = 3
84
+ ccc[6] = 0
85
+ mixed_song.append(ccc)
86
+
87
  else:
88
+ if cc[4] not in pitches:
89
+ ccc[3] = 0
90
+ ccc[6] = 0
91
+ mixed_song.append(ccc)
92
+ pitches.append(cc[4])
93
+
94
+ else:
95
+ mixed_song.append(ccc)
96
 
97
+ if input_remove_drums:
98
+ mixed_song = [e for e in mixed_song if e[3] != 9]
99
 
100
+ if input_output_tempo == 'Mix':
 
101
 
102
+ time_k = mel_avg_time / mix_avg_time
 
 
103
 
104
+ mixed_song = TMIDIX.adjust_escore_notes_timings(mixed_song, time_k)
105
 
106
+ elif input_output_tempo == 'Source Melody':
107
+
108
+ time_k = mel_avg_time / mix_mel_avg_time
109
 
110
+ mixed_song = TMIDIX.adjust_escore_notes_timings(mixed_song, time_k)
111
 
112
+ if input_transform == 'Flip Mix':
113
+ mixed_song = TMIDIX.flip_enhanced_score_notes(mixed_song)
114
+
115
+ elif input_transform == 'Reverse Mix':
116
+ mixed_song = TMIDIX.reverse_enhanced_score_notes(mixed_song)
117
+
118
+ if input_transpose_value != 0:
119
+ mixed_song = TMIDIX.transpose_escore_notes(mixed_song, input_transpose_value)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
 
121
+ if input_transpose_to_C4:
122
+ mixed_song = TMIDIX.transpose_escore_notes_to_pitch(mixed_song)
123
+
124
+ #===============================================================================
125
+ print('Rendering results...')
126
 
127
+ print('=' * 70)
128
+ print('Sample INTs', mixed_song[:5])
129
+ print('=' * 70)
130
 
131
+ output_score, patches, overflow_patches = TMIDIX.patch_enhanced_score_notes(mixed_song)
132
 
133
+ detailed_stats = TMIDIX.Tegridy_ms_SONG_to_MIDI_Converter(output_score,
134
+ output_signature = 'Harmonic Melody MIDI Mixer',
135
+ output_file_name = fn1,
136
+ track_name='Project Los Angeles',
137
+ list_of_MIDI_patches=patches,
138
+ timings_multiplier=16
139
+ )
140
+
141
+ new_fn = fn1+'.mid'
142
 
 
143
 
144
+ audio = midi_to_colab_audio(new_fn,
145
+ soundfont_path=soundfont,
146
+ sample_rate=16000,
147
+ volume_scale=10,
148
+ output_for_gradio=True
149
+ )
150
+
151
+ print('Done!')
152
+ print('=' * 70)
153
 
154
+ #========================================================
 
 
 
 
155
 
156
+ output_midi_title = str(fn1)
157
+ output_midi_summary = str(MIDI_Summary)
158
+ output_midi = str(new_fn)
159
+ output_audio = (16000, audio)
160
 
161
+ for o in output_score:
162
+ o[1] *= 16
163
+ o[2] *= 16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
164
 
165
+ output_plot = TMIDIX.plot_ms_SONG(output_score, plot_title=output_midi_title, return_plt=True)
166
+
167
+ print('Output MIDI file name:', output_midi)
168
+ print('Output MIDI title:', output_midi_title)
169
+ print('Output MIDI summary:', MIDI_Summary)
170
+ print('=' * 70)
 
 
 
 
 
 
 
 
 
 
171
 
172
+ #========================================================
 
 
 
 
 
173
 
174
+ print('-' * 70)
175
+ print('Req end time: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now(PDT)))
176
+ print('-' * 70)
177
+ print('Req execution time:', (reqtime.time() - start_time), 'sec')
178
+
179
+ return output_midi_title, output_midi_summary, output_midi, output_audio, output_plot
180
 
181
  # =================================================================================================
182