re-apply roll-delay after seek - fixes #5781
[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/debug.h"
22 #include "ardour/delivery.h"
23 #include "ardour/diskstream.h"
24 #include "ardour/io_processor.h"
25 #include "ardour/meter.h"
26 #include "ardour/playlist.h"
27 #include "ardour/port.h"
28 #include "ardour/processor.h"
29 #include "ardour/route_group_specialized.h"
30 #include "ardour/session.h"
31 #include "ardour/session_playlists.h"
32 #include "ardour/track.h"
33 #include "ardour/utils.h"
34
35 #include "i18n.h"
36
37 using namespace std;
38 using namespace ARDOUR;
39 using namespace PBD;
40
41 Track::Track (Session& sess, string name, Route::Flag flag, TrackMode mode, DataType default_type)
42         : Route (sess, name, flag, default_type)
43         , _saved_meter_point (_meter_point)
44         , _mode (mode)
45         , _monitoring (MonitorAuto)
46 {
47         _freeze_record.state = NoFreeze;
48         _declickable = true;
49 }
50
51 Track::~Track ()
52 {
53         DEBUG_TRACE (DEBUG::Destruction, string_compose ("track %1 destructor\n", _name));
54 }
55
56 int
57 Track::init ()
58 {
59         if (Route::init ()) {
60                 return -1;
61         }
62
63         boost::shared_ptr<Route> rp (shared_from_this());
64         boost::shared_ptr<Track> rt = boost::dynamic_pointer_cast<Track> (rp);
65         _rec_enable_control = boost::shared_ptr<RecEnableControl> (new RecEnableControl(rt));
66         _rec_enable_control->set_flags (Controllable::Toggle);
67
68         /* don't add rec_enable_control to controls because we don't want it to
69          * appear as an automatable parameter
70          */
71         track_number_changed.connect_same_thread (*this, boost::bind (&Track::resync_track_name, this));
72         _session.config.ParameterChanged.connect_same_thread (*this, boost::bind (&Track::parameter_changed, this, _1));
73
74         return 0;
75 }
76
77 void
78 Track::use_new_diskstream ()
79 {
80         boost::shared_ptr<Diskstream> ds = create_diskstream ();
81
82         ds->do_refill_with_alloc ();
83         ds->set_block_size (_session.get_block_size ());
84         ds->playlist()->set_orig_track_id (id());
85
86         set_diskstream (ds);
87 }
88
89 XMLNode&
90 Track::get_state ()
91 {
92         return state (true);
93 }
94
95 XMLNode&
96 Track::state (bool full)
97 {
98         XMLNode& root (Route::state (full));
99         root.add_property (X_("monitoring"), enum_2_string (_monitoring));
100         root.add_property (X_("saved-meter-point"), enum_2_string (_saved_meter_point));
101         root.add_child_nocopy (_rec_enable_control->get_state());
102         root.add_child_nocopy (_diskstream->get_state ());
103         
104         return root;
105 }       
106
107 int
108 Track::set_state (const XMLNode& node, int version)
109 {
110         if (Route::set_state (node, version)) {
111                 return -1;
112         }
113
114         XMLNode* child;
115
116         if (version >= 3000) {
117                 if ((child = find_named_node (node, X_("Diskstream"))) != 0) {
118                         boost::shared_ptr<Diskstream> ds = diskstream_factory (*child);
119                         ds->do_refill_with_alloc ();
120                         set_diskstream (ds);
121                 }
122         }
123
124         if (_diskstream) {
125                 _diskstream->playlist()->set_orig_track_id (id());
126         }
127
128         /* set rec-enable control *AFTER* setting up diskstream, because it may
129            want to operate on the diskstream as it sets its own state
130         */
131
132         XMLNodeList nlist = node.children();
133         for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
134                 child = *niter;
135
136                 XMLProperty* prop;
137                 if (child->name() == Controllable::xml_node_name && (prop = child->property ("name")) != 0) {
138                         if (prop->value() == X_("recenable")) {
139                                 _rec_enable_control->set_state (*child, version);
140                         }
141                 }
142         }
143         
144         const XMLProperty* prop;
145
146         if ((prop = node.property (X_("monitoring"))) != 0) {
147                 _monitoring = MonitorChoice (string_2_enum (prop->value(), _monitoring));
148         } else {
149                 _monitoring = MonitorAuto;
150         }
151
152         if ((prop = node.property (X_("saved-meter-point"))) != 0) {
153                 _saved_meter_point = MeterPoint (string_2_enum (prop->value(), _saved_meter_point));
154         } else {
155                 _saved_meter_point = _meter_point;
156         }
157
158         return 0;
159 }
160
161 XMLNode&
162 Track::get_template ()
163 {
164         return state (false);
165 }
166
167 Track::FreezeRecord::~FreezeRecord ()
168 {
169         for (vector<FreezeRecordProcessorInfo*>::iterator i = processor_info.begin(); i != processor_info.end(); ++i) {
170                 delete *i;
171         }
172 }
173
174 Track::FreezeState
175 Track::freeze_state() const
176 {
177         return _freeze_record.state;
178 }
179
180 Track::RecEnableControl::RecEnableControl (boost::shared_ptr<Track> t)
181         : AutomationControl (t->session(),
182                              RecEnableAutomation,
183                              ParameterDescriptor(Evoral::Parameter(RecEnableAutomation)),
184                              boost::shared_ptr<AutomationList>(),
185                              X_("recenable"))
186         , track (t)
187 {
188         boost::shared_ptr<AutomationList> gl(new AutomationList(Evoral::Parameter(RecEnableAutomation)));
189         set_list (gl);
190 }
191
192 void
193 Track::RecEnableControl::set_value (double val)
194 {
195         boost::shared_ptr<Track> t = track.lock ();
196         if (!t) {
197                 return;
198         }
199         
200         t->set_record_enabled (val >= 0.5 ? true : false, this);
201 }
202
203 double
204 Track::RecEnableControl::get_value () const
205 {
206         boost::shared_ptr<Track> t = track.lock ();
207         if (!t) {
208                 return 0;
209         }
210         
211         return (t->record_enabled() ? 1.0 : 0.0);
212 }
213
214 bool
215 Track::record_enabled () const
216 {
217         return _diskstream && _diskstream->record_enabled ();
218 }
219
220 bool
221 Track::can_record()
222 {
223         bool will_record = true;
224         for (PortSet::iterator i = _input->ports().begin(); i != _input->ports().end() && will_record; ++i) {
225                 if (!i->connected())
226                         will_record = false;
227         }
228
229         return will_record;
230 }
231
232 void
233 Track::prep_record_enabled (bool yn, void *src)
234 {
235         if (yn && record_safe ()) {
236             return;
237         }
238         
239         if (!_session.writable()) {
240                 return;
241         }
242
243         if (_freeze_record.state == Frozen) {
244                 return;
245         }
246
247         if (_route_group && src != _route_group && _route_group->is_active() && _route_group->is_recenable()) {
248                 _route_group->apply (&Track::prep_record_enabled, yn, _route_group);
249                 return;
250         }
251
252         /* keep track of the meter point as it was before we rec-enabled */
253         if (!_diskstream->record_enabled()) {
254                 _saved_meter_point = _meter_point;
255         }
256
257         bool will_follow;
258         
259         if (yn) {
260                 will_follow = _diskstream->prep_record_enable ();
261         } else {
262                 will_follow = _diskstream->prep_record_disable ();
263         }
264
265         if (will_follow) {
266                 if (yn) {
267                         if (_meter_point != MeterCustom) {
268                                 set_meter_point (MeterInput);
269                         }
270                 } else {
271                         set_meter_point (_saved_meter_point);
272                 }
273         }
274 }
275
276 void
277 Track::set_record_enabled (bool yn, void *src)
278 {
279         if (_diskstream->record_safe ()) {
280             return;
281         }
282         
283         if (!_session.writable()) {
284                 return;
285         }
286
287         if (_freeze_record.state == Frozen) {
288                 return;
289         }
290
291         if (_route_group && src != _route_group && _route_group->is_active() && _route_group->is_recenable()) {
292                 _route_group->apply (&Track::set_record_enabled, yn, _route_group);
293                 return;
294         }
295
296         _diskstream->set_record_enabled (yn);
297
298         _rec_enable_control->Changed ();
299 }
300
301 bool
302 Track::record_safe () const
303 {
304         return _diskstream && _diskstream->record_safe ();
305 }
306
307 void
308 Track::set_record_safe (bool yn, void *src)
309 {
310         if (!_session.writable()) { /* REQUIRES REVIEW */
311                 return;
312         }
313         
314         if (_freeze_record.state == Frozen) { /* REQUIRES REVIEW */
315                 return;
316         }
317         
318         if (_route_group && src != _route_group && _route_group->is_active() && _route_group->is_recenable()) {
319                 _route_group->apply (&Track::set_record_safe, yn, _route_group);
320                 return;
321         }
322         
323         _diskstream->set_record_safe (yn);
324 }
325
326 void
327 Track::parameter_changed (string const & p)
328 {
329         if (p == "track-name-number") {
330                 resync_track_name ();
331         }
332         else if (p == "track-name-take") {
333                 resync_track_name ();
334         }
335         else if (p == "take-name") {
336                 if (_session.config.get_track_name_take()) {
337                         resync_track_name ();
338                 }
339         }
340 }
341
342 void
343 Track::resync_track_name ()
344 {
345         set_name(name());
346 }
347
348 bool
349 Track::set_name (const string& str)
350 {
351         bool ret;
352
353         if (record_enabled() && _session.actively_recording()) {
354                 /* this messes things up if done while recording */
355                 return false;
356         }
357
358         string diskstream_name = "";
359         if (_session.config.get_track_name_take () && !_session.config.get_take_name ().empty()) {
360                 // Note: any text is fine, legalize_for_path() fixes this later
361                 diskstream_name += _session.config.get_take_name ();
362                 diskstream_name += "_";
363         }
364         const int64_t tracknumber = track_number();
365         if (tracknumber > 0 && _session.config.get_track_name_number()) {
366                 char num[64], fmt[10];
367                 snprintf(fmt, sizeof(fmt), "%%0%d" PRId64, _session.track_number_decimals());
368                 snprintf(num, sizeof(num), fmt, tracknumber);
369                 diskstream_name += num;
370                 diskstream_name += "_";
371         }
372         diskstream_name += str;
373
374         if (diskstream_name == _diskstream_name) {
375                 return true;
376         }
377         _diskstream_name = diskstream_name;
378
379         _diskstream->set_write_source_name (diskstream_name);
380
381         boost::shared_ptr<Track> me = boost::dynamic_pointer_cast<Track> (shared_from_this ());
382         if (_diskstream->playlist()->all_regions_empty () && _session.playlists->playlists_for_track (me).size() == 1) {
383                 /* Only rename the diskstream (and therefore the playlist) if
384                    a) the playlist has never had a region added to it and
385                    b) there is only one playlist for this track.
386
387                    If (a) is not followed, people can get confused if, say,
388                    they have notes about a playlist with a given name and then
389                    it changes (see mantis #4759).
390
391                    If (b) is not followed, we rename the current playlist and not
392                    the other ones, which is a bit confusing (see mantis #4977).
393                 */
394                 _diskstream->set_name (str);
395         }
396
397         /* save state so that the statefile fully reflects any filename changes */
398
399         if ((ret = Route::set_name (str)) == 0) {
400                 _session.save_state ("");
401         }
402
403         return ret;
404 }
405
406 void
407 Track::set_latency_compensation (framecnt_t longest_session_latency)
408 {
409         Route::set_latency_compensation (longest_session_latency);
410         _diskstream->set_roll_delay (_roll_delay);
411 }
412
413 int
414 Track::no_roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, bool session_state_changing)
415 {
416         Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
417
418         if (!lm.locked()) {
419                 return 0;
420         }
421
422         bool can_record = _session.actively_recording ();
423
424         /* no outputs? nothing to do ... what happens if we have sends etc. ? */
425
426         if (n_outputs().n_total() == 0) {
427                 return 0;
428         }
429
430         /* not active ... do the minimum possible by just outputting silence */
431
432         if (!_active) {
433                 silence (nframes);
434                 if (_meter_point == MeterInput && (_monitoring & MonitorInput || _diskstream->record_enabled())) {
435                         _meter->reset();
436                 }
437                 return 0;
438         }
439
440         if (session_state_changing) {
441                 if (_session.transport_speed() != 0.0f) {
442                         /* we're rolling but some state is changing (e.g. our diskstream contents)
443                            so we cannot use them. Be silent till this is over. Don't declick.
444
445                            XXX note the absurdity of ::no_roll() being called when we ARE rolling!
446                         */
447                         passthru_silence (start_frame, end_frame, nframes, 0);
448                         return 0;
449                 }
450                 /* we're really not rolling, so we're either delivery silence or actually
451                    monitoring, both of which are safe to do while session_state_changing is true.
452                 */
453         }
454
455         _diskstream->check_record_status (start_frame, can_record);
456
457         bool be_silent;
458
459         MonitorState const s = monitoring_state ();
460         /* we are not rolling, so be silent even if we are monitoring disk, as there
461            will be no disk data coming in.
462         */
463         switch (s) {
464         case MonitoringSilence:
465                 be_silent = true;
466                 break;
467         case MonitoringDisk:
468                 be_silent = true;
469                 break;
470         case MonitoringInput:
471                 be_silent = false;
472                 break;
473         default:
474                 be_silent = false;
475                 break;
476         }
477         
478         //if we have an internal generator, let it play regardless of monitoring state
479         if (_have_internal_generator) {
480                 be_silent = false;
481         }
482         
483         _amp->apply_gain_automation (false);
484
485         /* if have_internal_generator, or .. */
486
487         if (be_silent) {
488
489                 if (_meter_point == MeterInput) {
490                         /* still need input monitoring and metering */
491
492                         bool const track_rec = _diskstream->record_enabled ();
493                         bool const auto_input = _session.config.get_auto_input ();
494                         bool const software_monitor = Config->get_monitoring_model() == SoftwareMonitoring;
495                         bool const tape_machine_mode = Config->get_tape_machine_mode ();
496                         bool no_meter = false;
497
498                         /* this needs a proper K-map
499                          * and should be separated into a function similar to monitoring_state()
500                          * that also handles roll() states in audio_track.cc, midi_track.cc and route.cc
501                          *
502                          * see http://www.oofus.co.uk/ardour/Ardour3MonitorModesV3.pdf
503                          */
504                         if (!auto_input && !track_rec) {
505                                 no_meter=true;
506                         }
507                         else if (tape_machine_mode && !track_rec && auto_input) {
508                                 no_meter=true;
509                         }
510                         else if (!software_monitor && tape_machine_mode && !track_rec) {
511                                 no_meter=true;
512                         }
513                         else if (!software_monitor && !tape_machine_mode && !track_rec && !auto_input) {
514                                 no_meter=true;
515                         }
516
517                         if (no_meter) {
518                                 BufferSet& bufs (_session.get_silent_buffers (n_process_buffers()));
519                                 _meter->run (bufs, 0, 0, nframes, true);
520                                 _input->process_input (boost::shared_ptr<Processor>(), start_frame, end_frame, nframes);
521                         } else {
522                                 _input->process_input (_meter, start_frame, end_frame, nframes);
523                         }
524                 }
525
526                 passthru_silence (start_frame, end_frame, nframes, 0);
527
528         } else {
529
530                 BufferSet& bufs = _session.get_route_buffers (n_process_buffers());
531                 
532                 fill_buffers_with_input (bufs, _input, nframes);
533
534                 if (_meter_point == MeterInput) {
535                         _meter->run (bufs, start_frame, end_frame, nframes, true);
536                 }
537
538                 passthru (bufs, start_frame, end_frame, nframes, false);
539         }
540
541         for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
542                 boost::shared_ptr<Delivery> d = boost::dynamic_pointer_cast<Delivery> (*i);
543                 if (d) {
544                         d->flush_buffers (nframes);
545                 }
546         }
547
548         return 0;
549 }
550
551 int
552 Track::silent_roll (pframes_t nframes, framepos_t /*start_frame*/, framepos_t /*end_frame*/, bool& need_butler)
553 {
554         Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
555         if (!lm.locked()) {
556                 framecnt_t playback_distance = _diskstream->calculate_playback_distance(nframes);
557                 if (can_internal_playback_seek(playback_distance)) {
558                         internal_playback_seek(playback_distance);
559                 }
560                 return 0;
561         }
562
563         if (n_outputs().n_total() == 0 && _processors.empty()) {
564                 return 0;
565         }
566
567         if (!_active) {
568                 silence (nframes);
569                 return 0;
570         }
571
572         _silent = true;
573         _amp->apply_gain_automation(false);
574
575         silence (nframes);
576
577         framecnt_t playback_distance;
578
579         BufferSet& bufs (_session.get_route_buffers (n_process_buffers(), true));
580
581         int const dret = _diskstream->process (bufs, _session.transport_frame(), nframes, playback_distance, false);
582         need_butler = _diskstream->commit (playback_distance);
583         return dret;
584 }
585
586 void
587 Track::set_diskstream (boost::shared_ptr<Diskstream> ds)
588 {
589         _diskstream = ds;
590
591         ds->PlaylistChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_playlist_changed, this));
592         diskstream_playlist_changed ();
593         ds->RecordEnableChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_record_enable_changed, this));
594         ds->RecordSafeChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_record_safe_changed, this));
595         ds->SpeedChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_speed_changed, this));
596         ds->AlignmentStyleChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_alignment_style_changed, this));
597 }
598
599 void
600 Track::diskstream_playlist_changed ()
601 {
602         PlaylistChanged (); /* EMIT SIGNAL */
603 }
604
605 void
606 Track::diskstream_record_enable_changed ()
607 {
608         RecordEnableChanged (); /* EMIT SIGNAL */
609 }
610
611 void
612 Track::diskstream_record_safe_changed ()
613 {
614         RecordSafeChanged (); /* EMIT SIGNAL */
615 }
616
617 void
618 Track::diskstream_speed_changed ()
619 {
620         SpeedChanged (); /* EMIT SIGNAL */
621 }
622
623 void
624 Track::diskstream_alignment_style_changed ()
625 {
626         AlignmentStyleChanged (); /* EMIT SIGNAL */
627 }
628
629 boost::shared_ptr<Playlist>
630 Track::playlist ()
631 {
632         return _diskstream->playlist ();
633 }
634
635 void
636 Track::request_input_monitoring (bool m)
637 {
638         _diskstream->request_input_monitoring (m);
639 }
640
641 void
642 Track::ensure_input_monitoring (bool m)
643 {
644         _diskstream->ensure_input_monitoring (m);
645 }
646
647 bool
648 Track::destructive () const
649 {
650         return _diskstream->destructive ();
651 }
652
653 list<boost::shared_ptr<Source> > &
654 Track::last_capture_sources ()
655 {
656         return _diskstream->last_capture_sources ();
657 }
658
659 void
660 Track::set_capture_offset ()
661 {
662         _diskstream->set_capture_offset ();
663 }
664
665 std::string
666 Track::steal_write_source_name()
667 {
668         return _diskstream->steal_write_source_name ();
669 }
670
671 void
672 Track::reset_write_sources (bool r, bool force)
673 {
674         _diskstream->reset_write_sources (r, force);
675 }
676
677 float
678 Track::playback_buffer_load () const
679 {
680         return _diskstream->playback_buffer_load ();
681 }
682
683 float
684 Track::capture_buffer_load () const
685 {
686         return _diskstream->capture_buffer_load ();
687 }
688
689 int
690 Track::do_refill ()
691 {
692         return _diskstream->do_refill ();
693 }
694
695 int
696 Track::do_flush (RunContext c, bool force)
697 {
698         return _diskstream->do_flush (c, force);
699 }
700
701 void
702 Track::set_pending_overwrite (bool o)
703 {
704         _diskstream->set_pending_overwrite (o);
705 }
706
707 int
708 Track::seek (framepos_t p, bool complete_refill)
709 {
710         return _diskstream->seek (p, complete_refill);
711 }
712
713 bool
714 Track::hidden () const
715 {
716         return _diskstream->hidden ();
717 }
718
719 int
720 Track::can_internal_playback_seek (framecnt_t p)
721 {
722         return _diskstream->can_internal_playback_seek (p);
723 }
724
725 int
726 Track::internal_playback_seek (framecnt_t p)
727 {
728         return _diskstream->internal_playback_seek (p);
729 }
730
731 void
732 Track::non_realtime_input_change ()
733 {
734         _diskstream->non_realtime_input_change ();
735 }
736
737 void
738 Track::non_realtime_locate (framepos_t p)
739 {
740         Route::non_realtime_locate (p);
741
742         if (!hidden()) {
743                 /* don't waste i/o cycles and butler calls
744                    for hidden (secret) tracks
745                 */
746                 _diskstream->non_realtime_locate (p);
747         }
748 }
749
750 void
751 Track::non_realtime_set_speed ()
752 {
753         _diskstream->non_realtime_set_speed ();
754 }
755
756 int
757 Track::overwrite_existing_buffers ()
758 {
759         return _diskstream->overwrite_existing_buffers ();
760 }
761
762 framecnt_t
763 Track::get_captured_frames (uint32_t n) const
764 {
765         return _diskstream->get_captured_frames (n);
766 }
767
768 int
769 Track::set_loop (Location* l)
770 {
771         return _diskstream->set_loop (l);
772 }
773
774 void
775 Track::transport_looped (framepos_t p)
776 {
777         _diskstream->transport_looped (p);
778 }
779
780 bool
781 Track::realtime_set_speed (double s, bool g)
782 {
783         return _diskstream->realtime_set_speed (s, g);
784 }
785
786 void
787 Track::transport_stopped_wallclock (struct tm & n, time_t t, bool g)
788 {
789         _diskstream->transport_stopped_wallclock (n, t, g);
790 }
791
792 bool
793 Track::pending_overwrite () const
794 {
795         return _diskstream->pending_overwrite ();
796 }
797
798 double
799 Track::speed () const
800 {
801         return _diskstream->speed ();
802 }
803
804 void
805 Track::prepare_to_stop (framepos_t t, framepos_t a)
806 {
807         _diskstream->prepare_to_stop (t, a);
808 }
809
810 void
811 Track::set_slaved (bool s)
812 {
813         _diskstream->set_slaved (s);
814 }
815
816 ChanCount
817 Track::n_channels ()
818 {
819         return _diskstream->n_channels ();
820 }
821
822 framepos_t
823 Track::get_capture_start_frame (uint32_t n) const
824 {
825         return _diskstream->get_capture_start_frame (n);
826 }
827
828 AlignStyle
829 Track::alignment_style () const
830 {
831         return _diskstream->alignment_style ();
832 }
833
834 AlignChoice
835 Track::alignment_choice () const
836 {
837         return _diskstream->alignment_choice ();
838 }
839
840 framepos_t
841 Track::current_capture_start () const
842 {
843         return _diskstream->current_capture_start ();
844 }
845
846 framepos_t
847 Track::current_capture_end () const
848 {
849         return _diskstream->current_capture_end ();
850 }
851
852 void
853 Track::playlist_modified ()
854 {
855         _diskstream->playlist_modified ();
856 }
857
858 int
859 Track::use_playlist (boost::shared_ptr<Playlist> p)
860 {
861         int ret = _diskstream->use_playlist (p);
862         if (ret == 0) {
863                 p->set_orig_track_id (id());
864         }
865         return ret;
866 }
867
868 int
869 Track::use_copy_playlist ()
870 {
871         int ret =  _diskstream->use_copy_playlist ();
872
873         if (ret == 0) {
874                 _diskstream->playlist()->set_orig_track_id (id());
875         }
876
877         return ret;
878 }
879
880 int
881 Track::use_new_playlist ()
882 {
883         int ret = _diskstream->use_new_playlist ();
884
885         if (ret == 0) {
886                 _diskstream->playlist()->set_orig_track_id (id());
887         }
888
889         return ret;
890 }
891
892 void
893 Track::set_align_style (AlignStyle s, bool force)
894 {
895         _diskstream->set_align_style (s, force);
896 }
897
898 void
899 Track::set_align_choice (AlignChoice s, bool force)
900 {
901         _diskstream->set_align_choice (s, force);
902 }
903
904 bool
905 Track::using_diskstream_id (PBD::ID id) const
906 {
907         return (id == _diskstream->id ());
908 }
909
910 void
911 Track::set_block_size (pframes_t n)
912 {
913         Route::set_block_size (n);
914         _diskstream->set_block_size (n);
915 }
916
917 void
918 Track::adjust_playback_buffering ()
919 {
920         if (_diskstream) {
921                 _diskstream->adjust_playback_buffering ();
922         }
923 }
924
925 void
926 Track::adjust_capture_buffering ()
927 {
928         if (_diskstream) {
929                 _diskstream->adjust_capture_buffering ();
930         }
931 }
932
933 #ifdef USE_TRACKS_CODE_FEATURES
934
935 /* This is the Tracks version of Track::monitoring_state(). 
936  *
937  * Ardour developers: try to flag or fix issues if parts of the libardour API 
938  * change in ways that invalidate this
939  */
940
941 MonitorState
942 Track::monitoring_state () const
943 {
944         /* Explicit requests */
945         
946         if (_monitoring & MonitorInput) {
947                 return MonitoringInput;
948         }
949                 
950         if (_monitoring & MonitorDisk) {
951                 return MonitoringDisk;
952         }
953
954         /* This is an implementation of the truth table in doc/monitor_modes.pdf;
955            I don't think it's ever going to be too pretty too look at.
956         */
957         
958         // GZ: NOT USED IN TRACKS
959         //bool const auto_input = _session.config.get_auto_input ();
960         //bool const software_monitor = Config->get_monitoring_model() == SoftwareMonitoring;
961         //bool const tape_machine_mode = Config->get_tape_machine_mode ();
962         
963         bool const roll = _session.transport_rolling ();
964         bool const track_rec = _diskstream->record_enabled ();
965         bool session_rec = _session.actively_recording ();
966         
967         if (track_rec) {
968
969                 if (!session_rec && roll) {
970                         return MonitoringDisk;
971                 } else {
972                         return MonitoringInput;
973                 }
974
975         } else {
976
977                 if (roll) {
978                         return MonitoringDisk;
979                 }
980         }
981
982         return MonitoringSilence;
983 }
984
985 #else
986
987 /* This is the Ardour/Mixbus version of Track::monitoring_state(). 
988  *
989  * Tracks developers: do NOT modify this method under any circumstances.
990  */
991
992 MonitorState
993 Track::monitoring_state () const
994 {
995         /* Explicit requests */
996         
997         if (_monitoring & MonitorInput) {
998                 return MonitoringInput;
999         }
1000                 
1001         if (_monitoring & MonitorDisk) {
1002                 return MonitoringDisk;
1003         }
1004
1005         /* This is an implementation of the truth table in doc/monitor_modes.pdf;
1006            I don't think it's ever going to be too pretty too look at.
1007         */
1008
1009         bool const roll = _session.transport_rolling ();
1010         bool const track_rec = _diskstream->record_enabled ();
1011         bool const auto_input = _session.config.get_auto_input ();
1012         bool const software_monitor = Config->get_monitoring_model() == SoftwareMonitoring;
1013         bool const tape_machine_mode = Config->get_tape_machine_mode ();
1014         bool session_rec;
1015
1016         /* I suspect that just use actively_recording() is good enough all the
1017          * time, but just to keep the semantics the same as they were before
1018          * sept 26th 2012, we differentiate between the cases where punch is
1019          * enabled and those where it is not.
1020          */
1021
1022         if (_session.config.get_punch_in() || _session.config.get_punch_out()) {
1023                 session_rec = _session.actively_recording ();
1024         } else {
1025                 session_rec = _session.get_record_enabled();
1026         }
1027
1028         if (track_rec) {
1029
1030                 if (!session_rec && roll && auto_input) {
1031                         return MonitoringDisk;
1032                 } else {
1033                         return software_monitor ? MonitoringInput : MonitoringSilence;
1034                 }
1035
1036         } else {
1037
1038                 if (tape_machine_mode) {
1039
1040                         return MonitoringDisk;
1041
1042                 } else {
1043
1044                         if (!roll && auto_input) {
1045                                 return software_monitor ? MonitoringInput : MonitoringSilence;
1046                         } else {
1047                                 return MonitoringDisk;
1048                         }
1049                         
1050                 }
1051         }
1052
1053         abort(); /* NOTREACHED */
1054         return MonitoringSilence;
1055 }
1056
1057 #endif
1058
1059 void
1060 Track::maybe_declick (BufferSet& bufs, framecnt_t nframes, int declick)
1061 {
1062         /* never declick if there is an internal generator - we just want it to
1063            keep generating sound without interruption.
1064
1065            ditto if we are monitoring inputs.
1066         */
1067
1068         if (_have_internal_generator || monitoring_choice() == MonitorInput) {
1069                 return;
1070         }
1071
1072         if (!declick) {
1073                 declick = _pending_declick;
1074         }
1075
1076         if (declick != 0) {
1077                 Amp::declick (bufs, nframes, declick);
1078         }
1079 }
1080
1081 framecnt_t
1082 Track::check_initial_delay (framecnt_t nframes, framepos_t& transport_frame)
1083 {
1084         if (_roll_delay > nframes) {
1085
1086                 _roll_delay -= nframes;
1087                 silence_unlocked (nframes);
1088                 /* transport frame is not legal for caller to use */
1089                 return 0;
1090
1091         } else if (_roll_delay > 0) {
1092
1093                 nframes -= _roll_delay;
1094                 silence_unlocked (_roll_delay);
1095                 transport_frame += _roll_delay;
1096
1097                 /* shuffle all the port buffers for things that lead "out" of this Route
1098                    to reflect that we just wrote _roll_delay frames of silence.
1099                 */
1100
1101                 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
1102                 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1103                         boost::shared_ptr<IOProcessor> iop = boost::dynamic_pointer_cast<IOProcessor> (*i);
1104                         if (iop) {
1105                                 iop->increment_port_buffer_offset (_roll_delay);
1106                         }
1107                 }
1108                 _output->increment_port_buffer_offset (_roll_delay);
1109
1110                 _roll_delay = 0;
1111
1112         }
1113
1114         return nframes; 
1115 }
1116
1117 void
1118 Track::set_monitoring (MonitorChoice mc)
1119 {
1120         if (mc !=  _monitoring) {
1121                 _monitoring = mc;
1122
1123                 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1124                         (*i)->monitoring_changed ();
1125                 }
1126
1127                 MonitoringChanged (); /* EMIT SIGNAL */
1128         }
1129 }
1130
1131 MeterState
1132 Track::metering_state () const
1133 {
1134         bool rv;
1135         if (_session.transport_rolling ()) {
1136                 // audio_track.cc || midi_track.cc roll() runs meter IFF:
1137                 rv = _meter_point == MeterInput && (_monitoring & MonitorInput || _diskstream->record_enabled());
1138         } else {
1139                 // track no_roll() always metering if
1140                 rv = _meter_point == MeterInput;
1141         }
1142         return rv ? MeteringInput : MeteringRoute;
1143 }
1144