-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.py
384 lines (312 loc) · 10.1 KB
/
events.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
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
import math
class EventRegistry(object):
Events = {}
MetaEvents = {}
def register_event(cls, event, bases):
if (Event in bases) or (NoteEvent in bases):
assert event.statusmsg not in cls.Events, \
"Event %s already registered" % event.name
cls.Events[event.statusmsg] = event
elif (MetaEvent in bases) or (MetaEventWithText in bases):
if event.metacommand is not None:
assert event.metacommand not in cls.MetaEvents, \
"Event %s already registered" % event.name
cls.MetaEvents[event.metacommand] = event
else:
raise ValueError( "Unknown bases class in event type: "+event.name)
register_event = classmethod(register_event)
class AbstractEvent(object):
__slots__ = ['tick', 'data']
name = "Generic MIDI Event"
length = 0
statusmsg = 0x0
class __metaclass__(type):
def __init__(cls, name, bases, dict):
if name not in ['AbstractEvent', 'Event', 'MetaEvent', 'NoteEvent',
'MetaEventWithText']:
EventRegistry.register_event(cls, bases)
def __init__(self, **kw):
if type(self.length) == int:
defdata = [0] * self.length
else:
defdata = []
self.tick = 0
self.data = defdata
for key in kw:
setattr(self, key, kw[key])
def __lt__(self, other):
if(self.tick == other.tick):
return self.data < other.data
return self.tick < other.tick
# def __cmp__(self, other):
# if self.tick < other.tick: return -1
# elif self.tick > other.tick: return 1
# return cmp(self.data, other.data)
def __baserepr__(self, keys=[]):
keys = ['tick'] + keys + ['data']
body = []
for key in keys:
val = getattr(self, key)
keyval = "%s=%r" % (key, val)
body.append(keyval)
body = str.join(', ', body)
return "midi.%s(%s)" % (self.__class__.__name__, body)
def __repr__(self):
return self.__baserepr__()
class Event(AbstractEvent):
__slots__ = ['channel']
name = 'Event'
def __init__(self, **kw):
if 'channel' not in kw:
kw = kw.copy()
self.channel = 0 #hack edit
kw['channel'] = 0
super(Event, self).__init__(**kw)
def copy(self, **kw):
_kw = {'channel': self.channel, 'tick': self.tick, 'data': self.data}
_kw.update(kw)
return self.__class__(**_kw)
def __cmp__(self, other):
if self.tick < other.tick: return -1
elif self.tick > other.tick: return 1
return 0
def __repr__(self):
return self.__baserepr__(['channel'])
def is_event(cls, statusmsg):
return (cls.statusmsg == (statusmsg & 0xF0))
is_event = classmethod(is_event)
"""
MetaEvent is a special subclass of Event that is not meant to
be used as a concrete class. It defines a subset of Events known
as the Meta events.
"""
class MetaEvent(AbstractEvent):
statusmsg = 0xFF
metacommand = 0x0
name = 'Meta Event'
def is_event(cls, statusmsg):
return (statusmsg == 0xFF)
is_event = classmethod(is_event)
"""
NoteEvent is a special subclass of Event that is not meant to
be used as a concrete class. It defines the generalities of NoteOn
and NoteOff events.
"""
class NoteEvent(Event):
# __slots__ = ['pitch', 'velocity']
length = 2
def get_pitch(self):
return self.data[0]
def set_pitch(self, val):
self.data[0] = val
pitch = property(get_pitch, set_pitch)
def get_velocity(self):
return self.data[1]
def set_velocity(self, val):
self.data[1] = val
velocity = property(get_velocity, set_velocity)
class NoteOnEvent(NoteEvent):
statusmsg = 0x90
name = 'Note On'
class NoteOffEvent(NoteEvent):
statusmsg = 0x80
name = 'Note Off'
class AfterTouchEvent(Event):
statusmsg = 0xA0
length = 2
name = 'After Touch'
def get_pitch(self):
return self.data[0]
def set_pitch(self, val):
self.data[0] = val
pitch = property(get_pitch, set_pitch)
def get_value(self):
return self.data[1]
def set_value(self, val):
self.data[1] = val
value = property(get_value, set_value)
class ControlChangeEvent(Event):
# __slots__ = ['control', 'value']
statusmsg = 0xB0
length = 2
name = 'Control Change'
def set_control(self, val):
self.data[0] = val
def get_control(self):
return self.data[0]
control = property(get_control, set_control)
def set_value(self, val):
self.data[1] = val
def get_value(self):
return self.data[1]
value = property(get_value, set_value)
class ProgramChangeEvent(Event):
# __slots__ = ['value']
statusmsg = 0xC0
length = 1
name = 'Program Change'
def set_value(self, val):
self.data[0] = val
def get_value(self):
return self.data[0]
value = property(get_value, set_value)
class ChannelAfterTouchEvent(Event):
# __slots__ = ['value']
statusmsg = 0xD0
length = 1
name = 'Channel After Touch'
def set_value(self, val):
self.data[1] = val
def get_value(self):
return self.data[1]
value = property(get_value, set_value)
class PitchWheelEvent(Event):
# __slots__ = ['pitch']
statusmsg = 0xE0
length = 2
name = 'Pitch Wheel'
def get_pitch(self):
return ((self.data[1] << 7) | self.data[0]) - 0x2000
def set_pitch(self, pitch):
value = pitch + 0x2000
self.data[0] = value & 0x7F
self.data[1] = (value >> 7) & 0x7F
pitch = property(get_pitch, set_pitch)
class SysexEvent(Event):
statusmsg = 0xF0
name = 'SysEx'
length = 'varlen'
def is_event(cls, statusmsg):
return (cls.statusmsg == statusmsg)
is_event = classmethod(is_event)
class SequenceNumberMetaEvent(MetaEvent):
name = 'Sequence Number'
metacommand = 0x00
length = 2
class MetaEventWithText(MetaEvent):
def __init__(self, **kw):
super(MetaEventWithText, self).__init__(**kw)
if 'text' not in kw:
self.text = ''.join(chr(datum) for datum in self.data)
def __repr__(self):
return self.__baserepr__(['text'])
class TextMetaEvent(MetaEventWithText):
name = 'Text'
metacommand = 0x01
length = 'varlen'
class CopyrightMetaEvent(MetaEventWithText):
name = 'Copyright Notice'
metacommand = 0x02
length = 'varlen'
class TrackNameEvent(MetaEventWithText):
name = 'Track Name'
metacommand = 0x03
length = 'varlen'
class InstrumentNameEvent(MetaEventWithText):
name = 'Instrument Name'
metacommand = 0x04
length = 'varlen'
class LyricsEvent(MetaEventWithText):
name = 'Lyrics'
metacommand = 0x05
length = 'varlen'
class MarkerEvent(MetaEventWithText):
name = 'Marker'
metacommand = 0x06
length = 'varlen'
class CuePointEvent(MetaEventWithText):
name = 'Cue Point'
metacommand = 0x07
length = 'varlen'
class ProgramNameEvent(MetaEventWithText):
name = 'Program Name'
metacommand = 0x08
length = 'varlen'
class UnknownMetaEvent(MetaEvent):
name = 'Unknown'
# This class variable must be overriden by code calling the constructor,
# which sets a local variable of the same name to shadow the class variable.
metacommand = None
def __init__(self, **kw):
super().__init__(**kw)
self.metacommand = kw['metacommand']
def copy(self, **kw):
kw['metacommand'] = self.metacommand
return Event.copy(kw)
class ChannelPrefixEvent(MetaEvent):
name = 'Channel Prefix'
metacommand = 0x20
length = 1
class PortEvent(MetaEvent):
name = 'MIDI Port/Cable'
metacommand = 0x21
class TrackLoopEvent(MetaEvent):
name = 'Track Loop'
metacommand = 0x2E
class EndOfTrackEvent(MetaEvent):
name = 'End of Track'
metacommand = 0x2F
class SetTempoEvent(MetaEvent):
# __slots__ = ['bpm', 'mpqn']
name = 'Set Tempo'
metacommand = 0x51
length = 3
def set_bpm(self, bpm):
self.mpqn = int(float(6e7) / bpm)
def get_bpm(self):
return float(6e7) / self.mpqn
bpm = property(get_bpm, set_bpm)
def get_mpqn(self):
assert(len(self.data) == 3)
vals = [self.data[x] << (16 - (8 * x)) for x in range(3)]
return sum(vals)
def set_mpqn(self, val):
self.data = [(val >> (16 - (8 * x)) & 0xFF) for x in range(3)]
mpqn = property(get_mpqn, set_mpqn)
class SmpteOffsetEvent(MetaEvent):
name = 'SMPTE Offset'
metacommand = 0x54
class TimeSignatureEvent(MetaEvent):
# __slots__ = ['numerator', 'denominator', 'metronome', 'thirtyseconds']
name = 'Time Signature'
metacommand = 0x58
length = 4
def get_numerator(self):
return self.data[0]
def set_numerator(self, val):
self.data[0] = val
numerator = property(get_numerator, set_numerator)
def get_denominator(self):
return 2 ** self.data[1]
def set_denominator(self, val):
self.data[1] = int(math.log(val, 2))
denominator = property(get_denominator, set_denominator)
def get_metronome(self):
return self.data[2]
def set_metronome(self, val):
self.data[2] = val
metronome = property(get_metronome, set_metronome)
def get_thirtyseconds(self):
return self.data[3]
def set_thirtyseconds(self, val):
self.data[3] = val
thirtyseconds = property(get_thirtyseconds, set_thirtyseconds)
class KeySignatureEvent(MetaEvent):
# __slots__ = ['alternatives', 'minor']
name = 'Key Signature'
metacommand = 0x59
length = 2
def get_alternatives(self):
d = self.data[0]
return d - 256 if d > 127 else d
def set_alternatives(self, val):
self.data[0] = 256 + val if val < 0 else val
alternatives = property(get_alternatives, set_alternatives)
def get_minor(self):
return self.data[1]
def set_minor(self, val):
self.data[1] = val
minor = property(get_minor, set_minor)
class SequencerSpecificEvent(MetaEvent):
name = 'Sequencer Specific'
metacommand = 0x7F