substantive changes to the logic and safety for naming of (audio/MIDI) sources, espec...
[ardour.git] / libs / ardour / ardour / session.h
index 518a99d961bd00b48d988e1b0616465ad6617aa1..b93f932cc08821973737270bdf0d061aa89001c7 100644 (file)
@@ -34,7 +34,9 @@
 #include <boost/weak_ptr.hpp>
 #include <boost/utility.hpp>
 
-#include <glibmm/thread.h>
+#include <glibmm/threads.h>
+
+#include <ltc.h>
 
 #include "pbd/error.h"
 #include "pbd/event_loop.h"
@@ -64,6 +66,7 @@
 #include <jack/session.h>
 #endif
 
+
 class XMLTree;
 class XMLNode;
 struct _AEffect;
@@ -108,6 +111,8 @@ class IOProcessor;
 class ImportStatus;
 class MidiClockTicker;
 class MidiControlUI;
+class MidiPortManager;
+class MidiPort;
 class MidiRegion;
 class MidiSource;
 class MidiTrack;
@@ -157,6 +162,8 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi
 
        virtual ~Session ();
 
+        static int get_info_from_path (const std::string& xmlpath, float& sample_rate, SampleFormat& data_format);
+
        std::string path() const { return _path; }
        std::string name() const { return _name; }
        std::string snap_name() const { return _current_snapshot_name; }
@@ -187,18 +194,17 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi
 
        std::string peak_path (std::string) const;
 
-       std::string change_source_path_by_name (std::string oldpath, std::string oldname, std::string newname, bool destructive);
-
        std::string peak_path_from_audio_path (std::string) const;
-       std::string new_audio_source_name (const std::string&, uint32_t nchans, uint32_t chan, bool destructive);
-       std::string new_midi_source_name (const std::string&);
-       std::string new_source_path_from_name (DataType type, const std::string&);
-       RouteList new_route_from_template (uint32_t how_many, const std::string& template_path);
+       std::string new_audio_source_path (const std::string&, uint32_t nchans, uint32_t chan, bool destructive, bool take_required);
+       std::string new_midi_source_path (const std::string&);
+        RouteList new_route_from_template (uint32_t how_many, const std::string& template_path, const std::string& name);
+       std::vector<std::string> get_paths_for_new_sources (bool allow_replacing, const std::string& import_file_path, uint32_t channels);
 
        void process (pframes_t nframes);
 
        BufferSet& get_silent_buffers (ChanCount count = ChanCount::ZERO);
-       BufferSet& get_scratch_buffers (ChanCount count = ChanCount::ZERO);
+       BufferSet& get_scratch_buffers (ChanCount count = ChanCount::ZERO, bool silence = true );
+       BufferSet& get_route_buffers (ChanCount count = ChanCount::ZERO, bool silence = true);
        BufferSet& get_mix_buffers (ChanCount count = ChanCount::ZERO);
 
        bool have_rec_enabled_track () const;
@@ -234,7 +240,9 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi
                bool operator() (boost::shared_ptr<Route>, boost::shared_ptr<Route> b);
        };
 
-        void sync_order_keys (RouteSortOrderKey);
+       void set_order_hint (uint32_t order_hint) {_order_hint = order_hint;};
+        void notify_remote_id_change ();
+        void sync_order_keys ();
 
        template<class T> void foreach_route (T *obj, void (T::*func)(Route&));
        template<class T> void foreach_route (T *obj, void (T::*func)(boost::shared_ptr<Route>));
@@ -246,7 +254,7 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi
        boost::shared_ptr<Route> route_by_id (PBD::ID);
        boost::shared_ptr<Route> route_by_remote_id (uint32_t id);
        boost::shared_ptr<Track> track_by_diskstream_id (PBD::ID);
-       void routes_using_input_from (const std::string& str, RouteList& rl);
+        void routes_using_input_from (const std::string& str, RouteList& rl);
 
        bool route_name_unique (std::string) const;
        bool route_name_internal (std::string) const;
@@ -356,7 +364,7 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi
                return mtc_timecode_bits;   /* encoding of SMTPE type for MTC */
        }
 
