forked from ZWMiller/NNCompose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
midifile.py
188 lines (172 loc) · 7.89 KB
/
midifile.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
import subprocess
import os
import sys
import numpy as np
import pandas as pd
import random
import csv
import time
from operator import itemgetter
class midiFile(object):
def __init__(self,fname):
self.fname = fname
self.oname = os.path.splitext(self.fname)[0] + "_NNCompose.mid"
self.my_cols = ["instrument", "timing", "cmd","pitchshift","note","velocity","timeSig"]
self.convertMidToCsv()
self.df = self.convertCsvToDataframe()
self.header = self.df[self.df['cmd'].str.contains("Header")]
self.qn = int(self.header.iloc[0]['velocity'])
self.tempo = int(self.df[self.df['cmd'].str.contains("Tempo")].iloc[0]['pitchshift'])
self.command = self.df[self.df['instrument'] == 1]
self.track2 = self.df[self.df['instrument'] == 2]
self.startTrack = self.track2[self.track2['cmd'].str.contains("Start")]
self.eof = self.df[self.df['cmd'].str.contains("End_of_file")]
self.add_timing_info_to_df()
self.df = self.df[self.df['cmd'].str.contains("Note_on")]
self.df['note'] = self.df['note'].apply(np.int64)
def add_timing_info_to_df(self):
timing_df = self.df[self.df['cmd'].str.contains("Note_")]
timing = timing_df.sort_values(by=['note','timing'])
timing['length'] = timing.timing.diff()
timing['beat'] = timing.length.shift(-1)
beats = timing[timing['cmd'].str.contains("Note_on")]
beats['note_length'] = beats.beat/self.qn*4
beats['note_length'] = beats.note_length.astype(int)
self.df = self.df.join(beats['note_length'])
#self.df['note_length'] = self.df['note_length'].astype(int)
def forceDataTypes(self,df):
df['instrument'] = df['instrument'].astype(int)
df['timing'] = df['timing'].astype(int)
df['pitchshift'] = df['pitchshift'].astype(int)
df['note'] = df['note'].astype(int)
df['velocity'] = df['velocity'].astype(int)
df['cmd'] = df['cmd'].astype(str)
def convertMidToCsv(self):
basename = os.path.splitext(self.fname)
inp = "midiIn/" + str(basename[0]) + ".mid"
if not os.path.isfile(inp):
print("Did not find .mid file, trying .midi!")
inp = "midiIn/" + str(basename[0]) + ".midi"
if not os.path.isfile(inp):
raise ValueError("No MIDI File Found")
out = "csv/" + str(basename[0]) + ".csv"
cmd = "midicsv " + inp + " >! " + out
csvCreated = subprocess.Popen(cmd, shell=True)
print("Converting to CSV")
time.sleep(5)
def convertCsvToDataframe(self):
basename = os.path.splitext(self.fname)
inp = "csv/" + str(basename[0]) + ".csv"
return pd.read_csv(inp, names=self.my_cols)
def convertNotesToName(self):
notename = ['C','C#','D','D#','E','F','F#','G','G#','A','A#','B']
#self.df['noteName'] = self.df['note'].apply(lambda x: int(x % 12))
self.df['noteName'] = self.df['note'] #if trying to include octaves)
def convertMidiToMp3(self):
basename = os.path.splitext(self.oname)
inname = "midiOut/" + str(basename[0]) + ".mid"
outname = "mp3Out/" + str(basename[0]) + ".mp3"
cmd = "timidity -Ow -o - " + inname + "| lame - " + outname
mp3Created = subprocess.Popen(cmd, shell=True)
time.sleep(5)
print("Created ", outname)
def get_trainable_arrays(self, seq_length=10, col='note'):
data = self.df[col].values
x = []
y = []
for i in range(seq_length,len(data)):
x.append(data[i-seq_length:i])
y.append(data[i])
print("X & Y Correlation:\n",x[0],y[0],"\nActual Data Correlation:\n",data[seq_length-2:seq_length+1])
X = np.reshape(x, (len(x), seq_length, 1))
Y = np.reshape(y, len(y))
return X,Y
def encode_target(self, y, n_values=128):
ohe = []
for val in y:
el = np.zeros(n_values)
el[int(val)-1] = 1
ohe.append(el)
return np.array(ohe)
def convert_to_midi_format(self, result_notes):
notes = []
currentTime = 0
for ind in result_notes:
beatLength = 3
while beatLength in [3,5,6,7]:
beatLength = random.randint(1,4)
dt = int(self.qn/beatLength)
endtime = currentTime + dt
for note in ind:
notes.append([2,currentTime, "Note_on_c", 2, int(note), 95,False])
notes.append([2,endtime, "Note_off_c", 2, int(note), 95, False])
currentTime = endtime
notes.append([2, currentTime, "End_track"])
return sorted(notes,key=itemgetter(1))
def check_valid_entry(self, item):
if type(item) == str:
return True
elif not np.isnan(item):
return True
else:
return False
def makeMidiOut(self,notes):
basename = os.path.splitext(self.oname)
filename = "csv/"+str(basename[0])+".csv"
file_order = [self.header,self.command,self.startTrack,notes]
with open(filename, 'w') as f:
for section in file_order:
if not type(section) == list:
try:
l = section.values.tolist()
except AttributeError:
l = section.tolist()
else:
l = section
for item in l:
outstr = ""
if len(item) == 3:
if self.check_valid_entry(item[0]):
outstr = outstr + str(int(item[0])) + ", "
if self.check_valid_entry(item[1]):
outstr = outstr + str(int(item[1])) + ", "
if self.check_valid_entry(item[2]):
outstr = outstr + str(item[2].lstrip(' ')) + ", "
else:
if self.check_valid_entry(item[0]):
outstr = outstr + str(int(item[0])) + ", "
if self.check_valid_entry(item[1]):
outstr = outstr + str(int(item[1])) + ", "
if self.check_valid_entry(item[2]):
outstr = outstr + str(item[2].lstrip(' ')) + ", "
if self.check_valid_entry(item[3]):
if type(item[3]) == str:
outstr = outstr + str(item[3]) + ", "
else:
outstr = outstr + str(int(item[3])) + ", "
if self.check_valid_entry(item[4]):
outstr = outstr + str(int(item[4])) + ", "
if self.check_valid_entry(item[5]):
outstr = outstr + str(int(item[5])) + ", "
if self.check_valid_entry(item[6]):
outstr = outstr + str(int(item[6])) + ", "
outstr = outstr.rstrip(', ') + "\n"
f.write(outstr)
item = self.eof.iloc[0].values
if self.check_valid_entry(item[0]):
outstr = outstr + str(int(item[0])) + ", "
if self.check_valid_entry(item[1]):
outstr = outstr + str(int(item[1])) + ", "
if self.check_valid_entry(item[2]):
outstr = outstr + str(item[2].lstrip(' ')) + ", "
outstr = outstr.rstrip(', ') + "\n"
f.write(outstr)
self.csv_to_midi()
def csv_to_midi(self):
basename = os.path.splitext(self.oname)
filename = "csv/"+str(basename[0])+".csv"
out = "midiOut/" + str(basename[0]) + ".mid"
cmd = "csvmidi " + filename + " " + out
midCreated = subprocess.Popen(cmd, shell=True)
print("Converting to MIDI")
time.sleep(5)