1f00e58167e93eb172573ebddcc654663a2ee425
[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 */
19
20 #include <cstdio> /* for sprintf, sigh */
21 #include <climits>
22 #include <pbd/error.h>
23 #include <pbd/xml++.h>
24 #include <midi++/port.h>
25 #include <midi++/channel.h>
26
27 #include "midicontrollable.h"
28
29 using namespace sigc;
30 using namespace MIDI;
31 using namespace PBD;
32 using namespace ARDOUR;
33
34 MIDIControllable::MIDIControllable (Port& p, Controllable& c, bool is_bistate)
35         : controllable (c), _port (p), bistate (is_bistate)
36 {
37         setting = false;
38         last_value = 0; // got a better idea ?
39         control_type = none;
40         _control_description = "MIDI Control: none";
41         control_additional = (byte) -1;
42         connections = 0;
43         feedback = true; // for now
44         
45         /* use channel 0 ("1") as the initial channel */
46
47         midi_rebind (0);
48 }
49
50 MIDIControllable::~MIDIControllable ()
51 {
52         drop_external_control ();
53 }
54
55 void
56 MIDIControllable::midi_forget ()
57 {
58         /* stop listening for incoming messages, but retain
59            our existing event + type information.
60         */
61         
62         if (connections > 0) {
63                 midi_sense_connection[0].disconnect ();
64         } 
65         
66         if (connections > 1) {
67                 midi_sense_connection[1].disconnect ();
68         }
69         
70         connections = 0;
71         midi_learn_connection.disconnect ();
72         
73 }
74
75 void
76 MIDIControllable::midi_rebind (channel_t c)
77 {
78         if (c >= 0) {
79                 bind_midi (c, control_type, control_additional);
80         } else {
81                 midi_forget ();
82         }
83 }
84
85 void
86 MIDIControllable::learn_about_external_control ()
87 {
88         drop_external_control ();
89         midi_learn_connection = _port.input()->any.connect (mem_fun (*this, &MIDIControllable::midi_receiver));
90 }
91
92 void
93 MIDIControllable::stop_learning ()
94 {
95         midi_learn_connection.disconnect ();
96 }
97
98 void
99 MIDIControllable::drop_external_control ()
100 {
101         cerr << "Dropping existing control using " << connections << " connections\n";
102
103         if (connections > 0) {
104                 midi_sense_connection[0].disconnect ();
105         } 
106         if (connections > 1) {
107                 midi_sense_connection[1].disconnect ();
108         }
109
110         connections = 0;
111         midi_learn_connection.disconnect ();
112
113         control_type = none;
114         control_additional = (byte) -1;
115 }
116
117 void 
118 MIDIControllable::midi_sense_note_on (Parser &p, EventTwoBytes *tb) 
119 {
120         midi_sense_note (p, tb, true);
121 }
122
123 void 
124 MIDIControllable::midi_sense_note_off (Parser &p, EventTwoBytes *tb) 
125 {
126         midi_sense_note (p, tb, false);
127 }
128
129 void
130 MIDIControllable::midi_sense_note (Parser &p, EventTwoBytes *msg, bool is_on)
131 {
132         if (!bistate) {
133                 controllable.set_value (msg->note_number/127.0);
134         } else {
135
136                 /* Note: parser handles the use of zero velocity to
137                    mean note off. if we get called with is_on=true, then we
138                    got a *real* note on.  
139                 */
140
141                 if (msg->note_number == control_additional) {
142                         controllable.set_value (is_on ? 1 : 0);
143                 }
144         }
145
146         last_value = (MIDI::byte) (controllable.get_value() * 127.0); // to prevent feedback fights
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                 last_value = (MIDI::byte) (controllable.get_value() * 127.0); // to prevent feedback fights
164         }
165 }
166
167 void
168 MIDIControllable::midi_sense_program_change (Parser &p, byte msg)
169 {
170         /* XXX program change messages make no sense for bistates */
171
172         if (!bistate) {
173                 controllable.set_value (msg/127.0);
174                 last_value = (MIDI::byte) (controllable.get_value() * 127.0); // to prevent feedback fights
175         } 
176 }
177
178 void
179 MIDIControllable::midi_sense_pitchbend (Parser &p, pitchbend_t pb)
180 {
181         /* pitchbend messages make no sense for bistates */
182
183         /* XXX gack - get rid of assumption about typeof pitchbend_t */
184
185         controllable.set_value ((pb/(float) SHRT_MAX));
186         last_value = (MIDI::byte) (controllable.get_value() * 127.0); // to prevent feedback fights
187 }                       
188
189 void
190 MIDIControllable::midi_receiver (Parser &p, byte *msg, size_t len)
191 {
192         /* we only respond to channel messages */
193
194         if ((msg[0] & 0xF0) < 0x80 || (msg[0] & 0xF0) > 0xE0) {
195                 return;
196         }
197
198         /* if the our port doesn't do input anymore, forget it ... */
199
200         if (!_port.input()) {
201                 return;
202         }
203
204         bind_midi ((channel_t) (msg[0] & 0xf), eventType (msg[0] & 0xF0), msg[1]);
205
206         controllable.LearningFinished ();
207 }
208
209 void
210 MIDIControllable::bind_midi (channel_t chn, eventType ev, MIDI::byte additional)
211 {
212         char buf[64];
213
214         drop_external_control ();
215
216         control_type = ev;
217         control_channel = chn;
218         control_additional = additional;
219
220         if (_port.input() == 0) {
221                 return;
222         }
223         
224         Parser& p = *_port.input();
225
226         int chn_i = chn;
227         switch (ev) {
228         case MIDI::off:
229                 midi_sense_connection[0] = p.channel_note_off[chn_i].connect
230                         (mem_fun (*this, &MIDIControllable::midi_sense_note_off));
231
232                 /* if this is a bistate, connect to noteOn as well,
233                    and we'll toggle back and forth between the two.
234                 */
235
236                 if (bistate) {
237                         midi_sense_connection[1] = p.channel_note_on[chn_i].connect
238                                 (mem_fun (*this, &MIDIControllable::midi_sense_note_on));
239                         connections = 2;
240                 } else {
241                         connections = 1;
242                 }
243                 _control_description = "MIDI control: NoteOff";
244                 break;
245
246         case MIDI::on:
247                 midi_sense_connection[0] = p.channel_note_on[chn_i].connect
248                         (mem_fun (*this, &MIDIControllable::midi_sense_note_on));
249                 if (bistate) {
250                         midi_sense_connection[1] = p.channel_note_off[chn_i].connect 
251                                 (mem_fun (*this, &MIDIControllable::midi_sense_note_off));
252                         connections = 2;
253                 } else {
254                         connections = 1;
255                 }
256                 _control_description = "MIDI control: NoteOn";
257                 break;
258
259         case MIDI::controller:
260                 midi_sense_connection[0] = p.channel_controller[chn_i].connect 
261                         (mem_fun (*this, &MIDIControllable::midi_sense_controller));
262                 connections = 1;
263                 snprintf (buf, sizeof (buf), "MIDI control: Controller %d", control_additional);
264                 _control_description = buf;
265                 break;
266
267         case MIDI::program:
268                 if (!bistate) {
269                         midi_sense_connection[0] = p.channel_program_change[chn_i].connect
270                                 (mem_fun (*this, 
271                                        &MIDIControllable::midi_sense_program_change));
272                         connections = 1;
273                         _control_description = "MIDI control: ProgramChange";
274                 }
275                 break;
276
277         case MIDI::pitchbend:
278                 if (!bistate) {
279                         midi_sense_connection[0] = p.channel_pitchbend[chn_i].connect
280                                 (mem_fun (*this, &MIDIControllable::midi_sense_pitchbend));
281                         connections = 1;
282                         _control_description = "MIDI control: Pitchbend";
283                 }
284                 break;
285
286         default:
287                 break;
288         }
289
290         cerr << "MIDI bound with " << connections << endl;
291 }
292
293 void
294 MIDIControllable::send_feedback ()
295 {
296         byte msg[3];
297
298         if (setting || !feedback || control_type == none) {
299                 return;
300         }
301
302         msg[0] = (control_type & 0xF0) | (control_channel & 0xF); 
303         msg[1] = control_additional;
304         msg[2] = (byte) (controllable.get_value() * 127.0f);
305
306         _port.write (msg, 3);
307 }
308
309 MIDI::byte*
310 MIDIControllable::write_feedback (MIDI::byte* buf, int32_t& bufsize, bool force)
311 {
312         if (control_type != none && feedback && bufsize > 2) {
313
314                 MIDI::byte gm = (MIDI::byte) (controllable.get_value() * 127.0);
315                 
316                 if (gm != last_value) {
317                         *buf++ = (0xF0 & control_type) | (0xF & control_channel);
318                         *buf++ = control_additional; /* controller number */
319                         *buf++ = gm;
320                         last_value = gm;
321                         bufsize -= 3;
322                 }
323         }
324         
325         return buf;
326 }
327
328 int 
329 MIDIControllable::set_state (const XMLNode& node)
330 {
331         const XMLProperty* prop;
332         int xx;
333
334         if ((prop = node.property ("event")) != 0) {
335                 sscanf (prop->value().c_str(), "0x%x", &xx);
336                 control_type = (MIDI::eventType) xx;
337         } else {
338                 return -1;
339         }
340
341         if ((prop = node.property ("channel")) != 0) {
342                 sscanf (prop->value().c_str(), "%d", &xx);
343                 control_channel = (MIDI::channel_t) xx;
344         } else {
345                 return -1;
346         }
347
348         if ((prop = node.property ("additional")) != 0) {
349                 sscanf (prop->value().c_str(), "0x%x", &xx);
350                 control_additional = (MIDI::byte) xx;
351         } else {
352                 return -1;
353         }
354
355         if ((prop = node.property ("feedback")) != 0) {
356                 feedback = (prop->value() == "yes");
357         } else {
358                 feedback = true; // default
359         }
360
361         bind_midi (control_channel, control_type, control_additional);
362         
363         return 0;
364 }
365
366 XMLNode&
367 MIDIControllable::get_state ()
368 {
369         char buf[32];
370         XMLNode& node (controllable.get_state ());
371
372         snprintf (buf, sizeof(buf), "0x%x", (int) control_type);
373         node.add_property ("event", buf);
374         snprintf (buf, sizeof(buf), "%d", (int) control_channel);
375         node.add_property ("channel", buf);
376         snprintf (buf, sizeof(buf), "0x%x", (int) control_additional);
377         node.add_property ("additional", buf);
378         node.add_property ("feedback", (feedback ? "yes" : "no"));
379
380         return node;
381 }
382