-       float timecode_frames_per_second() const;
+       double timecode_frames_per_second() const;
        bool timecode_drop_frames() const;
 
        /* Locations */
@@ -377,9 +385,6 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi
        framecnt_t worst_track_latency ()  const { return _worst_track_latency; }
        framecnt_t worst_playback_latency () const { return _worst_output_latency + _worst_track_latency; }
 
-#ifdef HAVE_JACK_SESSION
-       void jack_session_event (jack_session_event_t* event);
-#endif
        int save_state (std::string snapshot_name, bool pending = false, bool switch_to_snapshot = false);
        int restore_state (std::string snapshot_name);
        int save_template (std::string template_name);
@@ -389,9 +394,12 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi
        void rename_state (std::string old_name, std::string new_name);
        void remove_pending_capture_state ();
        int rename (const std::string&);
+       bool get_nsm_state () const { return _under_nsm_control; }
+       void set_nsm_state (bool state) { _under_nsm_control = state; }
 
        PBD::Signal1<void,std::string> StateSaved;
        PBD::Signal0<void> StateReady;
+       PBD::Signal0<void> SaveSession;
 
        std::vector<std::string*>* possible_states() const;
        static std::vector<std::string*>* possible_states (std::string path);
@@ -494,7 +502,7 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi
        void timecode_time_subframes (framepos_t when, Timecode::Time&);
 
        void timecode_duration (framecnt_t, Timecode::Time&) const;
-       void timecode_duration_string (char *, framecnt_t) const;
+        void timecode_duration_string (char *, size_t len, framecnt_t) const;
 
        framecnt_t convert_to_frames (AnyTime const & position);
        framecnt_t any_duration_to_frames (framepos_t position, AnyTime const & duration);
@@ -502,9 +510,8 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi
        static PBD::Signal1<void, framepos_t> StartTimeChanged;
        static PBD::Signal1<void, framepos_t> EndTimeChanged;
 
-       std::vector<SyncSource> get_available_sync_options() const;
        void   request_sync_source (Slave*);
-       bool   synced_to_jack() const { return config.get_external_sync() && config.get_sync_source() == JACK; }
+       bool   synced_to_engine() const { return config.get_external_sync() && Config->get_sync_source() == Engine; }
 
        double transport_speed() const { return _transport_speed; }
        bool   transport_stopped() const { return _transport_speed == 0.0f; }
@@ -519,8 +526,6 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi
 
        boost::shared_ptr<Region> find_whole_file_parent (boost::shared_ptr<Region const>) const;
 
-       std::string path_from_region_name (DataType type, std::string name, std::string identifier);
-
        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);
@@ -550,6 +555,11 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi
 
        int remove_last_capture ();
 
+        /** handlers should return 0 for "everything OK", and any other value for
+        * "cannot setup audioengine".
+        */
+        static PBD::Signal1<int,uint32_t> AudioEngineSetupRequired;
+
        /** handlers should return -1 for "stop cleanup",
            0 for "yes, delete this playlist",
            1 for "no, don't delete this playlist".
@@ -568,11 +578,12 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi
        boost::shared_ptr<AudioFileSource> create_audio_source_for_session (
                size_t, std::string const &, uint32_t, bool destructive);
 
-       boost::shared_ptr<MidiSource> create_midi_source_for_session (
-               Track*, std::string const &);
+       boost::shared_ptr<MidiSource> create_midi_source_for_session (std::string const &);
+       boost::shared_ptr<MidiSource> create_midi_source_by_stealing_name (boost::shared_ptr<Track>);
 
        boost::shared_ptr<Source> source_by_id (const PBD::ID&);
-       boost::shared_ptr<Source> source_by_path_and_channel (const std::string&, uint16_t);
+       boost::shared_ptr<AudioFileSource> audio_source_by_path_and_channel (const std::string&, uint16_t) const;
+       boost::shared_ptr<MidiSource> midi_source_by_path (const std::string&) const;
        uint32_t count_sources_by_origin (const std::string&);
 
        void add_playlist (boost::shared_ptr<Playlist>, bool unused = false);
@@ -612,8 +623,8 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi
        void set_listen (boost::shared_ptr<RouteList>, bool, SessionEvent::RTeventCallback after = rt_cleanup, bool group_override = false);
        void set_record_enabled (boost::shared_ptr<RouteList>, bool, SessionEvent::RTeventCallback after = rt_cleanup, bool group_override = false);
        void set_solo_isolated (boost::shared_ptr<RouteList>, bool, SessionEvent::RTeventCallback after = rt_cleanup, bool group_override = false);
-       void set_exclusive_input_active (boost::shared_ptr<Route> rt, bool others_on);
        void set_monitoring (boost::shared_ptr<RouteList>, MonitorChoice, SessionEvent::RTeventCallback after = rt_cleanup, bool group_override = false);
+        void set_exclusive_input_active (boost::shared_ptr<RouteList> rt, bool onoff, bool flip_others=false);
 
        PBD::Signal1<void,bool> SoloActive;
        PBD::Signal0<void> SoloChanged;
@@ -730,6 +741,8 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi
        void request_play_range (std::list<AudioRange>*, bool leave_rolling = false);
        bool get_play_range () const { return _play_range; }
 
+       void maybe_update_session_range (framepos_t, framepos_t);
+
        /* buffers for gain and pan */
 
        gain_t* gain_automation_buffer () const;
