fc41d10160d41f996fcd1b29e968ace16402d6ae
[ardour.git] / libs / ardour / ardour / 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
20 #ifndef __ardour_diskstream_h__
21 #define __ardour_diskstream_h__
22
23 #include <string>
24 #include <queue>
25 #include <map>
26 #include <vector>
27 #include <cmath>
28 #include <time.h>
29
30 #include <boost/utility.hpp>
31
32 #include "evoral/types.hpp"
33
34 #include "ardour/ardour.h"
35 #include "ardour/chan_count.h"
36 #include "ardour/session_object.h"
37 #include "ardour/libardour_visibility.h"
38 #include "ardour/types.h"
39 #include "ardour/utils.h"
40 #include "ardour/public_diskstream.h"
41
42 struct tm;
43
44 namespace ARDOUR {
45
46 class IO;
47 class Playlist;
48 class Processor;
49 class Source;
50 class Session;
51 class Track;
52 class Location;
53 class BufferSet;
54
55 /** Parent class for classes which can stream data to and from disk.
56  *  These are used by Tracks to get playback and put recorded data.
57  */
58 class LIBARDOUR_API Diskstream : public SessionObject, public PublicDiskstream
59 {
60   public:
61         enum Flag {
62                 Recordable  = 0x1,
63                 Hidden      = 0x2,
64                 Destructive = 0x4,
65                 NonLayered   = 0x8
66         };
67
68         Diskstream (Session &, const std::string& name, Flag f = Recordable);
69         Diskstream (Session &, const XMLNode&);
70         virtual ~Diskstream();
71
72         virtual bool set_name (const std::string& str);
73         virtual bool set_write_source_name (const std::string& str);
74
75         std::string write_source_name () const {
76                 if (_write_source_name.empty()) {
77                         return name();
78                 } else {
79                         return _write_source_name;
80                 }
81         }
82
83         virtual std::string steal_write_source_name () { return std::string(); }
84
85         boost::shared_ptr<ARDOUR::IO> io() const { return _io; }
86         void set_track (ARDOUR::Track *);
87
88         /** @return A number between 0 and 1, where 0 indicates that the playback buffer
89          *  is dry (ie the disk subsystem could not keep up) and 1 indicates that the
90          *  buffer is full.
91          */
92         virtual float playback_buffer_load() const = 0;
93         virtual float capture_buffer_load() const = 0;
94
95         void set_flag (Flag f)   { _flags = Flag (_flags | f); }
96         void unset_flag (Flag f) { _flags = Flag (_flags & ~f); }
97
98         AlignStyle  alignment_style() const { return _alignment_style; }
99         AlignChoice alignment_choice() const { return _alignment_choice; }
100         void       set_align_style (AlignStyle, bool force=false);
101         void       set_align_choice (AlignChoice a, bool force=false);
102
103         framecnt_t roll_delay() const { return _roll_delay; }
104         void       set_roll_delay (framecnt_t);
105
106         bool         record_enabled() const { return g_atomic_int_get (&_record_enabled); }
107         bool         record_safe () const { return g_atomic_int_get (&_record_safe); }
108         virtual void set_record_enabled (bool yn) = 0;
109         virtual void set_record_safe (bool yn) = 0;
110
111         bool destructive() const { return _flags & Destructive; }
112         virtual int set_destructive (bool /*yn*/) { return -1; }
113         virtual int set_non_layered (bool /*yn*/) { return -1; }
114         virtual bool can_become_destructive (bool& /*requires_bounce*/) const { return false; }
115
116         bool           hidden()      const { return _flags & Hidden; }
117         bool           recordable()  const { return _flags & Recordable; }
118         bool           non_layered()  const { return _flags & NonLayered; }
119         bool           reversed()    const { return _actual_speed < 0.0f; }
120         double         speed()       const { return _visible_speed; }
121
122         virtual void punch_in()  {}
123         virtual void punch_out() {}
124
125         void non_realtime_set_speed ();
126         virtual void non_realtime_locate (framepos_t /*location*/) {};
127         virtual void playlist_modified ();
128
129         boost::shared_ptr<Playlist> playlist () { return _playlist; }
130
131         virtual int use_playlist (boost::shared_ptr<Playlist>);
132         virtual int use_new_playlist () = 0;
133         virtual int use_copy_playlist () = 0;
134
135         /** @return Start position of currently-running capture (in session frames) */
136         framepos_t current_capture_start() const { return capture_start_frame; }
137         framepos_t current_capture_end()   const { return capture_start_frame + capture_captured; }
138         framepos_t get_capture_start_frame (uint32_t n = 0) const;
139         framecnt_t get_captured_frames (uint32_t n = 0) const;
140
141         ChanCount n_channels() { return _n_channels; }
142
143         static framecnt_t disk_read_frames() { return disk_read_chunk_frames; }
144         static framecnt_t disk_write_frames() { return disk_write_chunk_frames; }
145         static void set_disk_read_chunk_frames (framecnt_t n) { disk_read_chunk_frames = n; }
146         static void set_disk_write_chunk_frames (framecnt_t n) { disk_write_chunk_frames = n; }
147         static framecnt_t default_disk_read_chunk_frames ();
148         static framecnt_t default_disk_write_chunk_frames ();
149
150         /* Stateful */
151         virtual XMLNode& get_state(void);
152         virtual int      set_state(const XMLNode&, int version);
153
154         virtual void request_input_monitoring (bool) {}
155         virtual void ensure_input_monitoring (bool) {}
156
157         framecnt_t   capture_offset() const { return _capture_offset; }
158         virtual void set_capture_offset ();
159
160         bool slaved() const      { return _slaved; }
161         void set_slaved(bool yn) { _slaved = yn; }
162
163         int set_loop (Location *loc);
164
165         std::list<boost::shared_ptr<Source> >& last_capture_sources () { return _last_capture_sources; }
166
167         void handle_input_change (IOChange, void *src);
168
169         void move_processor_automation (boost::weak_ptr<Processor>,
170                         std::list<Evoral::RangeMove<framepos_t> > const &);
171
172         /** For non-butler contexts (allocates temporary working buffers) */
173         virtual int do_refill_with_alloc() = 0;
174         virtual void set_block_size (pframes_t) = 0;
175
176         bool pending_overwrite () const {
177                 return _pending_overwrite;
178         }
179
180         PBD::Signal0<void>            RecordEnableChanged;
181         PBD::Signal0<void>            RecordSafeChanged;
182         PBD::Signal0<void>            SpeedChanged;
183         PBD::Signal0<void>            ReverseChanged;
184         /* Emitted when this diskstream is set to use a different playlist */
185         PBD::Signal0<void>            PlaylistChanged;
186         PBD::Signal0<void>            AlignmentStyleChanged;
187         PBD::Signal1<void,Location *> LoopSet;
188
189         static PBD::Signal0<void>     DiskOverrun;
190         static PBD::Signal0<void>     DiskUnderrun;
191
192   protected:
193         friend class Session;
194         friend class Butler;
195
196         /* the Session is the only point of access for these because they require
197          * that the Session is "inactive" while they are called.
198          */
199
200         virtual void set_pending_overwrite (bool) = 0;
201         virtual int  overwrite_existing_buffers () = 0;
202         virtual int  internal_playback_seek (framecnt_t distance) = 0;
203         virtual int  can_internal_playback_seek (framecnt_t distance) = 0;
204         virtual void reset_write_sources (bool, bool force = false) = 0;
205         virtual void non_realtime_input_change () = 0;
206
207   protected:
208         friend class Auditioner;
209         virtual int  seek (framepos_t which_sample, bool complete_refill = false) = 0;
210
211   protected:
212         friend class Track;
213
214     virtual int  process (BufferSet&, framepos_t transport_frame, pframes_t nframes, framecnt_t &, bool need_disk_signal) = 0;
215     virtual frameoffset_t calculate_playback_distance (pframes_t nframes) = 0;
216         virtual bool commit  (framecnt_t) = 0;
217
218         //private:
219
220         enum TransitionType {
221                 CaptureStart = 0,
222                 CaptureEnd
223         };
224
225         struct CaptureTransition {
226                 TransitionType   type;
227                 framepos_t       capture_val; ///< The start or end file frame position
228         };
229
230         /* The two central butler operations */
231         virtual int do_flush (RunContext context, bool force = false) = 0;
232         virtual int do_refill () = 0;
233
234         /* XXX fix this redundancy ... */
235
236         virtual void playlist_changed (const PBD::PropertyChange&);
237         virtual void playlist_deleted (boost::weak_ptr<Playlist>);
238         virtual void playlist_ranges_moved (std::list< Evoral::RangeMove<framepos_t> > const &, bool);
239
240         virtual void transport_stopped_wallclock (struct tm&, time_t, bool abort) = 0;
241         virtual void transport_looped (framepos_t transport_frame) = 0;
242
243         struct CaptureInfo {
244                 framepos_t start;
245                 framecnt_t frames;
246         };
247
248         virtual int use_new_write_source (uint32_t n=0) = 0;
249
250         virtual int find_and_use_playlist (const std::string&) = 0;
251
252         virtual void allocate_temporary_buffers () = 0;
253
254         virtual bool realtime_set_speed (double, bool global_change);
255
256         std::list<boost::shared_ptr<Source> > _last_capture_sources;
257
258         virtual int use_pending_capture_data (XMLNode& node) = 0;
259
260         virtual void check_record_status (framepos_t transport_frame, bool can_record);
261         virtual void prepare_record_status (framepos_t /*capture_start_frame*/) {}
262         virtual void set_align_style_from_io() {}
263         virtual void setup_destructive_playlist () {}
264         virtual void use_destructive_playlist () {}
265         virtual void prepare_to_stop (framepos_t transport_pos, framepos_t audible_frame);
266
267         void engage_record_enable ();
268         void disengage_record_enable ();
269         void engage_record_safe ();
270         void disengage_record_safe ();
271
272         virtual bool prep_record_enable () = 0;
273         virtual bool prep_record_disable () = 0;
274
275         void calculate_record_range (
276                 Evoral::OverlapType ot, framepos_t transport_frame, framecnt_t nframes,
277                 framecnt_t& rec_nframes, framecnt_t& rec_offset
278                 );
279
280         static framecnt_t disk_read_chunk_frames;
281         static framecnt_t disk_write_chunk_frames;
282
283         std::vector<CaptureInfo*> capture_info;
284         mutable Glib::Threads::Mutex capture_info_lock;
285
286         uint32_t i_am_the_modifier;
287
288         boost::shared_ptr<ARDOUR::IO>  _io;
289         Track*       _track;
290         ChanCount    _n_channels;
291
292         boost::shared_ptr<Playlist> _playlist;
293
294         gint         _record_enabled;
295         gint         _record_safe;
296         double       _visible_speed;
297         double       _actual_speed;
298         /* items needed for speed change logic */
299         bool         _buffer_reallocation_required;
300         bool         _seek_required;
301
302         /** Start of currently running capture in session frames */
303         framepos_t    capture_start_frame;
304         framecnt_t    capture_captured;
305         bool          was_recording;
306         framecnt_t    adjust_capture_position;
307         framecnt_t   _capture_offset;
308         /** The number of frames by which this diskstream's output should be delayed
309             with respect to the transport frame.  This is used for latency compensation.
310         */
311         framecnt_t   _roll_delay;
312         framepos_t    first_recordable_frame;
313         framepos_t    last_recordable_frame;
314         int           last_possibly_recording;
315         AlignStyle   _alignment_style;
316         AlignChoice  _alignment_choice;
317         bool         _slaved;
318         Location*     loop_location;
319         framepos_t    overwrite_frame;
320         off_t         overwrite_offset;
321         bool          _pending_overwrite;
322         bool          overwrite_queued;
323         IOChange      input_change_pending;
324         framecnt_t    wrap_buffer_size;
325         framecnt_t    speed_buffer_size;
326
327         double        _speed;
328         double        _target_speed;
329
330         /** The next frame position that we should be reading from in our playlist */
331         framepos_t     file_frame;
332         framepos_t     playback_sample;
333
334         bool          in_set_state;
335
336         std::string   _write_source_name;
337
338         Glib::Threads::Mutex state_lock;
339
340         PBD::ScopedConnectionList playlist_connections;
341
342         PBD::ScopedConnection ic_connection;
343
344         Flag _flags;
345         XMLNode* deprecated_io_node;
346
347         void route_going_away ();
348 };
349
350 }; /* namespace ARDOUR */
351
352 #endif /* __ardour_diskstream_h__ */