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/io_processor.h"
25 #include "ardour/meter.h"
26 #include "ardour/monitor_control.h"
27 #include "ardour/playlist.h"
28 #include "ardour/port.h"
29 #include "ardour/processor.h"
30 #include "ardour/record_enable_control.h"
31 #include "ardour/record_safe_control.h"
32 #include "ardour/route_group_specialized.h"
33 #include "ardour/session.h"
34 #include "ardour/session_playlists.h"
35 #include "ardour/track.h"
36 #include "ardour/utils.h"
41 using namespace ARDOUR;
44 Track::Track (Session& sess, string name, PresentationInfo::Flag flag, TrackMode mode, DataType default_type)
45 : Route (sess, name, flag, default_type)
46 , _saved_meter_point (_meter_point)
49 _freeze_record.state = NoFreeze;
55 DEBUG_TRACE (DEBUG::Destruction, string_compose ("track %1 destructor\n", _name));
65 boost::shared_ptr<Route> rp (shared_from_this());
66 boost::shared_ptr<Track> rt = boost::dynamic_pointer_cast<Track> (rp);
68 _record_enable_control.reset (new RecordEnableControl (_session, X_("recenable"), *this));
69 add_control (_record_enable_control);
71 _record_safe_control.reset (new RecordSafeControl (_session, X_("recsafe"), *this));
72 add_control (_record_safe_control);
74 _monitoring_control.reset (new MonitorControl (_session, X_("monitoring"), *this));
75 add_control (_monitoring_control);
77 _session.config.ParameterChanged.connect_same_thread (*this, boost::bind (&Track::parameter_changed, this, _1));
79 _monitoring_control->Changed.connect_same_thread (*this, boost::bind (&Track::monitoring_changed, this, _1, _2));
80 _record_safe_control->Changed.connect_same_thread (*this, boost::bind (&Track::record_safe_changed, this, _1, _2));
81 _record_enable_control->Changed.connect_same_thread (*this, boost::bind (&Track::record_enable_changed, this, _1, _2));
87 Track::use_new_diskstream ()
89 boost::shared_ptr<Diskstream> ds = create_diskstream ();
91 ds->do_refill_with_alloc ();
92 ds->set_block_size (_session.get_block_size ());
93 ds->playlist()->set_orig_track_id (id());
105 Track::state (bool full)
107 XMLNode& root (Route::state (full));
108 root.add_property (X_("saved-meter-point"), enum_2_string (_saved_meter_point));
109 root.add_child_nocopy (_diskstream->get_state ());
115 Track::set_state (const XMLNode& node, int version)
117 if (Route::set_state (node, version)) {
123 if (version >= 3000) {
124 if ((child = find_named_node (node, X_("Diskstream"))) != 0) {
125 boost::shared_ptr<Diskstream> ds = diskstream_factory (*child);
126 ds->do_refill_with_alloc ();
132 _diskstream->playlist()->set_orig_track_id (id());
135 /* set rec-enable control *AFTER* setting up diskstream, because it may
136 want to operate on the diskstream as it sets its own state
139 XMLNodeList nlist = node.children();
140 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
143 XMLProperty const * prop;
144 if (child->name() == Controllable::xml_node_name && (prop = child->property ("name")) != 0) {
145 if (prop->value() == X_("recenable")) {
146 _record_enable_control->set_state (*child, version);
151 XMLProperty const * prop;
153 if ((prop = node.property (X_("saved-meter-point"))) != 0) {
154 _saved_meter_point = MeterPoint (string_2_enum (prop->value(), _saved_meter_point));
156 _saved_meter_point = _meter_point;
163 Track::get_template ()
165 return state (false);
168 Track::FreezeRecord::~FreezeRecord ()
170 for (vector<FreezeRecordProcessorInfo*>::iterator i = processor_info.begin(); i != processor_info.end(); ++i) {
176 Track::freeze_state() const
178 return _freeze_record.state;
184 bool will_record = true;
185 for (PortSet::iterator i = _input->ports().begin(); i != _input->ports().end() && will_record; ++i) {
194 Track::prep_record_enabled (bool yn)
196 if (yn && _record_safe_control->get_value()) {
200 if (!can_be_record_enabled()) {
204 /* keep track of the meter point as it was before we rec-enabled */
205 if (!_diskstream->record_enabled()) {
206 _saved_meter_point = _meter_point;
212 will_follow = _diskstream->prep_record_enable ();
214 will_follow = _diskstream->prep_record_disable ();
219 if (_meter_point != MeterCustom) {
220 set_meter_point (MeterInput);
223 set_meter_point (_saved_meter_point);
231 Track::record_enable_changed (bool, Controllable::GroupControlDisposition)
233 _diskstream->set_record_enabled (_record_enable_control->get_value());
237 Track::record_safe_changed (bool, Controllable::GroupControlDisposition)
239 _diskstream->set_record_safe (_record_safe_control->get_value());
243 Track::can_be_record_safe ()
245 return !_record_enable_control->get_value() && _diskstream && _session.writable() && (_freeze_record.state != Frozen);
249 Track::can_be_record_enabled ()
251 return !_record_safe_control->get_value() && _diskstream && !_diskstream->record_safe() && _session.writable() && (_freeze_record.state != Frozen);
255 Track::parameter_changed (string const & p)
257 if (p == "track-name-number") {
258 resync_track_name ();
260 else if (p == "track-name-take") {
261 resync_track_name ();
263 else if (p == "take-name") {
264 if (_session.config.get_track_name_take()) {
265 resync_track_name ();
271 Track::resync_track_name ()
277 Track::set_name (const string& str)
281 if (_record_enable_control->get_value() && _session.actively_recording()) {
282 /* this messes things up if done while recording */
286 string diskstream_name = "";
287 if (_session.config.get_track_name_take () && !_session.config.get_take_name ().empty()) {
288 // Note: any text is fine, legalize_for_path() fixes this later
289 diskstream_name += _session.config.get_take_name ();
290 diskstream_name += "_";
292 const int64_t tracknumber = track_number();
293 if (tracknumber > 0 && _session.config.get_track_name_number()) {
294 char num[64], fmt[10];
295 snprintf(fmt, sizeof(fmt), "%%0%d" PRId64, _session.track_number_decimals());
296 snprintf(num, sizeof(num), fmt, tracknumber);
297 diskstream_name += num;
298 diskstream_name += "_";
300 diskstream_name += str;
302 if (diskstream_name == _diskstream_name) {
305 _diskstream_name = diskstream_name;
307 _diskstream->set_write_source_name (diskstream_name);
309 boost::shared_ptr<Track> me = boost::dynamic_pointer_cast<Track> (shared_from_this ());
310 if (_diskstream->playlist()->all_regions_empty () && _session.playlists->playlists_for_track (me).size() == 1) {
311 /* Only rename the diskstream (and therefore the playlist) if
312 a) the playlist has never had a region added to it and
313 b) there is only one playlist for this track.
315 If (a) is not followed, people can get confused if, say,
316 they have notes about a playlist with a given name and then
317 it changes (see mantis #4759).
319 If (b) is not followed, we rename the current playlist and not
320 the other ones, which is a bit confusing (see mantis #4977).
322 _diskstream->set_name (str);
325 /* save state so that the statefile fully reflects any filename changes */
327 if ((ret = Route::set_name (str)) == 0) {
328 _session.save_state ("");
335 Track::set_latency_compensation (framecnt_t longest_session_latency)
337 Route::set_latency_compensation (longest_session_latency);
338 _diskstream->set_roll_delay (_roll_delay);
342 Track::no_roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, bool session_state_changing)
344 Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
350 bool can_record = _session.actively_recording ();
352 /* no outputs? nothing to do ... what happens if we have sends etc. ? */
354 if (n_outputs().n_total() == 0) {
358 /* not active ... do the minimum possible by just outputting silence */
362 if (_meter_point == MeterInput && ((_monitoring_control->monitoring_choice() & MonitorInput) || _diskstream->record_enabled())) {
368 if (session_state_changing) {
369 if (_session.transport_speed() != 0.0f) {
370 /* we're rolling but some state is changing (e.g. our diskstream contents)
371 so we cannot use them. Be silent till this is over. Don't declick.
373 XXX note the absurdity of ::no_roll() being called when we ARE rolling!
375 passthru_silence (start_frame, end_frame, nframes, 0);
378 /* we're really not rolling, so we're either delivery silence or actually
379 monitoring, both of which are safe to do while session_state_changing is true.
383 _diskstream->check_record_status (start_frame, can_record);
387 MonitorState const s = monitoring_state ();
388 /* we are not rolling, so be silent even if we are monitoring disk, as there
389 will be no disk data coming in.
392 case MonitoringSilence:
398 case MonitoringInput:
406 //if we have an internal generator, let it play regardless of monitoring state
407 if (_have_internal_generator) {
411 _amp->apply_gain_automation (false);
413 /* if have_internal_generator, or .. */
417 if (_meter_point == MeterInput) {
418 /* still need input monitoring and metering */
420 bool const track_rec = _diskstream->record_enabled ();
421 bool const auto_input = _session.config.get_auto_input ();
422 bool const software_monitor = Config->get_monitoring_model() == SoftwareMonitoring;
423 bool const tape_machine_mode = Config->get_tape_machine_mode ();
424 bool no_meter = false;
426 /* this needs a proper K-map
427 * and should be separated into a function similar to monitoring_state()
428 * that also handles roll() states in audio_track.cc, midi_track.cc and route.cc
430 * see http://www.oofus.co.uk/ardour/Ardour3MonitorModesV3.pdf
432 if (!auto_input && !track_rec) {
435 else if (tape_machine_mode && !track_rec && auto_input) {
438 else if (!software_monitor && tape_machine_mode && !track_rec) {
441 else if (!software_monitor && !tape_machine_mode && !track_rec && !auto_input) {
446 BufferSet& bufs (_session.get_silent_buffers (n_process_buffers()));
447 _meter->run (bufs, start_frame, end_frame, 1.0, nframes, true);
448 _input->process_input (boost::shared_ptr<Processor>(), start_frame, end_frame, speed(), nframes);
450 _input->process_input (_meter, start_frame, end_frame, speed(), nframes);
454 passthru_silence (start_frame, end_frame, nframes, 0);
458 BufferSet& bufs = _session.get_route_buffers (n_process_buffers());
460 fill_buffers_with_input (bufs, _input, nframes);
462 if (_meter_point == MeterInput) {
463 _meter->run (bufs, start_frame, end_frame, 1.0 /*speed()*/, nframes, true);
466 passthru (bufs, start_frame, end_frame, nframes, false);
469 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
470 boost::shared_ptr<Delivery> d = boost::dynamic_pointer_cast<Delivery> (*i);
472 d->flush_buffers (nframes);
480 Track::silent_roll (pframes_t nframes, framepos_t /*start_frame*/, framepos_t /*end_frame*/, bool& need_butler)
482 Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
484 framecnt_t playback_distance = _diskstream->calculate_playback_distance(nframes);
485 if (can_internal_playback_seek(playback_distance)) {
486 internal_playback_seek(playback_distance);
491 if (n_outputs().n_total() == 0 && _processors.empty()) {
501 _amp->apply_gain_automation(false);
505 framecnt_t playback_distance;
507 BufferSet& bufs (_session.get_route_buffers (n_process_buffers(), true));
509 int const dret = _diskstream->process (bufs, _session.transport_frame(), nframes, playback_distance, false);
510 need_butler = _diskstream->commit (playback_distance);
515 Track::set_diskstream (boost::shared_ptr<Diskstream> ds)
519 ds->PlaylistChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_playlist_changed, this));
520 diskstream_playlist_changed ();
521 ds->SpeedChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_speed_changed, this));
522 ds->AlignmentStyleChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_alignment_style_changed, this));
526 Track::diskstream_playlist_changed ()
528 PlaylistChanged (); /* EMIT SIGNAL */
532 Track::diskstream_speed_changed ()
534 SpeedChanged (); /* EMIT SIGNAL */
538 Track::diskstream_alignment_style_changed ()
540 AlignmentStyleChanged (); /* EMIT SIGNAL */
543 boost::shared_ptr<Playlist>
546 return _diskstream->playlist ();
550 Track::request_input_monitoring (bool m)
552 _diskstream->request_input_monitoring (m);
556 Track::ensure_input_monitoring (bool m)
558 _diskstream->ensure_input_monitoring (m);
562 Track::destructive () const
564 return _diskstream->destructive ();
567 list<boost::shared_ptr<Source> > &
568 Track::last_capture_sources ()
570 return _diskstream->last_capture_sources ();
574 Track::set_capture_offset ()
576 _diskstream->set_capture_offset ();
580 Track::steal_write_source_name()
582 return _diskstream->steal_write_source_name ();
586 Track::reset_write_sources (bool r, bool force)
588 _diskstream->reset_write_sources (r, force);
592 Track::playback_buffer_load () const
594 return _diskstream->playback_buffer_load ();
598 Track::capture_buffer_load () const
600 return _diskstream->capture_buffer_load ();
606 return _diskstream->do_refill ();
610 Track::do_flush (RunContext c, bool force)
612 return _diskstream->do_flush (c, force);
616 Track::set_pending_overwrite (bool o)
618 _diskstream->set_pending_overwrite (o);
622 Track::seek (framepos_t p, bool complete_refill)
624 return _diskstream->seek (p, complete_refill);
628 Track::hidden () const
630 return _diskstream->hidden ();
634 Track::can_internal_playback_seek (framecnt_t p)
636 return _diskstream->can_internal_playback_seek (p);
640 Track::internal_playback_seek (framecnt_t p)
642 return _diskstream->internal_playback_seek (p);
646 Track::non_realtime_input_change ()
648 _diskstream->non_realtime_input_change ();
652 Track::non_realtime_locate (framepos_t p)
654 Route::non_realtime_locate (p);
657 /* don't waste i/o cycles and butler calls
658 for hidden (secret) tracks
660 _diskstream->non_realtime_locate (p);
665 Track::non_realtime_set_speed ()
667 _diskstream->non_realtime_set_speed ();
671 Track::overwrite_existing_buffers ()
673 return _diskstream->overwrite_existing_buffers ();
677 Track::get_captured_frames (uint32_t n) const
679 return _diskstream->get_captured_frames (n);
683 Track::set_loop (Location* l)
685 return _diskstream->set_loop (l);
689 Track::transport_looped (framepos_t p)
691 _diskstream->transport_looped (p);
695 Track::realtime_set_speed (double s, bool g)
697 return _diskstream->realtime_set_speed (s, g);
701 Track::transport_stopped_wallclock (struct tm & n, time_t t, bool g)
703 _diskstream->transport_stopped_wallclock (n, t, g);
707 Track::pending_overwrite () const
709 return _diskstream->pending_overwrite ();
713 Track::speed () const
715 return _diskstream->speed ();
719 Track::prepare_to_stop (framepos_t t, framepos_t a)
721 _diskstream->prepare_to_stop (t, a);
725 Track::set_slaved (bool s)
727 _diskstream->set_slaved (s);
733 return _diskstream->n_channels ();
737 Track::get_capture_start_frame (uint32_t n) const
739 return _diskstream->get_capture_start_frame (n);
743 Track::alignment_style () const
745 return _diskstream->alignment_style ();
749 Track::alignment_choice () const
751 return _diskstream->alignment_choice ();
755 Track::current_capture_start () const
757 return _diskstream->current_capture_start ();
761 Track::current_capture_end () const
763 return _diskstream->current_capture_end ();
767 Track::playlist_modified ()
769 _diskstream->playlist_modified ();
773 Track::use_playlist (boost::shared_ptr<Playlist> p)
775 int ret = _diskstream->use_playlist (p);
777 p->set_orig_track_id (id());
783 Track::use_copy_playlist ()
785 int ret = _diskstream->use_copy_playlist ();
788 _diskstream->playlist()->set_orig_track_id (id());
795 Track::use_new_playlist ()
797 int ret = _diskstream->use_new_playlist ();
800 _diskstream->playlist()->set_orig_track_id (id());
807 Track::set_align_style (AlignStyle s, bool force)
809 _diskstream->set_align_style (s, force);
813 Track::set_align_choice (AlignChoice s, bool force)
815 _diskstream->set_align_choice (s, force);
819 Track::using_diskstream_id (PBD::ID id) const
821 return (id == _diskstream->id ());
825 Track::set_block_size (pframes_t n)
827 Route::set_block_size (n);
828 _diskstream->set_block_size (n);
832 Track::adjust_playback_buffering ()
835 _diskstream->adjust_playback_buffering ();
840 Track::adjust_capture_buffering ()
843 _diskstream->adjust_capture_buffering ();
847 #ifdef USE_TRACKS_CODE_FEATURES
849 /* This is the Tracks version of Track::monitoring_state().
851 * Ardour developers: try to flag or fix issues if parts of the libardour API
852 * change in ways that invalidate this
856 Track::monitoring_state () const
858 /* Explicit requests */
860 if (_monitoring & MonitorInput) {
861 return MonitoringInput;
864 if (_monitoring & MonitorDisk) {
865 return MonitoringDisk;
868 /* This is an implementation of the truth table in doc/monitor_modes.pdf;
869 I don't think it's ever going to be too pretty too look at.
872 // GZ: NOT USED IN TRACKS
873 //bool const auto_input = _session.config.get_auto_input ();
874 //bool const software_monitor = Config->get_monitoring_model() == SoftwareMonitoring;
875 //bool const tape_machine_mode = Config->get_tape_machine_mode ();
877 bool const roll = _session.transport_rolling ();
878 bool const track_rec = _diskstream->record_enabled ();
879 bool session_rec = _session.actively_recording ();
883 if (!session_rec && roll) {
884 return MonitoringDisk;
886 return MonitoringInput;
892 return MonitoringDisk;
896 return MonitoringSilence;
901 /* This is the Ardour/Mixbus version of Track::monitoring_state().
903 * Tracks developers: do NOT modify this method under any circumstances.
907 Track::monitoring_state () const
909 /* Explicit requests */
910 MonitorChoice m (_monitoring_control->monitoring_choice());
912 if (m & MonitorInput) {
913 return MonitoringInput;
916 if (m & MonitorDisk) {
917 return MonitoringDisk;
920 /* This is an implementation of the truth table in doc/monitor_modes.pdf;
921 I don't think it's ever going to be too pretty too look at.
924 bool const roll = _session.transport_rolling ();
925 bool const track_rec = _diskstream->record_enabled ();
926 bool const auto_input = _session.config.get_auto_input ();
927 bool const software_monitor = Config->get_monitoring_model() == SoftwareMonitoring;
928 bool const tape_machine_mode = Config->get_tape_machine_mode ();
931 /* I suspect that just use actively_recording() is good enough all the
932 * time, but just to keep the semantics the same as they were before
933 * sept 26th 2012, we differentiate between the cases where punch is
934 * enabled and those where it is not.
937 if (_session.config.get_punch_in() || _session.config.get_punch_out()) {
938 session_rec = _session.actively_recording ();
940 session_rec = _session.get_record_enabled();
945 if (!session_rec && roll && auto_input) {
946 return MonitoringDisk;
948 return software_monitor ? MonitoringInput : MonitoringSilence;
953 if (tape_machine_mode) {
955 return MonitoringDisk;
959 if (!roll && auto_input) {
960 return software_monitor ? MonitoringInput : MonitoringSilence;
962 return MonitoringDisk;
968 abort(); /* NOTREACHED */
969 return MonitoringSilence;
975 Track::maybe_declick (BufferSet& bufs, framecnt_t nframes, int declick)
977 /* never declick if there is an internal generator - we just want it to
978 keep generating sound without interruption.
980 ditto if we are monitoring inputs.
983 if (_have_internal_generator || (_monitoring_control->monitoring_choice() == MonitorInput)) {
988 declick = _pending_declick;
992 Amp::declick (bufs, nframes, declick);
997 Track::check_initial_delay (framecnt_t nframes, framepos_t& transport_frame)
999 if (_roll_delay > nframes) {
1001 _roll_delay -= nframes;
1002 silence_unlocked (nframes);
1003 /* transport frame is not legal for caller to use */
1006 } else if (_roll_delay > 0) {
1008 nframes -= _roll_delay;
1009 silence_unlocked (_roll_delay);
1010 transport_frame += _roll_delay;
1012 /* shuffle all the port buffers for things that lead "out" of this Route
1013 to reflect that we just wrote _roll_delay frames of silence.
1016 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
1017 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1018 boost::shared_ptr<IOProcessor> iop = boost::dynamic_pointer_cast<IOProcessor> (*i);
1020 iop->increment_port_buffer_offset (_roll_delay);
1023 _output->increment_port_buffer_offset (_roll_delay);
1033 Track::monitoring_changed (bool, Controllable::GroupControlDisposition)
1035 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1036 (*i)->monitoring_changed ();
1041 Track::metering_state () const
1044 if (_session.transport_rolling ()) {
1045 // audio_track.cc || midi_track.cc roll() runs meter IFF:
1046 rv = _meter_point == MeterInput && ((_monitoring_control->monitoring_choice() & MonitorInput) || _diskstream->record_enabled());
1048 // track no_roll() always metering if
1049 rv = _meter_point == MeterInput;
1051 return rv ? MeteringInput : MeteringRoute;