Separate ChannelInfo for disk reader and writer
[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 // deprecated (kept only for enum compat)
54         };
55
56         static const std::string state_node_name;
57
58         DiskIOProcessor (Session&, const std::string& name, Flag f);
59         virtual ~DiskIOProcessor ();
60
61         void set_route (boost::shared_ptr<Route>);
62         void drop_route ();
63
64         static void set_buffering_parameters (BufferingPreset bp);
65
66         int set_block_size (pframes_t);
67         bool configure_io (ChanCount in, ChanCount out);
68         bool can_support_io_configuration (const ChanCount& in, ChanCount& out);
69
70         /** @return A number between 0 and 1, where 0 indicates that the playback/capture buffer
71          *  is dry (ie the disk subsystem could not keep up) and 1 indicates that the
72          *  buffer is full.
73          */
74         virtual float buffer_load() const = 0;
75
76         void set_flag (Flag f)   { _flags = Flag (_flags | f); }
77         void unset_flag (Flag f) { _flags = Flag (_flags & ~f); }
78
79         bool           hidden()      const { return _flags & Hidden; }
80         bool           recordable()  const { return _flags & Recordable; }
81
82         virtual void non_realtime_locate (samplepos_t);
83
84         void non_realtime_speed_change ();
85         bool realtime_speed_change ();
86
87         virtual void punch_in()  {}
88         virtual void punch_out() {}
89
90         bool slaved() const      { return _slaved; }
91         void set_slaved(bool yn) { _slaved = yn; }
92
93         PBD::Signal0<void>            SpeedChanged;
94         PBD::Signal0<void>            ReverseChanged;
95
96         int set_state (const XMLNode&, int version);
97
98         int add_channel (uint32_t how_many);
99         int remove_channel (uint32_t how_many);
100
101         bool need_butler() const { return _need_butler; }
102
103         boost::shared_ptr<Playlist>      get_playlist (DataType dt) const { return _playlists[dt]; }
104         boost::shared_ptr<MidiPlaylist>  midi_playlist() const;
105         boost::shared_ptr<AudioPlaylist> audio_playlist() const;
106
107         virtual void playlist_modified () {}
108         virtual int use_playlist (DataType, boost::shared_ptr<Playlist>);
109
110         virtual void adjust_buffering() = 0;
111
112 protected:
113         friend class Auditioner;
114         virtual int  seek (samplepos_t which_sample, bool complete_refill = false) = 0;
115
116 protected:
117         Flag         _flags;
118         uint32_t      i_am_the_modifier;
119         double       _actual_speed;
120         double       _target_speed;
121         bool         _seek_required;
122         bool         _slaved;
123         bool          in_set_state;
124         samplepos_t   playback_sample;
125         bool         _need_butler;
126         boost::shared_ptr<Route> _route;
127
128         void init ();
129
130         Glib::Threads::Mutex state_lock;
131
132         static bool get_buffering_presets (BufferingPreset bp,
133                                            samplecnt_t& read_chunk_size,
134                                            samplecnt_t& read_buffer_size,
135                                            samplecnt_t& write_chunk_size,
136                                            samplecnt_t& write_buffer_size);
137
138         enum TransitionType {
139                 CaptureStart = 0,
140                 CaptureEnd
141         };
142
143         struct CaptureTransition {
144                 TransitionType   type;
145                 samplepos_t       capture_val; ///< The start or end file sample position
146         };
147
148         /** Information about one audio channel, playback or capture
149          * (depending on the derived class)
150          */
151         struct ChannelInfo : public boost::noncopyable {
152
153                 ChannelInfo (samplecnt_t buffer_size);
154                 virtual ~ChannelInfo ();
155
156                 /** Ringbuffer for data to be played back.
157                  * written to in the butler thread, read from in the process thread.
158                  */
159                 PBD::RingBufferNPT<Sample>* buf;
160
161                 /** A ringbuffer for data to be recorded back, written to in the
162                  * process thread, read from in the butler thread.
163                  */
164                 PBD::RingBufferNPT<Sample>* wbuf;
165                 PBD::RingBufferNPT<Sample>::rw_vector rw_vector;
166
167                 /* used only by capture */
168                 boost::shared_ptr<AudioFileSource> write_source;
169                 PBD::RingBufferNPT<CaptureTransition>* capture_transition_buf;
170
171                 /* used in the butler thread only */
172                 samplecnt_t curr_capture_cnt;
173
174                 virtual void resize (samplecnt_t) = 0;
175         };
176
177         typedef std::vector<ChannelInfo*> ChannelList;
178         SerializedRCUManager<ChannelList> channels;
179
180         virtual int add_channel_to (boost::shared_ptr<ChannelList>, uint32_t how_many) = 0;
181         int remove_channel_from (boost::shared_ptr<ChannelList>, uint32_t how_many);
182
183         boost::shared_ptr<Playlist> _playlists[DataType::num_types];
184         PBD::ScopedConnectionList playlist_connections;
185
186         virtual void playlist_changed (const PBD::PropertyChange&) {}
187         virtual void playlist_deleted (boost::weak_ptr<Playlist>);
188         virtual void playlist_ranges_moved (std::list< Evoral::RangeMove<samplepos_t> > const &, bool) {}
189
190         /* The MIDI stuff */
191
192         MidiRingBuffer<samplepos_t>*  _midi_buf;
193         gint                         _samples_written_to_ringbuffer;
194         gint                         _samples_read_from_ringbuffer;
195
196         static void get_location_times (const Location* location, samplepos_t* start, samplepos_t* end, samplepos_t* length);
197 };
198
199 } // namespace ARDOUR
200
201 #endif /* __ardour_disk_io_h__ */