Work towards removal of Session's Diskstream list.
[ardour.git] / libs / ardour / ardour / audio_diskstream.h
1 /*
2     Copyright (C) 2000 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     $Id: diskstream.h 579 2006-06-12 19:56:37Z essej $
19 */
20
21 #ifndef __ardour_audio_diskstream_h__
22 #define __ardour_audio_diskstream_h__
23
24 #include <sigc++/signal.h>
25
26 #include <cmath>
27 #include <string>
28 #include <queue>
29 #include <map>
30 #include <vector>
31
32 #include <time.h>
33
34 #include <pbd/fastlog.h>
35 #include <pbd/ringbufferNPT.h>
36 #include <pbd/stateful.h> 
37
38 #include <ardour/ardour.h>
39 #include <ardour/configuration.h>
40 #include <ardour/session.h>
41 #include <ardour/route_group.h>
42 #include <ardour/route.h>
43 #include <ardour/port.h>
44 #include <ardour/utils.h>
45 #include <ardour/diskstream.h>
46 #include <ardour/audioplaylist.h>
47
48 struct tm;
49
50 namespace ARDOUR {
51
52 class AudioEngine;
53 class Send;
54 class Session;
55 class AudioPlaylist;
56 class AudioFileSource;
57 class IO;
58
59 class AudioDiskstream : public Diskstream
60 {       
61   public:
62         AudioDiskstream (Session &, const string& name, Diskstream::Flag f = Recordable);
63         AudioDiskstream (Session &, const XMLNode&);
64
65         void set_io (ARDOUR::IO& io);
66
67         AudioDiskstream& ref() { _refcnt++; return *this; }
68         //void unref() { if (_refcnt) _refcnt--; if (_refcnt == 0) delete this; }
69         //uint32_t refcnt() const { return _refcnt; }
70
71         float playback_buffer_load() const;
72         float capture_buffer_load() const;
73
74         //void set_align_style (AlignStyle);
75         //void set_persistent_align_style (AlignStyle);
76
77         string input_source (uint32_t n=0) const {
78                 if (n < channels.size()) {
79                         return channels[n].source ? channels[n].source->name() : "";
80                 } else {
81                         return ""; 
82                 }
83         }
84
85         Port *input_source_port (uint32_t n=0) const { 
86                 if (n < channels.size()) return channels[n].source; return 0; 
87         }
88
89         void set_record_enabled (bool yn, void *src);
90         //void set_speed (double);
91
92         float peak_power(uint32_t n=0) { 
93                 float x = channels[n].peak_power;
94                 channels[n].peak_power = 0.0f;
95                 if (x > 0.0f) {
96                         return 20.0f * fast_log10(x);
97                 } else {
98                         return minus_infinity();
99                 }
100         }
101
102         int use_playlist (Playlist *);
103         int use_new_playlist ();
104         int use_copy_playlist ();
105
106         void start_scrub (jack_nframes_t where) {} // FIXME?
107         void end_scrub () {} // FIXME?
108
109         Playlist *playlist () { return _playlist; }
110
111         Sample *playback_buffer (uint32_t n=0) {
112                 if (n < channels.size())
113                         return channels[n].current_playback_buffer;
114                 return 0;
115         }
116         
117         Sample *capture_buffer (uint32_t n=0) {
118                 if (n < channels.size())
119                         return channels[n].current_capture_buffer;
120                 return 0;
121         }
122
123         AudioFileSource *write_source (uint32_t n=0) {
124                 if (n < channels.size())
125                         return channels[n].write_source;
126                 return 0;
127         }
128
129         int add_channel ();
130         int remove_channel ();
131         
132         
133         /* stateful */
134
135         XMLNode& get_state(void);
136         int set_state(const XMLNode& node);
137
138         void monitor_input (bool);
139
140         // FIXME: these don't belong here
141         static void swap_by_ptr (Sample *first, Sample *last) {
142                 while (first < last) {
143                         Sample tmp = *first;
144                         *first++ = *last;
145                         *last-- = tmp;
146                 }
147         }
148
149         static void swap_by_ptr (Sample *first, Sample *last, jack_nframes_t n) {
150                 while (n--) {
151                         Sample tmp = *first;
152                         *first++ = *last;
153                         *last-- = tmp;
154                 }
155         }
156
157         //void handle_input_change (IOChange, void *src);
158         
159         //static sigc::signal<void> DiskOverrun;
160         //static sigc::signal<void> DiskUnderrun;
161         //static sigc::signal<void,AudioDiskstream*> AudioDiskstreamCreated;   // XXX use a ref with sigc2
162         static sigc::signal<void,list<AudioFileSource*>*> DeleteSources;
163
164         int set_loop (Location *loc);
165         sigc::signal<void,Location *> LoopSet;
166
167         std::list<Region*>& last_capture_regions () {
168                 return _last_capture_regions;
169         }
170
171         void handle_input_change (IOChange, void *src);
172
173         const PBD::ID& id() const { return _id; }
174
175         XMLNode* deprecated_io_node;
176
177   protected:
178         friend class Session;
179
180         /* the Session is the only point of access for these
181            because they require that the Session is "inactive"
182            while they are called.
183         */
184
185         void set_pending_overwrite(bool);
186         int  overwrite_existing_buffers ();
187         void reverse_scrub_buffer (bool to_forward) {} // FIXME?
188         void set_block_size (jack_nframes_t);
189         int  internal_playback_seek (jack_nframes_t distance);
190         int  can_internal_playback_seek (jack_nframes_t distance);
191         int  rename_write_sources ();
192         void reset_write_sources (bool, bool force = false);
193         void non_realtime_input_change ();
194
195   protected:
196         friend class Auditioner;
197         int  seek (jack_nframes_t which_sample, bool complete_refill = false);
198
199   protected:
200         friend class AudioTrack;
201
202         int  process (jack_nframes_t transport_frame, jack_nframes_t nframes, jack_nframes_t offset, bool can_record, bool rec_monitors_input);
203         bool commit  (jack_nframes_t nframes);
204
205   private:
206
207         /* use unref() to destroy a diskstream */
208         ~AudioDiskstream();
209
210         struct ChannelInfo {
211
212                 Sample     *playback_wrap_buffer;
213                 Sample     *capture_wrap_buffer;
214                 Sample     *speed_buffer;
215
216                 float       peak_power;
217
218                 AudioFileSource   *fades_source;
219                 AudioFileSource   *write_source;
220
221                 Port         *source;
222                 Sample       *current_capture_buffer;
223                 Sample       *current_playback_buffer;
224
225                 RingBufferNPT<Sample> *playback_buf;
226                 RingBufferNPT<Sample> *capture_buf;
227
228                 Sample* scrub_buffer;
229                 Sample* scrub_forward_buffer;
230                 Sample* scrub_reverse_buffer;
231
232                 RingBufferNPT<Sample>::rw_vector playback_vector;
233                 RingBufferNPT<Sample>::rw_vector capture_vector;
234
235                 RingBufferNPT<CaptureTransition> * capture_transition_buf;
236                 // the following are used in the butler thread only
237                 jack_nframes_t                     curr_capture_cnt;
238         };
239
240         typedef vector<ChannelInfo> ChannelList;
241
242         /* the two central butler operations */
243
244         int do_flush (char * workbuf, bool force = false);
245         int do_refill (Sample *mixdown_buffer, float *gain_buffer, char *workbuf);
246         
247         virtual int non_realtime_do_refill() { return do_refill(0, 0, 0); }
248
249         int read (Sample* buf, Sample* mixdown_buffer, float* gain_buffer, char * workbuf, jack_nframes_t& start, jack_nframes_t cnt, 
250                   ChannelInfo& channel_info, int channel, bool reversed);
251
252         /* XXX fix this redundancy ... */
253
254         //void playlist_changed (Change);
255         //void playlist_modified ();
256         void playlist_deleted (Playlist*);
257         void session_controls_changed (Session::ControlType) {} // FIXME?
258
259         void finish_capture (bool rec_monitors_input);
260         void clean_up_capture (struct tm&, time_t, bool abort) {} // FIXME?
261         void transport_stopped (struct tm&, time_t, bool abort);
262
263         struct CaptureInfo {
264             uint32_t start;
265             uint32_t frames;
266         };
267
268         vector<CaptureInfo*> capture_info;
269         Glib::Mutex  capture_info_lock;
270         
271         void init (Diskstream::Flag);
272
273         void init_channel (ChannelInfo &chan);
274         void destroy_channel (ChannelInfo &chan);
275         
276         int use_new_write_source (uint32_t n=0);
277         int use_new_fade_source (uint32_t n=0) { return 0; } // FIXME?
278
279         int find_and_use_playlist (const string&);
280
281         void allocate_temporary_buffers ();
282
283         int  create_input_port () { return 0; } // FIXME?
284         int  connect_input_port () { return 0; } // FIXME?
285         int  seek_unlocked (jack_nframes_t which_sample) { return 0; } // FIXME?
286
287         int ports_created () { return 0; } // FIXME?
288
289         //bool realtime_set_speed (double, bool global_change);
290         void non_realtime_set_speed ();
291
292         std::list<Region*> _last_capture_regions;
293         std::vector<AudioFileSource*> capturing_sources;
294         int use_pending_capture_data (XMLNode& node);
295
296         void get_input_sources ();
297         void check_record_status (jack_nframes_t transport_frame, jack_nframes_t nframes, bool can_record);
298         void set_align_style_from_io();
299         void setup_destructive_playlist ();
300         void use_destructive_playlist ();
301
302         ChannelList    channels;
303         AudioPlaylist* _playlist;
304         void engage_record_enable (void* src);
305         void disengage_record_enable (void* src);
306 };
307
308 }; /* namespace ARDOUR */
309
310 #endif /* __ardour_audio_diskstream_h__ */