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