Sanitize "well-known" ctrl API
[ardour.git] / libs / surfaces / osc / osc_select_observer.cc
index 7bd274643270dadd4e79815e1965ac65474343b7..151b00b6e05a8aef81bcdcb7da7b4498996ff061 100644 (file)
@@ -35,6 +35,8 @@
 #include "osc.h"
 #include "osc_select_observer.h"
 
+#include <glibmm.h>
+
 #include "pbd/i18n.h"
 
 using namespace std;
@@ -47,8 +49,10 @@ OSCSelectObserver::OSCSelectObserver (boost::shared_ptr<Stripable> s, lo_address
        ,gainmode (gm)
        ,feedback (fb)
        ,nsends (0)
+       ,_last_gain (0.0)
 {
        addr = lo_address_new (lo_address_get_hostname(a) , lo_address_get_port(a));
+       as = ARDOUR::Off;
 
        if (feedback[0]) { // buttons are separate feedback
                _strip->PropertyChanged.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::name_changed, this, boost::lambda::_1), OSC::instance());
@@ -97,13 +101,9 @@ OSCSelectObserver::OSCSelectObserver (boost::shared_ptr<Stripable> s, lo_address
        }
 
        if (feedback[1]) { // level controls
-               if (gainmode) {
-                       _strip->gain_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::gain_message, this, X_("/select/fader"), _strip->gain_control()), OSC::instance());
-                       gain_message ("/select/fader", _strip->gain_control());
-               } else {
-                       _strip->gain_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::gain_message, this, X_("/select/gain"), _strip->gain_control()), OSC::instance());
-                       gain_message ("/select/gain", _strip->gain_control());
-               }
+               _strip->gain_control()->alist()->automation_state_changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::gain_automation, this), OSC::instance());
+               _strip->gain_control()->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::gain_message, this), OSC::instance());
+               gain_automation ();
 
                boost::shared_ptr<Controllable> trim_controllable = boost::dynamic_pointer_cast<Controllable>(_strip->trim_control());
                if (trim_controllable) {
@@ -177,56 +177,55 @@ OSCSelectObserver::OSCSelectObserver (boost::shared_ptr<Stripable> s, lo_address
 
 OSCSelectObserver::~OSCSelectObserver ()
 {
-
        strip_connections.drop_connections ();
        // all strip buttons should be off and faders 0 and etc.
-       clear_strip ("/select/expand", 0);
        if (feedback[0]) { // buttons are separate feedback
+               send_float ("/select/expand", 0);
                text_message ("/select/name", " ");
                text_message ("/select/comment", " ");
-               clear_strip ("/select/mute", 0);
-               clear_strip ("/select/solo", 0);
-               clear_strip ("/select/recenable", 0);
-               clear_strip ("/select/record_safe", 0);
-               clear_strip ("/select/monitor_input", 0);
-               clear_strip ("/select/monitor_disk", 0);
-               clear_strip ("/select/polarity", 0);
-               clear_strip ("/select/n_inputs", 0);
-               clear_strip ("/select/n_outputs", 0);
+               send_float ("/select/mute", 0);
+               send_float ("/select/solo", 0);
+               send_float ("/select/recenable", 0);
+               send_float ("/select/record_safe", 0);
+               send_float ("/select/monitor_input", 0);
+               send_float ("/select/monitor_disk", 0);
+               send_float ("/select/polarity", 0);
+               send_float ("/select/n_inputs", 0);
+               send_float ("/select/n_outputs", 0);
        }
        if (feedback[1]) { // level controls
                if (gainmode) {
-                       clear_strip ("/select/fader", 0);
+                       send_float ("/select/fader", 0);
                } else {
-                       clear_strip ("/select/gain", -193);
+                       send_float ("/select/gain", -193);
                }
-               clear_strip ("/select/trimdB", 0);
-               clear_strip ("/select/pan_stereo_position", 0.5);
-               clear_strip ("/select/pan_stereo_width", 1);
+               send_float ("/select/trimdB", 0);
+               send_float ("/select/pan_stereo_position", 0.5);
+               send_float ("/select/pan_stereo_width", 1);
        }
        if (feedback[9]) {
-               clear_strip ("/select/signal", 0);
+               send_float ("/select/signal", 0);
        }
        if (feedback[7]) {
                if (gainmode) {
-                       clear_strip ("/select/meter", 0);
+                       send_float ("/select/meter", 0);
                } else {
-                       clear_strip ("/select/meter", -193);
+                       send_float ("/select/meter", -193);
                }
        }else if (feedback[8]) {
-               clear_strip ("/select/meter", 0);
+               send_float ("/select/meter", 0);
        }
        if (feedback[13]) { // Well known controls
-               clear_strip ("/select/pan_elevation_position", 0);
-               clear_strip ("/select/pan_frontback_position", .5);
-               clear_strip ("/select/pan_lfe_control", 0);
-               clear_strip ("/select/comp_enable", 0);
-               clear_strip ("/select/comp_threshold", 0);
-               clear_strip ("/select/comp_speed", 0);
-               clear_strip ("/select/comp_mode", 0);
+               send_float ("/select/pan_elevation_position", 0);
+               send_float ("/select/pan_frontback_position", .5);
+               send_float ("/select/pan_lfe_control", 0);
+               send_float ("/select/comp_enable", 0);
+               send_float ("/select/comp_threshold", 0);
+               send_float ("/select/comp_speed", 0);
+               send_float ("/select/comp_mode", 0);
                text_message ("/select/comp_mode_name", " ");
                text_message ("/select/comp_speed_name", " ");
-               clear_strip ("/select/comp_makeup", 0);
+               send_float ("/select/comp_makeup", 0);
        }
        send_end();
        eq_end();
@@ -242,27 +241,27 @@ OSCSelectObserver::send_init()
        do {
                sends = false;
                if (_strip->send_level_controllable (nsends)) {
-                       _strip->send_level_controllable(nsends)->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::send_gain, this, nsends, _strip->send_level_controllable(nsends)), OSC::instance());
+                       _strip->send_level_controllable(nsends)->Changed.connect (send_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::send_gain, this, nsends, _strip->send_level_controllable(nsends)), OSC::instance());
                        send_timeout.push_back (0);
                        send_gain (nsends, _strip->send_level_controllable(nsends));
                        sends = true;
                }
 
                if (_strip->send_enable_controllable (nsends)) {
-                       _strip->send_enable_controllable(nsends)->Changed.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::enable_message_with_id, this, X_("/select/send_enable"), nsends + 1, _strip->send_enable_controllable(nsends)), OSC::instance());
+                       _strip->send_enable_controllable(nsends)->Changed.connect (send_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::enable_message_with_id, this, X_("/select/send_enable"), nsends + 1, _strip->send_enable_controllable(nsends)), OSC::instance());
                        enable_message_with_id ("/select/send_enable", nsends + 1, _strip->send_enable_controllable(nsends));
                        sends = true;
                } else if (sends) {
                        boost::shared_ptr<Route> r = boost::dynamic_pointer_cast<Route> (_strip);
                        if (!r) {
                                // should never get here
-                               clear_strip_with_id ("/select/send_enable", nsends + 1, 0);
+                               send_float_with_id ("/select/send_enable", nsends + 1, 0);
                        }
                        boost::shared_ptr<Send> snd = boost::dynamic_pointer_cast<Send> (r->nth_send(nsends));
                        if (snd) {
                                boost::shared_ptr<Processor> proc = boost::dynamic_pointer_cast<Processor> (snd);
-                               proc->ActiveChanged.connect (strip_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::send_enable, this, X_("/select/send_enable"), nsends + 1, proc), OSC::instance());
-                               clear_strip_with_id ("/select/send_enable", nsends + 1, proc->enabled());
+                               proc->ActiveChanged.connect (send_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::send_enable, this, X_("/select/send_enable"), nsends + 1, proc), OSC::instance());
+                               send_float_with_id ("/select/send_enable", nsends + 1, proc->enabled());
                        }
                }
                // this should get signalled by the route the send goes to, (TODO)