@@ -800,13 +813,13 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi
        };
 
        SlaveState slave_state() const { return _slave_state; }
+        Slave* slave() const { return _slave; }
 
        boost::shared_ptr<SessionPlaylists> playlists;
 
        void send_mmc_locate (framepos_t);
-       int send_full_time_code (framepos_t);
-
-       PBD::Signal0<void> RouteOrderKeyChanged;
+        void queue_full_time_code () { _send_timecode_update = true; }
+        void queue_song_position_pointer () { /* currently does nothing */ }
 
        bool step_editing() const { return (_step_editors > 0); }
 
@@ -844,6 +857,29 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi
        /** Emitted when the session wants Ardour to quit */
        static PBD::Signal0<void> Quit;
 
+        /** Emitted when Ardour is asked to load a session in an older session
+        * format, and makes a backup copy.
+        */
+        static PBD::Signal2<void,std::string,std::string> VersionMismatch;
+
+        boost::shared_ptr<Port> ltc_input_port() const;
+        boost::shared_ptr<Port> ltc_output_port() const;
+
+       boost::shared_ptr<IO> ltc_input_io() { return _ltc_input; }
+       boost::shared_ptr<IO> ltc_output_io() { return _ltc_output; }
+
+    MIDI::Port* midi_input_port () const;
+    MIDI::Port* midi_output_port () const;
+    MIDI::Port* mmc_output_port () const;
+    MIDI::Port* mmc_input_port () const;
+
+    boost::shared_ptr<MidiPort> midi_clock_output_port () const;
+    boost::shared_ptr<MidiPort> midi_clock_input_port () const;
+    boost::shared_ptr<MidiPort> mtc_output_port () const;
+    boost::shared_ptr<MidiPort> mtc_input_port () const;
+
+    MIDI::MachineControl& mmc() { return *_mmc; }
+
   protected:
        friend class AudioEngine;
        void set_block_size (pframes_t nframes);
@@ -888,8 +924,6 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi
        Slave*                  _slave;
        bool                    _silent;
 
-       void maybe_update_session_range (framepos_t, framepos_t);
-
        // varispeed playback
        double                  _transport_speed;
        double                  _default_transport_speed;
@@ -913,6 +947,7 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi
        uint32_t                _solo_isolated_cnt;
        bool                    _writable;
        bool                    _was_seamless;
+       bool                    _under_nsm_control;
 
        void initialize_latencies ();
        void set_worst_io_latencies ();
@@ -932,7 +967,7 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi
        void process_without_events (pframes_t);
        void process_with_events    (pframes_t);
        void process_audition       (pframes_t);
-       void process_export         (pframes_t);
+        int  process_export         (pframes_t);
        int  process_export_fw      (pframes_t);
 
        void block_processing() { g_atomic_int_set (&processing_prohibited, 1); }
@@ -965,6 +1000,7 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi
        framepos_t post_export_position;
 
        bool _exporting;
+       bool _export_started;
        bool _export_rolling;
 
        boost::shared_ptr<ExportHandler> export_handler;
