For now, only use the multi-threaded process code if we are
[ardour.git] / libs / ardour / session.cc
index ecdd9bca8d5a846cdcfc8ed818b1d38c12df7e28..f2343e1013af059a50a7a03fffd07df3b940da2a 100644 (file)
@@ -45,6 +45,8 @@
 #include "pbd/file_utils.h"
 #include "pbd/convert.h"
 #include "pbd/strsplit.h"
+#include "pbd/strsplit.h"
+#include "pbd/unwind.h"
 
 #include "ardour/amp.h"
 #include "ardour/analyser.h"
@@ -86,6 +88,7 @@
 #include "ardour/recent_sessions.h"
 #include "ardour/region_factory.h"
 #include "ardour/return.h"
+#include "ardour/route_graph.h"
 #include "ardour/route_group.h"
 #include "ardour/send.h"
 #include "ardour/session.h"
@@ -128,10 +131,13 @@ PBD::Signal0<void> Session::AutoBindingOff;
 PBD::Signal2<void,std::string, std::string> Session::Exported;
 PBD::Signal1<int,boost::shared_ptr<Playlist> > Session::AskAboutPlaylistDeletion;
 PBD::Signal0<void> Session::Quit;
+PBD::Signal0<void> Session::FeedbackDetected;
+PBD::Signal0<void> Session::SuccessfulGraphSort;
 
 static void clean_up_session_event (SessionEvent* ev) { delete ev; }
 const SessionEvent::RTeventCallback Session::rt_cleanup (clean_up_session_event);
 
