the big Route structure refactor. !!!! THIS WILL ***NOT LOAD*** PRIOR 3.0 or 2.X...
[ardour.git] / libs / ardour / ardour / audio_diskstream.h
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 #include <sigc++/signal.h>
23
24 #include <cmath>
25 #include <string>
26 #include <queue>
27 #include <map>
28 #include <vector>
29
30 #include <time.h>
31
32 #include "pbd/fastlog.h"
33 #include "pbd/ringbufferNPT.h"
34 #include "pbd/stateful.h" 
35 #include "pbd/rcu.h" 
36
37 #include "ardour/ardour.h"
38 #include "ardour/utils.h"
39 #include "ardour/diskstream.h"
40 #include "ardour/audioplaylist.h"
41 #include "ardour/port.h"
42
43 struct tm;
44
45 namespace ARDOUR {
46
47 class AudioEngine;
48 class Send;
49 class Session;
50 class AudioPlaylist;
51 class AudioFileSource;
52 class IO;
53
54 class AudioDiskstream : public Diskstream
55 {       
56   public:
57         AudioDiskstream (Session &, const std::string& name, Diskstream::Flag f = Recordable);
58         AudioDiskstream (Session &, const XMLNode&);
59         ~AudioDiskstream();
60
61         float playback_buffer_load() const;
62         float capture_buffer_load() const;
63
64         std::string input_source (uint32_t n=0) const {
65                 boost::shared_ptr<ChannelList> c = channels.reader();
66                 if (n < c->size()) {
67                         return (*c)[n]->source ? (*c)[n]->source->name() : "";
68                 } else {
69                         return ""; 
70                 }
71         }
72
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; 
76         }
77
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;
82
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;
88                 if (x > 0.0f) {
89                         return 20.0f * fast_log10(x);
90                 } else {
91                         return minus_infinity();
92                 }
93         }
94         
95         boost::shared_ptr<AudioPlaylist> audio_playlist () { return boost::dynamic_pointer_cast<AudioPlaylist>(_playlist); }
96
97         int use_playlist (boost::shared_ptr<Playlist>);
98         int use_new_playlist ();
99         int use_copy_playlist ();
100
101         Sample *playback_buffer (uint32_t n = 0) {
102                 boost::shared_ptr<ChannelList> c = channels.reader();
103                 if (n < c->size())
104                         return (*c)[n]->current_playback_buffer;
105                 return 0;
106         }
107         
108         Sample *capture_buffer (uint32_t n = 0) {
109                 boost::shared_ptr<ChannelList> c = channels.reader();
110                 if (n < c->size())
111                         return (*c)[n]->current_capture_buffer;
112                 return 0;
113         }
114
115         boost::shared_ptr<AudioFileSource> write_source (uint32_t n=0) {
116                 boost::shared_ptr<ChannelList> c = channels.reader();
117                 if (n < c->size())
118                         return (*c)[n]->write_source;
119                 return boost::shared_ptr<AudioFileSource>();
120         }
121
122         int add_channel (uint32_t how_many);
123         int remove_channel (uint32_t how_many);
124         
125         /* stateful */
126
127         XMLNode& get_state(void);
128         int      set_state(const XMLNode& node);
129
130         void monitor_input (bool);
131
132         static void swap_by_ptr (Sample *first, Sample *last) {
133                 while (first < last) {
134                         Sample tmp = *first;
135                         *first++ = *last;
136                         *last-- = tmp;
137                 }
138         }
139
140         static void swap_by_ptr (Sample *first, Sample *last, nframes_t n) {
141                 while (n--) {
142                         Sample tmp = *first;
143                         *first++ = *last;
144                         *last-- = tmp;
145                 }
146         }
147
148         XMLNode* deprecated_io_node;
149
150         /**
151          * Calculate the playback distance during varispeed playback.
152          * Important for Session::process to know exactly, how many frames
153          * were passed by.
154          */
155         static nframes_t calculate_varispeed_playback_distance(
156                         nframes_t nframes, 
157                         uint64_t& the_last_phase, 
158                         uint64_t& the_phi,
159                         uint64_t& the_target_phi)
160         {
161                 // calculate playback distance in the same way 
162                 // as in AudioDiskstream::process_varispeed_playback
163                 uint64_t    phase = the_last_phase;
164                 int64_t     phi_delta;
165                 nframes_t   i = 0;
166                 
167                 const int64_t fractional_part_mask  = 0xFFFFFF;
168                 const Sample  binary_scaling_factor = 16777216.0f;
169
170                 if (the_phi != the_target_phi) {
171                         phi_delta = ((int64_t)(the_target_phi - the_phi)) / nframes;
172                 } else {
173                         phi_delta = 0;
174                 }
175
176                 Sample fractional_phase_part;
177
178                 i = 0;
179                 phase = the_last_phase;
180
181                 for (nframes_t outsample = 0; outsample < nframes; ++outsample) {
182                         i = phase >> 24;
183                         fractional_phase_part = (phase & fractional_part_mask) / binary_scaling_factor;
184                         phase += the_phi + phi_delta;
185                 }
186
187                 the_last_phase = (phase & fractional_part_mask);
188                 the_phi = the_target_phi;
189                 return i; // + 1;
190         }
191         
192   protected:
193         friend class Session;
194
195         /* the Session is the only point of access for these
196            because they require that the Session is "inactive"
197            while they are called.
198         */
199
200         void set_pending_overwrite(bool);
201         int  overwrite_existing_buffers ();
202         void set_block_size (nframes_t);
203         int  internal_playback_seek (nframes_t distance);
204         int  can_internal_playback_seek (nframes_t distance);
205         int  rename_write_sources ();
206         void reset_write_sources (bool, bool force = false);
207         void non_realtime_input_change ();
208         void non_realtime_locate (nframes_t location);
209
210   protected:
211         friend class Auditioner;
212         int  seek (nframes_t which_sample, bool complete_refill = false);
213
214   protected:
215         friend class AudioTrack;
216
217         int  process (nframes_t transport_frame, nframes_t nframes, bool can_record, bool rec_monitors_input);
218         bool commit  (nframes_t nframes);
219
220   private:
221
222         struct ChannelInfo {
223             
224             ChannelInfo (nframes_t buffer_size, nframes_t speed_buffer_size, nframes_t wrap_buffer_size);
225             ~ChannelInfo ();
226
227             Sample     *playback_wrap_buffer;
228             Sample     *capture_wrap_buffer;
229             Sample     *speed_buffer;
230             
231             float       peak_power;
232             
233             boost::shared_ptr<AudioFileSource> fades_source;
234             boost::shared_ptr<AudioFileSource> write_source;
235
236             /// the Port that our audio data comes from
237             Port         *source;
238             Sample       *current_capture_buffer;
239             Sample       *current_playback_buffer;
240             
241             RingBufferNPT<Sample> *playback_buf;
242             RingBufferNPT<Sample> *capture_buf;
243             
244             Sample* scrub_buffer;
245             Sample* scrub_forward_buffer;
246             Sample* scrub_reverse_buffer;
247             
248             RingBufferNPT<Sample>::rw_vector playback_vector;
249             RingBufferNPT<Sample>::rw_vector capture_vector;
250             
251             RingBufferNPT<CaptureTransition> * capture_transition_buf;
252             // the following are used in the butler thread only
253             nframes_t                     curr_capture_cnt;
254         };
255
256         typedef std::vector<ChannelInfo*> ChannelList;
257
258         void process_varispeed_playback(nframes_t nframes, boost::shared_ptr<ChannelList> c);
259         
260         /* The two central butler operations */
261         int do_flush (RunContext context, bool force = false);
262         int do_refill () { return _do_refill(_mixdown_buffer, _gain_buffer); }
263         
264         int do_refill_with_alloc ();
265
266         int read (Sample* buf, Sample* mixdown_buffer, float* gain_buffer,
267                   nframes_t& start, nframes_t cnt, 
268                   ChannelInfo* channel_info, int channel, bool reversed);
269
270
271         void finish_capture (bool rec_monitors_input, boost::shared_ptr<ChannelList>);
272         void transport_stopped (struct tm&, time_t, bool abort);
273         void transport_looped (nframes_t transport_frame);
274
275         void init (Diskstream::Flag);
276
277         void init_channel (ChannelInfo &chan);
278         void destroy_channel (ChannelInfo &chan);
279         
280         int use_new_write_source (uint32_t n=0);
281
282         int find_and_use_playlist (const std::string &);
283
284         void allocate_temporary_buffers ();
285
286         int use_pending_capture_data (XMLNode& node);
287
288         void get_input_sources ();
289         void check_record_status (nframes_t transport_frame, nframes_t nframes, bool can_record);
290         void set_align_style_from_io();
291         void setup_destructive_playlist ();
292         void use_destructive_playlist ();
293
294         void engage_record_enable ();
295         void disengage_record_enable ();
296
297         // Working buffers for do_refill (butler thread)
298         static void allocate_working_buffers();
299         static void free_working_buffers();
300
301         static size_t  _working_buffers_size;
302         static Sample* _mixdown_buffer;
303         static gain_t* _gain_buffer;
304
305         std::vector<boost::shared_ptr<AudioFileSource> > capturing_sources;
306         
307         SerializedRCUManager<ChannelList> channels;
308         
309  /* really */
310   private:
311         int _do_refill (Sample *mixdown_buffer, float *gain_buffer);
312
313         int add_channel_to (boost::shared_ptr<ChannelList>, uint32_t how_many);
314         int remove_channel_from (boost::shared_ptr<ChannelList>, uint32_t how_many);
315
316 };
317
318 } // namespace ARDOUR
319
320 #endif /* __ardour_audio_diskstream_h__ */