Spaces:
Runtime error
Runtime error
Update src/music/pipeline/midi2processed.py
Browse files
src/music/pipeline/midi2processed.py
CHANGED
@@ -91,62 +91,62 @@ def midi2processed(midi_path, processed_path=None, apply_filtering=True, verbose
|
|
91 |
if verbose: print(' ' * (level + 2) + 'Processed midi file already exists.')
|
92 |
return processed_path, ''
|
93 |
error_msg = 'Error in scrubbing. '
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
except:
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
|
|
91 |
if verbose: print(' ' * (level + 2) + 'Processed midi file already exists.')
|
92 |
return processed_path, ''
|
93 |
error_msg = 'Error in scrubbing. '
|
94 |
+
# try:
|
95 |
+
inst_error_msg = ''
|
96 |
+
# load mid file
|
97 |
+
error_msg += 'Error in midi loading?'
|
98 |
+
midi = pm.PrettyMIDI(midi_path)
|
99 |
+
error_msg += ' Nope. Removing invalid notes?'
|
100 |
+
midi.remove_invalid_notes() # filter invalid notes
|
101 |
+
error_msg += ' Nope. Filtering instruments?'
|
102 |
+
# filter instruments
|
103 |
+
instruments = midi.instruments.copy()
|
104 |
+
new_instru = []
|
105 |
+
instruments_data = []
|
106 |
+
for i_inst, inst in enumerate(instruments):
|
107 |
+
if inst.program <= 7 and not inst.is_drum and len(inst.notes) > 5:
|
108 |
+
# inst is a piano
|
109 |
+
# check data
|
110 |
+
inst.notes = sort_notes(inst.notes) # sort notes
|
111 |
+
inst.notes = delete_notes_end_after_start(inst.notes) # delete invalid notes
|
112 |
+
nb_notes, start, end, duration, largest_gap = analyze_instrument(inst)
|
113 |
+
is_valid, err_msg = is_valid_inst(largest_gap=largest_gap, duration=duration, nb_notes=nb_notes, gap_counts='maestro' not in midi_path)
|
114 |
+
if is_valid or not apply_filtering:
|
115 |
+
new_instru.append(inst)
|
116 |
+
instruments_data.append([nb_notes, start, end, duration, largest_gap])
|
117 |
+
else:
|
118 |
+
inst_error_msg += 'inst1: ' + err_msg + '\n'
|
119 |
+
instruments_data = np.array(instruments_data)
|
120 |
+
error_msg += ' Nope. Taking one instrument?'
|
121 |
+
|
122 |
+
if len(new_instru) == 0:
|
123 |
+
error_msg = f'No piano instrument. {inst_error_msg}'
|
124 |
+
assert False
|
125 |
+
elif len(new_instru) > 1:
|
126 |
+
# take instrument playing the most notes
|
127 |
+
instrument = new_instru[np.argmax(instruments_data[:, 0])]
|
128 |
+
else:
|
129 |
+
instrument = new_instru[0]
|
130 |
+
instrument.program = 0 # set the instrument to Grand Piano.
|
131 |
+
midi.instruments = [instrument] # put instrument in midi file
|
132 |
+
error_msg += ' Nope. Removing blanks?'
|
133 |
+
# remove first and last REMOVE_FIRST_AND_LAST seconds (avoid clapping and jingles)
|
134 |
+
end_time = midi.get_end_time()
|
135 |
+
if apply_filtering: midi = remove_beginning_and_end(midi, end_time)
|
136 |
+
|
137 |
+
# remove beginning and end
|
138 |
+
midi = remove_blanks_beginning_and_end(midi)
|
139 |
+
error_msg += ' Nope. Saving?'
|
140 |
+
|
141 |
+
# save midi file
|
142 |
+
midi.write(processed_path)
|
143 |
+
error_msg += ' Nope.'
|
144 |
+
if verbose:
|
145 |
+
extra = f' Saved to {processed_path}' if midi_path else ''
|
146 |
+
print(' ' * (level + 2) + f'Success! {extra}')
|
147 |
+
return processed_path, ''
|
148 |
+
#except:
|
149 |
+
# if verbose: print(' ' * (level + 2) + 'Scrubbing failed.')
|
150 |
+
# if os.path.exists(processed_path):
|
151 |
+
# os.remove(processed_path)
|
152 |
+
# return None, error_msg + ' Yes.'
|