move need-butler from DiskReader to DiskIOProcessor
[ardour.git] / libs / ardour / ardour / disk_io.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_io_h__
21 #define __ardour_disk_io_h__
22
23 #include <vector>
24 #include <string>
25 #include <exception>
26
27 #include "pbd/ringbufferNPT.h"
28 #include "pbd/rcu.h"
29
30 #include "ardour/interpolation.h"
31 #include "ardour/processor.h"
32
33 namespace ARDOUR {
34
35 class Session;
36 class Route;
37 class Location;
38
39 class LIBARDOUR_API DiskIOProcessor : public Processor
40 {
41   public:
42         enum Flag {
43                 Recordable  = 0x1,
44                 Hidden      = 0x2,
45                 Destructive = 0x4,
46                 NonLayered   = 0x8
47         };
48
49         static const std::string state_node_name;
50
51         DiskIOProcessor (Session&, const std::string& name, Flag f);
52
53         static void set_buffering_parameters (BufferingPreset bp);
54
55         /** @return A number between 0 and 1, where 0 indicates that the playback buffer
56          *  is dry (ie the disk subsystem could not keep up) and 1 indicates that the
57          *  buffer is full.
58          */
59         virtual float playback_buffer_load() const = 0;
60         virtual float capture_buffer_load() const = 0;
61
62         void set_flag (Flag f)   { _flags = Flag (_flags | f); }
63         void unset_flag (Flag f) { _flags = Flag (_flags & ~f); }
64
65         bool           hidden()      const { return _flags & Hidden; }
66         bool           recordable()  const { return _flags & Recordable; }
67         bool           non_layered()  const { return _flags & NonLayered; }
68         bool           reversed()    const { return _actual_speed < 0.0f; }
69         double         speed()       const { return _visible_speed; }
70
71         ChanCount n_channels() { return _n_channels; }
72
73         void non_realtime_set_speed ();
74         bool realtime_set_speed (double sp, bool global);
75
76         virtual void punch_in()  {}
77         virtual void punch_out() {}
78
79         virtual float buffer_load() const = 0;
80
81         bool slaved() const      { return _slaved; }
82         void set_slaved(bool yn) { _slaved = yn; }
83
84         int set_loop (Location *loc);
85
86         PBD::Signal1<void,Location *> LoopSet;
87         PBD::Signal0<void>            SpeedChanged;
88         PBD::Signal0<void>            ReverseChanged;
89
90         int set_state (const XMLNode&, int version);
91
92         int add_channel (uint32_t how_many);
93         int remove_channel (uint32_t how_many);
94
95         bool need_butler() const { return _need_butler; }
96
97   protected:
98         friend class Auditioner;
99         virtual int  seek (framepos_t which_sample, bool complete_refill = false) = 0;
100
101   protected:
102         Flag         _flags;
103         uint32_t i_am_the_modifier;
104         ChanCount    _n_channels;
105         double       _visible_speed;
106         double       _actual_speed;
107         double       _speed;
108         double       _target_speed;
109         /* items needed for speed change logic */
110         bool         _buffer_reallocation_required;
111         bool         _seek_required;
112         bool         _slaved;
113         Location*     loop_location;
114         bool          in_set_state;
115         framecnt_t    wrap_buffer_size;
116         framecnt_t    speed_buffer_size;
117         bool         _need_butler;
118
119         Glib::Threads::Mutex state_lock;
120
121         static bool get_buffering_presets (BufferingPreset bp,
122                                            framecnt_t& read_chunk_size,
123                                            framecnt_t& read_buffer_size,
124                                            framecnt_t& write_chunk_size,
125                                            framecnt_t& write_buffer_size);
126
127         virtual void allocate_temporary_buffers () = 0;
128
129         /** Information about one audio channel, playback or capture
130          * (depending on the derived class)
131          */
132         struct ChannelInfo : public boost::noncopyable {
133
134                 ChannelInfo (framecnt_t buffer_size,
135                              framecnt_t speed_buffer_size,
136                              framecnt_t wrap_buffer_size);
137                 ~ChannelInfo ();
138
139                 Sample     *wrap_buffer;
140                 Sample     *speed_buffer;
141                 Sample     *current_buffer;
142
143                 /** A ringbuffer for data to be played back, written to in the
144                     butler thread, read from in the process thread.
145                 */
146                 PBD::RingBufferNPT<Sample>* buf;
147
148                 Sample* scrub_buffer;
149                 Sample* scrub_forward_buffer;
150                 Sample* scrub_reverse_buffer;
151
152                 PBD::RingBufferNPT<Sample>::rw_vector read_vector;
153
154                 void resize (framecnt_t);
155         };
156
157         typedef std::vector<ChannelInfo*> ChannelList;
158         SerializedRCUManager<ChannelList> channels;
159
160         int add_channel_to (boost::shared_ptr<ChannelList>, uint32_t how_many);
161         int remove_channel_from (boost::shared_ptr<ChannelList>, uint32_t how_many);
162
163         CubicInterpolation interpolation;
164
165 };
166
167 } // namespace ARDOUR
168
169 #endif /* __ardour_disk_io_h__ */