move ownership of LTC I/O ports to Session, and manage as IO objects
[ardour.git] / libs / ardour / session.cc
index a5e6fca564d2a9a34b757525249354898390e9cf..c335e021aa616e7294ac25542db15af52c68aa2c 100644 (file)
@@ -30,7 +30,7 @@
 #include <unistd.h>
 #include <limits.h>
 
-#include <glibmm/thread.h>
+#include <glibmm/threads.h>
 #include <glibmm/miscutils.h>
 #include <glibmm/fileutils.h>
 
@@ -45,6 +45,7 @@
 #include "pbd/file_utils.h"
 #include "pbd/convert.h"
 #include "pbd/strsplit.h"
+#include "pbd/unwind.h"
 
 #include "ardour/amp.h"
 #include "ardour/analyser.h"
 #include "ardour/audio_track.h"
 #include "ardour/audioengine.h"
 #include "ardour/audiofilesource.h"
-#include "ardour/audioplaylist.h"
-#include "ardour/audioregion.h"
 #include "ardour/auditioner.h"
 #include "ardour/buffer_manager.h"
 #include "ardour/buffer_set.h"
 #include "ardour/bundle.h"
 #include "ardour/butler.h"
 #include "ardour/click.h"
-#include "ardour/configuration.h"
 #include "ardour/control_protocol_manager.h"
-#include "ardour/crossfade.h"
-#include "ardour/cycle_timer.h"
 #include "ardour/data_type.h"
 #include "ardour/debug.h"
 #include "ardour/filename_extensions.h"
-#include "ardour/internal_send.h"
-#include "ardour/io_processor.h"
-#include "ardour/midi_diskstream.h"
-#include "ardour/midi_playlist.h"
-#include "ardour/midi_region.h"
+#include "ardour/graph.h"
 #include "ardour/midi_track.h"
 #include "ardour/midi_ui.h"
-#include "ardour/named_selection.h"
-#include "ardour/process_thread.h"
+#include "ardour/operations.h"
 #include "ardour/playlist.h"
+#include "ardour/plugin.h"
 #include "ardour/plugin_insert.h"
-#include "ardour/port_insert.h"
-#include "ardour/processor.h"
+#include "ardour/process_thread.h"
 #include "ardour/rc_configuration.h"
 #include "ardour/recent_sessions.h"
+#include "ardour/region.h"
 #include "ardour/region_factory.h"
-#include "ardour/return.h"
-#include "ardour/route_dag.h"
+#include "ardour/route_graph.h"
 #include "ardour/route_group.h"
 #include "ardour/send.h"
 #include "ardour/session.h"
 #include "ardour/session_directory.h"
-#include "ardour/session_directory.h"
-#include "ardour/session_metadata.h"
 #include "ardour/session_playlists.h"
-#include "ardour/slave.h"
 #include "ardour/smf_source.h"
 #include "ardour/source_factory.h"
-#include "ardour/tape_file_matcher.h"
-#include "ardour/tempo.h"
 #include "ardour/utils.h"
-#include "ardour/graph.h"
-#include "ardour/speakers.h"
-#include "ardour/operations.h"
 
 #include "midi++/port.h"
+#include "midi++/jack_midi_port.h"
 #include "midi++/mmc.h"
 #include "midi++/manager.h"
 
 #include "i18n.h"
 
+namespace ARDOUR {
+class MidiSource;
+class Processor;
+class Speakers;
+}
+
 using namespace std;
 using namespace ARDOUR;
 using namespace PBD;
@@ -124,16 +114,16 @@ PBD::Signal3<int,Session*,std::string,DataType> Session::MissingFile;
 
 PBD::Signal1<void, framepos_t> Session::StartTimeChanged;
 PBD::Signal1<void, framepos_t> Session::EndTimeChanged;
-PBD::Signal0<void> Session::AutoBindingOn;
-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 */
+/** @param snapshot_name Snapshot name, without .ardour suffix */
 Session::Session (AudioEngine &eng,
                   const string& fullpath,
                   const string& snapshot_name,
@@ -149,21 +139,29 @@ 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)
+       , _total_free_4k_blocks_uncertain (false)
        , _bundles (new BundleList)
        , _bundle_xml_node (0)
        , _current_trans (0)
-       , _click_io ((IO*) 0)
        , click_data (0)
        , click_emphasis_data (0)
        , main_outs (0)
-       , _metadata (new SessionMetadata())
        , _have_rec_enabled_track (false)
        , _suspend_timecode_transmission (0)
 {
        _locations = new Locations (*this);
+#ifdef HAVE_LTC
+       ltc_encoder = NULL;
+#endif
+
+       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);
 
@@ -215,6 +213,9 @@ Session::Session (AudioEngine &eng,
 
 Session::~Session ()
 {
+#ifdef PT_TIMING       
+       ST.dump ("ST.dump");
+#endif 
        destroy ();
 }
 
@@ -233,6 +234,16 @@ Session::destroy ()
 
        _engine.remove_session ();
 
+       /* deregister all ports - there will be no process or any other
+        * callbacks from the engine any more.
+        */
+
+       Port::PortDrop (); /* EMIT SIGNAL */
+
+#ifdef HAVE_LTC
+       ltc_tx_cleanup();
+#endif
+
        /* clear history so that no references to objects are held any more */
 
        _history.clear ();
@@ -247,6 +258,8 @@ Session::destroy ()
 
        _butler->drop_references ();
        delete _butler;
+       _butler = 0;
+       
        delete midi_control_ui;
        delete _all_route_group;
 
@@ -272,9 +285,6 @@ Session::destroy ()
 
        /* tell everyone to drop references and delete objects as we go */
 
-       DEBUG_TRACE (DEBUG::Destruction, "delete named selections\n");
-       named_selections.clear ();
-
        DEBUG_TRACE (DEBUG::Destruction, "delete regions\n");
        RegionFactory::delete_all_regions ();
 
@@ -314,8 +324,6 @@ Session::destroy ()
                delete *i;
        }
 
