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 "pbd/fastlog.h"
32 #include "pbd/ringbufferNPT.h"
33 #include "pbd/stateful.h"
36 #include "ardour/ardour.h"
37 #include "ardour/utils.h"
38 #include "ardour/diskstream.h"
39 #include "ardour/audioplaylist.h"
40 #include "ardour/port.h"
41 #include "ardour/interpolation.h"
51 class AudioFileSource;
54 class AudioDiskstream : public Diskstream
57 AudioDiskstream (Session &, const std::string& name, Diskstream::Flag f = Recordable);
58 AudioDiskstream (Session &, const XMLNode&);
61 float playback_buffer_load() const;
62 float capture_buffer_load() const;
64 std::string input_source (uint32_t n=0) const {
65 boost::shared_ptr<ChannelList> c = channels.reader();
67 return (*c)[n]->source ? (*c)[n]->source->name() : "";
73 Port *input_source_port (uint32_t n=0) const {
74 boost::shared_ptr<ChannelList> c = channels.reader();
75 if (n < c->size()) return (*c)[n]->source; return 0;
78 void set_record_enabled (bool yn);
79 int set_destructive (bool yn);
80 int set_non_layered (bool yn);
81 bool can_become_destructive (bool& requires_bounce) const;
83 float peak_power(uint32_t n = 0) {
84 boost::shared_ptr<ChannelList> c = channels.reader();
85 ChannelInfo* chaninfo = (*c)[n];
86 float x = chaninfo->peak_power;
87 chaninfo->peak_power = 0.0f;
89 return 20.0f * fast_log10(x);
91 return minus_infinity();
95 boost::shared_ptr<AudioPlaylist> audio_playlist () { return boost::dynamic_pointer_cast<AudioPlaylist>(_playlist); }
97 int use_playlist (boost::shared_ptr<Playlist>);
98 int use_new_playlist ();
99 int use_copy_playlist ();
101 Sample *playback_buffer (uint32_t n = 0) {
102 boost::shared_ptr<ChannelList> c = channels.reader();
104 return (*c)[n]->current_playback_buffer;
108 Sample *capture_buffer (uint32_t n = 0) {
109 boost::shared_ptr<ChannelList> c = channels.reader();
111 return (*c)[n]->current_capture_buffer;
115 boost::shared_ptr<AudioFileSource> write_source (uint32_t n=0) {
116 boost::shared_ptr<ChannelList> c = channels.reader();
118 return (*c)[n]->write_source;
119 return boost::shared_ptr<AudioFileSource>();
122 int add_channel (uint32_t how_many);
123 int remove_channel (uint32_t how_many);
127 XMLNode& get_state(void);
128 int set_state(const XMLNode& node, int version);
130 void monitor_input (bool);
132 static void swap_by_ptr (Sample *first, Sample *last) {
133 while (first < last) {
140 static void swap_by_ptr (Sample *first, Sample *last, nframes_t n) {
148 CubicInterpolation interpolation;
150 XMLNode* deprecated_io_node;
153 friend class Session;
155 /* the Session is the only point of access for these
156 because they require that the Session is "inactive"
157 while they are called.
160 void set_pending_overwrite(bool);
161 int overwrite_existing_buffers ();
162 void set_block_size (nframes_t);
163 int internal_playback_seek (nframes_t distance);
164 int can_internal_playback_seek (nframes_t distance);
165 int rename_write_sources ();
166 void reset_write_sources (bool, bool force = false);
167 void non_realtime_input_change ();
168 void non_realtime_locate (nframes_t location);
171 friend class Auditioner;
172 int seek (nframes_t which_sample, bool complete_refill = false);
175 friend class AudioTrack;
177 int process (nframes_t transport_frame, nframes_t nframes, bool can_record, bool rec_monitors_input);
178 bool commit (nframes_t nframes);
184 ChannelInfo (nframes_t buffer_size, nframes_t speed_buffer_size, nframes_t wrap_buffer_size);
187 Sample *playback_wrap_buffer;
188 Sample *capture_wrap_buffer;
189 Sample *speed_buffer;
193 boost::shared_ptr<AudioFileSource> fades_source;
194 boost::shared_ptr<AudioFileSource> write_source;
196 /// the Port that our audio data comes from
198 Sample *current_capture_buffer;
199 Sample *current_playback_buffer;
201 RingBufferNPT<Sample> *playback_buf;
202 RingBufferNPT<Sample> *capture_buf;
204 Sample* scrub_buffer;
205 Sample* scrub_forward_buffer;
206 Sample* scrub_reverse_buffer;
208 RingBufferNPT<Sample>::rw_vector playback_vector;
209 RingBufferNPT<Sample>::rw_vector capture_vector;
211 RingBufferNPT<CaptureTransition> * capture_transition_buf;
212 // the following are used in the butler thread only
213 nframes_t curr_capture_cnt;
216 typedef std::vector<ChannelInfo*> ChannelList;
218 void process_varispeed_playback(nframes_t nframes, boost::shared_ptr<ChannelList> c);
220 /* The two central butler operations */
221 int do_flush (RunContext context, bool force = false);
222 int do_refill () { return _do_refill(_mixdown_buffer, _gain_buffer); }
224 int do_refill_with_alloc ();
226 int read (Sample* buf, Sample* mixdown_buffer, float* gain_buffer,
227 nframes_t& start, nframes_t cnt,
228 ChannelInfo* channel_info, int channel, bool reversed);
230 void finish_capture (bool rec_monitors_input, boost::shared_ptr<ChannelList>);
231 void transport_stopped (struct tm&, time_t, bool abort);
232 void transport_looped (nframes_t transport_frame);
234 void init (Diskstream::Flag);
236 void init_channel (ChannelInfo &chan);
237 void destroy_channel (ChannelInfo &chan);
239 int use_new_write_source (uint32_t n=0);
241 int find_and_use_playlist (const std::string &);
243 void allocate_temporary_buffers ();
245 int use_pending_capture_data (XMLNode& node);
247 void get_input_sources ();
248 void prepare_record_status(nframes_t capture_start_frame);
249 void set_align_style_from_io();
250 void setup_destructive_playlist ();
251 void use_destructive_playlist ();
253 void engage_record_enable ();
254 void disengage_record_enable ();
256 // Working buffers for do_refill (butler thread)
257 static void allocate_working_buffers();
258 static void free_working_buffers();
260 static size_t _working_buffers_size;
261 static Sample* _mixdown_buffer;
262 static gain_t* _gain_buffer;
264 std::vector<boost::shared_ptr<AudioFileSource> > capturing_sources;
266 SerializedRCUManager<ChannelList> channels;
270 int _do_refill (Sample *mixdown_buffer, float *gain_buffer);
272 int add_channel_to (boost::shared_ptr<ChannelList>, uint32_t how_many);
273 int remove_channel_from (boost::shared_ptr<ChannelList>, uint32_t how_many);
277 } // namespace ARDOUR
279 #endif /* __ardour_audio_diskstream_h__ */