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
76 Track::use_new_diskstream ()
78 boost::shared_ptr<Diskstream> ds = create_diskstream ();
80 ds->do_refill_with_alloc ();
81 ds->set_block_size (_session.get_block_size ());
82 ds->playlist()->set_orig_track_id (id());
94 Track::state (bool full)
96 XMLNode& root (Route::state (full));
97 root.add_property (X_("monitoring"), enum_2_string (_monitoring));
98 root.add_property (X_("saved-meter-point"), enum_2_string (_saved_meter_point));
99 root.add_child_nocopy (_rec_enable_control->get_state());
100 root.add_child_nocopy (_diskstream->get_state ());
106 Track::set_state (const XMLNode& node, int version)
108 if (Route::set_state (node, version)) {
114 if (version >= 3000) {
115 if ((child = find_named_node (node, X_("Diskstream"))) != 0) {
116 boost::shared_ptr<Diskstream> ds = diskstream_factory (*child);
117 ds->do_refill_with_alloc ();
123 _diskstream->playlist()->set_orig_track_id (id());
126 /* set rec-enable control *AFTER* setting up diskstream, because it may
127 want to operate on the diskstream as it sets its own state
130 XMLNodeList nlist = node.children();
131 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
135 if (child->name() == Controllable::xml_node_name && (prop = child->property ("name")) != 0) {
136 if (prop->value() == X_("recenable")) {
137 _rec_enable_control->set_state (*child, version);
142 const XMLProperty* prop;
144 if ((prop = node.property (X_("monitoring"))) != 0) {
145 _monitoring = MonitorChoice (string_2_enum (prop->value(), _monitoring));
147 _monitoring = MonitorAuto;
150 if ((prop = node.property (X_("saved-meter-point"))) != 0) {
151 _saved_meter_point = MeterPoint (string_2_enum (prop->value(), _saved_meter_point));
153 _saved_meter_point = _meter_point;
160 Track::get_template ()
162 return state (false);
165 Track::FreezeRecord::~FreezeRecord ()
167 for (vector<FreezeRecordProcessorInfo*>::iterator i = processor_info.begin(); i != processor_info.end(); ++i) {
173 Track::freeze_state() const
175 return _freeze_record.state;
178 Track::RecEnableControl::RecEnableControl (boost::shared_ptr<Track> t)
179 : AutomationControl (t->session(), RecEnableAutomation, boost::shared_ptr<AutomationList>(), X_("recenable"))
182 boost::shared_ptr<AutomationList> gl(new AutomationList(Evoral::Parameter(RecEnableAutomation)));
187 Track::RecEnableControl::set_value (double val)
189 boost::shared_ptr<Track> t = track.lock ();
194 t->set_record_enabled (val >= 0.5 ? true : false, this);
198 Track::RecEnableControl::get_value () const
200 boost::shared_ptr<Track> t = track.lock ();
205 return (t->record_enabled() ? 1.0 : 0.0);
209 Track::record_enabled () const
211 return _diskstream && _diskstream->record_enabled ();
217 bool will_record = true;
218 for (PortSet::iterator i = _input->ports().begin(); i != _input->ports().end() && will_record; ++i) {
227 Track::prep_record_enabled (bool yn, void *src)
229 if (!_session.writable()) {
233 if (_freeze_record.state == Frozen) {
237 if (_route_group && src != _route_group && _route_group->is_active() && _route_group->is_recenable()) {
238 _route_group->apply (&Track::prep_record_enabled, yn, _route_group);
242 /* keep track of the meter point as it was before we rec-enabled */
243 if (!_diskstream->record_enabled()) {
244 _saved_meter_point = _meter_point;
250 will_follow = _diskstream->prep_record_enable ();
252 will_follow = _diskstream->prep_record_disable ();
257 if (_meter_point != MeterCustom) {
258 set_meter_point (MeterInput);
261 set_meter_point (_saved_meter_point);
267 Track::set_record_enabled (bool yn, void *src)
269 if (!_session.writable()) {
273 if (_freeze_record.state == Frozen) {
277 if (_route_group && src != _route_group && _route_group->is_active() && _route_group->is_recenable()) {
278 _route_group->apply (&Track::set_record_enabled, yn, _route_group);
282 _diskstream->set_record_enabled (yn);
284 _rec_enable_control->Changed ();
288 Track::set_name (const string& str)
292 if (record_enabled() && _session.actively_recording()) {
293 /* this messes things up if done while recording */
297 boost::shared_ptr<Track> me = boost::dynamic_pointer_cast<Track> (shared_from_this ());
298 if (_diskstream->playlist()->all_regions_empty () && _session.playlists->playlists_for_track (me).size() == 1) {
299 /* Only rename the diskstream (and therefore the playlist) if
300 a) the playlist has never had a region added to it and
301 b) there is only one playlist for this track.
303 If (a) is not followed, people can get confused if, say,
304 they have notes about a playlist with a given name and then
305 it changes (see mantis #4759).
307 If (b) is not followed, we rename the current playlist and not
308 the other ones, which is a bit confusing (see mantis #4977).
310 _diskstream->set_name (str);
313 /* save state so that the statefile fully reflects any filename changes */
315 if ((ret = Route::set_name (str)) == 0) {
316 _session.save_state ("");
323 Track::set_latency_compensation (framecnt_t longest_session_latency)
325 Route::set_latency_compensation (longest_session_latency);
326 _diskstream->set_roll_delay (_roll_delay);
330 Track::no_roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, bool session_state_changing)
332 Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
338 bool can_record = _session.actively_recording ();
340 /* no outputs? nothing to do ... what happens if we have sends etc. ? */
342 if (n_outputs().n_total() == 0) {
346 /* not active ... do the minimum possible by just outputting silence */
350 if (_meter_point == MeterInput && (_monitoring & MonitorInput || _diskstream->record_enabled())) {
356 if (session_state_changing) {
357 if (_session.transport_speed() != 0.0f) {
358 /* we're rolling but some state is changing (e.g. our diskstream contents)
359 so we cannot use them. Be silent till this is over. Don't declick.
361 XXX note the absurdity of ::no_roll() being called when we ARE rolling!
363 passthru_silence (start_frame, end_frame, nframes, 0);
366 /* we're really not rolling, so we're either delivery silence or actually
367 monitoring, both of which are safe to do while session_state_changing is true.
371 _diskstream->check_record_status (start_frame, can_record);
375 if (_have_internal_generator) {
376 /* since the instrument has no input streams,
377 there is no reason to send any signal
384 MonitorState const s = monitoring_state ();
385 /* we are not rolling, so be silent even if we are monitoring disk, as there
386 will be no disk data coming in.
389 case MonitoringSilence:
390 /* if there is an instrument, be_silent should always
393 be_silent = (the_instrument_unlocked() == 0);
398 case MonitoringInput:
404 _amp->apply_gain_automation (false);
406 /* if have_internal_generator, or .. */
410 if (_meter_point == MeterInput) {
411 /* still need input monitoring and metering */
413 bool const track_rec = _diskstream->record_enabled ();
414 bool const auto_input = _session.config.get_auto_input ();
415 bool const software_monitor = Config->get_monitoring_model() == SoftwareMonitoring;
416 bool const tape_machine_mode = Config->get_tape_machine_mode ();
418 if (!software_monitor && tape_machine_mode && !track_rec) {
420 _input->process_input (boost::shared_ptr<Processor>(), start_frame, end_frame, nframes);
422 else if (!software_monitor && !tape_machine_mode && !track_rec && !auto_input) {
424 _input->process_input (boost::shared_ptr<Processor>(), start_frame, end_frame, nframes);
426 _input->process_input (_meter, start_frame, end_frame, nframes);
430 passthru_silence (start_frame, end_frame, nframes, 0);
434 BufferSet& bufs = _session.get_scratch_buffers (n_process_buffers());
436 fill_buffers_with_input (bufs, _input, nframes);
438 if (_meter_point == MeterInput) {
439 _meter->run (bufs, start_frame, end_frame, nframes, true);
442 passthru (bufs, start_frame, end_frame, nframes, false);
445 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
446 boost::shared_ptr<Delivery> d = boost::dynamic_pointer_cast<Delivery> (*i);
448 d->flush_buffers (nframes);
456 Track::silent_roll (pframes_t nframes, framepos_t /*start_frame*/, framepos_t /*end_frame*/, bool& need_butler)
458 Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
463 if (n_outputs().n_total() == 0 && _processors.empty()) {
473 _amp->apply_gain_automation(false);
477 framecnt_t playback_distance;
479 BufferSet& bufs (_session.get_silent_buffers (n_process_buffers()));
481 int const dret = _diskstream->process (bufs, _session.transport_frame(), nframes, playback_distance, false);
482 need_butler = _diskstream->commit (playback_distance);
487 Track::set_diskstream (boost::shared_ptr<Diskstream> ds)
491 ds->PlaylistChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_playlist_changed, this));
492 diskstream_playlist_changed ();
493 ds->RecordEnableChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_record_enable_changed, this));
494 ds->SpeedChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_speed_changed, this));
495 ds->AlignmentStyleChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_alignment_style_changed, this));
499 Track::diskstream_playlist_changed ()
501 PlaylistChanged (); /* EMIT SIGNAL */
505 Track::diskstream_record_enable_changed ()
507 RecordEnableChanged (); /* EMIT SIGNAL */
511 Track::diskstream_speed_changed ()
513 SpeedChanged (); /* EMIT SIGNAL */
517 Track::diskstream_alignment_style_changed ()
519 AlignmentStyleChanged (); /* EMIT SIGNAL */
522 boost::shared_ptr<Playlist>
525 return _diskstream->playlist ();
529 Track::request_jack_monitors_input (bool m)
531 _diskstream->request_jack_monitors_input (m);
535 Track::ensure_jack_monitors_input (bool m)
537 _diskstream->ensure_jack_monitors_input (m);
541 Track::destructive () const
543 return _diskstream->destructive ();
546 list<boost::shared_ptr<Source> > &
547 Track::last_capture_sources ()
549 return _diskstream->last_capture_sources ();
553 Track::set_capture_offset ()
555 _diskstream->set_capture_offset ();
558 list<boost::shared_ptr<Source> >
559 Track::steal_write_sources()
561 return _diskstream->steal_write_sources ();
565 Track::reset_write_sources (bool r, bool force)
567 _diskstream->reset_write_sources (r, force);
571 Track::playback_buffer_load () const
573 return _diskstream->playback_buffer_load ();
577 Track::capture_buffer_load () const
579 return _diskstream->capture_buffer_load ();
585 return _diskstream->do_refill ();
589 Track::do_flush (RunContext c, bool force)
591 return _diskstream->do_flush (c, force);
595 Track::set_pending_overwrite (bool o)
597 _diskstream->set_pending_overwrite (o);
601 Track::seek (framepos_t p, bool complete_refill)
603 return _diskstream->seek (p, complete_refill);
607 Track::hidden () const
609 return _diskstream->hidden ();
613 Track::can_internal_playback_seek (framecnt_t p)
615 return _diskstream->can_internal_playback_seek (p);
619 Track::internal_playback_seek (framecnt_t p)
621 return _diskstream->internal_playback_seek (p);
625 Track::non_realtime_input_change ()
627 _diskstream->non_realtime_input_change ();
631 Track::non_realtime_locate (framepos_t p)
633 Route::non_realtime_locate (p);
636 /* don't waste i/o cycles and butler calls
637 for hidden (secret) tracks
639 _diskstream->non_realtime_locate (p);
644 Track::non_realtime_set_speed ()
646 _diskstream->non_realtime_set_speed ();
650 Track::overwrite_existing_buffers ()
652 return _diskstream->overwrite_existing_buffers ();
656 Track::get_captured_frames (uint32_t n) const
658 return _diskstream->get_captured_frames (n);
662 Track::set_loop (Location* l)
664 return _diskstream->set_loop (l);
668 Track::transport_looped (framepos_t p)
670 _diskstream->transport_looped (p);
674 Track::realtime_set_speed (double s, bool g)
676 return _diskstream->realtime_set_speed (s, g);
680 Track::transport_stopped_wallclock (struct tm & n, time_t t, bool g)
682 _diskstream->transport_stopped_wallclock (n, t, g);
686 Track::pending_overwrite () const
688 return _diskstream->pending_overwrite ();
692 Track::speed () const
694 return _diskstream->speed ();
698 Track::prepare_to_stop (framepos_t p)
700 _diskstream->prepare_to_stop (p);
704 Track::set_slaved (bool s)
706 _diskstream->set_slaved (s);
712 return _diskstream->n_channels ();
716 Track::get_capture_start_frame (uint32_t n) const
718 return _diskstream->get_capture_start_frame (n);
722 Track::alignment_style () const
724 return _diskstream->alignment_style ();
728 Track::alignment_choice () const
730 return _diskstream->alignment_choice ();
734 Track::current_capture_start () const
736 return _diskstream->current_capture_start ();
740 Track::current_capture_end () const
742 return _diskstream->current_capture_end ();
746 Track::playlist_modified ()
748 _diskstream->playlist_modified ();
752 Track::use_playlist (boost::shared_ptr<Playlist> p)
754 int ret = _diskstream->use_playlist (p);
756 p->set_orig_track_id (id());
762 Track::use_copy_playlist ()
764 int ret = _diskstream->use_copy_playlist ();
767 _diskstream->playlist()->set_orig_track_id (id());
774 Track::use_new_playlist ()
776 int ret = _diskstream->use_new_playlist ();
779 _diskstream->playlist()->set_orig_track_id (id());
786 Track::set_align_style (AlignStyle s, bool force)
788 _diskstream->set_align_style (s, force);
792 Track::set_align_choice (AlignChoice s, bool force)
794 _diskstream->set_align_choice (s, force);
798 Track::using_diskstream_id (PBD::ID id) const
800 return (id == _diskstream->id ());
804 Track::set_block_size (pframes_t n)
806 Route::set_block_size (n);
807 _diskstream->set_block_size (n);
811 Track::adjust_playback_buffering ()
814 _diskstream->adjust_playback_buffering ();
819 Track::adjust_capture_buffering ()
822 _diskstream->adjust_capture_buffering ();
827 Track::monitoring_state () const
829 /* Explicit requests */
831 if (_monitoring & MonitorInput) {
832 return MonitoringInput;
835 if (_monitoring & MonitorDisk) {
836 return MonitoringDisk;
839 /* This is an implementation of the truth table in doc/monitor_modes.pdf;
840 I don't think it's ever going to be too pretty too look at.
843 bool const roll = _session.transport_rolling ();
844 bool const track_rec = _diskstream->record_enabled ();
845 bool const auto_input = _session.config.get_auto_input ();
846 bool const software_monitor = Config->get_monitoring_model() == SoftwareMonitoring;
847 bool const tape_machine_mode = Config->get_tape_machine_mode ();
850 /* I suspect that just use actively_recording() is good enough all the
851 * time, but just to keep the semantics the same as they were before
852 * sept 26th 2012, we differentiate between the cases where punch is
853 * enabled and those where it is not.
856 if (_session.config.get_punch_in() || _session.config.get_punch_out()) {
857 session_rec = _session.actively_recording ();
859 session_rec = _session.get_record_enabled();
864 if (!session_rec && roll && auto_input) {
865 return MonitoringDisk;
867 return software_monitor ? MonitoringInput : MonitoringSilence;
872 if (tape_machine_mode) {
874 return MonitoringDisk;
878 if (!roll && auto_input) {
879 return software_monitor ? MonitoringInput : MonitoringSilence;
881 return MonitoringDisk;
888 return MonitoringSilence;
892 Track::maybe_declick (BufferSet& bufs, framecnt_t nframes, int declick)
894 /* never declick if there is an internal generator - we just want it to
895 keep generating sound without interruption.
897 ditto if we are monitoring inputs.
900 if (_have_internal_generator || monitoring_choice() == MonitorInput) {
905 declick = _pending_declick;
909 Amp::declick (bufs, nframes, declick);
914 Track::check_initial_delay (framecnt_t nframes, framepos_t& transport_frame)
916 if (_roll_delay > nframes) {
918 _roll_delay -= nframes;
919 silence_unlocked (nframes);
920 /* transport frame is not legal for caller to use */
923 } else if (_roll_delay > 0) {
925 nframes -= _roll_delay;
926 silence_unlocked (_roll_delay);
927 transport_frame += _roll_delay;
929 /* shuffle all the port buffers for things that lead "out" of this Route
930 to reflect that we just wrote _roll_delay frames of silence.
933 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
934 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
935 boost::shared_ptr<IOProcessor> iop = boost::dynamic_pointer_cast<IOProcessor> (*i);
937 iop->increment_port_buffer_offset (_roll_delay);
940 _output->increment_port_buffer_offset (_roll_delay);
950 Track::set_monitoring (MonitorChoice mc)
952 if (mc != _monitoring) {
955 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
956 (*i)->monitoring_changed ();
959 MonitoringChanged (); /* EMIT SIGNAL */
964 Track::metering_state () const
967 if (_session.transport_rolling ()) {
968 // audio_track.cc || midi_track.cc roll() runs meter IFF:
969 rv = _meter_point == MeterInput && (_monitoring & MonitorInput || _diskstream->record_enabled());
971 // track no_roll() always metering if
972 rv = _meter_point == MeterInput;
974 return rv ? MeteringInput : MeteringRoute;