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/audioplaylist.h"
22 #include "ardour/audioregion.h"
23 #include "ardour/audiosource.h"
24 #include "ardour/debug.h"
25 #include "ardour/delivery.h"
26 #include "ardour/diskstream.h"
27 #include "ardour/io_processor.h"
28 #include "ardour/meter.h"
29 #include "ardour/port.h"
30 #include "ardour/processor.h"
31 #include "ardour/route_group_specialized.h"
32 #include "ardour/session.h"
33 #include "ardour/track.h"
34 #include "ardour/utils.h"
39 using namespace ARDOUR;
42 Track::Track (Session& sess, string name, Route::Flag flag, TrackMode mode, DataType default_type)
43 : Route (sess, name, flag, default_type)
44 , _saved_meter_point (_meter_point)
46 , _monitoring (MonitorAuto)
47 , _rec_enable_control (new RecEnableControllable(*this))
49 _freeze_record.state = NoFreeze;
52 Config->ParameterChanged.connect_same_thread (*this, boost::bind (&Track::parameter_changed, this, _1));
57 DEBUG_TRACE (DEBUG::Destruction, string_compose ("track %1 destructor\n", _name));
77 Track::state (bool full)
79 XMLNode& root (Route::state (full));
80 root.add_property (X_("monitoring"), enum_2_string (_monitoring));
81 root.add_property (X_("saved-meter-point"), enum_2_string (_saved_meter_point));
82 root.add_child_nocopy (_rec_enable_control->get_state());
83 root.add_child_nocopy (_diskstream->get_state ());
85 if (!_deactivated_processors.empty ()) {
86 XMLNode* node = new XMLNode (X_("DeactivatedProcessors"));
87 for (list<boost::weak_ptr<Processor> >::iterator i = _deactivated_processors.begin(); i != _deactivated_processors.end(); ++i) {
88 boost::shared_ptr<Processor> p = i->lock ();
90 XMLNode* c = new XMLNode (X_("Processor"));
91 c->add_property (X_("id"), p->id().to_s());
92 node->add_child_nocopy (*c);
95 root.add_child_nocopy (*node);
102 Track::set_state (const XMLNode& node, int version)
104 if (Route::set_state (node, version)) {
110 if (version >= 3000) {
111 if ((child = find_named_node (node, X_("Diskstream"))) != 0) {
112 boost::shared_ptr<Diskstream> ds = diskstream_factory (*child);
113 ds->do_refill_with_alloc ();
119 _diskstream->playlist()->set_orig_track_id (id());
122 /* set rec-enable control *AFTER* setting up diskstream, because it may
123 want to operate on the diskstream as it sets its own state
126 XMLNodeList nlist = node.children();
127 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
131 if (child->name() == Controllable::xml_node_name && (prop = child->property ("name")) != 0) {
132 if (prop->value() == X_("recenable")) {
133 _rec_enable_control->set_state (*child, version);
137 if (child->name() == X_("DeactivatedProcessors")) {
138 XMLNodeList dp = child->children ();
139 for (XMLNodeConstIterator i = dp.begin(); i != dp.end(); ++i) {
140 assert ((*i)->name() == X_("Processor"));
141 XMLProperty* prop = (*i)->property (X_("id"));
142 boost::shared_ptr<Processor> p = processor_by_id (PBD::ID (prop->value ()));
144 _deactivated_processors.push_back (p);
150 const XMLProperty* prop;
152 if ((prop = node.property (X_("monitoring"))) != 0) {
153 _monitoring = MonitorChoice (string_2_enum (prop->value(), _monitoring));
155 _monitoring = MonitorAuto;
158 if ((prop = node.property (X_("saved-meter-point"))) != 0) {
159 _saved_meter_point = MeterPoint (string_2_enum (prop->value(), _saved_meter_point));
161 _saved_meter_point = _meter_point;
168 Track::get_template ()
170 return state (false);
174 Track::toggle_monitor_input ()
176 for (PortSet::iterator i = _input->ports().begin(); i != _input->ports().end(); ++i) {
177 i->ensure_monitor_input(!i->monitoring_input());
181 Track::FreezeRecord::~FreezeRecord ()
183 for (vector<FreezeRecordProcessorInfo*>::iterator i = processor_info.begin(); i != processor_info.end(); ++i) {
189 Track::freeze_state() const
191 return _freeze_record.state;
194 Track::RecEnableControllable::RecEnableControllable (Track& s)
195 : Controllable (X_("recenable")), track (s)
200 Track::RecEnableControllable::set_value (double val)
202 bool bval = ((val >= 0.5) ? true: false);
203 track.set_record_enabled (bval, this);
207 Track::RecEnableControllable::get_value (void) const
209 if (track.record_enabled()) { return 1.0; }
214 Track::record_enabled () const
216 return _diskstream && _diskstream->record_enabled ();
222 bool will_record = true;
223 for (PortSet::iterator i = _input->ports().begin(); i != _input->ports().end() && will_record; ++i) {
231 /* Turn off visible processors (except Fader), keeping track of the old states */
233 Track::deactivate_visible_processors ()
235 _deactivated_processors.clear ();
236 Glib::RWLock::ReaderLock lm (_processor_lock);
238 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
239 if ((*i)->active() && (*i)->display_to_user() && boost::dynamic_pointer_cast<Amp> (*i) == 0) {
241 _deactivated_processors.push_back (*i);
246 /* Turn deactivated processors back on again */
248 Track::activate_deactivated_processors ()
250 for (list<boost::weak_ptr<Processor> >::iterator i = _deactivated_processors.begin(); i != _deactivated_processors.end(); ++i) {
251 boost::shared_ptr<Processor> p = i->lock ();
259 Track::set_record_enabled (bool yn, void *src)
261 if (!_session.writable()) {
265 if (_freeze_record.state == Frozen) {
269 if (_route_group && src != _route_group && _route_group->is_active() && _route_group->is_recenable()) {
270 _route_group->apply (&Track::set_record_enabled, yn, _route_group);
274 /* keep track of the meter point as it was before we rec-enabled */
275 if (!_diskstream->record_enabled()) {
276 _saved_meter_point = _meter_point;
279 if (Config->get_do_not_record_plugins ()) {
281 deactivate_visible_processors ();
283 activate_deactivated_processors ();
287 _diskstream->set_record_enabled (yn);
289 if (_diskstream->record_enabled()) {
290 if (_meter_point != MeterCustom) {
291 set_meter_point (MeterInput);
294 set_meter_point (_saved_meter_point);
297 _rec_enable_control->Changed ();
302 Track::set_name (const string& str)
306 if (record_enabled() && _session.actively_recording()) {
307 /* this messes things up if done while recording */
311 _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::RWLock::ReaderLock lm (_processor_lock, Glib::TRY_LOCK);
337 bool can_record = _session.actively_recording ();
339 if (n_outputs().n_total() == 0) {
348 if (session_state_changing) {
349 if (_session.transport_speed() != 0.0f) {
350 /* we're rolling but some state is changing (e.g. our diskstream contents)
351 so we cannot use them. Be silent till this is over. Don't declick.
353 XXX note the absurdity of ::no_roll() being called when we ARE rolling!
355 passthru_silence (start_frame, end_frame, nframes, 0);
358 /* we're really not rolling, so we're either delivery silence or actually
359 monitoring, both of which are safe to do while session_state_changing is true.
363 _diskstream->check_record_status (start_frame, can_record);
367 if (_have_internal_generator) {
368 /* since the instrument has no input streams,
369 there is no reason to send any signal
374 be_silent = send_silence ();
377 _amp->apply_gain_automation(false);
381 /* if we're sending silence, but we want the meters to show levels for the signal,
385 if (_have_internal_generator) {
386 passthru_silence (start_frame, end_frame, nframes, 0);
388 if (_meter_point == MeterInput) {
389 _input->process_input (_meter, start_frame, end_frame, nframes);
391 passthru_silence (start_frame, end_frame, nframes, 0);
396 /* we're sending signal, but we may still want to meter the input.
399 passthru (start_frame, end_frame, nframes, false);
402 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
403 boost::shared_ptr<Delivery> d = boost::dynamic_pointer_cast<Delivery> (*i);
405 d->flush_buffers (nframes, end_frame - start_frame - 1);
413 Track::silent_roll (pframes_t nframes, framepos_t /*start_frame*/, framepos_t /*end_frame*/, bool& need_butler)
415 Glib::RWLock::ReaderLock lm (_processor_lock, Glib::TRY_LOCK);
420 if (n_outputs().n_total() == 0 && _processors.empty()) {
430 _amp->apply_gain_automation(false);
434 framecnt_t playback_distance;
435 int const dret = _diskstream->process (_session.transport_frame(), nframes, playback_distance);
436 need_butler = _diskstream->commit (playback_distance);
441 Track::set_diskstream (boost::shared_ptr<Diskstream> ds)
445 ds->PlaylistChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_playlist_changed, this));
446 diskstream_playlist_changed ();
447 ds->RecordEnableChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_record_enable_changed, this));
448 ds->SpeedChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_speed_changed, this));
449 ds->AlignmentStyleChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_alignment_style_changed, this));
453 Track::diskstream_playlist_changed ()
455 PlaylistChanged (); /* EMIT SIGNAL */
459 Track::diskstream_record_enable_changed ()
461 RecordEnableChanged (); /* EMIT SIGNAL */
465 Track::diskstream_speed_changed ()
467 SpeedChanged (); /* EMIT SIGNAL */
471 Track::diskstream_alignment_style_changed ()
473 AlignmentStyleChanged (); /* EMIT SIGNAL */
476 boost::shared_ptr<Playlist>
479 return _diskstream->playlist ();
483 Track::monitor_input (bool m)
485 _diskstream->monitor_input (m);
489 Track::destructive () const
491 return _diskstream->destructive ();
494 list<boost::shared_ptr<Source> > &
495 Track::last_capture_sources ()
497 return _diskstream->last_capture_sources ();
501 Track::set_capture_offset ()
503 _diskstream->set_capture_offset ();
506 list<boost::shared_ptr<Source> >
507 Track::steal_write_sources()
509 return _diskstream->steal_write_sources ();
513 Track::reset_write_sources (bool r, bool force)
515 _diskstream->reset_write_sources (r, force);
519 Track::playback_buffer_load () const
521 return _diskstream->playback_buffer_load ();
525 Track::capture_buffer_load () const
527 return _diskstream->capture_buffer_load ();
533 return _diskstream->do_refill ();
537 Track::do_flush (RunContext c, bool force)
539 return _diskstream->do_flush (c, force);
543 Track::set_pending_overwrite (bool o)
545 _diskstream->set_pending_overwrite (o);
549 Track::seek (framepos_t p, bool complete_refill)
551 return _diskstream->seek (p, complete_refill);
555 Track::hidden () const
557 return _diskstream->hidden ();
561 Track::can_internal_playback_seek (framepos_t p)
563 return _diskstream->can_internal_playback_seek (p);
567 Track::internal_playback_seek (framepos_t p)
569 return _diskstream->internal_playback_seek (p);
573 Track::non_realtime_input_change ()
575 _diskstream->non_realtime_input_change ();
579 Track::non_realtime_locate (framepos_t p)
581 _diskstream->non_realtime_locate (p);
585 Track::non_realtime_set_speed ()
587 _diskstream->non_realtime_set_speed ();
591 Track::overwrite_existing_buffers ()
593 return _diskstream->overwrite_existing_buffers ();
597 Track::get_captured_frames (uint32_t n) const
599 return _diskstream->get_captured_frames (n);
603 Track::set_loop (Location* l)
605 return _diskstream->set_loop (l);
609 Track::transport_looped (framepos_t p)
611 _diskstream->transport_looped (p);
615 Track::realtime_set_speed (double s, bool g)
617 return _diskstream->realtime_set_speed (s, g);
621 Track::transport_stopped_wallclock (struct tm & n, time_t t, bool g)
623 _diskstream->transport_stopped_wallclock (n, t, g);
627 Track::pending_overwrite () const
629 return _diskstream->pending_overwrite ();
633 Track::speed () const
635 return _diskstream->speed ();
639 Track::prepare_to_stop (framepos_t p)
641 _diskstream->prepare_to_stop (p);
645 Track::set_slaved (bool s)
647 _diskstream->set_slaved (s);
653 return _diskstream->n_channels ();
657 Track::get_capture_start_frame (uint32_t n) const
659 return _diskstream->get_capture_start_frame (n);
663 Track::alignment_style () const
665 return _diskstream->alignment_style ();
669 Track::alignment_choice () const
671 return _diskstream->alignment_choice ();
675 Track::current_capture_start () const
677 return _diskstream->current_capture_start ();
681 Track::current_capture_end () const
683 return _diskstream->current_capture_end ();
687 Track::playlist_modified ()
689 _diskstream->playlist_modified ();
693 Track::use_playlist (boost::shared_ptr<Playlist> p)
695 return _diskstream->use_playlist (p);
699 Track::use_copy_playlist ()
701 int ret = _diskstream->use_copy_playlist ();
704 _diskstream->playlist()->set_orig_track_id (id());
711 Track::use_new_playlist ()
713 int ret = _diskstream->use_new_playlist ();
716 _diskstream->playlist()->set_orig_track_id (id());
723 Track::set_align_style (AlignStyle s, bool force)
725 _diskstream->set_align_style (s, force);
729 Track::set_align_choice (AlignChoice s, bool force)
731 _diskstream->set_align_choice (s, force);
735 Track::using_diskstream_id (PBD::ID id) const
737 return (id == _diskstream->id ());
741 Track::set_block_size (pframes_t n)
743 Route::set_block_size (n);
744 _diskstream->set_block_size (n);
748 Track::adjust_playback_buffering ()
751 _diskstream->adjust_playback_buffering ();
756 Track::adjust_capture_buffering ()
759 _diskstream->adjust_capture_buffering ();
764 Track::send_silence () const
768 if (Config->get_tape_machine_mode()) {
770 /* ADATs work in a strange way..
771 they monitor input always when stopped.and auto-input is engaged.
774 if ((Config->get_monitoring_model() == SoftwareMonitoring)
775 && ((_monitoring & MonitorInput) || (_diskstream->record_enabled()))) {
776 send_silence = false;
784 /* Other machines switch to input on stop if the track is record enabled,
785 regardless of the auto input setting (auto input only changes the
786 monitoring state when the transport is rolling)
789 if ((Config->get_monitoring_model() == SoftwareMonitoring)
790 && ((_monitoring & MonitorInput) ||
791 (!(_monitoring & MonitorDisk) && (_session.config.get_auto_input () || _diskstream->record_enabled())))){
793 DEBUG_TRACE (DEBUG::Monitor,
794 string_compose ("%1: no roll, use silence = FALSE, monitoring choice %2 recenable %3 sRA %4 autoinput %5\n",
795 name(), enum_2_string (_monitoring),
796 _diskstream->record_enabled(), _session.actively_recording(),
797 _session.config.get_auto_input()));
799 send_silence = false;
801 DEBUG_TRACE (DEBUG::Monitor,
802 string_compose ("%1: no roll, use silence = TRUE, monitoring choice %2 recenable %3 sRA %4 autoinput %5\n",
803 name(), enum_2_string (_monitoring),
804 _diskstream->record_enabled(), _session.actively_recording(),
805 _session.config.get_auto_input()));
814 Track::monitoring_state ()
816 MonitorState ms = MonitoringSilence;
818 if (_session.transport_rolling()) {
822 if (_monitoring & MonitorInput) { // explicitly requested input monitoring
824 ms = MonitoringInput;
826 } else if (_monitoring & MonitorDisk) { // explicitly requested disk monitoring
830 } else if (_diskstream->record_enabled() && _session.actively_recording()) { // Track actually recording
832 ms = MonitoringInput;
834 } else if (_diskstream->record_enabled() && !_session.actively_recording() && _session.config.get_auto_input()) { // Track armed but not recording, with auto input enabled
836 ms = MonitoringInput;
838 } else { // Every other state
848 if (send_silence()) {
850 ms = MonitoringSilence;
853 ms = MonitoringInput;
861 Track::maybe_declick (BufferSet& bufs, framecnt_t nframes, int declick)
863 /* never declick if there is an internal generator - we just want it to
864 keep generating sound without interruption.
866 ditto if we are monitoring inputs.
869 if (_have_internal_generator || monitoring_choice() == MonitorInput) {
874 declick = _pending_declick;
878 Amp::declick (bufs, nframes, declick);
883 Track::check_initial_delay (framecnt_t nframes, framecnt_t& transport_frame)
885 if (_roll_delay > nframes) {
887 _roll_delay -= nframes;
888 silence_unlocked (nframes);
889 /* transport frame is not legal for caller to use */
892 } else if (_roll_delay > 0) {
894 nframes -= _roll_delay;
895 silence_unlocked (_roll_delay);
896 transport_frame += _roll_delay;
898 /* shuffle all the port buffers for things that lead "out" of this Route
899 to reflect that we just wrote _roll_delay frames of silence.
902 Glib::RWLock::ReaderLock lm (_processor_lock);
903 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
904 boost::shared_ptr<IOProcessor> iop = boost::dynamic_pointer_cast<IOProcessor> (*i);
906 iop->increment_port_buffer_offset (_roll_delay);
909 _output->increment_port_buffer_offset (_roll_delay);
919 Track::set_monitoring (MonitorChoice mc)
921 if (mc != _monitoring) {
923 MonitoringChanged (); /* EMIT SIGNAL */
928 Track::parameter_changed (string p)
930 if (p != "do-not-record-plugins") {
934 if (record_enabled ()) {
935 if (Config->get_do_not_record_plugins ()) {
936 deactivate_visible_processors ();
938 activate_deactivated_processors ();