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