Selection of visible note range (full range vs fit contents, selectable from midi...
[ardour.git] / libs / ardour / ardour / session.h
index 25f06c03b4daceb5207fd260fa3acd4a9782e08c..8a5165396832da0645519a63eb5c21aea45fc92f 100644 (file)
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id$
 */
 
 #ifndef __ardour_session_h__
 #define __ardour_session_h__
 
-
 #include <string>
 #include <list>
 #include <map>
@@ -29,7 +27,9 @@
 #include <set>
 #include <stack>
 
+#include <boost/scoped_ptr.hpp>
 #include <boost/weak_ptr.hpp>
+#include <boost/dynamic_bitset.hpp>
 
 #include <stdint.h>
 
@@ -55,6 +55,8 @@
 #include <ardour/gain.h>
 #include <ardour/io.h>
 
+#include <ardour/smpte.h>
+
 class XMLTree;
 class XMLNode;
 class AEffect;
@@ -72,28 +74,40 @@ namespace ARDOUR {
 class Port;
 class AudioEngine;
 class Slave;
-class Diskstream;      
-class AudioDiskstream; 
+class Diskstream;
 class Route;
 class AuxInput;
 class Source;
 class AudioSource;
+class BufferSet;
+
+class Diskstream;
+class AudioDiskstream;
+class MidiDiskstream;
 class AudioFileSource;
+class MidiSource;
 class Auditioner;
-class Insert;
+class Processor;
 class Send;
-class Redirect;
+class IOProcessor;
 class PortInsert;
 class PluginInsert;
-class Connection;
+class Bundle;
 class TempoMap;
 class AudioTrack;
 class NamedSelection;
 class AudioRegion;
+
 class Region;
 class Playlist;
 class VSTPlugin;
-class ControlProtocolManager;
+class ControlProtocolInfo;
+
+class MidiTrack;
+class MidiRegion;
+class SMFSource;
+
+class SessionDirectory;
 
 struct AudioExportSpecification;
 struct RouteGroup;
@@ -103,13 +117,12 @@ using std::string;
 using std::map;
 using std::set;
 
-class Session : public sigc::trackable, public PBD::StatefulDestructible
-
+class Session : public PBD::StatefulDestructible
 {
   private:
-       typedef std::pair<boost::shared_ptr<Route>,bool> RouteBooleanState;
+       typedef std::pair<boost::weak_ptr<Route>,bool> RouteBooleanState;
        typedef vector<RouteBooleanState> GlobalRouteBooleanState;
-       typedef std::pair<boost::shared_ptr<Route>,MeterPoint> RouteMeterState;
+       typedef std::pair<boost::weak_ptr<Route>,MeterPoint> RouteMeterState;
        typedef vector<RouteMeterState> GlobalRouteMeterState;
 
   public:
@@ -119,17 +132,6 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
                Recording = 2
        };
 
-       enum SlaveSource {
-               None = 0,
-               MTC,
-               JACK
-       };
-       
-       enum AutoConnectOption {
-               AutoConnectPhysical = 0x1,
-               AutoConnectMaster = 0x2
-       };
-
        struct Event {
            enum Type {
                    SetTransportSpeed,
@@ -162,26 +164,26 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
                    Replace,
                    Clear
            };
-
-           Type           type;
+               
+               Type           type;
            Action         action;
-           jack_nframes_t action_frame;
-           jack_nframes_t target_frame;
+           nframes_t action_frame;
+           nframes_t target_frame;
            float          speed;
 
            union {
                        void*                ptr;
                        bool                 yes_or_no;
-                       Session::SlaveSource slave;
+                       SlaveSource slave;
                        Route*               route;
            };
 
-           boost::shared_ptr<Region>   region;
+           boost::shared_ptr<Region>     region;
 
            list<AudioRange>     audio_range;
            list<MusicRange>     music_range;
 
-           Event(Type t, Action a, jack_nframes_t when, jack_nframes_t where, float spd, bool yn = false)
+           Event(Type t, Action a, nframes_t when, nframes_t where, float spd, bool yn = false)
                    : type (t), 
                      action (a),
                      action_frame (when),
@@ -213,7 +215,7 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
                    pool.release (ptr);
            }
 
-           static const jack_nframes_t Immediate = 0;
+           static const nframes_t Immediate = 0;
 
         private:
            static MultiAllocSingleReleasePool pool;
@@ -237,46 +239,41 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
                 uint32_t master_out_channels,
                 uint32_t n_physical_in,
                 uint32_t n_physical_out,
-                jack_nframes_t initial_length);
+                nframes_t initial_length);
        
        virtual ~Session ();
 
-
-       static int find_session (string str, string& path, string& snapshot, bool& isnew);
-       
        string path() const { return _path; }
        string name() const { return _name; }
        string snap_name() const { return _current_snapshot_name; }
+       string raid_path () const;
 
        void set_snap_name ();
 
        void set_dirty ();
        void set_clean ();
        bool dirty() const { return _state_of_the_state & Dirty; }
+       void set_deletion_in_progress ();
+       bool deletion_in_progress() const { return _state_of_the_state & Deletion; }
        sigc::signal<void> DirtyChanged;
 
-       std::string sound_dir () const;
-       std::string tape_dir () const;
-       std::string peak_dir () const;
-       std::string dead_sound_dir () const;
+       const SessionDirectory& session_directory () const { return *(_session_dir.get()); }
+
        std::string automation_dir () const;
 
-       static string suffixed_search_path (std::string suffix, bool data);
-       static string control_protocol_path ();
-       static string template_path ();
-       static string template_dir ();
-       static void get_template_list (list<string>&);
-       
        static string change_audio_path_by_name (string oldpath, string oldname, string newname, bool destructive);
-       static string peak_path_from_audio_path (string);
+       static string change_midi_path_by_name (string oldpath, string oldname, string newname, bool destructive);
+       
+       string peak_path_from_audio_path (string) const;
        string audio_path_from_name (string, uint32_t nchans, uint32_t chan, bool destructive);
+       string midi_path_from_name (string);
 
-       void process (jack_nframes_t nframes);
-
-       vector<Sample*>& get_passthru_buffers() { return _passthru_buffers; }
-       vector<Sample*>& get_silent_buffers (uint32_t howmany);
-       vector<Sample*>& get_send_buffers () { return _send_buffers; }
+       void process (nframes_t nframes);
 
+       BufferSet& get_silent_buffers (ChanCount count = ChanCount::ZERO);
+       BufferSet& get_scratch_buffers (ChanCount count = ChanCount::ZERO);
+       BufferSet& get_mix_buffers (ChanCount count = ChanCount::ZERO);
+       
        void add_diskstream (boost::shared_ptr<Diskstream>);
        boost::shared_ptr<Diskstream> diskstream_by_id (const PBD::ID& id);
        boost::shared_ptr<Diskstream> diskstream_by_name (string name);
@@ -291,11 +288,11 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
        
        typedef std::list<boost::shared_ptr<Diskstream> > DiskstreamList;
        typedef std::list<boost::shared_ptr<Route> >      RouteList; 
-
+       
        boost::shared_ptr<RouteList> get_routes() const {
                return routes.reader ();
        }
-
+       
        uint32_t nroutes() const { return routes.reader()->size(); }
        uint32_t ntracks () const;
        uint32_t nbusses () const;
@@ -331,6 +328,8 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
        void disable_record (bool rt_context, bool force = false);
        void step_back_from_record ();
        
+       void maybe_write_autosave ();
+
        /* Proxy signal for region hidden changes */
 
        sigc::signal<void,boost::shared_ptr<Region> > RegionHiddenChange;
@@ -341,28 +340,33 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
        
        /* Record status signals */
 
-        sigc::signal<void> RecordStateChanged;
+       sigc::signal<void> RecordStateChanged;
 
        /* Transport mechanism signals */
 
        sigc::signal<void> TransportStateChange; /* generic */
