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/playlist.h"
27 #include "ardour/port.h"
28 #include "ardour/processor.h"
29 #include "ardour/route_group_specialized.h"
30 #include "ardour/session.h"
31 #include "ardour/session_playlists.h"
32 #include "ardour/track.h"
33 #include "ardour/utils.h"
38 using namespace ARDOUR;
41 Track::Track (Session& sess, string name, Route::Flag flag, TrackMode mode, DataType default_type)
42 : Route (sess, name, flag, default_type)
43 , _saved_meter_point (_meter_point)
45 , _monitoring (MonitorAuto)
47 _freeze_record.state = NoFreeze;
53 DEBUG_TRACE (DEBUG::Destruction, string_compose ("track %1 destructor\n", _name));
63 boost::shared_ptr<Route> rp (shared_from_this());
64 boost::shared_ptr<Track> rt = boost::dynamic_pointer_cast<Track> (rp);
65 _rec_enable_control = boost::shared_ptr<RecEnableControl> (new RecEnableControl(rt));
66 _rec_enable_control->set_flags (Controllable::Toggle);
68 /* don't add rec_enable_control to controls because we don't want it to
69 * appear as an automatable parameter
71 track_number_changed.connect_same_thread (*this, boost::bind (&Track::resync_track_name, this));
72 _session.config.ParameterChanged.connect_same_thread (*this, boost::bind (&Track::parameter_changed, this, _1));
78 Track::use_new_diskstream ()
80 boost::shared_ptr<Diskstream> ds = create_diskstream ();
82 ds->do_refill_with_alloc ();
83 ds->set_block_size (_session.get_block_size ());
84 ds->playlist()->set_orig_track_id (id());
96 Track::state (bool full)
98 XMLNode& root (Route::state (full));
99 root.add_property (X_("monitoring"), enum_2_string (_monitoring));
100 root.add_property (X_("saved-meter-point"), enum_2_string (_saved_meter_point));
101 root.add_child_nocopy (_rec_enable_control->get_state());
102 root.add_child_nocopy (_diskstream->get_state ());
108 Track::set_state (const XMLNode& node, int version)
110 if (Route::set_state (node, version)) {
116 if (version >= 3000) {
117 if ((child = find_named_node (node, X_("Diskstream"))) != 0) {
118 boost::shared_ptr<Diskstream> ds = diskstream_factory (*child);
119 ds->do_refill_with_alloc ();
125 _diskstream->playlist()->set_orig_track_id (id());
128 /* set rec-enable control *AFTER* setting up diskstream, because it may
129 want to operate on the diskstream as it sets its own state
132 XMLNodeList nlist = node.children();
133 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
137 if (child->name() == Controllable::xml_node_name && (prop = child->property ("name")) != 0) {
138 if (prop->value() == X_("recenable")) {
139 _rec_enable_control->set_state (*child, version);
144 const XMLProperty* prop;
146 if ((prop = node.property (X_("monitoring"))) != 0) {
147 _monitoring = MonitorChoice (string_2_enum (prop->value(), _monitoring));
149 _monitoring = MonitorAuto;
152 if ((prop = node.property (X_("saved-meter-point"))) != 0) {
153 _saved_meter_point = MeterPoint (string_2_enum (prop->value(), _saved_meter_point));
155 _saved_meter_point = _meter_point;
162 Track::get_template ()
164 return state (false);
167 Track::FreezeRecord::~FreezeRecord ()
169 for (vector<FreezeRecordProcessorInfo*>::iterator i = processor_info.begin(); i != processor_info.end(); ++i) {
175 Track::freeze_state() const
177 return _freeze_record.state;
180 Track::RecEnableControl::RecEnableControl (boost::shared_ptr<Track> t)
181 : AutomationControl (t->session(), RecEnableAutomation, boost::shared_ptr<AutomationList>(), X_("recenable"))
184 boost::shared_ptr<AutomationList> gl(new AutomationList(Evoral::Parameter(RecEnableAutomation)));
189 Track::RecEnableControl::set_value (double val)
191 boost::shared_ptr<Track> t = track.lock ();
196 t->set_record_enabled (val >= 0.5 ? true : false, this);
200 Track::RecEnableControl::get_value () const
202 boost::shared_ptr<Track> t = track.lock ();
207 return (t->record_enabled() ? 1.0 : 0.0);
211 Track::record_enabled () const
213 return _diskstream && _diskstream->record_enabled ();
219 bool will_record = true;
220 for (PortSet::iterator i = _input->ports().begin(); i != _input->ports().end() && will_record; ++i) {
229 Track::prep_record_enabled (bool yn, void *src)
231 if (!_session.writable()) {
235 if (_freeze_record.state == Frozen) {
239 if (_route_group && src != _route_group && _route_group->is_active() && _route_group->is_recenable()) {
240 _route_group->apply (&Track::prep_record_enabled, yn, _route_group);
244 /* keep track of the meter point as it was before we rec-enabled */
245 if (!_diskstream->record_enabled()) {
246 _saved_meter_point = _meter_point;
252 will_follow = _diskstream->prep_record_enable ();
254 will_follow = _diskstream->prep_record_disable ();
259 if (_meter_point != MeterCustom) {
260 set_meter_point (MeterInput);
263 set_meter_point (_saved_meter_point);
269 Track::set_record_enabled (bool yn, void *src)
271 if (!_session.writable()) {
275 if (_freeze_record.state == Frozen) {
279 if (_route_group && src != _route_group && _route_group->is_active() && _route_group->is_recenable()) {
280 _route_group->apply (&Track::set_record_enabled, yn, _route_group);
284 _diskstream->set_record_enabled (yn);
286 _rec_enable_control->Changed ();
290 Track::parameter_changed (string const & p)
292 if (p == "track-name-number") {
293 resync_track_name ();
295 else if (p == "track-name-take") {
296 resync_track_name ();
298 else if (p == "take-name") {
299 if (_session.config.get_track_name_take()) {
300 resync_track_name ();
306 Track::resync_track_name ()
312 Track::set_name (const string& str)
316 if (record_enabled() && _session.actively_recording()) {
317 /* this messes things up if done while recording */
321 string diskstream_name = "";
322 if (_session.config.get_track_name_take () && !_session.config.get_take_name ().empty()) {
323 // Note: any text is fine, legalize_for_path() fixes this later
324 diskstream_name += _session.config.get_take_name ();
325 diskstream_name += "_";
327 const int64_t tracknumber = track_number();
328 if (tracknumber > 0 && _session.config.get_track_name_number()) {
329 char num[64], fmt[10];
330 snprintf(fmt, sizeof(fmt), "%%0%d" PRId64, _session.track_number_decimals());
331 snprintf(num, sizeof(num), fmt, tracknumber);
332 diskstream_name += num;
333 diskstream_name += "_";
335 diskstream_name += str;
337 if (diskstream_name == _diskstream_name) {
340 _diskstream_name = diskstream_name;
342 _diskstream->set_write_source_name (diskstream_name);
344 boost::shared_ptr<Track> me = boost::dynamic_pointer_cast<Track> (shared_from_this ());
345 if (_diskstream->playlist()->all_regions_empty () && _session.playlists->playlists_for_track (me).size() == 1) {
346 /* Only rename the diskstream (and therefore the playlist) if
347 a) the playlist has never had a region added to it and
348 b) there is only one playlist for this track.
350 If (a) is not followed, people can get confused if, say,
351 they have notes about a playlist with a given name and then
352 it changes (see mantis #4759).
354 If (b) is not followed, we rename the current playlist and not
355 the other ones, which is a bit confusing (see mantis #4977).
357 _diskstream->set_name (str);
360 /* save state so that the statefile fully reflects any filename changes */
362 if ((ret = Route::set_name (str)) == 0) {
363 _session.save_state ("");
370 Track::set_latency_compensation (framecnt_t longest_session_latency)
372 Route::set_latency_compensation (longest_session_latency);
373 _diskstream->set_roll_delay (_roll_delay);
377 Track::no_roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, bool session_state_changing)
379 Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
385 bool can_record = _session.actively_recording ();
387 /* no outputs? nothing to do ... what happens if we have sends etc. ? */
389 if (n_outputs().n_total() == 0) {
393 /* not active ... do the minimum possible by just outputting silence */
397 if (_meter_point == MeterInput && (_monitoring & MonitorInput || _diskstream->record_enabled())) {
403 if (session_state_changing) {
404 if (_session.transport_speed() != 0.0f) {
405 /* we're rolling but some state is changing (e.g. our diskstream contents)
406 so we cannot use them. Be silent till this is over. Don't declick.
408 XXX note the absurdity of ::no_roll() being called when we ARE rolling!
410 passthru_silence (start_frame, end_frame, nframes, 0);
413 /* we're really not rolling, so we're either delivery silence or actually
414 monitoring, both of which are safe to do while session_state_changing is true.
418 _diskstream->check_record_status (start_frame, can_record);
422 MonitorState const s = monitoring_state ();
423 /* we are not rolling, so be silent even if we are monitoring disk, as there
424 will be no disk data coming in.
427 case MonitoringSilence:
433 case MonitoringInput:
441 //if we have an internal generator, let it play regardless of monitoring state
442 if (_have_internal_generator) {
446 _amp->apply_gain_automation (false);
448 /* if have_internal_generator, or .. */
452 if (_meter_point == MeterInput) {
453 /* still need input monitoring and metering */
455 bool const track_rec = _diskstream->record_enabled ();
456 bool const auto_input = _session.config.get_auto_input ();
457 bool const software_monitor = Config->get_monitoring_model() == SoftwareMonitoring;
458 bool const tape_machine_mode = Config->get_tape_machine_mode ();
459 bool no_meter = false;
461 /* this needs a proper K-map
462 * and should be separated into a function similar to monitoring_state()
463 * that also handles roll() states in audio_track.cc, midi_track.cc and route.cc
465 * see http://www.oofus.co.uk/ardour/Ardour3MonitorModesV3.pdf
467 if (!auto_input && !track_rec) {
470 else if (tape_machine_mode && !track_rec && auto_input) {
473 else if (!software_monitor && tape_machine_mode && !track_rec) {
476 else if (!software_monitor && !tape_machine_mode && !track_rec && !auto_input) {
481 BufferSet& bufs (_session.get_silent_buffers (n_process_buffers()));
482 _meter->run (bufs, 0, 0, nframes, true);
483 _input->process_input (boost::shared_ptr<Processor>(), start_frame, end_frame, nframes);
485 _input->process_input (_meter, start_frame, end_frame, nframes);
489 passthru_silence (start_frame, end_frame, nframes, 0);
493 BufferSet& bufs = _session.get_route_buffers (n_process_buffers());
495 fill_buffers_with_input (bufs, _input, nframes);
497 if (_meter_point == MeterInput) {
498 _meter->run (bufs, start_frame, end_frame, nframes, true);
501 passthru (bufs, start_frame, end_frame, nframes, false);
504 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
505 boost::shared_ptr<Delivery> d = boost::dynamic_pointer_cast<Delivery> (*i);
507 d->flush_buffers (nframes);
515 Track::silent_roll (pframes_t nframes, framepos_t /*start_frame*/, framepos_t /*end_frame*/, bool& need_butler)
517 Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
519 framecnt_t playback_distance = _diskstream->calculate_playback_distance(nframes);
520 if (can_internal_playback_seek(playback_distance)) {
521 internal_playback_seek(playback_distance);
526 if (n_outputs().n_total() == 0 && _processors.empty()) {
536 _amp->apply_gain_automation(false);
540 framecnt_t playback_distance;
542 BufferSet& bufs (_session.get_route_buffers (n_process_buffers(), true));
544 int const dret = _diskstream->process (bufs, _session.transport_frame(), nframes, playback_distance, false);
545 need_butler = _diskstream->commit (playback_distance);
550 Track::set_diskstream (boost::shared_ptr<Diskstream> ds)
554 ds->PlaylistChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_playlist_changed, this));
555 diskstream_playlist_changed ();
556 ds->RecordEnableChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_record_enable_changed, this));
557 ds->SpeedChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_speed_changed, this));
558 ds->AlignmentStyleChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_alignment_style_changed, this));
562 Track::diskstream_playlist_changed ()
564 PlaylistChanged (); /* EMIT SIGNAL */
568 Track::diskstream_record_enable_changed ()
570 RecordEnableChanged (); /* EMIT SIGNAL */
574 Track::diskstream_speed_changed ()
576 SpeedChanged (); /* EMIT SIGNAL */
580 Track::diskstream_alignment_style_changed ()
582 AlignmentStyleChanged (); /* EMIT SIGNAL */
585 boost::shared_ptr<Playlist>
588 return _diskstream->playlist ();
592 Track::request_input_monitoring (bool m)
594 _diskstream->request_input_monitoring (m);
598 Track::ensure_input_monitoring (bool m)
600 _diskstream->ensure_input_monitoring (m);
604 Track::destructive () const
606 return _diskstream->destructive ();
609 list<boost::shared_ptr<Source> > &
610 Track::last_capture_sources ()
612 return _diskstream->last_capture_sources ();
616 Track::set_capture_offset ()
618 _diskstream->set_capture_offset ();
622 Track::steal_write_source_name()
624 return _diskstream->steal_write_source_name ();
628 Track::reset_write_sources (bool r, bool force)
630 _diskstream->reset_write_sources (r, force);
634 Track::playback_buffer_load () const
636 return _diskstream->playback_buffer_load ();
640 Track::capture_buffer_load () const
642 return _diskstream->capture_buffer_load ();
648 return _diskstream->do_refill ();
652 Track::do_flush (RunContext c, bool force)
654 return _diskstream->do_flush (c, force);
658 Track::set_pending_overwrite (bool o)
660 _diskstream->set_pending_overwrite (o);
664 Track::seek (framepos_t p, bool complete_refill)
666 return _diskstream->seek (p, complete_refill);
670 Track::hidden () const
672 return _diskstream->hidden ();
676 Track::can_internal_playback_seek (framecnt_t p)
678 return _diskstream->can_internal_playback_seek (p);
682 Track::internal_playback_seek (framecnt_t p)
684 return _diskstream->internal_playback_seek (p);
688 Track::non_realtime_input_change ()
690 _diskstream->non_realtime_input_change ();
694 Track::non_realtime_locate (framepos_t p)
696 Route::non_realtime_locate (p);
699 /* don't waste i/o cycles and butler calls
700 for hidden (secret) tracks
702 _diskstream->non_realtime_locate (p);
707 Track::non_realtime_set_speed ()
709 _diskstream->non_realtime_set_speed ();
713 Track::overwrite_existing_buffers ()
715 return _diskstream->overwrite_existing_buffers ();
719 Track::get_captured_frames (uint32_t n) const
721 return _diskstream->get_captured_frames (n);
725 Track::set_loop (Location* l)
727 return _diskstream->set_loop (l);
731 Track::transport_looped (framepos_t p)
733 _diskstream->transport_looped (p);
737 Track::realtime_set_speed (double s, bool g)
739 return _diskstream->realtime_set_speed (s, g);
743 Track::transport_stopped_wallclock (struct tm & n, time_t t, bool g)
745 _diskstream->transport_stopped_wallclock (n, t, g);
749 Track::pending_overwrite () const
751 return _diskstream->pending_overwrite ();
755 Track::speed () const
757 return _diskstream->speed ();
761 Track::prepare_to_stop (framepos_t t, framepos_t a)
763 _diskstream->prepare_to_stop (t, a);
767 Track::set_slaved (bool s)
769 _diskstream->set_slaved (s);
775 return _diskstream->n_channels ();
779 Track::get_capture_start_frame (uint32_t n) const
781 return _diskstream->get_capture_start_frame (n);
785 Track::alignment_style () const
787 return _diskstream->alignment_style ();
791 Track::alignment_choice () const
793 return _diskstream->alignment_choice ();
797 Track::current_capture_start () const
799 return _diskstream->current_capture_start ();
803 Track::current_capture_end () const
805 return _diskstream->current_capture_end ();
809 Track::playlist_modified ()
811 _diskstream->playlist_modified ();
815 Track::use_playlist (boost::shared_ptr<Playlist> p)
817 int ret = _diskstream->use_playlist (p);
819 p->set_orig_track_id (id());
825 Track::use_copy_playlist ()
827 int ret = _diskstream->use_copy_playlist ();
830 _diskstream->playlist()->set_orig_track_id (id());
837 Track::use_new_playlist ()
839 int ret = _diskstream->use_new_playlist ();
842 _diskstream->playlist()->set_orig_track_id (id());
849 Track::set_align_style (AlignStyle s, bool force)
851 _diskstream->set_align_style (s, force);
855 Track::set_align_choice (AlignChoice s, bool force)
857 _diskstream->set_align_choice (s, force);
861 Track::using_diskstream_id (PBD::ID id) const
863 return (id == _diskstream->id ());
867 Track::set_block_size (pframes_t n)
869 Route::set_block_size (n);
870 _diskstream->set_block_size (n);
874 Track::adjust_playback_buffering ()
877 _diskstream->adjust_playback_buffering ();
882 Track::adjust_capture_buffering ()
885 _diskstream->adjust_capture_buffering ();
890 Track::monitoring_state () const
892 /* Explicit requests */
894 if (_monitoring & MonitorInput) {
895 return MonitoringInput;
898 if (_monitoring & MonitorDisk) {
899 return MonitoringDisk;
902 /* This is an implementation of the truth table in doc/monitor_modes.pdf;
903 I don't think it's ever going to be too pretty too look at.
906 bool const roll = _session.transport_rolling ();
907 bool const track_rec = _diskstream->record_enabled ();
908 bool const auto_input = _session.config.get_auto_input ();
909 bool const software_monitor = Config->get_monitoring_model() == SoftwareMonitoring;
910 bool const tape_machine_mode = Config->get_tape_machine_mode ();
913 /* I suspect that just use actively_recording() is good enough all the
914 * time, but just to keep the semantics the same as they were before
915 * sept 26th 2012, we differentiate between the cases where punch is
916 * enabled and those where it is not.
919 if (_session.config.get_punch_in() || _session.config.get_punch_out()) {
920 session_rec = _session.actively_recording ();
922 session_rec = _session.get_record_enabled();
927 if (!session_rec && roll && auto_input) {
928 return MonitoringDisk;
930 return software_monitor ? MonitoringInput : MonitoringSilence;
935 if (tape_machine_mode) {
937 return MonitoringDisk;
941 if (!roll && auto_input) {
942 return software_monitor ? MonitoringInput : MonitoringSilence;
944 return MonitoringDisk;
951 return MonitoringSilence;
955 Track::maybe_declick (BufferSet& bufs, framecnt_t nframes, int declick)
957 /* never declick if there is an internal generator - we just want it to
958 keep generating sound without interruption.
960 ditto if we are monitoring inputs.
963 if (_have_internal_generator || monitoring_choice() == MonitorInput) {
968 declick = _pending_declick;
972 Amp::declick (bufs, nframes, declick);
977 Track::check_initial_delay (framecnt_t nframes, framepos_t& transport_frame)
979 if (_roll_delay > nframes) {
981 _roll_delay -= nframes;
982 silence_unlocked (nframes);
983 /* transport frame is not legal for caller to use */
986 } else if (_roll_delay > 0) {
988 nframes -= _roll_delay;
989 silence_unlocked (_roll_delay);
990 transport_frame += _roll_delay;
992 /* shuffle all the port buffers for things that lead "out" of this Route
993 to reflect that we just wrote _roll_delay frames of silence.
996 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
997 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
998 boost::shared_ptr<IOProcessor> iop = boost::dynamic_pointer_cast<IOProcessor> (*i);
1000 iop->increment_port_buffer_offset (_roll_delay);
1003 _output->increment_port_buffer_offset (_roll_delay);
1013 Track::set_monitoring (MonitorChoice mc)
1015 if (mc != _monitoring) {
1018 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1019 (*i)->monitoring_changed ();
1022 MonitoringChanged (); /* EMIT SIGNAL */
1027 Track::metering_state () const
1030 if (_session.transport_rolling ()) {
1031 // audio_track.cc || midi_track.cc roll() runs meter IFF:
1032 rv = _meter_point == MeterInput && (_monitoring & MonitorInput || _diskstream->record_enabled());
1034 // track no_roll() always metering if
1035 rv = _meter_point == MeterInput;
1037 return rv ? MeteringInput : MeteringRoute;