e37963a07a0addffcef43586ea158c183852a918
[ardour.git] / libs / ardour / ardour / audio_diskstream.h
1 /*
2     Copyright (C) 2000-2006 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 #ifndef __ardour_audio_diskstream_h__
20 #define __ardour_audio_diskstream_h__
21
22
23 #include <cmath>
24 #include <string>
25 #include <queue>
26 #include <map>
27 #include <vector>
28
29 #include <time.h>
30
31 #include <boost/utility.hpp>
32
33 #include "pbd/fastlog.h"
34 #include "pbd/ringbufferNPT.h"
35 #include "pbd/stateful.h"
36 #include "pbd/rcu.h"
37
38 #include "ardour/ardour.h"
39 #include "ardour/utils.h"
40 #include "ardour/diskstream.h"
41 #include "ardour/audioplaylist.h"
42 #include "ardour/port.h"
43 #include "ardour/interpolation.h"
44
45 struct tm;
46
47 namespace ARDOUR {
48
49 class AudioEngine;
50 class Send;
51 class Session;
52 class AudioPlaylist;
53 class AudioFileSource;
54 class IO;
55
56 class AudioDiskstream : public Diskstream
57 {
58   public:
59         AudioDiskstream (Session &, const std::string& name, Diskstream::Flag f = Recordable);
60         AudioDiskstream (Session &, const XMLNode&);
61         ~AudioDiskstream();
62
63         float playback_buffer_load() const;
64         float capture_buffer_load() const;
65
66         std::string input_source (uint32_t n=0) const {
67                 boost::shared_ptr<ChannelList> c = channels.reader();
68                 if (n < c->size()) {
69                         return (*c)[n]->source.name;
70                 } else {
71                         return "";
72                 }
73         }
74
75         void set_record_enabled (bool yn);
76         int set_destructive (bool yn);
77         int set_non_layered (bool yn);
78         bool can_become_destructive (bool& requires_bounce) const;
79
80         float peak_power(uint32_t n = 0) {
81                 boost::shared_ptr<ChannelList> c = channels.reader();
82                 ChannelInfo* chaninfo = (*c)[n];
83                 float x = chaninfo->peak_power;
84                 chaninfo->peak_power = 0.0f;
85                 if (x > 0.0f) {
86                         return 20.0f * fast_log10(x);
87                 } else {
88                         return minus_infinity();
89                 }
90         }
91
92         boost::shared_ptr<AudioPlaylist> audio_playlist () { return boost::dynamic_pointer_cast<AudioPlaylist>(_playlist); }
93
94         int use_playlist (boost::shared_ptr<Playlist>);
95         int use_new_playlist ();
96         int use_copy_playlist ();
97
98         Sample *playback_buffer (uint32_t n = 0) {
99                 boost::shared_ptr<ChannelList> c = channels.reader();
100                 if (n < c->size())
101                         return (*c)[n]->current_playback_buffer;
102                 return 0;
103         }
104
105         Sample *capture_buffer (uint32_t n = 0) {
106                 boost::shared_ptr<ChannelList> c = channels.reader();
107                 if (n < c->size())
108                         return (*c)[n]->current_capture_buffer;
109                 return 0;
110         }
111
112         boost::shared_ptr<AudioFileSource> write_source (uint32_t n=0) {
113                 boost::shared_ptr<ChannelList> c = channels.reader();
114                 if (n < c->size())
115                         return (*c)[n]->write_source;
116                 return boost::shared_ptr<AudioFileSource>();
117         }
118
119         int add_channel (uint32_t how_many);
120         int remove_channel (uint32_t how_many);
121
122         bool set_name (std::string const &);
123
124         /* stateful */
125
126         XMLNode& get_state(void);
127         int      set_state(const XMLNode& node, int version);
128
129         void monitor_input (bool);
130
131         static void swap_by_ptr (Sample *first, Sample *last) {
132                 while (first < last) {
133                         Sample tmp = *first;
134                         *first++ = *last;
135                         *last-- = tmp;
136                 }
137         }
138
139         static void swap_by_ptr (Sample *first, Sample *last, framecnt_t n) {
140                 while (n--) {
141                         Sample tmp = *first;
142                         *first++ = *last;
143                         *last-- = tmp;
144                 }
145         }
146
147         CubicInterpolation interpolation;
148
149   protected:
150         friend class Session;
151
152         /* the Session is the only point of access for these
153            because they require that the Session is "inactive"
154            while they are called.
155         */
156
157         void set_pending_overwrite(bool);
158         int  overwrite_existing_buffers ();
159         void set_block_size (pframes_t);
160         int  internal_playback_seek (framecnt_t distance);
161         int  can_internal_playback_seek (framecnt_t distance);
162         std::list<boost::shared_ptr<Source> > steal_write_sources();
163         void reset_write_sources (bool, bool force = false);
164         void non_realtime_input_change ();
165         void non_realtime_locate (framepos_t location);
166
167   protected:
168         friend class Auditioner;
169         int  seek (framepos_t which_sample, bool complete_refill = false);
170
171   protected:
172         friend class AudioTrack;
173
174         int  process (framepos_t transport_frame, pframes_t nframes, bool can_record, bool rec_monitors_input, bool& need_butler);
175         bool commit  (framecnt_t nframes);
176
177   private:
178         struct ChannelSource {
179                 std::string name;
180
181                 bool is_physical () const;
182                 void ensure_monitor_input (bool) const;
183         };
184
185         struct ChannelInfo : public boost::noncopyable {
186
187                 ChannelInfo (framecnt_t playback_buffer_size,
188                              framecnt_t capture_buffer_size,
189                              framecnt_t speed_buffer_size,
190                              framecnt_t wrap_buffer_size);
191                 ~ChannelInfo ();
192
193                 Sample     *playback_wrap_buffer;
194                 Sample     *capture_wrap_buffer;
195                 Sample     *speed_buffer;
196
197                 float       peak_power;
198
199                 boost::shared_ptr<AudioFileSource> write_source;
200
201                 /// information the Port that our audio data comes from
202
203                 ChannelSource source;
204
205                 Sample       *current_capture_buffer;
206                 Sample       *current_playback_buffer;
207
208                 PBD::RingBufferNPT<Sample> *playback_buf;
209                 PBD::RingBufferNPT<Sample> *capture_buf;
210
211                 Sample* scrub_buffer;
212                 Sample* scrub_forward_buffer;
213                 Sample* scrub_reverse_buffer;
214
215                 PBD::RingBufferNPT<Sample>::rw_vector playback_vector;
216                 PBD::RingBufferNPT<Sample>::rw_vector capture_vector;
217
218                 PBD::RingBufferNPT<CaptureTransition> * capture_transition_buf;
219                 // the following are used in the butler thread only
220                 framecnt_t                     curr_capture_cnt;
221
222                 void resize_playback (framecnt_t);
223                 void resize_capture (framecnt_t);
224         };
225
226         typedef std::vector<ChannelInfo*> ChannelList;
227
228         void process_varispeed_playback (pframes_t nframes, boost::shared_ptr<ChannelList> c);
229
230         /* The two central butler operations */
231         int do_flush (RunContext context, bool force = false);
232         int do_refill () { return _do_refill(_mixdown_buffer, _gain_buffer); }
233
234         int do_refill_with_alloc ();
235
236         int read (Sample* buf, Sample* mixdown_buffer, float* gain_buffer,
237                   framepos_t& start, framecnt_t cnt,
238                   ChannelInfo* channel_info, int channel, bool reversed);
239
240         void finish_capture (bool rec_monitors_input, boost::shared_ptr<ChannelList>);
241         void transport_stopped_wallclock (struct tm&, time_t, bool abort);
242         void transport_looped (framepos_t transport_frame);
243
244         void init ();
245
246         void init_channel (ChannelInfo &chan);
247         void destroy_channel (ChannelInfo &chan);
248
249         int use_new_write_source (uint32_t n=0);
250
251         int find_and_use_playlist (const std::string &);
252
253         void allocate_temporary_buffers ();
254
255         int use_pending_capture_data (XMLNode& node);
256
257         void get_input_sources ();
258         void prepare_record_status(framepos_t capture_start_frame);
259         void set_align_style_from_io();
260         void setup_destructive_playlist ();
261         void use_destructive_playlist ();
262
263         void adjust_playback_buffering ();
264         void adjust_capture_buffering ();
265
266         void engage_record_enable ();
267         void disengage_record_enable ();
268
269         // Working buffers for do_refill (butler thread)
270         static void allocate_working_buffers();
271         static void free_working_buffers();
272
273         static size_t  _working_buffers_size;
274         static Sample* _mixdown_buffer;
275         static gain_t* _gain_buffer;
276
277         std::vector<boost::shared_ptr<AudioFileSource> > capturing_sources;
278
279         SerializedRCUManager<ChannelList> channels;
280
281  /* really */
282   private:
283         int _do_refill (Sample *mixdown_buffer, float *gain_buffer);
284
285         int add_channel_to (boost::shared_ptr<ChannelList>, uint32_t how_many);
286         int remove_channel_from (boost::shared_ptr<ChannelList>, uint32_t how_many);
287
288 };
289
290 } // namespace ARDOUR
291
292 #endif /* __ardour_audio_diskstream_h__ */