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