Abstraction cleanups/polish, towards merging with trunk
[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     $Id: diskstream.h 579 2006-06-12 19:56:37Z essej $
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/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
46
47 struct tm;
48
49 namespace ARDOUR {
50
51 class AudioEngine;
52 class Send;
53 class Session;
54 class Playlist;
55 class IO;
56
57 /* FIXME: There are (obviously) far too many virtual functions in this ATM.
58  * Just to get things off the ground, they'll be removed. */
59
60 class Diskstream : public Stateful, public sigc::trackable
61 {       
62   public:
63         enum Flag {
64                 Recordable = 0x1,
65                 Hidden = 0x2,
66                 Destructive = 0x4
67         };
68
69         string name () const { return _name; }
70         virtual int set_name (string str, void* src);
71
72         ARDOUR::IO* io() const { return _io; }
73         void set_io (ARDOUR::IO& io);
74
75         virtual 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         virtual float playback_buffer_load() const = 0;
80         virtual float capture_buffer_load() const = 0;
81
82         void set_flag (Flag f)   { _flags |= f; }
83         void unset_flag (Flag f) { _flags &= ~f; }
84
85         AlignStyle alignment_style() const { return _alignment_style; }
86         void set_align_style (AlignStyle);
87         void set_persistent_align_style (AlignStyle a) { _persistent_alignment_style = a; }
88         
89         jack_nframes_t roll_delay() const { return _roll_delay; }
90         void set_roll_delay (jack_nframes_t);
91
92         bool record_enabled() const { return g_atomic_int_get (&_record_enabled); }
93         virtual void set_record_enabled (bool yn, void *src) = 0;
94
95         bool destructive() const { return _flags & Destructive; }
96         virtual void set_destructive (bool yn);
97
98         const PBD::ID& id()          const { return _id; }
99         bool           hidden()      const { return _flags & Hidden; }
100         bool           recordable()  const { return _flags & Recordable; }
101         bool           reversed()    const { return _actual_speed < 0.0f; }
102         double         speed()       const { return _visible_speed; }
103         
104         virtual void punch_in()  {}
105         virtual void punch_out() {}
106
107         void set_speed (double);
108         void non_realtime_set_speed ();
109
110         Playlist* playlist () { return _playlist; }
111
112         virtual int use_playlist (Playlist *);
113         virtual int use_new_playlist () = 0;
114         virtual int use_copy_playlist () = 0;
115
116         jack_nframes_t current_capture_start() const { return capture_start_frame; }
117         jack_nframes_t current_capture_end()   const { return capture_start_frame + capture_captured; }
118         jack_nframes_t get_capture_start_frame (uint32_t n=0);
119         jack_nframes_t get_captured_frames (uint32_t n=0);
120         
121         uint32_t n_channels() { return _n_channels; }
122
123         static jack_nframes_t disk_io_frames()            { return disk_io_chunk_frames; }
124         static void set_disk_io_chunk_frames (uint32_t n) { disk_io_chunk_frames = n; }
125
126         /* Stateful */
127         virtual XMLNode& get_state(void) = 0;
128         virtual int      set_state(const XMLNode& node) = 0;
129         
130         // FIXME: makes sense for all diskstream types?
131         virtual void monitor_input (bool) {}
132
133         jack_nframes_t capture_offset() const { return _capture_offset; }
134         virtual void   set_capture_offset ();
135
136         bool slaved() const      { return _slaved; }
137         void set_slaved(bool yn) { _slaved = yn; }
138
139         virtual int set_loop (Location *loc);
140         sigc::signal<void,Location *> LoopSet;
141
142         std::list<Region*>& last_capture_regions () { return _last_capture_regions; }
143
144         void handle_input_change (IOChange, void *src);
145
146         sigc::signal<void,void*> RecordEnableChanged;
147         sigc::signal<void>       SpeedChanged;
148         sigc::signal<void,void*> ReverseChanged;
149         sigc::signal<void>       PlaylistChanged;
150         sigc::signal<void>       AlignmentStyleChanged;
151
152         static sigc::signal<void>                DiskOverrun;
153         static sigc::signal<void>                DiskUnderrun;
154         static sigc::signal<void,Diskstream*>    DiskstreamCreated; // XXX use a ref with sigc2
155         //static sigc::signal<void,list<Source*>*> DeleteSources;
156
157   protected:
158         friend class Session;
159
160         Diskstream (Session &, const string& name, Flag f = Recordable);
161         Diskstream (Session &, const XMLNode&);
162
163         /* the Session is the only point of access for these
164            because they require that the Session is "inactive"
165            while they are called.
166         */
167
168         virtual void set_pending_overwrite (bool) = 0;
169         virtual int  overwrite_existing_buffers () = 0;
170         virtual void set_block_size (jack_nframes_t) = 0;
171         virtual int  internal_playback_seek (jack_nframes_t distance) = 0;
172         virtual int  can_internal_playback_seek (jack_nframes_t distance) = 0;
173         virtual int  rename_write_sources () = 0;
174         virtual void reset_write_sources (bool, bool force = false) = 0;
175         virtual void non_realtime_input_change () = 0;
176
177         uint32_t read_data_count() const { return _read_data_count; }
178         uint32_t write_data_count() const { return _write_data_count; }
179
180   protected:
181         friend class Auditioner;
182         virtual int  seek (jack_nframes_t which_sample, bool complete_refill = false) = 0;
183
184   protected:
185         friend class Track;
186
187         virtual void prepare ();
188         virtual int  process (jack_nframes_t transport_frame, jack_nframes_t nframes, jack_nframes_t offset, bool can_record, bool rec_monitors_input) = 0;
189         virtual bool commit  (jack_nframes_t nframes) = 0;
190         virtual void recover (); /* called if commit will not be called, but process was */
191
192         //private:
193         
194         /* use unref() to destroy a diskstream */
195         virtual ~Diskstream();
196
197         enum TransitionType {
198                 CaptureStart = 0,
199                 CaptureEnd
200         };
201         
202         struct CaptureTransition {
203                 TransitionType   type;
204                 // the start or end file frame pos
205                 jack_nframes_t   capture_val;
206         };
207
208         /* The two central butler operations */
209         virtual int do_flush (Session::RunContext context, bool force = false) = 0;
210         virtual int do_refill () = 0;
211         
212         /** For non-butler contexts (allocates temporary working buffers) */
213         virtual int do_refill_with_alloc() = 0;
214
215         
216         /* XXX fix this redundancy ... */
217
218         virtual void playlist_changed (Change);
219         virtual void playlist_modified ();
220         virtual void playlist_deleted (Playlist*);
221
222         virtual void finish_capture (bool rec_monitors_input) = 0;
223         virtual void transport_stopped (struct tm&, time_t, bool abort) = 0;
224
225         struct CaptureInfo {
226             uint32_t start;
227             uint32_t frames;
228         };
229
230         virtual void init (Flag);
231
232         virtual int use_new_write_source (uint32_t n=0) = 0;
233
234         virtual int find_and_use_playlist (const string&) = 0;
235
236         virtual void allocate_temporary_buffers () = 0;
237
238         virtual bool realtime_set_speed (double, bool global_change);
239
240         std::list<Region*> _last_capture_regions;
241         virtual int use_pending_capture_data (XMLNode& node) = 0;
242
243         virtual void get_input_sources () = 0;
244         virtual void check_record_status (jack_nframes_t transport_frame, jack_nframes_t nframes, bool can_record) = 0;
245         virtual void set_align_style_from_io() {}
246         virtual void setup_destructive_playlist () = 0;
247         virtual void use_destructive_playlist () = 0;
248
249         static jack_nframes_t disk_io_chunk_frames;
250         vector<CaptureInfo*>  capture_info;
251         Glib::Mutex           capture_info_lock;
252
253         uint32_t i_am_the_modifier;
254
255         string            _name;
256         ARDOUR::Session&  _session;
257         ARDOUR::IO*       _io;
258         uint32_t          _n_channels;
259         PBD::ID           _id;
260         Playlist*         _playlist;
261
262         mutable gint             _record_enabled;
263         double                   _visible_speed;
264         double                   _actual_speed;
265         /* items needed for speed change logic */
266         bool                     _buffer_reallocation_required;
267         bool                     _seek_required;
268         
269         bool                      force_refill;
270         jack_nframes_t            capture_start_frame;
271         jack_nframes_t            capture_captured;
272         bool                      was_recording;
273         jack_nframes_t            adjust_capture_position;
274         jack_nframes_t           _capture_offset;
275         jack_nframes_t           _roll_delay;
276         jack_nframes_t            first_recordable_frame;
277         jack_nframes_t            last_recordable_frame;
278         int                       last_possibly_recording;
279         AlignStyle               _alignment_style;
280         bool                     _scrubbing;
281         bool                     _slaved;
282         bool                     _processed;
283         Location*                 loop_location;
284         jack_nframes_t            overwrite_frame;
285         off_t                     overwrite_offset;
286         bool                      pending_overwrite;
287         bool                      overwrite_queued;
288         IOChange                  input_change_pending;
289         jack_nframes_t            wrap_buffer_size;
290         jack_nframes_t            speed_buffer_size;
291
292         uint64_t                  last_phase;
293         uint64_t                  phi;
294         
295         jack_nframes_t            file_frame;           
296         jack_nframes_t            playback_sample;
297         jack_nframes_t            playback_distance;
298
299         uint32_t                 _read_data_count;
300         uint32_t                 _write_data_count;
301
302         bool                      in_set_state;
303         AlignStyle               _persistent_alignment_style;
304         bool                      first_input_change;
305
306         Glib::Mutex  state_lock;
307
308         jack_nframes_t scrub_start;
309         jack_nframes_t scrub_buffer_size;
310         jack_nframes_t scrub_offset;
311
312         uint32_t _refcnt;
313
314         sigc::connection ports_created_c;
315         sigc::connection plmod_connection;
316         sigc::connection plstate_connection;
317         sigc::connection plgone_connection;
318         
319         unsigned char _flags;
320 };
321
322 }; /* namespace ARDOUR */
323
324 #endif /* __ardour_diskstream_h__ */