-       sigc::signal<void,jack_nframes_t> PositionChanged; /* sent after any non-sequential motion */
+       sigc::signal<void,nframes_t> PositionChanged; /* sent after any non-sequential motion */
        sigc::signal<void> DurationChanged;
        sigc::signal<void> HaltOnXrun;
+       sigc::signal<void> TransportLooped;
 
        sigc::signal<void,RouteList&> RouteAdded;
 
        void request_roll ();
-       void request_bounded_roll (jack_nframes_t start, jack_nframes_t end);
+       void request_bounded_roll (nframes_t start, nframes_t end);
        void request_stop (bool abort = false);
-       void request_locate (jack_nframes_t frame, bool with_roll = false);
-       void request_auto_loop (bool yn);
-       jack_nframes_t  last_transport_start() const { return _last_roll_location; }
+       void request_locate (nframes_t frame, bool with_roll = false);
+
+       void request_play_loop (bool yn);
+       bool get_play_loop () const { return play_loop; }
+
+       nframes_t  last_transport_start() const { return _last_roll_location; }
        void goto_end ()   { request_locate (end_location->start(), false);}
        void goto_start () { request_locate (start_location->start(), false); }
-       void set_session_start (jack_nframes_t start) { start_location->set_start(start); }
-       void set_session_end (jack_nframes_t end) { end_location->set_start(end); _end_location_is_free = false; }
+       void set_session_start (nframes_t start) { start_location->set_start(start); }
+       void set_session_end (nframes_t end) { end_location->set_start(end); _end_location_is_free = false; }
        void use_rf_shuttle_speed ();
+       void allow_auto_play (bool yn);
        void request_transport_speed (float speed);
        void request_overwrite_buffer (Diskstream*);
        void request_diskstream_speed (Diskstream&, float speed);
@@ -372,17 +376,21 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
        bool transport_locked () const;
 
        int wipe ();
+       //int wipe_diskstream (AudioDiskstream *);
 
        int remove_region_from_region_list (boost::shared_ptr<Region>);
 
-       jack_nframes_t get_maximum_extent () const;
-       jack_nframes_t current_end_frame() const { return end_location->start(); }
-       jack_nframes_t current_start_frame() const { return start_location->start(); }
-       jack_nframes_t frame_rate() const   { return _current_frame_rate; }
-       jack_nframes_t frames_per_hour() const { return _frames_per_hour; }
+       nframes_t get_maximum_extent () const;
+       nframes_t current_end_frame() const { return end_location->start(); }
+       nframes_t current_start_frame() const { return start_location->start(); }
+       nframes_t frame_rate() const   { return _current_frame_rate; }
+       nframes_t frames_per_hour() const { return _frames_per_hour; }
 
        double frames_per_smpte_frame() const { return _frames_per_smpte_frame; }
-       jack_nframes_t smpte_frames_per_hour() const { return _smpte_frames_per_hour; }
+       nframes_t smpte_frames_per_hour() const { return _smpte_frames_per_hour; }
+
+       float smpte_frames_per_second() const;
+       bool smpte_drop_frames() const;
 
        /* Locations */
 
@@ -394,104 +402,26 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
 
        void set_auto_punch_location (Location *);
        void set_auto_loop_location (Location *);
+       int location_name(string& result, string base = string(""));
 
-
-       enum ControlType {
-               AutoPlay,
-               AutoLoop,
-               AutoReturn,
-               AutoInput,
-               PunchIn,
-               PunchOut,
-               SendMTC,
-               MMCControl,
-               SoloLatch,
-               SoloingModel,
-               RecordingPlugins,
-               CrossFadesActive,
-               SendMMC,
-               SlaveType,
-               Clicking,
-               EditingMode,
-               PlayRange,
-               LayeringModel,
-               CrossfadingModel,
-               SeamlessLoop,
-               MidiFeedback,
-               MidiControl,
-               TranzportControl,
-               Feedback,
-               SmpteMode,
-       };
-
-       sigc::signal<void,ControlType> ControlChanged;
-
-       void set_auto_play (bool yn);
-       void set_auto_return (bool yn);
-       void set_auto_input (bool yn);
        void reset_input_monitor_state ();
-       void set_input_auto_connect (bool yn);
-       void set_output_auto_connect (AutoConnectOption);
-       void set_punch_in (bool yn);
-       void set_punch_out (bool yn);
-       void set_send_mtc (bool yn);
-       void set_send_mmc (bool yn);
-       void set_mmc_control (bool yn);
-       void set_midi_feedback (bool yn);
-       void set_midi_control (bool yn);
-       void set_do_not_record_plugins (bool yn);
-       void set_crossfades_active (bool yn);
-       void set_seamless_loop (bool yn);
-
-       bool get_auto_play () const { return auto_play; }
-       bool get_auto_input () const { return auto_input; }
-       bool get_auto_loop () const { return auto_loop; }
-       bool get_seamless_loop () const { return seamless_loop; }
-       bool get_punch_in () const { return punch_in; }
-       bool get_punch_out () const { return punch_out; }
-       bool get_all_safe () const { return all_safe; }
-       bool get_auto_return () const { return auto_return; }
-       bool get_send_mtc () const;
-       bool get_send_mmc () const;
-       bool get_mmc_control () const;
-       bool get_midi_feedback () const;
-       bool get_midi_control () const;
-       bool get_do_not_record_plugins () const { return do_not_record_plugins; }
-       bool get_crossfades_active () const { return crossfades_active; }
-
-       bool get_input_auto_connect () const;
-       AutoConnectOption get_output_auto_connect () const { return output_auto_connect; }
-
-       enum LayerModel {
-               LaterHigher,
-               MoveAddHigher,
-               AddHigher
-       };
-
-       void set_layer_model (LayerModel);
-       LayerModel get_layer_model () const { return layer_model; }
-
-       void set_xfade_model (CrossfadeModel);
-       CrossfadeModel get_xfade_model () const { return xfade_model; }
 
-       void add_event (jack_nframes_t action_frame, Event::Type type, jack_nframes_t target_frame = 0);
-       void remove_event (jack_nframes_t frame, Event::Type type);
+       void add_event (nframes_t action_frame, Event::Type type, nframes_t target_frame = 0);
+       void remove_event (nframes_t frame, Event::Type type);
        void clear_events (Event::Type type);
 
-       jack_nframes_t get_block_size() const { return current_block_size; }
-       jack_nframes_t worst_output_latency () const { return _worst_output_latency; }
-       jack_nframes_t worst_input_latency () const { return _worst_input_latency; }
-       jack_nframes_t worst_track_latency () const { return _worst_track_latency; }
+       nframes_t get_block_size() const { return current_block_size; }
+       nframes_t worst_output_latency () const { return _worst_output_latency; }
+       nframes_t worst_input_latency () const { return _worst_input_latency; }
+       nframes_t worst_track_latency () const { return _worst_track_latency; }
 
        int save_state (string snapshot_name, bool pending = false);
        int restore_state (string snapshot_name);
        int save_template (string template_name);
-        int save_history (string snapshot_name = "");
-        int restore_history (string snapshot_name);
-
-       static int rename_template (string old_name, string new_name);
-
-       static int delete_template (string name);
+       int save_history (string snapshot_name = "");
+       int restore_history (string snapshot_name);
+       void remove_state (string snapshot_name);
+       void rename_state (string old_name, string new_name);
        
        sigc::signal<void,string> StateSaved;
        sigc::signal<void> StateReady;
@@ -503,7 +433,9 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
        int      set_state(const XMLNode& node); // not idempotent
        XMLNode& get_template();
 
