-
Notifications
You must be signed in to change notification settings - Fork 66
/
convert_clean.py
153 lines (121 loc) · 5.21 KB
/
convert_clean.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
from __future__ import print_function
import os
import json
import errno
from pypianoroll import Multitrack, Track
import pretty_midi
import shutil
ROOT_PATH = '/Users/sumuzhao/Downloads/'
converter_path = os.path.join(ROOT_PATH, 'MIDI/pop/pop_test/converter')
cleaner_path = os.path.join(ROOT_PATH, 'MIDI/pop/pop_test/cleaner')
def make_sure_path_exists(path):
"""Create all intermediate-level directories if the given path does not
exist"""
try:
os.makedirs(path)
except OSError as exception:
if exception.errno != errno.EEXIST:
raise
def get_midi_path(root):
"""Return a list of paths to MIDI files in `root` (recursively)"""
filepaths = []
for dirpath, _, filenames in os.walk(root):
for filename in filenames:
if filename.endswith('.mid'):
filepaths.append(os.path.join(dirpath, filename))
return filepaths
def get_midi_info(pm):
"""Return useful information from a pretty_midi.PrettyMIDI instance"""
if pm.time_signature_changes:
pm.time_signature_changes.sort(key=lambda x: x.time)
first_beat_time = pm.time_signature_changes[0].time
else:
first_beat_time = pm.estimate_beat_start()
tc_times, tempi = pm.get_tempo_changes()
if len(pm.time_signature_changes) == 1:
time_sign = '{}/{}'.format(pm.time_signature_changes[0].numerator,
pm.time_signature_changes[0].denominator)
else:
time_sign = None
midi_info = {
'first_beat_time': first_beat_time,
'num_time_signature_change': len(pm.time_signature_changes),
'time_signature': time_sign,
'tempo': tempi[0] if len(tc_times) == 1 else None
}
return midi_info
def midi_filter(midi_info):
"""Return True for qualified midi files and False for unwanted ones"""
if midi_info['first_beat_time'] > 0.0:
return False
elif midi_info['num_time_signature_change'] > 1:
return False
elif midi_info['time_signature'] not in ['4/4']:
return False
return True
def get_merged(multitrack):
"""Return a `pypianoroll.Multitrack` instance with piano-rolls merged to
five tracks (Bass, Drums, Guitar, Piano and Strings)"""
category_list = {'Bass': [], 'Drums': [], 'Guitar': [], 'Piano': [], 'Strings': []}
program_dict = {'Piano': 0, 'Drums': 0, 'Guitar': 24, 'Bass': 32, 'Strings': 48}
for idx, track in enumerate(multitrack.tracks):
if track.is_drum:
category_list['Drums'].append(idx)
elif track.program//8 == 0:
category_list['Piano'].append(idx)
elif track.program//8 == 3:
category_list['Guitar'].append(idx)
elif track.program//8 == 4:
category_list['Bass'].append(idx)
else:
category_list['Strings'].append(idx)
tracks = []
for key in category_list:
if category_list[key]:
merged = multitrack[category_list[key]].get_merged_pianoroll()
tracks.append(Track(merged, program_dict[key], key == 'Drums', key))
else:
tracks.append(Track(None, program_dict[key], key == 'Drums', key))
return Multitrack(None, tracks, multitrack.tempo, multitrack.downbeat, multitrack.beat_resolution, multitrack.name)
def converter(filepath):
"""Save a multi-track piano-roll converted from a MIDI file to target
dataset directory and update MIDI information to `midi_dict`"""
try:
midi_name = os.path.splitext(os.path.basename(filepath))[0]
multitrack = Multitrack(resolution=24, name=midi_name)
pm = pretty_midi.PrettyMIDI(filepath)
midi_info = get_midi_info(pm)
multitrack = pypianoroll.from_pretty_midi(pm)
merged = get_merged(multitrack)
make_sure_path_exists(converter_path)
merged.save(os.path.join(converter_path, midi_name + '.npz'))
return [midi_name, midi_info]
except:
return None
def main():
"""Main function of the converter"""
midi_paths = get_midi_path(os.path.join(ROOT_PATH, 'MIDI/pop/pop_test/origin_midi'))
midi_dict = {}
kv_pairs = [converter(midi_path) for midi_path in midi_paths]
for kv_pair in kv_pairs:
if kv_pair is not None:
midi_dict[kv_pair[0]] = kv_pair[1]
with open(os.path.join(ROOT_PATH, 'MIDI/pop/pop_test/midis.json'), 'w') as outfile:
json.dump(midi_dict, outfile)
print("[Done] {} files out of {} have been successfully converted".format(len(midi_dict), len(midi_paths)))
with open(os.path.join(ROOT_PATH, 'MIDI/pop/pop_test/midis.json')) as infile:
midi_dict = json.load(infile)
count = 0
make_sure_path_exists(cleaner_path)
midi_dict_clean = {}
for key in midi_dict:
if midi_filter(midi_dict[key]):
midi_dict_clean[key] = midi_dict[key]
count += 1
shutil.copyfile(os.path.join(converter_path, key + '.npz'),
os.path.join(cleaner_path, key + '.npz'))
with open(os.path.join(ROOT_PATH, 'MIDI/pop/pop_test/midis_clean.json'), 'w') as outfile:
json.dump(midi_dict_clean, outfile)
print("[Done] {} files out of {} have been successfully cleaned".format(count, len(midi_dict)))
if __name__ == "__main__":
main()