-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgenerator.py
More file actions
209 lines (176 loc) · 6.98 KB
/
generator.py
File metadata and controls
209 lines (176 loc) · 6.98 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
from music21 import converter, corpus, instrument, midi, note, tempo
from music21 import chord, pitch, environment, stream, analysis, duration
import glob
import numpy as np
from tqdm import tqdm
import os
import pandas as pd
import random
from config import get_config
import math
random.seed(123)
class Generator:
def __init__(self, args):
self.config = args
self.chars = {
",": "",
".": "",
'"': "",
"'": "",
"/": "",
"(": "",
")": "",
"{": "",
"}": "",
"[": "",
"]": "",
"!": "",
"?": "",
"#": "",
"$": "",
"%": "",
"&": "",
"*": "",
" ": "",
}
self.song_dict = dict()
self.name_id_map = pd.DataFrame(
columns=["composer", "composer_id", "orig_name", "midi_id", "saved_fname"]
) # df to store mapped info
def run(self):
dataset_dir = self.config.midi_files_path
input_path = self.config.input_save_path
data_list, composers = self.get_data_list(
dataset_dir + "maestro-v2.0.0_cleaned.csv"
)
for i, composer in tqdm(enumerate(composers)):
success = 0 # count files for each composer
track_list = list() # for uniq track id
print(
"\n################################## {} ####################################\n".format(
composer
)
)
for data in data_list:
track_comp, orig_name, file_name = data[0], data[1], data[2]
if track_comp is composer:
try:
mid = self.open_midi(dataset_dir + data[2])
segment = self.generate_segment(mid)
except:
print("ERROR: failed to open {}\t".format(file_name))
else:
# assign uniq id to midi
version = self.fetch_version(orig_name)
track_id = self.fetch_id(track_list, orig_name)
fsave_pth = (
input_path + "composer" + str(i) + "/midi" + str(track_id)
)
self.save_input(segment, fsave_pth, version) # TODO: enable
self.name_id_map = self.name_id_map.append(
{
"composer": composer,
"composer_id": i,
"orig_name": orig_name,
"midi_id": track_id,
"saved_fname": file_name,
},
ignore_index=True,
)
# print result
success += 1
print(
"{} success: {} => {} => midi{}_ver{}".format(
success, file_name, orig_name, track_id, version
)
)
# save mapped list
self.name_id_map.to_csv(input_path + "name_id_map.csv", sep=",") # TODO: enable
return
def get_data_list(self, fdir): # return preprocessed list of paths
data = pd.read_csv(fdir, encoding="euc-kr") # cleaned csv
data = data.drop(
["split", "year", "audio_filename", "duration"], axis=1
) # drop unnecessary columns
data_list = list(
zip(
data["canonical_composer"],
data["canonical_title"],
data["midi_filename"],
)
)
composers = data["canonical_composer"].unique()
return data_list, composers
def open_midi(self, file):
mf = midi.MidiFile()
mf.open(file)
mf.read()
mf.close()
return midi.translate.midiFileToStream(mf)
def generate_segment(self, mid):
stm_instr = instrument.partitionByInstrument(mid)
for pt in stm_instr.parts:
on, off, dur, pitch, vel = self.extract_notes(pt)
track_dur_sec = pt.seconds # last release
track_dur_len = int(math.ceil(track_dur_sec / 0.05))
segment = [
[[0 for k in range(128)] for i in range(track_dur_len)]
for j in range(2)
] # 2 x duration x 128
print(
"generating... dur: {:.2f}sec || len: {}".format(
track_dur_sec, track_dur_len
)
)
# iterate: each note
for j, note in enumerate(zip(on, off, dur, pitch, vel)):
x_index = int(note[0] // 0.05) # time
y_index = int(note[3]) # pitch
# onset (binary)
segment[0][x_index][y_index] = 1
# note events (velocity)
for t in range(int(note[2] // 0.05)):
# iterate: each 0.05 unit of a single note's duration
segment[1][x_index + t][y_index] = int(note[4])
return segment
def extract_notes(self, track):
offset_list = track.secondsMap
on, off, dur, pitch, vel = [], [], [], [], []
for evt in offset_list:
element = evt["element"]
if type(element) is note.Note:
on.append(evt["offsetSeconds"])
off.append(evt["endTimeSeconds"])
dur.append(evt["durationSeconds"])
pitch.append(element.pitch.ps)
vel.append(element.volume.velocity)
elif type(element) is chord.Chord:
for nt in element.notes:
on.append(evt["offsetSeconds"])
off.append(evt["endTimeSeconds"])
dur.append(evt["durationSeconds"])
pitch.append(nt.pitch.ps)
vel.append(nt.volume.velocity)
return on, off, dur, pitch, vel
def fetch_version(self, track):
track = track.lower() # case-insensitive comparison
track = track.translate(str.maketrans(self.chars)) # remove symbols
if track in self.song_dict:
self.song_dict[track] = self.song_dict[track] + 1 # update
else:
self.song_dict.update({track: 0})
return self.song_dict[track]
def fetch_id(self, lookup, name):
name = name.lower() # case-insensitive comparison
name = name.translate(str.maketrans(self.chars)) # remove symbols
if name not in lookup:
lookup.append(name)
return lookup.index(name)
def save_input(self, matrix, save_pth, vn):
if not os.path.exists(save_pth):
os.makedirs(save_pth)
np.save(save_pth + "/ver" + str(vn), matrix) # save as .npy
if __name__ == "__main__":
config, unparsed = get_config()
temp = Generator(config)
temp.run()