-       void add_instant_xml (XMLNode&, const std::string& dir);
+       /// The instant xml file is written to the session directory
+       void add_instant_xml (XMLNode&);
+       XMLNode * instant_xml (const std::string& str);
 
        enum StateOfTheState {
                Clean = 0x0,
@@ -547,61 +479,25 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
 
        std::list<boost::shared_ptr<AudioTrack> > new_audio_track (int input_channels, int output_channels, TrackMode mode = Normal, uint32_t how_many = 1);
        RouteList new_audio_route (int input_channels, int output_channels, uint32_t how_many);
+       
+       std::list<boost::shared_ptr<MidiTrack> > new_midi_track (TrackMode mode = Normal, uint32_t how_many = 1);
+       //boost::shared_ptr<Route>     new_midi_route (uint32_t how_many = 1);
 
        void   remove_route (boost::shared_ptr<Route>);
-
        void   resort_routes ();
        void   resort_routes_using (boost::shared_ptr<RouteList>);
+       
+       void    set_remote_control_ids();
 
        AudioEngine &engine() { return _engine; };
 
-       /* configuration. there should really be accessors/mutators
-          for these 
-       */
-
-       float   meter_hold () { return _meter_hold; }
-       void    set_meter_hold (float);
-       sigc::signal<void> MeterHoldChanged;
-
-       float   meter_falloff () { return _meter_falloff; }
-       void    set_meter_falloff (float);
-       sigc::signal<void> MeterFalloffChanged;
-       
        int32_t  max_level;
        int32_t  min_level;
-       string  click_emphasis_sound;
-       string  click_sound;
-       bool    click_requested;
-       jack_nframes_t over_length_short;
-       jack_nframes_t over_length_long;
-       bool    send_midi_timecode;
-       bool    send_midi_machine_control;
-       float   shuttle_speed_factor;
-       float   shuttle_speed_threshold;
-       float   rf_speed;
-       float   smpte_frames_per_second;
-       float   video_pullup;
-       bool    smpte_drop_frames;
-       AnyTime preroll;
-       AnyTime postroll;
-       
+
        /* Time */
 
-       jack_nframes_t transport_frame () const {return _transport_frame; }
-       jack_nframes_t audible_frame () const;
-
-       enum SmpteFormat {
-               smpte_23976,
-               smpte_24,
-               smpte_24976,
-               smpte_25,
-               smpte_2997,
-               smpte_2997drop,
-               smpte_30,
-               smpte_30drop,
-               smpte_5994,
-               smpte_60,
-       };
+       nframes_t transport_frame () const {return _transport_frame; }
+       nframes_t audible_frame () const;
 
        enum PullupFormat {
                pullup_Plus4Plus1,
@@ -612,63 +508,62 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
                pullup_Minus1,
                pullup_Minus4Plus1,
                pullup_Minus4,
-               pullup_Minus4Minus1,
+               pullup_Minus4Minus1
        };
 
-       int  set_smpte_type (float fps, bool drop_frames);
-       int  set_video_pullup (float pullup);
-
+       int  set_smpte_format (SmpteFormat);
        void sync_time_vars();
 
-       void bbt_time (jack_nframes_t when, BBT_Time&);
-       void smpte_to_sample( SMPTE::Time& smpte, jack_nframes_t& sample, bool use_offset, bool use_subframes ) const;
-       void sample_to_smpte( jack_nframes_t sample, SMPTE::Time& smpte, bool use_offset, bool use_subframes ) const;
+       void bbt_time (nframes_t when, BBT_Time&);
+       void smpte_to_sample( SMPTE::Time& smpte, nframes_t& sample, bool use_offset, bool use_subframes ) const;
+       void sample_to_smpte( nframes_t sample, SMPTE::Time& smpte, bool use_offset, bool use_subframes ) const;
        void smpte_time (SMPTE::Time &);
-       void smpte_time (jack_nframes_t when, SMPTE::Time&);
-       void smpte_time_subframes (jack_nframes_t when, SMPTE::Time&);
+       void smpte_time (nframes_t when, SMPTE::Time&);
+       void smpte_time_subframes (nframes_t when, SMPTE::Time&);
 
-       void smpte_duration (jack_nframes_t, SMPTE::Time&) const;
-       void smpte_duration_string (char *, jack_nframes_t) const;
+       void smpte_duration (nframes_t, SMPTE::Time&) const;
+       void smpte_duration_string (char *, nframes_t) const;
 
-       void           set_smpte_offset (jack_nframes_t);
-       jack_nframes_t smpte_offset () const { return _smpte_offset; }
+       void           set_smpte_offset (nframes_t);
+       nframes_t smpte_offset () const { return _smpte_offset; }
        void           set_smpte_offset_negative (bool);
        bool           smpte_offset_negative () const { return _smpte_offset_negative; }
 
-       jack_nframes_t convert_to_frames_at (jack_nframes_t position, AnyTime&);
+       nframes_t convert_to_frames_at (nframes_t position, AnyTime&);
 
        static sigc::signal<void> StartTimeChanged;
        static sigc::signal<void> EndTimeChanged;
        static sigc::signal<void> SMPTEOffsetChanged;
-       static sigc::signal<void> SMPTETypeChanged;
-       static sigc::signal<void> PullupChanged;
 
-       void        request_slave_source (SlaveSource, jack_nframes_t pos = 0);
-       SlaveSource slave_source() const { return _slave_type; }
-       bool        synced_to_jack() const { return _slave_type == JACK; }
+       void        request_slave_source (SlaveSource);
+       bool        synced_to_jack() const { return Config->get_slave_source() == JACK; }
+
        float       transport_speed() const { return _transport_speed; }
        bool        transport_stopped() const { return _transport_speed == 0.0f; }
        bool        transport_rolling() const { return _transport_speed != 0.0f; }
 
-       int jack_slave_sync (jack_nframes_t);
+       int jack_slave_sync (nframes_t);
 
        TempoMap& tempo_map() { return *_tempo_map; }
        
        /* region info  */
 
-       sigc::signal<void,boost::shared_ptr<AudioRegion> > AudioRegionAdded;
-       sigc::signal<void,boost::shared_ptr<AudioRegion> > AudioRegionRemoved;
+       sigc::signal<void,boost::shared_ptr<Region> > RegionAdded;
+       sigc::signal<void,boost::shared_ptr<Region> > RegionRemoved;
 
        int region_name (string& result, string base = string(""), bool newlevel = false) const;
        string new_region_name (string);
        string path_from_region_name (string name, string identifier);
 
-       boost::shared_ptr<AudioRegion> find_whole_file_parent (boost::shared_ptr<AudioRegion>);
+       boost::shared_ptr<Region> find_whole_file_parent (boost::shared_ptr<Region const>);
+       
        void find_equivalent_playlist_regions (boost::shared_ptr<Region>, std::vector<boost::shared_ptr<Region> >& result);
 
-       boost::shared_ptr<AudioRegion> XMLRegionFactory (const XMLNode&, bool full);
+       boost::shared_ptr<Region>      XMLRegionFactory (const XMLNode&, bool full);
+       boost::shared_ptr<AudioRegion> XMLAudioRegionFactory (const XMLNode&, bool full);
+       boost::shared_ptr<MidiRegion>  XMLMidiRegionFactory (const XMLNode&, bool full);
 
-       template<class T> void foreach_audio_region (T *obj, void (T::*func)(boost::shared_ptr<AudioRegion>));
+       template<class T> void foreach_region (T *obj, void (T::*func)(boost::shared_ptr<Region>));
 
        /* source management */
 
@@ -679,30 +574,29 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
            bool multichan;
            bool sample_convert;
            volatile bool freeze;
-           string pathname;
+           std::vector<Glib::ustring> paths;
            
            /* result */
-           std::vector<boost::shared_ptr<AudioRegion> > new_regions;
-           
+           std::vector<boost::shared_ptr<Region> > new_regions;
        };
 
        int import_audiofile (import_status&);
        bool sample_rate_convert (import_status&, string infile, string& outfile);
        string build_tmp_convert_name (string file);
 
