libardour added.
[ardour.git] / libs / ardour / ardour / 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$
19 */
20
21 #ifndef __ardour_diskstream_h__
22 #define __ardour_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/atomic.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/stateful.h>
46
47 struct tm;
48
49 namespace ARDOUR {
50
51 class AudioEngine;
52 class Send;
53 class Session;
54 class AudioPlaylist;
55 class FileSource;
56 class IO;
57
58 class DiskStream : public Stateful, public sigc::trackable
59 {       
60   public:
61         enum Flag {
62                 Recordable = 0x1,
63                 Hidden = 0x2
64         };
65
66         DiskStream (Session &, const string& name, Flag f = Recordable);
67         DiskStream (Session &, const XMLNode&);
68
69         string name() const { return _name; }
70
71         ARDOUR::IO* io() const { return _io; }
72         void set_io (ARDOUR::IO& io);
73
74         DiskStream& ref() { _refcnt++; return *this; }
75         void unref() { if (_refcnt) _refcnt--; if (_refcnt == 0) delete this; }
76         uint32_t refcnt() const { return _refcnt; }
77
78         float playback_buffer_load() const;
79         float capture_buffer_load() const;
80
81         void set_flag (Flag f) {
82                 _flags |= f;
83         }
84
85         void unset_flag (Flag f) {
86                 _flags &= ~f;
87         }
88
89         AlignStyle alignment_style() const { return _alignment_style; }
90         void set_align_style (AlignStyle);
91         void set_persistent_align_style (AlignStyle);
92
93         bool hidden() const { return _flags & Hidden; }
94         bool recordable() const { return _flags & Recordable; }
95
96         jack_nframes_t roll_delay() const { return _roll_delay; }
97         void set_roll_delay (jack_nframes_t);
98
99         void set_name (string str, void* src);
100
101         string input_source (uint32_t n=0) const {
102                 if (n < channels.size()) {
103                         return channels[n].source ? channels[n].source->name() : "";
104                 } else {
105                         return ""; 
106                 }
107         }
108
109         Port *input_source_port (uint32_t n=0) const { 
110                 if (n < channels.size()) return channels[n].source; return 0; 
111         }
112
113         void set_record_enabled (bool yn, void *src);
114         bool record_enabled() const { return atomic_read (&_record_enabled); }
115         void punch_in ();
116         void punch_out ();
117
118         bool  reversed() const { return _actual_speed < 0.0f; }
119         float speed() const { return _visible_speed; }
120         void set_speed (float);
121
122         float peak_power(uint32_t n=0) { 
123                 float x = channels[n].peak_power;
124                 channels[n].peak_power = 0.0f;
125                 if (x > 0.0f) {
126                         return 20.0f * fast_log10(x);
127                 } else {
128                         return minus_infinity();
129                 }
130         }
131
132         int  use_playlist (AudioPlaylist *);
133         int use_new_playlist ();
134         int use_copy_playlist ();
135
136         void start_scrub (jack_nframes_t where);
137         void end_scrub ();
138
139         Sample *playback_buffer (uint32_t n=0) {
140                 if (n < channels.size())
141                         return channels[n].current_playback_buffer;
142                 return 0;
143         }
144         
145         Sample *capture_buffer (uint32_t n=0) {
146                 if (n < channels.size())
147                         return channels[n].current_capture_buffer;
148                 return 0;
149         }
150
151         AudioPlaylist *playlist () { return _playlist; }
152
153         FileSource *fades_source (uint32_t n=0) {
154                 if (n < channels.size())
155                         return channels[n].fades_source;
156                 return 0;
157         }
158         FileSource *write_source (uint32_t n=0) {
159                 if (n < channels.size())
160                         return channels[n].write_source;
161                 return 0;
162         }
163
164         jack_nframes_t current_capture_start() const { return capture_start_frame; }
165         jack_nframes_t current_capture_end() const { return capture_start_frame + capture_captured; }
166         jack_nframes_t get_capture_start_frame (uint32_t n=0);
167         jack_nframes_t get_captured_frames (uint32_t n=0);
168         
169         uint32_t n_channels() { return _n_channels; }
170
171         int add_channel ();
172         int remove_channel ();
173         
174         static void set_disk_io_chunk_frames (uint32_t n) {
175                 disk_io_chunk_frames = n;
176         }
177
178         static jack_nframes_t disk_io_frames() { return disk_io_chunk_frames; }
179         
180         sigc::signal<void,void*> record_enable_changed;
181         sigc::signal<void>       speed_changed;
182         sigc::signal<void,void*> reverse_changed;
183         sigc::signal<void>       PlaylistChanged;
184         sigc::signal<void>       AlignmentStyleChanged;
185
186         static sigc::signal<void> DiskOverrun;
187         static sigc::signal<void> DiskUnderrun;
188         static sigc::signal<void,DiskStream*> DiskStreamCreated;   // XXX use a ref with sigc2
189         static sigc::signal<void,DiskStream*> CannotRecordNoInput; // XXX use a ref with sigc2
190         static sigc::signal<void,list<Source*>*> DeleteSources;
191
192         /* stateful */
193
194         XMLNode& get_state(void);
195         int set_state(const XMLNode& node);
196
197         void monitor_input (bool);
198
199         jack_nframes_t capture_offset() const { return _capture_offset; }
200         void           set_capture_offset ();
201
202         static void swap_by_ptr (Sample *first, Sample *last) {
203                 while (first < last) {
204                         Sample tmp = *first;
205                         *first++ = *last;
206                         *last-- = tmp;
207                 }
208         }
209
210         static void swap_by_ptr (Sample *first, Sample *last, jack_nframes_t n) {
211                 while (n--) {
212                         Sample tmp = *first;
213                         *first++ = *last;
214                         *last-- = tmp;
215                 }
216         }
217
218         bool slaved() const { return _slaved; }
219         void set_slaved(bool yn) { _slaved = yn; }
220
221         int set_loop (Location *loc);
222         sigc::signal<void,Location *> LoopSet;
223
224         std::list<Region*>& last_capture_regions () {
225                 return _last_capture_regions;
226         }
227
228         void handle_input_change (IOChange, void *src);
229
230         id_t id() const { return _id; }
231
232         XMLNode* deprecated_io_node;
233
234   protected:
235         friend class Session;
236
237         /* the Session is the only point of access for these
238            because they require that the Session is "inactive"
239            while they are called.
240         */
241
242         void set_pending_overwrite (bool);
243         int  overwrite_existing_buffers ();
244         void reverse_scrub_buffer (bool to_forward);
245         void set_block_size (jack_nframes_t);
246         int  internal_playback_seek (jack_nframes_t distance);
247         int  can_internal_playback_seek (jack_nframes_t distance);
248         void reset_write_sources (bool);
249         void non_realtime_input_change ();
250
251         uint32_t read_data_count() const { return _read_data_count; }
252         uint32_t write_data_count() const { return _write_data_count; }
253
254   protected:
255         friend class Auditioner;
256         int  seek (jack_nframes_t which_sample, bool complete_refill = false);
257
258   protected:
259         friend class AudioTrack;
260
261         void prepare ();
262         int  process (jack_nframes_t transport_frame, jack_nframes_t nframes, jack_nframes_t offset, bool can_record, bool rec_monitors_input);
263         bool commit  (jack_nframes_t nframes);
264         void recover (); /* called if commit will not be called, but process was */
265
266   private:
267
268         /* use unref() to destroy a diskstream */
269
270         ~DiskStream();
271
272         struct ChannelInfo {
273
274                 Sample     *playback_wrap_buffer;
275                 Sample     *capture_wrap_buffer;
276                 Sample     *speed_buffer;
277
278                 float       peak_power;
279
280                 FileSource   *fades_source;
281                 FileSource   *write_source;
282
283                 Port         *source;
284                 Sample       *current_capture_buffer;
285                 Sample       *current_playback_buffer;
286
287                 RingBufferNPT<Sample> *playback_buf;
288                 RingBufferNPT<Sample> *capture_buf;
289
290                 Sample* scrub_buffer;
291                 Sample* scrub_forward_buffer;
292                 Sample* scrub_reverse_buffer;
293
294                 RingBufferNPT<Sample>::rw_vector playback_vector;
295                 RingBufferNPT<Sample>::rw_vector capture_vector;
296         };
297
298         typedef vector<ChannelInfo> ChannelList;
299
300         string            _name;
301         ARDOUR::Session&  _session;
302         ARDOUR::IO*       _io;
303         ChannelList        channels;
304         uint32_t      _n_channels;
305         id_t              _id;
306
307         atomic_t                 _record_enabled;
308         bool                      rec_monitoring_off_for_roll;
309         AudioPlaylist*           _playlist;
310         float                    _visible_speed;
311         float                    _actual_speed;
312         /* items needed for speed change logic */
313         bool                     _buffer_reallocation_required;
314         bool                     _seek_required;
315         
316         bool                      force_refill;
317         jack_nframes_t            capture_start_frame;
318         jack_nframes_t            capture_captured;
319         bool                      was_recording;
320         jack_nframes_t            adjust_capture_position;
321         jack_nframes_t           _capture_offset;
322         jack_nframes_t           _roll_delay;
323         jack_nframes_t            first_recordable_frame;
324         jack_nframes_t            last_recordable_frame;
325         int                       last_possibly_recording;
326         AlignStyle               _alignment_style;
327         bool                     _scrubbing;
328         bool                     _slaved;
329         bool                     _processed;
330         Location*                 loop_location;
331         jack_nframes_t            overwrite_frame;
332         off_t                     overwrite_offset;
333         bool                      pending_overwrite;
334         bool                      overwrite_queued;
335         IOChange                  input_change_pending;
336         jack_nframes_t            wrap_buffer_size;
337         jack_nframes_t            speed_buffer_size;
338
339         uint64_t                  last_phase;
340         uint64_t                  phi;
341         
342         jack_nframes_t            file_frame;           
343         jack_nframes_t            playback_sample;
344         jack_nframes_t            playback_distance;
345
346         uint32_t                 _read_data_count;
347         uint32_t                 _write_data_count;
348
349         bool                      in_set_state;
350         AlignStyle               _persistent_alignment_style;
351         bool                      first_input_change;
352
353         PBD::NonBlockingLock  state_lock;
354
355         jack_nframes_t scrub_start;
356         jack_nframes_t scrub_buffer_size;
357         jack_nframes_t scrub_offset;
358         uint32_t _refcnt;
359
360         sigc::connection ports_created_c;
361         sigc::connection plmod_connection;
362         sigc::connection plstate_connection;
363         sigc::connection plgone_connection;
364
365         /* the two central butler operations */
366
367         int do_flush (bool force = false);
368         int do_refill (Sample *mixdown_buffer, float *gain_buffer);
369
370         int read (Sample* buf, Sample* mixdown_buffer, float* gain_buffer, jack_nframes_t& start, jack_nframes_t cnt, 
371                   ChannelInfo& channel_info, int channel, bool reversed);
372
373         uint32_t i_am_the_modifier;
374         
375         /* XXX fix this redundancy ... */
376
377         void playlist_changed (Change);
378         void playlist_modified ();
379         void playlist_deleted (Playlist*);
380         void session_controls_changed (Session::ControlType);
381
382         void finish_capture (bool rec_monitors_input);
383         void clean_up_capture (struct tm&, time_t, bool abort);
384         void transport_stopped (struct tm&, time_t, bool abort);
385
386         struct CaptureInfo {
387             uint32_t start;
388             uint32_t frames;
389         };
390
391         vector<CaptureInfo*> capture_info;
392         PBD::Lock  capture_info_lock;
393         
394         void init (Flag);
395
396         void init_channel (ChannelInfo &chan);
397         void destroy_channel (ChannelInfo &chan);
398         
399         static jack_nframes_t disk_io_chunk_frames;
400
401         int use_new_write_source (uint32_t n=0);
402         int use_new_fade_source (uint32_t n=0);
403
404         int find_and_use_playlist (const string&);
405
406         void allocate_temporary_buffers ();
407
408         unsigned char _flags;
409
410         int  create_input_port ();
411         int  connect_input_port ();
412         int  seek_unlocked (jack_nframes_t which_sample);
413
414         int ports_created ();
415
416         bool realtime_set_speed (float, bool global_change);
417         void non_realtime_set_speed ();
418
419         std::list<Region*> _last_capture_regions;
420         std::vector<FileSource*> capturing_sources;
421         int use_pending_capture_data (XMLNode& node);
422
423         void get_input_sources ();
424         
425         void check_record_status (jack_nframes_t transport_frame, jack_nframes_t nframes, bool can_record);
426
427         void set_align_style_from_io();
428
429 };
430
431 }; /* namespace ARDOUR */
432
433 #endif /* __ardour_diskstream_h__ */