OSC: set Automation mode for fader
[ardour.git] / libs / surfaces / osc / osc.cc
index eef4e065fb52dfa2a3774a024aa0fe8eed283a89..f97bdf55a57db12fe7312d3b59784974cdcf880c 100644 (file)
@@ -100,6 +100,7 @@ OSC::OSC (Session& s, uint32_t port)
        , default_gainmode (0)
        , tick (true)
        , bank_dirty (false)
+       , scrub_speed (0)
        , gui (0)
 {
        _instance = this;
@@ -421,6 +422,9 @@ OSC::register_callbacks()
                REGISTER_CALLBACK (serv, "/goto_start", "f", goto_start);
                REGISTER_CALLBACK (serv, "/goto_end", "", goto_end);
                REGISTER_CALLBACK (serv, "/goto_end", "f", goto_end);
+               REGISTER_CALLBACK (serv, "/scrub", "f", scrub);
+               REGISTER_CALLBACK (serv, "/jog", "f", jog);
+               REGISTER_CALLBACK (serv, "/jog/mode", "f", jog_mode);
                REGISTER_CALLBACK (serv, "/rewind", "", rewind);
                REGISTER_CALLBACK (serv, "/rewind", "f", rewind);
                REGISTER_CALLBACK (serv, "/ffwd", "", ffwd);
@@ -702,7 +706,7 @@ OSC::listen_to_route (boost::shared_ptr<Stripable> strip, lo_address addr)
 
        OSCSurface *s = get_surface(addr);
        uint32_t ssid = get_sid (strip, addr);
-       OSCRouteObserver* o = new OSCRouteObserver (strip, addr, ssid, s->gainmode, s->feedback);
+       OSCRouteObserver* o = new OSCRouteObserver (strip, addr, ssid, s);
        route_observers.push_back (o);
 
        strip->DropReferences.connect (*this, MISSING_INVALIDATOR, boost::bind (&OSC::route_lost, this, boost::weak_ptr<Stripable> (strip)), this);
@@ -858,6 +862,10 @@ OSC::catchall (const char *path, const char* types, lo_arg **argv, int argc, lo_
 
        len = strlen (path);
 
+       if (strstr (path, "/automation")) {
+               ret = set_automation (path, len, argv, argc, msg);
+
+       } else
        if (len >= 17 && !strcmp (&path[len-15], "/#current_value")) {
                current_value_query (path, len, argv, argc, msg);
                ret = 0;
@@ -874,7 +882,6 @@ OSC::catchall (const char *path, const char* types, lo_arg **argv, int argc, lo_
                        std::string action_path = path;
 
                        access_action (action_path.substr(15));
-                       std::cout << "access_action path = " << action_path.substr(15) << "\n";
                }
 
                ret = 0;
@@ -1231,8 +1238,8 @@ OSC::routes_list (lo_message msg)
                                lo_message_add_int32 (reply, s->rec_enable_control()->get_value());
                        }
 
-                       //Automatically listen to routes listed
-                       listen_to_route(r, get_address (msg));
+                       //Automatically listen to stripables listed
+                       listen_to_route(s, get_address (msg));
 
                        lo_send_message (get_address (msg), "#reply", reply);
                        lo_message_free (reply);
@@ -1282,6 +1289,7 @@ OSC::refresh_surface (lo_message msg)
 {
        if (address_only) {
                // get rid of all surfaces and observers.
+               // needs change to only clear those for this address on all ports
                clear_devices();
        }
        OSCSurface *s = get_surface(get_address (msg));
@@ -1326,6 +1334,17 @@ OSC::clear_devices ()
                        delete so;
                }
        }
+       // delete cue observers
+       for (CueObservers::iterator x = cue_observers.begin(); x != cue_observers.end();) {
+               OSCCueObserver* co;
+               if ((co = dynamic_cast<OSCCueObserver*>(*x)) != 0) {
+                       delete *x;
+                       x = cue_observers.erase (x);
+               } else {
+                       ++x;
+               }
+       }
+
        // clear out surfaces
        _surface.clear();
 }
@@ -1506,6 +1525,13 @@ OSC::_recalcbanks ()
                } else {
                        _set_bank (sur->bank, addr);
                }
