forked from jason9693/MusicTransformer-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate_midi.py
31 lines (25 loc) · 842 Bytes
/
validate_midi.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
from mido import MidiFile
import os
from joblib import Parallel, delayed
from tqdm import tqdm
os.makedirs("data/all_validated", exist_ok=True)
in_root = "data/all"
out_root = "data/all_validated"
def process_one(f):
try:
in_root = "data/all"
out_root = "data/all_validated"
mid = MidiFile(os.path.join(in_root, f))
max_len = 0
max_len_track = None
for i, track in enumerate(mid.tracks):
if max_len < len(track):
max_len = len(track)
max_len_track = track
new_mid = MidiFile()
new_mid.tracks.append(max_len_track)
new_mid.save(os.path.join(out_root, f))
except:
pass
# for f in tqdm(os.listdir(in_root)):
Parallel(n_jobs=-1, prefer='processes')(delayed(process_one)(f) for f in tqdm(os.listdir(in_root)))