Update classkeys to match new total LuaSignal count (windows only)
[ardour.git] / libs / surfaces / generic_midi / midicontrollable.cc
index b427bef03bcfadb3f79dd1e429c5a573bd2c972f..cbf525bd1b7b52348ff1b86d82a730471200cce7 100644 (file)
 #include <iostream>
 
 #include "pbd/error.h"
-#include "pbd/controllable_descriptor.h"
-#include "pbd/xml++.h"
-#include "pbd/stacktrace.h"
 #include "pbd/compose.h"
+#include "pbd/stacktrace.h"
+#include "pbd/types_convert.h"
+#include "pbd/xml++.h"
 
 #include "midi++/types.h" // Added by JE - 06-01-2009. All instances of 'byte' changed to 'MIDI::byte' (for clarification)
 #include "midi++/port.h"
@@ -35,7 +35,6 @@
 #include "ardour/async_midi_port.h"
 #include "ardour/automation_control.h"
 #include "ardour/midi_ui.h"
-#include "ardour/utils.h"
 #include "ardour/debug.h"
 
 #include "midicontrollable.h"
@@ -49,36 +48,41 @@ using namespace ARDOUR;
 MIDIControllable::MIDIControllable (GenericMidiControlProtocol* s, MIDI::Parser& p, bool m)
        : _surface (s)
        , controllable (0)
-       , _descriptor (0)
        , _parser (p)
        , _momentary (m)
 {
        _learned = false; /* from URI */
+       _ctltype = Ctl_Momentary;
+       _encoder = No_enc;
        setting = false;
        last_value = 0; // got a better idea ?
+       last_incoming = 256; // any out of band value
        last_controllable_value = 0.0f;
        control_type = none;
+       control_rpn = -1;
+       control_nrpn = -1;
        _control_description = "MIDI Control: none";
        control_additional = (MIDI::byte) -1;
-       feedback = true; // for now
 }
 
 MIDIControllable::MIDIControllable (GenericMidiControlProtocol* s, MIDI::Parser& p, Controllable& c, bool m)
        : _surface (s)
-       , _descriptor (0)
        , _parser (p)
        , _momentary (m)
 {
        set_controllable (&c);
-       
+
        _learned = true; /* from controllable */
+       _ctltype = Ctl_Momentary;
+       _encoder = No_enc;
        setting = false;
        last_value = 0; // got a better idea ?
        last_controllable_value = 0.0f;
        control_type = none;
+       control_rpn = -1;
+       control_nrpn = -1;
        _control_description = "MIDI Control: none";
        control_additional = (MIDI::byte) -1;
-       feedback = true; // for now
 }
 
 MIDIControllable::~MIDIControllable ()
@@ -90,9 +94,7 @@ int
 MIDIControllable::init (const std::string& s)
 {
        _current_uri = s;
-       delete _descriptor;
-       _descriptor = new ControllableDescriptor;
-       return _descriptor->set (s);
+       return 0;
 }
 
 void
@@ -111,6 +113,8 @@ void
 MIDIControllable::drop_external_control ()
 {
        midi_forget ();
+       control_rpn = -1;
+       control_nrpn = -1;
        control_type = none;
        control_additional = (MIDI::byte) -1;
 }
@@ -121,7 +125,7 @@ MIDIControllable::set_controllable (Controllable* c)
        if (c == controllable) {
                return;
        }
-       
+
        controllable_death_connection.disconnect ();
 
        controllable = c;
@@ -132,9 +136,11 @@ MIDIControllable::set_controllable (Controllable* c)
                last_controllable_value = 0.0f; // is there a better value?
        }
 
+       last_incoming = 256;
+
        if (controllable) {
                controllable->Destroyed.connect (controllable_death_connection, MISSING_INVALIDATOR,
-                                                boost::bind (&MIDIControllable::drop_controllable, this), 
+                                                boost::bind (&MIDIControllable::drop_controllable, this, _1),
                                                 MidiControlUI::instance());
        }
 }
