76b76b106140e28cef60952e2e743d4fc8b73793
[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
36 #include <ardour/ardour.h>
37 #include <ardour/configuration.h>
38 #include <ardour/session.h>
39 #include <ardour/route_group.h>
40 #include <ardour/route.h>
41 #include <ardour/port.h>
42 #include <ardour/utils.h>
43 #include <ardour/diskstream.h>
44 #include <ardour/audioplaylist.h>
45
46 struct tm;
47
48 namespace ARDOUR {
49
50 class AudioEngine;
51 class Send;
52 class Session;
53 class AudioPlaylist;
54 class AudioFileSource;
55 class IO;
56
57 class AudioDiskstream : public Diskstream
58 {       
59   public:
60         AudioDiskstream (Session &, const string& name, Diskstream::Flag f = Recordable);
61         AudioDiskstream (Session &, const XMLNode&);
62         ~AudioDiskstream();
63
64         float playback_buffer_load() const;
65         float capture_buffer_load() const;
66
67         string input_source (uint32_t n=0) const {
68                 if (n < channels.size()) {
69                         return channels[n].source ? channels[n].source->name() : "";
70                 } else {
71                         return ""; 
72                 }
73         }
74
75         Port *input_source_port (uint32_t n=0) const { 
76                 if (n < channels.size()) return channels[n].source; return 0; 
77         }
78
79         void set_record_enabled (bool yn);
80
81         float peak_power(uint32_t n=0) { 
82                 float x = channels[n].peak_power;
83                 channels[n].peak_power = 0.0f;
84                 if (x > 0.0f) {
85                         return 20.0f * fast_log10(x);
86                 } else {
87                         return minus_infinity();
88                 }
89         }
90         
91         AudioPlaylist* audio_playlist () { return dynamic_cast<AudioPlaylist*>(_playlist); }
92
93         int use_playlist (Playlist *);
94         int use_new_playlist ();
95         int use_copy_playlist ();
96
97         Sample *playback_buffer (uint32_t n=0) {
98                 if (n < channels.size())
99                         return channels[n].current_playback_buffer;
100                 return 0;
101         }
102         
103         Sample *capture_buffer (uint32_t n=0) {
104                 if (n < channels.size())
105                         return channels[n].current_capture_buffer;
106                 return 0;
107         }
108
109         boost::shared_ptr<AudioFileSource> write_source (uint32_t n=0) {
110                 if (n < channels.size())
111                         return channels[n].write_source;
112                 return boost::shared_ptr<AudioFileSource>();
113         }
114
115         int add_channel ();
116         int remove_channel ();
117         
118         
119         /* stateful */
120
121         XMLNode& get_state(void);
122         int      set_state(const XMLNode& node);
123
124         void monitor_input (bool);
125
126         static void swap_by_ptr (Sample *first, Sample *last) {
127                 while (first < last) {
128                         Sample tmp = *first;
129                         *first++ = *last;
130                         *last-- = tmp;
131                 }
132         }
133
134         static void swap_by_ptr (Sample *first, Sample *last, nframes_t n) {
135                 while (n--) {
136                         Sample tmp = *first;
137                         *first++ = *last;
138                         *last-- = tmp;
139                 }
140         }
141
142         XMLNode* deprecated_io_node;
143
144   protected:
145         friend class Session;
146
147         /* the Session is the only point of access for these
148            because they require that the Session is "inactive"
149            while they are called.
150         */
151
152         void set_pending_overwrite(bool);
153         int  overwrite_existing_buffers ();
154         void set_block_size (nframes_t);
155         int  internal_playback_seek (nframes_t distance);
156         int  can_internal_playback_seek (nframes_t distance);
157         int  rename_write_sources ();
158         void reset_write_sources (bool, bool force = false);
159         void non_realtime_input_change ();
160
161   protected:
162         friend class Auditioner;
163         int  seek (nframes_t which_sample, bool complete_refill = false);
164
165   protected:
166         friend class AudioTrack;
167
168         int  process (nframes_t transport_frame, nframes_t nframes, nframes_t offset, bool can_record, bool rec_monitors_input);
169         bool commit  (nframes_t nframes);
170
171   private:
172
173         struct ChannelInfo {
174
175                 Sample     *playback_wrap_buffer;
176                 Sample     *capture_wrap_buffer;
177                 Sample     *speed_buffer;
178
179                 float       peak_power;
180             
181                 boost::shared_ptr<AudioFileSource> fades_source;
182                 boost::shared_ptr<AudioFileSource> write_source;
183
184                 Port         *source;
185                 Sample       *current_capture_buffer;
186                 Sample       *current_playback_buffer;
187
188                 RingBufferNPT<Sample> *playback_buf;
189                 RingBufferNPT<Sample> *capture_buf;
190
191                 Sample* scrub_buffer;
192                 Sample* scrub_forward_buffer;
193                 Sample* scrub_reverse_buffer;
194
195                 RingBufferNPT<Sample>::rw_vector playback_vector;
196                 RingBufferNPT<Sample>::rw_vector capture_vector;
197
198                 RingBufferNPT<CaptureTransition> * capture_transition_buf;
199                 // the following are used in the butler thread only
200                 nframes_t                     curr_capture_cnt;
201         };
202
203         /* The two central butler operations */
204         int do_flush (Session::RunContext context, bool force = false);
205         int do_refill () { return _do_refill(_mixdown_buffer, _gain_buffer); }
206         
207         int do_refill_with_alloc();
208
209         int read (Sample* buf, Sample* mixdown_buffer, float* gain_buffer,
210                 nframes_t& start, nframes_t cnt, 
211                 ChannelInfo& channel_info, int channel, bool reversed);
212
213         void finish_capture (bool rec_monitors_input);
214         void transport_stopped (struct tm&, time_t, bool abort);
215
216         void init (Diskstream::Flag);
217
218         void init_channel (ChannelInfo &chan);
219         void destroy_channel (ChannelInfo &chan);
220         
221         int use_new_write_source (uint32_t n=0);
222
223         int find_and_use_playlist (const string&);
224
225         void allocate_temporary_buffers ();
226
227         int use_pending_capture_data (XMLNode& node);
228
229         void get_input_sources ();
230         void check_record_status (nframes_t transport_frame, nframes_t nframes, bool can_record);
231         void set_align_style_from_io();
232         void setup_destructive_playlist ();
233         void use_destructive_playlist ();
234
235         void engage_record_enable ();
236         void disengage_record_enable ();
237
238         // Working buffers for do_refill (butler thread)
239         static void allocate_working_buffers();
240         static void free_working_buffers();
241
242         static size_t  _working_buffers_size;
243         static Sample* _mixdown_buffer;
244         static gain_t* _gain_buffer;
245
246         // Uh, /really/ private? (there should probably be less friends of Diskstream)
247         int _do_refill (Sample *mixdown_buffer, float *gain_buffer);
248         
249         
250         std::vector<boost::shared_ptr<AudioFileSource> > capturing_sources;
251         
252         typedef vector<ChannelInfo> ChannelList;
253         ChannelList channels;
254 };
255
256 } // namespace ARDOUR
257
258 #endif /* __ardour_audio_diskstream_h__ */