second (and hopefully) final part of changes to respond to header format changes...
[ArdourMidi.git] / libs / ardour / track.cc
blob23afe6dea2ab5fdb5e9b58ff48ffe412ece6c58a
1 /*
2 Copyright (C) 2006 Paul Davis
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.
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.
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.
18 #include "pbd/error.h"
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"
36 #include "i18n.h"
38 using namespace std;
39 using namespace ARDOUR;
40 using namespace PBD;
42 Track::Track (Session& sess, string name, Route::Flag flag, TrackMode mode, DataType default_type)
43 : Route (sess, name, flag, default_type)
44 , _saved_meter_point (_meter_point)
45 , _mode (mode)
46 , _rec_enable_control (new RecEnableControllable(*this))
48 _freeze_record.state = NoFreeze;
49 _declickable = true;
52 Track::~Track ()
54 DEBUG_TRACE (DEBUG::Destruction, string_compose ("track %1 destructor\n", _name));
57 int
58 Track::init ()
60 if (Route::init ()) {
61 return -1;
64 return 0;
66 XMLNode&
67 Track::get_state ()
69 return state (true);
72 XMLNode&
73 Track::get_template ()
75 return state (false);
78 void
79 Track::toggle_monitor_input ()
81 for (PortSet::iterator i = _input->ports().begin(); i != _input->ports().end(); ++i) {
82 i->ensure_monitor_input(!i->monitoring_input());
86 ARDOUR::nframes_t
87 Track::update_total_latency ()
89 nframes_t old = _output->effective_latency();
90 nframes_t own_latency = _output->user_latency();
92 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
93 if ((*i)->active ()) {
94 own_latency += (*i)->signal_latency ();
98 #undef DEBUG_LATENCY
99 #ifdef DEBUG_LATENCY
100 cerr << _name << ": internal redirect (final) latency = " << own_latency << endl;
101 #endif
103 _output->set_port_latency (own_latency);
105 if (old != own_latency) {
106 _output->set_latency_delay (own_latency);
107 signal_latency_changed (); /* EMIT SIGNAL */
110 return _output->effective_latency();
113 Track::FreezeRecord::~FreezeRecord ()
115 for (vector<FreezeRecordProcessorInfo*>::iterator i = processor_info.begin(); i != processor_info.end(); ++i) {
116 delete *i;
120 Track::FreezeState
121 Track::freeze_state() const
123 return _freeze_record.state;
126 Track::RecEnableControllable::RecEnableControllable (Track& s)
127 : Controllable (X_("recenable")), track (s)
131 void
132 Track::RecEnableControllable::set_value (float val)
134 bool bval = ((val >= 0.5f) ? true: false);
135 track.set_record_enable (bval, this);
138 float
139 Track::RecEnableControllable::get_value (void) const
141 if (track.record_enabled()) { return 1.0f; }
142 return 0.0f;
145 bool
146 Track::record_enabled () const
148 return _diskstream && _diskstream->record_enabled ();
151 bool
152 Track::can_record()
154 bool will_record = true;
155 for (PortSet::iterator i = _input->ports().begin(); i != _input->ports().end() && will_record; ++i) {
156 if (!i->connected())
157 will_record = false;
160 return will_record;
163 void
164 Track::set_record_enable (bool yn, void *src)
166 if (!_session.writable()) {
167 return;
170 if (_freeze_record.state == Frozen) {
171 return;
174 if (_route_group && src != _route_group && _route_group->is_active() && _route_group->is_recenable()) {
175 _route_group->apply (&Track::set_record_enable, yn, _route_group);
176 return;
179 /* keep track of the meter point as it was before we rec-enabled */
180 if (!_diskstream->record_enabled()) {
181 _saved_meter_point = _meter_point;
184 _diskstream->set_record_enabled (yn);
186 if (_diskstream->record_enabled()) {
187 if (_meter_point != MeterCustom) {
188 set_meter_point (MeterInput);
190 } else {
191 set_meter_point (_saved_meter_point);
194 _rec_enable_control->Changed ();
198 bool
199 Track::set_name (const string& str)
201 bool ret;
203 if (record_enabled() && _session.actively_recording()) {
204 /* this messes things up if done while recording */
205 return false;
208 if (_diskstream->set_name (str)) {
209 return false;
212 /* save state so that the statefile fully reflects any filename changes */
214 if ((ret = Route::set_name (str)) == 0) {
215 _session.save_state ("");
218 return ret;
221 void
222 Track::set_latency_delay (nframes_t longest_session_latency)
224 Route::set_latency_delay (longest_session_latency);
225 _diskstream->set_roll_delay (_roll_delay);
228 void
229 Track::zero_diskstream_id_in_xml (XMLNode& node)
231 if (node.property ("diskstream-id")) {
232 node.add_property ("diskstream-id", "0");
237 Track::no_roll (nframes_t nframes, framepos_t start_frame, framepos_t end_frame,
238 bool session_state_changing, bool can_record, bool /*rec_monitors_input*/)
240 Glib::RWLock::ReaderLock lm (_processor_lock, Glib::TRY_LOCK);
241 if (!lm.locked()) {
242 return 0;
245 if (n_outputs().n_total() == 0) {
246 return 0;
249 if (!_active) {
250 silence (nframes);
251 return 0;
254 if (session_state_changing) {
255 if (_session.transport_speed() != 0.0f) {
256 /* we're rolling but some state is changing (e.g. our diskstream contents)
257 so we cannot use them. Be silent till this is over. Don't declick.
259 XXX note the absurdity of ::no_roll() being called when we ARE rolling!
261 passthru_silence (start_frame, end_frame, nframes, 0);
262 return 0;
264 /* we're really not rolling, so we're either delivery silence or actually
265 monitoring, both of which are safe to do while session_state_changing is true.
269 _diskstream->check_record_status (start_frame, nframes, can_record);
271 bool send_silence;
273 if (_have_internal_generator) {
274 /* since the instrument has no input streams,
275 there is no reason to send any signal
276 into the route.
278 send_silence = true;
279 } else {
280 if (!Config->get_tape_machine_mode()) {
282 ADATs work in a strange way..
283 they monitor input always when stopped.and auto-input is engaged.
285 if ((Config->get_monitoring_model() == SoftwareMonitoring)
286 && (_session.config.get_auto_input () || _diskstream->record_enabled())) {
287 send_silence = false;
288 } else {
289 send_silence = true;
291 } else {
293 Other machines switch to input on stop if the track is record enabled,
294 regardless of the auto input setting (auto input only changes the
295 monitoring state when the transport is rolling)
297 if ((Config->get_monitoring_model() == SoftwareMonitoring)
298 && _diskstream->record_enabled()) {
299 send_silence = false;
300 } else {
301 send_silence = true;
306 _amp->apply_gain_automation(false);
308 if (send_silence) {
310 /* if we're sending silence, but we want the meters to show levels for the signal,
311 meter right here.
314 if (_have_internal_generator) {
315 passthru_silence (start_frame, end_frame, nframes, 0);
316 } else {
317 if (_meter_point == MeterInput) {
318 _input->process_input (_meter, start_frame, end_frame, nframes);
320 passthru_silence (start_frame, end_frame, nframes, 0);
323 } else {
325 /* we're sending signal, but we may still want to meter the input.
328 passthru (start_frame, end_frame, nframes, false);
331 _main_outs->flush (nframes, end_frame - start_frame - 1);
333 return 0;
337 Track::silent_roll (nframes_t nframes, framepos_t /*start_frame*/, framepos_t /*end_frame*/,
338 bool can_record, bool rec_monitors_input, bool& need_butler)
340 Glib::RWLock::ReaderLock lm (_processor_lock, Glib::TRY_LOCK);
341 if (!lm.locked()) {
342 return 0;
345 if (n_outputs().n_total() == 0 && _processors.empty()) {
346 return 0;
349 if (!_active) {
350 silence (nframes);
351 return 0;
354 _silent = true;
355 _amp->apply_gain_automation(false);
357 silence (nframes);
359 return _diskstream->process (_session.transport_frame(), nframes, can_record, rec_monitors_input, need_butler);
362 ChanCount
363 Track::input_streams () const
365 ChanCount cc = _input->n_ports ();
367 if (cc.n_total() == 0 && _diskstream) {
368 return cc = _diskstream->n_channels();
371 return cc;
374 void
375 Track::set_diskstream (boost::shared_ptr<Diskstream> ds)
377 _diskstream = ds;
379 ds->PlaylistChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_playlist_changed, this));
380 diskstream_playlist_changed ();
381 ds->RecordEnableChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_record_enable_changed, this));
382 ds->SpeedChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_speed_changed, this));
383 ds->AlignmentStyleChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_alignment_style_changed, this));
386 void
387 Track::diskstream_playlist_changed ()
389 PlaylistChanged (); /* EMIT SIGNAL */
392 void
393 Track::diskstream_record_enable_changed ()
395 RecordEnableChanged (); /* EMIT SIGNAL */
398 void
399 Track::diskstream_speed_changed ()
401 SpeedChanged (); /* EMIT SIGNAL */
404 void
405 Track::diskstream_alignment_style_changed ()
407 AlignmentStyleChanged (); /* EMIT SIGNAL */
410 boost::shared_ptr<Playlist>
411 Track::playlist ()
413 return _diskstream->playlist ();
416 void
417 Track::monitor_input (bool m)
419 _diskstream->monitor_input (m);
422 bool
423 Track::destructive () const
425 return _diskstream->destructive ();
428 list<boost::shared_ptr<Source> > &
429 Track::last_capture_sources ()
431 return _diskstream->last_capture_sources ();
434 void
435 Track::set_capture_offset ()
437 _diskstream->set_capture_offset ();
440 list<boost::shared_ptr<Source> >
441 Track::steal_write_sources()
443 return _diskstream->steal_write_sources ();
446 void
447 Track::reset_write_sources (bool r, bool force)
449 _diskstream->reset_write_sources (r, force);
452 float
453 Track::playback_buffer_load () const
455 return _diskstream->playback_buffer_load ();
458 float
459 Track::capture_buffer_load () const
461 return _diskstream->capture_buffer_load ();
465 Track::do_refill ()
467 return _diskstream->do_refill ();
471 Track::do_flush (RunContext c, bool force)
473 return _diskstream->do_flush (c, force);
476 void
477 Track::set_pending_overwrite (bool o)
479 _diskstream->set_pending_overwrite (o);
483 Track::seek (nframes_t s, bool complete_refill)
485 return _diskstream->seek (s, complete_refill);
488 bool
489 Track::hidden () const
491 return _diskstream->hidden ();
495 Track::can_internal_playback_seek (nframes_t d)
497 return _diskstream->can_internal_playback_seek (d);
501 Track::internal_playback_seek (nframes_t d)
503 return _diskstream->internal_playback_seek (d);
506 void
507 Track::non_realtime_input_change ()
509 _diskstream->non_realtime_input_change ();
512 void
513 Track::non_realtime_locate (nframes_t p)
515 _diskstream->non_realtime_locate (p);
518 void
519 Track::non_realtime_set_speed ()
521 _diskstream->non_realtime_set_speed ();
525 Track::overwrite_existing_buffers ()
527 return _diskstream->overwrite_existing_buffers ();
530 nframes_t
531 Track::get_captured_frames (uint32_t n)
533 return _diskstream->get_captured_frames (n);
537 Track::set_loop (Location* l)
539 return _diskstream->set_loop (l);
542 void
543 Track::transport_looped (nframes_t f)
545 _diskstream->transport_looped (f);
548 bool
549 Track::realtime_set_speed (double s, bool g)
551 return _diskstream->realtime_set_speed (s, g);
554 void
555 Track::transport_stopped_wallclock (struct tm & n, time_t t, bool g)
557 _diskstream->transport_stopped_wallclock (n, t, g);
560 bool
561 Track::pending_overwrite () const
563 return _diskstream->pending_overwrite ();
566 double
567 Track::speed () const
569 return _diskstream->speed ();
572 void
573 Track::prepare_to_stop (framepos_t p)
575 _diskstream->prepare_to_stop (p);
578 void
579 Track::set_slaved (bool s)
581 _diskstream->set_slaved (s);
584 ChanCount
585 Track::n_channels ()
587 return _diskstream->n_channels ();
590 nframes_t
591 Track::get_capture_start_frame (uint32_t n)
593 return _diskstream->get_capture_start_frame (n);
596 AlignStyle
597 Track::alignment_style () const
599 return _diskstream->alignment_style ();
602 void
603 Track::set_record_enabled (bool r)
605 _diskstream->set_record_enabled (r);
608 nframes_t
609 Track::current_capture_start () const
611 return _diskstream->current_capture_start ();
614 nframes_t
615 Track::current_capture_end () const
617 return _diskstream->current_capture_end ();
620 void
621 Track::playlist_modified ()
623 _diskstream->playlist_modified ();
627 Track::use_playlist (boost::shared_ptr<Playlist> p)
629 return _diskstream->use_playlist (p);
633 Track::use_copy_playlist ()
635 return _diskstream->use_copy_playlist ();
639 Track::use_new_playlist ()
641 return _diskstream->use_new_playlist ();
644 uint32_t
645 Track::read_data_count () const
647 return _diskstream->read_data_count ();
650 void
651 Track::set_align_style (AlignStyle s)
653 _diskstream->set_align_style (s);
656 uint32_t
657 Track::write_data_count () const
659 return _diskstream->write_data_count ();
662 PBD::ID const &
663 Track::diskstream_id () const
665 return _diskstream->id ();
668 void
669 Track::set_block_size (nframes_t n)
671 Route::set_block_size (n);
672 _diskstream->set_block_size (n);
675 void
676 Track::adjust_playback_buffering ()
678 if (_diskstream) {
679 _diskstream->adjust_playback_buffering ();
683 void
684 Track::adjust_capture_buffering ()
686 if (_diskstream) {
687 _diskstream->adjust_capture_buffering ();