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