+               if (sur->no_clear) {
+                       // This surface uses /strip/list tell it routes have changed
+                       lo_message reply;
+                       reply = lo_message_new ();
+                       lo_send_message (addr, "/strip/list", reply);
+                       lo_message_free (reply);
+               }
        }
 }
 
@@ -1576,7 +1602,7 @@ OSC::_set_bank (uint32_t bank_start, lo_address addr)
                // top bank is always filled if there are enough strips for at least one bank
                bank_start = (uint32_t)((s->nstrips - b_size) + 1);
        }
-       //save bank in case we have had to change it
+       //save bank after bank limit checks
        s->bank = bank_start;
 
        if (s->feedback[0] || s->feedback[1]) {
@@ -1594,8 +1620,7 @@ OSC::_set_bank (uint32_t bank_start, lo_address addr)
                }
        }
        // light bankup or bankdown buttons if it is possible to bank in that direction
-       if (s->feedback[4]) {
-               // these two messages could be bundled
+       if (s->feedback[4] && !s->no_clear) {
                lo_message reply;
                reply = lo_message_new ();
                if ((s->bank > (s->nstrips - s->bank_size)) || (s->nstrips < s->bank_size)) {
@@ -1732,6 +1757,176 @@ OSC::record_enabled (lo_message msg)
        lo_message_free (reply);
 }
 
+int
+OSC::scrub (float delta, lo_message msg)
+{
+       if (!session) return -1;
+
+       scrub_place = session->transport_frame ();
+
+       float speed;
+
+       int64_t now = ARDOUR::get_microseconds ();
+       int64_t diff = now - scrub_time;
+       if (diff > 35000) {
+               // speed 1 (or 0 if jog wheel supports touch)
+               speed = delta;
+       } else if ((diff > 20000) && (fabs(scrub_speed) == 1)) {
+               // add some hysteresis to stop excess speed jumps
+               speed = delta;
+       } else {
+               speed = (int)(delta * 2);
+       }
+       scrub_time = now;
+       if (scrub_speed == speed) {
+               // Already at that speed no change
+               return 0;
+       }
+       scrub_speed = speed;
+
+       if (speed > 0) {
+               if (speed == 1) {
+                       session->request_transport_speed (.5);
+               } else {
+                       session->request_transport_speed (1);
+               }
+       } else if (speed < 0) {
+               if (speed == -1) {
+                       session->request_transport_speed (-.5);
+               } else {
+                       session->request_transport_speed (-1);
+               }
+       } else {
+               session->request_transport_speed (0);
+       }
+
+       return 0;
+}
+
+int
+OSC::jog (float delta, lo_message msg)
+{
+       if (!session) return -1;
+
+       OSCSurface *s = get_surface(get_address (msg));
+
+       string path = "/jog/mode/name";
+       switch(s->jogmode)
+       {
+               case JOG  :
+                       text_message (path, "Jog", get_address (msg));
+                       if (delta) {
+                               jump_by_seconds (delta / 5);
+                       }
+                       break;
+               case SCRUB:
+                       text_message (path, "Scrub", get_address (msg));
+                       scrub (delta, msg);
+                       break;
+               case SHUTTLE:
+                       text_message (path, "Shuttle", get_address (msg));
+                       if (delta) {
+                               double speed = get_transport_speed ();
+                               set_transport_speed (speed + (delta / 8));
+                       } else {
+                               set_transport_speed (0);
+                       }
+                       break;
+               case SCROLL:
+                       text_message (path, "Scroll", get_address (msg));
+                       if (delta > 0) {
+                               access_action ("Editor/scroll-forward");
+                       } else if (delta < 0) {
+                               access_action ("Editor/scroll-backward");
+                       }
+                       break;
+               case TRACK:
+                       text_message (path, "Track", get_address (msg));
+                       if (delta > 0) {
+                               set_bank (s->bank + 1, msg);
+                       } else if (delta < 0) {
+                               set_bank (s->bank - 1, msg);
+                       }
+                       break;
+               case BANK:
+                       text_message (path, "Bank", get_address (msg));
+                       if (delta > 0) {
+                               bank_up (msg);
+                       } else if (delta < 0) {
+                               bank_down (msg);
+                       }
+                       break;
+               case NUDGE:
+                       text_message (path, "Nudge", get_address (msg));
+                       if (delta > 0) {
+                               access_action ("Common/nudge-playhead-forward");
+                       } else if (delta < 0) {
+                               access_action ("Common/nudge-playhead-backward");
+                       }
+                       break;
+               case MARKER:
+                       text_message (path, "Marker", get_address (msg));
+                       if (delta > 0) {
+                               next_marker ();
+                       } else if (delta < 0) {
+                               prev_marker ();
+                       }
+                       break;
+               default:
+                       break;
+
+       }
+       return 0;
+
+}
+
+int
+OSC::jog_mode (float mode, lo_message msg)
+{
+       if (!session) return -1;
+
+       OSCSurface *s = get_surface(get_address (msg));
+
+       switch((uint32_t)mode)
+       {
+               case JOG  :
+                       s->jogmode = JOG;
+                       break;
+               case SCRUB:
+                       s->jogmode = SCRUB;
+                       break;
+               case SHUTTLE:
+                       s->jogmode = SHUTTLE;
+                       break;
+               case SCROLL:
+                       s->jogmode = SCROLL;
+                       break;
+               case TRACK:
+                       s->jogmode = TRACK;
+                       break;
+               case BANK:
+                       s->jogmode = BANK;
+                       break;
+               case NUDGE:
+                       s->jogmode = NUDGE;
+                       break;
+               case MARKER:
+                       s->jogmode = MARKER;
+                       break;
+               default:
+                       PBD::warning << "Jog Mode: " << mode << " is not valid." << endmsg;
+                       break;
+       lo_message reply = lo_message_new ();
+       lo_message_add_int32 (reply, s->jogmode);
+       lo_send_message (get_address(msg), "/jog/mode", reply);
+       lo_message_free (reply);
+
+       }
+       jog (0, msg);
+       return 0;
+
+}
+
 // master and monitor calls
 int
 OSC::master_set_gain (float dB)
@@ -1917,7 +2112,8 @@ OSC::route_get_sends(lo_message msg) {
                        lo_message_add_int32(reply, p->active() ? 1 : 0);
                }
        }
-       // if used dedicated message path to identify this reply in async operation. Naming it #reply wont help the client to identify the content.
+       // if used dedicated message path to identify this reply in async operation.
+       // Naming it #reply wont help the client to identify the content.
        lo_send_message(get_address (msg), "/strip/sends", reply);
 
        lo_message_free(reply);
@@ -1979,13 +2175,107 @@ OSC::route_get_receives(lo_message msg) {
                }
        }
 