@@ -165,9 +171,9 @@ MIDIControllable::stop_learning ()
 int
 MIDIControllable::control_to_midi (float val)
 {
-        if (controllable->is_gain_like()) {
-                return gain_to_slider_position (val) * max_value_for_type ();
-        }
+       if (controllable->is_gain_like()) {
+               return controllable->internal_to_interface (val) * max_value_for_type ();
+       }
 
        float control_min = controllable->lower ();
        float control_max = controllable->upper ();
@@ -188,27 +194,30 @@ MIDIControllable::control_to_midi (float val)
                        val = actl->internal_to_interface(val);
                }
        }
-
-       return (val - control_min) / control_range * max_value_for_type ();
+       // fiddle value of max so value doesn't jump from 125 to 127 for 1.0
+       // otherwise decrement won't work.
+       return (val - control_min) / control_range * (max_value_for_type () - 1);
 }
 
 float
 MIDIControllable::midi_to_control (int val)
 {
-        /* fiddle with MIDI value so that we get an odd number of integer steps
-           and can thus represent "middle" precisely as 0.5. this maps to
-           the range 0..+1.0
-        */
+       /* fiddle with MIDI value so that we get an odd number of integer steps
+               and can thus represent "middle" precisely as 0.5. this maps to
+               the range 0..+1.0 (0 to 126)
+       */
 
-        float fv = (val == 0 ? 0 : float (val - 1) / (max_value_for_type() - 1));
+       float fv = (val == 0 ? 0 : float (val - 1) / (max_value_for_type() - 1));
 
-        if (controllable->is_gain_like()) {
-                return slider_position_to_gain (fv);
-        }
+       if (controllable->is_gain_like()) {
+               return controllable->interface_to_internal (fv);
+       }
+       DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("Raw value %1 float %2\n", val, fv));
 
        float control_min = controllable->lower ();
        float control_max = controllable->upper ();
        float control_range = control_max - control_min;
