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