2 Copyright (C) 2000 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.
21 #include "libardour-config.h"
29 #include <boost/algorithm/string.hpp>
31 #include "pbd/xml++.h"
32 #include "pbd/enumwriter.h"
33 #include "pbd/memento_command.h"
34 #include "pbd/stacktrace.h"
35 #include "pbd/convert.h"
36 #include "pbd/unwind.h"
38 #include "ardour/amp.h"
39 #include "ardour/audio_buffer.h"
40 #include "ardour/audio_track.h"
41 #include "ardour/audio_port.h"
42 #include "ardour/audioengine.h"
43 #include "ardour/boost_debug.h"
44 #include "ardour/buffer.h"
45 #include "ardour/buffer_set.h"
46 #include "ardour/capturing_processor.h"
47 #include "ardour/debug.h"
48 #include "ardour/delivery.h"
49 #include "ardour/gain_control.h"
50 #include "ardour/internal_return.h"
51 #include "ardour/internal_send.h"
52 #include "ardour/meter.h"
53 #include "ardour/delayline.h"
54 #include "ardour/midi_buffer.h"
55 #include "ardour/midi_port.h"
56 #include "ardour/monitor_processor.h"
57 #include "ardour/pannable.h"
58 #include "ardour/panner.h"
59 #include "ardour/panner_shell.h"
60 #include "ardour/parameter_descriptor.h"
61 #include "ardour/phase_control.h"
62 #include "ardour/plugin_insert.h"
63 #include "ardour/port.h"
64 #include "ardour/port_insert.h"
65 #include "ardour/processor.h"
66 #include "ardour/profile.h"
67 #include "ardour/route.h"
68 #include "ardour/route_group.h"
69 #include "ardour/send.h"
70 #include "ardour/session.h"
71 #include "ardour/solo_control.h"
72 #include "ardour/solo_isolate_control.h"
73 #include "ardour/unknown_processor.h"
74 #include "ardour/utils.h"
75 #include "ardour/vca.h"
80 using namespace ARDOUR;
83 PBD::Signal3<int,boost::shared_ptr<Route>, boost::shared_ptr<PluginInsert>, Route::PluginSetupOptions > Route::PluginSetup;
85 /** Base class for all routable/mixable objects (tracks and busses) */
86 Route::Route (Session& sess, string name, PresentationInfo::Flag flag, DataType default_type)
87 : Stripable (sess, name, PresentationInfo (flag))
88 , GraphNode (sess._process_graph)
89 , Muteable (sess, name)
93 , _signal_latency_at_amp_position (0)
94 , _signal_latency_at_trim_position (0)
97 , _pending_process_reorder (0)
98 , _pending_signals (0)
99 , _pending_declick (true)
100 , _meter_point (MeterPostFader)
101 , _pending_meter_point (MeterPostFader)
102 , _meter_type (MeterPeak)
103 , _denormal_protection (false)
106 , _declickable (false)
107 , _have_internal_generator (false)
108 , _default_type (default_type)
110 , _in_configure_processors (false)
111 , _initial_io_setup (false)
112 , _in_sidechain_setup (false)
114 , _custom_meter_position_noted (false)
117 processor_max_streams.reset();
123 /* set default meter type */
125 _meter_type = Config->get_meter_type_master ();
127 else if (dynamic_cast<Track*>(this)) {
128 _meter_type = Config->get_meter_type_track ();
130 _meter_type = Config->get_meter_type_bus ();
133 /* add standard controls */
135 _gain_control.reset (new GainControl (_session, GainAutomation));
136 add_control (_gain_control);
138 _trim_control.reset (new GainControl (_session, TrimAutomation));
139 add_control (_trim_control);
141 _solo_control.reset (new SoloControl (_session, X_("solo"), *this, *this));
142 add_control (_solo_control);
143 _solo_control->Changed.connect_same_thread (*this, boost::bind (&Route::solo_control_changed, this, _1, _2));
145 _mute_control.reset (new MuteControl (_session, X_("mute"), *this));
146 add_control (_mute_control);
148 _phase_control.reset (new PhaseControl (_session, X_("phase")));
149 add_control (_phase_control);
151 _solo_isolate_control.reset (new SoloIsolateControl (_session, X_("solo-iso"), *this, *this));
152 add_control (_solo_isolate_control);
154 _solo_safe_control.reset (new SoloSafeControl (_session, X_("solo-safe")));
155 add_control (_solo_safe_control);
159 if (!(_presentation_info.flags() & PresentationInfo::MonitorOut)) {
160 _pannable.reset (new Pannable (_session));
163 /* input and output objects */
165 _input.reset (new IO (_session, _name, IO::Input, _default_type));
166 _output.reset (new IO (_session, _name, IO::Output, _default_type));
168 _input->changed.connect_same_thread (*this, boost::bind (&Route::input_change_handler, this, _1, _2));
169 _input->PortCountChanging.connect_same_thread (*this, boost::bind (&Route::input_port_count_changing, this, _1));
171 _output->changed.connect_same_thread (*this, boost::bind (&Route::output_change_handler, this, _1, _2));
172 _output->PortCountChanging.connect_same_thread (*this, boost::bind (&Route::output_port_count_changing, this, _1));
174 /* add the amp/fader processor.
175 * it should be the first processor to be added on every route.
178 _amp.reset (new Amp (_session, X_("Fader"), _gain_control, true));
179 add_processor (_amp, PostFader);
182 _amp->set_display_name (_("Monitor"));
185 #if 0 // not used - just yet
186 if (!is_master() && !is_monitor() && !is_auditioner()) {
187 _delayline.reset (new DelayLine (_session, _name));
188 add_processor (_delayline, PreFader);
194 _trim.reset (new Amp (_session, X_("Trim"), _trim_control, false));
195 _trim->set_display_to_user (false);
197 if (dynamic_cast<AudioTrack*>(this)) {
198 /* we can't do this in the AudioTrack's constructor
199 * because _trim does not exit then
203 else if (!dynamic_cast<Track*>(this) && ! ( is_monitor() || is_auditioner() )) {
208 /* create standard processors: meter, main outs, monitor out;
209 they will be added to _processors by setup_invisible_processors ()
212 _meter.reset (new PeakMeter (_session, _name));
213 _meter->set_owner (this);
214 _meter->set_display_to_user (false);
217 _main_outs.reset (new Delivery (_session, _output, _pannable, _mute_master, _name, Delivery::Main));
218 _main_outs->activate ();
221 /* where we listen to tracks */
222 _intreturn.reset (new InternalReturn (_session));
223 _intreturn->activate ();
225 /* the thing that provides proper control over a control/monitor/listen bus
226 (such as per-channel cut, dim, solo, invert, etc).
228 _monitor_control.reset (new MonitorProcessor (_session));
229 _monitor_control->activate ();
232 /* now that we have _meter, its safe to connect to this */
235 Glib::Threads::Mutex::Lock lx (AudioEngine::instance()->process_lock ());
236 configure_processors (0);
244 DEBUG_TRACE (DEBUG::Destruction, string_compose ("route %1 destructor\n", _name));
246 /* do this early so that we don't get incoming signals as we are going through destruction
251 /* don't use clear_processors here, as it depends on the session which may
252 be half-destroyed by now
255 Glib::Threads::RWLock::WriterLock lm (_processor_lock);
256 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
257 (*i)->drop_references ();
260 _processors.clear ();
264 Route::ensure_track_or_route_name(string name, Session &session)
266 string newname = name;
268 while (!session.io_name_is_legal (newname)) {
269 newname = bump_name_once (newname, ' ');
276 Route::set_trim (gain_t val, Controllable::GroupControlDisposition /* group override */)
278 // TODO route group, see set_gain()
279 // _trim_control->route_set_value (val);
283 Route::maybe_declick (BufferSet&, framecnt_t, int)
285 /* this is the "bus" implementation and they never declick.
290 /** Process this route for one (sub) cycle (process thread)
292 * @param bufs Scratch buffers to use for the signal path
293 * @param start_frame Initial transport frame
294 * @param end_frame Final transport frame
295 * @param nframes Number of frames to output (to ports)
297 * Note that (end_frame - start_frame) may not be equal to nframes when the
298 * transport speed isn't 1.0 (eg varispeed).
301 Route::process_output_buffers (BufferSet& bufs,
302 framepos_t start_frame, framepos_t end_frame, pframes_t nframes,
303 int declick, bool gain_automation_ok)
305 /* Caller must hold process lock */
306 assert (!AudioEngine::instance()->process_lock().trylock());
308 Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
310 // can this actually happen? functions calling process_output_buffers()
311 // already take a reader-lock.
312 bufs.silence (nframes, 0);
316 _mute_control->automation_run (start_frame, nframes);
318 /* figure out if we're going to use gain automation */
319 if (gain_automation_ok) {
320 _amp->set_gain_automation_buffer (_session.gain_automation_buffer ());
321 _amp->setup_gain_automation (
322 start_frame + _signal_latency_at_amp_position,
323 end_frame + _signal_latency_at_amp_position,
326 _trim->set_gain_automation_buffer (_session.trim_automation_buffer ());
327 _trim->setup_gain_automation (
328 start_frame + _signal_latency_at_trim_position,
329 end_frame + _signal_latency_at_trim_position,
332 _amp->apply_gain_automation (false);
333 _trim->apply_gain_automation (false);
336 /* Tell main outs what to do about monitoring. We do this so that
337 on a transition between monitoring states we get a de-clicking gain
338 change in the _main_outs delivery, if config.get_use_monitor_fades()
341 We override this in the case where we have an internal generator.
343 bool silence = _have_internal_generator ? false : (monitoring_state () == MonitoringSilence);
345 _main_outs->no_outs_cuz_we_no_monitor (silence);
347 /* -------------------------------------------------------------------------------------------
348 GLOBAL DECLICK (for transport changes etc.)
349 ----------------------------------------------------------------------------------------- */
351 maybe_declick (bufs, nframes, declick);
352 _pending_declick = 0;
354 /* -------------------------------------------------------------------------------------------
355 DENORMAL CONTROL/PHASE INVERT
356 ----------------------------------------------------------------------------------------- */
358 if (!_phase_control->none()) {
362 if (_denormal_protection || Config->get_denormal_protection()) {
364 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i, ++chn) {
365 Sample* const sp = i->data();
367 if (_phase_control->inverted (chn)) {
368 for (pframes_t nx = 0; nx < nframes; ++nx) {
373 for (pframes_t nx = 0; nx < nframes; ++nx) {
381 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i, ++chn) {
382 Sample* const sp = i->data();
384 if (_phase_control->inverted (chn)) {
385 for (pframes_t nx = 0; nx < nframes; ++nx) {
394 if (_denormal_protection || Config->get_denormal_protection()) {
396 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
397 Sample* const sp = i->data();
398 for (pframes_t nx = 0; nx < nframes; ++nx) {
406 /* -------------------------------------------------------------------------------------------
408 ----------------------------------------------------------------------------------------- */
410 /* set this to be true if the meter will already have been ::run() earlier */
411 bool const meter_already_run = metering_state() == MeteringInput;
413 framecnt_t latency = 0;
414 const double speed = _session.transport_speed ();
416 for (ProcessorList::const_iterator i = _processors.begin(); i != _processors.end(); ++i) {
418 if (meter_already_run && boost::dynamic_pointer_cast<PeakMeter> (*i)) {
419 /* don't ::run() the meter, otherwise it will have its previous peak corrupted */
424 /* if it has any inputs, make sure they match */
425 if (boost::dynamic_pointer_cast<UnknownProcessor> (*i) == 0 && (*i)->input_streams() != ChanCount::ZERO) {
426 if (bufs.count() != (*i)->input_streams()) {
428 DEBUG::Processors, string_compose (
429 "input port mismatch %1 bufs = %2 input for %3 = %4\n",
430 _name, bufs.count(), (*i)->name(), (*i)->input_streams()
437 /* should we NOT run plugins here if the route is inactive?
438 do we catch route != active somewhere higher?
441 if (boost::dynamic_pointer_cast<Send>(*i) != 0) {
442 boost::dynamic_pointer_cast<Send>(*i)->set_delay_in(_signal_latency - latency);
444 if (boost::dynamic_pointer_cast<PluginInsert>(*i) != 0) {
445 const framecnt_t longest_session_latency = _initial_delay + _signal_latency;
446 boost::dynamic_pointer_cast<PluginInsert>(*i)->set_sidechain_latency (
447 _initial_delay + latency, longest_session_latency - latency);
450 (*i)->run (bufs, start_frame - latency, end_frame - latency, speed, nframes, *i != _processors.back());
451 bufs.set_count ((*i)->output_streams());
453 if ((*i)->active ()) {
454 latency += (*i)->signal_latency ();
460 Route::bounce_process (BufferSet& buffers, framepos_t start, framecnt_t nframes,
461 boost::shared_ptr<Processor> endpoint,
462 bool include_endpoint, bool for_export, bool for_freeze)
464 /* If no processing is required, there's no need to go any further. */
465 if (!endpoint && !include_endpoint) {
469 framecnt_t latency = bounce_get_latency(_amp, false, for_export, for_freeze);
470 _amp->set_gain_automation_buffer (_session.gain_automation_buffer ());
471 _amp->setup_gain_automation (start - latency, start - latency + nframes, nframes);
473 /* trim is always at the top, for bounce no latency compensation is needed */
474 _trim->set_gain_automation_buffer (_session.trim_automation_buffer ());
475 _trim->setup_gain_automation (start, start + nframes, nframes);
478 const double speed = _session.transport_speed ();
479 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
481 if (!include_endpoint && (*i) == endpoint) {
485 /* if we're *not* exporting, stop processing if we come across a routing processor. */
486 if (!for_export && boost::dynamic_pointer_cast<PortInsert>(*i)) {
489 if (!for_export && for_freeze && (*i)->does_routing() && (*i)->active()) {
493 /* special case the panner (export outputs)
494 * Ideally we'd only run the panner, not the delivery itself...
495 * but panners need separate input/output buffers and some context
496 * (panshell, panner type, etc). AFAICT there is no ill side effect
497 * of re-using the main delivery when freewheeling/exporting a region.
499 if ((*i) == _main_outs) {
500 assert ((*i)->does_routing());
501 (*i)->run (buffers, start - latency, start - latency + nframes, speed, nframes, true);
502 buffers.set_count ((*i)->output_streams());
505 /* don't run any processors that do routing.
506 * Also don't bother with metering.
508 if (!(*i)->does_routing() && !boost::dynamic_pointer_cast<PeakMeter>(*i)) {
509 (*i)->run (buffers, start - latency, start - latency + nframes, 1.0, nframes, true);
510 buffers.set_count ((*i)->output_streams());
511 latency += (*i)->signal_latency ();
514 if ((*i) == endpoint) {
521 Route::bounce_get_latency (boost::shared_ptr<Processor> endpoint,
522 bool include_endpoint, bool for_export, bool for_freeze) const
524 framecnt_t latency = 0;
525 if (!endpoint && !include_endpoint) {
529 for (ProcessorList::const_iterator i = _processors.begin(); i != _processors.end(); ++i) {
530 if (!include_endpoint && (*i) == endpoint) {
533 if (!for_export && boost::dynamic_pointer_cast<PortInsert>(*i)) {
536 if (!for_export && for_freeze && (*i)->does_routing() && (*i)->active()) {
539 if (!(*i)->does_routing() && !boost::dynamic_pointer_cast<PeakMeter>(*i)) {
540 latency += (*i)->signal_latency ();
542 if ((*i) == endpoint) {
550 Route::bounce_get_output_streams (ChanCount &cc, boost::shared_ptr<Processor> endpoint,
551 bool include_endpoint, bool for_export, bool for_freeze) const
553 if (!endpoint && !include_endpoint) {
557 for (ProcessorList::const_iterator i = _processors.begin(); i != _processors.end(); ++i) {
558 if (!include_endpoint && (*i) == endpoint) {
561 if (!for_export && boost::dynamic_pointer_cast<PortInsert>(*i)) {
564 if (!for_export && for_freeze && (*i)->does_routing() && (*i)->active()) {
567 if (!(*i)->does_routing() && !boost::dynamic_pointer_cast<PeakMeter>(*i)) {
568 cc = (*i)->output_streams();
570 if ((*i) == endpoint) {
578 Route::n_process_buffers ()
580 return max (_input->n_ports(), processor_max_streams);
584 Route::monitor_run (framepos_t start_frame, framepos_t end_frame, pframes_t nframes, int declick)
586 assert (is_monitor());
587 BufferSet& bufs (_session.get_route_buffers (n_process_buffers()));
588 fill_buffers_with_input (bufs, _input, nframes);
589 passthru (bufs, start_frame, end_frame, nframes, declick);
593 Route::passthru (BufferSet& bufs, framepos_t start_frame, framepos_t end_frame, pframes_t nframes, int declick)
597 if (is_monitor() && _session.listening() && !_session.is_auditioning()) {
599 /* control/monitor bus ignores input ports when something is
600 feeding the listen "stream". data will "arrive" into the
601 route from the intreturn processor element.
604 bufs.silence (nframes, 0);
607 write_out_of_band_data (bufs, start_frame, end_frame, nframes);
608 process_output_buffers (bufs, start_frame, end_frame, nframes, declick, true);
612 Route::passthru_silence (framepos_t start_frame, framepos_t end_frame, pframes_t nframes, int declick)
614 BufferSet& bufs (_session.get_route_buffers (n_process_buffers(), true));
616 bufs.set_count (_input->n_ports());
617 write_out_of_band_data (bufs, start_frame, end_frame, nframes);
618 process_output_buffers (bufs, start_frame, end_frame, nframes, declick, false);
622 Route::set_listen (bool yn)
626 _monitor_send->activate ();
628 _monitor_send->deactivate ();
634 Route::solo_control_changed (bool, Controllable::GroupControlDisposition)
636 /* nothing to do if we're not using AFL/PFL. But if we are, we need
637 to alter the active state of the monitor send.
640 if (Config->get_solo_control_is_listen_control ()) {
641 set_listen (_solo_control->self_soloed() || _solo_control->get_masters_value());
646 Route::push_solo_isolate_upstream (int32_t delta)
648 /* forward propagate solo-isolate status to everything fed by this route, but not those via sends only */
650 boost::shared_ptr<RouteList> routes = _session.get_routes ();
651 for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
653 if ((*i).get() == this || !(*i)->can_solo()) {
658 bool does_feed = feeds (*i, &sends_only);
660 if (does_feed && !sends_only) {
661 (*i)->solo_isolate_control()->mod_solo_isolated_by_upstream (delta);
667 Route::push_solo_upstream (int delta)
669 DEBUG_TRACE (DEBUG::Solo, string_compose("\t ... INVERT push from %1\n", _name));
670 for (FedBy::iterator i = _fed_by.begin(); i != _fed_by.end(); ++i) {
674 boost::shared_ptr<Route> sr (i->r.lock());
676 sr->solo_control()->mod_solo_by_others_downstream (-delta);
683 dump_processors(const string& name, const list<boost::shared_ptr<Processor> >& procs)
685 cerr << name << " {" << endl;
686 for (list<boost::shared_ptr<Processor> >::const_iterator p = procs.begin();
687 p != procs.end(); ++p) {
688 cerr << "\t" << (*p)->name() << " ID = " << (*p)->id() << " @ " << (*p) << endl;
694 /** Supposing that we want to insert a Processor at a given Placement, return
695 * the processor to add the new one before (or 0 to add at the end).
697 boost::shared_ptr<Processor>
698 Route::before_processor_for_placement (Placement p)
700 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
702 ProcessorList::iterator loc;
705 /* generic pre-fader: insert immediately before the amp */
706 loc = find (_processors.begin(), _processors.end(), _amp);
708 /* generic post-fader: insert right before the main outs */
709 loc = find (_processors.begin(), _processors.end(), _main_outs);
712 return loc != _processors.end() ? *loc : boost::shared_ptr<Processor> ();
715 /** Supposing that we want to insert a Processor at a given index, return
716 * the processor to add the new one before (or 0 to add at the end).
718 boost::shared_ptr<Processor>
719 Route::before_processor_for_index (int index)
722 return boost::shared_ptr<Processor> ();
725 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
727 ProcessorList::iterator i = _processors.begin ();
729 while (i != _processors.end() && j < index) {
730 if ((*i)->display_to_user()) {
737 return (i != _processors.end() ? *i : boost::shared_ptr<Processor> ());
740 /** Add a processor either pre- or post-fader
741 * @return 0 on success, non-0 on failure.
744 Route::add_processor (boost::shared_ptr<Processor> processor, Placement placement, ProcessorStreams* err, bool activation_allowed)
746 return add_processor (processor, before_processor_for_placement (placement), err, activation_allowed);
750 /** Add a processor to a route such that it ends up with a given index into the visible processors.
751 * @param index Index to add the processor at, or -1 to add at the end of the list.
752 * @return 0 on success, non-0 on failure.
755 Route::add_processor_by_index (boost::shared_ptr<Processor> processor, int index, ProcessorStreams* err, bool activation_allowed)
757 return add_processor (processor, before_processor_for_index (index), err, activation_allowed);
760 /** Add a processor to the route.
761 * @param before An existing processor in the list, or 0; the new processor will be inserted immediately before it (or at the end).
762 * @return 0 on success, non-0 on failure.
765 Route::add_processor (boost::shared_ptr<Processor> processor, boost::shared_ptr<Processor> before, ProcessorStreams* err, bool activation_allowed)
767 assert (processor != _meter);
768 assert (processor != _main_outs);
770 DEBUG_TRACE (DEBUG::Processors, string_compose (
771 "%1 adding processor %2\n", name(), processor->name()));
775 pl.push_back (processor);
776 int rv = add_processors (pl, before, err);
782 if (activation_allowed && (!_session.get_bypass_all_loaded_plugins () || !processor->display_to_user ())) {
783 processor->activate ();
790 Route::processor_selfdestruct (boost::weak_ptr<Processor> wp)
792 /* We cannot destruct the processor here (usually RT-thread
793 * with various locks held - in case of sends also io_locks).
794 * Queue for deletion in low-priority thread.
796 Glib::Threads::Mutex::Lock lx (selfdestruct_lock);
797 selfdestruct_sequence.push_back (wp);
801 Route::add_processor_from_xml_2X (const XMLNode& node, int version)
803 XMLProperty const * prop;
806 boost::shared_ptr<Processor> processor;
808 /* bit of a hack: get the `placement' property from the <Redirect> tag here
809 so that we can add the processor in the right place (pre/post-fader)
812 XMLNodeList const & children = node.children ();
813 XMLNodeList::const_iterator i = children.begin ();
815 while (i != children.end() && (*i)->name() != X_("Redirect")) {
819 Placement placement = PreFader;
821 if (i != children.end()) {
822 if ((prop = (*i)->property (X_("placement"))) != 0) {
823 placement = Placement (string_2_enum (prop->value(), placement));
827 if (node.name() == "Insert") {
829 if ((prop = node.property ("type")) != 0) {
831 if (prop->value() == "ladspa" || prop->value() == "Ladspa" ||
832 prop->value() == "lv2" ||
833 prop->value() == "windows-vst" ||
834 prop->value() == "lxvst" ||
835 prop->value() == "audiounit") {
837 if (_session.get_disable_all_loaded_plugins ()) {
838 processor.reset (new UnknownProcessor (_session, node));
840 processor.reset (new PluginInsert (_session));
841 processor->set_owner (this);
846 processor.reset (new PortInsert (_session, _pannable, _mute_master));
851 } else if (node.name() == "Send") {
853 boost::shared_ptr<Pannable> sendpan (new Pannable (_session));
854 processor.reset (new Send (_session, sendpan, _mute_master));
858 error << string_compose(_("unknown Processor type \"%1\"; ignored"), node.name()) << endmsg;
862 if (processor->set_state (node, version)) {
866 //A2 uses the "active" flag in the toplevel redirect node, not in the child plugin/IO
867 if (i != children.end()) {
868 if ((prop = (*i)->property (X_("active"))) != 0) {
869 if ( string_is_affirmative (prop->value()) && (!_session.get_bypass_all_loaded_plugins () || !processor->display_to_user () ) )
870 processor->activate();
872 processor->deactivate();
876 return (add_processor (processor, placement, 0, false) == 0);
879 catch (failed_constructor &err) {
880 warning << _("processor could not be created. Ignored.") << endmsg;
886 inline Route::PluginSetupOptions operator|= (Route::PluginSetupOptions& a, const Route::PluginSetupOptions& b) {
887 return a = static_cast<Route::PluginSetupOptions> (static_cast <int>(a) | static_cast<int> (b));
890 inline Route::PluginSetupOptions operator&= (Route::PluginSetupOptions& a, const Route::PluginSetupOptions& b) {
891 return a = static_cast<Route::PluginSetupOptions> (static_cast <int>(a) & static_cast<int> (b));
895 Route::add_processors (const ProcessorList& others, boost::shared_ptr<Processor> before, ProcessorStreams* err)
897 ProcessorList::iterator loc;
900 loc = find(_processors.begin(), _processors.end(), before);
901 if (loc == _processors.end ()) {
905 /* nothing specified - at end */
906 loc = _processors.end ();
909 if (!AudioEngine::instance()->connected()) {
913 if (others.empty()) {
917 ProcessorList to_skip;
919 // check if there's an instrument to replace or configure
920 for (ProcessorList::const_iterator i = others.begin(); i != others.end(); ++i) {
921 boost::shared_ptr<PluginInsert> pi;
922 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(*i)) == 0) {
925 if (!pi->plugin ()->get_info ()->is_instrument ()) {
928 boost::shared_ptr<Processor> instrument = the_instrument ();
929 ChanCount in (DataType::MIDI, 1);
930 ChanCount out (DataType::AUDIO, 2); // XXX route's out?!
932 PluginSetupOptions flags = None;
935 in = instrument->input_streams ();
936 out = instrument->output_streams ();
938 if (pi->has_output_presets (in, out)) {
942 pi->set_strict_io (_strict_io);
944 PluginSetupOptions mask = None;
945 if (Config->get_ask_replace_instrument ()) {
948 if (Config->get_ask_setup_instrument ()) {
955 boost::optional<int> rv = PluginSetup (shared_from_this (), pi, flags); /* EMIT SIGNAL */
956 switch (rv.get_value_or (0)) {
958 to_skip.push_back (*i); // don't add this one;
961 replace_processor (instrument, *i, err);
962 to_skip.push_back (*i);
971 Glib::Threads::Mutex::Lock lx (AudioEngine::instance()->process_lock ());
972 Glib::Threads::RWLock::WriterLock lm (_processor_lock);
973 ProcessorState pstate (this);
975 for (ProcessorList::const_iterator i = others.begin(); i != others.end(); ++i) {
980 ProcessorList::iterator check = find (to_skip.begin(), to_skip.end(), *i);
981 if (check != to_skip.end()) {
985 boost::shared_ptr<PluginInsert> pi;
987 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(*i)) != 0) {
988 pi->set_strict_io (_strict_io);
992 /* Ensure that only one amp is in the list at any time */
993 ProcessorList::iterator check = find (_processors.begin(), _processors.end(), *i);
994 if (check != _processors.end()) {
995 if (before == _amp) {
996 /* Already in position; all is well */
999 _processors.erase (check);
1004 assert (find (_processors.begin(), _processors.end(), *i) == _processors.end ());
1006 _processors.insert (loc, *i);
1007 (*i)->set_owner (this);
1010 if (configure_processors_unlocked (err, &lm)) {
1012 configure_processors_unlocked (0, &lm); // it worked before we tried to add it ...
1017 if ((*i)->active()) {
1018 // why? emit ActiveChanged() ??
1022 (*i)->ActiveChanged.connect_same_thread (*this, boost::bind (&Session::update_latency_compensation, &_session, false));
1024 boost::shared_ptr<Send> send;
1025 if ((send = boost::dynamic_pointer_cast<Send> (*i))) {
1026 send->SelfDestruct.connect_same_thread (*this,
1027 boost::bind (&Route::processor_selfdestruct, this, boost::weak_ptr<Processor> (*i)));
1031 for (ProcessorList::const_iterator i = _processors.begin(); i != _processors.end(); ++i) {
1032 boost::shared_ptr<PluginInsert> pi;
1034 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(*i)) != 0) {
1035 if (pi->has_no_inputs ()) {
1036 _have_internal_generator = true;
1042 _output->set_user_latency (0);
1045 reset_instrument_info ();
1046 processors_changed (RouteProcessorChange ()); /* EMIT SIGNAL */
1047 set_processor_positions ();
1053 Route::placement_range(Placement p, ProcessorList::iterator& start, ProcessorList::iterator& end)
1055 if (p == PreFader) {
1056 start = _processors.begin();
1057 end = find(_processors.begin(), _processors.end(), _amp);
1059 start = find(_processors.begin(), _processors.end(), _amp);
1061 end = _processors.end();
1065 /** Turn off all processors with a given placement
1066 * @param p Placement of processors to disable
1069 Route::disable_processors (Placement p)
1071 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
1073 ProcessorList::iterator start, end;
1074 placement_range(p, start, end);
1076 for (ProcessorList::iterator i = start; i != end; ++i) {
1077 (*i)->enable (false);
1080 _session.set_dirty ();
1083 /** Turn off all redirects
1086 Route::disable_processors ()
1088 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
1090 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1091 (*i)->enable (false);
1094 _session.set_dirty ();
1097 /** Turn off all redirects with a given placement
1098 * @param p Placement of redirects to disable
1101 Route::disable_plugins (Placement p)
1103 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
1105 ProcessorList::iterator start, end;
1106 placement_range(p, start, end);
1108 for (ProcessorList::iterator i = start; i != end; ++i) {
1109 if (boost::dynamic_pointer_cast<PluginInsert> (*i)) {
1110 (*i)->enable (false);
1114 _session.set_dirty ();
1117 /** Turn off all plugins
1120 Route::disable_plugins ()
1122 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
1124 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1125 if (boost::dynamic_pointer_cast<PluginInsert> (*i)) {
1126 (*i)->enable (false);
1130 _session.set_dirty ();
1135 Route::ab_plugins (bool forward)
1137 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
1141 /* forward = turn off all active redirects, and mark them so that the next time
1142 we go the other way, we will revert them
1145 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1146 if (!boost::dynamic_pointer_cast<PluginInsert> (*i)) {
1150 if ((*i)->enabled ()) {
1151 (*i)->enable (false);
1152 (*i)->set_next_ab_is_active (true);
1154 (*i)->set_next_ab_is_active (false);
1160 /* backward = if the redirect was marked to go active on the next ab, do so */
1162 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1164 if (!boost::dynamic_pointer_cast<PluginInsert> (*i)) {
1168 (*i)->enable ((*i)->get_next_ab_is_active ());
1172 _session.set_dirty ();
1176 /** Remove processors with a given placement.
1177 * @param p Placement of processors to remove.
1180 Route::clear_processors (Placement p)
1182 if (!_session.engine().connected()) {
1186 bool already_deleting = _session.deletion_in_progress();
1187 if (!already_deleting) {
1188 _session.set_deletion_in_progress();
1192 Glib::Threads::Mutex::Lock lx (AudioEngine::instance()->process_lock ());
1193 Glib::Threads::RWLock::WriterLock lm (_processor_lock);
1194 ProcessorList new_list;
1195 ProcessorStreams err;
1196 bool seen_amp = false;
1198 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1204 if ((*i) == _amp || (*i) == _meter || (*i) == _main_outs || (*i) == _delayline || (*i) == _trim) {
1206 /* you can't remove these */
1208 new_list.push_back (*i);
1215 new_list.push_back (*i);
1218 (*i)->drop_references ();
1226 (*i)->drop_references ();
1229 new_list.push_back (*i);
1236 _processors = new_list;
1237 configure_processors_unlocked (&err, &lm); // this can't fail
1240 processor_max_streams.reset();
1241 _have_internal_generator = false;
1242 processors_changed (RouteProcessorChange ()); /* EMIT SIGNAL */
1243 set_processor_positions ();
1245 reset_instrument_info ();
1247 if (!already_deleting) {
1248 _session.clear_deletion_in_progress();
1253 Route::remove_processor (boost::shared_ptr<Processor> processor, ProcessorStreams* err, bool need_process_lock)
1255 // TODO once the export point can be configured properly, do something smarter here
1256 if (processor == _capturing_processor) {
1257 Glib::Threads::Mutex::Lock lx (AudioEngine::instance()->process_lock (), Glib::Threads::NOT_LOCK);
1258 if (need_process_lock) {
1262 _capturing_processor.reset();
1264 if (need_process_lock) {
1269 /* these can never be removed */
1271 if (processor == _amp || processor == _meter || processor == _main_outs || processor == _delayline || processor == _trim) {
1275 if (!_session.engine().connected()) {
1279 processor_max_streams.reset();
1282 Glib::Threads::Mutex::Lock lx (AudioEngine::instance()->process_lock (), Glib::Threads::NOT_LOCK);
1283 if (need_process_lock) {
1287 /* Caller must hold process lock */
1288 assert (!AudioEngine::instance()->process_lock().trylock());
1290 Glib::Threads::RWLock::WriterLock lm (_processor_lock); // XXX deadlock after export
1292 ProcessorState pstate (this);
1294 ProcessorList::iterator i;
1295 bool removed = false;
1297 for (i = _processors.begin(); i != _processors.end(); ) {
1298 if (*i == processor) {
1300 /* move along, see failure case for configure_processors()
1301 where we may need to reconfigure the processor.
1304 /* stop redirects that send signals to JACK ports
1305 from causing noise as a result of no longer being
1309 boost::shared_ptr<IOProcessor> iop = boost::dynamic_pointer_cast<IOProcessor> (*i);
1310 boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert>(*i);
1314 iop = pi->sidechain();
1321 i = _processors.erase (i);
1329 _output->set_user_latency (0);
1337 if (configure_processors_unlocked (err, &lm)) {
1339 /* we know this will work, because it worked before :) */
1340 configure_processors_unlocked (0, &lm);
1344 _have_internal_generator = false;
1346 for (i = _processors.begin(); i != _processors.end(); ++i) {
1347 boost::shared_ptr<PluginInsert> pi;
1349 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(*i)) != 0) {
1350 if (pi->has_no_inputs ()) {
1351 _have_internal_generator = true;
1356 if (need_process_lock) {
1361 reset_instrument_info ();
1362 processor->drop_references ();
1363 processors_changed (RouteProcessorChange ()); /* EMIT SIGNAL */
1364 set_processor_positions ();
1370 Route::replace_processor (boost::shared_ptr<Processor> old, boost::shared_ptr<Processor> sub, ProcessorStreams* err)
1372 /* these can never be removed */
1373 if (old == _amp || old == _meter || old == _main_outs || old == _delayline || old == _trim) {
1376 /* and can't be used as substitute, either */
1377 if (sub == _amp || sub == _meter || sub == _main_outs || sub == _delayline || sub == _trim) {
1381 /* I/Os are out, too */
1382 if (boost::dynamic_pointer_cast<IOProcessor> (old) || boost::dynamic_pointer_cast<IOProcessor> (sub)) {
1386 /* this function cannot be used to swap/reorder processors */
1387 if (find (_processors.begin(), _processors.end(), sub) != _processors.end ()) {
1391 if (!AudioEngine::instance()->connected() || !old || !sub) {
1395 /* ensure that sub is not owned by another route */
1396 if (sub->owner ()) {
1401 Glib::Threads::Mutex::Lock lx (AudioEngine::instance()->process_lock ());
1402 Glib::Threads::RWLock::WriterLock lm (_processor_lock);
1403 ProcessorState pstate (this);
1405 assert (find (_processors.begin(), _processors.end(), sub) == _processors.end ());
1407 ProcessorList::iterator i;
1408 bool replaced = false;
1409 bool enable = old->enabled ();
1411 for (i = _processors.begin(); i != _processors.end(); ) {
1413 i = _processors.erase (i);
1414 _processors.insert (i, sub);
1415 sub->set_owner (this);
1428 boost::shared_ptr<PluginInsert> pi;
1429 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(sub)) != 0) {
1430 pi->set_strict_io (true);
1434 if (configure_processors_unlocked (err, &lm)) {
1436 configure_processors_unlocked (0, &lm);
1440 _have_internal_generator = false;
1442 for (i = _processors.begin(); i != _processors.end(); ++i) {
1443 boost::shared_ptr<PluginInsert> pi;
1444 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(*i)) != 0) {
1445 if (pi->has_no_inputs ()) {
1446 _have_internal_generator = true;
1456 sub->ActiveChanged.connect_same_thread (*this, boost::bind (&Session::update_latency_compensation, &_session, false));
1457 _output->set_user_latency (0);
1460 reset_instrument_info ();
1461 old->drop_references ();
1462 processors_changed (RouteProcessorChange ()); /* EMIT SIGNAL */
1463 set_processor_positions ();
1468 Route::remove_processors (const ProcessorList& to_be_deleted, ProcessorStreams* err)
1470 ProcessorList deleted;
1472 if (!_session.engine().connected()) {
1476 processor_max_streams.reset();
1479 Glib::Threads::Mutex::Lock lx (AudioEngine::instance()->process_lock ());
1480 Glib::Threads::RWLock::WriterLock lm (_processor_lock);
1481 ProcessorState pstate (this);
1483 ProcessorList::iterator i;
1484 boost::shared_ptr<Processor> processor;
1486 for (i = _processors.begin(); i != _processors.end(); ) {
1490 /* these can never be removed */
1492 if (processor == _amp || processor == _meter || processor == _main_outs || processor == _delayline || processor == _trim) {
1497 /* see if its in the list of processors to delete */
1499 if (find (to_be_deleted.begin(), to_be_deleted.end(), processor) == to_be_deleted.end()) {
1504 /* stop IOProcessors that send to JACK ports
1505 from causing noise as a result of no longer being
1509 boost::shared_ptr<IOProcessor> iop = boost::dynamic_pointer_cast<IOProcessor>(processor);
1510 boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert>(processor);
1513 iop = pi->sidechain();
1520 deleted.push_back (processor);
1521 i = _processors.erase (i);
1524 if (deleted.empty()) {
1525 /* none of those in the requested list were found */
1529 _output->set_user_latency (0);
1531 if (configure_processors_unlocked (err, &lm)) {
1533 /* we know this will work, because it worked before :) */
1534 configure_processors_unlocked (0, &lm);
1539 _have_internal_generator = false;
1541 for (i = _processors.begin(); i != _processors.end(); ++i) {
1542 boost::shared_ptr<PluginInsert> pi;
1544 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(*i)) != 0) {
1545 if (pi->has_no_inputs ()) {
1546 _have_internal_generator = true;
1553 /* now try to do what we need to so that those that were removed will be deleted */
1555 for (ProcessorList::iterator i = deleted.begin(); i != deleted.end(); ++i) {
1556 (*i)->drop_references ();
1559 reset_instrument_info ();
1560 processors_changed (RouteProcessorChange ()); /* EMIT SIGNAL */
1561 set_processor_positions ();
1567 Route::reset_instrument_info ()
1569 boost::shared_ptr<Processor> instr = the_instrument();
1571 _instrument_info.set_internal_instrument (instr);
1575 /** Caller must hold process lock */
1577 Route::configure_processors (ProcessorStreams* err)
1579 #ifndef PLATFORM_WINDOWS
1580 assert (!AudioEngine::instance()->process_lock().trylock());
1583 if (!_in_configure_processors) {
1584 Glib::Threads::RWLock::WriterLock lm (_processor_lock);
1585 return configure_processors_unlocked (err, &lm);
1592 Route::input_streams () const
1594 return _input->n_ports ();
1597 list<pair<ChanCount, ChanCount> >
1598 Route::try_configure_processors (ChanCount in, ProcessorStreams* err)
1600 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
1602 return try_configure_processors_unlocked (in, err);
1605 list<pair<ChanCount, ChanCount> >
1606 Route::try_configure_processors_unlocked (ChanCount in, ProcessorStreams* err)
1608 // Check each processor in order to see if we can configure as requested
1610 list<pair<ChanCount, ChanCount> > configuration;
1613 DEBUG_TRACE (DEBUG::Processors, string_compose ("%1: configure processors\n", _name));
1614 DEBUG_TRACE (DEBUG::Processors, "{\n");
1616 for (ProcessorList::iterator p = _processors.begin(); p != _processors.end(); ++p, ++index) {
1618 if ((*p)->can_support_io_configuration(in, out)) {
1620 if (boost::dynamic_pointer_cast<Delivery> (*p)
1621 && boost::dynamic_pointer_cast<Delivery> (*p)->role() == Delivery::Main
1623 && (is_monitor() || _strict_io || Profile->get_mixbus ())) {
1624 /* with strict I/O the panner + output are forced to
1625 * follow the last processor's output.
1627 * Delivery::can_support_io_configuration() will only add ports,
1628 * but not remove excess ports.
1630 * This works because the delivery only requires
1631 * as many outputs as there are inputs.
1632 * Delivery::configure_io() will do the actual removal
1633 * by calling _output->ensure_io()
1635 if (!is_master() && _session.master_out () && in.n_audio() > 0) {
1636 /* ..but at least as many as there are master-inputs, if
1637 * the delivery is dealing with audio */
1638 // XXX this may need special-casing for mixbus (master-outputs)
1639 // and should maybe be a preference anyway ?!
1640 out = ChanCount::max (in, _session.master_out ()->n_inputs ());
1646 DEBUG_TRACE (DEBUG::Processors, string_compose ("\t%1 ID=%2 in=%3 out=%4\n",(*p)->name(), (*p)->id(), in, out));
1647 configuration.push_back(make_pair(in, out));
1650 // restriction for Monitor Section Processors
1651 if (in.n_audio() != out.n_audio() || out.n_midi() > 0) {
1652 /* do not allow to add/remove channels (for now)
1653 * The Monitor follows the master-bus and has no panner (unpan)
1654 * but do allow processors with midi-in to be added (e.g VSTs with control that
1655 * will remain unconnected)
1657 DEBUG_TRACE (DEBUG::Processors, "Monitor: Channel configuration not allowed.\n");
1658 return list<pair<ChanCount, ChanCount> > ();
1660 if (boost::dynamic_pointer_cast<InternalSend> (*p)) {
1661 // internal sends make no sense, only feedback
1662 DEBUG_TRACE (DEBUG::Processors, "Monitor: No Sends allowed.\n");
1663 return list<pair<ChanCount, ChanCount> > ();
1665 if (boost::dynamic_pointer_cast<PortInsert> (*p)) {
1666 /* External Sends can be problematic. one can add/remove ports
1667 * there signal leaves the DAW to external monitors anyway, so there's
1668 * no real use for allowing them here anyway.
1670 DEBUG_TRACE (DEBUG::Processors, "Monitor: No External Sends allowed.\n");
1671 return list<pair<ChanCount, ChanCount> > ();
1673 if (boost::dynamic_pointer_cast<Send> (*p)) {
1675 DEBUG_TRACE (DEBUG::Processors, "Monitor: No Sends allowed.\n");
1676 return list<pair<ChanCount, ChanCount> > ();
1685 DEBUG_TRACE (DEBUG::Processors, "---- CONFIGURATION FAILED.\n");
1686 DEBUG_TRACE (DEBUG::Processors, string_compose ("---- %1 cannot support in=%2 out=%3\n", (*p)->name(), in, out));
1687 DEBUG_TRACE (DEBUG::Processors, "}\n");
1688 return list<pair<ChanCount, ChanCount> > ();
1692 DEBUG_TRACE (DEBUG::Processors, "}\n");
1694 return configuration;
1697 /** Set the input/output configuration of each processor in the processors list.
1698 * Caller must hold process lock.
1699 * Return 0 on success, otherwise configuration is impossible.
1702 Route::configure_processors_unlocked (ProcessorStreams* err, Glib::Threads::RWLock::WriterLock* lm)
1704 #ifndef PLATFORM_WINDOWS
1705 assert (!AudioEngine::instance()->process_lock().trylock());
1708 if (_in_configure_processors) {
1712 /* put invisible processors where they should be */
1713 setup_invisible_processors ();
1715 _in_configure_processors = true;
1717 list<pair<ChanCount, ChanCount> > configuration = try_configure_processors_unlocked (input_streams (), err);
1719 if (configuration.empty ()) {
1720 _in_configure_processors = false;
1725 bool seen_mains_out = false;
1726 processor_out_streams = _input->n_ports();
1727 processor_max_streams.reset();
1729 /* processor configure_io() may result in adding ports
1730 * e.g. Delivery::configure_io -> ARDOUR::IO::ensure_io ()
1732 * with jack2 adding ports results in a graph-order callback,
1733 * which calls Session::resort_routes() and eventually
1734 * Route::direct_feeds_according_to_reality()
1735 * which takes a ReaderLock (_processor_lock).
1737 * so we can't hold a WriterLock here until jack2 threading
1740 * NB. we still hold the process lock
1742 * (ardour's own engines do call graph-order from the
1743 * process-thread and hence do not have this issue; besides
1744 * merely adding ports won't trigger a graph-order, only
1745 * making connections does)
1749 // TODO check for a potential ReaderLock after ReaderLock ??
1750 Glib::Threads::RWLock::ReaderLock lr (_processor_lock);
1752 list< pair<ChanCount,ChanCount> >::iterator c = configuration.begin();
1753 for (ProcessorList::iterator p = _processors.begin(); p != _processors.end(); ++p, ++c) {
1755 if (!(*p)->configure_io(c->first, c->second)) {
1756 DEBUG_TRACE (DEBUG::Processors, string_compose ("%1: configuration failed\n", _name));
1757 _in_configure_processors = false;
1762 processor_max_streams = ChanCount::max(processor_max_streams, c->first);
1763 processor_max_streams = ChanCount::max(processor_max_streams, c->second);
1765 boost::shared_ptr<IOProcessor> iop;
1766 boost::shared_ptr<PluginInsert> pi;
1767 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(*p)) != 0) {
1768 /* plugins connected via Split or Hide Match may have more channels.
1769 * route/scratch buffers are needed for all of them
1770 * The configuration may only be a subset (both input and output)
1772 processor_max_streams = ChanCount::max(processor_max_streams, pi->required_buffers());
1774 else if ((iop = boost::dynamic_pointer_cast<IOProcessor>(*p)) != 0) {
1775 processor_max_streams = ChanCount::max(processor_max_streams, iop->natural_input_streams());
1776 processor_max_streams = ChanCount::max(processor_max_streams, iop->natural_output_streams());
1780 if (boost::dynamic_pointer_cast<Delivery> (*p)
1781 && boost::dynamic_pointer_cast<Delivery> (*p)->role() == Delivery::Main) {
1782 /* main delivery will increase port count to match input.
1783 * the Delivery::Main is usually the last processor - followed only by
1786 seen_mains_out = true;
1788 if (!seen_mains_out) {
1789 processor_out_streams = out;
1798 _meter->set_max_channels (processor_max_streams);
1801 /* make sure we have sufficient scratch buffers to cope with the new processor
1804 _session.ensure_buffers (n_process_buffers ());
1806 DEBUG_TRACE (DEBUG::Processors, string_compose ("%1: configuration complete\n", _name));
1808 _in_configure_processors = false;
1812 /** Set all visible processors to a given active state (except Fader, whose state is not changed)
1813 * @param state New active state for those processors.
1816 Route::all_visible_processors_active (bool state)
1818 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
1820 if (_processors.empty()) {
1824 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1825 if (!(*i)->display_to_user() || boost::dynamic_pointer_cast<Amp> (*i)) {
1828 (*i)->enable (state);
1831 _session.set_dirty ();
1835 Route::processors_reorder_needs_configure (const ProcessorList& new_order)
1837 /* check if re-order requires re-configuration of any processors
1838 * -> compare channel configuration for all processors
1840 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
1841 ChanCount c = input_streams ();
1843 for (ProcessorList::const_iterator j = new_order.begin(); j != new_order.end(); ++j) {
1845 if (c != (*j)->input_streams()) {
1848 for (ProcessorList::const_iterator i = _processors.begin(); i != _processors.end(); ++i) {
1851 if ((*i)->input_streams() != c) {
1854 c = (*i)->output_streams();
1866 __attribute__((annotate("realtime")))
1869 Route::apply_processor_order (const ProcessorList& new_order)
1871 /* need to hold processor_lock; either read or write lock
1872 * and the engine process_lock.
1873 * Due to r/w lock ambiguity we can only assert the latter
1875 assert (!AudioEngine::instance()->process_lock().trylock());
1878 /* "new_order" is an ordered list of processors to be positioned according to "placement".
1879 * NOTE: all processors in "new_order" MUST be marked as display_to_user(). There maybe additional
1880 * processors in the current actual processor list that are hidden. Any visible processors
1881 * in the current list but not in "new_order" will be assumed to be deleted.
1884 /* "as_it_will_be" and "_processors" are lists of shared pointers.
1885 * actual memory usage is small, but insert/erase is not actually rt-safe :(
1886 * (note though that ::processors_reorder_needs_configure() ensured that
1887 * this function will only ever be called from the rt-thread if no processor were removed)
1889 * either way, I can't proove it, but an x-run due to re-order here is less likley
1890 * than an x-run-less 'ardour-silent cycle' both of which effectively "click".
1893 ProcessorList as_it_will_be;
1894 ProcessorList::iterator oiter;
1895 ProcessorList::const_iterator niter;
1897 oiter = _processors.begin();
1898 niter = new_order.begin();
1900 while (niter != new_order.end()) {
1902 /* if the next processor in the old list is invisible (i.e. should not be in the new order)
1903 then append it to the temp list.
1905 Otherwise, see if the next processor in the old list is in the new list. if not,
1906 its been deleted. If its there, append it to the temp list.
1909 if (oiter == _processors.end()) {
1911 /* no more elements in the old list, so just stick the rest of
1912 the new order onto the temp list.
1915 as_it_will_be.insert (as_it_will_be.end(), niter, new_order.end());
1916 while (niter != new_order.end()) {
1923 if (!(*oiter)->display_to_user()) {
1925 as_it_will_be.push_back (*oiter);
1929 /* visible processor: check that its in the new order */
1931 if (find (new_order.begin(), new_order.end(), (*oiter)) == new_order.end()) {
1932 /* deleted: do nothing, shared_ptr<> will clean up */
1934 /* ignore this one, and add the next item from the new order instead */
1935 as_it_will_be.push_back (*niter);
1940 /* now remove from old order - its taken care of no matter what */
1941 oiter = _processors.erase (oiter);
1945 _processors.insert (oiter, as_it_will_be.begin(), as_it_will_be.end());
1947 /* If the meter is in a custom position, find it and make a rough note of its position */
1948 maybe_note_meter_position ();
1952 Route::reorder_processors (const ProcessorList& new_order, ProcessorStreams* err)
1954 // it a change is already queued, wait for it
1955 // (unless engine is stopped. apply immediately and proceed
1956 while (g_atomic_int_get (&_pending_process_reorder)) {
1957 if (!AudioEngine::instance()->running()) {
1958 DEBUG_TRACE (DEBUG::Processors, "offline apply queued processor re-order.\n");
1959 Glib::Threads::RWLock::WriterLock lm (_processor_lock);
1961 apply_processor_order(_pending_processor_order);
1962 setup_invisible_processors ();
1964 g_atomic_int_set (&_pending_process_reorder, 0);
1966 processors_changed (RouteProcessorChange ()); /* EMIT SIGNAL */
1967 set_processor_positions ();
1969 // TODO rather use a semaphore or something.
1970 // but since ::reorder_processors() is called
1971 // from the GUI thread, this is fine..
1976 if (processors_reorder_needs_configure (new_order) || !AudioEngine::instance()->running()) {
1978 Glib::Threads::Mutex::Lock lx (AudioEngine::instance()->process_lock ());
1979 Glib::Threads::RWLock::WriterLock lm (_processor_lock);
1980 ProcessorState pstate (this);
1982 apply_processor_order (new_order);
1984 if (configure_processors_unlocked (err, &lm)) {
1992 processors_changed (RouteProcessorChange ()); /* EMIT SIGNAL */
1993 set_processor_positions ();
1996 DEBUG_TRACE (DEBUG::Processors, "Queue clickless processor re-order.\n");
1997 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
1999 // _pending_processor_order is protected by _processor_lock
2000 _pending_processor_order = new_order;
2001 g_atomic_int_set (&_pending_process_reorder, 1);
2008 Route::add_remove_sidechain (boost::shared_ptr<Processor> proc, bool add)
2010 boost::shared_ptr<PluginInsert> pi;
2011 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(proc)) == 0) {
2015 if (pi->has_sidechain () == add) {
2016 return true; // ?? call failed, but result is as expected.
2020 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
2021 ProcessorList::iterator i = find (_processors.begin(), _processors.end(), proc);
2022 if (i == _processors.end ()) {
2028 Glib::Threads::Mutex::Lock lx (AudioEngine::instance()->process_lock ()); // take before Writerlock to avoid deadlock
2029 Glib::Threads::RWLock::WriterLock lm (_processor_lock);
2030 PBD::Unwinder<bool> uw (_in_sidechain_setup, true);
2032 lx.release (); // IO::add_port() and ~IO takes process lock - XXX check if this is safe
2034 if (!pi->add_sidechain ()) {
2038 if (!pi->del_sidechain ()) {
2044 list<pair<ChanCount, ChanCount> > c = try_configure_processors_unlocked (n_inputs (), 0);
2049 pi->del_sidechain ();
2051 pi->add_sidechain ();
2052 // TODO restore side-chain's state.
2057 configure_processors_unlocked (0, &lm);
2060 if (pi->has_sidechain ()) {
2061 pi->sidechain_input ()->changed.connect_same_thread (*this, boost::bind (&Route::sidechain_change_handler, this, _1, _2));
2064 processors_changed (RouteProcessorChange ()); /* EMIT SIGNAL */
2065 _session.set_dirty ();
2070 Route::plugin_preset_output (boost::shared_ptr<Processor> proc, ChanCount outs)
2072 boost::shared_ptr<PluginInsert> pi;
2073 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(proc)) == 0) {
2078 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
2079 ProcessorList::iterator i = find (_processors.begin(), _processors.end(), proc);
2080 if (i == _processors.end ()) {
2086 Glib::Threads::Mutex::Lock lx (AudioEngine::instance()->process_lock ());
2087 Glib::Threads::RWLock::WriterLock lm (_processor_lock);
2089 const ChanCount& old (pi->preset_out ());
2090 if (!pi->set_preset_out (outs)) {
2091 return true; // no change, OK
2094 list<pair<ChanCount, ChanCount> > c = try_configure_processors_unlocked (n_inputs (), 0);
2097 pi->set_preset_out (old);
2100 configure_processors_unlocked (0, &lm);
2103 processors_changed (RouteProcessorChange ()); /* EMIT SIGNAL */
2104 _session.set_dirty ();
2109 Route::reset_plugin_insert (boost::shared_ptr<Processor> proc)
2112 return customize_plugin_insert (proc, 0, unused, unused);
2116 Route::customize_plugin_insert (boost::shared_ptr<Processor> proc, uint32_t count, ChanCount outs, ChanCount sinks)
2118 boost::shared_ptr<PluginInsert> pi;
2119 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(proc)) == 0) {
2124 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
2125 ProcessorList::iterator i = find (_processors.begin(), _processors.end(), proc);
2126 if (i == _processors.end ()) {
2132 Glib::Threads::Mutex::Lock lx (AudioEngine::instance()->process_lock ());
2133 Glib::Threads::RWLock::WriterLock lm (_processor_lock);
2135 bool old_cust = pi->custom_cfg ();
2136 uint32_t old_cnt = pi->get_count ();
2137 ChanCount old_chan = pi->output_streams ();
2138 ChanCount old_sinks = pi->natural_input_streams ();
2141 pi->set_custom_cfg (false);
2143 pi->set_custom_cfg (true);
2144 pi->set_count (count);
2145 pi->set_outputs (outs);
2146 pi->set_sinks (sinks);
2149 list<pair<ChanCount, ChanCount> > c = try_configure_processors_unlocked (n_inputs (), 0);
2153 pi->set_count (old_cnt);
2154 pi->set_sinks (old_sinks);
2155 pi->set_outputs (old_chan);
2156 pi->set_custom_cfg (old_cust);
2160 configure_processors_unlocked (0, &lm);
2163 processors_changed (RouteProcessorChange ()); /* EMIT SIGNAL */
2164 _session.set_dirty ();
2169 Route::set_strict_io (const bool enable)
2171 Glib::Threads::Mutex::Lock lx (AudioEngine::instance()->process_lock ());
2173 if (_strict_io != enable) {
2174 _strict_io = enable;
2175 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
2176 for (ProcessorList::iterator p = _processors.begin(); p != _processors.end(); ++p) {
2177 boost::shared_ptr<PluginInsert> pi;
2178 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(*p)) != 0) {
2179 pi->set_strict_io (_strict_io);
2183 list<pair<ChanCount, ChanCount> > c = try_configure_processors_unlocked (n_inputs (), 0);
2187 _strict_io = !enable; // restore old value
2188 for (ProcessorList::iterator p = _processors.begin(); p != _processors.end(); ++p) {
2189 boost::shared_ptr<PluginInsert> pi;
2190 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(*p)) != 0) {
2191 pi->set_strict_io (_strict_io);
2198 configure_processors (0);
2201 processors_changed (RouteProcessorChange ()); /* EMIT SIGNAL */
2202 _session.set_dirty ();
2214 Route::get_template()
2216 return state(false);
2220 Route::state(bool full_state)
2223 if (!_session._template_state_dir.empty()) {
2224 assert (!full_state); // only for templates
2225 foreach_processor (sigc::bind (sigc::mem_fun (*this, &Route::set_plugin_state_dir), _session._template_state_dir));
2228 XMLNode *node = new XMLNode("Route");
2229 ProcessorList::iterator i;
2232 id().print (buf, sizeof (buf));
2233 node->add_property("id", buf);
2234 node->add_property ("name", _name);
2235 node->add_property("default-type", _default_type.to_string());
2236 node->add_property ("strict-io", _strict_io);
2238 node->add_child_nocopy (_presentation_info.get_state());
2240 node->add_property("active", _active?"yes":"no");
2242 node->add_property("denormal-protection", _denormal_protection?"yes":"no");
2243 node->add_property("meter-point", enum_2_string (_meter_point));
2245 node->add_property("meter-type", enum_2_string (_meter_type));
2248 node->add_property("route-group", _route_group->name());
2251 node->add_child_nocopy (_solo_control->get_state ());
2252 node->add_child_nocopy (_solo_isolate_control->get_state ());
2253 node->add_child_nocopy (_solo_safe_control->get_state ());
2255 node->add_child_nocopy (_input->state (full_state));
2256 node->add_child_nocopy (_output->state (full_state));
2257 node->add_child_nocopy (_mute_master->get_state ());
2259 node->add_child_nocopy (_mute_control->get_state ());
2260 node->add_child_nocopy (_phase_control->get_state ());
2263 node->add_child_nocopy (Automatable::get_automation_xml_state ());
2266 if (_comment.length()) {
2267 XMLNode *cmt = node->add_child ("Comment");
2268 cmt->add_content (_comment);
2272 node->add_child_nocopy (_pannable->state (full_state));
2276 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
2277 for (i = _processors.begin(); i != _processors.end(); ++i) {
2279 /* template save: do not include internal sends functioning as
2280 aux sends because the chance of the target ID
2281 in the session where this template is used
2284 similarly, do not save listen sends which connect to
2285 the monitor section, because these will always be
2288 boost::shared_ptr<InternalSend> is;
2290 if ((is = boost::dynamic_pointer_cast<InternalSend> (*i)) != 0) {
2291 if (is->role() == Delivery::Listen) {
2296 node->add_child_nocopy((*i)->state (full_state));
2301 node->add_child_copy (*_extra_xml);
2304 if (_custom_meter_position_noted) {
2305 boost::shared_ptr<Processor> after = _processor_after_last_custom_meter.lock ();
2307 after->id().print (buf, sizeof (buf));
2308 node->add_property (X_("processor-after-last-custom-meter"), buf);
2312 if (!_session._template_state_dir.empty()) {
2313 foreach_processor (sigc::bind (sigc::mem_fun (*this, &Route::set_plugin_state_dir), ""));
2316 node->add_child_copy (Slavable::get_state());
2322 Route::set_state (const XMLNode& node, int version)
2324 if (version < 3000) {
2325 return set_state_2X (node, version);
2329 XMLNodeConstIterator niter;
2331 XMLProperty const * prop;
2333 if (node.name() != "Route"){
2334 error << string_compose(_("Bad node sent to Route::set_state() [%1]"), node.name()) << endmsg;
2338 if ((prop = node.property (X_("name"))) != 0) {
2339 Route::set_name (prop->value());
2343 _initial_io_setup = true;
2345 Stripable::set_state (node, version);
2347 if ((prop = node.property (X_("strict-io"))) != 0) {
2348 _strict_io = string_is_affirmative (prop->value());
2352 /* monitor bus does not get a panner, but if (re)created
2353 via XML, it will already have one by the time we
2354 call ::set_state(). so ... remove it.
2359 /* add all processors (except amp, which is always present) */
2361 nlist = node.children();
2362 XMLNode processor_state (X_("processor_state"));
2364 Stateful::save_extra_xml (node);
2366 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
2370 if (child->name() == IO::state_node_name) {
2371 if ((prop = child->property (X_("direction"))) == 0) {
2375 if (prop->value() == "Input") {
2376 _input->set_state (*child, version);
2377 } else if (prop->value() == "Output") {
2378 _output->set_state (*child, version);
2381 } else if (child->name() == X_("Processor")) {
2382 processor_state.add_child_copy (*child);
2383 } else if (child->name() == X_("Pannable")) {
2385 _pannable->set_state (*child, version);
2387 warning << string_compose (_("Pannable state found for route (%1) without a panner!"), name()) << endmsg;
2389 } else if (child->name() == Slavable::xml_node_name) {
2390 Slavable::set_state (*child, version);
2394 if ((prop = node.property (X_("meter-point"))) != 0) {
2395 MeterPoint mp = MeterPoint (string_2_enum (prop->value (), _meter_point));
2396 set_meter_point (mp, true);
2398 _meter->set_display_to_user (_meter_point == MeterCustom);
2402 if ((prop = node.property (X_("meter-type"))) != 0) {
2403 _meter_type = MeterType (string_2_enum (prop->value (), _meter_type));
2406 _initial_io_setup = false;
2408 set_processor_state (processor_state);
2410 // this looks up the internal instrument in processors
2411 reset_instrument_info();
2413 if ((prop = node.property (X_("denormal-protection"))) != 0) {
2414 set_denormal_protection (string_is_affirmative (prop->value()));
2417 if ((prop = node.property (X_("active"))) != 0) {
2418 bool yn = string_is_affirmative (prop->value());
2419 set_active (yn, this);
2422 if ((prop = node.property (X_("processor-after-last-custom-meter"))) != 0) {
2423 PBD::ID id (prop->value ());
2424 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
2425 ProcessorList::const_iterator i = _processors.begin ();
2426 while (i != _processors.end() && (*i)->id() != id) {
2430 if (i != _processors.end ()) {
2431 _processor_after_last_custom_meter = *i;
2432 _custom_meter_position_noted = true;
2436 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
2439 if (child->name() == X_("Comment")) {
2441 /* XXX this is a terrible API design in libxml++ */
2443 XMLNode *cmt = *(child->children().begin());
2444 _comment = cmt->content();
2446 } else if (child->name() == Controllable::xml_node_name) {
2447 if ((prop = child->property (X_("name"))) == 0) {
2451 if (prop->value() == _gain_control->name()) {
2452 _gain_control->set_state (*child, version);
2453 } else if (prop->value() == _solo_control->name()) {
2454 _solo_control->set_state (*child, version);
2455 } else if (prop->value() == _solo_safe_control->name()) {
2456 _solo_safe_control->set_state (*child, version);
2457 } else if (prop->value() == _solo_isolate_control->name()) {
2458 _solo_isolate_control->set_state (*child, version);
2459 } else if (prop->value() == _solo_control->name()) {
2460 _mute_control->set_state (*child, version);
2461 } else if (prop->value() == _phase_control->name()) {
2462 _phase_control->set_state (*child, version);
2464 } else if (child->name() == MuteMaster::xml_node_name) {
2465 _mute_master->set_state (*child, version);
2467 } else if (child->name() == Automatable::xml_node_name) {
2468 set_automation_xml_state (*child, Evoral::Parameter(NullAutomation));
2476 Route::set_state_2X (const XMLNode& node, int version)
2480 XMLNodeConstIterator niter;
2482 XMLProperty const * prop;
2484 /* 2X things which still remain to be handled:
2490 if (node.name() != "Route") {
2491 error << string_compose(_("Bad node sent to Route::set_state() [%1]"), node.name()) << endmsg;
2495 Stripable::set_state (node, version);
2497 if ((prop = node.property (X_("denormal-protection"))) != 0) {
2498 set_denormal_protection (string_is_affirmative (prop->value()));
2501 if ((prop = node.property (X_("muted"))) != 0) {
2504 bool muted = string_is_affirmative (prop->value());
2510 if ((prop = node.property (X_("mute-affects-pre-fader"))) != 0) {
2512 if (string_is_affirmative (prop->value())){
2513 mute_point = mute_point + "PreFader";
2518 if ((prop = node.property (X_("mute-affects-post-fader"))) != 0) {
2520 if (string_is_affirmative (prop->value())){
2523 mute_point = mute_point + ",";
2526 mute_point = mute_point + "PostFader";
2531 if ((prop = node.property (X_("mute-affects-control-outs"))) != 0) {
2533 if (string_is_affirmative (prop->value())){
2536 mute_point = mute_point + ",";
2539 mute_point = mute_point + "Listen";
2544 if ((prop = node.property (X_("mute-affects-main-outs"))) != 0) {
2546 if (string_is_affirmative (prop->value())){
2549 mute_point = mute_point + ",";
2552 mute_point = mute_point + "Main";
2556 _mute_master->set_mute_points (mute_point);
2557 _mute_master->set_muted_by_self (true);
2561 if ((prop = node.property (X_("meter-point"))) != 0) {
2562 _meter_point = MeterPoint (string_2_enum (prop->value (), _meter_point));
2567 nlist = node.children ();
2568 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2572 if (child->name() == IO::state_node_name) {
2574 /* there is a note in IO::set_state_2X() about why we have to call
2578 _input->set_state_2X (*child, version, true);
2579 _output->set_state_2X (*child, version, false);
2581 if ((prop = child->property (X_("name"))) != 0) {
2582 Route::set_name (prop->value ());
2587 if ((prop = child->property (X_("active"))) != 0) {
2588 bool yn = string_is_affirmative (prop->value());
2589 _active = !yn; // force switch
2590 set_active (yn, this);
2593 if ((prop = child->property (X_("gain"))) != 0) {
2596 if (sscanf (prop->value().c_str(), "%f", &val) == 1) {
2597 _amp->gain_control()->set_value (val, Controllable::NoGroup);
2601 /* Set up Panners in the IO */
2602 XMLNodeList io_nlist = child->children ();
2604 XMLNodeConstIterator io_niter;
2607 for (io_niter = io_nlist.begin(); io_niter != io_nlist.end(); ++io_niter) {
2609 io_child = *io_niter;
2611 if (io_child->name() == X_("Panner")) {
2612 _main_outs->panner_shell()->set_state(*io_child, version);
2613 } else if (io_child->name() == X_("Automation")) {
2614 /* IO's automation is for the fader */
2615 _amp->set_automation_xml_state (*io_child, Evoral::Parameter (GainAutomation));
2621 XMLNodeList redirect_nodes;
2623 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
2627 if (child->name() == X_("Send") || child->name() == X_("Insert")) {
2628 redirect_nodes.push_back(child);
2633 set_processor_state_2X (redirect_nodes, version);
2635 Stateful::save_extra_xml (node);
2637 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
2640 if (child->name() == X_("Comment")) {
2642 /* XXX this is a terrible API design in libxml++ */
2644 XMLNode *cmt = *(child->children().begin());
2645 _comment = cmt->content();
2647 } else if (child->name() == Controllable::xml_node_name && (prop = child->property("name")) != 0) {
2648 if (prop->value() == X_("solo")) {
2649 _solo_control->set_state (*child, version);
2650 } else if (prop->value() == X_("mute")) {
2651 _mute_control->set_state (*child, version);
2661 Route::get_processor_state ()
2663 XMLNode* root = new XMLNode (X_("redirects"));
2664 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
2665 root->add_child_nocopy ((*i)->state (true));
2672 Route::set_processor_state_2X (XMLNodeList const & nList, int version)
2674 /* We don't bother removing existing processors not in nList, as this
2675 method will only be called when creating a Route from scratch, not
2676 for undo purposes. Just put processors in at the appropriate place
2680 for (XMLNodeConstIterator i = nList.begin(); i != nList.end(); ++i) {
2681 add_processor_from_xml_2X (**i, version);
2686 Route::set_processor_state (const XMLNode& node)
2688 const XMLNodeList &nlist = node.children();
2689 XMLNodeConstIterator niter;
2690 ProcessorList new_order;
2691 bool must_configure = false;
2693 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2695 XMLProperty* prop = (*niter)->property ("type");
2697 if (prop->value() == "amp") {
2698 _amp->set_state (**niter, Stateful::current_state_version);
2699 new_order.push_back (_amp);
2700 } else if (prop->value() == "trim") {
2701 _trim->set_state (**niter, Stateful::current_state_version);
2702 new_order.push_back (_trim);
2703 } else if (prop->value() == "meter") {
2704 _meter->set_state (**niter, Stateful::current_state_version);
2705 new_order.push_back (_meter);
2706 } else if (prop->value() == "delay") {
2708 _delayline->set_state (**niter, Stateful::current_state_version);
2709 new_order.push_back (_delayline);
2711 } else if (prop->value() == "main-outs") {
2712 _main_outs->set_state (**niter, Stateful::current_state_version);
2713 } else if (prop->value() == "intreturn") {
2715 _intreturn.reset (new InternalReturn (_session));
2716 must_configure = true;
2718 _intreturn->set_state (**niter, Stateful::current_state_version);
2719 } else if (is_monitor() && prop->value() == "monitor") {
2720 if (!_monitor_control) {
2721 _monitor_control.reset (new MonitorProcessor (_session));
2722 must_configure = true;
2724 _monitor_control->set_state (**niter, Stateful::current_state_version);
2725 } else if (prop->value() == "capture") {
2726 /* CapturingProcessor should never be restored, it's always
2727 added explicitly when needed */
2729 ProcessorList::iterator o;
2731 for (o = _processors.begin(); o != _processors.end(); ++o) {
2732 XMLProperty const * id_prop = (*niter)->property(X_("id"));
2733 if (id_prop && (*o)->id() == id_prop->value()) {
2734 (*o)->set_state (**niter, Stateful::current_state_version);
2735 new_order.push_back (*o);
2740 // If the processor (*niter) is not on the route then create it
2742 if (o == _processors.end()) {
2744 boost::shared_ptr<Processor> processor;
2746 if (prop->value() == "intsend") {
2748 processor.reset (new InternalSend (_session, _pannable, _mute_master, boost::dynamic_pointer_cast<ARDOUR::Route>(shared_from_this()), boost::shared_ptr<Route>(), Delivery::Aux, true));
2750 } else if (prop->value() == "ladspa" || prop->value() == "Ladspa" ||
2751 prop->value() == "lv2" ||
2752 prop->value() == "windows-vst" ||
2753 prop->value() == "lxvst" ||
2754 prop->value() == "luaproc" ||
2755 prop->value() == "audiounit") {
2757 if (_session.get_disable_all_loaded_plugins ()) {
2758 processor.reset (new UnknownProcessor (_session, **niter));
2760 processor.reset (new PluginInsert (_session));
2761 processor->set_owner (this);
2763 boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert>(processor);
2764 pi->set_strict_io (true);
2768 } else if (prop->value() == "port") {
2770 processor.reset (new PortInsert (_session, _pannable, _mute_master));
2772 } else if (prop->value() == "send") {
2774 processor.reset (new Send (_session, _pannable, _mute_master, Delivery::Send, true));
2775 boost::shared_ptr<Send> send = boost::dynamic_pointer_cast<Send> (processor);
2776 send->SelfDestruct.connect_same_thread (*this,
2777 boost::bind (&Route::processor_selfdestruct, this, boost::weak_ptr<Processor> (processor)));
2780 error << string_compose(_("unknown Processor type \"%1\"; ignored"), prop->value()) << endmsg;
2784 if (processor->set_state (**niter, Stateful::current_state_version) != 0) {
2785 /* This processor could not be configured. Turn it into a UnknownProcessor */
2786 processor.reset (new UnknownProcessor (_session, **niter));
2789 /* subscribe to Sidechain IO changes */
2790 boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (processor);
2791 if (pi && pi->has_sidechain ()) {
2792 pi->sidechain_input ()->changed.connect_same_thread (*this, boost::bind (&Route::sidechain_change_handler, this, _1, _2));
2795 /* we have to note the monitor send here, otherwise a new one will be created
2796 and the state of this one will be lost.
2798 boost::shared_ptr<InternalSend> isend = boost::dynamic_pointer_cast<InternalSend> (processor);
2799 if (isend && isend->role() == Delivery::Listen) {
2800 _monitor_send = isend;
2803 /* it doesn't matter if invisible processors are added here, as they
2804 will be sorted out by setup_invisible_processors () shortly.
2807 new_order.push_back (processor);
2808 must_configure = true;
2814 Glib::Threads::RWLock::WriterLock lm (_processor_lock);
2815 /* re-assign _processors w/o process-lock.
2816 * if there's an IO-processor present in _processors but
2817 * not in new_order, it will be deleted and ~IO takes
2820 _processors = new_order;
2821 Glib::Threads::Mutex::Lock lx (AudioEngine::instance()->process_lock ());
2823 if (must_configure) {
2824 configure_processors_unlocked (0, &lm);
2827 for (ProcessorList::const_iterator i = _processors.begin(); i != _processors.end(); ++i) {
2829 (*i)->set_owner (this);
2830 (*i)->ActiveChanged.connect_same_thread (*this, boost::bind (&Session::update_latency_compensation, &_session, false));
2832 boost::shared_ptr<PluginInsert> pi;
2834 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(*i)) != 0) {
2835 if (pi->has_no_inputs ()) {
2836 _have_internal_generator = true;
2843 reset_instrument_info ();
2844 processors_changed (RouteProcessorChange ()); /* EMIT SIGNAL */
2845 set_processor_positions ();
2849 Route::curve_reallocate ()
2851 // _gain_automation_curve.finish_resize ();
2852 // _pan_automation_curve.finish_resize ();
2856 Route::silence (framecnt_t nframes)
2858 Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
2863 silence_unlocked (nframes);
2867 Route::silence_unlocked (framecnt_t nframes)
2869 /* Must be called with the processor lock held */
2871 const framepos_t now = _session.transport_frame ();
2875 _output->silence (nframes);
2877 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
2878 boost::shared_ptr<PluginInsert> pi;
2880 if (!_active && (pi = boost::dynamic_pointer_cast<PluginInsert> (*i)) != 0) {
2881 // skip plugins, they don't need anything when we're not active
2885 (*i)->silence (nframes, now);
2888 if (nframes == _session.get_block_size()) {
2895 Route::add_internal_return ()
2898 _intreturn.reset (new InternalReturn (_session));
2899 add_processor (_intreturn, PreFader);
2904 Route::add_send_to_internal_return (InternalSend* send)
2906 Glib::Threads::RWLock::ReaderLock rm (_processor_lock);
2908 for (ProcessorList::const_iterator x = _processors.begin(); x != _processors.end(); ++x) {
2909 boost::shared_ptr<InternalReturn> d = boost::dynamic_pointer_cast<InternalReturn>(*x);
2912 return d->add_send (send);
2918 Route::remove_send_from_internal_return (InternalSend* send)
2920 Glib::Threads::RWLock::ReaderLock rm (_processor_lock);
2922 for (ProcessorList::const_iterator x = _processors.begin(); x != _processors.end(); ++x) {
2923 boost::shared_ptr<InternalReturn> d = boost::dynamic_pointer_cast<InternalReturn>(*x);
2926 return d->remove_send (send);
2932 Route::enable_monitor_send ()
2934 /* Caller must hold process lock */
2935 assert (!AudioEngine::instance()->process_lock().trylock());
2937 /* master never sends to monitor section via the normal mechanism */
2938 assert (!is_master ());
2939 assert (!is_monitor ());
2941 /* make sure we have one */
2942 if (!_monitor_send) {
2943 _monitor_send.reset (new InternalSend (_session, _pannable, _mute_master, boost::dynamic_pointer_cast<ARDOUR::Route>(shared_from_this()), _session.monitor_out(), Delivery::Listen));
2944 _monitor_send->set_display_to_user (false);
2948 configure_processors (0);
2951 /** Add an aux send to a route.
2952 * @param route route to send to.
2953 * @param before Processor to insert before, or 0 to insert at the end.
2956 Route::add_aux_send (boost::shared_ptr<Route> route, boost::shared_ptr<Processor> before)
2958 assert (route != _session.monitor_out ());
2961 Glib::Threads::RWLock::ReaderLock rm (_processor_lock);
2963 for (ProcessorList::iterator x = _processors.begin(); x != _processors.end(); ++x) {
2965 boost::shared_ptr<InternalSend> d = boost::dynamic_pointer_cast<InternalSend> (*x);
2967 if (d && d->target_route() == route) {
2968 /* already listening via the specified IO: do nothing */
2976 boost::shared_ptr<InternalSend> listener;
2979 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
2980 boost::shared_ptr<Pannable> sendpan (new Pannable (_session));
2981 listener.reset (new InternalSend (_session, sendpan, _mute_master, boost::dynamic_pointer_cast<ARDOUR::Route>(shared_from_this()), route, Delivery::Aux));
2984 add_processor (listener, before);
2986 } catch (failed_constructor& err) {
2994 Route::remove_aux_or_listen (boost::shared_ptr<Route> route)
2996 ProcessorStreams err;
2997 ProcessorList::iterator tmp;
3000 Glib::Threads::RWLock::ReaderLock rl(_processor_lock);
3002 /* have to do this early because otherwise processor reconfig
3003 * will put _monitor_send back in the list
3006 if (route == _session.monitor_out()) {
3007 _monitor_send.reset ();
3011 for (ProcessorList::iterator x = _processors.begin(); x != _processors.end(); ++x) {
3013 boost::shared_ptr<InternalSend> d = boost::dynamic_pointer_cast<InternalSend>(*x);
3015 if (d && d->target_route() == route) {
3017 if (remove_processor (*x, &err, false) > 0) {
3023 /* list could have been demolished while we dropped the lock
3026 if (_session.engine().connected()) {
3027 /* i/o processors cannot be removed if the engine is not running
3028 * so don't live-loop in case the engine is N/A or dies
3038 Route::set_comment (string cmt, void *src)
3042 _session.set_dirty ();
3046 Route::add_fed_by (boost::shared_ptr<Route> other, bool via_sends_only)
3048 FeedRecord fr (other, via_sends_only);
3050 pair<FedBy::iterator,bool> result = _fed_by.insert (fr);
3052 if (!result.second) {
3054 /* already a record for "other" - make sure sends-only information is correct */
3055 if (!via_sends_only && result.first->sends_only) {
3056 FeedRecord* frp = const_cast<FeedRecord*>(&(*result.first));
3057 frp->sends_only = false;
3061 return result.second;
3065 Route::clear_fed_by ()
3071 Route::feeds (boost::shared_ptr<Route> other, bool* via_sends_only)
3073 const FedBy& fed_by (other->fed_by());
3075 for (FedBy::const_iterator f = fed_by.begin(); f != fed_by.end(); ++f) {
3076 boost::shared_ptr<Route> sr = f->r.lock();
3078 if (sr && (sr.get() == this)) {
3080 if (via_sends_only) {
3081 *via_sends_only = f->sends_only;
3092 Route::all_inputs () const
3094 /* TODO, if this works as expected,
3095 * cache the IOVector and maintain it via
3096 * input_change_handler(), sidechain_change_handler() etc
3099 ios.push_back (_input);
3101 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
3102 for (ProcessorList::const_iterator r = _processors.begin(); r != _processors.end(); ++r) {
3104 boost::shared_ptr<IOProcessor> iop = boost::dynamic_pointer_cast<IOProcessor>(*r);
3105 boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert>(*r);
3108 iop = pi->sidechain();
3111 if (iop != 0 && iop->input()) {
3112 ios.push_back (iop->input());
3119 Route::all_outputs () const
3122 // _output is included via Delivery
3123 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
3124 for (ProcessorList::const_iterator r = _processors.begin(); r != _processors.end(); ++r) {
3125 boost::shared_ptr<IOProcessor> iop = boost::dynamic_pointer_cast<IOProcessor>(*r);
3126 if (iop != 0 && iop->output()) {
3127 ios.push_back (iop->output());
3134 Route::direct_feeds_according_to_reality (boost::shared_ptr<Route> other, bool* via_send_only)
3136 DEBUG_TRACE (DEBUG::Graph, string_compose ("Feeds? %1\n", _name));
3137 if (other->all_inputs().fed_by (_output)) {
3138 DEBUG_TRACE (DEBUG::Graph, string_compose ("\tdirect FEEDS %2\n", other->name()));
3139 if (via_send_only) {
3140 *via_send_only = false;
3146 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
3148 for (ProcessorList::iterator r = _processors.begin(); r != _processors.end(); ++r) {
3150 boost::shared_ptr<IOProcessor> iop = boost::dynamic_pointer_cast<IOProcessor>(*r);
3151 boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert>(*r);
3154 iop = pi->sidechain();
3158 boost::shared_ptr<const IO> iop_out = iop->output();
3159 if (other.get() == this && iop_out && iop->input() && iop_out->connected_to (iop->input())) {
3160 // TODO this needs a delaylines in the Insert to align connections (!)
3161 DEBUG_TRACE (DEBUG::Graph, string_compose ("\tIOP %1 does feed its own return (%2)\n", iop->name(), other->name()));
3164 if ((iop_out && other->all_inputs().fed_by (iop_out)) || iop->feeds (other)) {
3165 DEBUG_TRACE (DEBUG::Graph, string_compose ("\tIOP %1 does feed %2\n", iop->name(), other->name()));
3166 if (via_send_only) {
3167 *via_send_only = true;
3171 DEBUG_TRACE (DEBUG::Graph, string_compose ("\tIOP %1 does NOT feed %2\n", iop->name(), other->name()));
3174 DEBUG_TRACE (DEBUG::Graph, string_compose ("\tPROC %1 is not an IOP\n", (*r)->name()));
3179 DEBUG_TRACE (DEBUG::Graph, string_compose ("\tdoes NOT feed %1\n", other->name()));
3184 Route::direct_feeds_according_to_graph (boost::shared_ptr<Route> other, bool* via_send_only)
3186 return _session._current_route_graph.has (shared_from_this (), other, via_send_only);
3190 Route::feeds_according_to_graph (boost::shared_ptr<Route> other)
3192 return _session._current_route_graph.feeds (shared_from_this (), other);
3195 /** Called from the (non-realtime) butler thread when the transport is stopped */
3197 Route::nonrealtime_handle_transport_stopped (bool /*abort_ignored*/, bool /*did_locate*/, bool can_flush_processors)
3199 framepos_t now = _session.transport_frame();
3202 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
3204 Automatable::transport_stopped (now);
3206 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
3208 if (!_have_internal_generator && (Config->get_plugins_stop_with_transport() && can_flush_processors)) {
3212 (*i)->transport_stopped (now);
3216 _roll_delay = _initial_delay;
3220 Route::input_change_handler (IOChange change, void * /*src*/)
3222 if ((change.type & IOChange::ConfigurationChanged)) {
3223 /* This is called with the process lock held if change
3224 contains ConfigurationChanged
3226 configure_processors (0);
3227 _phase_control->resize (_input->n_ports().n_audio ());
3228 io_changed (); /* EMIT SIGNAL */
3231 if (_solo_control->soloed_by_others_upstream() || _solo_isolate_control->solo_isolated_by_upstream()) {
3234 boost::shared_ptr<RouteList> routes = _session.get_routes ();
3235 if (_input->connected()) {
3236 for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
3237 if ((*i).get() == this || (*i)->is_master() || (*i)->is_monitor() || (*i)->is_auditioner()) {
3241 bool does_feed = (*i)->direct_feeds_according_to_reality (shared_from_this(), &sends_only);
3242 if (does_feed && !sends_only) {
3243 if ((*i)->soloed()) {
3246 if ((*i)->solo_isolate_control()->solo_isolated()) {
3253 int delta = sbou - _solo_control->soloed_by_others_upstream();
3254 int idelta = ibou - _solo_isolate_control->solo_isolated_by_upstream();
3257 PBD::warning << string_compose (
3258 _("Invalid Solo-Isolate propagation: from:%1 new:%2 - old:%3 = delta:%4"),
3259 _name, ibou, _solo_isolate_control->solo_isolated_by_upstream(), idelta)
3264 if (_solo_control->soloed_by_others_upstream()) {
3265 // ignore new connections (they're not propagated)
3267 _solo_control->mod_solo_by_others_upstream (delta);
3271 if (_solo_isolate_control->solo_isolated_by_upstream()) {
3272 // solo-isolate currently only propagates downstream
3274 _solo_isolate_control->mod_solo_isolated_by_upstream (1);
3276 //_solo_isolated_by_upstream = ibou;
3279 // Session::route_solo_changed does not propagate indirect solo-changes
3280 // propagate downstream to tracks
3281 for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
3282 if ((*i).get() == this || (*i)->is_master() || (*i)->is_monitor() || (*i)->is_auditioner()) {
3286 bool does_feed = feeds (*i, &sends_only);
3287 if (delta <= 0 && does_feed && !sends_only) {
3288 (*i)->solo_control()->mod_solo_by_others_upstream (delta);
3291 if (idelta < 0 && does_feed && !sends_only) {
3292 (*i)->solo_isolate_control()->mod_solo_isolated_by_upstream (-1);
3299 Route::output_change_handler (IOChange change, void * /*src*/)
3301 if (_initial_io_setup) {
3305 if ((change.type & IOChange::ConfigurationChanged)) {
3306 /* This is called with the process lock held if change
3307 contains ConfigurationChanged
3309 configure_processors (0);
3312 _session.reset_monitor_section();
3315 io_changed (); /* EMIT SIGNAL */
3318 if (_solo_control->soloed_by_others_downstream()) {
3320 /* checking all all downstream routes for
3321 * explicit of implict solo is a rather drastic measure,
3322 * ideally the input_change_handler() of the other route
3323 * would propagate the change to us.
3325 boost::shared_ptr<RouteList> routes = _session.get_routes ();
3326 if (_output->connected()) {
3327 for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
3328 if ((*i).get() == this || (*i)->is_master() || (*i)->is_monitor() || (*i)->is_auditioner()) {
3332 bool does_feed = direct_feeds_according_to_reality (*i, &sends_only);
3333 if (does_feed && !sends_only) {
3334 if ((*i)->soloed()) {
3341 int delta = sbod - _solo_control->soloed_by_others_downstream();
3343 // do not allow new connections to change implicit solo (no propagation)
3344 _solo_control->mod_solo_by_others_downstream (delta);
3345 // Session::route_solo_changed() does not propagate indirect solo-changes
3346 // propagate upstream to tracks
3347 for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
3348 if ((*i).get() == this || !can_solo()) {
3352 bool does_feed = (*i)->feeds (shared_from_this(), &sends_only);
3353 if (delta != 0 && does_feed && !sends_only) {
3354 (*i)->solo_control()->mod_solo_by_others_downstream (delta);
3363 Route::sidechain_change_handler (IOChange change, void* src)
3365 if (_initial_io_setup || _in_sidechain_setup) {
3369 input_change_handler (change, src);
3373 Route::pans_required () const
3375 if (n_outputs().n_audio() < 2) {
3379 return max (n_inputs ().n_audio(), processor_max_streams.n_audio());
3383 Route::flush_processor_buffers_locked (framecnt_t nframes)
3385 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
3386 boost::shared_ptr<Delivery> d = boost::dynamic_pointer_cast<Delivery> (*i);
3388 d->flush_buffers (nframes);
3390 boost::shared_ptr<PortInsert> p = boost::dynamic_pointer_cast<PortInsert> (*i);
3392 p->flush_buffers (nframes);
3399 Route::no_roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, bool session_state_changing)
3401 Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
3407 if (n_outputs().n_total() == 0) {
3411 if (!_active || n_inputs() == ChanCount::ZERO) {
3412 silence_unlocked (nframes);
3416 if (session_state_changing) {
3417 if (_session.transport_speed() != 0.0f) {
3418 /* we're rolling but some state is changing (e.g. our diskstream contents)
3419 so we cannot use them. Be silent till this is over.
3421 XXX note the absurdity of ::no_roll() being called when we ARE rolling!
3423 silence_unlocked (nframes);
3426 /* we're really not rolling, so we're either delivery silence or actually
3427 monitoring, both of which are safe to do while session_state_changing is true.
3431 BufferSet& bufs = _session.get_route_buffers (n_process_buffers());
3433 fill_buffers_with_input (bufs, _input, nframes);
3435 if (_meter_point == MeterInput) {
3436 _meter->run (bufs, start_frame, end_frame, 0.0, nframes, true);
3439 _amp->apply_gain_automation (false);
3440 _trim->apply_gain_automation (false);
3441 passthru (bufs, start_frame, end_frame, nframes, 0);
3443 flush_processor_buffers_locked (nframes);
3449 Route::roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame, int declick, bool& /* need_butler */)
3451 Glib::Threads::RWLock::ReaderLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
3456 if (n_outputs().n_total() == 0) {
3460 if (!_active || n_inputs().n_total() == 0) {
3461 silence_unlocked (nframes);
3465 framepos_t unused = 0;
3467 if ((nframes = check_initial_delay (nframes, unused)) == 0) {
3473 BufferSet& bufs = _session.get_route_buffers (n_process_buffers());
3475 fill_buffers_with_input (bufs, _input, nframes);
3477 if (_meter_point == MeterInput) {
3478 _meter->run (bufs, start_frame, end_frame, 1.0, nframes, true);
3481 passthru (bufs, start_frame, end_frame, nframes, declick);
3483 flush_processor_buffers_locked (nframes);
3489 Route::silent_roll (pframes_t nframes, framepos_t /*start_frame*/, framepos_t /*end_frame*/, bool& /* need_butler */)
3492 flush_processor_buffers_locked (nframes);
3497 Route::flush_processors ()
3499 /* XXX shouldn't really try to take this lock, since
3500 this is called from the RT audio thread.
3503 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
3505 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
3511 __attribute__((annotate("realtime")))
3514 Route::apply_processor_changes_rt ()
3516 int emissions = EmitNone;
3518 if (_pending_meter_point != _meter_point) {
3519 Glib::Threads::RWLock::WriterLock pwl (_processor_lock, Glib::Threads::TRY_LOCK);
3521 /* meters always have buffers for 'processor_max_streams'
3522 * they can be re-positioned without re-allocation */
3523 if (set_meter_point_unlocked()) {
3524 emissions |= EmitMeterChanged | EmitMeterVisibilityChange;;
3526 emissions |= EmitMeterChanged;
3531 bool changed = false;
3533 if (g_atomic_int_get (&_pending_process_reorder)) {
3534 Glib::Threads::RWLock::WriterLock pwl (_processor_lock, Glib::Threads::TRY_LOCK);
3536 apply_processor_order (_pending_processor_order);
3537 setup_invisible_processors ();
3539 g_atomic_int_set (&_pending_process_reorder, 0);
3540 emissions |= EmitRtProcessorChange;
3544 set_processor_positions ();
3546 if (emissions != 0) {
3547 g_atomic_int_set (&_pending_signals, emissions);
3550 return (!selfdestruct_sequence.empty ());
3554 Route::emit_pending_signals ()
3556 int sig = g_atomic_int_and (&_pending_signals, 0);
3557 if (sig & EmitMeterChanged) {
3558 _meter->emit_configuration_changed();
3559 meter_change (); /* EMIT SIGNAL */
3560 if (sig & EmitMeterVisibilityChange) {
3561 processors_changed (RouteProcessorChange (RouteProcessorChange::MeterPointChange, true)); /* EMIT SIGNAL */
3563 processors_changed (RouteProcessorChange (RouteProcessorChange::MeterPointChange, false)); /* EMIT SIGNAL */
3566 if (sig & EmitRtProcessorChange) {
3567 processors_changed (RouteProcessorChange (RouteProcessorChange::RealTimeChange)); /* EMIT SIGNAL */
3570 /* this would be a job for the butler.
3571 * Conceptually we should not take processe/processor locks here.
3572 * OTOH its more efficient (less overhead for summoning the butler and
3573 * telling her what do do) and signal emission is called
3574 * directly after the process callback, which decreases the chance
3575 * of x-runs when taking the locks.
3577 while (!selfdestruct_sequence.empty ()) {
3578 Glib::Threads::Mutex::Lock lx (selfdestruct_lock);
3579 if (selfdestruct_sequence.empty ()) { break; } // re-check with lock
3580 boost::shared_ptr<Processor> proc = selfdestruct_sequence.back ().lock ();
3581 selfdestruct_sequence.pop_back ();
3584 remove_processor (proc);
3590 Route::set_meter_point (MeterPoint p, bool force)
3592 if (_pending_meter_point == p && !force) {
3596 if (force || !AudioEngine::instance()->running()) {
3597 Glib::Threads::Mutex::Lock lx (AudioEngine::instance()->process_lock ());
3598 Glib::Threads::RWLock::WriterLock lm (_processor_lock);
3599 _pending_meter_point = p;
3600 _meter->emit_configuration_changed();
3601 meter_change (); /* EMIT SIGNAL */
3602 if (set_meter_point_unlocked()) {
3603 processors_changed (RouteProcessorChange (RouteProcessorChange::MeterPointChange, true)); /* EMIT SIGNAL */
3605 processors_changed (RouteProcessorChange (RouteProcessorChange::MeterPointChange, false)); /* EMIT SIGNAL */
3608 _pending_meter_point = p;
3614 __attribute__((annotate("realtime")))
3617 Route::set_meter_point_unlocked ()
3620 /* Caller must hold process and processor write lock */
3621 assert (!AudioEngine::instance()->process_lock().trylock());
3622 Glib::Threads::RWLock::WriterLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
3623 assert (!lm.locked ());
3626 _meter_point = _pending_meter_point;
3628 bool meter_was_visible_to_user = _meter->display_to_user ();
3630 if (!_custom_meter_position_noted) {
3631 maybe_note_meter_position ();
3634 if (_meter_point != MeterCustom) {
3636 _meter->set_display_to_user (false);
3638 setup_invisible_processors ();
3641 _meter->set_display_to_user (true);
3643 /* If we have a previous position for the custom meter, try to put it there */
3644 boost::shared_ptr<Processor> after = _processor_after_last_custom_meter.lock ();
3646 ProcessorList::iterator i = find (_processors.begin(), _processors.end(), after);
3647 if (i != _processors.end ()) {
3648 _processors.remove (_meter);
3649 _processors.insert (i, _meter);
3651 } else {// at end, right before the mains_out/panner
3652 _processors.remove (_meter);
3653 ProcessorList::iterator main = _processors.end();
3654 _processors.insert (--main, _meter);
3658 /* Set up the meter for its new position */
3660 ProcessorList::iterator loc = find (_processors.begin(), _processors.end(), _meter);
3664 if (loc == _processors.begin()) {
3665 m_in = _input->n_ports();
3667 ProcessorList::iterator before = loc;
3669 m_in = (*before)->output_streams ();
3672 _meter->reflect_inputs (m_in);
3674 /* we do not need to reconfigure the processors, because the meter
3675 (a) is always ready to handle processor_max_streams
3676 (b) is always an N-in/N-out processor, and thus moving
3677 it doesn't require any changes to the other processors.
3680 /* these should really be done after releasing the lock
3681 * but all those signals are subscribed to with gui_thread()
3684 return (_meter->display_to_user() != meter_was_visible_to_user);
3688 Route::listen_position_changed ()
3691 Glib::Threads::Mutex::Lock lx (AudioEngine::instance()->process_lock ());
3692 Glib::Threads::RWLock::WriterLock lm (_processor_lock);
3693 ProcessorState pstate (this);
3695 if (configure_processors_unlocked (0, &lm)) {
3696 DEBUG_TRACE (DEBUG::Processors, "---- CONFIGURATION FAILED.\n");
3698 configure_processors_unlocked (0, &lm); // it worked before we tried to add it ...
3703 processors_changed (RouteProcessorChange ()); /* EMIT SIGNAL */
3704 _session.set_dirty ();
3707 boost::shared_ptr<CapturingProcessor>
3708 Route::add_export_point()
3710 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
3711 if (!_capturing_processor) {
3713 Glib::Threads::Mutex::Lock lx (AudioEngine::instance()->process_lock ());
3714 Glib::Threads::RWLock::WriterLock lw (_processor_lock);
3716 // this aligns all tracks; but not tracks + busses
3717 assert (_session.worst_track_latency () >= _initial_delay);
3718 _capturing_processor.reset (new CapturingProcessor (_session, _session.worst_track_latency () - _initial_delay));
3719 _capturing_processor->activate ();
3721 configure_processors_unlocked (0, &lw);
3725 return _capturing_processor;
3729 Route::update_signal_latency ()
3731 framecnt_t l = _output->user_latency();
3732 framecnt_t lamp = 0;
3733 bool before_amp = true;
3734 framecnt_t ltrim = 0;
3735 bool before_trim = true;
3737 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
3738 if ((*i)->active ()) {
3739 l += (*i)->signal_latency ();
3744 if ((*i) == _trim) {
3755 DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: internal signal latency = %2\n", _name, l));
3757 // TODO: (lamp - _signal_latency) to sync to output (read-ahed), currently _roll_delay shifts this around
3758 _signal_latency_at_amp_position = lamp;
3759 _signal_latency_at_trim_position = ltrim;
3761 if (_signal_latency != l) {
3762 _signal_latency = l;
3763 signal_latency_changed (); /* EMIT SIGNAL */
3766 return _signal_latency;
3770 Route::set_user_latency (framecnt_t nframes)
3772 _output->set_user_latency (nframes);
3773 _session.update_latency_compensation ();
3777 Route::set_latency_compensation (framecnt_t longest_session_latency)
3779 framecnt_t old = _initial_delay;
3781 if (_signal_latency < longest_session_latency) {
3782 _initial_delay = longest_session_latency - _signal_latency;
3787 DEBUG_TRACE (DEBUG::Latency, string_compose (
3788 "%1: compensate for maximum latency of %2,"
3789 "given own latency of %3, using initial delay of %4\n",
3790 name(), longest_session_latency, _signal_latency, _initial_delay));
3792 if (_initial_delay != old) {
3793 initial_delay_changed (); /* EMIT SIGNAL */
3796 if (_session.transport_stopped()) {
3797 _roll_delay = _initial_delay;
3802 Route::set_block_size (pframes_t nframes)
3804 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
3805 (*i)->set_block_size (nframes);
3808 _session.ensure_buffers (n_process_buffers ());
3812 Route::protect_automation ()
3814 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i)
3815 (*i)->protect_automation();
3818 /** @param declick 1 to set a pending declick fade-in,
3819 * -1 to set a pending declick fade-out
3822 Route::set_pending_declick (int declick)
3825 /* this call is not allowed to turn off a pending declick */
3827 _pending_declick = declick;
3830 _pending_declick = 0;
3834 /** Shift automation forwards from a particular place, thereby inserting time.
3835 * Adds undo commands for any shifts that are performed.
3837 * @param pos Position to start shifting from.
3838 * @param frames Amount to shift forwards by.
3842 Route::shift (framepos_t pos, framecnt_t frames)
3844 /* gain automation */
3846 boost::shared_ptr<AutomationControl> gc = _amp->gain_control();
3848 XMLNode &before = gc->alist()->get_state ();
3849 gc->alist()->shift (pos, frames);
3850 XMLNode &after = gc->alist()->get_state ();
3851 _session.add_command (new MementoCommand<AutomationList> (*gc->alist().get(), &before, &after));
3854 /* gain automation */
3856 boost::shared_ptr<AutomationControl> gc = _trim->gain_control();
3858 XMLNode &before = gc->alist()->get_state ();
3859 gc->alist()->shift (pos, frames);
3860 XMLNode &after = gc->alist()->get_state ();
3861 _session.add_command (new MementoCommand<AutomationList> (*gc->alist().get(), &before, &after));
3864 // TODO mute automation ??
3866 /* pan automation */
3868 ControlSet::Controls& c (_pannable->controls());
3870 for (ControlSet::Controls::const_iterator ci = c.begin(); ci != c.end(); ++ci) {
3871 boost::shared_ptr<AutomationControl> pc = boost::dynamic_pointer_cast<AutomationControl> (ci->second);
3873 boost::shared_ptr<AutomationList> al = pc->alist();
3874 XMLNode& before = al->get_state ();
3875 al->shift (pos, frames);
3876 XMLNode& after = al->get_state ();
3877 _session.add_command (new MementoCommand<AutomationList> (*al.get(), &before, &after));
3882 /* redirect automation */
3884 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
3885 for (ProcessorList::iterator i = _processors.begin (); i != _processors.end (); ++i) {
3887 set<Evoral::Parameter> parameters = (*i)->what_can_be_automated();
3889 for (set<Evoral::Parameter>::const_iterator p = parameters.begin (); p != parameters.end (); ++p) {
3890 boost::shared_ptr<AutomationControl> ac = (*i)->automation_control (*p);
3892 boost::shared_ptr<AutomationList> al = ac->alist();
3893 XMLNode &before = al->get_state ();
3894 al->shift (pos, frames);
3895 XMLNode &after = al->get_state ();
3896 _session.add_command (new MementoCommand<AutomationList> (*al.get(), &before, &after));
3904 Route::set_plugin_state_dir (boost::weak_ptr<Processor> p, const std::string& d)
3906 boost::shared_ptr<Processor> processor (p.lock ());
3907 boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert> (processor);
3911 pi->set_state_dir (d);
3915 Route::save_as_template (const string& path, const string& name)
3917 std::string state_dir = path.substr (0, path.find_last_of ('.')); // strip template_suffix
3918 PBD::Unwinder<std::string> uw (_session._template_state_dir, state_dir);
3920 XMLNode& node (state (false));
3924 IO::set_name_in_state (*node.children().front(), name);
3926 tree.set_root (&node);
3928 /* return zero on success, non-zero otherwise */
3929 return !tree.write (path.c_str());
3934 Route::set_name (const string& str)
3936 if (str == name()) {
3940 string name = Route::ensure_track_or_route_name (str, _session);
3941 SessionObject::set_name (name);
3943 bool ret = (_input->set_name(name) && _output->set_name(name));
3946 /* rename the main outs. Leave other IO processors
3947 * with whatever name they already have, because its
3948 * just fine as it is (it will not contain the route
3949 * name if its a port insert, port send or port return).
3953 if (_main_outs->set_name (name)) {
3954 /* XXX returning false here is stupid because
3955 we already changed the route name.
3965 /** Set the name of a route in an XML description.
3966 * @param node XML <Route> node to set the name in.
3967 * @param name New name.
3970 Route::set_name_in_state (XMLNode& node, string const & name, bool rename_playlist)
3972 node.add_property (X_("name"), name);
3974 XMLNodeList children = node.children();
3975 for (XMLNodeIterator i = children.begin(); i != children.end(); ++i) {
3977 if ((*i)->name() == X_("IO")) {
3979 IO::set_name_in_state (**i, name);
3981 } else if ((*i)->name() == X_("Processor")) {
3983 XMLProperty const * role = (*i)->property (X_("role"));
3984 if (role && role->value() == X_("Main")) {
3985 (*i)->add_property (X_("name"), name);
3988 } else if ((*i)->name() == X_("Diskstream")) {
3990 if (rename_playlist) {
3991 (*i)->add_property (X_("playlist"), string_compose ("%1.1", name).c_str());
3993 (*i)->add_property (X_("name"), name);
3999 boost::shared_ptr<Send>
4000 Route::internal_send_for (boost::shared_ptr<const Route> target) const
4002 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
4004 for (ProcessorList::const_iterator i = _processors.begin(); i != _processors.end(); ++i) {
4005 boost::shared_ptr<InternalSend> send;
4007 if ((send = boost::dynamic_pointer_cast<InternalSend>(*i)) != 0) {
4008 if (send->target_route() == target) {
4014 return boost::shared_ptr<Send>();
4018 Route::set_denormal_protection (bool yn)
4020 if (_denormal_protection != yn) {
4021 _denormal_protection = yn;
4022 denormal_protection_changed (); /* EMIT SIGNAL */
4027 Route::denormal_protection () const
4029 return _denormal_protection;
4033 Route::set_active (bool yn, void* src)
4035 if (_session.transport_rolling()) {
4039 if (_route_group && src != _route_group && _route_group->is_active() && _route_group->is_route_active()) {
4040 _route_group->foreach_route (boost::bind (&Route::set_active, _1, yn, _route_group));
4044 if (_active != yn) {
4046 _input->set_active (yn);
4047 _output->set_active (yn);
4048 active_changed (); // EMIT SIGNAL
4049 _session.set_dirty ();
4053 boost::shared_ptr<Pannable>
4054 Route::pannable() const
4059 boost::shared_ptr<Panner>
4060 Route::panner() const
4063 return _main_outs->panner_shell()->panner();
4066 boost::shared_ptr<PannerShell>
4067 Route::panner_shell() const
4069 return _main_outs->panner_shell();
4072 boost::shared_ptr<GainControl>
4073 Route::gain_control() const
4075 return _gain_control;
4078 boost::shared_ptr<GainControl>
4079 Route::trim_control() const
4081 return _trim_control;
4084 boost::shared_ptr<PhaseControl>
4085 Route::phase_control() const
4087 return _phase_control;
4090 boost::shared_ptr<AutomationControl>
4091 Route::get_control (const Evoral::Parameter& param)
4093 /* either we own the control or .... */
4095 boost::shared_ptr<AutomationControl> c = boost::dynamic_pointer_cast<AutomationControl>(control (param));
4099 /* maybe one of our processors does or ... */
4101 Glib::Threads::RWLock::ReaderLock rm (_processor_lock);
4102 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
4103 if ((c = boost::dynamic_pointer_cast<AutomationControl>((*i)->control (param))) != 0) {
4111 /* nobody does so we'll make a new one */
4113 c = boost::dynamic_pointer_cast<AutomationControl>(control_factory(param));
4120 boost::shared_ptr<Processor>
4121 Route::nth_plugin (uint32_t n) const
4123 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
4124 ProcessorList::const_iterator i;
4126 for (i = _processors.begin(); i != _processors.end(); ++i) {
4127 if (boost::dynamic_pointer_cast<PluginInsert> (*i)) {
4134 return boost::shared_ptr<Processor> ();
4137 boost::shared_ptr<Processor>
4138 Route::nth_send (uint32_t n) const
4140 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
4141 ProcessorList::const_iterator i;
4143 for (i = _processors.begin(); i != _processors.end(); ++i) {
4144 if (boost::dynamic_pointer_cast<Send> (*i)) {
4146 if ((*i)->name().find (_("Monitor")) == 0) {
4147 /* send to monitor section is not considered
4148 to be an accessible send.
4159 return boost::shared_ptr<Processor> ();
4163 Route::has_io_processor_named (const string& name)
4165 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
4166 ProcessorList::iterator i;
4168 for (i = _processors.begin(); i != _processors.end(); ++i) {
4169 if (boost::dynamic_pointer_cast<Send> (*i) ||
4170 boost::dynamic_pointer_cast<PortInsert> (*i)) {
4171 if ((*i)->name() == name) {
4181 Route::set_processor_positions ()
4183 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
4185 bool had_amp = false;
4186 for (ProcessorList::const_iterator i = _processors.begin(); i != _processors.end(); ++i) {
4187 (*i)->set_pre_fader (!had_amp);
4194 /** Called when there is a proposed change to the input port count */
4196 Route::input_port_count_changing (ChanCount to)
4198 list<pair<ChanCount, ChanCount> > c = try_configure_processors (to, 0);
4200 /* The processors cannot be configured with the new input arrangement, so
4206 /* The change is ok */
4210 /** Called when there is a proposed change to the output port count */
4212 Route::output_port_count_changing (ChanCount to)
4214 if (_strict_io && !_in_configure_processors) {
4217 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
4218 if (processor_out_streams.get(*t) > to.get(*t)) {
4222 /* The change is ok */
4227 Route::unknown_processors () const
4231 if (_session.get_disable_all_loaded_plugins ()) {
4232 // Do not list "missing plugins" if they are explicitly disabled
4236 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
4237 for (ProcessorList::const_iterator i = _processors.begin(); i != _processors.end(); ++i) {
4238 if (boost::dynamic_pointer_cast<UnknownProcessor const> (*i)) {
4239 p.push_back ((*i)->name ());
4248 Route::update_port_latencies (PortSet& from, PortSet& to, bool playback, framecnt_t our_latency) const
4250 /* we assume that all our input ports feed all our output ports. its not
4251 universally true, but the alternative is way too corner-case to worry about.
4254 LatencyRange all_connections;
4257 all_connections.min = 0;
4258 all_connections.max = 0;
4260 all_connections.min = ~((pframes_t) 0);
4261 all_connections.max = 0;
4263 /* iterate over all "from" ports and determine the latency range for all of their
4264 connections to the "outside" (outside of this Route).
4267 for (PortSet::iterator p = from.begin(); p != from.end(); ++p) {
4271 p->get_connected_latency_range (range, playback);
4273 all_connections.min = min (all_connections.min, range.min);
4274 all_connections.max = max (all_connections.max, range.max);
4278 /* set the "from" port latencies to the max/min range of all their connections */
4280 for (PortSet::iterator p = from.begin(); p != from.end(); ++p) {
4281 p->set_private_latency_range (all_connections, playback);
4284 /* set the ports "in the direction of the flow" to the same value as above plus our own signal latency */
4286 all_connections.min += our_latency;
4287 all_connections.max += our_latency;
4289 for (PortSet::iterator p = to.begin(); p != to.end(); ++p) {
4290 p->set_private_latency_range (all_connections, playback);
4293 return all_connections.max;
4297 Route::set_private_port_latencies (bool playback) const
4299 framecnt_t own_latency = 0;
4301 /* Processor list not protected by lock: MUST BE CALLED FROM PROCESS THREAD
4302 OR LATENCY CALLBACK.
4304 This is called (early) from the latency callback. It computes the REAL
4305 latency associated with each port and stores the result as the "private"
4306 latency of the port. A later call to Route::set_public_port_latencies()
4307 sets all ports to the same value to reflect the fact that we do latency
4308 compensation and so all signals are delayed by the same amount as they
4309 flow through ardour.
4312 for (ProcessorList::const_iterator i = _processors.begin(); i != _processors.end(); ++i) {
4313 if ((*i)->active ()) {
4314 own_latency += (*i)->signal_latency ();
4319 /* playback: propagate latency from "outside the route" to outputs to inputs */
4320 return update_port_latencies (_output->ports (), _input->ports (), true, own_latency);
4322 /* capture: propagate latency from "outside the route" to inputs to outputs */
4323 return update_port_latencies (_input->ports (), _output->ports (), false, own_latency);
4328 Route::set_public_port_latencies (framecnt_t value, bool playback) const
4330 /* this is called to set the JACK-visible port latencies, which take
4331 latency compensation into account.
4340 const PortSet& ports (_input->ports());
4341 for (PortSet::const_iterator p = ports.begin(); p != ports.end(); ++p) {
4342 p->set_public_latency_range (range, playback);
4347 const PortSet& ports (_output->ports());
4348 for (PortSet::const_iterator p = ports.begin(); p != ports.end(); ++p) {
4349 p->set_public_latency_range (range, playback);
4354 /** Put the invisible processors in the right place in _processors.
4355 * Must be called with a writer lock on _processor_lock held.
4358 __attribute__((annotate("realtime")))
4361 Route::setup_invisible_processors ()
4364 Glib::Threads::RWLock::WriterLock lm (_processor_lock, Glib::Threads::TRY_LOCK);
4365 assert (!lm.locked ());
4369 /* too early to be doing this stuff */
4373 /* we'll build this new list here and then use it
4375 * TODO put the ProcessorList is on the stack for RT-safety.
4378 ProcessorList new_processors;
4380 /* find visible processors */
4382 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
4383 if ((*i)->display_to_user ()) {
4384 new_processors.push_back (*i);
4390 ProcessorList::iterator amp = find (new_processors.begin(), new_processors.end(), _amp);
4392 if (amp == new_processors.end ()) {
4393 error << string_compose (_("Amp/Fader on Route '%1' went AWOL. Re-added."), name()) << endmsg;
4394 new_processors.push_front (_amp);
4395 amp = find (new_processors.begin(), new_processors.end(), _amp);
4398 /* and the processor after the amp */
4400 ProcessorList::iterator after_amp = amp;
4406 switch (_meter_point) {
4408 assert (!_meter->display_to_user ());
4409 new_processors.push_front (_meter);
4412 assert (!_meter->display_to_user ());
4413 new_processors.insert (amp, _meter);
4415 case MeterPostFader:
4416 /* do nothing here */
4419 /* do nothing here */
4422 /* the meter is visible, so we don't touch it here */
4429 assert (_main_outs);
4430 assert (!_main_outs->display_to_user ());
4431 new_processors.push_back (_main_outs);
4433 /* iterator for the main outs */
4435 ProcessorList::iterator main = new_processors.end();
4438 /* OUTPUT METERING */
4440 if (_meter && (_meter_point == MeterOutput || _meter_point == MeterPostFader)) {
4441 assert (!_meter->display_to_user ());
4443 /* add the processor just before or just after the main outs */
4445 ProcessorList::iterator meter_point = main;
4447 if (_meter_point == MeterOutput) {
4450 new_processors.insert (meter_point, _meter);
4455 if (_monitor_send && !is_monitor ()) {
4456 assert (!_monitor_send->display_to_user ());
4457 switch (Config->get_listen_position ()) {
4458 case PreFaderListen:
4459 switch (Config->get_pfl_position ()) {
4460 case PFLFromBeforeProcessors:
4461 new_processors.push_front (_monitor_send);
4463 case PFLFromAfterProcessors:
4464 new_processors.insert (amp, _monitor_send);
4467 _monitor_send->set_can_pan (false);
4469 case AfterFaderListen:
4470 switch (Config->get_afl_position ()) {
4471 case AFLFromBeforeProcessors:
4472 new_processors.insert (after_amp, _monitor_send);
4474 case AFLFromAfterProcessors:
4475 new_processors.insert (new_processors.end(), _monitor_send);
4478 _monitor_send->set_can_pan (true);
4483 #if 0 // not used - just yet
4484 if (!is_master() && !is_monitor() && !is_auditioner()) {
4485 new_processors.push_front (_delayline);
4489 /* MONITOR CONTROL */
4491 if (_monitor_control && is_monitor ()) {
4492 assert (!_monitor_control->display_to_user ());
4493 new_processors.insert (amp, _monitor_control);
4498 if (_trim && _trim->active()) {
4499 assert (!_trim->display_to_user ());
4500 new_processors.push_front (_trim);
4503 /* INTERNAL RETURN */
4505 /* doing this here means that any monitor control will come after
4506 the return and trim.
4510 assert (!_intreturn->display_to_user ());
4511 new_processors.push_front (_intreturn);
4514 /* EXPORT PROCESSOR */
4516 if (_capturing_processor) {
4517 assert (!_capturing_processor->display_to_user ());
4518 new_processors.push_front (_capturing_processor);
4521 _processors = new_processors;
4523 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
4524 if (!(*i)->display_to_user () && !(*i)->enabled () && (*i) != _monitor_send) {
4525 (*i)->enable (true);
4529 DEBUG_TRACE (DEBUG::Processors, string_compose ("%1: setup_invisible_processors\n", _name));
4530 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
4531 DEBUG_TRACE (DEBUG::Processors, string_compose ("\t%1\n", (*i)->name ()));
4538 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
4539 Glib::Threads::RWLock::ReaderLock lp (_processor_lock);
4543 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
4544 boost::shared_ptr<Delivery> d = boost::dynamic_pointer_cast<Delivery>(*i);
4551 /** If the meter point is `Custom', make a note of where the meter is.
4552 * This is so that if the meter point is subsequently set to something else,
4553 * and then back to custom, we can put the meter back where it was last time
4554 * custom was enabled.
4556 * Must be called with the _processor_lock held.
4559 Route::maybe_note_meter_position ()
4561 if (_meter_point != MeterCustom) {
4565 _custom_meter_position_noted = true;
4566 /* custom meter points range from after trim to before panner/main_outs
4567 * this is a limitation by the current processor UI
4569 bool seen_trim = false;
4570 _processor_after_last_custom_meter.reset();
4571 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
4572 if ((*i) == _trim) {
4575 if ((*i) == _main_outs) {
4576 _processor_after_last_custom_meter = *i;
4579 if (boost::dynamic_pointer_cast<PeakMeter> (*i)) {
4581 _processor_after_last_custom_meter = _trim;
4583 ProcessorList::iterator j = i;
4585 assert(j != _processors.end ()); // main_outs should be before
4586 _processor_after_last_custom_meter = *j;
4591 assert(_processor_after_last_custom_meter.lock());
4594 boost::shared_ptr<Processor>
4595 Route::processor_by_id (PBD::ID id) const
4597 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
4598 for (ProcessorList::const_iterator i = _processors.begin(); i != _processors.end(); ++i) {
4599 if ((*i)->id() == id) {
4604 return boost::shared_ptr<Processor> ();
4607 /** @return the monitoring state, or in other words what data we are pushing
4608 * into the route (data from the inputs, data from disk or silence)
4611 Route::monitoring_state () const
4613 return MonitoringInput;
4616 /** @return what we should be metering; either the data coming from the input
4617 * IO or the data that is flowing through the route.
4620 Route::metering_state () const
4622 return MeteringRoute;
4626 Route::has_external_redirects () const
4628 for (ProcessorList::const_iterator i = _processors.begin(); i != _processors.end(); ++i) {
4630 /* ignore inactive processors and obviously ignore the main
4631 * outs since everything has them and we don't care.
4634 if ((*i)->active() && (*i) != _main_outs && (*i)->does_routing()) {
4642 boost::shared_ptr<Processor>
4643 Route::the_instrument () const
4645 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
4646 return the_instrument_unlocked ();
4649 boost::shared_ptr<Processor>
4650 Route::the_instrument_unlocked () const
4652 for (ProcessorList::const_iterator i = _processors.begin(); i != _processors.end(); ++i) {
4653 boost::shared_ptr<PluginInsert> pi = boost::dynamic_pointer_cast<PluginInsert>(*i);
4654 if (pi && pi->plugin ()->get_info ()->is_instrument ()) {
4658 return boost::shared_ptr<Processor>();
4664 Route::non_realtime_locate (framepos_t pos)
4667 _pannable->transport_located (pos);
4670 if (_delayline.get()) {
4671 _delayline.get()->flush();
4675 //Glib::Threads::Mutex::Lock lx (AudioEngine::instance()->process_lock ());
4676 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
4678 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
4679 (*i)->transport_located (pos);
4682 _roll_delay = _initial_delay;
4686 Route::fill_buffers_with_input (BufferSet& bufs, boost::shared_ptr<IO> io, pframes_t nframes)
4693 * We don't currently mix MIDI input together, so we don't need the
4694 * complex logic of the audio case.
4697 n_buffers = bufs.count().n_midi ();
4699 for (i = 0; i < n_buffers; ++i) {
4701 boost::shared_ptr<MidiPort> source_port = io->midi (i);
4702 MidiBuffer& buf (bufs.get_midi (i));
4705 buf.copy (source_port->get_midi_buffer(nframes));
4707 buf.silence (nframes);
4713 n_buffers = bufs.count().n_audio();
4715 size_t n_ports = io->n_ports().n_audio();
4716 float scaling = 1.0f;
4718 if (n_ports > n_buffers) {
4719 scaling = ((float) n_buffers) / n_ports;
4722 for (i = 0; i < n_ports; ++i) {
4724 /* if there are more ports than buffers, map them onto buffers
4725 * in a round-robin fashion
4728 boost::shared_ptr<AudioPort> source_port = io->audio (i);
4729 AudioBuffer& buf (bufs.get_audio (i%n_buffers));
4732 if (i < n_buffers) {
4734 /* first time through just copy a channel into
4738 buf.read_from (source_port->get_audio_buffer (nframes), nframes);
4740 if (scaling != 1.0f) {
4741 buf.apply_gain (scaling, nframes);
4746 /* on subsequent times around, merge data from
4747 * the port with what is already there
4750 if (scaling != 1.0f) {
4751 buf.accumulate_with_gain_from (source_port->get_audio_buffer (nframes), nframes, 0, scaling);
4753 buf.accumulate_from (source_port->get_audio_buffer (nframes), nframes);
4758 /* silence any remaining buffers */
4760 for (; i < n_buffers; ++i) {
4761 AudioBuffer& buf (bufs.get_audio (i));
4762 buf.silence (nframes);
4765 /* establish the initial setup of the buffer set, reflecting what was
4766 copied into it. unless, of course, we are the auditioner, in which
4767 case nothing was fed into it from the inputs at all.
4770 if (!is_auditioner()) {
4771 bufs.set_count (io->n_ports());
4775 boost::shared_ptr<AutomationControl>
4776 Route::pan_azimuth_control() const
4779 boost::shared_ptr<ARDOUR::PluginInsert> plug = ch_post();
4781 return boost::shared_ptr<AutomationControl>();
4783 const uint32_t port_channel_post_pan = 2; // gtk2_ardour/mixbus_ports.h
4784 return boost::dynamic_pointer_cast<ARDOUR::AutomationControl> (plug->control (Evoral::Parameter (ARDOUR::PluginAutomation, 0, port_channel_post_pan)));
4786 if (!_pannable || !panner()) {
4787 return boost::shared_ptr<AutomationControl>();
4789 return _pannable->pan_azimuth_control;
4793 boost::shared_ptr<AutomationControl>
4794 Route::pan_elevation_control() const
4796 if (Profile->get_mixbus() || !_pannable || !panner()) {
4797 return boost::shared_ptr<AutomationControl>();
4800 set<Evoral::Parameter> c = panner()->what_can_be_automated ();
4802 if (c.find (PanElevationAutomation) != c.end()) {
4803 return _pannable->pan_elevation_control;
4805 return boost::shared_ptr<AutomationControl>();
4808 boost::shared_ptr<AutomationControl>
4809 Route::pan_width_control() const
4811 if (Profile->get_mixbus() || !_pannable || !panner()) {
4812 return boost::shared_ptr<AutomationControl>();
4815 set<Evoral::Parameter> c = panner()->what_can_be_automated ();
4817 if (c.find (PanWidthAutomation) != c.end()) {
4818 return _pannable->pan_width_control;
4820 return boost::shared_ptr<AutomationControl>();
4823 boost::shared_ptr<AutomationControl>
4824 Route::pan_frontback_control() const
4826 if (Profile->get_mixbus() || !_pannable || !panner()) {
4827 return boost::shared_ptr<AutomationControl>();
4830 set<Evoral::Parameter> c = panner()->what_can_be_automated ();
4832 if (c.find (PanFrontBackAutomation) != c.end()) {
4833 return _pannable->pan_frontback_control;
4835 return boost::shared_ptr<AutomationControl>();
4838 boost::shared_ptr<AutomationControl>
4839 Route::pan_lfe_control() const
4841 if (Profile->get_mixbus() || !_pannable || !panner()) {
4842 return boost::shared_ptr<AutomationControl>();
4845 set<Evoral::Parameter> c = panner()->what_can_be_automated ();
4847 if (c.find (PanLFEAutomation) != c.end()) {
4848 return _pannable->pan_lfe_control;
4850 return boost::shared_ptr<AutomationControl>();
4855 Route::eq_band_cnt () const
4857 if (Profile->get_mixbus()) {
4860 /* Ardour has no well-known EQ object */
4865 boost::shared_ptr<AutomationControl>
4866 Route::eq_gain_controllable (uint32_t band) const
4869 boost::shared_ptr<PluginInsert> eq = ch_eq();
4872 return boost::shared_ptr<AutomationControl>();
4875 uint32_t port_number;
4878 if (is_master() || mixbus()) {
4885 if (is_master() || mixbus()) {
4892 if (is_master() || mixbus()) {
4899 return boost::shared_ptr<AutomationControl>();
4902 return boost::dynamic_pointer_cast<ARDOUR::AutomationControl> (eq->control (Evoral::Parameter (ARDOUR::PluginAutomation, 0, port_number)));
4904 return boost::shared_ptr<AutomationControl>();
4907 boost::shared_ptr<AutomationControl>
4908 Route::eq_freq_controllable (uint32_t band) const
4912 if (mixbus() || is_master()) {
4913 /* no frequency controls for mixbusses or master */
4914 return boost::shared_ptr<AutomationControl>();
4917 boost::shared_ptr<PluginInsert> eq = ch_eq();
4920 return boost::shared_ptr<AutomationControl>();
4923 uint32_t port_number;
4935 return boost::shared_ptr<AutomationControl>();
4938 return boost::dynamic_pointer_cast<ARDOUR::AutomationControl> (eq->control (Evoral::Parameter (ARDOUR::PluginAutomation, 0, port_number)));
4940 return boost::shared_ptr<AutomationControl>();
4944 boost::shared_ptr<AutomationControl>
4945 Route::eq_q_controllable (uint32_t band) const
4947 return boost::shared_ptr<AutomationControl>();
4950 boost::shared_ptr<AutomationControl>
4951 Route::eq_shape_controllable (uint32_t band) const
4953 return boost::shared_ptr<AutomationControl>();
4956 boost::shared_ptr<AutomationControl>
4957 Route::eq_enable_controllable () const
4960 boost::shared_ptr<PluginInsert> eq = ch_eq();
4963 return boost::shared_ptr<AutomationControl>();
4966 return boost::dynamic_pointer_cast<ARDOUR::AutomationControl> (eq->control (Evoral::Parameter (ARDOUR::PluginAutomation, 0, 1)));
4968 return boost::shared_ptr<AutomationControl>();
4972 boost::shared_ptr<AutomationControl>
4973 Route::eq_hpf_controllable () const
4976 boost::shared_ptr<PluginInsert> eq = ch_eq();
4979 return boost::shared_ptr<AutomationControl>();
4982 return boost::dynamic_pointer_cast<ARDOUR::AutomationControl> (eq->control (Evoral::Parameter (ARDOUR::PluginAutomation, 0, 2)));
4984 return boost::shared_ptr<AutomationControl>();
4989 Route::eq_band_name (uint32_t band) const
4991 if (Profile->get_mixbus()) {
5007 boost::shared_ptr<AutomationControl>
5008 Route::comp_enable_controllable () const
5011 boost::shared_ptr<PluginInsert> comp = ch_comp();
5014 return boost::shared_ptr<AutomationControl>();
5017 return boost::dynamic_pointer_cast<ARDOUR::AutomationControl> (comp->control (Evoral::Parameter (ARDOUR::PluginAutomation, 0, 1)));
5019 return boost::shared_ptr<AutomationControl>();
5022 boost::shared_ptr<AutomationControl>
5023 Route::comp_threshold_controllable () const
5026 boost::shared_ptr<PluginInsert> comp = ch_comp();
5029 return boost::shared_ptr<AutomationControl>();
5032 return boost::dynamic_pointer_cast<ARDOUR::AutomationControl> (comp->control (Evoral::Parameter (ARDOUR::PluginAutomation, 0, 2)));
5035 return boost::shared_ptr<AutomationControl>();
5038 boost::shared_ptr<AutomationControl>
5039 Route::comp_speed_controllable () const
5042 boost::shared_ptr<PluginInsert> comp = ch_comp();
5045 return boost::shared_ptr<AutomationControl>();
5048 return boost::dynamic_pointer_cast<ARDOUR::AutomationControl> (comp->control (Evoral::Parameter (ARDOUR::PluginAutomation, 0, 3)));
5050 return boost::shared_ptr<AutomationControl>();
5053 boost::shared_ptr<AutomationControl>
5054 Route::comp_mode_controllable () const
5057 boost::shared_ptr<PluginInsert> comp = ch_comp();
5060 return boost::shared_ptr<AutomationControl>();
5063 return boost::dynamic_pointer_cast<ARDOUR::AutomationControl> (comp->control (Evoral::Parameter (ARDOUR::PluginAutomation, 0, 4)));
5065 return boost::shared_ptr<AutomationControl>();
5068 boost::shared_ptr<AutomationControl>
5069 Route::comp_makeup_controllable () const
5072 boost::shared_ptr<PluginInsert> comp = ch_comp();
5075 return boost::shared_ptr<AutomationControl>();
5078 return boost::dynamic_pointer_cast<ARDOUR::AutomationControl> (comp->control (Evoral::Parameter (ARDOUR::PluginAutomation, 0, 5)));
5080 return boost::shared_ptr<AutomationControl>();
5083 boost::shared_ptr<AutomationControl>
5084 Route::comp_redux_controllable () const
5087 boost::shared_ptr<PluginInsert> comp = ch_comp();
5090 return boost::shared_ptr<AutomationControl>();
5093 return boost::dynamic_pointer_cast<ARDOUR::AutomationControl> (comp->control (Evoral::Parameter (ARDOUR::PluginAutomation, 0, 6)));
5095 return boost::shared_ptr<AutomationControl>();
5100 Route::comp_mode_name (uint32_t mode) const
5105 return _("Leveler");
5107 return _("Compressor");
5109 return _("Limiter");
5111 return mixbus() ? _("Sidechain") : _("Limiter");
5121 Route::comp_speed_name (uint32_t mode) const
5139 boost::shared_ptr<AutomationControl>
5140 Route::send_level_controllable (uint32_t n) const
5143 boost::shared_ptr<ARDOUR::PluginInsert> plug = ch_post();
5145 return boost::shared_ptr<AutomationControl>();
5150 return boost::shared_ptr<AutomationControl>();
5153 const uint32_t port_id = port_channel_post_aux1_level + (2*n); // gtk2_ardour/mixbus_ports.h
5154 return boost::dynamic_pointer_cast<ARDOUR::AutomationControl> (plug->control (Evoral::Parameter (ARDOUR::PluginAutomation, 0, port_id)));
5156 boost::shared_ptr<Send> s = boost::dynamic_pointer_cast<Send>(nth_send (n));
5158 return boost::shared_ptr<AutomationControl>();
5160 return s->gain_control ();
5164 boost::shared_ptr<AutomationControl>
5165 Route::send_enable_controllable (uint32_t n) const
5168 boost::shared_ptr<ARDOUR::PluginInsert> plug = ch_post();
5170 return boost::shared_ptr<AutomationControl>();
5175 return boost::shared_ptr<AutomationControl>();
5178 const uint32_t port_id = port_channel_post_aux1_asgn + (2*n); // gtk2_ardour/mixbus_ports.h
5179 return boost::dynamic_pointer_cast<ARDOUR::AutomationControl> (plug->control (Evoral::Parameter (ARDOUR::PluginAutomation, 0, port_id)));
5181 /* although Ardour sends have enable/disable as part of the Processor
5182 API, it is not exposed as a controllable.
5184 XXX: we should fix this.
5186 return boost::shared_ptr<AutomationControl>();
5191 Route::send_name (uint32_t n) const
5197 boost::shared_ptr<Route> r = _session.get_mixbus (n);
5201 boost::shared_ptr<Processor> p = nth_send (n);
5210 boost::shared_ptr<AutomationControl>
5211 Route::master_send_enable_controllable () const
5214 boost::shared_ptr<ARDOUR::PluginInsert> plug = ch_post();
5216 return boost::shared_ptr<AutomationControl>();
5218 return boost::dynamic_pointer_cast<ARDOUR::AutomationControl> (plug->control (Evoral::Parameter (ARDOUR::PluginAutomation, 0, port_channel_post_mstr_assign)));
5220 return boost::shared_ptr<AutomationControl>();
5225 Route::slaved () const
5227 if (!_gain_control) {
5230 /* just test one particular control, not all of them */
5231 return _gain_control->slaved ();
5235 Route::slaved_to (boost::shared_ptr<VCA> vca) const
5237 if (!vca || !_gain_control) {
5241 /* just test one particular control, not all of them */
5243 return _gain_control->slaved_to (vca->gain_control());
5247 Route::muted_by_others_soloing () const
5249 if (!can_be_muted_by_others ()) {
5253 return _session.soloing() && !_solo_control->soloed() && !_solo_isolate_control->solo_isolated();
5257 Route::clear_all_solo_state ()
5259 double v = _solo_safe_control->get_value ();
5261 _solo_control->clear_all_solo_state ();
5264 _solo_safe_control->set_value (v, Controllable::NoGroup);