-       // I have used a dedicated message path to identify this reply in async operation. Naming it #reply wont help the client to identify the content.
+       // I have used a dedicated message path to identify this reply in async operation.
+       // Naming it #reply wont help the client to identify the content.
        lo_send_message(get_address (msg), "/strip/receives", reply);
        lo_message_free(reply);
        return 0;
 }
 
 // strip calls
+
+int
+OSC::set_automation (const char *path, size_t len, lo_arg **argv, int argc, lo_message msg)
+{
+       if (!session) return -1;
+
+       int ret = 1;
+       OSCSurface *sur = get_surface(get_address (msg));
+       boost::shared_ptr<Stripable> strp = boost::shared_ptr<Stripable>();
+       uint32_t ctr = 0;
+       uint32_t aut = 0;
+
+       //parse path first to find stripable
+       if (!strncmp (path, "/strip/", 7)) {
+               // find ssid and stripable
+               if (argc > 1) {
+                       strp = get_strip (argv[0]->i, get_address (msg));
+                       if (argv[1]->f) {
+                               aut = (int)argv[1]->f;
+                       } else {
+                               aut = argv[1]->i;
+                       }
+               } else {
+                       uint32_t ssid = atoi (&(strrchr (path, '/' ))[1]);
+                       strp = get_strip (ssid, get_address (msg));
+                       if (argv[0]->f) {
+                               aut = (int)argv[0]->f;
+                       } else {
+                               aut = argv[0]->i;
+                       }
+               }
+               ctr = 7;
+       } else if (!strncmp (path, "/select/", 8)) {
+               if (sur->expand_enable && sur->expand) {
+                       strp = get_strip (sur->expand, get_address (msg));
+               } else {
+                       strp = ControlProtocol::first_selected_stripable();
+               }
+               if (argv[0]->f) {
+                       aut = (int)argv[0]->f;
+               } else {
+                       aut = argv[0]->i;
+               }
+               ctr = 8;
+       } else {
+               return ret;
+       }
+       if (strp) {
+               if ((!strncmp (&path[ctr], "fader", 5)) || (!strncmp (&path[ctr], "gain", 4))) {
+                       if (strp->gain_control ()) {
+                               switch (aut) {
+                                       case 0:
+                                               strp->gain_control()->set_automation_state (ARDOUR::AutoState::Off);
+                                               ret = 0;
+                                               break;
+                                       case 'm':
+                                               strp->gain_control()->set_automation_state (ARDOUR::AutoState::Off);
+                                               ret = 0;
+                                               break;
+                                       case 1:
+                                               strp->gain_control()->set_automation_state (ARDOUR::AutoState::Play);
+                                               ret = 0;
+                                               break;
+                                       case 'p':
+                                               strp->gain_control()->set_automation_state (ARDOUR::AutoState::Play);
+                                               ret = 0;
+                                               break;
+                                       case 2:
+                                               strp->gain_control()->set_automation_state (ARDOUR::AutoState::Write);
+                                               ret = 0;
+                                               break;
+                                       case 'w':
+                                               strp->gain_control()->set_automation_state (ARDOUR::AutoState::Write);
+                                               ret = 0;
+                                               break;
+                                       case 3:
+                                               strp->gain_control()->set_automation_state (ARDOUR::AutoState::Touch);
+                                               ret = 0;
+                                               break;
+                                       case 't':
+                                               strp->gain_control()->set_automation_state (ARDOUR::AutoState::Touch);
+                                               ret = 0;
+                                               break;
+                                       default:
+                                               break;
+                               }
+                       }
+               }
+       }
+
+       return ret;
+}
+
 int
 OSC::route_mute (int ssid, int yn, lo_message msg)
 {
@@ -3545,6 +3835,18 @@ OSC::periodic (void)
                }
        }
 
