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