move _file_frame from DiskIOProcessor into DiskReader (only place where it is needed...
[ardour.git] / libs / ardour / ardour / disk_io.h
index 4819b632935eab16711b8a1b59d971fe53c7ec74..7f9232b426dfdc65fb40f8859359131c2cf56077 100644 (file)
 #include <string>
 #include <exception>
 
+#include "pbd/ringbufferNPT.h"
+#include "pbd/rcu.h"
+
+#include "ardour/interpolation.h"
 #include "ardour/processor.h"
 
 namespace ARDOUR {
 
-class Session;
+class AudioFileSource;
+class AudioPlaylist;
+class Location;
+class MidiPlaylist;
+class Playlist;
 class Route;
+class Route;
+class Session;
+
+template<typename T> class MidiRingBuffer;
 
 class LIBARDOUR_API DiskIOProcessor : public Processor
 {
   public:
+       enum Flag {
+               Recordable  = 0x1,
+               Hidden      = 0x2,
+               Destructive = 0x4,
+               NonLayered   = 0x8
+       };
+
        static const std::string state_node_name;
 
-       DiskIOProcessor(Session&, const std::string& name);
+       DiskIOProcessor (Session&, const std::string& name, Flag f);
 
-       void run (BufferSet& /*bufs*/, framepos_t /*start_frame*/, framepos_t /*end_frame*/, double speed, pframes_t /*nframes*/, bool /*result_required*/) {}
-       void silence (framecnt_t /*nframes*/, framepos_t /*start_frame*/) {}
+       void set_route (boost::shared_ptr<Route>);
+       void drop_route ();
 
-       bool configure_io (ChanCount in, ChanCount out);
-       bool can_support_io_configuration (const ChanCount& in, ChanCount& out) = 0;
-       ChanCount input_streams () const { return _configured_input; }
-       ChanCount output_streams() const { return _configured_output; }
+       static void set_buffering_parameters (BufferingPreset bp);
 
-       virtual void realtime_handle_transport_stopped () {}
-       virtual void realtime_locate () {}
+       int set_block_size (pframes_t);
+       bool configure_io (ChanCount in, ChanCount out);
+       bool can_support_io_configuration (const ChanCount& in, ChanCount& out);
 
-       /* note: derived classes should implement state(), NOT get_state(), to allow
-          us to merge C++ inheritance and XML lack-of-inheritance reasonably
-          smoothly.
+       /** @return A number between 0 and 1, where 0 indicates that the playback/capture buffer
+        *  is dry (ie the disk subsystem could not keep up) and 1 indicates that the
+        *  buffer is full.
         */
+       virtual float buffer_load() const = 0;
+
+       void set_flag (Flag f)   { _flags = Flag (_flags | f); }
+       void unset_flag (Flag f) { _flags = Flag (_flags & ~f); }
+
+       bool           hidden()      const { return _flags & Hidden; }
+       bool           recordable()  const { return _flags & Recordable; }
+       bool           non_layered()  const { return _flags & NonLayered; }
+
+       virtual void non_realtime_locate (framepos_t);
+
+       void non_realtime_speed_change ();
+       bool realtime_speed_change ();
+
+       virtual void punch_in()  {}
+       virtual void punch_out() {}
+
+       bool slaved() const      { return _slaved; }
+       void set_slaved(bool yn) { _slaved = yn; }
+
+       int set_loop (Location *loc);
+
+       PBD::Signal1<void,Location *> LoopSet;
+       PBD::Signal0<void>            SpeedChanged;
+       PBD::Signal0<void>            ReverseChanged;
 
-       virtual XMLNode& state (bool full);
-       XMLNode& get_state (void);
        int set_state (const XMLNode&, int version);
 
-       static framecnt_t disk_read_frames() { return disk_read_chunk_frames; }
-       static framecnt_t disk_write_frames() { return disk_write_chunk_frames; }
-       static void set_disk_read_chunk_frames (framecnt_t n) { disk_read_chunk_frames = n; }
-       static void set_disk_write_chunk_frames (framecnt_t n) { disk_write_chunk_frames = n; }
-       static framecnt_t default_disk_read_chunk_frames ();
-       static framecnt_t default_disk_write_chunk_frames ();
+       int add_channel (uint32_t how_many);
+       int remove_channel (uint32_t how_many);
 
-       static void set_buffering_parameters (BufferingPreset bp);
+       bool need_butler() const { return _need_butler; }
+
+       boost::shared_ptr<Playlist>      get_playlist (DataType dt) const { return _playlists[dt]; }
+       boost::shared_ptr<MidiPlaylist>  midi_playlist() const;
+       boost::shared_ptr<AudioPlaylist> audio_playlist() const;
 
-protected:
-       static framecnt_t disk_read_chunk_frames;
-       static framecnt_t disk_write_chunk_frames;
+       virtual void playlist_modified () {}
+       virtual int use_playlist (DataType, boost::shared_ptr<Playlist>);
 
-       uint32_t i_am_the_modifier;
+       PBD::Signal1<void,DataType>   PlaylistChanged;
 
-       Track*       _track;
-       ChanCount    _n_channels;
+       virtual void adjust_buffering() = 0;
 
-       double       _visible_speed;
+  protected:
+       friend class Auditioner;
+       virtual int  seek (framepos_t which_sample, bool complete_refill = false) = 0;
+
+  protected:
+       Flag         _flags;
+       uint32_t      i_am_the_modifier;
        double       _actual_speed;
+       double       _target_speed;
        /* items needed for speed change logic */
        bool         _buffer_reallocation_required;
        bool         _seek_required;
        bool         _slaved;
        Location*     loop_location;
-       double        _speed;
-       double        _target_speed;
        bool          in_set_state;
+       framepos_t     playback_sample;
+       framecnt_t    wrap_buffer_size;
+       framecnt_t    speed_buffer_size;
+       bool         _need_butler;
+       boost::shared_ptr<Route> _route;
+
+       void init ();
 
        Glib::Threads::Mutex state_lock;
-       Flag _flags;
-};
 
-class LIBARDOUR_API DiskReader : public DiskIOProcessor
-{
-  public:
-       DiskReader (Session&, std::string const & name);
-       ~DiskReader ();
+       static bool get_buffering_presets (BufferingPreset bp,
+                                          framecnt_t& read_chunk_size,
+                                          framecnt_t& read_buffer_size,
+                                          framecnt_t& write_chunk_size,
+                                          framecnt_t& write_buffer_size);
 
-  private:
-       boost::shared_ptr<Playlist> _playlist;
+       enum TransitionType {
+               CaptureStart = 0,
+               CaptureEnd
+       };
 
-       framepos_t    overwrite_frame;
-       off_t         overwrite_offset;
-       bool          _pending_overwrite;
-       bool          overwrite_queued;
-       IOChange      input_change_pending;
-       framecnt_t    wrap_buffer_size;
-       framecnt_t    speed_buffer_size;
+       struct CaptureTransition {
+               TransitionType   type;
+               framepos_t       capture_val; ///< The start or end file frame position
+       };
 
-       double        _speed;
-       double        _target_speed;
+       /** Information about one audio channel, playback or capture
+        * (depending on the derived class)
+        */
+       struct ChannelInfo : public boost::noncopyable {
 
-       /** The next frame position that we should be reading from in our playlist */
-       framepos_t     file_frame;
-       framepos_t     playback_sample;
+               ChannelInfo (framecnt_t buffer_size);
+               ~ChannelInfo ();
 
-       PBD::ScopedConnectionList playlist_connections;
-       PBD::ScopedConnection ic_connection;
-};
+               /** A ringbuffer for data to be played back, written to in the
+                   butler thread, read from in the process thread.
+               */
+               PBD::RingBufferNPT<Sample>* buf;
 
-class LIBARDOUR_API DiskWriter : public DiskIOProcessor
-{
-  public:
-       DiskWriter (Session&, std::string const & name);
-       ~DiskWriter ();
+               Sample* scrub_buffer;
+               Sample* scrub_forward_buffer;
+               Sample* scrub_reverse_buffer;
 
-  private:
-       std::vector<CaptureInfo*> capture_info;
-       mutable Glib::Threads::Mutex capture_info_lock;
+               PBD::RingBufferNPT<Sample>::rw_vector rw_vector;
 
-       double       _visible_speed;
-       double       _actual_speed;
-       /* items needed for speed change logic */
-       bool         _buffer_reallocation_required;
-       bool         _seek_required;
+               /* used only by capture */
+               boost::shared_ptr<AudioFileSource> write_source;
+               PBD::RingBufferNPT<CaptureTransition> * capture_transition_buf;
+               // the following are used in the butler thread only
+               framecnt_t                     curr_capture_cnt;
 
-       /** Start of currently running capture in session frames */
-       framepos_t    capture_start_frame;
-       framecnt_t    capture_captured;
-       bool          was_recording;
-       framecnt_t    adjust_capture_position;
-       framecnt_t   _capture_offset;
-       framepos_t    first_recordable_frame;
-       framepos_t    last_recordable_frame;
-       int           last_possibly_recording;
-       AlignStyle   _alignment_style;
-       AlignChoice  _alignment_choice;
-       framecnt_t    wrap_buffer_size;
-       framecnt_t    speed_buffer_size;
+               void resize (framecnt_t);
+       };
 
-       std::string   _write_source_name;
+       typedef std::vector<ChannelInfo*> ChannelList;
+       SerializedRCUManager<ChannelList> channels;
 
-       PBD::ScopedConnection ic_connection;
-};
+       int add_channel_to (boost::shared_ptr<ChannelList>, uint32_t how_many);
+       int remove_channel_from (boost::shared_ptr<ChannelList>, uint32_t how_many);
+
+       CubicInterpolation interpolation;
 
+       boost::shared_ptr<Playlist> _playlists[DataType::num_types];
+       PBD::ScopedConnectionList playlist_connections;
+
+       virtual void playlist_changed (const PBD::PropertyChange&) {}
+       virtual void playlist_deleted (boost::weak_ptr<Playlist>);
+       virtual void playlist_ranges_moved (std::list< Evoral::RangeMove<framepos_t> > const &, bool) {}
+
+       /* The MIDI stuff */
+
+       MidiRingBuffer<framepos_t>*  _midi_buf;
+       gint                         _frames_written_to_ringbuffer;
+       gint                         _frames_read_from_ringbuffer;
+       CubicMidiInterpolation        midi_interpolation;
+
+       static void get_location_times (const Location* location, framepos_t* start, framepos_t* end, framepos_t* length);
+};
 
 } // namespace ARDOUR
 
-#endif /* __ardour_processor_h__ */
+#endif /* __ardour_disk_io_h__ */