id_t becomes a fully-fledged object, UUID's used for IDs, generic MIDI now owns bindi...
[ardour.git] / libs / surfaces / generic_midi / midicontrollable.cc
1 /*
2     Copyright (C) 1998-2006 Paul Davis
3  
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18     $Id: midicontrollable.cc 629 2006-06-21 23:01:03Z paul $
19 */
20
21 #include <cstdio> /* for sprintf, sigh */
22 #include <climits>
23 #include <pbd/error.h>
24 #include <pbd/xml++.h>
25 #include <midi++/port.h>
26 #include <midi++/channel.h>
27
28 #include "midicontrollable.h"
29
30 using namespace sigc;
31 using namespace MIDI;
32 using namespace PBD;
33 using namespace ARDOUR;
34
35 bool MIDIControllable::_send_feedback = false;
36
37 MIDIControllable::MIDIControllable (Port& p, Controllable& c, bool is_bistate)
38         : controllable (c), _port (p), bistate (is_bistate)
39 {
40         setting = false;
41         last_written = 0; // got a better idea ?
42         control_type = none;
43         _control_description = "MIDI Control: none";
44         control_additional = (byte) -1;
45         connections = 0;
46         feedback = true; // for now
47         
48         /* use channel 0 ("1") as the initial channel */
49
50         midi_rebind (0);
51 }
52
53 MIDIControllable::~MIDIControllable ()
54 {
55         drop_external_control ();
56 }
57
58 void
59 MIDIControllable::midi_forget ()
60 {
61         /* stop listening for incoming messages, but retain
62            our existing event + type information.
63         */
64         
65         if (connections > 0) {
66                 midi_sense_connection[0].disconnect ();
67         } 
68         
69         if (connections > 1) {
70                 midi_sense_connection[1].disconnect ();
71         }
72         
73         connections = 0;
74         midi_learn_connection.disconnect ();
75         
76 }
77
78 void
79 MIDIControllable::midi_rebind (channel_t c)
80 {
81         if (c >= 0) {
82                 bind_midi (c, control_type, control_additional);
83         } else {
84                 midi_forget ();
85         }
86 }
87
88 void
89 MIDIControllable::learn_about_external_control ()
90 {
91         drop_external_control ();
92         midi_learn_connection = _port.input()->any.connect (mem_fun (*this, &MIDIControllable::midi_receiver));
93         learning_started ();
94 }
95
96 void
97 MIDIControllable::stop_learning ()
98 {
99         midi_learn_connection.disconnect ();
100 }
101
102 void
103 MIDIControllable::drop_external_control ()
104 {
105         if (connections > 0) {
106                 midi_sense_connection[0].disconnect ();
107         } 
108         if (connections > 1) {
109                 midi_sense_connection[1].disconnect ();
110         }
111
112         connections = 0;
113         midi_learn_connection.disconnect ();
114
115         control_type = none;
116         control_additional = (byte) -1;
117 }
118
119 void 
120 MIDIControllable::midi_sense_note_on (Parser &p, EventTwoBytes *tb) 
121 {
122         midi_sense_note (p, tb, true);
123 }
124
125 void 
126 MIDIControllable::midi_sense_note_off (Parser &p, EventTwoBytes *tb) 
127 {
128         midi_sense_note (p, tb, false);
129 }
130
131 void
132 MIDIControllable::midi_sense_note (Parser &p, EventTwoBytes *msg, bool is_on)
133 {
134         if (!bistate) {
135                 controllable.set_value (msg->note_number/127.0);
136         } else {
137
138                 /* Note: parser handles the use of zero velocity to
139                    mean note off. if we get called with is_on=true, then we
140                    got a *real* note on.  
141                 */
142
143                 if (msg->note_number == control_additional) {
144                         controllable.set_value (is_on ? 1 : 0);
145                 }
146         }
147 }
148
149 void
150 MIDIControllable::midi_sense_controller (Parser &, EventTwoBytes *msg)
151 {
152         if (control_additional == msg->controller_number) {
153                 if (!bistate) {
154                         controllable.set_value (msg->value/127.0);
155                 } else {
156                         if (msg->value > 64.0) {
157                                 controllable.set_value (1);
158                         } else {
159                                 controllable.set_value (0);
160                         }
161                 }
162         }
163 }
164
165 void
166 MIDIControllable::midi_sense_program_change (Parser &p, byte msg)
167 {
168         /* XXX program change messages make no sense for bistates */
169
170         if (!bistate) {
171                 controllable.set_value (msg/127.0);
172         } 
173 }
174
175 void
176 MIDIControllable::midi_sense_pitchbend (Parser &p, pitchbend_t pb)
177 {
178         /* pitchbend messages make no sense for bistates */
179
180         /* XXX gack - get rid of assumption about typeof pitchbend_t */
181
182         controllable.set_value ((pb/(float) SHRT_MAX));
183 }                       
184
185 void
186 MIDIControllable::midi_receiver (Parser &p, byte *msg, size_t len)
187 {
188         /* we only respond to channel messages */
189
190         if ((msg[0] & 0xF0) < 0x80 || (msg[0] & 0xF0) > 0xE0) {
191                 return;
192         }
193
194         /* if the our port doesn't do input anymore, forget it ... */
195
196         if (!_port.input()) {
197                 return;
198         }
199
200         bind_midi ((channel_t) (msg[0] & 0xf), eventType (msg[0] & 0xF0), msg[1]);
201
202         learning_stopped ();
203 }
204
205 void
206 MIDIControllable::bind_midi (channel_t chn, eventType ev, MIDI::byte additional)
207 {
208         char buf[64];
209
210         drop_external_control ();
211
212         control_type = ev;
213         control_channel = chn;
214         control_additional = additional;
215
216         if (_port.input() == 0) {
217                 return;
218         }
219         
220         Parser& p = *_port.input();
221
222         int chn_i = chn;
223         switch (ev) {
224         case MIDI::off:
225                 midi_sense_connection[0] = p.channel_note_off[chn_i].connect
226                         (mem_fun (*this, &MIDIControllable::midi_sense_note_off));
227
228                 /* if this is a bistate, connect to noteOn as well,
229                    and we'll toggle back and forth between the two.
230                 */
231
232                 if (bistate) {
233                         midi_sense_connection[1] = p.channel_note_on[chn_i].connect
234                                 (mem_fun (*this, &MIDIControllable::midi_sense_note_on));
235                         connections = 2;
236                 } else {
237                         connections = 1;
238                 }
239                 _control_description = "MIDI control: NoteOff";
240                 break;
241
242         case MIDI::on:
243                 midi_sense_connection[0] = p.channel_note_on[chn_i].connect
244                         (mem_fun (*this, &MIDIControllable::midi_sense_note_on));
245                 if (bistate) {
246                         midi_sense_connection[1] = p.channel_note_off[chn_i].connect 
247                                 (mem_fun (*this, &MIDIControllable::midi_sense_note_off));
248                         connections = 2;
249                 } else {
250                         connections = 1;
251                 }
252                 _control_description = "MIDI control: NoteOn";
253                 break;
254
255         case MIDI::controller:
256                 midi_sense_connection[0] = p.channel_controller[chn_i].connect 
257                         (mem_fun (*this, &MIDIControllable::midi_sense_controller));
258                 connections = 1;
259                 snprintf (buf, sizeof (buf), "MIDI control: Controller %d", control_additional);
260                 _control_description = buf;
261                 break;
262
263         case MIDI::program:
264                 if (!bistate) {
265                         midi_sense_connection[0] = p.channel_program_change[chn_i].connect
266                                 (mem_fun (*this, 
267                                        &MIDIControllable::midi_sense_program_change));
268                         connections = 1;
269                         _control_description = "MIDI control: ProgramChange";
270                 }
271                 break;
272
273         case MIDI::pitchbend:
274                 if (!bistate) {
275                         midi_sense_connection[0] = p.channel_pitchbend[chn_i].connect
276                                 (mem_fun (*this, &MIDIControllable::midi_sense_pitchbend));
277                         connections = 1;
278                         _control_description = "MIDI control: Pitchbend";
279                 }
280                 break;
281
282         default:
283                 break;
284         }
285 }
286
287 void
288 MIDIControllable::send_feedback ()
289 {
290         byte msg[3];
291
292         if (setting || !_send_feedback || control_type == none) {
293                 return;
294         }
295         
296         msg[0] = (control_type & 0xF0) | (control_channel & 0xF); 
297         msg[1] = control_additional;
298         msg[2] = (byte) (controllable.get_value() * 127.0f);
299
300         _port.write (msg, 3);
301 }
302
303 MIDI::byte*
304 MIDIControllable::write_feedback (MIDI::byte* buf, int32_t& bufsize, bool force)
305 {
306         if (control_type != none &&_send_feedback && bufsize > 2) {
307
308                 MIDI::byte gm = (MIDI::byte) (controllable.get_value() * 127.0);
309                 
310                 if (gm != last_written) {
311                         *buf++ = (0xF0 & control_type) | (0xF & control_channel);
312                         *buf++ = control_additional; /* controller number */
313                         *buf++ = gm;
314                         last_written = gm;
315                         bufsize -= 3;
316                 }
317         }
318         
319         return buf;
320 }
321
322 int 
323 MIDIControllable::set_state (const XMLNode& node)
324 {
325         const XMLProperty* prop;
326         int xx;
327
328         if ((prop = node.property ("event")) != 0) {
329                 sscanf (prop->value().c_str(), "0x%x", &xx);
330                 control_type = (MIDI::eventType) xx;
331         } else {
332                 return -1;
333         }
334
335         if ((prop = node.property ("channel")) != 0) {
336                 sscanf (prop->value().c_str(), "%d", &xx);
337                 control_channel = (MIDI::channel_t) xx;
338         } else {
339                 return -1;
340         }
341
342         if ((prop = node.property ("additional")) != 0) {
343                 sscanf (prop->value().c_str(), "0x%x", &xx);
344                 control_additional = (MIDI::byte) xx;
345         } else {
346                 return -1;
347         }
348
349         return 0;
350 }
351
352 XMLNode&
353 MIDIControllable::get_state ()
354 {
355         char buf[32];
356         XMLNode& node (controllable.get_state ());
357
358         snprintf (buf, sizeof(buf), "0x%x", (int) control_type);
359         node.add_property ("event", buf);
360         snprintf (buf, sizeof(buf), "%d", (int) control_channel);
361         node.add_property ("channel", buf);
362         snprintf (buf, sizeof(buf), "0x%x", (int) control_additional);
363         node.add_property ("additional", buf);
364
365         return node;
366 }
367