2 Copyright (C) 2000-2006 Paul Davis
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.
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.
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.
19 #ifndef __ardour_audio_diskstream_h__
20 #define __ardour_audio_diskstream_h__
31 #include <boost/utility.hpp>
33 #include "pbd/fastlog.h"
34 #include "pbd/ringbufferNPT.h"
35 #include "pbd/stateful.h"
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"
53 class AudioFileSource;
56 class AudioDiskstream : public Diskstream
59 AudioDiskstream (Session &, const std::string& name, Diskstream::Flag f = Recordable);
60 AudioDiskstream (Session &, const XMLNode&);
63 float playback_buffer_load() const;
64 float capture_buffer_load() const;
66 std::string input_source (uint32_t n=0) const {
67 boost::shared_ptr<ChannelList> c = channels.reader();
69 return (*c)[n]->source.name;
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;
80 boost::shared_ptr<AudioPlaylist> audio_playlist () { return boost::dynamic_pointer_cast<AudioPlaylist>(_playlist); }
82 int use_playlist (boost::shared_ptr<Playlist>);
83 int use_new_playlist ();
84 int use_copy_playlist ();
86 Sample *playback_buffer (uint32_t n = 0) {
87 boost::shared_ptr<ChannelList> c = channels.reader();
89 return (*c)[n]->current_playback_buffer;
93 Sample *capture_buffer (uint32_t n = 0) {
94 boost::shared_ptr<ChannelList> c = channels.reader();
96 return (*c)[n]->current_capture_buffer;
100 boost::shared_ptr<AudioFileSource> write_source (uint32_t n=0) {
101 boost::shared_ptr<ChannelList> c = channels.reader();
103 return (*c)[n]->write_source;
104 return boost::shared_ptr<AudioFileSource>();
107 int add_channel (uint32_t how_many);
108 int remove_channel (uint32_t how_many);
110 bool set_name (std::string const &);
114 XMLNode& get_state(void);
115 int set_state(const XMLNode& node, int version);
117 void request_jack_monitors_input (bool);
119 static void swap_by_ptr (Sample *first, Sample *last) {
120 while (first < last) {
127 CubicInterpolation interpolation;
130 friend class Session;
132 /* the Session is the only point of access for these
133 because they require that the Session is "inactive"
134 while they are called.
137 void set_pending_overwrite(bool);
138 int overwrite_existing_buffers ();
139 void set_block_size (pframes_t);
140 int internal_playback_seek (framecnt_t distance);
141 int can_internal_playback_seek (framecnt_t distance);
142 std::list<boost::shared_ptr<Source> > steal_write_sources();
143 void reset_write_sources (bool, bool force = false);
144 void non_realtime_input_change ();
145 void non_realtime_locate (framepos_t location);
148 friend class Auditioner;
149 int seek (framepos_t which_sample, bool complete_refill = false);
152 friend class AudioTrack;
154 int process (framepos_t transport_frame, pframes_t nframes, framecnt_t &);
155 bool commit (framecnt_t);
158 struct ChannelSource {
161 bool is_physical () const;
162 void request_jack_monitors_input (bool) const;
165 /** Information about one of our channels */
166 struct ChannelInfo : public boost::noncopyable {
168 ChannelInfo (framecnt_t playback_buffer_size,
169 framecnt_t capture_buffer_size,
170 framecnt_t speed_buffer_size,
171 framecnt_t wrap_buffer_size);
174 Sample *playback_wrap_buffer;
175 Sample *capture_wrap_buffer;
176 Sample *speed_buffer;
178 boost::shared_ptr<AudioFileSource> write_source;
180 /** Information about the Port that our audio data comes from */
181 ChannelSource source;
183 Sample *current_capture_buffer;
184 Sample *current_playback_buffer;
186 /** A ringbuffer for data to be played back, written to in the
187 butler thread, read from in the process thread.
189 PBD::RingBufferNPT<Sample> *playback_buf;
190 PBD::RingBufferNPT<Sample> *capture_buf;
192 Sample* scrub_buffer;
193 Sample* scrub_forward_buffer;
194 Sample* scrub_reverse_buffer;
196 PBD::RingBufferNPT<Sample>::rw_vector playback_vector;
197 PBD::RingBufferNPT<Sample>::rw_vector capture_vector;
199 PBD::RingBufferNPT<CaptureTransition> * capture_transition_buf;
200 // the following are used in the butler thread only
201 framecnt_t curr_capture_cnt;
203 void resize_playback (framecnt_t);
204 void resize_capture (framecnt_t);
207 typedef std::vector<ChannelInfo*> ChannelList;
209 /* The two central butler operations */
210 int do_flush (RunContext context, bool force = false);
211 int do_refill () { return _do_refill(_mixdown_buffer, _gain_buffer); }
213 int do_refill_with_alloc ();
215 int read (Sample* buf, Sample* mixdown_buffer, float* gain_buffer,
216 framepos_t& start, framecnt_t cnt,
217 int channel, bool reversed);
219 void finish_capture (boost::shared_ptr<ChannelList>);
220 void transport_stopped_wallclock (struct tm&, time_t, bool abort);
221 void transport_looped (framepos_t transport_frame);
225 void init_channel (ChannelInfo &chan);
226 void destroy_channel (ChannelInfo &chan);
228 int use_new_write_source (uint32_t n=0);
230 int find_and_use_playlist (const std::string &);
232 void allocate_temporary_buffers ();
234 int use_pending_capture_data (XMLNode& node);
236 void get_input_sources ();
237 void prepare_record_status(framepos_t capture_start_frame);
238 void set_align_style_from_io();
239 void setup_destructive_playlist ();
240 void use_destructive_playlist ();
242 void adjust_playback_buffering ();
243 void adjust_capture_buffering ();
245 void engage_record_enable ();
246 void disengage_record_enable ();
248 // Working buffers for do_refill (butler thread)
249 static void allocate_working_buffers();
250 static void free_working_buffers();
252 static size_t _working_buffers_size;
253 static Sample* _mixdown_buffer;
254 static gain_t* _gain_buffer;
256 std::vector<boost::shared_ptr<AudioFileSource> > capturing_sources;
258 SerializedRCUManager<ChannelList> channels;
262 int _do_refill (Sample *mixdown_buffer, float *gain_buffer);
264 int add_channel_to (boost::shared_ptr<ChannelList>, uint32_t how_many);
265 int remove_channel_from (boost::shared_ptr<ChannelList>, uint32_t how_many);
269 } // namespace ARDOUR
271 #endif /* __ardour_audio_diskstream_h__ */