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);
656 return n_inputs(); // FIXME?
660 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)
662 BufferSet& bufs = _session.get_scratch_buffers(n_process_buffers());
666 collect_input (bufs, nframes, offset);
668 #define meter_stream meter_first
671 _meter->run(bufs, nframes);
672 meter_stream = false;
677 process_output_buffers (bufs, start_frame, end_frame, nframes, offset, true, declick, meter_stream);
683 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)
685 process_output_buffers (_session.get_silent_buffers (n_process_buffers()), start_frame, end_frame, nframes, offset, true, declick, meter);
689 Route::set_solo (bool yn, void *src)
695 if (_mix_group && src != _mix_group && _mix_group->is_active()) {
696 _mix_group->apply (&Route::set_solo, yn, _mix_group);
702 solo_changed (src); /* EMIT SIGNAL */
703 _solo_control.Changed (); /* EMIT SIGNAL */
708 Route::set_solo_mute (bool yn)
710 Glib::Mutex::Lock lm (declick_lock);
712 /* Called by Session in response to another Route being soloed.
715 desired_solo_gain = (yn?0.0:1.0);
719 Route::set_solo_safe (bool yn, void *src)
721 if (_solo_safe != yn) {
723 solo_safe_changed (src); /* EMIT SIGNAL */
728 Route::set_mute (bool yn, void *src)
731 if (_mix_group && src != _mix_group && _mix_group->is_active()) {
732 _mix_group->apply (&Route::set_mute, yn, _mix_group);
738 mute_changed (src); /* EMIT SIGNAL */
740 _mute_control.Changed (); /* EMIT SIGNAL */
742 Glib::Mutex::Lock lm (declick_lock);
743 desired_mute_gain = (yn?0.0f:1.0f);
748 Route::add_redirect (boost::shared_ptr<Redirect> redirect, void *src, uint32_t* err_streams)
750 ChanCount old_rmo = redirect_max_outs;
752 if (!_session.engine().connected()) {
757 Glib::RWLock::WriterLock lm (redirect_lock);
759 boost::shared_ptr<PluginInsert> pi;
760 boost::shared_ptr<PortInsert> porti;
762 redirect->set_default_type(_default_type);
764 ChanCount potential_max_streams;
766 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(redirect)) != 0) {
769 if (pi->input_streams() == ChanCount::ZERO) {
770 /* generator plugin */
771 _have_internal_generator = true;
774 potential_max_streams = max(pi->input_streams(), pi->output_streams());
776 } else if ((porti = boost::dynamic_pointer_cast<PortInsert>(redirect)) != 0) {
778 /* force new port inserts to start out with an i/o configuration
779 that matches this route's i/o configuration.
781 the "inputs" for the port are supposed to match the output
784 the "outputs" of the route should match the inputs of this
785 route. XXX shouldn't they match the number of active signal
786 streams at the point of insertion?
790 porti->ensure_io (n_outputs (), n_inputs(), false, this);
793 // Ensure peak vector sizes before the plugin is activated
794 _meter->setup(potential_max_streams);
796 _redirects.push_back (redirect);
798 if (_reset_plugin_counts (err_streams)) {
799 _redirects.pop_back ();
800 _reset_plugin_counts (0); // it worked before we tried to add it ...
804 redirect->activate ();
805 redirect->active_changed.connect (mem_fun (*this, &Route::redirect_active_proxy));
808 if (redirect_max_outs != old_rmo || old_rmo == ChanCount::ZERO) {
813 redirects_changed (src); /* EMIT SIGNAL */
818 Route::add_redirects (const RedirectList& others, void *src, uint32_t* err_streams)
820 ChanCount old_rmo = redirect_max_outs;
822 if (!_session.engine().connected()) {
827 Glib::RWLock::WriterLock lm (redirect_lock);
829 RedirectList::iterator existing_end = _redirects.end();
832 ChanCount potential_max_streams;
834 for (RedirectList::const_iterator i = others.begin(); i != others.end(); ++i) {
836 boost::shared_ptr<PluginInsert> pi;
838 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(*i)) != 0) {
841 ChanCount m = max(pi->input_streams(), pi->output_streams());
842 if (m > potential_max_streams)
843 potential_max_streams = m;
846 // Ensure peak vector sizes before the plugin is activated
847 _meter->setup(potential_max_streams);
849 _redirects.push_back (*i);
851 if (_reset_plugin_counts (err_streams)) {
853 _redirects.erase (existing_end, _redirects.end());
854 _reset_plugin_counts (0); // it worked before we tried to add it ...
859 (*i)->active_changed.connect (mem_fun (*this, &Route::redirect_active_proxy));
863 if (redirect_max_outs != old_rmo || old_rmo == ChanCount::ZERO) {
867 redirects_changed (src); /* EMIT SIGNAL */
872 Route::clear_redirects (void *src)
874 ChanCount old_rmo = redirect_max_outs;
876 if (!_session.engine().connected()) {
881 Glib::RWLock::WriterLock lm (redirect_lock);
885 if (redirect_max_outs != old_rmo) {
889 redirect_max_outs.reset();
890 _have_internal_generator = false;
891 redirects_changed (src); /* EMIT SIGNAL */
895 Route::remove_redirect (boost::shared_ptr<Redirect> redirect, void *src, uint32_t* err_streams)
897 ChanCount old_rmo = redirect_max_outs;
899 if (!_session.engine().connected()) {
903 redirect_max_outs.reset();
906 Glib::RWLock::WriterLock lm (redirect_lock);
907 RedirectList::iterator i;
908 bool removed = false;
910 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
911 if (*i == redirect) {
913 RedirectList::iterator tmp;
915 /* move along, see failure case for reset_plugin_counts()
916 where we may need to reinsert the redirect.
922 /* stop redirects that send signals to JACK ports
923 from causing noise as a result of no longer being
927 boost::shared_ptr<Send> send;
928 boost::shared_ptr<PortInsert> port_insert;
930 if ((send = boost::dynamic_pointer_cast<Send> (*i)) != 0) {
931 send->disconnect_inputs (this);
932 send->disconnect_outputs (this);
933 } else if ((port_insert = boost::dynamic_pointer_cast<PortInsert> (*i)) != 0) {
934 port_insert->disconnect_inputs (this);
935 port_insert->disconnect_outputs (this);
938 _redirects.erase (i);
951 if (_reset_plugin_counts (err_streams)) {
952 /* get back to where we where */
953 _redirects.insert (i, redirect);
954 /* we know this will work, because it worked before :) */
955 _reset_plugin_counts (0);
961 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
962 boost::shared_ptr<PluginInsert> pi;
964 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(*i)) != 0) {
965 if (pi->is_generator()) {
971 _have_internal_generator = foo;
974 if (old_rmo != redirect_max_outs) {
978 redirects_changed (src); /* EMIT SIGNAL */
983 Route::reset_plugin_counts (uint32_t* lpc)
985 Glib::RWLock::WriterLock lm (redirect_lock);
986 return _reset_plugin_counts (lpc);
991 Route::_reset_plugin_counts (uint32_t* err_streams)
993 RedirectList::iterator r;
996 map<Placement,list<InsertCount> > insert_map;
997 jack_nframes_t initial_streams;
999 redirect_max_outs.reset();
1003 /* divide inserts up by placement so we get the signal flow
1004 properly modelled. we need to do this because the _redirects
1005 list is not sorted by placement, and because other reasons may
1006 exist now or in the future for this separate treatment.
1009 for (r = _redirects.begin(); r != _redirects.end(); ++r) {
1011 boost::shared_ptr<Insert> insert;
1013 /* do this here in case we bomb out before we get to the end of
1017 redirect_max_outs = max ((*r)->output_streams (), redirect_max_outs);
1019 if ((insert = boost::dynamic_pointer_cast<Insert>(*r)) != 0) {
1021 insert_map[insert->placement()].push_back (InsertCount (insert));
1023 /* reset plugin counts back to one for now so
1024 that we have a predictable, controlled
1025 state to try to configure.
1028 boost::shared_ptr<PluginInsert> pi;
1030 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(insert)) != 0) {
1034 } else if (boost::dynamic_pointer_cast<Send> (*r) != 0) {
1047 /* Now process each placement in order, checking to see if we
1048 can really do what has been requested.
1053 if (check_some_plugin_counts (insert_map[PreFader], n_inputs ().get(_default_type), err_streams)) {
1057 /* figure out the streams that will feed into PreFader */
1059 if (!insert_map[PreFader].empty()) {
1060 InsertCount& ic (insert_map[PreFader].back());
1061 initial_streams = ic.insert->compute_output_streams (ic.cnt);
1063 initial_streams = n_inputs ().get(_default_type);
1068 if (check_some_plugin_counts (insert_map[PostFader], initial_streams, err_streams)) {
1072 /* OK, everything can be set up correctly, so lets do it */
1074 apply_some_plugin_counts (insert_map[PreFader]);
1075 apply_some_plugin_counts (insert_map[PostFader]);
1077 /* recompute max outs of any redirect */
1081 redirect_max_outs.reset();
1082 RedirectList::iterator prev = _redirects.end();
1084 for (r = _redirects.begin(); r != _redirects.end(); prev = r, ++r) {
1085 boost::shared_ptr<Send> s;
1087 if ((s = boost::dynamic_pointer_cast<Send> (*r)) != 0) {
1088 if (r == _redirects.begin()) {
1089 s->expect_inputs (n_inputs());
1091 s->expect_inputs ((*prev)->output_streams());
1095 redirect_max_outs = max ((*r)->output_streams(), redirect_max_outs);
1104 Route::apply_some_plugin_counts (list<InsertCount>& iclist)
1106 list<InsertCount>::iterator i;
1108 for (i = iclist.begin(); i != iclist.end(); ++i) {
1110 if ((*i).insert->configure_io ((*i).cnt, (*i).in, (*i).out)) {
1113 /* make sure that however many we have, they are all active */
1114 (*i).insert->activate ();
1121 Route::check_some_plugin_counts (list<InsertCount>& iclist, int32_t required_inputs, uint32_t* err_streams)
1123 list<InsertCount>::iterator i;
1125 for (i = iclist.begin(); i != iclist.end(); ++i) {
1127 if (((*i).cnt = (*i).insert->can_support_input_configuration (required_inputs)) < 0) {
1129 *err_streams = required_inputs;
1134 (*i).in = required_inputs;
1135 (*i).out = (*i).insert->compute_output_streams ((*i).cnt);
1137 required_inputs = (*i).out;
1144 Route::copy_redirects (const Route& other, Placement placement, uint32_t* err_streams)
1146 ChanCount old_rmo = redirect_max_outs;
1152 RedirectList to_be_deleted;
1155 Glib::RWLock::WriterLock lm (redirect_lock);
1156 RedirectList::iterator tmp;
1157 RedirectList the_copy;
1159 the_copy = _redirects;
1161 /* remove all relevant redirects */
1163 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ) {
1167 if ((*i)->placement() == placement) {
1168 to_be_deleted.push_back (*i);
1169 _redirects.erase (i);
1175 /* now copy the relevant ones from "other" */
1177 for (RedirectList::const_iterator i = other._redirects.begin(); i != other._redirects.end(); ++i) {
1178 if ((*i)->placement() == placement) {
1179 _redirects.push_back (Redirect::clone (*i));
1183 /* reset plugin stream handling */
1185 if (_reset_plugin_counts (err_streams)) {
1187 /* FAILED COPY ATTEMPT: we have to restore order */
1189 /* delete all cloned redirects */
1191 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ) {
1196 if ((*i)->placement() == placement) {
1197 _redirects.erase (i);
1203 /* restore the natural order */
1205 _redirects = the_copy;
1206 redirect_max_outs = old_rmo;
1208 /* we failed, even though things are OK again */
1214 /* SUCCESSFUL COPY ATTEMPT: delete the redirects we removed pre-copy */
1215 to_be_deleted.clear ();
1219 if (redirect_max_outs != old_rmo || old_rmo == ChanCount::ZERO) {
1223 redirects_changed (this); /* EMIT SIGNAL */
1228 Route::all_redirects_flip ()
1230 Glib::RWLock::ReaderLock lm (redirect_lock);
1232 if (_redirects.empty()) {
1236 bool first_is_on = _redirects.front()->active();
1238 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
1239 (*i)->set_active (!first_is_on, this);
1244 Route::all_redirects_active (bool state)
1246 Glib::RWLock::ReaderLock lm (redirect_lock);
1248 if (_redirects.empty()) {
1252 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
1253 (*i)->set_active (state, this);
1257 struct RedirectSorter {
1258 bool operator() (boost::shared_ptr<const Redirect> a, boost::shared_ptr<const Redirect> b) {
1259 return a->sort_key() < b->sort_key();
1264 Route::sort_redirects (uint32_t* err_streams)
1267 RedirectSorter comparator;
1268 Glib::RWLock::WriterLock lm (redirect_lock);
1269 ChanCount old_rmo = redirect_max_outs;
1271 /* the sweet power of C++ ... */
1273 RedirectList as_it_was_before = _redirects;
1275 _redirects.sort (comparator);
1277 if (_reset_plugin_counts (err_streams)) {
1278 _redirects = as_it_was_before;
1279 redirect_max_outs = old_rmo;
1285 redirects_changed (this); /* EMIT SIGNAL */
1297 Route::get_template()
1299 return state(false);
1303 Route::state(bool full_state)
1305 XMLNode *node = new XMLNode("Route");
1307 RedirectList:: iterator i;
1311 snprintf (buf, sizeof (buf), "0x%x", _flags);
1312 node->add_property("flags", buf);
1315 node->add_property("default-type", _default_type.to_string());
1317 node->add_property("active", _active?"yes":"no");
1318 node->add_property("muted", _muted?"yes":"no");
1319 node->add_property("soloed", _soloed?"yes":"no");
1320 node->add_property("phase-invert", _phase_invert?"yes":"no");
1321 node->add_property("mute-affects-pre-fader", _mute_affects_pre_fader?"yes":"no");
1322 node->add_property("mute-affects-post-fader", _mute_affects_post_fader?"yes":"no");
1323 node->add_property("mute-affects-control-outs", _mute_affects_control_outs?"yes":"no");
1324 node->add_property("mute-affects-main-outs", _mute_affects_main_outs?"yes":"no");
1327 node->add_property("edit-group", _edit_group->name());
1330 node->add_property("mix-group", _mix_group->name());
1333 string order_string;
1334 OrderKeys::iterator x = order_keys.begin();
1336 while (x != order_keys.end()) {
1337 order_string += (*x).first;
1338 order_string += '=';
1339 snprintf (buf, sizeof(buf), "%ld", (*x).second);
1340 order_string += buf;
1344 if (x == order_keys.end()) {
1348 order_string += ':';
1350 node->add_property ("order-keys", order_string);
1352 node->add_child_nocopy (IO::state (full_state));
1354 if (_control_outs) {
1355 XMLNode* cnode = new XMLNode (X_("ControlOuts"));
1356 cnode->add_child_nocopy (_control_outs->state (full_state));
1357 node->add_child_nocopy (*cnode);
1360 if (_comment.length()) {
1361 XMLNode *cmt = node->add_child ("Comment");
1362 cmt->add_content (_comment);
1368 path = _session.snap_name();
1370 path += legalize_for_path (_name);
1371 path += ".automation";
1373 /* XXX we didn't ask for a state save, we asked for the current state.
1377 if (save_automation (path)) {
1378 error << _("Could not get state of route. Problem with save_automation") << endmsg;
1381 aevents = node->add_child ("Automation");
1382 aevents->add_property ("path", path);
1385 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
1386 node->add_child_nocopy((*i)->state (full_state));
1390 node->add_child_copy (*_extra_xml);
1397 Route::set_deferred_state ()
1400 XMLNodeConstIterator niter;
1402 if (!deferred_state) {
1406 nlist = deferred_state->children();
1408 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
1409 add_redirect_from_xml (**niter);
1412 delete deferred_state;
1417 Route::add_redirect_from_xml (const XMLNode& node)
1419 const XMLProperty *prop;
1421 if (node.name() == "Send") {
1425 boost::shared_ptr<Send> send (new Send (_session, node));
1426 add_redirect (send, this);
1429 catch (failed_constructor &err) {
1430 error << _("Send construction failed") << endmsg;
1434 } else if (node.name() == "Insert") {
1437 if ((prop = node.property ("type")) != 0) {
1439 boost::shared_ptr<Insert> insert;
1441 if (prop->value() == "ladspa" || prop->value() == "Ladspa" || prop->value() == "vst") {
1443 insert.reset (new PluginInsert(_session, node));
1445 } else if (prop->value() == "port") {
1448 insert.reset (new PortInsert (_session, node));
1452 error << string_compose(_("unknown Insert type \"%1\"; ignored"), prop->value()) << endmsg;
1455 add_redirect (insert, this);
1458 error << _("Insert XML node has no type property") << endmsg;
1462 catch (failed_constructor &err) {
1463 warning << _("insert could not be created. Ignored.") << endmsg;
1470 Route::set_state (const XMLNode& node)
1473 XMLNodeConstIterator niter;
1475 XMLPropertyList plist;
1476 const XMLProperty *prop;
1478 if (node.name() != "Route"){
1479 error << string_compose(_("Bad node sent to Route::set_state() [%1]"), node.name()) << endmsg;
1483 if ((prop = node.property ("flags")) != 0) {
1485 sscanf (prop->value().c_str(), "0x%x", &x);
1491 if ((prop = node.property ("default-type")) != 0) {
1492 _default_type = DataType(prop->value());
1493 assert(_default_type != DataType::NIL);
1496 if ((prop = node.property ("phase-invert")) != 0) {
1497 set_phase_invert(prop->value()=="yes"?true:false, this);
1500 if ((prop = node.property ("active")) != 0) {
1501 set_active (prop->value() == "yes");
1504 if ((prop = node.property ("muted")) != 0) {
1505 bool yn = prop->value()=="yes"?true:false;
1507 /* force reset of mute status */
1511 mute_gain = desired_mute_gain;
1514 if ((prop = node.property ("soloed")) != 0) {
1515 bool yn = prop->value()=="yes"?true:false;
1517 /* force reset of solo status */
1520 set_solo (yn, this);
1521 solo_gain = desired_solo_gain;
1524 if ((prop = node.property ("mute-affects-pre-fader")) != 0) {
1525 _mute_affects_pre_fader = (prop->value()=="yes")?true:false;
1528 if ((prop = node.property ("mute-affects-post-fader")) != 0) {
1529 _mute_affects_post_fader = (prop->value()=="yes")?true:false;
1532 if ((prop = node.property ("mute-affects-control-outs")) != 0) {
1533 _mute_affects_control_outs = (prop->value()=="yes")?true:false;
1536 if ((prop = node.property ("mute-affects-main-outs")) != 0) {
1537 _mute_affects_main_outs = (prop->value()=="yes")?true:false;
1540 if ((prop = node.property ("edit-group")) != 0) {
1541 RouteGroup* edit_group = _session.edit_group_by_name(prop->value());
1542 if(edit_group == 0) {
1543 error << string_compose(_("Route %1: unknown edit group \"%2 in saved state (ignored)"), _name, prop->value()) << endmsg;
1545 set_edit_group(edit_group, this);
1549 if ((prop = node.property ("order-keys")) != 0) {
1553 string::size_type colon, equal;
1554 string remaining = prop->value();
1556 while (remaining.length()) {
1558 if ((equal = remaining.find_first_of ('=')) == string::npos || equal == remaining.length()) {
1559 error << string_compose (_("badly formed order key string in state file! [%1] ... ignored."), remaining)
1562 if (sscanf (remaining.substr (equal+1).c_str(), "%ld", &n) != 1) {
1563 error << string_compose (_("badly formed order key string in state file! [%1] ... ignored."), remaining)
1566 set_order_key (remaining.substr (0, equal), n);
1570 colon = remaining.find_first_of (':');
1572 if (colon != string::npos) {
1573 remaining = remaining.substr (colon+1);
1580 nlist = node.children();
1582 if (deferred_state) {
1583 delete deferred_state;
1586 deferred_state = new XMLNode("deferred state");
1588 /* set parent class properties before anything else */
1590 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
1594 if (child->name() == IO::state_node_name) {
1596 IO::set_state (*child);
1601 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
1605 if (child->name() == "Send") {
1608 if (!IO::ports_legal) {
1610 deferred_state->add_child_copy (*child);
1613 add_redirect_from_xml (*child);
1616 } else if (child->name() == "Insert") {
1618 if (!IO::ports_legal) {
1620 deferred_state->add_child_copy (*child);
1624 add_redirect_from_xml (*child);
1627 } else if (child->name() == "Automation") {
1629 XMLPropertyList plist;
1630 XMLPropertyConstIterator piter;
1633 plist = child->properties();
1634 for (piter = plist.begin(); piter != plist.end(); ++piter) {
1636 if (prop->name() == "path") {
1637 load_automation (prop->value());
1641 } else if (child->name() == "ControlOuts") {
1643 string coutname = _name;
1644 coutname += _("[control]");
1646 _control_outs = new IO (_session, coutname);
1647 _control_outs->set_state (**(child->children().begin()));
1649 } else if (child->name() == "Comment") {
1651 /* XXX this is a terrible API design in libxml++ */
1653 XMLNode *cmt = *(child->children().begin());
1654 _comment = cmt->content();
1656 } else if (child->name() == "extra") {
1657 _extra_xml = new XMLNode (*child);
1661 if ((prop = node.property ("mix-group")) != 0) {
1662 RouteGroup* mix_group = _session.mix_group_by_name(prop->value());
1663 if (mix_group == 0) {
1664 error << string_compose(_("Route %1: unknown mix group \"%2 in saved state (ignored)"), _name, prop->value()) << endmsg;
1666 set_mix_group(mix_group, this);
1674 Route::curve_reallocate ()
1676 // _gain_automation_curve.finish_resize ();
1677 // _pan_automation_curve.finish_resize ();
1681 Route::silence (jack_nframes_t nframes, jack_nframes_t offset)
1685 IO::silence (nframes, offset);
1687 if (_control_outs) {
1688 _control_outs->silence (nframes, offset);
1692 Glib::RWLock::ReaderLock lm (redirect_lock, Glib::TRY_LOCK);
1695 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
1696 boost::shared_ptr<PluginInsert> pi;
1697 if (!_active && (pi = boost::dynamic_pointer_cast<PluginInsert> (*i)) != 0) {
1698 // skip plugins, they don't need anything when we're not active
1702 (*i)->silence (nframes, offset);
1705 if (nframes == _session.get_block_size() && offset == 0) {
1715 Route::set_control_outs (const vector<string>& ports)
1717 Glib::Mutex::Lock lm (control_outs_lock);
1718 vector<string>::const_iterator i;
1720 if (_control_outs) {
1721 delete _control_outs;
1725 if (ports.empty()) {
1729 string coutname = _name;
1730 coutname += _("[control]");
1732 _control_outs = new IO (_session, coutname);
1734 /* our control outs need as many outputs as we
1735 have audio outputs. we track the changes in ::output_change_handler().
1738 _control_outs->ensure_io (ChanCount::ZERO, ChanCount(DataType::AUDIO, n_outputs().get(DataType::AUDIO)), true, this);
1744 Route::set_edit_group (RouteGroup *eg, void *src)
1747 if (eg == _edit_group) {
1752 _edit_group->remove (this);
1755 if ((_edit_group = eg) != 0) {
1756 _edit_group->add (this);
1759 _session.set_dirty ();
1760 edit_group_changed (src); /* EMIT SIGNAL */
1764 Route::drop_edit_group (void *src)
1767 _session.set_dirty ();
1768 edit_group_changed (src); /* EMIT SIGNAL */
1772 Route::set_mix_group (RouteGroup *mg, void *src)
1775 if (mg == _mix_group) {
1780 _mix_group->remove (this);
1783 if ((_mix_group = mg) != 0) {
1784 _mix_group->add (this);
1787 _session.set_dirty ();
1788 mix_group_changed (src); /* EMIT SIGNAL */
1792 Route::drop_mix_group (void *src)
1795 _session.set_dirty ();
1796 mix_group_changed (src); /* EMIT SIGNAL */
1800 Route::set_comment (string cmt, void *src)
1803 comment_changed (src);
1804 _session.set_dirty ();
1808 Route::feeds (boost::shared_ptr<Route> other)
1813 uint32_t no = self.n_outputs().get_total();
1814 uint32_t ni = other->n_inputs ().get_total();
1816 for (i = 0; i < no; ++i) {
1817 for (j = 0; j < ni; ++j) {
1818 if (self.output(i)->connected_to (other->input(j)->name())) {
1824 /* check Redirects which may also interconnect Routes */
1826 for (RedirectList::iterator r = _redirects.begin(); r != _redirects.end(); r++) {
1828 no = (*r)->n_outputs().get_total();
1830 for (i = 0; i < no; ++i) {
1831 for (j = 0; j < ni; ++j) {
1832 if ((*r)->output(i)->connected_to (other->input (j)->name())) {
1839 /* check for control room outputs which may also interconnect Routes */
1841 if (_control_outs) {
1843 no = _control_outs->n_outputs().get_total();
1845 for (i = 0; i < no; ++i) {
1846 for (j = 0; j < ni; ++j) {
1847 if (_control_outs->output(i)->connected_to (other->input (j)->name())) {
1858 Route::set_mute_config (mute_type t, bool onoff, void *src)
1862 _mute_affects_pre_fader = onoff;
1863 pre_fader_changed(src); /* EMIT SIGNAL */
1867 _mute_affects_post_fader = onoff;
1868 post_fader_changed(src); /* EMIT SIGNAL */
1872 _mute_affects_control_outs = onoff;
1873 control_outs_changed(src); /* EMIT SIGNAL */
1877 _mute_affects_main_outs = onoff;
1878 main_outs_changed(src); /* EMIT SIGNAL */
1884 Route::get_mute_config (mute_type t)
1890 onoff = _mute_affects_pre_fader;
1893 onoff = _mute_affects_post_fader;
1896 onoff = _mute_affects_control_outs;
1899 onoff = _mute_affects_main_outs;
1907 Route::set_active (bool yn)
1910 active_changed(); /* EMIT SIGNAL */
1914 Route::handle_transport_stopped (bool abort_ignored, bool did_locate, bool can_flush_redirects)
1916 jack_nframes_t now = _session.transport_frame();
1919 Glib::RWLock::ReaderLock lm (redirect_lock);
1922 automation_snapshot (now);
1925 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
1927 if (Config->get_plugins_stop_with_transport() && can_flush_redirects) {
1928 (*i)->deactivate ();
1932 (*i)->transport_stopped (now);
1936 IO::transport_stopped (now);
1938 _roll_delay = _initial_delay;
1942 Route::get_memento() const
1944 void (Route::*pmf)(state_id_t) = &Route::set_state;
1945 return sigc::bind (mem_fun (*(const_cast<Route *>(this)), pmf), _current_state_id);
1949 Route::set_state (state_id_t id)
1955 Route::input_change_handler (IOChange change, void *ignored)
1957 if (change & ConfigurationChanged) {
1958 reset_plugin_counts (0);
1963 Route::output_change_handler (IOChange change, void *ignored)
1965 if (change & ConfigurationChanged) {
1966 if (_control_outs) {
1967 _control_outs->ensure_io (ChanCount::ZERO, ChanCount(DataType::AUDIO, n_outputs().get(DataType::AUDIO)), true, this);
1970 reset_plugin_counts (0);
1975 Route::pans_required () const
1977 if (n_outputs().get(DataType::AUDIO) < 2) {
1981 return max (n_inputs ().get(DataType::AUDIO), static_cast<size_t>(redirect_max_outs.get(DataType::AUDIO)));
1985 Route::no_roll (jack_nframes_t nframes, jack_nframes_t start_frame, jack_nframes_t end_frame, jack_nframes_t offset,
1986 bool session_state_changing, bool can_record, bool rec_monitors_input)
1988 if (n_outputs().get_total() == 0) {
1992 if (session_state_changing || !_active) {
1993 silence (nframes, offset);
1997 apply_gain_automation = false;
1999 if (n_inputs().get_total()) {
2000 passthru (start_frame, end_frame, nframes, offset, 0, false);
2002 silence (nframes, offset);
2009 Route::check_initial_delay (jack_nframes_t nframes, jack_nframes_t& offset, jack_nframes_t& transport_frame)
2011 if (_roll_delay > nframes) {
2013 _roll_delay -= nframes;
2014 silence (nframes, offset);
2015 /* transport frame is not legal for caller to use */
2018 } else if (_roll_delay > 0) {
2020 nframes -= _roll_delay;
2022 silence (_roll_delay, offset);
2024 offset += _roll_delay;
2025 transport_frame += _roll_delay;
2034 Route::roll (jack_nframes_t nframes, jack_nframes_t start_frame, jack_nframes_t end_frame, jack_nframes_t offset, int declick,
2035 bool can_record, bool rec_monitors_input)
2038 Glib::RWLock::ReaderLock lm (redirect_lock, Glib::TRY_LOCK);
2040 // automation snapshot can also be called from the non-rt context
2041 // and it uses the redirect list, so we take the lock out here
2042 automation_snapshot (_session.transport_frame());
2046 if ((n_outputs().get_total() == 0 && _redirects.empty()) || n_inputs().get_total() == 0 || !_active) {
2047 silence (nframes, offset);
2051 jack_nframes_t unused = 0;
2053 if ((nframes = check_initial_delay (nframes, offset, unused)) == 0) {
2059 apply_gain_automation = false;
2062 Glib::Mutex::Lock am (automation_lock, Glib::TRY_LOCK);
2064 if (am.locked() && _session.transport_rolling()) {
2066 if (gain_automation_playback()) {
2067 apply_gain_automation = _gain_automation_curve.rt_safe_get_vector (start_frame, end_frame, _session.gain_automation_buffer(), nframes);
2072 passthru (start_frame, end_frame, nframes, offset, declick, false);
2078 Route::silent_roll (jack_nframes_t nframes, jack_nframes_t start_frame, jack_nframes_t end_frame, jack_nframes_t offset,
2079 bool can_record, bool rec_monitors_input)
2081 silence (nframes, offset);
2086 Route::toggle_monitor_input ()
2088 for (PortSet::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
2089 i->ensure_monitor_input( ! i->monitoring_input());
2094 Route::has_external_redirects () const
2096 boost::shared_ptr<const PortInsert> pi;
2098 for (RedirectList::const_iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2099 if ((pi = boost::dynamic_pointer_cast<const PortInsert>(*i)) != 0) {
2101 for (PortSet::const_iterator port = pi->outputs().begin();
2102 port != pi->outputs().end(); ++port) {
2104 string port_name = port->name();
2105 string client_name = port_name.substr (0, port_name.find(':'));
2107 /* only say "yes" if the redirect is actually in use */
2109 if (client_name != "ardour" && pi->active()) {
2120 Route::flush_redirects ()
2122 /* XXX shouldn't really try to take this lock, since
2123 this is called from the RT audio thread.
2126 Glib::RWLock::ReaderLock lm (redirect_lock);
2128 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2129 (*i)->deactivate ();
2135 Route::set_meter_point (MeterPoint p, void *src)
2137 if (_meter_point != p) {
2139 meter_change (src); /* EMIT SIGNAL */
2140 _session.set_dirty ();
2145 Route::update_total_latency ()
2149 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2150 if ((*i)->active ()) {
2151 _own_latency += (*i)->latency ();
2155 set_port_latency (_own_latency);
2157 /* this (virtual) function is used for pure Routes,
2158 not derived classes like AudioTrack. this means
2159 that the data processed here comes from an input
2160 port, not prerecorded material, and therefore we
2161 have to take into account any input latency.
2164 _own_latency += input_latency ();
2166 return _own_latency;
2170 Route::set_latency_delay (jack_nframes_t longest_session_latency)
2172 _initial_delay = longest_session_latency - _own_latency;
2174 if (_session.transport_stopped()) {
2175 _roll_delay = _initial_delay;
2180 Route::automation_snapshot (jack_nframes_t now)
2182 IO::automation_snapshot (now);
2184 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2185 (*i)->automation_snapshot (now);
2189 Route::ToggleControllable::ToggleControllable (Route& s, ToggleType tp)
2190 : route (s), type(tp)
2196 Route::ToggleControllable::set_value (float val)
2198 bool bval = ((val >= 0.5f) ? true: false);
2202 route.set_mute (bval, this);
2205 route.set_solo (bval, this);
2213 Route::ToggleControllable::get_value (void) const
2219 val = route.muted() ? 1.0f : 0.0f;
2222 val = route.soloed() ? 1.0f : 0.0f;
2232 Route::set_block_size (jack_nframes_t nframes)
2234 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2235 (*i)->set_block_size (nframes);
2240 Route::redirect_active_proxy (Redirect* ignored, void* ignored_src)
2242 _session.update_latency_compensation (false, false);
2246 Route::protect_automation ()
2248 switch (gain_automation_state()) {
2251 set_gain_automation_state (Off);
2257 switch (panner().automation_state ()) {
2260 panner().set_automation_state (Off);
2266 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2267 boost::shared_ptr<PluginInsert> pi;
2268 if ((pi = boost::dynamic_pointer_cast<PluginInsert> (*i)) != 0) {
2269 pi->protect_automation ();
2275 Route::set_pending_declick (int declick)
2278 /* this call is not allowed to turn off a pending declick unless "force" is true */
2280 _pending_declick = declick;
2282 // cerr << _name << ": after setting to " << declick << " pending declick = " << _pending_declick << endl;
2284 _pending_declick = 0;