]>
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 | ||
21 | # MIDI protocol decoder | |
22 | ||
23 | import sigrokdecode as srd | |
24 | from .lists import * | |
25 | ||
26 | RX = 0 | |
27 | TX = 1 | |
28 | ||
29 | class Decoder(srd.Decoder): | |
30 | api_version = 1 | |
31 | id = 'midi' | |
32 | name = 'MIDI' | |
33 | longname = 'Musical Instrument Digital Interface' | |
34 | desc = 'Musical Instrument Digital Interface (MIDI) protocol.' | |
35 | license = 'gplv2+' | |
36 | inputs = ['uart'] | |
37 | outputs = ['midi'] | |
38 | probes = [] | |
39 | optional_probes = [] | |
40 | options = {} | |
41 | annotations = [ | |
42 | ['Text (verbose)', 'Human-readable text (verbose)'], | |
43 | # ['Text', 'Human-readable text'], | |
44 | ] | |
45 | ||
46 | def __init__(self, **kwargs): | |
47 | self.cmd = [] | |
48 | self.state = 'IDLE' | |
49 | self.ss = None | |
50 | self.es = None | |
51 | self.ss_block = None | |
52 | self.es_block = None | |
53 | ||
8915b346 | 54 | def start(self): |
be465111 BV |
55 | # self.out_proto = self.register(srd.OUTPUT_PYTHON) |
56 | self.out_ann = self.register(srd.OUTPUT_ANN) | |
17f5df4f | 57 | |
17f5df4f UH |
58 | def putx(self, data): |
59 | self.put(self.ss_block, self.es_block, self.out_ann, data) | |
60 | ||
61 | def handle_channel_msg_0x80(self): | |
62 | # Note off: 8n kk vv | |
63 | # n = channel, kk = note, vv = velocity | |
64 | c = self.cmd | |
65 | if len(c) < 3: | |
66 | return | |
67 | self.es_block = self.es | |
68 | msg, chan, note, velocity = c[0] & 0xf0, (c[0] & 0x0f) + 1, c[1], c[2] | |
69 | self.putx([0, ['Channel %d: %s (note = %d, velocity = %d)' % \ | |
70 | (chan, status_bytes[msg], note, velocity)]]) | |
71 | self.cmd, self.state = [], 'IDLE' | |
72 | ||
73 | def handle_channel_msg_0x90(self): | |
74 | # Note on: 9n kk vv | |
75 | # n = channel, kk = note, vv = velocity | |
76 | # If velocity == 0 that actually means 'note off', though. | |
77 | c = self.cmd | |
78 | if len(c) < 3: | |
79 | return | |
80 | self.es_block = self.ss | |
81 | msg, chan, note, velocity = c[0] & 0xf0, (c[0] & 0x0f) + 1, c[1], c[2] | |
82 | s = 'note off' if (velocity == 0) else status_bytes[msg] | |
83 | self.putx([0, ['Channel %d: %s (note = %d, velocity = %d)' % \ | |
84 | (chan, s, note, velocity)]]) | |
85 | self.cmd, self.state = [], 'IDLE' | |
86 | ||
87 | def handle_channel_msg_0xa0(self): | |
88 | # Polyphonic key pressure / aftertouch: An kk vv | |
89 | # n = channel, kk = polyphonic key pressure, vv = pressure value | |
90 | pass # TODO | |
91 | ||
92 | def handle_controller_0x44(self): | |
93 | # Legato footswitch: Bn 44 vv | |
94 | # n = channel, vv = value (<= 0x3f: normal, > 0x3f: legato) | |
95 | chan, vv = (self.cmd[0] & 0x0f) + 1, self.cmd[2] | |
96 | t = 'normal' if vv <= 0x3f else 'legato' | |
97 | self.putx([0, ['Channel %d: control function \'%s\' = %s' % \ | |
98 | (chan, control_functions[0x44], t)]]) | |
99 | ||
100 | def handle_controller_0x54(self): | |
101 | # Portamento control (PTC): Bn 54 kk | |
102 | # n = channel, kk = source note for pitch reference | |
103 | chan, kk = (self.cmd[0] & 0x0f) + 1, self.cmd[2] | |
104 | self.putx([0, ['Channel %d: control function \'%s\' (source note ' \ | |
105 | '= %d)' % (chan, control_functions[0x54], kk)]]) | |
106 | ||
107 | def handle_controller_generic(self): | |
108 | c = self.cmd | |
109 | chan, fn, param = (c[0] & 0x0f) + 1, c[1], c[2] | |
110 | ctrl_fn = control_functions.get(fn, 'undefined') | |
111 | self.putx([0, ['Channel %d: control change to function \'%s\' ' \ | |
112 | '(param = 0x%02x)' % (chan, ctrl_fn, param)]]) | |
113 | ||
114 | def handle_channel_msg_0xb0(self): | |
115 | # Control change (or channel mode messages): Bn cc vv | |
116 | # n = channel, cc = control number (0 - 119), vv = control value | |
117 | c = self.cmd | |
118 | if (len(c) >= 2) and (c[1] in range(0x78, 0x7f + 1)): | |
119 | # This is not a control change, but rather a channel mode message. | |
120 | # TODO: Handle channel mode messages. | |
121 | return | |
122 | if len(c) < 3: | |
123 | return | |
124 | self.es_block = self.es | |
125 | handle_ctrl = getattr(self, 'handle_controller_0x%02x' % c[1], | |
126 | self.handle_controller_generic) | |
127 | handle_ctrl() | |
128 | self.cmd, self.state = [], 'IDLE' | |
129 | ||
130 | def handle_channel_msg_0xc0(self): | |
131 | # Program change: Cn pp | |
132 | # n = channel, pp = program number (0 - 127) | |
133 | pass # TODO | |
134 | ||
135 | def handle_channel_msg_0xd0(self): | |
136 | # Channel pressure / aftertouch: Dn vv | |
137 | # n = channel, vv = pressure value | |
138 | pass # TODO | |
139 | ||
140 | def handle_channel_msg_0xe0(self): | |
141 | # Pitch bend change: En ll mm | |
142 | # n = channel, ll = pitch bend change LSB, mm = pitch bend change MSB | |
143 | pass # TODO | |
144 | ||
145 | def handle_channel_msg_generic(self): | |
146 | msg_type = self.cmd[0] & 0xf0 | |
147 | self.putx([0, ['Unknown channel message type: 0x%02x' % msg_type]]) | |
148 | # TODO: Handle properly. | |
149 | ||
150 | def handle_channel_msg(self, newbyte): | |
151 | self.cmd.append(newbyte) | |
152 | msg_type = self.cmd[0] & 0xf0 | |
153 | handle_msg = getattr(self, 'handle_channel_msg_0x%02x' % msg_type, | |
154 | self.handle_channel_msg_generic) | |
155 | handle_msg() | |
156 | ||
157 | def handle_sysex_msg(self, newbyte): | |
158 | # SysEx message: 1 status byte, x data bytes, EOX byte | |
159 | self.cmd.append(newbyte) | |
160 | if newbyte != 0xf7: # EOX | |
161 | return | |
162 | self.es_block = self.es | |
163 | # TODO: Get message ID, vendor ID, message contents, etc. | |
164 | self.putx([0, ['SysEx message']]) | |
165 | self.cmd, self.state = [], 'IDLE' | |
166 | ||
167 | def handle_syscommon_msg(self, newbyte): | |
168 | pass # TODO | |
169 | ||
170 | def handle_sysrealtime_msg(self, newbyte): | |
171 | # System realtime message: 0b11111ttt (t = message type) | |
172 | self.es_block = self.ss | |
173 | self.putx([0, ['System realtime message: %s' % status_bytes[newbyte]]]) | |
174 | self.cmd, self.state = [], 'IDLE' | |
175 | ||
176 | def decode(self, ss, es, data): | |
177 | ptype, rxtx, pdata = data | |
178 | ||
179 | # For now, ignore all UART packets except the actual data packets. | |
180 | if ptype != 'DATA': | |
181 | return | |
182 | ||
183 | self.ss, self.es = ss, es | |
184 | ||
185 | # Short MIDI overview: | |
186 | # - Status bytes are 0x80-0xff, data bytes are 0x00-0x7f. | |
187 | # - Most messages: 1 status byte, 1-2 data bytes. | |
188 | # - Real-time system messages: always 1 byte. | |
189 | # - SysEx messages: 1 status byte, n data bytes, EOX byte. | |
190 | ||
191 | # State machine. | |
192 | if self.state == 'IDLE': | |
193 | # Wait until we see a status byte (bit 7 must be set). | |
194 | if pdata < 0x80: | |
195 | return # TODO: How to handle? Ignore? | |
196 | # This is a status byte, remember the start sample. | |
197 | self.ss_block = ss | |
198 | if pdata in range(0x80, 0xef + 1): | |
199 | self.state = 'HANDLE CHANNEL MSG' | |
200 | elif pdata == 0xf0: | |
201 | self.state = 'HANDLE SYSEX MSG' | |
202 | elif pdata in range(0xf1, 0xf7 + 1): | |
203 | self.state = 'HANDLE SYSCOMMON MSG' | |
204 | elif pdata in range(0xf8, 0xff + 1): | |
205 | self.state = 'HANDLE SYSREALTIME MSG' | |
206 | ||
207 | # Yes, this is intentionally _not_ an 'elif' here. | |
208 | if self.state == 'HANDLE CHANNEL MSG': | |
209 | self.handle_channel_msg(pdata) | |
210 | elif self.state == 'HANDLE SYSEX MSG': | |
211 | self.handle_sysex_msg(pdata) | |
212 | elif self.state == 'HANDLE SYSCOMMON MSG': | |
213 | self.handle_syscommon_msg(pdata) | |
214 | elif self.state == 'HANDLE SYSREALTIME MSG': | |
215 | self.handle_sysrealtime_msg(pdata) | |
216 | else: | |
217 | raise Exception('Invalid state: %s' % self.state) | |
218 |