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