-       Session::SlaveSource post_export_slave;
-       jack_nframes_t post_export_position;
+       SlaveSource post_export_slave;
+       nframes_t post_export_position;
 
        int start_audio_export (ARDOUR::AudioExportSpecification&);
        int stop_audio_export (ARDOUR::AudioExportSpecification&);
-       
+       void finalize_audio_export ();
+
        void add_source (boost::shared_ptr<Source>);
        void remove_source (boost::weak_ptr<Source>);
-       int  cleanup_audio_file_source (boost::shared_ptr<AudioFileSource>);
 
        struct cleanup_report {
            vector<string> paths;
-           int32_t space;
+           int64_t space;
        };
 
        int  cleanup_sources (cleanup_report&);
@@ -718,8 +612,7 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
           this playlist.
        */
        
-       sigc::signal<int,ARDOUR::Playlist*> AskAboutPlaylistDeletion;
-
+       sigc::signal<int,boost::shared_ptr<ARDOUR::Playlist> > AskAboutPlaylistDeletion;
 
        /* handlers should return !0 for use pending state, 0 for
           ignore it.
@@ -727,23 +620,24 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
 
        static sigc::signal<int> AskAboutPendingState;
        
-       sigc::signal<void,boost::shared_ptr<Source> > SourceAdded;
-       sigc::signal<void,boost::shared_ptr<Source> > SourceRemoved;
-
        boost::shared_ptr<AudioFileSource> create_audio_source_for_session (ARDOUR::AudioDiskstream&, uint32_t which_channel, bool destructive);
 
+       boost::shared_ptr<MidiSource> create_midi_source_for_session (ARDOUR::MidiDiskstream&);
+
        boost::shared_ptr<Source> source_by_id (const PBD::ID&);
+       boost::shared_ptr<Source> source_by_path_and_channel (const Glib::ustring&, uint16_t);
 
        /* playlist management */
 
-       Playlist* playlist_by_name (string name);
-       void add_playlist (Playlist *);
-       sigc::signal<void,Playlist*> PlaylistAdded;
-       sigc::signal<void,Playlist*> PlaylistRemoved;
+       boost::shared_ptr<Playlist> playlist_by_name (string name);
+       void add_playlist (boost::shared_ptr<Playlist>);
+       sigc::signal<void,boost::shared_ptr<Playlist> > PlaylistAdded;
+       sigc::signal<void,boost::shared_ptr<Playlist> > PlaylistRemoved;
 
        uint32_t n_playlists() const;
 
-       template<class T> void foreach_playlist (T *obj, void (T::*func)(Playlist *));
+       template<class T> void foreach_playlist (T *obj, void (T::*func)(boost::shared_ptr<Playlist>));
+       void get_playlists (std::vector<boost::shared_ptr<Playlist> >&);
 
        /* named selections */
 
@@ -755,9 +649,11 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
        sigc::signal<void> NamedSelectionAdded;
        sigc::signal<void> NamedSelectionRemoved;
 
-        /* Curves and AutomationLists (TODO when they go away) */
-        void add_curve(Curve*);
-        void add_automation_list(AutomationList*);
+       /* Curves and AutomationLists (TODO when they go away) */
+       void add_curve(Curve*);
+       void add_automation_list(AutomationList*);
+       
+       nframes_t automation_interval () const { return _automation_interval; }
 
        /* fade curves */
 
@@ -777,29 +673,19 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
 
        /* flattening stuff */
 
-       int write_one_audio_track (AudioTrack&, jack_nframes_t start, jack_nframes_t cnt, bool overwrite, vector<boost::shared_ptr<AudioSource> >&,
+       int write_one_audio_track (AudioTrack&, nframes_t start, nframes_t cnt, bool overwrite, vector<boost::shared_ptr<Source> >&,
                                   InterThreadInfo& wot);
        int freeze (InterThreadInfo&);
 
        /* session-wide solo/mute/rec-enable */
-
-       enum SoloModel {
-               InverseMute,
-               SoloBus
-       };
        
        bool soloing() const { return currently_soloing; }
 
-       SoloModel solo_model() const { return _solo_model; }
-       void set_solo_model (SoloModel);
-
-       bool solo_latched() const { return _solo_latched; }
-       void set_solo_latched (bool yn);
-       
        void set_all_solo (bool);
        void set_all_mute (bool);
 
        sigc::signal<void,bool> SoloActive;
+       sigc::signal<void> SoloChanged;
        
        void record_disenable_all ();
        void record_enable_all ();
@@ -815,25 +701,27 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
        uint32_t n_plugin_inserts() const { return _plugin_inserts.size(); }
        uint32_t n_sends() const { return _sends.size(); }
 
-       string next_send_name();
-       string next_insert_name();
-       
+       uint32_t next_send_id();
+       uint32_t next_insert_id();
+       void mark_send_id (uint32_t);
+       void mark_insert_id (uint32_t);
+
        /* s/w "RAID" management */
        
-       jack_nframes_t available_capture_duration();
+       nframes_t available_capture_duration();
 
-       /* I/O Connections */
+       /* I/O bundles */
 
-       template<class T> void foreach_connection (T *obj, void (T::*func)(Connection *));
-       void add_connection (Connection *);
-       void remove_connection (Connection *);
-       Connection *connection_by_name (string) const;
+       template<class T> void foreach_bundle (T *obj, void (T::*func)(Bundle *));
+       void add_bundle (Bundle *);
+       void remove_bundle (Bundle *);
+       Bundle *bundle_by_name (string) const;
 
-       sigc::signal<void,Connection *> ConnectionAdded;
-       sigc::signal<void,Connection *> ConnectionRemoved;
+       sigc::signal<void,Bundle *> BundleAdded;
+       sigc::signal<void,Bundle *> BundleRemoved;
 
        /* MIDI */
-       
+
        int set_mtc_port (string port_tag);
        int set_mmc_port (string port_tag);
        int set_midi_port (string port_tag);
@@ -855,27 +743,35 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
 
        void deliver_midi (MIDI::Port*, MIDI::byte*, int32_t size);
 
+       void set_mmc_device_id (uint32_t id);
+       
        /* Scrubbing */
 
-       void start_scrub (jack_nframes_t where);
+       void start_scrub (nframes_t where);
        void stop_scrub ();
        void set_scrub_speed (float);
-       jack_nframes_t scrub_buffer_size() const;
+       nframes_t scrub_buffer_size() const;
        sigc::signal<void> ScrubReady;
 
        /* History (for editors, mixers, UIs etc.) */
 
+       /** Undo some transactions.
+        * @param n Number of transactions to undo.
+        */
        void undo (uint32_t n) {
-               history.undo (n);
+               _history.undo (n);
        }
+
        void redo (uint32_t n) {
-               history.redo (n);
+               _history.redo (n);
        }
 
-       uint32_t undo_depth() const { return history.undo_depth(); }
-       uint32_t redo_depth() const { return history.redo_depth(); }
-       string next_undo() const { return history.next_undo(); }
-       string next_redo() const { return history.next_redo(); }
+       UndoHistory& history() { return _history; }
+       
+       uint32_t undo_depth() const { return _history.undo_depth(); }
+       uint32_t redo_depth() const { return _history.redo_depth(); }
+       string next_undo() const { return _history.next_undo(); }
+       string next_redo() const { return _history.next_redo(); }
 
        void begin_reversible_command (string cmd_name);
        void commit_reversible_command (Command* cmd = 0);
@@ -884,77 +780,83 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
                current_trans->add_command (cmd);
        }
 
-       std::map<PBD::ID, PBD::StatefulDestructible*> registry;
+       std::map<PBD::ID, PBD::StatefulThingWithGoingAway*> registry;
+
+       // these commands are implemented in libs/ardour/session_command.cc
+       Command* memento_command_factory(XMLNode* n);
+       void register_with_memento_command_factory(PBD::ID, PBD::StatefulThingWithGoingAway*);
 
