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>
28 #include <ardour/timestamps.h>
29 #include <ardour/audioengine.h>
30 #include <ardour/route.h>
31 #include <ardour/buffer.h>
32 #include <ardour/insert.h>
33 #include <ardour/send.h>
34 #include <ardour/session.h>
35 #include <ardour/utils.h>
36 #include <ardour/configuration.h>
37 #include <ardour/cycle_timer.h>
38 #include <ardour/route_group.h>
39 #include <ardour/port.h>
40 #include <ardour/audio_port.h>
41 #include <ardour/ladspa_plugin.h>
42 #include <ardour/panner.h>
43 #include <ardour/dB.h>
44 #include <ardour/mix.h>
45 #include <ardour/amp.h>
46 #include <ardour/meter.h>
47 #include <ardour/buffer_set.h>
51 using namespace ARDOUR;
55 uint32_t Route::order_key_cnt = 0;
58 Route::Route (Session& sess, string name, int input_min, int input_max, int output_min, int output_max, Flag flg, DataType default_type)
59 : IO (sess, name, input_min, input_max, output_min, output_max, default_type),
61 _solo_control (*this, ToggleControllable::SoloControl),
62 _mute_control (*this, ToggleControllable::MuteControl)
67 Route::Route (Session& sess, const XMLNode& node)
69 _solo_control (*this, ToggleControllable::SoloControl),
70 _mute_control (*this, ToggleControllable::MuteControl)
79 redirect_max_outs.reset();
83 _phase_invert = false;
84 order_keys[N_("signal")] = order_key_cnt++;
87 _meter_point = MeterPostFader;
91 _have_internal_generator = false;
93 _pending_declick = true;
94 _remote_control_id = 0;
99 _mute_affects_pre_fader = Config->get_mute_affects_pre_fader();
100 _mute_affects_post_fader = Config->get_mute_affects_post_fader();
101 _mute_affects_control_outs = Config->get_mute_affects_control_outs();
102 _mute_affects_main_outs = Config->get_mute_affects_main_outs();
105 desired_solo_gain = 1.0;
107 desired_mute_gain = 1.0;
111 input_changed.connect (mem_fun (this, &Route::input_change_handler));
112 output_changed.connect (mem_fun (this, &Route::output_change_handler));
117 clear_redirects (this);
120 delete _control_outs;
125 Route::set_remote_control_id (uint32_t id)
127 if (id != _remote_control_id) {
128 _remote_control_id = id;
129 RemoteControlIDChanged ();
134 Route::remote_control_id() const
136 return _remote_control_id;
140 Route::order_key (string name) const
142 OrderKeys::const_iterator i;
144 if ((i = order_keys.find (name)) == order_keys.end()) {
152 Route::set_order_key (string name, long n)
154 order_keys[name] = n;
155 _session.set_dirty ();
159 Route::inc_gain (gain_t fraction, void *src)
161 IO::inc_gain (fraction, src);
165 Route::set_gain (gain_t val, void *src)
167 if (src != 0 && _mix_group && src != _mix_group && _mix_group->is_active()) {
169 if (_mix_group->is_relative()) {
172 gain_t usable_gain = gain();
173 if (usable_gain < 0.000001f) {
174 usable_gain=0.000001f;
178 if (delta < 0.000001f) {
182 delta -= usable_gain;
184 if (delta == 0.0f) return;
186 gain_t factor = delta / usable_gain;
189 factor = _mix_group->get_max_factor(factor);
190 if (factor == 0.0f) {
195 factor = _mix_group->get_min_factor(factor);
196 if (factor == 0.0f) {
202 _mix_group->apply (&Route::inc_gain, factor, _mix_group);
206 _mix_group->apply (&Route::set_gain, val, _mix_group);
216 IO::set_gain (val, src);
219 /** Process this route for one (sub) cycle (process thread)
221 * @param bufs Scratch buffers to use for the signal path
222 * @param start_frame Initial transport frame
223 * @param end_frame Final transport frame
224 * @param nframes Number of frames to output (to ports)
225 * @param offset Output offset (of port buffers, for split cycles)
227 * Note that (end_frame - start_frame) may not be equal to nframes when the
228 * transport speed isn't 1.0 (eg varispeed).
231 Route::process_output_buffers (BufferSet& bufs,
232 jack_nframes_t start_frame, jack_nframes_t end_frame,
233 jack_nframes_t nframes, jack_nframes_t offset, bool with_redirects, int declick,
236 // This is definitely very audio-only for now
237 assert(_default_type == DataType::AUDIO);
239 RedirectList::iterator i;
240 bool post_fader_work = false;
241 bool mute_declick_applied = false;
243 vector<Sample*>::iterator bufiter;
247 bool no_monitor = (Config->get_use_hardware_monitoring() || !Config->get_use_sw_monitoring ());
248 gain_t* gab = _session.gain_automation_buffer();
250 declick = _pending_declick;
253 Glib::Mutex::Lock cm (control_outs_lock, Glib::TRY_LOCK);
263 Glib::Mutex::Lock dm (declick_lock, Glib::TRY_LOCK);
266 dmg = desired_mute_gain;
267 dsg = desired_solo_gain;
276 /* ----------------------------------------------------------------------------------------------------
277 GLOBAL DECLICK (for transport changes etc.)
278 -------------------------------------------------------------------------------------------------- */
281 Amp::run (bufs, nframes, 0.0, 1.0, _phase_invert);
282 _pending_declick = 0;
283 } else if (declick < 0) {
284 Amp::run (bufs, nframes, 1.0, 0.0, _phase_invert);
285 _pending_declick = 0;
288 /* no global declick */
290 if (solo_gain != dsg) {
291 Amp::run (bufs, nframes, solo_gain, dsg, _phase_invert);
297 /* ----------------------------------------------------------------------------------------------------
298 INPUT METERING & MONITORING
299 -------------------------------------------------------------------------------------------------- */
301 if (meter && (_meter_point == MeterInput)) {
302 _meter->run(bufs, nframes);
305 if (!_soloed && _mute_affects_pre_fader && (mute_gain != dmg)) {
306 Amp::run (bufs, nframes, mute_gain, dmg, _phase_invert);
308 mute_declick_applied = true;
311 if ((_meter_point == MeterInput) && co) {
313 solo_audible = dsg > 0;
314 mute_audible = dmg > 0;// || !_mute_affects_pre_fader;
316 if ( // muted by solo of another track
320 // muted by mute of this track
324 // rec-enabled but not s/w monitoring
326 // TODO: this is probably wrong
328 (no_monitor && record_enabled() && (!_session.get_auto_input() || _session.actively_recording()))
332 co->silence (nframes, offset);
336 co->deliver_output (bufs, start_frame, end_frame, nframes, offset);
341 /* ----------------------------------------------------------------------------------------------------
343 -------------------------------------------------------------------------------------------------- */
345 if (with_redirects) {
346 Glib::RWLock::ReaderLock rm (redirect_lock, Glib::TRY_LOCK);
348 if (mute_gain > 0 || !_mute_affects_pre_fader) {
349 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
350 switch ((*i)->placement()) {
352 (*i)->run (bufs, start_frame, end_frame, nframes, offset);
355 post_fader_work = true;
360 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
361 switch ((*i)->placement()) {
363 (*i)->silence (nframes, offset);
366 post_fader_work = true;
375 if (!_soloed && (mute_gain != dmg) && !mute_declick_applied && _mute_affects_post_fader) {
376 Amp::run (bufs, nframes, mute_gain, dmg, _phase_invert);
378 mute_declick_applied = true;
381 /* ----------------------------------------------------------------------------------------------------
382 PRE-FADER METERING & MONITORING
383 -------------------------------------------------------------------------------------------------- */
385 if (meter && (_meter_point == MeterPreFader)) {
386 _meter->run(bufs, nframes);
390 if ((_meter_point == MeterPreFader) && co) {
392 solo_audible = dsg > 0;
393 mute_audible = dmg > 0 || !_mute_affects_pre_fader;
395 if ( // muted by solo of another track
399 // muted by mute of this track
403 // rec-enabled but not s/w monitoring
405 (no_monitor && record_enabled() && (!_session.get_auto_input() || _session.actively_recording()))
409 co->silence (nframes, offset);
413 co->deliver_output (bufs, start_frame, end_frame, nframes, offset);
418 /* ----------------------------------------------------------------------------------------------------
420 -------------------------------------------------------------------------------------------------- */
422 /* if not recording or recording and requiring any monitor signal, then apply gain */
424 if ( // not recording
426 !(record_enabled() && _session.actively_recording()) ||
430 // h/w monitoring not in use
432 (!Config->get_use_hardware_monitoring() &&
434 // AND software monitoring required
436 Config->get_use_sw_monitoring())) {
438 if (apply_gain_automation) {
441 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
442 Sample* const sp = i->data(nframes);
444 for (jack_nframes_t nx = 0; nx < nframes; ++nx) {
449 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
450 Sample* const sp = i->data(nframes);
452 for (jack_nframes_t nx = 0; nx < nframes; ++nx) {
458 if (apply_gain_automation && _session.transport_rolling() && nframes > 0) {
459 _effective_gain = gab[nframes-1];
464 /* manual (scalar) gain */
468 Amp::run (bufs, nframes, _gain, dg, _phase_invert);
471 } else if (_gain != 0 && (_phase_invert || _gain != 1.0)) {
473 /* no need to interpolate current gain value,
474 but its non-unity, so apply it. if the gain
475 is zero, do nothing because we'll ship silence
487 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
488 Sample* const sp = i->data(nframes);
489 apply_gain_to_buffer(sp,nframes,this_gain);
492 } else if (_gain == 0) {
493 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
501 /* actively recording, no monitoring required; leave buffers as-is to save CPU cycles */
505 /* ----------------------------------------------------------------------------------------------------
507 -------------------------------------------------------------------------------------------------- */
509 /* note that post_fader_work cannot be true unless with_redirects was also true, so don't test both */
511 if (post_fader_work) {
513 Glib::RWLock::ReaderLock rm (redirect_lock, Glib::TRY_LOCK);
515 if (mute_gain > 0 || !_mute_affects_post_fader) {
516 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
517 switch ((*i)->placement()) {
521 (*i)->run (bufs, start_frame, end_frame, nframes, offset);
526 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
527 switch ((*i)->placement()) {
531 (*i)->silence (nframes, offset);
539 if (!_soloed && (mute_gain != dmg) && !mute_declick_applied && _mute_affects_control_outs) {
540 Amp::run (bufs, nframes, mute_gain, dmg, _phase_invert);
542 mute_declick_applied = true;
545 /* ----------------------------------------------------------------------------------------------------
547 -------------------------------------------------------------------------------------------------- */
549 if ((_meter_point == MeterPostFader) && co) {
551 solo_audible = solo_gain > 0;
552 mute_audible = dmg > 0 || !_mute_affects_control_outs;
554 if ( // silent anyway
556 (_gain == 0 && !apply_gain_automation) ||
558 // muted by solo of another track
562 // muted by mute of this track
566 // recording but not s/w monitoring
568 (no_monitor && record_enabled() && (!_session.get_auto_input() || _session.actively_recording()))
572 co->silence (nframes, offset);
576 co->deliver_output (bufs, start_frame, end_frame, nframes, offset);
580 /* ----------------------------------------------------------------------
582 ----------------------------------------------------------------------*/
584 if (!_soloed && (mute_gain != dmg) && !mute_declick_applied && _mute_affects_main_outs) {
585 Amp::run (bufs, nframes, mute_gain, dmg, _phase_invert);
587 mute_declick_applied = true;
590 /* ----------------------------------------------------------------------------------------------------
592 -------------------------------------------------------------------------------------------------- */
594 solo_audible = dsg > 0;
595 mute_audible = dmg > 0 || !_mute_affects_main_outs;
597 if (n_outputs().get(_default_type) == 0) {
601 } else if (no_monitor && record_enabled() && (!_session.get_auto_input() || _session.actively_recording())) {
603 IO::silence (nframes, offset);
607 if ( // silent anyway
609 (_gain == 0 && !apply_gain_automation) ||
611 // muted by solo of another track, but not using control outs for solo
613 (!solo_audible && (_session.solo_model() != Session::SoloBus)) ||
615 // muted by mute of this track
621 /* don't use Route::silence() here, because that causes
622 all outputs (sends, port inserts, etc. to be silent).
625 if (_meter_point == MeterPostFader) {
626 peak_meter().reset();
629 IO::silence (nframes, offset);
633 deliver_output(bufs, start_frame, end_frame, nframes, offset);
639 /* ----------------------------------------------------------------------------------------------------
641 -------------------------------------------------------------------------------------------------- */
643 if (meter && (_meter_point == MeterPostFader)) {
644 if ((_gain == 0 && !apply_gain_automation) || dmg == 0) {
647 _meter->run(output_buffers(), nframes, offset);
653 Route::n_process_buffers ()
655 return max (n_inputs(), redirect_max_outs);
659 Route::passthru (jack_nframes_t start_frame, jack_nframes_t end_frame, jack_nframes_t nframes, jack_nframes_t offset, int declick, bool meter_first)
661 BufferSet& bufs = _session.get_scratch_buffers(n_process_buffers());
665 collect_input (bufs, nframes, offset);
667 #define meter_stream meter_first
670 _meter->run(bufs, nframes);
671 meter_stream = false;
676 process_output_buffers (bufs, start_frame, end_frame, nframes, offset, true, declick, meter_stream);
682 Route::passthru_silence (jack_nframes_t start_frame, jack_nframes_t end_frame, jack_nframes_t nframes, jack_nframes_t offset, int declick, bool meter)
684 process_output_buffers (_session.get_silent_buffers (n_process_buffers()), start_frame, end_frame, nframes, offset, true, declick, meter);
688 Route::set_solo (bool yn, void *src)
694 if (_mix_group && src != _mix_group && _mix_group->is_active()) {
695 _mix_group->apply (&Route::set_solo, yn, _mix_group);
701 solo_changed (src); /* EMIT SIGNAL */
702 _solo_control.Changed (); /* EMIT SIGNAL */
707 Route::set_solo_mute (bool yn)
709 Glib::Mutex::Lock lm (declick_lock);
711 /* Called by Session in response to another Route being soloed.
714 desired_solo_gain = (yn?0.0:1.0);
718 Route::set_solo_safe (bool yn, void *src)
720 if (_solo_safe != yn) {
722 solo_safe_changed (src); /* EMIT SIGNAL */
727 Route::set_mute (bool yn, void *src)
730 if (_mix_group && src != _mix_group && _mix_group->is_active()) {
731 _mix_group->apply (&Route::set_mute, yn, _mix_group);
737 mute_changed (src); /* EMIT SIGNAL */
739 _mute_control.Changed (); /* EMIT SIGNAL */
741 Glib::Mutex::Lock lm (declick_lock);
742 desired_mute_gain = (yn?0.0f:1.0f);
747 Route::add_redirect (boost::shared_ptr<Redirect> redirect, void *src, uint32_t* err_streams)
749 ChanCount old_rmo = redirect_max_outs;
751 if (!_session.engine().connected()) {
756 Glib::RWLock::WriterLock lm (redirect_lock);
758 boost::shared_ptr<PluginInsert> pi;
759 boost::shared_ptr<PortInsert> porti;
761 ChanCount potential_max_streams;
763 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(redirect)) != 0) {
766 if (pi->input_streams() == ChanCount::ZERO) {
767 /* generator plugin */
768 _have_internal_generator = true;
771 potential_max_streams = max(pi->input_streams(), pi->output_streams());
773 } else if ((porti = boost::dynamic_pointer_cast<PortInsert>(redirect)) != 0) {
775 /* force new port inserts to start out with an i/o configuration
776 that matches this route's i/o configuration.
778 the "inputs" for the port are supposed to match the output
781 the "outputs" of the route should match the inputs of this
782 route. XXX shouldn't they match the number of active signal
783 streams at the point of insertion?
787 porti->ensure_io (n_outputs ().get(DataType::AUDIO), n_inputs().get(DataType::AUDIO), false, this);
790 // Ensure peak vector sizes before the plugin is activated
791 _meter->setup(potential_max_streams);
793 _redirects.push_back (redirect);
795 if (_reset_plugin_counts (err_streams)) {
796 _redirects.pop_back ();
797 _reset_plugin_counts (0); // it worked before we tried to add it ...
801 redirect->activate ();
802 redirect->active_changed.connect (mem_fun (*this, &Route::redirect_active_proxy));
805 if (redirect_max_outs != old_rmo || old_rmo == ChanCount::ZERO) {
810 redirects_changed (src); /* EMIT SIGNAL */
815 Route::add_redirects (const RedirectList& others, void *src, uint32_t* err_streams)
817 ChanCount old_rmo = redirect_max_outs;
819 if (!_session.engine().connected()) {
824 Glib::RWLock::WriterLock lm (redirect_lock);
826 RedirectList::iterator existing_end = _redirects.end();
829 ChanCount potential_max_streams;
831 for (RedirectList::const_iterator i = others.begin(); i != others.end(); ++i) {
833 boost::shared_ptr<PluginInsert> pi;
835 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(*i)) != 0) {
838 ChanCount m = max(pi->input_streams(), pi->output_streams());
839 if (m > potential_max_streams)
840 potential_max_streams = m;
843 // Ensure peak vector sizes before the plugin is activated
844 _meter->setup(potential_max_streams);
846 _redirects.push_back (*i);
848 if (_reset_plugin_counts (err_streams)) {
850 _redirects.erase (existing_end, _redirects.end());
851 _reset_plugin_counts (0); // it worked before we tried to add it ...
856 (*i)->active_changed.connect (mem_fun (*this, &Route::redirect_active_proxy));
860 if (redirect_max_outs != old_rmo || old_rmo == ChanCount::ZERO) {
864 redirects_changed (src); /* EMIT SIGNAL */
869 Route::clear_redirects (void *src)
871 ChanCount old_rmo = redirect_max_outs;
873 if (!_session.engine().connected()) {
878 Glib::RWLock::WriterLock lm (redirect_lock);
882 if (redirect_max_outs != old_rmo) {
886 redirect_max_outs.reset();
887 _have_internal_generator = false;
888 redirects_changed (src); /* EMIT SIGNAL */
892 Route::remove_redirect (boost::shared_ptr<Redirect> redirect, void *src, uint32_t* err_streams)
894 ChanCount old_rmo = redirect_max_outs;
896 if (!_session.engine().connected()) {
900 redirect_max_outs.reset();
903 Glib::RWLock::WriterLock lm (redirect_lock);
904 RedirectList::iterator i;
905 bool removed = false;
907 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
908 if (*i == redirect) {
910 RedirectList::iterator tmp;
912 /* move along, see failure case for reset_plugin_counts()
913 where we may need to reinsert the redirect.
919 /* stop redirects that send signals to JACK ports
920 from causing noise as a result of no longer being
924 boost::shared_ptr<Send> send;
925 boost::shared_ptr<PortInsert> port_insert;
927 if ((send = boost::dynamic_pointer_cast<Send> (*i)) != 0) {
928 send->disconnect_inputs (this);
929 send->disconnect_outputs (this);
930 } else if ((port_insert = boost::dynamic_pointer_cast<PortInsert> (*i)) != 0) {
931 port_insert->disconnect_inputs (this);
932 port_insert->disconnect_outputs (this);
935 _redirects.erase (i);
948 if (_reset_plugin_counts (err_streams)) {
949 /* get back to where we where */
950 _redirects.insert (i, redirect);
951 /* we know this will work, because it worked before :) */
952 _reset_plugin_counts (0);
958 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
959 boost::shared_ptr<PluginInsert> pi;
961 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(*i)) != 0) {
962 if (pi->is_generator()) {
968 _have_internal_generator = foo;
971 if (old_rmo != redirect_max_outs) {
975 redirects_changed (src); /* EMIT SIGNAL */
980 Route::reset_plugin_counts (uint32_t* lpc)
982 Glib::RWLock::WriterLock lm (redirect_lock);
983 return _reset_plugin_counts (lpc);
988 Route::_reset_plugin_counts (uint32_t* err_streams)
990 RedirectList::iterator r;
993 map<Placement,list<InsertCount> > insert_map;
994 jack_nframes_t initial_streams;
996 redirect_max_outs.reset();
1000 /* divide inserts up by placement so we get the signal flow
1001 properly modelled. we need to do this because the _redirects
1002 list is not sorted by placement, and because other reasons may
1003 exist now or in the future for this separate treatment.
1006 for (r = _redirects.begin(); r != _redirects.end(); ++r) {
1008 boost::shared_ptr<Insert> insert;
1010 /* do this here in case we bomb out before we get to the end of
1014 redirect_max_outs = max ((*r)->output_streams (), redirect_max_outs);
1016 if ((insert = boost::dynamic_pointer_cast<Insert>(*r)) != 0) {
1018 insert_map[insert->placement()].push_back (InsertCount (insert));
1020 /* reset plugin counts back to one for now so
1021 that we have a predictable, controlled
1022 state to try to configure.
1025 boost::shared_ptr<PluginInsert> pi;
1027 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(insert)) != 0) {
1031 } else if (boost::dynamic_pointer_cast<Send> (*r) != 0) {
1044 /* Now process each placement in order, checking to see if we
1045 can really do what has been requested.
1050 if (check_some_plugin_counts (insert_map[PreFader], n_inputs ().get(_default_type), err_streams)) {
1054 /* figure out the streams that will feed into PreFader */
1056 if (!insert_map[PreFader].empty()) {
1057 InsertCount& ic (insert_map[PreFader].back());
1058 initial_streams = ic.insert->compute_output_streams (ic.cnt);
1060 initial_streams = n_inputs ().get(_default_type);
1065 if (check_some_plugin_counts (insert_map[PostFader], initial_streams, err_streams)) {
1069 /* OK, everything can be set up correctly, so lets do it */
1071 apply_some_plugin_counts (insert_map[PreFader]);
1072 apply_some_plugin_counts (insert_map[PostFader]);
1074 /* recompute max outs of any redirect */
1078 redirect_max_outs.reset();
1079 RedirectList::iterator prev = _redirects.end();
1081 for (r = _redirects.begin(); r != _redirects.end(); prev = r, ++r) {
1082 boost::shared_ptr<Send> s;
1084 if ((s = boost::dynamic_pointer_cast<Send> (*r)) != 0) {
1085 if (r == _redirects.begin()) {
1086 s->expect_inputs (n_inputs());
1088 s->expect_inputs ((*prev)->output_streams());
1092 redirect_max_outs = max ((*r)->output_streams(), redirect_max_outs);
1101 Route::apply_some_plugin_counts (list<InsertCount>& iclist)
1103 list<InsertCount>::iterator i;
1105 for (i = iclist.begin(); i != iclist.end(); ++i) {
1107 if ((*i).insert->configure_io ((*i).cnt, (*i).in, (*i).out)) {
1110 /* make sure that however many we have, they are all active */
1111 (*i).insert->activate ();
1118 Route::check_some_plugin_counts (list<InsertCount>& iclist, int32_t required_inputs, uint32_t* err_streams)
1120 list<InsertCount>::iterator i;
1122 for (i = iclist.begin(); i != iclist.end(); ++i) {
1124 if (((*i).cnt = (*i).insert->can_support_input_configuration (required_inputs)) < 0) {
1126 *err_streams = required_inputs;
1131 (*i).in = required_inputs;
1132 (*i).out = (*i).insert->compute_output_streams ((*i).cnt);
1134 required_inputs = (*i).out;
1141 Route::copy_redirects (const Route& other, Placement placement, uint32_t* err_streams)
1143 ChanCount old_rmo = redirect_max_outs;
1149 RedirectList to_be_deleted;
1152 Glib::RWLock::WriterLock lm (redirect_lock);
1153 RedirectList::iterator tmp;
1154 RedirectList the_copy;
1156 the_copy = _redirects;
1158 /* remove all relevant redirects */
1160 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ) {
1164 if ((*i)->placement() == placement) {
1165 to_be_deleted.push_back (*i);
1166 _redirects.erase (i);
1172 /* now copy the relevant ones from "other" */
1174 for (RedirectList::const_iterator i = other._redirects.begin(); i != other._redirects.end(); ++i) {
1175 if ((*i)->placement() == placement) {
1176 _redirects.push_back (Redirect::clone (*i));
1180 /* reset plugin stream handling */
1182 if (_reset_plugin_counts (err_streams)) {
1184 /* FAILED COPY ATTEMPT: we have to restore order */
1186 /* delete all cloned redirects */
1188 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ) {
1193 if ((*i)->placement() == placement) {
1194 _redirects.erase (i);
1200 /* restore the natural order */
1202 _redirects = the_copy;
1203 redirect_max_outs = old_rmo;
1205 /* we failed, even though things are OK again */
1211 /* SUCCESSFUL COPY ATTEMPT: delete the redirects we removed pre-copy */
1212 to_be_deleted.clear ();
1216 if (redirect_max_outs != old_rmo || old_rmo == ChanCount::ZERO) {
1220 redirects_changed (this); /* EMIT SIGNAL */
1225 Route::all_redirects_flip ()
1227 Glib::RWLock::ReaderLock lm (redirect_lock);
1229 if (_redirects.empty()) {
1233 bool first_is_on = _redirects.front()->active();
1235 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
1236 (*i)->set_active (!first_is_on, this);
1241 Route::all_redirects_active (bool state)
1243 Glib::RWLock::ReaderLock lm (redirect_lock);
1245 if (_redirects.empty()) {
1249 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
1250 (*i)->set_active (state, this);
1254 struct RedirectSorter {
1255 bool operator() (boost::shared_ptr<const Redirect> a, boost::shared_ptr<const Redirect> b) {
1256 return a->sort_key() < b->sort_key();
1261 Route::sort_redirects (uint32_t* err_streams)
1264 RedirectSorter comparator;
1265 Glib::RWLock::WriterLock lm (redirect_lock);
1266 ChanCount old_rmo = redirect_max_outs;
1268 /* the sweet power of C++ ... */
1270 RedirectList as_it_was_before = _redirects;
1272 _redirects.sort (comparator);
1274 if (_reset_plugin_counts (err_streams)) {
1275 _redirects = as_it_was_before;
1276 redirect_max_outs = old_rmo;
1282 redirects_changed (this); /* EMIT SIGNAL */
1294 Route::get_template()
1296 return state(false);
1300 Route::state(bool full_state)
1302 XMLNode *node = new XMLNode("Route");
1304 RedirectList:: iterator i;
1308 snprintf (buf, sizeof (buf), "0x%x", _flags);
1309 node->add_property("flags", buf);
1312 node->add_property("default-type", _default_type.to_string());
1314 node->add_property("active", _active?"yes":"no");
1315 node->add_property("muted", _muted?"yes":"no");
1316 node->add_property("soloed", _soloed?"yes":"no");
1317 node->add_property("phase-invert", _phase_invert?"yes":"no");
1318 node->add_property("mute-affects-pre-fader", _mute_affects_pre_fader?"yes":"no");
1319 node->add_property("mute-affects-post-fader", _mute_affects_post_fader?"yes":"no");
1320 node->add_property("mute-affects-control-outs", _mute_affects_control_outs?"yes":"no");
1321 node->add_property("mute-affects-main-outs", _mute_affects_main_outs?"yes":"no");
1324 node->add_property("edit-group", _edit_group->name());
1327 node->add_property("mix-group", _mix_group->name());
1330 string order_string;
1331 OrderKeys::iterator x = order_keys.begin();
1333 while (x != order_keys.end()) {
1334 order_string += (*x).first;
1335 order_string += '=';
1336 snprintf (buf, sizeof(buf), "%ld", (*x).second);
1337 order_string += buf;
1341 if (x == order_keys.end()) {
1345 order_string += ':';
1347 node->add_property ("order-keys", order_string);
1349 node->add_child_nocopy (IO::state (full_state));
1351 if (_control_outs) {
1352 XMLNode* cnode = new XMLNode (X_("ControlOuts"));
1353 cnode->add_child_nocopy (_control_outs->state (full_state));
1354 node->add_child_nocopy (*cnode);
1357 if (_comment.length()) {
1358 XMLNode *cmt = node->add_child ("Comment");
1359 cmt->add_content (_comment);
1365 path = _session.snap_name();
1367 path += legalize_for_path (_name);
1368 path += ".automation";
1370 /* XXX we didn't ask for a state save, we asked for the current state.
1374 if (save_automation (path)) {
1375 error << _("Could not get state of route. Problem with save_automation") << endmsg;
1378 aevents = node->add_child ("Automation");
1379 aevents->add_property ("path", path);
1382 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
1383 node->add_child_nocopy((*i)->state (full_state));
1387 node->add_child_copy (*_extra_xml);
1394 Route::set_deferred_state ()
1397 XMLNodeConstIterator niter;
1399 if (!deferred_state) {
1403 nlist = deferred_state->children();
1405 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
1406 add_redirect_from_xml (**niter);
1409 delete deferred_state;
1414 Route::add_redirect_from_xml (const XMLNode& node)
1416 const XMLProperty *prop;
1418 if (node.name() == "Send") {
1422 boost::shared_ptr<Send> send (new Send (_session, node));
1423 add_redirect (send, this);
1426 catch (failed_constructor &err) {
1427 error << _("Send construction failed") << endmsg;
1431 } else if (node.name() == "Insert") {
1434 if ((prop = node.property ("type")) != 0) {
1436 boost::shared_ptr<Insert> insert;
1438 if (prop->value() == "ladspa" || prop->value() == "Ladspa" || prop->value() == "vst") {
1440 insert.reset (new PluginInsert(_session, node));
1442 } else if (prop->value() == "port") {
1445 insert.reset (new PortInsert (_session, node));
1449 error << string_compose(_("unknown Insert type \"%1\"; ignored"), prop->value()) << endmsg;
1452 add_redirect (insert, this);
1455 error << _("Insert XML node has no type property") << endmsg;
1459 catch (failed_constructor &err) {
1460 warning << _("insert could not be created. Ignored.") << endmsg;
1467 Route::set_state (const XMLNode& node)
1470 XMLNodeConstIterator niter;
1472 XMLPropertyList plist;
1473 const XMLProperty *prop;
1475 if (node.name() != "Route"){
1476 error << string_compose(_("Bad node sent to Route::set_state() [%1]"), node.name()) << endmsg;
1480 if ((prop = node.property ("flags")) != 0) {
1482 sscanf (prop->value().c_str(), "0x%x", &x);
1488 if ((prop = node.property ("default-type")) != 0) {
1489 _default_type = DataType(prop->value());
1490 assert(_default_type != DataType::NIL);
1493 if ((prop = node.property ("phase-invert")) != 0) {
1494 set_phase_invert(prop->value()=="yes"?true:false, this);
1497 if ((prop = node.property ("active")) != 0) {
1498 set_active (prop->value() == "yes");
1501 if ((prop = node.property ("muted")) != 0) {
1502 bool yn = prop->value()=="yes"?true:false;
1504 /* force reset of mute status */
1508 mute_gain = desired_mute_gain;
1511 if ((prop = node.property ("soloed")) != 0) {
1512 bool yn = prop->value()=="yes"?true:false;
1514 /* force reset of solo status */
1517 set_solo (yn, this);
1518 solo_gain = desired_solo_gain;
1521 if ((prop = node.property ("mute-affects-pre-fader")) != 0) {
1522 _mute_affects_pre_fader = (prop->value()=="yes")?true:false;
1525 if ((prop = node.property ("mute-affects-post-fader")) != 0) {
1526 _mute_affects_post_fader = (prop->value()=="yes")?true:false;
1529 if ((prop = node.property ("mute-affects-control-outs")) != 0) {
1530 _mute_affects_control_outs = (prop->value()=="yes")?true:false;
1533 if ((prop = node.property ("mute-affects-main-outs")) != 0) {
1534 _mute_affects_main_outs = (prop->value()=="yes")?true:false;
1537 if ((prop = node.property ("edit-group")) != 0) {
1538 RouteGroup* edit_group = _session.edit_group_by_name(prop->value());
1539 if(edit_group == 0) {
1540 error << string_compose(_("Route %1: unknown edit group \"%2 in saved state (ignored)"), _name, prop->value()) << endmsg;
1542 set_edit_group(edit_group, this);
1546 if ((prop = node.property ("order-keys")) != 0) {
1550 string::size_type colon, equal;
1551 string remaining = prop->value();
1553 while (remaining.length()) {
1555 if ((equal = remaining.find_first_of ('=')) == string::npos || equal == remaining.length()) {
1556 error << string_compose (_("badly formed order key string in state file! [%1] ... ignored."), remaining)
1559 if (sscanf (remaining.substr (equal+1).c_str(), "%ld", &n) != 1) {
1560 error << string_compose (_("badly formed order key string in state file! [%1] ... ignored."), remaining)
1563 set_order_key (remaining.substr (0, equal), n);
1567 colon = remaining.find_first_of (':');
1569 if (colon != string::npos) {
1570 remaining = remaining.substr (colon+1);
1577 nlist = node.children();
1579 if (deferred_state) {
1580 delete deferred_state;
1583 deferred_state = new XMLNode("deferred state");
1585 /* set parent class properties before anything else */
1587 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
1591 if (child->name() == IO::state_node_name) {
1593 IO::set_state (*child);
1598 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
1602 if (child->name() == "Send") {
1605 if (!IO::ports_legal) {
1607 deferred_state->add_child_copy (*child);
1610 add_redirect_from_xml (*child);
1613 } else if (child->name() == "Insert") {
1615 if (!IO::ports_legal) {
1617 deferred_state->add_child_copy (*child);
1621 add_redirect_from_xml (*child);
1624 } else if (child->name() == "Automation") {
1626 XMLPropertyList plist;
1627 XMLPropertyConstIterator piter;
1630 plist = child->properties();
1631 for (piter = plist.begin(); piter != plist.end(); ++piter) {
1633 if (prop->name() == "path") {
1634 load_automation (prop->value());
1638 } else if (child->name() == "ControlOuts") {
1640 string coutname = _name;
1641 coutname += _("[control]");
1643 _control_outs = new IO (_session, coutname);
1644 _control_outs->set_state (**(child->children().begin()));
1646 } else if (child->name() == "Comment") {
1648 /* XXX this is a terrible API design in libxml++ */
1650 XMLNode *cmt = *(child->children().begin());
1651 _comment = cmt->content();
1653 } else if (child->name() == "extra") {
1654 _extra_xml = new XMLNode (*child);
1658 if ((prop = node.property ("mix-group")) != 0) {
1659 RouteGroup* mix_group = _session.mix_group_by_name(prop->value());
1660 if (mix_group == 0) {
1661 error << string_compose(_("Route %1: unknown mix group \"%2 in saved state (ignored)"), _name, prop->value()) << endmsg;
1663 set_mix_group(mix_group, this);
1671 Route::curve_reallocate ()
1673 // _gain_automation_curve.finish_resize ();
1674 // _pan_automation_curve.finish_resize ();
1678 Route::silence (jack_nframes_t nframes, jack_nframes_t offset)
1682 IO::silence (nframes, offset);
1684 if (_control_outs) {
1685 _control_outs->silence (nframes, offset);
1689 Glib::RWLock::ReaderLock lm (redirect_lock, Glib::TRY_LOCK);
1692 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
1693 boost::shared_ptr<PluginInsert> pi;
1694 if (!_active && (pi = boost::dynamic_pointer_cast<PluginInsert> (*i)) != 0) {
1695 // skip plugins, they don't need anything when we're not active
1699 (*i)->silence (nframes, offset);
1702 if (nframes == _session.get_block_size() && offset == 0) {
1712 Route::set_control_outs (const vector<string>& ports)
1714 Glib::Mutex::Lock lm (control_outs_lock);
1715 vector<string>::const_iterator i;
1717 if (_control_outs) {
1718 delete _control_outs;
1722 if (ports.empty()) {
1726 string coutname = _name;
1727 coutname += _("[control]");
1729 _control_outs = new IO (_session, coutname);
1731 /* our control outs need as many outputs as we
1732 have outputs. we track the changes in ::output_change_handler().
1735 _control_outs->ensure_io (0, n_outputs().get(DataType::AUDIO), true, this);
1741 Route::set_edit_group (RouteGroup *eg, void *src)
1744 if (eg == _edit_group) {
1749 _edit_group->remove (this);
1752 if ((_edit_group = eg) != 0) {
1753 _edit_group->add (this);
1756 _session.set_dirty ();
1757 edit_group_changed (src); /* EMIT SIGNAL */
1761 Route::drop_edit_group (void *src)
1764 _session.set_dirty ();
1765 edit_group_changed (src); /* EMIT SIGNAL */
1769 Route::set_mix_group (RouteGroup *mg, void *src)
1772 if (mg == _mix_group) {
1777 _mix_group->remove (this);
1780 if ((_mix_group = mg) != 0) {
1781 _mix_group->add (this);
1784 _session.set_dirty ();
1785 mix_group_changed (src); /* EMIT SIGNAL */
1789 Route::drop_mix_group (void *src)
1792 _session.set_dirty ();
1793 mix_group_changed (src); /* EMIT SIGNAL */
1797 Route::set_comment (string cmt, void *src)
1800 comment_changed (src);
1801 _session.set_dirty ();
1805 Route::feeds (boost::shared_ptr<Route> other)
1810 uint32_t no = self.n_outputs().get_total();
1811 uint32_t ni = other->n_inputs ().get_total();
1813 for (i = 0; i < no; ++i) {
1814 for (j = 0; j < ni; ++j) {
1815 if (self.output(i)->connected_to (other->input(j)->name())) {
1821 /* check Redirects which may also interconnect Routes */
1823 for (RedirectList::iterator r = _redirects.begin(); r != _redirects.end(); r++) {
1825 no = (*r)->n_outputs().get_total();
1827 for (i = 0; i < no; ++i) {
1828 for (j = 0; j < ni; ++j) {
1829 if ((*r)->output(i)->connected_to (other->input (j)->name())) {
1836 /* check for control room outputs which may also interconnect Routes */
1838 if (_control_outs) {
1840 no = _control_outs->n_outputs().get_total();
1842 for (i = 0; i < no; ++i) {
1843 for (j = 0; j < ni; ++j) {
1844 if (_control_outs->output(i)->connected_to (other->input (j)->name())) {
1855 Route::set_mute_config (mute_type t, bool onoff, void *src)
1859 _mute_affects_pre_fader = onoff;
1860 pre_fader_changed(src); /* EMIT SIGNAL */
1864 _mute_affects_post_fader = onoff;
1865 post_fader_changed(src); /* EMIT SIGNAL */
1869 _mute_affects_control_outs = onoff;
1870 control_outs_changed(src); /* EMIT SIGNAL */
1874 _mute_affects_main_outs = onoff;
1875 main_outs_changed(src); /* EMIT SIGNAL */
1881 Route::get_mute_config (mute_type t)
1887 onoff = _mute_affects_pre_fader;
1890 onoff = _mute_affects_post_fader;
1893 onoff = _mute_affects_control_outs;
1896 onoff = _mute_affects_main_outs;
1904 Route::set_active (bool yn)
1907 active_changed(); /* EMIT SIGNAL */
1911 Route::handle_transport_stopped (bool abort_ignored, bool did_locate, bool can_flush_redirects)
1913 jack_nframes_t now = _session.transport_frame();
1916 Glib::RWLock::ReaderLock lm (redirect_lock);
1919 automation_snapshot (now);
1922 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
1924 if (Config->get_plugins_stop_with_transport() && can_flush_redirects) {
1925 (*i)->deactivate ();
1929 (*i)->transport_stopped (now);
1933 IO::transport_stopped (now);
1935 _roll_delay = _initial_delay;
1939 Route::get_memento() const
1941 void (Route::*pmf)(state_id_t) = &Route::set_state;
1942 return sigc::bind (mem_fun (*(const_cast<Route *>(this)), pmf), _current_state_id);
1946 Route::set_state (state_id_t id)
1952 Route::input_change_handler (IOChange change, void *ignored)
1954 if (change & ConfigurationChanged) {
1955 reset_plugin_counts (0);
1960 Route::output_change_handler (IOChange change, void *ignored)
1962 if (change & ConfigurationChanged) {
1963 if (_control_outs) {
1964 _control_outs->ensure_io (0, n_outputs().get(DataType::AUDIO), true, this);
1967 reset_plugin_counts (0);
1972 Route::pans_required () const
1974 if (n_outputs().get(DataType::AUDIO) < 2) {
1978 return max (n_inputs ().get(DataType::AUDIO), static_cast<size_t>(redirect_max_outs.get(DataType::AUDIO)));
1982 Route::no_roll (jack_nframes_t nframes, jack_nframes_t start_frame, jack_nframes_t end_frame, jack_nframes_t offset,
1983 bool session_state_changing, bool can_record, bool rec_monitors_input)
1985 if (n_outputs().get_total() == 0) {
1989 if (session_state_changing || !_active) {
1990 silence (nframes, offset);
1994 apply_gain_automation = false;
1996 if (n_inputs().get_total()) {
1997 passthru (start_frame, end_frame, nframes, offset, 0, false);
1999 silence (nframes, offset);
2006 Route::check_initial_delay (jack_nframes_t nframes, jack_nframes_t& offset, jack_nframes_t& transport_frame)
2008 if (_roll_delay > nframes) {
2010 _roll_delay -= nframes;
2011 silence (nframes, offset);
2012 /* transport frame is not legal for caller to use */
2015 } else if (_roll_delay > 0) {
2017 nframes -= _roll_delay;
2019 silence (_roll_delay, offset);
2021 offset += _roll_delay;
2022 transport_frame += _roll_delay;
2031 Route::roll (jack_nframes_t nframes, jack_nframes_t start_frame, jack_nframes_t end_frame, jack_nframes_t offset, int declick,
2032 bool can_record, bool rec_monitors_input)
2035 Glib::RWLock::ReaderLock lm (redirect_lock, Glib::TRY_LOCK);
2037 // automation snapshot can also be called from the non-rt context
2038 // and it uses the redirect list, so we take the lock out here
2039 automation_snapshot (_session.transport_frame());
2043 if ((n_outputs().get_total() == 0 && _redirects.empty()) || n_inputs().get_total() == 0 || !_active) {
2044 silence (nframes, offset);
2048 jack_nframes_t unused = 0;
2050 if ((nframes = check_initial_delay (nframes, offset, unused)) == 0) {
2056 apply_gain_automation = false;
2059 Glib::Mutex::Lock am (automation_lock, Glib::TRY_LOCK);
2061 if (am.locked() && _session.transport_rolling()) {
2063 if (gain_automation_playback()) {
2064 apply_gain_automation = _gain_automation_curve.rt_safe_get_vector (start_frame, end_frame, _session.gain_automation_buffer(), nframes);
2069 passthru (start_frame, end_frame, nframes, offset, declick, false);
2075 Route::silent_roll (jack_nframes_t nframes, jack_nframes_t start_frame, jack_nframes_t end_frame, jack_nframes_t offset,
2076 bool can_record, bool rec_monitors_input)
2078 silence (nframes, offset);
2083 Route::toggle_monitor_input ()
2085 for (PortSet::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
2086 i->request_monitor_input( ! i->monitoring_input());
2091 Route::has_external_redirects () const
2093 boost::shared_ptr<const PortInsert> pi;
2095 for (RedirectList::const_iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2096 if ((pi = boost::dynamic_pointer_cast<const PortInsert>(*i)) != 0) {
2098 for (PortSet::const_iterator port = pi->outputs().begin();
2099 port != pi->outputs().end(); ++port) {
2101 string port_name = port->name();
2102 string client_name = port_name.substr (0, port_name.find(':'));
2104 /* only say "yes" if the redirect is actually in use */
2106 if (client_name != "ardour" && pi->active()) {
2117 Route::flush_redirects ()
2119 /* XXX shouldn't really try to take this lock, since
2120 this is called from the RT audio thread.
2123 Glib::RWLock::ReaderLock lm (redirect_lock);
2125 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2126 (*i)->deactivate ();
2132 Route::set_meter_point (MeterPoint p, void *src)
2134 if (_meter_point != p) {
2136 meter_change (src); /* EMIT SIGNAL */
2137 _session.set_dirty ();
2142 Route::update_total_latency ()
2146 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2147 if ((*i)->active ()) {
2148 _own_latency += (*i)->latency ();
2152 set_port_latency (_own_latency);
2154 /* this (virtual) function is used for pure Routes,
2155 not derived classes like AudioTrack. this means
2156 that the data processed here comes from an input
2157 port, not prerecorded material, and therefore we
2158 have to take into account any input latency.
2161 _own_latency += input_latency ();
2163 return _own_latency;
2167 Route::set_latency_delay (jack_nframes_t longest_session_latency)
2169 _initial_delay = longest_session_latency - _own_latency;
2171 if (_session.transport_stopped()) {
2172 _roll_delay = _initial_delay;
2177 Route::automation_snapshot (jack_nframes_t now)
2179 IO::automation_snapshot (now);
2181 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2182 (*i)->automation_snapshot (now);
2186 Route::ToggleControllable::ToggleControllable (Route& s, ToggleType tp)
2187 : route (s), type(tp)
2193 Route::ToggleControllable::set_value (float val)
2195 bool bval = ((val >= 0.5f) ? true: false);
2199 route.set_mute (bval, this);
2202 route.set_solo (bval, this);
2210 Route::ToggleControllable::get_value (void) const
2216 val = route.muted() ? 1.0f : 0.0f;
2219 val = route.soloed() ? 1.0f : 0.0f;
2229 Route::set_block_size (jack_nframes_t nframes)
2231 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2232 (*i)->set_block_size (nframes);
2237 Route::redirect_active_proxy (Redirect* ignored, void* ignored_src)
2239 _session.update_latency_compensation (false, false);
2243 Route::protect_automation ()
2245 switch (gain_automation_state()) {
2248 set_gain_automation_state (Off);
2254 switch (panner().automation_state ()) {
2257 panner().set_automation_state (Off);
2263 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2264 boost::shared_ptr<PluginInsert> pi;
2265 if ((pi = boost::dynamic_pointer_cast<PluginInsert> (*i)) != 0) {
2266 pi->protect_automation ();
2272 Route::set_pending_declick (int declick)
2275 /* this call is not allowed to turn off a pending declick unless "force" is true */
2277 _pending_declick = declick;
2279 // cerr << _name << ": after setting to " << declick << " pending declick = " << _pending_declick << endl;
2281 _pending_declick = 0;