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