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