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