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