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