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