+       DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("Min %1 Max %2 Range %3\n", control_min, control_max, control_range));
 
        AutomationControl *actl = dynamic_cast<AutomationControl*> (controllable);
        if (actl) {
@@ -237,11 +246,11 @@ MIDIControllable::midi_sense_note_off (Parser &p, EventTwoBytes *tb)
 int
 MIDIControllable::lookup_controllable()
 {
-       if (!_descriptor) {
+       if (_current_uri.empty()) {
                return -1;
        }
 
-       boost::shared_ptr<Controllable> c = _surface->lookup_controllable (*_descriptor);
+       boost::shared_ptr<Controllable> c = _surface->lookup_controllable (_current_uri);
 
        if (!c) {
                return -1;
@@ -253,29 +262,33 @@ MIDIControllable::lookup_controllable()
 }
 
 void
-MIDIControllable::drop_controllable ()
+MIDIControllable::drop_controllable (Controllable* c)
 {
-       set_controllable (0);
+       if (c == controllable) {
+               set_controllable (0);
+       }
 }
 
 void
 MIDIControllable::midi_sense_note (Parser &, EventTwoBytes *msg, bool /*is_on*/)
 {
-       if (!controllable) { 
+       if (!controllable) {
                if (lookup_controllable()) {
                        return;
                }
        }
 
+       _surface->maybe_start_touch (controllable);
+
        if (!controllable->is_toggle()) {
                if (control_additional == msg->note_number) {
-                       controllable->set_value (midi_to_control (msg->velocity));
+                       controllable->set_value (midi_to_control (msg->velocity), Controllable::UseGroup);
                        DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("Note %1 value %2  %3\n", (int) msg->note_number, (float) midi_to_control (msg->velocity), current_uri() ));
                }
        } else {
                if (control_additional == msg->note_number) {
                        float new_value = controllable->get_value() > 0.5f ? 0.0f : 1.0f;
-                       controllable->set_value (new_value);
+                       controllable->set_value (new_value, Controllable::UseGroup);
                        DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("Note %1 Value %2  %3\n", (int) msg->note_number, (float) new_value, current_uri()));
                }
        }
@@ -286,7 +299,7 @@ MIDIControllable::midi_sense_note (Parser &, EventTwoBytes *msg, bool /*is_on*/)
 void
 MIDIControllable::midi_sense_controller (Parser &, EventTwoBytes *msg)
 {
-       if (!controllable) { 
+       if (!controllable) {
                if (lookup_controllable ()) {
                        return;
                }
@@ -294,44 +307,109 @@ MIDIControllable::midi_sense_controller (Parser &, EventTwoBytes *msg)
 
        assert (controllable);
 
-       if (controllable->touching()) {
-               return; // to prevent feedback fights when e.g. dragging a UI slider
-       }
+       _surface->maybe_start_touch (controllable);
 
        if (control_additional == msg->controller_number) {
 
                if (!controllable->is_toggle()) {
+                       if (get_encoder() == No_enc) {
+                               float new_value = msg->value;
+                               float max_value = max(last_controllable_value, new_value);
+                               float min_value = min(last_controllable_value, new_value);
+                               float range = max_value - min_value;
+                               float threshold = (float) _surface->threshold ();
+
+                               bool const in_sync = (
+                                       range < threshold &&
+                                       controllable->get_value() <= midi_to_control(max_value) &&
+                                       controllable->get_value() >= midi_to_control(min_value)
+                                       );
+
+                               /* If the surface is not motorised, we try to prevent jumps when
+                                  the MIDI controller and controllable are out of sync.
+                                  There might be a better way of doing this.
+                               */
+
+                               if (in_sync || _surface->motorised ()) {
+                                       controllable->set_value (midi_to_control (new_value), Controllable::UseGroup);
+                               }
+                               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() ));
+
+                               last_controllable_value = new_value;
+                       } else {
+                               int offset = (msg->value & 0x3f);
+                               switch (get_encoder()) {
+                                       case Enc_L:
+                                               if (msg->value > 0x40) {
+                                                       controllable->set_value (midi_to_control (last_value - offset + 1), Controllable::UseGroup);
+                                               } else {
+                                                       controllable->set_value (midi_to_control (last_value + offset + 1), Controllable::UseGroup);
+                                               }
+                                               break;
+                                       case Enc_R:
+                                               if (msg->value > 0x40) {
+                                                       controllable->set_value (midi_to_control (last_value + offset + 1), Controllable::UseGroup);
+                                               } else {
+                                                       controllable->set_value (midi_to_control (last_value - offset + 1), Controllable::UseGroup);
+                                               }
+                                               break;
+                                       case Enc_2:
+                                               if (msg->value > 0x40) {
+                                                       controllable->set_value (midi_to_control (last_value - (0x7f - msg->value) + 1), Controllable::UseGroup);
+                                               } else {
+                                                       controllable->set_value (midi_to_control (last_value + offset + 1), Controllable::UseGroup);
+                                               }
+                                               break;
+                                       case Enc_B:
+                                               if (msg->value > 0x40) {
+                                                       controllable->set_value (midi_to_control (last_value + offset + 1), Controllable::UseGroup);
+                                               } else {
+                                                       controllable->set_value (midi_to_control (last_value - (0x40 - offset)), Controllable::UseGroup);
+                                               }
+                                               break;
+                                       default:
+                                               break;
+                               }
+                               DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("MIDI CC %1 value %2  %3\n", (int) msg->controller_number, (int) last_value, current_uri() ));
 
-                       float new_value = msg->value;
-                       float max_value = max(last_controllable_value, new_value);
-                       float min_value = min(last_controllable_value, new_value);
-                       float range = max_value - min_value;
-                       float threshold = (float) _surface->threshold ();
-
-                       bool const in_sync = (
-                               range < threshold &&
-                               controllable->get_value() <= midi_to_control(max_value) &&
-                               controllable->get_value() >= midi_to_control(min_value)
-                               );
-
-                       /* If the surface is not motorised, we try to prevent jumps when
-                          the MIDI controller and controllable are out of sync.
-                          There might be a better way of doing this.
-                       */
-
-                       if (in_sync || _surface->motorised ()) {
-                               controllable->set_value (midi_to_control (new_value));
                        }
-                       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() ));
-
-                       last_controllable_value = new_value;
                } else {
-                       if (msg->value > 64.0f) {
-                               controllable->set_value (1);
-                               DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("Midi CC %1 value 1  %2\n", (int) msg->controller_number, current_uri()));
-                       } else {
-                               controllable->set_value (0);
-                               DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("Midi CC %1 value 0  %2\n", (int) msg->controller_number, current_uri()));
+
+                       switch (get_ctltype()) {
+                       case Ctl_Dial:
+                               /* toggle value whenever direction of knob motion changes */
+                               if (last_incoming > 127) {
+                                       /* relax ... first incoming message */
+                               } else {
+                                       if (msg->value > last_incoming) {
+                                               controllable->set_value (1.0, Controllable::UseGroup);
+                                       } else {
+                                               controllable->set_value (0.0, Controllable::UseGroup);
+                                       }
+                                       DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("dial Midi CC %1 value 1  %2\n", (int) msg->controller_number, current_uri()));
+                               }
+                               last_incoming = msg->value;
+                               break;
+                       case Ctl_Momentary:
+                               /* toggle it if over 64, otherwise leave it alone. This behaviour that works with buttons which send a value > 64 each
+                                * time they are pressed.
+                                */
+                               if (msg->value >= 0x40) {
+                                       controllable->set_value (controllable->get_value() >= 0.5 ? 0.0 : 1.0, Controllable::UseGroup);
+                                       DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("toggle Midi CC %1 value 1  %2\n", (int) msg->controller_number, current_uri()));
+                               }
+                               break;
+                       case Ctl_Toggle:
+                               /* toggle if value is over 64, otherwise turn it off. This is behaviour designed for buttons which send a value > 64 when pressed,
+                                  maintain state (i.e. they know they were pressed) and then send zero the next time.
+                               */
+                               if (msg->value >= 0x40) {
+                                       controllable->set_value (controllable->get_value() >= 0.5 ? 0.0 : 1.0, Controllable::UseGroup);
+                               } else {
+                                       controllable->set_value (0.0, Controllable::NoGroup);
+                                       DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("Midi CC %1 value 0  %2\n", (int) msg->controller_number, current_uri()));
+                                       break;
+                               }
                        }
                }
 
