remove all remaining vestiges of per-track varispeed from libardour
[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 AudioFileSource;
36 class AudioPlaylist;
37 class Location;
38 class MidiPlaylist;
39 class Playlist;
40 class Route;
41 class Route;
42 class Session;
43
44 template<typename T> class MidiRingBuffer;
45
46 class LIBARDOUR_API DiskIOProcessor : public Processor
47 {
48   public:
49         enum Flag {
50                 Recordable  = 0x1,
51                 Hidden      = 0x2,
52                 Destructive = 0x4,
53                 NonLayered   = 0x8
54         };
55
56         static const std::string state_node_name;
57
58         DiskIOProcessor (Session&, const std::string& name, Flag f);
59
60         void set_route (boost::shared_ptr<Route>);
61
62         static void set_buffering_parameters (BufferingPreset bp);
63
64         int set_block_size (pframes_t);
65         bool configure_io (ChanCount in, ChanCount out);
66         bool can_support_io_configuration (const ChanCount& in, ChanCount& out);
67
68         /** @return A number between 0 and 1, where 0 indicates that the playback/capture buffer
69          *  is dry (ie the disk subsystem could not keep up) and 1 indicates that the
70          *  buffer is full.
71          */
72         virtual float buffer_load() const = 0;
73
74         void set_flag (Flag f)   { _flags = Flag (_flags | f); }
75         void unset_flag (Flag f) { _flags = Flag (_flags & ~f); }
76
77         bool           hidden()      const { return _flags & Hidden; }
78         bool           recordable()  const { return _flags & Recordable; }
79         bool           non_layered()  const { return _flags & NonLayered; }
80
81         virtual void non_realtime_locate (framepos_t);
82
83         void non_realtime_speed_change ();
84         bool realtime_speed_change ();
85
86         virtual void punch_in()  {}
87         virtual void punch_out() {}
88
89         bool slaved() const      { return _slaved; }
90         void set_slaved(bool yn) { _slaved = yn; }
91
92         int set_loop (Location *loc);
93
94         PBD::Signal1<void,Location *> LoopSet;
95         PBD::Signal0<void>            SpeedChanged;
96         PBD::Signal0<void>            ReverseChanged;
97
98         int set_state (const XMLNode&, int version);
99
100         int add_channel (uint32_t how_many);
101         int remove_channel (uint32_t how_many);
102
103         bool need_butler() const { return _need_butler; }
104
105         boost::shared_ptr<Playlist>      get_playlist (DataType dt) const { return _playlists[dt]; }
106         boost::shared_ptr<MidiPlaylist>  midi_playlist() const;
107         boost::shared_ptr<AudioPlaylist> audio_playlist() const;
108
109         virtual void playlist_modified () {}
110         virtual int use_playlist (DataType, boost::shared_ptr<Playlist>);
111
112         PBD::Signal1<void,DataType>   PlaylistChanged;
113
114         virtual void adjust_buffering() = 0;
115
116   protected:
117         friend class Auditioner;
118         virtual int  seek (framepos_t which_sample, bool complete_refill = false) = 0;
119
120   protected:
121         Flag         _flags;
122         uint32_t      i_am_the_modifier;
123         double       _actual_speed;
124         double       _target_speed;
125         /* items needed for speed change logic */
126         bool         _buffer_reallocation_required;
127         bool         _seek_required;
128         bool         _slaved;
129         Location*     loop_location;
130         bool          in_set_state;
131         framepos_t     file_frame;
132         framepos_t     playback_sample;
133         framecnt_t    wrap_buffer_size;
134         framecnt_t    speed_buffer_size;
135         bool         _need_butler;
136         boost::shared_ptr<Route> _route;
137
138         void init ();
139
140         Glib::Threads::Mutex state_lock;
141
142         static bool get_buffering_presets (BufferingPreset bp,
143                                            framecnt_t& read_chunk_size,
144                                            framecnt_t& read_buffer_size,
145                                            framecnt_t& write_chunk_size,
146                                            framecnt_t& write_buffer_size);
147
148         enum TransitionType {
149                 CaptureStart = 0,
150                 CaptureEnd
151         };
152
153         struct CaptureTransition {
154                 TransitionType   type;
155                 framepos_t       capture_val; ///< The start or end file frame position
156         };
157
158         /** Information about one audio channel, playback or capture
159          * (depending on the derived class)
160          */
161         struct ChannelInfo : public boost::noncopyable {
162
163                 ChannelInfo (framecnt_t buffer_size);
164                 ~ChannelInfo ();
165
166                 /** A ringbuffer for data to be played back, written to in the
167                     butler thread, read from in the process thread.
168                 */
169                 PBD::RingBufferNPT<Sample>* buf;
170
171                 Sample* scrub_buffer;
172                 Sample* scrub_forward_buffer;
173                 Sample* scrub_reverse_buffer;
174
175                 PBD::RingBufferNPT<Sample>::rw_vector rw_vector;
176
177                 /* used only by capture */
178                 boost::shared_ptr<AudioFileSource> write_source;
179                 PBD::RingBufferNPT<CaptureTransition> * capture_transition_buf;
180                 // the following are used in the butler thread only
181                 framecnt_t                     curr_capture_cnt;
182
183                 void resize (framecnt_t);
184         };
185
186         typedef std::vector<ChannelInfo*> ChannelList;
187         SerializedRCUManager<ChannelList> channels;
188
189         int add_channel_to (boost::shared_ptr<ChannelList>, uint32_t how_many);
190         int remove_channel_from (boost::shared_ptr<ChannelList>, uint32_t how_many);
191
192         CubicInterpolation interpolation;
193
194         boost::shared_ptr<Playlist> _playlists[DataType::num_types];
195         PBD::ScopedConnectionList playlist_connections;
196
197         virtual void playlist_changed (const PBD::PropertyChange&) {}
198         virtual void playlist_deleted (boost::weak_ptr<Playlist>);
199         virtual void playlist_ranges_moved (std::list< Evoral::RangeMove<framepos_t> > const &, bool) {}
200
201         /* The MIDI stuff */
202
203         MidiRingBuffer<framepos_t>*  _midi_buf;
204         gint                         _frames_written_to_ringbuffer;
205         gint                         _frames_read_from_ringbuffer;
206         CubicMidiInterpolation        midi_interpolation;
207
208         static void get_location_times (const Location* location, framepos_t* start, framepos_t* end, framepos_t* length);
209 };
210
211 } // namespace ARDOUR
212
213 #endif /* __ardour_disk_io_h__ */