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/memento_command.h"
30 #include "evoral/Curve.hpp"
32 #include "ardour/amp.h"
33 #include "ardour/audio_port.h"
34 #include "ardour/audioengine.h"
35 #include "ardour/buffer.h"
36 #include "ardour/buffer_set.h"
37 #include "ardour/configuration.h"
38 #include "ardour/cycle_timer.h"
39 #include "ardour/debug.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 _meter->set_display_to_user (_meter_point == MeterCustom);
84 add_processor (_meter, PreFader);
86 if (_flags & ControlOut) {
87 /* where we listen to tracks */
88 _intreturn.reset (new InternalReturn (_session));
89 add_processor (_intreturn, PreFader);
92 _main_outs.reset (new Delivery (_session, _output, _mute_master, _name, Delivery::Main));
93 add_processor (_main_outs, PostFader);
95 /* now that we have _meter, its safe to connect to this */
97 _meter_connection = Metering::connect (mem_fun (*this, &Route::meter));
100 Route::Route (Session& sess, const XMLNode& node, DataType default_type)
101 : SessionObject (sess, "toBeReset")
102 , AutomatableControls (sess)
103 , _solo_control (new SoloControllable (X_("solo"), *this))
104 , _mute_master (new MuteMaster (sess, "toBeReset"))
105 , _default_type (default_type)
109 _set_state (node, Stateful::loading_state_version, false);
111 /* now that we have _meter, its safe to connect to this */
113 _meter_connection = Metering::connect (mem_fun (*this, &Route::meter));
120 _soloed_by_others = 0;
121 _solo_isolated = false;
124 processor_max_streams.reset();
126 order_keys[N_("signal")] = order_key_cnt++;
128 _meter_point = MeterPostFader;
131 _have_internal_generator = false;
132 _declickable = false;
133 _pending_declick = true;
134 _remote_control_id = 0;
135 _in_configure_processors = false;
136 _mute_points = MuteMaster::AllPoints;
141 _denormal_protection = false;
143 /* add standard controls */
145 add_control (_solo_control);
146 add_control (_mute_master);
148 /* input and output objects */
150 _input.reset (new IO (_session, _name, IO::Input, _default_type));
151 _output.reset (new IO (_session, _name, IO::Output, _default_type));
153 _input->changed.connect (mem_fun (this, &Route::input_change_handler));
154 _output->changed.connect (mem_fun (this, &Route::output_change_handler));
156 /* add amp processor */
158 _amp.reset (new Amp (_session, _mute_master));
159 add_processor (_amp, PostFader);
164 Metering::disconnect (_meter_connection);
166 /* don't use clear_processors here, as it depends on the session which may
167 be half-destroyed by now */
169 Glib::RWLock::WriterLock lm (_processor_lock);
170 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
171 (*i)->drop_references ();
173 _processors.clear ();
177 Route::set_remote_control_id (uint32_t id)
179 if (id != _remote_control_id) {
180 _remote_control_id = id;
181 RemoteControlIDChanged ();
186 Route::remote_control_id() const
188 return _remote_control_id;
192 Route::order_key (std::string const & name) const
194 OrderKeys::const_iterator i = order_keys.find (name);
195 if (i == order_keys.end()) {
203 Route::set_order_key (std::string const & name, long n)
205 order_keys[name] = n;
207 if (Config->get_sync_all_route_ordering()) {
208 for (OrderKeys::iterator x = order_keys.begin(); x != order_keys.end(); ++x) {
213 _session.set_dirty ();
216 /** Set all order keys to be the same as that for `base', if such a key
217 * exists in this route.
218 * @param base Base key.
221 Route::sync_order_keys (std::string const & base)
223 if (order_keys.empty()) {
227 OrderKeys::iterator i;
230 if ((i = order_keys.find (base)) == order_keys.end()) {
231 /* key doesn't exist, use the first existing key (during session initialization) */
232 i = order_keys.begin();
236 /* key exists - use it and reset all others (actually, itself included) */
238 i = order_keys.begin();
241 for (; i != order_keys.end(); ++i) {
247 Route::ensure_track_or_route_name(string name, Session &session)
249 string newname = name;
251 while (session.route_by_name (newname) != NULL) {
252 newname = bump_name_once (newname);
260 Route::inc_gain (gain_t fraction, void *src)
262 _amp->inc_gain (fraction, src);
266 Route::set_gain (gain_t val, void *src)
268 if (src != 0 && _route_group && src != _route_group && _route_group->active_property (RouteGroup::Gain)) {
270 if (_route_group->is_relative()) {
272 gain_t usable_gain = _amp->gain();
273 if (usable_gain < 0.000001f) {
274 usable_gain = 0.000001f;
278 if (delta < 0.000001f) {
282 delta -= usable_gain;
287 gain_t factor = delta / usable_gain;
290 factor = _route_group->get_max_factor(factor);
291 if (factor == 0.0f) {
292 _amp->gain_control()->Changed(); /* EMIT SIGNAL */
296 factor = _route_group->get_min_factor(factor);
297 if (factor == 0.0f) {
298 _amp->gain_control()->Changed(); /* EMIT SIGNAL */
303 _route_group->apply (&Route::inc_gain, factor, _route_group);
307 _route_group->apply (&Route::set_gain, val, _route_group);
313 if (val == _amp->gain()) {
317 _amp->set_gain (val, src);
320 /** Process this route for one (sub) cycle (process thread)
322 * @param bufs Scratch buffers to use for the signal path
323 * @param start_frame Initial transport frame
324 * @param end_frame Final transport frame
325 * @param nframes Number of frames to output (to ports)
327 * Note that (end_frame - start_frame) may not be equal to nframes when the
328 * transport speed isn't 1.0 (eg varispeed).
331 Route::process_output_buffers (BufferSet& bufs,
332 sframes_t start_frame, sframes_t end_frame, nframes_t nframes,
333 bool /*with_processors*/, int declick)
337 bufs.is_silent (false);
339 switch (Config->get_monitoring_model()) {
340 case HardwareMonitoring:
341 case ExternalMonitoring:
342 monitor = !record_enabled() || (_session.config.get_auto_input() && !_session.actively_recording());
349 declick = _pending_declick;
352 /* figure out if we're going to use gain automation */
353 _amp->setup_gain_automation (start_frame, end_frame, nframes);
356 /* tell main outs what to do about monitoring */
357 _main_outs->no_outs_cuz_we_no_monitor (!monitor);
360 /* -------------------------------------------------------------------------------------------
361 GLOBAL DECLICK (for transport changes etc.)
362 ----------------------------------------------------------------------------------------- */
365 Amp::apply_gain (bufs, nframes, 0.0, 1.0);
366 } else if (declick < 0) {
367 Amp::apply_gain (bufs, nframes, 1.0, 0.0);
370 _pending_declick = 0;
372 /* -------------------------------------------------------------------------------------------
373 DENORMAL CONTROL/PHASE INVERT
374 ----------------------------------------------------------------------------------------- */
380 if (_denormal_protection || Config->get_denormal_protection()) {
382 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i, ++chn) {
383 Sample* const sp = i->data();
385 if (_phase_invert & chn) {
386 for (nframes_t nx = 0; nx < nframes; ++nx) {
391 for (nframes_t nx = 0; nx < nframes; ++nx) {
399 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i, ++chn) {
400 Sample* const sp = i->data();
402 if (_phase_invert & chn) {
403 for (nframes_t nx = 0; nx < nframes; ++nx) {
412 if (_denormal_protection || Config->get_denormal_protection()) {
414 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
415 Sample* const sp = i->data();
416 for (nframes_t nx = 0; nx < nframes; ++nx) {
424 /* -------------------------------------------------------------------------------------------
426 ----------------------------------------------------------------------------------------- */
428 Glib::RWLock::ReaderLock rm (_processor_lock, Glib::TRY_LOCK);
431 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
433 if (bufs.count() != (*i)->input_streams()) {
434 cerr << _name << " bufs = " << bufs.count()
435 << " input for " << (*i)->name() << " = " << (*i)->input_streams()
438 assert (bufs.count() == (*i)->input_streams());
440 (*i)->run (bufs, start_frame, end_frame, nframes, *i != _processors.back());
441 bufs.set_count (ChanCount::max(bufs.count(), (*i)->output_streams()));
444 if (!_processors.empty()) {
445 bufs.set_count (ChanCount::max (bufs.count(), _processors.back()->output_streams()));
451 Route::n_process_buffers ()
453 return max (_input->n_ports(), processor_max_streams);
457 Route::passthru (sframes_t start_frame, sframes_t end_frame, nframes_t nframes, int declick)
459 BufferSet& bufs = _session.get_scratch_buffers (n_process_buffers());
463 assert (bufs.available() >= _input->n_ports());
465 if (_input->n_ports() == ChanCount::ZERO) {
469 bufs.set_count (_input->n_ports());
471 if (is_control() && _session.listening()) {
473 /* control/monitor bus ignores input ports when something is
474 feeding the listen "stream". data will "arrive" into the
475 route from the intreturn processor element.
478 bufs.silence (nframes, 0);
482 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
484 BufferSet::iterator o = bufs.begin(*t);
485 PortSet& ports (_input->ports());
487 for (PortSet::iterator i = ports.begin(*t); i != ports.end(*t); ++i, ++o) {
488 o->read_from (i->get_buffer(nframes), nframes);
493 write_out_of_band_data (bufs, start_frame, end_frame, nframes);
494 process_output_buffers (bufs, start_frame, end_frame, nframes, true, declick);
498 Route::passthru_silence (sframes_t start_frame, sframes_t end_frame, nframes_t nframes, int declick)
500 BufferSet& bufs (_session.get_silent_buffers (n_process_buffers()));
501 bufs.set_count (_input->n_ports());
502 write_out_of_band_data (bufs, start_frame, end_frame, nframes);
503 process_output_buffers (bufs, start_frame, end_frame, nframes, true, declick);
507 Route::set_listen (bool yn, void* src)
510 if (yn != _control_outs->active()) {
512 _control_outs->activate ();
514 _control_outs->deactivate ();
517 listen_changed (src); /* EMIT SIGNAL */
523 Route::listening () const
526 return _control_outs->active ();
533 Route::set_solo_safe (bool yn, void *src)
535 if (_solo_safe != yn) {
537 solo_safe_changed (src);
542 Route::solo_safe() const
548 Route::set_solo (bool yn, void *src)
554 if (_route_group && src != _route_group && _route_group->active_property (RouteGroup::Solo)) {
555 _route_group->apply (&Route::set_solo, yn, _route_group);
559 if (self_soloed() != yn) {
561 set_delivery_solo ();
562 solo_changed (src); /* EMIT SIGNAL */
563 _solo_control->Changed (); /* EMIT SIGNAL */
568 Route::set_self_solo (bool yn)
574 Route::mod_solo_by_others (int32_t delta)
577 if (_soloed_by_others >= (uint32_t) delta) {
578 _soloed_by_others += delta;
580 _soloed_by_others = 0;
583 _soloed_by_others += delta;
586 set_delivery_solo ();
590 Route::set_delivery_solo ()
592 /* tell all delivery processors what the solo situation is, so that they keep
593 delivering even though Session::soloing() is true and they were not
597 Glib::RWLock::ReaderLock rm (_processor_lock);
598 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
599 boost::shared_ptr<Delivery> d;
601 if ((d = boost::dynamic_pointer_cast<Delivery> (*i)) != 0) {
602 d->set_solo_level (soloed ());
603 d->set_solo_isolated (solo_isolated());
609 Route::set_solo_isolated (bool yn, void *src)
611 if (_route_group && src != _route_group && _route_group->active_property (RouteGroup::Solo)) {
612 _route_group->apply (&Route::set_solo_isolated, yn, _route_group);
616 if (yn != _solo_isolated) {
618 set_delivery_solo ();
619 solo_isolated_changed (src);
624 Route::solo_isolated () const
626 return _solo_isolated;
630 Route::set_mute_points (MuteMaster::MutePoint mp)
633 mute_points_changed (); /* EMIT SIGNAL */
635 if (_mute_master->muted()) {
636 _mute_master->mute_at (_mute_points);
637 mute_changed (this); /* EMIT SIGNAL */
642 Route::set_mute (bool yn, void *src)
644 if (_route_group && src != _route_group && _route_group->active_property (RouteGroup::Mute)) {
645 _route_group->apply (&Route::set_mute, yn, _route_group);
651 _mute_master->mute_at (_mute_points);
653 _mute_master->clear_mute ();
656 mute_changed (src); /* EMIT SIGNAL */
663 return _mute_master->muted ();
668 dump_processors(const string& name, const list<boost::shared_ptr<Processor> >& procs)
670 cerr << name << " {" << endl;
671 for (list<boost::shared_ptr<Processor> >::const_iterator p = procs.begin();
672 p != procs.end(); ++p) {
673 cerr << "\t" << (*p)->name() << " ID = " << (*p)->id() << endl;
680 Route::add_processor (boost::shared_ptr<Processor> processor, Placement placement, ProcessorStreams* err)
682 ProcessorList::iterator loc;
684 /* XXX this is not thread safe - we don't hold the lock across determining the iter
685 to add before and actually doing the insertion. dammit.
688 if (placement == PreFader) {
689 /* generic pre-fader: insert immediately before the amp */
690 loc = find (_processors.begin(), _processors.end(), _amp);
692 /* generic post-fader: insert right before the main outs */
693 loc = find (_processors.begin(), _processors.end(), _main_outs);
696 return add_processor (processor, loc, err);
700 /** Add a processor to the route.
701 * If @a iter is not NULL, it must point to an iterator in _processors and the new
702 * processor will be inserted immediately before this location. Otherwise,
703 * @a position is used.
706 Route::add_processor (boost::shared_ptr<Processor> processor, ProcessorList::iterator iter, ProcessorStreams* err)
708 ChanCount old_pms = processor_max_streams;
710 if (!_session.engine().connected() || !processor) {
715 Glib::RWLock::WriterLock lm (_processor_lock);
717 boost::shared_ptr<PluginInsert> pi;
718 boost::shared_ptr<PortInsert> porti;
720 ProcessorList::iterator loc = find(_processors.begin(), _processors.end(), processor);
722 if (processor == _amp || processor == _meter || processor == _main_outs) {
723 // Ensure only one of these are in the list at any time
724 if (loc != _processors.end()) {
725 if (iter == loc) { // Already in place, do nothing
727 } else { // New position given, relocate
728 _processors.erase (loc);
733 if (loc != _processors.end()) {
734 cerr << "ERROR: Processor added to route twice!" << endl;
741 _processors.insert (loc, processor);
743 // Set up processor list channels. This will set processor->[input|output]_streams(),
744 // configure redirect ports properly, etc.
746 if (configure_processors_unlocked (err)) {
747 ProcessorList::iterator ploc = loc;
749 _processors.erase(ploc);
750 configure_processors_unlocked (0); // it worked before we tried to add it ...
751 cerr << "configure failed\n";
755 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(processor)) != 0) {
757 if (pi->natural_input_streams() == ChanCount::ZERO) {
758 /* generator plugin */
759 _have_internal_generator = true;
764 if (_control_outs != processor) {
765 // XXX: do we want to emit the signal here ? change call order.
766 processor->activate ();
768 processor->ActiveChanged.connect (bind (mem_fun (_session, &Session::update_latency_compensation), false, false));
770 _output->set_user_latency (0);
773 processors_changed (); /* EMIT SIGNAL */
779 Route::add_processor_from_xml (const XMLNode& node, ProcessorList::iterator iter)
781 const XMLProperty *prop;
783 if (node.name() != "Processor") {
788 if ((prop = node.property ("type")) != 0) {
790 boost::shared_ptr<Processor> processor;
792 if (prop->value() == "ladspa" || prop->value() == "Ladspa" ||
793 prop->value() == "lv2" ||
794 prop->value() == "vst" ||
795 prop->value() == "audiounit") {
797 processor.reset (new PluginInsert(_session, node));
799 } else if (prop->value() == "port") {
801 processor.reset (new PortInsert (_session, _mute_master, node));
803 } else if (prop->value() == "send") {
805 processor.reset (new Send (_session, _mute_master, node));
807 } else if (prop->value() == "meter") {
810 if (_meter->set_state (node, Stateful::loading_state_version)) {
817 _meter.reset (new PeakMeter (_session, node));
818 _meter->set_display_to_user (_meter_point == MeterCustom);
821 } else if (prop->value() == "amp") {
823 /* amp always exists */
826 if (processor->set_state (node, Stateful::loading_state_version)) {
829 /* never any reason to add it */
833 } else if (prop->value() == "intsend") {
835 processor.reset (new InternalSend (_session, _mute_master, node));
837 } else if (prop->value() == "intreturn") {
840 if (_intreturn->set_state (node, Stateful::loading_state_version)) {
846 _intreturn.reset (new InternalReturn (_session, node));
847 processor = _intreturn;
849 } else if (prop->value() == "main-outs") {
852 if (_main_outs->set_state (node, Stateful::loading_state_version)) {
859 _main_outs.reset (new Delivery (_session, _output, _mute_master, node));
860 processor = _main_outs;
863 error << string_compose(_("unknown Processor type \"%1\"; ignored"), prop->value()) << endmsg;
867 if (iter == _processors.end() && processor->display_to_user() && !_processors.empty()) {
868 /* check for invisible processors stacked at the end and leave them there */
869 ProcessorList::iterator p;
870 p = _processors.end();
872 while (!(*p)->display_to_user() && p != _processors.begin()) {
879 return (add_processor (processor, iter) == 0);
882 error << _("Processor XML node has no type property") << endmsg;
887 catch (failed_constructor &err) {
888 warning << _("processor could not be created. Ignored.") << endmsg;
895 Route::add_processor_from_xml_2X (const XMLNode& node, int version, ProcessorList::iterator iter)
897 const XMLProperty *prop;
900 boost::shared_ptr<Processor> processor;
902 if (node.name() == "Insert") {
904 if ((prop = node.property ("type")) != 0) {
906 if (prop->value() == "ladspa" || prop->value() == "Ladspa" ||
907 prop->value() == "lv2" ||
908 prop->value() == "vst" ||
909 prop->value() == "audiounit") {
911 processor.reset (new PluginInsert (_session, node));
915 processor.reset (new PortInsert (_session, _mute_master, node));
920 } else if (node.name() == "Send") {
922 processor.reset (new Send (_session, _mute_master, node, version));
926 error << string_compose(_("unknown Processor type \"%1\"; ignored"), node.name()) << endmsg;
930 if (iter == _processors.end() && processor->display_to_user() && !_processors.empty()) {
931 /* check for invisible processors stacked at the end and leave them there */
932 ProcessorList::iterator p;
933 p = _processors.end();
935 while (!(*p)->display_to_user() && p != _processors.begin()) {
942 return (add_processor (processor, iter) == 0);
945 catch (failed_constructor &err) {
946 warning << _("processor could not be created. Ignored.") << endmsg;
952 Route::add_processors (const ProcessorList& others, boost::shared_ptr<Processor> before, ProcessorStreams* err)
954 ProcessorList::iterator loc;
957 loc = find(_processors.begin(), _processors.end(), before);
959 /* nothing specified - at end but before main outs */
960 loc = find (_processors.begin(), _processors.end(), _main_outs);
963 return add_processors (others, loc, err);
967 Route::add_processors (const ProcessorList& others, ProcessorList::iterator iter, ProcessorStreams* err)
969 /* NOTE: this is intended to be used ONLY when copying
970 processors from another Route. Hence the subtle
971 differences between this and ::add_processor()
974 ChanCount old_pms = processor_max_streams;
976 if (!_session.engine().connected()) {
980 if (others.empty()) {
985 Glib::RWLock::WriterLock lm (_processor_lock);
986 ProcessorList::iterator existing_end = _processors.end();
989 ChanCount potential_max_streams = ChanCount::max (_input->n_ports(), _output->n_ports());
991 for (ProcessorList::const_iterator i = others.begin(); i != others.end(); ++i) {
993 // Ensure meter only appears in the list once
995 ProcessorList::iterator m = find(_processors.begin(), _processors.end(), *i);
996 if (m != _processors.end()) {
997 _processors.erase(m);
1001 boost::shared_ptr<PluginInsert> pi;
1003 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(*i)) != 0) {
1006 ChanCount m = max (pi->input_streams(), pi->output_streams());
1008 if (m > potential_max_streams) {
1009 potential_max_streams = m;
1013 _processors.insert (iter, *i);
1015 if (configure_processors_unlocked (err)) {
1017 _processors.erase (existing_end, _processors.end());
1018 configure_processors_unlocked (0); // it worked before we tried to add it ...
1022 (*i)->ActiveChanged.connect (bind (mem_fun (_session, &Session::update_latency_compensation), false, false));
1025 _output->set_user_latency (0);
1028 processors_changed (); /* EMIT SIGNAL */
1034 Route::placement_range(Placement p, ProcessorList::iterator& start, ProcessorList::iterator& end)
1036 if (p == PreFader) {
1037 start = _processors.begin();
1038 end = find(_processors.begin(), _processors.end(), _amp);
1040 start = find(_processors.begin(), _processors.end(), _amp);
1042 end = _processors.end();
1046 /** Turn off all processors with a given placement
1047 * @param p Placement of processors to disable
1050 Route::disable_processors (Placement p)
1052 Glib::RWLock::ReaderLock lm (_processor_lock);
1054 ProcessorList::iterator start, end;
1055 placement_range(p, start, end);
1057 for (ProcessorList::iterator i = start; i != end; ++i) {
1058 (*i)->deactivate ();
1061 _session.set_dirty ();
1064 /** Turn off all redirects
1067 Route::disable_processors ()
1069 Glib::RWLock::ReaderLock lm (_processor_lock);
1071 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1072 (*i)->deactivate ();
1075 _session.set_dirty ();
1078 /** Turn off all redirects with a given placement
1079 * @param p Placement of redirects to disable
1082 Route::disable_plugins (Placement p)
1084 Glib::RWLock::ReaderLock lm (_processor_lock);
1086 ProcessorList::iterator start, end;
1087 placement_range(p, start, end);
1089 for (ProcessorList::iterator i = start; i != end; ++i) {
1090 if (boost::dynamic_pointer_cast<PluginInsert> (*i)) {
1091 (*i)->deactivate ();
1095 _session.set_dirty ();
1098 /** Turn off all plugins
1101 Route::disable_plugins ()
1103 Glib::RWLock::ReaderLock lm (_processor_lock);
1105 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1106 if (boost::dynamic_pointer_cast<PluginInsert> (*i)) {
1107 (*i)->deactivate ();
1111 _session.set_dirty ();
1116 Route::ab_plugins (bool forward)
1118 Glib::RWLock::ReaderLock lm (_processor_lock);
1122 /* forward = turn off all active redirects, and mark them so that the next time
1123 we go the other way, we will revert them
1126 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1127 if (!boost::dynamic_pointer_cast<PluginInsert> (*i)) {
1131 if ((*i)->active()) {
1132 (*i)->deactivate ();
1133 (*i)->set_next_ab_is_active (true);
1135 (*i)->set_next_ab_is_active (false);
1141 /* backward = if the redirect was marked to go active on the next ab, do so */
1143 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1145 if (!boost::dynamic_pointer_cast<PluginInsert> (*i)) {
1149 if ((*i)->get_next_ab_is_active()) {
1152 (*i)->deactivate ();
1157 _session.set_dirty ();
1161 /** Remove processors with a given placement.
1162 * @param p Placement of processors to remove.
1165 Route::clear_processors (Placement p)
1167 const ChanCount old_pms = processor_max_streams;
1169 if (!_session.engine().connected()) {
1173 bool already_deleting = _session.deletion_in_progress();
1174 if (!already_deleting) {
1175 _session.set_deletion_in_progress();
1179 Glib::RWLock::WriterLock lm (_processor_lock);
1180 ProcessorList new_list;
1181 ProcessorStreams err;
1182 bool seen_amp = false;
1184 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1190 if ((*i) == _amp || (*i) == _meter || (*i) == _main_outs) {
1192 /* you can't remove these */
1194 new_list.push_back (*i);
1201 new_list.push_back (*i);
1204 (*i)->drop_references ();
1212 (*i)->drop_references ();
1215 new_list.push_back (*i);
1222 _processors = new_list;
1223 configure_processors_unlocked (&err); // this can't fail
1226 processor_max_streams.reset();
1227 _have_internal_generator = false;
1228 processors_changed (); /* EMIT SIGNAL */
1230 if (!already_deleting) {
1231 _session.clear_deletion_in_progress();
1236 Route::remove_processor (boost::shared_ptr<Processor> processor, ProcessorStreams* err)
1238 /* these can never be removed */
1240 if (processor == _amp || processor == _meter || processor == _main_outs) {
1244 ChanCount old_pms = processor_max_streams;
1246 if (!_session.engine().connected()) {
1250 processor_max_streams.reset();
1253 Glib::RWLock::WriterLock lm (_processor_lock);
1254 ProcessorList::iterator i;
1255 bool removed = false;
1257 for (i = _processors.begin(); i != _processors.end(); ) {
1258 if (*i == processor) {
1260 /* move along, see failure case for configure_processors()
1261 where we may need to reconfigure the processor.
1264 /* stop redirects that send signals to JACK ports
1265 from causing noise as a result of no longer being
1269 boost::shared_ptr<IOProcessor> iop;
1271 if ((iop = boost::dynamic_pointer_cast<IOProcessor> (*i)) != 0) {
1273 iop->input()->disconnect (this);
1275 if (iop->output()) {
1276 iop->output()->disconnect (this);
1280 i = _processors.erase (i);
1288 _output->set_user_latency (0);
1296 if (configure_processors_unlocked (err)) {
1297 /* get back to where we where */
1298 _processors.insert (i, processor);
1299 /* we know this will work, because it worked before :) */
1300 configure_processors_unlocked (0);
1304 _have_internal_generator = false;
1306 for (i = _processors.begin(); i != _processors.end(); ++i) {
1307 boost::shared_ptr<PluginInsert> pi;
1309 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(*i)) != 0) {
1310 if (pi->is_generator()) {
1311 _have_internal_generator = true;
1318 processor->drop_references ();
1319 processors_changed (); /* EMIT SIGNAL */
1325 Route::remove_processors (const ProcessorList& to_be_deleted, ProcessorStreams* err)
1327 ProcessorList deleted;
1328 ProcessorList as_we_were;
1330 if (!_session.engine().connected()) {
1334 processor_max_streams.reset();
1337 Glib::RWLock::WriterLock lm (_processor_lock);
1338 ProcessorList::iterator i;
1339 boost::shared_ptr<Processor> processor;
1341 as_we_were = _processors;
1343 for (i = _processors.begin(); i != _processors.end(); ) {
1347 /* these can never be removed */
1349 if (processor == _amp || processor == _meter || processor == _main_outs) {
1354 /* see if its in the list of processors to delete */
1356 if (find (to_be_deleted.begin(), to_be_deleted.end(), processor) == to_be_deleted.end()) {
1361 /* stop IOProcessors that send to JACK ports
1362 from causing noise as a result of no longer being
1366 boost::shared_ptr<IOProcessor> iop;
1368 if ((iop = boost::dynamic_pointer_cast<IOProcessor> (processor)) != 0) {
1372 deleted.push_back (processor);
1373 i = _processors.erase (i);
1376 if (deleted.empty()) {
1377 /* none of those in the requested list were found */
1381 _output->set_user_latency (0);
1383 if (configure_processors_unlocked (err)) {
1384 /* get back to where we where */
1385 _processors = as_we_were;
1386 /* we know this will work, because it worked before :) */
1387 configure_processors_unlocked (0);
1391 _have_internal_generator = false;
1393 for (i = _processors.begin(); i != _processors.end(); ++i) {
1394 boost::shared_ptr<PluginInsert> pi;
1396 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(*i)) != 0) {
1397 if (pi->is_generator()) {
1398 _have_internal_generator = true;
1405 /* now try to do what we need to so that those that were removed will be deleted */
1407 for (ProcessorList::iterator i = deleted.begin(); i != deleted.end(); ++i) {
1408 (*i)->drop_references ();
1411 processors_changed (); /* EMIT SIGNAL */
1418 Route::configure_processors (ProcessorStreams* err)
1420 if (!_in_configure_processors) {
1421 Glib::RWLock::WriterLock lm (_processor_lock);
1422 return configure_processors_unlocked (err);
1427 /** Configure the input/output configuration of each processor in the processors list.
1428 * Return 0 on success, otherwise configuration is impossible.
1431 Route::configure_processors_unlocked (ProcessorStreams* err)
1433 if (_in_configure_processors) {
1437 _in_configure_processors = true;
1439 // Check each processor in order to see if we can configure as requested
1440 ChanCount in = _input->n_ports ();
1442 list< pair<ChanCount,ChanCount> > configuration;
1445 DEBUG_TRACE (DEBUG::Processors, string_compose ("%1: configure processors\n", _name));
1447 DEBUG_TRACE (DEBUG::Processors, "{\n");
1448 for (list<boost::shared_ptr<Processor> >::const_iterator p = _processors.begin(); p != _processors.end(); ++p) {
1449 DEBUG_TRACE (DEBUG::Processors, string_compose ("\t%1 ID = %2\n", (*p)->name(), (*p)->id()));
1451 DEBUG_TRACE (DEBUG::Processors, "}\n");
1454 for (ProcessorList::iterator p = _processors.begin(); p != _processors.end(); ++p, ++index) {
1456 if ((*p)->can_support_io_configuration(in, out)) {
1457 DEBUG_TRACE (DEBUG::Processors, string_compose ("\t%1in = %2 out = %3\n",(*p)->name(), in, out));
1458 configuration.push_back(make_pair(in, out));
1465 _in_configure_processors = false;
1470 /* Take the process lock so that if we add a processor which increases the required
1471 number of scratch buffers, we create those scratch buffers before the process
1472 thread has a chance to ask for them.
1473 XXX: in an ideal world we'd perhaps use some RCU magic to avoid having to take
1477 Glib::Mutex::Lock pl (_session.engine().process_lock ());
1479 // We can, so configure everything
1480 list< pair<ChanCount,ChanCount> >::iterator c = configuration.begin();
1481 for (ProcessorList::iterator p = _processors.begin(); p != _processors.end(); ++p, ++c) {
1482 (*p)->configure_io(c->first, c->second);
1483 processor_max_streams = ChanCount::max(processor_max_streams, c->first);
1484 processor_max_streams = ChanCount::max(processor_max_streams, c->second);
1488 /* make sure we have sufficient scratch buffers to cope with the new processor
1490 _session.ensure_buffers (n_process_buffers ());
1492 _in_configure_processors = false;
1497 Route::all_processors_flip ()
1499 Glib::RWLock::ReaderLock lm (_processor_lock);
1501 if (_processors.empty()) {
1505 bool first_is_on = _processors.front()->active();
1507 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1509 (*i)->deactivate ();
1515 _session.set_dirty ();
1518 /** Set all processors with a given placement to a given active state.
1519 * @param p Placement of processors to change.
1520 * @param state New active state for those processors.
1523 Route::all_processors_active (Placement p, bool state)
1525 Glib::RWLock::ReaderLock lm (_processor_lock);
1527 if (_processors.empty()) {
1530 ProcessorList::iterator start, end;
1531 placement_range(p, start, end);
1533 bool before_amp = true;
1534 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1539 if (p == PreFader && before_amp) {
1543 (*i)->deactivate ();
1548 _session.set_dirty ();
1552 Route::processor_is_prefader (boost::shared_ptr<Processor> p)
1554 bool pre_fader = true;
1555 Glib::RWLock::ReaderLock lm (_processor_lock);
1557 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1559 /* semantic note: if p == amp, we want to return true, so test
1560 for equality before checking if this is the amp
1577 Route::reorder_processors (const ProcessorList& new_order, ProcessorStreams* err)
1579 /* "new_order" is an ordered list of processors to be positioned according to "placement".
1580 NOTE: all processors in "new_order" MUST be marked as display_to_user(). There maybe additional
1581 processors in the current actual processor list that are hidden. Any visible processors
1582 in the current list but not in "new_order" will be assumed to be deleted.
1586 Glib::RWLock::WriterLock lm (_processor_lock);
1587 ChanCount old_pms = processor_max_streams;
1588 ProcessorList::iterator oiter;
1589 ProcessorList::const_iterator niter;
1590 ProcessorList as_it_was_before = _processors;
1591 ProcessorList as_it_will_be;
1593 oiter = _processors.begin();
1594 niter = new_order.begin();
1596 while (niter != new_order.end()) {
1598 /* if the next processor in the old list is invisible (i.e. should not be in the new order)
1599 then append it to the temp list.
1601 Otherwise, see if the next processor in the old list is in the new list. if not,
1602 its been deleted. If its there, append it to the temp list.
1605 if (oiter == _processors.end()) {
1607 /* no more elements in the old list, so just stick the rest of
1608 the new order onto the temp list.
1611 as_it_will_be.insert (as_it_will_be.end(), niter, new_order.end());
1612 while (niter != new_order.end()) {
1619 if (!(*oiter)->display_to_user()) {
1621 as_it_will_be.push_back (*oiter);
1625 /* visible processor: check that its in the new order */
1627 if (find (new_order.begin(), new_order.end(), (*oiter)) == new_order.end()) {
1628 /* deleted: do nothing, shared_ptr<> will clean up */
1630 /* ignore this one, and add the next item from the new order instead */
1631 as_it_will_be.push_back (*niter);
1636 /* now remove from old order - its taken care of no matter what */
1637 oiter = _processors.erase (oiter);
1642 _processors.insert (oiter, as_it_will_be.begin(), as_it_will_be.end());
1644 if (configure_processors_unlocked (err)) {
1645 _processors = as_it_was_before;
1646 processor_max_streams = old_pms;
1651 processors_changed (); /* EMIT SIGNAL */
1663 Route::get_template()
1665 return state(false);
1669 Route::state(bool full_state)
1671 XMLNode *node = new XMLNode("Route");
1672 ProcessorList::iterator i;
1675 id().print (buf, sizeof (buf));
1676 node->add_property("id", buf);
1677 node->add_property ("name", _name);
1678 node->add_property("default-type", _default_type.to_string());
1681 node->add_property("flags", enum_2_string (_flags));
1684 node->add_property("active", _active?"yes":"no");
1685 node->add_property("phase-invert", _phase_invert?"yes":"no");
1686 node->add_property("denormal-protection", _denormal_protection?"yes":"no");
1687 node->add_property("meter-point", enum_2_string (_meter_point));
1690 node->add_property("route-group", _route_group->name());
1693 string order_string;
1694 OrderKeys::iterator x = order_keys.begin();
1696 while (x != order_keys.end()) {
1697 order_string += string ((*x).first);
1698 order_string += '=';
1699 snprintf (buf, sizeof(buf), "%ld", (*x).second);
1700 order_string += buf;
1704 if (x == order_keys.end()) {
1708 order_string += ':';
1710 node->add_property ("order-keys", order_string);
1711 node->add_property ("self-solo", (_self_solo ? "yes" : "no"));
1712 snprintf (buf, sizeof (buf), "%d", _soloed_by_others);
1713 node->add_property ("soloed-by-others", buf);
1715 node->add_child_nocopy (_input->state (full_state));
1716 node->add_child_nocopy (_output->state (full_state));
1717 node->add_child_nocopy (_solo_control->get_state ());
1718 node->add_child_nocopy (_mute_master->get_state ());
1720 XMLNode* remote_control_node = new XMLNode (X_("RemoteControl"));
1721 snprintf (buf, sizeof (buf), "%d", _remote_control_id);
1722 remote_control_node->add_property (X_("id"), buf);
1723 node->add_child_nocopy (*remote_control_node);
1725 if (_comment.length()) {
1726 XMLNode *cmt = node->add_child ("Comment");
1727 cmt->add_content (_comment);
1730 for (i = _processors.begin(); i != _processors.end(); ++i) {
1731 node->add_child_nocopy((*i)->state (full_state));
1735 node->add_child_copy (*_extra_xml);
1742 Route::set_state (const XMLNode& node, int version)
1744 return _set_state (node, version, true);
1748 Route::_set_state (const XMLNode& node, int version, bool /*call_base*/)
1750 if (version < 3000) {
1751 return _set_state_2X (node, version);
1755 XMLNodeConstIterator niter;
1757 XMLPropertyList plist;
1758 const XMLProperty *prop;
1760 if (node.name() != "Route"){
1761 error << string_compose(_("Bad node sent to Route::set_state() [%1]"), node.name()) << endmsg;
1765 if ((prop = node.property (X_("name"))) != 0) {
1766 Route::set_name (prop->value());
1769 if ((prop = node.property ("id")) != 0) {
1770 _id = prop->value ();
1773 if ((prop = node.property (X_("flags"))) != 0) {
1774 _flags = Flag (string_2_enum (prop->value(), _flags));
1779 /* add all processors (except amp, which is always present) */
1781 nlist = node.children();
1782 XMLNode processor_state (X_("processor_state"));
1784 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
1788 if (child->name() == IO::state_node_name) {
1789 if ((prop = child->property (X_("direction"))) == 0) {
1793 if (prop->value() == "Input") {
1794 _input->set_state (*child, version);
1795 } else if (prop->value() == "Output") {
1796 _output->set_state (*child, version);
1800 if (child->name() == X_("Processor")) {
1801 processor_state.add_child_copy (*child);
1805 set_processor_state (processor_state);
1807 if ((prop = node.property ("self-solo")) != 0) {
1808 set_self_solo (string_is_affirmative (prop->value()));
1811 if ((prop = node.property ("soloed-by-others")) != 0) {
1812 _soloed_by_others = 0; // needed for mod_solo_by_others () to work
1813 mod_solo_by_others (atoi (prop->value()));
1816 if ((prop = node.property ("solo-isolated")) != 0) {
1817 set_solo_isolated (string_is_affirmative (prop->value()), this);
1820 if ((prop = node.property (X_("phase-invert"))) != 0) {
1821 set_phase_invert (string_is_affirmative (prop->value()));
1824 if ((prop = node.property (X_("denormal-protection"))) != 0) {
1825 set_denormal_protection (string_is_affirmative (prop->value()));
1828 if ((prop = node.property (X_("active"))) != 0) {
1829 bool yn = string_is_affirmative (prop->value());
1830 _active = !yn; // force switch
1834 if ((prop = node.property (X_("meter-point"))) != 0) {
1835 _meter_point = MeterPoint (string_2_enum (prop->value (), _meter_point));
1837 _meter->set_display_to_user (_meter_point == MeterCustom);
1841 if ((prop = node.property (X_("route-group"))) != 0) {
1842 RouteGroup* route_group = _session.route_group_by_name(prop->value());
1843 if (route_group == 0) {
1844 error << string_compose(_("Route %1: unknown route group \"%2 in saved state (ignored)"), _name, prop->value()) << endmsg;
1846 set_route_group (route_group, this);
1850 if ((prop = node.property (X_("order-keys"))) != 0) {
1854 string::size_type colon, equal;
1855 string remaining = prop->value();
1857 while (remaining.length()) {
1859 if ((equal = remaining.find_first_of ('=')) == string::npos || equal == remaining.length()) {
1860 error << string_compose (_("badly formed order key string in state file! [%1] ... ignored."), remaining)
1863 if (sscanf (remaining.substr (equal+1).c_str(), "%ld", &n) != 1) {
1864 error << string_compose (_("badly formed order key string in state file! [%1] ... ignored."), remaining)
1867 set_order_key (remaining.substr (0, equal), n);
1871 colon = remaining.find_first_of (':');
1873 if (colon != string::npos) {
1874 remaining = remaining.substr (colon+1);
1881 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
1884 if (child->name() == X_("Comment")) {
1886 /* XXX this is a terrible API design in libxml++ */
1888 XMLNode *cmt = *(child->children().begin());
1889 _comment = cmt->content();
1891 } else if (child->name() == X_("Extra")) {
1893 _extra_xml = new XMLNode (*child);
1895 } else if (child->name() == X_("Controllable") && (prop = child->property("name")) != 0) {
1897 if (prop->value() == "solo") {
1898 _solo_control->set_state (*child, version);
1899 _session.add_controllable (_solo_control);
1902 } else if (child->name() == X_("RemoteControl")) {
1903 if ((prop = child->property (X_("id"))) != 0) {
1905 sscanf (prop->value().c_str(), "%d", &x);
1906 set_remote_control_id (x);
1909 } else if (child->name() == X_("MuteMaster")) {
1910 _mute_master->set_state (*child, version);
1918 Route::_set_state_2X (const XMLNode& node, int version)
1921 XMLNodeConstIterator niter;
1923 XMLPropertyList plist;
1924 const XMLProperty *prop;
1926 /* 2X things which still remain to be handled:
1929 * mute-affects-pre-fader
1930 * mute-affects-post-fader
1931 * mute-affects-control-outs
1932 * mute-affects-main-outs
1937 if (node.name() != "Route") {
1938 error << string_compose(_("Bad node sent to Route::set_state() [%1]"), node.name()) << endmsg;
1942 if ((prop = node.property (X_("flags"))) != 0) {
1943 _flags = Flag (string_2_enum (prop->value(), _flags));
1948 /* add standard processors */
1950 _meter.reset (new PeakMeter (_session));
1951 add_processor (_meter, PreFader);
1953 if (_flags & ControlOut) {
1954 /* where we listen to tracks */
1955 _intreturn.reset (new InternalReturn (_session));
1956 add_processor (_intreturn, PreFader);
1959 _main_outs.reset (new Delivery (_session, _output, _mute_master, _name, Delivery::Main));
1960 add_processor (_main_outs, PostFader);
1964 nlist = node.children ();
1965 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1969 if (child->name() == IO::state_node_name) {
1971 /* there is a note in IO::set_state_2X() about why we have to call
1975 _input->set_state_2X (*child, version, true);
1976 _output->set_state_2X (*child, version, false);
1978 if ((prop = child->property (X_("name"))) != 0) {
1979 set_name (prop->value ());
1982 if ((prop = child->property (X_("id"))) != 0) {
1983 _id = prop->value ();
1986 if ((prop = child->property (X_("active"))) != 0) {
1987 bool yn = string_is_affirmative (prop->value());
1988 _active = !yn; // force switch
1996 if ((prop = node.property (X_("phase-invert"))) != 0) {
1997 set_phase_invert (string_is_affirmative (prop->value()));
2000 if ((prop = node.property (X_("denormal-protection"))) != 0) {
2001 set_denormal_protection (string_is_affirmative (prop->value()));
2004 if ((prop = node.property (X_("soloed"))) != 0) {
2005 bool yn = string_is_affirmative (prop->value());
2007 /* XXX force reset of solo status */
2009 set_solo (yn, this);
2012 if ((prop = node.property (X_("meter-point"))) != 0) {
2013 _meter_point = MeterPoint (string_2_enum (prop->value (), _meter_point));
2016 /* XXX: if the route was in both a mix group and an edit group, it'll end up
2017 just in the edit group. */
2019 if ((prop = node.property (X_("mix-group"))) != 0) {
2020 RouteGroup* route_group = _session.route_group_by_name(prop->value());
2021 if (route_group == 0) {
2022 error << string_compose(_("Route %1: unknown route group \"%2 in saved state (ignored)"), _name, prop->value()) << endmsg;
2024 set_route_group (route_group, this);
2028 if ((prop = node.property (X_("edit-group"))) != 0) {
2029 RouteGroup* route_group = _session.route_group_by_name(prop->value());
2030 if (route_group == 0) {
2031 error << string_compose(_("Route %1: unknown route group \"%2 in saved state (ignored)"), _name, prop->value()) << endmsg;
2033 set_route_group (route_group, this);
2037 if ((prop = node.property (X_("order-keys"))) != 0) {
2041 string::size_type colon, equal;
2042 string remaining = prop->value();
2044 while (remaining.length()) {
2046 if ((equal = remaining.find_first_of ('=')) == string::npos || equal == remaining.length()) {
2047 error << string_compose (_("badly formed order key string in state file! [%1] ... ignored."), remaining)
2050 if (sscanf (remaining.substr (equal+1).c_str(), "%ld", &n) != 1) {
2051 error << string_compose (_("badly formed order key string in state file! [%1] ... ignored."), remaining)
2054 set_order_key (remaining.substr (0, equal), n);
2058 colon = remaining.find_first_of (':');
2060 if (colon != string::npos) {
2061 remaining = remaining.substr (colon+1);
2068 XMLNodeList redirect_nodes;
2070 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
2074 if (child->name() == X_("Send") || child->name() == X_("Insert")) {
2075 redirect_nodes.push_back(child);
2080 set_processor_state_2X (redirect_nodes, version);
2082 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
2085 if (child->name() == X_("Comment")) {
2087 /* XXX this is a terrible API design in libxml++ */
2089 XMLNode *cmt = *(child->children().begin());
2090 _comment = cmt->content();
2092 } else if (child->name() == X_("Extra")) {
2094 _extra_xml = new XMLNode (*child);
2096 } else if (child->name() == X_("Controllable") && (prop = child->property("name")) != 0) {
2098 if (prop->value() == "solo") {
2099 _solo_control->set_state (*child, version);
2100 _session.add_controllable (_solo_control);
2103 } else if (child->name() == X_("RemoteControl")) {
2104 if ((prop = child->property (X_("id"))) != 0) {
2106 sscanf (prop->value().c_str(), "%d", &x);
2107 set_remote_control_id (x);
2117 Route::get_processor_state ()
2119 XMLNode* root = new XMLNode (X_("redirects"));
2120 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
2121 root->add_child_nocopy ((*i)->state (true));
2128 Route::set_processor_state_2X (XMLNodeList const & nList, int version)
2130 /* We don't bother removing existing processors not in nList, as this
2131 method will only be called when creating a Route from scratch, not
2132 for undo purposes. Just put processors in at the appropriate place
2136 for (XMLNodeConstIterator i = nList.begin(); i != nList.end(); ++i) {
2137 add_processor_from_xml_2X (**i, version, _processors.begin ());
2142 Route::set_processor_state (const XMLNode& node)
2144 const XMLNodeList &nlist = node.children();
2145 XMLNodeConstIterator niter;
2146 ProcessorList::iterator i, o;
2148 // Iterate through existing processors, remove those which are not in the state list
2150 for (i = _processors.begin(); i != _processors.end(); ) {
2152 /* leave amp alone, always */
2159 ProcessorList::iterator tmp = i;
2162 bool processorInStateList = false;
2164 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2166 XMLProperty* id_prop = (*niter)->property(X_("id"));
2168 if (id_prop && (*i)->id() == id_prop->value()) {
2169 processorInStateList = true;
2174 if (!processorInStateList) {
2175 remove_processor (*i);
2181 // Iterate through state list and make sure all processors are on the track and in the correct order,
2182 // set the state of existing processors according to the new state on the same go
2184 i = _processors.begin();
2186 for (niter = nlist.begin(); niter != nlist.end(); ++niter, ++i) {
2188 XMLProperty* prop = (*niter)->property ("type");
2192 // Check whether the next processor in the list is the right one,
2193 // except for "amp" which is always there and may not have the
2194 // old ID since it is always created anew in every Route
2196 if (prop->value() != "amp") {
2197 while (o != _processors.end()) {
2198 XMLProperty* id_prop = (*niter)->property(X_("id"));
2199 if (id_prop && (*o)->id() == id_prop->value()) {
2207 // If the processor (*niter) is not on the route,
2208 // create it and move it to the correct location
2210 if (o == _processors.end()) {
2212 if (add_processor_from_xml (**niter, i)) {
2213 --i; // move iterator to the newly inserted processor
2215 cerr << "Error restoring route: unable to restore processor" << endl;
2220 // Otherwise, the processor already exists; just
2221 // ensure it is at the location provided in the XML state
2224 boost::shared_ptr<Processor> tmp = (*o);
2225 _processors.erase (o); // remove the old copy
2226 _processors.insert (i, tmp); // insert the processor at the correct location
2227 --i; // move iterator to the correct processor
2230 // and make it (just) so
2232 (*i)->set_state (**niter, Stateful::current_state_version);
2236 /* note: there is no configure_processors() call because we figure that
2237 the XML state represents a working signal route.
2240 processors_changed ();
2244 Route::curve_reallocate ()
2246 // _gain_automation_curve.finish_resize ();
2247 // _pan_automation_curve.finish_resize ();
2251 Route::silence (nframes_t nframes)
2255 _output->silence (nframes);
2258 Glib::RWLock::ReaderLock lm (_processor_lock, Glib::TRY_LOCK);
2261 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
2262 boost::shared_ptr<PluginInsert> pi;
2264 if (!_active && (pi = boost::dynamic_pointer_cast<PluginInsert> (*i)) != 0) {
2265 // skip plugins, they don't need anything when we're not active
2269 (*i)->silence (nframes);
2272 if (nframes == _session.get_block_size()) {
2282 Route::add_internal_return ()
2285 _intreturn.reset (new InternalReturn (_session));
2286 add_processor (_intreturn, PreFader);
2291 Route::get_return_buffer () const
2293 Glib::RWLock::ReaderLock rm (_processor_lock);
2295 for (ProcessorList::const_iterator x = _processors.begin(); x != _processors.end(); ++x) {
2296 boost::shared_ptr<InternalReturn> d = boost::dynamic_pointer_cast<InternalReturn>(*x);
2299 BufferSet* bs = d->get_buffers ();
2308 Route::release_return_buffer () const
2310 Glib::RWLock::ReaderLock rm (_processor_lock);
2312 for (ProcessorList::const_iterator x = _processors.begin(); x != _processors.end(); ++x) {
2313 boost::shared_ptr<InternalReturn> d = boost::dynamic_pointer_cast<InternalReturn>(*x);
2316 return d->release_buffers ();
2322 Route::listen_via (boost::shared_ptr<Route> route, Placement placement, bool /*active*/, bool aux)
2324 vector<string> ports;
2325 vector<string>::const_iterator i;
2328 Glib::RWLock::ReaderLock rm (_processor_lock);
2330 for (ProcessorList::iterator x = _processors.begin(); x != _processors.end(); ++x) {
2332 boost::shared_ptr<InternalSend> d = boost::dynamic_pointer_cast<InternalSend>(*x);
2334 if (d && d->target_route() == route) {
2336 /* if the target is the control outs, then make sure
2337 we take note of which i-send is doing that.
2340 if (route == _session.control_out()) {
2341 _control_outs = boost::dynamic_pointer_cast<Delivery>(d);
2344 /* already listening via the specified IO: do nothing */
2351 boost::shared_ptr<InternalSend> listener;
2354 listener.reset (new InternalSend (_session, _mute_master, route, (aux ? Delivery::Aux : Delivery::Listen)));
2356 } catch (failed_constructor& err) {
2360 if (route == _session.control_out()) {
2361 _control_outs = listener;
2364 add_processor (listener, placement);
2370 Route::drop_listen (boost::shared_ptr<Route> route)
2372 ProcessorStreams err;
2373 ProcessorList::iterator tmp;
2375 Glib::RWLock::ReaderLock rl(_processor_lock);
2379 for (ProcessorList::iterator x = _processors.begin(); x != _processors.end(); ) {
2381 boost::shared_ptr<InternalSend> d = boost::dynamic_pointer_cast<InternalSend>(*x);
2383 if (d && d->target_route() == route) {
2385 remove_processor (*x, &err);
2388 /* list could have been demolished while we dropped the lock
2398 if (route == _session.control_out()) {
2399 _control_outs.reset ();
2404 Route::set_route_group (RouteGroup *rg, void *src)
2406 if (rg == _route_group) {
2411 _route_group->remove (this);
2414 if ((_route_group = rg) != 0) {
2415 _route_group->add (this);
2418 _session.set_dirty ();
2419 route_group_changed (src); /* EMIT SIGNAL */
2423 Route::drop_route_group (void *src)
2426 _session.set_dirty ();
2427 route_group_changed (src); /* EMIT SIGNAL */
2431 Route::set_comment (string cmt, void *src)
2434 comment_changed (src);
2435 _session.set_dirty ();
2439 Route::feeds (boost::shared_ptr<Route> other, bool* only_send)
2441 DEBUG_TRACE (DEBUG::Graph, string_compose ("Feeds? %1\n", _name));
2443 if (_output->connected_to (other->input())) {
2444 DEBUG_TRACE (DEBUG::Graph, string_compose ("\tdirect FEEDS %2\n", other->name()));
2453 for (ProcessorList::iterator r = _processors.begin(); r != _processors.end(); r++) {
2455 boost::shared_ptr<IOProcessor> iop;
2457 if ((iop = boost::dynamic_pointer_cast<IOProcessor>(*r)) != 0) {
2458 if (iop->feeds (other)) {
2459 DEBUG_TRACE (DEBUG::Graph, string_compose ("\tIOP %1 does feed %2\n", iop->name(), other->name()));
2465 DEBUG_TRACE (DEBUG::Graph, string_compose ("\tIOP %1 does NOT feed %2\n", iop->name(), other->name()));
2468 DEBUG_TRACE (DEBUG::Graph, string_compose ("\tPROC %1 is not an IOP\n", (*r)->name()));
2473 DEBUG_TRACE (DEBUG::Graph, string_compose ("\tdoes NOT feed %1\n", other->name()));
2478 Route::handle_transport_stopped (bool /*abort_ignored*/, bool did_locate, bool can_flush_processors)
2480 nframes_t now = _session.transport_frame();
2483 Glib::RWLock::ReaderLock lm (_processor_lock);
2486 automation_snapshot (now, true);
2489 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
2491 if (Config->get_plugins_stop_with_transport() && can_flush_processors) {
2492 (*i)->deactivate ();
2496 (*i)->transport_stopped (now);
2500 _roll_delay = _initial_delay;
2504 Route::input_change_handler (IOChange change, void * /*src*/)
2506 if ((change & ConfigurationChanged)) {
2507 configure_processors (0);
2512 Route::output_change_handler (IOChange change, void * /*src*/)
2514 if ((change & ConfigurationChanged)) {
2516 /* XXX resize all listeners to match _main_outs? */
2518 // configure_processors (0);
2523 Route::pans_required () const
2525 if (n_outputs().n_audio() < 2) {
2529 return max (n_inputs ().n_audio(), processor_max_streams.n_audio());
2533 Route::no_roll (nframes_t nframes, sframes_t start_frame, sframes_t end_frame,
2534 bool session_state_changing, bool /*can_record*/, bool /*rec_monitors_input*/)
2536 if (n_outputs().n_total() == 0) {
2540 if (session_state_changing || !_active || n_inputs() == ChanCount::ZERO) {
2545 _amp->apply_gain_automation (false);
2546 passthru (start_frame, end_frame, nframes, 0);
2552 Route::check_initial_delay (nframes_t nframes, nframes_t& transport_frame)
2554 if (_roll_delay > nframes) {
2556 _roll_delay -= nframes;
2558 /* transport frame is not legal for caller to use */
2561 } else if (_roll_delay > 0) {
2563 nframes -= _roll_delay;
2564 silence (_roll_delay);
2565 /* we've written _roll_delay of samples into the
2566 output ports, so make a note of that for
2569 _main_outs->increment_output_offset (_roll_delay);
2570 transport_frame += _roll_delay;
2579 Route::roll (nframes_t nframes, sframes_t start_frame, sframes_t end_frame, int declick,
2580 bool /*can_record*/, bool /*rec_monitors_input*/)
2583 // automation snapshot can also be called from the non-rt context
2584 // and it uses the processor list, so we try to acquire the lock here
2585 Glib::RWLock::ReaderLock lm (_processor_lock, Glib::TRY_LOCK);
2588 automation_snapshot (_session.transport_frame(), false);
2592 if (n_outputs().n_total() == 0) {
2596 if (!_active || n_inputs().n_total() == 0) {
2601 nframes_t unused = 0;
2603 if ((nframes = check_initial_delay (nframes, unused)) == 0) {
2609 passthru (start_frame, end_frame, nframes, declick);
2615 Route::silent_roll (nframes_t nframes, sframes_t /*start_frame*/, sframes_t /*end_frame*/,
2616 bool /*can_record*/, bool /*rec_monitors_input*/)
2623 Route::toggle_monitor_input ()
2625 for (PortSet::iterator i = _input->ports().begin(); i != _input->ports().end(); ++i) {
2626 i->ensure_monitor_input( ! i->monitoring_input());
2631 Route::has_external_redirects () const
2633 // FIXME: what about sends? - they don't return a signal back to ardour?
2635 boost::shared_ptr<const PortInsert> pi;
2637 for (ProcessorList::const_iterator i = _processors.begin(); i != _processors.end(); ++i) {
2639 if ((pi = boost::dynamic_pointer_cast<const PortInsert>(*i)) != 0) {
2641 for (PortSet::const_iterator port = pi->output()->ports().begin(); port != pi->output()->ports().end(); ++port) {
2643 string port_name = port->name();
2644 string client_name = port_name.substr (0, port_name.find(':'));
2646 /* only say "yes" if the redirect is actually in use */
2648 if (client_name != "ardour" && pi->active()) {
2659 Route::flush_processors ()
2661 /* XXX shouldn't really try to take this lock, since
2662 this is called from the RT audio thread.
2665 Glib::RWLock::ReaderLock lm (_processor_lock);
2667 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
2668 (*i)->deactivate ();
2674 Route::set_meter_point (MeterPoint p, void *src)
2676 if (_meter_point == p) {
2681 Glib::RWLock::WriterLock lm (_processor_lock);
2682 ProcessorList as_it_was (_processors);
2684 if (p != MeterCustom) {
2685 // Move meter in the processors list to reflect the new position
2686 ProcessorList::iterator loc = find(_processors.begin(), _processors.end(), _meter);
2687 _processors.erase(loc);
2690 loc = _processors.begin();
2693 loc = find(_processors.begin(), _processors.end(), _amp);
2695 case MeterPostFader:
2696 loc = _processors.end();
2702 _processors.insert(loc, _meter);
2704 if (configure_processors_unlocked (0)) {
2705 _processors = as_it_was;
2706 configure_processors_unlocked (0); // it worked before we tried to add it ...
2710 _meter->set_display_to_user (false);
2714 // just make it visible and let the user move it
2716 _meter->set_display_to_user (true);
2722 meter_change (src); /* EMIT SIGNAL */
2723 processors_changed (); /* EMIT SIGNAL */
2724 _session.set_dirty ();
2728 Route::put_control_outs_at (Placement p)
2730 if (!_control_outs) {
2735 Glib::RWLock::WriterLock lm (_processor_lock);
2736 ProcessorList as_it_was (_processors);
2737 // Move meter in the processors list
2738 ProcessorList::iterator loc = find(_processors.begin(), _processors.end(), _control_outs);
2739 _processors.erase(loc);
2743 loc = find(_processors.begin(), _processors.end(), _amp);
2744 if (loc != _processors.begin()) {
2749 loc = find(_processors.begin(), _processors.end(), _amp);
2750 assert (loc != _processors.end());
2755 _processors.insert(loc, _control_outs);
2757 if (configure_processors_unlocked (0)) {
2758 _processors = as_it_was;
2759 configure_processors_unlocked (0); // it worked before we tried to add it ...
2764 processors_changed (); /* EMIT SIGNAL */
2765 _session.set_dirty ();
2769 Route::update_total_latency ()
2771 nframes_t old = _output->effective_latency();
2772 nframes_t own_latency = _output->user_latency();
2774 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
2775 if ((*i)->active ()) {
2776 own_latency += (*i)->signal_latency ();
2780 DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: internal redirect latency = %2\n", _name, own_latency));
2782 _output->set_port_latency (own_latency);
2784 if (_output->user_latency() == 0) {
2786 /* this (virtual) function is used for pure Routes,
2787 not derived classes like AudioTrack. this means
2788 that the data processed here comes from an input
2789 port, not prerecorded material, and therefore we
2790 have to take into account any input latency.
2793 own_latency += _input->signal_latency ();
2796 if (old != own_latency) {
2797 _output->set_latency_delay (own_latency);
2798 signal_latency_changed (); /* EMIT SIGNAL */
2801 DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: input latency = %2 total = %3\n", _name, _input->signal_latency(), own_latency));
2803 return _output->effective_latency ();
2807 Route::set_user_latency (nframes_t nframes)
2809 _output->set_user_latency (nframes);
2810 _session.update_latency_compensation (false, false);
2814 Route::set_latency_delay (nframes_t longest_session_latency)
2816 nframes_t old = _initial_delay;
2818 if (_output->effective_latency() < longest_session_latency) {
2819 _initial_delay = longest_session_latency - _output->effective_latency();
2824 if (_initial_delay != old) {
2825 initial_delay_changed (); /* EMIT SIGNAL */
2828 if (_session.transport_stopped()) {
2829 _roll_delay = _initial_delay;
2834 Route::automation_snapshot (nframes_t now, bool force)
2836 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
2837 (*i)->automation_snapshot (now, force);
2841 Route::SoloControllable::SoloControllable (std::string name, Route& r)
2842 : AutomationControl (r.session(), Evoral::Parameter (SoloAutomation),
2843 boost::shared_ptr<AutomationList>(), name)
2846 boost::shared_ptr<AutomationList> gl(new AutomationList(Evoral::Parameter(SoloAutomation)));
2851 Route::SoloControllable::set_value (float val)
2853 bool bval = ((val >= 0.5f) ? true: false);
2855 route.set_solo (bval, this);
2859 Route::SoloControllable::get_value (void) const
2861 return route.self_soloed() ? 1.0f : 0.0f;
2865 Route::set_block_size (nframes_t nframes)
2867 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
2868 (*i)->set_block_size (nframes);
2871 _session.ensure_buffers (n_process_buffers ());
2875 Route::protect_automation ()
2877 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i)
2878 (*i)->protect_automation();
2882 Route::set_pending_declick (int declick)
2885 /* this call is not allowed to turn off a pending declick unless "force" is true */
2887 _pending_declick = declick;
2889 // cerr << _name << ": after setting to " << declick << " pending declick = " << _pending_declick << endl;
2891 _pending_declick = 0;
2896 /** Shift automation forwards from a particular place, thereby inserting time.
2897 * Adds undo commands for any shifts that are performed.
2899 * @param pos Position to start shifting from.
2900 * @param frames Amount to shift forwards by.
2904 Route::shift (nframes64_t /*pos*/, nframes64_t /*frames*/)
2906 #ifdef THIS_NEEDS_FIXING_FOR_V3
2908 /* gain automation */
2909 XMLNode &before = _gain_control->get_state ();
2910 _gain_control->shift (pos, frames);
2911 XMLNode &after = _gain_control->get_state ();
2912 _session.add_command (new MementoCommand<AutomationList> (_gain_automation_curve, &before, &after));
2914 /* pan automation */
2915 for (std::vector<StreamPanner*>::iterator i = _panner->begin (); i != _panner->end (); ++i) {
2916 Curve & c = (*i)->automation ();
2917 XMLNode &before = c.get_state ();
2918 c.shift (pos, frames);
2919 XMLNode &after = c.get_state ();
2920 _session.add_command (new MementoCommand<AutomationList> (c, &before, &after));
2923 /* redirect automation */
2925 Glib::RWLock::ReaderLock lm (redirect_lock);
2926 for (RedirectList::iterator i = _redirects.begin (); i != _redirects.end (); ++i) {
2929 (*i)->what_has_automation (a);
2931 for (set<uint32_t>::const_iterator j = a.begin (); j != a.end (); ++j) {
2932 AutomationList & al = (*i)->automation_list (*j);
2933 XMLNode &before = al.get_state ();
2934 al.shift (pos, frames);
2935 XMLNode &after = al.get_state ();
2936 _session.add_command (new MementoCommand<AutomationList> (al, &before, &after));
2946 Route::save_as_template (const string& path, const string& name)
2948 XMLNode& node (state (false));
2951 IO::set_name_in_state (*node.children().front(), name);
2953 tree.set_root (&node);
2954 return tree.write (path.c_str());
2959 Route::set_name (const string& str)
2965 name = Route::ensure_track_or_route_name (str, _session);
2966 SessionObject::set_name (name);
2968 ret = (_input->set_name(name) && _output->set_name(name));
2972 Glib::RWLock::ReaderLock lm (_processor_lock);
2974 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
2976 /* rename all I/O processors that have inputs or outputs */
2978 boost::shared_ptr<IOProcessor> iop = boost::dynamic_pointer_cast<IOProcessor> (*i);
2980 if (iop && (iop->output() || iop->input())) {
2981 if (!iop->set_name (name)) {
2992 boost::shared_ptr<Send>
2993 Route::internal_send_for (boost::shared_ptr<const Route> target) const
2995 Glib::RWLock::ReaderLock lm (_processor_lock);
2997 for (ProcessorList::const_iterator i = _processors.begin(); i != _processors.end(); ++i) {
2998 boost::shared_ptr<InternalSend> send;
3000 if ((send = boost::dynamic_pointer_cast<InternalSend>(*i)) != 0) {
3001 if (send->target_route() == target) {
3007 return boost::shared_ptr<Send>();
3011 Route::set_phase_invert (bool yn)
3013 if (_phase_invert != yn) {
3014 _phase_invert = 0xffff; // XXX all channels
3015 phase_invert_changed (); /* EMIT SIGNAL */
3020 Route::phase_invert () const
3022 return _phase_invert != 0;
3026 Route::set_denormal_protection (bool yn)
3028 if (_denormal_protection != yn) {
3029 _denormal_protection = yn;
3030 denormal_protection_changed (); /* EMIT SIGNAL */
3035 Route::denormal_protection () const
3037 return _denormal_protection;
3041 Route::set_active (bool yn)
3043 if (_active != yn) {
3045 _input->set_active (yn);
3046 _output->set_active (yn);
3047 active_changed (); // EMIT SIGNAL
3054 Glib::RWLock::ReaderLock rm (_processor_lock, Glib::TRY_LOCK);
3060 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
3062 boost::shared_ptr<Send> s;
3063 boost::shared_ptr<Return> r;
3065 if ((s = boost::dynamic_pointer_cast<Send> (*i)) != 0) {
3066 s->meter()->meter();
3067 } else if ((r = boost::dynamic_pointer_cast<Return> (*i)) != 0) {
3068 r->meter()->meter ();
3073 boost::shared_ptr<Panner>
3074 Route::panner() const
3077 return _main_outs->panner();
3080 boost::shared_ptr<AutomationControl>
3081 Route::gain_control() const
3084 return _amp->gain_control();
3087 boost::shared_ptr<AutomationControl>
3088 Route::get_control (const Evoral::Parameter& param)
3090 /* either we own the control or .... */
3092 boost::shared_ptr<AutomationControl> c = boost::dynamic_pointer_cast<AutomationControl>(data().control (param));
3096 /* maybe one of our processors does or ... */
3098 Glib::RWLock::ReaderLock rm (_processor_lock, Glib::TRY_LOCK);
3099 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
3100 if ((c = boost::dynamic_pointer_cast<AutomationControl>((*i)->data().control (param))) != 0) {
3108 /* nobody does so we'll make a new one */
3110 c = boost::dynamic_pointer_cast<AutomationControl>(control_factory(param));