-        // these commands are implemented in libs/ardour/session_command.cc
-       Command *memento_command_factory(XMLNode *n);
-        void register_with_memento_command_factory(PBD::ID, PBD::StatefulDestructible *);
+       Command* global_state_command_factory (const XMLNode& n);
 
-        class GlobalSoloStateCommand : public Command
+       class GlobalRouteStateCommand : public Command
+       {
+         public:
+               GlobalRouteStateCommand (Session&, void*);
+               GlobalRouteStateCommand (Session&, const XMLNode& node);
+               int set_state (const XMLNode&);
+               XMLNode& get_state ();
+
+         protected:
+               GlobalRouteBooleanState before, after;
+               Session& sess;
+               void* src;
+               
+       };
+
+        class GlobalSoloStateCommand : public GlobalRouteStateCommand
         {
-            GlobalRouteBooleanState before, after;
-            Session &sess;
-            void *src;
-        public:
-            GlobalSoloStateCommand(Session &, void *src);
-            void operator()();
-            void undo();
-            XMLNode &get_state();
-            void mark();
+         public:
+               GlobalSoloStateCommand (Session &, void *src);
+               GlobalSoloStateCommand (Session&, const XMLNode&);
+               void operator()(); //redo
+               void undo();
+               XMLNode &get_state();
+               void mark();
         };
 
-        class GlobalMuteStateCommand : public Command
+        class GlobalMuteStateCommand : public GlobalRouteStateCommand
         {
-            GlobalRouteBooleanState before, after;
-            Session &sess;
-            void *src;
-        public:
-            GlobalMuteStateCommand(Session &, void *src);
-            void operator()();
-            void undo();
-            XMLNode &get_state();
-            void mark();
+         public:
+               GlobalMuteStateCommand(Session &, void *src);
+               GlobalMuteStateCommand (Session&, const XMLNode&);
+               void operator()(); // redo
+               void undo();
+               XMLNode &get_state();
+               void mark();
         };
 
-        class GlobalRecordEnableStateCommand : public Command
+        class GlobalRecordEnableStateCommand : public GlobalRouteStateCommand
         {
-            GlobalRouteBooleanState before, after;
-            Session &sess;
-            void *src;
-        public:
-            GlobalRecordEnableStateCommand(Session &, void *src);
-            void operator()();
-            void undo();
-            XMLNode &get_state();
-            void mark();
+         public:
+               GlobalRecordEnableStateCommand(Session &, void *src);
+               GlobalRecordEnableStateCommand (Session&, const XMLNode&);
+               void operator()(); // redo
+               void undo();
+               XMLNode &get_state();
+               void mark();
         };
 
         class GlobalMeteringStateCommand : public Command
         {
-            GlobalRouteMeterState before, after;
-            Session &sess;
-            void *src;
-        public:
-            GlobalMeteringStateCommand(Session &, void *src);
-            void operator()();
-            void undo();
-            XMLNode &get_state();
-            void mark();
+         public:
+               GlobalMeteringStateCommand(Session &, void *src);
+               GlobalMeteringStateCommand (Session&, const XMLNode&);
+               void operator()();
+               void undo();
+               XMLNode &get_state();
+               int set_state (const XMLNode&);
+               void mark();
+
+         protected:
+               Session& sess;
+               void* src;
+               GlobalRouteMeterState before;
+               GlobalRouteMeterState after;
         };
 
-       /* edit mode */
-
-       void set_edit_mode (EditMode);
-       EditMode get_edit_mode () const { return _edit_mode; }
-
        /* clicking */
 
        boost::shared_ptr<IO>  click_io() { return _click_io; }
-       void set_clicking (bool yn);
-       bool get_clicking() const;
-
-       void set_click_sound (string path);
-       void set_click_emphasis_sound (string path);
                
        /* tempo FX */
 
@@ -971,13 +873,6 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
 
        boost::shared_ptr<AudioRegion> tempoize_region (TimeStretchRequest&);
 
-       string raid_path() const;
-       void   set_raid_path(string);
-
-       /* need to call this whenever we change native file formats */
-
-       void reset_native_file_format();
-
        /* disk, buffer loads */
 
        uint32_t playback_load ();
@@ -999,23 +894,10 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
        void request_play_range (bool yn);
        bool get_play_range () const { return _play_range; }
 
-       /* favorite dirs */
-       typedef vector<string> FavoriteDirs;
-
-       static int read_favorite_dirs (FavoriteDirs&);
-
-       static int write_favorite_dirs (FavoriteDirs&);
-       
-       /* file suffixes */
-
-       static const char* template_suffix() { return _template_suffix; }
-       static const char* statefile_suffix() { return _statefile_suffix; }
-       static const char* pending_suffix() { return _pending_suffix; }
-
        /* buffers for gain and pan */
 
        gain_t* gain_automation_buffer () const { return _gain_automation_buffer; }
-       pan_t** pan_automation_buffer() const { return _pan_automation_buffer; }
+       pan_t** pan_automation_buffer () const  { return _pan_automation_buffer; }
 
        /* buffers for conversion */
        enum RunContext {
@@ -1033,26 +915,19 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
                                  void* ptr,
                                  float opt);
 
-       typedef float (*compute_peak_t)                         (Sample *, jack_nframes_t, float);
-       typedef void  (*apply_gain_to_buffer_t)         (Sample *, jack_nframes_t, float);
-       typedef void  (*mix_buffers_with_gain_t)        (Sample *, Sample *, jack_nframes_t, float);
-       typedef void  (*mix_buffers_no_gain_t)          (Sample *, Sample *, jack_nframes_t);
-
-       static compute_peak_t                   compute_peak;
-       static apply_gain_to_buffer_t   apply_gain_to_buffer;
-       static mix_buffers_with_gain_t  mix_buffers_with_gain;
-       static mix_buffers_no_gain_t    mix_buffers_no_gain;
-
        static sigc::signal<void> SendFeedback;
 
        /* Controllables */
 
-       PBD::Controllable* controllable_by_id (const PBD::ID&);
+       boost::shared_ptr<PBD::Controllable> controllable_by_id (const PBD::ID&);
+
+       void add_controllable (boost::shared_ptr<PBD::Controllable>);
+       void remove_controllable (PBD::Controllable*);
 
   protected:
        friend class AudioEngine;
-       void set_block_size (jack_nframes_t nframes);
-       void set_frame_rate (jack_nframes_t nframes);
+       void set_block_size (nframes_t nframes);
+       void set_frame_rate (nframes_t nframes);
 
   protected:
        friend class Diskstream;
@@ -1065,11 +940,13 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
        void update_latency_compensation (bool, bool);
        
   private:
-       int  create (bool& new_session, string* mix_template, jack_nframes_t initial_length);
+       void destroy ();
+
+       void initialize_start_and_end_locations(nframes_t start, nframes_t end);
+       bool create_session_file();
+       bool create_session_file_from_template (const string& template_path);
 
