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