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.
25 #include <sigc++/bind.h>
26 #include "pbd/xml++.h"
27 #include "pbd/enumwriter.h"
28 #include "pbd/stacktrace.h"
29 #include "pbd/memento_command.h"
31 #include "evoral/Curve.hpp"
33 #include "ardour/amp.h"
34 #include "ardour/audio_port.h"
35 #include "ardour/audioengine.h"
36 #include "ardour/buffer.h"
37 #include "ardour/buffer_set.h"
38 #include "ardour/configuration.h"
39 #include "ardour/cycle_timer.h"
40 #include "ardour/delivery.h"
41 #include "ardour/dB.h"
42 #include "ardour/internal_send.h"
43 #include "ardour/internal_return.h"
44 #include "ardour/ladspa_plugin.h"
45 #include "ardour/meter.h"
46 #include "ardour/mix.h"
47 #include "ardour/panner.h"
48 #include "ardour/plugin_insert.h"
49 #include "ardour/port.h"
50 #include "ardour/port_insert.h"
51 #include "ardour/processor.h"
52 #include "ardour/profile.h"
53 #include "ardour/route.h"
54 #include "ardour/route_group.h"
55 #include "ardour/send.h"
56 #include "ardour/session.h"
57 #include "ardour/timestamps.h"
58 #include "ardour/utils.h"
63 using namespace ARDOUR;
66 uint32_t Route::order_key_cnt = 0;
67 sigc::signal<void, string const &> Route::SyncOrderKeys;
69 Route::Route (Session& sess, string name, Flag flg, DataType default_type)
70 : SessionObject (sess, name)
71 , AutomatableControls (sess)
73 , _solo_control (new SoloControllable (X_("solo"), *this))
74 , _mute_master (new MuteMaster (sess, name))
75 , _default_type (default_type)
80 /* add standard processors other than amp (added by ::init()) */
82 _meter.reset (new PeakMeter (_session));
83 add_processor (_meter, PreFader);
85 if (_flags & ControlOut) {
86 /* where we listen to tracks */
87 _intreturn.reset (new InternalReturn (_session));
88 add_processor (_intreturn, PreFader);
91 _main_outs.reset (new Delivery (_session, _output, _mute_master, _name, Delivery::Main));
92 add_processor (_main_outs, PostFader);
94 /* now that we have _meter, its safe to connect to this */
96 _meter_connection = Metering::connect (mem_fun (*this, &Route::meter));
99 Route::Route (Session& sess, const XMLNode& node, DataType default_type)
100 : SessionObject (sess, "toBeReset")
101 , AutomatableControls (sess)
102 , _solo_control (new SoloControllable (X_("solo"), *this))
103 , _mute_master (new MuteMaster (sess, "toBeReset"))
104 , _default_type (default_type)
108 _set_state (node, Stateful::loading_state_version, false);
110 /* now that we have _meter, its safe to connect to this */
112 _meter_connection = Metering::connect (mem_fun (*this, &Route::meter));
119 _solo_isolated = false;
121 processor_max_streams.reset();
124 order_keys[N_("signal")] = order_key_cnt++;
126 _meter_point = MeterPostFader;
129 _have_internal_generator = false;
130 _declickable = false;
131 _pending_declick = true;
132 _remote_control_id = 0;
133 _in_configure_processors = false;
138 _denormal_protection = false;
140 /* add standard controls */
142 add_control (_solo_control);
143 add_control (_mute_master);
145 /* input and output objects */
147 _input.reset (new IO (_session, _name, IO::Input, _default_type));
148 _output.reset (new IO (_session, _name, IO::Output, _default_type));
150 _input->changed.connect (mem_fun (this, &Route::input_change_handler));
151 _output->changed.connect (mem_fun (this, &Route::output_change_handler));
153 /* add amp processor */
155 _amp.reset (new Amp (_session, _mute_master));
156 add_processor (_amp, PostFader);
161 Metering::disconnect (_meter_connection);
163 clear_processors (PreFader);
164 clear_processors (PostFader);
168 Route::set_remote_control_id (uint32_t id)
170 if (id != _remote_control_id) {
171 _remote_control_id = id;
172 RemoteControlIDChanged ();
177 Route::remote_control_id() const
179 return _remote_control_id;
183 Route::order_key (std::string const & name) const
185 OrderKeys::const_iterator i = order_keys.find (name);
186 if (i == order_keys.end()) {
194 Route::set_order_key (std::string const & name, long n)
196 order_keys[name] = n;
198 if (Config->get_sync_all_route_ordering()) {
199 for (OrderKeys::iterator x = order_keys.begin(); x != order_keys.end(); ++x) {
204 _session.set_dirty ();
207 /** Set all order keys to be the same as that for `base', if such a key
208 * exists in this route.
209 * @param base Base key.
212 Route::sync_order_keys (std::string const & base)
214 if (order_keys.empty()) {
218 OrderKeys::iterator i;
221 if ((i = order_keys.find (base)) == order_keys.end()) {
222 /* key doesn't exist, use the first existing key (during session initialization) */
223 i = order_keys.begin();
227 /* key exists - use it and reset all others (actually, itself included) */
229 i = order_keys.begin();
232 for (; i != order_keys.end(); ++i) {
238 Route::ensure_track_or_route_name(string name, Session &session)
240 string newname = name;
242 while (session.route_by_name (newname) != NULL) {
243 newname = bump_name_once (newname);
251 Route::inc_gain (gain_t fraction, void *src)
253 _amp->inc_gain (fraction, src);
257 Route::set_gain (gain_t val, void *src)
259 if (src != 0 && _route_group && src != _route_group && _route_group->active_property (RouteGroup::Gain)) {
261 if (_route_group->is_relative()) {
263 gain_t usable_gain = _amp->gain();
264 if (usable_gain < 0.000001f) {
265 usable_gain = 0.000001f;
269 if (delta < 0.000001f) {
273 delta -= usable_gain;
278 gain_t factor = delta / usable_gain;
281 factor = _route_group->get_max_factor(factor);
282 if (factor == 0.0f) {
283 _amp->gain_control()->Changed(); /* EMIT SIGNAL */
287 factor = _route_group->get_min_factor(factor);
288 if (factor == 0.0f) {
289 _amp->gain_control()->Changed(); /* EMIT SIGNAL */
294 _route_group->apply (&Route::inc_gain, factor, _route_group);
298 _route_group->apply (&Route::set_gain, val, _route_group);
304 if (val == _amp->gain()) {
308 _amp->set_gain (val, src);
311 /** Process this route for one (sub) cycle (process thread)
313 * @param bufs Scratch buffers to use for the signal path
314 * @param start_frame Initial transport frame
315 * @param end_frame Final transport frame
316 * @param nframes Number of frames to output (to ports)
318 * Note that (end_frame - start_frame) may not be equal to nframes when the
319 * transport speed isn't 1.0 (eg varispeed).
322 Route::process_output_buffers (BufferSet& bufs,
323 sframes_t start_frame, sframes_t end_frame, nframes_t nframes,
324 bool /*with_processors*/, int declick)
328 bufs.is_silent (false);
330 switch (Config->get_monitoring_model()) {
331 case HardwareMonitoring:
332 case ExternalMonitoring:
333 monitor = !record_enabled() || (_session.config.get_auto_input() && !_session.actively_recording());
340 declick = _pending_declick;
343 /* figure out if we're going to use gain automation */
344 _amp->setup_gain_automation (start_frame, end_frame, nframes);
347 /* tell main outs what to do about monitoring */
348 _main_outs->no_outs_cuz_we_no_monitor (!monitor);
351 /* -------------------------------------------------------------------------------------------
352 GLOBAL DECLICK (for transport changes etc.)
353 ----------------------------------------------------------------------------------------- */
356 Amp::apply_gain (bufs, nframes, 0.0, 1.0);
357 } else if (declick < 0) {
358 Amp::apply_gain (bufs, nframes, 1.0, 0.0);
361 _pending_declick = 0;
363 /* -------------------------------------------------------------------------------------------
364 DENORMAL CONTROL/PHASE INVERT
365 ----------------------------------------------------------------------------------------- */
371 if (_denormal_protection || Config->get_denormal_protection()) {
373 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i, ++chn) {
374 Sample* const sp = i->data();
376 if (_phase_invert & chn) {
377 for (nframes_t nx = 0; nx < nframes; ++nx) {
382 for (nframes_t nx = 0; nx < nframes; ++nx) {
390 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i, ++chn) {
391 Sample* const sp = i->data();
393 if (_phase_invert & chn) {
394 for (nframes_t nx = 0; nx < nframes; ++nx) {
403 if (_denormal_protection || Config->get_denormal_protection()) {
405 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
406 Sample* const sp = i->data();
407 for (nframes_t nx = 0; nx < nframes; ++nx) {
415 /* -------------------------------------------------------------------------------------------
417 ----------------------------------------------------------------------------------------- */
419 Glib::RWLock::ReaderLock rm (_processor_lock, Glib::TRY_LOCK);
422 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
423 if (bufs.count() != (*i)->input_streams()) {
424 cerr << _name << " bufs = " << bufs.count()
425 << " input for " << (*i)->name() << " = " << (*i)->input_streams()
428 assert (bufs.count() == (*i)->input_streams());
429 (*i)->run (bufs, start_frame, end_frame, nframes);
430 bufs.set_count (ChanCount::max(bufs.count(), (*i)->output_streams()));
433 if (!_processors.empty()) {
434 bufs.set_count (ChanCount::max (bufs.count(), _processors.back()->output_streams()));
440 Route::n_process_buffers ()
442 return max (_input->n_ports(), processor_max_streams);
446 Route::passthru (sframes_t start_frame, sframes_t end_frame, nframes_t nframes, int declick)
448 BufferSet& bufs = _session.get_scratch_buffers (n_process_buffers());
452 assert (bufs.available() >= _input->n_ports());
454 if (_input->n_ports() == ChanCount::ZERO) {
458 bufs.set_count (_input->n_ports());
460 if (is_control() && _session.listening()) {
462 /* control/monitor bus ignores input ports when something is
463 feeding the listen "stream". data will "arrive" into the
464 route from the intreturn processor element.
467 bufs.silence (nframes, 0);
471 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
473 BufferSet::iterator o = bufs.begin(*t);
474 PortSet& ports (_input->ports());
476 for (PortSet::iterator i = ports.begin(*t); i != ports.end(*t); ++i, ++o) {
477 o->read_from (i->get_buffer(nframes), nframes);
482 write_out_of_band_data (bufs, start_frame, end_frame, nframes);
483 process_output_buffers (bufs, start_frame, end_frame, nframes, true, declick);
487 Route::passthru_silence (sframes_t start_frame, sframes_t end_frame, nframes_t nframes, int declick)
489 BufferSet& bufs (_session.get_silent_buffers (n_process_buffers()));
490 bufs.set_count (_input->n_ports());
491 write_out_of_band_data (bufs, start_frame, end_frame, nframes);
492 process_output_buffers (bufs, start_frame, end_frame, nframes, true, declick);
496 Route::set_listen (bool yn, void* src)
499 if (yn != _control_outs->active()) {
501 _control_outs->activate ();
503 _control_outs->deactivate ();
506 listen_changed (src); /* EMIT SIGNAL */
512 Route::listening () const
515 return _control_outs->active ();
522 Route::set_solo (bool yn, void *src)
524 if (_solo_safe || _solo_isolated) {
528 if (_route_group && src != _route_group && _route_group->active_property (RouteGroup::Solo)) {
529 _route_group->apply (&Route::set_solo, yn, _route_group);
533 if (soloed() != yn) {
534 mod_solo_level (yn ? 1 : -1);
535 solo_changed (src); /* EMIT SIGNAL */
536 _solo_control->Changed (); /* EMIT SIGNAL */
541 Route::mod_solo_level (int32_t delta)
544 if (_solo_level >= (uint32_t) delta) {
545 _solo_level += delta;
550 _solo_level += delta;
553 /* tell main outs what the solo situation is
556 _main_outs->set_solo_level (_solo_level);
557 _main_outs->set_solo_isolated (_solo_isolated);
561 Route::set_solo_isolated (bool yn, void *src)
563 if (_route_group && src != _route_group && _route_group->active_property (RouteGroup::Solo)) {
564 _route_group->apply (&Route::set_solo_isolated, yn, _route_group);
568 if (yn != _solo_isolated) {
571 /* tell main outs what the solo situation is
574 _main_outs->set_solo_level (_solo_level);
575 _main_outs->set_solo_isolated (_solo_isolated);
577 solo_isolated_changed (src);
582 Route::solo_isolated () const
584 return _solo_isolated;
588 Route::set_mute (bool yn, void *src)
590 if (_route_group && src != _route_group && _route_group->active_property (RouteGroup::Mute)) {
591 _route_group->apply (&Route::set_mute, yn, _route_group);
596 _mute_master->mute (yn);
604 return _mute_master->muted ();
609 dump_processors(const string& name, const list<boost::shared_ptr<Processor> >& procs)
611 cerr << name << " {" << endl;
612 for (list<boost::shared_ptr<Processor> >::const_iterator p = procs.begin();
613 p != procs.end(); ++p) {
614 cerr << "\t" << (*p)->name() << " ID = " << (*p)->id() << endl;
621 Route::add_processor (boost::shared_ptr<Processor> processor, Placement placement, ProcessorStreams* err)
623 ProcessorList::iterator loc;
625 /* XXX this is not thread safe - we don't hold the lock across determining the iter
626 to add before and actually doing the insertion. dammit.
629 if (placement == PreFader) {
630 /* generic pre-fader: insert immediately before the amp */
631 loc = find (_processors.begin(), _processors.end(), _amp);
633 /* generic post-fader: insert right before the main outs */
634 loc = find (_processors.begin(), _processors.end(), _main_outs);
637 return add_processor (processor, loc, err);
641 /** Add a processor to the route.
642 * If @a iter is not NULL, it must point to an iterator in _processors and the new
643 * processor will be inserted immediately before this location. Otherwise,
644 * @a position is used.
647 Route::add_processor (boost::shared_ptr<Processor> processor, ProcessorList::iterator iter, ProcessorStreams* err)
649 ChanCount old_pms = processor_max_streams;
651 if (!_session.engine().connected() || !processor) {
656 Glib::RWLock::WriterLock lm (_processor_lock);
658 boost::shared_ptr<PluginInsert> pi;
659 boost::shared_ptr<PortInsert> porti;
661 ProcessorList::iterator loc = find(_processors.begin(), _processors.end(), processor);
663 if (processor == _amp || processor == _meter || processor == _main_outs) {
664 // Ensure only one of these are in the list at any time
665 if (loc != _processors.end()) {
666 if (iter == loc) { // Already in place, do nothing
668 } else { // New position given, relocate
669 _processors.erase (loc);
674 if (loc != _processors.end()) {
675 cerr << "ERROR: Processor added to route twice!" << endl;
682 _processors.insert (loc, processor);
684 // Set up processor list channels. This will set processor->[input|output]_streams(),
685 // configure redirect ports properly, etc.
687 if (configure_processors_unlocked (err)) {
688 ProcessorList::iterator ploc = loc;
690 _processors.erase(ploc);
691 configure_processors_unlocked (0); // it worked before we tried to add it ...
692 cerr << "configure failed\n";
696 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(processor)) != 0) {
698 if (pi->natural_input_streams() == ChanCount::ZERO) {
699 /* generator plugin */
700 _have_internal_generator = true;
705 if (_control_outs != processor) {
706 // XXX: do we want to emit the signal here ? change call order.
707 processor->activate ();
709 processor->ActiveChanged.connect (bind (mem_fun (_session, &Session::update_latency_compensation), false, false));
711 _output->set_user_latency (0);
714 processors_changed (); /* EMIT SIGNAL */
720 Route::add_processor_from_xml (const XMLNode& node, ProcessorList::iterator iter)
722 const XMLProperty *prop;
724 if (node.name() != "Processor") {
729 if ((prop = node.property ("type")) != 0) {
731 boost::shared_ptr<Processor> processor;
733 if (prop->value() == "ladspa" || prop->value() == "Ladspa" ||
734 prop->value() == "lv2" ||
735 prop->value() == "vst" ||
736 prop->value() == "audiounit") {
738 processor.reset (new PluginInsert(_session, node));
740 } else if (prop->value() == "port") {
742 processor.reset (new PortInsert (_session, _mute_master, node));
744 } else if (prop->value() == "send") {
746 processor.reset (new Send (_session, _mute_master, node));
748 } else if (prop->value() == "meter") {
751 if (_meter->set_state (node, Stateful::loading_state_version)) {
758 _meter.reset (new PeakMeter (_session, node));
761 } else if (prop->value() == "amp") {
763 /* amp always exists */
766 if (processor->set_state (node, Stateful::loading_state_version)) {
769 /* never any reason to add it */
773 } else if (prop->value() == "intsend") {
775 processor.reset (new InternalSend (_session, _mute_master, node));
777 } else if (prop->value() == "intreturn") {
780 if (_intreturn->set_state (node, Stateful::loading_state_version)) {
786 _intreturn.reset (new InternalReturn (_session, node));
787 processor = _intreturn;
789 } else if (prop->value() == "main-outs") {
792 if (_main_outs->set_state (node, Stateful::loading_state_version)) {
799 _main_outs.reset (new Delivery (_session, _output, _mute_master, node));
800 processor = _main_outs;
803 error << string_compose(_("unknown Processor type \"%1\"; ignored"), prop->value()) << endmsg;
807 if (iter == _processors.end() && processor->visible() && !_processors.empty()) {
808 /* check for invisible processors stacked at the end and leave them there */
809 ProcessorList::iterator p;
810 p = _processors.end();
812 while (!(*p)->visible() && p != _processors.begin()) {
819 return (add_processor (processor, iter) == 0);
822 error << _("Processor XML node has no type property") << endmsg;
827 catch (failed_constructor &err) {
828 warning << _("processor could not be created. Ignored.") << endmsg;
835 Route::add_processor_from_xml_2X (const XMLNode& node, int version, ProcessorList::iterator iter)
837 const XMLProperty *prop;
840 boost::shared_ptr<Processor> processor;
842 if (node.name() == "Insert") {
844 if ((prop = node.property ("type")) != 0) {
846 if (prop->value() == "ladspa" || prop->value() == "Ladspa" ||
847 prop->value() == "lv2" ||
848 prop->value() == "vst" ||
849 prop->value() == "audiounit") {
851 processor.reset (new PluginInsert (_session, node));
855 processor.reset (new PortInsert (_session, _mute_master, node));
860 } else if (node.name() == "Send") {
862 processor.reset (new Send (_session, _mute_master, node, version));
866 error << string_compose(_("unknown Processor type \"%1\"; ignored"), node.name()) << endmsg;
870 if (iter == _processors.end() && processor->visible() && !_processors.empty()) {
871 /* check for invisible processors stacked at the end and leave them there */
872 ProcessorList::iterator p;
873 p = _processors.end();
875 while (!(*p)->visible() && p != _processors.begin()) {
882 return (add_processor (processor, iter) == 0);
885 catch (failed_constructor &err) {
886 warning << _("processor could not be created. Ignored.") << endmsg;
892 Route::add_processors (const ProcessorList& others, boost::shared_ptr<Processor> before, ProcessorStreams* err)
894 ProcessorList::iterator loc;
897 loc = find(_processors.begin(), _processors.end(), before);
899 /* nothing specified - at end but before main outs */
900 loc = find (_processors.begin(), _processors.end(), _main_outs);
903 return add_processors (others, loc, err);
907 Route::add_processors (const ProcessorList& others, ProcessorList::iterator iter, ProcessorStreams* err)
909 /* NOTE: this is intended to be used ONLY when copying
910 processors from another Route. Hence the subtle
911 differences between this and ::add_processor()
914 ChanCount old_pms = processor_max_streams;
916 if (!_session.engine().connected()) {
920 if (others.empty()) {
925 Glib::RWLock::WriterLock lm (_processor_lock);
926 ProcessorList::iterator existing_end = _processors.end();
929 ChanCount potential_max_streams = ChanCount::max (_input->n_ports(), _output->n_ports());
931 for (ProcessorList::const_iterator i = others.begin(); i != others.end(); ++i) {
933 // Ensure meter only appears in the list once
935 ProcessorList::iterator m = find(_processors.begin(), _processors.end(), *i);
936 if (m != _processors.end()) {
937 _processors.erase(m);
941 boost::shared_ptr<PluginInsert> pi;
943 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(*i)) != 0) {
946 ChanCount m = max (pi->input_streams(), pi->output_streams());
948 if (m > potential_max_streams) {
949 potential_max_streams = m;
953 _processors.insert (iter, *i);
955 if (configure_processors_unlocked (err)) {
957 _processors.erase (existing_end, _processors.end());
958 configure_processors_unlocked (0); // it worked before we tried to add it ...
962 (*i)->ActiveChanged.connect (bind (mem_fun (_session, &Session::update_latency_compensation), false, false));
965 _output->set_user_latency (0);
968 processors_changed (); /* EMIT SIGNAL */
974 Route::placement_range(Placement p, ProcessorList::iterator& start, ProcessorList::iterator& end)
977 start = _processors.begin();
978 end = find(_processors.begin(), _processors.end(), _amp);
980 start = find(_processors.begin(), _processors.end(), _amp);
982 end = _processors.end();
986 /** Turn off all processors with a given placement
987 * @param p Placement of processors to disable
990 Route::disable_processors (Placement p)
992 Glib::RWLock::ReaderLock lm (_processor_lock);
994 ProcessorList::iterator start, end;
995 placement_range(p, start, end);
997 for (ProcessorList::iterator i = start; i != end; ++i) {
1001 _session.set_dirty ();
1004 /** Turn off all redirects
1007 Route::disable_processors ()
1009 Glib::RWLock::ReaderLock lm (_processor_lock);
1011 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1012 (*i)->deactivate ();
1015 _session.set_dirty ();
1018 /** Turn off all redirects with a given placement
1019 * @param p Placement of redirects to disable
1022 Route::disable_plugins (Placement p)
1024 Glib::RWLock::ReaderLock lm (_processor_lock);
1026 ProcessorList::iterator start, end;
1027 placement_range(p, start, end);
1029 for (ProcessorList::iterator i = start; i != end; ++i) {
1030 if (boost::dynamic_pointer_cast<PluginInsert> (*i)) {
1031 (*i)->deactivate ();
1035 _session.set_dirty ();
1038 /** Turn off all plugins
1041 Route::disable_plugins ()
1043 Glib::RWLock::ReaderLock lm (_processor_lock);
1045 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1046 if (boost::dynamic_pointer_cast<PluginInsert> (*i)) {
1047 (*i)->deactivate ();
1051 _session.set_dirty ();
1056 Route::ab_plugins (bool forward)
1058 Glib::RWLock::ReaderLock lm (_processor_lock);
1062 /* forward = turn off all active redirects, and mark them so that the next time
1063 we go the other way, we will revert them
1066 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1067 if (!boost::dynamic_pointer_cast<PluginInsert> (*i)) {
1071 if ((*i)->active()) {
1072 (*i)->deactivate ();
1073 (*i)->set_next_ab_is_active (true);
1075 (*i)->set_next_ab_is_active (false);
1081 /* backward = if the redirect was marked to go active on the next ab, do so */
1083 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1085 if (!boost::dynamic_pointer_cast<PluginInsert> (*i)) {
1089 if ((*i)->get_next_ab_is_active()) {
1092 (*i)->deactivate ();
1097 _session.set_dirty ();
1101 /** Remove processors with a given placement.
1102 * @param p Placement of processors to remove.
1105 Route::clear_processors (Placement p)
1107 const ChanCount old_pms = processor_max_streams;
1109 if (!_session.engine().connected()) {
1113 bool already_deleting = _session.deletion_in_progress();
1114 if (!already_deleting) {
1115 _session.set_deletion_in_progress();
1119 Glib::RWLock::WriterLock lm (_processor_lock);
1120 ProcessorList new_list;
1121 ProcessorStreams err;
1122 bool seen_amp = false;
1124 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1130 if ((*i) == _amp || (*i) == _meter || (*i) == _main_outs) {
1132 /* you can't remove these */
1134 new_list.push_back (*i);
1141 new_list.push_back (*i);
1144 (*i)->drop_references ();
1152 (*i)->drop_references ();
1155 new_list.push_back (*i);
1162 _processors = new_list;
1163 configure_processors_unlocked (&err); // this can't fail
1166 processor_max_streams.reset();
1167 _have_internal_generator = false;
1168 processors_changed (); /* EMIT SIGNAL */
1170 if (!already_deleting) {
1171 _session.clear_deletion_in_progress();
1176 Route::remove_processor (boost::shared_ptr<Processor> processor, ProcessorStreams* err)
1178 /* these can never be removed */
1180 if (processor == _amp || processor == _meter || processor == _main_outs) {
1184 ChanCount old_pms = processor_max_streams;
1186 if (!_session.engine().connected()) {
1190 processor_max_streams.reset();
1193 Glib::RWLock::WriterLock lm (_processor_lock);
1194 ProcessorList::iterator i;
1195 bool removed = false;
1197 for (i = _processors.begin(); i != _processors.end(); ) {
1198 if (*i == processor) {
1200 /* move along, see failure case for configure_processors()
1201 where we may need to reconfigure the processor.
1204 /* stop redirects that send signals to JACK ports
1205 from causing noise as a result of no longer being
1209 boost::shared_ptr<IOProcessor> iop;
1211 if ((iop = boost::dynamic_pointer_cast<IOProcessor> (*i)) != 0) {
1213 iop->input()->disconnect (this);
1215 if (iop->output()) {
1216 iop->output()->disconnect (this);
1220 i = _processors.erase (i);
1228 _output->set_user_latency (0);
1236 if (configure_processors_unlocked (err)) {
1237 /* get back to where we where */
1238 _processors.insert (i, processor);
1239 /* we know this will work, because it worked before :) */
1240 configure_processors_unlocked (0);
1244 _have_internal_generator = false;
1246 for (i = _processors.begin(); i != _processors.end(); ++i) {
1247 boost::shared_ptr<PluginInsert> pi;
1249 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(*i)) != 0) {
1250 if (pi->is_generator()) {
1251 _have_internal_generator = true;
1258 processor->drop_references ();
1259 processors_changed (); /* EMIT SIGNAL */
1265 Route::remove_processors (const ProcessorList& to_be_deleted, ProcessorStreams* err)
1267 ProcessorList deleted;
1268 ProcessorList as_we_were;
1270 if (!_session.engine().connected()) {
1274 processor_max_streams.reset();
1277 Glib::RWLock::WriterLock lm (_processor_lock);
1278 ProcessorList::iterator i;
1279 boost::shared_ptr<Processor> processor;
1281 as_we_were = _processors;
1283 for (i = _processors.begin(); i != _processors.end(); ) {
1287 /* these can never be removed */
1289 if (processor == _amp || processor == _meter || processor == _main_outs) {
1294 /* see if its in the list of processors to delete */
1296 if (find (to_be_deleted.begin(), to_be_deleted.end(), processor) == to_be_deleted.end()) {
1301 /* stop IOProcessors that send to JACK ports
1302 from causing noise as a result of no longer being
1306 boost::shared_ptr<IOProcessor> iop;
1308 if ((iop = boost::dynamic_pointer_cast<IOProcessor> (processor)) != 0) {
1312 deleted.push_back (processor);
1313 i = _processors.erase (i);
1316 if (deleted.empty()) {
1317 /* none of those in the requested list were found */
1321 _output->set_user_latency (0);
1323 if (configure_processors_unlocked (err)) {
1324 /* get back to where we where */
1325 _processors = as_we_were;
1326 /* we know this will work, because it worked before :) */
1327 configure_processors_unlocked (0);
1331 _have_internal_generator = false;
1333 for (i = _processors.begin(); i != _processors.end(); ++i) {
1334 boost::shared_ptr<PluginInsert> pi;
1336 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(*i)) != 0) {
1337 if (pi->is_generator()) {
1338 _have_internal_generator = true;
1345 /* now try to do what we need to so that those that were removed will be deleted */
1347 for (ProcessorList::iterator i = deleted.begin(); i != deleted.end(); ++i) {
1348 (*i)->drop_references ();
1351 processors_changed (); /* EMIT SIGNAL */
1358 Route::configure_processors (ProcessorStreams* err)
1360 if (!_in_configure_processors) {
1361 Glib::RWLock::WriterLock lm (_processor_lock);
1362 return configure_processors_unlocked (err);
1367 /** Configure the input/output configuration of each processor in the processors list.
1368 * Return 0 on success, otherwise configuration is impossible.
1371 Route::configure_processors_unlocked (ProcessorStreams* err)
1373 if (_in_configure_processors) {
1377 _in_configure_processors = true;
1379 // Check each processor in order to see if we can configure as requested
1380 ChanCount in = _input->n_ports ();
1382 list< pair<ChanCount,ChanCount> > configuration;
1385 for (ProcessorList::iterator p = _processors.begin(); p != _processors.end(); ++p, ++index) {
1387 if ((*p)->can_support_io_configuration(in, out)) {
1388 configuration.push_back(make_pair(in, out));
1395 _in_configure_processors = false;
1400 /* Take the process lock so that if we add a processor which increases the required
1401 number of scratch buffers, we create those scratch buffers before the process
1402 thread has a chance to ask for them.
1403 XXX: in an ideal world we'd perhaps use some RCU magic to avoid having to take
1407 Glib::Mutex::Lock pl (_session.engine().process_lock ());
1409 // We can, so configure everything
1410 list< pair<ChanCount,ChanCount> >::iterator c = configuration.begin();
1411 for (ProcessorList::iterator p = _processors.begin(); p != _processors.end(); ++p, ++c) {
1412 (*p)->configure_io(c->first, c->second);
1413 processor_max_streams = ChanCount::max(processor_max_streams, c->first);
1414 processor_max_streams = ChanCount::max(processor_max_streams, c->second);
1418 /* make sure we have sufficient scratch buffers to cope with the new processor
1420 _session.ensure_buffers (n_process_buffers ());
1422 _session.ensure_buffers (n_process_buffers ());
1424 _in_configure_processors = false;
1429 Route::all_processors_flip ()
1431 Glib::RWLock::ReaderLock lm (_processor_lock);
1433 if (_processors.empty()) {
1437 bool first_is_on = _processors.front()->active();
1439 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1441 (*i)->deactivate ();
1447 _session.set_dirty ();
1450 /** Set all processors with a given placement to a given active state.
1451 * @param p Placement of processors to change.
1452 * @param state New active state for those processors.
1455 Route::all_processors_active (Placement p, bool state)
1457 Glib::RWLock::ReaderLock lm (_processor_lock);
1459 if (_processors.empty()) {
1462 ProcessorList::iterator start, end;
1463 placement_range(p, start, end);
1465 bool before_amp = true;
1466 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1471 if (p == PreFader && before_amp) {
1475 (*i)->deactivate ();
1480 _session.set_dirty ();
1484 Route::processor_is_prefader (boost::shared_ptr<Processor> p)
1486 bool pre_fader = true;
1487 Glib::RWLock::ReaderLock lm (_processor_lock);
1489 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1491 /* semantic note: if p == amp, we want to return true, so test
1492 for equality before checking if this is the amp
1509 Route::reorder_processors (const ProcessorList& new_order, ProcessorStreams* err)
1511 /* "new_order" is an ordered list of processors to be positioned according to "placement".
1512 NOTE: all processors in "new_order" MUST be marked as visible. There maybe additional
1513 processors in the current actual processor list that are hidden. Any visible processors
1514 in the current list but not in "new_order" will be assumed to be deleted.
1518 Glib::RWLock::WriterLock lm (_processor_lock);
1519 ChanCount old_pms = processor_max_streams;
1520 ProcessorList::iterator oiter;
1521 ProcessorList::const_iterator niter;
1522 ProcessorList as_it_was_before = _processors;
1523 ProcessorList as_it_will_be;
1525 oiter = _processors.begin();
1526 niter = new_order.begin();
1528 while (niter != new_order.end()) {
1530 /* if the next processor in the old list is invisible (i.e. should not be in the new order)
1531 then append it to the temp list.
1533 Otherwise, see if the next processor in the old list is in the new list. if not,
1534 its been deleted. If its there, append it to the temp list.
1537 if (oiter == _processors.end()) {
1539 /* no more elements in the old list, so just stick the rest of
1540 the new order onto the temp list.
1543 as_it_will_be.insert (as_it_will_be.end(), niter, new_order.end());
1544 while (niter != new_order.end()) {
1551 if (!(*oiter)->visible()) {
1553 as_it_will_be.push_back (*oiter);
1557 /* visible processor: check that its in the new order */
1559 if (find (new_order.begin(), new_order.end(), (*oiter)) == new_order.end()) {
1560 /* deleted: do nothing, shared_ptr<> will clean up */
1562 /* ignore this one, and add the next item from the new order instead */
1563 as_it_will_be.push_back (*niter);
1568 /* now remove from old order - its taken care of no matter what */
1569 oiter = _processors.erase (oiter);
1574 _processors.insert (oiter, as_it_will_be.begin(), as_it_will_be.end());
1576 if (configure_processors_unlocked (err)) {
1577 _processors = as_it_was_before;
1578 processor_max_streams = old_pms;
1583 processors_changed (); /* EMIT SIGNAL */
1595 Route::get_template()
1597 return state(false);
1601 Route::state(bool full_state)
1603 XMLNode *node = new XMLNode("Route");
1604 ProcessorList::iterator i;
1607 id().print (buf, sizeof (buf));
1608 node->add_property("id", buf);
1609 node->add_property ("name", _name);
1610 node->add_property("default-type", _default_type.to_string());
1613 node->add_property("flags", enum_2_string (_flags));
1616 node->add_property("active", _active?"yes":"no");
1617 node->add_property("phase-invert", _phase_invert?"yes":"no");
1618 node->add_property("denormal-protection", _denormal_protection?"yes":"no");
1619 node->add_property("meter-point", enum_2_string (_meter_point));
1622 node->add_property("route-group", _route_group->name());
1625 string order_string;
1626 OrderKeys::iterator x = order_keys.begin();
1628 while (x != order_keys.end()) {
1629 order_string += string ((*x).first);
1630 order_string += '=';
1631 snprintf (buf, sizeof(buf), "%ld", (*x).second);
1632 order_string += buf;
1636 if (x == order_keys.end()) {
1640 order_string += ':';
1642 node->add_property ("order-keys", order_string);
1644 node->add_child_nocopy (_input->state (full_state));
1645 node->add_child_nocopy (_output->state (full_state));
1646 node->add_child_nocopy (_solo_control->get_state ());
1647 node->add_child_nocopy (_mute_master->get_state ());
1649 XMLNode* remote_control_node = new XMLNode (X_("RemoteControl"));
1650 snprintf (buf, sizeof (buf), "%d", _remote_control_id);
1651 remote_control_node->add_property (X_("id"), buf);
1652 node->add_child_nocopy (*remote_control_node);
1654 if (_comment.length()) {
1655 XMLNode *cmt = node->add_child ("Comment");
1656 cmt->add_content (_comment);
1659 for (i = _processors.begin(); i != _processors.end(); ++i) {
1660 node->add_child_nocopy((*i)->state (full_state));
1664 node->add_child_copy (*_extra_xml);
1671 Route::set_state (const XMLNode& node, int version)
1673 return _set_state (node, version, true);
1677 Route::_set_state (const XMLNode& node, int version, bool /*call_base*/)
1679 if (version < 3000) {
1680 return _set_state_2X (node, version);
1684 XMLNodeConstIterator niter;
1686 XMLPropertyList plist;
1687 const XMLProperty *prop;
1689 if (node.name() != "Route"){
1690 error << string_compose(_("Bad node sent to Route::set_state() [%1]"), node.name()) << endmsg;
1694 if ((prop = node.property (X_("name"))) != 0) {
1695 Route::set_name (prop->value());
1698 if ((prop = node.property ("id")) != 0) {
1699 _id = prop->value ();
1702 if ((prop = node.property (X_("flags"))) != 0) {
1703 _flags = Flag (string_2_enum (prop->value(), _flags));
1708 /* add all processors (except amp, which is always present) */
1710 nlist = node.children();
1711 XMLNode processor_state (X_("processor_state"));
1713 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
1717 if (child->name() == IO::state_node_name) {
1718 if ((prop = child->property (X_("direction"))) == 0) {
1722 if (prop->value() == "Input") {
1723 _input->set_state (*child, version);
1724 } else if (prop->value() == "Output") {
1725 _output->set_state (*child, version);
1729 if (child->name() == X_("Processor")) {
1730 processor_state.add_child_copy (*child);
1734 set_processor_state (processor_state);
1736 if ((prop = node.property ("solo_level")) != 0) {
1737 _solo_level = 0; // needed for mod_solo_level() to work
1738 mod_solo_level (atoi (prop->value()));
1741 if ((prop = node.property ("solo-isolated")) != 0) {
1742 set_solo_isolated (string_is_affirmative (prop->value()), this);
1745 if ((prop = node.property (X_("phase-invert"))) != 0) {
1746 set_phase_invert (string_is_affirmative (prop->value()));
1749 if ((prop = node.property (X_("denormal-protection"))) != 0) {
1750 set_denormal_protection (string_is_affirmative (prop->value()));
1753 if ((prop = node.property (X_("active"))) != 0) {
1754 bool yn = string_is_affirmative (prop->value());
1755 _active = !yn; // force switch
1759 if ((prop = node.property (X_("soloed"))) != 0) {
1760 bool yn = string_is_affirmative (prop->value());
1762 /* XXX force reset of solo status */
1764 set_solo (yn, this);
1767 if ((prop = node.property (X_("meter-point"))) != 0) {
1768 _meter_point = MeterPoint (string_2_enum (prop->value (), _meter_point));
1771 if ((prop = node.property (X_("route-group"))) != 0) {
1772 RouteGroup* route_group = _session.route_group_by_name(prop->value());
1773 if (route_group == 0) {
1774 error << string_compose(_("Route %1: unknown route group \"%2 in saved state (ignored)"), _name, prop->value()) << endmsg;
1776 set_route_group (route_group, this);
1780 if ((prop = node.property (X_("order-keys"))) != 0) {
1784 string::size_type colon, equal;
1785 string remaining = prop->value();
1787 while (remaining.length()) {
1789 if ((equal = remaining.find_first_of ('=')) == string::npos || equal == remaining.length()) {
1790 error << string_compose (_("badly formed order key string in state file! [%1] ... ignored."), remaining)
1793 if (sscanf (remaining.substr (equal+1).c_str(), "%ld", &n) != 1) {
1794 error << string_compose (_("badly formed order key string in state file! [%1] ... ignored."), remaining)
1797 set_order_key (remaining.substr (0, equal), n);
1801 colon = remaining.find_first_of (':');
1803 if (colon != string::npos) {
1804 remaining = remaining.substr (colon+1);
1811 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
1814 if (child->name() == X_("Comment")) {
1816 /* XXX this is a terrible API design in libxml++ */
1818 XMLNode *cmt = *(child->children().begin());
1819 _comment = cmt->content();
1821 } else if (child->name() == X_("Extra")) {
1823 _extra_xml = new XMLNode (*child);
1825 } else if (child->name() == X_("Controllable") && (prop = child->property("name")) != 0) {
1827 if (prop->value() == "solo") {
1828 _solo_control->set_state (*child, version);
1829 _session.add_controllable (_solo_control);
1832 } else if (child->name() == X_("RemoteControl")) {
1833 if ((prop = child->property (X_("id"))) != 0) {
1835 sscanf (prop->value().c_str(), "%d", &x);
1836 set_remote_control_id (x);
1839 } else if (child->name() == X_("MuteMaster")) {
1840 _mute_master->set_state (*child, version);
1848 Route::_set_state_2X (const XMLNode& node, int version)
1851 XMLNodeConstIterator niter;
1853 XMLPropertyList plist;
1854 const XMLProperty *prop;
1856 /* 2X things which still remain to be handled:
1859 * mute-affects-pre-fader
1860 * mute-affects-post-fader
1861 * mute-affects-control-outs
1862 * mute-affects-main-outs
1867 if (node.name() != "Route") {
1868 error << string_compose(_("Bad node sent to Route::set_state() [%1]"), node.name()) << endmsg;
1872 if ((prop = node.property (X_("flags"))) != 0) {
1873 _flags = Flag (string_2_enum (prop->value(), _flags));
1878 /* add standard processors */
1880 _meter.reset (new PeakMeter (_session));
1881 add_processor (_meter, PreFader);
1883 if (_flags & ControlOut) {
1884 /* where we listen to tracks */
1885 _intreturn.reset (new InternalReturn (_session));
1886 add_processor (_intreturn, PreFader);
1889 _main_outs.reset (new Delivery (_session, _output, _mute_master, _name, Delivery::Main));
1890 add_processor (_main_outs, PostFader);
1894 nlist = node.children ();
1895 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1899 if (child->name() == IO::state_node_name) {
1901 /* there is a note in IO::set_state_2X() about why we have to call
1905 _input->set_state_2X (*child, version, true);
1906 _output->set_state_2X (*child, version, false);
1908 if ((prop = child->property (X_("name"))) != 0) {
1909 set_name (prop->value ());
1912 if ((prop = child->property (X_("id"))) != 0) {
1913 _id = prop->value ();
1916 if ((prop = child->property (X_("active"))) != 0) {
1917 bool yn = string_is_affirmative (prop->value());
1918 _active = !yn; // force switch
1926 if ((prop = node.property (X_("phase-invert"))) != 0) {
1927 set_phase_invert (string_is_affirmative (prop->value()));
1930 if ((prop = node.property (X_("denormal-protection"))) != 0) {
1931 set_denormal_protection (string_is_affirmative (prop->value()));
1934 if ((prop = node.property (X_("soloed"))) != 0) {
1935 bool yn = string_is_affirmative (prop->value());
1937 /* XXX force reset of solo status */
1939 set_solo (yn, this);
1942 if ((prop = node.property (X_("meter-point"))) != 0) {
1943 _meter_point = MeterPoint (string_2_enum (prop->value (), _meter_point));
1946 /* XXX: if the route was in both a mix group and an edit group, it'll end up
1947 just in the edit group. */
1949 if ((prop = node.property (X_("mix-group"))) != 0) {
1950 RouteGroup* route_group = _session.route_group_by_name(prop->value());
1951 if (route_group == 0) {
1952 error << string_compose(_("Route %1: unknown route group \"%2 in saved state (ignored)"), _name, prop->value()) << endmsg;
1954 set_route_group (route_group, this);
1958 if ((prop = node.property (X_("edit-group"))) != 0) {
1959 RouteGroup* route_group = _session.route_group_by_name(prop->value());
1960 if (route_group == 0) {
1961 error << string_compose(_("Route %1: unknown route group \"%2 in saved state (ignored)"), _name, prop->value()) << endmsg;
1963 set_route_group (route_group, this);
1967 if ((prop = node.property (X_("order-keys"))) != 0) {
1971 string::size_type colon, equal;
1972 string remaining = prop->value();
1974 while (remaining.length()) {
1976 if ((equal = remaining.find_first_of ('=')) == string::npos || equal == remaining.length()) {
1977 error << string_compose (_("badly formed order key string in state file! [%1] ... ignored."), remaining)
1980 if (sscanf (remaining.substr (equal+1).c_str(), "%ld", &n) != 1) {
1981 error << string_compose (_("badly formed order key string in state file! [%1] ... ignored."), remaining)
1984 set_order_key (remaining.substr (0, equal), n);
1988 colon = remaining.find_first_of (':');
1990 if (colon != string::npos) {
1991 remaining = remaining.substr (colon+1);
1998 XMLNodeList redirect_nodes;
2000 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
2004 if (child->name() == X_("Send") || child->name() == X_("Insert")) {
2005 redirect_nodes.push_back(child);
2010 set_processor_state_2X (redirect_nodes, version);
2012 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
2015 if (child->name() == X_("Comment")) {
2017 /* XXX this is a terrible API design in libxml++ */
2019 XMLNode *cmt = *(child->children().begin());
2020 _comment = cmt->content();
2022 } else if (child->name() == X_("Extra")) {
2024 _extra_xml = new XMLNode (*child);
2026 } else if (child->name() == X_("Controllable") && (prop = child->property("name")) != 0) {
2028 if (prop->value() == "solo") {
2029 _solo_control->set_state (*child, version);
2030 _session.add_controllable (_solo_control);
2033 } else if (child->name() == X_("RemoteControl")) {
2034 if ((prop = child->property (X_("id"))) != 0) {
2036 sscanf (prop->value().c_str(), "%d", &x);
2037 set_remote_control_id (x);
2047 Route::get_processor_state ()
2049 XMLNode* root = new XMLNode (X_("redirects"));
2050 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
2051 root->add_child_nocopy ((*i)->state (true));
2058 Route::set_processor_state_2X (XMLNodeList const & nList, int version)
2060 /* We don't bother removing existing processors not in nList, as this
2061 method will only be called when creating a Route from scratch, not
2062 for undo purposes. Just put processors in at the appropriate place
2066 for (XMLNodeConstIterator i = nList.begin(); i != nList.end(); ++i) {
2067 add_processor_from_xml_2X (**i, version, _processors.begin ());
2072 Route::set_processor_state (const XMLNode& node)
2074 const XMLNodeList &nlist = node.children();
2075 XMLNodeConstIterator niter;
2076 ProcessorList::iterator i, o;
2078 // Iterate through existing processors, remove those which are not in the state list
2080 for (i = _processors.begin(); i != _processors.end(); ) {
2082 /* leave amp alone, always */
2089 ProcessorList::iterator tmp = i;
2092 bool processorInStateList = false;
2094 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2096 XMLProperty* id_prop = (*niter)->property(X_("id"));
2098 if (id_prop && (*i)->id() == id_prop->value()) {
2099 processorInStateList = true;
2104 if (!processorInStateList) {
2105 remove_processor (*i);
2111 // Iterate through state list and make sure all processors are on the track and in the correct order,
2112 // set the state of existing processors according to the new state on the same go
2114 i = _processors.begin();
2116 for (niter = nlist.begin(); niter != nlist.end(); ++niter, ++i) {
2118 XMLProperty* prop = (*niter)->property ("type");
2122 // Check whether the next processor in the list is the right one,
2123 // except for "amp" which is always there and may not have the
2124 // old ID since it is always created anew in every Route
2126 if (prop->value() != "amp") {
2127 while (o != _processors.end()) {
2128 XMLProperty* id_prop = (*niter)->property(X_("id"));
2129 if (id_prop && (*o)->id() == id_prop->value()) {
2137 // If the processor (*niter) is not on the route,
2138 // create it and move it to the correct location
2140 if (o == _processors.end()) {
2142 if (add_processor_from_xml (**niter, i)) {
2143 --i; // move iterator to the newly inserted processor
2145 cerr << "Error restoring route: unable to restore processor" << endl;
2150 // Otherwise, the processor already exists; just
2151 // ensure it is at the location provided in the XML state
2154 boost::shared_ptr<Processor> tmp = (*o);
2155 _processors.erase (o); // remove the old copy
2156 _processors.insert (i, tmp); // insert the processor at the correct location
2157 --i; // move iterator to the correct processor
2160 // and make it (just) so
2162 (*i)->set_state (**niter, Stateful::current_state_version);
2166 /* note: there is no configure_processors() call because we figure that
2167 the XML state represents a working signal route.
2170 processors_changed ();
2174 Route::curve_reallocate ()
2176 // _gain_automation_curve.finish_resize ();
2177 // _pan_automation_curve.finish_resize ();
2181 Route::silence (nframes_t nframes)
2185 _output->silence (nframes);
2188 Glib::RWLock::ReaderLock lm (_processor_lock, Glib::TRY_LOCK);
2191 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
2192 boost::shared_ptr<PluginInsert> pi;
2194 if (!_active && (pi = boost::dynamic_pointer_cast<PluginInsert> (*i)) != 0) {
2195 // skip plugins, they don't need anything when we're not active
2199 (*i)->silence (nframes);
2202 if (nframes == _session.get_block_size()) {
2212 Route::add_internal_return ()
2215 _intreturn.reset (new InternalReturn (_session));
2216 add_processor (_intreturn, PreFader);
2221 Route::get_return_buffer () const
2223 Glib::RWLock::ReaderLock rm (_processor_lock);
2225 for (ProcessorList::const_iterator x = _processors.begin(); x != _processors.end(); ++x) {
2226 boost::shared_ptr<InternalReturn> d = boost::dynamic_pointer_cast<InternalReturn>(*x);
2229 BufferSet* bs = d->get_buffers ();
2238 Route::release_return_buffer () const
2240 Glib::RWLock::ReaderLock rm (_processor_lock);
2242 for (ProcessorList::const_iterator x = _processors.begin(); x != _processors.end(); ++x) {
2243 boost::shared_ptr<InternalReturn> d = boost::dynamic_pointer_cast<InternalReturn>(*x);
2246 return d->release_buffers ();
2252 Route::listen_via (boost::shared_ptr<Route> route, Placement placement, bool /*active*/, bool aux)
2254 vector<string> ports;
2255 vector<string>::const_iterator i;
2258 Glib::RWLock::ReaderLock rm (_processor_lock);
2260 for (ProcessorList::iterator x = _processors.begin(); x != _processors.end(); ++x) {
2262 boost::shared_ptr<InternalSend> d = boost::dynamic_pointer_cast<InternalSend>(*x);
2264 if (d && d->target_route() == route) {
2266 /* if the target is the control outs, then make sure
2267 we take note of which i-send is doing that.
2270 if (route == _session.control_out()) {
2271 _control_outs = boost::dynamic_pointer_cast<Delivery>(d);
2274 /* already listening via the specified IO: do nothing */
2281 boost::shared_ptr<InternalSend> listener;
2284 listener.reset (new InternalSend (_session, _mute_master, route, (aux ? Delivery::Aux : Delivery::Listen)));
2286 } catch (failed_constructor& err) {
2290 if (route == _session.control_out()) {
2291 _control_outs = listener;
2294 add_processor (listener, placement);
2300 Route::drop_listen (boost::shared_ptr<Route> route)
2302 ProcessorStreams err;
2303 ProcessorList::iterator tmp;
2305 Glib::RWLock::ReaderLock rl(_processor_lock);
2309 for (ProcessorList::iterator x = _processors.begin(); x != _processors.end(); ) {
2311 boost::shared_ptr<InternalSend> d = boost::dynamic_pointer_cast<InternalSend>(*x);
2313 if (d && d->target_route() == route) {
2315 remove_processor (*x, &err);
2318 /* list could have been demolished while we dropped the lock
2328 if (route == _session.control_out()) {
2329 _control_outs.reset ();
2334 Route::set_route_group (RouteGroup *rg, void *src)
2336 if (rg == _route_group) {
2341 _route_group->remove (this);
2344 if ((_route_group = rg) != 0) {
2345 _route_group->add (this);
2348 _session.set_dirty ();
2349 route_group_changed (src); /* EMIT SIGNAL */
2353 Route::drop_route_group (void *src)
2356 _session.set_dirty ();
2357 route_group_changed (src); /* EMIT SIGNAL */
2361 Route::set_comment (string cmt, void *src)
2364 comment_changed (src);
2365 _session.set_dirty ();
2369 Route::feeds (boost::shared_ptr<Route> other)
2371 // cerr << _name << endl;
2373 if (_output->connected_to (other->input())) {
2374 // cerr << "\tdirect FEEDS " << other->name() << endl;
2378 for (ProcessorList::iterator r = _processors.begin(); r != _processors.end(); r++) {
2380 boost::shared_ptr<IOProcessor> iop;
2382 if ((iop = boost::dynamic_pointer_cast<IOProcessor>(*r)) != 0) {
2383 if (iop->feeds (other)) {
2384 // cerr << "\tIOP " << iop->name() << " feeds " << other->name() << endl;
2387 // cerr << "\tIOP " << iop->name() << " does NOT feeds " << other->name() << endl;
2392 // cerr << "\tdoes NOT FEED " << other->name() << endl;
2397 Route::handle_transport_stopped (bool /*abort_ignored*/, bool did_locate, bool can_flush_processors)
2399 nframes_t now = _session.transport_frame();
2402 Glib::RWLock::ReaderLock lm (_processor_lock);
2405 automation_snapshot (now, true);
2408 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
2410 if (Config->get_plugins_stop_with_transport() && can_flush_processors) {
2411 (*i)->deactivate ();
2415 (*i)->transport_stopped (now);
2419 _roll_delay = _initial_delay;
2423 Route::input_change_handler (IOChange change, void * /*src*/)
2425 if ((change & ConfigurationChanged)) {
2426 configure_processors (0);
2431 Route::output_change_handler (IOChange change, void * /*src*/)
2433 if ((change & ConfigurationChanged)) {
2435 /* XXX resize all listeners to match _main_outs? */
2437 // configure_processors (0);
2442 Route::pans_required () const
2444 if (n_outputs().n_audio() < 2) {
2448 return max (n_inputs ().n_audio(), processor_max_streams.n_audio());
2452 Route::no_roll (nframes_t nframes, sframes_t start_frame, sframes_t end_frame,
2453 bool session_state_changing, bool /*can_record*/, bool /*rec_monitors_input*/)
2455 if (n_outputs().n_total() == 0) {
2459 if (session_state_changing || !_active || n_inputs() == ChanCount::ZERO) {
2464 _amp->apply_gain_automation (false);
2465 passthru (start_frame, end_frame, nframes, 0);
2471 Route::check_initial_delay (nframes_t nframes, nframes_t& transport_frame)
2473 if (_roll_delay > nframes) {
2475 _roll_delay -= nframes;
2477 /* transport frame is not legal for caller to use */
2480 } else if (_roll_delay > 0) {
2482 nframes -= _roll_delay;
2483 silence (_roll_delay);
2484 /* we've written _roll_delay of samples into the
2485 output ports, so make a note of that for
2488 _main_outs->increment_output_offset (_roll_delay);
2489 transport_frame += _roll_delay;
2498 Route::roll (nframes_t nframes, sframes_t start_frame, sframes_t end_frame, int declick,
2499 bool /*can_record*/, bool /*rec_monitors_input*/)
2502 // automation snapshot can also be called from the non-rt context
2503 // and it uses the processor list, so we try to acquire the lock here
2504 Glib::RWLock::ReaderLock lm (_processor_lock, Glib::TRY_LOCK);
2507 automation_snapshot (_session.transport_frame(), false);
2511 if (n_outputs().n_total() == 0) {
2515 if (!_active || n_inputs().n_total() == 0) {
2520 nframes_t unused = 0;
2522 if ((nframes = check_initial_delay (nframes, unused)) == 0) {
2528 passthru (start_frame, end_frame, nframes, declick);
2534 Route::silent_roll (nframes_t nframes, sframes_t /*start_frame*/, sframes_t /*end_frame*/,
2535 bool /*can_record*/, bool /*rec_monitors_input*/)
2542 Route::toggle_monitor_input ()
2544 for (PortSet::iterator i = _input->ports().begin(); i != _input->ports().end(); ++i) {
2545 i->ensure_monitor_input( ! i->monitoring_input());
2550 Route::has_external_redirects () const
2552 // FIXME: what about sends? - they don't return a signal back to ardour?
2554 boost::shared_ptr<const PortInsert> pi;
2556 for (ProcessorList::const_iterator i = _processors.begin(); i != _processors.end(); ++i) {
2558 if ((pi = boost::dynamic_pointer_cast<const PortInsert>(*i)) != 0) {
2560 for (PortSet::const_iterator port = pi->output()->ports().begin(); port != pi->output()->ports().end(); ++port) {
2562 string port_name = port->name();
2563 string client_name = port_name.substr (0, port_name.find(':'));
2565 /* only say "yes" if the redirect is actually in use */
2567 if (client_name != "ardour" && pi->active()) {
2578 Route::flush_processors ()
2580 /* XXX shouldn't really try to take this lock, since
2581 this is called from the RT audio thread.
2584 Glib::RWLock::ReaderLock lm (_processor_lock);
2586 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
2587 (*i)->deactivate ();
2593 Route::set_meter_point (MeterPoint p, void *src)
2595 if (_meter_point != p) {
2598 // Move meter in the processors list
2599 ProcessorList::iterator loc = find(_processors.begin(), _processors.end(), _meter);
2600 _processors.erase(loc);
2603 loc = _processors.begin();
2606 loc = find(_processors.begin(), _processors.end(), _amp);
2608 case MeterPostFader:
2609 loc = _processors.end();
2612 _processors.insert(loc, _meter);
2614 meter_change (src); /* EMIT SIGNAL */
2615 processors_changed (); /* EMIT SIGNAL */
2616 _session.set_dirty ();
2620 Route::put_control_outs_at (Placement p)
2622 if (!_control_outs) {
2626 // Move meter in the processors list
2627 ProcessorList::iterator loc = find(_processors.begin(), _processors.end(), _control_outs);
2628 _processors.erase(loc);
2632 loc = find(_processors.begin(), _processors.end(), _amp);
2633 if (loc != _processors.begin()) {
2638 loc = find(_processors.begin(), _processors.end(), _amp);
2639 assert (loc != _processors.end());
2644 _processors.insert(loc, _control_outs);
2646 processors_changed (); /* EMIT SIGNAL */
2647 _session.set_dirty ();
2651 Route::update_total_latency ()
2653 nframes_t old = _output->effective_latency();
2654 nframes_t own_latency = _output->user_latency();
2656 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
2657 if ((*i)->active ()) {
2658 own_latency += (*i)->signal_latency ();
2662 #undef DEBUG_LATENCY
2663 #ifdef DEBUG_LATENCY
2664 cerr << _name << ": internal redirect latency = " << own_latency << endl;
2667 _output->set_port_latency (own_latency);
2669 if (_output->user_latency() == 0) {
2671 /* this (virtual) function is used for pure Routes,
2672 not derived classes like AudioTrack. this means
2673 that the data processed here comes from an input
2674 port, not prerecorded material, and therefore we
2675 have to take into account any input latency.
2678 own_latency += _input->signal_latency ();
2681 if (old != own_latency) {
2682 _output->set_latency_delay (own_latency);
2683 signal_latency_changed (); /* EMIT SIGNAL */
2686 #ifdef DEBUG_LATENCY
2687 cerr << _name << ": input latency = " << _input->signal_latency() << " total = "
2688 << own_latency << endl;
2691 return _output->effective_latency ();
2695 Route::set_user_latency (nframes_t nframes)
2697 _output->set_user_latency (nframes);
2698 _session.update_latency_compensation (false, false);
2702 Route::set_latency_delay (nframes_t longest_session_latency)
2704 nframes_t old = _initial_delay;
2706 if (_output->effective_latency() < longest_session_latency) {
2707 _initial_delay = longest_session_latency - _output->effective_latency();
2712 if (_initial_delay != old) {
2713 initial_delay_changed (); /* EMIT SIGNAL */
2716 if (_session.transport_stopped()) {
2717 _roll_delay = _initial_delay;
2722 Route::automation_snapshot (nframes_t now, bool force)
2724 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
2725 (*i)->automation_snapshot (now, force);
2729 Route::SoloControllable::SoloControllable (std::string name, Route& r)
2730 : AutomationControl (r.session(), Evoral::Parameter (SoloAutomation),
2731 boost::shared_ptr<AutomationList>(), name)
2734 boost::shared_ptr<AutomationList> gl(new AutomationList(Evoral::Parameter(SoloAutomation)));
2739 Route::SoloControllable::set_value (float val)
2741 bool bval = ((val >= 0.5f) ? true: false);
2743 route.set_solo (bval, this);
2747 Route::SoloControllable::get_value (void) const
2749 return route.soloed() ? 1.0f : 0.0f;
2753 Route::set_block_size (nframes_t nframes)
2755 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
2756 (*i)->set_block_size (nframes);
2759 _session.ensure_buffers (n_process_buffers ());
2763 Route::protect_automation ()
2765 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i)
2766 (*i)->protect_automation();
2770 Route::set_pending_declick (int declick)
2773 /* this call is not allowed to turn off a pending declick unless "force" is true */
2775 _pending_declick = declick;
2777 // cerr << _name << ": after setting to " << declick << " pending declick = " << _pending_declick << endl;
2779 _pending_declick = 0;
2784 /** Shift automation forwards from a particular place, thereby inserting time.
2785 * Adds undo commands for any shifts that are performed.
2787 * @param pos Position to start shifting from.
2788 * @param frames Amount to shift forwards by.
2792 Route::shift (nframes64_t /*pos*/, nframes64_t /*frames*/)
2794 #ifdef THIS_NEEDS_FIXING_FOR_V3
2796 /* gain automation */
2797 XMLNode &before = _gain_control->get_state ();
2798 _gain_control->shift (pos, frames);
2799 XMLNode &after = _gain_control->get_state ();
2800 _session.add_command (new MementoCommand<AutomationList> (_gain_automation_curve, &before, &after));
2802 /* pan automation */
2803 for (std::vector<StreamPanner*>::iterator i = _panner->begin (); i != _panner->end (); ++i) {
2804 Curve & c = (*i)->automation ();
2805 XMLNode &before = c.get_state ();
2806 c.shift (pos, frames);
2807 XMLNode &after = c.get_state ();
2808 _session.add_command (new MementoCommand<AutomationList> (c, &before, &after));
2811 /* redirect automation */
2813 Glib::RWLock::ReaderLock lm (redirect_lock);
2814 for (RedirectList::iterator i = _redirects.begin (); i != _redirects.end (); ++i) {
2817 (*i)->what_has_automation (a);
2819 for (set<uint32_t>::const_iterator j = a.begin (); j != a.end (); ++j) {
2820 AutomationList & al = (*i)->automation_list (*j);
2821 XMLNode &before = al.get_state ();
2822 al.shift (pos, frames);
2823 XMLNode &after = al.get_state ();
2824 _session.add_command (new MementoCommand<AutomationList> (al, &before, &after));
2834 Route::save_as_template (const string& path, const string& name)
2836 XMLNode& node (state (false));
2839 IO::set_name_in_state (*node.children().front(), name);
2841 tree.set_root (&node);
2842 return tree.write (path.c_str());
2847 Route::set_name (const string& str)
2853 name = Route::ensure_track_or_route_name (str, _session);
2854 SessionObject::set_name (name);
2856 ret = (_input->set_name(name) && _output->set_name(name));
2860 Glib::RWLock::ReaderLock lm (_processor_lock);
2862 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
2864 /* rename all I/O processors that have inputs or outputs */
2866 boost::shared_ptr<IOProcessor> iop = boost::dynamic_pointer_cast<IOProcessor> (*i);
2868 if (iop && (iop->output() || iop->input())) {
2869 if (!iop->set_name (name)) {
2880 boost::shared_ptr<Send>
2881 Route::internal_send_for (boost::shared_ptr<const Route> target) const
2883 Glib::RWLock::ReaderLock lm (_processor_lock);
2885 for (ProcessorList::const_iterator i = _processors.begin(); i != _processors.end(); ++i) {
2886 boost::shared_ptr<InternalSend> send;
2888 if ((send = boost::dynamic_pointer_cast<InternalSend>(*i)) != 0) {
2889 if (send->target_route() == target) {
2895 return boost::shared_ptr<Send>();
2899 Route::set_phase_invert (bool yn)
2901 if (_phase_invert != yn) {
2902 _phase_invert = 0xffff; // XXX all channels
2903 phase_invert_changed (); /* EMIT SIGNAL */
2908 Route::phase_invert () const
2910 return _phase_invert != 0;
2914 Route::set_denormal_protection (bool yn)
2916 if (_denormal_protection != yn) {
2917 _denormal_protection = yn;
2918 denormal_protection_changed (); /* EMIT SIGNAL */
2923 Route::denormal_protection () const
2925 return _denormal_protection;
2929 Route::set_active (bool yn)
2931 if (_active != yn) {
2933 _input->set_active (yn);
2934 _output->set_active (yn);
2935 active_changed (); // EMIT SIGNAL
2942 Glib::RWLock::ReaderLock rm (_processor_lock, Glib::TRY_LOCK);
2948 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
2950 boost::shared_ptr<Send> s;
2951 boost::shared_ptr<Return> r;
2953 if ((s = boost::dynamic_pointer_cast<Send> (*i)) != 0) {
2954 s->meter()->meter();
2955 } else if ((r = boost::dynamic_pointer_cast<Return> (*i)) != 0) {
2956 r->meter()->meter ();
2961 boost::shared_ptr<Panner>
2962 Route::panner() const
2965 return _main_outs->panner();
2968 boost::shared_ptr<AutomationControl>
2969 Route::gain_control() const
2972 return _amp->gain_control();
2975 boost::shared_ptr<AutomationControl>
2976 Route::get_control (const Evoral::Parameter& param)
2978 /* either we own the control or .... */
2980 boost::shared_ptr<AutomationControl> c = boost::dynamic_pointer_cast<AutomationControl>(data().control (param));
2984 /* maybe one of our processors does or ... */
2986 Glib::RWLock::ReaderLock rm (_processor_lock, Glib::TRY_LOCK);
2987 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
2988 if ((c = boost::dynamic_pointer_cast<AutomationControl>((*i)->data().control (param))) != 0) {
2996 /* nobody does so we'll make a new one */
2998 c = boost::dynamic_pointer_cast<AutomationControl>(control_factory(param));