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