-       Crossfade::set_buffer_size (0);
-
        /* not strictly necessary, but doing it here allows the shared_ptr debugging to work */
        playlists.reset ();
 
@@ -362,21 +370,52 @@ Session::when_engine_running ()
 
        try {
                XMLNode* child = 0;
+               
+               _ltc_input.reset (new IO (*this, _("LTC In"), IO::Input));
+               _ltc_output.reset (new IO (*this, _("LTC Out"), IO::Output));
+               
+               if (state_tree && (child = find_named_node (*state_tree->root(), "LTC-In")) != 0) {
+                       _ltc_input->set_state (*(child->children().front()), Stateful::loading_state_version);
+               } else {
+                       {
+                               Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
+                               _ltc_input->ensure_io (ChanCount (DataType::AUDIO, 1), true, this);
+                       }
+                       reconnect_ltc_input ();
+               }
 
+               if (state_tree && (child = find_named_node (*state_tree->root(), "LTC-Out")) != 0) {
+                       _ltc_output->set_state (*(child->children().front()), Stateful::loading_state_version);
+               } else {
+                       {
+                               Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
+                               _ltc_output->ensure_io (ChanCount (DataType::AUDIO, 1), true, this);
+                       }
+                       reconnect_ltc_output ();
+               }
+                                               
                _click_io.reset (new ClickIO (*this, "click"));
+               _click_gain.reset (new Amp (*this));
+               _click_gain->activate ();
 
                if (state_tree && (child = find_named_node (*state_tree->root(), "Click")) != 0) {
 
                        /* existing state for Click */
-                       int c;
+                       int c = 0;
 
                        if (Stateful::loading_state_version < 3000) {
                                c = _click_io->set_state_2X (*child->children().front(), Stateful::loading_state_version, false);
                        } else {
-                               c = _click_io->set_state (*child->children().front(), Stateful::loading_state_version);
+                               const XMLNodeList& children (child->children());
+                               XMLNodeList::const_iterator i = children.begin();
+                               if ((c = _click_io->set_state (**i, Stateful::loading_state_version)) == 0) {
+                                       ++i;
+                                       if (i != children.end()) {
+                                               c = _click_gain->set_state (**i, Stateful::loading_state_version);
+                                       }
+                               }
                        }
-
-
+                       
                        if (c == 0) {
                                _clicking = Config->get_clicking ();
 
@@ -520,11 +559,21 @@ Session::when_engine_running ()
 
        BootMessage (_("Setup signal flow and plugins"));
 
+       /* Reset all panners */
+
+       Delivery::reset_panners ();
+
+       /* this will cause the CPM to instantiate any protocols that are in use
+        * (or mandatory), which will pass it this Session, and then call
+        * set_state() on each instantiated protocol to match stored state.
+        */
+
        ControlProtocolManager::instance().set_session (this);
 
        /* This must be done after the ControlProtocolManager set_session above,
           as it will set states for ports which the ControlProtocolManager creates.
        */
+
        MIDI::Manager::instance()->set_port_states (Config->midi_port_states ());
 
        /* And this must be done after the MIDI::Manager::set_port_states as
@@ -533,106 +582,215 @@ Session::when_engine_running ()
 
        hookup_io ();
 
-       if (_is_new && !no_auto_connect()) {
+       /* Let control protocols know that we are now all connected, so they
+        * could start talking to surfaces if they want to.
+        */
 
-               Glib::Mutex::Lock lm (AudioEngine::instance()->process_lock());
+       ControlProtocolManager::instance().midi_connectivity_established ();
 
-               /* don't connect the master bus outputs if there is a monitor bus */
+       if (_is_new && !no_auto_connect()) {
+               Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock());
+               auto_connect_master_bus ();
+       }
 
-               if (_master_out && Config->get_auto_connect_standard_busses() && !_monitor_out) {
+       _state_of_the_state = StateOfTheState (_state_of_the_state & ~(CannotSave|Dirty));
 
-                       /* if requested auto-connect the outputs to the first N physical ports.
-                        */
+        /* update latencies */
 
-                       uint32_t limit = _master_out->n_outputs().n_total();
+        initialize_latencies ();
 
-                       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];
-                               }
+       /* hook us up to the engine */
 
-                               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;
-                                       }
-                               }
+       BootMessage (_("Connect to engine"));
+       _engine.set_session (this);
+       _engine.reset_timebase ();
+}
+
+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-Place */
+       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::Threads::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) {
-                                       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);
+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::Threads::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, 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, true, 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) {
-
-                                                       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;
-                                                               }
-                                                       }
+                       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;
                                                }
                                        }
                                }
@@ -640,16 +798,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::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
 
-        /* update latencies */
+       /* 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.
+       */
 
-        initialize_latencies ();
 
-       /* hook us up to the engine */
+       boost::shared_ptr<RouteList> rls = routes.reader ();
 
-       BootMessage (_("Connect to engine"));
-       _engine.set_session (this);
+       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
@@ -690,35 +864,7 @@ Session::hookup_io ()
        /* Tell all IO objects to connect themselves together */
 
        IO::enable_connecting ();
-       MIDI::Port::MakeConnections ();
-
-       /* Now reset all panners */
-
-       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 ();
-                       }
-               }
-       }
+       MIDI::JackMIDIPort::MakeConnections ();
 
        /* Anyone who cares about input state, wake up and do something */
 