@@ -342,19 +420,24 @@ MIDIControllable::midi_sense_controller (Parser &, EventTwoBytes *msg)
 void
 MIDIControllable::midi_sense_program_change (Parser &, MIDI::byte msg)
 {
-       if (!controllable) { 
+       if (!controllable) {
                if (lookup_controllable ()) {
                        return;
                }
        }
 
-       if (!controllable->is_toggle()) {
-               controllable->set_value (midi_to_control (msg));
-               DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("MIDI program %1 value %2  %3\n", (int) msg, (float) midi_to_control (msg), current_uri() ));
-       } else if (msg == control_additional) {
-               float new_value = controllable->get_value() > 0.5f ? 0.0f : 1.0f;
-               controllable->set_value (new_value);
-               DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("MIDI program %1 value %2  %3\n", (int) msg, (float) new_value, current_uri()));
+       _surface->maybe_start_touch (controllable);
+
+       if (msg == control_additional) {
+
+               if (!controllable->is_toggle()) {
+                       controllable->set_value (1.0, Controllable::UseGroup);
+                       DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("MIDI program %1 value 1.0  %3\n", (int) msg, current_uri() ));
+               } else  {
+                       float new_value = controllable->get_value() > 0.5f ? 0.0f : 1.0f;
+                       controllable->set_value (new_value, Controllable::UseGroup);
+                       DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("MIDI program %1 value %2  %3\n", (int) msg, (float) new_value, current_uri()));
+               }
        }
 
        last_value = (MIDI::byte) (controllable->get_value() * 127.0); // to prevent feedback fights
