forked from gwinndr/MusicTransformer-Pytorch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
preprocess_midi.py
196 lines (157 loc) · 5.25 KB
/
preprocess_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
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
# Requirements for this updated version/TMIDI version
# git clone https://github.com/asigalov61/tegridy-tools
# Then import TMIDI module from ./tegridy-tools/tegridy-tools dir
# pip install pretty_midi
import argparse
import os
import pickle
import json
import TMIDI
import pickle
import sys
from abc import ABC, abstractmethod
import pretty_midi as pyd
from pretty_midi import Note
from pprint import pprint
JSON_FILE = "maestro-v2.0.0.json"
'''
This is the data processing script
Courtesy of X-Labs
https://github.com/music-x-lab/POP909-Dataset
PLEASE NOTE THAT THE LICENSE FOR THIS CODE IS MIT
============
This script will allow you to quickly process the MIDI Files into the Google Magenta's music representation
as like [Music Transformer](https://magenta.tensorflow.org/music-transformer)
[Performance RNN](https://magenta.tensorflow.org/performance-rnn).
'''
total = 0
def process_midi(path):
global total
data = pyd.PrettyMIDI(path)
main_notes = []
acc_notes = []
for ins in data.instruments:
acc_notes.extend(ins.notes)
for i in range(len(main_notes)):
main_notes[i].start = round(main_notes[i].start,2)
main_notes[i].end = round(main_notes[i].end,2)
for i in range(len(acc_notes)):
acc_notes[i].start = round(acc_notes[i].start,2)
acc_notes[i].end = round(acc_notes[i].end,2)
main_notes.sort(key = lambda x:x.start)
acc_notes.sort(key = lambda x:x.start)
mpr = TMIDI.Tegridy_RPR_MidiEventProcessor()
repr_seq = mpr.encode(acc_notes)
total += len(repr_seq)
print('Converted file:', path)
print('Total INTs count:', len(repr_seq))
print('=' * 70)
return repr_seq
def process_all_midis(midi_root, save_dir):
save_py = []
midi_paths = [d for d in os.listdir(midi_root)]
i = 0
out_fmt = '{}-{}.data'
for path in midi_paths:
pprint(path)
filename = midi_root + path
try:
data = process_midi(filename)
except KeyboardInterrupt:
print(' Abort')
return
except EOFError:
print('EOF Error')
return
save_py.append(data)
# pprint(save_py, compact=True)
save_py = np.array(save_py, dtype='object')
print('=' * 70)
print('Total number of converted MIDIs:', save_py.size)
print('Total INTs count:', total)
np.save(save_dir + 'notes_representations.npy', save_py)
# prep_midi
def prep_midi(maestro_root, output_dir):
"""
----------
Author: Damon Gwinn
----------
Pre-processes the maestro dataset, putting processed midi data (train, eval, test) into the
given output folder
----------
"""
train_dir = os.path.join(output_dir, "train")
os.makedirs(train_dir, exist_ok=True)
val_dir = os.path.join(output_dir, "val")
os.makedirs(val_dir, exist_ok=True)
test_dir = os.path.join(output_dir, "test")
os.makedirs(test_dir, exist_ok=True)
maestro_json_file = os.path.join(maestro_root, JSON_FILE)
if(not os.path.isfile(maestro_json_file)):
print("ERROR: Could not find file:", maestro_json_file)
return False
maestro_json = json.load(open(maestro_json_file, "r"))
print("Found", len(maestro_json), "pieces")
print("Preprocessing...")
total_count = 0
train_count = 0
val_count = 0
test_count = 0
for piece in maestro_json:
mid = os.path.join(maestro_root, piece["midi_filename"])
split_type = piece["split"]
f_name = mid.split("/")[-1] + ".pickle"
if(split_type == "train"):
o_file = os.path.join(train_dir, f_name)
train_count += 1
elif(split_type == "validation"):
o_file = os.path.join(val_dir, f_name)
val_count += 1
elif(split_type == "test"):
o_file = os.path.join(test_dir, f_name)
test_count += 1
else:
print("ERROR: Unrecognized split type:", split_type)
return False
prepped = process_midi(mid)
o_stream = open(o_file, "wb")
pickle.dump(prepped, o_stream)
o_stream.close()
total_count += 1
if(total_count % 50 == 0):
print(total_count, "/", len(maestro_json))
print("Num Train:", train_count)
print("Num Val:", val_count)
print("Num Test:", test_count)
return True
# parse_args
def parse_args():
"""
----------
Author: Damon Gwinn
----------
Parses arguments for preprocess_midi using argparse
----------
"""
parser = argparse.ArgumentParser()
parser.add_argument("maestro_root", type=str, help="Root folder for the Maestro dataset")
parser.add_argument("-output_dir", type=str, default="./dataset/e_piano", help="Output folder to put the preprocessed midi into")
return parser.parse_args()
# main
def main():
"""
----------
Author: Damon Gwinn
----------
Entry point. Preprocesses maestro and saved midi to specified output folder.
----------
"""
args = parse_args()
maestro_root = args.maestro_root
output_dir = args.output_dir
print("Preprocessing midi files and saving to", output_dir)
prep_midi(maestro_root, output_dir)
print("Done!")
print("")
if __name__ == "__main__":
main()