-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmidi-splitter.py
295 lines (268 loc) · 7.75 KB
/
midi-splitter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import argparse
import os
import string
import mido
from mido import MidiFile
from glob import glob
from threading import Thread
from tqdm import tqdm
# Instrument names list
INSTRUMENTS = [
"Acoustic Grand Piano",
"Bright Acoustic Piano",
"Electric Grand Piano",
"Honky-Tonk Piano",
"Rhodes Piano",
"Chorused Piano",
"Harpsichord",
"Clavinet",
"Celesta",
"Glockenspiel",
"Music Box",
"Vibraphone",
"Marimba",
"Xylophone",
"Tubular Bells",
"Dulcimer",
"Hammond Organ",
"Percussive Organ",
"Rock Organ",
"Church Organ",
"Reed Organ",
"Accordion",
"Harmonica",
"Tango Accordion",
"Acoustic Guitar - Nylon",
"Acoustic Guitar - Steel",
"Electric Guitar - Jazz",
"Electric Guitar - Clean",
"Electric Guitar - Muted",
"Overdriven Guitar",
"Distortion Guitar",
"Guitar Harmonics",
"Acoustic Bass",
"Electric Bass - Finger",
"Electric Bass - Pick",
"Fretless Bass",
"Slap Bass 1",
"Slap Bass 2",
"Synth Bass 1",
"Synth Bass 2",
"Violin",
"Viola",
"Cello",
"Contrabass",
"Tremolo Strings",
"Pizzicato Strings",
"Orchestral Harp",
"Timpani",
"String Ensemble 1",
"String Ensemble 2",
"Synth. Strings 1",
"Synth. Strings 2",
"Choir Aahs",
"Voice Oohs",
"Synth Voice",
"Orchestra Hit",
"Trumpet",
"Trombone",
"Tuba",
"Muted Trumpet",
"French Horn",
"Brass Section",
"Synth. Brass 1",
"Synth. Brass 2",
"Soprano Sax",
"Alto Sax",
"Tenor Sax",
"Baritone Sax",
"Oboe",
"English Horn",
"Bassoon",
"Clarinet",
"Piccolo",
"Flute",
"Recorder",
"Pan Flute",
"Bottle Blow",
"Shakuhachi",
"Whistle",
"Ocarina",
"Synth Lead 1 - Square",
"Synth Lead 2 - Sawtooth",
"Synth Lead 3 - Calliope",
"Synth Lead 4 - Chiff",
"Synth Lead 5 - Charang",
"Synth Lead 6 - Voice",
"Synth Lead 7 - Fifths",
"Synth Lead 8 - Brass + Lead",
"Synth Pad 1 - New Age",
"Synth Pad 2 - Warm",
"Synth Pad 3 - Polysynth",
"Synth Pad 4 - Choir",
"Synth Pad 5 - Bowed",
"Synth Pad 6 - Metallic",
"Synth Pad 7 - Halo",
"Synth Pad 8 - Sweep",
"FX 1 - Rain",
"FX 2 - Soundtrack",
"FX 3 - Crystal",
"FX 4 - Atmosphere",
"FX 5 - Brightness",
"FX 6 - Goblins",
"FX 7 - Echoes",
"FX 8 - Sci-Fi",
"Sitar",
"Banjo",
"Shamisen",
"Koto",
"Kalimba",
"Bagpipe",
"Fiddle",
"Shanai",
"Tinkle Bell",
"Agogo",
"Steel Drums",
"Woodblock",
"Taiko Drum",
"Melodic Tom",
"Synth Drum",
"Reverse Cymbal",
"Guitar Fret Noise",
"Breath Noise",
"Seashore",
"Bird Tweet",
"Telephone Ring",
"Helicopter",
"Applause",
"Gunshot",
]
def sanitize_filename(filename):
valid_chars = "-_.() %s%s" % (string.ascii_letters, string.digits)
return "".join(c for c in filename if c in valid_chars)
def unique_filename(output_path, filename):
base, extension = os.path.splitext(filename)
counter = 1
unique_filename = filename
while os.path.exists(os.path.join(output_path, unique_filename)):
unique_filename = f"{base}_{counter}{extension}"
counter += 1
return unique_filename
def get_instrument_name(program_number):
if 0 <= program_number < len(INSTRUMENTS):
return INSTRUMENTS[program_number]
return "Unknown Instrument"
def is_track_empty(track):
# Define which message types are considered meaningful
meaningful_message_types = {"note_on", "note_off", "control_change"}
for msg in track:
if msg.type in meaningful_message_types:
# Consider track not empty if a meaningful message is found
return False
return True
def process_midi_file(midi_file, output_path, args):
try:
midi = MidiFile(midi_file)
if args.verbose:
print(f"Successfully loaded MIDI file: {midi_file}")
except IOError:
print(f"Error: Could not open MIDI file: {midi_file}")
return
except Exception as e:
print(f"An unexpected error occurred: {e}")
return
base_name = os.path.splitext(os.path.basename(midi_file))[0]
specific_output_path = os.path.join(output_path, sanitize_filename(base_name))
if not os.path.exists(specific_output_path) and args.verbose:
os.makedirs(specific_output_path)
print(f"Created directory: {specific_output_path}")
elif not os.path.exists(specific_output_path):
os.makedirs(specific_output_path)
for i, track in enumerate(midi.tracks):
if args.remove_empty_tracks and is_track_empty(track) and args.verbose:
print(f"Skipping empty track: Track {i+1}")
continue
elif args.remove_empty_tracks and is_track_empty(track):
continue
new_midi = MidiFile()
if args.duplicate_first_track and i > 0:
new_midi.tracks.append(midi.tracks[0])
new_midi.tracks.append(track)
track_name = f"Track {i+1}"
instrument_name = "No Instrument"
if args.track_names or args.instrument_names:
for msg in track:
if msg.type == "track_name" and args.track_names:
track_name = msg.name
if msg.type == "program_change" and args.instrument_names:
instrument_name = get_instrument_name(msg.program)
break
if instrument_name != "No Instrument":
filename = f"{sanitize_filename(track_name)}_{sanitize_filename(instrument_name)}.mid"
else:
filename = f"{sanitize_filename(track_name)}.mid"
filename = unique_filename(specific_output_path, filename)
file_path = os.path.join(specific_output_path, filename)
new_midi.save(file_path)
if args.verbose:
print(f"Track saved: {file_path} - Instrument: {instrument_name}")
def process_files(midi_files, args):
threads = []
for midi_file in midi_files:
thread = Thread(
target=process_midi_file, args=(midi_file, args.output_path, args)
)
thread.start()
threads.append(thread)
for thread in tqdm(threads, desc="Processing MIDI files"):
thread.join()
def main():
parser = argparse.ArgumentParser(
description="MIDI Splitter Py with Threading and Progress Bar"
)
parser.add_argument(
"input",
type=str,
nargs="+",
help="Path to MIDI files or directories to process.",
)
parser.add_argument(
"-o",
"--output_path",
required=True,
help="Output directory for the split MIDI files.",
)
parser.add_argument(
"-d",
"--duplicate-first-track",
action="store_true",
help="Duplicate the first track onto all tracks.",
)
parser.add_argument(
"-t", "--track-names", action="store_true", help="Read and display track names."
)
parser.add_argument(
"-i",
"--instrument-names",
action="store_true",
help="Read and display instrument names.",
)
parser.add_argument(
"-r",
"--remove-empty-tracks",
action="store_true",
help="Remove tracks that do not contain meaningful MIDI events.",
)
parser.add_argument(
"-v", "--verbose", action="store_true", help="Enable verbose output."
)
args = parser.parse_args()
midi_files = []
for input_path in args.input:
if os.path.isdir(input_path):
midi_files.extend(glob(os.path.join(input_path, "*.mid")))
elif os.path.isfile(input_path) and input_path.endswith(".mid"):
midi_files.append(input_path)
process_files(midi_files, args)
if __name__ == "__main__":
main()