]>
Commit | Line | Data |
---|---|---|
17f5df4f UH |
1 | ## |
2 | ## This file is part of the libsigrokdecode project. | |
3 | ## | |
4 | ## Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de> | |
5 | ## | |
6 | ## This program is free software; you can redistribute it and/or modify | |
7 | ## it under the terms of the GNU General Public License as published by | |
8 | ## the Free Software Foundation; either version 2 of the License, or | |
9 | ## (at your option) any later version. | |
10 | ## | |
11 | ## This program is distributed in the hope that it will be useful, | |
12 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | ## GNU General Public License for more details. | |
15 | ## | |
16 | ## You should have received a copy of the GNU General Public License | |
17 | ## along with this program; if not, write to the Free Software | |
18 | ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | ## | |
20 | ||
17f5df4f UH |
21 | import sigrokdecode as srd |
22 | from .lists import * | |
23 | ||
24 | RX = 0 | |
25 | TX = 1 | |
26 | ||
27 | class Decoder(srd.Decoder): | |
12851357 | 28 | api_version = 2 |
17f5df4f UH |
29 | id = 'midi' |
30 | name = 'MIDI' | |
31 | longname = 'Musical Instrument Digital Interface' | |
32 | desc = 'Musical Instrument Digital Interface (MIDI) protocol.' | |
33 | license = 'gplv2+' | |
34 | inputs = ['uart'] | |
35 | outputs = ['midi'] | |
da9bcbd9 BV |
36 | annotations = ( |
37 | ('text-verbose', 'Human-readable text (verbose)'), | |
6aa03e3a C |
38 | ('text-sysreal-verbose', 'Human-readable SysReal text (verbose)'), |
39 | ('text-error', 'Human-readable Error text'), | |
40 | ) | |
41 | annotation_rows = ( | |
42 | ('normal', 'Normal', (0, 2)), | |
43 | ('sys-real', 'SysReal', (1,)), | |
da9bcbd9 | 44 | ) |
17f5df4f | 45 | |
92b7b49f | 46 | def __init__(self): |
17f5df4f | 47 | self.state = 'IDLE' |
6aa03e3a C |
48 | self.status_byte = 0 |
49 | self.explicit_status_byte = False | |
50 | self.cmd = [] | |
17f5df4f UH |
51 | self.ss = None |
52 | self.es = None | |
53 | self.ss_block = None | |
54 | self.es_block = None | |
55 | ||
8915b346 | 56 | def start(self): |
be465111 | 57 | self.out_ann = self.register(srd.OUTPUT_ANN) |
17f5df4f | 58 | |
17f5df4f UH |
59 | def putx(self, data): |
60 | self.put(self.ss_block, self.es_block, self.out_ann, data) | |
61 | ||
b0fc934a CD |
62 | def get_note_name(self, channel, note): |
63 | if channel != 10: | |
64 | return chromatic_notes[note] | |
65 | else: | |
66 | return 'assuming ' + percussion_notes.get(note, 'undefined') | |
67 | ||
6aa03e3a C |
68 | def check_for_garbage_flush(self, is_flushed): |
69 | if is_flushed == True: | |
70 | if self.explicit_status_byte == True: | |
71 | self.cmd.insert(0, self.status_byte) | |
72 | self.handle_garbage_msg(None) | |
73 | ||
74 | def soft_clear_status_byte(self): | |
75 | self.explicit_status_byte = False | |
76 | ||
77 | def hard_clear_status_byte(self): | |
78 | self.status_byte = 0 | |
79 | self.explicit_status_byte = False | |
80 | ||
81 | def set_status_byte(self, newbyte): | |
82 | self.status_byte = newbyte | |
83 | self.explicit_status_byte = True | |
84 | ||
85 | def handle_channel_msg_0x80(self, is_flushed): | |
17f5df4f UH |
86 | # Note off: 8n kk vv |
87 | # n = channel, kk = note, vv = velocity | |
88 | c = self.cmd | |
6aa03e3a C |
89 | if len(c) < 2: |
90 | self.check_for_garbage_flush(is_flushed) | |
17f5df4f UH |
91 | return |
92 | self.es_block = self.es | |
6aa03e3a C |
93 | msg, chan = self.status_byte & 0xf0, (self.status_byte & 0x0f) + 1 |
94 | note, velocity = c[0], c[1] | |
b0fc934a CD |
95 | note_name = self.get_note_name(chan, note) |
96 | self.putx([0, ['Channel %d: %s (note = %d \'%s\', velocity = %d)' % \ | |
9cc1ddf9 C |
97 | (chan, status_bytes[msg][0], note, note_name, velocity), |
98 | 'ch %d: %s %d, velocity = %d' % \ | |
99 | (chan, status_bytes[msg][1], note, velocity), | |
100 | '%d: %s %d, vel %d' % \ | |
101 | (chan, status_bytes[msg][2], note, velocity)]]) | |
17f5df4f | 102 | self.cmd, self.state = [], 'IDLE' |
6aa03e3a | 103 | self.soft_clear_status_byte() |
17f5df4f | 104 | |
6aa03e3a | 105 | def handle_channel_msg_0x90(self, is_flushed): |
17f5df4f UH |
106 | # Note on: 9n kk vv |
107 | # n = channel, kk = note, vv = velocity | |
108 | # If velocity == 0 that actually means 'note off', though. | |
109 | c = self.cmd | |
6aa03e3a C |
110 | if len(c) < 2: |
111 | self.check_for_garbage_flush(is_flushed) | |
17f5df4f | 112 | return |
ee7de90f | 113 | self.es_block = self.es |
6aa03e3a C |
114 | msg, chan = self.status_byte & 0xf0, (self.status_byte & 0x0f) + 1 |
115 | note, velocity = c[0], c[1] | |
9cc1ddf9 | 116 | s = status_bytes[0x80] if (velocity == 0) else status_bytes[msg] |
b0fc934a CD |
117 | note_name = self.get_note_name(chan, note) |
118 | self.putx([0, ['Channel %d: %s (note = %d \'%s\', velocity = %d)' % \ | |
9cc1ddf9 C |
119 | (chan, s[0], note, note_name, velocity), |
120 | 'ch %d: %s %d, velocity = %d' % \ | |
121 | (chan, s[1], note, velocity), | |
122 | '%d: %s %d, vel %d' % \ | |
123 | (chan, s[2], note, velocity)]]) | |
17f5df4f | 124 | self.cmd, self.state = [], 'IDLE' |
6aa03e3a | 125 | self.soft_clear_status_byte() |
17f5df4f | 126 | |
6aa03e3a | 127 | def handle_channel_msg_0xa0(self, is_flushed): |
17f5df4f UH |
128 | # Polyphonic key pressure / aftertouch: An kk vv |
129 | # n = channel, kk = polyphonic key pressure, vv = pressure value | |
b0fc934a | 130 | c = self.cmd |
6aa03e3a C |
131 | if len(c) < 2: |
132 | self.check_for_garbage_flush(is_flushed) | |
b0fc934a CD |
133 | return |
134 | self.es_block = self.es | |
6aa03e3a C |
135 | msg, chan = self.status_byte & 0xf0, (self.status_byte & 0x0f) + 1 |
136 | note, pressure = c[0], c[1] | |
b0fc934a | 137 | note_name = self.get_note_name(chan, note) |
9cc1ddf9 C |
138 | self.putx([0, ['Channel %d: %s of %d for note = %d \'%s\'' % \ |
139 | (chan, status_bytes[msg][0], pressure, note, note_name), | |
140 | 'ch %d: %s %d for note %d' % \ | |
141 | (chan, status_bytes[msg][1], pressure, note), | |
142 | '%d: %s %d, N %d' % \ | |
143 | (chan, status_bytes[msg][2], pressure, note)]]) | |
b0fc934a | 144 | self.cmd, self.state = [], 'IDLE' |
6aa03e3a | 145 | self.soft_clear_status_byte() |
17f5df4f UH |
146 | |
147 | def handle_controller_0x44(self): | |
148 | # Legato footswitch: Bn 44 vv | |
149 | # n = channel, vv = value (<= 0x3f: normal, > 0x3f: legato) | |
9cc1ddf9 | 150 | c = self.cmd |
6aa03e3a C |
151 | msg, chan = self.status_byte & 0xf0, (self.status_byte & 0x0f) + 1 |
152 | vv = c[1] | |
9cc1ddf9 C |
153 | t = ('normal', 'no') if vv <= 0x3f else ('legato', 'yes') |
154 | self.putx([0, ['Channel %d: %s \'%s\' = %s' % \ | |
155 | (chan, status_bytes[msg][0], | |
156 | control_functions[0x44][0], t[0]), | |
157 | 'ch %d: %s \'%s\' = %s' % \ | |
158 | (chan, status_bytes[msg][1], | |
159 | control_functions[0x44][1], t[0]), | |
160 | '%d: %s \'%s\' = %s' % \ | |
161 | (chan, status_bytes[msg][2], | |
162 | control_functions[0x44][2], t[1])]]) | |
17f5df4f UH |
163 | |
164 | def handle_controller_0x54(self): | |
165 | # Portamento control (PTC): Bn 54 kk | |
166 | # n = channel, kk = source note for pitch reference | |
9cc1ddf9 | 167 | c = self.cmd |
6aa03e3a C |
168 | msg, chan = self.status_byte & 0xf0, (self.status_byte & 0x0f) + 1 |
169 | kk = c[1] | |
b0fc934a | 170 | kk_name = self.get_note_name(chan, kk) |
9cc1ddf9 C |
171 | self.putx([0, ['Channel %d: %s \'%s\' (source note = %d / %s)' % \ |
172 | (chan, status_bytes[msg][0], | |
173 | control_functions[0x54][0], kk, kk_name), | |
174 | 'ch %d: %s \'%s\' (source note = %d)' % \ | |
175 | (chan, status_bytes[msg][1], | |
176 | control_functions[0x54][1], kk), | |
177 | '%d: %s \'%s\' (src N %d)' % \ | |
178 | (chan, status_bytes[msg][2], | |
179 | control_functions[0x54][2], kk)]]) | |
17f5df4f UH |
180 | |
181 | def handle_controller_generic(self): | |
182 | c = self.cmd | |
6aa03e3a C |
183 | msg, chan = self.status_byte & 0xf0, (self.status_byte & 0x0f) + 1 |
184 | fn, param = c[0], c[1] | |
b0fc934a CD |
185 | default_name = 'undefined' |
186 | ctrl_fn = control_functions.get(fn, default_name) | |
187 | if ctrl_fn == default_name: | |
9cc1ddf9 C |
188 | ctrl_fn = ('undefined 0x%02x' % fn, 'undef 0x%02x' % fn, '0x%02x' % fn) |
189 | self.putx([0, ['Channel %d: %s \'%s\' (param = 0x%02x)' % \ | |
190 | (chan, status_bytes[msg][0], ctrl_fn[0], param), | |
191 | 'ch %d: %s \'%s\' (param = 0x%02x)' % \ | |
192 | (chan, status_bytes[msg][1], ctrl_fn[1], param), | |
193 | '%d: %s \'%s\' is 0x%02x' % \ | |
194 | (chan, status_bytes[msg][2], ctrl_fn[2], param)]]) | |
17f5df4f | 195 | |
b0fc934a CD |
196 | def handle_channel_mode(self): |
197 | # Channel Mode: Bn mm vv | |
198 | # n = channel, mm = mode number (120 - 127), vv = value | |
199 | c = self.cmd | |
6aa03e3a C |
200 | msg, chan = self.status_byte & 0xf0, (self.status_byte & 0x0f) + 1 |
201 | mm, vv = c[0], c[1] | |
9cc1ddf9 | 202 | mode_fn = control_functions.get(mm, ('undefined', 'undef', 'undef')) |
b0fc934a | 203 | # Decode the value based on the mode number. |
9cc1ddf9 | 204 | vv_string = ('', '') |
b0fc934a CD |
205 | if mm == 122: # mode = local control? |
206 | if vv == 0: | |
9cc1ddf9 | 207 | vv_string = ('off', 'off') |
b0fc934a | 208 | elif vv == 127: # mode = poly mode on? |
9cc1ddf9 | 209 | vv_string = ('on', 'on') |
b0fc934a | 210 | else: |
9cc1ddf9 C |
211 | vv_string = ('(non-standard param value of 0x%02x)' % vv, |
212 | '0x%02x' % vv) | |
b0fc934a CD |
213 | elif mm == 126: # mode = mono mode on? |
214 | if vv != 0: | |
9cc1ddf9 | 215 | vv_string = ('(%d channels)' % vv, '(%d ch)' % vv) |
b0fc934a | 216 | else: |
9cc1ddf9 C |
217 | vv_string = ('(channels \'basic\' through 16)', |
218 | '(ch \'basic\' thru 16)') | |
b0fc934a | 219 | elif vv != 0: # All other channel mode messages expect vv == 0. |
9cc1ddf9 C |
220 | vv_string = ('(non-standard param value of 0x%02x)' % vv, |
221 | '0x%02x' % vv) | |
222 | self.putx([0, ['Channel %d: %s \'%s\' %s' % \ | |
223 | (chan, status_bytes[msg][0], mode_fn[0], vv_string[0]), | |
224 | 'ch %d: %s \'%s\' %s' % \ | |
225 | (chan, status_bytes[msg][1], mode_fn[1], vv_string[1]), | |
226 | '%d: %s \'%s\' %s' % \ | |
227 | (chan, status_bytes[msg][2], mode_fn[2], vv_string[1])]]) | |
b0fc934a | 228 | self.cmd, self.state = [], 'IDLE' |
6aa03e3a | 229 | self.soft_clear_status_byte() |
b0fc934a | 230 | |
6aa03e3a | 231 | def handle_channel_msg_0xb0(self, is_flushed): |
17f5df4f UH |
232 | # Control change (or channel mode messages): Bn cc vv |
233 | # n = channel, cc = control number (0 - 119), vv = control value | |
234 | c = self.cmd | |
6aa03e3a C |
235 | if len(c) < 2: |
236 | self.check_for_garbage_flush(is_flushed) | |
17f5df4f UH |
237 | return |
238 | self.es_block = self.es | |
6aa03e3a | 239 | if c[0] in range(0x78, 0x7f + 1): |
b0fc934a CD |
240 | self.handle_channel_mode() |
241 | return | |
6aa03e3a | 242 | handle_ctrl = getattr(self, 'handle_controller_0x%02x' % c[0], |
17f5df4f UH |
243 | self.handle_controller_generic) |
244 | handle_ctrl() | |
245 | self.cmd, self.state = [], 'IDLE' | |
6aa03e3a | 246 | self.soft_clear_status_byte() |
17f5df4f | 247 | |
6aa03e3a | 248 | def handle_channel_msg_0xc0(self, is_flushed): |
17f5df4f UH |
249 | # Program change: Cn pp |
250 | # n = channel, pp = program number (0 - 127) | |
b0fc934a | 251 | c = self.cmd |
6aa03e3a C |
252 | if len(c) < 1: |
253 | self.check_for_garbage_flush(is_flushed) | |
b0fc934a CD |
254 | return |
255 | self.es_block = self.es | |
6aa03e3a C |
256 | msg, chan = self.status_byte & 0xf0, (self.status_byte & 0x0f) + 1 |
257 | pp = self.cmd[0] + 1 | |
b0fc934a CD |
258 | change_type = 'instrument' |
259 | name = '' | |
260 | if chan != 10: # channel != percussion | |
261 | name = gm_instruments.get(pp, 'undefined') | |
262 | else: | |
263 | change_type = 'drum kit' | |
264 | name = drum_kit.get(pp, 'undefined') | |
265 | self.putx([0, ['Channel %d: %s to %s %d (assuming %s)' % \ | |
9cc1ddf9 C |
266 | (chan, status_bytes[msg][0], change_type, pp, name), |
267 | 'ch %d: %s to %s %d' % \ | |
268 | (chan, status_bytes[msg][1], change_type, pp), | |
269 | '%d: %s %d' % \ | |
270 | (chan, status_bytes[msg][2], pp)]]) | |
b0fc934a | 271 | self.cmd, self.state = [], 'IDLE' |
6aa03e3a | 272 | self.soft_clear_status_byte() |
17f5df4f | 273 | |
6aa03e3a | 274 | def handle_channel_msg_0xd0(self, is_flushed): |
17f5df4f UH |
275 | # Channel pressure / aftertouch: Dn vv |
276 | # n = channel, vv = pressure value | |
b0fc934a | 277 | c = self.cmd |
6aa03e3a C |
278 | if len(c) < 1: |
279 | self.check_for_garbage_flush(is_flushed) | |
b0fc934a CD |
280 | return |
281 | self.es_block = self.es | |
6aa03e3a C |
282 | msg, chan = self.status_byte & 0xf0, (self.status_byte & 0x0f) + 1 |
283 | vv = self.cmd[0] | |
9cc1ddf9 C |
284 | self.putx([0, ['Channel %d: %s %d' % (chan, status_bytes[msg][0], vv), |
285 | 'ch %d: %s %d' % (chan, status_bytes[msg][1], vv), | |
286 | '%d: %s %d' % (chan, status_bytes[msg][2], vv)]]) | |
b0fc934a | 287 | self.cmd, self.state = [], 'IDLE' |
6aa03e3a | 288 | self.soft_clear_status_byte() |
17f5df4f | 289 | |
6aa03e3a | 290 | def handle_channel_msg_0xe0(self, is_flushed): |
17f5df4f UH |
291 | # Pitch bend change: En ll mm |
292 | # n = channel, ll = pitch bend change LSB, mm = pitch bend change MSB | |
b0fc934a | 293 | c = self.cmd |
6aa03e3a C |
294 | if len(c) < 2: |
295 | self.check_for_garbage_flush(is_flushed) | |
b0fc934a CD |
296 | return |
297 | self.es_block = self.es | |
6aa03e3a C |
298 | msg, chan = self.status_byte & 0xf0, (self.status_byte & 0x0f) + 1 |
299 | ll, mm = self.cmd[0], self.cmd[1] | |
b0fc934a CD |
300 | decimal = (mm << 7) + ll |
301 | self.putx([0, ['Channel %d: %s 0x%02x 0x%02x (%d)' % \ | |
9cc1ddf9 C |
302 | (chan, status_bytes[msg][0], ll, mm, decimal), |
303 | 'ch %d: %s 0x%02x 0x%02x (%d)' % \ | |
304 | (chan, status_bytes[msg][1], ll, mm, decimal), | |
305 | '%d: %s (%d)' % \ | |
306 | (chan, status_bytes[msg][2], decimal)]]) | |
b0fc934a | 307 | self.cmd, self.state = [], 'IDLE' |
6aa03e3a | 308 | self.soft_clear_status_byte() |
17f5df4f | 309 | |
6aa03e3a | 310 | def handle_channel_msg_generic(self, is_flushed): |
b0fc934a CD |
311 | # TODO: It should not be possible to hit this code. |
312 | # It currently can not be unit tested. | |
6aa03e3a | 313 | msg_type = self.status_byte & 0xf0 |
b0fc934a | 314 | self.es_block = self.es |
6aa03e3a | 315 | self.putx([2, ['Unknown channel message type: 0x%02x' % msg_type]]) |
b0fc934a | 316 | self.cmd, self.state = [], 'IDLE' |
6aa03e3a | 317 | self.soft_clear_status_byte() |
17f5df4f UH |
318 | |
319 | def handle_channel_msg(self, newbyte): | |
6aa03e3a C |
320 | if newbyte != None: |
321 | if newbyte >= 0x80: | |
322 | self.set_status_byte(newbyte) | |
323 | else: | |
324 | self.cmd.append(newbyte) | |
325 | msg_type = self.status_byte & 0xf0 | |
17f5df4f UH |
326 | handle_msg = getattr(self, 'handle_channel_msg_0x%02x' % msg_type, |
327 | self.handle_channel_msg_generic) | |
6aa03e3a | 328 | handle_msg(newbyte == None) |
17f5df4f UH |
329 | |
330 | def handle_sysex_msg(self, newbyte): | |
b0fc934a | 331 | # SysEx message: 1 status byte, 1-3 manuf. bytes, x data bytes, EOX byte |
6aa03e3a C |
332 | # |
333 | # SysEx message are variable length, can be terminated by EOX byte or | |
334 | # by any non-SysReal status byte, and it clears self.status_byte. | |
335 | # Note: all System message code doesn't utilize self.status_byte | |
336 | self.hard_clear_status_byte() | |
337 | if newbyte != 0xf7 and newbyte != None: # EOX | |
338 | self.cmd.append(newbyte) | |
17f5df4f UH |
339 | return |
340 | self.es_block = self.es | |
b0fc934a CD |
341 | # Note: Unlike other methods, this code pops bytes out of self.cmd |
342 | # to isolate the data. | |
6aa03e3a | 343 | msg = self.cmd.pop(0) |
b0fc934a | 344 | if len(self.cmd) < 1: |
6aa03e3a | 345 | self.putx([2, ['%s: truncated manufacturer code (<1 bytes)' % \ |
9cc1ddf9 C |
346 | status_bytes[msg][0], |
347 | '%s: truncated manufacturer (<1 bytes)' % \ | |
348 | status_bytes[msg][1], | |
349 | '%s: trunc. manu.' % status_bytes[msg][2]]]) | |
b0fc934a CD |
350 | self.cmd, self.state = [], 'IDLE' |
351 | return | |
352 | # Extract the manufacturer name (or SysEx realtime or non-realtime). | |
353 | m1 = self.cmd.pop(0) | |
354 | manu = (m1,) | |
355 | if m1 == 0x00: # If byte == 0, then 2 more manufacturer bytes follow. | |
356 | if len(self.cmd) < 2: | |
6aa03e3a | 357 | self.putx([2, ['%s: truncated manufacturer code (<3 bytes)' % \ |
9cc1ddf9 C |
358 | status_bytes[msg][0], |
359 | '%s: truncated manufacturer (<3 bytes)' % \ | |
360 | status_bytes[msg][1], | |
361 | '%s: trunc. manu.' % status_bytes[msg][2]]]) | |
b0fc934a CD |
362 | self.cmd, self.state = [], 'IDLE' |
363 | return | |
364 | manu = (m1, self.cmd.pop(0), self.cmd.pop(0)) | |
365 | default_name = 'undefined' | |
366 | manu_name = sysex_manufacturer_ids.get(manu, default_name) | |
367 | if manu_name == default_name: | |
368 | if len(manu) == 3: | |
9cc1ddf9 C |
369 | manu_name = ('%s (0x%02x 0x%02x 0x%02x)' % \ |
370 | (default_name, manu[0], manu[1], manu[2]), | |
371 | default_name) | |
b0fc934a | 372 | else: |
9cc1ddf9 C |
373 | manu_name = ('%s (0x%02x)' % (default_name, manu[0]), |
374 | default_name) | |
375 | else: | |
376 | manu_name = (manu_name, manu_name) | |
377 | # Extract the payload, display in 1 of 2 formats | |
b0fc934a | 378 | # TODO: Write methods to decode SysEx realtime & non-realtime payloads. |
9cc1ddf9 C |
379 | payload0 = '' |
380 | payload1 = '' | |
b0fc934a | 381 | while len(self.cmd) > 0: |
9cc1ddf9 C |
382 | byte = self.cmd.pop(0) |
383 | payload0 += '0x%02x ' % (byte) | |
384 | payload1 += '%02x ' % (byte) | |
385 | if payload0 == '': | |
386 | payload0 = '<empty>' | |
387 | payload1 = '<>' | |
388 | payload = (payload0, payload1) | |
389 | self.putx([0, ['%s: for \'%s\' with payload %s' % \ | |
390 | (status_bytes[msg][0], manu_name[0], payload[0]), | |
391 | '%s: \'%s\', payload %s' % \ | |
392 | (status_bytes[msg][1], manu_name[1], payload[1]), | |
393 | '%s: \'%s\', payload %s' % \ | |
394 | (status_bytes[msg][2], manu_name[1], payload[1])]]) | |
b0fc934a CD |
395 | self.cmd, self.state = [], 'IDLE' |
396 | ||
397 | def handle_syscommon_midi_time_code_quarter_frame_msg(self, newbyte): | |
398 | # MIDI time code quarter frame: F1 nd | |
399 | # n = message type | |
400 | # d = values | |
6aa03e3a C |
401 | # |
402 | # Note: all System message code don't utilize self.status_byte, | |
403 | # and System Exclusive and System Common clear it. | |
b0fc934a CD |
404 | c = self.cmd |
405 | if len(c) < 2: | |
6aa03e3a C |
406 | if newbyte == None: |
407 | self.handle_garbage_msg(None) | |
b0fc934a | 408 | return |
6aa03e3a C |
409 | msg = c[0] |
410 | nn, dd = (c[1] & 0x70) >> 4, c[1] & 0x0f | |
9cc1ddf9 | 411 | group = ('System Common', 'SysCom', 'SC') |
b0fc934a CD |
412 | self.es_block = self.es |
413 | if nn != 7: # If message type does not contain SMPTE type. | |
414 | self.putx([0, ['%s: %s of %s, value 0x%01x' % \ | |
9cc1ddf9 C |
415 | (group[0], status_bytes[msg][0], |
416 | quarter_frame_type[nn][0], dd), | |
417 | '%s: %s of %s, value 0x%01x' % \ | |
418 | (group[1], status_bytes[msg][1], | |
419 | quarter_frame_type[nn][1], dd), | |
420 | '%s: %s of %s, value 0x%01x' % \ | |
421 | (group[2], status_bytes[msg][2], | |
422 | quarter_frame_type[nn][1], dd)]]) | |
b0fc934a CD |
423 | self.cmd, self.state = [], 'IDLE' |
424 | return | |
425 | tt = (dd & 0x6) >> 1 | |
426 | self.putx([0, ['%s: %s of %s, value 0x%01x for %s' % \ | |
9cc1ddf9 C |
427 | (group[0], status_bytes[msg][0], \ |
428 | quarter_frame_type[nn][0], dd, smpte_type[tt]), | |
429 | '%s: %s of %s, value 0x%01x for %s' % \ | |
430 | (group[1], status_bytes[msg][1], \ | |
431 | quarter_frame_type[nn][1], dd, smpte_type[tt]), | |
432 | '%s: %s of %s, value 0x%01x for %s' % \ | |
433 | (group[2], status_bytes[msg][2], \ | |
434 | quarter_frame_type[nn][1], dd, smpte_type[tt])]]) | |
17f5df4f UH |
435 | self.cmd, self.state = [], 'IDLE' |
436 | ||
437 | def handle_syscommon_msg(self, newbyte): | |
b0fc934a CD |
438 | # System common messages |
439 | # | |
440 | # There are 5 simple formats (which are directly handled here) and | |
441 | # 1 complex one called MIDI time code quarter frame. | |
442 | # | |
443 | # Note: While the MIDI lists 0xf7 as a "system common" message, it | |
444 | # is actually only used with SysEx messages so it is processed there. | |
6aa03e3a C |
445 | # Note 2: all System message code doesn't utilize self.status_byte |
446 | self.hard_clear_status_byte() | |
447 | if newbyte != None: | |
448 | self.cmd.append(newbyte) | |
b0fc934a | 449 | c = self.cmd |
6aa03e3a | 450 | msg = c[0] |
9cc1ddf9 | 451 | group = ('System Common', 'SysCom', 'SC') |
b0fc934a CD |
452 | if msg == 0xf1: |
453 | # MIDI time code quarter frame | |
454 | self.handle_syscommon_midi_time_code_quarter_frame_msg(newbyte) | |
455 | return | |
456 | elif msg == 0xf2: | |
457 | # Song position pointer: F2 ll mm | |
458 | # ll = LSB position, mm = MSB position | |
459 | if len(c) < 3: | |
6aa03e3a C |
460 | if newbyte == None: |
461 | self.handle_garbage_msg(None) | |
b0fc934a | 462 | return |
6aa03e3a | 463 | ll, mm = c[1], c[2] |
b0fc934a CD |
464 | decimal = (mm << 7) + ll |
465 | self.es_block = self.es | |
466 | self.putx([0, ['%s: %s 0x%02x 0x%02x (%d)' % \ | |
9cc1ddf9 C |
467 | (group[0], status_bytes[msg][0], ll, mm, decimal), |
468 | '%s: %s 0x%02x 0x%02x (%d)' % \ | |
469 | (group[1], status_bytes[msg][1], ll, mm, decimal), | |
470 | '%s: %s (%d)' % \ | |
471 | (group[2], status_bytes[msg][2], decimal)]]) | |
b0fc934a CD |
472 | elif msg == 0xf3: |
473 | # Song select: F3 ss | |
474 | # ss = song selection number | |
475 | if len(c) < 2: | |
6aa03e3a C |
476 | if newbyte == None: |
477 | self.handle_garbage_msg(None) | |
b0fc934a | 478 | return |
6aa03e3a | 479 | ss = c[1] |
b0fc934a | 480 | self.es_block = self.es |
9cc1ddf9 C |
481 | self.putx([0, ['%s: %s number %d' % \ |
482 | (group[0], status_bytes[msg][0], ss), | |
483 | '%s: %s number %d' % \ | |
484 | (group[1], status_bytes[msg][1], ss), | |
485 | '%s: %s # %d' % \ | |
486 | (group[2], status_bytes[msg][2], ss)]]) | |
b0fc934a CD |
487 | elif msg == 0xf4 or msg == 0xf5 or msg == 0xf6: |
488 | # Undefined 0xf4, Undefined 0xf5, and Tune Request (respectively). | |
489 | # All are only 1 byte long with no data bytes. | |
490 | self.es_block = self.es | |
9cc1ddf9 C |
491 | self.putx([0, ['%s: %s' % (group[0], status_bytes[msg][0]), |
492 | '%s: %s' % (group[1], status_bytes[msg][1]), | |
493 | '%s: %s' % (group[2], status_bytes[msg][2])]]) | |
b0fc934a | 494 | self.cmd, self.state = [], 'IDLE' |
17f5df4f UH |
495 | |
496 | def handle_sysrealtime_msg(self, newbyte): | |
497 | # System realtime message: 0b11111ttt (t = message type) | |
6aa03e3a C |
498 | # |
499 | # Important: these messages are handled different from all others | |
500 | # because they are allowed to temporarily interrupt other messages. | |
501 | # The interrupted messages resume after the realtime message is done. | |
502 | # Thus, they mostly leave 'self' the way it was found. | |
503 | # Note: all System message code doesn't utilize self.status_byte | |
504 | old_ss_block = self.ss_block | |
505 | old_es_block = self.es_block | |
506 | self.ss_block = self.ss | |
ee7de90f | 507 | self.es_block = self.es |
9cc1ddf9 | 508 | group = ('System Realtime', 'SysReal', 'SR') |
6aa03e3a | 509 | self.putx([1, ['%s: %s' % (group[0], status_bytes[newbyte][0]), |
9cc1ddf9 C |
510 | '%s: %s' % (group[1], status_bytes[newbyte][1]), |
511 | '%s: %s' % (group[2], status_bytes[newbyte][2])]]) | |
6aa03e3a C |
512 | self.ss_block = old_ss_block |
513 | self.es_block = old_es_block | |
514 | # Deliberately not resetting self.cmd or self.state | |
515 | ||
516 | def handle_garbage_msg(self, newbyte): | |
517 | # Handles messages that are either not handled or are corrupt | |
518 | self.es_block = self.es | |
519 | if newbyte != None: | |
520 | self.cmd.append(newbyte) | |
521 | return | |
522 | payload = '<empty>' | |
523 | max_bytes = 16 # Put a limit on the length on the hex dump | |
524 | for index in range( 0, len(self.cmd) ): | |
525 | if index == max_bytes: | |
526 | payload = payload + ' ...' | |
527 | break | |
528 | if index == 0: | |
529 | payload = '0x%02x' % self.cmd[index] | |
530 | else: | |
531 | payload = payload + ' 0x%02x' % self.cmd[index] | |
532 | self.putx([2, ['UNHANDLED DATA: %s' % payload, | |
533 | 'UNHANDLED', | |
534 | '???', | |
535 | '?']]) | |
17f5df4f | 536 | self.cmd, self.state = [], 'IDLE' |
6aa03e3a C |
537 | self.hard_clear_status_byte() |
538 | ||
539 | def handle_state(self, state, newbyte): | |
540 | # 'newbyte' can either be: | |
541 | # 1. Value between 0x00-0xff, deal with the byte normally | |
542 | # 2. Value of 'None' which means flush any buffered data. | |
543 | if state == 'HANDLE CHANNEL MSG': | |
544 | self.handle_channel_msg(newbyte) | |
545 | elif state == 'HANDLE SYSEX MSG': | |
546 | self.handle_sysex_msg(newbyte) | |
547 | elif state == 'HANDLE SYSCOMMON MSG': | |
548 | self.handle_syscommon_msg(newbyte) | |
549 | elif state == 'HANDLE SYSREALTIME MSG': | |
550 | self.handle_sysrealtime_msg(newbyte) | |
551 | elif state == 'BUFFER GARBAGE MSG': | |
552 | self.handle_garbage_msg(newbyte) | |
553 | ||
554 | def get_next_state(self, newbyte): | |
555 | # 'newbyte' must be a valid byte between 0x00 and 0xff | |
556 | # | |
557 | # Try to determine the state based off of 'newbyte' parameter | |
558 | # ... if it is >= 0x80. | |
559 | if newbyte in range(0x80, 0xef + 1): | |
560 | return 'HANDLE CHANNEL MSG' | |
561 | if newbyte == 0xf0: | |
562 | return 'HANDLE SYSEX MSG' | |
563 | if newbyte in range(0xf1, 0xf7): | |
564 | return'HANDLE SYSCOMMON MSG' | |
565 | if newbyte in range(0xf8, 0xff + 1): | |
566 | return 'HANDLE SYSREALTIME MSG' | |
567 | # Passing 0xf7 is an error; messages don't start with 0xf7 | |
568 | if newbyte == 0xf7: | |
569 | return 'BUFFER GARBAGE MSG' | |
570 | # Next, base the state off of self.status_byte | |
571 | if self.status_byte < 0x80: | |
572 | return 'BUFFER GARBAGE MSG' | |
573 | return self.get_next_state(self.status_byte) | |
17f5df4f UH |
574 | |
575 | def decode(self, ss, es, data): | |
576 | ptype, rxtx, pdata = data | |
6aa03e3a | 577 | state = 'IDLE' |
17f5df4f UH |
578 | |
579 | # For now, ignore all UART packets except the actual data packets. | |
580 | if ptype != 'DATA': | |
581 | return | |
582 | ||
7cf698c5 UH |
583 | # We're only interested in the byte value (not individual bits). |
584 | pdata = pdata[0] | |
585 | ||
17f5df4f UH |
586 | # Short MIDI overview: |
587 | # - Status bytes are 0x80-0xff, data bytes are 0x00-0x7f. | |
588 | # - Most messages: 1 status byte, 1-2 data bytes. | |
589 | # - Real-time system messages: always 1 byte. | |
590 | # - SysEx messages: 1 status byte, n data bytes, EOX byte. | |
591 | ||
6aa03e3a C |
592 | # Aspects of the MIDI protocol that complicate decoding: |
593 | # - MIDI System Realtime messages can briefly interrupt other | |
594 | # message already in progress. | |
595 | # - "Running Status" allows for omitting the status byte in most | |
596 | # scenarios if sequential messages have the same status byte. | |
597 | # - System Exclusive (SysEx) messages can be terminated by ANY | |
598 | # status byte (not limited to EOX byte). | |
599 | ||
17f5df4f | 600 | # State machine. |
6aa03e3a C |
601 | if pdata >= 0x80 and pdata != 0xf7: |
602 | state = self.get_next_state(pdata) | |
603 | if state != 'HANDLE SYSREALTIME MSG' and self.state != 'IDLE': | |
604 | # Flush the previous data since a new message is starting | |
605 | self.handle_state(self.state, None) | |
606 | # Cache ss and es -after- flushing previous data | |
607 | self.ss, self.es = ss, es | |
17f5df4f | 608 | # This is a status byte, remember the start sample. |
6aa03e3a C |
609 | if state != 'HANDLE SYSREALTIME MSG': self.ss_block = ss |
610 | elif self.state == 'IDLE' or self.state == 'BUFFER GARBAGE MSG': | |
611 | # Deal with "running status" or that we're buffering garbage | |
612 | self.ss, self.es = ss, es | |
613 | if self.state == 'IDLE': self.ss_block = ss | |
614 | state = self.get_next_state(pdata) | |
615 | else: | |
616 | self.ss, self.es = ss, es | |
617 | state = self.state | |
17f5df4f UH |
618 | |
619 | # Yes, this is intentionally _not_ an 'elif' here. | |
6aa03e3a C |
620 | if state != 'HANDLE SYSREALTIME MSG': |
621 | self.state = state | |
622 | if state == 'BUFFER GARBAGE MSG': | |
623 | self.status_byte = 0 | |
624 | self.handle_state(state, pdata) |