1 /*
2  * Copyright (C) 2006-2011 David Robillard <d@drobilla.net>
3  * Copyright (C) 2008-2018 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
5  * Copyright (C) 2014-2019 Robin Gareus <robin@gareus.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
22 #ifndef __ardour_track_h__
23 #define __ardour_track_h__
24 
25 #include <boost/shared_ptr.hpp>
26 
27 #include "pbd/enum_convert.h"
28 
29 #include "ardour/interthread_info.h"
30 #include "ardour/recordable.h"
31 #include "ardour/route.h"
32 
33 namespace ARDOUR {
34 
35 class Session;
36 class Playlist;
37 class RouteGroup;
38 class Source;
39 class Region;
40 class DiskReader;
41 class DiskWriter;
42 class IO;
43 class RecordEnableControl;
44 class RecordSafeControl;
45 class MidiStateTracker;
46 
47 /** A track is an route (bus) with a recordable diskstream and
48  * related objects relevant to recording, playback and editing.
49  *
50  * Specifically a track has a playlist object that describes material
51  * to be played from disk, and modifies that object during recording and
52  * editing.
53  */
54 class LIBARDOUR_API Track : public Route, public Recordable
55 {
56 public:
57 	Track (Session&, std::string name, PresentationInfo::Flag f = PresentationInfo::Flag (0), TrackMode m = Normal, DataType default_type = DataType::AUDIO);
58 	virtual ~Track ();
59 
60 	int init ();
61 
62 	bool set_name (const std::string& str);
63 	int resync_take_name (std::string newname = "");
64 
mode()65 	TrackMode mode () const { return _mode; }
66 
67 	bool set_processor_state (XMLNode const& node, int version, XMLProperty const* prop, ProcessorList& new_order, bool& must_configure);
68 
69 	bool declick_in_progress () const;
70 
71 	bool can_record();
72 
73 	enum FreezeState {
74 		NoFreeze,
75 		Frozen,
76 		UnFrozen
77 	};
78 
79 	FreezeState freeze_state() const;
80 
81 	virtual void freeze_me (InterThreadInfo&) = 0;
82 	virtual void unfreeze () = 0;
83 
84 	/** Test if the track can be bounced with the given settings.
85 	 * If sends/inserts/returns are present in the signal path or the given track
86 	 * has no audio outputs bouncing is not possible.
87 	 *
88 	 * @param endpoint the processor to tap the signal off (or nil for the top)
89 	 * @param include_endpoint include the given processor in the bounced audio.
90 	 * @return true if the track can be bounced, or false otherwise.
91 	 */
92 	virtual bool bounceable (boost::shared_ptr<Processor> endpoint, bool include_endpoint) const = 0;
93 
94 	/** bounce track from session start to session end to new region
95 	 *
96 	 * @param itt asynchronous progress report and cancel
97 	 * @return a new audio region (or nil in case of error)
98 	 */
99 	virtual boost::shared_ptr<Region> bounce (InterThreadInfo& itt, std::string const& name) = 0;
100 
101 	/** Bounce the given range to a new audio region.
102 	 * @param start start time (in samples)
103 	 * @param end end time (in samples)
104 	 * @param itt asynchronous progress report and cancel
105 	 * @param endpoint the processor to tap the signal off (or nil for the top)
106 	 * @param include_endpoint include the given processor in the bounced audio.
107 	 * @return a new audio region (or nil in case of error)
108 	 */
109 	virtual boost::shared_ptr<Region> bounce_range (samplepos_t start, samplepos_t end, InterThreadInfo& itt,
110 	                                                boost::shared_ptr<Processor> endpoint, bool include_endpoint,
111 	                                                std::string const& name) = 0;
112 
113 	virtual int export_stuff (BufferSet& bufs, samplepos_t start_sample, samplecnt_t nframes,
114 	                          boost::shared_ptr<Processor> endpoint, bool include_endpoint, bool for_export, bool for_freeze,
115 	                          MidiStateTracker&) = 0;
116 
117 	virtual int set_state (const XMLNode&, int version);
118 	static void zero_diskstream_id_in_xml (XMLNode&);
119 
rec_enable_control()120 	boost::shared_ptr<AutomationControl> rec_enable_control() const { return _record_enable_control; }
rec_safe_control()121 	boost::shared_ptr<AutomationControl> rec_safe_control() const { return _record_safe_control; }
122 
123 	int prep_record_enabled (bool);
124 	bool can_be_record_enabled ();
125 	bool can_be_record_safe ();
126 
127 	void use_captured_sources (SourceList&, CaptureInfos const &);
128 
129 	void set_block_size (pframes_t);
130 
131 	/* used by DiskReader request_overwrite_buffer(), to create
132 	 * a SessionEvent with weak_ptr<> reference
133 	 */
shared_ptr()134 	boost::shared_ptr<Track> shared_ptr () {
135 		return boost::dynamic_pointer_cast<Track> (shared_from_this());
136 	}
137 
138 	boost::shared_ptr<Playlist> playlist ();
139 	void request_input_monitoring (bool);
140 	void ensure_input_monitoring (bool);
141 	std::list<boost::shared_ptr<Source> > & last_capture_sources ();
142 	std::string steal_write_source_name ();
143 	void reset_write_sources (bool, bool force = false);
144 	float playback_buffer_load () const;
145 	float capture_buffer_load () const;
146 	int do_refill ();
147 	int do_flush (RunContext, bool force = false);
148 	void set_pending_overwrite (OverwriteReason);
149 	int seek (samplepos_t, bool complete_refill = false);
150 	bool can_internal_playback_seek (samplecnt_t);
151 	void internal_playback_seek (samplecnt_t);
152 	void non_realtime_locate (samplepos_t);
153 	bool overwrite_existing_buffers ();
154 	samplecnt_t get_captured_samples (uint32_t n = 0) const;
155 	void transport_looped (samplepos_t);
156 	void transport_stopped_wallclock (struct tm &, time_t, bool);
157 	void mark_capture_xrun ();
158 	bool pending_overwrite () const;
159 	void set_slaved (bool);
160 	ChanCount n_channels ();
161 	samplepos_t get_capture_start_sample (uint32_t n = 0) const;
162 	AlignStyle alignment_style () const;
163 	AlignChoice alignment_choice () const;
164 	samplepos_t current_capture_start () const;
165 	samplepos_t current_capture_end () const;
166 	void set_align_style (AlignStyle, bool force=false);
167 	void set_align_choice (AlignChoice, bool force=false);
168 	void playlist_modified ();
169 	int use_playlist (DataType, boost::shared_ptr<Playlist>, bool set_orig = true);
170 	int find_and_use_playlist (DataType, PBD::ID const &);
171 	int use_copy_playlist ();
172 	int use_new_playlist (DataType);
use_default_new_playlist()173 	int use_default_new_playlist () {
174 		return use_new_playlist (data_type());
175 	}
176 	void adjust_playback_buffering ();
177 	void adjust_capture_buffering ();
178 
179 	PBD::Signal0<void> FreezeChange;
180 	PBD::Signal0<void> PlaylistChanged;
181 	PBD::Signal0<void> PlaylistAdded;
182 	PBD::Signal0<void> SpeedChanged;
183 	PBD::Signal0<void> AlignmentStyleChanged;
184 	PBD::Signal0<void> ChanCountChanged;
185 
186 protected:
187 	XMLNode& state (bool save_template);
188 
189 	boost::shared_ptr<Playlist>   _playlists[DataType::num_types];
190 
191 	MeterPoint    _saved_meter_point;
192 	TrackMode     _mode;
193 
194 	//private: (FIXME)
195 	struct FreezeRecordProcessorInfo {
FreezeRecordProcessorInfoFreezeRecordProcessorInfo196 		FreezeRecordProcessorInfo(XMLNode& st, boost::shared_ptr<Processor> proc)
197 			: state (st), processor (proc) {}
198 
199 		XMLNode                      state;
200 		boost::shared_ptr<Processor> processor;
201 		PBD::ID                      id;
202 	};
203 
204 	struct FreezeRecord {
FreezeRecordFreezeRecord205 		FreezeRecord()
206 			: have_mementos(false)
207 		{}
208 
209 		~FreezeRecord();
210 
211 		boost::shared_ptr<Playlist>        playlist;
212 		std::vector<FreezeRecordProcessorInfo*> processor_info;
213 		bool                               have_mementos;
214 		FreezeState                        state;
215 	};
216 
217 	virtual void set_state_part_two () = 0;
218 
219 	FreezeRecord _freeze_record;
220 	XMLNode*      pending_state;
221 
222 	boost::shared_ptr<AutomationControl> _record_enable_control;
223 	boost::shared_ptr<AutomationControl> _record_safe_control;
224 
225 	virtual void record_enable_changed (bool, PBD::Controllable::GroupControlDisposition);
226 	virtual void record_safe_changed (bool, PBD::Controllable::GroupControlDisposition);
227 
228 	virtual void monitoring_changed (bool, PBD::Controllable::GroupControlDisposition);
229 
230 	AlignChoice _alignment_choice;
231 	void set_align_choice_from_io ();
232 
233 	void use_captured_audio_sources (SourceList&, CaptureInfos const &);
234 	void use_captured_midi_sources (SourceList&, CaptureInfos const &);
235 
236 private:
237 	void parameter_changed (std::string const & p);
238 	void input_changed ();
239 	void chan_count_changed ();
240 
241 	std::string _diskstream_name;
242 	bool        _pending_name_change;
243 };
244 
245 }; /* namespace ARDOUR*/
246 
247 namespace PBD {
248 	DEFINE_ENUM_CONVERT(ARDOUR::Track::FreezeState);
249 }
250 
251 #endif /* __ardour_track_h__ */
252