Make Session::audible_frame() return a (more) correct answer when the transport has...
[ardour.git] / libs / ardour / session.cc
index c29a6fe6758620d7309d6b31de738fa21e8576f9..4ac6f9ebbc5ed965a3dad9149987db5d5ab6c20e 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 1999-2004 Paul Davis
+    Copyright (C) 1999-2010 Paul Davis
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
 #include <unistd.h>
 #include <limits.h>
 
-
 #include <glibmm/thread.h>
 #include <glibmm/miscutils.h>
 #include <glibmm/fileutils.h>
-#include <glibmm/thread.h>
 
 #include "pbd/error.h"
 #include "pbd/boost_debug.h"
@@ -54,6 +52,7 @@
 #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"
@@ -72,6 +71,7 @@
 #include "ardour/midi_track.h"
 #include "ardour/midi_ui.h"
 #include "ardour/named_selection.h"
+#include "ardour/process_thread.h"
 #include "ardour/playlist.h"
 #include "ardour/plugin_insert.h"
 #include "ardour/port_insert.h"
@@ -131,9 +131,6 @@ Session::Session (AudioEngine &eng,
        : _engine (eng),
          _target_transport_speed (0.0),
          _requested_return_frame (-1),
-         _scratch_buffers(new BufferSet()),
-         _silent_buffers(new BufferSet()),
-         _mix_buffers(new BufferSet()),
          mmc (0),
          _mmc_port (default_mmc_port),
          _mtc_port (default_mtc_port),
@@ -144,7 +141,6 @@ Session::Session (AudioEngine &eng,
          _butler (new Butler (*this)),
          _post_transport_work (0),
          _send_timecode_update (false),
-         diskstreams (new DiskstreamList),
          routes (new RouteList),
          _total_free_4k_blocks (0),
          _bundles (new BundleList),
@@ -154,7 +150,7 @@ Session::Session (AudioEngine &eng,
          click_emphasis_data (0),
          main_outs (0),
          _metadata (new SessionMetadata()),
-         _have_rec_enabled_diskstream (false)
+         _have_rec_enabled_track (false)
 
 {
        playlists.reset (new SessionPlaylists);
@@ -232,6 +228,7 @@ Session::destroy ()
 
        Stateful::loading_state_version = 0;
 
+       _butler->drop_references ();
        delete _butler;
        delete midi_control_ui;
 
@@ -245,14 +242,9 @@ Session::destroy ()
 
        clear_clicks ();
 
-       delete _scratch_buffers;
-       delete _silent_buffers;
-       delete _mix_buffers;
-
        /* clear out any pending dead wood from RCU managed objects */
 
        routes.flush ();
-       diskstreams.flush ();
        _bundles.flush ();
        
        AudioDiskstream::free_working_buffers();
@@ -292,19 +284,6 @@ Session::destroy ()
 
        boost::shared_ptr<RouteList> r = routes.reader ();
 
-       DEBUG_TRACE (DEBUG::Destruction, "delete diskstreams\n");
-       {
-               RCUWriter<DiskstreamList> dwriter (diskstreams);
-               boost::shared_ptr<DiskstreamList> dsl = dwriter.get_copy();
-               for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
-                       DEBUG_TRACE(DEBUG::Destruction, string_compose ("Dropping for diskstream %1 ; pre-ref = %2\n", (*i)->name(), (*i).use_count()));
-                       (*i)->drop_references ();
-               }
-
-               dsl->clear ();
-       }
-       diskstreams.flush ();
-
        DEBUG_TRACE (DEBUG::Destruction, "delete sources\n");
        for (SourceMap::iterator i = sources.begin(); i != sources.end(); ++i) {
                DEBUG_TRACE(DEBUG::Destruction, string_compose ("Dropping for source %1 ; pre-ref = %2\n", i->second->path(), i->second.use_count()));
@@ -718,9 +697,9 @@ Session::hookup_io ()
 void
 Session::playlist_length_changed ()
 {
-       /* we can't just increase end_location->end() if pl->get_maximum_extent()
+       /* we can't just increase session_range_location->end() if pl->get_maximum_extent()
           if larger. if the playlist used to be the longest playlist,
-          and its now shorter, we have to decrease end_location->end(). hence,
+          and its now shorter, we have to decrease session_range_location->end(). hence,
           we have to iterate over all diskstreams and check the
           playlists currently in use.
        */
@@ -728,16 +707,16 @@ Session::playlist_length_changed ()
 }
 
 void
-Session::diskstream_playlist_changed (boost::weak_ptr<Diskstream> wp)
+Session::track_playlist_changed (boost::weak_ptr<Track> wp)
 {
-       boost::shared_ptr<Diskstream> dstream = wp.lock ();
-       if (!dstream) {
+       boost::shared_ptr<Track> track = wp.lock ();
+       if (!track) {
                return;
        }
        
        boost::shared_ptr<Playlist> playlist;
 
-       if ((playlist = dstream->playlist()) != 0) {
+       if ((playlist = track->playlist()) != 0) {
                playlist->LengthChanged.connect_same_thread (*this, boost::bind (&Session::playlist_length_changed, this));
        }
 
@@ -764,21 +743,23 @@ Session::reset_input_monitor_state ()
 {
        if (transport_rolling()) {
 
-               boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
-
-               for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
-                       if ((*i)->record_enabled ()) {
+               boost::shared_ptr<RouteList> rl = routes.reader ();
+               for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
+                       boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
+                       if (tr && tr->record_enabled ()) {
                                //cerr << "switching to input = " << !auto_input << __FILE__ << __LINE__ << endl << endl;
-                               (*i)->monitor_input (Config->get_monitoring_model() == HardwareMonitoring && !config.get_auto_input());
+                               tr->monitor_input (Config->get_monitoring_model() == HardwareMonitoring && !config.get_auto_input());
                        }
                }
+               
        } else {
-               boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
-
-               for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
-                       if ((*i)->record_enabled ()) {
+               
+               boost::shared_ptr<RouteList> rl = routes.reader ();
+               for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
+                       boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
+                       if (tr && tr->record_enabled ()) {
                                //cerr << "switching to input = " << !Config->get_auto_input() << __FILE__ << __LINE__ << endl << endl;
-                               (*i)->monitor_input (Config->get_monitoring_model() == HardwareMonitoring);
+                               tr->monitor_input (Config->get_monitoring_model() == HardwareMonitoring);
                        }
                }
        }
@@ -960,11 +941,8 @@ Session::handle_locations_changed (Locations::LocationList& locations)
                        set_loop = true;
                }
 
-               if (location->is_start()) {
-                       start_location = location;
-               }
-               if (location->is_end()) {
-                       end_location = location;
+               if (location->is_session_range()) {
+                       _session_range_location = location;
                }
        }
 
@@ -988,10 +966,12 @@ Session::enable_record ()
                deliver_mmc(MIDI::MachineControl::cmdRecordStrobe, _last_record_location);
 
                if (Config->get_monitoring_model() == HardwareMonitoring && config.get_auto_input()) {
-                       boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
-                       for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
-                               if ((*i)->record_enabled ()) {
-                                       (*i)->monitor_input (true);
+                       
+                       boost::shared_ptr<RouteList> rl = routes.reader ();
+                       for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
+                               boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
+                               if (tr && tr->record_enabled ()) {
+                                       tr->monitor_input (true);
                                }
                        }
                }
@@ -1023,11 +1003,12 @@ Session::disable_record (bool rt_context, bool force)
                }
 
                if (Config->get_monitoring_model() == HardwareMonitoring && config.get_auto_input()) {
-                       boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
 
-                       for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
-                               if ((*i)->record_enabled ()) {
-                                       (*i)->monitor_input (false);
+                       boost::shared_ptr<RouteList> rl = routes.reader ();
+                       for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
+                               boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
+                               if (tr && tr->record_enabled ()) {
+                                       tr->monitor_input (false);
                                }
                        }
                }
@@ -1043,17 +1024,15 @@ Session::disable_record (bool rt_context, bool force)
 void
 Session::step_back_from_record ()
 {
-       /* XXX really atomic compare+swap here */
-       if (g_atomic_int_get (&_record_status) == Recording) {
-               g_atomic_int_set (&_record_status, Enabled);
+       if (g_atomic_int_compare_and_exchange (&_record_status, Recording, Enabled)) {
 
                if (Config->get_monitoring_model() == HardwareMonitoring && config.get_auto_input()) {
-                       boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
-
-                       for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
-                               if ((*i)->record_enabled ()) {
+                       boost::shared_ptr<RouteList> rl = routes.reader ();
+                       for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
+                               boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
+                               if (tr && tr->record_enabled ()) {
                                        //cerr << "switching from input" << __FILE__ << __LINE__ << endl << endl;
-                                       (*i)->monitor_input (false);
+                                       tr->monitor_input (false);
                                }
                        }
                }
@@ -1125,17 +1104,20 @@ Session::audible_frame () const
 
                /* MOVING */
 
-               /* check to see if we have passed the first guaranteed
+               /* Check to see if we have passed the first guaranteed
                   audible frame past our last start position. if not,
                   return that last start point because in terms
                   of audible frames, we have not moved yet.
+
+                  `Start position' in this context means the time we last
+                  either started or changed transport direction.
                */
 
                if (_transport_speed > 0.0f) {
 
                        if (!play_loop || !have_looped) {
-                               if (tf < _last_roll_location + offset) {
-                                       return _last_roll_location;
+                               if (tf < _last_roll_or_reversal_location + offset) {
+                                       return _last_roll_or_reversal_location;
                                }
                        }
 
@@ -1147,8 +1129,8 @@ Session::audible_frame () const
 
                        /* XXX wot? no backward looping? */
 
-                       if (tf > _last_roll_location - offset) {
-                               return _last_roll_location;
+                       if (tf > _last_roll_or_reversal_location - offset) {
+                               return _last_roll_or_reversal_location;
                        } else {
                                /* backwards */
                                ret += offset;
@@ -1197,12 +1179,7 @@ Session::set_block_size (nframes_t nframes)
        {
                current_block_size = nframes;
 
-               ensure_buffers(_scratch_buffers->available());
-
-               delete [] _gain_automation_buffer;
-               _gain_automation_buffer = new gain_t[nframes];
-
-               allocate_pan_automation_buffers (nframes, _npan_buffers, true);
+               ensure_buffers ();
 
                boost::shared_ptr<RouteList> r = routes.reader ();
 
@@ -1210,9 +1187,12 @@ Session::set_block_size (nframes_t nframes)
                        (*i)->set_block_size (nframes);
                }
 
-               boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
-               for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
-                       (*i)->set_block_size (nframes);
+               boost::shared_ptr<RouteList> rl = routes.reader ();
+               for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
+                       boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
+                       if (tr) {
+                               tr->set_block_size (nframes);
+                       }
                }
 
                set_worst_io_latencies ();
@@ -1484,7 +1464,7 @@ Session::new_midi_track (TrackMode mode, RouteGroup* route_group, uint32_t how_m
 
                        auto_connect_route (track, existing_inputs, existing_outputs);
 
-                       track->midi_diskstream()->non_realtime_input_change();
+                       track->non_realtime_input_change();
                        if (route_group) {
                                route_group->add (track);
                        }
@@ -1498,36 +1478,12 @@ Session::new_midi_track (TrackMode mode, RouteGroup* route_group, uint32_t how_m
 
                catch (failed_constructor &err) {
                        error << _("Session: could not create new midi track.") << endmsg;
-
-                       if (track) {
-                               /* we need to get rid of this, since the track failed to be created */
-                               /* XXX arguably, AudioTrack::AudioTrack should not do the Session::add_diskstream() */
-
-                               {
-                                       RCUWriter<DiskstreamList> writer (diskstreams);
-                                       boost::shared_ptr<DiskstreamList> ds = writer.get_copy();
-                                       ds->remove (track->midi_diskstream());
-                               }
-                       }
-
                        goto failed;
                }
 
                catch (AudioEngine::PortRegistrationFailure& pfe) {
 
                        error << string_compose (_("No more JACK ports are available. You will need to stop %1 and restart JACK with ports if you need this many tracks."), PROGRAM_NAME) << endmsg;
-
-                       if (track) {
-                               /* we need to get rid of this, since the track failed to be created */
-                               /* XXX arguably, MidiTrack::MidiTrack should not do the Session::add_diskstream() */
-
-                               {
-                                       RCUWriter<DiskstreamList> writer (diskstreams);
-                                       boost::shared_ptr<DiskstreamList> ds = writer.get_copy();
-                                       ds->remove (track->midi_diskstream());
-                               }
-                       }
-
                        goto failed;
                }
 
@@ -1677,7 +1633,7 @@ Session::new_audio_track (int input_channels, int output_channels, TrackMode mod
                                route_group->add (track);
                        }
 
-                       track->audio_diskstream()->non_realtime_input_change();
+                       track->non_realtime_input_change();
 
                        track->DiskstreamChanged.connect_same_thread (*this, boost::bind (&Session::resort_routes, this));
                        track->set_remote_control_id (control_id);
@@ -1689,36 +1645,12 @@ Session::new_audio_track (int input_channels, int output_channels, TrackMode mod
 
                catch (failed_constructor &err) {
                        error << _("Session: could not create new audio track.") << endmsg;
-
-                       if (track) {
-                               /* we need to get rid of this, since the track failed to be created */
-                               /* XXX arguably, AudioTrack::AudioTrack should not do the Session::add_diskstream() */
-
-                               {
-                                       RCUWriter<DiskstreamList> writer (diskstreams);
-                                       boost::shared_ptr<DiskstreamList> ds = writer.get_copy();
-                                       ds->remove (track->audio_diskstream());
-                               }
-                       }
-
                        goto failed;
                }
 
                catch (AudioEngine::PortRegistrationFailure& pfe) {
 
                        error << pfe.what() << endmsg;
-
-                       if (track) {
-                               /* we need to get rid of this, since the track failed to be created */
-                               /* XXX arguably, AudioTrack::AudioTrack should not do the Session::add_diskstream() */
-
-                               {
-                                       RCUWriter<DiskstreamList> writer (diskstreams);
-                                       boost::shared_ptr<DiskstreamList> ds = writer.get_copy();
-                                       ds->remove (track->audio_diskstream());
-                               }
-                       }
-
                        goto failed;
                }
 
@@ -1963,6 +1895,13 @@ Session::add_routes (RouteList& new_routes, bool save)
                if (r->is_monitor()) {
                        _monitor_out = r;
                }
+
+               boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (r);
+               if (tr) {
+                       tr->PlaylistChanged.connect_same_thread (*this, boost::bind (&Session::track_playlist_changed, this, boost::weak_ptr<Track> (tr)));
+                       track_playlist_changed (boost::weak_ptr<Track> (tr));
+                       tr->RecordEnableChanged.connect_same_thread (*this, boost::bind (&Session::update_have_rec_enabled_track, this));
+               }
        }
 
        if (_monitor_out && IO::connecting_legal) {
@@ -2083,31 +2022,6 @@ Session::add_internal_sends (boost::shared_ptr<Route> dest, Placement p, boost::
        graph_reordered ();
 }
 
-void
-Session::add_diskstream (boost::shared_ptr<Diskstream> dstream)
-{
-       /* need to do this in case we're rolling at the time, to prevent false underruns */
-       dstream->do_refill_with_alloc ();
-
-       dstream->set_block_size (current_block_size);
-
-       {
-               RCUWriter<DiskstreamList> writer (diskstreams);
-               boost::shared_ptr<DiskstreamList> ds = writer.get_copy();
-               ds->push_back (dstream);
-               /* writer goes out of scope, copies ds back to main */
-       }
-
-       dstream->PlaylistChanged.connect_same_thread (*this, boost::bind (&Session::diskstream_playlist_changed, this, boost::weak_ptr<Diskstream> (dstream)));
-       /* this will connect to future changes, and check the current length */
-       diskstream_playlist_changed (boost::weak_ptr<Diskstream> (dstream));
-
-       dstream->RecordEnableChanged.connect_same_thread (*this, boost::bind (&Session::update_have_rec_enabled_diskstream, this));
-
-       dstream->prepare ();
-
-}
-
 void
 Session::remove_route (shared_ptr<Route> route)
 {
@@ -2142,22 +2056,6 @@ Session::remove_route (shared_ptr<Route> route)
                /* writer goes out of scope, forces route list update */
        }
 
-       boost::shared_ptr<Track> t;
-       boost::shared_ptr<Diskstream> ds;
-
-       if ((t = boost::dynamic_pointer_cast<Track>(route)) != 0) {
-               ds = t->diskstream();
-       }
-
-       if (ds) {
-
-               {
-                       RCUWriter<DiskstreamList> dsl (diskstreams);
-                       boost::shared_ptr<DiskstreamList> d = dsl.get_copy();
-                       d->remove (ds);
-               }
-       }
-
        find_current_end ();
 
        // We need to disconnect the routes inputs and outputs
@@ -2248,7 +2146,7 @@ Session::route_solo_changed (void* /*src*/, boost::weak_ptr<Route> wpr)
                delta = -1;
        }
 
-       /* now mod the solo level of all other routes except master & control outs
+       /* now mod the solo level of all other routes except master/control outs/auditioner
           so that they will be silent if appropriate.
        */
 
@@ -2266,18 +2164,6 @@ Session::route_solo_changed (void* /*src*/, boost::weak_ptr<Route> wpr)
                } 
        }
 
-       /* make sure master is never muted by solo */
-
-       if (_master_out && route != _master_out && _master_out->soloed_by_others() == 0 && !_master_out->soloed()) {
-               _master_out->mod_solo_by_others (1);
-       }
-       /* ditto for control outs make sure master is never muted by solo */
-
-       if (_monitor_out && route != _monitor_out && _monitor_out && _monitor_out->soloed_by_others() == 0) {
-               _monitor_out->mod_solo_by_others (1);
-       }
-
        solo_update_disabled = false;
        update_route_solo_state (r);
        SoloChanged (); /* EMIT SIGNAL */
@@ -2303,7 +2189,11 @@ Session::update_route_solo_state (boost::shared_ptr<RouteList> r)
                }
 
                 if (!(*i)->is_hidden() && (*i)->listening()) {
-                        listeners++;
+                        if (Config->get_solo_control_is_listen_control()) {
+                                listeners++;
+                        } else {
+                                (*i)->set_listen (false, this);
+                        }
                 }
        }
 
@@ -2331,6 +2221,24 @@ Session::get_routes_with_internal_returns() const
        return rl;
 }
 
+bool
+Session::io_name_is_legal (const std::string& name)
+{
+        shared_ptr<RouteList> r = routes.reader ();
+        
+        for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
+                if ((*i)->name() == name) {
+                        return false;
+                }
+                
+                if ((*i)->has_io_processor_named (name)) {
+                        return false;
+                }
+        }
+        
+        return true;
+}
+
 shared_ptr<Route>
 Session::route_by_name (string name)
 {
@@ -2382,8 +2290,8 @@ Session::find_current_end ()
 
        nframes_t max = get_maximum_extent ();
 
-       if (max > end_location->end()) {
-               end_location->set_end (max);
+       if (max > _session_range_location->end()) {
+               _session_range_location->set_end (max);
                set_dirty();
                DurationChanged(); /* EMIT SIGNAL */
        }
@@ -2394,13 +2302,16 @@ Session::get_maximum_extent () const
 {
        nframes_t max = 0;
        nframes_t me;
-
-       boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
-
-       for (DiskstreamList::const_iterator i = dsl->begin(); i != dsl->end(); ++i) {
-               if ((*i)->destructive())  //ignore tape tracks when getting max extents
+       
+       boost::shared_ptr<RouteList> rl = routes.reader ();
+       for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
+               boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
+               if (!tr || tr->destructive()) {
+                       //ignore tape tracks when getting max extents
                        continue;
-               boost::shared_ptr<Playlist> pl = (*i)->playlist();
+               }
+               
+               boost::shared_ptr<Playlist> pl = tr->playlist();
                if ((me = pl->get_maximum_extent()) > max) {
                        max = me;
                }
@@ -2409,224 +2320,8 @@ Session::get_maximum_extent () const
        return max;
 }
 
-boost::shared_ptr<Diskstream>
-Session::diskstream_by_name (string name)
-{
-       boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
-
-       for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
-               if ((*i)->name() == name) {
-                       return *i;
-               }
-       }
-
-       return boost::shared_ptr<Diskstream>((Diskstream*) 0);
-}
-
-boost::shared_ptr<Diskstream>
-Session::diskstream_by_id (const PBD::ID& id)
-{
-       boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
-
-       for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
-               if ((*i)->id() == id) {
-                       return *i;
-               }
-       }
-
-       return boost::shared_ptr<Diskstream>((Diskstream*) 0);
-}
-
 /* Region management */
 
-string
-Session::new_region_name (string old)
-{
-       string::size_type last_period;
-       uint32_t number;
-       string::size_type len = old.length() + 64;
-       char buf[len];
-
-       if ((last_period = old.find_last_of ('.')) == string::npos) {
-
-               /* no period present - add one explicitly */
-
-               old += '.';
-               last_period = old.length() - 1;
-               number = 0;
-
-       } else {
-
-               number = atoi (old.substr (last_period+1).c_str());
-
-       }
-
-       while (number < (UINT_MAX-1)) {
-
-                const RegionFactory::RegionMap& regions (RegionFactory::regions());
-               RegionFactory::RegionMap::const_iterator i;
-               string sbuf;
-
-               number++;
-
-               snprintf (buf, len, "%s%" PRIu32, old.substr (0, last_period + 1).c_str(), number);
-               sbuf = buf;
-
-               for (i = regions.begin(); i != regions.end(); ++i) {
-                       if (i->second->name() == sbuf) {
-                               break;
-                       }
-               }
-
-               if (i == regions.end()) {
-                       break;
-               }
-       }
-
-       if (number != (UINT_MAX-1)) {
-               return buf;
-       }
-
-       error << string_compose (_("cannot create new name for region \"%1\""), old) << endmsg;
-       return old;
-}
-
-int
-Session::region_name (string& result, string base, bool newlevel)
-{
-       char buf[16];
-       string subbase;
-
-       if (base.find("/") != string::npos) {
-               base = base.substr(base.find_last_of("/") + 1);
-       }
-
-       if (base == "") {
-
-               snprintf (buf, sizeof (buf), "%d", RegionFactory::nregions() + 1);
-               result = "region.";
-               result += buf;
-
-       } else {
-
-               if (newlevel) {
-                       subbase = base;
-               } else {
-                       string::size_type pos;
-
-                       pos = base.find_last_of ('.');
-
-                       /* pos may be npos, but then we just use entire base */
-
-                       subbase = base.substr (0, pos);
-
-               }
-
-               {
-                       Glib::Mutex::Lock lm (region_lock);
-
-                       map<string,uint32_t>::iterator x;
-
-                       result = subbase;
-
-                       if ((x = region_name_map.find (subbase)) == region_name_map.end()) {
-                               result += ".1";
-                               region_name_map[subbase] = 1;
-                       } else {
-                               x->second++;
-                               snprintf (buf, sizeof (buf), ".%d", x->second);
-
-                               result += buf;
-                       }
-               }
-       }
-
-       return 0;
-}
-
-void
-Session::add_region (boost::shared_ptr<Region> region)
-{
-       vector<boost::shared_ptr<Region> > v;
-       v.push_back (region);
-       add_regions (v);
-}
-
-void
-Session::add_regions (vector<boost::shared_ptr<Region> >& new_regions)
-{
-       /* mark dirty because something has changed
-       */
-
-       set_dirty ();
-        
-        for (vector<boost::shared_ptr<Region> >::iterator ii = new_regions.begin(); ii != new_regions.end(); ++ii) {
-                
-                boost::shared_ptr<Region> region = *ii;
-                assert (region);
-                
-                region->PropertyChanged.connect_same_thread (*this, boost::bind (&Session::region_changed, this, _1, boost::weak_ptr<Region>(region)));
-                update_region_name_map (region);
-        }
-        
-        if (!new_regions.empty()) {
-                RegionsAdded (new_regions); /* EMIT SIGNAL */
-        }
-}
-
-void
-Session::update_region_name_map (boost::shared_ptr<Region> region)
-{
-       string::size_type last_period = region->name().find_last_of ('.');
-
-       if (last_period != string::npos && last_period < region->name().length() - 1) {
-
-               string base = region->name().substr (0, last_period);
-               string number = region->name().substr (last_period+1);
-               map<string,uint32_t>::iterator x;
-
-               /* note that if there is no number, we get zero from atoi,
-                  which is just fine
-               */
-
-               region_name_map[base] = atoi (number);
-       }
-}
-
-void
-Session::region_changed (const PropertyChange& what_changed, boost::weak_ptr<Region> weak_region)
-{
-       boost::shared_ptr<Region> region (weak_region.lock ());
-
-       if (!region) {
-               return;
-       }
-
-       if (what_changed.contains (Properties::hidden)) {
-               /* relay hidden changes */
-               RegionHiddenChange (region);
-       }
-
-       if (what_changed.contains (Properties::name)) {
-               update_region_name_map (region);
-       }
-}
-
-void
-Session::remove_region (boost::weak_ptr<Region> weak_region)
-{
-       boost::shared_ptr<Region> region (weak_region.lock ());
-
-       if (!region) {
-               return;
-       }
-
-        RegionFactory::map_remove (region);
-       set_dirty();
-
-        RegionRemoved(region); /* EMIT SIGNAL */
-}
-
 boost::shared_ptr<Region>
 Session::find_whole_file_parent (boost::shared_ptr<Region const> child) const
 {
@@ -2693,10 +2388,14 @@ Session::remove_last_capture ()
 {
        list<boost::shared_ptr<Region> > r;
 
-       boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
-
-       for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
-               list<boost::shared_ptr<Region> >& l = (*i)->last_capture_regions();
+       boost::shared_ptr<RouteList> rl = routes.reader ();
+       for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
+               boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
+               if (!tr) {
+                       continue;
+               }
+               
+               list<boost::shared_ptr<Region> >& l = tr->last_capture_regions();
 
                if (!l.empty()) {
                        r.insert (r.end(), l.begin(), l.end());
@@ -2728,6 +2427,7 @@ Session::add_source (boost::shared_ptr<Source> source)
        }
 
        if (result.second) {
+               source->DropReferences.connect_same_thread (*this, boost::bind (&Session::remove_source, this, boost::weak_ptr<Source> (source)));
                set_dirty();
        }
 
@@ -3037,10 +2737,9 @@ Session::new_audio_source_name (const string& base, uint32_t nchan, uint32_t cha
 
 /** Create a new within-session audio source */
 boost::shared_ptr<AudioFileSource>
-Session::create_audio_source_for_session (AudioDiskstream& ds, uint32_t chan, bool destructive)
+Session::create_audio_source_for_session (size_t n_chans, string const & n, uint32_t chan, bool destructive)
 {
-       const size_t n_chans = ds.n_channels().n_audio();
-       const string name    = new_audio_source_name (ds.name(), n_chans, chan, destructive);
+       const string name    = new_audio_source_name (n, n_chans, chan, destructive);
        const string path    = new_source_path_from_name(DataType::AUDIO, name);
 
        return boost::dynamic_pointer_cast<AudioFileSource> (
@@ -3098,9 +2797,9 @@ Session::new_midi_source_name (const string& base)
 
 /** Create a new within-session MIDI source */
 boost::shared_ptr<MidiSource>
-Session::create_midi_source_for_session (MidiDiskstream& ds)
+Session::create_midi_source_for_session (string const & n)
 {
-       const string name = new_midi_source_name (ds.name());
+       const string name = new_midi_source_name (n);
        const string path = new_source_path_from_name (DataType::MIDI, name);
 
        return boost::dynamic_pointer_cast<SMFSource> (
@@ -3247,21 +2946,6 @@ Session::is_auditioning () const
        }
 }
 
-uint32_t
-Session::n_diskstreams () const
-{
-       uint32_t n = 0;
-
-       boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
-
-       for (DiskstreamList::const_iterator i = dsl->begin(); i != dsl->end(); ++i) {
-               if (!(*i)->hidden()) {
-                       n++;
-               }
-       }
-       return n;
-}
-
 void
 Session::graph_reordered ()
 {
@@ -3285,40 +2969,13 @@ Session::graph_reordered ()
           reflect any changes in latencies within the graph.
        */
 
-       boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
-
-       for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
-               (*i)->set_capture_offset ();
-       }
-}
-
-void
-Session::add_processor (Processor* processor)
-{
-       /* Session does not own Processors (they belong to a Route) but we do want to track
-          the arrival and departure of port inserts, sends and returns for naming
-          purposes.
-       */
-       processor->DropReferences.connect_same_thread (*this, boost::bind (&Session::remove_processor, this, processor));
-       set_dirty();
-}
-
-void
-Session::remove_processor (Processor* processor)
-{
-       Send* send;
-       Return* retrn;
-       PortInsert* port_insert;
-
-       if ((port_insert = dynamic_cast<PortInsert *> (processor)) != 0) {
-               insert_bitset[port_insert->bit_slot()] = false;
-       } else if ((send = dynamic_cast<Send *> (processor)) != 0) {
-               send_bitset[send->bit_slot()] = false;
-       } else if ((retrn = dynamic_cast<Return *> (processor)) != 0) {
-               return_bitset[retrn->bit_slot()] = false;
+       boost::shared_ptr<RouteList> rl = routes.reader ();
+       for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
+               boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
+               if (tr) {
+                       tr->set_capture_offset ();
+               }
        }
-
-       set_dirty();
 }
 
 nframes_t
@@ -3423,18 +3080,7 @@ Session::tempo_map_changed (const PropertyChange&)
 void
 Session::ensure_buffers (ChanCount howmany)
 {
-       if (current_block_size == 0) {
-               return; // too early? (is this ok?)
-       }
-
-       for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
-               size_t count = std::max(_scratch_buffers->available().get(*t), howmany.get(*t));
-               _scratch_buffers->ensure_buffers (*t, count, _engine.raw_buffer_size(*t));
-               _mix_buffers->ensure_buffers (*t, count, _engine.raw_buffer_size(*t));
-               _silent_buffers->ensure_buffers (*t, count, _engine.raw_buffer_size(*t));
-       }
-
-       allocate_pan_automation_buffers (current_block_size, howmany.n_audio(), false);
+        BufferManager::ensure_buffers (howmany);
 }
 
 void
@@ -3541,6 +3187,31 @@ Session::mark_insert_id (uint32_t id)
        insert_bitset[id] = true;
 }
 
+void
+Session::unmark_send_id (uint32_t id)
+{
+       if (id < send_bitset.size()) {
+                send_bitset[id] = false;
+        }
+}
+
+void
+Session::unmark_return_id (uint32_t id)
+{
+       if (id < return_bitset.size()) {
+                return_bitset[id] = false;
+        }
+}
+
+void
+Session::unmark_insert_id (uint32_t id)
+{
+       if (id < insert_bitset.size()) {
+                insert_bitset[id] = false;
+        }
+}
+
+
 /* Named Selection management */
 
 boost::shared_ptr<NamedSelection>
@@ -3593,10 +3264,12 @@ Session::remove_named_selection (boost::shared_ptr<NamedSelection> named_selecti
 void
 Session::reset_native_file_format ()
 {
-       boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
-
-       for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
-               (*i)->reset_write_sources (false);
+       boost::shared_ptr<RouteList> rl = routes.reader ();
+       for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
+               boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
+               if (tr) {
+                       tr->reset_write_sources (false);
+               }
        }
 }
 
@@ -3628,31 +3301,6 @@ Session::route_name_internal (string n) const
        return false;
 }
 
-void
-Session::allocate_pan_automation_buffers (nframes_t nframes, uint32_t howmany, bool force)
-{
-       if (!force && howmany <= _npan_buffers) {
-               return;
-       }
-
-       if (_pan_automation_buffer) {
-
-               for (uint32_t i = 0; i < _npan_buffers; ++i) {
-                       delete [] _pan_automation_buffer[i];
-               }
-
-               delete [] _pan_automation_buffer;
-       }
-
-       _pan_automation_buffer = new pan_t*[howmany];
-
-       for (uint32_t i = 0; i < howmany; ++i) {
-               _pan_automation_buffer[i] = new pan_t[nframes];
-       }
-
-       _npan_buffers = howmany;
-}
-
 int
 Session::freeze_all (InterThreadInfo& itt)
 {
@@ -3683,7 +3331,7 @@ Session::write_one_track (AudioTrack& track, nframes_t start, nframes_t end,
        boost::shared_ptr<AudioFileSource> fsource;
        uint32_t x;
        char buf[PATH_MAX+1];
-       ChanCount nchans(track.audio_diskstream()->n_channels());
+       ChanCount nchans(track.n_channels());
        nframes_t position;
        nframes_t this_chunk;
        nframes_t to_do;
@@ -3706,7 +3354,7 @@ Session::write_one_track (AudioTrack& track, nframes_t start, nframes_t end,
 
        /* call tree *MUST* hold route_lock */
 
-       if ((playlist = track.diskstream()->playlist()) == 0) {
+       if ((playlist = track.playlist()) == 0) {
                goto out;
        }
 
@@ -3838,9 +3486,23 @@ Session::write_one_track (AudioTrack& track, nframes_t start, nframes_t end,
        return result;
 }
 
+gain_t*
+Session::gain_automation_buffer() const
+{
+        return ProcessThread::gain_automation_buffer ();
+}
+
+pan_t**
+Session::pan_automation_buffer() const
+{
+        return ProcessThread::pan_automation_buffer ();
+}
+
 BufferSet&
 Session::get_silent_buffers (ChanCount count)
 {
+        return ProcessThread::get_silent_buffers (count);
+#if 0
        assert(_silent_buffers->available() >= count);
        _silent_buffers->set_count(count);
 
@@ -3851,11 +3513,14 @@ Session::get_silent_buffers (ChanCount count)
        }
 
        return *_silent_buffers;
+#endif
 }
 
 BufferSet&
 Session::get_scratch_buffers (ChanCount count)
 {
+        return ProcessThread::get_scratch_buffers (count);
+#if 0
        if (count != ChanCount::ZERO) {
                assert(_scratch_buffers->available() >= count);
                _scratch_buffers->set_count(count);
@@ -3864,14 +3529,18 @@ Session::get_scratch_buffers (ChanCount count)
        }
 
        return *_scratch_buffers;
+#endif
 }
 
 BufferSet&
 Session::get_mix_buffers (ChanCount count)
 {
+        return ProcessThread::get_mix_buffers (count);
+#if 0
        assert(_mix_buffers->available() >= count);
        _mix_buffers->set_count(count);
        return *_mix_buffers;
+#endif
 }
 
 uint32_t
@@ -3941,28 +3610,34 @@ Session::sync_order_keys (std::string const & base)
        set_remote_control_ids ();
 }
 
-/** @return true if there is at least one record-enabled diskstream, otherwise false */
+/** @return true if there is at least one record-enabled track, otherwise false */
 bool
-Session::have_rec_enabled_diskstream () const
+Session::have_rec_enabled_track () const
 {
-       return g_atomic_int_get (&_have_rec_enabled_diskstream) == 1;
+       return g_atomic_int_get (&_have_rec_enabled_track) == 1;
 }
 
-/** Update the state of our rec-enabled diskstreams flag */
+/** Update the state of our rec-enabled tracks flag */
 void
-Session::update_have_rec_enabled_diskstream ()
+Session::update_have_rec_enabled_track ()
 {
-       boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader ();
-       DiskstreamList::iterator i = dsl->begin ();
-       while (i != dsl->end () && (*i)->record_enabled () == false) {
+       boost::shared_ptr<RouteList> rl = routes.reader ();
+       RouteList::iterator i = rl->begin();
+       while (i != rl->end ()) {
+
+               boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
+               if (tr && tr->record_enabled ()) {
+                       break;
+               }
+               
                ++i;
        }
 
-       int const old = g_atomic_int_get (&_have_rec_enabled_diskstream);
+       int const old = g_atomic_int_get (&_have_rec_enabled_track);
 
-       g_atomic_int_set (&_have_rec_enabled_diskstream, i != dsl->end () ? 1 : 0);
+       g_atomic_int_set (&_have_rec_enabled_track, i != rl->end () ? 1 : 0);
 
-       if (g_atomic_int_get (&_have_rec_enabled_diskstream) != old) {
+       if (g_atomic_int_get (&_have_rec_enabled_track) != old) {
                RecordStateChanged (); /* EMIT SIGNAL */
        }
 }
@@ -4037,12 +3712,7 @@ Session::get_routes_with_regions_at (nframes64_t const p) const
                        continue;
                }
                
-               boost::shared_ptr<Diskstream> ds = tr->diskstream ();
-               if (!ds) {
-                       continue;
-               }
-
-               boost::shared_ptr<Playlist> pl = ds->playlist ();
+               boost::shared_ptr<Playlist> pl = tr->playlist ();
                if (!pl) {
                        continue;
                }