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