Merged with trunk R776
[ardour.git] / libs / ardour / session.cc
index d7d7e290ed60945058968872f8c23520fdcb2242..bfcb69572a8f80d97e7cf7c2859eecf92c91bd67 100644 (file)
 #include <ardour/audioengine.h>
 #include <ardour/configuration.h>
 #include <ardour/session.h>
-#include <ardour/audio_diskstream.h>
 #include <ardour/utils.h>
+#include <ardour/audio_diskstream.h>
 #include <ardour/audioplaylist.h>
 #include <ardour/audioregion.h>
 #include <ardour/audiofilesource.h>
+#include <ardour/midi_diskstream.h>
+#include <ardour/midi_playlist.h>
+#include <ardour/midi_region.h>
+#include <ardour/smf_source.h>
 #include <ardour/destructive_filesource.h>
 #include <ardour/auditioner.h>
 #include <ardour/recent_sessions.h>
 #include <ardour/slave.h>
 #include <ardour/tempo.h>
 #include <ardour/audio_track.h>
+#include <ardour/midi_track.h>
 #include <ardour/cycle_timer.h>
 #include <ardour/named_selection.h>
 #include <ardour/crossfade.h>
 #include <ardour/playlist.h>
 #include <ardour/click.h>
+#include <ardour/data_type.h>
 
 #ifdef HAVE_LIBLO
 #include <ardour/osc.h>
@@ -73,7 +79,8 @@
 
 using namespace std;
 using namespace ARDOUR;
-//using namespace sigc;
+using namespace PBD;
+using boost::shared_ptr;
 
 const char* Session::_template_suffix = X_(".template");
 const char* Session::_statefile_suffix = X_(".ardour");
@@ -90,6 +97,8 @@ Session::mix_buffers_no_gain_t                Session::mix_buffers_no_gain    = 0;
 
 sigc::signal<int> Session::AskAboutPendingState;
 sigc::signal<void> Session::SMPTEOffsetChanged;
+sigc::signal<void> Session::SendFeedback;
+
 
 int
 Session::find_session (string str, string& path, string& snapshot, bool& isnew)
