forked from TeamTeamUSA/mid2cnc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
midiparser.py
executable file
·307 lines (245 loc) · 7.23 KB
/
midiparser.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# Placed into Public Domain in June 2006 by Sean D. Spencer
# Sean D. Spencer
# 2/19/2006
# Last Revision: 4/19/2007
# MIDI Parsing Library for Python.
import sys
TRUE = -1
FALSE = 0
class format:
SingleTrack = 0
MultipleTracksSync = 1
MultipleTracksAsync = 2
class voice:
NoteOff = 0x80
NoteOn = 0x90
PolyphonicKeyPressure = 0xA0 # note aftertouch
ControllerChange = 0xB0
ProgramChange = 0xC0
ChannelPressure = 0xD0
PitchBend = 0xE0
class meta:
FileMetaEvent = 0xFF
SMPTEOffsetMetaEvent = 0x54
SystemExclusive = 0xF0
SystemExclusivePacket = 0xF7
SequenceNumber = 0x00
TextMetaEvent = 0x01
CopyrightMetaEvent = 0x02
TrackName = 0x03
InstrumentName = 0x04
Lyric = 0x05
Marker = 0x06
CuePoint = 0x07
ChannelPrefix = 0x20
MidiPort = 0x21
EndTrack = 0x2F
SetTempo = 0x51
TimeSignature = 0x58
KeySignature = 0x59
SequencerSpecificMetaEvent = 0x7F
class EventNote:
def __init__(self):
self.note_no = None
self.velocity = None
class EventValue:
def __init__(self):
self.type = None
self.value = None
class EventAmount:
def __init__(self):
self.amount = None
class MetaEventKeySignature:
def __init__(self):
self.fifths = None
self.mode = None
class MetaEventTimeSignature:
def __init__(self):
self.numerator = None
self.log_denominator = None
self.midi_clocks = None
self.thirty_seconds = None
class MetaEventText:
def __init__(self):
self.length = None
self.text = None
class MetaEventSMPTEOffset:
def __init__(self):
self.hour = None
self.minute = None
self.second = None
self.frame = None
self.sub_frame = None
class MetaValues:
def __init__(self):
self.length = None
self.values = None
def getNumber(theString, length):
# MIDI uses big-endian for everything
sum = 0
#print "Length: " + str(length) + " strlen: " + str(len(theString))
for i in range(length):
#sum = (sum *256) + int(str[i])
sum = (sum << 8) + ord(theString[i])
return sum, theString[length:]
def getVariableLengthNumber(str):
sum = 0
i = 0
while 1:
x = ord(str[i])
i = i + 1
# sum = (sum * 127) + (x (mask) 127) # mask off the 7th bit
sum = (sum << 7) + (x & 0x7F)
# Is 7th bit clear?
if not (x & 0x80):
return sum, str[i:]
def getValues(str, n=16):
temp = []
for x in str[:n]:
temp.append(repr(ord(x)))
return temp
class File:
def __init__(self, file):
self.file = file
self.format = None
self.num_tracks = None
self.division = None
self.tracks = []
self.file = open(self.file, 'rb')
str = self.file.read()
self.file.close()
self.read(str)
def read(self, str):
assert str[:4] == "MThd"
str = str[4:]
length, str = getNumber(str, 4)
assert length == 6
self.format, str = getNumber(str, 2)
self.num_tracks, str = getNumber(str, 2)
self.division, str = getNumber(str, 2)
for i in range(self.num_tracks):
track = Track(i+1)
str = track.read(str)
self.tracks.append(track)
class Track:
def __init__(self, index):
self.number = index
self.length = None
self.events = []
def read(self, str):
self.length, str = getNumber(str[4:], 4)
track_str = str[:self.length]
prev_absolute = 0
prev_status = 0
i = 0
while track_str:
event = Event(self.number, i+1)
track_str = event.read(prev_absolute, prev_status, track_str)
prev_absolute += event.delta
prev_status = event.status
self.events.append(event)
i += 1
return str[self.length:]
class Event:
def __init__(self, track, index):
self.number = index
self.type = None
self.delta = None
self.absolute = None
self.status = None
self.channel = None
def read(self, prev_time, prev_status, str):
self.delta, str = getVariableLengthNumber(str)
self.absolute = prev_time + self.delta
# use running status?
if not (ord(str[0]) & 0x80):
# squeeze a duplication of the running status into the data string
str = prev_status + str
self.status = str[0]
self.channel = ord(self.status) & 0xF
# increment one byte, past the status
str = str[1:]
has_channel = has_meta = TRUE
# handle voice events
channel_msg = ord(self.status) & 0xF0
if channel_msg == voice.NoteOn or \
channel_msg == voice.NoteOff or \
channel_msg == voice.PolyphonicKeyPressure:
self.detail = EventNote()
self.detail.note_no = ord(str[0])
self.detail.velocity = ord(str[1])
str = str[2:]
elif channel_msg == voice.ControllerChange:
self.detail = EventValue()
self.detail.type = ord(str[0])
self.detail.value = ord(str[1])
str = str[2:]
elif channel_msg == voice.ProgramChange or \
channel_msg == voice.ChannelPressure:
self.detail = EventAmount()
self.detail.amount = ord(str[0])
str = str[1:]
elif channel_msg == voice.PitchBend:
# Pitch bend uses high accuracy 14 bit unsigned integer.
self.detail = EventAmount()
self.detail.amount = (ord(str[0]) << 7) | ord(str[1])
str = str[2:]
else: has_channel = FALSE
# handle meta events
meta_msg = ord(self.status)
if meta_msg == meta.FileMetaEvent:
meta_msg = type = ord(str[0])
length, str = getVariableLengthNumber(str[1:])
if type == meta.SetTempo or \
type == meta.ChannelPrefix:
self.detail = EventAmount()
self.detail.tempo, str = getNumber(str, length)
elif type == meta.KeySignature:
self.detail = MetaEventKeySignature()
self.detail.fifths = ord(str[0])
if ord(str[1]): self.detail.mode = "minor"
else: self.detail.mode = "major"
str = str[length:]
elif type == meta.TimeSignature:
self.detail = MetaEventTimeSignature()
self.detail.numerator = ord(str[0])
self.detail.log_denominator = ord(str[1])
self.detail.midi_clocks = ord(str[2])
self.detail.thirty_seconds = ord(str[3])
str = str[length:]
elif type == meta.TrackName or \
type == meta.TextMetaEvent or \
type == meta.Lyric or \
type == meta.CuePoint or \
type == meta.CopyrightMetaEvent:
self.detail = MetaEventText()
self.detail.length = length
self.detail.text = str[:length]
str = str[length:]
elif type == meta.SMPTEOffsetMetaEvent:
self.detail = MetaEventSMPTEOffset()
self.detail.hour = ord(str[0])
self.detail.minute = ord(str[1])
self.detail.second = ord(str[2])
self.detail.frame = ord(str[3])
self.detail.sub_frame = ord(str[4])
str = str[length:]
elif type == meta.EndTrack:
str = str[length:] # pass on to next track
else: has_meta = FALSE
elif meta_msg == meta.SystemExclusive or \
meta_msg == meta.SystemExclusivePacket:
self.detail = MetaValues()
self.detail.length, str = getVariableLengthNumber(str)
self.detail.values = getValues(str, self.detail.length)
str = str[self.detail.length:]
else: has_meta = FALSE
if has_channel:
self.type = channel_msg
elif has_meta:
self.type = meta_msg
else:
#raise "Unknown event."
self.type = None
return str