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/buffer.h>
30 #include <ardour/audioengine.h>
31 #include <ardour/route.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/ladspa_plugin.h>
41 #include <ardour/panner.h>
42 #include <ardour/dB.h>
43 #include <ardour/mix.h>
48 using namespace ARDOUR;
52 uint32_t Route::order_key_cnt = 0;
55 Route::Route (Session& sess, string name, int input_min, int input_max, int output_min, int output_max, Flag flg, DataType default_type)
56 : IO (sess, name, input_min, input_max, output_min, output_max, default_type),
58 _solo_control (X_("solo"), *this, ToggleControllable::SoloControl),
59 _mute_control (X_("mute"), *this, ToggleControllable::MuteControl)
64 Route::Route (Session& sess, const XMLNode& node)
66 _solo_control (X_("solo"), *this, ToggleControllable::SoloControl),
67 _mute_control (X_("mute"), *this, ToggleControllable::MuteControl)
76 redirect_max_outs = 0;
80 _phase_invert = false;
81 order_keys[N_("signal")] = order_key_cnt++;
84 _meter_point = MeterPostFader;
88 _have_internal_generator = false;
90 _pending_declick = true;
91 _remote_control_id = 0;
92 _ignore_gain_on_deliver = true;
97 _mute_affects_pre_fader = Config->get_mute_affects_pre_fader();
98 _mute_affects_post_fader = Config->get_mute_affects_post_fader();
99 _mute_affects_control_outs = Config->get_mute_affects_control_outs();
100 _mute_affects_main_outs = Config->get_mute_affects_main_outs();
103 desired_solo_gain = 1.0;
105 desired_mute_gain = 1.0;
109 input_changed.connect (mem_fun (this, &Route::input_change_handler));
110 output_changed.connect (mem_fun (this, &Route::output_change_handler));
115 clear_redirects (this);
118 delete _control_outs;
123 Route::set_remote_control_id (uint32_t id)
125 if (id != _remote_control_id) {
126 _remote_control_id = id;
127 RemoteControlIDChanged ();
132 Route::remote_control_id() const
134 return _remote_control_id;
138 Route::order_key (string name) const
140 OrderKeys::const_iterator i;
142 if ((i = order_keys.find (name)) == order_keys.end()) {
150 Route::set_order_key (string name, long n)
152 order_keys[name] = n;
153 _session.set_dirty ();
157 Route::inc_gain (gain_t fraction, void *src)
159 IO::inc_gain (fraction, src);
163 Route::set_gain (gain_t val, void *src)
165 if (src != 0 && _mix_group && src != _mix_group && _mix_group->is_active()) {
167 if (_mix_group->is_relative()) {
170 gain_t usable_gain = gain();
171 if (usable_gain < 0.000001f) {
172 usable_gain=0.000001f;
176 if (delta < 0.000001f) {
180 delta -= usable_gain;
182 if (delta == 0.0f) return;
184 gain_t factor = delta / usable_gain;
187 factor = _mix_group->get_max_factor(factor);
188 if (factor == 0.0f) {
193 factor = _mix_group->get_min_factor(factor);
194 if (factor == 0.0f) {
200 _mix_group->apply (&Route::inc_gain, factor, _mix_group);
204 _mix_group->apply (&Route::set_gain, val, _mix_group);
214 IO::set_gain (val, src);
218 Route::process_output_buffers (vector<Sample*>& bufs, uint32_t nbufs,
219 nframes_t start_frame, nframes_t end_frame,
220 nframes_t nframes, nframes_t offset, bool with_redirects, int declick,
224 RedirectList::iterator i;
225 bool post_fader_work = false;
226 bool mute_declick_applied = false;
228 vector<Sample*>::iterator bufiter;
233 gain_t* gab = _session.gain_automation_buffer();
235 switch (Config->get_monitoring_model()) {
236 case HardwareMonitoring:
237 case ExternalMonitoring:
244 declick = _pending_declick;
247 Glib::Mutex::Lock cm (control_outs_lock, Glib::TRY_LOCK);
257 Glib::Mutex::Lock dm (declick_lock, Glib::TRY_LOCK);
260 dmg = desired_mute_gain;
261 dsg = desired_solo_gain;
270 /* ----------------------------------------------------------------------------------------------------
271 GLOBAL DECLICK (for transport changes etc.)
272 -------------------------------------------------------------------------------------------------- */
275 apply_declick (bufs, nbufs, nframes, 0.0, 1.0, false);
276 _pending_declick = 0;
277 } else if (declick < 0) {
278 apply_declick (bufs, nbufs, nframes, 1.0, 0.0, false);
279 _pending_declick = 0;
282 /* no global declick */
284 if (solo_gain != dsg) {
285 apply_declick (bufs, nbufs, nframes, solo_gain, dsg, false);
291 /* ----------------------------------------------------------------------------------------------------
292 INPUT METERING & MONITORING
293 -------------------------------------------------------------------------------------------------- */
295 if (meter && (_meter_point == MeterInput)) {
296 for (n = 0; n < nbufs; ++n) {
297 _peak_power[n] = Session::compute_peak (bufs[n], nframes, _peak_power[n]);
301 if (!_soloed && _mute_affects_pre_fader && (mute_gain != dmg)) {
302 apply_declick (bufs, nbufs, nframes, mute_gain, dmg, false);
304 mute_declick_applied = true;
307 if ((_meter_point == MeterInput) && co) {
309 solo_audible = dsg > 0;
310 mute_audible = dmg > 0;// || !_mute_affects_pre_fader;
312 if ( // muted by solo of another track
316 // muted by mute of this track
320 // rec-enabled but not s/w monitoring
322 // TODO: this is probably wrong
324 (no_monitor && record_enabled() && (!Config->get_auto_input() || _session.actively_recording()))
328 co->silence (nframes, offset);
332 co->deliver_output (bufs, nbufs, nframes, offset);
337 /* ----------------------------------------------------------------------------------------------------
339 -------------------------------------------------------------------------------------------------- */
341 if (with_redirects) {
342 Glib::RWLock::ReaderLock rm (redirect_lock, Glib::TRY_LOCK);
344 if (mute_gain > 0 || !_mute_affects_pre_fader) {
345 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
346 switch ((*i)->placement()) {
348 (*i)->run (bufs, nbufs, nframes, offset);
351 post_fader_work = true;
356 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
357 switch ((*i)->placement()) {
359 (*i)->silence (nframes, offset);
362 post_fader_work = true;
371 if (!_soloed && (mute_gain != dmg) && !mute_declick_applied && _mute_affects_post_fader) {
372 apply_declick (bufs, nbufs, nframes, mute_gain, dmg, false);
374 mute_declick_applied = true;
377 /* ----------------------------------------------------------------------------------------------------
378 PRE-FADER METERING & MONITORING
379 -------------------------------------------------------------------------------------------------- */
381 if (meter && (_meter_point == MeterPreFader)) {
382 for (n = 0; n < nbufs; ++n) {
383 _peak_power[n] = Session::compute_peak (bufs[n], nframes, _peak_power[n]);
388 if ((_meter_point == MeterPreFader) && co) {
390 solo_audible = dsg > 0;
391 mute_audible = dmg > 0 || !_mute_affects_pre_fader;
393 if ( // muted by solo of another track
397 // muted by mute of this track
401 // rec-enabled but not s/w monitoring
403 (no_monitor && record_enabled() && (!Config->get_auto_input() || _session.actively_recording()))
407 co->silence (nframes, offset);
411 co->deliver_output (bufs, nbufs, nframes, offset);
416 /* ----------------------------------------------------------------------------------------------------
418 -------------------------------------------------------------------------------------------------- */
420 /* if not recording or recording and requiring any monitor signal, then apply gain */
422 if ( // not recording
424 !(record_enabled() && _session.actively_recording()) ||
428 // h/w monitoring not in use
430 (!Config->get_monitoring_model() == HardwareMonitoring &&
432 // AND software monitoring required
434 Config->get_monitoring_model() == SoftwareMonitoring)) {
436 if (apply_gain_automation) {
439 for (n = 0; n < nbufs; ++n) {
440 Sample *sp = bufs[n];
442 for (nframes_t nx = 0; nx < nframes; ++nx) {
447 for (n = 0; n < nbufs; ++n) {
448 Sample *sp = bufs[n];
450 for (nframes_t nx = 0; nx < nframes; ++nx) {
456 if (apply_gain_automation && _session.transport_rolling() && nframes > 0) {
457 _effective_gain = gab[nframes-1];
462 /* manual (scalar) gain */
466 apply_declick (bufs, nbufs, nframes, _gain, dg, _phase_invert);
469 } else if (_gain != 0 && (_phase_invert || _gain != 1.0)) {
471 /* no need to interpolate current gain value,
472 but its non-unity, so apply it. if the gain
473 is zero, do nothing because we'll ship silence
485 for (n = 0; n < nbufs; ++n) {
486 Sample *sp = bufs[n];
487 Session::apply_gain_to_buffer(sp,nframes,this_gain);
490 } else if (_gain == 0) {
491 for (n = 0; n < nbufs; ++n) {
492 memset (bufs[n], 0, sizeof (Sample) * nframes);
499 /* actively recording, no monitoring required; leave buffers as-is to save CPU cycles */
503 /* ----------------------------------------------------------------------------------------------------
505 -------------------------------------------------------------------------------------------------- */
507 /* note that post_fader_work cannot be true unless with_redirects was also true, so don't test both */
509 if (post_fader_work) {
511 Glib::RWLock::ReaderLock rm (redirect_lock, Glib::TRY_LOCK);
513 if (mute_gain > 0 || !_mute_affects_post_fader) {
514 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
515 switch ((*i)->placement()) {
519 (*i)->run (bufs, nbufs, nframes, offset);
524 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
525 switch ((*i)->placement()) {
529 (*i)->silence (nframes, offset);
537 if (!_soloed && (mute_gain != dmg) && !mute_declick_applied && _mute_affects_control_outs) {
538 apply_declick (bufs, nbufs, nframes, mute_gain, dmg, false);
540 mute_declick_applied = true;
543 /* ----------------------------------------------------------------------------------------------------
545 -------------------------------------------------------------------------------------------------- */
547 if ((_meter_point == MeterPostFader) && co) {
549 solo_audible = solo_gain > 0;
550 mute_audible = dmg > 0 || !_mute_affects_control_outs;
552 if ( // silent anyway
554 (_gain == 0 && !apply_gain_automation) ||
556 // muted by solo of another track
560 // muted by mute of this track
564 // recording but not s/w monitoring
566 (no_monitor && record_enabled() && (!Config->get_auto_input() || _session.actively_recording()))
570 co->silence (nframes, offset);
574 co->deliver_output_no_pan (bufs, nbufs, nframes, offset);
578 /* ----------------------------------------------------------------------
580 ----------------------------------------------------------------------*/
582 if (!_soloed && (mute_gain != dmg) && !mute_declick_applied && _mute_affects_main_outs) {
583 apply_declick (bufs, nbufs, nframes, mute_gain, dmg, false);
585 mute_declick_applied = true;
588 /* ----------------------------------------------------------------------------------------------------
590 -------------------------------------------------------------------------------------------------- */
592 solo_audible = dsg > 0;
593 mute_audible = dmg > 0 || !_mute_affects_main_outs;
595 if (n_outputs() == 0) {
599 } else if (no_monitor && record_enabled() && (!Config->get_auto_input() || _session.actively_recording())) {
601 IO::silence (nframes, offset);
605 if ( // silent anyway
607 (_gain == 0 && !apply_gain_automation) ||
609 // muted by solo of another track, but not using control outs for solo
611 (!solo_audible && (Config->get_solo_model() != SoloBus)) ||
613 // muted by mute of this track
619 /* don't use Route::silence() here, because that causes
620 all outputs (sends, port inserts, etc. to be silent).
623 if (_meter_point == MeterPostFader) {
624 reset_peak_meters ();
627 IO::silence (nframes, offset);
631 if ((_session.transport_speed() > 1.5f ||
632 _session.transport_speed() < -1.5f) &&
633 Config->get_quieten_at_speed()) {
634 pan (bufs, nbufs, nframes, offset, speed_quietning);
636 // cerr << _name << " panner state = " << _panner->automation_state() << endl;
637 if (!_panner->empty() &&
638 (_panner->automation_state() & Play ||
639 ((_panner->automation_state() & Touch) && !_panner->touching()))) {
640 pan_automated (bufs, nbufs, start_frame, end_frame, nframes, offset);
642 pan (bufs, nbufs, nframes, offset, 1.0);
649 /* ----------------------------------------------------------------------------------------------------
651 -------------------------------------------------------------------------------------------------- */
653 if (meter && (_meter_point == MeterPostFader)) {
654 // cerr << "meter post" << endl;
656 if ((_gain == 0 && !apply_gain_automation) || dmg == 0) {
657 uint32_t no = n_outputs();
658 for (n = 0; n < no; ++n) {
662 uint32_t no = n_outputs();
663 for (n = 0; n < no; ++n) {
664 _peak_power[n] = Session::compute_peak (output(n)->get_buffer (nframes) + offset, nframes, _peak_power[n]);
671 Route::n_process_buffers ()
673 return max (n_inputs(), redirect_max_outs);
678 Route::passthru (nframes_t start_frame, nframes_t end_frame, nframes_t nframes, nframes_t offset, int declick, bool meter_first)
680 vector<Sample*>& bufs = _session.get_passthru_buffers();
681 uint32_t limit = n_process_buffers ();
685 collect_input (bufs, limit, nframes, offset);
687 #define meter_stream meter_first
690 for (uint32_t n = 0; n < limit; ++n) {
691 _peak_power[n] = Session::compute_peak (bufs[n], nframes, _peak_power[n]);
693 meter_stream = false;
698 process_output_buffers (bufs, limit, start_frame, end_frame, nframes, offset, true, declick, meter_stream);
704 Route::set_phase_invert (bool yn, void *src)
706 if (_phase_invert != yn) {
709 // phase_invert_changed (src); /* EMIT SIGNAL */
713 Route::set_solo (bool yn, void *src)
719 if (_mix_group && src != _mix_group && _mix_group->is_active()) {
720 _mix_group->apply (&Route::set_solo, yn, _mix_group);
726 solo_changed (src); /* EMIT SIGNAL */
727 _solo_control.Changed (); /* EMIT SIGNAL */
732 Route::set_solo_mute (bool yn)
734 Glib::Mutex::Lock lm (declick_lock);
736 /* Called by Session in response to another Route being soloed.
739 desired_solo_gain = (yn?0.0:1.0);
743 Route::set_solo_safe (bool yn, void *src)
745 if (_solo_safe != yn) {
747 solo_safe_changed (src); /* EMIT SIGNAL */
752 Route::set_mute (bool yn, void *src)
755 if (_mix_group && src != _mix_group && _mix_group->is_active()) {
756 _mix_group->apply (&Route::set_mute, yn, _mix_group);
762 mute_changed (src); /* EMIT SIGNAL */
764 _mute_control.Changed (); /* EMIT SIGNAL */
766 Glib::Mutex::Lock lm (declick_lock);
767 desired_mute_gain = (yn?0.0f:1.0f);
772 Route::add_redirect (boost::shared_ptr<Redirect> redirect, void *src, uint32_t* err_streams)
774 uint32_t old_rmo = redirect_max_outs;
776 if (!_session.engine().connected()) {
781 Glib::RWLock::WriterLock lm (redirect_lock);
783 boost::shared_ptr<PluginInsert> pi;
784 boost::shared_ptr<PortInsert> porti;
786 uint32_t potential_max_streams = 0;
788 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(redirect)) != 0) {
791 if (pi->input_streams() == 0) {
792 /* instrument plugin */
793 _have_internal_generator = true;
796 potential_max_streams = max(pi->input_streams(), pi->output_streams());
798 } else if ((porti = boost::dynamic_pointer_cast<PortInsert>(redirect)) != 0) {
800 /* force new port inserts to start out with an i/o configuration
801 that matches this route's i/o configuration.
803 the "inputs" for the port are supposed to match the output
806 the "outputs" of the route should match the inputs of this
807 route. XXX shouldn't they match the number of active signal
808 streams at the point of insertion?
812 porti->ensure_io (n_outputs (), n_inputs(), false, this);
815 // Ensure peak vector sizes before the plugin is activated
816 while (_peak_power.size() < potential_max_streams) {
817 _peak_power.push_back(0);
819 while (_visible_peak_power.size() < potential_max_streams) {
820 _visible_peak_power.push_back(0);
823 _redirects.push_back (redirect);
825 if (_reset_plugin_counts (err_streams)) {
826 _redirects.pop_back ();
827 _reset_plugin_counts (0); // it worked before we tried to add it ...
831 redirect->activate ();
832 redirect->active_changed.connect (mem_fun (*this, &Route::redirect_active_proxy));
835 if (redirect_max_outs != old_rmo || old_rmo == 0) {
840 redirects_changed (src); /* EMIT SIGNAL */
845 Route::add_redirects (const RedirectList& others, void *src, uint32_t* err_streams)
847 uint32_t old_rmo = redirect_max_outs;
849 if (!_session.engine().connected()) {
854 Glib::RWLock::WriterLock lm (redirect_lock);
856 RedirectList::iterator existing_end = _redirects.end();
859 uint32_t potential_max_streams = 0;
861 for (RedirectList::const_iterator i = others.begin(); i != others.end(); ++i) {
863 boost::shared_ptr<PluginInsert> pi;
865 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(*i)) != 0) {
868 uint32_t m = max(pi->input_streams(), pi->output_streams());
869 if (m > potential_max_streams)
870 potential_max_streams = m;
873 // Ensure peak vector sizes before the plugin is activated
874 while (_peak_power.size() < potential_max_streams) {
875 _peak_power.push_back(0);
877 while (_visible_peak_power.size() < potential_max_streams) {
878 _visible_peak_power.push_back(0);
881 _redirects.push_back (*i);
883 if (_reset_plugin_counts (err_streams)) {
885 _redirects.erase (existing_end, _redirects.end());
886 _reset_plugin_counts (0); // it worked before we tried to add it ...
891 (*i)->active_changed.connect (mem_fun (*this, &Route::redirect_active_proxy));
895 if (redirect_max_outs != old_rmo || old_rmo == 0) {
899 redirects_changed (src); /* EMIT SIGNAL */
904 Route::clear_redirects (void *src)
906 uint32_t old_rmo = redirect_max_outs;
908 if (!_session.engine().connected()) {
913 Glib::RWLock::WriterLock lm (redirect_lock);
917 if (redirect_max_outs != old_rmo) {
921 redirect_max_outs = 0;
922 _have_internal_generator = false;
923 redirects_changed (src); /* EMIT SIGNAL */
927 Route::remove_redirect (boost::shared_ptr<Redirect> redirect, void *src, uint32_t* err_streams)
929 uint32_t old_rmo = redirect_max_outs;
931 if (!_session.engine().connected()) {
935 redirect_max_outs = 0;
938 Glib::RWLock::WriterLock lm (redirect_lock);
939 RedirectList::iterator i;
940 bool removed = false;
942 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
943 if (*i == redirect) {
945 RedirectList::iterator tmp;
947 /* move along, see failure case for reset_plugin_counts()
948 where we may need to reinsert the redirect.
954 /* stop redirects that send signals to JACK ports
955 from causing noise as a result of no longer being
959 boost::shared_ptr<Send> send;
960 boost::shared_ptr<PortInsert> port_insert;
962 if ((send = boost::dynamic_pointer_cast<Send> (*i)) != 0) {
963 send->disconnect_inputs (this);
964 send->disconnect_outputs (this);
965 } else if ((port_insert = boost::dynamic_pointer_cast<PortInsert> (*i)) != 0) {
966 port_insert->disconnect_inputs (this);
967 port_insert->disconnect_outputs (this);
970 _redirects.erase (i);
983 if (_reset_plugin_counts (err_streams)) {
984 /* get back to where we where */
985 _redirects.insert (i, redirect);
986 /* we know this will work, because it worked before :) */
987 _reset_plugin_counts (0);
993 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
994 boost::shared_ptr<PluginInsert> pi;
996 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(*i)) != 0) {
997 if (pi->is_generator()) {
1003 _have_internal_generator = foo;
1006 if (old_rmo != redirect_max_outs) {
1010 redirects_changed (src); /* EMIT SIGNAL */
1015 Route::reset_plugin_counts (uint32_t* lpc)
1017 Glib::RWLock::WriterLock lm (redirect_lock);
1018 return _reset_plugin_counts (lpc);
1023 Route::_reset_plugin_counts (uint32_t* err_streams)
1025 RedirectList::iterator r;
1028 map<Placement,list<InsertCount> > insert_map;
1029 nframes_t initial_streams;
1031 redirect_max_outs = 0;
1035 /* divide inserts up by placement so we get the signal flow
1036 properly modelled. we need to do this because the _redirects
1037 list is not sorted by placement, and because other reasons may
1038 exist now or in the future for this separate treatment.
1041 for (r = _redirects.begin(); r != _redirects.end(); ++r) {
1043 boost::shared_ptr<Insert> insert;
1045 /* do this here in case we bomb out before we get to the end of
1049 redirect_max_outs = max ((*r)->output_streams (), redirect_max_outs);
1051 if ((insert = boost::dynamic_pointer_cast<Insert>(*r)) != 0) {
1053 insert_map[insert->placement()].push_back (InsertCount (insert));
1055 /* reset plugin counts back to one for now so
1056 that we have a predictable, controlled
1057 state to try to configure.
1060 boost::shared_ptr<PluginInsert> pi;
1062 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(insert)) != 0) {
1066 } else if (boost::dynamic_pointer_cast<Send> (*r) != 0) {
1079 /* Now process each placement in order, checking to see if we
1080 can really do what has been requested.
1085 if (check_some_plugin_counts (insert_map[PreFader], n_inputs (), err_streams)) {
1089 /* figure out the streams that will feed into PreFader */
1091 if (!insert_map[PreFader].empty()) {
1092 InsertCount& ic (insert_map[PreFader].back());
1093 initial_streams = ic.insert->compute_output_streams (ic.cnt);
1095 initial_streams = n_inputs ();
1100 if (check_some_plugin_counts (insert_map[PostFader], initial_streams, err_streams)) {
1104 /* OK, everything can be set up correctly, so lets do it */
1106 apply_some_plugin_counts (insert_map[PreFader]);
1107 apply_some_plugin_counts (insert_map[PostFader]);
1109 /* recompute max outs of any redirect */
1113 redirect_max_outs = 0;
1114 RedirectList::iterator prev = _redirects.end();
1116 for (r = _redirects.begin(); r != _redirects.end(); prev = r, ++r) {
1117 boost::shared_ptr<Send> s;
1119 if ((s = boost::dynamic_pointer_cast<Send> (*r)) != 0) {
1120 if (r == _redirects.begin()) {
1121 s->expect_inputs (n_inputs());
1123 s->expect_inputs ((*prev)->output_streams());
1127 redirect_max_outs = max ((*r)->output_streams (), redirect_max_outs);
1136 Route::apply_some_plugin_counts (list<InsertCount>& iclist)
1138 list<InsertCount>::iterator i;
1140 for (i = iclist.begin(); i != iclist.end(); ++i) {
1142 if ((*i).insert->configure_io ((*i).cnt, (*i).in, (*i).out)) {
1145 /* make sure that however many we have, they are all active */
1146 (*i).insert->activate ();
1153 Route::check_some_plugin_counts (list<InsertCount>& iclist, int32_t required_inputs, uint32_t* err_streams)
1155 list<InsertCount>::iterator i;
1157 for (i = iclist.begin(); i != iclist.end(); ++i) {
1159 if (((*i).cnt = (*i).insert->can_support_input_configuration (required_inputs)) < 0) {
1161 *err_streams = required_inputs;
1166 (*i).in = required_inputs;
1167 (*i).out = (*i).insert->compute_output_streams ((*i).cnt);
1169 required_inputs = (*i).out;
1176 Route::copy_redirects (const Route& other, Placement placement, uint32_t* err_streams)
1178 uint32_t old_rmo = redirect_max_outs;
1184 RedirectList to_be_deleted;
1187 Glib::RWLock::WriterLock lm (redirect_lock);
1188 RedirectList::iterator tmp;
1189 RedirectList the_copy;
1191 the_copy = _redirects;
1193 /* remove all relevant redirects */
1195 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ) {
1199 if ((*i)->placement() == placement) {
1200 to_be_deleted.push_back (*i);
1201 _redirects.erase (i);
1207 /* now copy the relevant ones from "other" */
1209 for (RedirectList::const_iterator i = other._redirects.begin(); i != other._redirects.end(); ++i) {
1210 if ((*i)->placement() == placement) {
1211 _redirects.push_back (Redirect::clone (*i));
1215 /* reset plugin stream handling */
1217 if (_reset_plugin_counts (err_streams)) {
1219 /* FAILED COPY ATTEMPT: we have to restore order */
1221 /* delete all cloned redirects */
1223 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ) {
1228 if ((*i)->placement() == placement) {
1229 _redirects.erase (i);
1235 /* restore the natural order */
1237 _redirects = the_copy;
1238 redirect_max_outs = old_rmo;
1240 /* we failed, even though things are OK again */
1246 /* SUCCESSFUL COPY ATTEMPT: delete the redirects we removed pre-copy */
1247 to_be_deleted.clear ();
1251 if (redirect_max_outs != old_rmo || old_rmo == 0) {
1255 redirects_changed (this); /* EMIT SIGNAL */
1260 Route::all_redirects_flip ()
1262 Glib::RWLock::ReaderLock lm (redirect_lock);
1264 if (_redirects.empty()) {
1268 bool first_is_on = _redirects.front()->active();
1270 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
1271 (*i)->set_active (!first_is_on, this);
1276 Route::all_redirects_active (bool state)
1278 Glib::RWLock::ReaderLock lm (redirect_lock);
1280 if (_redirects.empty()) {
1284 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
1285 (*i)->set_active (state, this);
1289 struct RedirectSorter {
1290 bool operator() (boost::shared_ptr<const Redirect> a, boost::shared_ptr<const Redirect> b) {
1291 return a->sort_key() < b->sort_key();
1296 Route::sort_redirects (uint32_t* err_streams)
1299 RedirectSorter comparator;
1300 Glib::RWLock::WriterLock lm (redirect_lock);
1301 uint32_t old_rmo = redirect_max_outs;
1303 /* the sweet power of C++ ... */
1305 RedirectList as_it_was_before = _redirects;
1307 _redirects.sort (comparator);
1309 if (_reset_plugin_counts (err_streams)) {
1310 _redirects = as_it_was_before;
1311 redirect_max_outs = old_rmo;
1317 redirects_changed (this); /* EMIT SIGNAL */
1329 Route::get_template()
1331 return state(false);
1335 Route::state(bool full_state)
1337 XMLNode *node = new XMLNode("Route");
1338 RedirectList:: iterator i;
1342 snprintf (buf, sizeof (buf), "0x%x", _flags);
1343 node->add_property("flags", buf);
1346 node->add_property("default-type", _default_type.to_string());
1348 node->add_property("active", _active?"yes":"no");
1349 node->add_property("muted", _muted?"yes":"no");
1350 node->add_property("soloed", _soloed?"yes":"no");
1351 node->add_property("phase-invert", _phase_invert?"yes":"no");
1352 node->add_property("mute-affects-pre-fader", _mute_affects_pre_fader?"yes":"no");
1353 node->add_property("mute-affects-post-fader", _mute_affects_post_fader?"yes":"no");
1354 node->add_property("mute-affects-control-outs", _mute_affects_control_outs?"yes":"no");
1355 node->add_property("mute-affects-main-outs", _mute_affects_main_outs?"yes":"no");
1358 node->add_property("edit-group", _edit_group->name());
1361 node->add_property("mix-group", _mix_group->name());
1364 string order_string;
1365 OrderKeys::iterator x = order_keys.begin();
1367 while (x != order_keys.end()) {
1368 order_string += (*x).first;
1369 order_string += '=';
1370 snprintf (buf, sizeof(buf), "%ld", (*x).second);
1371 order_string += buf;
1375 if (x == order_keys.end()) {
1379 order_string += ':';
1381 node->add_property ("order-keys", order_string);
1383 node->add_child_nocopy (IO::state (full_state));
1384 node->add_child_nocopy (_solo_control.get_state ());
1385 node->add_child_nocopy (_mute_control.get_state ());
1387 if (_control_outs) {
1388 XMLNode* cnode = new XMLNode (X_("ControlOuts"));
1389 cnode->add_child_nocopy (_control_outs->state (full_state));
1390 node->add_child_nocopy (*cnode);
1393 if (_comment.length()) {
1394 XMLNode *cmt = node->add_child ("Comment");
1395 cmt->add_content (_comment);
1398 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
1399 node->add_child_nocopy((*i)->state (full_state));
1403 node->add_child_copy (*_extra_xml);
1410 Route::set_deferred_state ()
1413 XMLNodeConstIterator niter;
1415 if (!deferred_state) {
1419 nlist = deferred_state->children();
1421 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
1422 add_redirect_from_xml (**niter);
1425 delete deferred_state;
1430 Route::add_redirect_from_xml (const XMLNode& node)
1432 const XMLProperty *prop;
1434 if (node.name() == "Send") {
1438 boost::shared_ptr<Send> send (new Send (_session, node));
1439 add_redirect (send, this);
1442 catch (failed_constructor &err) {
1443 error << _("Send construction failed") << endmsg;
1447 } else if (node.name() == "Insert") {
1450 if ((prop = node.property ("type")) != 0) {
1452 boost::shared_ptr<Insert> insert;
1454 if (prop->value() == "ladspa" || prop->value() == "Ladspa" || prop->value() == "vst") {
1456 insert.reset (new PluginInsert(_session, node));
1458 } else if (prop->value() == "port") {
1461 insert.reset (new PortInsert (_session, node));
1465 error << string_compose(_("unknown Insert type \"%1\"; ignored"), prop->value()) << endmsg;
1468 add_redirect (insert, this);
1471 error << _("Insert XML node has no type property") << endmsg;
1475 catch (failed_constructor &err) {
1476 warning << _("insert could not be created. Ignored.") << endmsg;
1483 Route::set_state (const XMLNode& node)
1486 XMLNodeConstIterator niter;
1488 XMLPropertyList plist;
1489 const XMLProperty *prop;
1491 if (node.name() != "Route"){
1492 error << string_compose(_("Bad node sent to Route::set_state() [%1]"), node.name()) << endmsg;
1496 if ((prop = node.property ("flags")) != 0) {
1498 sscanf (prop->value().c_str(), "0x%x", &x);
1504 if ((prop = node.property ("default-type")) != 0) {
1505 _default_type = DataType(prop->value());
1506 assert(_default_type != DataType::NIL);
1509 if ((prop = node.property ("phase-invert")) != 0) {
1510 set_phase_invert(prop->value()=="yes"?true:false, this);
1513 if ((prop = node.property ("active")) != 0) {
1514 set_active (prop->value() == "yes");
1517 if ((prop = node.property ("muted")) != 0) {
1518 bool yn = prop->value()=="yes"?true:false;
1520 /* force reset of mute status */
1524 mute_gain = desired_mute_gain;
1527 if ((prop = node.property ("soloed")) != 0) {
1528 bool yn = prop->value()=="yes"?true:false;
1530 /* force reset of solo status */
1533 set_solo (yn, this);
1534 solo_gain = desired_solo_gain;
1537 if ((prop = node.property ("mute-affects-pre-fader")) != 0) {
1538 _mute_affects_pre_fader = (prop->value()=="yes")?true:false;
1541 if ((prop = node.property ("mute-affects-post-fader")) != 0) {
1542 _mute_affects_post_fader = (prop->value()=="yes")?true:false;
1545 if ((prop = node.property ("mute-affects-control-outs")) != 0) {
1546 _mute_affects_control_outs = (prop->value()=="yes")?true:false;
1549 if ((prop = node.property ("mute-affects-main-outs")) != 0) {
1550 _mute_affects_main_outs = (prop->value()=="yes")?true:false;
1553 if ((prop = node.property ("edit-group")) != 0) {
1554 RouteGroup* edit_group = _session.edit_group_by_name(prop->value());
1555 if(edit_group == 0) {
1556 error << string_compose(_("Route %1: unknown edit group \"%2 in saved state (ignored)"), _name, prop->value()) << endmsg;
1558 set_edit_group(edit_group, this);
1562 if ((prop = node.property ("order-keys")) != 0) {
1566 string::size_type colon, equal;
1567 string remaining = prop->value();
1569 while (remaining.length()) {
1571 if ((equal = remaining.find_first_of ('=')) == string::npos || equal == remaining.length()) {
1572 error << string_compose (_("badly formed order key string in state file! [%1] ... ignored."), remaining)
1575 if (sscanf (remaining.substr (equal+1).c_str(), "%ld", &n) != 1) {
1576 error << string_compose (_("badly formed order key string in state file! [%1] ... ignored."), remaining)
1579 set_order_key (remaining.substr (0, equal), n);
1583 colon = remaining.find_first_of (':');
1585 if (colon != string::npos) {
1586 remaining = remaining.substr (colon+1);
1593 nlist = node.children();
1595 if (deferred_state) {
1596 delete deferred_state;
1599 deferred_state = new XMLNode("deferred state");
1601 /* set parent class properties before anything else */
1603 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
1607 if (child->name() == IO::state_node_name) {
1609 IO::set_state (*child);
1614 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
1618 if (child->name() == "Send") {
1621 if (!IO::ports_legal) {
1623 deferred_state->add_child_copy (*child);
1626 add_redirect_from_xml (*child);
1629 } else if (child->name() == "Insert") {
1631 if (!IO::ports_legal) {
1633 deferred_state->add_child_copy (*child);
1637 add_redirect_from_xml (*child);
1640 } else if (child->name() == "ControlOuts") {
1642 string coutname = _name;
1643 coutname += _("[control]");
1645 _control_outs = new IO (_session, coutname);
1646 _control_outs->set_state (**(child->children().begin()));
1648 } else if (child->name() == "Comment") {
1650 /* XXX this is a terrible API design in libxml++ */
1652 XMLNode *cmt = *(child->children().begin());
1653 _comment = cmt->content();
1655 } else if (child->name() == "extra") {
1656 _extra_xml = new XMLNode (*child);
1657 } else if (child->name() == "solo") {
1658 _solo_control.set_state (*child);
1659 _session.add_controllable (&_solo_control);
1660 } else if (child->name() == "mute") {
1661 _mute_control.set_state (*child);
1662 _session.add_controllable (&_mute_control);
1666 if ((prop = node.property ("mix-group")) != 0) {
1667 RouteGroup* mix_group = _session.mix_group_by_name(prop->value());
1668 if (mix_group == 0) {
1669 error << string_compose(_("Route %1: unknown mix group \"%2 in saved state (ignored)"), _name, prop->value()) << endmsg;
1671 set_mix_group(mix_group, this);
1679 Route::curve_reallocate ()
1681 // _gain_automation_curve.finish_resize ();
1682 // _pan_automation_curve.finish_resize ();
1686 Route::silence (nframes_t nframes, nframes_t offset)
1690 // reset_peak_meters ();
1692 IO::silence (nframes, offset);
1694 if (_control_outs) {
1695 _control_outs->silence (nframes, offset);
1699 Glib::RWLock::ReaderLock lm (redirect_lock, Glib::TRY_LOCK);
1702 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
1703 boost::shared_ptr<PluginInsert> pi;
1704 if (!_active && (pi = boost::dynamic_pointer_cast<PluginInsert> (*i)) != 0) {
1705 // skip plugins, they don't need anything when we're not active
1709 (*i)->silence (nframes, offset);
1712 if (nframes == _session.get_block_size() && offset == 0) {
1722 Route::set_control_outs (const vector<string>& ports)
1724 Glib::Mutex::Lock lm (control_outs_lock);
1725 vector<string>::const_iterator i;
1727 if (_control_outs) {
1728 delete _control_outs;
1732 if (ports.empty()) {
1736 string coutname = _name;
1737 coutname += _("[control]");
1739 _control_outs = new IO (_session, coutname);
1741 /* our control outs need as many outputs as we
1742 have outputs. we track the changes in ::output_change_handler().
1745 _control_outs->ensure_io (0, n_outputs(), true, this);
1751 Route::set_edit_group (RouteGroup *eg, void *src)
1754 if (eg == _edit_group) {
1759 _edit_group->remove (this);
1762 if ((_edit_group = eg) != 0) {
1763 _edit_group->add (this);
1766 _session.set_dirty ();
1767 edit_group_changed (src); /* EMIT SIGNAL */
1771 Route::drop_edit_group (void *src)
1774 _session.set_dirty ();
1775 edit_group_changed (src); /* EMIT SIGNAL */
1779 Route::set_mix_group (RouteGroup *mg, void *src)
1782 if (mg == _mix_group) {
1787 _mix_group->remove (this);
1790 if ((_mix_group = mg) != 0) {
1791 _mix_group->add (this);
1794 _session.set_dirty ();
1795 mix_group_changed (src); /* EMIT SIGNAL */
1799 Route::drop_mix_group (void *src)
1802 _session.set_dirty ();
1803 mix_group_changed (src); /* EMIT SIGNAL */
1807 Route::set_comment (string cmt, void *src)
1810 comment_changed (src);
1811 _session.set_dirty ();
1815 Route::feeds (boost::shared_ptr<Route> other)
1820 uint32_t no = self.n_outputs();
1821 uint32_t ni = other->n_inputs ();
1823 for (i = 0; i < no; ++i) {
1824 for (j = 0; j < ni; ++j) {
1825 if (self.output(i)->connected_to (other->input(j)->name())) {
1831 /* check Redirects which may also interconnect Routes */
1833 for (RedirectList::iterator r = _redirects.begin(); r != _redirects.end(); r++) {
1835 no = (*r)->n_outputs();
1837 for (i = 0; i < no; ++i) {
1838 for (j = 0; j < ni; ++j) {
1839 if ((*r)->output(i)->connected_to (other->input (j)->name())) {
1846 /* check for control room outputs which may also interconnect Routes */
1848 if (_control_outs) {
1850 no = _control_outs->n_outputs();
1852 for (i = 0; i < no; ++i) {
1853 for (j = 0; j < ni; ++j) {
1854 if (_control_outs->output(i)->connected_to (other->input (j)->name())) {
1865 Route::set_mute_config (mute_type t, bool onoff, void *src)
1869 _mute_affects_pre_fader = onoff;
1870 pre_fader_changed(src); /* EMIT SIGNAL */
1874 _mute_affects_post_fader = onoff;
1875 post_fader_changed(src); /* EMIT SIGNAL */
1879 _mute_affects_control_outs = onoff;
1880 control_outs_changed(src); /* EMIT SIGNAL */
1884 _mute_affects_main_outs = onoff;
1885 main_outs_changed(src); /* EMIT SIGNAL */
1891 Route::get_mute_config (mute_type t)
1897 onoff = _mute_affects_pre_fader;
1900 onoff = _mute_affects_post_fader;
1903 onoff = _mute_affects_control_outs;
1906 onoff = _mute_affects_main_outs;
1914 Route::set_active (bool yn)
1917 active_changed(); /* EMIT SIGNAL */
1921 Route::handle_transport_stopped (bool abort_ignored, bool did_locate, bool can_flush_redirects)
1923 nframes_t now = _session.transport_frame();
1926 Glib::RWLock::ReaderLock lm (redirect_lock);
1928 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
1930 if (Config->get_plugins_stop_with_transport() && can_flush_redirects) {
1931 (*i)->deactivate ();
1935 (*i)->transport_stopped (now);
1939 IO::transport_stopped (now);
1941 _roll_delay = _initial_delay;
1945 Route::input_change_handler (IOChange change, void *ignored)
1947 if (change & ConfigurationChanged) {
1948 reset_plugin_counts (0);
1953 Route::output_change_handler (IOChange change, void *ignored)
1955 if (change & ConfigurationChanged) {
1956 if (_control_outs) {
1957 _control_outs->ensure_io (0, n_outputs(), true, this);
1960 reset_plugin_counts (0);
1965 Route::pans_required () const
1967 if (n_outputs() < 2) {
1971 return max (n_inputs (), redirect_max_outs);
1975 Route::no_roll (nframes_t nframes, nframes_t start_frame, nframes_t end_frame, nframes_t offset,
1976 bool session_state_changing, bool can_record, bool rec_monitors_input)
1978 if (n_outputs() == 0) {
1982 if (session_state_changing || !_active) {
1983 silence (nframes, offset);
1987 apply_gain_automation = false;
1990 passthru (start_frame, end_frame, nframes, offset, 0, false);
1992 silence (nframes, offset);
1999 Route::check_initial_delay (nframes_t nframes, nframes_t& offset, nframes_t& transport_frame)
2001 if (_roll_delay > nframes) {
2003 _roll_delay -= nframes;
2004 silence (nframes, offset);
2005 /* transport frame is not legal for caller to use */
2008 } else if (_roll_delay > 0) {
2010 nframes -= _roll_delay;
2012 silence (_roll_delay, offset);
2014 offset += _roll_delay;
2015 transport_frame += _roll_delay;
2024 Route::roll (nframes_t nframes, nframes_t start_frame, nframes_t end_frame, nframes_t offset, int declick,
2025 bool can_record, bool rec_monitors_input)
2027 if ((n_outputs() == 0 && _redirects.empty()) || n_inputs() == 0 || !_active) {
2028 silence (nframes, offset);
2032 nframes_t unused = 0;
2034 if ((nframes = check_initial_delay (nframes, offset, unused)) == 0) {
2040 apply_gain_automation = false;
2043 Glib::Mutex::Lock am (automation_lock, Glib::TRY_LOCK);
2045 if (am.locked() && _session.transport_rolling()) {
2047 nframes_t start_frame = end_frame - nframes;
2049 if (gain_automation_playback()) {
2050 apply_gain_automation = _gain_automation_curve.rt_safe_get_vector (start_frame, end_frame, _session.gain_automation_buffer(), nframes);
2055 passthru (start_frame, end_frame, nframes, offset, declick, false);
2061 Route::silent_roll (nframes_t nframes, nframes_t start_frame, nframes_t end_frame, nframes_t offset,
2062 bool can_record, bool rec_monitors_input)
2064 silence (nframes, offset);
2069 Route::toggle_monitor_input ()
2071 for (vector<Port*>::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
2072 (*i)->ensure_monitor_input(!(*i)->monitoring_input());
2077 Route::has_external_redirects () const
2079 boost::shared_ptr<const PortInsert> pi;
2081 for (RedirectList::const_iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2082 if ((pi = boost::dynamic_pointer_cast<const PortInsert>(*i)) != 0) {
2084 uint32_t no = pi->n_outputs();
2086 for (uint32_t n = 0; n < no; ++n) {
2088 string port_name = pi->output(n)->name();
2089 string client_name = port_name.substr (0, port_name.find(':'));
2091 /* only say "yes" if the redirect is actually in use */
2093 if (client_name != "ardour" && pi->active()) {
2104 Route::flush_redirects ()
2106 /* XXX shouldn't really try to take this lock, since
2107 this is called from the RT audio thread.
2110 Glib::RWLock::ReaderLock lm (redirect_lock);
2112 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2113 (*i)->deactivate ();
2119 Route::set_meter_point (MeterPoint p, void *src)
2121 if (_meter_point != p) {
2123 meter_change (src); /* EMIT SIGNAL */
2124 _session.set_dirty ();
2129 Route::update_total_latency ()
2133 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2134 if ((*i)->active ()) {
2135 _own_latency += (*i)->latency ();
2139 set_port_latency (_own_latency);
2141 /* this (virtual) function is used for pure Routes,
2142 not derived classes like AudioTrack. this means
2143 that the data processed here comes from an input
2144 port, not prerecorded material, and therefore we
2145 have to take into account any input latency.
2148 _own_latency += input_latency ();
2150 return _own_latency;
2154 Route::set_latency_delay (nframes_t longest_session_latency)
2156 _initial_delay = longest_session_latency - _own_latency;
2158 if (_session.transport_stopped()) {
2159 _roll_delay = _initial_delay;
2163 Route::ToggleControllable::ToggleControllable (std::string name, Route& s, ToggleType tp)
2164 : Controllable (name), route (s), type(tp)
2170 Route::ToggleControllable::set_value (float val)
2172 bool bval = ((val >= 0.5f) ? true: false);
2176 route.set_mute (bval, this);
2179 route.set_solo (bval, this);
2187 Route::ToggleControllable::get_value (void) const
2193 val = route.muted() ? 1.0f : 0.0f;
2196 val = route.soloed() ? 1.0f : 0.0f;
2206 Route::set_block_size (nframes_t nframes)
2208 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2209 (*i)->set_block_size (nframes);
2214 Route::redirect_active_proxy (Redirect* ignored, void* ignored_src)
2216 _session.update_latency_compensation (false, false);
2220 Route::protect_automation ()
2222 switch (gain_automation_state()) {
2225 set_gain_automation_state (Off);
2231 switch (panner().automation_state ()) {
2234 panner().set_automation_state (Off);
2240 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2241 boost::shared_ptr<PluginInsert> pi;
2242 if ((pi = boost::dynamic_pointer_cast<PluginInsert> (*i)) != 0) {
2243 pi->protect_automation ();
2249 Route::set_pending_declick (int declick)
2252 /* this call is not allowed to turn off a pending declick unless "force" is true */
2254 _pending_declick = declick;
2256 // cerr << _name << ": after setting to " << declick << " pending declick = " << _pending_declick << endl;
2258 _pending_declick = 0;