-       static const char* _template_suffix;
-       static const char* _statefile_suffix;
-       static const char* _pending_suffix;
+       nframes_t compute_initial_length ();
 
        enum SubState {
                PendingDeclickIn   = 0x1,
@@ -1084,35 +961,36 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
           maximise cache hits
        */
 
-       typedef void (Session::*process_function_type)(jack_nframes_t);
+       typedef void (Session::*process_function_type)(nframes_t);
 
        AudioEngine            &_engine;
        mutable gint            processing_prohibited;
+       /// the function called when the main JACK process callback happens
        process_function_type    process_function;
        process_function_type    last_process_function;
        bool                     waiting_for_sync_offset;
-       jack_nframes_t          _base_frame_rate;
-       jack_nframes_t          _current_frame_rate;  //this includes video pullup offset
+       nframes_t          _base_frame_rate;
+       nframes_t          _current_frame_rate;  //this includes video pullup offset
        int                      transport_sub_state;
        mutable gint           _record_status;
-       jack_nframes_t          _transport_frame;
+       nframes_t          _transport_frame;
        Location*                end_location;
        Location*                start_location;
        Slave                  *_slave;
-       SlaveSource             _slave_type;
        volatile float          _transport_speed;
        volatile float          _desired_transport_speed;
        float                   _last_transport_speed;
-       jack_nframes_t          _last_slave_transport_frame;
-       jack_nframes_t           maximum_output_latency;
-       jack_nframes_t           last_stop_frame;
-       vector<Sample *>        _passthru_buffers;
-       vector<Sample *>        _silent_buffers;
-       vector<Sample *>        _send_buffers;
-       jack_nframes_t           current_block_size;
-       jack_nframes_t          _worst_output_latency;
-       jack_nframes_t          _worst_input_latency;
-       jack_nframes_t          _worst_track_latency;
+       bool                     auto_play_legal;
+       nframes_t          _last_slave_transport_frame;
+       nframes_t           maximum_output_latency;
+       nframes_t           last_stop_frame;
+       BufferSet*              _scratch_buffers;
+       BufferSet*              _silent_buffers;
+       BufferSet*              _mix_buffers;
+       nframes_t           current_block_size;
+       nframes_t          _worst_output_latency;
+       nframes_t          _worst_input_latency;
+       nframes_t          _worst_track_latency;
        bool                    _have_captured;
        float                   _meter_hold;
        float                   _meter_falloff;
@@ -1125,13 +1003,13 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
 
        void update_latency_compensation_proxy (void* ignored);
 
-       void ensure_passthru_buffers (uint32_t howmany);
+       void ensure_buffers (ChanCount howmany);
        
-       void process_scrub          (jack_nframes_t);
-       void process_without_events (jack_nframes_t);
-       void process_with_events    (jack_nframes_t);
-       void process_audition       (jack_nframes_t);
-       int  process_export         (jack_nframes_t, ARDOUR::AudioExportSpecification*);
+       void process_scrub          (nframes_t);
+       void process_without_events (nframes_t);
+       void process_with_events    (nframes_t);
+       void process_audition       (nframes_t);
+       int  process_export         (nframes_t, ARDOUR::AudioExportSpecification*);
        
        /* slave tracking */
 
@@ -1149,24 +1027,25 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
        };
        
        SlaveState slave_state;
-       jack_nframes_t slave_wait_end;
+       nframes_t slave_wait_end;
 
        void reset_slave_state ();
-       bool follow_slave (jack_nframes_t, jack_nframes_t);
+       bool follow_slave (nframes_t, nframes_t);
+       void set_slave_source (SlaveSource);
 
        bool _exporting;
        int prepare_to_export (ARDOUR::AudioExportSpecification&);
 
        void prepare_diskstreams ();
