merge AudioDiskstream playback code into DiskReader
[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 "pbd/ringbufferNPT.h"
24 #include "pbd/rcu.h"
25
26 #include "ardour/disk_io.h"
27 #include "ardour/interpolation.h"
28
29 namespace ARDOUR
30 {
31
32 class Playlist;
33 class AudioPlaylist;
34 class MidiPlaylist;
35
36 class LIBARDOUR_API DiskReader : public DiskIOProcessor
37 {
38   public:
39         DiskReader (Session&, std::string const & name, DiskIOProcessor::Flag f = DiskIOProcessor::Flag (0));
40         ~DiskReader ();
41
42         bool set_name (std::string const & str);
43
44         static framecnt_t chunk_frames() { return _chunk_frames; }
45         static framecnt_t default_chunk_frames ();
46         static void set_chunk_frames (framecnt_t n) { _chunk_frames = n; }
47
48         void run (BufferSet& /*bufs*/, framepos_t /*start_frame*/, framepos_t /*end_frame*/, double speed, pframes_t /*nframes*/, bool /*result_required*/);
49         int set_block_size (pframes_t);
50         bool configure_io (ChanCount in, ChanCount out);
51         bool can_support_io_configuration (const ChanCount& in, ChanCount& out) = 0;
52         void realtime_handle_transport_stopped ();
53         void realtime_locate ();
54         void non_realtime_locate (framepos_t);
55         int overwrite_existing_buffers ();
56         void set_pending_overwrite (bool yn);
57
58         framecnt_t roll_delay() const { return _roll_delay; }
59         void set_roll_delay (framecnt_t);
60
61         virtual XMLNode& state (bool full);
62         int set_state (const XMLNode&, int version);
63
64         boost::shared_ptr<Playlist>      playlist();
65         boost::shared_ptr<Playlist>      get_playlist (DataType);
66         boost::shared_ptr<MidiPlaylist>  midi_playlist();
67         boost::shared_ptr<AudioPlaylist> audio_playlist();
68
69         virtual void playlist_modified ();
70         virtual int use_playlist (boost::shared_ptr<Playlist>);
71         virtual int use_new_playlist ();
72         virtual int use_copy_playlist ();
73
74         PBD::Signal0<void>            PlaylistChanged;
75         PBD::Signal0<void>            AlignmentStyleChanged;
76
77         float buffer_load() const;
78
79         void move_processor_automation (boost::weak_ptr<Processor>, std::list<Evoral::RangeMove<framepos_t> > const &);
80
81         /* called by the Butler in a non-realtime context */
82
83         int do_refill () {
84                 return _do_refill (_mixdown_buffer, _gain_buffer, 0);
85         }
86
87         /** For non-butler contexts (allocates temporary working buffers)
88          *
89          * This accessible method has a default argument; derived classes
90          * must inherit the virtual method that we call which does NOT
91          * have a default argument, to avoid complications with inheritance
92          */
93         int do_refill_with_alloc (bool partial_fill = true) {
94                 return _do_refill_with_alloc (partial_fill);
95         }
96
97         bool pending_overwrite () const { return _pending_overwrite; }
98
99         virtual int find_and_use_playlist (std::string const &);
100
101         // Working buffers for do_refill (butler thread)
102         static void allocate_working_buffers();
103         static void free_working_buffers();
104
105         void adjust_buffering ();
106
107         int can_internal_playback_seek (framecnt_t distance);
108         int seek (framepos_t frame, bool complete_refill = false);
109
110         int add_channel (uint32_t how_many);
111         int remove_channel (uint32_t how_many);
112
113         PBD::Signal0<void> Underrun;
114
115   protected:
116         boost::shared_ptr<Playlist> _playlist;
117
118         virtual void playlist_changed (const PBD::PropertyChange&);
119         virtual void playlist_deleted (boost::weak_ptr<Playlist>);
120         virtual void playlist_ranges_moved (std::list< Evoral::RangeMove<framepos_t> > const &, bool);
121
122
123   private:
124         typedef std::map<DataType,boost::shared_ptr<Playlist> > Playlists;
125
126         /** The number of frames by which this diskstream's output should be delayed
127             with respect to the transport frame.  This is used for latency compensation.
128         */
129         framecnt_t   _roll_delay;
130         Playlists     _playlists;
131         framepos_t    overwrite_frame;
132         off_t         overwrite_offset;
133         bool          _pending_overwrite;
134         bool          overwrite_queued;
135         IOChange      input_change_pending;
136         framecnt_t    wrap_buffer_size;
137         framecnt_t    speed_buffer_size;
138         framepos_t     file_frame;
139         framepos_t     playback_sample;
140         MonitorChoice   _monitoring_choice;
141
142         PBD::ScopedConnectionList playlist_connections;
143
144         virtual int _do_refill_with_alloc (bool partial_fill);
145
146         static framecnt_t _chunk_frames;
147
148         /** Information about one of our channels */
149         struct ChannelInfo : public boost::noncopyable {
150
151                 ChannelInfo (framecnt_t buffer_size,
152                              framecnt_t speed_buffer_size,
153                              framecnt_t wrap_buffer_size);
154                 ~ChannelInfo ();
155
156                 Sample     *wrap_buffer;
157                 Sample     *speed_buffer;
158                 Sample     *current_buffer;
159
160                 /** A ringbuffer for data to be played back, written to in the
161                     butler thread, read from in the process thread.
162                 */
163                 PBD::RingBufferNPT<Sample> *buf;
164
165                 Sample* scrub_buffer;
166                 Sample* scrub_forward_buffer;
167                 Sample* scrub_reverse_buffer;
168
169                 PBD::RingBufferNPT<Sample>::rw_vector read_vector;
170
171                 void resize (framecnt_t);
172         };
173
174         typedef std::vector<ChannelInfo*> ChannelList;
175         SerializedRCUManager<ChannelList> channels;
176
177         CubicInterpolation interpolation;
178
179         int read (Sample* buf, Sample* mixdown_buffer, float* gain_buffer,
180                   framepos_t& start, framecnt_t cnt,
181                   int channel, bool reversed);
182
183         static Sample* _mixdown_buffer;
184         static gain_t* _gain_buffer;
185
186         int _do_refill (Sample *mixdown_buffer, float *gain_buffer, framecnt_t fill_level);
187
188         int add_channel_to (boost::shared_ptr<ChannelList>, uint32_t how_many);
189         int remove_channel_from (boost::shared_ptr<ChannelList>, uint32_t how_many);
190
191         int internal_playback_seek (framecnt_t distance);
192         frameoffset_t calculate_playback_distance (pframes_t);
193
194         void allocate_temporary_buffers();
195 };
196
197 } // namespace
198
199 #endif /* __ardour_disk_reader_h__ */