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/track.h"
32 #include "ardour/utils.h"
37 using namespace ARDOUR;
40 Track::Track (Session& sess, string name, Route::Flag flag, TrackMode mode, DataType default_type)
41 : Route (sess, name, flag, default_type)
42 , _saved_meter_point (_meter_point)
44 , _monitoring (MonitorAuto)
46 _freeze_record.state = NoFreeze;
49 Config->ParameterChanged.connect_same_thread (*this, boost::bind (&Track::parameter_changed, this, _1));
54 DEBUG_TRACE (DEBUG::Destruction, string_compose ("track %1 destructor\n", _name));
64 boost::shared_ptr<Route> rp (shared_from_this());
65 boost::shared_ptr<Track> rt = boost::dynamic_pointer_cast<Track> (rp);
66 _rec_enable_control = boost::shared_ptr<RecEnableControl> (new RecEnableControl(rt));
67 _rec_enable_control->set_flags (Controllable::Toggle);
69 /* don't add rec_enable_control to controls because we don't want it to
70 * appear as an automatable parameter
77 Track::use_new_diskstream ()
79 boost::shared_ptr<Diskstream> ds = create_diskstream ();
81 ds->do_refill_with_alloc ();
82 ds->set_block_size (_session.get_block_size ());
83 ds->playlist()->set_orig_track_id (id());
95 Track::state (bool full)
97 XMLNode& root (Route::state (full));
98 root.add_property (X_("monitoring"), enum_2_string (_monitoring));
99 root.add_property (X_("saved-meter-point"), enum_2_string (_saved_meter_point));
100 root.add_child_nocopy (_rec_enable_control->get_state());
101 root.add_child_nocopy (_diskstream->get_state ());
103 if (!_deactivated_processors.empty ()) {
104 XMLNode* node = new XMLNode (X_("DeactivatedProcessors"));
105 for (list<boost::weak_ptr<Processor> >::iterator i = _deactivated_processors.begin(); i != _deactivated_processors.end(); ++i) {
106 boost::shared_ptr<Processor> p = i->lock ();
108 XMLNode* c = new XMLNode (X_("Processor"));
109 c->add_property (X_("id"), p->id().to_s());
110 node->add_child_nocopy (*c);
113 root.add_child_nocopy (*node);
120 Track::set_state (const XMLNode& node, int version)
122 if (Route::set_state (node, version)) {
128 if (version >= 3000) {
129 if ((child = find_named_node (node, X_("Diskstream"))) != 0) {
130 boost::shared_ptr<Diskstream> ds = diskstream_factory (*child);
131 ds->do_refill_with_alloc ();
137 _diskstream->playlist()->set_orig_track_id (id());
140 /* set rec-enable control *AFTER* setting up diskstream, because it may
141 want to operate on the diskstream as it sets its own state
144 XMLNodeList nlist = node.children();
145 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
149 if (child->name() == Controllable::xml_node_name && (prop = child->property ("name")) != 0) {
150 if (prop->value() == X_("recenable")) {
151 _rec_enable_control->set_state (*child, version);
155 if (child->name() == X_("DeactivatedProcessors")) {
156 XMLNodeList dp = child->children ();
157 for (XMLNodeConstIterator i = dp.begin(); i != dp.end(); ++i) {
158 assert ((*i)->name() == X_("Processor"));
159 XMLProperty* prop = (*i)->property (X_("id"));
160 boost::shared_ptr<Processor> p = processor_by_id (PBD::ID (prop->value ()));
162 _deactivated_processors.push_back (p);
168 const XMLProperty* prop;
170 if ((prop = node.property (X_("monitoring"))) != 0) {
171 _monitoring = MonitorChoice (string_2_enum (prop->value(), _monitoring));
173 _monitoring = MonitorAuto;
176 if ((prop = node.property (X_("saved-meter-point"))) != 0) {
177 _saved_meter_point = MeterPoint (string_2_enum (prop->value(), _saved_meter_point));
179 _saved_meter_point = _meter_point;
186 Track::get_template ()
188 return state (false);
191 Track::FreezeRecord::~FreezeRecord ()
193 for (vector<FreezeRecordProcessorInfo*>::iterator i = processor_info.begin(); i != processor_info.end(); ++i) {
199 Track::freeze_state() const
201 return _freeze_record.state;
204 Track::RecEnableControl::RecEnableControl (boost::shared_ptr<Track> t)
205 : AutomationControl (t->session(), RecEnableAutomation, boost::shared_ptr<AutomationList>(), X_("recenable"))
208 boost::shared_ptr<AutomationList> gl(new AutomationList(Evoral::Parameter(RecEnableAutomation)));
213 Track::RecEnableControl::set_value (double val)
215 boost::shared_ptr<Track> t = track.lock ();
220 t->set_record_enabled (val >= 0.5 ? true : false, this);
224 Track::RecEnableControl::get_value () const
226 boost::shared_ptr<Track> t = track.lock ();
231 return (t->record_enabled() ? 1.0 : 0.0);
235 Track::record_enabled () const
237 return _diskstream && _diskstream->record_enabled ();
243 bool will_record = true;
244 for (PortSet::iterator i = _input->ports().begin(); i != _input->ports().end() && will_record; ++i) {
252 /* Turn off visible processors (except Fader), keeping track of the old states */
254 Track::deactivate_visible_processors ()
256 _deactivated_processors.clear ();
257 Glib::RWLock::ReaderLock lm (_processor_lock);
259 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
260 if ((*i)->active() && (*i)->display_to_user() && boost::dynamic_pointer_cast<Amp> (*i) == 0) {
262 _deactivated_processors.push_back (*i);
267 /* Turn deactivated processors back on again */
269 Track::activate_deactivated_processors ()
271 for (list<boost::weak_ptr<Processor> >::iterator i = _deactivated_processors.begin(); i != _deactivated_processors.end(); ++i) {
272 boost::shared_ptr<Processor> p = i->lock ();
280 Track::set_record_enabled (bool yn, void *src)
282 if (!_session.writable()) {
286 if (_freeze_record.state == Frozen) {
290 if (_route_group && src != _route_group && _route_group->is_active() && _route_group->is_recenable()) {
291 _route_group->apply (&Track::set_record_enabled, yn, _route_group);
295 /* keep track of the meter point as it was before we rec-enabled */
296 if (!_diskstream->record_enabled()) {
297 _saved_meter_point = _meter_point;
300 if (Config->get_do_not_record_plugins ()) {
302 deactivate_visible_processors ();
304 activate_deactivated_processors ();
308 _diskstream->set_record_enabled (yn);
310 if (_diskstream->record_enabled()) {
311 if (_meter_point != MeterCustom) {
312 set_meter_point (MeterInput);
315 set_meter_point (_saved_meter_point);
318 _rec_enable_control->Changed ();
323 Track::set_name (const string& str)
327 if (record_enabled() && _session.actively_recording()) {
328 /* this messes things up if done while recording */
332 _diskstream->set_name (str);
334 /* save state so that the statefile fully reflects any filename changes */
336 if ((ret = Route::set_name (str)) == 0) {
337 _session.save_state ("");
344 Track::set_latency_compensation (framecnt_t longest_session_latency)
346 Route::set_latency_compensation (longest_session_latency);
347 _diskstream->set_roll_delay (_roll_delay);
351 Track::no_roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, bool session_state_changing)
353 Glib::RWLock::ReaderLock lm (_processor_lock, Glib::TRY_LOCK);
358 bool can_record = _session.actively_recording ();
360 if (n_outputs().n_total() == 0) {
369 if (session_state_changing) {
370 if (_session.transport_speed() != 0.0f) {
371 /* we're rolling but some state is changing (e.g. our diskstream contents)
372 so we cannot use them. Be silent till this is over. Don't declick.
374 XXX note the absurdity of ::no_roll() being called when we ARE rolling!
376 passthru_silence (start_frame, end_frame, nframes, 0);
379 /* we're really not rolling, so we're either delivery silence or actually
380 monitoring, both of which are safe to do while session_state_changing is true.
384 _diskstream->check_record_status (start_frame, can_record);
388 if (_have_internal_generator) {
389 /* since the instrument has no input streams,
390 there is no reason to send any signal
395 MonitorState const s = monitoring_state ();
396 /* we are not rolling, so be silent even if we are monitoring disk, as there
397 will be no disk data coming in.
399 be_silent = (s == MonitoringSilence || s == MonitoringDisk);
402 if (!_have_internal_generator && metering_state() == MeteringInput) {
403 _input->process_input (_meter, start_frame, end_frame, nframes);
406 _amp->apply_gain_automation(false);
408 /* if have_internal_generator, or .. */
409 //_input->process_input (_meter, start_frame, end_frame, nframes);
413 passthru_silence (start_frame, end_frame, nframes, 0);
417 /* we're sending signal, but we may still want to meter the input.
420 passthru (start_frame, end_frame, nframes, false);
423 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
424 boost::shared_ptr<Delivery> d = boost::dynamic_pointer_cast<Delivery> (*i);
426 d->flush_buffers (nframes);
434 Track::silent_roll (pframes_t nframes, framepos_t /*start_frame*/, framepos_t /*end_frame*/, bool& need_butler)
436 Glib::RWLock::ReaderLock lm (_processor_lock, Glib::TRY_LOCK);
441 if (n_outputs().n_total() == 0 && _processors.empty()) {
451 _amp->apply_gain_automation(false);
455 framecnt_t playback_distance;
456 int const dret = _diskstream->process (_session.transport_frame(), nframes, playback_distance);
457 need_butler = _diskstream->commit (playback_distance);
462 Track::set_diskstream (boost::shared_ptr<Diskstream> ds)
466 ds->PlaylistChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_playlist_changed, this));
467 diskstream_playlist_changed ();
468 ds->RecordEnableChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_record_enable_changed, this));
469 ds->SpeedChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_speed_changed, this));
470 ds->AlignmentStyleChanged.connect_same_thread (*this, boost::bind (&Track::diskstream_alignment_style_changed, this));
474 Track::diskstream_playlist_changed ()
476 PlaylistChanged (); /* EMIT SIGNAL */
480 Track::diskstream_record_enable_changed ()
482 RecordEnableChanged (); /* EMIT SIGNAL */
486 Track::diskstream_speed_changed ()
488 SpeedChanged (); /* EMIT SIGNAL */
492 Track::diskstream_alignment_style_changed ()
494 AlignmentStyleChanged (); /* EMIT SIGNAL */
497 boost::shared_ptr<Playlist>
500 return _diskstream->playlist ();
504 Track::request_jack_monitors_input (bool m)
506 _diskstream->request_jack_monitors_input (m);
510 Track::ensure_jack_monitors_input (bool m)
512 _diskstream->ensure_jack_monitors_input (m);
516 Track::destructive () const
518 return _diskstream->destructive ();
521 list<boost::shared_ptr<Source> > &
522 Track::last_capture_sources ()
524 return _diskstream->last_capture_sources ();
528 Track::set_capture_offset ()
530 _diskstream->set_capture_offset ();
533 list<boost::shared_ptr<Source> >
534 Track::steal_write_sources()
536 return _diskstream->steal_write_sources ();
540 Track::reset_write_sources (bool r, bool force)
542 _diskstream->reset_write_sources (r, force);
546 Track::playback_buffer_load () const
548 return _diskstream->playback_buffer_load ();
552 Track::capture_buffer_load () const
554 return _diskstream->capture_buffer_load ();
560 return _diskstream->do_refill ();
564 Track::do_flush (RunContext c, bool force)
566 return _diskstream->do_flush (c, force);
570 Track::set_pending_overwrite (bool o)
572 _diskstream->set_pending_overwrite (o);
576 Track::seek (framepos_t p, bool complete_refill)
578 return _diskstream->seek (p, complete_refill);
582 Track::hidden () const
584 return _diskstream->hidden ();
588 Track::can_internal_playback_seek (framecnt_t p)
590 return _diskstream->can_internal_playback_seek (p);
594 Track::internal_playback_seek (framecnt_t p)
596 return _diskstream->internal_playback_seek (p);
600 Track::non_realtime_input_change ()
602 _diskstream->non_realtime_input_change ();
606 Track::non_realtime_locate (framepos_t p)
608 _diskstream->non_realtime_locate (p);
612 Track::non_realtime_set_speed ()
614 _diskstream->non_realtime_set_speed ();
618 Track::overwrite_existing_buffers ()
620 return _diskstream->overwrite_existing_buffers ();
624 Track::get_captured_frames (uint32_t n) const
626 return _diskstream->get_captured_frames (n);
630 Track::set_loop (Location* l)
632 return _diskstream->set_loop (l);
636 Track::transport_looped (framepos_t p)
638 _diskstream->transport_looped (p);
642 Track::realtime_set_speed (double s, bool g)
644 return _diskstream->realtime_set_speed (s, g);
648 Track::transport_stopped_wallclock (struct tm & n, time_t t, bool g)
650 _diskstream->transport_stopped_wallclock (n, t, g);
654 Track::pending_overwrite () const
656 return _diskstream->pending_overwrite ();
660 Track::speed () const
662 return _diskstream->speed ();
666 Track::prepare_to_stop (framepos_t p)
668 _diskstream->prepare_to_stop (p);
672 Track::set_slaved (bool s)
674 _diskstream->set_slaved (s);
680 return _diskstream->n_channels ();
684 Track::get_capture_start_frame (uint32_t n) const
686 return _diskstream->get_capture_start_frame (n);
690 Track::alignment_style () const
692 return _diskstream->alignment_style ();
696 Track::alignment_choice () const
698 return _diskstream->alignment_choice ();
702 Track::current_capture_start () const
704 return _diskstream->current_capture_start ();
708 Track::current_capture_end () const
710 return _diskstream->current_capture_end ();
714 Track::playlist_modified ()
716 _diskstream->playlist_modified ();
720 Track::use_playlist (boost::shared_ptr<Playlist> p)
722 int ret = _diskstream->use_playlist (p);
724 p->set_orig_track_id (id());
730 Track::use_copy_playlist ()
732 int ret = _diskstream->use_copy_playlist ();
735 _diskstream->playlist()->set_orig_track_id (id());
742 Track::use_new_playlist ()
744 int ret = _diskstream->use_new_playlist ();
747 _diskstream->playlist()->set_orig_track_id (id());
754 Track::set_align_style (AlignStyle s, bool force)
756 _diskstream->set_align_style (s, force);
760 Track::set_align_choice (AlignChoice s, bool force)
762 _diskstream->set_align_choice (s, force);
766 Track::using_diskstream_id (PBD::ID id) const
768 return (id == _diskstream->id ());
772 Track::set_block_size (pframes_t n)
774 Route::set_block_size (n);
775 _diskstream->set_block_size (n);
779 Track::adjust_playback_buffering ()
782 _diskstream->adjust_playback_buffering ();
787 Track::adjust_capture_buffering ()
790 _diskstream->adjust_capture_buffering ();
795 Track::monitoring_state () const
797 /* Explicit requests */
799 if (_monitoring & MonitorInput) {
800 return MonitoringInput;
803 if (_monitoring & MonitorDisk) {
804 return MonitoringDisk;
807 /* This is an implementation of the truth table in doc/monitor_modes.pdf;
808 I don't think it's ever going to be too pretty too look at.
811 bool const roll = _session.transport_rolling ();
812 bool const track_rec = _diskstream->record_enabled ();
813 bool const session_rec = _session.get_record_enabled ();
814 bool const auto_input = _session.config.get_auto_input ();
815 bool const software_monitor = Config->get_monitoring_model() == SoftwareMonitoring;
816 bool const tape_machine_mode = Config->get_tape_machine_mode ();
820 if (!session_rec && roll && auto_input) {
821 return MonitoringDisk;
823 return software_monitor ? MonitoringInput : MonitoringSilence;
828 if (tape_machine_mode) {
830 return MonitoringDisk;
834 if (!roll && auto_input) {
835 return software_monitor ? MonitoringInput : MonitoringSilence;
837 return MonitoringDisk;
844 return MonitoringSilence;
848 Track::maybe_declick (BufferSet& bufs, framecnt_t nframes, int declick)
850 /* never declick if there is an internal generator - we just want it to
851 keep generating sound without interruption.
853 ditto if we are monitoring inputs.
856 if (_have_internal_generator || monitoring_choice() == MonitorInput) {
861 declick = _pending_declick;
865 Amp::declick (bufs, nframes, declick);
870 Track::check_initial_delay (framecnt_t nframes, framepos_t& transport_frame)
872 if (_roll_delay > nframes) {
874 _roll_delay -= nframes;
875 silence_unlocked (nframes);
876 /* transport frame is not legal for caller to use */
879 } else if (_roll_delay > 0) {
881 nframes -= _roll_delay;
882 silence_unlocked (_roll_delay);
883 transport_frame += _roll_delay;
885 /* shuffle all the port buffers for things that lead "out" of this Route
886 to reflect that we just wrote _roll_delay frames of silence.
889 Glib::RWLock::ReaderLock lm (_processor_lock);
890 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
891 boost::shared_ptr<IOProcessor> iop = boost::dynamic_pointer_cast<IOProcessor> (*i);
893 iop->increment_port_buffer_offset (_roll_delay);
896 _output->increment_port_buffer_offset (_roll_delay);
906 Track::set_monitoring (MonitorChoice mc)
908 if (mc != _monitoring) {
911 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
912 (*i)->monitoring_changed ();
915 MonitoringChanged (); /* EMIT SIGNAL */
920 Track::parameter_changed (string p)
922 if (p != "do-not-record-plugins") {
926 if (record_enabled ()) {
927 if (Config->get_do_not_record_plugins ()) {
928 deactivate_visible_processors ();
930 activate_deactivated_processors ();
936 Track::metering_state () const
938 return _diskstream->record_enabled() ? MeteringInput : MeteringRoute;