@@ -253,6 +262,9 @@ Session::Session (AudioEngine &eng,
          pending_events (2048),
          //midi_requests (128), // the size of this should match the midi request pool size
          _send_smpte_update (false),
+         routes (new RouteList),
+         auditioner ((Auditioner*) 0),
+         _click_io ((IO*) 0),
          main_outs (0)
 {
        bool new_session;
@@ -300,6 +312,8 @@ Session::Session (AudioEngine &eng,
          _midi_port (default_midi_port),
          pending_events (2048),
          //midi_requests (16),
+         _send_smpte_update (false),
+         routes (new RouteList),
          main_outs (0)
 
 {
@@ -317,15 +331,13 @@ Session::Session (AudioEngine &eng,
        }
 
        if (control_out_channels) {
-               Route* r;
-               r = new Route (*this, _("monitor"), -1, control_out_channels, -1, control_out_channels, Route::ControlOut);
+               shared_ptr<Route> r (new Route (*this, _("monitor"), -1, control_out_channels, -1, control_out_channels, Route::ControlOut));
                add_route (r);
                _control_out = r;
        }
 
        if (master_out_channels) {
-               Route* r;
-               r = new Route (*this, _("master"), -1, master_out_channels, -1, master_out_channels, Route::MasterOut);
+               shared_ptr<Route> r (new Route (*this, _("master"), -1, master_out_channels, -1, master_out_channels, Route::MasterOut));
                add_route (r);
                _master_out = r;
        } else {
@@ -377,15 +389,6 @@ Session::~Session ()
 
        clear_clicks ();
 
-       if (_click_io) {
-               delete _click_io;
-       }
-
-
-       if (auditioner) {
-               delete auditioner;
-       }
-
        for (vector<Sample*>::iterator i = _passthru_buffers.begin(); i != _passthru_buffers.end(); ++i) {
                free(*i);
        }
@@ -401,6 +404,8 @@ Session::~Session ()
        for (map<RunContext,char*>::iterator i = _conversion_buffers.begin(); i != _conversion_buffers.end(); ++i) {
                delete [] (i->second);
        }
+
+       AudioDiskstream::free_working_buffers();
        
 #undef TRACK_DESTRUCTION
 #ifdef TRACK_DESTRUCTION
@@ -439,27 +444,16 @@ Session::~Session ()
                tmp =i;
                ++tmp;
 
-               delete (*i).second;
+               delete i->second;
 
                i = tmp;
        }
        
 #ifdef TRACK_DESTRUCTION
-       cerr << "delete routes\n";
+       cerr << "delete diskstreams\n";
 #endif /* TRACK_DESTRUCTION */
-       for (RouteList::iterator i = routes.begin(); i != routes.end(); ) {
-               RouteList::iterator tmp;
-               tmp = i;
-               ++tmp;
-               delete *i;
-               i = tmp;
-       }
-
-#ifdef TRACK_DESTRUCTION
-       cerr << "delete audio_diskstreams\n";
-#endif /* TRACK_DESTRUCTION */
-       for (AudioDiskstreamList::iterator i = audio_diskstreams.begin(); i != audio_diskstreams.end(); ) {
-               AudioDiskstreamList::iterator tmp;
+       for (DiskstreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ) {
+               DiskstreamList::iterator tmp;
 
                tmp = i;
                ++tmp;
@@ -478,7 +472,7 @@ Session::~Session ()
                tmp = i;
                ++tmp;
 
-               delete (*i).second;
+               delete i->second;
 
                i = tmp;
        }
@@ -545,7 +539,7 @@ Session::~Session ()
 }
 
 void
-Session::set_worst_io_latencies (bool take_lock)
+Session::set_worst_io_latencies ()
 {
        _worst_output_latency = 0;
        _worst_input_latency = 0;
@@ -554,18 +548,12 @@ Session::set_worst_io_latencies (bool take_lock)
                return;
        }
 
-       if (take_lock) {
-               route_lock.reader_lock ();
-       }
+       boost::shared_ptr<RouteList> r = routes.reader ();
        
-       for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
+       for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
                _worst_output_latency = max (_worst_output_latency, (*i)->output_latency());
                _worst_input_latency = max (_worst_input_latency, (*i)->input_latency());
        }
-
-       if (take_lock) {
-               route_lock.reader_unlock ();
-       }
 }
 
 void
@@ -582,7 +570,7 @@ Session::when_engine_running ()
 
        /* every time we reconnect, recompute worst case output latencies */
 
-       _engine.Running.connect (sigc::bind (mem_fun (*this, &Session::set_worst_io_latencies), true));
+       _engine.Running.connect (mem_fun (*this, &Session::set_worst_io_latencies));
 
        if (synced_to_jack()) {
                _engine.transport_stop ();
@@ -597,7 +585,7 @@ Session::when_engine_running ()
        try {
                XMLNode* child = 0;
                
-               _click_io = new ClickIO (*this, "click", 0, 0, -1, -1);
+               _click_io.reset (new ClickIO (*this, "click", 0, 0, -1, -1));
 
                if (state_tree && (child = find_named_node (*state_tree->root(), "Click")) != 0) {
 
@@ -617,8 +605,13 @@ Session::when_engine_running ()
                        
                        /* default state for Click */
 
+                       // FIXME: there's no JackPortIsAudio flag or anything like that, so this is _bad_.
+                       // we need a get_nth_physical_audio_output or similar, but the existing one just
+                       // deals with strings :/
+
                        first_physical_output = _engine.get_nth_physical_output (0);
-                       
+                       cerr << "FIXME: click type" << endl;
+
                        if (first_physical_output.length()) {
                                if (_click_io->add_output_port (first_physical_output, this)) {
                                        // relax, even though its an error
@@ -633,7 +626,7 @@ Session::when_engine_running ()
                error << _("cannot setup Click I/O") << endmsg;
        }
 
-       set_worst_io_latencies (true);
+       set_worst_io_latencies ();
 
        if (_clicking) {
                 ControlChanged (Clicking); /* EMIT SIGNAL */
@@ -648,7 +641,7 @@ Session::when_engine_running ()
                */
 
                try {
-                       auditioner = new Auditioner (*this);
+                       auditioner.reset (new Auditioner (*this));
                }
 
                catch (failed_constructor& err) {
@@ -736,7 +729,7 @@ Session::when_engine_running ()
                        _master_out->defer_pan_reset ();
                        
                        while ((int) _master_out->n_inputs() < _master_out->input_maximum()) {
-                               if (_master_out->add_input_port ("", this)) {
+                               if (_master_out->add_input_port ("", this, DataType::AUDIO)) {
                                        error << _("cannot setup master inputs") 
                                              << endmsg;
                                        break;
@@ -744,7 +737,7 @@ Session::when_engine_running ()
                        }
                        n = 0;
                        while ((int) _master_out->n_outputs() < _master_out->output_maximum()) {
-                               if (_master_out->add_output_port (_engine.get_nth_physical_output (n), this)) {
+                               if (_master_out->add_output_port (_engine.get_nth_physical_output (n), this, DataType::AUDIO)) {
                                        error << _("cannot setup master outputs")
                                              << endmsg;
                                        break;
@@ -882,7 +875,7 @@ Session::playlist_length_changed (Playlist* pl)
 }
 
 void
-Session::diskstream_playlist_changed (AudioDiskstream* dstream)
+Session::diskstream_playlist_changed (Diskstream* dstream)
 {
        Playlist *playlist;
 
@@ -962,7 +955,7 @@ Session::set_auto_input (bool yn)
                           The rarity and short potential lock duration makes this "OK"
                        */
                        Glib::RWLock::ReaderLock dsm (diskstream_lock);
-                       for (AudioDiskstreamList::iterator i = audio_diskstreams.begin(); i != audio_diskstreams.end(); ++i) {
+                       for (DiskstreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
                                if ((*i)->record_enabled ()) {
                                        //cerr << "switching to input = " << !auto_input << __FILE__ << __LINE__ << endl << endl;
                                        (*i)->monitor_input (!auto_input);   
@@ -980,7 +973,7 @@ Session::reset_input_monitor_state ()
 {
        if (transport_rolling()) {
                Glib::RWLock::ReaderLock dsm (diskstream_lock);
-               for (AudioDiskstreamList::iterator i = audio_diskstreams.begin(); i != audio_diskstreams.end(); ++i) {
+               for (DiskstreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
                        if ((*i)->record_enabled ()) {
                                //cerr << "switching to input = " << !auto_input << __FILE__ << __LINE__ << endl << endl;
                                (*i)->monitor_input (Config->get_use_hardware_monitoring() && !auto_input);
@@ -988,7 +981,7 @@ Session::reset_input_monitor_state ()
                }
        } else {
                Glib::RWLock::ReaderLock dsm (diskstream_lock);
-               for (AudioDiskstreamList::iterator i = audio_diskstreams.begin(); i != audio_diskstreams.end(); ++i) {
+               for (DiskstreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
                        if ((*i)->record_enabled ()) {
                                //cerr << "switching to input = " << !auto_input << __FILE__ << __LINE__ << endl << endl;
                                (*i)->monitor_input (Config->get_use_hardware_monitoring());
@@ -1030,6 +1023,7 @@ Session::auto_punch_start_changed (Location* location)
        if (get_record_enabled() && get_punch_in()) {
                /* capture start has been changed, so save new pending state */
                save_state ("", true);
+                save_history();
        }
 }      
 
@@ -1069,7 +1063,7 @@ Session::auto_loop_changed (Location* location)
                }
                else if (seamless_loop && !loop_changing) {
                        
-                       // schedule a locate-roll to refill the audio_diskstreams at the
+                       // schedule a locate-roll to refill the diskstreams at the
                        // previous loop end
                        loop_changing = true;
 
@@ -1266,7 +1260,7 @@ Session::enable_record ()
                        */
                        Glib::RWLock::ReaderLock dsm (diskstream_lock);
                        
-                       for (AudioDiskstreamList::iterator i = audio_diskstreams.begin(); i != audio_diskstreams.end(); ++i) {
+                       for (DiskstreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
                                if ((*i)->record_enabled ()) {
                                        (*i)->monitor_input (true);   
                                }
@@ -1305,7 +1299,7 @@ Session::disable_record (bool rt_context, bool force)
                        */
                        Glib::RWLock::ReaderLock dsm (diskstream_lock);
                        
-                       for (AudioDiskstreamList::iterator i = audio_diskstreams.begin(); i != audio_diskstreams.end(); ++i) {
+                       for (DiskstreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
                                if ((*i)->record_enabled ()) {
                                        (*i)->monitor_input (false);   
                                }
@@ -1332,7 +1326,7 @@ Session::step_back_from_record ()
                */
                Glib::RWLock::ReaderLock dsm (diskstream_lock);
                
-               for (AudioDiskstreamList::iterator i = audio_diskstreams.begin(); i != audio_diskstreams.end(); ++i) {
+               for (DiskstreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
                        if (auto_input && (*i)->record_enabled ()) {
                                //cerr << "switching from input" << __FILE__ << __LINE__ << endl << endl;
                                (*i)->monitor_input (false);   
@@ -1351,6 +1345,7 @@ Session::maybe_enable_record ()
        */
 
        save_state ("", true);
+        save_history();
 
        if (_transport_speed) {
                if (!punch_in) {
@@ -1455,7 +1450,6 @@ Session::set_block_size (jack_nframes_t nframes)
        */
 
        { 
-               Glib::RWLock::ReaderLock lm (route_lock);
                Glib::RWLock::ReaderLock dsm (diskstream_lock);
                vector<Sample*>::iterator i;
                uint32_t np;
@@ -1497,15 +1491,17 @@ Session::set_block_size (jack_nframes_t nframes)
 
                allocate_pan_automation_buffers (nframes, _npan_buffers, true);
 
-               for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
+               boost::shared_ptr<RouteList> r = routes.reader ();
+
+               for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
                        (*i)->set_block_size (nframes);
                }
                
-               for (AudioDiskstreamList::iterator i = audio_diskstreams.begin(); i != audio_diskstreams.end(); ++i) {
+               for (DiskstreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
                        (*i)->set_block_size (nframes);
                }
 
-               set_worst_io_latencies (false);
+               set_worst_io_latencies ();
        }
 }
 
@@ -1547,7 +1543,7 @@ Session::set_default_fade (float steepness, float fade_msecs)
 }
 
 struct RouteSorter {
-    bool operator() (Route* r1, Route* r2) {
+    bool operator() (boost::shared_ptr<Route> r1, boost::shared_ptr<Route> r2) {
            if (r1->fed_by.find (r2) != r1->fed_by.end()) {
                    return false;
            } else if (r2->fed_by.find (r1) != r2->fed_by.end()) {
@@ -1569,9 +1565,9 @@ struct RouteSorter {
 };
 
 static void
-trace_terminal (Route* r1, Route* rbase)
+trace_terminal (shared_ptr<Route> r1, shared_ptr<Route> rbase)
 {
-       Route* r2;
+       shared_ptr<Route> r2;
 
        if ((r1->fed_by.find (rbase) != r1->fed_by.end()) && (rbase->fed_by.find (r1) != rbase->fed_by.end())) {
                info << string_compose(_("feedback loop setup between %1 and %2"), r1->name(), rbase->name()) << endmsg;
@@ -1580,13 +1576,13 @@ trace_terminal (Route* r1, Route* rbase)
 
        /* make a copy of the existing list of routes that feed r1 */
 
-       set<Route *> existing = r1->fed_by;
+       set<shared_ptr<Route> > existing = r1->fed_by;
 
        /* for each route that feeds r1, recurse, marking it as feeding
           rbase as well.
        */
 
-       for (set<Route *>::iterator i = existing.begin(); i != existing.end(); ++i) {
+       for (set<shared_ptr<Route> >::iterator i = existing.begin(); i != existing.end(); ++i) {
                r2 =* i;
 
                /* r2 is a route that feeds r1 which somehow feeds base. mark
@@ -1616,7 +1612,7 @@ trace_terminal (Route* r1, Route* rbase)
 }
 
 void
-Session::resort_routes (void* src)
+Session::resort_routes ()
 {
        /* don't do anything here with signals emitted
           by Routes while we are being destroyed.
@@ -1626,54 +1622,257 @@ Session::resort_routes (void* src)
                return;
        }
 
-       /* Caller MUST hold the route_lock */
 
-       RouteList::iterator i, j;
+       {
 
-       for (i = routes.begin(); i != routes.end(); ++i) {
+               RCUWriter<RouteList> writer (routes);
+               shared_ptr<RouteList> r = writer.get_copy ();
+               resort_routes_using (r);
+               /* writer goes out of scope and forces update */
+       }
 
+}
+void
+Session::resort_routes_using (shared_ptr<RouteList> r)
+{
+       RouteList::iterator i, j;
+       
+       for (i = r->begin(); i != r->end(); ++i) {
+               
                (*i)->fed_by.clear ();
                
-               for (j = routes.begin(); j != routes.end(); ++j) {
-
+               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;
                        }
-
+                       
                        if ((*j)->feeds (*i)) {
                                (*i)->fed_by.insert (*j);
                        } 
                }
        }
        
-       for (i = routes.begin(); i != routes.end(); ++i) {
+       for (i = r->begin(); i != r->end(); ++i) {
                trace_terminal (*i, *i);
        }
-
+       
        RouteSorter cmp;
-       routes.sort (cmp);
-
+       r->sort (cmp);
+       
 #if 0
        cerr << "finished route resort\n";
        
-       for (i = routes.begin(); i != routes.end(); ++i) {
+       for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
                cerr << " " << (*i)->name() << " signal order = " << (*i)->order_key ("signal") << endl;
        }
        cerr << endl;
 #endif
+       
+}
 
+boost::shared_ptr<MidiTrack>
+Session::new_midi_track (TrackMode mode)
+{
+       char track_name[32];
+       uint32_t n = 0;
+       uint32_t channels_used = 0;
+       string port;
+
+       /* count existing midi tracks */
+
+       {
+               shared_ptr<RouteList> r = routes.reader ();
+
+               for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
+                       if (dynamic_cast<MidiTrack*>((*i).get()) != 0) {
+                               if (!(*i)->hidden()) {
+                                       n++;
+                                       channels_used += (*i)->n_inputs();
+                               }
+                       }
+               }
+       }
+
+       /* check for duplicate route names, since we might have pre-existing
+          routes with this name (e.g. create Midi1, Midi2, delete Midi1,
+          save, close,restart,add new route - first named route is now
+          Midi2)
+       */
+
+       do {
+               snprintf (track_name, sizeof(track_name), "Midi %" PRIu32, n+1);
+               if (route_by_name (track_name) == 0) {
+                       break;
+               }
+               n++;
+
+       } while (n < (UINT_MAX-1));
+
+       try {
+               shared_ptr<MidiTrack> track (new MidiTrack (*this, track_name, Route::Flag (0), mode));
+
+               if (track->ensure_io (1, 1, false, this)) {
+                       error << string_compose (_("cannot configure %1 in/%2 out configuration for new midi track"), track_name)
+                             << endmsg;
+               }
+#if 0
+               if (nphysical_in) {
+                       for (uint32_t x = 0; x < track->n_inputs() && x < nphysical_in; ++x) {
+                               
+                               port = "";
+                               
+                               if (input_auto_connect & AutoConnectPhysical) {
+                                       port = _engine.get_nth_physical_input ((channels_used+x)%nphysical_in);
+                               } 
+                               
+                               if (port.length() && track->connect_input (track->input (x), port, this)) {
+                                       break;
+                               }
+                       }
+               }
+               
+               for (uint32_t x = 0; x < track->n_outputs(); ++x) {
+                       
+                       port = "";
+
+                       if (nphysical_out && (output_auto_connect & AutoConnectPhysical)) {
+                               port = _engine.get_nth_physical_output ((channels_used+x)%nphysical_out);
+                       } else if (output_auto_connect & AutoConnectMaster) {
+                               if (_master_out) {
+                                       port = _master_out->input (x%_master_out->n_inputs())->name();
+                               }
+                       }
+
+                       if (port.length() && track->connect_output (track->output (x), port, this)) {
+                               break;
+                       }
+               }
+
+               if (_control_out) {
+                       vector<string> cports;
+                       uint32_t ni = _control_out->n_inputs();
+
+                       for (n = 0; n < ni; ++n) {
+                               cports.push_back (_control_out->input(n)->name());
+                       }
+
+                       track->set_control_outs (cports);
+               }
+#endif
+               track->DiskstreamChanged.connect (mem_fun (this, &Session::resort_routes));
+
+               add_route (track);
+
+               track->set_remote_control_id (ntracks());
+               return track;
+       }
+
+       catch (failed_constructor &err) {
+               error << _("Session: could not create new midi track.") << endmsg;
+               return shared_ptr<MidiTrack> ((MidiTrack*) 0);
+       }
 }
 
-AudioTrack*
+
+boost::shared_ptr<Route>
+Session::new_midi_route ()
+{
+       char bus_name[32];
+       uint32_t n = 0;
+       string port;
+
+       /* count existing midi busses */
+       {
+               shared_ptr<RouteList> r = routes.reader ();
+
+               for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
+                       if (dynamic_cast<MidiTrack*>((*i).get()) == 0) {
+                               if (!(*i)->hidden()) {
+                                       n++;
+                               }
+                       }
+               }
+       }
+
+       do {
+               snprintf (bus_name, sizeof(bus_name), "Bus %" PRIu32, n+1);
+               if (route_by_name (bus_name) == 0) {
+                       break;
+               }
+               n++;
+
+       } while (n < (UINT_MAX-1));
+
+       try {
+               shared_ptr<Route> bus (new Route (*this, bus_name, -1, -1, -1, -1, Route::Flag(0), DataType::MIDI));
+               
+               if (bus->ensure_io (1, 1, false, this)) {
+                       error << (_("cannot configure 1 in/1 out configuration for new midi track"))
+                             << endmsg;
+               }
+#if 0
+               for (uint32_t x = 0; x < bus->n_inputs(); ++x) {
+                       
+                       port = "";
+
+                       if (input_auto_connect & AutoConnectPhysical) {
+                               port = _engine.get_nth_physical_input ((n+x)%n_physical_inputs);
+                       } 
+                       
+                       if (port.length() && bus->connect_input (bus->input (x), port, this)) {
+                               break;
+                       }
+               }
+
+               for (uint32_t x = 0; x < bus->n_outputs(); ++x) {
+                       
+                       port = "";
+
+                       if (output_auto_connect & AutoConnectPhysical) {
+                               port = _engine.get_nth_physical_input ((n+x)%n_physical_outputs);
+                       } else if (output_auto_connect & AutoConnectMaster) {
+                               if (_master_out) {
+                                       port = _master_out->input (x%_master_out->n_inputs())->name();
+                               }
+                       }
+
+                       if (port.length() && bus->connect_output (bus->output (x), port, this)) {
+                               break;
+                       }
+               }
+#endif
+/*
+               if (_control_out) {
+                       vector<string> cports;
+                       uint32_t ni = _control_out->n_inputs();
+
+                       for (uint32_t n = 0; n < ni; ++n) {
+                               cports.push_back (_control_out->input(n)->name());
+                       }
+                       bus->set_control_outs (cports);
+               }
+*/             
+               add_route (bus);
+               return bus;
+       }
+
+       catch (failed_constructor &err) {
+               error << _("Session: could not create new MIDI route.") << endmsg;
+               return shared_ptr<Route> ((Route*) 0);
+       }
+}
+
+
+shared_ptr<AudioTrack>
 Session::new_audio_track (int input_channels, int output_channels, TrackMode mode)
 {
-       AudioTrack *track;
        char track_name[32];
        uint32_t n = 0;
        uint32_t channels_used = 0;
@@ -1684,9 +1883,10 @@ Session::new_audio_track (int input_channels, int output_channels, TrackMode mod
        /* count existing audio tracks */
 
        {
-               Glib::RWLock::ReaderLock lm (route_lock);
-               for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
-                       if (dynamic_cast<AudioTrack*>(*i) != 0) {
+               shared_ptr<RouteList> r = routes.reader ();
+
+               for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
+                       if (dynamic_cast<AudioTrack*>((*i).get()) != 0) {
                                if (!(*i)->hidden()) {
                                        n++;
                                        channels_used += (*i)->n_inputs();
@@ -1723,7 +1923,7 @@ Session::new_audio_track (int input_channels, int output_channels, TrackMode mod
        }
 
        try {
-               track = new AudioTrack (*this, track_name, Route::Flag (0), mode);
+               shared_ptr<AudioTrack> track (new AudioTrack (*this, track_name, Route::Flag (0), mode));
 
                if (track->ensure_io (input_channels, output_channels, false, this)) {
                        error << string_compose (_("cannot configure %1 in/%2 out configuration for new audio track"),
@@ -1774,25 +1974,23 @@ Session::new_audio_track (int input_channels, int output_channels, TrackMode mod
                        track->set_control_outs (cports);
                }
 
-               track->diskstream_changed.connect (mem_fun (this, &Session::resort_routes));
+               track->DiskstreamChanged.connect (mem_fun (this, &Session::resort_routes));
 
                add_route (track);
 
                track->set_remote_control_id (ntracks());
+               return track;
        }
 
        catch (failed_constructor &err) {
                error << _("Session: could not create new audio track.") << endmsg;
-               return 0;
+               return shared_ptr<AudioTrack> ((AudioTrack*) 0);
        }
-
-       return track;
 }
 
-Route*
+shared_ptr<Route>
 Session::new_audio_route (int input_channels, int output_channels)
 {
-       Route *bus;
        char bus_name[32];
        uint32_t n = 0;
        string port;
@@ -1800,9 +1998,10 @@ Session::new_audio_route (int input_channels, int output_channels)
        /* count existing audio busses */
 
        {
-               Glib::RWLock::ReaderLock lm (route_lock);
-               for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
-                       if (dynamic_cast<AudioTrack*>(*i) == 0) {
+               shared_ptr<RouteList> r = routes.reader ();
+
+               for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
+                       if (dynamic_cast<AudioTrack*>((*i).get()) == 0) {
                                if (!(*i)->hidden()) {
                                        n++;
                                }
@@ -1820,7 +2019,7 @@ Session::new_audio_route (int input_channels, int output_channels)
        } while (n < (UINT_MAX-1));
 
        try {
-               bus = new Route (*this, bus_name, -1, -1, -1, -1);
+               shared_ptr<Route> bus (new Route (*this, bus_name, -1, -1, -1, -1, Route::Flag(0), DataType::AUDIO));
 
                if (bus->ensure_io (input_channels, output_channels, false, this)) {
                        error << string_compose (_("cannot configure %1 in/%2 out configuration for new audio track"),
@@ -1869,23 +2068,23 @@ Session::new_audio_route (int input_channels, int output_channels)
                }
                
                add_route (bus);
+               return bus;
        }
 
        catch (failed_constructor &err) {
-               error << _("Session: could not create new route.") << endmsg;
-               return 0;
+               error << _("Session: could not create new audio route.") << endmsg;
+               return shared_ptr<Route> ((Route*) 0);
        }
-
-       return bus;
 }
 
 void
-Session::add_route (Route* route)
+Session::add_route (shared_ptr<Route> route)
 {
        { 
-               Glib::RWLock::WriterLock lm (route_lock);
-               routes.push_front (route);
-               resort_routes(0);
+               RCUWriter<RouteList> writer (routes);
+               shared_ptr<RouteList> r = writer.get_copy ();
+               r->push_front (route);
+               resort_routes_using (r);
        }
 
        route->solo_changed.connect (sigc::bind (mem_fun (*this, &Session::route_solo_changed), route));
@@ -1908,14 +2107,14 @@ Session::add_route (Route* route)
 }
 
 void
-Session::add_diskstream (AudioDiskstream* dstream)
+Session::add_diskstream (Diskstream* dstream)
 {
        /* need to do this in case we're rolling at the time, to prevent false underruns */
-       dstream->do_refill(0, 0, 0);
+       dstream->do_refill_with_alloc();
        
        { 
                Glib::RWLock::WriterLock lm (diskstream_lock);
-               audio_diskstreams.push_back (dstream);
+               diskstreams.push_back (dstream);
        }
 
        /* take a reference to the diskstream, preventing it from
@@ -1934,53 +2133,57 @@ Session::add_diskstream (AudioDiskstream* dstream)
 
        set_dirty();
        save_state (_current_snapshot_name);
+        save_history();
 
-       AudioDiskstreamAdded (dstream); /* EMIT SIGNAL */
+       DiskstreamAdded (dstream); /* EMIT SIGNAL */
 }
 
 void
-Session::remove_route (Route& route)
+Session::remove_route (shared_ptr<Route> route)
 {
        {       
-               Glib::RWLock::WriterLock lm (route_lock);
-               routes.remove (&route);
+               RCUWriter<RouteList> writer (routes);
+               shared_ptr<RouteList> rs = writer.get_copy ();
+               rs->remove (route);
                
                /* deleting the master out seems like a dumb
                   idea, but its more of a UI policy issue
                   than our concern.
                */
 
-               if (&route == _master_out) {
-                       _master_out = 0;
+               if (route == _master_out) {
+                       _master_out = shared_ptr<Route> ((Route*) 0);
                }
 
-               if (&route == _control_out) {
-                       _control_out = 0;
+               if (route == _control_out) {
+                       _control_out = shared_ptr<Route> ((Route*) 0);
 
                        /* cancel control outs for all routes */
 
                        vector<string> empty;
 
-                       for (RouteList::iterator r = routes.begin(); r != routes.end(); ++r) {
+                       for (RouteList::iterator r = rs->begin(); r != rs->end(); ++r) {
                                (*r)->set_control_outs (empty);
                        }
                }
 
                update_route_solo_state ();
+               
+               /* writer goes out of scope, forces route list update */
        }
 
        AudioTrack* at;
        AudioDiskstream* ds = 0;
        
-       if ((at = dynamic_cast<AudioTrack*>(&route)) != 0) {
-               ds = &at->disk_stream();
+       if ((at = dynamic_cast<AudioTrack*>(route.get())) != 0) {
+               ds = &at->audio_diskstream();
        }
        
        if (ds) {
 
                {
                        Glib::RWLock::WriterLock lm (diskstream_lock);
-                       audio_diskstreams.remove (ds);
+                       diskstreams.remove (ds);
                }
 
                ds->unref ();
@@ -1995,7 +2198,7 @@ Session::remove_route (Route& route)
 
        save_state (_current_snapshot_name);
 
-       delete &route;
+       /* all shared ptrs to route should go out of scope here */
 }      
 
 void
@@ -2005,19 +2208,20 @@ Session::route_mute_changed (void* src)
 }
 
 void
-Session::route_solo_changed (void* src, Route* route)
+Session::route_solo_changed (void* src, shared_ptr<Route> route)
 {      
        if (solo_update_disabled) {
                // We know already
                return;
        }
        
-       Glib::RWLock::ReaderLock lm (route_lock);
        bool is_track;
        
-       is_track = (dynamic_cast<AudioTrack*>(route) != 0);
+       is_track = (dynamic_cast<AudioTrack*>(route.get()) != 0);
        
-       for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
+       shared_ptr<RouteList> r = routes.reader ();
+
+       for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
                
                /* soloing a track mutes all other tracks, soloing a bus mutes all other busses */
                
@@ -2025,7 +2229,7 @@ Session::route_solo_changed (void* src, Route* route)
                        
                        /* don't mess with busses */
                        
-                       if (dynamic_cast<AudioTrack*>(*i) == 0) {
+                       if (dynamic_cast<AudioTrack*>((*i).get()) == 0) {
                                continue;
                        }
                        
@@ -2033,7 +2237,7 @@ Session::route_solo_changed (void* src, Route* route)
                        
                        /* don't mess with tracks */
                        
-                       if (dynamic_cast<AudioTrack*>(*i) != 0) {
+                       if (dynamic_cast<AudioTrack*>((*i).get()) != 0) {
                                continue;
                        }
                }
@@ -2066,10 +2270,10 @@ Session::route_solo_changed (void* src, Route* route)
        bool same_thing_soloed = false;
        bool signal = false;
 
-        for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
+        for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
                if ((*i)->soloed()) {
                        something_soloed = true;
-                       if (dynamic_cast<AudioTrack*>(*i)) {
+                       if (dynamic_cast<AudioTrack*>((*i).get())) {
                                if (is_track) {
                                        same_thing_soloed = true;
                                        break;
@@ -2120,11 +2324,13 @@ Session::update_route_solo_state ()
        /* this is where we actually implement solo by changing
           the solo mute setting of each track.
        */
-               
-        for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
+       
+       shared_ptr<RouteList> r = routes.reader ();
+
+        for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
                if ((*i)->soloed()) {
                        mute = true;
-                       if (dynamic_cast<AudioTrack*>(*i)) {
+                       if (dynamic_cast<AudioTrack*>((*i).get())) {
                                is_track = true;
                        }
                        break;
@@ -2140,7 +2346,7 @@ Session::update_route_solo_state ()
 
                /* nothing is soloed */
 
-               for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
+               for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
                        (*i)->set_solo_mute (false);
                }
                
@@ -2161,13 +2367,15 @@ Session::update_route_solo_state ()
 void
 Session::modify_solo_mute (bool is_track, bool mute)
 {
-        for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
+       shared_ptr<RouteList> r = routes.reader ();
+
+        for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
                
                if (is_track) {
                        
                        /* only alter track solo mute */
                        
-                       if (dynamic_cast<AudioTrack*>(*i)) {
+                       if (dynamic_cast<AudioTrack*>((*i).get())) {
                                if ((*i)->soloed()) {
                                        (*i)->set_solo_mute (!mute);
                                } else {
@@ -2179,7 +2387,7 @@ Session::modify_solo_mute (bool is_track, bool mute)
 
                        /* only alter bus solo mute */
 
-                       if (!dynamic_cast<AudioTrack*>(*i)) {
+                       if (!dynamic_cast<AudioTrack*>((*i).get())) {
 
                                if ((*i)->soloed()) {
 
@@ -2211,36 +2419,35 @@ Session::catch_up_on_solo ()
           basis, but needs the global overview that only the session
           has.
        */
-        Glib::RWLock::ReaderLock lm (route_lock);
        update_route_solo_state();
 }      
                
-Route *
+shared_ptr<Route>
 Session::route_by_name (string name)
 {
-       Glib::RWLock::ReaderLock lm (route_lock);
+       shared_ptr<RouteList> r = routes.reader ();
 
-       for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
+       for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
                if ((*i)->name() == name) {
-                       returni;
+                       return *i;
                }
        }
 
-       return 0;
+       return shared_ptr<Route> ((Route*) 0);
 }
 
-Route *
+shared_ptr<Route>
 Session::route_by_remote_id (uint32_t id)
 {
-       Glib::RWLock::ReaderLock lm (route_lock);
+       shared_ptr<RouteList> r = routes.reader ();
 
-       for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
+       for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
                if ((*i)->remote_control_id() == id) {
-                       returni;
+                       return *i;
                }
        }
 
-       return 0;
+       return shared_ptr<Route> ((Route*) 0);
 }
 
 void
@@ -2269,7 +2476,7 @@ Session::get_maximum_extent () const
           ensure atomicity.
        */
 
-       for (AudioDiskstreamList::const_iterator i = audio_diskstreams.begin(); i != audio_diskstreams.end(); ++i) {
+       for (DiskstreamList::const_iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
                Playlist* pl = (*i)->playlist();
                if ((me = pl->get_maximum_extent()) > max) {
                        max = me;
@@ -2279,12 +2486,12 @@ Session::get_maximum_extent () const
        return max;
 }
 
-AudioDiskstream *
+Diskstream *
 Session::diskstream_by_name (string name)
 {
        Glib::RWLock::ReaderLock lm (diskstream_lock);
 
-       for (AudioDiskstreamList::iterator i = audio_diskstreams.begin(); i != audio_diskstreams.end(); ++i) {
+       for (DiskstreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
                if ((*i)->name() == name) {
                        return* i;
                }
@@ -2293,12 +2500,12 @@ Session::diskstream_by_name (string name)
        return 0;
 }
 
-AudioDiskstream *
-Session::diskstream_by_id (id_t id)
+Diskstream *
+Session::diskstream_by_id (const PBD::ID& id)
 {
        Glib::RWLock::ReaderLock lm (diskstream_lock);
 
-       for (AudioDiskstreamList::iterator i = audio_diskstreams.begin(); i != audio_diskstreams.end(); ++i) {
+       for (DiskstreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
                if ((*i)->id() == id) {
                        return *i;
                }
@@ -2342,7 +2549,7 @@ Session::new_region_name (string old)
                sbuf = buf;
 
                for (i = audio_regions.begin(); i != audio_regions.end(); ++i) {
-                       if ((*i).second->name() == sbuf) {
+                       if (i->second->name() == sbuf) {
                                break;
                        }
                }
@@ -2407,7 +2614,7 @@ Session::region_name (string& result, string base, bool newlevel) const
                                name_taken = false;
                                
                                for (AudioRegionList::const_iterator i = audio_regions.begin(); i != audio_regions.end(); ++i) {
-                                       if ((*i).second->name() == result) {
+                                       if (i->second->name() == result) {
                                                name_taken = true;
                                                break;
                                        }
@@ -2452,8 +2659,8 @@ Session::add_region (Region* region)
 
                        if (x == audio_regions.end()) {
 
-                               pair<AudioRegionList::key_type, AudioRegionList::mapped_type> entry;
-       
+                               pair<AudioRegionList::key_type,AudioRegionList::mapped_type> entry;
+
                                entry.first = region->id();
                                entry.second = ar;
 
@@ -2510,16 +2717,18 @@ Session::remove_region (Region* region)
        AudioRegionList::iterator i;
        AudioRegion* ar = 0;
        bool removed = false;
-
+       
        { 
                Glib::Mutex::Lock lm (region_lock);
 
-               if ((ar = dynamic_cast<AudioRegion*> (region)) != 0) {
+               if ((ar = dynamic_cast<AudioRegion*> (region)) != 0) {
                        if ((i = audio_regions.find (region->id())) != audio_regions.end()) {
                                audio_regions.erase (i);
                                removed = true;
-                       } 
+                       }
+
                } else {
+
                        fatal << _("programming error: ") 
                              << X_("unknown region type passed to Session::remove_region()")
                              << endmsg;
@@ -2547,7 +2756,7 @@ Session::find_whole_file_parent (AudioRegion& child)
 
        for (i = audio_regions.begin(); i != audio_regions.end(); ++i) {
 
-               region = (*i).second;
+               region = i->second;
 
                if (region->whole_file()) {
 
@@ -2561,18 +2770,10 @@ Session::find_whole_file_parent (AudioRegion& child)
 }      
 
 void
-Session::find_equivalent_playlist_regions (AudioRegion& region, vector<AudioRegion*>& result)
+Session::find_equivalent_playlist_regions (Region& region, vector<Region*>& result)
 {
-       for (PlaylistList::iterator i = playlists.begin(); i != playlists.end(); ++i) {
-
-               AudioPlaylist* pl;
-
-               if ((pl = dynamic_cast<AudioPlaylist*>(*i)) == 0) {
-                       continue;
-               }
-
-               pl->get_region_list_equivalent_regions (region, result);
-       }
+       for (PlaylistList::iterator i = playlists.begin(); i != playlists.end(); ++i)
+               (*i)->get_region_list_equivalent_regions (region, result);
 }
 
 int
@@ -2624,7 +2825,7 @@ Session::remove_last_capture ()
 
        Glib::RWLock::ReaderLock lm (diskstream_lock);
        
-       for (AudioDiskstreamList::iterator i = audio_diskstreams.begin(); i != audio_diskstreams.end(); ++i) {
+       for (DiskstreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
                list<Region*>& l = (*i)->last_capture_regions();
                
                if (!l.empty()) {
@@ -2651,12 +2852,12 @@ Session::add_audio_source (AudioSource* source)
 {
        pair<AudioSourceList::key_type, AudioSourceList::mapped_type> entry;
 
-       {
-               Glib::Mutex::Lock lm (audio_source_lock);
+       {
+               Glib::Mutex::Lock lm (audio_source_lock);
                entry.first = source->id();
                entry.second = source;
                audio_sources.insert (entry);
-       }
+       }
        
        source->GoingAway.connect (mem_fun (this, &Session::remove_source));
        set_dirty();
@@ -2674,7 +2875,7 @@ Session::remove_source (Source* source)
 
                if ((i = audio_sources.find (source->id())) != audio_sources.end()) {
                        audio_sources.erase (i);
-               }
+               } 
        }
 
        if (!_state_of_the_state & InCleanup) {
@@ -2684,29 +2885,26 @@ Session::remove_source (Source* source)
                */
                
                save_state (_current_snapshot_name);
+                save_history();
        }
 
        SourceRemoved(source); /* EMIT SIGNAL */
 }
 
 Source *
-Session::get_source (ARDOUR::id_t id)
+Session::source_by_id (const PBD::ID& id)
 {
        Glib::Mutex::Lock lm (audio_source_lock);
        AudioSourceList::iterator i;
        Source* source = 0;
 
        if ((i = audio_sources.find (id)) != audio_sources.end()) {
-               source = (*i).second;
-       }
-
-       if (source) {
-               return source;
+               source = i->second;
        }
 
        /* XXX search MIDI or other searches here */
        
-       return 0;
+       return source;
 }
 
 string
@@ -2781,12 +2979,13 @@ Session::change_audio_path_by_name (string path, string oldname, string newname,
                    the task here is to replace NAME with the new name.
                */
                
-               /* find last slash */
-
                string dir;
                string suffix;
                string::size_type slash;
                string::size_type dash;
+               string::size_type postfix;
+
+               /* find last slash */
 
                if ((slash = path.find_last_of ('/')) == string::npos) {
                        return "";
@@ -2800,11 +2999,41 @@ Session::change_audio_path_by_name (string path, string oldname, string newname,
                        return "";
                }
 
-               suffix = path.substr (dash);
+               suffix = path.substr (dash+1);
+               
+               // Suffix is now everything after the dash. Now we need to eliminate
+               // the nnnnn part, which is done by either finding a '%' or a '.'
+
+               postfix = suffix.find_last_of ("%");
+               if (postfix == string::npos) {
+                       postfix = suffix.find_last_of ('.');
+               }
+
+               if (postfix != string::npos) {
+                       suffix = suffix.substr (postfix);
+               } else {
+                       error << "Logic error in Session::change_audio_path_by_name(), please report to the developers" << endl;
+                       return "";
+               }
+
+               const uint32_t limit = 10000;
+               char buf[PATH_MAX+1];
+
+               for (uint32_t cnt = 1; cnt <= limit; ++cnt) {
+
+                       snprintf (buf, sizeof(buf), "%s%s-%u%s", dir.c_str(), newname.c_str(), cnt, suffix.c_str());
+
+                       if (access (buf, F_OK) != 0) {
+                               path = buf;
+                               break;
+                       }
+                       path = "";
+               }
+
+               if (path == "") {
+                       error << "FATAL ERROR! Could not find a " << endl;
+               }
 
-               path = dir;
-               path += new_legalized;
-               path += suffix;
        }
 
        return path;
@@ -2827,20 +3056,20 @@ Session::audio_path_from_name (string name, uint32_t nchan, uint32_t chan, bool
        */
 
        for (cnt = (destructive ? ++destructive_index : 1); cnt <= limit; ++cnt) {
-               
+
                vector<space_and_path>::iterator i;
                uint32_t existing = 0;
-               
+
                for (i = session_dirs.begin(); i != session_dirs.end(); ++i) {
-                       
+
                        spath = (*i).path;
-                       
+
                        if (destructive) {
                                spath += tape_dir_name;
                        } else {
                                spath += sound_dir_name;
                        }
-                       
+
                        if (destructive) {
                                if (nchan < 2) {
                                        snprintf (buf, sizeof(buf), "%s/T%04d-%s.wav", spath.c_str(), cnt, legalized.c_str());
@@ -2856,10 +3085,10 @@ Session::audio_path_from_name (string name, uint32_t nchan, uint32_t chan, bool
                                        snprintf (buf, sizeof(buf), "%s/T%04d-%s.wav", spath.c_str(), cnt, legalized.c_str());
                                }
                        } else {
-                               
+
                                spath += '/';
                                spath += legalized;
-                                       
+
                                if (nchan < 2) {
                                        snprintf (buf, sizeof(buf), "%s-%u.wav", spath.c_str(), cnt);
                                } else if (nchan == 2) {
@@ -2879,7 +3108,7 @@ Session::audio_path_from_name (string name, uint32_t nchan, uint32_t chan, bool
                                existing++;
                        }
                }
-                       
+
                if (existing == 0) {
                        break;
                }
@@ -2935,18 +3164,6 @@ Session::create_audio_source_for_session (AudioDiskstream& ds, uint32_t chan, bo
 
 /* Playlist management */
 
-Playlist *
-Session::get_playlist (string name)
-{
-       Playlist* ret = 0;
-
-       if ((ret = playlist_by_name (name)) == 0) {
-               ret = new AudioPlaylist (*this, name);
-       }
-
-       return ret;
-}
-
 Playlist *
 Session::playlist_by_name (string name)
 {
@@ -3076,11 +3293,14 @@ Session::audition_playlist ()
 }
 
 void
-Session::audition_region (AudioRegion& r)
+Session::audition_region (Region& r)
 {
-       Event* ev = new Event (Event::Audition, Event::Add, Event::Immediate, 0, 0.0);
-       ev->set_ptr (&r);
-       queue_event (ev);
+       AudioRegion* ar = dynamic_cast<AudioRegion*>(&r);
+       if (ar) {
+               Event* ev = new Event (Event::Audition, Event::Add, Event::Immediate, 0, 0.0);
+               ev->set_ptr (ar);
+               queue_event (ev);
+       }
 }
 
 void
@@ -3093,7 +3313,7 @@ Session::cancel_audition ()
 }
 
 bool
-Session::RoutePublicOrderSorter::operator() (Route* a, Route* b)
+Session::RoutePublicOrderSorter::operator() (boost::shared_ptr<Route> a, boost::shared_ptr<Route> b)
 {
        return a->order_key(N_("signal")) < b->order_key(N_("signal"));
 }
@@ -3139,13 +3359,11 @@ Session::is_auditioning () const
 void
 Session::set_all_solo (bool yn)
 {
-       {
-               Glib::RWLock::ReaderLock lm (route_lock);
-               
-               for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
-                       if (!(*i)->hidden()) {
-                               (*i)->set_solo (yn, this);
-                       }
+       shared_ptr<RouteList> r = routes.reader ();
+       
+       for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
+               if (!(*i)->hidden()) {
+                       (*i)->set_solo (yn, this);
                }
        }
 
@@ -3155,13 +3373,11 @@ Session::set_all_solo (bool yn)
 void
 Session::set_all_mute (bool yn)
 {
-       {
-               Glib::RWLock::ReaderLock lm (route_lock);
-               
-               for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
-                       if (!(*i)->hidden()) {
-                               (*i)->set_mute (yn, this);
-                       }
+       shared_ptr<RouteList> r = routes.reader ();
+       
+       for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
+               if (!(*i)->hidden()) {
+                       (*i)->set_mute (yn, this);
                }
        }
 
@@ -3169,12 +3385,12 @@ Session::set_all_mute (bool yn)
 }
                
 uint32_t
-Session::n_audio_diskstreams () const
+Session::n_diskstreams () const
 {
        Glib::RWLock::ReaderLock lm (diskstream_lock);
        uint32_t n = 0;
 
-       for (AudioDiskstreamList::const_iterator i = audio_diskstreams.begin(); i != audio_diskstreams.end(); ++i) {
+       for (DiskstreamList::const_iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
                if (!(*i)->hidden()) {
                        n++;
                }
@@ -3182,17 +3398,6 @@ Session::n_audio_diskstreams () const
        return n;
 }
 
-void 
-Session::foreach_audio_diskstream (void (AudioDiskstream::*func)(void)) 
-{
-       Glib::RWLock::ReaderLock lm (diskstream_lock);
-       for (AudioDiskstreamList::iterator i = audio_diskstreams.begin(); i != audio_diskstreams.end(); ++i) {
-               if (!(*i)->hidden()) {
-                       ((*i)->*func)();
-               }
-       }
-}
-
 void
 Session::graph_reordered ()
 {
@@ -3204,16 +3409,15 @@ Session::graph_reordered ()
                return;
        }
        
-       Glib::RWLock::WriterLock lm1 (route_lock);
        Glib::RWLock::ReaderLock lm2 (diskstream_lock);
 
-       resort_routes (0);
+       resort_routes ();
 
        /* force all diskstreams to update their capture offset values to 
           reflect any changes in latencies within the graph.
        */
        
-       for (AudioDiskstreamList::iterator i = audio_diskstreams.begin(); i != audio_diskstreams.end(); ++i) {
+       for (DiskstreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
                (*i)->set_capture_offset ();
        }
 }
@@ -3233,12 +3437,12 @@ Session::record_enable_all ()
 void
 Session::record_enable_change_all (bool yn)
 {
-       Glib::RWLock::ReaderLock lm1 (route_lock);
+       shared_ptr<RouteList> r = routes.reader ();
        
-       for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
+       for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
                AudioTrack* at;
 
-               if ((at = dynamic_cast<AudioTrack*>(*i)) != 0) {
+               if ((at = dynamic_cast<AudioTrack*>((*i).get())) != 0) {
                        at->set_record_enable (yn, this);
                }
        }
@@ -3496,7 +3700,7 @@ Session::reset_native_file_format ()
        //RWLockMonitor lm1 (route_lock, true, __LINE__, __FILE__);
        Glib::RWLock::ReaderLock lm2 (diskstream_lock);
 
-       for (AudioDiskstreamList::iterator i = audio_diskstreams.begin(); i != audio_diskstreams.end(); ++i) {
+       for (DiskstreamList::iterator i = diskstreams.begin(); i != diskstreams.end(); ++i) {
                (*i)->reset_write_sources (false);
        }
 }
@@ -3504,9 +3708,9 @@ Session::reset_native_file_format ()
 bool
 Session::route_name_unique (string n) const
 {
-       Glib::RWLock::ReaderLock lm (route_lock);
+       shared_ptr<RouteList> r = routes.reader ();
        
-       for (RouteList::const_iterator i = routes.begin(); i != routes.end(); ++i) {
+       for (RouteList::const_iterator i = r->begin(); i != r->end(); ++i) {
                if ((*i)->name() == n) {
                        return false;
                }
@@ -3563,23 +3767,16 @@ Session::allocate_pan_automation_buffers (jack_nframes_t nframes, uint32_t howma
        _npan_buffers = howmany;
 }
 
-void 
-Session::add_instant_xml (XMLNode& node, const std::string& dir)
-{
-       Stateful::add_instant_xml (node, dir);
-       Config->add_instant_xml (node, get_user_ardour_path());
-}
-
 int
 Session::freeze (InterThreadInfo& itt)
 {
-       Glib::RWLock::ReaderLock lm (route_lock);
+       shared_ptr<RouteList> r = routes.reader ();
 
-       for (RouteList::iterator i = routes.begin(); i != routes.end(); ++i) {
+       for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
 
                AudioTrack *at;
 
-               if ((at = dynamic_cast<AudioTrack*>(*i)) != 0) {
+               if ((at = dynamic_cast<AudioTrack*>((*i).get())) != 0) {
                        /* XXX this is wrong because itt.progress will keep returning to zero at the start
                           of every track.
                        */
@@ -3606,13 +3803,15 @@ Session::write_one_audio_track (AudioTrack& track, jack_nframes_t start, jack_nf
        jack_nframes_t to_do;
        vector<Sample*> buffers;
        char *  workbuf = 0;
-       const jack_nframes_t chunk_size = (256 * 1024)/4;
+
+       // any bigger than this seems to cause stack overflows in called functions
+       const jack_nframes_t chunk_size = (128 * 1024)/4;
 
        g_atomic_int_set (&processing_prohibited, 1);
        
        /* call tree *MUST* hold route_lock */
        
-       if ((playlist = track.disk_stream().playlist()) == 0) {
+       if ((playlist = track.diskstream().playlist()) == 0) {
                goto out;
        }
 
@@ -3622,7 +3821,7 @@ Session::write_one_audio_track (AudioTrack& track, jack_nframes_t start, jack_nf
                goto out;
        }
 
-       nchans = track.disk_stream().n_channels();
+       nchans = track.audio_diskstream().n_channels();
        
        dir = discover_best_sound_dir ();
 
@@ -3766,10 +3965,10 @@ uint32_t
 Session::ntracks () const
 {
        uint32_t n = 0;
-       Glib::RWLock::ReaderLock lm (route_lock);
+       shared_ptr<RouteList> r = routes.reader ();
 
-       for (RouteList::const_iterator i = routes.begin(); i != routes.end(); ++i) {
-               if (dynamic_cast<AudioTrack*> (*i)) {
+       for (RouteList::const_iterator i = r->begin(); i != r->end(); ++i) {
+               if (dynamic_cast<AudioTrack*> ((*i).get())) {
                        ++n;
                }
        }
@@ -3781,10 +3980,10 @@ uint32_t
 Session::nbusses () const
 {
        uint32_t n = 0;
-       Glib::RWLock::ReaderLock lm (route_lock);
+       shared_ptr<RouteList> r = routes.reader ();
 
-       for (RouteList::const_iterator i = routes.begin(); i != routes.end(); ++i) {
-               if (dynamic_cast<AudioTrack*> (*i) == 0) {
+       for (RouteList::const_iterator i = r->begin(); i != r->end(); ++i) {
+               if (dynamic_cast<AudioTrack*> ((*i).get()) == 0) {
                        ++n;
                }
        }