and plain realloc (not mlocked) as baseline
[ardour.git] / libs / ardour / ardour / track.h
1 /*
2     Copyright (C) 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 #ifndef __ardour_track_h__
20 #define __ardour_track_h__
21
22 #include <boost/shared_ptr.hpp>
23
24 #include "ardour/interthread_info.h"
25 #include "ardour/recordable.h"
26 #include "ardour/route.h"
27 #include "ardour/public_diskstream.h"
28
29 namespace ARDOUR {
30
31 class Session;
32 class Playlist;
33 class RouteGroup;
34 class Source;
35 class Region;
36 class Diskstream;
37 class IO;
38 class MonitorControl;
39 class RecordEnableControl;
40 class RecordSafeControl;
41
42 /** A track is an route (bus) with a recordable diskstream and
43  * related objects relevant to tracking, playback and editing.
44  *
45  * Specifically a track has regions and playlist objects.
46  */
47 class LIBARDOUR_API Track : public Route, public Recordable, public PublicDiskstream
48 {
49   public:
50         Track (Session&, std::string name, PresentationInfo::Flag f = PresentationInfo::Flag (0), TrackMode m = Normal, DataType default_type = DataType::AUDIO);
51         virtual ~Track ();
52
53         int init ();
54
55         bool set_name (const std::string& str);
56         void resync_track_name ();
57
58         TrackMode mode () const { return _mode; }
59         virtual int set_mode (TrackMode /*m*/) { return false; }
60         virtual bool can_use_mode (TrackMode /*m*/, bool& /*bounce_required*/) { return false; }
61         PBD::Signal0<void> TrackModeChanged;
62
63         boost::shared_ptr<MonitorControl> monitoring_control() const { return _monitoring_control; }
64
65         MonitorState monitoring_state () const;
66         MeterState metering_state () const;
67
68         virtual int no_roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame,
69                              bool state_changing);
70
71         int silent_roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame,
72                          bool& need_butler);
73
74         virtual int roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame,
75                           int declick, bool& need_butler) = 0;
76
77         bool needs_butler() const { return _needs_butler; }
78
79         virtual DataType data_type () const = 0;
80
81         bool can_record();
82
83         void use_new_diskstream ();
84         virtual boost::shared_ptr<Diskstream> create_diskstream() = 0;
85         virtual void set_diskstream (boost::shared_ptr<Diskstream>);
86
87         void set_latency_compensation (framecnt_t);
88
89         enum FreezeState {
90                 NoFreeze,
91                 Frozen,
92                 UnFrozen
93         };
94
95         FreezeState freeze_state() const;
96
97         virtual void freeze_me (InterThreadInfo&) = 0;
98         virtual void unfreeze () = 0;
99
100         /** Test if the track can be bounced with the given settings.
101          * If sends/inserts/returns are present in the signal path or the given track
102          * has no audio outputs bouncing is not possible.
103          *
104          * @param endpoint the processor to tap the signal off (or nil for the top)
105          * @param include_endpoint include the given processor in the bounced audio.
106          * @return true if the track can be bounced, or false otherwise.
107          */
108         virtual bool bounceable (boost::shared_ptr<Processor> endpoint, bool include_endpoint) const = 0;
109
110         /** bounce track from session start to session end to new region
111          *
112          * @param itt asynchronous progress report and cancel
113          * @return a new audio region (or nil in case of error)
114          */
115         virtual boost::shared_ptr<Region> bounce (InterThreadInfo& itt) = 0;
116
117         /** Bounce the given range to a new audio region.
118          * @param start start time (in samples)
119          * @param end end time (in samples)
120          * @param itt asynchronous progress report and cancel
121          * @param endpoint the processor to tap the signal off (or nil for the top)
122          * @param include_endpoint include the given processor in the bounced audio.
123          * @return a new audio region (or nil in case of error)
124          */
125         virtual boost::shared_ptr<Region> bounce_range (framepos_t start, framepos_t end, InterThreadInfo& itt,
126                                                         boost::shared_ptr<Processor> endpoint, bool include_endpoint) = 0;
127         virtual int export_stuff (BufferSet& bufs, framepos_t start_frame, framecnt_t nframes,
128                                   boost::shared_ptr<Processor> endpoint, bool include_endpoint, bool for_export, bool for_freeze) = 0;
129
130         XMLNode&    get_state();
131         XMLNode&    get_template();
132         virtual int set_state (const XMLNode&, int version);
133         static void zero_diskstream_id_in_xml (XMLNode&);
134
135         boost::shared_ptr<AutomationControl> rec_enable_control() const { return _record_enable_control; }
136         boost::shared_ptr<AutomationControl> rec_safe_control() const { return _record_safe_control; }
137
138         int prep_record_enabled (bool);
139         bool can_be_record_enabled ();
140         bool can_be_record_safe ();
141
142         bool using_diskstream_id (PBD::ID) const;
143
144         void set_block_size (pframes_t);
145
146         /* PublicDiskstream interface */
147         boost::shared_ptr<Playlist> playlist ();
148         void request_input_monitoring (bool);
149         void ensure_input_monitoring (bool);
150         bool destructive () const;
151         std::list<boost::shared_ptr<Source> > & last_capture_sources ();
152         void set_capture_offset ();
153         std::string steal_write_source_name ();
154         void reset_write_sources (bool, bool force = false);
155         float playback_buffer_load () const;
156         float capture_buffer_load () const;
157         int do_refill ();
158         int do_flush (RunContext, bool force = false);
159         void set_pending_overwrite (bool);
160         int seek (framepos_t, bool complete_refill = false);
161         bool hidden () const;
162         int can_internal_playback_seek (framecnt_t);
163         int internal_playback_seek (framecnt_t);
164         void non_realtime_input_change ();
165         void non_realtime_locate (framepos_t);
166         void non_realtime_set_speed ();
167         int overwrite_existing_buffers ();
168         framecnt_t get_captured_frames (uint32_t n = 0) const;
169         int set_loop (Location *);
170         void transport_looped (framepos_t);
171         bool realtime_set_speed (double, bool);
172         void transport_stopped_wallclock (struct tm &, time_t, bool);
173         bool pending_overwrite () const;
174         double speed () const;
175         void prepare_to_stop (framepos_t, framepos_t);
176         void set_slaved (bool);
177         ChanCount n_channels ();
178         framepos_t get_capture_start_frame (uint32_t n = 0) const;
179         AlignStyle alignment_style () const;
180         AlignChoice alignment_choice () const;
181         framepos_t current_capture_start () const;
182         framepos_t current_capture_end () const;
183         void playlist_modified ();
184         int use_playlist (boost::shared_ptr<Playlist>);
185         void set_align_style (AlignStyle, bool force=false);
186         void set_align_choice (AlignChoice, bool force=false);
187         int use_copy_playlist ();
188         int use_new_playlist ();
189         void adjust_playback_buffering ();
190         void adjust_capture_buffering ();
191
192         PBD::Signal0<void> DiskstreamChanged;
193         PBD::Signal0<void> FreezeChange;
194         /* Emitted when our diskstream is set to use a different playlist */
195         PBD::Signal0<void> PlaylistChanged;
196         PBD::Signal0<void> SpeedChanged;
197         PBD::Signal0<void> AlignmentStyleChanged;
198
199   protected:
200         XMLNode& state (bool full);
201
202         boost::shared_ptr<Diskstream> _diskstream;
203         MeterPoint    _saved_meter_point;
204         TrackMode     _mode;
205         bool          _needs_butler;
206         boost::shared_ptr<MonitorControl> _monitoring_control;
207
208         //private: (FIXME)
209         struct FreezeRecordProcessorInfo {
210                 FreezeRecordProcessorInfo(XMLNode& st, boost::shared_ptr<Processor> proc)
211                         : state (st), processor (proc) {}
212
213                 XMLNode                      state;
214                 boost::shared_ptr<Processor> processor;
215                 PBD::ID                      id;
216         };
217
218         struct FreezeRecord {
219                 FreezeRecord()
220                         : have_mementos(false)
221                 {}
222
223                 ~FreezeRecord();
224
225                 boost::shared_ptr<Playlist>        playlist;
226                 std::vector<FreezeRecordProcessorInfo*> processor_info;
227                 bool                               have_mementos;
228                 FreezeState                        state;
229         };
230
231         virtual void set_state_part_two () = 0;
232
233         FreezeRecord          _freeze_record;
234         XMLNode*              pending_state;
235         bool                  _destructive;
236
237         void maybe_declick (BufferSet&, framecnt_t, int);
238
239         boost::shared_ptr<AutomationControl> _record_enable_control;
240         boost::shared_ptr<AutomationControl> _record_safe_control;
241
242         virtual void record_enable_changed (bool, PBD::Controllable::GroupControlDisposition);
243         virtual void record_safe_changed (bool, PBD::Controllable::GroupControlDisposition);
244
245         framecnt_t check_initial_delay (framecnt_t nframes, framepos_t&);
246         virtual void monitoring_changed (bool, PBD::Controllable::GroupControlDisposition);
247
248 private:
249
250         virtual boost::shared_ptr<Diskstream> diskstream_factory (XMLNode const &) = 0;
251
252         void diskstream_playlist_changed ();
253         void diskstream_speed_changed ();
254         void diskstream_alignment_style_changed ();
255         void parameter_changed (std::string const & p);
256
257         std::string _diskstream_name;
258 };
259
260 }; /* namespace ARDOUR*/
261
262 #endif /* __ardour_track_h__ */