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