29271418a7fb18f7719d4fbae56fd2faf26b7bf0
[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 #include "pbd/stacktrace.h"
29 #include "pbd/compose.h"
30
31 #include "midi++/types.h" // Added by JE - 06-01-2009. All instances of 'byte' changed to 'MIDI::byte' (for clarification)
32 #include "midi++/port.h"
33 #include "midi++/channel.h"
34
35 #include "ardour/async_midi_port.h"
36 #include "ardour/automation_control.h"
37 #include "ardour/midi_ui.h"
38 #include "ardour/utils.h"
39 #include "ardour/debug.h"
40
41 #include "midicontrollable.h"
42 #include "generic_midi_control_protocol.h"
43
44 using namespace std;
45 using namespace MIDI;
46 using namespace PBD;
47 using namespace ARDOUR;
48
49 MIDIControllable::MIDIControllable (GenericMidiControlProtocol* s, MIDI::Parser& p, bool m)
50         : _surface (s)
51         , controllable (0)
52         , _descriptor (0)
53         , _parser (p)
54         , _momentary (m)
55 {
56         _learned = false; /* from URI */
57         _encoder = No_enc;
58         setting = false;
59         last_value = 0; // got a better idea ?
60         last_controllable_value = 0.0f;
61         control_type = none;
62         control_rpn = -1;
63         control_nrpn = -1;
64         _control_description = "MIDI Control: none";
65         control_additional = (MIDI::byte) -1;
66         feedback = true; // for now
67 }
68
69 MIDIControllable::MIDIControllable (GenericMidiControlProtocol* s, MIDI::Parser& p, Controllable& c, bool m)
70         : _surface (s)
71         , _descriptor (0)
72         , _parser (p)
73         , _momentary (m)
74 {
75         set_controllable (&c);
76
77         _learned = true; /* from controllable */
78         _encoder = No_enc;
79         setting = false;
80         last_value = 0; // got a better idea ?
81         last_controllable_value = 0.0f;
82         control_type = none;
83         control_rpn = -1;
84         control_nrpn = -1;
85         _control_description = "MIDI Control: none";
86         control_additional = (MIDI::byte) -1;
87         feedback = true; // for now
88 }
89
90 MIDIControllable::~MIDIControllable ()
91 {
92         drop_external_control ();
93 }
94
95 int
96 MIDIControllable::init (const std::string& s)
97 {
98         _current_uri = s;
99         delete _descriptor;
100         _descriptor = new ControllableDescriptor;
101         return _descriptor->set (s);
102 }
103
104 void
105 MIDIControllable::midi_forget ()
106 {
107         /* stop listening for incoming messages, but retain
108            our existing event + type information.
109         */
110
111         midi_sense_connection[0].disconnect ();
112         midi_sense_connection[1].disconnect ();
113         midi_learn_connection.disconnect ();
114 }
115
116 void
117 MIDIControllable::drop_external_control ()
118 {
119         midi_forget ();
120         control_rpn = -1;
121         control_nrpn = -1;
122         control_type = none;
123         control_additional = (MIDI::byte) -1;
124 }
125
126 void
127 MIDIControllable::set_controllable (Controllable* c)
128 {
129         if (c == controllable) {
130                 return;
131         }
132
133         controllable_death_connection.disconnect ();
134
135         controllable = c;
136
137         if (controllable) {
138                 last_controllable_value = controllable->get_value();
139         } else {
140                 last_controllable_value = 0.0f; // is there a better value?
141         }
142
143         if (controllable) {
144                 controllable->Destroyed.connect (controllable_death_connection, MISSING_INVALIDATOR,
145                                                  boost::bind (&MIDIControllable::drop_controllable, this),
146                                                  MidiControlUI::instance());
147         }
148 }
149
150 void
151 MIDIControllable::midi_rebind (channel_t c)
152 {
153         if (c >= 0) {
154                 bind_midi (c, control_type, control_additional);
155         } else {
156                 midi_forget ();
157         }
158 }
159
160 void
161 MIDIControllable::learn_about_external_control ()
162 {
163         drop_external_control ();
164         _parser.any.connect_same_thread (midi_learn_connection, boost::bind (&MIDIControllable::midi_receiver, this, _1, _2, _3));
165 }
166
167 void
168 MIDIControllable::stop_learning ()
169 {
170         midi_learn_connection.disconnect ();
171 }
172
173 int
174 MIDIControllable::control_to_midi (float val)
175 {
176         if (controllable->is_gain_like()) {
177                 return gain_to_slider_position (val) * max_value_for_type ();
178         }
179
180         float control_min = controllable->lower ();
181         float control_max = controllable->upper ();
182         float control_range = control_max - control_min;
183
184         if (controllable->is_toggle()) {
185                 if (val >= (control_min + (control_range/2.0f))) {
186                         return max_value_for_type();
187                 } else {
188                         return 0;
189                 }
190         } else {
191                 AutomationControl *actl = dynamic_cast<AutomationControl*> (controllable);
192                 if (actl) {
193                         control_min = actl->internal_to_interface(control_min);
194                         control_max = actl->internal_to_interface(control_max);
195                         control_range = control_max - control_min;
196                         val = actl->internal_to_interface(val);
197                 }
198         }
199         // fiddle value of max so value doesn't jump from 125 to 127 for 1.0
200         // otherwise decrement won't work.
201         return (val - control_min) / control_range * (max_value_for_type () - 1);
202 }
203
204 float
205 MIDIControllable::midi_to_control (int val)
206 {
207         /* fiddle with MIDI value so that we get an odd number of integer steps
208            and can thus represent "middle" precisely as 0.5. this maps to
209            the range 0..+1.0 (0 to 126)
210         */
211
212         float fv = (val == 0 ? 0 : float (val - 1) / (max_value_for_type() - 1));
213
214         if (controllable->is_gain_like()) {
215                 return slider_position_to_gain (fv);
216         }
217
218         float control_min = controllable->lower ();
219         float control_max = controllable->upper ();
220         float control_range = control_max - control_min;
221
222         AutomationControl *actl = dynamic_cast<AutomationControl*> (controllable);
223         if (actl) {
224                 if (fv == 0.f) return control_min;
225                 if (fv == 1.f) return control_max;
226                 control_min = actl->internal_to_interface(control_min);
227                 control_max = actl->internal_to_interface(control_max);
228                 control_range = control_max - control_min;
229                 return actl->interface_to_internal((fv * control_range) + control_min);
230         }
231         return (fv * control_range) + control_min;
232 }
233
234 void
235 MIDIControllable::midi_sense_note_on (Parser &p, EventTwoBytes *tb)
236 {
237         midi_sense_note (p, tb, true);
238 }
239
240 void
241 MIDIControllable::midi_sense_note_off (Parser &p, EventTwoBytes *tb)
242 {
243         midi_sense_note (p, tb, false);
244 }
245
246 int
247 MIDIControllable::lookup_controllable()
248 {
249         if (!_descriptor) {
250                 return -1;
251         }
252
253         boost::shared_ptr<Controllable> c = _surface->lookup_controllable (*_descriptor);
254
255         if (!c) {
256                 return -1;
257         }
258
259         set_controllable (c.get ());
260
261         return 0;
262 }
263
264 void
265 MIDIControllable::drop_controllable ()
266 {
267         set_controllable (0);
268 }
269
270 void
271 MIDIControllable::midi_sense_note (Parser &, EventTwoBytes *msg, bool /*is_on*/)
272 {
273         if (!controllable) {
274                 if (lookup_controllable()) {
275                         return;
276                 }
277         }
278
279         if (!controllable->is_toggle()) {
280                 if (control_additional == msg->note_number) {
281                         controllable->set_value (midi_to_control (msg->velocity));
282                         DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("Note %1 value %2  %3\n", (int) msg->note_number, (float) midi_to_control (msg->velocity), current_uri() ));
283                 }
284         } else {
285                 if (control_additional == msg->note_number) {
286                         float new_value = controllable->get_value() > 0.5f ? 0.0f : 1.0f;
287                         controllable->set_value (new_value);
288                         DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("Note %1 Value %2  %3\n", (int) msg->note_number, (float) new_value, current_uri()));
289                 }
290         }
291
292         last_value = (MIDI::byte) (controllable->get_value() * 127.0); // to prevent feedback fights
293 }
294
295 void
296 MIDIControllable::midi_sense_controller (Parser &, EventTwoBytes *msg)
297 {
298         if (!controllable) {
299                 if (lookup_controllable ()) {
300                         return;
301                 }
302         }
303
304         assert (controllable);
305
306         if (controllable->touching()) {
307                 return; // to prevent feedback fights when e.g. dragging a UI slider
308         }
309
310         if (control_additional == msg->controller_number) {
311
312                 if (!controllable->is_toggle()) {
313                         if (get_encoder() == No_enc) {
314                                 float new_value = msg->value;
315                                 float max_value = max(last_controllable_value, new_value);
316                                 float min_value = min(last_controllable_value, new_value);
317                                 float range = max_value - min_value;
318                                 float threshold = (float) _surface->threshold ();
319
320                                 bool const in_sync = (
321                                         range < threshold &&
322                                         controllable->get_value() <= midi_to_control(max_value) &&
323                                         controllable->get_value() >= midi_to_control(min_value)
324                                         );
325
326                                 /* If the surface is not motorised, we try to prevent jumps when
327                                    the MIDI controller and controllable are out of sync.
328                                    There might be a better way of doing this.
329                                 */
330
331                                 if (in_sync || _surface->motorised ()) {
332                                         controllable->set_value (midi_to_control (new_value));
333                                 }
334                                 DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("MIDI CC %1 value %2  %3\n", (int) msg->controller_number, (float) midi_to_control(new_value), current_uri() ));
335
336                                 last_controllable_value = new_value;
337                         } else {
338                                 int offset = (msg->value & 0x3f);
339                                 switch (get_encoder()) {
340                                         case Enc_L:
341                                                 if (msg->value > 0x40) {
342                                                         controllable->set_value (midi_to_control (last_value - offset + 1));
343                                                 } else {
344                                                         controllable->set_value (midi_to_control (last_value + offset + 1));
345                                                 }
346                                                 break;
347                                         case Enc_R:
348                                                 if (msg->value > 0x40) {
349                                                         controllable->set_value (midi_to_control (last_value + offset + 1));
350                                                 } else {
351                                                         controllable->set_value (midi_to_control (last_value - offset + 1));
352                                                 }
353                                                 break;
354                                         case Enc_2:
355                                                 if (msg->value > 0x40) {
356                                                         controllable->set_value (midi_to_control (last_value - (0x7f - msg->value) + 1));
357                                                 } else {
358                                                         controllable->set_value (midi_to_control (last_value + offset + 1));
359                                                 }
360                                                 break;
361                                         case Enc_B:
362                                                 if (msg->value > 0x40) {
363                                                         controllable->set_value (midi_to_control (last_value + offset + 1));
364                                                 } else {
365                                                         controllable->set_value (midi_to_control (last_value - (0x40 - offset)));
366                                                 }
367                                                 break;
368                                         default:
369                                                 break;
370                                 }
371                                 DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("MIDI CC %1 value %2  %3\n", (int) msg->controller_number, (int) last_value, current_uri() ));
372
373                         }
374                 } else {
375                         if (msg->value > 64.0f) {
376                                 controllable->set_value (1);
377                                 DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("Midi CC %1 value 1  %2\n", (int) msg->controller_number, current_uri()));
378                         } else {
379                                 controllable->set_value (0);
380                                 DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("Midi CC %1 value 0  %2\n", (int) msg->controller_number, current_uri()));
381                         }
382                 }
383
384                 last_value = (MIDI::byte) (control_to_midi(controllable->get_value())); // to prevent feedback fights
385         }
386 }
387
388 void
389 MIDIControllable::midi_sense_program_change (Parser &, MIDI::byte msg)
390 {
391         if (!controllable) {
392                 if (lookup_controllable ()) {
393                         return;
394                 }
395         }
396         if (msg == control_additional) {
397
398                 if (!controllable->is_toggle()) {
399                         controllable->set_value (1.0);
400                         DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("MIDI program %1 value 1.0  %3\n", (int) msg, current_uri() ));
401                 } else  {
402                         float new_value = controllable->get_value() > 0.5f ? 0.0f : 1.0f;
403                         controllable->set_value (new_value);
404                         DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("MIDI program %1 value %2  %3\n", (int) msg, (float) new_value, current_uri()));
405                 }
406         }
407
408         last_value = (MIDI::byte) (controllable->get_value() * 127.0); // to prevent feedback fights
409 }
410
411 void
412 MIDIControllable::midi_sense_pitchbend (Parser &, pitchbend_t pb)
413 {
414         if (!controllable) {
415                 if (lookup_controllable ()) {
416                         return;
417                 }
418         }
419
420         if (!controllable->is_toggle()) {
421                 controllable->set_value (midi_to_control (pb));
422                 DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("MIDI pitchbend %1 value %2  %3\n", (int) control_channel, (float) midi_to_control (pb), current_uri() ));
423         } else {
424                 if (pb > 8065.0f) {
425                         controllable->set_value (1);
426                         DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("Midi pitchbend %1 value 1  %2\n", (int) control_channel, current_uri()));
427                 } else {
428                         controllable->set_value (0);
429                         DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("Midi pitchbend %1 value 0  %2\n", (int) control_channel, current_uri()));
430                 }
431         }
432
433         last_value = control_to_midi (controllable->get_value ());
434 }
435
436 void
437 MIDIControllable::midi_receiver (Parser &, MIDI::byte *msg, size_t /*len*/)
438 {
439         /* we only respond to channel messages */
440
441         if ((msg[0] & 0xF0) < 0x80 || (msg[0] & 0xF0) > 0xE0) {
442                 return;
443         }
444
445         _surface->check_used_event(msg[0], msg[1]);
446         bind_midi ((channel_t) (msg[0] & 0xf), eventType (msg[0] & 0xF0), msg[1]);
447
448         if (controllable) {
449                 controllable->LearningFinished ();
450         }
451 }
452
453 void
454 MIDIControllable::rpn_value_change (Parser&, uint16_t rpn, float val)
455 {
456         if (control_rpn == rpn) {
457                 if (controllable) {
458                         controllable->set_value (val);
459                 }
460         }
461 }
462
463 void
464 MIDIControllable::nrpn_value_change (Parser&, uint16_t nrpn, float val)
465 {
466         if (control_nrpn == nrpn) {
467                 if (controllable) {
468                         controllable->set_value (val);
469                 }
470         }
471 }
472
473 void
474 MIDIControllable::rpn_change (Parser&, uint16_t rpn, int dir)
475 {
476         if (control_rpn == rpn) {
477                 if (controllable) {
478                         /* XXX how to increment/decrement ? */
479                         // controllable->set_value (val);
480                 }
481         }
482 }
483
484 void
485 MIDIControllable::nrpn_change (Parser&, uint16_t nrpn, int dir)
486 {
487         if (control_nrpn == nrpn) {
488                 if (controllable) {
489                         /* XXX how to increment/decrement ? */
490                         // controllable->set_value (val);
491                 }
492         }
493 }
494
495 void
496 MIDIControllable::bind_rpn_value (channel_t chn, uint16_t rpn)
497 {
498         int chn_i = chn;
499         drop_external_control ();
500         control_rpn = rpn;
501         control_channel = chn;
502         _parser.channel_rpn[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::rpn_value_change, this, _1, _2, _3));
503 }
504
505 void
506 MIDIControllable::bind_nrpn_value (channel_t chn, uint16_t nrpn)
507 {
508         int chn_i = chn;
509         drop_external_control ();
510         control_nrpn = nrpn;
511         control_channel = chn;
512         _parser.channel_nrpn[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::rpn_value_change, this, _1, _2, _3));
513 }
514
515 void
516 MIDIControllable::bind_nrpn_change (channel_t chn, uint16_t nrpn)
517 {
518         int chn_i = chn;
519         drop_external_control ();
520         control_nrpn = nrpn;
521         control_channel = chn;
522         _parser.channel_nrpn_change[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::rpn_change, this, _1, _2, _3));
523 }
524
525 void
526 MIDIControllable::bind_rpn_change (channel_t chn, uint16_t rpn)
527 {
528         int chn_i = chn;
529         drop_external_control ();
530         control_rpn = rpn;
531         control_channel = chn;
532         _parser.channel_rpn_change[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::nrpn_change, this, _1, _2, _3));
533 }
534
535 void
536 MIDIControllable::bind_midi (channel_t chn, eventType ev, MIDI::byte additional)
537 {
538         char buf[64];
539
540         drop_external_control ();
541
542         control_type = ev;
543         control_channel = chn;
544         control_additional = additional;
545
546         int chn_i = chn;
547         switch (ev) {
548         case MIDI::off:
549                 _parser.channel_note_off[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_note_off, this, _1, _2));
550
551                 /* if this is a togglee, connect to noteOn as well,
552                    and we'll toggle back and forth between the two.
553                 */
554
555                 if (_momentary) {
556                         _parser.channel_note_on[chn_i].connect_same_thread (midi_sense_connection[1], boost::bind (&MIDIControllable::midi_sense_note_on, this, _1, _2));
557                 }
558
559                 _control_description = "MIDI control: NoteOff";
560                 break;
561
562         case MIDI::on:
563                 _parser.channel_note_on[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_note_on, this, _1, _2));
564                 if (_momentary) {
565                         _parser.channel_note_off[chn_i].connect_same_thread (midi_sense_connection[1], boost::bind (&MIDIControllable::midi_sense_note_off, this, _1, _2));
566                 }
567                 _control_description = "MIDI control: NoteOn";
568                 break;
569
570         case MIDI::controller:
571                 _parser.channel_controller[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_controller, this, _1, _2));
572                 snprintf (buf, sizeof (buf), "MIDI control: Controller %d", control_additional);
573                 _control_description = buf;
574                 break;
575
576         case MIDI::program:
577                 _parser.channel_program_change[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_program_change, this, _1, _2));
578                 _control_description = "MIDI control: ProgramChange";
579                 break;
580
581         case MIDI::pitchbend:
582                 _parser.channel_pitchbend[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_pitchbend, this, _1, _2));
583                 _control_description = "MIDI control: Pitchbend";
584                 break;
585
586         default:
587                 break;
588         }
589         DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("Controlable: bind_midi: %1 on Channel %2 value %3 \n", _control_description, chn_i + 1, (int) additional));
590 }
591
592 MIDI::byte*
593 MIDIControllable::write_feedback (MIDI::byte* buf, int32_t& bufsize, bool /*force*/)
594 {
595         if (!controllable || !feedback) {
596                 return buf;
597         }
598
599         float val = controllable->get_value ();
600
601         /* Note that when sending RPN/NPRN we do two things:
602          *
603          * always send MSB first, then LSB
604          * null/reset the parameter ID after sending.
605          *
606          * this follows recommendations found online, eg. http://www.philrees.co.uk/nrpnq.htm
607          */
608
609         if (control_rpn >= 0) {
610                 if (bufsize < 13) {
611                         return buf;
612                 }
613                 int rpn_val = (int) lrintf (val * 16384.0);
614                 if (last_value == rpn_val) {
615                         return buf;
616                 }
617                 *buf++ = (0xb0) | control_channel;
618                 *buf++ = 0x62;
619                 *buf++ = (int) ((control_rpn) >> 7);
620                 *buf++ = 0x63;
621                 *buf++ = (int) (control_rpn & 0x7f);
622                 *buf++ = 0x06;
623                 *buf++ = (int) (rpn_val >> 7);
624                 *buf++ = 0x26;
625                 *buf++ = (int) (rpn_val & 0x7f);
626                 *buf++ = 0x62;
627                 *buf++ = 0x7f;
628                 *buf++ = 0x63;
629                 *buf++ = 0x7f;
630                 bufsize -= 13;
631                 last_value = rpn_val;
632                 DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("MIDI out: RPN %1 Channel %2 Value %3\n", control_rpn, (int) control_channel, val));
633                 return buf;
634         }
635
636         if (control_nrpn >= 0) {
637                 int rpn_val = (int) lrintf (val * 16384.0);
638                 if (last_value == rpn_val) {
639                         return buf;
640                 }
641                 *buf++ = (0xb0) | control_channel;
642                 *buf++ = 0x64;
643                 *buf++ = (int) ((control_rpn) >> 7);
644                 *buf++ = 0x65;
645                 *buf++ = (int) (control_rpn & 0x7f);
646                 *buf++ = 0x06;
647                 *buf++ = (int) (rpn_val >> 7);
648                 *buf++ = 0x26;
649                 *buf++ = (int) (rpn_val & 0x7f);
650                 *buf++ = 0x64;
651                 *buf++ = 0x7f;
652                 *buf++ = 0x65;
653                 *buf++ = 0x7f;
654                 last_value = rpn_val;
655                 bufsize -= 13;
656                 DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("MIDI out: NRPN %1 Channel %2 Value %3\n", control_nrpn, (int) control_channel, val));
657                 return buf;
658         }
659
660         if (control_type == none || bufsize <= 2) {
661                 return buf;
662         }
663
664         int const gm = control_to_midi (val);
665
666         if (gm == last_value) {
667                 return buf;
668         }
669
670         DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("Feedback: %1 %2\n", control_description(), current_uri()));
671
672         *buf++ = (0xF0 & control_type) | (0xF & control_channel);
673         int ev_size = 3;
674         switch (control_type) {
675         case MIDI::pitchbend:
676                 *buf++ = int (gm) & 127;
677                 *buf++ = (int (gm) >> 7) & 127;
678                 break;
679         case MIDI::program:
680                 *buf++ = control_additional; /* program number */
681                 ev_size = 2;
682                 break;
683         default:
684                 *buf++ = control_additional; /* controller number */
685                 *buf++ = gm;
686                 break;
687         }
688         DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("MIDI out: Type %1 Channel %2 Bytes %3 %4\n", (int) control_type, (int) control_channel , (int) *(buf - 2), (int) *(buf - 1)));
689
690         last_value = gm;
691         bufsize -= ev_size;
692
693         return buf;
694 }
695
696 int
697 MIDIControllable::set_state (const XMLNode& node, int /*version*/)
698 {
699         const XMLProperty* prop;
700         int xx;
701
702         if ((prop = node.property ("event")) != 0) {
703                 sscanf (prop->value().c_str(), "0x%x", &xx);
704                 control_type = (MIDI::eventType) xx;
705         } else {
706                 return -1;
707         }
708
709         if ((prop = node.property ("channel")) != 0) {
710                 sscanf (prop->value().c_str(), "%d", &xx);
711                 control_channel = (MIDI::channel_t) xx;
712         } else {
713                 return -1;
714         }
715
716         if ((prop = node.property ("additional")) != 0) {
717                 sscanf (prop->value().c_str(), "0x%x", &xx);
718                 control_additional = (MIDI::byte) xx;
719         } else {
720                 return -1;
721         }
722
723         if ((prop = node.property ("feedback")) != 0) {
724                 feedback = (prop->value() == "yes");
725         } else {
726                 feedback = true; // default
727         }
728
729         bind_midi (control_channel, control_type, control_additional);
730
731         return 0;
732 }
733
734 XMLNode&
735 MIDIControllable::get_state ()
736 {
737         char buf[32];
738
739         XMLNode* node = new XMLNode ("MIDIControllable");
740
741         if (_current_uri.empty()) {
742                 node->add_property ("id", controllable->id().to_s());
743         } else {
744                 node->add_property ("uri", _current_uri);
745         }
746
747         if (controllable) {
748                 snprintf (buf, sizeof(buf), "0x%x", (int) control_type);
749                 node->add_property ("event", buf);
750                 snprintf (buf, sizeof(buf), "%d", (int) control_channel);
751                 node->add_property ("channel", buf);
752                 snprintf (buf, sizeof(buf), "0x%x", (int) control_additional);
753                 node->add_property ("additional", buf);
754                 node->add_property ("feedback", (feedback ? "yes" : "no"));
755         }
756
757         return *node;
758 }
759
760 /** @return the maximum value for a control value transmitted
761  *  using a given MIDI::eventType.
762  */
763 int
764 MIDIControllable::max_value_for_type () const
765 {
766         /* XXX: this is not complete */
767
768         if (control_type == MIDI::pitchbend) {
769                 return 16383;
770         }
771
772         return 127;
773 }