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/types_convert.h"
39 #include "ardour/utils.h"
44 using namespace ARDOUR;
47 Track::Track (Session& sess, string name, PresentationInfo::Flag flag, TrackMode mode, DataType default_type)
48 : Route (sess, name, flag, default_type)
49 , _saved_meter_point (_meter_point)
52 _freeze_record.state = NoFreeze;
58 DEBUG_TRACE (DEBUG::Destruction, string_compose ("track %1 destructor\n", _name));
68 boost::shared_ptr<Route> rp (shared_from_this());
69 boost::shared_ptr<Track> rt = boost::dynamic_pointer_cast<Track> (rp);
71 _record_enable_control.reset (new RecordEnableControl (_session, EventTypeMap::instance().to_symbol (RecEnableAutomation), *this));
72 add_control (_record_enable_control);
74 _record_safe_control.reset (new RecordSafeControl (_session, EventTypeMap::instance().to_symbol (RecSafeAutomation), *this));
75 add_control (_record_safe_control);
77 _monitoring_control.reset (new MonitorControl (_session, EventTypeMap::instance().to_symbol (MonitoringAutomation), *this));
78 add_control (_monitoring_control);
80 _session.config.ParameterChanged.connect_same_thread (*this, boost::bind (&Track::parameter_changed, this, _1));
82 _monitoring_control->Changed.connect_same_thread (*this, boost::bind (&Track::monitoring_changed, this, _1, _2));
83 _record_safe_control->Changed.connect_same_thread (*this, boost::bind (&Track::record_safe_changed, this, _1, _2));
84 _record_enable_control->Changed.connect_same_thread (*this, boost::bind (&Track::record_enable_changed, this, _1, _2));
90 Track::use_new_diskstream ()
92 boost::shared_ptr<Diskstream> ds = create_diskstream ();
94 ds->do_refill_with_alloc ();
95 ds->set_block_size (_session.get_block_size ());
96 ds->playlist()->set_orig_track_id (id());
108 Track::state (bool full)
110 XMLNode& root (Route::state (full));
112 root.add_child_nocopy (_monitoring_control->get_state ());
113 root.add_child_nocopy (_record_safe_control->get_state ());
114 root.add_child_nocopy (_record_enable_control->get_state ());
116 root.set_property (X_("saved-meter-point"), _saved_meter_point);
117 root.add_child_nocopy (_diskstream->get_state ());
123 Track::set_state (const XMLNode& node, int version)
125 if (Route::set_state (node, version)) {
131 if (version >= 3000) {
132 if ((child = find_named_node (node, X_("Diskstream"))) != 0) {
133 boost::shared_ptr<Diskstream> ds = diskstream_factory (*child);
134 ds->do_refill_with_alloc ();
140 _diskstream->playlist()->set_orig_track_id (id());
143 /* set rec-enable control *AFTER* setting up diskstream, because it may
144 want to operate on the diskstream as it sets its own state
147 XMLNodeList nlist = node.children();
148 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
151 if (child->name() == Controllable::xml_node_name) {
153 if (!child->get_property ("name", name)) {
157 if (name == _record_enable_control->name()) {
158 _record_enable_control->set_state (*child, version);
159 } else if (name == _record_safe_control->name()) {
160 _record_safe_control->set_state (*child, version);
161 } else if (name == _monitoring_control->name()) {
162 _monitoring_control->set_state (*child, version);
167 /* convert old 3001 state */
168 MonitorChoice monitoring;
169 if (node.get_property (X_("monitoring"), monitoring)) {
170 XMLNode mon_node ("backwardscompat");
171 mon_node.set_property (X_("monitoring"), monitoring);
172 mon_node.set_property (X_("value"), (int) monitoring);
173 _monitoring_control->set_state (mon_node, version);
176 if (!node.get_property (X_("saved-meter-point"), _saved_meter_point)) {
177 _saved_meter_point = _meter_point;
184 Track::get_template ()
186 return state (false);
189 Track::FreezeRecord::~FreezeRecord ()
191 for (vector<FreezeRecordProcessorInfo*>::iterator i = processor_info.begin(); i != processor_info.end(); ++i) {
197 Track::freeze_state() const
199 return _freeze_record.state;
205 bool will_record = true;
206 for (PortSet::iterator i = _input->ports().begin(); i != _input->ports().end() && will_record; ++i) {
215 Track::prep_record_enabled (bool yn)
217 if (yn && _record_safe_control->get_value()) {
221 if (!can_be_record_enabled()) {
225 /* keep track of the meter point as it was before we rec-enabled */
226 if (!_diskstream->record_enabled()) {
227 _saved_meter_point = _meter_point;
233 will_follow = _diskstream->prep_record_enable ();
235 will_follow = _diskstream->prep_record_disable ();
240 if (_meter_point != MeterCustom) {
241 set_meter_point (MeterInput);
244 set_meter_point (_saved_meter_point);
252 Track::record_enable_changed (bool, Controllable::GroupControlDisposition)
254 _diskstream->set_record_enabled (_record_enable_control->get_value());
258 Track::record_safe_changed (bool, Controllable::GroupControlDisposition)
260 _diskstream->set_record_safe (_record_safe_control->get_value());
264 Track::can_be_record_safe ()
266 return !_record_enable_control->get_value() && _diskstream && _session.writable() && (_freeze_record.state != Frozen);
270 Track::can_be_record_enabled ()
272 return !_record_safe_control->get_value() && _diskstream && !_diskstream->record_safe() && _session.writable() && (_freeze_record.state != Frozen);
276 Track::parameter_changed (string const & p)
278 if (p == "track-name-number") {
279 resync_track_name ();
281 else if (p == "track-name-take") {
282 resync_track_name ();
284 else if (p == "take-name") {
285 if (_session.config.get_track_name_take()) {
286 resync_track_name ();
292 Track::resync_track_name ()
298 Track::set_name (const string& str)
306 if (_record_enable_control->get_value()) {
307 /* when re-arm'ed the file (named after the track) is already ready to rolll */
311 string diskstream_name = "";
312 if (_session.config.get_track_name_take () && !_session.config.get_take_name ().empty()) {
313 // Note: any text is fine, legalize_for_path() fixes this later
314 diskstream_name += _session.config.get_take_name ();
315 diskstream_name += "_";
317 const int64_t tracknumber = track_number();
318 if (tracknumber > 0 && _session.config.get_track_name_number()) {
319 char num[64], fmt[10];
320 snprintf(fmt, sizeof(fmt), "%%0%d" PRId64, _session.track_number_decimals());
321 snprintf(num, sizeof(num), fmt, tracknumber);
322 diskstream_name += num;
323 diskstream_name += "_";
325 diskstream_name += str;
327 if (diskstream_name == _diskstream_name) {
330 _diskstream_name = diskstream_name;
332 _diskstream->set_write_source_name (diskstream_name);
334 boost::shared_ptr<Track> me = boost::dynamic_pointer_cast<Track> (shared_from_this ());
335 if (_diskstream->playlist()->all_regions_empty () && _session.playlists->playlists_for_track (me).size() == 1) {
336 /* Only rename the diskstream (and therefore the playlist) if
337 a) the playlist has never had a region added to it and
338 b) there is only one playlist for this track.
340 If (a) is not followed, people can get confused if, say,
341 they have notes about a playlist with a given name and then
342 it changes (see mantis #4759).
344 If (b) is not followed, we rename the current playlist and not
345 the other ones, which is a bit confusing (see mantis #4977).
347 _diskstream->set_name (str);
350 /* save state so that the statefile fully reflects any filename changes */
352 if ((ret = Route::set_name (str)) == 0) {
353 _session.save_state ("");
360 Track::set_latency_compensation (framecnt_t longest_session_latency)
362 Route::set_latency_compensation (longest_session_latency);
363 _diskstream->set_roll_delay (_roll_delay);
367 Track::no_roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, bool session_state_changing)
369 Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
375 bool can_record = _session.actively_recording ();
377 /* no outputs? nothing to do ... what happens if we have sends etc. ? */
379 if (n_outputs().n_total() == 0 && !ARDOUR::Profile->get_mixbus()) {
380 //Note: Mixbus has its own output mechanism, so we should operate even if no explicit outputs are assigned
384 /* not active ... do the minimum possible by just outputting silence */
388 if (_meter_point == MeterInput && ((_monitoring_control->monitoring_choice() & MonitorInput) || _diskstream->record_enabled())) {
394 if (session_state_changing) {
395 if (_session.transport_speed() != 0.0f) {
396 /* we're rolling but some state is changing (e.g. our diskstream contents)
397 so we cannot use them. Be silent till this is over. Don't declick.
399 XXX note the absurdity of ::no_roll() being called when we ARE rolling!
401 passthru_silence (start_frame, end_frame, nframes, 0);
404 /* we're really not rolling, so we're either delivery silence or actually
405 monitoring, both of which are safe to do while session_state_changing is true.
409 _diskstream->check_record_status (start_frame, can_record);
413 MonitorState const s = monitoring_state ();
414 /* we are not rolling, so be silent even if we are monitoring disk, as there
415 will be no disk data coming in.
418 case MonitoringSilence:
424 case MonitoringInput:
432 //if we have an internal generator, let it play regardless of monitoring state
433 if (_have_internal_generator) {
437 _amp->apply_gain_automation (false);
439 /* if have_internal_generator, or .. */
443 if (_meter_point == MeterInput) {
444 /* still need input monitoring and metering */
446 bool const track_rec = _diskstream->record_enabled ();
447 bool const auto_input = _session.config.get_auto_input ();
448 bool const software_monitor = Config->get_monitoring_model() == SoftwareMonitoring;
449 bool const tape_machine_mode = Config->get_tape_machine_mode ();
450 bool no_meter = false;
452 /* this needs a proper K-map
453 * and should be separated into a function similar to monitoring_state()
454 * that also handles roll() states in audio_track.cc, midi_track.cc and route.cc
456 * see http://www.oofus.co.uk/ardour/Ardour3MonitorModesV3.pdf
458 if (!auto_input && !track_rec) {
461 else if (tape_machine_mode && !track_rec && auto_input) {
464 else if (!software_monitor && tape_machine_mode && !track_rec) {
467 else if (!software_monitor && !tape_machine_mode && !track_rec && !auto_input) {
472 BufferSet& bufs (_session.get_silent_buffers (n_process_buffers()));
473 _meter->run (bufs, start_frame, end_frame, 1.0, nframes, true);
474 _input->process_input (boost::shared_ptr<Processor>(), start_frame, end_frame, speed(), nframes);
476 _input->process_input (_meter, start_frame, end_frame, speed(), nframes);
480 passthru_silence (start_frame, end_frame, nframes, 0);
484 BufferSet& bufs = _session.get_route_buffers (n_process_buffers());
486 fill_buffers_with_input (bufs, _input, nframes);
488 if (_meter_point == MeterInput) {
489 _meter->run (bufs, start_frame, end_frame, 1.0 /*speed()*/, nframes, true);
492 passthru (bufs, start_frame, end_frame, nframes, false);
495 flush_processor_buffers_locked (nframes);
501 Track::silent_roll (pframes_t nframes, framepos_t /*start_frame*/, framepos_t /*end_frame*/, bool& need_butler)
503 Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
505 framecnt_t playback_distance = _diskstream->calculate_playback_distance(nframes);
506 if (can_internal_playback_seek(playback_distance)) {
507 internal_playback_seek(playback_distance);
512 if (n_outputs().n_total() == 0 && _processors.empty()) {
522 _amp->apply_gain_automation(false);
525 flush_processor_buffers_locked (nframes);
527 framecnt_t playback_distance;
529 BufferSet& bufs (_session.get_route_buffers (n_process_buffers(), true));
531 int const dret = _diskstream->process (bufs, _session.transport_frame(), nframes, playback_distance, false);
532 need_butler = _diskstream->commit (playback_distance);
537 Track::set_diskstream (boost::shared_ptr<Diskstream> ds)
541 ds->PlaylistChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_playlist_changed, this));
542 diskstream_playlist_changed ();
543 ds->SpeedChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_speed_changed, this));
544 ds->AlignmentStyleChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_alignment_style_changed, this));
548 Track::diskstream_playlist_changed ()
550 PlaylistChanged (); /* EMIT SIGNAL */
554 Track::diskstream_speed_changed ()
556 SpeedChanged (); /* EMIT SIGNAL */
560 Track::diskstream_alignment_style_changed ()
562 AlignmentStyleChanged (); /* EMIT SIGNAL */
565 boost::shared_ptr<Playlist>
568 return _diskstream->playlist ();
572 Track::request_input_monitoring (bool m)
574 _diskstream->request_input_monitoring (m);
578 Track::ensure_input_monitoring (bool m)
580 _diskstream->ensure_input_monitoring (m);
584 Track::destructive () const
586 return _diskstream->destructive ();
589 list<boost::shared_ptr<Source> > &
590 Track::last_capture_sources ()
592 return _diskstream->last_capture_sources ();
596 Track::set_capture_offset ()
598 _diskstream->set_capture_offset ();
602 Track::steal_write_source_name()
604 return _diskstream->steal_write_source_name ();
608 Track::reset_write_sources (bool r, bool force)
610 _diskstream->reset_write_sources (r, force);
614 Track::playback_buffer_load () const
616 return _diskstream->playback_buffer_load ();
620 Track::capture_buffer_load () const
622 return _diskstream->capture_buffer_load ();
628 return _diskstream->do_refill ();
632 Track::do_flush (RunContext c, bool force)
634 return _diskstream->do_flush (c, force);
638 Track::set_pending_overwrite (bool o)
640 _diskstream->set_pending_overwrite (o);
644 Track::seek (framepos_t p, bool complete_refill)
646 return _diskstream->seek (p, complete_refill);
650 Track::hidden () const
652 return _diskstream->hidden ();
656 Track::can_internal_playback_seek (framecnt_t p)
658 return _diskstream->can_internal_playback_seek (p);
662 Track::internal_playback_seek (framecnt_t p)
664 return _diskstream->internal_playback_seek (p);
668 Track::non_realtime_input_change ()
670 _diskstream->non_realtime_input_change ();
674 Track::non_realtime_locate (framepos_t p)
676 Route::non_realtime_locate (p);
679 /* don't waste i/o cycles and butler calls
680 for hidden (secret) tracks
682 _diskstream->non_realtime_locate (p);
687 Track::non_realtime_set_speed ()
689 _diskstream->non_realtime_set_speed ();
693 Track::overwrite_existing_buffers ()
695 return _diskstream->overwrite_existing_buffers ();
699 Track::get_captured_frames (uint32_t n) const
701 return _diskstream->get_captured_frames (n);
705 Track::set_loop (Location* l)
707 return _diskstream->set_loop (l);
711 Track::transport_looped (framepos_t p)
713 _diskstream->transport_looped (p);
717 Track::realtime_set_speed (double s, bool g)
719 return _diskstream->realtime_set_speed (s, g);
723 Track::transport_stopped_wallclock (struct tm & n, time_t t, bool g)
725 _diskstream->transport_stopped_wallclock (n, t, g);
729 Track::pending_overwrite () const
731 return _diskstream->pending_overwrite ();
735 Track::speed () const
737 return _diskstream->speed ();
741 Track::prepare_to_stop (framepos_t t, framepos_t a)
743 _diskstream->prepare_to_stop (t, a);
747 Track::set_slaved (bool s)
749 _diskstream->set_slaved (s);
755 return _diskstream->n_channels ();
759 Track::get_capture_start_frame (uint32_t n) const
761 return _diskstream->get_capture_start_frame (n);
765 Track::alignment_style () const
767 return _diskstream->alignment_style ();
771 Track::alignment_choice () const
773 return _diskstream->alignment_choice ();
777 Track::current_capture_start () const
779 return _diskstream->current_capture_start ();
783 Track::current_capture_end () const
785 return _diskstream->current_capture_end ();
789 Track::playlist_modified ()
791 _diskstream->playlist_modified ();
795 Track::use_playlist (boost::shared_ptr<Playlist> p)
797 int ret = _diskstream->use_playlist (p);
799 p->set_orig_track_id (id());
805 Track::use_copy_playlist ()
807 int ret = _diskstream->use_copy_playlist ();
810 _diskstream->playlist()->set_orig_track_id (id());
817 Track::use_new_playlist ()
819 int ret = _diskstream->use_new_playlist ();
822 _diskstream->playlist()->set_orig_track_id (id());
829 Track::set_align_style (AlignStyle s, bool force)
831 _diskstream->set_align_style (s, force);
835 Track::set_align_choice (AlignChoice s, bool force)
837 _diskstream->set_align_choice (s, force);
841 Track::using_diskstream_id (PBD::ID id) const
843 return (id == _diskstream->id ());
847 Track::set_block_size (pframes_t n)
849 Route::set_block_size (n);
850 _diskstream->set_block_size (n);
854 Track::adjust_playback_buffering ()
857 _diskstream->adjust_playback_buffering ();
862 Track::adjust_capture_buffering ()
865 _diskstream->adjust_capture_buffering ();
869 #ifdef USE_TRACKS_CODE_FEATURES
871 /* This is the Tracks version of Track::monitoring_state().
873 * Ardour developers: try to flag or fix issues if parts of the libardour API
874 * change in ways that invalidate this
878 Track::monitoring_state () const
880 /* Explicit requests */
882 if (_monitoring & MonitorInput) {
883 return MonitoringInput;
886 if (_monitoring & MonitorDisk) {
887 return MonitoringDisk;
890 /* This is an implementation of the truth table in doc/monitor_modes.pdf;
891 I don't think it's ever going to be too pretty too look at.
894 // GZ: NOT USED IN TRACKS
895 //bool const auto_input = _session.config.get_auto_input ();
896 //bool const software_monitor = Config->get_monitoring_model() == SoftwareMonitoring;
897 //bool const tape_machine_mode = Config->get_tape_machine_mode ();
899 bool const roll = _session.transport_rolling ();
900 bool const track_rec = _diskstream->record_enabled ();
901 bool session_rec = _session.actively_recording ();
905 if (!session_rec && roll) {
906 return MonitoringDisk;
908 return MonitoringInput;
914 return MonitoringDisk;
918 return MonitoringSilence;
923 /* This is the Ardour/Mixbus version of Track::monitoring_state().
925 * Tracks developers: do NOT modify this method under any circumstances.
929 Track::monitoring_state () const
931 /* Explicit requests */
932 MonitorChoice m (_monitoring_control->monitoring_choice());
934 if (m & MonitorInput) {
935 return MonitoringInput;
938 if (m & MonitorDisk) {
939 return MonitoringDisk;
942 switch (_session.config.get_session_monitoring ()) {
944 return MonitoringDisk;
947 return MonitoringInput;
953 /* This is an implementation of the truth table in doc/monitor_modes.pdf;
954 I don't think it's ever going to be too pretty too look at.
957 bool const roll = _session.transport_rolling ();
958 bool const track_rec = _diskstream->record_enabled ();
959 bool const auto_input = _session.config.get_auto_input ();
960 bool const software_monitor = Config->get_monitoring_model() == SoftwareMonitoring;
961 bool const tape_machine_mode = Config->get_tape_machine_mode ();
964 /* I suspect that just use actively_recording() is good enough all the
965 * time, but just to keep the semantics the same as they were before
966 * sept 26th 2012, we differentiate between the cases where punch is
967 * enabled and those where it is not.
969 * rg: I suspect this is not the case: monitoring may differ
972 if (_session.config.get_punch_in() || _session.config.get_punch_out() || _session.preroll_record_punch_enabled ()) {
973 session_rec = _session.actively_recording ();
975 session_rec = _session.get_record_enabled();
980 if (!session_rec && roll && auto_input) {
981 return MonitoringDisk;
983 return software_monitor ? MonitoringInput : MonitoringSilence;
988 if (tape_machine_mode) {
990 return MonitoringDisk;
994 if (!roll && auto_input) {
995 return software_monitor ? MonitoringInput : MonitoringSilence;
997 return MonitoringDisk;
1003 abort(); /* NOTREACHED */
1004 return MonitoringSilence;
1010 Track::maybe_declick (BufferSet& bufs, framecnt_t nframes, int declick)
1012 /* never declick if there is an internal generator - we just want it to
1013 keep generating sound without interruption.
1015 ditto if we are monitoring inputs.
1018 if (_have_internal_generator || (_monitoring_control->monitoring_choice() == MonitorInput)) {
1023 declick = _pending_declick;
1027 Amp::declick (bufs, nframes, declick);
1032 Track::check_initial_delay (framecnt_t nframes, framepos_t& transport_frame)
1034 if (_roll_delay > nframes) {
1036 _roll_delay -= nframes;
1037 silence_unlocked (nframes);
1038 /* transport frame is not legal for caller to use */
1041 } else if (_roll_delay > 0) {
1043 nframes -= _roll_delay;
1044 silence_unlocked (_roll_delay);
1045 transport_frame += _roll_delay;
1047 /* shuffle all the port buffers for things that lead "out" of this Route
1048 to reflect that we just wrote _roll_delay frames of silence.
1051 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
1052 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1053 boost::shared_ptr<IOProcessor> iop = boost::dynamic_pointer_cast<IOProcessor> (*i);
1055 iop->increment_port_buffer_offset (_roll_delay);
1058 _output->increment_port_buffer_offset (_roll_delay);
1068 Track::monitoring_changed (bool, Controllable::GroupControlDisposition)
1070 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1071 (*i)->monitoring_changed ();
1076 Track::metering_state () const
1079 if (_session.transport_rolling ()) {
1080 // audio_track.cc || midi_track.cc roll() runs meter IFF:
1081 rv = _meter_point == MeterInput && ((_monitoring_control->monitoring_choice() & MonitorInput) || _diskstream->record_enabled());
1083 // track no_roll() always metering if
1084 rv = _meter_point == MeterInput;
1086 return rv ? MeteringInput : MeteringRoute;