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 clear_processors (PreFader);
167 clear_processors (PostFader);
171 Route::set_remote_control_id (uint32_t id)
173 if (id != _remote_control_id) {
174 _remote_control_id = id;
175 RemoteControlIDChanged ();
180 Route::remote_control_id() const
182 return _remote_control_id;
186 Route::order_key (std::string const & name) const
188 OrderKeys::const_iterator i = order_keys.find (name);
189 if (i == order_keys.end()) {
197 Route::set_order_key (std::string const & name, long n)
199 order_keys[name] = n;
201 if (Config->get_sync_all_route_ordering()) {
202 for (OrderKeys::iterator x = order_keys.begin(); x != order_keys.end(); ++x) {
207 _session.set_dirty ();
210 /** Set all order keys to be the same as that for `base', if such a key
211 * exists in this route.
212 * @param base Base key.
215 Route::sync_order_keys (std::string const & base)
217 if (order_keys.empty()) {
221 OrderKeys::iterator i;
224 if ((i = order_keys.find (base)) == order_keys.end()) {
225 /* key doesn't exist, use the first existing key (during session initialization) */
226 i = order_keys.begin();
230 /* key exists - use it and reset all others (actually, itself included) */
232 i = order_keys.begin();
235 for (; i != order_keys.end(); ++i) {
241 Route::ensure_track_or_route_name(string name, Session &session)
243 string newname = name;
245 while (session.route_by_name (newname) != NULL) {
246 newname = bump_name_once (newname);
254 Route::inc_gain (gain_t fraction, void *src)
256 _amp->inc_gain (fraction, src);
260 Route::set_gain (gain_t val, void *src)
262 if (src != 0 && _route_group && src != _route_group && _route_group->active_property (RouteGroup::Gain)) {
264 if (_route_group->is_relative()) {
266 gain_t usable_gain = _amp->gain();
267 if (usable_gain < 0.000001f) {
268 usable_gain = 0.000001f;
272 if (delta < 0.000001f) {
276 delta -= usable_gain;
281 gain_t factor = delta / usable_gain;
284 factor = _route_group->get_max_factor(factor);
285 if (factor == 0.0f) {
286 _amp->gain_control()->Changed(); /* EMIT SIGNAL */
290 factor = _route_group->get_min_factor(factor);
291 if (factor == 0.0f) {
292 _amp->gain_control()->Changed(); /* EMIT SIGNAL */
297 _route_group->apply (&Route::inc_gain, factor, _route_group);
301 _route_group->apply (&Route::set_gain, val, _route_group);
307 if (val == _amp->gain()) {
311 _amp->set_gain (val, src);
314 /** Process this route for one (sub) cycle (process thread)
316 * @param bufs Scratch buffers to use for the signal path
317 * @param start_frame Initial transport frame
318 * @param end_frame Final transport frame
319 * @param nframes Number of frames to output (to ports)
321 * Note that (end_frame - start_frame) may not be equal to nframes when the
322 * transport speed isn't 1.0 (eg varispeed).
325 Route::process_output_buffers (BufferSet& bufs,
326 sframes_t start_frame, sframes_t end_frame, nframes_t nframes,
327 bool /*with_processors*/, int declick)
331 bufs.is_silent (false);
333 switch (Config->get_monitoring_model()) {
334 case HardwareMonitoring:
335 case ExternalMonitoring:
336 monitor = !record_enabled() || (_session.config.get_auto_input() && !_session.actively_recording());
343 declick = _pending_declick;
346 /* figure out if we're going to use gain automation */
347 _amp->setup_gain_automation (start_frame, end_frame, nframes);
350 /* tell main outs what to do about monitoring */
351 _main_outs->no_outs_cuz_we_no_monitor (!monitor);
354 /* -------------------------------------------------------------------------------------------
355 GLOBAL DECLICK (for transport changes etc.)
356 ----------------------------------------------------------------------------------------- */
359 Amp::apply_gain (bufs, nframes, 0.0, 1.0);
360 } else if (declick < 0) {
361 Amp::apply_gain (bufs, nframes, 1.0, 0.0);
364 _pending_declick = 0;
366 /* -------------------------------------------------------------------------------------------
367 DENORMAL CONTROL/PHASE INVERT
368 ----------------------------------------------------------------------------------------- */
374 if (_denormal_protection || Config->get_denormal_protection()) {
376 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i, ++chn) {
377 Sample* const sp = i->data();
379 if (_phase_invert & chn) {
380 for (nframes_t nx = 0; nx < nframes; ++nx) {
385 for (nframes_t nx = 0; nx < nframes; ++nx) {
393 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i, ++chn) {
394 Sample* const sp = i->data();
396 if (_phase_invert & chn) {
397 for (nframes_t nx = 0; nx < nframes; ++nx) {
406 if (_denormal_protection || Config->get_denormal_protection()) {
408 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
409 Sample* const sp = i->data();
410 for (nframes_t nx = 0; nx < nframes; ++nx) {
418 /* -------------------------------------------------------------------------------------------
420 ----------------------------------------------------------------------------------------- */
422 Glib::RWLock::ReaderLock rm (_processor_lock, Glib::TRY_LOCK);
425 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
426 if (bufs.count() != (*i)->input_streams()) {
427 cerr << _name << " bufs = " << bufs.count()
428 << " input for " << (*i)->name() << " = " << (*i)->input_streams()
431 assert (bufs.count() == (*i)->input_streams());
432 (*i)->run (bufs, start_frame, end_frame, nframes);
433 bufs.set_count (ChanCount::max(bufs.count(), (*i)->output_streams()));
436 if (!_processors.empty()) {
437 bufs.set_count (ChanCount::max (bufs.count(), _processors.back()->output_streams()));
443 Route::n_process_buffers ()
445 return max (_input->n_ports(), processor_max_streams);
449 Route::passthru (sframes_t start_frame, sframes_t end_frame, nframes_t nframes, int declick)
451 BufferSet& bufs = _session.get_scratch_buffers (n_process_buffers());
455 assert (bufs.available() >= _input->n_ports());
457 if (_input->n_ports() == ChanCount::ZERO) {
461 bufs.set_count (_input->n_ports());
463 if (is_control() && _session.listening()) {
465 /* control/monitor bus ignores input ports when something is
466 feeding the listen "stream". data will "arrive" into the
467 route from the intreturn processor element.
470 bufs.silence (nframes, 0);
474 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
476 BufferSet::iterator o = bufs.begin(*t);
477 PortSet& ports (_input->ports());
479 for (PortSet::iterator i = ports.begin(*t); i != ports.end(*t); ++i, ++o) {
480 o->read_from (i->get_buffer(nframes), nframes);
485 write_out_of_band_data (bufs, start_frame, end_frame, nframes);
486 process_output_buffers (bufs, start_frame, end_frame, nframes, true, declick);
490 Route::passthru_silence (sframes_t start_frame, sframes_t end_frame, nframes_t nframes, int declick)
492 BufferSet& bufs (_session.get_silent_buffers (n_process_buffers()));
493 bufs.set_count (_input->n_ports());
494 write_out_of_band_data (bufs, start_frame, end_frame, nframes);
495 process_output_buffers (bufs, start_frame, end_frame, nframes, true, declick);
499 Route::set_listen (bool yn, void* src)
502 if (yn != _control_outs->active()) {
504 _control_outs->activate ();
506 _control_outs->deactivate ();
509 listen_changed (src); /* EMIT SIGNAL */
515 Route::listening () const
518 return _control_outs->active ();
525 Route::set_solo (bool yn, void *src)
531 if (_route_group && src != _route_group && _route_group->active_property (RouteGroup::Solo)) {
532 _route_group->apply (&Route::set_solo, yn, _route_group);
536 if (self_soloed() != yn) {
538 set_delivery_solo ();
539 solo_changed (src); /* EMIT SIGNAL */
540 _solo_control->Changed (); /* EMIT SIGNAL */
545 Route::set_self_solo (bool yn)
551 Route::mod_solo_by_others (int32_t delta)
554 if (_soloed_by_others >= (uint32_t) delta) {
555 _soloed_by_others += delta;
557 _soloed_by_others = 0;
560 _soloed_by_others += delta;
563 set_delivery_solo ();
567 Route::set_delivery_solo ()
569 /* tell all delivery processors what the solo situation is, so that they keep
570 delivering even though Session::soloing() is true and they were not
574 Glib::RWLock::ReaderLock rm (_processor_lock);
575 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
576 boost::shared_ptr<Delivery> d;
578 if ((d = boost::dynamic_pointer_cast<Delivery> (*i)) != 0) {
579 d->set_solo_level (soloed ());
580 d->set_solo_isolated (solo_isolated());
586 Route::set_solo_isolated (bool yn, void *src)
588 if (_route_group && src != _route_group && _route_group->active_property (RouteGroup::Solo)) {
589 _route_group->apply (&Route::set_solo_isolated, yn, _route_group);
593 if (yn != _solo_isolated) {
595 set_delivery_solo ();
596 solo_isolated_changed (src);
601 Route::solo_isolated () const
603 return _solo_isolated;
607 Route::set_mute_points (MuteMaster::MutePoint mp)
610 mute_points_changed (); /* EMIT SIGNAL */
612 if (_mute_master->muted()) {
613 _mute_master->mute_at (_mute_points);
614 mute_changed (this); /* EMIT SIGNAL */
619 Route::set_mute (bool yn, void *src)
621 if (_route_group && src != _route_group && _route_group->active_property (RouteGroup::Mute)) {
622 _route_group->apply (&Route::set_mute, yn, _route_group);
628 _mute_master->mute_at (_mute_points);
630 _mute_master->clear_mute ();
633 mute_changed (src); /* EMIT SIGNAL */
640 return _mute_master->muted ();
645 dump_processors(const string& name, const list<boost::shared_ptr<Processor> >& procs)
647 cerr << name << " {" << endl;
648 for (list<boost::shared_ptr<Processor> >::const_iterator p = procs.begin();
649 p != procs.end(); ++p) {
650 cerr << "\t" << (*p)->name() << " ID = " << (*p)->id() << endl;
657 Route::add_processor (boost::shared_ptr<Processor> processor, Placement placement, ProcessorStreams* err)
659 ProcessorList::iterator loc;
661 /* XXX this is not thread safe - we don't hold the lock across determining the iter
662 to add before and actually doing the insertion. dammit.
665 if (placement == PreFader) {
666 /* generic pre-fader: insert immediately before the amp */
667 loc = find (_processors.begin(), _processors.end(), _amp);
669 /* generic post-fader: insert right before the main outs */
670 loc = find (_processors.begin(), _processors.end(), _main_outs);
673 return add_processor (processor, loc, err);
677 /** Add a processor to the route.
678 * If @a iter is not NULL, it must point to an iterator in _processors and the new
679 * processor will be inserted immediately before this location. Otherwise,
680 * @a position is used.
683 Route::add_processor (boost::shared_ptr<Processor> processor, ProcessorList::iterator iter, ProcessorStreams* err)
685 ChanCount old_pms = processor_max_streams;
687 if (!_session.engine().connected() || !processor) {
692 Glib::RWLock::WriterLock lm (_processor_lock);
694 boost::shared_ptr<PluginInsert> pi;
695 boost::shared_ptr<PortInsert> porti;
697 ProcessorList::iterator loc = find(_processors.begin(), _processors.end(), processor);
699 if (processor == _amp || processor == _meter || processor == _main_outs) {
700 // Ensure only one of these are in the list at any time
701 if (loc != _processors.end()) {
702 if (iter == loc) { // Already in place, do nothing
704 } else { // New position given, relocate
705 _processors.erase (loc);
710 if (loc != _processors.end()) {
711 cerr << "ERROR: Processor added to route twice!" << endl;
718 _processors.insert (loc, processor);
720 // Set up processor list channels. This will set processor->[input|output]_streams(),
721 // configure redirect ports properly, etc.
723 if (configure_processors_unlocked (err)) {
724 ProcessorList::iterator ploc = loc;
726 _processors.erase(ploc);
727 configure_processors_unlocked (0); // it worked before we tried to add it ...
728 cerr << "configure failed\n";
732 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(processor)) != 0) {
734 if (pi->natural_input_streams() == ChanCount::ZERO) {
735 /* generator plugin */
736 _have_internal_generator = true;
741 if (_control_outs != processor) {
742 // XXX: do we want to emit the signal here ? change call order.
743 processor->activate ();
745 processor->ActiveChanged.connect (bind (mem_fun (_session, &Session::update_latency_compensation), false, false));
747 _output->set_user_latency (0);
750 processors_changed (); /* EMIT SIGNAL */
756 Route::add_processor_from_xml (const XMLNode& node, ProcessorList::iterator iter)
758 const XMLProperty *prop;
760 if (node.name() != "Processor") {
765 if ((prop = node.property ("type")) != 0) {
767 boost::shared_ptr<Processor> processor;
769 if (prop->value() == "ladspa" || prop->value() == "Ladspa" ||
770 prop->value() == "lv2" ||
771 prop->value() == "vst" ||
772 prop->value() == "audiounit") {
774 processor.reset (new PluginInsert(_session, node));
776 } else if (prop->value() == "port") {
778 processor.reset (new PortInsert (_session, _mute_master, node));
780 } else if (prop->value() == "send") {
782 processor.reset (new Send (_session, _mute_master, node));
784 } else if (prop->value() == "meter") {
787 if (_meter->set_state (node, Stateful::loading_state_version)) {
794 _meter.reset (new PeakMeter (_session, node));
795 _meter->set_display_to_user (_meter_point == MeterCustom);
798 } else if (prop->value() == "amp") {
800 /* amp always exists */
803 if (processor->set_state (node, Stateful::loading_state_version)) {
806 /* never any reason to add it */
810 } else if (prop->value() == "intsend") {
812 processor.reset (new InternalSend (_session, _mute_master, node));
814 } else if (prop->value() == "intreturn") {
817 if (_intreturn->set_state (node, Stateful::loading_state_version)) {
823 _intreturn.reset (new InternalReturn (_session, node));
824 processor = _intreturn;
826 } else if (prop->value() == "main-outs") {
829 if (_main_outs->set_state (node, Stateful::loading_state_version)) {
836 _main_outs.reset (new Delivery (_session, _output, _mute_master, node));
837 processor = _main_outs;
840 error << string_compose(_("unknown Processor type \"%1\"; ignored"), prop->value()) << endmsg;
844 if (iter == _processors.end() && processor->display_to_user() && !_processors.empty()) {
845 /* check for invisible processors stacked at the end and leave them there */
846 ProcessorList::iterator p;
847 p = _processors.end();
849 while (!(*p)->display_to_user() && p != _processors.begin()) {
856 return (add_processor (processor, iter) == 0);
859 error << _("Processor XML node has no type property") << endmsg;
864 catch (failed_constructor &err) {
865 warning << _("processor could not be created. Ignored.") << endmsg;
872 Route::add_processor_from_xml_2X (const XMLNode& node, int version, ProcessorList::iterator iter)
874 const XMLProperty *prop;
877 boost::shared_ptr<Processor> processor;
879 if (node.name() == "Insert") {
881 if ((prop = node.property ("type")) != 0) {
883 if (prop->value() == "ladspa" || prop->value() == "Ladspa" ||
884 prop->value() == "lv2" ||
885 prop->value() == "vst" ||
886 prop->value() == "audiounit") {
888 processor.reset (new PluginInsert (_session, node));
892 processor.reset (new PortInsert (_session, _mute_master, node));
897 } else if (node.name() == "Send") {
899 processor.reset (new Send (_session, _mute_master, node, version));
903 error << string_compose(_("unknown Processor type \"%1\"; ignored"), node.name()) << endmsg;
907 if (iter == _processors.end() && processor->display_to_user() && !_processors.empty()) {
908 /* check for invisible processors stacked at the end and leave them there */
909 ProcessorList::iterator p;
910 p = _processors.end();
912 while (!(*p)->display_to_user() && p != _processors.begin()) {
919 return (add_processor (processor, iter) == 0);
922 catch (failed_constructor &err) {
923 warning << _("processor could not be created. Ignored.") << endmsg;
929 Route::add_processors (const ProcessorList& others, boost::shared_ptr<Processor> before, ProcessorStreams* err)
931 ProcessorList::iterator loc;
934 loc = find(_processors.begin(), _processors.end(), before);
936 /* nothing specified - at end but before main outs */
937 loc = find (_processors.begin(), _processors.end(), _main_outs);
940 return add_processors (others, loc, err);
944 Route::add_processors (const ProcessorList& others, ProcessorList::iterator iter, ProcessorStreams* err)
946 /* NOTE: this is intended to be used ONLY when copying
947 processors from another Route. Hence the subtle
948 differences between this and ::add_processor()
951 ChanCount old_pms = processor_max_streams;
953 if (!_session.engine().connected()) {
957 if (others.empty()) {
962 Glib::RWLock::WriterLock lm (_processor_lock);
963 ProcessorList::iterator existing_end = _processors.end();
966 ChanCount potential_max_streams = ChanCount::max (_input->n_ports(), _output->n_ports());
968 for (ProcessorList::const_iterator i = others.begin(); i != others.end(); ++i) {
970 // Ensure meter only appears in the list once
972 ProcessorList::iterator m = find(_processors.begin(), _processors.end(), *i);
973 if (m != _processors.end()) {
974 _processors.erase(m);
978 boost::shared_ptr<PluginInsert> pi;
980 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(*i)) != 0) {
983 ChanCount m = max (pi->input_streams(), pi->output_streams());
985 if (m > potential_max_streams) {
986 potential_max_streams = m;
990 _processors.insert (iter, *i);
992 if (configure_processors_unlocked (err)) {
994 _processors.erase (existing_end, _processors.end());
995 configure_processors_unlocked (0); // it worked before we tried to add it ...
999 (*i)->ActiveChanged.connect (bind (mem_fun (_session, &Session::update_latency_compensation), false, false));
1002 _output->set_user_latency (0);
1005 processors_changed (); /* EMIT SIGNAL */
1011 Route::placement_range(Placement p, ProcessorList::iterator& start, ProcessorList::iterator& end)
1013 if (p == PreFader) {
1014 start = _processors.begin();
1015 end = find(_processors.begin(), _processors.end(), _amp);
1017 start = find(_processors.begin(), _processors.end(), _amp);
1019 end = _processors.end();
1023 /** Turn off all processors with a given placement
1024 * @param p Placement of processors to disable
1027 Route::disable_processors (Placement p)
1029 Glib::RWLock::ReaderLock lm (_processor_lock);
1031 ProcessorList::iterator start, end;
1032 placement_range(p, start, end);
1034 for (ProcessorList::iterator i = start; i != end; ++i) {
1035 (*i)->deactivate ();
1038 _session.set_dirty ();
1041 /** Turn off all redirects
1044 Route::disable_processors ()
1046 Glib::RWLock::ReaderLock lm (_processor_lock);
1048 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1049 (*i)->deactivate ();
1052 _session.set_dirty ();
1055 /** Turn off all redirects with a given placement
1056 * @param p Placement of redirects to disable
1059 Route::disable_plugins (Placement p)
1061 Glib::RWLock::ReaderLock lm (_processor_lock);
1063 ProcessorList::iterator start, end;
1064 placement_range(p, start, end);
1066 for (ProcessorList::iterator i = start; i != end; ++i) {
1067 if (boost::dynamic_pointer_cast<PluginInsert> (*i)) {
1068 (*i)->deactivate ();
1072 _session.set_dirty ();
1075 /** Turn off all plugins
1078 Route::disable_plugins ()
1080 Glib::RWLock::ReaderLock lm (_processor_lock);
1082 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1083 if (boost::dynamic_pointer_cast<PluginInsert> (*i)) {
1084 (*i)->deactivate ();
1088 _session.set_dirty ();
1093 Route::ab_plugins (bool forward)
1095 Glib::RWLock::ReaderLock lm (_processor_lock);
1099 /* forward = turn off all active redirects, and mark them so that the next time
1100 we go the other way, we will revert them
1103 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1104 if (!boost::dynamic_pointer_cast<PluginInsert> (*i)) {
1108 if ((*i)->active()) {
1109 (*i)->deactivate ();
1110 (*i)->set_next_ab_is_active (true);
1112 (*i)->set_next_ab_is_active (false);
1118 /* backward = if the redirect was marked to go active on the next ab, do so */
1120 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1122 if (!boost::dynamic_pointer_cast<PluginInsert> (*i)) {
1126 if ((*i)->get_next_ab_is_active()) {
1129 (*i)->deactivate ();
1134 _session.set_dirty ();
1138 /** Remove processors with a given placement.
1139 * @param p Placement of processors to remove.
1142 Route::clear_processors (Placement p)
1144 const ChanCount old_pms = processor_max_streams;
1146 if (!_session.engine().connected()) {
1150 bool already_deleting = _session.deletion_in_progress();
1151 if (!already_deleting) {
1152 _session.set_deletion_in_progress();
1156 Glib::RWLock::WriterLock lm (_processor_lock);
1157 ProcessorList new_list;
1158 ProcessorStreams err;
1159 bool seen_amp = false;
1161 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1167 if ((*i) == _amp || (*i) == _meter || (*i) == _main_outs) {
1169 /* you can't remove these */
1171 new_list.push_back (*i);
1178 new_list.push_back (*i);
1181 (*i)->drop_references ();
1189 (*i)->drop_references ();
1192 new_list.push_back (*i);
1199 _processors = new_list;
1200 configure_processors_unlocked (&err); // this can't fail
1203 processor_max_streams.reset();
1204 _have_internal_generator = false;
1205 processors_changed (); /* EMIT SIGNAL */
1207 if (!already_deleting) {
1208 _session.clear_deletion_in_progress();
1213 Route::remove_processor (boost::shared_ptr<Processor> processor, ProcessorStreams* err)
1215 /* these can never be removed */
1217 if (processor == _amp || processor == _meter || processor == _main_outs) {
1221 ChanCount old_pms = processor_max_streams;
1223 if (!_session.engine().connected()) {
1227 processor_max_streams.reset();
1230 Glib::RWLock::WriterLock lm (_processor_lock);
1231 ProcessorList::iterator i;
1232 bool removed = false;
1234 for (i = _processors.begin(); i != _processors.end(); ) {
1235 if (*i == processor) {
1237 /* move along, see failure case for configure_processors()
1238 where we may need to reconfigure the processor.
1241 /* stop redirects that send signals to JACK ports
1242 from causing noise as a result of no longer being
1246 boost::shared_ptr<IOProcessor> iop;
1248 if ((iop = boost::dynamic_pointer_cast<IOProcessor> (*i)) != 0) {
1250 iop->input()->disconnect (this);
1252 if (iop->output()) {
1253 iop->output()->disconnect (this);
1257 i = _processors.erase (i);
1265 _output->set_user_latency (0);
1273 if (configure_processors_unlocked (err)) {
1274 /* get back to where we where */
1275 _processors.insert (i, processor);
1276 /* we know this will work, because it worked before :) */
1277 configure_processors_unlocked (0);
1281 _have_internal_generator = false;
1283 for (i = _processors.begin(); i != _processors.end(); ++i) {
1284 boost::shared_ptr<PluginInsert> pi;
1286 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(*i)) != 0) {
1287 if (pi->is_generator()) {
1288 _have_internal_generator = true;
1295 processor->drop_references ();
1296 processors_changed (); /* EMIT SIGNAL */
1302 Route::remove_processors (const ProcessorList& to_be_deleted, ProcessorStreams* err)
1304 ProcessorList deleted;
1305 ProcessorList as_we_were;
1307 if (!_session.engine().connected()) {
1311 processor_max_streams.reset();
1314 Glib::RWLock::WriterLock lm (_processor_lock);
1315 ProcessorList::iterator i;
1316 boost::shared_ptr<Processor> processor;
1318 as_we_were = _processors;
1320 for (i = _processors.begin(); i != _processors.end(); ) {
1324 /* these can never be removed */
1326 if (processor == _amp || processor == _meter || processor == _main_outs) {
1331 /* see if its in the list of processors to delete */
1333 if (find (to_be_deleted.begin(), to_be_deleted.end(), processor) == to_be_deleted.end()) {
1338 /* stop IOProcessors that send to JACK ports
1339 from causing noise as a result of no longer being
1343 boost::shared_ptr<IOProcessor> iop;
1345 if ((iop = boost::dynamic_pointer_cast<IOProcessor> (processor)) != 0) {
1349 deleted.push_back (processor);
1350 i = _processors.erase (i);
1353 if (deleted.empty()) {
1354 /* none of those in the requested list were found */
1358 _output->set_user_latency (0);
1360 if (configure_processors_unlocked (err)) {
1361 /* get back to where we where */
1362 _processors = as_we_were;
1363 /* we know this will work, because it worked before :) */
1364 configure_processors_unlocked (0);
1368 _have_internal_generator = false;
1370 for (i = _processors.begin(); i != _processors.end(); ++i) {
1371 boost::shared_ptr<PluginInsert> pi;
1373 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(*i)) != 0) {
1374 if (pi->is_generator()) {
1375 _have_internal_generator = true;
1382 /* now try to do what we need to so that those that were removed will be deleted */
1384 for (ProcessorList::iterator i = deleted.begin(); i != deleted.end(); ++i) {
1385 (*i)->drop_references ();
1388 processors_changed (); /* EMIT SIGNAL */
1395 Route::configure_processors (ProcessorStreams* err)
1397 if (!_in_configure_processors) {
1398 Glib::RWLock::WriterLock lm (_processor_lock);
1399 return configure_processors_unlocked (err);
1404 /** Configure the input/output configuration of each processor in the processors list.
1405 * Return 0 on success, otherwise configuration is impossible.
1408 Route::configure_processors_unlocked (ProcessorStreams* err)
1410 if (_in_configure_processors) {
1414 _in_configure_processors = true;
1416 // Check each processor in order to see if we can configure as requested
1417 ChanCount in = _input->n_ports ();
1419 list< pair<ChanCount,ChanCount> > configuration;
1422 DEBUG_TRACE (DEBUG::Processors, string_compose ("%1: configure processors\n", _name));
1424 DEBUG_TRACE (DEBUG::Processors, "{\n");
1425 for (list<boost::shared_ptr<Processor> >::const_iterator p = _processors.begin(); p != _processors.end(); ++p) {
1426 DEBUG_TRACE (DEBUG::Processors, string_compose ("\t%1 ID = %2\n", (*p)->name(), (*p)->id()));
1428 DEBUG_TRACE (DEBUG::Processors, "}\n");
1431 for (ProcessorList::iterator p = _processors.begin(); p != _processors.end(); ++p, ++index) {
1433 if ((*p)->can_support_io_configuration(in, out)) {
1434 DEBUG_TRACE (DEBUG::Processors, string_compose ("\t%1in = %2 out = %3\n",(*p)->name(), in, out));
1435 configuration.push_back(make_pair(in, out));
1442 _in_configure_processors = false;
1447 /* Take the process lock so that if we add a processor which increases the required
1448 number of scratch buffers, we create those scratch buffers before the process
1449 thread has a chance to ask for them.
1450 XXX: in an ideal world we'd perhaps use some RCU magic to avoid having to take
1454 Glib::Mutex::Lock pl (_session.engine().process_lock ());
1456 // We can, so configure everything
1457 list< pair<ChanCount,ChanCount> >::iterator c = configuration.begin();
1458 for (ProcessorList::iterator p = _processors.begin(); p != _processors.end(); ++p, ++c) {
1459 (*p)->configure_io(c->first, c->second);
1460 processor_max_streams = ChanCount::max(processor_max_streams, c->first);
1461 processor_max_streams = ChanCount::max(processor_max_streams, c->second);
1465 /* make sure we have sufficient scratch buffers to cope with the new processor
1467 _session.ensure_buffers (n_process_buffers ());
1469 _in_configure_processors = false;
1474 Route::all_processors_flip ()
1476 Glib::RWLock::ReaderLock lm (_processor_lock);
1478 if (_processors.empty()) {
1482 bool first_is_on = _processors.front()->active();
1484 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1486 (*i)->deactivate ();
1492 _session.set_dirty ();
1495 /** Set all processors with a given placement to a given active state.
1496 * @param p Placement of processors to change.
1497 * @param state New active state for those processors.
1500 Route::all_processors_active (Placement p, bool state)
1502 Glib::RWLock::ReaderLock lm (_processor_lock);
1504 if (_processors.empty()) {
1507 ProcessorList::iterator start, end;
1508 placement_range(p, start, end);
1510 bool before_amp = true;
1511 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1516 if (p == PreFader && before_amp) {
1520 (*i)->deactivate ();
1525 _session.set_dirty ();
1529 Route::processor_is_prefader (boost::shared_ptr<Processor> p)
1531 bool pre_fader = true;
1532 Glib::RWLock::ReaderLock lm (_processor_lock);
1534 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1536 /* semantic note: if p == amp, we want to return true, so test
1537 for equality before checking if this is the amp
1554 Route::reorder_processors (const ProcessorList& new_order, ProcessorStreams* err)
1556 /* "new_order" is an ordered list of processors to be positioned according to "placement".
1557 NOTE: all processors in "new_order" MUST be marked as display_to_user(). There maybe additional
1558 processors in the current actual processor list that are hidden. Any visible processors
1559 in the current list but not in "new_order" will be assumed to be deleted.
1563 Glib::RWLock::WriterLock lm (_processor_lock);
1564 ChanCount old_pms = processor_max_streams;
1565 ProcessorList::iterator oiter;
1566 ProcessorList::const_iterator niter;
1567 ProcessorList as_it_was_before = _processors;
1568 ProcessorList as_it_will_be;
1570 oiter = _processors.begin();
1571 niter = new_order.begin();
1573 while (niter != new_order.end()) {
1575 /* if the next processor in the old list is invisible (i.e. should not be in the new order)
1576 then append it to the temp list.
1578 Otherwise, see if the next processor in the old list is in the new list. if not,
1579 its been deleted. If its there, append it to the temp list.
1582 if (oiter == _processors.end()) {
1584 /* no more elements in the old list, so just stick the rest of
1585 the new order onto the temp list.
1588 as_it_will_be.insert (as_it_will_be.end(), niter, new_order.end());
1589 while (niter != new_order.end()) {
1596 if (!(*oiter)->display_to_user()) {
1598 as_it_will_be.push_back (*oiter);
1602 /* visible processor: check that its in the new order */
1604 if (find (new_order.begin(), new_order.end(), (*oiter)) == new_order.end()) {
1605 /* deleted: do nothing, shared_ptr<> will clean up */
1607 /* ignore this one, and add the next item from the new order instead */
1608 as_it_will_be.push_back (*niter);
1613 /* now remove from old order - its taken care of no matter what */
1614 oiter = _processors.erase (oiter);
1619 _processors.insert (oiter, as_it_will_be.begin(), as_it_will_be.end());
1621 if (configure_processors_unlocked (err)) {
1622 _processors = as_it_was_before;
1623 processor_max_streams = old_pms;
1628 processors_changed (); /* EMIT SIGNAL */
1640 Route::get_template()
1642 return state(false);
1646 Route::state(bool full_state)
1648 XMLNode *node = new XMLNode("Route");
1649 ProcessorList::iterator i;
1652 id().print (buf, sizeof (buf));
1653 node->add_property("id", buf);
1654 node->add_property ("name", _name);
1655 node->add_property("default-type", _default_type.to_string());
1658 node->add_property("flags", enum_2_string (_flags));
1661 node->add_property("active", _active?"yes":"no");
1662 node->add_property("phase-invert", _phase_invert?"yes":"no");
1663 node->add_property("denormal-protection", _denormal_protection?"yes":"no");
1664 node->add_property("meter-point", enum_2_string (_meter_point));
1667 node->add_property("route-group", _route_group->name());
1670 string order_string;
1671 OrderKeys::iterator x = order_keys.begin();
1673 while (x != order_keys.end()) {
1674 order_string += string ((*x).first);
1675 order_string += '=';
1676 snprintf (buf, sizeof(buf), "%ld", (*x).second);
1677 order_string += buf;
1681 if (x == order_keys.end()) {
1685 order_string += ':';
1687 node->add_property ("order-keys", order_string);
1688 node->add_property ("self-solo", (_self_solo ? "yes" : "no"));
1689 snprintf (buf, sizeof (buf), "%d", _soloed_by_others);
1690 node->add_property ("soloed-by-others", buf);
1692 node->add_child_nocopy (_input->state (full_state));
1693 node->add_child_nocopy (_output->state (full_state));
1694 node->add_child_nocopy (_solo_control->get_state ());
1695 node->add_child_nocopy (_mute_master->get_state ());
1697 XMLNode* remote_control_node = new XMLNode (X_("RemoteControl"));
1698 snprintf (buf, sizeof (buf), "%d", _remote_control_id);
1699 remote_control_node->add_property (X_("id"), buf);
1700 node->add_child_nocopy (*remote_control_node);
1702 if (_comment.length()) {
1703 XMLNode *cmt = node->add_child ("Comment");
1704 cmt->add_content (_comment);
1707 for (i = _processors.begin(); i != _processors.end(); ++i) {
1708 node->add_child_nocopy((*i)->state (full_state));
1712 node->add_child_copy (*_extra_xml);
1719 Route::set_state (const XMLNode& node, int version)
1721 return _set_state (node, version, true);
1725 Route::_set_state (const XMLNode& node, int version, bool /*call_base*/)
1727 if (version < 3000) {
1728 return _set_state_2X (node, version);
1732 XMLNodeConstIterator niter;
1734 XMLPropertyList plist;
1735 const XMLProperty *prop;
1737 if (node.name() != "Route"){
1738 error << string_compose(_("Bad node sent to Route::set_state() [%1]"), node.name()) << endmsg;
1742 if ((prop = node.property (X_("name"))) != 0) {
1743 Route::set_name (prop->value());
1746 if ((prop = node.property ("id")) != 0) {
1747 _id = prop->value ();
1750 if ((prop = node.property (X_("flags"))) != 0) {
1751 _flags = Flag (string_2_enum (prop->value(), _flags));
1756 /* add all processors (except amp, which is always present) */
1758 nlist = node.children();
1759 XMLNode processor_state (X_("processor_state"));
1761 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
1765 if (child->name() == IO::state_node_name) {
1766 if ((prop = child->property (X_("direction"))) == 0) {
1770 if (prop->value() == "Input") {
1771 _input->set_state (*child, version);
1772 } else if (prop->value() == "Output") {
1773 _output->set_state (*child, version);
1777 if (child->name() == X_("Processor")) {
1778 processor_state.add_child_copy (*child);
1782 set_processor_state (processor_state);
1784 if ((prop = node.property ("self-solo")) != 0) {
1785 set_self_solo (string_is_affirmative (prop->value()));
1788 if ((prop = node.property ("soloed-by-others")) != 0) {
1789 _soloed_by_others = 0; // needed for mod_solo_by_others () to work
1790 mod_solo_by_others (atoi (prop->value()));
1793 if ((prop = node.property ("solo-isolated")) != 0) {
1794 set_solo_isolated (string_is_affirmative (prop->value()), this);
1797 if ((prop = node.property (X_("phase-invert"))) != 0) {
1798 set_phase_invert (string_is_affirmative (prop->value()));
1801 if ((prop = node.property (X_("denormal-protection"))) != 0) {
1802 set_denormal_protection (string_is_affirmative (prop->value()));
1805 if ((prop = node.property (X_("active"))) != 0) {
1806 bool yn = string_is_affirmative (prop->value());
1807 _active = !yn; // force switch
1811 if ((prop = node.property (X_("meter-point"))) != 0) {
1812 _meter_point = MeterPoint (string_2_enum (prop->value (), _meter_point));
1814 _meter->set_display_to_user (_meter_point == MeterCustom);
1818 if ((prop = node.property (X_("route-group"))) != 0) {
1819 RouteGroup* route_group = _session.route_group_by_name(prop->value());
1820 if (route_group == 0) {
1821 error << string_compose(_("Route %1: unknown route group \"%2 in saved state (ignored)"), _name, prop->value()) << endmsg;
1823 set_route_group (route_group, this);
1827 if ((prop = node.property (X_("order-keys"))) != 0) {
1831 string::size_type colon, equal;
1832 string remaining = prop->value();
1834 while (remaining.length()) {
1836 if ((equal = remaining.find_first_of ('=')) == string::npos || equal == remaining.length()) {
1837 error << string_compose (_("badly formed order key string in state file! [%1] ... ignored."), remaining)
1840 if (sscanf (remaining.substr (equal+1).c_str(), "%ld", &n) != 1) {
1841 error << string_compose (_("badly formed order key string in state file! [%1] ... ignored."), remaining)
1844 set_order_key (remaining.substr (0, equal), n);
1848 colon = remaining.find_first_of (':');
1850 if (colon != string::npos) {
1851 remaining = remaining.substr (colon+1);
1858 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
1861 if (child->name() == X_("Comment")) {
1863 /* XXX this is a terrible API design in libxml++ */
1865 XMLNode *cmt = *(child->children().begin());
1866 _comment = cmt->content();
1868 } else if (child->name() == X_("Extra")) {
1870 _extra_xml = new XMLNode (*child);
1872 } else if (child->name() == X_("Controllable") && (prop = child->property("name")) != 0) {
1874 if (prop->value() == "solo") {
1875 _solo_control->set_state (*child, version);
1876 _session.add_controllable (_solo_control);
1879 } else if (child->name() == X_("RemoteControl")) {
1880 if ((prop = child->property (X_("id"))) != 0) {
1882 sscanf (prop->value().c_str(), "%d", &x);
1883 set_remote_control_id (x);
1886 } else if (child->name() == X_("MuteMaster")) {
1887 _mute_master->set_state (*child, version);
1895 Route::_set_state_2X (const XMLNode& node, int version)
1898 XMLNodeConstIterator niter;
1900 XMLPropertyList plist;
1901 const XMLProperty *prop;
1903 /* 2X things which still remain to be handled:
1906 * mute-affects-pre-fader
1907 * mute-affects-post-fader
1908 * mute-affects-control-outs
1909 * mute-affects-main-outs
1914 if (node.name() != "Route") {
1915 error << string_compose(_("Bad node sent to Route::set_state() [%1]"), node.name()) << endmsg;
1919 if ((prop = node.property (X_("flags"))) != 0) {
1920 _flags = Flag (string_2_enum (prop->value(), _flags));
1925 /* add standard processors */
1927 _meter.reset (new PeakMeter (_session));
1928 add_processor (_meter, PreFader);
1930 if (_flags & ControlOut) {
1931 /* where we listen to tracks */
1932 _intreturn.reset (new InternalReturn (_session));
1933 add_processor (_intreturn, PreFader);
1936 _main_outs.reset (new Delivery (_session, _output, _mute_master, _name, Delivery::Main));
1937 add_processor (_main_outs, PostFader);
1941 nlist = node.children ();
1942 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1946 if (child->name() == IO::state_node_name) {
1948 /* there is a note in IO::set_state_2X() about why we have to call
1952 _input->set_state_2X (*child, version, true);
1953 _output->set_state_2X (*child, version, false);
1955 if ((prop = child->property (X_("name"))) != 0) {
1956 set_name (prop->value ());
1959 if ((prop = child->property (X_("id"))) != 0) {
1960 _id = prop->value ();
1963 if ((prop = child->property (X_("active"))) != 0) {
1964 bool yn = string_is_affirmative (prop->value());
1965 _active = !yn; // force switch
1973 if ((prop = node.property (X_("phase-invert"))) != 0) {
1974 set_phase_invert (string_is_affirmative (prop->value()));
1977 if ((prop = node.property (X_("denormal-protection"))) != 0) {
1978 set_denormal_protection (string_is_affirmative (prop->value()));
1981 if ((prop = node.property (X_("soloed"))) != 0) {
1982 bool yn = string_is_affirmative (prop->value());
1984 /* XXX force reset of solo status */
1986 set_solo (yn, this);
1989 if ((prop = node.property (X_("meter-point"))) != 0) {
1990 _meter_point = MeterPoint (string_2_enum (prop->value (), _meter_point));
1993 /* XXX: if the route was in both a mix group and an edit group, it'll end up
1994 just in the edit group. */
1996 if ((prop = node.property (X_("mix-group"))) != 0) {
1997 RouteGroup* route_group = _session.route_group_by_name(prop->value());
1998 if (route_group == 0) {
1999 error << string_compose(_("Route %1: unknown route group \"%2 in saved state (ignored)"), _name, prop->value()) << endmsg;
2001 set_route_group (route_group, this);
2005 if ((prop = node.property (X_("edit-group"))) != 0) {
2006 RouteGroup* route_group = _session.route_group_by_name(prop->value());
2007 if (route_group == 0) {
2008 error << string_compose(_("Route %1: unknown route group \"%2 in saved state (ignored)"), _name, prop->value()) << endmsg;
2010 set_route_group (route_group, this);
2014 if ((prop = node.property (X_("order-keys"))) != 0) {
2018 string::size_type colon, equal;
2019 string remaining = prop->value();
2021 while (remaining.length()) {
2023 if ((equal = remaining.find_first_of ('=')) == string::npos || equal == remaining.length()) {
2024 error << string_compose (_("badly formed order key string in state file! [%1] ... ignored."), remaining)
2027 if (sscanf (remaining.substr (equal+1).c_str(), "%ld", &n) != 1) {
2028 error << string_compose (_("badly formed order key string in state file! [%1] ... ignored."), remaining)
2031 set_order_key (remaining.substr (0, equal), n);
2035 colon = remaining.find_first_of (':');
2037 if (colon != string::npos) {
2038 remaining = remaining.substr (colon+1);
2045 XMLNodeList redirect_nodes;
2047 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
2051 if (child->name() == X_("Send") || child->name() == X_("Insert")) {
2052 redirect_nodes.push_back(child);
2057 set_processor_state_2X (redirect_nodes, version);
2059 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
2062 if (child->name() == X_("Comment")) {
2064 /* XXX this is a terrible API design in libxml++ */
2066 XMLNode *cmt = *(child->children().begin());
2067 _comment = cmt->content();
2069 } else if (child->name() == X_("Extra")) {
2071 _extra_xml = new XMLNode (*child);
2073 } else if (child->name() == X_("Controllable") && (prop = child->property("name")) != 0) {
2075 if (prop->value() == "solo") {
2076 _solo_control->set_state (*child, version);
2077 _session.add_controllable (_solo_control);
2080 } else if (child->name() == X_("RemoteControl")) {
2081 if ((prop = child->property (X_("id"))) != 0) {
2083 sscanf (prop->value().c_str(), "%d", &x);
2084 set_remote_control_id (x);
2094 Route::get_processor_state ()
2096 XMLNode* root = new XMLNode (X_("redirects"));
2097 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
2098 root->add_child_nocopy ((*i)->state (true));
2105 Route::set_processor_state_2X (XMLNodeList const & nList, int version)
2107 /* We don't bother removing existing processors not in nList, as this
2108 method will only be called when creating a Route from scratch, not
2109 for undo purposes. Just put processors in at the appropriate place
2113 for (XMLNodeConstIterator i = nList.begin(); i != nList.end(); ++i) {
2114 add_processor_from_xml_2X (**i, version, _processors.begin ());
2119 Route::set_processor_state (const XMLNode& node)
2121 const XMLNodeList &nlist = node.children();
2122 XMLNodeConstIterator niter;
2123 ProcessorList::iterator i, o;
2125 // Iterate through existing processors, remove those which are not in the state list
2127 for (i = _processors.begin(); i != _processors.end(); ) {
2129 /* leave amp alone, always */
2136 ProcessorList::iterator tmp = i;
2139 bool processorInStateList = false;
2141 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2143 XMLProperty* id_prop = (*niter)->property(X_("id"));
2145 if (id_prop && (*i)->id() == id_prop->value()) {
2146 processorInStateList = true;
2151 if (!processorInStateList) {
2152 remove_processor (*i);
2158 // Iterate through state list and make sure all processors are on the track and in the correct order,
2159 // set the state of existing processors according to the new state on the same go
2161 i = _processors.begin();
2163 for (niter = nlist.begin(); niter != nlist.end(); ++niter, ++i) {
2165 XMLProperty* prop = (*niter)->property ("type");
2169 // Check whether the next processor in the list is the right one,
2170 // except for "amp" which is always there and may not have the
2171 // old ID since it is always created anew in every Route
2173 if (prop->value() != "amp") {
2174 while (o != _processors.end()) {
2175 XMLProperty* id_prop = (*niter)->property(X_("id"));
2176 if (id_prop && (*o)->id() == id_prop->value()) {
2184 // If the processor (*niter) is not on the route,
2185 // create it and move it to the correct location
2187 if (o == _processors.end()) {
2189 if (add_processor_from_xml (**niter, i)) {
2190 --i; // move iterator to the newly inserted processor
2192 cerr << "Error restoring route: unable to restore processor" << endl;
2197 // Otherwise, the processor already exists; just
2198 // ensure it is at the location provided in the XML state
2201 boost::shared_ptr<Processor> tmp = (*o);
2202 _processors.erase (o); // remove the old copy
2203 _processors.insert (i, tmp); // insert the processor at the correct location
2204 --i; // move iterator to the correct processor
2207 // and make it (just) so
2209 (*i)->set_state (**niter, Stateful::current_state_version);
2213 /* note: there is no configure_processors() call because we figure that
2214 the XML state represents a working signal route.
2217 processors_changed ();
2221 Route::curve_reallocate ()
2223 // _gain_automation_curve.finish_resize ();
2224 // _pan_automation_curve.finish_resize ();
2228 Route::silence (nframes_t nframes)
2232 _output->silence (nframes);
2235 Glib::RWLock::ReaderLock lm (_processor_lock, Glib::TRY_LOCK);
2238 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
2239 boost::shared_ptr<PluginInsert> pi;
2241 if (!_active && (pi = boost::dynamic_pointer_cast<PluginInsert> (*i)) != 0) {
2242 // skip plugins, they don't need anything when we're not active
2246 (*i)->silence (nframes);
2249 if (nframes == _session.get_block_size()) {
2259 Route::add_internal_return ()
2262 _intreturn.reset (new InternalReturn (_session));
2263 add_processor (_intreturn, PreFader);
2268 Route::get_return_buffer () const
2270 Glib::RWLock::ReaderLock rm (_processor_lock);
2272 for (ProcessorList::const_iterator x = _processors.begin(); x != _processors.end(); ++x) {
2273 boost::shared_ptr<InternalReturn> d = boost::dynamic_pointer_cast<InternalReturn>(*x);
2276 BufferSet* bs = d->get_buffers ();
2285 Route::release_return_buffer () const
2287 Glib::RWLock::ReaderLock rm (_processor_lock);
2289 for (ProcessorList::const_iterator x = _processors.begin(); x != _processors.end(); ++x) {
2290 boost::shared_ptr<InternalReturn> d = boost::dynamic_pointer_cast<InternalReturn>(*x);
2293 return d->release_buffers ();
2299 Route::listen_via (boost::shared_ptr<Route> route, Placement placement, bool /*active*/, bool aux)
2301 vector<string> ports;
2302 vector<string>::const_iterator i;
2305 Glib::RWLock::ReaderLock rm (_processor_lock);
2307 for (ProcessorList::iterator x = _processors.begin(); x != _processors.end(); ++x) {
2309 boost::shared_ptr<InternalSend> d = boost::dynamic_pointer_cast<InternalSend>(*x);
2311 if (d && d->target_route() == route) {
2313 /* if the target is the control outs, then make sure
2314 we take note of which i-send is doing that.
2317 if (route == _session.control_out()) {
2318 _control_outs = boost::dynamic_pointer_cast<Delivery>(d);
2321 /* already listening via the specified IO: do nothing */
2328 boost::shared_ptr<InternalSend> listener;
2331 listener.reset (new InternalSend (_session, _mute_master, route, (aux ? Delivery::Aux : Delivery::Listen)));
2333 } catch (failed_constructor& err) {
2337 if (route == _session.control_out()) {
2338 _control_outs = listener;
2341 add_processor (listener, placement);
2347 Route::drop_listen (boost::shared_ptr<Route> route)
2349 ProcessorStreams err;
2350 ProcessorList::iterator tmp;
2352 Glib::RWLock::ReaderLock rl(_processor_lock);
2356 for (ProcessorList::iterator x = _processors.begin(); x != _processors.end(); ) {
2358 boost::shared_ptr<InternalSend> d = boost::dynamic_pointer_cast<InternalSend>(*x);
2360 if (d && d->target_route() == route) {
2362 remove_processor (*x, &err);
2365 /* list could have been demolished while we dropped the lock
2375 if (route == _session.control_out()) {
2376 _control_outs.reset ();
2381 Route::set_route_group (RouteGroup *rg, void *src)
2383 if (rg == _route_group) {
2388 _route_group->remove (this);
2391 if ((_route_group = rg) != 0) {
2392 _route_group->add (this);
2395 _session.set_dirty ();
2396 route_group_changed (src); /* EMIT SIGNAL */
2400 Route::drop_route_group (void *src)
2403 _session.set_dirty ();
2404 route_group_changed (src); /* EMIT SIGNAL */
2408 Route::set_comment (string cmt, void *src)
2411 comment_changed (src);
2412 _session.set_dirty ();
2416 Route::feeds (boost::shared_ptr<Route> other, bool* only_send)
2418 DEBUG_TRACE (DEBUG::Graph, string_compose ("Feeds? %1\n", _name));
2420 if (_output->connected_to (other->input())) {
2421 DEBUG_TRACE (DEBUG::Graph, string_compose ("\tdirect FEEDS %2\n", other->name()));
2430 for (ProcessorList::iterator r = _processors.begin(); r != _processors.end(); r++) {
2432 boost::shared_ptr<IOProcessor> iop;
2434 if ((iop = boost::dynamic_pointer_cast<IOProcessor>(*r)) != 0) {
2435 if (iop->feeds (other)) {
2436 DEBUG_TRACE (DEBUG::Graph, string_compose ("\tIOP %1 does feed %2\n", iop->name(), other->name()));
2442 DEBUG_TRACE (DEBUG::Graph, string_compose ("\tIOP %1 does NOT feed %2\n", iop->name(), other->name()));
2445 DEBUG_TRACE (DEBUG::Graph, string_compose ("\tPROC %1 is not an IOP\n", (*r)->name()));
2450 DEBUG_TRACE (DEBUG::Graph, string_compose ("\tdoes NOT feed %1\n", other->name()));
2455 Route::handle_transport_stopped (bool /*abort_ignored*/, bool did_locate, bool can_flush_processors)
2457 nframes_t now = _session.transport_frame();
2460 Glib::RWLock::ReaderLock lm (_processor_lock);
2463 automation_snapshot (now, true);
2466 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
2468 if (Config->get_plugins_stop_with_transport() && can_flush_processors) {
2469 (*i)->deactivate ();
2473 (*i)->transport_stopped (now);
2477 _roll_delay = _initial_delay;
2481 Route::input_change_handler (IOChange change, void * /*src*/)
2483 if ((change & ConfigurationChanged)) {
2484 configure_processors (0);
2489 Route::output_change_handler (IOChange change, void * /*src*/)
2491 if ((change & ConfigurationChanged)) {
2493 /* XXX resize all listeners to match _main_outs? */
2495 // configure_processors (0);
2500 Route::pans_required () const
2502 if (n_outputs().n_audio() < 2) {
2506 return max (n_inputs ().n_audio(), processor_max_streams.n_audio());
2510 Route::no_roll (nframes_t nframes, sframes_t start_frame, sframes_t end_frame,
2511 bool session_state_changing, bool /*can_record*/, bool /*rec_monitors_input*/)
2513 if (n_outputs().n_total() == 0) {
2517 if (session_state_changing || !_active || n_inputs() == ChanCount::ZERO) {
2522 _amp->apply_gain_automation (false);
2523 passthru (start_frame, end_frame, nframes, 0);
2529 Route::check_initial_delay (nframes_t nframes, nframes_t& transport_frame)
2531 if (_roll_delay > nframes) {
2533 _roll_delay -= nframes;
2535 /* transport frame is not legal for caller to use */
2538 } else if (_roll_delay > 0) {
2540 nframes -= _roll_delay;
2541 silence (_roll_delay);
2542 /* we've written _roll_delay of samples into the
2543 output ports, so make a note of that for
2546 _main_outs->increment_output_offset (_roll_delay);
2547 transport_frame += _roll_delay;
2556 Route::roll (nframes_t nframes, sframes_t start_frame, sframes_t end_frame, int declick,
2557 bool /*can_record*/, bool /*rec_monitors_input*/)
2560 // automation snapshot can also be called from the non-rt context
2561 // and it uses the processor list, so we try to acquire the lock here
2562 Glib::RWLock::ReaderLock lm (_processor_lock, Glib::TRY_LOCK);
2565 automation_snapshot (_session.transport_frame(), false);
2569 if (n_outputs().n_total() == 0) {
2573 if (!_active || n_inputs().n_total() == 0) {
2578 nframes_t unused = 0;
2580 if ((nframes = check_initial_delay (nframes, unused)) == 0) {
2586 passthru (start_frame, end_frame, nframes, declick);
2592 Route::silent_roll (nframes_t nframes, sframes_t /*start_frame*/, sframes_t /*end_frame*/,
2593 bool /*can_record*/, bool /*rec_monitors_input*/)
2600 Route::toggle_monitor_input ()
2602 for (PortSet::iterator i = _input->ports().begin(); i != _input->ports().end(); ++i) {
2603 i->ensure_monitor_input( ! i->monitoring_input());
2608 Route::has_external_redirects () const
2610 // FIXME: what about sends? - they don't return a signal back to ardour?
2612 boost::shared_ptr<const PortInsert> pi;
2614 for (ProcessorList::const_iterator i = _processors.begin(); i != _processors.end(); ++i) {
2616 if ((pi = boost::dynamic_pointer_cast<const PortInsert>(*i)) != 0) {
2618 for (PortSet::const_iterator port = pi->output()->ports().begin(); port != pi->output()->ports().end(); ++port) {
2620 string port_name = port->name();
2621 string client_name = port_name.substr (0, port_name.find(':'));
2623 /* only say "yes" if the redirect is actually in use */
2625 if (client_name != "ardour" && pi->active()) {
2636 Route::flush_processors ()
2638 /* XXX shouldn't really try to take this lock, since
2639 this is called from the RT audio thread.
2642 Glib::RWLock::ReaderLock lm (_processor_lock);
2644 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
2645 (*i)->deactivate ();
2651 Route::set_meter_point (MeterPoint p, void *src)
2653 if (_meter_point == p) {
2658 Glib::RWLock::WriterLock lm (_processor_lock);
2659 ProcessorList as_it_was (_processors);
2661 if (p != MeterCustom) {
2662 // Move meter in the processors list to reflect the new position
2663 ProcessorList::iterator loc = find(_processors.begin(), _processors.end(), _meter);
2664 _processors.erase(loc);
2667 loc = _processors.begin();
2670 loc = find(_processors.begin(), _processors.end(), _amp);
2672 case MeterPostFader:
2673 loc = _processors.end();
2679 _processors.insert(loc, _meter);
2681 if (configure_processors_unlocked (0)) {
2682 _processors = as_it_was;
2683 configure_processors_unlocked (0); // it worked before we tried to add it ...
2687 _meter->set_display_to_user (false);
2691 // just make it visible and let the user move it
2693 _meter->set_display_to_user (true);
2699 meter_change (src); /* EMIT SIGNAL */
2700 processors_changed (); /* EMIT SIGNAL */
2701 _session.set_dirty ();
2705 Route::put_control_outs_at (Placement p)
2707 if (!_control_outs) {
2712 Glib::RWLock::WriterLock lm (_processor_lock);
2713 ProcessorList as_it_was (_processors);
2714 // Move meter in the processors list
2715 ProcessorList::iterator loc = find(_processors.begin(), _processors.end(), _control_outs);
2716 _processors.erase(loc);
2720 loc = find(_processors.begin(), _processors.end(), _amp);
2721 if (loc != _processors.begin()) {
2726 loc = find(_processors.begin(), _processors.end(), _amp);
2727 assert (loc != _processors.end());
2732 _processors.insert(loc, _control_outs);
2734 if (configure_processors_unlocked (0)) {
2735 _processors = as_it_was;
2736 configure_processors_unlocked (0); // it worked before we tried to add it ...
2741 processors_changed (); /* EMIT SIGNAL */
2742 _session.set_dirty ();
2746 Route::update_total_latency ()
2748 nframes_t old = _output->effective_latency();
2749 nframes_t own_latency = _output->user_latency();
2751 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
2752 if ((*i)->active ()) {
2753 own_latency += (*i)->signal_latency ();
2757 DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: internal redirect latency = %2\n", _name, own_latency));
2759 _output->set_port_latency (own_latency);
2761 if (_output->user_latency() == 0) {
2763 /* this (virtual) function is used for pure Routes,
2764 not derived classes like AudioTrack. this means
2765 that the data processed here comes from an input
2766 port, not prerecorded material, and therefore we
2767 have to take into account any input latency.
2770 own_latency += _input->signal_latency ();
2773 if (old != own_latency) {
2774 _output->set_latency_delay (own_latency);
2775 signal_latency_changed (); /* EMIT SIGNAL */
2778 DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: input latency = %2 total = %3\n", _name, _input->signal_latency(), own_latency));
2780 return _output->effective_latency ();
2784 Route::set_user_latency (nframes_t nframes)
2786 _output->set_user_latency (nframes);
2787 _session.update_latency_compensation (false, false);
2791 Route::set_latency_delay (nframes_t longest_session_latency)
2793 nframes_t old = _initial_delay;
2795 if (_output->effective_latency() < longest_session_latency) {
2796 _initial_delay = longest_session_latency - _output->effective_latency();
2801 if (_initial_delay != old) {
2802 initial_delay_changed (); /* EMIT SIGNAL */
2805 if (_session.transport_stopped()) {
2806 _roll_delay = _initial_delay;
2811 Route::automation_snapshot (nframes_t now, bool force)
2813 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
2814 (*i)->automation_snapshot (now, force);
2818 Route::SoloControllable::SoloControllable (std::string name, Route& r)
2819 : AutomationControl (r.session(), Evoral::Parameter (SoloAutomation),
2820 boost::shared_ptr<AutomationList>(), name)
2823 boost::shared_ptr<AutomationList> gl(new AutomationList(Evoral::Parameter(SoloAutomation)));
2828 Route::SoloControllable::set_value (float val)
2830 bool bval = ((val >= 0.5f) ? true: false);
2832 route.set_solo (bval, this);
2836 Route::SoloControllable::get_value (void) const
2838 return route.self_soloed() ? 1.0f : 0.0f;
2842 Route::set_block_size (nframes_t nframes)
2844 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
2845 (*i)->set_block_size (nframes);
2848 _session.ensure_buffers (n_process_buffers ());
2852 Route::protect_automation ()
2854 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i)
2855 (*i)->protect_automation();
2859 Route::set_pending_declick (int declick)
2862 /* this call is not allowed to turn off a pending declick unless "force" is true */
2864 _pending_declick = declick;
2866 // cerr << _name << ": after setting to " << declick << " pending declick = " << _pending_declick << endl;
2868 _pending_declick = 0;
2873 /** Shift automation forwards from a particular place, thereby inserting time.
2874 * Adds undo commands for any shifts that are performed.
2876 * @param pos Position to start shifting from.
2877 * @param frames Amount to shift forwards by.
2881 Route::shift (nframes64_t /*pos*/, nframes64_t /*frames*/)
2883 #ifdef THIS_NEEDS_FIXING_FOR_V3
2885 /* gain automation */
2886 XMLNode &before = _gain_control->get_state ();
2887 _gain_control->shift (pos, frames);
2888 XMLNode &after = _gain_control->get_state ();
2889 _session.add_command (new MementoCommand<AutomationList> (_gain_automation_curve, &before, &after));
2891 /* pan automation */
2892 for (std::vector<StreamPanner*>::iterator i = _panner->begin (); i != _panner->end (); ++i) {
2893 Curve & c = (*i)->automation ();
2894 XMLNode &before = c.get_state ();
2895 c.shift (pos, frames);
2896 XMLNode &after = c.get_state ();
2897 _session.add_command (new MementoCommand<AutomationList> (c, &before, &after));
2900 /* redirect automation */
2902 Glib::RWLock::ReaderLock lm (redirect_lock);
2903 for (RedirectList::iterator i = _redirects.begin (); i != _redirects.end (); ++i) {
2906 (*i)->what_has_automation (a);
2908 for (set<uint32_t>::const_iterator j = a.begin (); j != a.end (); ++j) {
2909 AutomationList & al = (*i)->automation_list (*j);
2910 XMLNode &before = al.get_state ();
2911 al.shift (pos, frames);
2912 XMLNode &after = al.get_state ();
2913 _session.add_command (new MementoCommand<AutomationList> (al, &before, &after));
2923 Route::save_as_template (const string& path, const string& name)
2925 XMLNode& node (state (false));
2928 IO::set_name_in_state (*node.children().front(), name);
2930 tree.set_root (&node);
2931 return tree.write (path.c_str());
2936 Route::set_name (const string& str)
2942 name = Route::ensure_track_or_route_name (str, _session);
2943 SessionObject::set_name (name);
2945 ret = (_input->set_name(name) && _output->set_name(name));
2949 Glib::RWLock::ReaderLock lm (_processor_lock);
2951 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
2953 /* rename all I/O processors that have inputs or outputs */
2955 boost::shared_ptr<IOProcessor> iop = boost::dynamic_pointer_cast<IOProcessor> (*i);
2957 if (iop && (iop->output() || iop->input())) {
2958 if (!iop->set_name (name)) {
2969 boost::shared_ptr<Send>
2970 Route::internal_send_for (boost::shared_ptr<const Route> target) const
2972 Glib::RWLock::ReaderLock lm (_processor_lock);
2974 for (ProcessorList::const_iterator i = _processors.begin(); i != _processors.end(); ++i) {
2975 boost::shared_ptr<InternalSend> send;
2977 if ((send = boost::dynamic_pointer_cast<InternalSend>(*i)) != 0) {
2978 if (send->target_route() == target) {
2984 return boost::shared_ptr<Send>();
2988 Route::set_phase_invert (bool yn)
2990 if (_phase_invert != yn) {
2991 _phase_invert = 0xffff; // XXX all channels
2992 phase_invert_changed (); /* EMIT SIGNAL */
2997 Route::phase_invert () const
2999 return _phase_invert != 0;
3003 Route::set_denormal_protection (bool yn)
3005 if (_denormal_protection != yn) {
3006 _denormal_protection = yn;
3007 denormal_protection_changed (); /* EMIT SIGNAL */
3012 Route::denormal_protection () const
3014 return _denormal_protection;
3018 Route::set_active (bool yn)
3020 if (_active != yn) {
3022 _input->set_active (yn);
3023 _output->set_active (yn);
3024 active_changed (); // EMIT SIGNAL
3031 Glib::RWLock::ReaderLock rm (_processor_lock, Glib::TRY_LOCK);
3037 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
3039 boost::shared_ptr<Send> s;
3040 boost::shared_ptr<Return> r;
3042 if ((s = boost::dynamic_pointer_cast<Send> (*i)) != 0) {
3043 s->meter()->meter();
3044 } else if ((r = boost::dynamic_pointer_cast<Return> (*i)) != 0) {
3045 r->meter()->meter ();
3050 boost::shared_ptr<Panner>
3051 Route::panner() const
3054 return _main_outs->panner();
3057 boost::shared_ptr<AutomationControl>
3058 Route::gain_control() const
3061 return _amp->gain_control();
3064 boost::shared_ptr<AutomationControl>
3065 Route::get_control (const Evoral::Parameter& param)
3067 /* either we own the control or .... */
3069 boost::shared_ptr<AutomationControl> c = boost::dynamic_pointer_cast<AutomationControl>(data().control (param));
3073 /* maybe one of our processors does or ... */
3075 Glib::RWLock::ReaderLock rm (_processor_lock, Glib::TRY_LOCK);
3076 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
3077 if ((c = boost::dynamic_pointer_cast<AutomationControl>((*i)->data().control (param))) != 0) {
3085 /* nobody does so we'll make a new one */
3087 c = boost::dynamic_pointer_cast<AutomationControl>(control_factory(param));