Part 1 of loading 2.X sessions; some things work, some things don't, hacks a-plenty.
[ardour.git] / libs / surfaces / generic_midi / midicontrollable.cc
index 1cf32f11f60d6a6fce5c1c16fc715902987393a2..8275d0849c465f16e5f716cac354576d2c2f43b2 100644 (file)
@@ -15,7 +15,6 @@
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id: midicontrollable.cc 629 2006-06-21 23:01:03Z paul $
 */
 
 #include <cstdio> /* for sprintf, sigh */
@@ -24,6 +23,7 @@
 #include <pbd/xml++.h>
 #include <midi++/port.h>
 #include <midi++/channel.h>
+#include <ardour/automation_control.h>
 
 #include "midicontrollable.h"
 
@@ -32,13 +32,11 @@ using namespace MIDI;
 using namespace PBD;
 using namespace ARDOUR;
 
-bool MIDIControllable::_send_feedback = false;
-
 MIDIControllable::MIDIControllable (Port& p, Controllable& c, bool is_bistate)
        : controllable (c), _port (p), bistate (is_bistate)
 {
        setting = false;
-       last_written = 0; // got a better idea ?
+       last_value = 0; // got a better idea ?
        control_type = none;
        _control_description = "MIDI Control: none";
        control_additional = (byte) -1;
@@ -115,6 +113,40 @@ MIDIControllable::drop_external_control ()
        control_additional = (byte) -1;
 }
 
+float
+MIDIControllable::control_to_midi(float val)
+{
+       float control_min = 0.0f;
+       float control_max = 1.0f;
+       ARDOUR::AutomationControl* ac = dynamic_cast<ARDOUR::AutomationControl*>(&controllable);
+       if (ac) {
+               control_min = ac->parameter().min();
+               control_max = ac->parameter().max();
+       }
+
+       const float control_range = control_max - control_min;
+       const float midi_range    = 127.0f; // TODO: NRPN etc.
+
+       return (val - control_min) / control_range * midi_range;
+}
+
+float
+MIDIControllable::midi_to_control(float val)
+{
+       float control_min = 0.0f;
+       float control_max = 1.0f;
+       ARDOUR::AutomationControl* ac = dynamic_cast<ARDOUR::AutomationControl*>(&controllable);
+       if (ac) {
+               control_min = ac->parameter().min();
+               control_max = ac->parameter().max();
+       }
+
+       const float control_range = control_max - control_min;
+       const float midi_range    = 127.0f; // TODO: NRPN etc.
+
+       return val / midi_range * control_range + control_min;
+}
+
 void 
 MIDIControllable::midi_sense_note_on (Parser &p, EventTwoBytes *tb) 
 {
@@ -128,7 +160,7 @@ MIDIControllable::midi_sense_note_off (Parser &p, EventTwoBytes *tb)
 }
 
 void
-MIDIControllable::midi_sense_note (Parser &p, EventTwoBytes *msg, bool is_on)
+MIDIControllable::midi_sense_note (Parser &, EventTwoBytes *msg, bool is_on)
 {
        if (!bistate) {
                controllable.set_value (msg->note_number/127.0);
@@ -143,14 +175,20 @@ MIDIControllable::midi_sense_note (Parser &p, EventTwoBytes *msg, bool is_on)
                        controllable.set_value (is_on ? 1 : 0);
                }
        }
+
+       last_value = (MIDI::byte) (controllable.get_value() * 127.0); // to prevent feedback fights
 }
 
 void
 MIDIControllable::midi_sense_controller (Parser &, EventTwoBytes *msg)
 {
+       if (controllable.touching()) {
+               return; // to prevent feedback fights when e.g. dragging a UI slider
+       }
+
        if (control_additional == msg->controller_number) {
                if (!bistate) {
-                       controllable.set_value (msg->value/127.0);
+                       controllable.set_value (midi_to_control(msg->value));
                } else {
                        if (msg->value > 64.0) {
                                controllable.set_value (1);
@@ -158,31 +196,35 @@ MIDIControllable::midi_sense_controller (Parser &, EventTwoBytes *msg)
                                controllable.set_value (0);
                        }
                }
+
+               last_value = (MIDI::byte) (control_to_midi(controllable.get_value())); // to prevent feedback fights
        }
 }
 
 void