@@ -973,6 +1009,7 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi
        int  pre_export ();
        int  stop_audio_export ();
        void finalize_audio_export ();
+       void finalize_export_internal (bool stop_freewheel);
        bool _pre_export_mmc_enabled;
 
        PBD::ScopedConnection export_freewheel_connection;
@@ -1026,7 +1063,6 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi
        boost::scoped_ptr<SessionDirectory> _session_dir;
 
        void hookup_io ();
-       void when_engine_running ();
        void graph_reordered ();
 
        /** current snapshot name, without the .ardour suffix */
@@ -1065,7 +1101,7 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi
                                PostTransportClearSubstate);
 
        gint _post_transport_work; /* accessed only atomic ops */
-       PostTransportWork post_transport_work() const        { return (PostTransportWork) g_atomic_int_get (&_post_transport_work); }
+        PostTransportWork post_transport_work() const        { return (PostTransportWork) g_atomic_int_get (const_cast<gint*>(&_post_transport_work)); }
        void set_post_transport_work (PostTransportWork ptw) { g_atomic_int_set (&_post_transport_work, (gint) ptw); }
        void add_post_transport_work (PostTransportWork ptw);
 
@@ -1092,8 +1128,10 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi
        void             auto_loop_changed (Location *);
        void             auto_loop_declick_range (Location *, framepos_t &, framepos_t &);
 
-       void first_stage_init (std::string path, std::string snapshot_name);
-       int  second_stage_init ();
+        int  ensure_engine (uint32_t desired_sample_rate);
+       void pre_engine_init (std::string path);
+       int  post_engine_init ();
+        int  immediately_post_engine ();
        void remove_empty_sounds ();
 
        void setup_midi_control ();
@@ -1160,7 +1198,7 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi
        MIDI::byte mtc_msg[16];
        MIDI::byte mtc_timecode_bits;   /* encoding of SMTPE type for MTC */
        MIDI::byte midi_msg[16];
-       framepos_t outbound_mtc_timecode_frame;
+       double outbound_mtc_timecode_frame;
        Timecode::Time transmitting_timecode_time;
        int next_quarter_frame_to_send;
 
@@ -1178,6 +1216,34 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi
 
        int send_midi_time_code_for_cycle (framepos_t, framepos_t, pframes_t nframes);
 
+       LTCEncoder*       ltc_encoder;
+       ltcsnd_sample_t*  ltc_enc_buf;
+
+       Timecode::TimecodeFormat ltc_enc_tcformat;
+       int32_t           ltc_buf_off;
+       int32_t           ltc_buf_len;
+
+       double            ltc_speed;
+       int32_t           ltc_enc_byte;
+       framepos_t        ltc_enc_pos;
+       double            ltc_enc_cnt;
+       framepos_t        ltc_enc_off;
+       bool              restarting;
+       framepos_t        ltc_prev_cycle;
+
+       framepos_t        ltc_timecode_offset;
+       bool              ltc_timecode_negative_offset;
+
+       LatencyRange      ltc_out_latency;
+
+       void ltc_tx_initialize();
+       void ltc_tx_cleanup();
+       void ltc_tx_reset();
+       void ltc_tx_resync_latency();
+       void ltc_tx_recalculate_position();
+       void ltc_tx_parse_offset();
+       void ltc_tx_send_time_code_for_cycle (framepos_t, framepos_t, double, double, pframes_t nframes);
+
        void reset_record_status ();
 
        int no_roll (pframes_t nframes);
@@ -1213,6 +1279,13 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi
        void engine_halted ();
        void xrun_recovery ();
 
+    /* These are synchronous and so can only be called from within the process
+     * cycle
+     */
+
+    int  send_full_time_code (framepos_t, pframes_t nframes);
+    void send_song_position_pointer (framepos_t);
+
        TempoMap    *_tempo_map;
        void          tempo_map_changed (const PBD::PropertyChange&);
 
@@ -1230,6 +1303,8 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi
        SerializedRCUManager<RouteList>  routes;
 
        void add_routes (RouteList&, bool input_auto_connect, bool output_auto_connect, bool save);
