4c5244ab99f3a85029f9f160ea9d290a1abf28ea
[ardour.git] / libs / ardour / ardour / disk_reader.h
1 /*
2     Copyright (C) 2009-2016 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #ifndef __ardour_disk_reader_h__
21 #define __ardour_disk_reader_h__
22
23 #include "ardour/disk_io.h"
24 #include "ardour/midi_buffer.h"
25
26 namespace ARDOUR
27 {
28
29 class Playlist;
30 class AudioPlaylist;
31 class MidiPlaylist;
32 template<typename T> class MidiRingBuffer;
33
34 class LIBARDOUR_API DiskReader : public DiskIOProcessor
35 {
36   public:
37         DiskReader (Session&, std::string const & name, DiskIOProcessor::Flag f = DiskIOProcessor::Flag (0));
38         ~DiskReader ();
39
40         bool set_name (std::string const & str);
41
42         static framecnt_t chunk_frames() { return _chunk_frames; }
43         static framecnt_t default_chunk_frames ();
44         static void set_chunk_frames (framecnt_t n) { _chunk_frames = n; }
45
46         void run (BufferSet& /*bufs*/, framepos_t /*start_frame*/, framepos_t /*end_frame*/, double speed, pframes_t /*nframes*/, bool /*result_required*/);
47         int set_block_size (pframes_t);
48         bool configure_io (ChanCount in, ChanCount out);
49         bool can_support_io_configuration (const ChanCount& in, ChanCount& out) = 0;
50         void realtime_handle_transport_stopped ();
51         void realtime_locate ();
52         void non_realtime_locate (framepos_t);
53         int overwrite_existing_buffers ();
54         void set_pending_overwrite (bool yn);
55
56         framecnt_t roll_delay() const { return _roll_delay; }
57         void set_roll_delay (framecnt_t);
58
59         virtual XMLNode& state (bool full);
60         int set_state (const XMLNode&, int version);
61
62         boost::shared_ptr<Playlist>      get_playlist (DataType dt) const { return _playlists[dt]; }
63         boost::shared_ptr<MidiPlaylist>  midi_playlist() const;
64         boost::shared_ptr<AudioPlaylist> audio_playlist() const;
65
66         virtual void playlist_modified ();
67         virtual int use_playlist (DataType, boost::shared_ptr<Playlist>);
68         virtual int use_new_playlist (DataType);
69         virtual int use_copy_playlist (DataType);
70
71         PBD::Signal1<void,DataType>   PlaylistChanged;
72         PBD::Signal0<void>            AlignmentStyleChanged;
73
74         int set_loop (Location *loc);
75
76         float buffer_load() const;
77
78         void move_processor_automation (boost::weak_ptr<Processor>, std::list<Evoral::RangeMove<framepos_t> > const &);
79
80         /* called by the Butler in a non-realtime context */
81
82         int do_refill () {
83                 return refill (_mixdown_buffer, _gain_buffer, 0);
84         }
85
86         /** For non-butler contexts (allocates temporary working buffers)
87          *
88          * This accessible method has a default argument; derived classes
89          * must inherit the virtual method that we call which does NOT
90          * have a default argument, to avoid complications with inheritance
91          */
92         int do_refill_with_alloc (bool partial_fill = true) {
93                 return _do_refill_with_alloc (partial_fill);
94         }
95
96         bool pending_overwrite () const { return _pending_overwrite; }
97
98         virtual int find_and_use_playlist (DataType, std::string const &);
99
100         // Working buffers for do_refill (butler thread)
101         static void allocate_working_buffers();
102         static void free_working_buffers();
103
104         void adjust_buffering ();
105
106         int can_internal_playback_seek (framecnt_t distance);
107         int seek (framepos_t frame, bool complete_refill = false);
108
109         bool need_butler() const { return _need_butler; }
110
111         PBD::Signal0<void> Underrun;
112
113   protected:
114         boost::shared_ptr<Playlist> _playlists[DataType::num_types];
115
116         virtual void playlist_changed (const PBD::PropertyChange&);
117         virtual void playlist_deleted (boost::weak_ptr<Playlist>);
118         virtual void playlist_ranges_moved (std::list< Evoral::RangeMove<framepos_t> > const &, bool);
119
120         void reset_tracker ();
121         void resolve_tracker (Evoral::EventSink<framepos_t>& buffer, framepos_t time);
122         boost::shared_ptr<MidiBuffer> get_gui_feed_buffer () const;
123
124   private:
125         /** The number of frames by which this diskstream's output should be delayed
126             with respect to the transport frame.  This is used for latency compensation.
127         */
128         framecnt_t   _roll_delay;
129         Location*     loop_location;
130         framepos_t    overwrite_frame;
131         off_t         overwrite_offset;
132         bool          _pending_overwrite;
133         bool          overwrite_queued;
134         IOChange      input_change_pending;
135         framecnt_t    wrap_buffer_size;
136         framecnt_t    speed_buffer_size;
137         framepos_t     file_frame;
138         framepos_t     playback_sample;
139         MonitorChoice   _monitoring_choice;
140         bool            _need_butler;
141
142         PBD::ScopedConnectionList playlist_connections;
143
144         int _do_refill_with_alloc (bool partial_fill);
145
146         static framecnt_t _chunk_frames;
147         static framecnt_t midi_readahead;
148
149         /* The MIDI stuff */
150
151         MidiRingBuffer<framepos_t>*  _midi_buf;
152
153         /** A buffer that we use to put newly-arrived MIDI data in for
154             the GUI to read (so that it can update itself).
155         */
156         MidiBuffer                   _gui_feed_buffer;
157         mutable Glib::Threads::Mutex _gui_feed_buffer_mutex;
158         CubicMidiInterpolation midi_interpolation;
159         gint                         _frames_written_to_ringbuffer;
160         gint                         _frames_read_from_ringbuffer;
161
162
163         int audio_read (Sample* buf, Sample* mixdown_buffer, float* gain_buffer,
164                         framepos_t& start, framecnt_t cnt,
165                         int channel, bool reversed);
166         int midi_read (framepos_t& start, framecnt_t cnt, bool reversed);
167
168         static Sample* _mixdown_buffer;
169         static gain_t* _gain_buffer;
170
171         int refill (Sample* mixdown_buffer, float* gain_buffer, framecnt_t fill_level);
172         int refill_audio (Sample *mixdown_buffer, float *gain_buffer, framecnt_t fill_level);
173         int refill_midi ();
174
175         int internal_playback_seek (framecnt_t distance);
176         frameoffset_t calculate_playback_distance (pframes_t);
177
178         void allocate_temporary_buffers();
179         void get_playback (MidiBuffer& dst, framecnt_t nframes);
180         void flush_playback (framepos_t start, framepos_t end);
181 };
182
183 } // namespace
184
185 #endif /* __ardour_disk_reader_h__ */