-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEventDispatcher.py
287 lines (195 loc) · 9.11 KB
/
EventDispatcher.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
# -*- coding: ISO-8859-1 -*-
# std library
from struct import unpack
# custom
from DataTypeConverters import readBew, readVar, varLen, toBytes
# uhh I don't really like this, but there are so many constants to
# import otherwise
from constants import *
class EventDispatcher:
def __init__(self, outstream):
"""
The event dispatcher generates events on the outstream.
"""
# internal values, don't mess with 'em directly
self.outstream = outstream
# public flags
# A note_on with a velocity of 0x00 is actually the same as a
# note_off with a velocity of 0x40. When
# "convert_zero_velocity" is set, the zero velocity note_on's
# automatically gets converted into note_off's. This is a less
# suprising behaviour for those that are not into the intimate
# details of the midi spec.
self.convert_zero_velocity = 1
# If dispatch_continuos_controllers is true, continuos
# controllers gets dispatched to their defined handlers. Else
# they just trigger the "continuous_controller" event handler.
self.dispatch_continuos_controllers = 1 # NOT IMPLEMENTED YET
# If dispatch_meta_events is true, meta events get's dispatched
# to their defined events. Else they all they trigger the
# "meta_event" handler.
self.dispatch_meta_events = 1
def header(self, format, nTracks, division):
"Triggers the header event"
self.outstream.header(format, nTracks, division)
def start_of_track(self, current_track):
"Triggers the start of track event"
# I do this twice so that users can overwrite the
# start_of_track event handler without worrying whether the
# track number is updated correctly.
self.outstream.set_current_track(current_track)
self.outstream.start_of_track(current_track)
def sysex_event(self, data):
"Dispatcher for sysex events"
self.outstream.sysex_event(data)
def eof(self):
"End of file!"
self.outstream.eof()
def update_time(self, new_time=0, relative=1):
"Updates relative/absolute time."
self.outstream.update_time(new_time, relative)
def reset_time(self):
"Updates relative/absolute time."
self.outstream.reset_time()
# Event dispatchers for similar types of events
def channel_messages(self, hi_nible, channel, data):
"Dispatches channel messages"
stream = self.outstream
data = toBytes(data)
if (NOTE_ON & 0xF0) == hi_nible:
note, velocity = data
# note_on with velocity 0x00 are same as note
# off with velocity 0x40 according to spec!
if velocity==0 and self.convert_zero_velocity:
stream.note_off(channel, note, 0x40)
else:
stream.note_on(channel, note, velocity)
elif (NOTE_OFF & 0xF0) == hi_nible:
note, velocity = data
stream.note_off(channel, note, velocity)
elif (AFTERTOUCH & 0xF0) == hi_nible:
note, velocity = data
stream.aftertouch(channel, note, velocity)
elif (CONTINUOUS_CONTROLLER & 0xF0) == hi_nible:
controller, value = data
# A lot of the cc's are defined, so we trigger those directly
if self.dispatch_continuos_controllers:
self.continuous_controllers(channel, controller, value)
else:
stream.continuous_controller(channel, controller, value)
elif (PATCH_CHANGE & 0xF0) == hi_nible:
program = data[0]
stream.patch_change(channel, program)
elif (CHANNEL_PRESSURE & 0xF0) == hi_nible:
pressure = data[0]
stream.channel_pressure(channel, pressure)
elif (PITCH_BEND & 0xF0) == hi_nible:
hibyte, lobyte = data
value = (hibyte<<7) + lobyte
stream.pitch_bend(channel, value)
else:
raise ValueError, 'Illegal channel message!'
def continuous_controllers(self, channel, controller, value):
"Dispatches channel messages"
stream = self.outstream
# I am not really shure if I ought to dispatch continuous controllers
# There's so many of them that it can clutter up the OutStream
# classes.
# So I just trigger the default event handler
stream.continuous_controller(channel, controller, value)
def system_commons(self, common_type, common_data):
"Dispatches system common messages"
stream = self.outstream
# MTC Midi time code Quarter value
if common_type == MTC:
data = readBew(common_data)
msg_type = (data & 0x07) >> 4
values = (data & 0x0F)
stream.midi_time_code(msg_type, values)
elif common_type == SONG_POSITION_POINTER:
hibyte, lobyte = toBytes(common_data)
value = (hibyte<<7) + lobyte
stream.song_position_pointer(value)
elif common_type == SONG_SELECT:
data = readBew(common_data)
stream.song_select(data)
elif common_type == TUNING_REQUEST:
# no data then
stream.tuning_request(time=None)
def meta_event(self, meta_type, data):
"Dispatches meta events"
stream = self.outstream
# SEQUENCE_NUMBER = 0x00 (00 02 ss ss (seq-number))
if meta_type == SEQUENCE_NUMBER:
number = readBew(data)
stream.sequence_number(number)
# TEXT = 0x01 (01 len text...)
elif meta_type == TEXT:
stream.text(data)
# COPYRIGHT = 0x02 (02 len text...)
elif meta_type == COPYRIGHT:
stream.copyright(data)
# SEQUENCE_NAME = 0x03 (03 len text...)
elif meta_type == SEQUENCE_NAME:
stream.sequence_name(data)
# INSTRUMENT_NAME = 0x04 (04 len text...)
elif meta_type == INSTRUMENT_NAME:
stream.instrument_name(data)
# LYRIC = 0x05 (05 len text...)
elif meta_type == LYRIC:
stream.lyric(data)
# MARKER = 0x06 (06 len text...)
elif meta_type == MARKER:
stream.marker(data)
# CUEPOINT = 0x07 (07 len text...)
elif meta_type == CUEPOINT:
stream.cuepoint(data)
# PROGRAM_NAME = 0x08 (05 len text...)
elif meta_type == PROGRAM_NAME:
stream.program_name(data)
# DEVICE_NAME = 0x09 (09 len text...)
elif meta_type == DEVICE_NAME:
stream.device_name(data)
# MIDI_CH_PREFIX = 0x20 (20 01 channel)
elif meta_type == MIDI_CH_PREFIX:
channel = readBew(data)
stream.midi_ch_prefix(channel)
# MIDI_PORT = 0x21 (21 01 port (legacy stuff))
elif meta_type == MIDI_PORT:
port = readBew(data)
stream.midi_port(port)
# END_OFF_TRACK = 0x2F (2F 00)
elif meta_type == END_OF_TRACK:
stream.end_of_track()
# TEMPO = 0x51 (51 03 tt tt tt (tempo in us/quarternote))
elif meta_type == TEMPO:
b1, b2, b3 = toBytes(data)
# uses 3 bytes to represent time between quarter
# notes in microseconds
stream.tempo((b1<<16) + (b2<<8) + b3)
# SMTP_OFFSET = 0x54 (54 05 hh mm ss ff xx)
elif meta_type == SMTP_OFFSET:
hour, minute, second, frame, framePart = toBytes(data)
stream.smtp_offset(
hour, minute, second, frame, framePart)
# TIME_SIGNATURE = 0x58 (58 04 nn dd cc bb)
elif meta_type == TIME_SIGNATURE:
nn, dd, cc, bb = toBytes(data)
stream.time_signature(nn, dd, cc, bb)
# KEY_SIGNATURE = 0x59 (59 02 sf mi)
elif meta_type == KEY_SIGNATURE:
sf, mi = toBytes(data)
stream.key_signature(sf, mi)
# SPECIFIC = 0x7F (Sequencer specific event)
elif meta_type == SPECIFIC:
meta_data = toBytes(data)
stream.sequencer_specific(meta_data)
# Handles any undefined meta events
else: # undefined meta type
meta_data = toBytes(data)
stream.meta_event(meta_type, meta_data)
if __name__ == '__main__':
from MidiToText import MidiToText
outstream = MidiToText()
dispatcher = EventDispatcher(outstream)
dispatcher.channel_messages(NOTE_ON, 0x00, '\x40\x40')