Merge edit and mix groups to just being route groups. Add properties to route groups...
[ardour.git] / libs / ardour / track.cc
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 #include "pbd/error.h"
19 #include <sigc++/retype.h>
20 #include <sigc++/retype_return.h>
21 #include <sigc++/bind.h>
22
23 #include "ardour/amp.h"
24 #include "ardour/audioplaylist.h"
25 #include "ardour/audioregion.h"
26 #include "ardour/audiosource.h"
27 #include "ardour/diskstream.h"
28 #include "ardour/io_processor.h"
29 #include "ardour/meter.h"
30 #include "ardour/port.h"
31 #include "ardour/processor.h"
32 #include "ardour/route_group_specialized.h"
33 #include "ardour/session.h"
34 #include "ardour/track.h"
35 #include "ardour/utils.h"
36
37 #include "i18n.h"
38
39 using namespace std;
40 using namespace ARDOUR;
41 using namespace PBD;
42
43 Track::Track (Session& sess, string name, Route::Flag flag, TrackMode mode, DataType default_type)
44         : Route (sess, name, flag, default_type)
45         , _rec_enable_control (new RecEnableControllable(*this))
46 {
47         _declickable = true;
48         _freeze_record.state = NoFreeze;
49         _saved_meter_point = _meter_point;
50         _mode = mode;
51 }
52
53 Track::Track (Session& sess, const XMLNode& node, DataType default_type)
54         : Route (sess, node, default_type)
55         , _rec_enable_control (new RecEnableControllable(*this))
56 {
57         _freeze_record.state = NoFreeze;
58         _declickable = true;
59         _saved_meter_point = _meter_point;
60 }
61
62 Track::~Track ()
63 {
64 }
65
66 void
67 Track::set_meter_point (MeterPoint p, void *src)
68 {
69         Route::set_meter_point (p, src);
70 }
71
72 XMLNode&
73 Track::get_state ()
74 {
75         return state (true);
76 }
77
78 XMLNode&
79 Track::get_template ()
80 {
81         return state (false);
82 }
83
84 void
85 Track::toggle_monitor_input ()
86 {
87         for (PortSet::iterator i = _input->ports().begin(); i != _input->ports().end(); ++i) {
88                 i->ensure_monitor_input(!i->monitoring_input());
89         }
90 }
91
92 ARDOUR::nframes_t
93 Track::update_total_latency ()
94 {
95         nframes_t old = _output->effective_latency();
96         nframes_t own_latency = _output->user_latency();
97         
98         for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
99                 if ((*i)->active ()) {
100                         own_latency += (*i)->signal_latency ();
101                 }
102         }
103
104 #undef DEBUG_LATENCY
105 #ifdef DEBUG_LATENCY
106         cerr << _name << ": internal redirect (final) latency = " << own_latency << endl;
107 #endif
108
109         _output->set_port_latency (own_latency);
110         
111         if (old != own_latency) {
112                 _output->set_latency_delay (own_latency);
113                 signal_latency_changed (); /* EMIT SIGNAL */
114         }
115
116         return _output->effective_latency();
117 }
118
119 Track::FreezeRecord::~FreezeRecord ()
120 {
121         for (vector<FreezeRecordProcessorInfo*>::iterator i = processor_info.begin(); i != processor_info.end(); ++i) {
122                 delete *i;
123         }
124 }
125
126 Track::FreezeState
127 Track::freeze_state() const
128 {
129         return _freeze_record.state;
130 }
131
132 Track::RecEnableControllable::RecEnableControllable (Track& s)
133         : Controllable (X_("recenable")), track (s)
134 {
135 }
136
137 void
138 Track::RecEnableControllable::set_value (float val)
139 {
140         bool bval = ((val >= 0.5f) ? true: false);
141         track.set_record_enable (bval, this);
142 }
143
144 float
145 Track::RecEnableControllable::get_value (void) const
146 {
147         if (track.record_enabled()) { return 1.0f; }
148         return 0.0f;
149 }
150
151 bool
152 Track::record_enabled () const
153 {
154         return _diskstream && _diskstream->record_enabled ();
155 }
156
157 bool
158 Track::can_record()
159 {
160         bool will_record = true;
161         for (PortSet::iterator i = _input->ports().begin(); i != _input->ports().end() && will_record; ++i) {
162                 if (!i->connected())
163                         will_record = false;
164         }
165
166         return will_record;
167 }
168         
169 void
170 Track::set_record_enable (bool yn, void *src)
171 {
172         if (_freeze_record.state == Frozen) {
173                 return;
174         }
175
176         if (_route_group && src != _route_group && _route_group->active_property (RouteGroup::RecEnable)) {
177                 _route_group->apply (&Track::set_record_enable, yn, _route_group);
178                 return;
179         }
180
181         /* keep track of the meter point as it was before we rec-enabled */
182         if (!_diskstream->record_enabled()) {
183                 _saved_meter_point = _meter_point;
184         }
185         
186         _diskstream->set_record_enabled (yn);
187
188         if (_diskstream->record_enabled()) {
189                 set_meter_point (MeterInput, this);
190         } else {
191                 set_meter_point (_saved_meter_point, this);
192         }
193
194         _rec_enable_control->Changed ();
195 }
196
197
198 bool
199 Track::set_name (const string& str)
200 {
201         bool ret;
202
203         if (record_enabled() && _session.actively_recording()) {
204                 /* this messes things up if done while recording */
205                 return false;
206         }
207
208         if (_diskstream->set_name (str)) {
209                 return false;
210         }
211
212         /* save state so that the statefile fully reflects any filename changes */
213         
214         if ((ret = Route::set_name (str)) == 0) {
215                 _session.save_state ("");
216         }
217
218         return ret;
219 }
220
221 void
222 Track::set_latency_delay (nframes_t longest_session_latency)
223 {
224         Route::set_latency_delay (longest_session_latency);
225         _diskstream->set_roll_delay (_roll_delay);
226 }
227
228 void
229 Track::zero_diskstream_id_in_xml (XMLNode& node)
230 {
231         if (node.property ("diskstream-id")) {
232                 node.add_property ("diskstream-id", "0");
233         }
234 }
235
236 int 
237 Track::no_roll (nframes_t nframes, sframes_t start_frame, sframes_t end_frame, 
238                 bool session_state_changing, bool can_record, bool rec_monitors_input)
239 {
240         if (n_outputs().n_total() == 0) {
241                 return 0;
242         }
243
244         if (!_active) {
245                 silence (nframes);
246                 return 0;
247         }
248
249         if (session_state_changing) {
250
251                 /* XXX is this safe to do against transport state changes? */
252
253                 passthru_silence (start_frame, end_frame, nframes, 0);
254                 return 0;
255         }
256
257         diskstream()->check_record_status (start_frame, nframes, can_record);
258
259         bool send_silence;
260         
261         if (_have_internal_generator) {
262                 /* since the instrument has no input streams,
263                    there is no reason to send any signal
264                    into the route.
265                 */
266                 send_silence = true;
267         } else {
268                 if (!Config->get_tape_machine_mode()) {
269                         /* 
270                            ADATs work in a strange way.. 
271                            they monitor input always when stopped.and auto-input is engaged. 
272                         */
273                         if ((Config->get_monitoring_model() == SoftwareMonitoring)
274                                         && (_session.config.get_auto_input () || _diskstream->record_enabled())) {
275                                 send_silence = false;
276                         } else {
277                                 send_silence = true;
278                         }
279                 } else {
280                         /* 
281                            Other machines switch to input on stop if the track is record enabled,
282                            regardless of the auto input setting (auto input only changes the 
283                            monitoring state when the transport is rolling) 
284                         */
285                         if ((Config->get_monitoring_model() == SoftwareMonitoring)
286                                         && _diskstream->record_enabled()) {
287                                 send_silence = false;
288                         } else {
289                                 send_silence = true;
290                         }
291                 }
292         }
293
294         _amp->apply_gain_automation(false);
295
296         if (send_silence) {
297                 
298                 /* if we're sending silence, but we want the meters to show levels for the signal,
299                    meter right here.
300                 */
301                 
302                 if (_have_internal_generator) {
303                         passthru_silence (start_frame, end_frame, nframes, 0);
304                 } else {
305                         if (_meter_point == MeterInput) {
306                                 _input->process_input (_meter, start_frame, end_frame, nframes);
307                         }
308                         passthru_silence (start_frame, end_frame, nframes, 0);
309                 }
310
311         } else {
312         
313                 /* we're sending signal, but we may still want to meter the input. 
314                  */
315
316                 passthru (start_frame, end_frame, nframes, false);
317         }
318
319         return 0;
320 }
321
322 int
323 Track::silent_roll (nframes_t nframes, sframes_t start_frame, sframes_t end_frame,  
324                     bool can_record, bool rec_monitors_input)
325 {
326         if (n_outputs().n_total() == 0 && _processors.empty()) {
327                 return 0;
328         }
329
330         if (!_active) {
331                 silence (nframes);
332                 return 0;
333         }
334
335         _silent = true;
336         _amp->apply_gain_automation(false);
337
338         silence (nframes);
339
340         return diskstream()->process (_session.transport_frame(), nframes, can_record, rec_monitors_input);
341 }