+       if (scrub_speed != 0) {
+               // for those jog wheels that don't have 0 on release (touch), time out.
+               int64_t now = ARDOUR::get_microseconds ();
+               int64_t diff = now - scrub_time;
+               if (diff > 120000) {
+                       scrub_speed = 0;
+                       session->request_transport_speed (0);
+                       // locate to the place PH was at last tick
+                       session->request_locate (scrub_place, false);
+               }
+       }
+
        for (GlobalObservers::iterator x = global_observers.begin(); x != global_observers.end(); x++) {
 
                OSCGlobalObserver* go;
@@ -3655,23 +3957,23 @@ XMLNode&
 OSC::get_state ()
 {
        XMLNode& node (ControlProtocol::get_state());
-       node.add_property("debugmode", (int) _debugmode); // TODO: enum2str
-       node.add_property ("address-only", address_only);
-       node.add_property ("remote-port", remote_port);
-       node.add_property ("banksize", default_banksize);
-       node.add_property ("striptypes", default_strip);
-       node.add_property ("feedback", default_feedback);
-       node.add_property ("gainmode", default_gainmode);
+       node.set_property ("debugmode", (int32_t) _debugmode); // TODO: enum2str
+       node.set_property ("address-only", address_only);
+       node.set_property ("remote-port", remote_port);
+       node.set_property ("banksize", default_banksize);
+       node.set_property ("striptypes", default_strip);
+       node.set_property ("feedback", default_feedback);
+       node.set_property ("gainmode", default_gainmode);
        if (_surface.size()) {
                XMLNode* config = new XMLNode (X_("Configurations"));
                for (uint32_t it = 0; it < _surface.size(); ++it) {
                        OSCSurface* sur = &_surface[it];
                        XMLNode* devnode = new XMLNode (X_("Configuration"));
-                       devnode->add_property (X_("url"), sur->remote_url);
-                       devnode->add_property (X_("bank-size"), sur->bank_size);
-                       devnode->add_property (X_("strip-types"), sur->strip_types.to_ulong());
-                       devnode->add_property (X_("feedback"), sur->feedback.to_ulong());
-                       devnode->add_property (X_("gainmode"), sur->gainmode);
+                       devnode->set_property (X_("url"), sur->remote_url);
+                       devnode->set_property (X_("bank-size"), sur->bank_size);
+                       devnode->set_property (X_("strip-types"), (uint64_t)sur->strip_types.to_ulong());
+                       devnode->set_property (X_("feedback"), (uint64_t)sur->feedback.to_ulong());
+                       devnode->set_property (X_("gainmode"), sur->gainmode);
                        config->add_child_nocopy (*devnode);
                }
                node.add_child_nocopy (*config);
@@ -3685,68 +3987,48 @@ OSC::set_state (const XMLNode& node, int version)
        if (ControlProtocol::set_state (node, version)) {
                return -1;
        }
-       XMLProperty const * p = node.property (X_("debugmode"));
-       if (p) {
-               _debugmode = OSCDebugMode (PBD::atoi(p->value ()));
-       }
-       p = node.property (X_("address-only"));
-       if (p) {
-               address_only = OSCDebugMode (PBD::atoi(p->value ()));
-       }
-       p = node.property (X_("remote-port"));
-       if (p) {
-               remote_port = p->value ();
-       }
-       p = node.property (X_("banksize"));
-       if (p) {
-               default_banksize = OSCDebugMode (PBD::atoi(p->value ()));
-       }
-       p = node.property (X_("striptypes"));
-       if (p) {
-               default_strip = OSCDebugMode (PBD::atoi(p->value ()));
-       }
-       p = node.property (X_("feedback"));
-       if (p) {
-               default_feedback = OSCDebugMode (PBD::atoi(p->value ()));
-       }
-       p = node.property (X_("gainmode"));
-       if (p) {
-               default_gainmode = OSCDebugMode (PBD::atoi(p->value ()));
+       int32_t debugmode;
+       if (node.get_property (X_("debugmode"), debugmode)) {
+               _debugmode = OSCDebugMode (debugmode);
        }
+
+       node.get_property (X_("address-only"), address_only);
+       node.get_property (X_("remote-port"), remote_port);
+       node.get_property (X_("banksize"), default_banksize);
+       node.get_property (X_("striptypes"), default_strip);
+       node.get_property (X_("feedback"), default_feedback);
+       node.get_property (X_("gainmode"), default_gainmode);
+
        XMLNode* cnode = node.child (X_("Configurations"));
 
        if (cnode) {
                XMLNodeList const& devices = cnode->children();
                for (XMLNodeList::const_iterator d = devices.begin(); d != devices.end(); ++d) {
-                       XMLProperty const * prop = (*d)->property (X_("url"));
-                       if (prop) {
-                               OSCSurface s;
-                               bank_dirty = true;
-                               s.remote_url = prop->value();
-                               prop = (*d)->property (X_("bank-size"));
-                               if (prop) {
-                                       s.bank_size = atoi (prop->value().c_str());
-                               }
-                               prop = (*d)->property (X_("strip-types"));
-                               if (prop) {
-                                       s.strip_types = atoi (prop->value().c_str());
-                               }
-                               prop = (*d)->property (X_("feedback"));
-                               if (prop) {
-                                       s.feedback = atoi (prop->value().c_str());
-                               }
-                               prop = (*d)->property (X_("gainmode"));
-                               if (prop) {
-                                       s.gainmode = atoi (prop->value().c_str());
-                               }
-                               s.bank = 1;
-                               s.sel_obs = 0;
-                               s.expand = 0;
-                               s.expand_enable = false;
-                               s.strips = get_sorted_stripables(s.strip_types, s.cue);
-                               s.nstrips = s.strips.size();
-                               _surface.push_back (s);
+                       OSCSurface s;
+                       if (!(*d)->get_property (X_("url"), s.remote_url)) {
+                               continue;
+                       }
+
+                       bank_dirty = true;
+
+                       (*d)->get_property (X_("bank-size"), s.bank_size);
+
+                       uint64_t bits;
+                       if ((*d)->get_property (X_ ("strip-types"), bits)) {
+                               s.strip_types = bits;
+                       }
+                       if ((*d)->get_property (X_("feedback"), bits)) {
+                               s.feedback = bits;
                        }
+                       (*d)->get_property (X_("gainmode"), s.gainmode);
+
+                       s.bank = 1;
+                       s.sel_obs = 0;
+                       s.expand = 0;
+                       s.expand_enable = false;
+                       s.strips = get_sorted_stripables (s.strip_types, s.cue);
+                       s.nstrips = s.strips.size ();
+                       _surface.push_back (s);
                }
        }
        global_init = true;
@@ -3812,17 +4094,13 @@ OSC::get_sorted_stripables(std::bitset<32> types, bool cue)
                                                sorted.push_back (s);
                                        }
                                }
-                       } else
-                       if (types[3] && (s->presentation_info().flags() & PresentationInfo::MidiBus)) {
+                       } else if (types[3] && (s->presentation_info().flags() & PresentationInfo::MidiBus)) {
                                sorted.push_back (s);
-                       } else
-                       if (types[4] && (s->presentation_info().flags() & PresentationInfo::VCA)) {
+                       } else if (types[4] && (s->presentation_info().flags() & PresentationInfo::VCA)) {
                                sorted.push_back (s);
-                       } else
-                       if (types[8] && (s->presentation_info().flags() & PresentationInfo::Selected)) {
+                       } else if (types[8] && (s->is_selected())) {
                                sorted.push_back (s);
-                       } else
-                       if (types[9] && (s->presentation_info().flags() & PresentationInfo::Hidden)) {
+                       } else if (types[9] && (s->presentation_info().flags() & PresentationInfo::Hidden)) {
                                sorted.push_back (s);
                        }
                }
@@ -3849,18 +4127,24 @@ OSC::cue_parse (const char *path, const char* types, lo_arg **argv, int argc, lo
                ret = 0;
        }
        else if (!strncmp (path, "/cue/connect", 12)) {
-               // switch to next Aux bus
-               cue_set (0, msg);
+               // Connect to default Aux bus
+               if ((!argc) || argv[0]->i) {
+                       cue_set (1, msg);
+               }
                ret = 0;
        }
        else if (!strncmp (path, "/cue/next_aux", 13)) {
                // switch to next Aux bus
-               cue_next (msg);
+               if ((!argc) || argv[0]->i) {
+                       cue_next (msg);
+               }
                ret = 0;
        }
        else if (!strncmp (path, "/cue/previous_aux", 17)) {
                // switch to previous Aux bus
-               cue_previous (msg);
+               if ((!argc) || argv[0]->i) {
+                       cue_previous (msg);
+               }
                ret = 0;
        }
        else if (!strncmp (path, "/cue/send/fader/", 16) && strlen (path) > 16) {
@@ -3882,12 +4166,6 @@ OSC::cue_parse (const char *path, const char* types, lo_arg **argv, int argc, lo
                ret = 0;
        }
 
-       if ((ret && _debugmode == Unhandled)) {
-               debugmsg (_("Unhandled OSC cue message"), path, types, argv, argc);
-       } else if ((!ret && _debugmode == All)) {
-               debugmsg (_("OSC cue"), path, types, argv, argc);
-       }
-
        return ret;
 }
 
@@ -3906,12 +4184,19 @@ OSC::_cue_set (uint32_t aux, lo_address addr)
        s->feedback = 0;
        s->gainmode = 1;
        s->cue = true;
-       s->aux = aux;
        s->strips = get_sorted_stripables(s->strip_types, s->cue);
 
        s->nstrips = s->strips.size();
+
+       if (aux < 1) {
+               aux = 1;
+       } else if (aux > s->nstrips) {
+               aux = s->nstrips;
+       }
+       s->aux = aux;
+
        // get rid of any old CueObsevers for this address
-       cueobserver_connections.drop_connections ();
+       //cueobserver_connections.drop_connections ();
        CueObservers::iterator x;
        for (x = cue_observers.begin(); x != cue_observers.end();) {
 
@@ -3959,6 +4244,11 @@ int
 OSC::cue_next (lo_message msg)
 {
        OSCSurface *s = get_surface(get_address (msg));
+
+       if (!s->cue) {
+               cue_set (1, msg);
+               return 0;
+       }
        if (s->aux < s->nstrips) {
                cue_set (s->aux + 1, msg);
        } else {
@@ -3971,6 +4261,10 @@ int
 OSC::cue_previous (lo_message msg)
 {
        OSCSurface *s = get_surface(get_address (msg));
+       if (!s->cue) {
+               cue_set (1, msg);
+               return 0;
+       }
        if (s->aux > 1) {
                cue_set (s->aux - 1, msg);
        }