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