ardour
audio_diskstream.h
Go to the documentation of this file.
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 
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.name;
70  } else {
71  return "";
72  }
73  }
74 
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;
79 
81 
82  int use_playlist (boost::shared_ptr<Playlist>);
83  int use_new_playlist ();
84  int use_copy_playlist ();
85 
86  Sample *playback_buffer (uint32_t n = 0) {
87  boost::shared_ptr<ChannelList> c = channels.reader();
88  if (n < c->size())
89  return (*c)[n]->current_playback_buffer;
90  return 0;
91  }
92 
93  Sample *capture_buffer (uint32_t n = 0) {
94  boost::shared_ptr<ChannelList> c = channels.reader();
95  if (n < c->size())
96  return (*c)[n]->current_capture_buffer;
97  return 0;
98  }
99 
101  boost::shared_ptr<ChannelList> c = channels.reader();
102  if (n < c->size())
103  return (*c)[n]->write_source;
105  }
106 
107  int add_channel (uint32_t how_many);
108  int remove_channel (uint32_t how_many);
109 
110  bool set_name (std::string const &);
111  bool set_write_source_name (const std::string& str);
112 
113  /* stateful */
114 
115  XMLNode& get_state(void);
116  int set_state(const XMLNode& node, int version);
117 
118  void request_input_monitoring (bool);
119 
120  static void swap_by_ptr (Sample *first, Sample *last) {
121  while (first < last) {
122  Sample tmp = *first;
123  *first++ = *last;
124  *last-- = tmp;
125  }
126  }
127 
128 
129  protected:
130  friend class Session;
131 
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.
135  */
136 
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  void reset_write_sources (bool, bool force = false);
143  void non_realtime_input_change ();
144  void non_realtime_locate (framepos_t location);
145 
146  protected:
147  friend class Auditioner;
148  friend class AudioTrack;
149  int seek (framepos_t which_sample, bool complete_refill = false);
150 
151  int process (BufferSet&, framepos_t transport_frame, pframes_t nframes, framecnt_t &, bool need_disk_signal);
152  frameoffset_t calculate_playback_distance (pframes_t nframes);
153  bool commit (framecnt_t);
154 
155  private:
156  struct ChannelSource {
157  std::string name;
158 
159  bool is_physical () const;
160  void request_input_monitoring (bool) const;
161  };
162 
164  struct ChannelInfo : public boost::noncopyable {
165 
166  ChannelInfo (framecnt_t playback_buffer_size,
167  framecnt_t capture_buffer_size,
168  framecnt_t speed_buffer_size,
169  framecnt_t wrap_buffer_size);
170  ~ChannelInfo ();
171 
175 
177 
180 
183 
189 
193 
196 
198  // the following are used in the butler thread only
200 
201  void resize_playback (framecnt_t);
202  void resize_capture (framecnt_t);
203  };
204 
205  typedef std::vector<ChannelInfo*> ChannelList;
206 
208 
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); }
212 
213  int do_refill_with_alloc ();
214 
215  int read (Sample* buf, Sample* mixdown_buffer, float* gain_buffer,
217  int channel, bool reversed);
218 
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);
222 
223  void init ();
224 
225  void init_channel (ChannelInfo &chan);
226  void destroy_channel (ChannelInfo &chan);
227 
228  int use_new_write_source (uint32_t n=0);
229 
230  int find_and_use_playlist (const std::string &);
231 
232  void allocate_temporary_buffers ();
233 
234  int use_pending_capture_data (XMLNode& node);
235 
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 ();
241 
242  void adjust_playback_buffering ();
243  void adjust_capture_buffering ();
244 
245  bool prep_record_enable ();
246  bool prep_record_disable ();
247 
248  // Working buffers for do_refill (butler thread)
249  static void allocate_working_buffers();
250  static void free_working_buffers();
251 
252  static size_t _working_buffers_size;
255 
256  std::vector<boost::shared_ptr<AudioFileSource> > capturing_sources;
257 
259 
260  /* really */
261  private:
262  int _do_refill (Sample *mixdown_buffer, float *gain_buffer);
263 
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);
266 
267 };
268 
269 } // namespace ARDOUR
270 
271 #endif /* __ardour_audio_diskstream_h__ */
PBD::RingBufferNPT< Sample > * capture_buf
shared_ptr< T > dynamic_pointer_cast(shared_ptr< U > const &r)
Definition: shared_ptr.hpp:396
static Sample * _mixdown_buffer
uint32_t pframes_t
Definition: types.h:61
tuple f
Definition: signals.py:35
PBD::RingBufferNPT< CaptureTransition > * capture_transition_buf
float gain_t
Definition: types.h:58
RunContext
Definition: types.h:492
static size_t _working_buffers_size
LIBARDOUR_API PBD::PropertyDescriptor< framepos_t > start
Definition: region.cc:63
PBD::RingBufferNPT< Sample >::rw_vector capture_vector
boost::shared_ptr< AudioFileSource > write_source(uint32_t n=0)
std::vector< boost::shared_ptr< AudioFileSource > > capturing_sources
CubicInterpolation interpolation
PBD::RingBufferNPT< Sample > * playback_buf
int64_t framecnt_t
Definition: types.h:76
float Sample
Definition: types.h:54
Definition: amp.h:29
Sample * playback_buffer(uint32_t n=0)
std::vector< ChannelInfo * > ChannelList
Sample * capture_buffer(uint32_t n=0)
int64_t framepos_t
Definition: types.h:66
int64_t frameoffset_t
Definition: types.h:71
SerializedRCUManager< ChannelList > channels
#define LIBARDOUR_API
boost::shared_ptr< AudioFileSource > write_source
std::string input_source(uint32_t n=0) const
const char * name
Definition: xml++.h:95
LIBARDOUR_API uint64_t AudioEngine
Definition: session.h:982
boost::shared_ptr< AudioPlaylist > audio_playlist()
PBD::RingBufferNPT< Sample >::rw_vector playback_vector
LIBARDOUR_API bool init(bool with_vst, bool try_optimization, const char *localedir)
Definition: globals.cc:376
static gain_t * _gain_buffer
static void swap_by_ptr(Sample *first, Sample *last)