+        void add_routes_inner (RouteList&, bool input_auto_connect, bool output_auto_connect);
+        bool _adding_routes_in_progress;
        uint32_t destructive_index;
 
        boost::shared_ptr<Route> XMLRouteFactory (const XMLNode&, int);
@@ -1259,7 +1334,7 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi
 
        /* REGION MANAGEMENT */
 
-       mutable Glib::Mutex region_lock;
+       mutable Glib::Threads::Mutex region_lock;
 
        int load_regions (const XMLNode& node);
        int load_compounds (const XMLNode& node);
@@ -1270,7 +1345,7 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi
 
        /* SOURCES */
 
-       mutable Glib::Mutex source_lock;
+       mutable Glib::Threads::Mutex source_lock;
 
   public:
        typedef std::map<PBD::ID,boost::shared_ptr<Source> > SourceMap;
@@ -1278,9 +1353,6 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi
   private:
        SourceMap sources;
 
-  public:
-       SourceMap get_sources() { return sources; }
-
   private:
        int load_sources (const XMLNode& node);
        XMLNode& get_sources_as_xml ();
@@ -1355,11 +1427,11 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi
            could not report free space.
        */
        bool _total_free_4k_blocks_uncertain;
-       Glib::Mutex space_lock;
+       Glib::Threads::Mutex space_lock;
 
        bool no_questions_about_missing_files;
 
-       std::string get_best_session_directory_for_new_source ();
+       std::string get_best_session_directory_for_new_audio ();
 
        mutable gint _playback_load;
        mutable gint _capture_load;
@@ -1379,9 +1451,8 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi
         */
        std::list<GQuark> _current_trans_quarks;
 
-       void jack_timebase_callback (jack_transport_state_t, pframes_t, jack_position_t*, int);
-       int  jack_sync_callback (jack_transport_state_t, jack_position_t*);
-       void reset_jack_connection (jack_client_t* jack);
+        int  backend_sync_callback (TransportState, framepos_t);
+
        void process_rtop (SessionEvent*);
 
        void  update_latency (bool playback);
@@ -1398,7 +1469,7 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi
        Sample*                 click_emphasis_data;
        framecnt_t              click_length;
        framecnt_t              click_emphasis_length;
-       mutable Glib::RWLock    click_lock;
+       mutable Glib::Threads::RWLock    click_lock;
 
        static const Sample     default_click[];
        static const framecnt_t default_click_length;
@@ -1440,18 +1511,11 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi
                float opt
                );
 
-       /* number of hardware ports we're using,
-          based on max (requested,available)
-       */
-
-       ChanCount n_physical_outputs;
-       ChanCount n_physical_inputs;
-
        int find_all_sources (std::string path, std::set<std::string>& result);
        int find_all_sources_across_snapshots (std::set<std::string>& result, bool exclude_this_snapshot);
 
        typedef std::set<boost::shared_ptr<PBD::Controllable> > Controllables;
-       Glib::Mutex controllables_lock;
+       Glib::Threads::Mutex controllables_lock;
        Controllables controllables;
 
        boost::shared_ptr<PBD::Controllable> _solo_cut_control;
@@ -1503,8 +1567,6 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi
 
        void setup_midi_machine_control ();
 
-       void route_order_key_changed ();
-
        void step_edit_status_change (bool);
        uint32_t _step_editors;
 
@@ -1528,9 +1590,27 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi
        GraphEdges _current_route_graph;
 
        uint32_t next_control_id () const;
+       uint32_t _order_hint;
        bool ignore_route_processor_changes;
 
        MidiClockTicker* midi_clock;
+
+        boost::shared_ptr<IO>   _ltc_input;
+        boost::shared_ptr<IO>   _ltc_output;
+
+        void reconnect_ltc_input ();
+        void reconnect_ltc_output ();
+
+    /* persistent, non-track related MIDI ports */
+    MidiPortManager* _midi_ports;
+    MIDI::MachineControl* _mmc;
+
+    void setup_ltc ();
+    void setup_click ();
+    void setup_click_state (const XMLNode*);
+    void setup_bundles ();
+
+    static int get_session_info_from_path (XMLTree& state_tree, const std::string& xmlpath);
 };
 
 } // namespace ARDOUR