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