drop connections to incoming MIDI signals before deleting MTC Slave object
[ardour.git] / libs / ardour / track.cc
1 /*
2     Copyright (C) 2006 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18 #include "pbd/error.h"
19
20 #include "ardour/amp.h"
21 #include "ardour/debug.h"
22 #include "ardour/delivery.h"
23 #include "ardour/diskstream.h"
24 #include "ardour/io_processor.h"
25 #include "ardour/meter.h"
26 #include "ardour/playlist.h"
27 #include "ardour/port.h"
28 #include "ardour/processor.h"
29 #include "ardour/route_group_specialized.h"
30 #include "ardour/session.h"
31 #include "ardour/session_playlists.h"
32 #include "ardour/track.h"
33 #include "ardour/utils.h"
34
35 #include "i18n.h"
36
37 using namespace std;
38 using namespace ARDOUR;
39 using namespace PBD;
40
41 Track::Track (Session& sess, string name, Route::Flag flag, TrackMode mode, DataType default_type)
42         : Route (sess, name, flag, default_type)
43         , _saved_meter_point (_meter_point)
44         , _mode (mode)
45         , _monitoring (MonitorAuto)
46 {
47         _freeze_record.state = NoFreeze;
48         _declickable = true;
49
50         Config->ParameterChanged.connect_same_thread (*this, boost::bind (&Track::parameter_changed, this, _1));
51 }
52
53 Track::~Track ()
54 {
55         DEBUG_TRACE (DEBUG::Destruction, string_compose ("track %1 destructor\n", _name));
56 }
57
58 int
59 Track::init ()
60 {
61         if (Route::init ()) {
62                 return -1;
63         }
64
65         boost::shared_ptr<Route> rp (shared_from_this());
66         boost::shared_ptr<Track> rt = boost::dynamic_pointer_cast<Track> (rp);
67         _rec_enable_control = boost::shared_ptr<RecEnableControl> (new RecEnableControl(rt));
68         _rec_enable_control->set_flags (Controllable::Toggle);
69
70         /* don't add rec_enable_control to controls because we don't want it to
71          * appear as an automatable parameter
72          */
73
74         return 0;
75 }
76
77 void
78 Track::use_new_diskstream ()
79 {
80         boost::shared_ptr<Diskstream> ds = create_diskstream ();
81
82         ds->do_refill_with_alloc ();
83         ds->set_block_size (_session.get_block_size ());
84         ds->playlist()->set_orig_track_id (id());
85
86         set_diskstream (ds);
87 }
88
89 XMLNode&
90 Track::get_state ()
91 {
92         return state (true);
93 }
94
95 XMLNode&
96 Track::state (bool full)
97 {
98         XMLNode& root (Route::state (full));
99         root.add_property (X_("monitoring"), enum_2_string (_monitoring));
100         root.add_property (X_("saved-meter-point"), enum_2_string (_saved_meter_point));
101         root.add_child_nocopy (_rec_enable_control->get_state());
102         root.add_child_nocopy (_diskstream->get_state ());
103
104         if (!_deactivated_processors.empty ()) {
105                 XMLNode* node = new XMLNode (X_("DeactivatedProcessors"));
106                 for (list<boost::weak_ptr<Processor> >::iterator i = _deactivated_processors.begin(); i != _deactivated_processors.end(); ++i) {
107                         boost::shared_ptr<Processor> p = i->lock ();
108                         if (p) {
109                                 XMLNode* c = new XMLNode (X_("Processor"));
110                                 c->add_property (X_("id"), p->id().to_s());
111                                 node->add_child_nocopy (*c);
112                         }
113                 }
114                 root.add_child_nocopy (*node);
115         }
116         
117         return root;
118 }       
119
120 int
121 Track::set_state (const XMLNode& node, int version)
122 {
123         if (Route::set_state (node, version)) {
124                 return -1;
125         }
126
127         XMLNode* child;
128
129         if (version >= 3000) {
130                 if ((child = find_named_node (node, X_("Diskstream"))) != 0) {
131                         boost::shared_ptr<Diskstream> ds = diskstream_factory (*child);
132                         ds->do_refill_with_alloc ();
133                         set_diskstream (ds);
134                 }
135         }
136
137         if (_diskstream) {
138                 _diskstream->playlist()->set_orig_track_id (id());
139         }
140
141         /* set rec-enable control *AFTER* setting up diskstream, because it may
142            want to operate on the diskstream as it sets its own state
143         */
144
145         XMLNodeList nlist = node.children();
146         for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
147                 child = *niter;
148
149                 XMLProperty* prop;
150                 if (child->name() == Controllable::xml_node_name && (prop = child->property ("name")) != 0) {
151                         if (prop->value() == X_("recenable")) {
152                                 _rec_enable_control->set_state (*child, version);
153                         }
154                 }
155
156                 if (child->name() == X_("DeactivatedProcessors")) {
157                         XMLNodeList dp = child->children ();
158                         for (XMLNodeConstIterator i = dp.begin(); i != dp.end(); ++i) {
159                                 assert ((*i)->name() == X_("Processor"));
160                                 XMLProperty* prop = (*i)->property (X_("id"));
161                                 boost::shared_ptr<Processor> p = processor_by_id (PBD::ID (prop->value ()));
162                                 if (p) {
163                                         _deactivated_processors.push_back (p);
164                                 }
165                         }
166                 }
167         }
168         
169         const XMLProperty* prop;
170
171         if ((prop = node.property (X_("monitoring"))) != 0) {
172                 _monitoring = MonitorChoice (string_2_enum (prop->value(), _monitoring));
173         } else {
174                 _monitoring = MonitorAuto;
175         }
176
177         if ((prop = node.property (X_("saved-meter-point"))) != 0) {
178                 _saved_meter_point = MeterPoint (string_2_enum (prop->value(), _saved_meter_point));
179         } else {
180                 _saved_meter_point = _meter_point;
181         }
182
183         return 0;
184 }
185
186 XMLNode&
187 Track::get_template ()
188 {
189         return state (false);
190 }
191
192 Track::FreezeRecord::~FreezeRecord ()
193 {
194         for (vector<FreezeRecordProcessorInfo*>::iterator i = processor_info.begin(); i != processor_info.end(); ++i) {
195                 delete *i;
196         }
197 }
198
199 Track::FreezeState
200 Track::freeze_state() const
201 {
202         return _freeze_record.state;
203 }
204
205 Track::RecEnableControl::RecEnableControl (boost::shared_ptr<Track> t)
206         : AutomationControl (t->session(), RecEnableAutomation, boost::shared_ptr<AutomationList>(), X_("recenable"))
207         , track (t)
208 {
209         boost::shared_ptr<AutomationList> gl(new AutomationList(Evoral::Parameter(RecEnableAutomation)));
210         set_list (gl);
211 }
212
213 void
214 Track::RecEnableControl::set_value (double val)
215 {
216         boost::shared_ptr<Track> t = track.lock ();
217         if (!t) {
218                 return;
219         }
220         
221         t->set_record_enabled (val >= 0.5 ? true : false, this);
222 }
223
224 double
225 Track::RecEnableControl::get_value () const
226 {
227         boost::shared_ptr<Track> t = track.lock ();
228         if (!t) {
229                 return 0;
230         }
231         
232         return (t->record_enabled() ? 1.0 : 0.0);
233 }
234
235 bool
236 Track::record_enabled () const
237 {
238         return _diskstream && _diskstream->record_enabled ();
239 }
240
241 bool
242 Track::can_record()
243 {
244         bool will_record = true;
245         for (PortSet::iterator i = _input->ports().begin(); i != _input->ports().end() && will_record; ++i) {
246                 if (!i->connected())
247                         will_record = false;
248         }
249
250         return will_record;
251 }
252
253 /* Turn off visible processors (except Fader), keeping track of the old states */
254 void
255 Track::deactivate_visible_processors ()
256 {
257         _deactivated_processors.clear ();
258         Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
259         
260         for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
261                 if ((*i)->active() && (*i)->display_to_user() && boost::dynamic_pointer_cast<Amp> (*i) == 0) {
262                         (*i)->deactivate ();
263                         _deactivated_processors.push_back (*i);
264                 }
265         }
266 }
267
268 /* Turn deactivated processors back on again */
269 void
270 Track::activate_deactivated_processors ()
271 {
272         for (list<boost::weak_ptr<Processor> >::iterator i = _deactivated_processors.begin(); i != _deactivated_processors.end(); ++i) {
273                 boost::shared_ptr<Processor> p = i->lock ();
274                 if (p) {
275                         p->activate ();
276                 }
277         }
278 }
279
280 void
281 Track::set_record_enabled (bool yn, void *src)
282 {
283         if (!_session.writable()) {
284                 return;
285         }
286
287         if (_freeze_record.state == Frozen) {
288                 return;
289         }
290
291         if (_route_group && src != _route_group && _route_group->is_active() && _route_group->is_recenable()) {
292                 _route_group->apply (&Track::set_record_enabled, yn, _route_group);
293                 return;
294         }
295
296         /* keep track of the meter point as it was before we rec-enabled */
297         if (!_diskstream->record_enabled()) {
298                 _saved_meter_point = _meter_point;
299         }
300
301         if (Config->get_do_not_record_plugins ()) {
302                 if (yn) {
303                         deactivate_visible_processors ();
304                 } else {
305                         activate_deactivated_processors ();
306                 }
307         }
308
309         _diskstream->set_record_enabled (yn);
310
311         if (_diskstream->record_enabled()) {
312                 if (_meter_point != MeterCustom) {
313                         set_meter_point (MeterInput);
314                 }
315         } else {
316                 set_meter_point (_saved_meter_point);
317         }
318
319         _rec_enable_control->Changed ();
320 }
321
322
323 bool
324 Track::set_name (const string& str)
325 {
326         bool ret;
327
328         if (record_enabled() && _session.actively_recording()) {
329                 /* this messes things up if done while recording */
330                 return false;
331         }
332
333         boost::shared_ptr<Track> me = boost::dynamic_pointer_cast<Track> (shared_from_this ());
334         if (_diskstream->playlist()->all_regions_empty () && _session.playlists->playlists_for_track (me).size() == 1) {
335                 /* Only rename the diskstream (and therefore the playlist) if
336                    a) the playlist has never had a region added to it and
337                    b) there is only one playlist for this track.
338
339                    If (a) is not followed, people can get confused if, say,
340                    they have notes about a playlist with a given name and then
341                    it changes (see mantis #4759).
342
343                    If (b) is not followed, we rename the current playlist and not
344                    the other ones, which is a bit confusing (see mantis #4977).
345                 */
346                 _diskstream->set_name (str);
347         }
348
349         /* save state so that the statefile fully reflects any filename changes */
350
351         if ((ret = Route::set_name (str)) == 0) {
352                 _session.save_state ("");
353         }
354
355         return ret;
356 }
357
358 void
359 Track::set_latency_compensation (framecnt_t longest_session_latency)
360 {
361         Route::set_latency_compensation (longest_session_latency);
362         _diskstream->set_roll_delay (_roll_delay);
363 }
364
365 int
366 Track::no_roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, bool session_state_changing)
367 {
368         Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
369         if (!lm.locked()) {
370                 return 0;
371         }
372
373         bool can_record = _session.actively_recording ();
374
375         if (n_outputs().n_total() == 0) {
376                 return 0;
377         }
378
379         if (!_active) {
380                 silence (nframes);
381                 return 0;
382         }
383
384         if (session_state_changing) {
385                 if (_session.transport_speed() != 0.0f) {
386                         /* we're rolling but some state is changing (e.g. our diskstream contents)
387                            so we cannot use them. Be silent till this is over. Don't declick.
388
389                            XXX note the absurdity of ::no_roll() being called when we ARE rolling!
390                         */
391                         passthru_silence (start_frame, end_frame, nframes, 0);
392                         return 0;
393                 }
394                 /* we're really not rolling, so we're either delivery silence or actually
395                    monitoring, both of which are safe to do while session_state_changing is true.
396                 */
397         }
398
399         _diskstream->check_record_status (start_frame, can_record);
400
401         bool be_silent;
402
403         if (_have_internal_generator) {
404                 /* since the instrument has no input streams,
405                    there is no reason to send any signal
406                    into the route.
407                 */
408                 be_silent = true;
409         } else {
410                 MonitorState const s = monitoring_state ();
411                 /* we are not rolling, so be silent even if we are monitoring disk, as there
412                    will be no disk data coming in.
413                 */
414                 be_silent = (s == MonitoringSilence || s == MonitoringDisk);
415         }
416         
417         if (!_have_internal_generator && metering_state() == MeteringInput) {
418                 _input->process_input (_meter, start_frame, end_frame, nframes);
419         }
420
421         _amp->apply_gain_automation(false);
422
423         /* if have_internal_generator, or .. */
424         //_input->process_input (_meter, start_frame, end_frame, nframes);
425
426         if (be_silent) {
427
428                 passthru_silence (start_frame, end_frame, nframes, 0);
429
430         } else {
431
432                 /* we're sending signal, but we may still want to meter the input.
433                  */
434
435                 passthru (start_frame, end_frame, nframes, false);
436         }
437
438         for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
439                 boost::shared_ptr<Delivery> d = boost::dynamic_pointer_cast<Delivery> (*i);
440                 if (d) {
441                         d->flush_buffers (nframes);
442                 }
443         }
444
445         return 0;
446 }
447
448 int
449 Track::silent_roll (pframes_t nframes, framepos_t /*start_frame*/, framepos_t /*end_frame*/, bool& need_butler)
450 {
451         Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
452         if (!lm.locked()) {
453                 return 0;
454         }
455
456         if (n_outputs().n_total() == 0 && _processors.empty()) {
457                 return 0;
458         }
459
460         if (!_active) {
461                 silence (nframes);
462                 return 0;
463         }
464
465         _silent = true;
466         _amp->apply_gain_automation(false);
467
468         silence (nframes);
469
470         framecnt_t playback_distance;
471         int const dret = _diskstream->process (_session.transport_frame(), nframes, playback_distance);
472         need_butler = _diskstream->commit (playback_distance);
473         return dret;
474 }
475
476 void
477 Track::set_diskstream (boost::shared_ptr<Diskstream> ds)
478 {
479         _diskstream = ds;
480
481         ds->PlaylistChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_playlist_changed, this));
482         diskstream_playlist_changed ();
483         ds->RecordEnableChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_record_enable_changed, this));
484         ds->SpeedChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_speed_changed, this));
485         ds->AlignmentStyleChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_alignment_style_changed, this));
486 }
487
488 void
489 Track::diskstream_playlist_changed ()
490 {
491         PlaylistChanged (); /* EMIT SIGNAL */
492 }
493
494 void
495 Track::diskstream_record_enable_changed ()
496 {
497         RecordEnableChanged (); /* EMIT SIGNAL */
498 }
499
500 void
501 Track::diskstream_speed_changed ()
502 {
503         SpeedChanged (); /* EMIT SIGNAL */
504 }
505
506 void
507 Track::diskstream_alignment_style_changed ()
508 {
509         AlignmentStyleChanged (); /* EMIT SIGNAL */
510 }
511
512 boost::shared_ptr<Playlist>
513 Track::playlist ()
514 {
515         return _diskstream->playlist ();
516 }
517
518 void
519 Track::request_jack_monitors_input (bool m)
520 {
521         _diskstream->request_jack_monitors_input (m);
522 }
523
524 void
525 Track::ensure_jack_monitors_input (bool m)
526 {
527         _diskstream->ensure_jack_monitors_input (m);
528 }
529
530 bool
531 Track::destructive () const
532 {
533         return _diskstream->destructive ();
534 }
535
536 list<boost::shared_ptr<Source> > &
537 Track::last_capture_sources ()
538 {
539         return _diskstream->last_capture_sources ();
540 }
541
542 void
543 Track::set_capture_offset ()
544 {
545         _diskstream->set_capture_offset ();
546 }
547
548 list<boost::shared_ptr<Source> >
549 Track::steal_write_sources()
550 {
551         return _diskstream->steal_write_sources ();
552 }
553
554 void
555 Track::reset_write_sources (bool r, bool force)
556 {
557         _diskstream->reset_write_sources (r, force);
558 }
559
560 float
561 Track::playback_buffer_load () const
562 {
563         return _diskstream->playback_buffer_load ();
564 }
565
566 float
567 Track::capture_buffer_load () const
568 {
569         return _diskstream->capture_buffer_load ();
570 }
571
572 int
573 Track::do_refill ()
574 {
575         return _diskstream->do_refill ();
576 }
577
578 int
579 Track::do_flush (RunContext c, bool force)
580 {
581         return _diskstream->do_flush (c, force);
582 }
583
584 void
585 Track::set_pending_overwrite (bool o)
586 {
587         _diskstream->set_pending_overwrite (o);
588 }
589
590 int
591 Track::seek (framepos_t p, bool complete_refill)
592 {
593         return _diskstream->seek (p, complete_refill);
594 }
595
596 bool
597 Track::hidden () const
598 {
599         return _diskstream->hidden ();
600 }
601
602 int
603 Track::can_internal_playback_seek (framecnt_t p)
604 {
605         return _diskstream->can_internal_playback_seek (p);
606 }
607
608 int
609 Track::internal_playback_seek (framecnt_t p)
610 {
611         return _diskstream->internal_playback_seek (p);
612 }
613
614 void
615 Track::non_realtime_input_change ()
616 {
617         _diskstream->non_realtime_input_change ();
618 }
619
620 void
621 Track::non_realtime_locate (framepos_t p)
622 {
623         Route::non_realtime_locate (p);
624
625         if (!hidden()) {
626                 /* don't waste i/o cycles and butler calls
627                    for hidden (secret) tracks
628                 */
629                 _diskstream->non_realtime_locate (p);
630         }
631 }
632
633 void
634 Track::non_realtime_set_speed ()
635 {
636         _diskstream->non_realtime_set_speed ();
637 }
638
639 int
640 Track::overwrite_existing_buffers ()
641 {
642         return _diskstream->overwrite_existing_buffers ();
643 }
644
645 framecnt_t
646 Track::get_captured_frames (uint32_t n) const
647 {
648         return _diskstream->get_captured_frames (n);
649 }
650
651 int
652 Track::set_loop (Location* l)
653 {
654         return _diskstream->set_loop (l);
655 }
656
657 void
658 Track::transport_looped (framepos_t p)
659 {
660         _diskstream->transport_looped (p);
661 }
662
663 bool
664 Track::realtime_set_speed (double s, bool g)
665 {
666         return _diskstream->realtime_set_speed (s, g);
667 }
668
669 void
670 Track::transport_stopped_wallclock (struct tm & n, time_t t, bool g)
671 {
672         _diskstream->transport_stopped_wallclock (n, t, g);
673 }
674
675 bool
676 Track::pending_overwrite () const
677 {
678         return _diskstream->pending_overwrite ();
679 }
680
681 double
682 Track::speed () const
683 {
684         return _diskstream->speed ();
685 }
686
687 void
688 Track::prepare_to_stop (framepos_t p)
689 {
690         _diskstream->prepare_to_stop (p);
691 }
692
693 void
694 Track::set_slaved (bool s)
695 {
696         _diskstream->set_slaved (s);
697 }
698
699 ChanCount
700 Track::n_channels ()
701 {
702         return _diskstream->n_channels ();
703 }
704
705 framepos_t
706 Track::get_capture_start_frame (uint32_t n) const
707 {
708         return _diskstream->get_capture_start_frame (n);
709 }
710
711 AlignStyle
712 Track::alignment_style () const
713 {
714         return _diskstream->alignment_style ();
715 }
716
717 AlignChoice
718 Track::alignment_choice () const
719 {
720         return _diskstream->alignment_choice ();
721 }
722
723 framepos_t
724 Track::current_capture_start () const
725 {
726         return _diskstream->current_capture_start ();
727 }
728
729 framepos_t
730 Track::current_capture_end () const
731 {
732         return _diskstream->current_capture_end ();
733 }
734
735 void
736 Track::playlist_modified ()
737 {
738         _diskstream->playlist_modified ();
739 }
740
741 int
742 Track::use_playlist (boost::shared_ptr<Playlist> p)
743 {
744         int ret = _diskstream->use_playlist (p);
745         if (ret == 0) {
746                 p->set_orig_track_id (id());
747         }
748         return ret;
749 }
750
751 int
752 Track::use_copy_playlist ()
753 {
754         int ret =  _diskstream->use_copy_playlist ();
755
756         if (ret == 0) {
757                 _diskstream->playlist()->set_orig_track_id (id());
758         }
759
760         return ret;
761 }
762
763 int
764 Track::use_new_playlist ()
765 {
766         int ret = _diskstream->use_new_playlist ();
767
768         if (ret == 0) {
769                 _diskstream->playlist()->set_orig_track_id (id());
770         }
771
772         return ret;
773 }
774
775 void
776 Track::set_align_style (AlignStyle s, bool force)
777 {
778         _diskstream->set_align_style (s, force);
779 }
780
781 void
782 Track::set_align_choice (AlignChoice s, bool force)
783 {
784         _diskstream->set_align_choice (s, force);
785 }
786
787 bool
788 Track::using_diskstream_id (PBD::ID id) const
789 {
790         return (id == _diskstream->id ());
791 }
792
793 void
794 Track::set_block_size (pframes_t n)
795 {
796         Route::set_block_size (n);
797         _diskstream->set_block_size (n);
798 }
799
800 void
801 Track::adjust_playback_buffering ()
802 {
803         if (_diskstream) {
804                 _diskstream->adjust_playback_buffering ();
805         }
806 }
807
808 void
809 Track::adjust_capture_buffering ()
810 {
811         if (_diskstream) {
812                 _diskstream->adjust_capture_buffering ();
813         }
814 }
815
816 MonitorState
817 Track::monitoring_state () const
818 {
819         /* Explicit requests */
820         
821         if (_monitoring & MonitorInput) {
822                 return MonitoringInput;
823         }
824                 
825         if (_monitoring & MonitorDisk) {
826                 return MonitoringDisk;
827         }
828
829         /* This is an implementation of the truth table in doc/monitor_modes.pdf;
830            I don't think it's ever going to be too pretty too look at.
831         */
832
833         bool const roll = _session.transport_rolling ();
834         bool const track_rec = _diskstream->record_enabled ();
835         bool const auto_input = _session.config.get_auto_input ();
836         bool const software_monitor = Config->get_monitoring_model() == SoftwareMonitoring;
837         bool const tape_machine_mode = Config->get_tape_machine_mode ();
838         bool session_rec;
839
840         /* I suspect that just use actively_recording() is good enough all the
841          * time, but just to keep the semantics the same as they were before
842          * sept 26th 2012, we differentiate between the cases where punch is
843          * enabled and those where it is not.
844          */
845
846         if (_session.config.get_punch_in() || _session.config.get_punch_out()) {
847                 session_rec = _session.actively_recording ();
848         } else {
849                 session_rec = _session.get_record_enabled();
850         }
851
852         if (track_rec) {
853
854                 if (!session_rec && roll && auto_input) {
855                         return MonitoringDisk;
856                 } else {
857                         return software_monitor ? MonitoringInput : MonitoringSilence;
858                 }
859
860         } else {
861
862                 if (tape_machine_mode) {
863
864                         return MonitoringDisk;
865
866                 } else {
867
868                         if (!roll && auto_input) {
869                                 return software_monitor ? MonitoringInput : MonitoringSilence;
870                         } else {
871                                 return MonitoringDisk;
872                         }
873                         
874                 }
875         }
876
877         /* NOTREACHED */
878         return MonitoringSilence;
879 }
880
881 void
882 Track::maybe_declick (BufferSet& bufs, framecnt_t nframes, int declick)
883 {
884         /* never declick if there is an internal generator - we just want it to
885            keep generating sound without interruption.
886
887            ditto if we are monitoring inputs.
888         */
889
890         if (_have_internal_generator || monitoring_choice() == MonitorInput) {
891                 return;
892         }
893
894         if (!declick) {
895                 declick = _pending_declick;
896         }
897
898         if (declick != 0) {
899                 Amp::declick (bufs, nframes, declick);
900         }
901 }
902
903 framecnt_t
904 Track::check_initial_delay (framecnt_t nframes, framepos_t& transport_frame)
905 {
906         if (_roll_delay > nframes) {
907
908                 _roll_delay -= nframes;
909                 silence_unlocked (nframes);
910                 /* transport frame is not legal for caller to use */
911                 return 0;
912
913         } else if (_roll_delay > 0) {
914
915                 nframes -= _roll_delay;
916                 silence_unlocked (_roll_delay);
917                 transport_frame += _roll_delay;
918
919                 /* shuffle all the port buffers for things that lead "out" of this Route
920                    to reflect that we just wrote _roll_delay frames of silence.
921                 */
922
923                 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
924                 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
925                         boost::shared_ptr<IOProcessor> iop = boost::dynamic_pointer_cast<IOProcessor> (*i);
926                         if (iop) {
927                                 iop->increment_port_buffer_offset (_roll_delay);
928                         }
929                 }
930                 _output->increment_port_buffer_offset (_roll_delay);
931
932                 _roll_delay = 0;
933
934         }
935
936         return nframes; 
937 }
938
939 void
940 Track::set_monitoring (MonitorChoice mc)
941 {
942         if (mc !=  _monitoring) {
943                 _monitoring = mc;
944
945                 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
946                         (*i)->monitoring_changed ();
947                 }
948
949                 MonitoringChanged (); /* EMIT SIGNAL */
950         }
951 }
952
953 void
954 Track::parameter_changed (string p)
955 {
956         if (p != "do-not-record-plugins") {
957                 return;
958         }
959
960         if (record_enabled ()) {
961                 if (Config->get_do_not_record_plugins ()) {
962                         deactivate_visible_processors ();
963                 } else {
964                         activate_deactivated_processors ();
965                 }
966         }
967 }
968         
969 MeterState
970 Track::metering_state () const
971 {
972         return _diskstream->record_enabled() ? MeteringInput : MeteringRoute;
973 }