Large nasty commit in the form of a 5000 line patch chock-full of completely
[ardour.git] / libs / ardour / ardour / session.h
index 0d968fcff109fb665a1d6647d0fd7bdc0347ed50..6284e949005ab5ebbdb6bcb7ad5f1e5962a2d065 100644 (file)
@@ -31,9 +31,9 @@
 
 #include <sndfile.h>
 
+#include <glibmm/thread.h>
+
 #include <pbd/error.h>
-#include <pbd/atomic.h>
-#include <pbd/lockmonitor.h>
 #include <pbd/undo.h>
 #include <pbd/pool.h>
 
@@ -47,6 +47,8 @@
 #include <ardour/gain.h>
 #include <ardour/io.h>
 
+#include <ardour/smpte.h>
+
 class XMLTree;
 class XMLNode;
 class AEffect;
@@ -60,11 +62,14 @@ namespace ARDOUR {
 class Port;
 class AudioEngine;
 class Slave;
-class DiskStream;      
+class Diskstream;
 class Route;
 class AuxInput;
 class Source;
-class FileSource;
+class AudioSource;
+
+class AudioDiskstream;
+class AudioFileSource;
 class Auditioner;
 class Insert;
 class Send;
@@ -76,11 +81,18 @@ class TempoMap;
 class AudioTrack;
 class NamedSelection;
 class AudioRegion;
+
 class Region;
 class Playlist;
 class VSTPlugin;
 class ControlProtocolManager;
 
+//class MidiDiskstream;
+class MidiSource;
+class MidiTrack;
+class MidiRegion;
+class SMFSource;
+
 struct AudioExportSpecification;
 struct RouteGroup;
 
@@ -261,30 +273,35 @@ class Session : public sigc::trackable, public Stateful
        vector<Sample*>& get_silent_buffers (uint32_t howmany);
        vector<Sample*>& get_send_buffers () { return _send_buffers; }
 
-       DiskStream    *diskstream_by_id (id_t id);
-       DiskStream    *diskstream_by_name (string name);
+       Diskstream    *diskstream_by_id (id_t id);
+       Diskstream    *diskstream_by_name (string name);
 
        bool have_captured() const { return _have_captured; }
 
        void refill_all_diskstream_buffers ();
        uint32_t diskstream_buffer_size() const { return dstream_buffer_size; }
-       uint32_t get_next_diskstream_id() const { return n_diskstreams(); }
-       uint32_t n_diskstreams() const;
        
-       typedef list<DiskStream *> DiskStreamList;
+       /* XXX fix required here when we get new diskstream types *, but
+          not sure of the direction to take this in until then.
+       */
+
+       uint32_t get_next_diskstream_id() const { return n_audio_diskstreams(); }
+       uint32_t n_audio_diskstreams() const;
+       
+       typedef list<AudioDiskstream *> AudioDiskstreamList;
 
-       Session::DiskStreamList disk_streams() const {
-               RWLockMonitor lm (diskstream_lock, false, __LINE__, __FILE__);
-               return diskstreams; /* XXX yes, force a copy */
+       Session::AudioDiskstreamList audio_disk_streams() const {
+               Glib::RWLock::ReaderLock lm (diskstream_lock);
+               return audio_diskstreams; /* XXX yes, force a copy */
        }
 
-       void foreach_diskstream (void (DiskStream::*func)(void));
-       template<class T> void foreach_diskstream (T *obj, void (T::*func)(DiskStream&));
+       void foreach_audio_diskstream (void (AudioDiskstream::*func)(void));
+       template<class T> void foreach_audio_diskstream (T *obj, void (T::*func)(AudioDiskstream&));
 
        typedef list<Route *> RouteList;
 
        RouteList get_routes() const {
-               RWLockMonitor rlock (route_lock, false, __LINE__, __FILE__);
+               Glib::RWLock::ReaderLock rlock (route_lock);
                return routes; /* XXX yes, force a copy */
        }
 
@@ -310,7 +327,7 @@ class Session : public sigc::trackable, public Stateful
        }
 
        RecordState record_status() const {
-               return (RecordState) atomic_read (&_record_status);
+               return (RecordState) g_atomic_int_get (&_record_status);
        }
 
        bool actively_recording () {
@@ -344,7 +361,7 @@ class Session : public sigc::trackable, public Stateful
        sigc::signal<void> HaltOnXrun;
 
        sigc::signal<void,Route*> RouteAdded;
-       sigc::signal<void,DiskStream*> DiskStreamAdded;
+       sigc::signal<void,Diskstream*> DiskstreamAdded;
 
        void request_roll ();
        void request_bounded_roll (jack_nframes_t start, jack_nframes_t end);
@@ -356,15 +373,15 @@ class Session : public sigc::trackable, public Stateful
        void goto_start () { request_locate (start_location->start(), false); }
        void use_rf_shuttle_speed ();
        void request_transport_speed (float speed);
-       void request_overwrite_buffer (DiskStream*);
-       void request_diskstream_speed (DiskStream&, float speed);
+       void request_overwrite_buffer (Diskstream*);
+       void request_diskstream_speed (Diskstream&, float speed);
        void request_input_change_handling ();
 
        bool locate_pending() const { return static_cast<bool>(post_transport_work&PostTransportLocate); }
        bool transport_locked () const;
 
        int wipe ();
-       int wipe_diskstream (DiskStream *);
+       //int wipe_diskstream (AudioDiskstream *);
 
        int remove_region_from_region_list (Region&);
 
@@ -537,10 +554,11 @@ class Session : public sigc::trackable, public Stateful
 
        /* fundamental operations. duh. */
 
-
        AudioTrack *new_audio_track (int input_channels, int output_channels, TrackMode mode = Normal);
-
        Route *new_audio_route (int input_channels, int output_channels);
+       
+       MidiTrack *new_midi_track (TrackMode mode = Normal);
+       Route *new_midi_route ();
 
        void   remove_route (Route&);
        void   resort_routes (void *src);
@@ -584,25 +602,13 @@ class Session : public sigc::trackable, public Stateful
        int  set_smpte_type (float fps, bool drop_frames);
 
        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 smpte_time (SMPTE::Time &);
+       void smpte_time (jack_nframes_t when, SMPTE::Time&);
+       void smpte_time_subframes (jack_nframes_t when, SMPTE::Time&);
 
-       ARDOUR::smpte_wrap_t smpte_increment( SMPTE_Time& smpte ) const;
-       ARDOUR::smpte_wrap_t smpte_decrement( SMPTE_Time& smpte ) const;
-       ARDOUR::smpte_wrap_t smpte_increment_subframes( SMPTE_Time& smpte ) const;
-       ARDOUR::smpte_wrap_t smpte_decrement_subframes( SMPTE_Time& smpte ) const;
-       ARDOUR::smpte_wrap_t smpte_increment_seconds( SMPTE_Time& smpte ) const;
-       ARDOUR::smpte_wrap_t smpte_increment_minutes( SMPTE_Time& smpte ) const;
-       ARDOUR::smpte_wrap_t smpte_increment_hours( SMPTE_Time& smpte ) const;
-       void smpte_frames_floor( SMPTE_Time& smpte ) const;
-       void smpte_seconds_floor( SMPTE_Time& smpte ) const;
-       void smpte_minutes_floor( SMPTE_Time& smpte ) const;
-       void smpte_hours_floor( SMPTE_Time& smpte ) const;
-       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 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_duration (jack_nframes_t, SMPTE_Time&) const;
+       void smpte_duration (jack_nframes_t, SMPTE::Time&) const;
        void smpte_duration_string (char *, jack_nframes_t) const;
 
        void           set_smpte_offset (jack_nframes_t);
@@ -612,7 +618,7 @@ class Session : public sigc::trackable, public Stateful
 
        jack_nframes_t convert_to_frames_at (jack_nframes_t position, AnyTime&);
 
-       sigc::signal<void> SMPTEOffsetChanged;
+       static sigc::signal<void> SMPTEOffsetChanged;
        sigc::signal<void> SMPTETypeChanged;
 
        void        request_slave_source (SlaveSource, jack_nframes_t pos = 0);
@@ -668,8 +674,9 @@ class Session : public sigc::trackable, public Stateful
        int start_audio_export (ARDOUR::AudioExportSpecification&);
        int stop_audio_export (ARDOUR::AudioExportSpecification&);
        
-       void add_source (Source *);
-       int  remove_file_source (FileSource&);
+       void add_audio_source (AudioSource *);
+       void remove_source (Source *);
+       int  cleanup_audio_file_source (AudioFileSource&);
 
        struct cleanup_report {
            vector<string> paths;
@@ -701,7 +708,8 @@ class Session : public sigc::trackable, public Stateful
        sigc::signal<void,Source *> SourceAdded;
        sigc::signal<void,Source *> SourceRemoved;
 
-       FileSource *create_file_source (ARDOUR::DiskStream&, int32_t chan, bool destructive);
+       AudioFileSource *create_audio_source_for_session (ARDOUR::AudioDiskstream&, uint32_t which_channel, bool destructive);
+
        Source *get_source (ARDOUR::id_t);
 
        /* playlist management */
@@ -745,8 +753,8 @@ class Session : public sigc::trackable, public Stateful
 
        /* flattening stuff */
 
-       int write_one_track (AudioTrack&, jack_nframes_t start, jack_nframes_t cnt, bool overwrite, vector<Source*>&,
-                            InterThreadInfo& wot);
+       int write_one_audio_track (AudioTrack&, jack_nframes_t start, jack_nframes_t cnt, bool overwrite, vector<AudioSource*>&,
+                                  InterThreadInfo& wot);
        int freeze (InterThreadInfo&);
 
        /* session-wide solo/mute/rec-enable */
@@ -972,7 +980,7 @@ class Session : public sigc::trackable, public Stateful
        void set_frame_rate (jack_nframes_t nframes);
 
   protected:
-       friend class DiskStream;
+       friend class AudioDiskstream;
        void stop_butler ();
        void wait_till_butler_finished();
 
@@ -1004,12 +1012,12 @@ class Session : public sigc::trackable, public Stateful
        typedef void (Session::*process_function_type)(jack_nframes_t);
 
        AudioEngine            &_engine;
-       atomic_t                 processing_prohibited;
+       mutable gint            processing_prohibited;
        process_function_type    process_function;
        process_function_type    last_process_function;
        jack_nframes_t          _current_frame_rate;
        int                      transport_sub_state;
-       atomic_t                _record_status;
+       mutable gint           _record_status;
        jack_nframes_t          _transport_frame;
        Location*                end_location;
        Location*                start_location;
@@ -1166,14 +1174,14 @@ class Session : public sigc::trackable, public Stateful
        bool              pending_abort;
        bool              pending_auto_loop;
        
-       Sample*              butler_mixdown_buffer;
-       float*               butler_gain_buffer;
-       pthread_t            butler_thread;
-       PBD::NonBlockingLock butler_request_lock;
-       pthread_cond_t       butler_paused;
-       bool                 butler_should_run;
-       atomic_t             butler_should_do_transport_work;
-       int                  butler_request_pipe[2];
+       Sample*           butler_mixdown_buffer;
+       float*            butler_gain_buffer;
+       pthread_t         butler_thread;
+       Glib::Mutex       butler_request_lock;
+        Glib::Cond        butler_paused;
+       bool              butler_should_run;
+       mutable gint      butler_should_do_transport_work;
+       int               butler_request_pipe[2];
        
        struct ButlerRequest {
            enum Type {
@@ -1271,7 +1279,7 @@ class Session : public sigc::trackable, public Stateful
        void remove_empty_sounds ();
 
        void setup_midi_control ();
-       int  midi_read (MIDI::Port *);
+       //int  midi_read (MIDI::Port *);
 
        void enable_record ();
        
@@ -1297,13 +1305,13 @@ class Session : public sigc::trackable, public Stateful
        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_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&);
@@ -1338,7 +1346,7 @@ class Session : public sigc::trackable, public Stateful
        MIDI::byte mtc_smpte_bits;   /* encoding of SMTPE type for MTC */
        MIDI::byte midi_msg[16];
        jack_nframes_t  outbound_mtc_smpte_frame;
-       SMPTE_Time transmitting_smpte_time;
+       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 */
@@ -1347,22 +1355,18 @@ class Session : public sigc::trackable, public Stateful
        jack_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;
+       /* 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;
+       
+       bool _send_smpte_update; ///< Flag to send a full frame (SMPTE) MTC message this cycle
 
-       int send_full_time_code ();
-       int send_midi_time_code ();
+       int send_full_time_code(jack_nframes_t nframes);
+       int send_midi_time_code_for_cycle(jack_nframes_t nframes);
 
-       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);
+       //void send_mmc_in_another_thread (MIDI::MachineControl::Command, jack_nframes_t frame = 0);
 
        jack_nframes_t adjust_apparent_position (jack_nframes_t frames);
        
@@ -1417,29 +1421,30 @@ class Session : public sigc::trackable, public Stateful
            static MultiAllocSingleReleasePool pool;
        };
 
-       PBD::Lock       midi_lock;
-       pthread_t       midi_thread;
-       int             midi_request_pipe[2];
-       atomic_t        butler_active;
-       RingBuffer<MIDIRequest*> midi_requests;
+       mutable  gint   butler_active;
+       
+       //PBD::Lock       midi_lock;
+       //pthread_t       midi_thread;
+       //int             midi_request_pipe[2];
 
-       int           start_midi_thread ();
+       //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          midi_thread_work ();*/
        void          change_midi_ports ();
        int           use_config_midi_ports ();
 
        bool waiting_to_start;
 
        void set_auto_loop (bool yn);
-       void overwrite_some_buffers (DiskStream*);
+       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 set_diskstream_speed (DiskStream*, float speed);
+       void set_diskstream_speed (Diskstream*, float speed);
        void set_transport_speed (float speed, bool abort = false);
        void stop_transport (bool abort = false);
        void start_transport ();
@@ -1470,16 +1475,16 @@ class Session : public sigc::trackable, public Stateful
 
        /* disk-streams */
 
-       DiskStreamList  diskstreams; 
-       mutable PBD::NonBlockingRWLock diskstream_lock;
+       AudioDiskstreamList  audio_diskstreams; 
+       mutable Glib::RWLock diskstream_lock;
        uint32_t dstream_buffer_size;
-       void add_diskstream (DiskStream*);
+       void add_diskstream (Diskstream*);
        int  load_diskstreams (const XMLNode&);
 
        /* routes stuff */
 
        RouteList       routes;
-       mutable PBD::NonBlockingRWLock route_lock;
+       mutable Glib::RWLock route_lock;
        void   add_route (Route*);
        uint32_t destructive_index;
 
@@ -1502,7 +1507,7 @@ class Session : public sigc::trackable, public Stateful
 
        /* REGION MANAGEMENT */
 
-       mutable PBD::Lock region_lock;
+       mutable Glib::Mutex region_lock;
        typedef map<ARDOUR::id_t,AudioRegion *> AudioRegionList;
        AudioRegionList audio_regions;
        
@@ -1515,21 +1520,19 @@ class Session : public sigc::trackable, public Stateful
 
        /* SOURCES */
        
-       mutable PBD::Lock source_lock;
-       typedef std::map<id_t, Source *>    SourceList;
+       mutable Glib::Mutex audio_source_lock;
+       typedef std::map<id_t, AudioSource *>    AudioSourceList;
 
-       SourceList sources;
+       AudioSourceList audio_sources;
 
        int load_sources (const XMLNode& node);
        XMLNode& get_sources_as_xml ();
 
-       void remove_source (Source *);
-
        Source *XMLSourceFactory (const XMLNode&);
 
        /* PLAYLISTS */
        
-       mutable PBD::Lock playlist_lock;
+       mutable Glib::Mutex playlist_lock;
        typedef set<Playlist *> PlaylistList;
        PlaylistList playlists;
        PlaylistList unused_playlists;
@@ -1543,11 +1546,11 @@ class Session : public sigc::trackable, public Stateful
        Playlist *XMLPlaylistFactory (const XMLNode&);
 
        void playlist_length_changed (Playlist *);
-       void diskstream_playlist_changed (DiskStream *);
+       void diskstream_playlist_changed (Diskstream *);
 
        /* NAMED SELECTIONS */
 
-       mutable PBD::Lock named_selection_lock;
+       mutable Glib::Mutex named_selection_lock;
        typedef set<NamedSelection *> NamedSelectionList;
        NamedSelectionList named_selections;
 
@@ -1607,7 +1610,7 @@ class Session : public sigc::trackable, public Stateful
        vector<space_and_path> session_dirs;
        vector<space_and_path>::iterator last_rr_session_dir;
        uint32_t _total_free_4k_blocks;
-       PBD::Lock space_lock;
+       Glib::Mutex space_lock;
 
        static const char* sound_dir_name;
        static const char* tape_dir_name;
@@ -1618,15 +1621,15 @@ class Session : public sigc::trackable, public Stateful
        int ensure_sound_dir (string, string&);
        void refresh_disk_space ();
 
-       atomic_t _playback_load;
-       atomic_t _capture_load;
-       atomic_t _playback_load_min;
-       atomic_t _capture_load_min;
+       mutable gint _playback_load;
+       mutable gint _capture_load;
+       mutable gint _playback_load_min;
+       mutable gint _capture_load_min;
 
        /* I/O Connections */
 
        typedef list<Connection *> ConnectionList;
-       mutable PBD::Lock connection_lock;
+       mutable Glib::Mutex connection_lock;
        ConnectionList _connections;
        int load_connections (const XMLNode&);
 
@@ -1685,7 +1688,7 @@ class Session : public sigc::trackable, public Stateful
        Sample*         click_emphasis_data;
        jack_nframes_t  click_length;
        jack_nframes_t  click_emphasis_length;
-       mutable PBD::NonBlockingRWLock click_lock;
+       mutable Glib::RWLock click_lock;
 
        static const Sample         default_click[];
        static const jack_nframes_t default_click_length;