@@ -282,12 +281,12 @@ OSCSelectObserver::send_end ()
        send_connections.drop_connections ();
        for (uint32_t i = 1; i <= nsends; i++) {
                if (gainmode) {
-                       clear_strip_with_id ("/select/send_fader", i, 0);
+                       send_float_with_id ("/select/send_fader", i, 0);
                } else {
-                       clear_strip_with_id ("/select/send_gain", i, -193);
+                       send_float_with_id ("/select/send_gain", i, -193);
                }
                // next enable
-               clear_strip_with_id ("/select/send_enable", i, 0);
+               send_float_with_id ("/select/send_enable", i, 0);
                // next name
                text_with_id ("/select/send_name", i, " ");
        }
@@ -354,6 +353,13 @@ OSCSelectObserver::tick ()
                        }
                        gain_timeout--;
                }
+
+               if (as == ARDOUR::Play ||  as == ARDOUR::Touch) {
+                       if(_last_gain != _strip->gain_control()->get_value()) {
+                               _last_gain = _strip->gain_control()->get_value();
+                                       gain_message ();
+                       }
+               }
        }
        if (feedback[13]) {
                for (uint32_t i = 0; i < send_timeout.size(); i++) {
@@ -385,9 +391,9 @@ OSCSelectObserver::name_changed (const PBD::PropertyChange& what_changed)
                //spit out the comment at the same time
                text_message ("/select/comment", route->comment());
                // lets tell the surface how many inputs this strip has
-               clear_strip ("/select/n_inputs", (float) route->n_inputs().n_total());
+               send_float ("/select/n_inputs", (float) route->n_inputs().n_total());
                // lets tell the surface how many outputs this strip has
-               clear_strip ("/select/n_outputs", (float) route->n_outputs().n_total());
+               send_float ("/select/n_outputs", (float) route->n_outputs().n_total());
        }
 }
 
