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.
24 #include <sigc++/bind.h>
25 #include <pbd/xml++.h>
27 #include <ardour/timestamps.h>
28 #include <ardour/audioengine.h>
29 #include <ardour/route.h>
30 #include <ardour/insert.h>
31 #include <ardour/send.h>
32 #include <ardour/session.h>
33 #include <ardour/utils.h>
34 #include <ardour/configuration.h>
35 #include <ardour/cycle_timer.h>
36 #include <ardour/route_group.h>
37 #include <ardour/port.h>
38 #include <ardour/ladspa_plugin.h>
39 #include <ardour/panner.h>
40 #include <ardour/dB.h>
41 #include <ardour/mix.h>
46 using namespace ARDOUR;
50 uint32_t Route::order_key_cnt = 0;
53 Route::Route (Session& sess, string name, int input_min, int input_max, int output_min, int output_max, Flag flg)
54 : IO (sess, name, input_min, input_max, output_min, output_max),
56 _solo_control (*this, ToggleControllable::SoloControl),
57 _mute_control (*this, ToggleControllable::MuteControl)
62 Route::Route (Session& sess, const XMLNode& node)
64 _solo_control (*this, ToggleControllable::SoloControl),
65 _mute_control (*this, ToggleControllable::MuteControl)
74 redirect_max_outs = 0;
78 _phase_invert = false;
79 order_keys[N_("signal")] = order_key_cnt++;
82 _meter_point = MeterPostFader;
86 _have_internal_generator = false;
88 _pending_declick = true;
89 _remote_control_id = 0;
94 _mute_affects_pre_fader = Config->get_mute_affects_pre_fader();
95 _mute_affects_post_fader = Config->get_mute_affects_post_fader();
96 _mute_affects_control_outs = Config->get_mute_affects_control_outs();
97 _mute_affects_main_outs = Config->get_mute_affects_main_outs();
100 desired_solo_gain = 1.0;
102 desired_mute_gain = 1.0;
106 input_changed.connect (mem_fun (this, &Route::input_change_handler));
107 output_changed.connect (mem_fun (this, &Route::output_change_handler));
112 GoingAway (); /* EMIT SIGNAL */
113 clear_redirects (this);
116 delete _control_outs;
121 Route::set_remote_control_id (uint32_t id)
123 if (id != _remote_control_id) {
124 _remote_control_id = id;
125 RemoteControlIDChanged ();
130 Route::remote_control_id() const
132 return _remote_control_id;
136 Route::order_key (string name) const
138 OrderKeys::const_iterator i;
140 if ((i = order_keys.find (name)) == order_keys.end()) {
148 Route::set_order_key (string name, long n)
150 order_keys[name] = n;
151 _session.set_dirty ();
155 Route::inc_gain (gain_t fraction, void *src)
157 IO::inc_gain (fraction, src);
161 Route::set_gain (gain_t val, void *src)
163 if (src != 0 && _mix_group && src != _mix_group && _mix_group->is_active()) {
165 if (_mix_group->is_relative()) {
168 gain_t usable_gain = gain();
169 if (usable_gain < 0.000001f) {
170 usable_gain=0.000001f;
174 if (delta < 0.000001f) {
178 delta -= usable_gain;
180 if (delta == 0.0f) return;
182 gain_t factor = delta / usable_gain;
185 factor = _mix_group->get_max_factor(factor);
186 if (factor == 0.0f) {
191 factor = _mix_group->get_min_factor(factor);
192 if (factor == 0.0f) {
198 _mix_group->apply (&Route::inc_gain, factor, _mix_group);
202 _mix_group->apply (&Route::set_gain, val, _mix_group);
212 IO::set_gain (val, src);
216 Route::process_output_buffers (vector<Sample*>& bufs, uint32_t nbufs,
217 jack_nframes_t start_frame, jack_nframes_t end_frame,
218 jack_nframes_t nframes, jack_nframes_t offset, bool with_redirects, int declick,
222 RedirectList::iterator i;
223 bool post_fader_work = false;
224 bool mute_declick_applied = false;
226 vector<Sample*>::iterator bufiter;
230 bool no_monitor = (Config->get_use_hardware_monitoring() || !Config->get_use_sw_monitoring ());
231 gain_t* gab = _session.gain_automation_buffer();
233 declick = _pending_declick;
236 Glib::Mutex::Lock cm (control_outs_lock, Glib::TRY_LOCK);
246 Glib::Mutex::Lock dm (declick_lock, Glib::TRY_LOCK);
249 dmg = desired_mute_gain;
250 dsg = desired_solo_gain;
259 /* ----------------------------------------------------------------------------------------------------
260 GLOBAL DECLICK (for transport changes etc.)
261 -------------------------------------------------------------------------------------------------- */
264 apply_declick (bufs, nbufs, nframes, 0.0, 1.0, _phase_invert);
265 _pending_declick = 0;
266 } else if (declick < 0) {
267 apply_declick (bufs, nbufs, nframes, 1.0, 0.0, _phase_invert);
268 _pending_declick = 0;
271 /* no global declick */
273 if (solo_gain != dsg) {
274 apply_declick (bufs, nbufs, nframes, solo_gain, dsg, _phase_invert);
280 /* ----------------------------------------------------------------------------------------------------
281 INPUT METERING & MONITORING
282 -------------------------------------------------------------------------------------------------- */
284 if (meter && (_meter_point == MeterInput)) {
285 for (n = 0; n < nbufs; ++n) {
286 _peak_power[n] = Session::compute_peak (bufs[n], nframes, _peak_power[n]);
290 if (!_soloed && _mute_affects_pre_fader && (mute_gain != dmg)) {
291 apply_declick (bufs, nbufs, nframes, mute_gain, dmg, _phase_invert);
293 mute_declick_applied = true;
296 if ((_meter_point == MeterInput) && co) {
298 solo_audible = dsg > 0;
299 mute_audible = dmg > 0;// || !_mute_affects_pre_fader;
301 if ( // muted by solo of another track
305 // muted by mute of this track
309 // rec-enabled but not s/w monitoring
311 // TODO: this is probably wrong
313 (no_monitor && record_enabled() && (!_session.get_auto_input() || _session.actively_recording()))
317 co->silence (nframes, offset);
321 co->deliver_output (bufs, nbufs, nframes, offset);
326 /* ----------------------------------------------------------------------------------------------------
328 -------------------------------------------------------------------------------------------------- */
330 if (with_redirects) {
331 Glib::RWLock::ReaderLock rm (redirect_lock, Glib::TRY_LOCK);
333 if (mute_gain > 0 || !_mute_affects_pre_fader) {
334 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
335 switch ((*i)->placement()) {
337 (*i)->run (bufs, nbufs, nframes, offset);
340 post_fader_work = true;
345 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
346 switch ((*i)->placement()) {
348 (*i)->silence (nframes, offset);
351 post_fader_work = true;
360 if (!_soloed && (mute_gain != dmg) && !mute_declick_applied && _mute_affects_post_fader) {
361 apply_declick (bufs, nbufs, nframes, mute_gain, dmg, _phase_invert);
363 mute_declick_applied = true;
366 /* ----------------------------------------------------------------------------------------------------
367 PRE-FADER METERING & MONITORING
368 -------------------------------------------------------------------------------------------------- */
370 if (meter && (_meter_point == MeterPreFader)) {
371 for (n = 0; n < nbufs; ++n) {
372 _peak_power[n] = Session::compute_peak (bufs[n], nframes, _peak_power[n]);
377 if ((_meter_point == MeterPreFader) && co) {
379 solo_audible = dsg > 0;
380 mute_audible = dmg > 0 || !_mute_affects_pre_fader;
382 if ( // muted by solo of another track
386 // muted by mute of this track
390 // rec-enabled but not s/w monitoring
392 (no_monitor && record_enabled() && (!_session.get_auto_input() || _session.actively_recording()))
396 co->silence (nframes, offset);
400 co->deliver_output (bufs, nbufs, nframes, offset);
405 /* ----------------------------------------------------------------------------------------------------
407 -------------------------------------------------------------------------------------------------- */
409 /* if not recording or recording and requiring any monitor signal, then apply gain */
411 if ( // not recording
413 !(record_enabled() && _session.actively_recording()) ||
417 // h/w monitoring not in use
419 (!Config->get_use_hardware_monitoring() &&
421 // AND software monitoring required
423 Config->get_use_sw_monitoring())) {
425 if (apply_gain_automation) {
428 for (n = 0; n < nbufs; ++n) {
429 Sample *sp = bufs[n];
431 for (jack_nframes_t nx = 0; nx < nframes; ++nx) {
436 for (n = 0; n < nbufs; ++n) {
437 Sample *sp = bufs[n];
439 for (jack_nframes_t nx = 0; nx < nframes; ++nx) {
445 if (apply_gain_automation && _session.transport_rolling() && nframes > 0) {
446 _effective_gain = gab[nframes-1];
451 /* manual (scalar) gain */
455 apply_declick (bufs, nbufs, nframes, _gain, dg, _phase_invert);
458 } else if (_gain != 0 && (_phase_invert || _gain != 1.0)) {
460 /* no need to interpolate current gain value,
461 but its non-unity, so apply it. if the gain
462 is zero, do nothing because we'll ship silence
474 for (n = 0; n < nbufs; ++n) {
475 Sample *sp = bufs[n];
476 apply_gain_to_buffer(sp,nframes,this_gain);
479 } else if (_gain == 0) {
480 for (n = 0; n < nbufs; ++n) {
481 memset (bufs[n], 0, sizeof (Sample) * nframes);
488 /* actively recording, no monitoring required; leave buffers as-is to save CPU cycles */
492 /* ----------------------------------------------------------------------------------------------------
494 -------------------------------------------------------------------------------------------------- */
496 /* note that post_fader_work cannot be true unless with_redirects was also true, so don't test both */
498 if (post_fader_work) {
500 Glib::RWLock::ReaderLock rm (redirect_lock, Glib::TRY_LOCK);
502 if (mute_gain > 0 || !_mute_affects_post_fader) {
503 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
504 switch ((*i)->placement()) {
508 (*i)->run (bufs, nbufs, nframes, offset);
513 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
514 switch ((*i)->placement()) {
518 (*i)->silence (nframes, offset);
526 if (!_soloed && (mute_gain != dmg) && !mute_declick_applied && _mute_affects_control_outs) {
527 apply_declick (bufs, nbufs, nframes, mute_gain, dmg, _phase_invert);
529 mute_declick_applied = true;
532 /* ----------------------------------------------------------------------------------------------------
534 -------------------------------------------------------------------------------------------------- */
536 if ((_meter_point == MeterPostFader) && co) {
538 solo_audible = solo_gain > 0;
539 mute_audible = dmg > 0 || !_mute_affects_control_outs;
541 if ( // silent anyway
543 (_gain == 0 && !apply_gain_automation) ||
545 // muted by solo of another track
549 // muted by mute of this track
553 // recording but not s/w monitoring
555 (no_monitor && record_enabled() && (!_session.get_auto_input() || _session.actively_recording()))
559 co->silence (nframes, offset);
563 co->deliver_output_no_pan (bufs, nbufs, nframes, offset);
567 /* ----------------------------------------------------------------------
569 ----------------------------------------------------------------------*/
571 if (!_soloed && (mute_gain != dmg) && !mute_declick_applied && _mute_affects_main_outs) {
572 apply_declick (bufs, nbufs, nframes, mute_gain, dmg, _phase_invert);
574 mute_declick_applied = true;
577 /* ----------------------------------------------------------------------------------------------------
579 -------------------------------------------------------------------------------------------------- */
581 solo_audible = dsg > 0;
582 mute_audible = dmg > 0 || !_mute_affects_main_outs;
584 if (n_outputs() == 0) {
588 } else if (no_monitor && record_enabled() && (!_session.get_auto_input() || _session.actively_recording())) {
590 IO::silence (nframes, offset);
594 if ( // silent anyway
596 (_gain == 0 && !apply_gain_automation) ||
598 // muted by solo of another track, but not using control outs for solo
600 (!solo_audible && (_session.solo_model() != Session::SoloBus)) ||
602 // muted by mute of this track
608 /* don't use Route::silence() here, because that causes
609 all outputs (sends, port inserts, etc. to be silent).
612 if (_meter_point == MeterPostFader) {
613 reset_peak_meters ();
616 IO::silence (nframes, offset);
620 if (_session.transport_speed() > 1.5f || _session.transport_speed() < -1.5f) {
621 pan (bufs, nbufs, nframes, offset, speed_quietning);
623 // cerr << _name << " panner state = " << _panner->automation_state() << endl;
624 if (!_panner->empty() &&
625 (_panner->automation_state() & Play ||
626 ((_panner->automation_state() & Touch) && !_panner->touching()))) {
627 pan_automated (bufs, nbufs, start_frame, end_frame, nframes, offset);
629 pan (bufs, nbufs, nframes, offset, 1.0);
636 /* ----------------------------------------------------------------------------------------------------
638 -------------------------------------------------------------------------------------------------- */
640 if (meter && (_meter_point == MeterPostFader)) {
641 // cerr << "meter post" << endl;
643 if ((_gain == 0 && !apply_gain_automation) || dmg == 0) {
644 uint32_t no = n_outputs();
645 for (n = 0; n < no; ++n) {
649 uint32_t no = n_outputs();
650 for (n = 0; n < no; ++n) {
651 _peak_power[n] = Session::compute_peak (output(n)->get_buffer (nframes) + offset, nframes, _peak_power[n]);
658 Route::n_process_buffers ()
660 return max (n_inputs(), redirect_max_outs);
665 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)
667 vector<Sample*>& bufs = _session.get_passthru_buffers();
668 uint32_t limit = n_process_buffers ();
672 collect_input (bufs, limit, nframes, offset);
674 #define meter_stream meter_first
677 for (uint32_t n = 0; n < limit; ++n) {
678 _peak_power[n] = Session::compute_peak (bufs[n], nframes, _peak_power[n]);
680 meter_stream = false;
685 process_output_buffers (bufs, limit, start_frame, end_frame, nframes, offset, true, declick, meter_stream);
691 Route::set_phase_invert (bool yn, void *src)
693 if (_phase_invert != yn) {
696 // phase_invert_changed (src); /* EMIT SIGNAL */
700 Route::set_solo (bool yn, void *src)
706 if (_mix_group && src != _mix_group && _mix_group->is_active()) {
707 _mix_group->apply (&Route::set_solo, yn, _mix_group);
713 solo_changed (src); /* EMIT SIGNAL */
714 _solo_control.Changed (); /* EMIT SIGNAL */
719 Route::set_solo_mute (bool yn)
721 Glib::Mutex::Lock lm (declick_lock);
723 /* Called by Session in response to another Route being soloed.
726 desired_solo_gain = (yn?0.0:1.0);
730 Route::set_solo_safe (bool yn, void *src)
732 if (_solo_safe != yn) {
734 solo_safe_changed (src); /* EMIT SIGNAL */
739 Route::set_mute (bool yn, void *src)
742 if (_mix_group && src != _mix_group && _mix_group->is_active()) {
743 _mix_group->apply (&Route::set_mute, yn, _mix_group);
749 mute_changed (src); /* EMIT SIGNAL */
751 _mute_control.Changed (); /* EMIT SIGNAL */
753 Glib::Mutex::Lock lm (declick_lock);
754 desired_mute_gain = (yn?0.0f:1.0f);
759 Route::add_redirect (Redirect *redirect, void *src, uint32_t* err_streams)
761 uint32_t old_rmo = redirect_max_outs;
763 if (!_session.engine().connected()) {
768 Glib::RWLock::WriterLock lm (redirect_lock);
773 uint32_t potential_max_streams = 0;
775 if ((pi = dynamic_cast<PluginInsert*>(redirect)) != 0) {
778 if (pi->input_streams() == 0) {
779 /* instrument plugin */
780 _have_internal_generator = true;
783 potential_max_streams = max(pi->input_streams(), pi->output_streams());
785 } else if ((porti = dynamic_cast<PortInsert*>(redirect)) != 0) {
787 /* force new port inserts to start out with an i/o configuration
788 that matches this route's i/o configuration.
790 the "inputs" for the port are supposed to match the output
793 the "outputs" of the route should match the inputs of this
794 route. XXX shouldn't they match the number of active signal
795 streams at the point of insertion?
799 porti->ensure_io (n_outputs (), n_inputs(), false, this);
802 // Ensure peak vector sizes before the plugin is activated
803 while (_peak_power.size() < potential_max_streams) {
804 _peak_power.push_back(0);
806 while (_visible_peak_power.size() < potential_max_streams) {
807 _visible_peak_power.push_back(0);
810 _redirects.push_back (redirect);
812 if (_reset_plugin_counts (err_streams)) {
813 _redirects.pop_back ();
814 _reset_plugin_counts (0); // it worked before we tried to add it ...
818 redirect->activate ();
819 redirect->active_changed.connect (mem_fun (*this, &Route::redirect_active_proxy));
822 if (redirect_max_outs != old_rmo || old_rmo == 0) {
827 redirects_changed (src); /* EMIT SIGNAL */
832 Route::add_redirects (const RedirectList& others, void *src, uint32_t* err_streams)
834 uint32_t old_rmo = redirect_max_outs;
836 if (!_session.engine().connected()) {
841 Glib::RWLock::WriterLock lm (redirect_lock);
843 RedirectList::iterator existing_end = _redirects.end();
846 uint32_t potential_max_streams = 0;
848 for (RedirectList::const_iterator i = others.begin(); i != others.end(); ++i) {
852 if ((pi = dynamic_cast<PluginInsert*>(*i)) != 0) {
855 uint32_t m = max(pi->input_streams(), pi->output_streams());
856 if (m > potential_max_streams)
857 potential_max_streams = m;
860 // Ensure peak vector sizes before the plugin is activated
861 while (_peak_power.size() < potential_max_streams) {
862 _peak_power.push_back(0);
864 while (_visible_peak_power.size() < potential_max_streams) {
865 _visible_peak_power.push_back(0);
868 _redirects.push_back (*i);
870 if (_reset_plugin_counts (err_streams)) {
872 _redirects.erase (existing_end, _redirects.end());
873 _reset_plugin_counts (0); // it worked before we tried to add it ...
878 (*i)->active_changed.connect (mem_fun (*this, &Route::redirect_active_proxy));
882 if (redirect_max_outs != old_rmo || old_rmo == 0) {
886 redirects_changed (src); /* EMIT SIGNAL */
891 Route::clear_redirects (void *src)
893 uint32_t old_rmo = redirect_max_outs;
895 if (!_session.engine().connected()) {
900 Glib::RWLock::WriterLock lm (redirect_lock);
902 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
909 if (redirect_max_outs != old_rmo) {
913 redirect_max_outs = 0;
914 _have_internal_generator = false;
915 redirects_changed (src); /* EMIT SIGNAL */
919 Route::remove_redirect (Redirect *redirect, void *src, uint32_t* err_streams)
921 uint32_t old_rmo = redirect_max_outs;
923 if (!_session.engine().connected()) {
927 redirect_max_outs = 0;
930 Glib::RWLock::WriterLock lm (redirect_lock);
931 RedirectList::iterator i;
932 bool removed = false;
934 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
935 if (*i == redirect) {
937 RedirectList::iterator tmp;
939 /* move along, see failure case for reset_plugin_counts()
940 where we may need to reinsert the redirect.
946 /* stop redirects that send signals to JACK ports
947 from causing noise as a result of no longer being
952 PortInsert* port_insert;
954 if ((send = dynamic_cast<Send*> (*i)) != 0) {
955 send->disconnect_inputs (this);
956 send->disconnect_outputs (this);
957 } else if ((port_insert = dynamic_cast<PortInsert*> (*i)) != 0) {
958 port_insert->disconnect_inputs (this);
959 port_insert->disconnect_outputs (this);
962 _redirects.erase (i);
975 if (_reset_plugin_counts (err_streams)) {
976 /* get back to where we where */
977 _redirects.insert (i, redirect);
978 /* we know this will work, because it worked before :) */
979 _reset_plugin_counts (0);
985 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
988 if ((pi = dynamic_cast<PluginInsert*>(*i)) != 0) {
989 if (pi->is_generator()) {
995 _have_internal_generator = foo;
998 if (old_rmo != redirect_max_outs) {
1002 redirects_changed (src); /* EMIT SIGNAL */
1007 Route::reset_plugin_counts (uint32_t* lpc)
1009 Glib::RWLock::WriterLock lm (redirect_lock);
1010 return _reset_plugin_counts (lpc);
1015 Route::_reset_plugin_counts (uint32_t* err_streams)
1017 RedirectList::iterator r;
1020 map<Placement,list<InsertCount> > insert_map;
1021 jack_nframes_t initial_streams;
1023 redirect_max_outs = 0;
1027 /* divide inserts up by placement so we get the signal flow
1028 properly modelled. we need to do this because the _redirects
1029 list is not sorted by placement, and because other reasons may
1030 exist now or in the future for this separate treatment.
1033 for (r = _redirects.begin(); r != _redirects.end(); ++r) {
1037 /* do this here in case we bomb out before we get to the end of
1041 redirect_max_outs = max ((*r)->output_streams (), redirect_max_outs);
1043 if ((insert = dynamic_cast<Insert*>(*r)) != 0) {
1045 insert_map[insert->placement()].push_back (InsertCount (*insert));
1047 /* reset plugin counts back to one for now so
1048 that we have a predictable, controlled
1049 state to try to configure.
1054 if ((pi = dynamic_cast<PluginInsert*>(insert)) != 0) {
1058 } else if (dynamic_cast<Send*> (*r) != 0) {
1071 /* Now process each placement in order, checking to see if we
1072 can really do what has been requested.
1077 if (check_some_plugin_counts (insert_map[PreFader], n_inputs (), err_streams)) {
1081 /* figure out the streams that will feed into PreFader */
1083 if (!insert_map[PreFader].empty()) {
1084 InsertCount& ic (insert_map[PreFader].back());
1085 initial_streams = ic.insert.compute_output_streams (ic.cnt);
1087 initial_streams = n_inputs ();
1092 if (check_some_plugin_counts (insert_map[PostFader], initial_streams, err_streams)) {
1096 /* OK, everything can be set up correctly, so lets do it */
1098 apply_some_plugin_counts (insert_map[PreFader]);
1099 apply_some_plugin_counts (insert_map[PostFader]);
1101 /* recompute max outs of any redirect */
1105 redirect_max_outs = 0;
1106 RedirectList::iterator prev = _redirects.end();
1108 for (r = _redirects.begin(); r != _redirects.end(); prev = r, ++r) {
1111 if ((s = dynamic_cast<Send*> (*r)) != 0) {
1112 if (r == _redirects.begin()) {
1113 s->expect_inputs (n_inputs());
1115 s->expect_inputs ((*prev)->output_streams());
1119 redirect_max_outs = max ((*r)->output_streams (), redirect_max_outs);
1128 Route::apply_some_plugin_counts (list<InsertCount>& iclist)
1130 list<InsertCount>::iterator i;
1132 for (i = iclist.begin(); i != iclist.end(); ++i) {
1134 if ((*i).insert.configure_io ((*i).cnt, (*i).in, (*i).out)) {
1137 /* make sure that however many we have, they are all active */
1138 (*i).insert.activate ();
1145 Route::check_some_plugin_counts (list<InsertCount>& iclist, int32_t required_inputs, uint32_t* err_streams)
1147 list<InsertCount>::iterator i;
1149 for (i = iclist.begin(); i != iclist.end(); ++i) {
1151 if (((*i).cnt = (*i).insert.can_support_input_configuration (required_inputs)) < 0) {
1153 *err_streams = required_inputs;
1158 (*i).in = required_inputs;
1159 (*i).out = (*i).insert.compute_output_streams ((*i).cnt);
1161 required_inputs = (*i).out;
1168 Route::copy_redirects (const Route& other, Placement placement, uint32_t* err_streams)
1170 uint32_t old_rmo = redirect_max_outs;
1176 RedirectList to_be_deleted;
1179 Glib::RWLock::WriterLock lm (redirect_lock);
1180 RedirectList::iterator tmp;
1181 RedirectList the_copy;
1183 the_copy = _redirects;
1185 /* remove all relevant redirects */
1187 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ) {
1191 if ((*i)->placement() == placement) {
1192 to_be_deleted.push_back (*i);
1193 _redirects.erase (i);
1199 /* now copy the relevant ones from "other" */
1201 for (RedirectList::const_iterator i = other._redirects.begin(); i != other._redirects.end(); ++i) {
1202 if ((*i)->placement() == placement) {
1203 _redirects.push_back (Redirect::clone (**i));
1207 /* reset plugin stream handling */
1209 if (_reset_plugin_counts (err_streams)) {
1211 /* FAILED COPY ATTEMPT: we have to restore order */
1213 /* delete all cloned redirects */
1215 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ) {
1220 if ((*i)->placement() == placement) {
1222 _redirects.erase (i);
1228 /* restore the natural order */
1230 _redirects = the_copy;
1231 redirect_max_outs = old_rmo;
1233 /* we failed, even though things are OK again */
1239 /* SUCCESSFUL COPY ATTEMPT: delete the redirects we removed pre-copy */
1241 for (RedirectList::iterator i = to_be_deleted.begin(); i != to_be_deleted.end(); ++i) {
1247 if (redirect_max_outs != old_rmo || old_rmo == 0) {
1251 redirects_changed (this); /* EMIT SIGNAL */
1256 Route::all_redirects_flip ()
1258 Glib::RWLock::ReaderLock lm (redirect_lock);
1260 if (_redirects.empty()) {
1264 bool first_is_on = _redirects.front()->active();
1266 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
1267 (*i)->set_active (!first_is_on, this);
1272 Route::all_redirects_active (bool state)
1274 Glib::RWLock::ReaderLock lm (redirect_lock);
1276 if (_redirects.empty()) {
1280 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
1281 (*i)->set_active (state, this);
1285 struct RedirectSorter {
1286 bool operator() (const Redirect *a, const Redirect *b) {
1287 return a->sort_key() < b->sort_key();
1292 Route::sort_redirects (uint32_t* err_streams)
1295 RedirectSorter comparator;
1296 Glib::RWLock::WriterLock lm (redirect_lock);
1297 uint32_t old_rmo = redirect_max_outs;
1299 /* the sweet power of C++ ... */
1301 RedirectList as_it_was_before = _redirects;
1303 _redirects.sort (comparator);
1305 if (_reset_plugin_counts (err_streams)) {
1306 _redirects = as_it_was_before;
1307 redirect_max_outs = old_rmo;
1313 redirects_changed (this); /* EMIT SIGNAL */
1325 Route::get_template()
1327 return state(false);
1331 Route::state(bool full_state)
1333 XMLNode *node = new XMLNode("Route");
1335 RedirectList:: iterator i;
1339 snprintf (buf, sizeof (buf), "0x%x", _flags);
1340 node->add_property("flags", buf);
1342 node->add_property("active", _active?"yes":"no");
1343 node->add_property("muted", _muted?"yes":"no");
1344 node->add_property("soloed", _soloed?"yes":"no");
1345 node->add_property("phase-invert", _phase_invert?"yes":"no");
1346 node->add_property("mute-affects-pre-fader", _mute_affects_pre_fader?"yes":"no");
1347 node->add_property("mute-affects-post-fader", _mute_affects_post_fader?"yes":"no");
1348 node->add_property("mute-affects-control-outs", _mute_affects_control_outs?"yes":"no");
1349 node->add_property("mute-affects-main-outs", _mute_affects_main_outs?"yes":"no");
1352 node->add_property("edit-group", _edit_group->name());
1355 node->add_property("mix-group", _mix_group->name());
1358 string order_string;
1359 OrderKeys::iterator x = order_keys.begin();
1361 while (x != order_keys.end()) {
1362 order_string += (*x).first;
1363 order_string += '=';
1364 snprintf (buf, sizeof(buf), "%ld", (*x).second);
1365 order_string += buf;
1369 if (x == order_keys.end()) {
1373 order_string += ':';
1375 node->add_property ("order-keys", order_string);
1377 node->add_child_nocopy (IO::state (full_state));
1379 if (_control_outs) {
1380 XMLNode* cnode = new XMLNode (X_("ControlOuts"));
1381 cnode->add_child_nocopy (_control_outs->state (full_state));
1382 node->add_child_nocopy (*cnode);
1385 if (_comment.length()) {
1386 XMLNode *cmt = node->add_child ("Comment");
1387 cmt->add_content (_comment);
1393 path = _session.snap_name();
1395 path += legalize_for_path (_name);
1396 path += ".automation";
1398 /* XXX we didn't ask for a state save, we asked for the current state.
1402 if (save_automation (path)) {
1403 error << _("Could not get state of route. Problem with save_automation") << endmsg;
1406 aevents = node->add_child ("Automation");
1407 aevents->add_property ("path", path);
1410 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
1411 node->add_child_nocopy((*i)->state (full_state));
1415 node->add_child_copy (*_extra_xml);
1422 Route::set_deferred_state ()
1425 XMLNodeConstIterator niter;
1427 if (!deferred_state) {
1431 nlist = deferred_state->children();
1433 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
1434 add_redirect_from_xml (**niter);
1437 delete deferred_state;
1442 Route::add_redirect_from_xml (const XMLNode& node)
1444 const XMLProperty *prop;
1448 if (node.name() == "Send") {
1451 send = new Send (_session, node);
1454 catch (failed_constructor &err) {
1455 error << _("Send construction failed") << endmsg;
1459 add_redirect (send, this);
1461 } else if (node.name() == "Insert") {
1464 if ((prop = node.property ("type")) != 0) {
1466 if (prop->value() == "ladspa" || prop->value() == "Ladspa" || prop->value() == "vst") {
1468 insert = new PluginInsert(_session, node);
1470 } else if (prop->value() == "port") {
1473 insert = new PortInsert (_session, node);
1477 error << string_compose(_("unknown Insert type \"%1\"; ignored"), prop->value()) << endmsg;
1480 add_redirect (insert, this);
1483 error << _("Insert XML node has no type property") << endmsg;
1487 catch (failed_constructor &err) {
1488 warning << _("insert could not be created. Ignored.") << endmsg;
1495 Route::set_state (const XMLNode& node)
1498 XMLNodeConstIterator niter;
1500 XMLPropertyList plist;
1501 const XMLProperty *prop;
1503 if (node.name() != "Route"){
1504 error << string_compose(_("Bad node sent to Route::set_state() [%1]"), node.name()) << endmsg;
1508 if ((prop = node.property ("flags")) != 0) {
1510 sscanf (prop->value().c_str(), "0x%x", &x);
1516 if ((prop = node.property ("phase-invert")) != 0) {
1517 set_phase_invert(prop->value()=="yes"?true:false, this);
1520 if ((prop = node.property ("active")) != 0) {
1521 set_active (prop->value() == "yes");
1524 if ((prop = node.property ("muted")) != 0) {
1525 bool yn = prop->value()=="yes"?true:false;
1527 /* force reset of mute status */
1531 mute_gain = desired_mute_gain;
1534 if ((prop = node.property ("soloed")) != 0) {
1535 bool yn = prop->value()=="yes"?true:false;
1537 /* force reset of solo status */
1540 set_solo (yn, this);
1541 solo_gain = desired_solo_gain;
1544 if ((prop = node.property ("mute-affects-pre-fader")) != 0) {
1545 _mute_affects_pre_fader = (prop->value()=="yes")?true:false;
1548 if ((prop = node.property ("mute-affects-post-fader")) != 0) {
1549 _mute_affects_post_fader = (prop->value()=="yes")?true:false;
1552 if ((prop = node.property ("mute-affects-control-outs")) != 0) {
1553 _mute_affects_control_outs = (prop->value()=="yes")?true:false;
1556 if ((prop = node.property ("mute-affects-main-outs")) != 0) {
1557 _mute_affects_main_outs = (prop->value()=="yes")?true:false;
1560 if ((prop = node.property ("edit-group")) != 0) {
1561 RouteGroup* edit_group = _session.edit_group_by_name(prop->value());
1562 if(edit_group == 0) {
1563 error << string_compose(_("Route %1: unknown edit group \"%2 in saved state (ignored)"), _name, prop->value()) << endmsg;
1565 set_edit_group(edit_group, this);
1569 if ((prop = node.property ("order-keys")) != 0) {
1573 string::size_type colon, equal;
1574 string remaining = prop->value();
1576 while (remaining.length()) {
1578 if ((equal = remaining.find_first_of ('=')) == string::npos || equal == remaining.length()) {
1579 error << string_compose (_("badly formed order key string in state file! [%1] ... ignored."), remaining)
1582 if (sscanf (remaining.substr (equal+1).c_str(), "%ld", &n) != 1) {
1583 error << string_compose (_("badly formed order key string in state file! [%1] ... ignored."), remaining)
1586 set_order_key (remaining.substr (0, equal), n);
1590 colon = remaining.find_first_of (':');
1592 if (colon != string::npos) {
1593 remaining = remaining.substr (colon+1);
1600 nlist = node.children();
1602 if (deferred_state) {
1603 delete deferred_state;
1606 deferred_state = new XMLNode("deferred state");
1608 /* set parent class properties before anything else */
1610 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
1614 if (child->name() == IO::state_node_name) {
1616 IO::set_state (*child);
1621 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
1625 if (child->name() == "Send") {
1628 if (!IO::ports_legal) {
1630 deferred_state->add_child_copy (*child);
1633 add_redirect_from_xml (*child);
1636 } else if (child->name() == "Insert") {
1638 if (!IO::ports_legal) {
1640 deferred_state->add_child_copy (*child);
1644 add_redirect_from_xml (*child);
1647 } else if (child->name() == "Automation") {
1649 XMLPropertyList plist;
1650 XMLPropertyConstIterator piter;
1653 plist = child->properties();
1654 for (piter = plist.begin(); piter != plist.end(); ++piter) {
1656 if (prop->name() == "path") {
1657 load_automation (prop->value());
1661 } else if (child->name() == "ControlOuts") {
1663 string coutname = _name;
1664 coutname += _("[control]");
1666 _control_outs = new IO (_session, coutname);
1667 _control_outs->set_state (**(child->children().begin()));
1669 } else if (child->name() == "Comment") {
1671 /* XXX this is a terrible API design in libxml++ */
1673 XMLNode *cmt = *(child->children().begin());
1674 _comment = cmt->content();
1676 } else if (child->name() == "extra") {
1677 _extra_xml = new XMLNode (*child);
1681 if ((prop = node.property ("mix-group")) != 0) {
1682 RouteGroup* mix_group = _session.mix_group_by_name(prop->value());
1683 if (mix_group == 0) {
1684 error << string_compose(_("Route %1: unknown mix group \"%2 in saved state (ignored)"), _name, prop->value()) << endmsg;
1686 set_mix_group(mix_group, this);
1694 Route::curve_reallocate ()
1696 // _gain_automation_curve.finish_resize ();
1697 // _pan_automation_curve.finish_resize ();
1701 Route::silence (jack_nframes_t nframes, jack_nframes_t offset)
1705 // reset_peak_meters ();
1707 IO::silence (nframes, offset);
1709 if (_control_outs) {
1710 _control_outs->silence (nframes, offset);
1714 Glib::RWLock::ReaderLock lm (redirect_lock, Glib::TRY_LOCK);
1717 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
1719 if (!_active && (pi = dynamic_cast<PluginInsert*> (*i)) != 0) {
1720 // skip plugins, they don't need anything when we're not active
1724 (*i)->silence (nframes, offset);
1727 if (nframes == _session.get_block_size() && offset == 0) {
1737 Route::set_control_outs (const vector<string>& ports)
1739 Glib::Mutex::Lock lm (control_outs_lock);
1740 vector<string>::const_iterator i;
1742 if (_control_outs) {
1743 delete _control_outs;
1747 if (ports.empty()) {
1751 string coutname = _name;
1752 coutname += _("[control]");
1754 _control_outs = new IO (_session, coutname);
1756 /* our control outs need as many outputs as we
1757 have outputs. we track the changes in ::output_change_handler().
1760 _control_outs->ensure_io (0, n_outputs(), true, this);
1766 Route::set_edit_group (RouteGroup *eg, void *src)
1769 if (eg == _edit_group) {
1774 _edit_group->remove (this);
1777 if ((_edit_group = eg) != 0) {
1778 _edit_group->add (this);
1781 _session.set_dirty ();
1782 edit_group_changed (src); /* EMIT SIGNAL */
1786 Route::drop_edit_group (void *src)
1789 _session.set_dirty ();
1790 edit_group_changed (src); /* EMIT SIGNAL */
1794 Route::set_mix_group (RouteGroup *mg, void *src)
1797 if (mg == _mix_group) {
1802 _mix_group->remove (this);
1805 if ((_mix_group = mg) != 0) {
1806 _mix_group->add (this);
1809 _session.set_dirty ();
1810 mix_group_changed (src); /* EMIT SIGNAL */
1814 Route::drop_mix_group (void *src)
1817 _session.set_dirty ();
1818 mix_group_changed (src); /* EMIT SIGNAL */
1822 Route::set_comment (string cmt, void *src)
1825 comment_changed (src);
1826 _session.set_dirty ();
1830 Route::feeds (Route *o)
1836 uint32_t no = self.n_outputs();
1837 uint32_t ni = other.n_inputs ();
1839 for (i = 0; i < no; ++i) {
1840 for (j = 0; j < ni; ++j) {
1841 if (self.output(i)->connected_to (other.input(j)->name())) {
1847 /* check Redirects which may also interconnect Routes */
1849 for (RedirectList::iterator r = _redirects.begin(); r != _redirects.end(); r++) {
1851 no = (*r)->n_outputs();
1853 for (i = 0; i < no; ++i) {
1854 for (j = 0; j < ni; ++j) {
1855 if ((*r)->output(i)->connected_to (other.input (j)->name())) {
1862 /* check for control room outputs which may also interconnect Routes */
1864 if (_control_outs) {
1866 no = _control_outs->n_outputs();
1868 for (i = 0; i < no; ++i) {
1869 for (j = 0; j < ni; ++j) {
1870 if (_control_outs->output(i)->connected_to (other.input (j)->name())) {
1881 Route::set_mute_config (mute_type t, bool onoff, void *src)
1885 _mute_affects_pre_fader = onoff;
1886 pre_fader_changed(src); /* EMIT SIGNAL */
1890 _mute_affects_post_fader = onoff;
1891 post_fader_changed(src); /* EMIT SIGNAL */
1895 _mute_affects_control_outs = onoff;
1896 control_outs_changed(src); /* EMIT SIGNAL */
1900 _mute_affects_main_outs = onoff;
1901 main_outs_changed(src); /* EMIT SIGNAL */
1907 Route::get_mute_config (mute_type t)
1913 onoff = _mute_affects_pre_fader;
1916 onoff = _mute_affects_post_fader;
1919 onoff = _mute_affects_control_outs;
1922 onoff = _mute_affects_main_outs;
1930 Route::set_active (bool yn)
1933 active_changed(); /* EMIT SIGNAL */
1937 Route::handle_transport_stopped (bool abort_ignored, bool did_locate, bool can_flush_redirects)
1939 jack_nframes_t now = _session.transport_frame();
1942 Glib::RWLock::ReaderLock lm (redirect_lock);
1945 automation_snapshot (now);
1948 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
1950 if (Config->get_plugins_stop_with_transport() && can_flush_redirects) {
1951 (*i)->deactivate ();
1955 (*i)->transport_stopped (now);
1959 IO::transport_stopped (now);
1961 _roll_delay = _initial_delay;
1965 Route::get_memento() const
1967 void (Route::*pmf)(state_id_t) = &Route::set_state;
1968 return sigc::bind (mem_fun (*(const_cast<Route *>(this)), pmf), _current_state_id);
1972 Route::set_state (state_id_t id)
1978 Route::input_change_handler (IOChange change, void *ignored)
1980 if (change & ConfigurationChanged) {
1981 reset_plugin_counts (0);
1986 Route::output_change_handler (IOChange change, void *ignored)
1988 if (change & ConfigurationChanged) {
1989 if (_control_outs) {
1990 _control_outs->ensure_io (0, n_outputs(), true, this);
1993 reset_plugin_counts (0);
1998 Route::pans_required () const
2000 if (n_outputs() < 2) {
2004 return max (n_inputs (), redirect_max_outs);
2008 Route::no_roll (jack_nframes_t nframes, jack_nframes_t start_frame, jack_nframes_t end_frame, jack_nframes_t offset,
2009 bool session_state_changing, bool can_record, bool rec_monitors_input)
2011 if (n_outputs() == 0) {
2015 if (session_state_changing || !_active) {
2016 silence (nframes, offset);
2020 apply_gain_automation = false;
2023 passthru (start_frame, end_frame, nframes, offset, 0, false);
2025 silence (nframes, offset);
2032 Route::check_initial_delay (jack_nframes_t nframes, jack_nframes_t& offset, jack_nframes_t& transport_frame)
2034 if (_roll_delay > nframes) {
2036 _roll_delay -= nframes;
2037 silence (nframes, offset);
2038 /* transport frame is not legal for caller to use */
2041 } else if (_roll_delay > 0) {
2043 nframes -= _roll_delay;
2045 silence (_roll_delay, offset);
2047 offset += _roll_delay;
2048 transport_frame += _roll_delay;
2057 Route::roll (jack_nframes_t nframes, jack_nframes_t start_frame, jack_nframes_t end_frame, jack_nframes_t offset, int declick,
2058 bool can_record, bool rec_monitors_input)
2061 Glib::RWLock::ReaderLock lm (redirect_lock, Glib::TRY_LOCK);
2063 // automation snapshot can also be called from the non-rt context
2064 // and it uses the redirect list, so we take the lock out here
2065 automation_snapshot (_session.transport_frame());
2069 if ((n_outputs() == 0 && _redirects.empty()) || n_inputs() == 0 || !_active) {
2070 silence (nframes, offset);
2074 jack_nframes_t unused = 0;
2076 if ((nframes = check_initial_delay (nframes, offset, unused)) == 0) {
2082 apply_gain_automation = false;
2085 Glib::Mutex::Lock am (automation_lock, Glib::TRY_LOCK);
2087 if (am.locked() && _session.transport_rolling()) {
2089 jack_nframes_t start_frame = end_frame - nframes;
2091 if (gain_automation_playback()) {
2092 apply_gain_automation = _gain_automation_curve.rt_safe_get_vector (start_frame, end_frame, _session.gain_automation_buffer(), nframes);
2097 passthru (start_frame, end_frame, nframes, offset, declick, false);
2103 Route::silent_roll (jack_nframes_t nframes, jack_nframes_t start_frame, jack_nframes_t end_frame, jack_nframes_t offset,
2104 bool can_record, bool rec_monitors_input)
2106 silence (nframes, offset);
2111 Route::toggle_monitor_input ()
2113 for (vector<Port*>::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
2114 (*i)->request_monitor_input(!(*i)->monitoring_input());
2119 Route::has_external_redirects () const
2121 const PortInsert* pi;
2123 for (RedirectList::const_iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2124 if ((pi = dynamic_cast<const PortInsert*>(*i)) != 0) {
2126 uint32_t no = pi->n_outputs();
2128 for (uint32_t n = 0; n < no; ++n) {
2130 string port_name = pi->output(n)->name();
2131 string client_name = port_name.substr (0, port_name.find(':'));
2133 /* only say "yes" if the redirect is actually in use */
2135 if (client_name != "ardour" && pi->active()) {
2146 Route::flush_redirects ()
2148 /* XXX shouldn't really try to take this lock, since
2149 this is called from the RT audio thread.
2152 Glib::RWLock::ReaderLock lm (redirect_lock);
2154 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2155 (*i)->deactivate ();
2161 Route::set_meter_point (MeterPoint p, void *src)
2163 if (_meter_point != p) {
2165 meter_change (src); /* EMIT SIGNAL */
2166 _session.set_dirty ();
2171 Route::update_total_latency ()
2175 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2176 if ((*i)->active ()) {
2177 _own_latency += (*i)->latency ();
2181 set_port_latency (_own_latency);
2183 /* this (virtual) function is used for pure Routes,
2184 not derived classes like AudioTrack. this means
2185 that the data processed here comes from an input
2186 port, not prerecorded material, and therefore we
2187 have to take into account any input latency.
2190 _own_latency += input_latency ();
2192 return _own_latency;
2196 Route::set_latency_delay (jack_nframes_t longest_session_latency)
2198 _initial_delay = longest_session_latency - _own_latency;
2200 if (_session.transport_stopped()) {
2201 _roll_delay = _initial_delay;
2206 Route::automation_snapshot (jack_nframes_t now)
2208 IO::automation_snapshot (now);
2210 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2211 (*i)->automation_snapshot (now);
2215 Route::ToggleControllable::ToggleControllable (Route& s, ToggleType tp)
2216 : route (s), type(tp)
2222 Route::ToggleControllable::set_value (float val)
2224 bool bval = ((val >= 0.5f) ? true: false);
2228 route.set_mute (bval, this);
2231 route.set_solo (bval, this);
2239 Route::ToggleControllable::get_value (void) const
2245 val = route.muted() ? 1.0f : 0.0f;
2248 val = route.soloed() ? 1.0f : 0.0f;
2258 Route::set_block_size (jack_nframes_t nframes)
2260 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2261 (*i)->set_block_size (nframes);
2266 Route::redirect_active_proxy (Redirect* ignored, void* ignored_src)
2268 _session.update_latency_compensation (false, false);
2272 Route::protect_automation ()
2274 switch (gain_automation_state()) {
2277 set_gain_automation_state (Off);
2283 switch (panner().automation_state ()) {
2286 panner().set_automation_state (Off);
2292 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2294 if ((pi = dynamic_cast<PluginInsert*> (*i)) != 0) {
2295 pi->protect_automation ();
2301 Route::set_pending_declick (int declick)
2304 /* this call is not allowed to turn off a pending declick unless "force" is true */
2306 _pending_declick = declick;
2308 // cerr << _name << ": after setting to " << declick << " pending declick = " << _pending_declick << endl;
2310 _pending_declick = 0;