@@ -363,19 +446,25 @@ MIDIControllable::midi_sense_program_change (Parser &, MIDI::byte msg)
 void
 MIDIControllable::midi_sense_pitchbend (Parser &, pitchbend_t pb)
 {
-       if (!controllable) { 
+       if (!controllable) {
                if (lookup_controllable ()) {
                        return;
                }
        }
 
+       _surface->maybe_start_touch (controllable);
+
        if (!controllable->is_toggle()) {
-               controllable->set_value (midi_to_control (pb));
+               controllable->set_value (midi_to_control (pb), Controllable::UseGroup);
                DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("MIDI pitchbend %1 value %2  %3\n", (int) control_channel, (float) midi_to_control (pb), current_uri() ));
        } else {
-               float new_value = controllable->get_value() > 0.5f ? 0.0f : 1.0f;
-               controllable->set_value (new_value);
-               DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("MIDI pitchbend %1 value %2  %3\n", (int) control_channel, (float) new_value, current_uri()));
+               if (pb > 8065.0f) {
+                       controllable->set_value (1, Controllable::UseGroup);
+                       DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("Midi pitchbend %1 value 1  %2\n", (int) control_channel, current_uri()));
+               } else {
+                       controllable->set_value (0, Controllable::UseGroup);
+                       DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("Midi pitchbend %1 value 0  %2\n", (int) control_channel, current_uri()));
+               }
        }
 
        last_value = control_to_midi (controllable->get_value ());
@@ -390,6 +479,7 @@ MIDIControllable::midi_receiver (Parser &, MIDI::byte *msg, size_t /*len*/)
                return;
        }
 
+       _surface->check_used_event(msg[0], msg[1]);
        bind_midi ((channel_t) (msg[0] & 0xf), eventType (msg[0] & 0xF0), msg[1]);
 
        if (controllable) {
@@ -397,6 +487,88 @@ MIDIControllable::midi_receiver (Parser &, MIDI::byte *msg, size_t /*len*/)
        }
 }
 
+void
+MIDIControllable::rpn_value_change (Parser&, uint16_t rpn, float val)
+{
+       if (control_rpn == rpn) {
+               if (controllable) {
+                       controllable->set_value (val, Controllable::UseGroup);
+               }
+       }
+}
+
+void
+MIDIControllable::nrpn_value_change (Parser&, uint16_t nrpn, float val)
+{
+       if (control_nrpn == nrpn) {
+               if (controllable) {
+                       controllable->set_value (val, Controllable::UseGroup);
+               }
+       }
+}
+
+void
+MIDIControllable::rpn_change (Parser&, uint16_t rpn, int dir)
+{
+       if (control_rpn == rpn) {
+               if (controllable) {
+                       /* XXX how to increment/decrement ? */
+                       // controllable->set_value (val);
+               }
+       }
+}
+
+void
+MIDIControllable::nrpn_change (Parser&, uint16_t nrpn, int dir)
+{
+       if (control_nrpn == nrpn) {
+               if (controllable) {
+                       /* XXX how to increment/decrement ? */
+                       // controllable->set_value (val);
+               }
+       }
+}
+
+void
+MIDIControllable::bind_rpn_value (channel_t chn, uint16_t rpn)
+{
+       int chn_i = chn;
+       drop_external_control ();
+       control_rpn = rpn;
+       control_channel = chn;
+       _parser.channel_rpn[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::rpn_value_change, this, _1, _2, _3));
+}
+
+void
+MIDIControllable::bind_nrpn_value (channel_t chn, uint16_t nrpn)
+{
+       int chn_i = chn;
+       drop_external_control ();
+       control_nrpn = nrpn;
+       control_channel = chn;
+       _parser.channel_nrpn[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::rpn_value_change, this, _1, _2, _3));
+}
+
+void
+MIDIControllable::bind_nrpn_change (channel_t chn, uint16_t nrpn)
+{
+       int chn_i = chn;
+       drop_external_control ();
+       control_nrpn = nrpn;
+       control_channel = chn;
+       _parser.channel_nrpn_change[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::rpn_change, this, _1, _2, _3));
+}
+
+void
+MIDIControllable::bind_rpn_change (channel_t chn, uint16_t rpn)
+{
+       int chn_i = chn;
+       drop_external_control ();
+       control_rpn = rpn;
+       control_channel = chn;
+       _parser.channel_rpn_change[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::nrpn_change, this, _1, _2, _3));
+}
+
 void
 MIDIControllable::bind_midi (channel_t chn, eventType ev, MIDI::byte additional)
 {
@@ -419,7 +591,7 @@ MIDIControllable::bind_midi (channel_t chn, eventType ev, MIDI::byte additional)
 
                if (_momentary) {
                        _parser.channel_note_on[chn_i].connect_same_thread (midi_sense_connection[1], boost::bind (&MIDIControllable::midi_sense_note_on, this, _1, _2));
-               } 
+               }
 
                _control_description = "MIDI control: NoteOff";
                break;