@@ -780,21 +926,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)
 {
@@ -824,10 +960,25 @@ Session::auto_punch_changed (Location* location)
        replace_event (SessionEvent::PunchOut, when_to_stop);
 }
 
+/** @param loc A loop location.
+ *  @param pos Filled in with the start time of the required fade-out (in session frames).
+ *  @param length Filled in with the length of the required fade-out.
+ */
+void
+Session::auto_loop_declick_range (Location* loc, framepos_t & pos, framepos_t & length)
+{
+       pos = max (loc->start(), loc->end() - 64);
+       length = loc->end() - pos;
+}
+
 void
 Session::auto_loop_changed (Location* location)
 {
        replace_event (SessionEvent::AutoLoop, location->end(), location->start());
+       framepos_t dcp;
+       framecnt_t dcl;
+       auto_loop_declick_range (location, dcp, dcl);
+       replace_event (SessionEvent::AutoLoopDeclick, dcp, dcl);
 
        if (transport_rolling() && play_loop) {
 
@@ -905,6 +1056,10 @@ Session::set_auto_loop_location (Location* location)
                loop_connections.drop_connections ();
                existing->set_auto_loop (false, this);
                remove_event (existing->end(), SessionEvent::AutoLoop);
+               framepos_t dcp;
+               framecnt_t dcl;
+               auto_loop_declick_range (existing, dcp, dcl);
+               remove_event (dcp, SessionEvent::AutoLoopDeclick);
                auto_loop_location_changed (0);
        }
 
@@ -1134,7 +1289,7 @@ Session::audible_frame () const
                   of audible frames, we have not moved yet.
 
                   `Start position' in this context means the time we last
-                  either started or changed transport direction.
+                  either started, located, or changed transport direction.
                */
 
                if (_transport_speed > 0.0f) {
@@ -1293,8 +1448,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) {
@@ -1313,51 +1466,100 @@ Session::resort_routes ()
 
 }
 
+/** 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)
 {
-       DAGEdges edges;
-
+       /* 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 (RouteList::iterator 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;
-                       }
-
                        bool via_sends_only;
 
-                       if ((*j)->direct_feeds (*i, &via_sends_only)) {
-                               edges.add (*j, *i);
+                       /* 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 (RouteList::iterator 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.
+
+                  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;
+
+               /* 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);
+               }
 
-       boost::shared_ptr<RouteList> sorted_routes = topographical_sort (r, edges);
-       route_graph->rechain (sorted_routes);
+               *r = *sorted_routes;
 
 #ifndef NDEBUG
-       DEBUG_TRACE (DEBUG::Graph, "Routes resorted, order follows:\n");
-       for (RouteList::iterator i = sorted_routes->begin(); i != sorted_routes->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 (MixerSort)));
+               }
 #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
@@ -1414,24 +1616,25 @@ 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".
+ *  @param instrument plugin info for the instrument to insert pre-fader, if any
  */
 list<boost::shared_ptr<MidiTrack> >
-Session::new_midi_track (TrackMode mode, RouteGroup* route_group, uint32_t how_many, string name_template)
+Session::new_midi_track (const ChanCount& input, const ChanCount& output, boost::shared_ptr<PluginInfo> instrument, 
+                        TrackMode mode, RouteGroup* route_group, uint32_t how_many, string name_template)
 {
        char track_name[32];
        uint32_t track_id = 0;
        string port;
        RouteList new_routes;
        list<boost::shared_ptr<MidiTrack> > ret;
-       uint32_t control_id;
 
-       control_id = ntracks() + nbusses();
+       cerr << "Adding MIDI track with in = " << input << " out = " << output << endl;
 
-       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;
                }
@@ -1451,14 +1654,14 @@ Session::new_midi_track (TrackMode mode, RouteGroup* route_group, uint32_t how_m
                        // boost_debug_shared_ptr_mark_interesting (track.get(), "Track");
 #endif
                        {
-                               Glib::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
-                               if (track->input()->ensure_io (ChanCount(DataType::MIDI, 1), false, this)) {
-                                       error << "cannot configure 1 in/1 out configuration for new midi track" << endmsg;
+                               Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
+                               if (track->input()->ensure_io (input, false, this)) {
+                                       error << "cannot configure " << input << " out configuration for new midi track" << endmsg;     
                                        goto failed;
                                }
 
-                               if (track->output()->ensure_io (ChanCount(DataType::MIDI, 1), false, this)) {
-                                       error << "cannot configure 1 in/1 out configuration for new midi track" << endmsg;
+                               if (track->output()->ensure_io (output, false, this)) {
+                                       error << "cannot configure " << output << " out configuration for new midi track" << endmsg;
                                        goto failed;
                                }
                        }
@@ -1470,7 +1673,10 @@ Session::new_midi_track (TrackMode mode, RouteGroup* route_group, uint32_t how_m
                        }
 
                        track->DiskstreamChanged.connect_same_thread (*this, boost::bind (&Session::resort_routes, this));
-                       track->set_remote_control_id (control_id);
+
+                       if (Config->get_remote_model() == UserOrdered) {
+                               track->set_remote_control_id (next_control_id());
+                       }
 
                        new_routes.push_back (track);
                        ret.push_back (track);
@@ -1492,7 +1698,16 @@ Session::new_midi_track (TrackMode mode, RouteGroup* route_group, uint32_t how_m
 
   failed:
        if (!new_routes.empty()) {
-               add_routes (new_routes, true, true);
+               add_routes (new_routes, true, true, true);
+
+               if (instrument) {
+                       for (RouteList::iterator r = new_routes.begin(); r != new_routes.end(); ++r) {
+                               PluginPtr plugin = instrument->load (*this);
+                               boost::shared_ptr<Processor> p (new PluginInsert (*this, plugin));
+                               (*r)->add_processor (p, PreFader);
+                               
+                       }
+               }
        }
 
        return ret;
@@ -1539,7 +1754,7 @@ Session::auto_connect_route (boost::shared_ptr<Route> route, ChanCount& existing
                return;
        }
 
-       Glib::Mutex::Lock lm (AudioEngine::instance()->process_lock (), Glib::NOT_LOCK);
+       Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock (), Glib::Threads::NOT_LOCK);
 
        if (with_lock) {
                lm.acquire ();
@@ -1642,18 +1857,14 @@ 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;
        string port;
        RouteList new_routes;
        list<boost::shared_ptr<AudioTrack> > ret;
-       uint32_t control_id;
-
-       control_id = ntracks() + nbusses() + 1;
 
        bool const use_number = (how_many != 1) || name_template.empty () || name_template == _("Audio");
 
@@ -1678,7 +1889,7 @@ Session::new_audio_track (
                        // boost_debug_shared_ptr_mark_interesting (track.get(), "Track");
 #endif
                        {
-                               Glib::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
+                               Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
 
                                if (track->input()->ensure_io (ChanCount(DataType::AUDIO, input_channels), false, this)) {
                                        error << string_compose (
@@ -1704,8 +1915,9 @@ Session::new_audio_track (
                        track->non_realtime_input_change();
 
                        track->DiskstreamChanged.connect_same_thread (*this, boost::bind (&Session::resort_routes, this));
-                       track->set_remote_control_id (control_id);
-                       ++control_id;
+                       if (Config->get_remote_model() == UserOrdered) {
+                               track->set_remote_control_id (next_control_id());
+                       }
 
                        new_routes.push_back (track);
                        ret.push_back (track);
@@ -1727,39 +1939,12 @@ Session::new_audio_track (
 
   failed:
        if (!new_routes.empty()) {
-               add_routes (new_routes, true, true);
+               add_routes (new_routes, true, true, true);
        }
 
        return ret;
 }
 
-void
-Session::set_remote_control_ids ()
-{
-       RemoteModel m = Config->get_remote_model();
-       bool emit_signal = false;
-
-       boost::shared_ptr<RouteList> r = routes.reader ();
-
-       for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
-               if (MixerOrdered == m) {
-                       int32_t order = (*i)->order_key(N_("signal"));
-                       (*i)->set_remote_control_id (order+1, false);
-                       emit_signal = true;
-               } else if (EditorOrdered == m) {
-                       int32_t order = (*i)->order_key(N_("editor"));
-                       (*i)->set_remote_control_id (order+1, false);
-                       emit_signal = true;
-               } else if (UserOrdered == m) {
-                       //do nothing ... only changes to remote id's are initiated by user
-               }
-       }
-
-       if (emit_signal) {
-               Route::RemoteControlIDChange();
-       }
-}
-
 /** Caller must not hold process lock.
  *  @param name_template string to use for the start of the name, or "" to use "Bus".
  */
@@ -1770,9 +1955,6 @@ Session::new_audio_route (int input_channels, int output_channels, RouteGroup* r
        uint32_t bus_id = 0;
        string port;
        RouteList ret;
-       uint32_t control_id;
-
-       control_id = ntracks() + nbusses() + 1;
 
        bool const use_number = (how_many != 1) || name_template.empty () || name_template == _("Bus");
        
@@ -1793,7 +1975,7 @@ Session::new_audio_route (int input_channels, int output_channels, RouteGroup* r
                        // boost_debug_shared_ptr_mark_interesting (bus.get(), "Route");
 #endif
                        {
-                               Glib::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
+                               Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
 
                                if (bus->input()->ensure_io (ChanCount(DataType::AUDIO, input_channels), false, this)) {
                                        error << string_compose (_("cannot configure %1 in/%2 out configuration for new audio track"),
@@ -1814,8 +1996,9 @@ Session::new_audio_route (int input_channels, int output_channels, RouteGroup* r
                        if (route_group) {
                                route_group->add (bus);
                        }
-                       bus->set_remote_control_id (control_id);
-                       ++control_id;
+                       if (Config->get_remote_model() == UserOrdered) {
+                               bus->set_remote_control_id (next_control_id());
+                       }
 
                        bus->add_internal_return ();
 
@@ -1839,7 +2022,7 @@ Session::new_audio_route (int input_channels, int output_channels, RouteGroup* r
 
   failure:
        if (!ret.empty()) {
-               add_routes (ret, false, true);
+               add_routes (ret, false, true, true); // autoconnect outputs only
        }
 
        return ret;
@@ -1849,7 +2032,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;
@@ -1861,31 +2043,41 @@ Session::new_route_from_template (uint32_t how_many, const std::string& template
 
        XMLNode* node = tree.root();
 
-       control_id = ntracks() + nbusses() + 1;
+       IO::disable_connecting ();
 
-       while (how_many) {
+       control_id = next_control_id ();
 
-               XMLNode node_copy (*node); // make a copy so we can change the name if we need to
+       while (how_many) {
 
-               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) {
@@ -1899,7 +2091,7 @@ Session::new_route_from_template (uint32_t how_many, const std::string& template
                                   loading this normally happens in a different way.
                                */
 
-                               Glib::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
+                               Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
 
                                IOChange change (IOChange::Type (IOChange::ConfigurationChanged | IOChange::ConnectionsChanged));
                                change.after = route->input()->n_ports();
@@ -1929,17 +2121,19 @@ Session::new_route_from_template (uint32_t how_many, const std::string& template
 
   out:
        if (!ret.empty()) {
-               add_routes (ret, true, true);
+               add_routes (ret, true, true, true);
+               IO::enable_connecting ();
        }
 
        return ret;
 }
 
 void
-Session::add_routes (RouteList& new_routes, bool auto_connect, bool save)
+Session::add_routes (RouteList& new_routes, bool input_auto_connect, bool output_auto_connect, bool save)
 {
         ChanCount existing_inputs;
         ChanCount existing_outputs;
+       uint32_t order = next_control_id();
 
         count_existing_track_channels (existing_inputs, existing_outputs);
 
@@ -1970,7 +2164,6 @@ Session::add_routes (RouteList& new_routes, bool auto_connect, bool save)
                r->mute_changed.connect_same_thread (*this, boost::bind (&Session::route_mute_changed, this, _1));
                r->output()->changed.connect_same_thread (*this, boost::bind (&Session::set_worst_io_latencies_x, this, _1, _2));
                r->processors_changed.connect_same_thread (*this, boost::bind (&Session::route_processors_changed, this, _1));
-               r->order_key_changed.connect_same_thread (*this, boost::bind (&Session::route_order_key_changed, this));
 
                if (r->is_master()) {
                        _master_out = r;
@@ -1993,20 +2186,42 @@ Session::add_routes (RouteList& new_routes, bool auto_connect, bool save)
                        }
                }
 
-               if (auto_connect) {
-                       auto_connect_route (r, existing_inputs, existing_outputs, true);
+               if (input_auto_connect || output_auto_connect) {
+                       auto_connect_route (r, existing_inputs, existing_outputs, true, input_auto_connect);
+               }
+
+               /* order keys are a GUI responsibility but we need to set up
+                  reasonable defaults because they also affect the remote control
+                  ID in most situations.
+               */
+               
+               if (!r->has_order_key (EditorSort)) {
+                       if (r->is_hidden()) {
+                               /* use an arbitrarily high value */
+                               r->set_order_key (EditorSort, UINT_MAX);
+                               r->set_order_key (MixerSort, UINT_MAX);
+                       } else {
+                               DEBUG_TRACE (DEBUG::OrderKeys, string_compose ("while adding, set %1 to order key %2\n", r->name(), order));
+                               r->set_order_key (EditorSort, order);
+                               r->set_order_key (MixerSort, order);
+                               order++;
+                       }
                }
        }
 
        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::Threads::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 ();
+                               }
                        }
                }
 
@@ -2020,7 +2235,6 @@ Session::add_routes (RouteList& new_routes, bool auto_connect, bool save)
        }
 
        RouteAdded (new_routes); /* EMIT SIGNAL */
-       Route::RemoteControlIDChange (); /* EMIT SIGNAL */
 }
 
 void
@@ -2082,22 +2296,29 @@ Session::globally_add_internal_sends (boost::shared_ptr<Route> dest, Placement p
 void
 Session::add_internal_sends (boost::shared_ptr<Route> dest, Placement p, boost::shared_ptr<RouteList> senders)
 {
-       if (dest->is_monitor() || dest->is_master()) {
+       for (RouteList::iterator i = senders->begin(); i != senders->end(); ++i) {
+               add_internal_send (dest, (*i)->before_processor_for_placement (p), *i);
+       }
+}
+
+void
+Session::add_internal_send (boost::shared_ptr<Route> dest, int index, boost::shared_ptr<Route> sender)
+{
+       add_internal_send (dest, sender->before_processor_for_index (index), sender);
+}
+
+void
+Session::add_internal_send (boost::shared_ptr<Route> dest, boost::shared_ptr<Processor> before, boost::shared_ptr<Route> sender)
+{
+       if (sender->is_monitor() || sender->is_master() || sender == dest || dest->is_monitor() || dest->is_master()) {
                return;
        }
 
        if (!dest->internal_return()) {
-               dest->add_internal_return();
+               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);
-       }
+       sender->add_aux_send (dest, before);
 
        graph_reordered ();
 }
@@ -2105,7 +2326,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;
        }
 
@@ -2127,13 +2348,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 ();
                }
 
@@ -2174,7 +2388,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 */
 
@@ -2186,8 +2402,6 @@ Session::remove_route (boost::shared_ptr<Route> route)
 
        route->drop_references ();
 
-       sync_order_keys (N_("session"));
-
        Route::RemoteControlIDChange(); /* EMIT SIGNAL */
 
        /* save the new state of the world */
@@ -2548,6 +2762,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)
 {
@@ -2654,7 +2883,7 @@ Session::find_whole_file_parent (boost::shared_ptr<Region const> child) const
        RegionFactory::RegionMap::const_iterator i;
        boost::shared_ptr<Region> region;
 
-       Glib::Mutex::Lock lm (region_lock);
+       Glib::Threads::Mutex::Lock lm (region_lock);
 
        for (i = regions.begin(); i != regions.end(); ++i) {
 
@@ -2700,7 +2929,7 @@ Session::destroy_sources (list<boost::shared_ptr<Source> > srcs)
        for (list<boost::shared_ptr<Source> >::iterator s = srcs.begin(); s != srcs.end(); ) {
 
                {
-                       Glib::Mutex::Lock ls (source_lock);
+                       Glib::Threads::Mutex::Lock ls (source_lock);
                        /* remove from the main source list */
                        sources.erase ((*s)->id());
                }
@@ -2753,7 +2982,7 @@ Session::add_source (boost::shared_ptr<Source> source)
        entry.second = source;
 
        {
-               Glib::Mutex::Lock lm (source_lock);
+               Glib::Threads::Mutex::Lock lm (source_lock);
                result = sources.insert (entry);
        }
 
@@ -2761,6 +2990,14 @@ Session::add_source (boost::shared_ptr<Source> source)
 
                /* yay, new source */
 
+               boost::shared_ptr<FileSource> fs = boost::dynamic_pointer_cast<FileSource> (source);
+               
+               if (fs) {
+                       if (!fs->within_session()) {
+                               ensure_search_path_includes (Glib::path_get_dirname (fs->path()), fs->type());
+                       }
+               }
+               
                set_dirty();
 
                boost::shared_ptr<AudioFileSource> afs;
@@ -2790,14 +3027,14 @@ Session::remove_source (boost::weak_ptr<Source> src)
        }
 
        {
-               Glib::Mutex::Lock lm (source_lock);
+               Glib::Threads::Mutex::Lock lm (source_lock);
 
                if ((i = sources.find (source->id())) != sources.end()) {
                        sources.erase (i);
                }
        }
 
-       if (!_state_of_the_state & InCleanup) {
+       if (!(_state_of_the_state & InCleanup)) {
 
                /* save state so we don't end up with a session file
                   referring to non-existent sources.
@@ -2810,7 +3047,7 @@ Session::remove_source (boost::weak_ptr<Source> src)
 boost::shared_ptr<Source>
 Session::source_by_id (const PBD::ID& id)
 {
-       Glib::Mutex::Lock lm (source_lock);
+       Glib::Threads::Mutex::Lock lm (source_lock);
        SourceMap::iterator i;
        boost::shared_ptr<Source> source;
 
@@ -2824,7 +3061,7 @@ Session::source_by_id (const PBD::ID& id)
 boost::shared_ptr<Source>
 Session::source_by_path_and_channel (const string& path, uint16_t chn)
 {
-       Glib::Mutex::Lock lm (source_lock);
+       Glib::Threads::Mutex::Lock lm (source_lock);
 
        for (SourceMap::iterator i = sources.begin(); i != sources.end(); ++i) {
                boost::shared_ptr<AudioFileSource> afs
@@ -2841,7 +3078,7 @@ uint32_t
 Session::count_sources_by_origin (const string& path)
 {
        uint32_t cnt = 0;
-       Glib::Mutex::Lock lm (source_lock);
+       Glib::Threads::Mutex::Lock lm (source_lock);
 
        for (SourceMap::iterator i = sources.begin(); i != sources.end(); ++i) {
                boost::shared_ptr<FileSource> fs
@@ -2971,7 +3208,7 @@ Session::new_source_path_from_name (DataType type, const string& name)
 
        SessionDirectory sdir(get_best_session_directory_for_new_source());
 
-       sys::path p;
+       std::string p;
        if (type == DataType::AUDIO) {
                p = sdir.sound_path();
        } else if (type == DataType::MIDI) {
@@ -2981,16 +3218,13 @@ Session::new_source_path_from_name (DataType type, const string& name)
                return "";
        }
 
-       p /= name;
-       return p.to_string();
+       return Glib::build_filename (p, name);
 }
 
 string
 Session::peak_path (string base) const
 {
-       sys::path peakfile_path(_session_dir->peak_path());
-       peakfile_path /= base + peakfile_suffix;
-       return peakfile_path.to_string();
+       return Glib::build_filename (_session_dir->peak_path(), base + peakfile_suffix);
 }
 
 /** Return a unique name based on \a base for a new internal audio source */
@@ -3054,7 +3288,7 @@ Session::new_audio_source_name (const string& base, uint32_t nchan, uint32_t cha
 
                        SessionDirectory sdir((*i).path);
 
-                       string spath = sdir.sound_path().to_string();
+                       string spath = sdir.sound_path();
 
                        /* note that we search *without* the extension so that
                           we don't end up both "Audio 1-1.wav" and "Audio 1-1.caf"
@@ -3117,12 +3351,11 @@ Session::new_midi_source_name (const string& base)
 
                        SessionDirectory sdir((*i).path);
 
-                       sys::path p = sdir.midi_path();
-                       p /= legalized;
+                       std::string p = Glib::build_filename (sdir.midi_path(), legalized);
 
-                       snprintf (buf, sizeof(buf), "%s-%u.mid", p.to_string().c_str(), cnt);
+                       snprintf (buf, sizeof(buf), "%s-%u.mid", p.c_str(), cnt);
 
-                       if (sys::exists (buf)) {
+                       if (Glib::file_test (buf, Glib::FILE_TEST_EXISTS)) {
                                existing++;
                        }
                }
@@ -3226,12 +3459,9 @@ Session::audition_playlist ()
 void
 Session::non_realtime_set_audition ()
 {
-       if (!pending_audition_region) {
-               auditioner->audition_current_playlist ();
-       } else {
-               auditioner->audition_region (pending_audition_region);
-               pending_audition_region.reset ();
-       }
+       assert (pending_audition_region);
+       auditioner->audition_region (pending_audition_region);
+       pending_audition_region.reset ();
        AuditionActive (true); /* EMIT SIGNAL */
 }
 
@@ -3261,7 +3491,7 @@ Session::RoutePublicOrderSorter::operator() (boost::shared_ptr<Route> a, boost::
        if (b->is_monitor()) {
                return false;
        }
-       return a->order_key(N_("signal")) < b->order_key(N_("signal"));
+       return a->order_key (MixerSort) < b->order_key (MixerSort);
 }
 
 bool
@@ -3307,9 +3537,18 @@ Session::graph_reordered ()
        }
 }
 
-framecnt_t
+/** @return Number of frames that there is disk space available to write,
+ *  if known.
+ */
+boost::optional<framecnt_t>
 Session::available_capture_duration ()
 {
+       Glib::Threads::Mutex::Lock lm (space_lock);
+
+       if (_total_free_4k_blocks_uncertain) {
+               return boost::optional<framecnt_t> ();
+       }
+       
        float sample_bytes_on_disk = 4.0; // keep gcc happy
 
        switch (config.get_native_file_data_format()) {
@@ -3470,6 +3709,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 ()
 {
@@ -3502,6 +3761,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)
 {
@@ -3534,6 +3805,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)
 {
@@ -3550,56 +3829,6 @@ Session::unmark_insert_id (uint32_t id)
        }
 }
 
-
-/* Named Selection management */
-
-boost::shared_ptr<NamedSelection>
-Session::named_selection_by_name (string name)
-{
-       Glib::Mutex::Lock lm (named_selection_lock);
-       for (NamedSelectionList::iterator i = named_selections.begin(); i != named_selections.end(); ++i) {
-               if ((*i)->name == name) {
-                       return *i;
-               }
-       }
-       return boost::shared_ptr<NamedSelection>();
-}
-
-void
-Session::add_named_selection (boost::shared_ptr<NamedSelection> named_selection)
-{
-       {
-               Glib::Mutex::Lock lm (named_selection_lock);
-               named_selections.insert (named_selections.begin(), named_selection);
-       }
-
-       set_dirty();
-
-       NamedSelectionAdded (); /* EMIT SIGNAL */
-}
-
-void
-Session::remove_named_selection (boost::shared_ptr<NamedSelection> named_selection)
-{
-       bool removed = false;
-
-       {
-               Glib::Mutex::Lock lm (named_selection_lock);
-
-               NamedSelectionList::iterator i = find (named_selections.begin(), named_selections.end(), named_selection);
-
-               if (i != named_selections.end()) {
-                       named_selections.erase (i);
-                       set_dirty();
-                       removed = true;
-               }
-       }
-
-       if (removed) {
-                NamedSelectionRemoved (); /* EMIT SIGNAL */
-       }
-}
-
 void
 Session::reset_native_file_format ()
 {
@@ -3668,7 +3897,9 @@ Session::freeze_all (InterThreadInfo& itt)
 boost::shared_ptr<Region>
 Session::write_one_track (AudioTrack& track, framepos_t start, framepos_t end,
                          bool /*overwrite*/, vector<boost::shared_ptr<Source> >& srcs,
-                         InterThreadInfo& itt, bool enable_processing)
+                         InterThreadInfo& itt, 
+                         boost::shared_ptr<Processor> endpoint, bool include_endpoint,
+                         bool for_export)
 {
        boost::shared_ptr<Region> result;
        boost::shared_ptr<Playlist> playlist;
@@ -3681,7 +3912,7 @@ Session::write_one_track (AudioTrack& track, framepos_t start, framepos_t end,
        framepos_t to_do;
        BufferSet buffers;
        SessionDirectory sdir(get_best_session_directory_for_new_source ());
-       const string sound_dir = sdir.sound_path().to_string();
+       const string sound_dir = sdir.sound_path();
        framepos_t len = end - start;
        bool need_block_size_reset = false;
        string ext;
@@ -3705,12 +3936,6 @@ Session::write_one_track (AudioTrack& track, framepos_t start, framepos_t end,
                goto out;
        }
 
-       /* external redirects will be a problem */
-
-       if (track.has_external_redirects()) {
-               goto out;
-       }
-
        ext = native_header_format_extension (config.get_native_file_header_format(), DataType::AUDIO);
 
        for (uint32_t chan_n = 0; chan_n < diskstream_channels.n_audio(); ++chan_n) {
@@ -3740,13 +3965,14 @@ Session::write_one_track (AudioTrack& track, framepos_t start, framepos_t end,
                srcs.push_back (fsource);
        }
 
-       /* tell redirects that care that we are about to use a much larger blocksize */
+       /* tell redirects that care that we are about to use a much larger
+        * blocksize. this will flush all plugins too, so that they are ready
+        * to be used for this process.
+        */
 
        need_block_size_reset = true;
        track.set_block_size (chunk_size);
 
-       /* XXX need to flush all redirects */
-
        position = start;
        to_do = len;
 
@@ -3764,7 +3990,7 @@ Session::write_one_track (AudioTrack& track, framepos_t start, framepos_t end,
 
                this_chunk = min (to_do, chunk_size);
 
-               if (track.export_stuff (buffers, start, this_chunk, enable_processing)) {
+               if (track.export_stuff (buffers, start, this_chunk, endpoint, include_endpoint, for_export)) {
                        goto out;
                }
 
@@ -3851,6 +4077,12 @@ Session::gain_automation_buffer() const
        return ProcessThread::gain_automation_buffer ();
 }
 
+gain_t*
+Session::send_gain_automation_buffer() const
+{
+       return ProcessThread::send_gain_automation_buffer ();
+}
+
 pan_t**
 Session::pan_automation_buffer() const
 {
@@ -3911,31 +4143,6 @@ Session::add_automation_list(AutomationList *al)
        automation_lists[al->id()] = al;
 }
 
-void
-Session::sync_order_keys (std::string const & base)
-{
-       if (deletion_in_progress()) {
-               return;
-       }
-
-       if (!Config->get_sync_all_route_ordering()) {
-               /* leave order keys as they are */
-               return;
-       }
-
-       boost::shared_ptr<RouteList> r = routes.reader ();
-
-       for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
-               (*i)->sync_order_keys (base);
-       }
-
-       Route::SyncOrderKeys (base); // EMIT SIGNAL
-
-       /* this might not do anything */
-
-       set_remote_control_ids ();
-}
-
 /** @return true if there is at least one record-enabled track, otherwise false */
 bool
 Session::have_rec_enabled_track () const
@@ -4011,18 +4218,6 @@ Session::route_removed_from_route_group (RouteGroup* rg, boost::weak_ptr<Route>
        RouteRemovedFromRouteGroup (rg, r);
 }
 
-vector<SyncSource>
-Session::get_available_sync_options () const
-{
-       vector<SyncSource> ret;
-
-       ret.push_back (JACK);
-       ret.push_back (MTC);
-       ret.push_back (MIDIClock);
-
-       return ret;
-}
-
 boost::shared_ptr<RouteList>
 Session::get_routes_with_regions_at (framepos_t const p) const
 {
@@ -4087,13 +4282,6 @@ Session::add_session_range_location (framepos_t start, framepos_t end)
        _locations->add (_session_range_location);
 }
 
-/** Called when one of our routes' order keys has changed */
-void
-Session::route_order_key_changed ()
-{
-       RouteOrderKeyChanged (); /* EMIT SIGNAL */
-}
-
 void
 Session::step_edit_status_change (bool yn)
 {
@@ -4134,7 +4322,7 @@ Session::start_time_changed (framepos_t old)
 
        Location* l = _locations->auto_loop_location ();
 
-       if (l->start() == old) {
+       if (l && l->start() == old) {
                l->set_start (s->start(), true);
        }
 }
@@ -4161,35 +4349,39 @@ 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());
                        break;
                case DataType::MIDI:
-                       search_path = _session_dir->midi_path().to_string();
+                       s.push_back (_session_dir->midi_path());
                        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());
                                break;
                        case DataType::MIDI:
-                               search_path += sdir.midi_path().to_string();
+                               s.push_back (sdir.midi_path());
                                break;
                        }
                }
        }
 
-       /* now add user-specified locations
+       if (type == DataType::AUDIO) {
+               const string sound_path_2X = _session_dir->sound_path_2X();
+               if (Glib::file_test (sound_path_2X, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_DIR)) {
+                       s.push_back (sound_path_2X);
+               }
+       }
+
+       /* now check the explicit (possibly user-specified) search path
         */
 
        vector<string> dirs;
@@ -4204,9 +4396,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;
@@ -4234,7 +4444,13 @@ Session::ensure_search_path_includes (const string& path, DataType type)
        split (search_path, dirs, ':');
 
        for (vector<string>::iterator i = dirs.begin(); i != dirs.end(); ++i) {
-               if (*i == path) {
+               /* No need to add this new directory if it has the same inode as
+                  an existing one; checking inode rather than name prevents duplicated
+                  directories when we are using symlinks.
+
+                  On Windows, I think we could just do if (*i == path) here.
+               */
+               if (PBD::equivalent_paths (*i, path)) {
                        return;
                }
        }
@@ -4370,7 +4586,7 @@ void
 Session::initialize_latencies ()
 {
         {
-                Glib::Mutex::Lock lm (_engine.process_lock());
+                Glib::Threads::Mutex::Lock lm (_engine.process_lock());
                 update_latency (false);
                 update_latency (true);
         }
@@ -4473,3 +4689,120 @@ Session::update_latency_compensation (bool force_whole_graph)
        }
 }
 
+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
+{
+       int subtract = 0;
+
+       /* the monitor bus remote ID is in a different
+        * "namespace" than regular routes. its existence doesn't
+        * affect normal (low) numbered routes.
+        */
+
+       if (_monitor_out) {
+               subtract++;
+       }
+
+       return nroutes() - subtract;
+}
+
+void
+Session::notify_remote_id_change ()
+{
+       if (deletion_in_progress()) {
+               return;
+       }
+
+       switch (Config->get_remote_model()) {
+       case MixerSort:
+       case EditorSort:
+               Route::RemoteControlIDChange (); /* EMIT SIGNAL */
+               break;
+       default:
+               break;
+       }
+}
+
+void
+Session::sync_order_keys (RouteSortOrderKey sort_key_changed)
+{
+       if (deletion_in_progress()) {
+               return;
+       }
+
+       /* tell everyone that something has happened to the sort keys
+          and let them sync up with the change(s)
+          this will give objects that manage the sort order keys the
+          opportunity to keep them in sync if they wish to.
+       */
+
+       DEBUG_TRACE (DEBUG::OrderKeys, string_compose ("Sync Order Keys, based on %1\n", enum_2_string (sort_key_changed)));
+
+       Route::SyncOrderKeys (sort_key_changed); /* EMIT SIGNAL */
+
+       DEBUG_TRACE (DEBUG::OrderKeys, "\tsync done\n");
+}
+
+bool
+Session::operation_in_progress (GQuark op) const
+{
+       return (find (_current_trans_quarks.begin(), _current_trans_quarks.end(), op) != _current_trans_quarks.end());
+}
+
+boost::shared_ptr<Port>
+Session::ltc_input_port () const
+{
+       return _ltc_input->nth (0);
+}
+
+boost::shared_ptr<Port>
+Session::ltc_output_port () const
+{
+       return _ltc_output->nth (0);
+}
+
+void
+Session::reconnect_ltc_input ()
+{
+       if (_ltc_input) {
+
+               string src = Config->get_ltc_source_port();
+
+               _ltc_input->disconnect (this);
+
+               if (src != _("None") && !src.empty())  {
+                       _ltc_input->nth (0)->connect (src);
+               }
+       }
+}
+
+void
+Session::reconnect_ltc_output ()
+{
+       if (_ltc_output) {
+
+#if 0
+               string src = Config->get_ltc_sink_port();
+
+               _ltc_output->disconnect (this);
+
+               if (src != _("None") && !src.empty())  {
+                       _ltc_output->nth (0)->connect (src);
+               }
+#endif
+       }
+}