-
Notifications
You must be signed in to change notification settings - Fork 23
/
preprocess.py
46 lines (38 loc) · 1.3 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
45
46
import os
import re
import sys
import torch
import hashlib
from progress.bar import Bar
from sequence import NoteSeq, EventSeq, ControlSeq
import utils
import config
def preprocess_midi(path):
note_seq = NoteSeq.from_midi_file(path)
note_seq.adjust_time(-note_seq.notes[0].start)
event_seq = EventSeq.from_note_seq(note_seq)
control_seq = ControlSeq.from_event_seq(event_seq)
return event_seq.to_array(), control_seq.to_compressed_array()
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 Bar('Processing').iter(midi_paths):
print(' ', end='[{}]'.format(path), flush=True)
try:
data = preprocess_midi(path)
except KeyboardInterrupt:
print(' Abort')
return
except:
print(' Error')
continue
name = os.path.basename(path)
code = hashlib.md5(path.encode()).hexdigest()
save_path = os.path.join(save_dir, out_fmt.format(name, code))
torch.save(data, save_path)
print('Done')
if __name__ == '__main__':
preprocess_midi_files_under(
midi_root=sys.argv[1],
save_dir=sys.argv[2])