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