+/** @param snapshot_name Snapshot name, without .ardour prefix */
 Session::Session (AudioEngine &eng,
                   const string& fullpath,
                   const string& snapshot_name,
@@ -147,7 +153,6 @@ Session::Session (AudioEngine &eng,
        , _post_transport_work (0)
        , _send_timecode_update (false)
        , _all_route_group (new RouteGroup (*this, "all"))
-       , route_graph (new Graph(*this))
        , routes (new RouteList)
        , _total_free_4k_blocks (0)
        , _bundles (new BundleList)
@@ -163,6 +168,13 @@ Session::Session (AudioEngine &eng,
 {
        _locations = new Locations (*this);
 
+       if (how_many_dsp_threads () > 1) {
+               /* For now, only create the graph if we are using >1 DSP threads, as
+                  it is a bit slower than the old code with 1 thread.
+               */
+               _process_graph.reset (new Graph (*this));
+       }
+
        playlists.reset (new SessionPlaylists);
 
        _all_route_group->set_active (true, this);
@@ -213,6 +225,9 @@ Session::Session (AudioEngine &eng,
 
 Session::~Session ()
 {
+#ifdef PT_TIMING       
+       ST.dump ("ST.dump");
+#endif 
        destroy ();
 }
 
@@ -346,7 +361,7 @@ Session::when_engine_running ()
 
        /* every time we reconnect, recompute worst case output latencies */
 
-       _engine.Running.connect_same_thread (*this, boost::bind (&Session::set_worst_io_latencies, this));
+       _engine.Running.connect_same_thread (*this, boost::bind (&Session::initialize_latencies, this));
 
        if (synced_to_jack()) {
                _engine.transport_stop ();
@@ -532,105 +547,207 @@ Session::when_engine_running ()
        hookup_io ();
 
        if (_is_new && !no_auto_connect()) {
-
                Glib::Mutex::Lock lm (AudioEngine::instance()->process_lock());
+               auto_connect_master_bus ();
+       }
 
-               /* don't connect the master bus outputs if there is a monitor bus */
+       _state_of_the_state = StateOfTheState (_state_of_the_state & ~(CannotSave|Dirty));
 
-               if (_master_out && Config->get_auto_connect_standard_busses() && !_monitor_out) {
+        /* update latencies */
 
-                       /* if requested auto-connect the outputs to the first N physical ports.
-                        */
+        initialize_latencies ();
 
-                       uint32_t limit = _master_out->n_outputs().n_total();
+       /* hook us up to the engine */
 
-                       for (uint32_t n = 0; n < limit; ++n) {
-                               Port* p = _master_out->output()->nth (n);
-                               string connect_to;
-                               if (outputs[p->type()].size() > n) {
-                                       connect_to = outputs[p->type()][n];
-                               }
+       BootMessage (_("Connect to engine"));
+       _engine.set_session (this);
+}
 
-                               if (!connect_to.empty() && p->connected_to (connect_to) == false) {
-                                       if (_master_out->output()->connect (p, connect_to, this)) {
-                                               error << string_compose (_("cannot connect master output %1 to %2"), n, connect_to)
-                                                     << endmsg;
-                                               break;
-                                       }
-                               }
+void
+Session::auto_connect_master_bus ()
+{
+       if (!_master_out || !Config->get_auto_connect_standard_busses() || _monitor_out) {
+               return;
+       }
+               
+       /* if requested auto-connect the outputs to the first N physical ports.
+        */
+       
+       uint32_t limit = _master_out->n_outputs().n_total();
+       vector<string> outputs[DataType::num_types];
+       
+       for (uint32_t i = 0; i < DataType::num_types; ++i) {
+               _engine.get_physical_outputs (DataType (DataType::Symbol (i)), outputs[i]);
+       }
+       
+       for (uint32_t n = 0; n < limit; ++n) {
+               boost::shared_ptr<Port> p = _master_out->output()->nth (n);
+               string connect_to;
+               if (outputs[p->type()].size() > n) {
+                       connect_to = outputs[p->type()][n];
+               }
+               
+               if (!connect_to.empty() && p->connected_to (connect_to) == false) {
+                       if (_master_out->output()->connect (p, connect_to, this)) {
+                               error << string_compose (_("cannot connect master output %1 to %2"), n, connect_to)
+                                     << endmsg;
+                               break;
                        }
                }
+       }
+}
 
-               if (_monitor_out) {
+void
+Session::remove_monitor_section ()
+{
+       if (!_monitor_out) {
+               return;
+       }
 
-                       /* AUDIO ONLY as of june 29th 2009, because listen semantics for anything else
-                          are undefined, at best.
-                        */
+       /* force reversion to Solo-In-Pace */
+       Config->set_solo_control_is_listen_control (false);
 
-                       /* control out listens to master bus (but ignores it
-                          under some conditions)
-                       */
+       {
+               /* Hold process lock while doing this so that we don't hear bits and
+                * pieces of audio as we work on each route.
+                */
+               
+               Glib::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
+               
+               /* Connect tracks to monitor section. Note that in an
+                  existing session, the internal sends will already exist, but we want the
+                  routes to notice that they connect to the control out specifically.
+               */
+               
+               
+               boost::shared_ptr<RouteList> r = routes.reader ();
+               PBD::Unwinder<bool> uw (ignore_route_processor_changes, true);
+               
+               for (RouteList::iterator x = r->begin(); x != r->end(); ++x) {
+                       
+                       if ((*x)->is_monitor()) {
+                               /* relax */
+                       } else if ((*x)->is_master()) {
+                               /* relax */
+                       } else {
+                               (*x)->remove_aux_or_listen (_monitor_out);
+                       }
+               }
+       }
 
-                       uint32_t limit = _monitor_out->n_inputs().n_audio();
+       remove_route (_monitor_out);
+       auto_connect_master_bus ();
+}
 
-                       if (_master_out) {
-                               for (uint32_t n = 0; n < limit; ++n) {
-                                       AudioPort* p = _monitor_out->input()->ports().nth_audio_port (n);
-                                       AudioPort* o = _master_out->output()->ports().nth_audio_port (n);
+void
+Session::add_monitor_section ()
+{
+       RouteList rl;
 
-                                       if (o) {
-                                               string connect_to = o->name();
-                                               if (_monitor_out->input()->connect (p, connect_to, this)) {
-                                                       error << string_compose (_("cannot connect control input %1 to %2"), n, connect_to)
-                                                             << endmsg;
-                                                       break;
-                                               }
-                                       }
-                               }
-                       }
+       if (_monitor_out || !_master_out) {
+               return;
+       }
 
-                       /* if control out is not connected, connect control out to physical outs
-                       */
+       boost::shared_ptr<Route> r (new Route (*this, _("monitor"), Route::MonitorOut, DataType::AUDIO));
 
-                       if (!_monitor_out->output()->connected ()) {
+       if (r->init ()) {
+               return;
+       }
 
-                               if (!Config->get_monitor_bus_preferred_bundle().empty()) {
+#ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
+       // boost_debug_shared_ptr_mark_interesting (r.get(), "Route");
+#endif
+       {
+               Glib::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
+               r->input()->ensure_io (_master_out->output()->n_ports(), false, this);
+               r->output()->ensure_io (_master_out->output()->n_ports(), false, this);
+       }
 
-                                       boost::shared_ptr<Bundle> b = bundle_by_name (Config->get_monitor_bus_preferred_bundle());
+       rl.push_back (r);
+       add_routes (rl, false, false);
+       
+       assert (_monitor_out);
 
-                                       if (b) {
-                                               _monitor_out->output()->connect_ports_to_bundle (b, this);
-                                       } else {
-                                               warning << string_compose (_("The preferred I/O for the monitor bus (%1) cannot be found"),
-                                                                          Config->get_monitor_bus_preferred_bundle())
-                                                       << endmsg;
-                                       }
+       /* AUDIO ONLY as of june 29th 2009, because listen semantics for anything else
+          are undefined, at best.
+       */
+       
+       uint32_t limit = _monitor_out->n_inputs().n_audio();
+       
+       if (_master_out) {
+               
+               /* connect the inputs to the master bus outputs. this
+                * represents a separate data feed from the internal sends from
+                * each route. as of jan 2011, it allows the monitor section to
+                * conditionally ignore either the internal sends or the normal
+                * input feed, but we should really find a better way to do
+                * this, i think.
+                */
+
+               _master_out->output()->disconnect (this);
+
+               for (uint32_t n = 0; n < limit; ++n) {
+                       boost::shared_ptr<AudioPort> p = _monitor_out->input()->ports().nth_audio_port (n);
+                       boost::shared_ptr<AudioPort> o = _master_out->output()->ports().nth_audio_port (n);
+                       
+                       if (o) {
+                               string connect_to = o->name();
+                               if (_monitor_out->input()->connect (p, connect_to, this)) {
+                                       error << string_compose (_("cannot connect control input %1 to %2"), n, connect_to)
+                                             << endmsg;
+                                       break;
+                               }
+                       }
+               }
+       }
+       
+       /* if monitor section is not connected, connect it to physical outs
+        */
+       
+       if (Config->get_auto_connect_standard_busses() && !_monitor_out->output()->connected ()) {
+               
+               if (!Config->get_monitor_bus_preferred_bundle().empty()) {
+                       
+                       boost::shared_ptr<Bundle> b = bundle_by_name (Config->get_monitor_bus_preferred_bundle());
+                       
+                       if (b) {
+                               _monitor_out->output()->connect_ports_to_bundle (b, this);
+                       } else {
+                               warning << string_compose (_("The preferred I/O for the monitor bus (%1) cannot be found"),
+                                                          Config->get_monitor_bus_preferred_bundle())
+                                       << endmsg;
+                       }
+                       
+               } else {
+                       
+                       /* Monitor bus is audio only */
 
-                               } else {
+                       uint32_t mod = n_physical_outputs.get (DataType::AUDIO);
+                       uint32_t limit = _monitor_out->n_outputs().get (DataType::AUDIO);
+                       vector<string> outputs[DataType::num_types];
 
-                                       /* Monitor bus is audio only */
-                                       uint32_t mod = n_physical_outputs.get (DataType::AUDIO);
-                                       uint32_t limit = _monitor_out->n_outputs().get (DataType::AUDIO);
-
-                                       if (mod != 0) {
-
-                                               for (uint32_t n = 0; n < limit; ++n) {
-
-                                                       Port* p = _monitor_out->output()->ports().port(DataType::AUDIO, n);
-                                                       string connect_to;
-                                                       if (outputs[DataType::AUDIO].size() > (n % mod)) {
-                                                               connect_to = outputs[DataType::AUDIO][n % mod];
-                                                       }
-
-                                                       if (!connect_to.empty()) {
-                                                               if (_monitor_out->output()->connect (p, connect_to, this)) {
-                                                                       error << string_compose (
-                                                                                       _("cannot connect control output %1 to %2"),
-                                                                                       n, connect_to)
-                                                                               << endmsg;
-                                                                       break;
-                                                               }
-                                                       }
+                       for (uint32_t i = 0; i < DataType::num_types; ++i) {
+                               _engine.get_physical_outputs (DataType (DataType::Symbol (i)), outputs[i]);
+                       }
+                       
+                       
+                       if (mod != 0) {
+                               
+                               for (uint32_t n = 0; n < limit; ++n) {
+                                       
+                                       boost::shared_ptr<Port> p = _monitor_out->output()->ports().port(DataType::AUDIO, n);
+                                       string connect_to;
+                                       if (outputs[DataType::AUDIO].size() > (n % mod)) {
+                                               connect_to = outputs[DataType::AUDIO][n % mod];
+                                       }
+                                       
+                                       if (!connect_to.empty()) {
+                                               if (_monitor_out->output()->connect (p, connect_to, this)) {
+                                                       error << string_compose (
+                                                               _("cannot connect control output %1 to %2"),
+                                                               n, connect_to)
+                                                             << endmsg;
+                                                       break;
                                                }
                                        }
                                }
@@ -638,14 +755,32 @@ Session::when_engine_running ()
                }
        }
 
-       _state_of_the_state = StateOfTheState (_state_of_the_state & ~(CannotSave|Dirty));
+       /* Hold process lock while doing this so that we don't hear bits and
+        * pieces of audio as we work on each route.
+        */
+        
+       Glib::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
 
-       /* hook us up to the engine */
+       /* Connect tracks to monitor section. Note that in an
+          existing session, the internal sends will already exist, but we want the
+          routes to notice that they connect to the control out specifically.
+       */
 
-       BootMessage (_("Connect to engine"));
-       _engine.set_session (this);
 
-       update_latency_compensation (true);
+       boost::shared_ptr<RouteList> rls = routes.reader ();
+
+       PBD::Unwinder<bool> uw (ignore_route_processor_changes, true);
+
+       for (RouteList::iterator x = rls->begin(); x != rls->end(); ++x) {
+               
+               if ((*x)->is_monitor()) {
+                       /* relax */
+               } else if ((*x)->is_master()) {
+                       /* relax */
+               } else {
+                       (*x)->enable_monitor_send ();
+               }
+       }
 }
 
 void
@@ -692,30 +827,6 @@ Session::hookup_io ()
 
        Delivery::reset_panners ();
 
-       /* Connect tracks to monitor/listen bus if there is one.  Note that in an
-          existing session, the internal sends will already exist, but we want the
-          routes to notice that they connect to the control out specifically.
-       */
-
-       if (_monitor_out) {
-               boost::shared_ptr<RouteList> r = routes.reader ();
-               for (RouteList::iterator x = r->begin(); x != r->end(); ++x) {
-
-                       if ((*x)->is_monitor()) {
-
-                               /* relax */
-
-                       } else if ((*x)->is_master()) {
-
-                               /* relax */
-
-                       } else {
-
-                               (*x)->listen_via_monitor ();
-                       }
-               }
-       }
-
        /* Anyone who cares about input state, wake up and do something */
 
        IOConnectionsComplete (); /* EMIT SIGNAL */
@@ -776,21 +887,11 @@ Session::set_track_monitor_input_status (bool yn)
                boost::shared_ptr<AudioTrack> tr = boost::dynamic_pointer_cast<AudioTrack> (*i);
                if (tr && tr->record_enabled ()) {
                        //cerr << "switching to input = " << !auto_input << __FILE__ << __LINE__ << endl << endl;
-                       tr->monitor_input (yn);
+                       tr->request_jack_monitors_input (yn);
                }
        }
 }
 
-void
-Session::reset_input_monitor_state ()
-{
-       if (transport_rolling()) {
-               set_track_monitor_input_status (Config->get_monitoring_model() == HardwareMonitoring && !config.get_auto_input());
-       } else {
-               set_track_monitor_input_status (Config->get_monitoring_model() == HardwareMonitoring);
-       }
-}
-
 void
 Session::auto_punch_start_changed (Location* location)
 {
@@ -985,13 +1086,18 @@ Session::handle_locations_changed (Locations::LocationList& locations)
 void
 Session::enable_record ()
 {
+       if (_transport_speed != 0.0 && _transport_speed != 1.0) {
+               /* no recording at anything except normal speed */
+               return;
+       }
+
        while (1) {
                RecordState rs = (RecordState) g_atomic_int_get (&_record_status);
 
                if (rs == Recording) {
                        break;
                }
-
+               
                if (g_atomic_int_compare_and_exchange (&_record_status, rs, Recording)) {
 
                        _last_record_location = _transport_frame;
@@ -1043,6 +1149,8 @@ Session::step_back_from_record ()
                if (Config->get_monitoring_model() == HardwareMonitoring && config.get_auto_input()) {
                        set_track_monitor_input_status (false);
                }
+
+               RecordStateChanged (); /* emit signal */
        }
 }
 
@@ -1212,56 +1320,6 @@ Session::set_block_size (pframes_t nframes)
        }
 }
 
-struct RouteSorter {
-    /** @return true to run r1 before r2, otherwise false */
-    bool sort_by_rec_enabled (const boost::shared_ptr<Route>& r1, const boost::shared_ptr<Route>& r2) {
-            if (r1->record_enabled()) {
-                    if (r2->record_enabled()) {
-                            /* both rec-enabled, just use signal order */
-                            return r1->order_key(N_("signal")) < r2->order_key(N_("signal"));
-                    } else {
-                            /* r1 rec-enabled, r2 not rec-enabled, run r2 early */
-                            return false;
-                    }
-            } else {
-                    if (r2->record_enabled()) {
-                            /* r2 rec-enabled, r1 not rec-enabled, run r1 early */
-                            return true;
-                    } else {
-                            /* neither rec-enabled, use signal order */
-                            return r1->order_key(N_("signal")) < r2->order_key(N_("signal"));
-                    }
-            }
-    }
-
-    bool operator() (boost::shared_ptr<Route> r1, boost::shared_ptr<Route> r2) {
-           if (r2->feeds (r1)) {
-                   /* r1 fed by r2; run r2 early */
-                   return false;
-           } else if (r1->feeds (r2)) {
-                   /* r2 fed by r1; run r1 early */
-                   return true;
-           } else {
-                   if (r1->not_fed ()) {
-                           if (r2->not_fed ()) {
-                                   /* no ardour-based connections inbound to either route. */
-                                    return sort_by_rec_enabled (r1, r2);
-                           } else {
-                                   /* r2 has connections, r1 does not; run r1 early */
-                                   return true;
-                           }
-                   } else {
-                           if (r2->not_fed()) {
-                                   /* r1 has connections, r2 does not; run r2 early */
-                                   return false;
-                           } else {
-                                   /* both r1 and r2 have connections, but not to each other. just use signal order */
-                                   return r1->order_key(N_("signal")) < r2->order_key(N_("signal"));
-                           }
-                   }
-           }
-    }
-};
 
 static void
 trace_terminal (boost::shared_ptr<Route> r1, boost::shared_ptr<Route> rbase)
@@ -1317,10 +1375,11 @@ void
 Session::resort_routes ()
 {
        /* don't do anything here with signals emitted
-          by Routes while we are being destroyed.
+          by Routes during initial setup or while we
+          are being destroyed.
        */
 
-       if (_state_of_the_state & Deletion) {
+       if (_state_of_the_state & (InitialConnecting | Deletion)) {
                return;
        }
 
@@ -1331,8 +1390,6 @@ Session::resort_routes ()
                /* writer goes out of scope and forces update */
        }
 
-       //route_graph->dump(1);
-
 #ifndef NDEBUG
        boost::shared_ptr<RouteList> rl = routes.reader ();
        for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
@@ -1350,52 +1407,101 @@ Session::resort_routes ()
 #endif
 
 }
+
+/** This is called whenever we need to rebuild the graph of how we will process
+ *  routes.
+ *  @param r List of routes, in any order.
+ */
+
 void
 Session::resort_routes_using (boost::shared_ptr<RouteList> r)
 {
-       RouteList::iterator i, j;
-
-       for (i = r->begin(); i != r->end(); ++i) {
+       /* We are going to build a directed graph of our routes;
+          this is where the edges of that graph are put.
+       */
+       
+       GraphEdges edges;
+
+       /* Go through all routes doing two things:
+        *
+        * 1. Collect the edges of the route graph.  Each of these edges
+        *    is a pair of routes, one of which directly feeds the other
+        *    either by a JACK connection or by an internal send.
+        *
+        * 2. Begin the process of making routes aware of which other
+        *    routes directly or indirectly feed them.  This information
+        *    is used by the solo code.
+        */
+          
+       for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
 
+               /* Clear out the route's list of direct or indirect feeds */
                (*i)->clear_fed_by ();
 
-               for (j = r->begin(); j != r->end(); ++j) {
-
-                       /* although routes can feed themselves, it will
-                          cause an endless recursive descent if we
-                          detect it. so don't bother checking for
-                          self-feeding.
-                       */
-
-                       if (*j == *i) {
-                               continue;
-                       }
+               for (RouteList::iterator j = r->begin(); j != r->end(); ++j) {
 
                        bool via_sends_only;
 
-                       if ((*j)->direct_feeds (*i, &via_sends_only)) {
+                       /* See if this *j feeds *i according to the current state of the JACK
+                          connections and internal sends.
+                       */
+                       if ((*j)->direct_feeds_according_to_reality (*i, &via_sends_only)) {
+                               /* add the edge to the graph (part #1) */
+                               edges.add (*j, *i, via_sends_only);
+                               /* tell the route (for part #2) */
                                (*i)->add_fed_by (*j, via_sends_only);
                        }
                }
        }
 
-       for (i = r->begin(); i != r->end(); ++i) {
-               trace_terminal (*i, *i);
-       }
+       /* Attempt a topological sort of the route graph */
+       boost::shared_ptr<RouteList> sorted_routes = topological_sort (r, edges);
+       
+       if (sorted_routes) {
+               /* We got a satisfactory topological sort, so there is no feedback;
+                  use this new graph.
 
-       RouteSorter cmp;
-       r->sort (cmp);
+                  Note: the process graph rechain does not require a
+                  topologically-sorted list, but hey ho.
+               */
+               if (_process_graph) {
+                       _process_graph->rechain (sorted_routes, edges);
+               }
+               
+               _current_route_graph = edges;
 
-       route_graph->rechain (r);
+               /* Complete the building of the routes' lists of what directly
+                  or indirectly feeds them.
+               */
+               for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
+                       trace_terminal (*i, *i);
+               }
+
+               r = sorted_routes;
 
 #ifndef NDEBUG
-       DEBUG_TRACE (DEBUG::Graph, "Routes resorted, order follows:\n");
-       for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
-               DEBUG_TRACE (DEBUG::Graph, string_compose ("\t%1 signal order %2\n",
-                                                          (*i)->name(), (*i)->order_key ("signal")));
-       }
+               DEBUG_TRACE (DEBUG::Graph, "Routes resorted, order follows:\n");
+               for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
+                       DEBUG_TRACE (DEBUG::Graph, string_compose ("\t%1 signal order %2\n",
+                                                                  (*i)->name(), (*i)->order_key ("signal")));
+               }
 #endif
 
+               SuccessfulGraphSort (); /* EMIT SIGNAL */
+
+       } else {
+               /* The topological sort failed, so we have a problem.  Tell everyone
+                  and stick to the old graph; this will continue to be processed, so
+                  until the feedback is fixed, what is played back will not quite
+                  reflect what is actually connected.  Note also that we do not
+                  do trace_terminal here, as it would fail due to an endless recursion,
+                  so the solo code will think that everything is still connected
+                  as it was before.
+               */
+               
+               FeedbackDetected (); /* EMIT SIGNAL */
+       }
+
 }
 
 /** Find a route name starting with \a base, maybe followed by the
@@ -1452,7 +1558,7 @@ Session::count_existing_track_channels (ChanCount& in, ChanCount& out)
 }
 
 /** Caller must not hold process lock
- *  @param name_template string to use for the start of the name, or "" to use "Midi".
+ *  @param name_template string to use for the start of the name, or "" to use "MIDI".
  */
 list<boost::shared_ptr<MidiTrack> >
 Session::new_midi_track (TrackMode mode, RouteGroup* route_group, uint32_t how_many, string name_template)
@@ -1464,12 +1570,12 @@ Session::new_midi_track (TrackMode mode, RouteGroup* route_group, uint32_t how_m
        list<boost::shared_ptr<MidiTrack> > ret;
        uint32_t control_id;
 
-       control_id = ntracks() + nbusses();
+       control_id = next_control_id ();
 
-       bool const use_number = (how_many != 1) || name_template.empty () || name_template == _("Midi");
+       bool const use_number = (how_many != 1) || name_template.empty () || name_template == _("MIDI");
 
        while (how_many) {
-               if (!find_route_name (name_template.empty() ? _("Midi") : name_template, ++track_id, track_name, sizeof(track_name), use_number)) {
+               if (!find_route_name (name_template.empty() ? _("MIDI") : name_template, ++track_id, track_name, sizeof(track_name), use_number)) {
                        error << "cannot find name for new midi track" << endmsg;
                        goto failed;
                }
@@ -1486,7 +1592,7 @@ Session::new_midi_track (TrackMode mode, RouteGroup* route_group, uint32_t how_m
                        track->use_new_diskstream();
 
 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
-                       boost_debug_shared_ptr_mark_interesting (track.get(), "Track");
+                       // boost_debug_shared_ptr_mark_interesting (track.get(), "Track");
 #endif
                        {
                                Glib::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
@@ -1680,9 +1786,8 @@ Session::auto_connect_route (boost::shared_ptr<Route> route, ChanCount& existing
  *  @param name_template string to use for the start of the name, or "" to use "Audio".
  */
 list< boost::shared_ptr<AudioTrack> >
-Session::new_audio_track (
-       int input_channels, int output_channels, TrackMode mode, RouteGroup* route_group, uint32_t how_many, string name_template
-       )
+Session::new_audio_track (int input_channels, int output_channels, TrackMode mode, RouteGroup* route_group, 
+                         uint32_t how_many, string name_template)
 {
        char track_name[32];
        uint32_t track_id = 0;
@@ -1691,7 +1796,7 @@ Session::new_audio_track (
        list<boost::shared_ptr<AudioTrack> > ret;
        uint32_t control_id;
 
-       control_id = ntracks() + nbusses() + 1;
+       control_id = next_control_id ();
 
        bool const use_number = (how_many != 1) || name_template.empty () || name_template == _("Audio");
 
@@ -1713,7 +1818,7 @@ Session::new_audio_track (
                        track->use_new_diskstream();
 
 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
-                       boost_debug_shared_ptr_mark_interesting (track.get(), "Track");
+                       // boost_debug_shared_ptr_mark_interesting (track.get(), "Track");
 #endif
                        {
                                Glib::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
@@ -1810,7 +1915,7 @@ Session::new_audio_route (int input_channels, int output_channels, RouteGroup* r
        RouteList ret;
        uint32_t control_id;
 
-       control_id = ntracks() + nbusses() + 1;
+       control_id = next_control_id ();
 
        bool const use_number = (how_many != 1) || name_template.empty () || name_template == _("Bus");
        
@@ -1828,7 +1933,7 @@ Session::new_audio_route (int input_channels, int output_channels, RouteGroup* r
                        }
 
 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
-                       boost_debug_shared_ptr_mark_interesting (bus.get(), "Route");
+                       // boost_debug_shared_ptr_mark_interesting (bus.get(), "Route");
 #endif
                        {
                                Glib::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
@@ -1877,7 +1982,7 @@ Session::new_audio_route (int input_channels, int output_channels, RouteGroup* r
 
   failure:
        if (!ret.empty()) {
-               add_routes (ret, true, true);
+               add_routes (ret, false, true);
        }
 
        return ret;
@@ -1887,7 +1992,6 @@ Session::new_audio_route (int input_channels, int output_channels, RouteGroup* r
 RouteList
 Session::new_route_from_template (uint32_t how_many, const std::string& template_path)
 {
-       char name[32];
        RouteList ret;
        uint32_t control_id;
        XMLTree tree;
@@ -1899,31 +2003,39 @@ Session::new_route_from_template (uint32_t how_many, const std::string& template
 
        XMLNode* node = tree.root();
 
-       control_id = ntracks() + nbusses() + 1;
+       control_id = next_control_id ();
 
        while (how_many) {
 
-               XMLNode node_copy (*node); // make a copy so we can change the name if we need to
-
-               std::string node_name = IO::name_from_state (*node_copy.children().front());
+               XMLNode node_copy (*node);
 
-               /* generate a new name by adding a number to the end of the template name */
-               if (!find_route_name (node_name.c_str(), ++number, name, sizeof(name), true)) {
-                       fatal << _("Session: UINT_MAX routes? impossible!") << endmsg;
-                       /*NOTREACHED*/
-               }
+               /* Remove IDs of everything so that new ones are used */
+               node_copy.remove_property_recursively (X_("id"));
 
-               /* set IO children to use the new name */
-               XMLNodeList const & children = node_copy.children ();
-               for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
-                       if ((*i)->name() == IO::state_node_name) {
-                               IO::set_name_in_state (**i, name);
+               try {
+                       string const route_name = node_copy.property(X_("name"))->value ();
+                       
+                       /* generate a new name by adding a number to the end of the template name */
+                       char name[32];
+                       if (!find_route_name (route_name.c_str(), ++number, name, sizeof(name), true)) {
+                               fatal << _("Session: UINT_MAX routes? impossible!") << endmsg;
+                               /*NOTREACHED*/
                        }
-               }
 
-               Track::zero_diskstream_id_in_xml (node_copy);
+                       /* set this name in the XML description that we are about to use */
+                       Route::set_name_in_state (node_copy, name);
 
-               try {
+                       /* trim bitslots from listen sends so that new ones are used */
+                       XMLNodeList children = node_copy.children ();
+                       for (XMLNodeList::iterator i = children.begin(); i != children.end(); ++i) {
+                               if ((*i)->name() == X_("Processor")) {
+                                       XMLProperty* role = (*i)->property (X_("role"));
+                                       if (role && role->value() == X_("Listen")) {
+                                               (*i)->remove_property (X_("bitslot"));
+                                       }
+                               }
+                       }
+                       
                        boost::shared_ptr<Route> route (XMLRouteFactory (node_copy, 3000));
 
                        if (route == 0) {
@@ -1986,7 +2098,6 @@ Session::add_routes (RouteList& new_routes, bool auto_connect, bool save)
                boost::shared_ptr<RouteList> r = writer.get_copy ();
                r->insert (r->end(), new_routes.begin(), new_routes.end());
 
-
                /* if there is no control out and we're not in the middle of loading,
                   resort the graph here. if there is a control out, we will resort
                   toward the end of this method. if we are in the middle of loading,
@@ -2039,13 +2150,17 @@ Session::add_routes (RouteList& new_routes, bool auto_connect, bool save)
 
        if (_monitor_out && IO::connecting_legal) {
 
-               for (RouteList::iterator x = new_routes.begin(); x != new_routes.end(); ++x) {
-                       if ((*x)->is_monitor()) {
-                               /* relax */
-                       } else if ((*x)->is_master()) {
-                               /* relax */
-                       } else {
-                               (*x)->listen_via_monitor ();
+               {
+                       Glib::Mutex::Lock lm (_engine.process_lock());          
+                       
+                       for (RouteList::iterator x = new_routes.begin(); x != new_routes.end(); ++x) {
+                               if ((*x)->is_monitor()) {
+                                       /* relax */
+                               } else if ((*x)->is_master()) {
+                                       /* relax */
+                               } else {
+                                       (*x)->enable_monitor_send ();
+                               }
                        }
                }
 
@@ -2109,7 +2224,8 @@ Session::globally_add_internal_sends (boost::shared_ptr<Route> dest, Placement p
        boost::shared_ptr<RouteList> t (new RouteList);
 
        for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
-               if (include_buses || boost::dynamic_pointer_cast<Track>(*i)) {
+               /* no MIDI sends because there are no MIDI busses yet */
+               if (include_buses || boost::dynamic_pointer_cast<AudioTrack>(*i)) {
                        t->push_back (*i);
                }
        }
@@ -2128,13 +2244,14 @@ Session::add_internal_sends (boost::shared_ptr<Route> dest, Placement p, boost::
                dest->add_internal_return();
        }
 
+       
        for (RouteList::iterator i = senders->begin(); i != senders->end(); ++i) {
-
+               
                if ((*i)->is_monitor() || (*i)->is_master() || (*i) == dest) {
                        continue;
                }
-
-               (*i)->listen_via (dest, p);
+               
+               (*i)->add_aux_send (dest, p);
        }
 
        graph_reordered ();
@@ -2143,7 +2260,7 @@ Session::add_internal_sends (boost::shared_ptr<Route> dest, Placement p, boost::
 void
 Session::remove_route (boost::shared_ptr<Route> route)
 {
-       if (((route == _master_out) || (route == _monitor_out)) && !Config->get_allow_special_bus_removal()) {
+       if (route == _master_out) {
                return;
        }
 
@@ -2165,13 +2282,6 @@ Session::remove_route (boost::shared_ptr<Route> route)
                }
 
                if (route == _monitor_out) {
-
-                       /* cancel control outs for all routes */
-
-                       for (RouteList::iterator r = rs->begin(); r != rs->end(); ++r) {
-                               (*r)->drop_listen (_monitor_out);
-                       }
-
                        _monitor_out.reset ();
                }
 
@@ -2212,7 +2322,9 @@ Session::remove_route (boost::shared_ptr<Route> route)
         */
 
        resort_routes ();
-       route_graph->clear_other_chain ();
+       if (_process_graph) {
+               _process_graph->clear_other_chain ();
+       }
 
        /* get rid of it from the dead wood collection in the route list manager */
 
@@ -2305,6 +2417,8 @@ Session::route_solo_isolated_changed (void* /*src*/, boost::weak_ptr<Route> wpr)
 void
 Session::route_solo_changed (bool self_solo_change, void* /*src*/, boost::weak_ptr<Route> wpr)
 {
+       DEBUG_TRACE (DEBUG::Solo, string_compose ("route solo change, self = %1\n", self_solo_change));
+
        if (!self_solo_change) {
                // session doesn't care about changes to soloed-by-others
                return;
@@ -2312,16 +2426,12 @@ Session::route_solo_changed (bool self_solo_change, void* /*src*/, boost::weak_p
 
        if (solo_update_disabled) {
                // We know already
+               DEBUG_TRACE (DEBUG::Solo, "solo update disabled - changed ignored\n");
                return;
        }
 
        boost::shared_ptr<Route> route = wpr.lock ();
-
-       if (!route) {
-               /* should not happen */
-               error << string_compose (_("programming error: %1"), X_("invalid route weak ptr passed to route_solo_changed")) << endmsg;
-               return;
-       }
+       assert (route);
 
        boost::shared_ptr<RouteList> r = routes.reader ();
        int32_t delta;
@@ -2332,42 +2442,77 @@ Session::route_solo_changed (bool self_solo_change, void* /*src*/, boost::weak_p
                delta = -1;
        }
 
+       RouteGroup* rg = route->route_group ();
+       bool leave_group_alone = (rg && rg->is_active() && rg->is_solo());
+
        if (delta == 1 && Config->get_exclusive_solo()) {
-               /* new solo: disable all other solos */
+               
+               /* new solo: disable all other solos, but not the group if its solo-enabled */
+
                for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
-                       if ((*i) == route || (*i)->solo_isolated() || (*i)->is_master() || (*i)->is_monitor() || (*i)->is_hidden()) {
+                       if ((*i) == route || (*i)->solo_isolated() || (*i)->is_master() || (*i)->is_monitor() || (*i)->is_hidden() ||
+                           (leave_group_alone && ((*i)->route_group() == rg))) {
                                continue;
                        }
                        (*i)->set_solo (false, this);
                }
        }
 
+       DEBUG_TRACE (DEBUG::Solo, string_compose ("propagate solo change, delta = %1\n", delta));
+
        solo_update_disabled = true;
 
        RouteList uninvolved;
 
+       DEBUG_TRACE (DEBUG::Solo, string_compose ("%1\n", route->name()));
+
        for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
                bool via_sends_only;
                bool in_signal_flow;
 
-               if ((*i) == route || (*i)->solo_isolated() || (*i)->is_master() || (*i)->is_monitor() || (*i)->is_hidden()) {
+               if ((*i) == route || (*i)->solo_isolated() || (*i)->is_master() || (*i)->is_monitor() || (*i)->is_hidden() ||
+                   (leave_group_alone && ((*i)->route_group() == rg))) {
                        continue;
                }
 
                in_signal_flow = false;
 
+               DEBUG_TRACE (DEBUG::Solo, string_compose ("check feed from %1\n", (*i)->name()));
+               
                if ((*i)->feeds (route, &via_sends_only)) {
                        if (!via_sends_only) {
                                if (!route->soloed_by_others_upstream()) {
                                        (*i)->mod_solo_by_others_downstream (delta);
                                }
-                               in_signal_flow = true;
                        }
+                       in_signal_flow = true;
+               } else {
+                       DEBUG_TRACE (DEBUG::Solo, "\tno feed from\n");
                }
+               
+               DEBUG_TRACE (DEBUG::Solo, string_compose ("check feed to %1\n", (*i)->name()));
 
                if (route->feeds (*i, &via_sends_only)) {
-                       (*i)->mod_solo_by_others_upstream (delta);
+                       /* propagate solo upstream only if routing other than
+                          sends is involved, but do consider the other route
+                          (*i) to be part of the signal flow even if only
+                          sends are involved.
+                       */
+                       DEBUG_TRACE (DEBUG::Solo, string_compose ("%1 feeds %2 via sends only %3 sboD %4 sboU %5\n",
+                                                                 route->name(),
+                                                                 (*i)->name(),
+                                                                 via_sends_only,
+                                                                 route->soloed_by_others_downstream(),
+                                                                 route->soloed_by_others_upstream()));
+                       if (!via_sends_only) {
+                               if (!route->soloed_by_others_downstream()) {
+                                       DEBUG_TRACE (DEBUG::Solo, string_compose ("\tmod %1 by %2\n", (*i)->name(), delta));
+                                       (*i)->mod_solo_by_others_upstream (delta);
+                               }
+                       }
                        in_signal_flow = true;
+               } else {
+                       DEBUG_TRACE (DEBUG::Solo, "\tno feed to\n");
                }
 
                if (!in_signal_flow) {
@@ -2376,6 +2521,8 @@ Session::route_solo_changed (bool self_solo_change, void* /*src*/, boost::weak_p
        }
 
        solo_update_disabled = false;
+       DEBUG_TRACE (DEBUG::Solo, "propagation complete\n");
+
        update_route_solo_state (r);
 
        /* now notify that the mute state of the routes not involved in the signal
@@ -2383,6 +2530,7 @@ Session::route_solo_changed (bool self_solo_change, void* /*src*/, boost::weak_p
        */
 
        for (RouteList::iterator i = uninvolved.begin(); i != uninvolved.end(); ++i) {
+               DEBUG_TRACE (DEBUG::Solo, string_compose ("mute change for %1\n", (*i)->name()));
                (*i)->mute_changed (this);
        }
 
@@ -2466,6 +2614,62 @@ Session::io_name_is_legal (const std::string& name)
        return true;
 }
 
+void
+Session::set_exclusive_input_active (boost::shared_ptr<Route> rt, bool /*others_on*/)
+{
+       RouteList rl;
+       vector<string> connections;
+
+       PortSet& ps (rt->input()->ports());
+
+       for (PortSet::iterator p = ps.begin(); p != ps.end(); ++p) {
+               p->get_connections (connections);
+       }
+
+       for (vector<string>::iterator s = connections.begin(); s != connections.end(); ++s) {
+               routes_using_input_from (*s, rl);
+       }
+
+       /* scan all relevant routes to see if others are on or off */
+
+       bool others_are_already_on = false;
+
+       for (RouteList::iterator r = rl.begin(); r != rl.end(); ++r) {
+               if ((*r) != rt) {
+                       boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack> (*r);
+                       if (mt) {
+                               if (mt->input_active()) {
+                                       others_are_already_on = true;
+                                       break;
+                               }
+                       }
+               }
+       }
+
+       /* globally reverse other routes */
+
+       for (RouteList::iterator r = rl.begin(); r != rl.end(); ++r) {
+               if ((*r) != rt) {
+                       boost::shared_ptr<MidiTrack> mt = boost::dynamic_pointer_cast<MidiTrack> (*r);
+                       if (mt) {
+                               mt->set_input_active (!others_are_already_on);
+                       }
+               }
+       }
+}
+
+void
+Session::routes_using_input_from (const string& str, RouteList& rl)
+{
+       boost::shared_ptr<RouteList> r = routes.reader ();
+
+       for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
+               if ((*i)->input()->connected_to (str)) {
+                       rl.push_back (*i);
+               }
+       }
+}
+
 boost::shared_ptr<Route>
 Session::route_by_name (string name)
 {
@@ -2494,6 +2698,21 @@ Session::route_by_id (PBD::ID id)
        return boost::shared_ptr<Route> ((Route*) 0);
 }
 
+boost::shared_ptr<Track>
+Session::track_by_diskstream_id (PBD::ID id)
+{
+       boost::shared_ptr<RouteList> r = routes.reader ();
+
+       for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
+               boost::shared_ptr<Track> t = boost::dynamic_pointer_cast<Track> (*i);
+               if (t && t->using_diskstream_id (id)) {
+                       return t;
+               }
+       }
+
+       return boost::shared_ptr<Track> ();
+}
+
 boost::shared_ptr<Route>
 Session::route_by_remote_id (uint32_t id)
 {
@@ -3416,6 +3635,26 @@ Session::next_send_id ()
        }
 }
 
+uint32_t
+Session::next_aux_send_id ()
+{
+       /* this doesn't really loop forever. just think about it */
+
+       while (true) {
+               for (boost::dynamic_bitset<uint32_t>::size_type n = 0; n < aux_send_bitset.size(); ++n) {
+                       if (!aux_send_bitset[n]) {
+                               aux_send_bitset[n] = true;
+                               return n;
+
+                       }
+               }
+
+               /* none available, so resize and try again */
+
+               aux_send_bitset.resize (aux_send_bitset.size() + 16, false);
+       }
+}
+
 uint32_t
 Session::next_return_id ()
 {
@@ -3448,6 +3687,18 @@ Session::mark_send_id (uint32_t id)
        send_bitset[id] = true;
 }
 
+void
+Session::mark_aux_send_id (uint32_t id)
+{
+       if (id >= aux_send_bitset.size()) {
+               aux_send_bitset.resize (id+16, false);
+       }
+       if (aux_send_bitset[id]) {
+               warning << string_compose (_("aux send ID %1 appears to be in use already"), id) << endmsg;
+       }
+       aux_send_bitset[id] = true;
+}
+
 void
 Session::mark_return_id (uint32_t id)
 {
@@ -3480,6 +3731,14 @@ Session::unmark_send_id (uint32_t id)
        }
 }
 
+void
+Session::unmark_aux_send_id (uint32_t id)
+{
+       if (id < aux_send_bitset.size()) {
+               aux_send_bitset[id] = false;
+       }
+}
+
 void
 Session::unmark_return_id (uint32_t id)
 {
@@ -3936,11 +4195,25 @@ Session::solo_control_mode_changed ()
        }
 }
 
-/** Called when anything about any of our route groups changes (membership, state etc.) */
+/** Called when a property of one of our route groups changes */
 void
-Session::route_group_changed ()
+Session::route_group_property_changed (RouteGroup* rg)
 {
-       RouteGroupChanged (); /* EMIT SIGNAL */
+       RouteGroupPropertyChanged (rg); /* EMIT SIGNAL */
+}
+
+/** Called when a route is added to one of our route groups */
+void
+Session::route_added_to_route_group (RouteGroup* rg, boost::weak_ptr<Route> r)
+{
+       RouteAddedToRouteGroup (rg, r);
+}
+
+/** Called when a route is removed from one of our route groups */
+void
+Session::route_removed_from_route_group (RouteGroup* rg, boost::weak_ptr<Route> r)
+{
+       RouteRemovedFromRouteGroup (rg, r);
 }
 
 vector<SyncSource>
@@ -4085,7 +4358,7 @@ Session::end_time_changed (framepos_t old)
 
        Location* l = _locations->auto_loop_location ();
 
-       if (l->end() == old) {
+       if (l && l->end() == old) {
                l->set_end (s->end(), true);
        }
 }
@@ -4093,35 +4366,32 @@ Session::end_time_changed (framepos_t old)
 string
 Session::source_search_path (DataType type) const
 {
-       string search_path;
+       vector<string> s;
 
        if (session_dirs.size() == 1) {
                switch (type) {
                case DataType::AUDIO:
-                       search_path = _session_dir->sound_path().to_string();
+                       s.push_back ( _session_dir->sound_path().to_string());
                        break;
                case DataType::MIDI:
-                       search_path = _session_dir->midi_path().to_string();
+                       s.push_back (_session_dir->midi_path().to_string());
                        break;
                }
        } else {
                for (vector<space_and_path>::const_iterator i = session_dirs.begin(); i != session_dirs.end(); ++i) {
                        SessionDirectory sdir (i->path);
-                       if (!search_path.empty()) {
-                               search_path += ':';
-                       }
                        switch (type) {
                        case DataType::AUDIO:
-                               search_path += sdir.sound_path().to_string();
+                               s.push_back (sdir.sound_path().to_string());
                                break;
                        case DataType::MIDI:
-                               search_path += sdir.midi_path().to_string();
+                               s.push_back (sdir.midi_path().to_string());
                                break;
                        }
                }
        }
 
-       /* now add user-specified locations
+       /* now check the explicit (possibly user-specified) search path
         */
 
        vector<string> dirs;
@@ -4136,9 +4406,27 @@ Session::source_search_path (DataType type) const
        }
 
        for (vector<string>::iterator i = dirs.begin(); i != dirs.end(); ++i) {
-               search_path += ':';
-               search_path += *i;
 
+               vector<string>::iterator si;
+
+               for (si = s.begin(); si != s.end(); ++si) {
+                       if ((*si) == *i) {
+                               break;
+                       }
+               }
+
+               if (si == s.end()) {
+                       s.push_back (*i);
+               }
+       }
+       
+       string search_path;
+
+       for (vector<string>::iterator si = s.begin(); si != s.end(); ++si) {
+               if (!search_path.empty()) {
+                       search_path += ':';
+               }
+               search_path += *si;
        }
 
        return search_path;
@@ -4162,7 +4450,7 @@ Session::ensure_search_path_includes (const string& path, DataType type)
                search_path = config.get_midi_search_path ();
                break;
        }
-
+       
        split (search_path, dirs, ':');
 
        for (vector<string>::iterator i = dirs.begin(); i != dirs.end(); ++i) {
@@ -4224,6 +4512,8 @@ Session::update_latency (bool playback)
 
        if (playback) {
                /* reverse the list so that we work backwards from the last route to run to the first */
+                RouteList* rl = routes.reader().get();
+                r.reset (new RouteList (*rl));
                reverse (r->begin(), r->end());
        }
 
@@ -4267,14 +4557,15 @@ Session::post_playback_latency ()
        set_worst_playback_latency ();
 
        boost::shared_ptr<RouteList> r = routes.reader ();
+
        for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
                if (!(*i)->is_hidden() && ((*i)->active())) {
                        _worst_track_latency = max (_worst_track_latency, (*i)->update_signal_latency ());
                }
+       }
 
-               for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
-                       (*i)->set_latency_compensation (_worst_track_latency);
-               }
+       for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
+               (*i)->set_latency_compensation (_worst_track_latency);
        }
 }
 
@@ -4295,6 +4586,18 @@ Session::post_capture_latency ()
        }
 }
 
+void
+Session::initialize_latencies ()
+{
+        {
+                Glib::Mutex::Lock lm (_engine.process_lock());
+                update_latency (false);
+                update_latency (true);
+        }
+
+        set_worst_io_latencies ();
+}
+
 void
 Session::set_worst_io_latencies ()
 {
@@ -4374,15 +4677,38 @@ Session::update_latency_compensation (bool force_whole_graph)
        DEBUG_TRACE (DEBUG::Latency, string_compose ("worst signal processing latency: %1 (changed ? %2)\n", _worst_track_latency,
                                                     (some_track_latency_changed ? "yes" : "no")));
 
-       if (force_whole_graph || some_track_latency_changed) {
-               /* trigger a full recompute of latency numbers for the graph.
-                  everything else that we need to do will be done in the latency
-                  callback.
-               */
-               _engine.update_total_latencies ();
-               return; // everything else will be done in the latency callback
+       DEBUG_TRACE(DEBUG::Latency, "---------------------------- DONE update latency compensation\n\n");
+       
+       if (some_track_latency_changed || force_whole_graph)  {
+               _engine.update_latencies ();
        }
 
-       DEBUG_TRACE(DEBUG::Latency, "---------------------------- DONE update latency compensation\n\n")
+
+       for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
+               boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
+               if (!tr) {
+                       continue;
+               }
+               tr->set_capture_offset ();
+       }
 }
 
+char
+Session::session_name_is_legal (const string& path)
+{
+       char illegal_chars[] = { '/', '\\', ':', ';', '\0' };
+
+       for (int i = 0; illegal_chars[i]; ++i) {
+               if (path.find (illegal_chars[i]) != string::npos) {
+                       return illegal_chars[i];
+               }
+       }
+
+       return 0;
+}
+
+uint32_t 
+Session::next_control_id () const
+{
+       return ntracks() + nbusses() + 1;
+}