2 Copyright (C) 2006 Paul Davis
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.
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.
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.
18 #include "pbd/error.h"
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"
43 using namespace ARDOUR;
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)
51 _freeze_record.state = NoFreeze;
57 DEBUG_TRACE (DEBUG::Destruction, string_compose ("track %1 destructor\n", _name));
67 boost::shared_ptr<Route> rp (shared_from_this());
68 boost::shared_ptr<Track> rt = boost::dynamic_pointer_cast<Track> (rp);
70 _record_enable_control.reset (new RecordEnableControl (_session, EventTypeMap::instance().to_symbol (RecEnableAutomation), *this));
71 add_control (_record_enable_control);
73 _record_safe_control.reset (new RecordSafeControl (_session, EventTypeMap::instance().to_symbol (RecSafeAutomation), *this));
74 add_control (_record_safe_control);
76 _monitoring_control.reset (new MonitorControl (_session, EventTypeMap::instance().to_symbol (MonitoringAutomation), *this));
77 add_control (_monitoring_control);
79 _session.config.ParameterChanged.connect_same_thread (*this, boost::bind (&Track::parameter_changed, this, _1));
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));
89 Track::use_new_diskstream ()
91 boost::shared_ptr<Diskstream> ds = create_diskstream ();
93 ds->do_refill_with_alloc ();
94 ds->set_block_size (_session.get_block_size ());
95 ds->playlist()->set_orig_track_id (id());
107 Track::state (bool full)
109 XMLNode& root (Route::state (full));
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 ());
115 root.add_property (X_("saved-meter-point"), enum_2_string (_saved_meter_point));
116 root.add_child_nocopy (_diskstream->get_state ());
122 Track::set_state (const XMLNode& node, int version)
124 if (Route::set_state (node, version)) {
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 ();
139 _diskstream->playlist()->set_orig_track_id (id());
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
146 XMLNodeList nlist = node.children();
147 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
150 XMLProperty const * prop;
152 if (child->name() == Controllable::xml_node_name) {
153 if ((prop = child->property ("name")) == 0) {
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);
167 XMLProperty const * prop;
169 if ((prop = node.property (X_("saved-meter-point"))) != 0) {
170 _saved_meter_point = MeterPoint (string_2_enum (prop->value(), _saved_meter_point));
172 _saved_meter_point = _meter_point;
179 Track::get_template ()
181 return state (false);
184 Track::FreezeRecord::~FreezeRecord ()
186 for (vector<FreezeRecordProcessorInfo*>::iterator i = processor_info.begin(); i != processor_info.end(); ++i) {
192 Track::freeze_state() const
194 return _freeze_record.state;
200 bool will_record = true;
201 for (PortSet::iterator i = _input->ports().begin(); i != _input->ports().end() && will_record; ++i) {
210 Track::prep_record_enabled (bool yn)
212 if (yn && _record_safe_control->get_value()) {
216 if (!can_be_record_enabled()) {
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;
228 will_follow = _diskstream->prep_record_enable ();
230 will_follow = _diskstream->prep_record_disable ();
235 if (_meter_point != MeterCustom) {
236 set_meter_point (MeterInput);
239 set_meter_point (_saved_meter_point);
247 Track::record_enable_changed (bool, Controllable::GroupControlDisposition)
249 _diskstream->set_record_enabled (_record_enable_control->get_value());
253 Track::record_safe_changed (bool, Controllable::GroupControlDisposition)
255 _diskstream->set_record_safe (_record_safe_control->get_value());
259 Track::can_be_record_safe ()
261 return !_record_enable_control->get_value() && _diskstream && _session.writable() && (_freeze_record.state != Frozen);
265 Track::can_be_record_enabled ()
267 return !_record_safe_control->get_value() && _diskstream && !_diskstream->record_safe() && _session.writable() && (_freeze_record.state != Frozen);
271 Track::parameter_changed (string const & p)
273 if (p == "track-name-number") {
274 resync_track_name ();
276 else if (p == "track-name-take") {
277 resync_track_name ();
279 else if (p == "take-name") {
280 if (_session.config.get_track_name_take()) {
281 resync_track_name ();
287 Track::resync_track_name ()
293 Track::set_name (const string& str)
301 if (_record_enable_control->get_value()) {
302 /* when re-arm'ed the file (named after the track) is already ready to rolll */
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 += "_";
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 += "_";
320 diskstream_name += str;
322 if (diskstream_name == _diskstream_name) {
325 _diskstream_name = diskstream_name;
327 _diskstream->set_write_source_name (diskstream_name);
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.
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).
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).
342 _diskstream->set_name (str);
345 /* save state so that the statefile fully reflects any filename changes */
347 if ((ret = Route::set_name (str)) == 0) {
348 _session.save_state ("");
355 Track::set_latency_compensation (framecnt_t longest_session_latency)
357 Route::set_latency_compensation (longest_session_latency);
358 _diskstream->set_roll_delay (_roll_delay);
362 Track::no_roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, bool session_state_changing)
364 Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
370 bool can_record = _session.actively_recording ();
372 /* no outputs? nothing to do ... what happens if we have sends etc. ? */
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
379 /* not active ... do the minimum possible by just outputting silence */
383 if (_meter_point == MeterInput && ((_monitoring_control->monitoring_choice() & MonitorInput) || _diskstream->record_enabled())) {
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.
394 XXX note the absurdity of ::no_roll() being called when we ARE rolling!
396 passthru_silence (start_frame, end_frame, nframes, 0);
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.
404 _diskstream->check_record_status (start_frame, can_record);
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.
413 case MonitoringSilence:
419 case MonitoringInput:
427 //if we have an internal generator, let it play regardless of monitoring state
428 if (_have_internal_generator) {
432 _amp->apply_gain_automation (false);
434 /* if have_internal_generator, or .. */
438 if (_meter_point == MeterInput) {
439 /* still need input monitoring and metering */
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;
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
451 * see http://www.oofus.co.uk/ardour/Ardour3MonitorModesV3.pdf
453 if (!auto_input && !track_rec) {
456 else if (tape_machine_mode && !track_rec && auto_input) {
459 else if (!software_monitor && tape_machine_mode && !track_rec) {
462 else if (!software_monitor && !tape_machine_mode && !track_rec && !auto_input) {
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);
471 _input->process_input (_meter, start_frame, end_frame, speed(), nframes);
475 passthru_silence (start_frame, end_frame, nframes, 0);
479 BufferSet& bufs = _session.get_route_buffers (n_process_buffers());
481 fill_buffers_with_input (bufs, _input, nframes);
483 if (_meter_point == MeterInput) {
484 _meter->run (bufs, start_frame, end_frame, 1.0 /*speed()*/, nframes, true);
487 passthru (bufs, start_frame, end_frame, nframes, false);
490 flush_processor_buffers_locked (nframes);
496 Track::silent_roll (pframes_t nframes, framepos_t /*start_frame*/, framepos_t /*end_frame*/, bool& need_butler)
498 Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
500 framecnt_t playback_distance = _diskstream->calculate_playback_distance(nframes);
501 if (can_internal_playback_seek(playback_distance)) {
502 internal_playback_seek(playback_distance);
507 if (n_outputs().n_total() == 0 && _processors.empty()) {
517 _amp->apply_gain_automation(false);
520 flush_processor_buffers_locked (nframes);
522 framecnt_t playback_distance;
524 BufferSet& bufs (_session.get_route_buffers (n_process_buffers(), true));
526 int const dret = _diskstream->process (bufs, _session.transport_frame(), nframes, playback_distance, false);
527 need_butler = _diskstream->commit (playback_distance);
532 Track::set_diskstream (boost::shared_ptr<Diskstream> ds)
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));
543 Track::diskstream_playlist_changed ()
545 PlaylistChanged (); /* EMIT SIGNAL */
549 Track::diskstream_speed_changed ()
551 SpeedChanged (); /* EMIT SIGNAL */
555 Track::diskstream_alignment_style_changed ()
557 AlignmentStyleChanged (); /* EMIT SIGNAL */
560 boost::shared_ptr<Playlist>
563 return _diskstream->playlist ();
567 Track::request_input_monitoring (bool m)
569 _diskstream->request_input_monitoring (m);
573 Track::ensure_input_monitoring (bool m)
575 _diskstream->ensure_input_monitoring (m);
579 Track::destructive () const
581 return _diskstream->destructive ();
584 list<boost::shared_ptr<Source> > &
585 Track::last_capture_sources ()
587 return _diskstream->last_capture_sources ();
591 Track::set_capture_offset ()
593 _diskstream->set_capture_offset ();
597 Track::steal_write_source_name()
599 return _diskstream->steal_write_source_name ();
603 Track::reset_write_sources (bool r, bool force)
605 _diskstream->reset_write_sources (r, force);
609 Track::playback_buffer_load () const
611 return _diskstream->playback_buffer_load ();
615 Track::capture_buffer_load () const
617 return _diskstream->capture_buffer_load ();
623 return _diskstream->do_refill ();
627 Track::do_flush (RunContext c, bool force)
629 return _diskstream->do_flush (c, force);
633 Track::set_pending_overwrite (bool o)
635 _diskstream->set_pending_overwrite (o);
639 Track::seek (framepos_t p, bool complete_refill)
641 return _diskstream->seek (p, complete_refill);
645 Track::hidden () const
647 return _diskstream->hidden ();
651 Track::can_internal_playback_seek (framecnt_t p)
653 return _diskstream->can_internal_playback_seek (p);
657 Track::internal_playback_seek (framecnt_t p)
659 return _diskstream->internal_playback_seek (p);
663 Track::non_realtime_input_change ()
665 _diskstream->non_realtime_input_change ();
669 Track::non_realtime_locate (framepos_t p)
671 Route::non_realtime_locate (p);
674 /* don't waste i/o cycles and butler calls
675 for hidden (secret) tracks
677 _diskstream->non_realtime_locate (p);
682 Track::non_realtime_set_speed ()
684 _diskstream->non_realtime_set_speed ();
688 Track::overwrite_existing_buffers ()
690 return _diskstream->overwrite_existing_buffers ();
694 Track::get_captured_frames (uint32_t n) const
696 return _diskstream->get_captured_frames (n);
700 Track::set_loop (Location* l)
702 return _diskstream->set_loop (l);
706 Track::transport_looped (framepos_t p)
708 _diskstream->transport_looped (p);
712 Track::realtime_set_speed (double s, bool g)
714 return _diskstream->realtime_set_speed (s, g);
718 Track::transport_stopped_wallclock (struct tm & n, time_t t, bool g)
720 _diskstream->transport_stopped_wallclock (n, t, g);
724 Track::pending_overwrite () const
726 return _diskstream->pending_overwrite ();
730 Track::speed () const
732 return _diskstream->speed ();
736 Track::prepare_to_stop (framepos_t t, framepos_t a)
738 _diskstream->prepare_to_stop (t, a);
742 Track::set_slaved (bool s)
744 _diskstream->set_slaved (s);
750 return _diskstream->n_channels ();
754 Track::get_capture_start_frame (uint32_t n) const
756 return _diskstream->get_capture_start_frame (n);
760 Track::alignment_style () const
762 return _diskstream->alignment_style ();
766 Track::alignment_choice () const
768 return _diskstream->alignment_choice ();
772 Track::current_capture_start () const
774 return _diskstream->current_capture_start ();
778 Track::current_capture_end () const
780 return _diskstream->current_capture_end ();
784 Track::playlist_modified ()
786 _diskstream->playlist_modified ();
790 Track::use_playlist (boost::shared_ptr<Playlist> p)
792 int ret = _diskstream->use_playlist (p);
794 p->set_orig_track_id (id());
800 Track::use_copy_playlist ()
802 int ret = _diskstream->use_copy_playlist ();
805 _diskstream->playlist()->set_orig_track_id (id());
812 Track::use_new_playlist ()
814 int ret = _diskstream->use_new_playlist ();
817 _diskstream->playlist()->set_orig_track_id (id());
824 Track::set_align_style (AlignStyle s, bool force)
826 _diskstream->set_align_style (s, force);
830 Track::set_align_choice (AlignChoice s, bool force)
832 _diskstream->set_align_choice (s, force);
836 Track::using_diskstream_id (PBD::ID id) const
838 return (id == _diskstream->id ());
842 Track::set_block_size (pframes_t n)
844 Route::set_block_size (n);
845 _diskstream->set_block_size (n);
849 Track::adjust_playback_buffering ()
852 _diskstream->adjust_playback_buffering ();
857 Track::adjust_capture_buffering ()
860 _diskstream->adjust_capture_buffering ();
864 #ifdef USE_TRACKS_CODE_FEATURES
866 /* This is the Tracks version of Track::monitoring_state().
868 * Ardour developers: try to flag or fix issues if parts of the libardour API
869 * change in ways that invalidate this
873 Track::monitoring_state () const
875 /* Explicit requests */
877 if (_monitoring & MonitorInput) {
878 return MonitoringInput;
881 if (_monitoring & MonitorDisk) {
882 return MonitoringDisk;
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.
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 ();
894 bool const roll = _session.transport_rolling ();
895 bool const track_rec = _diskstream->record_enabled ();
896 bool session_rec = _session.actively_recording ();
900 if (!session_rec && roll) {
901 return MonitoringDisk;
903 return MonitoringInput;
909 return MonitoringDisk;
913 return MonitoringSilence;
918 /* This is the Ardour/Mixbus version of Track::monitoring_state().
920 * Tracks developers: do NOT modify this method under any circumstances.
924 Track::monitoring_state () const
926 /* Explicit requests */
927 MonitorChoice m (_monitoring_control->monitoring_choice());
929 if (m & MonitorInput) {
930 return MonitoringInput;
933 if (m & MonitorDisk) {
934 return MonitoringDisk;
937 switch (_session.config.get_session_monitoring ()) {
939 return MonitoringDisk;
942 return MonitoringInput;
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.
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 ();
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.
964 * rg: I suspect this is not the case: monitoring may differ
967 if (_session.config.get_punch_in() || _session.config.get_punch_out() || _session.preroll_record_punch_enabled ()) {
968 session_rec = _session.actively_recording ();
970 session_rec = _session.get_record_enabled();
975 if (!session_rec && roll && auto_input) {
976 return MonitoringDisk;
978 return software_monitor ? MonitoringInput : MonitoringSilence;
983 if (tape_machine_mode) {
985 return MonitoringDisk;
989 if (!roll && auto_input) {
990 return software_monitor ? MonitoringInput : MonitoringSilence;
992 return MonitoringDisk;
998 abort(); /* NOTREACHED */
999 return MonitoringSilence;
1005 Track::maybe_declick (BufferSet& bufs, framecnt_t nframes, int declick)
1007 /* never declick if there is an internal generator - we just want it to
1008 keep generating sound without interruption.
1010 ditto if we are monitoring inputs.
1013 if (_have_internal_generator || (_monitoring_control->monitoring_choice() == MonitorInput)) {
1018 declick = _pending_declick;
1022 Amp::declick (bufs, nframes, declick);
1027 Track::check_initial_delay (framecnt_t nframes, framepos_t& transport_frame)
1029 if (_roll_delay > nframes) {
1031 _roll_delay -= nframes;
1032 silence_unlocked (nframes);
1033 /* transport frame is not legal for caller to use */
1036 } else if (_roll_delay > 0) {
1038 nframes -= _roll_delay;
1039 silence_unlocked (_roll_delay);
1040 transport_frame += _roll_delay;
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.
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);
1050 iop->increment_port_buffer_offset (_roll_delay);
1053 _output->increment_port_buffer_offset (_roll_delay);
1063 Track::monitoring_changed (bool, Controllable::GroupControlDisposition)
1065 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1066 (*i)->monitoring_changed ();
1071 Track::metering_state () const
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());
1078 // track no_roll() always metering if
1079 rv = _meter_point == MeterInput;
1081 return rv ? MeteringInput : MeteringRoute;