@@ -408,9 +414,9 @@ OSCSelectObserver::enable_message (string path, boost::shared_ptr<Controllable>
 {
        float val = controllable->get_value();
        if (val) {
-               clear_strip (path, 1);
+               send_float (path, 1);
        } else {
-               clear_strip (path, 0);
+               send_float (path, 0);
        }
 
 }
@@ -437,9 +443,9 @@ OSCSelectObserver::enable_message_with_id (string path, uint32_t id, boost::shar
 {
        float val = controllable->get_value();
        if (val) {
-               clear_strip_with_id (path, id, 1);
+               send_float_with_id (path, id, 1);
        } else {
-               clear_strip_with_id (path, id, 0);
+               send_float_with_id (path, id, 0);
        }
 }
 
@@ -473,8 +479,8 @@ OSCSelectObserver::monitor_status (boost::shared_ptr<Controllable> controllable)
                        input = 0;
        }
 
-       clear_strip ("/select/monitor_input", (float) input);
-       clear_strip ("/select/monitor_disk", (float) disk);
+       send_float ("/select/monitor_input", (float) input);
+       send_float ("/select/monitor_disk", (float) disk);
 }
 
 void
@@ -489,24 +495,52 @@ OSCSelectObserver::trim_message (string path, boost::shared_ptr<Controllable> co
 }
 
 void
