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__
22 #include <sigc++/signal.h>
32 #include <pbd/fastlog.h>
33 #include <pbd/ringbufferNPT.h>
34 #include <pbd/stateful.h>
37 #include <ardour/ardour.h>
38 #include <ardour/configuration.h>
39 #include <ardour/session.h>
40 #include <ardour/route_group.h>
41 #include <ardour/route.h>
42 #include <ardour/port.h>
43 #include <ardour/utils.h>
44 #include <ardour/diskstream.h>
45 #include <ardour/audioplaylist.h>
55 class AudioFileSource;
58 class AudioDiskstream : public Diskstream
61 AudioDiskstream (Session &, const string& name, Diskstream::Flag f = Recordable);
62 AudioDiskstream (Session &, const XMLNode&);
65 float playback_buffer_load() const;
66 float capture_buffer_load() const;
68 string input_source (uint32_t n=0) const {
69 boost::shared_ptr<ChannelList> c = channels.reader();
71 return (*c)[n]->source ? (*c)[n]->source->name() : "";
77 Port *input_source_port (uint32_t n=0) const {
78 boost::shared_ptr<ChannelList> c = channels.reader();
79 if (n < c->size()) return (*c)[n]->source; return 0;
82 void set_record_enabled (bool yn);
83 int set_destructive (bool yn);
84 bool can_become_destructive (bool& requires_bounce) const;
86 float peak_power(uint32_t n = 0) {
87 boost::shared_ptr<ChannelList> c = channels.reader();
88 ChannelInfo* chaninfo = (*c)[n];
89 float x = chaninfo->peak_power;
90 chaninfo->peak_power = 0.0f;
92 return 20.0f * fast_log10(x);
94 return minus_infinity();
98 boost::shared_ptr<AudioPlaylist> audio_playlist () { return boost::dynamic_pointer_cast<AudioPlaylist>(_playlist); }
100 int use_playlist (boost::shared_ptr<Playlist>);
101 int use_new_playlist ();
102 int use_copy_playlist ();
104 Sample *playback_buffer (uint32_t n = 0) {
105 boost::shared_ptr<ChannelList> c = channels.reader();
107 return (*c)[n]->current_playback_buffer;
111 Sample *capture_buffer (uint32_t n = 0) {
112 boost::shared_ptr<ChannelList> c = channels.reader();
114 return (*c)[n]->current_capture_buffer;
118 boost::shared_ptr<AudioFileSource> write_source (uint32_t n=0) {
119 boost::shared_ptr<ChannelList> c = channels.reader();
121 return (*c)[n]->write_source;
122 return boost::shared_ptr<AudioFileSource>();
125 int add_channel (uint32_t how_many);
126 int remove_channel (uint32_t how_many);
130 XMLNode& get_state(void);
131 int set_state(const XMLNode& node);
133 void monitor_input (bool);
135 static void swap_by_ptr (Sample *first, Sample *last) {
136 while (first < last) {
143 static void swap_by_ptr (Sample *first, Sample *last, nframes_t n) {
151 XMLNode* deprecated_io_node;
154 friend class Session;
156 /* the Session is the only point of access for these
157 because they require that the Session is "inactive"
158 while they are called.
161 void set_pending_overwrite(bool);
162 int overwrite_existing_buffers ();
163 void set_block_size (nframes_t);
164 int internal_playback_seek (nframes_t distance);
165 int can_internal_playback_seek (nframes_t distance);
166 int rename_write_sources ();
167 void reset_write_sources (bool, bool force = false);
168 void non_realtime_input_change ();
169 void non_realtime_locate (nframes_t location);
172 friend class Auditioner;
173 int seek (nframes_t which_sample, bool complete_refill = false);
176 friend class AudioTrack;
178 int process (nframes_t transport_frame, nframes_t nframes, nframes_t offset, bool can_record, bool rec_monitors_input);
179 bool commit (nframes_t nframes);
185 ChannelInfo (nframes_t buffer_size, nframes_t speed_buffer_size, nframes_t wrap_buffer_size);
188 Sample *playback_wrap_buffer;
189 Sample *capture_wrap_buffer;
190 Sample *speed_buffer;
194 boost::shared_ptr<AudioFileSource> fades_source;
195 boost::shared_ptr<AudioFileSource> write_source;
197 /// the Port that our audio data comes from
199 Sample *current_capture_buffer;
200 Sample *current_playback_buffer;
202 RingBufferNPT<Sample> *playback_buf;
203 RingBufferNPT<Sample> *capture_buf;
205 Sample* scrub_buffer;
206 Sample* scrub_forward_buffer;
207 Sample* scrub_reverse_buffer;
209 RingBufferNPT<Sample>::rw_vector playback_vector;
210 RingBufferNPT<Sample>::rw_vector capture_vector;
212 RingBufferNPT<CaptureTransition> * capture_transition_buf;
213 // the following are used in the butler thread only
214 nframes_t curr_capture_cnt;
217 typedef std::vector<ChannelInfo*> ChannelList;
219 /* The two central butler operations */
220 int do_flush (Session::RunContext context, bool force = false);
221 int do_refill () { return _do_refill(_mixdown_buffer, _gain_buffer); }
223 int do_refill_with_alloc ();
225 int read (Sample* buf, Sample* mixdown_buffer, float* gain_buffer,
226 nframes_t& start, nframes_t cnt,
227 ChannelInfo* channel_info, int channel, bool reversed);
229 void finish_capture (bool rec_monitors_input, boost::shared_ptr<ChannelList>);
230 void transport_stopped (struct tm&, time_t, bool abort);
231 void transport_looped (nframes_t transport_frame);
233 void init (Diskstream::Flag);
235 void init_channel (ChannelInfo &chan);
236 void destroy_channel (ChannelInfo &chan);
238 int use_new_write_source (uint32_t n=0);
240 int find_and_use_playlist (const string&);
242 void allocate_temporary_buffers ();
244 int use_pending_capture_data (XMLNode& node);
246 void get_input_sources ();
247 void check_record_status (nframes_t transport_frame, nframes_t nframes, bool can_record);
248 void set_align_style_from_io();
249 void setup_destructive_playlist ();
250 void use_destructive_playlist ();
252 void engage_record_enable ();
253 void disengage_record_enable ();
255 // Working buffers for do_refill (butler thread)
256 static void allocate_working_buffers();
257 static void free_working_buffers();
259 static size_t _working_buffers_size;
260 static Sample* _mixdown_buffer;
261 static gain_t* _gain_buffer;
263 std::vector<boost::shared_ptr<AudioFileSource> > capturing_sources;
265 SerializedRCUManager<ChannelList> channels;
269 int _do_refill (Sample *mixdown_buffer, float *gain_buffer);
271 int add_channel_to (boost::shared_ptr<ChannelList>, uint32_t how_many);
272 int remove_channel_from (boost::shared_ptr<ChannelList>, uint32_t how_many);
276 } // namespace ARDOUR
278 #endif /* __ardour_audio_diskstream_h__ */