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