2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
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
21 # MIDI protocol decoder
23 import sigrokdecode as srd
29 class Decoder(srd.Decoder):
33 longname = 'Musical Instrument Digital Interface'
34 desc = 'Musical Instrument Digital Interface (MIDI) protocol.'
42 ['Text (verbose)', 'Human-readable text (verbose)'],
43 # ['Text', 'Human-readable text'],
46 def __init__(self, **kwargs):
55 # self.out_proto = self.add(srd.OUTPUT_PROTO, 'midi')
56 self.out_ann = self.add(srd.OUTPUT_ANN, 'midi')
62 self.put(self.ss_block, self.es_block, self.out_ann, data)
64 def handle_channel_msg_0x80(self):
66 # n = channel, kk = note, vv = velocity
70 self.es_block = self.es
71 msg, chan, note, velocity = c[0] & 0xf0, (c[0] & 0x0f) + 1, c[1], c[2]
72 self.putx([0, ['Channel %d: %s (note = %d, velocity = %d)' % \
73 (chan, status_bytes[msg], note, velocity)]])
74 self.cmd, self.state = [], 'IDLE'
76 def handle_channel_msg_0x90(self):
78 # n = channel, kk = note, vv = velocity
79 # If velocity == 0 that actually means 'note off', though.
83 self.es_block = self.ss
84 msg, chan, note, velocity = c[0] & 0xf0, (c[0] & 0x0f) + 1, c[1], c[2]
85 s = 'note off' if (velocity == 0) else status_bytes[msg]
86 self.putx([0, ['Channel %d: %s (note = %d, velocity = %d)' % \
87 (chan, s, note, velocity)]])
88 self.cmd, self.state = [], 'IDLE'
90 def handle_channel_msg_0xa0(self):
91 # Polyphonic key pressure / aftertouch: An kk vv
92 # n = channel, kk = polyphonic key pressure, vv = pressure value
95 def handle_controller_0x44(self):
96 # Legato footswitch: Bn 44 vv
97 # n = channel, vv = value (<= 0x3f: normal, > 0x3f: legato)
98 chan, vv = (self.cmd[0] & 0x0f) + 1, self.cmd[2]
99 t = 'normal' if vv <= 0x3f else 'legato'
100 self.putx([0, ['Channel %d: control function \'%s\' = %s' % \
101 (chan, control_functions[0x44], t)]])
103 def handle_controller_0x54(self):
104 # Portamento control (PTC): Bn 54 kk
105 # n = channel, kk = source note for pitch reference
106 chan, kk = (self.cmd[0] & 0x0f) + 1, self.cmd[2]
107 self.putx([0, ['Channel %d: control function \'%s\' (source note ' \
108 '= %d)' % (chan, control_functions[0x54], kk)]])
110 def handle_controller_generic(self):
112 chan, fn, param = (c[0] & 0x0f) + 1, c[1], c[2]
113 ctrl_fn = control_functions.get(fn, 'undefined')
114 self.putx([0, ['Channel %d: control change to function \'%s\' ' \
115 '(param = 0x%02x)' % (chan, ctrl_fn, param)]])
117 def handle_channel_msg_0xb0(self):
118 # Control change (or channel mode messages): Bn cc vv
119 # n = channel, cc = control number (0 - 119), vv = control value
121 if (len(c) >= 2) and (c[1] in range(0x78, 0x7f + 1)):
122 # This is not a control change, but rather a channel mode message.
123 # TODO: Handle channel mode messages.
127 self.es_block = self.es
128 handle_ctrl = getattr(self, 'handle_controller_0x%02x' % c[1],
129 self.handle_controller_generic)
131 self.cmd, self.state = [], 'IDLE'
133 def handle_channel_msg_0xc0(self):
134 # Program change: Cn pp
135 # n = channel, pp = program number (0 - 127)
138 def handle_channel_msg_0xd0(self):
139 # Channel pressure / aftertouch: Dn vv
140 # n = channel, vv = pressure value
143 def handle_channel_msg_0xe0(self):
144 # Pitch bend change: En ll mm
145 # n = channel, ll = pitch bend change LSB, mm = pitch bend change MSB
148 def handle_channel_msg_generic(self):
149 msg_type = self.cmd[0] & 0xf0
150 self.putx([0, ['Unknown channel message type: 0x%02x' % msg_type]])
151 # TODO: Handle properly.
153 def handle_channel_msg(self, newbyte):
154 self.cmd.append(newbyte)
155 msg_type = self.cmd[0] & 0xf0
156 handle_msg = getattr(self, 'handle_channel_msg_0x%02x' % msg_type,
157 self.handle_channel_msg_generic)
160 def handle_sysex_msg(self, newbyte):
161 # SysEx message: 1 status byte, x data bytes, EOX byte
162 self.cmd.append(newbyte)
163 if newbyte != 0xf7: # EOX
165 self.es_block = self.es
166 # TODO: Get message ID, vendor ID, message contents, etc.
167 self.putx([0, ['SysEx message']])
168 self.cmd, self.state = [], 'IDLE'
170 def handle_syscommon_msg(self, newbyte):
173 def handle_sysrealtime_msg(self, newbyte):
174 # System realtime message: 0b11111ttt (t = message type)
175 self.es_block = self.ss
176 self.putx([0, ['System realtime message: %s' % status_bytes[newbyte]]])
177 self.cmd, self.state = [], 'IDLE'
179 def decode(self, ss, es, data):
180 ptype, rxtx, pdata = data
182 # For now, ignore all UART packets except the actual data packets.
186 self.ss, self.es = ss, es
188 # Short MIDI overview:
189 # - Status bytes are 0x80-0xff, data bytes are 0x00-0x7f.
190 # - Most messages: 1 status byte, 1-2 data bytes.
191 # - Real-time system messages: always 1 byte.
192 # - SysEx messages: 1 status byte, n data bytes, EOX byte.
195 if self.state == 'IDLE':
196 # Wait until we see a status byte (bit 7 must be set).
198 return # TODO: How to handle? Ignore?
199 # This is a status byte, remember the start sample.
201 if pdata in range(0x80, 0xef + 1):
202 self.state = 'HANDLE CHANNEL MSG'
204 self.state = 'HANDLE SYSEX MSG'
205 elif pdata in range(0xf1, 0xf7 + 1):
206 self.state = 'HANDLE SYSCOMMON MSG'
207 elif pdata in range(0xf8, 0xff + 1):
208 self.state = 'HANDLE SYSREALTIME MSG'
210 # Yes, this is intentionally _not_ an 'elif' here.
211 if self.state == 'HANDLE CHANNEL MSG':
212 self.handle_channel_msg(pdata)
213 elif self.state == 'HANDLE SYSEX MSG':
214 self.handle_sysex_msg(pdata)
215 elif self.state == 'HANDLE SYSCOMMON MSG':
216 self.handle_syscommon_msg(pdata)
217 elif self.state == 'HANDLE SYSREALTIME MSG':
218 self.handle_sysrealtime_msg(pdata)
220 raise Exception('Invalid state: %s' % self.state)