-MIDIControllable::midi_sense_program_change (Parser &p, byte msg)
+MIDIControllable::midi_sense_program_change (Parser &, byte msg)
 {
        /* XXX program change messages make no sense for bistates */
 
        if (!bistate) {
                controllable.set_value (msg/127.0);
+               last_value = (MIDI::byte) (controllable.get_value() * 127.0); // to prevent feedback fights
        } 
 }
 
 void
-MIDIControllable::midi_sense_pitchbend (Parser &p, pitchbend_t pb)
+MIDIControllable::midi_sense_pitchbend (Parser &, pitchbend_t pb)
 {
        /* pitchbend messages make no sense for bistates */
 
        /* XXX gack - get rid of assumption about typeof pitchbend_t */
 
        controllable.set_value ((pb/(float) SHRT_MAX));
+       last_value = (MIDI::byte) (controllable.get_value() * 127.0); // to prevent feedback fights
 }                      
 
 void
-MIDIControllable::midi_receiver (Parser &p, byte *msg, size_t len)
+MIDIControllable::midi_receiver (Parser &, byte *msg, size_t /*len*/)
 {
        /* we only respond to channel messages */
 
@@ -288,29 +330,29 @@ MIDIControllable::send_feedback ()
 {
        byte msg[3];
 
-       if (setting || !_send_feedback || control_type == none) {
+       if (setting || !feedback || control_type == none) {
                return;
        }
-       
+
        msg[0] = (control_type & 0xF0) | (control_channel & 0xF); 
        msg[1] = control_additional;
-       msg[2] = (byte) (controllable.get_value() * 127.0f);
+       msg[2] = (byte) (control_to_midi(controllable.get_value()));
 
-       _port.write (msg, 3);
+       _port.write (msg, 3, 0);
 }
 
 MIDI::byte*
-MIDIControllable::write_feedback (MIDI::byte* buf, int32_t& bufsize, bool force)
+MIDIControllable::write_feedback (MIDI::byte* buf, int32_t& bufsize, bool /*force*/)
 {
-       if (control_type != none &&_send_feedback && bufsize > 2) {
-
-               MIDI::byte gm = (MIDI::byte) (controllable.get_value() * 127.0);
+       if (control_type != none && feedback && bufsize > 2) {
+               
+               MIDI::byte gm = (MIDI::byte) (control_to_midi(controllable.get_value()));
                
-               if (gm != last_written) {
+               if (gm != last_value) {
                        *buf++ = (0xF0 & control_type) | (0xF & control_channel);
                        *buf++ = control_additional; /* controller number */
                        *buf++ = gm;
-                       last_written = gm;
+                       last_value = gm;
                        bufsize -= 3;
                }
        }
@@ -319,7 +361,7 @@ MIDIControllable::write_feedback (MIDI::byte* buf, int32_t& bufsize, bool force)
 }
 
 int 
-MIDIControllable::set_state (const XMLNode& node)
+MIDIControllable::set_state (const XMLNode& node, int version)
 {
        const XMLProperty* prop;
        int xx;
@@ -345,6 +387,12 @@ MIDIControllable::set_state (const XMLNode& node)
                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;
@@ -362,6 +410,7 @@ MIDIControllable::get_state ()
        node.add_property ("channel", buf);
        snprintf (buf, sizeof(buf), "0x%x", (int) control_additional);
        node.add_property ("additional", buf);
+       node.add_property ("feedback", (feedback ? "yes" : "no"));
 
        return node;
 }