@@ -431,7 +603,7 @@ MIDIControllable::bind_midi (channel_t chn, eventType ev, MIDI::byte additional)
                }
                _control_description = "MIDI control: NoteOn";
                break;
-               
+
        case MIDI::controller:
                _parser.channel_controller[chn_i].connect_same_thread (midi_sense_connection[0], boost::bind (&MIDIControllable::midi_sense_controller, this, _1, _2));
                snprintf (buf, sizeof (buf), "MIDI control: Controller %d", control_additional);
@@ -457,11 +629,76 @@ MIDIControllable::bind_midi (channel_t chn, eventType ev, MIDI::byte additional)
 MIDI::byte*
 MIDIControllable::write_feedback (MIDI::byte* buf, int32_t& bufsize, bool /*force*/)
 {
-       if (!controllable || control_type == none || !feedback || bufsize <= 2) {
+       if (!controllable || !_surface->get_feedback ()) {
+               return buf;
+       }
+
+       float val = controllable->get_value ();
+
+       /* Note that when sending RPN/NPRN we do two things:
+        *
+        * always send MSB first, then LSB
+        * null/reset the parameter ID after sending.
+        *
+        * this follows recommendations found online, eg. http://www.philrees.co.uk/nrpnq.htm
+        */
+
+       if (control_rpn >= 0) {
+               if (bufsize < 13) {
+                       return buf;
+               }
+               int rpn_val = (int) lrintf (val * 16384.0);
+               if (last_value == rpn_val) {
+                       return buf;
+               }
+               *buf++ = (0xb0) | control_channel;
+               *buf++ = 0x62;
+               *buf++ = (int) ((control_rpn) >> 7);
+               *buf++ = 0x63;
+               *buf++ = (int) (control_rpn & 0x7f);
+               *buf++ = 0x06;
+               *buf++ = (int) (rpn_val >> 7);
+               *buf++ = 0x26;
+               *buf++ = (int) (rpn_val & 0x7f);
+               *buf++ = 0x62;
+               *buf++ = 0x7f;
+               *buf++ = 0x63;
+               *buf++ = 0x7f;
+               bufsize -= 13;
+               last_value = rpn_val;
+               DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("MIDI out: RPN %1 Channel %2 Value %3\n", control_rpn, (int) control_channel, val));
+               return buf;
+       }
+
+       if (control_nrpn >= 0) {
+               int rpn_val = (int) lrintf (val * 16384.0);
+               if (last_value == rpn_val) {
+                       return buf;
+               }
+               *buf++ = (0xb0) | control_channel;
+               *buf++ = 0x64;
+               *buf++ = (int) ((control_rpn) >> 7);
+               *buf++ = 0x65;
+               *buf++ = (int) (control_rpn & 0x7f);
+               *buf++ = 0x06;
+               *buf++ = (int) (rpn_val >> 7);
+               *buf++ = 0x26;
+               *buf++ = (int) (rpn_val & 0x7f);
+               *buf++ = 0x64;
+               *buf++ = 0x7f;
+               *buf++ = 0x65;
+               *buf++ = 0x7f;
+               last_value = rpn_val;
+               bufsize -= 13;
+               DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("MIDI out: NRPN %1 Channel %2 Value %3\n", control_nrpn, (int) control_channel, val));
                return buf;
        }