-       void commit_diskstreams (jack_nframes_t, bool& session_requires_butler);
-       int  process_routes (jack_nframes_t, jack_nframes_t);
-       int  silent_process_routes (jack_nframes_t, jack_nframes_t);
+       void commit_diskstreams (nframes_t, bool& session_requires_butler);
+       int  process_routes (nframes_t, nframes_t);
+       int  silent_process_routes (nframes_t, nframes_t);
 
        bool get_rec_monitors_input () {
                if (actively_recording()) {
                        return true;
                } else {
-                       if (auto_input) {
+                       if (Config->get_auto_input()) {
                                return false;
                        } else {
                                return true;
@@ -1186,7 +1065,7 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
                }
        }
 
-       bool maybe_stop (jack_nframes_t limit) {
+       bool maybe_stop (nframes_t limit) {
                if ((_transport_speed > 0.0f && _transport_frame >= limit) || (_transport_speed < 0.0f && _transport_frame == 0)) {
                        stop_transport ();
                        return true;
@@ -1194,7 +1073,7 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
                return false;
        }
 
-       bool maybe_sync_start (jack_nframes_t&, jack_nframes_t&);
+       bool maybe_sync_start (nframes_t&, nframes_t&);
 
        void check_declick_out ();
 
@@ -1204,32 +1083,19 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
        MIDI::Port*             _midi_port;
        string                  _path;
        string                  _name;
-       bool                     do_not_record_plugins;
-
-       /* toggles */
-
-       bool auto_play;
-       bool punch_in;
-       bool punch_out;
-       bool auto_loop;
-       bool seamless_loop;
-       bool loop_changing;
-       jack_nframes_t last_loopend;
-       bool auto_input;
-       bool crossfades_active;
-       bool all_safe;
-       bool auto_return;
-       bool monitor_in;
-       bool send_mtc;
-       bool send_mmc;
-       bool mmc_control;
-       bool midi_control;
+       bool                     session_send_mmc;
+       bool                     session_send_mtc;
+       bool                     session_midi_feedback;
+       bool                     play_loop;
+       bool                     loop_changing;
+       nframes_t           last_loopend;
+
+       boost::scoped_ptr<SessionDirectory>        _session_dir;
 
        RingBuffer<Event*> pending_events;
 
        void hookup_io ();
        void when_engine_running ();
-       sigc::connection first_time_running;
        void graph_reordered ();
 
        string _current_snapshot_name;
@@ -1242,11 +1108,12 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
        int      load_options (const XMLNode&);
        XMLNode& get_options () const;
        int      load_state (string snapshot_name);
+       bool     save_config_options_predicate (ConfigVariableBase::Owner owner) const;
 
-       jack_nframes_t   _last_roll_location;
-       jack_nframes_t   _last_record_location;
+       nframes_t   _last_roll_location;
+       nframes_t   _last_record_location;
        bool              pending_locate_roll;
-       jack_nframes_t    pending_locate_frame;
+       nframes_t    pending_locate_frame;
 
        bool              pending_locate_flush;
        bool              pending_abort;
@@ -1260,6 +1127,8 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
        bool              butler_should_run;
        mutable gint      butler_should_do_transport_work;
        int               butler_request_pipe[2];
+
+       inline bool transport_work_requested() const { return g_atomic_int_get(&butler_should_do_transport_work); }
        
        struct ButlerRequest {
            enum Type {
@@ -1311,7 +1180,7 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
        uint32_t    rf_scale;
 
        void set_rf_speed (float speed);
-       void reset_rf_scale (jack_nframes_t frames_moved);
+       void reset_rf_scale (nframes_t frames_moved);
 
        Locations        _locations;
        void              locations_changed ();
@@ -1346,7 +1215,7 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
        void dump_events () const;
        void queue_event (Event *ev);
        void merge_event (Event*);
-       void replace_event (Event::Type, jack_nframes_t action_frame, jack_nframes_t target = 0);
+       void replace_event (Event::Type, nframes_t action_frame, nframes_t target = 0);
        bool _replace_event (Event*);
        bool _remove_event (Event *);
        void _clear_event_type (Event::Type);
@@ -1357,7 +1226,7 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
        void remove_empty_sounds ();
 
        void setup_midi_control ();
-       int  midi_read (MIDI::Port *);
+       //int  midi_read (MIDI::Port *);
 
        void enable_record ();
        
@@ -1383,13 +1252,13 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
        void *do_work();
 
        void set_next_event ();
-       void process_event (Event *);
+       void process_event (Event *ev);
 
        /* MIDI Machine Control */
 
-       void deliver_mmc (MIDI::MachineControl::Command, jack_nframes_t);
-       void deliver_midi_message (MIDI::Port * port, MIDI::eventType ev, MIDI::channel_t, MIDI::EventTwoBytes);
-       void deliver_data (MIDI::Port* port, MIDI::byte*, int32_t size);
+       void deliver_mmc (MIDI::MachineControl::Command, nframes_t);
+       //void deliver_midi_message (MIDI::Port * port, MIDI::eventType ev, MIDI::channel_t, MIDI::EventTwoBytes);
+       //void deliver_data (MIDI::Port* port, MIDI::byte*, int32_t size);
 
        void spp_start (MIDI::Parser&);
        void spp_continue (MIDI::Parser&);
@@ -1402,8 +1271,7 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
        void mmc_record_pause (MIDI::MachineControl &);
        void mmc_record_strobe (MIDI::MachineControl &);
        void mmc_record_exit (MIDI::MachineControl &);
-       void mmc_track_record_status (MIDI::MachineControl &, 
-                                     uint32_t track, bool enabled);
+       void mmc_track_record_status (MIDI::MachineControl &, uint32_t track, bool enabled);
        void mmc_fast_forward (MIDI::MachineControl &);
        void mmc_rewind (MIDI::MachineControl &);
        void mmc_locate (MIDI::MachineControl &, const MIDI::byte *);
@@ -1423,38 +1291,32 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
        MIDI::byte mtc_msg[16];
        MIDI::byte mtc_smpte_bits;   /* encoding of SMTPE type for MTC */
        MIDI::byte midi_msg[16];
-       jack_nframes_t  outbound_mtc_smpte_frame;
+       nframes_t  outbound_mtc_smpte_frame;
        SMPTE::Time transmitting_smpte_time;
        int next_quarter_frame_to_send;
        
        double _frames_per_smpte_frame; /* has to be floating point because of drop frame */
-       jack_nframes_t _frames_per_hour;
-       jack_nframes_t _smpte_frames_per_hour;
-       jack_nframes_t _smpte_offset;
+       nframes_t _frames_per_hour;
+       nframes_t _smpte_frames_per_hour;
+       nframes_t _smpte_offset;
        bool _smpte_offset_negative;
-       
-       /* cache the most-recently requested time conversions.
-          this helps when we have multiple clocks showing the
-          same time (e.g. the transport frame)
-       */
 
-       bool       last_smpte_valid;
-       jack_nframes_t  last_smpte_when;
-       SMPTE::Time last_smpte;
-
-       int send_full_time_code ();
-       int send_midi_time_code ();
+       /* cache the most-recently requested time conversions. This helps when we
+        * have multiple clocks showing the same time (e.g. the transport frame) */
+       bool           last_smpte_valid;
+       nframes_t last_smpte_when;
+       SMPTE::Time    last_smpte;
+       
+       bool _send_smpte_update; ///< Flag to send a full frame (SMPTE) MTC message this cycle
 
-       void send_full_time_code_in_another_thread ();
-       void send_midi_time_code_in_another_thread ();
-       void send_time_code_in_another_thread (bool full);
-       void send_mmc_in_another_thread (MIDI::MachineControl::Command, jack_nframes_t frame = 0);
+       int send_full_time_code(nframes_t nframes);
+       int send_midi_time_code_for_cycle(nframes_t nframes);
 
-       jack_nframes_t adjust_apparent_position (jack_nframes_t frames);
+       nframes_t adjust_apparent_position (nframes_t frames);
        
        void reset_record_status ();
        
-       int no_roll (jack_nframes_t nframes, jack_nframes_t offset);
+       int no_roll (nframes_t nframes, nframes_t offset);
        
        bool non_realtime_work_pending() const { return static_cast<bool>(post_transport_work); }
        bool process_can_proceed() const { return !(post_transport_work & ProcessCannotProceedMask); }
@@ -1473,7 +1335,7 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
            
            Type type;
            MIDI::MachineControl::Command mmc_cmd;
-           jack_nframes_t locate_frame;
+           nframes_t locate_frame;
 
            // for SendMessage type
 
@@ -1503,28 +1365,19 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
            static MultiAllocSingleReleasePool pool;
        };
 
-       Glib::Mutex       midi_lock;
-       pthread_t       midi_thread;
-       int             midi_request_pipe[2];
        mutable  gint   butler_active;
-       RingBuffer<MIDIRequest*> midi_requests;
-
-       int           start_midi_thread ();
-       void          terminate_midi_thread ();
-       void          poke_midi_thread ();
-       static void *_midi_thread_work (void *arg);
-       void          midi_thread_work ();
+       
        void          change_midi_ports ();
        int           use_config_midi_ports ();
 
        bool waiting_to_start;
 
-       void set_auto_loop (bool yn);
+       void set_play_loop (bool yn);
        void overwrite_some_buffers (Diskstream*);
-       void flush_all_redirects ();
-       void locate (jack_nframes_t, bool with_roll, bool with_flush, bool with_loop=false);
-       void start_locate (jack_nframes_t, bool with_roll, bool with_flush, bool with_loop=false);
-       void force_locate (jack_nframes_t frame, bool with_roll = false);
+       void flush_all_inserts ();
+       void locate (nframes_t, bool with_roll, bool with_flush, bool with_loop=false);
+       void start_locate (nframes_t, bool with_roll, bool with_flush, bool with_loop=false);
+       void force_locate (nframes_t frame, bool with_roll = false);
        void set_diskstream_speed (Diskstream*, float speed);
        void set_transport_speed (float speed, bool abort = false);
        void stop_transport (bool abort = false);
@@ -1533,9 +1386,8 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
        void realtime_stop (bool abort);
        void non_realtime_start_scrub ();
        void non_realtime_set_speed ();
-       void non_realtime_stop (bool abort);
-       void non_realtime_overwrite ();
-       void non_realtime_buffer_fill ();
+       void non_realtime_stop (bool abort, int entry_request_count, bool& finished);
+       void non_realtime_overwrite (int entry_request_count, bool& finished);
        void butler_transport_work ();
        void post_transport ();
        void engine_halted ();
@@ -1573,13 +1425,11 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
 
        /* mixer stuff */
 
-       bool      _solo_latched;
-       SoloModel _solo_model;
        bool       solo_update_disabled;
        bool       currently_soloing;
        
        void route_mute_changed (void *src);
-       void route_solo_changed (void *src, boost::shared_ptr<Route>);
+       void route_solo_changed (void *src, boost::weak_ptr<Route>);
        void catch_up_on_solo ();
        void update_route_solo_state ();
        void modify_solo_mute (bool, bool);
@@ -1588,22 +1438,21 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
        /* REGION MANAGEMENT */
 
        mutable Glib::Mutex region_lock;
-       typedef map<PBD::ID,boost::shared_ptr<AudioRegion> > AudioRegionList;
-       AudioRegionList audio_regions;
+       typedef map<PBD::ID,boost::shared_ptr<Region> > RegionList;
+       RegionList regions;
        
-       void region_renamed (boost::shared_ptr<Region>);
-       void region_changed (Change, boost::shared_ptr<Region>);
        void add_region (boost::shared_ptr<Region>);
-       void remove_region (boost::shared_ptr<Region>);
+       void region_changed (Change, boost::weak_ptr<Region>);
+       void remove_region (boost::weak_ptr<Region>);
 
        int load_regions (const XMLNode& node);
 
        /* SOURCES */
        
-       mutable Glib::Mutex audio_source_lock;
-       typedef std::map<PBD::ID,boost::shared_ptr<AudioSource> > AudioSourceList;
+       mutable Glib::Mutex source_lock;
+       typedef std::map<PBD::ID,boost::shared_ptr<Source> > SourceMap;
 
-       AudioSourceList audio_sources;
+       SourceMap sources;
 
        int load_sources (const XMLNode& node);
        XMLNode& get_sources_as_xml ();
@@ -1613,19 +1462,19 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
        /* PLAYLISTS */
        
        mutable Glib::Mutex playlist_lock;
-       typedef set<Playlist *> PlaylistList;
+       typedef set<boost::shared_ptr<Playlist> > PlaylistList;
        PlaylistList playlists;
        PlaylistList unused_playlists;
 
        int load_playlists (const XMLNode&);
        int load_unused_playlists (const XMLNode&);
-       void remove_playlist (Playlist *);
-       void track_playlist (Playlist *, bool);
+       void remove_playlist (boost::weak_ptr<Playlist>);
+       void track_playlist (bool, boost::weak_ptr<Playlist>);
 
-       Playlist *playlist_factory (string name);
-       Playlist *XMLPlaylistFactory (const XMLNode&);
+       boost::shared_ptr<Playlist> playlist_factory (string name);
+       boost::shared_ptr<Playlist> XMLPlaylistFactory (const XMLNode&);
 
-       void playlist_length_changed (Playlist *);
+       void playlist_length_changed ();
        void diskstream_playlist_changed (boost::shared_ptr<Diskstream>);
 
        /* NAMED SELECTIONS */
@@ -1639,9 +1488,9 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
        NamedSelection *named_selection_factory (string name);
        NamedSelection *XMLNamedSelectionFactory (const XMLNode&);
 
-        /* CURVES and AUTOMATION LISTS */
-        std::map<PBD::ID, Curve*> curves;
-        std::map<PBD::ID, AutomationList*> automation_lists;
+       /* CURVES and AUTOMATION LISTS */
+       std::map<PBD::ID, Curve*> curves;
+       std::map<PBD::ID, AutomationList*> automation_lists;
 
        /* DEFAULT FADE CURVES */
 
@@ -1659,18 +1508,21 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
 
        /* FLATTEN */
 
-       int flatten_one_track (AudioTrack&, jack_nframes_t start, jack_nframes_t cnt);
+       int flatten_one_track (AudioTrack&, nframes_t start, nframes_t cnt);
 
        /* INSERT AND SEND MANAGEMENT */
        
        list<PortInsert *>   _port_inserts;
        list<PluginInsert *> _plugin_inserts;
        list<Send *>         _sends;
+       boost::dynamic_bitset<uint32_t>  send_bitset;
+       boost::dynamic_bitset<uint32_t>  insert_bitset;
        uint32_t          send_cnt;
        uint32_t          insert_cnt;
 
-       void add_redirect (Redirect *);
-       void remove_redirect (Redirect *);
+
+       void add_processor (Processor *);
+       void remove_processor (Processor *);
 
        /* S/W RAID */
 
@@ -1695,14 +1547,9 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
        vector<space_and_path>::iterator last_rr_session_dir;
        uint32_t _total_free_4k_blocks;
        Glib::Mutex space_lock;
-
-       static const char* sound_dir_name;
-       static const char* tape_dir_name;
-       static const char* dead_sound_dir_name;
-       static const char* peak_dir_name;
-
-       string discover_best_sound_dir (bool destructive = false);
-       int ensure_sound_dir (string, string&);
+       
+       string old_sound_dir (bool with_path = true) const;
+       string get_best_session_directory_for_new_source ();
        void refresh_disk_space ();
 
        mutable gint _playback_load;
@@ -1710,18 +1557,16 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
        mutable gint _playback_load_min;
        mutable gint _capture_load_min;
 
-       /* I/O Connections */
-
-       typedef list<Connection *> ConnectionList;
-       mutable Glib::Mutex connection_lock;
-       ConnectionList _connections;
-       int load_connections (const XMLNode&);
+       /* I/O bundles */
 
-       int set_slave_source (SlaveSource, jack_nframes_t);
+       typedef list<Bundle *> BundleList;
+       mutable Glib::Mutex bundle_lock;
+       BundleList  _bundles;
+       int load_bundles (const XMLNode&);
 
        void reverse_diskstream_buffers ();
 
-       UndoHistory history;
+       UndoHistory _history;
        UndoTransaction* current_trans;
 
        GlobalRouteBooleanState get_global_route_boolean (bool (Route::*method)(void) const);
@@ -1734,8 +1579,9 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
        void set_global_solo (GlobalRouteBooleanState s, void *src);
        void set_global_record_enable (GlobalRouteBooleanState s, void *src);
 
-       void jack_timebase_callback (jack_transport_state_t, jack_nframes_t, jack_position_t*, int);
+       void jack_timebase_callback (jack_transport_state_t, nframes_t, jack_position_t*, int);
        int  jack_sync_callback (jack_transport_state_t, jack_position_t*);
+       void reset_jack_connection (jack_client_t* jack);
        void record_enable_change_all (bool yn);
 
        XMLNode& state(bool);
@@ -1743,12 +1589,12 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
        /* click track */
 
        struct Click {
-           jack_nframes_t start;
-           jack_nframes_t duration;
-           jack_nframes_t offset;
+           nframes_t start;
+           nframes_t duration;
+           nframes_t offset;
            const Sample *data;
 
-           Click (jack_nframes_t s, jack_nframes_t d, const Sample *b) 
+           Click (nframes_t s, nframes_t d, const Sample *b) 
                    : start (s), duration (d), data (b) { offset = 0; }
            
            void *operator new(size_t ignored) {
@@ -1770,25 +1616,22 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
        boost::shared_ptr<IO> _click_io;
        Sample*         click_data;
        Sample*         click_emphasis_data;
-       jack_nframes_t  click_length;
-       jack_nframes_t  click_emphasis_length;
+       nframes_t  click_length;
+       nframes_t  click_emphasis_length;
        mutable Glib::RWLock click_lock;
 
        static const Sample         default_click[];
-       static const jack_nframes_t default_click_length;
+       static const nframes_t default_click_length;
        static const Sample         default_click_emphasis[];
-       static const jack_nframes_t default_click_emphasis_length;
+       static const nframes_t default_click_emphasis_length;
 
        Click *get_click();
        void   setup_click_sounds (int which);
        void   clear_clicks ();
-       void   click (jack_nframes_t start, jack_nframes_t nframes, jack_nframes_t offset);
+       void   click (nframes_t start, nframes_t nframes, nframes_t offset);
 
        vector<Route*> master_outs;
        
-       EditMode _edit_mode;
-       EditMode pending_edit_mode;
-
        /* range playback */
 
        list<AudioRange> current_audio_range;
@@ -1802,14 +1645,13 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
        boost::shared_ptr<IO> _master_out;
        boost::shared_ptr<IO> _control_out;
 
-       AutoConnectOption input_auto_connect;
-       AutoConnectOption output_auto_connect;
-
        gain_t* _gain_automation_buffer;
        pan_t** _pan_automation_buffer;
-       void allocate_pan_automation_buffers (jack_nframes_t nframes, uint32_t howmany, bool force);
+       void allocate_pan_automation_buffers (nframes_t nframes, uint32_t howmany, bool force);
        uint32_t _npan_buffers;
 
+       nframes_t _automation_interval;
+
        /* VST support */
 
        long _vst_callback (VSTPlugin*,
@@ -1819,7 +1661,7 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
                            void* ptr,
                            float opt);
 
-       /* number of hardware audio ports we're using,
+       /* number of hardware ports we're using,
           based on max (requested,available)
        */
 
@@ -1834,14 +1676,18 @@ class Session : public sigc::trackable, public PBD::StatefulDestructible
        LayerModel layer_model;
        CrossfadeModel xfade_model;
 
-       typedef std::list<PBD::Controllable*> Controllables;
+       typedef std::set<boost::shared_ptr<PBD::Controllable> > Controllables;
        Glib::Mutex controllables_lock;
        Controllables controllables;
 
-       void add_controllable (PBD::Controllable*);
-       void remove_controllable (PBD::Controllable*);
+       void reset_native_file_format();
+       bool first_file_data_format_reset;
+       bool first_file_header_format_reset;
+
+       void config_changed (const char*);
 
-       void handle_configuration_change (const char*);
+       XMLNode& get_control_protocol_state ();
+       
 };
 
 } // namespace ARDOUR