Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[b75e77]: plugin.audio.mozart / resources / lib / midi.py Maximize Restore History

Download this file

midi.py    425 lines (367 with data), 14.7 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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
"""
midi.py -- MIDI classes and parser in Python
Placed into the public domain in December 2001 by Will Ware
Modifications to achieve concatenation of MIDI files: 2011, Assen Totin, assen.totin@gmail.com
"""
import sys, string, types, exceptions
def showstr(str, n=16):
for x in str[:n]:
print ('%02x' % ord(x)),
print
def getNumber(str, length):
# MIDI uses big-endian for everything
sum = 0
for i in range(length):
sum = (sum << 8) + ord(str[i])
return sum, str[length:]
def getVariableLengthNumber(str):
sum = 0
i = 0
while 1:
x = ord(str[i])
i = i + 1
sum = (sum << 7) + (x & 0x7F)
if not (x & 0x80):
return sum, str[i:]
def putNumber(num, length):
# MIDI uses big-endian for everything
lst = [ ]
for i in range(length):
n = 8 * (length - 1 - i)
lst.append(chr((num >> n) & 0xFF))
return string.join(lst, "")
def putVariableLengthNumber(x):
lst = [ ]
while 1:
y, x = x & 0x7F, x >> 7
lst.append(chr(y + 0x80))
if x == 0:
break
lst.reverse()
lst[-1] = chr(ord(lst[-1]) & 0x7f)
return string.join(lst, "")
class EnumException(exceptions.Exception):
pass
class Enumeration:
def __init__(self, enumList):
lookup = { }
reverseLookup = { }
i = 0
uniqueNames = [ ]
uniqueValues = [ ]
for x in enumList:
if type(x) == types.TupleType:
x, i = x
if type(x) != types.StringType:
raise EnumException, "enum name is not a string: " + x
if type(i) != types.IntType:
raise EnumException, "enum value is not an integer: " + i
if x in uniqueNames:
raise EnumException, "enum name is not unique: " + x
if i in uniqueValues:
raise EnumException, "enum value is not unique for " + x
uniqueNames.append(x)
uniqueValues.append(i)
lookup[x] = i
reverseLookup[i] = x
i = i + 1
self.lookup = lookup
self.reverseLookup = reverseLookup
def __add__(self, other):
lst = [ ]
for k in self.lookup.keys():
lst.append((k, self.lookup[k]))
for k in other.lookup.keys():
lst.append((k, other.lookup[k]))
return Enumeration(lst)
def hasattr(self, attr):
return self.lookup.has_key(attr)
def has_value(self, attr):
return self.reverseLookup.has_key(attr)
def __getattr__(self, attr):
if not self.lookup.has_key(attr):
raise AttributeError
return self.lookup[attr]
def whatis(self, value):
return self.reverseLookup[value]
channelVoiceMessages = Enumeration([("NOTE_OFF", 0x80),
("NOTE_ON", 0x90),
("POLYPHONIC_KEY_PRESSURE", 0xA0),
("CONTROLLER_CHANGE", 0xB0),
("PROGRAM_CHANGE", 0xC0),
("CHANNEL_KEY_PRESSURE", 0xD0),
("PITCH_BEND", 0xE0)])
channelModeMessages = Enumeration([("ALL_SOUND_OFF", 0x78),
("RESET_ALL_CONTROLLERS", 0x79),
("LOCAL_CONTROL", 0x7A),
("ALL_NOTES_OFF", 0x7B),
("OMNI_MODE_OFF", 0x7C),
("OMNI_MODE_ON", 0x7D),
("MONO_MODE_ON", 0x7E),
("POLY_MODE_ON", 0x7F)])
metaEvents = Enumeration([("SEQUENCE_NUMBER", 0x00),
("TEXT_EVENT", 0x01),
("COPYRIGHT_NOTICE", 0x02),
("SEQUENCE_TRACK_NAME", 0x03),
("INSTRUMENT_NAME", 0x04),
("LYRIC", 0x05),
("MARKER", 0x06),
("CUE_POINT", 0x07),
("MIDI_CHANNEL_PREFIX", 0x20),
("MIDI_PORT", 0x21),
("END_OF_TRACK", 0x2F),
("SET_TEMPO", 0x51),
("SMTPE_OFFSET", 0x54),
("TIME_SIGNATURE", 0x58),
("KEY_SIGNATURE", 0x59),
("SEQUENCER_SPECIFIC_META_EVENT", 0x7F)])
# runningStatus appears to want to be an attribute of a MidiTrack. But
# it doesn't seem to do any harm to implement it as a global.
runningStatus = None
class MidiEvent:
def __init__(self, track):
self.track = track
self.time = None
self.channel = self.pitch = self.velocity = self.data = None
def __cmp__(self, other):
# assert self.time != None and other.time != None
return cmp(self.time, other.time)
def __repr__(self):
r = ("<MidiEvent %s, t=%s, track=%s, channel=%s" %
(self.type,
repr(self.time),
self.track.index,
repr(self.channel)))
for attrib in ["pitch", "data", "velocity"]:
if getattr(self, attrib) != None:
r = r + ", " + attrib + "=" + repr(getattr(self, attrib))
return r + ">"
def read(self, time, str):
global runningStatus
self.time = time
# do we need to use running status?
if not (ord(str[0]) & 0x80):
str = runningStatus + str
runningStatus = x = str[0]
x = ord(x)
y = x & 0xF0
z = ord(str[1])
if channelVoiceMessages.has_value(y):
self.channel = (x & 0x0F) + 1
self.type = channelVoiceMessages.whatis(y)
if (self.type == "PROGRAM_CHANGE" or
self.type == "CHANNEL_KEY_PRESSURE"):
self.data = z
return str[2:]
else:
self.pitch = z
self.velocity = ord(str[2])
channel = self.track.channels[self.channel - 1]
if (self.type == "NOTE_OFF" or
(self.velocity == 0 and self.type == "NOTE_ON")):
channel.noteOff(self.pitch, self.time)
elif self.type == "NOTE_ON":
channel.noteOn(self.pitch, self.time, self.velocity)
return str[3:]
elif y == 0xB0 and channelModeMessages.has_value(z):
self.channel = (x & 0x0F) + 1
self.type = channelModeMessages.whatis(z)
if self.type == "LOCAL_CONTROL":
self.data = (ord(str[2]) == 0x7F)
elif self.type == "MONO_MODE_ON":
self.data = ord(str[2])
return str[3:]
elif x == 0xF0 or x == 0xF7:
self.type = {0xF0: "F0_SYSEX_EVENT",
0xF7: "F7_SYSEX_EVENT"}[x]
length, str = getVariableLengthNumber(str[1:])
self.data = str[:length]
return str[length:]
elif x == 0xFF:
if not metaEvents.has_value(z):
print "Unknown meta event: FF %02X" % z
sys.stdout.flush()
raise "Unknown midi event type"
self.type = metaEvents.whatis(z)
length, str = getVariableLengthNumber(str[2:])
self.data = str[:length]
return str[length:]
raise "Unknown midi event type"
def write(self):
sysex_event_dict = {"F0_SYSEX_EVENT": 0xF0,
"F7_SYSEX_EVENT": 0xF7}
if channelVoiceMessages.hasattr(self.type):
x = chr((self.channel - 1) +
getattr(channelVoiceMessages, self.type))
if (self.type != "PROGRAM_CHANGE" and
self.type != "CHANNEL_KEY_PRESSURE"):
data = chr(self.pitch) + chr(self.velocity)
else:
data = chr(self.data)
return x + data
elif channelModeMessages.hasattr(self.type):
x = getattr(channelModeMessages, self.type)
x = (chr(0xB0 + (self.channel - 1)) +
chr(x) +
chr(self.data))
return x
elif sysex_event_dict.has_key(self.type):
str = chr(sysex_event_dict[self.type])
str = str + putVariableLengthNumber(len(self.data))
return str + self.data
elif metaEvents.hasattr(self.type):
str = chr(0xFF) + chr(getattr(metaEvents, self.type))
str = str + putVariableLengthNumber(len(self.data))
return str + self.data
else:
raise "unknown midi event type: " + self.type
"""
register_note() is a hook that can be overloaded from a script that
imports this module. Here is how you might do that, if you wanted to
store the notes as tuples in a list. Including the distinction
between track and channel offers more flexibility in assigning voices.
import midi
notelist = [ ]
def register_note(t, c, p, v, t1, t2):
notelist.append((t, c, p, v, t1, t2))
midi.register_note = register_note
"""
def register_note(track_index, channel_index, pitch, velocity,
keyDownTime, keyUpTime):
pass
class MidiChannel:
"""A channel (together with a track) provides the continuity connecting
a NOTE_ON event with its corresponding NOTE_OFF event. Together, those
define the beginning and ending times for a Note."""
def __init__(self, track, index):
self.index = index
self.track = track
self.pitches = { }
def __repr__(self):
return "<MIDI channel %d>" % self.index
def noteOn(self, pitch, time, velocity):
self.pitches[pitch] = (time, velocity)
def noteOff(self, pitch, time):
if self.pitches.has_key(pitch):
keyDownTime, velocity = self.pitches[pitch]
register_note(self.track.index, self.index, pitch, velocity,
keyDownTime, time)
del self.pitches[pitch]
# The case where the pitch isn't in the dictionary is illegal,
# I think, but we probably better just ignore it.
class DeltaTime(MidiEvent):
type = "DeltaTime"
def read(self, oldstr):
self.time, newstr = getVariableLengthNumber(oldstr)
return self.time, newstr
def write(self):
str = putVariableLengthNumber(self.time)
return str
class MidiTrack:
def __init__(self, index):
self.index = index
self.events = [ ]
self.channels = [ ]
self.length = 0
for i in range(16):
self.channels.append(MidiChannel(self, i+1))
def readEvents(self, str, is_first=0, is_last=0):
time = 0
assert str[:4] == "MTrk"
length, str = getNumber(str[4:], 4)
self.length = length
mystr = str[:length]
remainder = str[length:]
while mystr:
delta_t = DeltaTime(self)
dt, mystr = delta_t.read(mystr)
time = time + dt
#self.events.append(delta_t)
e = MidiEvent(self)
mystr = e.read(time, mystr)
# From the first track, read all events except the END_OF_TRACK
if is_first == 1:
if e.type != "END_OF_TRACK":
self.events.append(delta_t)
self.events.append(e)
# From the last track, read only notes plus the END_OF_TRACK
elif is_last == 1:
if e.type == "NOTE_ON" or e.type == "NOTE_OFF" or e.type == "END_OF_TRACK":
self.events.append(delta_t)
self.events.append(e)
# From all other tracks read, read only notes
else:
if e.type == "NOTE_ON" or e.type == "NOTE_OFF":
self.events.append(delta_t)
self.events.append(e)
return remainder
def write(self):
time = self.events[0].time
# build str using MidiEvents
str = ""
for e in self.events:
str = str + e.write()
return "MTrk" + putNumber(len(str), 4) + str
def __repr__(self):
r = "<MidiTrack %d -- %d events\n" % (self.index, len(self.events))
for e in self.events:
r = r + " " + `e` + "\n"
return r + " >"
class MidiFile:
def __init__(self):
self.file = None
self.format = 1
self.tracks = [ ]
self.ticksPerQuarterNote = None
self.ticksPerSecond = None
def open(self, filename, attrib="rb"):
if filename == None:
if attrib in ["r", "rb"]:
self.file = sys.stdin
else:
self.file = sys.stdout
else:
self.file = open(filename, attrib)
def __repr__(self):
r = "<MidiFile %d tracks\n" % len(self.tracks)
for t in self.tracks:
r = r + " " + `t` + "\n"
return r + ">"
def close(self):
self.file.close()
def readEvents(self, is_first, is_last):
self.readEventsStr(self.file.read(), is_first, is_last)
def readEventsStr(self, str, is_first=0, is_last=0):
assert str[:4] == "MThd"
length, str = getNumber(str[4:], 4)
assert length == 6
format, str = getNumber(str, 2)
self.format = format
assert format == 0 or format == 1 # dunno how to handle 2
numTracks, str = getNumber(str, 2)
division, str = getNumber(str, 2)
if division & 0x8000:
framesPerSecond = -((division >> 8) | -128)
ticksPerFrame = division & 0xFF
assert ticksPerFrame == 24 or ticksPerFrame == 25 or \
ticksPerFrame == 29 or ticksPerFrame == 30
if ticksPerFrame == 29: ticksPerFrame = 30 # drop frame
self.ticksPerSecond = ticksPerFrame * framesPerSecond
else:
self.ticksPerQuarterNote = division & 0x7FFF
for i in range(numTracks):
trk = MidiTrack(i)
str = trk.readEvents(str, is_first, is_last)
self.tracks.append(trk)
def write(self):
self.file.write(self.writestr())
def writestr(self):
division = self.ticksPerQuarterNote
# Don't handle ticksPerSecond yet, too confusing
assert (division & 0x8000) == 0
str = "MThd" + putNumber(6, 4) + putNumber(self.format, 2)
str = str + putNumber(len(self.tracks), 2)
str = str + putNumber(division, 2)
for trk in self.tracks:
str = str + trk.write()
return str