-       
-       int const gm = control_to_midi (controllable->get_value());
+
+       if (control_type == none || bufsize <= 2) {
+               return buf;
+       }
+
+       int const gm = control_to_midi (val);
 
        if (gm == last_value) {
                return buf;
@@ -470,12 +707,16 @@ MIDIControllable::write_feedback (MIDI::byte* buf, int32_t& bufsize, bool /*forc
         DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("Feedback: %1 %2\n", control_description(), current_uri()));
 
        *buf++ = (0xF0 & control_type) | (0xF & control_channel);
-       
+       int ev_size = 3;
        switch (control_type) {
        case MIDI::pitchbend:
                *buf++ = int (gm) & 127;
                *buf++ = (int (gm) >> 7) & 127;
                break;
+       case MIDI::program:
+               *buf++ = control_additional; /* program number */
+               ev_size = 2;
+               break;
        default:
                *buf++ = control_additional; /* controller number */
                *buf++ = gm;
@@ -484,7 +725,7 @@ MIDIControllable::write_feedback (MIDI::byte* buf, int32_t& bufsize, bool /*forc
        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)));
 
        last_value = gm;
-       bufsize -= 3;
+       bufsize -= ev_size;
 
        return buf;
 }
@@ -492,36 +733,29 @@ MIDIControllable::write_feedback (MIDI::byte* buf, int32_t& bufsize, bool /*forc
 int
 MIDIControllable::set_state (const XMLNode& node, int /*version*/)
 {
-       const XMLProperty* prop;
        int xx;
 
-       if ((prop = node.property ("event")) != 0) {
-               sscanf (prop->value().c_str(), "0x%x", &xx);
+       std::string str;
+       if (node.get_property ("event", str)) {
+               sscanf (str.c_str(), "0x%x", &xx);
                control_type = (MIDI::eventType) xx;
        } else {
                return -1;
        }
 
-       if ((prop = node.property ("channel")) != 0) {
-               sscanf (prop->value().c_str(), "%d", &xx);
-               control_channel = (MIDI::channel_t) xx;
+       if (node.get_property ("channel", xx)) {
+               control_channel = xx;
        } else {
                return -1;
        }
 
-       if ((prop = node.property ("additional")) != 0) {
-               sscanf (prop->value().c_str(), "0x%x", &xx);
+       if (node.get_property ("additional", str)) {
+               sscanf (str.c_str(), "0x%x", &xx);
                control_additional = (MIDI::byte) xx;
        } else {
                return -1;
        }
 
-       if ((prop = node.property ("feedback")) != 0) {
-               feedback = (prop->value() == "yes");
-       } else {
-               feedback = true; // default
-       }
-
        bind_midi (control_channel, control_type, control_additional);
 
        return 0;
@@ -535,19 +769,17 @@ MIDIControllable::get_state ()
        XMLNode* node = new XMLNode ("MIDIControllable");
 
        if (_current_uri.empty()) {
-                node->add_property ("id", controllable->id().to_s());
+               node->set_property ("id", controllable->id ());
        } else {
-               node->add_property ("uri", _current_uri);
-        }
+               node->set_property ("uri", _current_uri);
+       }
 
        if (controllable) {
                snprintf (buf, sizeof(buf), "0x%x", (int) control_type);
-               node->add_property ("event", buf);
-               snprintf (buf, sizeof(buf), "%d", (int) control_channel);
-               node->add_property ("channel", buf);
+               node->set_property ("event", (const char *)buf);
+               node->set_property ("channel", (int16_t)control_channel);
                snprintf (buf, sizeof(buf), "0x%x", (int) control_additional);
-               node->add_property ("additional", buf);
-               node->add_property ("feedback", (feedback ? "yes" : "no"));
+               node->set_property ("additional", (const char *)buf);
        }
 
        return *node;
@@ -560,7 +792,7 @@ int
 MIDIControllable::max_value_for_type () const
 {
        /* XXX: this is not complete */
-       
+
        if (control_type == MIDI::pitchbend) {
                return 16383;
        }