forked from jason9693/MusicTransformer-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess.py
44 lines (36 loc) · 1.29 KB
/
preprocess.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
import pickle
import os
import sys
from tqdm import tqdm
import utils
from midi_processor_fixed.processor import encode_midi
from joblib import Parallel, delayed
def preprocess_midi(path):
return encode_midi(path)
def process_one(path, save_dir):
try:
data = preprocess_midi(path)
with open('{}/{}.pickle'.format(save_dir, path.split('/')[-1]), 'wb') as f:
pickle.dump(data, f)
return 0
except:
return 1
def preprocess_midi_files_under(midi_root, save_dir):
midi_paths = list(utils.find_files_by_extensions(midi_root, ['.mid', '.midi']))
os.makedirs(save_dir, exist_ok=True)
out_fmt = '{}-{}.data'
# for path in tqdm(midi_paths):
# print(' ', end='[{}]'.format(path), flush=True)
# try:
# data = preprocess_midi(path)
# with open('{}/{}.pickle'.format(save_dir, path.split('/')[-1]), 'wb') as f:
# pickle.dump(data, f)
# except:
# pass
skipped = Parallel(n_jobs=-1, prefer="processes")(delayed(process_one)(p, save_dir) for p in tqdm(midi_paths))
print("Total: ", len(midi_paths), "skipped", sum(skipped))
if __name__ == '__main__':
preprocess_midi_files_under(
midi_root=sys.argv[1],
save_dir=sys.argv[2])
pass