forked from davepagurek/Chordi.co
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.py
183 lines (158 loc) · 5.46 KB
/
parse.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
import midi
import music21
import sys
class Song:
chordMap = {
"M0": 0,
"m0": 1,
"o2": 2,
"m2": 3,
"m4": 4,
"M4": 5,
"m5": 6,
"M5": 7,
"m7": 8,
"M7": 9,
"M7^7": 10,
"m9": 11,
"M9": 12,
"o11": 13
# "M1": 0,
# "m1": 1,
# "o2": 2,
# "m2": 3,
# "m3": 4,
# "M3": 5,
# "m4": 6,
# "M4": 7,
# "m5": 8,
# "M5": 9,
# "M5^7": 10,
# "m6": 11,
# "M6": 12,
# "o7": 13
}
def __init__(self, filename, key):
pattern = midi.read_midifile("midi/"+filename)
self.key = music21.key.Key(key)
self.song = []
self.tempo = 120
self.quarterNote = pattern.resolution * 2
self.name = filename
#print self.quarterNote
track = []
for t in pattern:
track += t
track.sort(key=lambda x: x.tick)
print track
quarterBegin = 0
quarterEnd = self.quarterNote - 1
endTick = 0
for event in track:
endTick += event.tick
if type(event) is midi.SetTempoEvent:
self.setTempo(event.data)
# if type(event) is midi.EndOfTrackEvent:
# break
currentTick = 0
while quarterBegin <= endTick:
currentTick = 0
currentNotes = set()
noteEnds = set()
for event in track:
currentTick += event.tick
if currentTick > quarterEnd:
break
if currentTick >= quarterBegin and currentTick <= quarterEnd:
if type(event) is midi.NoteOnEvent:
currentNotes.add(event.data[0])
#print currentNotes
if type(event) is midi.NoteOffEvent:
noteEnds.add(event.data[0])
if len(currentNotes) > 0:
chord = music21.chord.Chord(currentNotes)
self.song.append({"from": quarterBegin, "to": quarterEnd, "chord": self.chordNumber(chord)})
elif len(noteEnds) > 0:
chord = music21.chord.Chord(noteEnds)
self.song.append({"from": quarterBegin, "to": quarterEnd, "chord": self.chordNumber(chord)})
quarterBegin += self.quarterNote
quarterEnd = quarterBegin + self.quarterNote - 1
#if type(event) is midi.NoteOnEvent:
#currentNotes.add(event.data[0])
#if type(event) is midi.NoteOffEvent:
#if event.data[0] in currentNotes:
#currentNotes.remove(event.data[0])
def dataString(self):
string = "-1"
for chord in self.song:
string += " " + str(chord.get("chord"))
string += " " + str(chord.get("chord"))
string += " 14"
return string
def chordNumber(self, chord):
#print chord.pitchedCommonName
name = chord.commonName
stream = music21.stream.Stream()
stream.append(self.key)
stream.append(chord)
# print chord.findRoot().midi%12
chordNumber = (chord.findRoot().midi + 3)%12
chordName = ""
if name == "major triad":
chordName = "M" + str(chordNumber)
elif name == "minor triad":
chordName = "m" + str(chordNumber)
elif name == "dominant seventh chord" or name == "incomplete major-seventh chord":
chordName = "M" + str(chordNumber) + "^7"
elif name == "unison" and self.key.mode == "minor":
chordName = "m" + str(chordNumber)
elif name == "unison":
chordName = "M" + str(chordNumber)
elif name == "interval class 4":
if self.key.mode == "minor":
chordName = "m" + str(chordNumber)
else:
chordName = "M" + str(chordNumber)
elif name == "interval class 5":
if self.key.mode == "minor":
chordName = "m" + str(chordNumber)
else:
chordName = "M" + str(chordNumber)
elif name == "interval class 2":
if self.key.mode == "minor":
chordName = "m" + str(chordNumber)
else:
chordName = "M" + str(chordNumber)
elif name == "interval class 3":
if self.key.mode == "minor":
chordName = "m" + str(chordNumber)
else:
chordName = "M" + str(chordNumber)
else:
print "Couldn't find: ", name
if self.key.mode == "minor":
chordName = "m" + str(chordNumber)
else:
chordName = "M" + str(chordNumber)
print name
return Song.chordMap.get(chordName, 0)
def setTempo(self, data):
timePerQuarter = ""
for digit in data:
timePerQuarter += hex(digit)[2:]
self.tempo = 60000000/self.quarterNote
def noteFromMidi(self, num):
index = ((num-21)%12 - 3) % 12
if 0 <= index < len(Song.notes):
return Song.notes[index]
else:
return "undef: " + str(index)
def save(self):
f = open("training/"+self.name+".txt",'w')
f.write(self.dataString())
f.close()
if len(sys.argv) > 1:
twinkle = Song(sys.argv[1], sys.argv[2])
twinkle.save()
print twinkle.dataString()
print twinkle.key.mode