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));
71 Track::use_new_diskstream ()
73 boost::shared_ptr<Diskstream> ds = create_diskstream ();
75 ds->do_refill_with_alloc ();
76 ds->set_block_size (_session.get_block_size ());
77 ds->playlist()->set_orig_track_id (id());
89 Track::state (bool full)
91 XMLNode& root (Route::state (full));
92 root.add_property (X_("monitoring"), enum_2_string (_monitoring));
93 root.add_property (X_("saved-meter-point"), enum_2_string (_saved_meter_point));
94 root.add_child_nocopy (_rec_enable_control->get_state());
95 root.add_child_nocopy (_diskstream->get_state ());
97 if (!_deactivated_processors.empty ()) {
98 XMLNode* node = new XMLNode (X_("DeactivatedProcessors"));
99 for (list<boost::weak_ptr<Processor> >::iterator i = _deactivated_processors.begin(); i != _deactivated_processors.end(); ++i) {
100 boost::shared_ptr<Processor> p = i->lock ();
102 XMLNode* c = new XMLNode (X_("Processor"));
103 c->add_property (X_("id"), p->id().to_s());
104 node->add_child_nocopy (*c);
107 root.add_child_nocopy (*node);
114 Track::set_state (const XMLNode& node, int version)
116 if (Route::set_state (node, version)) {
122 if (version >= 3000) {
123 if ((child = find_named_node (node, X_("Diskstream"))) != 0) {
124 boost::shared_ptr<Diskstream> ds = diskstream_factory (*child);
125 ds->do_refill_with_alloc ();
131 _diskstream->playlist()->set_orig_track_id (id());
134 /* set rec-enable control *AFTER* setting up diskstream, because it may
135 want to operate on the diskstream as it sets its own state
138 XMLNodeList nlist = node.children();
139 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
143 if (child->name() == Controllable::xml_node_name && (prop = child->property ("name")) != 0) {
144 if (prop->value() == X_("recenable")) {
145 _rec_enable_control->set_state (*child, version);
149 if (child->name() == X_("DeactivatedProcessors")) {
150 XMLNodeList dp = child->children ();
151 for (XMLNodeConstIterator i = dp.begin(); i != dp.end(); ++i) {
152 assert ((*i)->name() == X_("Processor"));
153 XMLProperty* prop = (*i)->property (X_("id"));
154 boost::shared_ptr<Processor> p = processor_by_id (PBD::ID (prop->value ()));
156 _deactivated_processors.push_back (p);
162 const XMLProperty* prop;
164 if ((prop = node.property (X_("monitoring"))) != 0) {
165 _monitoring = MonitorChoice (string_2_enum (prop->value(), _monitoring));
167 _monitoring = MonitorAuto;
170 if ((prop = node.property (X_("saved-meter-point"))) != 0) {
171 _saved_meter_point = MeterPoint (string_2_enum (prop->value(), _saved_meter_point));
173 _saved_meter_point = _meter_point;
180 Track::get_template ()
182 return state (false);
185 Track::FreezeRecord::~FreezeRecord ()
187 for (vector<FreezeRecordProcessorInfo*>::iterator i = processor_info.begin(); i != processor_info.end(); ++i) {
193 Track::freeze_state() const
195 return _freeze_record.state;
198 Track::RecEnableControllable::RecEnableControllable (Track& s)
199 : Controllable (X_("recenable")), track (s)
204 Track::RecEnableControllable::set_value (double val)
206 bool bval = ((val >= 0.5) ? true: false);
207 track.set_record_enabled (bval, this);
211 Track::RecEnableControllable::get_value (void) const
213 if (track.record_enabled()) { return 1.0; }
218 Track::record_enabled () const
220 return _diskstream && _diskstream->record_enabled ();
226 bool will_record = true;
227 for (PortSet::iterator i = _input->ports().begin(); i != _input->ports().end() && will_record; ++i) {
235 /* Turn off visible processors (except Fader), keeping track of the old states */
237 Track::deactivate_visible_processors ()
239 _deactivated_processors.clear ();
240 Glib::RWLock::ReaderLock lm (_processor_lock);
242 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
243 if ((*i)->active() && (*i)->display_to_user() && boost::dynamic_pointer_cast<Amp> (*i) == 0) {
245 _deactivated_processors.push_back (*i);
250 /* Turn deactivated processors back on again */
252 Track::activate_deactivated_processors ()
254 for (list<boost::weak_ptr<Processor> >::iterator i = _deactivated_processors.begin(); i != _deactivated_processors.end(); ++i) {
255 boost::shared_ptr<Processor> p = i->lock ();
263 Track::set_record_enabled (bool yn, void *src)
265 if (!_session.writable()) {
269 if (_freeze_record.state == Frozen) {
273 if (_route_group && src != _route_group && _route_group->is_active() && _route_group->is_recenable()) {
274 _route_group->apply (&Track::set_record_enabled, yn, _route_group);
278 /* keep track of the meter point as it was before we rec-enabled */
279 if (!_diskstream->record_enabled()) {
280 _saved_meter_point = _meter_point;
283 if (Config->get_do_not_record_plugins ()) {
285 deactivate_visible_processors ();
287 activate_deactivated_processors ();
291 _diskstream->set_record_enabled (yn);
293 if (_diskstream->record_enabled()) {
294 if (_meter_point != MeterCustom) {
295 set_meter_point (MeterInput);
298 set_meter_point (_saved_meter_point);
301 _rec_enable_control->Changed ();
306 Track::set_name (const string& str)
310 if (record_enabled() && _session.actively_recording()) {
311 /* this messes things up if done while recording */
315 _diskstream->set_name (str);
317 /* save state so that the statefile fully reflects any filename changes */
319 if ((ret = Route::set_name (str)) == 0) {
320 _session.save_state ("");
327 Track::set_latency_compensation (framecnt_t longest_session_latency)
329 Route::set_latency_compensation (longest_session_latency);
330 _diskstream->set_roll_delay (_roll_delay);
334 Track::no_roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, bool session_state_changing)
336 Glib::RWLock::ReaderLock lm (_processor_lock, Glib::TRY_LOCK);
341 bool can_record = _session.actively_recording ();
343 if (n_outputs().n_total() == 0) {
352 if (session_state_changing) {
353 if (_session.transport_speed() != 0.0f) {
354 /* we're rolling but some state is changing (e.g. our diskstream contents)
355 so we cannot use them. Be silent till this is over. Don't declick.
357 XXX note the absurdity of ::no_roll() being called when we ARE rolling!
359 passthru_silence (start_frame, end_frame, nframes, 0);
362 /* we're really not rolling, so we're either delivery silence or actually
363 monitoring, both of which are safe to do while session_state_changing is true.
367 _diskstream->check_record_status (start_frame, can_record);
371 if (_have_internal_generator) {
372 /* since the instrument has no input streams,
373 there is no reason to send any signal
378 MonitorState const s = monitoring_state ();
379 /* we are not rolling, so be silent even if we are monitoring disk, as there
380 will be no disk data coming in.
382 be_silent = (s == MonitoringSilence || s == MonitoringDisk);
385 if (!_have_internal_generator && metering_state() == MeteringInput) {
386 _input->process_input (_meter, start_frame, end_frame, nframes);
389 _amp->apply_gain_automation(false);
391 /* if have_internal_generator, or .. */
392 //_input->process_input (_meter, start_frame, end_frame, nframes);
396 passthru_silence (start_frame, end_frame, nframes, 0);
400 /* we're sending signal, but we may still want to meter the input.
403 passthru (start_frame, end_frame, nframes, false);
406 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
407 boost::shared_ptr<Delivery> d = boost::dynamic_pointer_cast<Delivery> (*i);
409 d->flush_buffers (nframes, end_frame - start_frame - 1);
417 Track::silent_roll (pframes_t nframes, framepos_t /*start_frame*/, framepos_t /*end_frame*/, bool& need_butler)
419 Glib::RWLock::ReaderLock lm (_processor_lock, Glib::TRY_LOCK);
424 if (n_outputs().n_total() == 0 && _processors.empty()) {
434 _amp->apply_gain_automation(false);
438 framecnt_t playback_distance;
439 int const dret = _diskstream->process (_session.transport_frame(), nframes, playback_distance);
440 need_butler = _diskstream->commit (playback_distance);
445 Track::set_diskstream (boost::shared_ptr<Diskstream> ds)
449 ds->PlaylistChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_playlist_changed, this));
450 diskstream_playlist_changed ();
451 ds->RecordEnableChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_record_enable_changed, this));
452 ds->SpeedChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_speed_changed, this));
453 ds->AlignmentStyleChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_alignment_style_changed, this));
457 Track::diskstream_playlist_changed ()
459 PlaylistChanged (); /* EMIT SIGNAL */
463 Track::diskstream_record_enable_changed ()
465 RecordEnableChanged (); /* EMIT SIGNAL */
469 Track::diskstream_speed_changed ()
471 SpeedChanged (); /* EMIT SIGNAL */
475 Track::diskstream_alignment_style_changed ()
477 AlignmentStyleChanged (); /* EMIT SIGNAL */
480 boost::shared_ptr<Playlist>
483 return _diskstream->playlist ();
487 Track::request_jack_monitors_input (bool m)
489 _diskstream->request_jack_monitors_input (m);
493 Track::ensure_jack_monitors_input (bool m)
495 _diskstream->ensure_jack_monitors_input (m);
499 Track::destructive () const
501 return _diskstream->destructive ();
504 list<boost::shared_ptr<Source> > &
505 Track::last_capture_sources ()
507 return _diskstream->last_capture_sources ();
511 Track::set_capture_offset ()
513 _diskstream->set_capture_offset ();
516 list<boost::shared_ptr<Source> >
517 Track::steal_write_sources()
519 return _diskstream->steal_write_sources ();
523 Track::reset_write_sources (bool r, bool force)
525 _diskstream->reset_write_sources (r, force);
529 Track::playback_buffer_load () const
531 return _diskstream->playback_buffer_load ();
535 Track::capture_buffer_load () const
537 return _diskstream->capture_buffer_load ();
543 return _diskstream->do_refill ();
547 Track::do_flush (RunContext c, bool force)
549 return _diskstream->do_flush (c, force);
553 Track::set_pending_overwrite (bool o)
555 _diskstream->set_pending_overwrite (o);
559 Track::seek (framepos_t p, bool complete_refill)
561 return _diskstream->seek (p, complete_refill);
565 Track::hidden () const
567 return _diskstream->hidden ();
571 Track::can_internal_playback_seek (framepos_t p)
573 return _diskstream->can_internal_playback_seek (p);
577 Track::internal_playback_seek (framepos_t p)
579 return _diskstream->internal_playback_seek (p);
583 Track::non_realtime_input_change ()
585 _diskstream->non_realtime_input_change ();
589 Track::non_realtime_locate (framepos_t p)
591 _diskstream->non_realtime_locate (p);
595 Track::non_realtime_set_speed ()
597 _diskstream->non_realtime_set_speed ();
601 Track::overwrite_existing_buffers ()
603 return _diskstream->overwrite_existing_buffers ();
607 Track::get_captured_frames (uint32_t n) const
609 return _diskstream->get_captured_frames (n);
613 Track::set_loop (Location* l)
615 return _diskstream->set_loop (l);
619 Track::transport_looped (framepos_t p)
621 _diskstream->transport_looped (p);
625 Track::realtime_set_speed (double s, bool g)
627 return _diskstream->realtime_set_speed (s, g);
631 Track::transport_stopped_wallclock (struct tm & n, time_t t, bool g)
633 _diskstream->transport_stopped_wallclock (n, t, g);
637 Track::pending_overwrite () const
639 return _diskstream->pending_overwrite ();
643 Track::speed () const
645 return _diskstream->speed ();
649 Track::prepare_to_stop (framepos_t p)
651 _diskstream->prepare_to_stop (p);
655 Track::set_slaved (bool s)
657 _diskstream->set_slaved (s);
663 return _diskstream->n_channels ();
667 Track::get_capture_start_frame (uint32_t n) const
669 return _diskstream->get_capture_start_frame (n);
673 Track::alignment_style () const
675 return _diskstream->alignment_style ();
679 Track::alignment_choice () const
681 return _diskstream->alignment_choice ();
685 Track::current_capture_start () const
687 return _diskstream->current_capture_start ();
691 Track::current_capture_end () const
693 return _diskstream->current_capture_end ();
697 Track::playlist_modified ()
699 _diskstream->playlist_modified ();
703 Track::use_playlist (boost::shared_ptr<Playlist> p)
705 int ret = _diskstream->use_playlist (p);
707 p->set_orig_track_id (id());
713 Track::use_copy_playlist ()
715 int ret = _diskstream->use_copy_playlist ();
718 _diskstream->playlist()->set_orig_track_id (id());
725 Track::use_new_playlist ()
727 int ret = _diskstream->use_new_playlist ();
730 _diskstream->playlist()->set_orig_track_id (id());
737 Track::set_align_style (AlignStyle s, bool force)
739 _diskstream->set_align_style (s, force);
743 Track::set_align_choice (AlignChoice s, bool force)
745 _diskstream->set_align_choice (s, force);
749 Track::using_diskstream_id (PBD::ID id) const
751 return (id == _diskstream->id ());
755 Track::set_block_size (pframes_t n)
757 Route::set_block_size (n);
758 _diskstream->set_block_size (n);
762 Track::adjust_playback_buffering ()
765 _diskstream->adjust_playback_buffering ();
770 Track::adjust_capture_buffering ()
773 _diskstream->adjust_capture_buffering ();
778 Track::monitoring_state () const
780 /* Explicit requests */
782 if (_monitoring & MonitorInput) {
783 return MonitoringInput;
786 if (_monitoring & MonitorDisk) {
787 return MonitoringDisk;
790 /* This is an implementation of the truth table in doc/monitor_modes.pdf;
791 I don't think it's ever going to be too pretty too look at.
794 bool const roll = _session.transport_rolling ();
795 bool const track_rec = _diskstream->record_enabled ();
796 bool const session_rec = _session.get_record_enabled ();
797 bool const auto_input = _session.config.get_auto_input ();
798 bool const software_monitor = Config->get_monitoring_model() == SoftwareMonitoring;
799 bool const tape_machine_mode = Config->get_tape_machine_mode ();
803 if (!session_rec && roll && auto_input) {
804 return MonitoringDisk;
806 return software_monitor ? MonitoringInput : MonitoringSilence;
811 if (tape_machine_mode) {
813 return MonitoringDisk;
817 if (!roll && auto_input) {
818 return software_monitor ? MonitoringInput : MonitoringSilence;
820 return MonitoringDisk;
827 return MonitoringSilence;
831 Track::maybe_declick (BufferSet& bufs, framecnt_t nframes, int declick)
833 /* never declick if there is an internal generator - we just want it to
834 keep generating sound without interruption.
836 ditto if we are monitoring inputs.
839 if (_have_internal_generator || monitoring_choice() == MonitorInput) {
844 declick = _pending_declick;
848 Amp::declick (bufs, nframes, declick);
853 Track::check_initial_delay (framecnt_t nframes, framecnt_t& transport_frame)
855 if (_roll_delay > nframes) {
857 _roll_delay -= nframes;
858 silence_unlocked (nframes);
859 /* transport frame is not legal for caller to use */
862 } else if (_roll_delay > 0) {
864 nframes -= _roll_delay;
865 silence_unlocked (_roll_delay);
866 transport_frame += _roll_delay;
868 /* shuffle all the port buffers for things that lead "out" of this Route
869 to reflect that we just wrote _roll_delay frames of silence.
872 Glib::RWLock::ReaderLock lm (_processor_lock);
873 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
874 boost::shared_ptr<IOProcessor> iop = boost::dynamic_pointer_cast<IOProcessor> (*i);
876 iop->increment_port_buffer_offset (_roll_delay);
879 _output->increment_port_buffer_offset (_roll_delay);
889 Track::set_monitoring (MonitorChoice mc)
891 if (mc != _monitoring) {
894 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
895 (*i)->monitoring_changed ();
898 MonitoringChanged (); /* EMIT SIGNAL */
903 Track::parameter_changed (string p)
905 if (p != "do-not-record-plugins") {
909 if (record_enabled ()) {
910 if (Config->get_do_not_record_plugins ()) {
911 deactivate_visible_processors ();
913 activate_deactivated_processors ();
919 Track::metering_state () const
921 return _diskstream->record_enabled() ? MeteringInput : MeteringRoute;