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 LIBARDOUR_API 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 void set_record_safe (bool yn);
77 #ifdef XXX_OLD_DESTRUCTIVE_API_XXX
78 int set_destructive (bool yn);
79 int set_non_layered (bool yn);
80 bool can_become_destructive (bool& requires_bounce) const;
83 boost::shared_ptr<AudioPlaylist> audio_playlist () { return boost::dynamic_pointer_cast<AudioPlaylist>(_playlist); }
85 int use_playlist (boost::shared_ptr<Playlist>);
86 int use_new_playlist ();
87 int use_copy_playlist ();
89 Sample *playback_buffer (uint32_t n = 0) {
90 boost::shared_ptr<ChannelList> c = channels.reader();
92 return (*c)[n]->current_playback_buffer;
96 Sample *capture_buffer (uint32_t n = 0) {
97 boost::shared_ptr<ChannelList> c = channels.reader();
99 return (*c)[n]->current_capture_buffer;
103 boost::shared_ptr<AudioFileSource> write_source (uint32_t n=0) {
104 boost::shared_ptr<ChannelList> c = channels.reader();
106 return (*c)[n]->write_source;
107 return boost::shared_ptr<AudioFileSource>();
110 int add_channel (uint32_t how_many);
111 int remove_channel (uint32_t how_many);
113 bool set_name (std::string const &);
114 bool set_write_source_name (const std::string& str);
118 XMLNode& get_state(void);
119 int set_state(const XMLNode& node, int version);
121 void request_input_monitoring (bool);
123 static void swap_by_ptr (Sample *first, Sample *last) {
124 while (first < last) {
133 friend class Session;
135 /* the Session is the only point of access for these
136 because they require that the Session is "inactive"
137 while they are called.
140 void set_pending_overwrite(bool);
141 int overwrite_existing_buffers ();
142 void set_block_size (pframes_t);
143 int internal_playback_seek (framecnt_t distance);
144 int can_internal_playback_seek (framecnt_t distance);
145 void reset_write_sources (bool, bool force = false);
146 void non_realtime_input_change ();
147 void non_realtime_locate (framepos_t location);
150 friend class Auditioner;
151 friend class AudioTrack;
152 int seek (framepos_t which_sample, bool complete_refill = false);
154 int process (BufferSet&, framepos_t transport_frame, pframes_t nframes, framecnt_t &, bool need_disk_signal);
155 frameoffset_t calculate_playback_distance (pframes_t nframes);
156 bool commit (framecnt_t);
159 struct ChannelSource {
162 bool is_physical () const;
163 void request_input_monitoring (bool) const;
166 /** Information about one of our channels */
167 struct ChannelInfo : public boost::noncopyable {
169 ChannelInfo (framecnt_t playback_buffer_size,
170 framecnt_t capture_buffer_size,
171 framecnt_t speed_buffer_size,
172 framecnt_t wrap_buffer_size);
175 Sample *playback_wrap_buffer;
176 Sample *capture_wrap_buffer;
177 Sample *speed_buffer;
179 boost::shared_ptr<AudioFileSource> write_source;
181 /** Information about the Port that our audio data comes from */
182 ChannelSource source;
184 Sample *current_capture_buffer;
185 Sample *current_playback_buffer;
187 /** A ringbuffer for data to be played back, written to in the
188 butler thread, read from in the process thread.
190 PBD::RingBufferNPT<Sample> *playback_buf;
191 PBD::RingBufferNPT<Sample> *capture_buf;
193 Sample* scrub_buffer;
194 Sample* scrub_forward_buffer;
195 Sample* scrub_reverse_buffer;
197 PBD::RingBufferNPT<Sample>::rw_vector playback_vector;
198 PBD::RingBufferNPT<Sample>::rw_vector capture_vector;
200 PBD::RingBufferNPT<CaptureTransition> * capture_transition_buf;
201 // the following are used in the butler thread only
202 framecnt_t curr_capture_cnt;
204 void resize_playback (framecnt_t);
205 void resize_capture (framecnt_t);
208 typedef std::vector<ChannelInfo*> ChannelList;
210 CubicInterpolation interpolation;
212 /* The two central butler operations */
213 int do_flush (RunContext context, bool force = false);
214 int do_refill () { return _do_refill(_mixdown_buffer, _gain_buffer, 0); }
217 int read (Sample* buf, Sample* mixdown_buffer, float* gain_buffer,
218 framepos_t& start, framecnt_t cnt,
219 int channel, bool reversed);
221 void finish_capture (boost::shared_ptr<ChannelList>);
222 void transport_stopped_wallclock (struct tm&, time_t, bool abort);
223 void transport_looped (framepos_t transport_frame);
227 void init_channel (ChannelInfo &chan);
228 void destroy_channel (ChannelInfo &chan);
230 int use_new_write_source (uint32_t n=0);
232 int find_and_use_playlist (const std::string &);
234 void allocate_temporary_buffers ();
236 int use_pending_capture_data (XMLNode& node);
238 void get_input_sources ();
239 void prepare_record_status(framepos_t capture_start_frame);
240 void set_align_style_from_io();
241 void setup_destructive_playlist ();
242 void use_destructive_playlist ();
244 void adjust_playback_buffering ();
245 void adjust_capture_buffering ();
247 bool prep_record_enable ();
248 bool prep_record_disable ();
250 // Working buffers for do_refill (butler thread)
251 static void allocate_working_buffers();
252 static void free_working_buffers();
254 static Sample* _mixdown_buffer;
255 static gain_t* _gain_buffer;
257 std::vector<boost::shared_ptr<AudioFileSource> > capturing_sources;
259 SerializedRCUManager<ChannelList> channels;
262 int _do_refill_with_alloc (bool one_chunk_only);
266 int _do_refill (Sample *mixdown_buffer, float *gain_buffer, framecnt_t fill_level);
268 int add_channel_to (boost::shared_ptr<ChannelList>, uint32_t how_many);
269 int remove_channel_from (boost::shared_ptr<ChannelList>, uint32_t how_many);
273 } // namespace ARDOUR
275 #endif /* __ardour_audio_diskstream_h__ */