-OSCSelectObserver::gain_message (string path, boost::shared_ptr<Controllable> controllable)
+OSCSelectObserver::gain_message ()
 {
-       lo_message msg = lo_message_new ();
+       float value = _strip->gain_control()->get_value();
 
        if (gainmode) {
-               lo_message_add_float (msg, gain_to_slider_position (controllable->get_value()));
-               text_message ("/select/name", string_compose ("%1%2%3", std::fixed, std::setprecision(2), accurate_coefficient_to_dB (controllable->get_value())));
+               text_message ("/select/name", string_compose ("%1%2%3", std::fixed, std::setprecision(2), accurate_coefficient_to_dB (value)));
                gain_timeout = 8;
+               send_float ("/select/fader", gain_to_slider_position (value));
        } else {
-               if (controllable->get_value() < 1e-15) {
-                       lo_message_add_float (msg, -200);
+               if (value < 1e-15) {
+                       send_float ("/select/gain", -200);
                } else {
-                       lo_message_add_float (msg, accurate_coefficient_to_dB (controllable->get_value()));
+                       send_float ("/select/gain", accurate_coefficient_to_dB (value));
                }
        }
+}
 
-       lo_send_message (addr, path.c_str(), msg);
-       lo_message_free (msg);
+void
+OSCSelectObserver::gain_automation ()
+{
+       float output;
+       as = _strip->gain_control()->alist()->automation_state();
+       switch (as) {
+               case ARDOUR::Off:
+                       output = 0;
+                       break;
+               case ARDOUR::Play:
+                       output = 1;
+                       break;
+               case ARDOUR::Write:
+                       output = 2;
+                       break;
+               case ARDOUR::Touch:
+                       output = 3;
+                       break;
+               default:
+                       break;
+       }
+
+       if (gainmode) {
+               send_float ("/select/fader/automation", output);
+       } else {
+               send_float ("/select/gain/automation", output);
+       }
+
+       gain_message ();
 }
 
 void
@@ -557,9 +591,9 @@ void
 OSCSelectObserver::send_enable (string path, uint32_t id, boost::shared_ptr<Processor> proc)
 {
        // with no delay value is wrong
-       usleep(10);
+       Glib::usleep(10);
 
-       clear_strip_with_id ("/select/send_enable", id, proc->enabled());
+       send_float_with_id ("/select/send_enable", id, proc->enabled());
 }
 
 void
@@ -590,10 +624,11 @@ void
 OSCSelectObserver::eq_init()
 {
        // HPF and enable are special case, rest are in bands
-       if (_strip->eq_hpf_controllable ()) {
-               _strip->eq_hpf_controllable ()->Changed.connect (eq_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/eq_hpf"), _strip->eq_hpf_controllable()), OSC::instance());
-               change_message ("/select/eq_hpf", _strip->eq_hpf_controllable());
+       if (_strip->filter_freq_controllable (true)) {
+               _strip->filter_freq_controllable (true)->Changed.connect (eq_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::change_message, this, X_("/select/eq_hpf"), _strip->filter_freq_controllable (true)), OSC::instance());
+               change_message ("/select/eq_hpf", _strip->filter_freq_controllable(true));
        }
+       // TODO LPF and LPF/HPF enable ctrls.
        if (_strip->eq_enable_controllable ()) {
                _strip->eq_enable_controllable ()->Changed.connect (eq_connections, MISSING_INVALIDATOR, boost::bind (&OSCSelectObserver::enable_message, this, X_("/select/eq_enable"), _strip->eq_enable_controllable()), OSC::instance());
                enable_message ("/select/eq_enable", _strip->eq_enable_controllable());
@@ -632,15 +667,19 @@ OSCSelectObserver::eq_end ()
 {
        //need to check feedback for [13]
        eq_connections.drop_connections ();
-       clear_strip ("/select/eq_hpf", 0);
-       clear_strip ("/select/eq_enable", 0);
+       if (_strip->filter_freq_controllable (true)) {
+               send_float ("/select/eq_hpf", 0);
+       }
+       if (_strip->eq_enable_controllable ()) {
+               send_float ("/select/eq_enable", 0);
+       }
 
        for (uint32_t i = 1; i <= _strip->eq_band_cnt (); i++) {
                text_with_id ("/select/eq_band_name", i, " ");
-               clear_strip_with_id ("/select/eq_gain", i, 0);
-               clear_strip_with_id ("/select/eq_freq", i, 0);
-               clear_strip_with_id ("/select/eq_q", i, 0);
-               clear_strip_with_id ("/select/eq_shape", i, 0);
+               send_float_with_id ("/select/eq_gain", i, 0);
+               send_float_with_id ("/select/eq_freq", i, 0);
+               send_float_with_id ("/select/eq_q", i, 0);
+               send_float_with_id ("/select/eq_shape", i, 0);
 
 
        }
@@ -663,7 +702,7 @@ OSCSelectObserver::set_path (string path, uint32_t id)
 }
 
 void
-OSCSelectObserver::clear_strip (string path, float val)
+OSCSelectObserver::send_float (string path, float val)
 {
        lo_message msg = lo_message_new ();
        lo_message_add_float (msg, val);
@@ -674,7 +713,7 @@ OSCSelectObserver::clear_strip (string path, float val)
 }
 
 void
-OSCSelectObserver::clear_strip_with_id (string path, uint32_t id, float val)
+OSCSelectObserver::send_float_with_id (string path, uint32_t id, float val)
 {
        lo_message msg = lo_message_new ();
        if (feedback[2]) {