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