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/insert.h>
32 #include <ardour/send.h>
33 #include <ardour/session.h>
34 #include <ardour/utils.h>
35 #include <ardour/configuration.h>
36 #include <ardour/cycle_timer.h>
37 #include <ardour/route_group.h>
38 #include <ardour/port.h>
39 #include <ardour/ladspa_plugin.h>
40 #include <ardour/panner.h>
41 #include <ardour/dB.h>
42 #include <ardour/mix.h>
47 using namespace ARDOUR;
51 uint32_t Route::order_key_cnt = 0;
54 Route::Route (Session& sess, string name, int input_min, int input_max, int output_min, int output_max, Flag flg, Buffer::Type default_type)
55 : IO (sess, name, input_min, input_max, output_min, output_max, default_type),
57 _solo_control (*this, ToggleControllable::SoloControl),
58 _mute_control (*this, ToggleControllable::MuteControl)
63 Route::Route (Session& sess, const XMLNode& node)
65 _solo_control (*this, ToggleControllable::SoloControl),
66 _mute_control (*this, ToggleControllable::MuteControl)
75 redirect_max_outs = 0;
79 _phase_invert = false;
80 order_keys[N_("signal")] = order_key_cnt++;
83 _meter_point = MeterPostFader;
87 _have_internal_generator = false;
89 _pending_declick = true;
90 _remote_control_id = 0;
95 _mute_affects_pre_fader = Config->get_mute_affects_pre_fader();
96 _mute_affects_post_fader = Config->get_mute_affects_post_fader();
97 _mute_affects_control_outs = Config->get_mute_affects_control_outs();
98 _mute_affects_main_outs = Config->get_mute_affects_main_outs();
101 desired_solo_gain = 1.0;
103 desired_mute_gain = 1.0;
107 input_changed.connect (mem_fun (this, &Route::input_change_handler));
108 output_changed.connect (mem_fun (this, &Route::output_change_handler));
113 GoingAway (); /* EMIT SIGNAL */
114 clear_redirects (this);
117 delete _control_outs;
122 Route::set_remote_control_id (uint32_t id)
124 if (id != _remote_control_id) {
125 _remote_control_id = id;
126 RemoteControlIDChanged ();
131 Route::remote_control_id() const
133 return _remote_control_id;
137 Route::order_key (string name) const
139 OrderKeys::const_iterator i;
141 if ((i = order_keys.find (name)) == order_keys.end()) {
149 Route::set_order_key (string name, long n)
151 order_keys[name] = n;
152 _session.set_dirty ();
156 Route::inc_gain (gain_t fraction, void *src)
158 IO::inc_gain (fraction, src);
162 Route::set_gain (gain_t val, void *src)
164 if (src != 0 && _mix_group && src != _mix_group && _mix_group->is_active()) {
166 if (_mix_group->is_relative()) {
169 gain_t usable_gain = gain();
170 if (usable_gain < 0.000001f) {
171 usable_gain=0.000001f;
175 if (delta < 0.000001f) {
179 delta -= usable_gain;
181 if (delta == 0.0f) return;
183 gain_t factor = delta / usable_gain;
186 factor = _mix_group->get_max_factor(factor);
187 if (factor == 0.0f) {
192 factor = _mix_group->get_min_factor(factor);
193 if (factor == 0.0f) {
199 _mix_group->apply (&Route::inc_gain, factor, _mix_group);
203 _mix_group->apply (&Route::set_gain, val, _mix_group);
213 IO::set_gain (val, src);
217 Route::process_output_buffers (vector<Sample*>& bufs, uint32_t nbufs,
218 jack_nframes_t start_frame, jack_nframes_t end_frame,
219 jack_nframes_t nframes, jack_nframes_t offset, bool with_redirects, int declick,
223 RedirectList::iterator i;
224 bool post_fader_work = false;
225 bool mute_declick_applied = false;
227 vector<Sample*>::iterator bufiter;
231 bool no_monitor = (Config->get_use_hardware_monitoring() || !Config->get_use_sw_monitoring ());
232 gain_t* gab = _session.gain_automation_buffer();
234 declick = _pending_declick;
237 Glib::Mutex::Lock cm (control_outs_lock, Glib::TRY_LOCK);
247 Glib::Mutex::Lock dm (declick_lock, Glib::TRY_LOCK);
250 dmg = desired_mute_gain;
251 dsg = desired_solo_gain;
260 /* ----------------------------------------------------------------------------------------------------
261 GLOBAL DECLICK (for transport changes etc.)
262 -------------------------------------------------------------------------------------------------- */
265 apply_declick (bufs, nbufs, nframes, 0.0, 1.0, _phase_invert);
266 _pending_declick = 0;
267 } else if (declick < 0) {
268 apply_declick (bufs, nbufs, nframes, 1.0, 0.0, _phase_invert);
269 _pending_declick = 0;
272 /* no global declick */
274 if (solo_gain != dsg) {
275 apply_declick (bufs, nbufs, nframes, solo_gain, dsg, _phase_invert);
281 /* ----------------------------------------------------------------------------------------------------
282 INPUT METERING & MONITORING
283 -------------------------------------------------------------------------------------------------- */
285 if (meter && (_meter_point == MeterInput)) {
286 for (n = 0; n < nbufs; ++n) {
287 _peak_power[n] = Session::compute_peak (bufs[n], nframes, _peak_power[n]);
291 if (!_soloed && _mute_affects_pre_fader && (mute_gain != dmg)) {
292 apply_declick (bufs, nbufs, nframes, mute_gain, dmg, _phase_invert);
294 mute_declick_applied = true;
297 if ((_meter_point == MeterInput) && co) {
299 solo_audible = dsg > 0;
300 mute_audible = dmg > 0;// || !_mute_affects_pre_fader;
302 if ( // muted by solo of another track
306 // muted by mute of this track
310 // rec-enabled but not s/w monitoring
312 // TODO: this is probably wrong
314 (no_monitor && record_enabled() && (!_session.get_auto_input() || _session.actively_recording()))
318 co->silence (nframes, offset);
322 co->deliver_output (bufs, nbufs, nframes, offset);
327 /* ----------------------------------------------------------------------------------------------------
329 -------------------------------------------------------------------------------------------------- */
331 if (with_redirects) {
332 Glib::RWLock::ReaderLock rm (redirect_lock, Glib::TRY_LOCK);
334 if (mute_gain > 0 || !_mute_affects_pre_fader) {
335 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
336 switch ((*i)->placement()) {
338 (*i)->run (bufs, nbufs, nframes, offset);
341 post_fader_work = true;
346 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
347 switch ((*i)->placement()) {
349 (*i)->silence (nframes, offset);
352 post_fader_work = true;
361 if (!_soloed && (mute_gain != dmg) && !mute_declick_applied && _mute_affects_post_fader) {
362 apply_declick (bufs, nbufs, nframes, mute_gain, dmg, _phase_invert);
364 mute_declick_applied = true;
367 /* ----------------------------------------------------------------------------------------------------
368 PRE-FADER METERING & MONITORING
369 -------------------------------------------------------------------------------------------------- */
371 if (meter && (_meter_point == MeterPreFader)) {
372 for (n = 0; n < nbufs; ++n) {
373 _peak_power[n] = Session::compute_peak (bufs[n], nframes, _peak_power[n]);
378 if ((_meter_point == MeterPreFader) && co) {
380 solo_audible = dsg > 0;
381 mute_audible = dmg > 0 || !_mute_affects_pre_fader;
383 if ( // muted by solo of another track
387 // muted by mute of this track
391 // rec-enabled but not s/w monitoring
393 (no_monitor && record_enabled() && (!_session.get_auto_input() || _session.actively_recording()))
397 co->silence (nframes, offset);
401 co->deliver_output (bufs, nbufs, nframes, offset);
406 /* ----------------------------------------------------------------------------------------------------
408 -------------------------------------------------------------------------------------------------- */
410 /* if not recording or recording and requiring any monitor signal, then apply gain */
412 if ( // not recording
414 !(record_enabled() && _session.actively_recording()) ||
418 // h/w monitoring not in use
420 (!Config->get_use_hardware_monitoring() &&
422 // AND software monitoring required
424 Config->get_use_sw_monitoring())) {
426 if (apply_gain_automation) {
429 for (n = 0; n < nbufs; ++n) {
430 Sample *sp = bufs[n];
432 for (jack_nframes_t nx = 0; nx < nframes; ++nx) {
437 for (n = 0; n < nbufs; ++n) {
438 Sample *sp = bufs[n];
440 for (jack_nframes_t nx = 0; nx < nframes; ++nx) {
446 if (apply_gain_automation && _session.transport_rolling() && nframes > 0) {
447 _effective_gain = gab[nframes-1];
452 /* manual (scalar) gain */
456 apply_declick (bufs, nbufs, nframes, _gain, dg, _phase_invert);
459 } else if (_gain != 0 && (_phase_invert || _gain != 1.0)) {
461 /* no need to interpolate current gain value,
462 but its non-unity, so apply it. if the gain
463 is zero, do nothing because we'll ship silence
475 for (n = 0; n < nbufs; ++n) {
476 Sample *sp = bufs[n];
477 apply_gain_to_buffer(sp,nframes,this_gain);
480 } else if (_gain == 0) {
481 for (n = 0; n < nbufs; ++n) {
482 memset (bufs[n], 0, sizeof (Sample) * nframes);
489 /* actively recording, no monitoring required; leave buffers as-is to save CPU cycles */
493 /* ----------------------------------------------------------------------------------------------------
495 -------------------------------------------------------------------------------------------------- */
497 /* note that post_fader_work cannot be true unless with_redirects was also true, so don't test both */
499 if (post_fader_work) {
501 Glib::RWLock::ReaderLock rm (redirect_lock, Glib::TRY_LOCK);
503 if (mute_gain > 0 || !_mute_affects_post_fader) {
504 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
505 switch ((*i)->placement()) {
509 (*i)->run (bufs, nbufs, nframes, offset);
514 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
515 switch ((*i)->placement()) {
519 (*i)->silence (nframes, offset);
527 if (!_soloed && (mute_gain != dmg) && !mute_declick_applied && _mute_affects_control_outs) {
528 apply_declick (bufs, nbufs, nframes, mute_gain, dmg, _phase_invert);
530 mute_declick_applied = true;
533 /* ----------------------------------------------------------------------------------------------------
535 -------------------------------------------------------------------------------------------------- */
537 if ((_meter_point == MeterPostFader) && co) {
539 solo_audible = solo_gain > 0;
540 mute_audible = dmg > 0 || !_mute_affects_control_outs;
542 if ( // silent anyway
544 (_gain == 0 && !apply_gain_automation) ||
546 // muted by solo of another track
550 // muted by mute of this track
554 // recording but not s/w monitoring
556 (no_monitor && record_enabled() && (!_session.get_auto_input() || _session.actively_recording()))
560 co->silence (nframes, offset);
564 co->deliver_output_no_pan (bufs, nbufs, nframes, offset);
568 /* ----------------------------------------------------------------------
570 ----------------------------------------------------------------------*/
572 if (!_soloed && (mute_gain != dmg) && !mute_declick_applied && _mute_affects_main_outs) {
573 apply_declick (bufs, nbufs, nframes, mute_gain, dmg, _phase_invert);
575 mute_declick_applied = true;
578 /* ----------------------------------------------------------------------------------------------------
580 -------------------------------------------------------------------------------------------------- */
582 solo_audible = dsg > 0;
583 mute_audible = dmg > 0 || !_mute_affects_main_outs;
585 if (n_outputs() == 0) {
589 } else if (no_monitor && record_enabled() && (!_session.get_auto_input() || _session.actively_recording())) {
591 IO::silence (nframes, offset);
595 if ( // silent anyway
597 (_gain == 0 && !apply_gain_automation) ||
599 // muted by solo of another track, but not using control outs for solo
601 (!solo_audible && (_session.solo_model() != Session::SoloBus)) ||
603 // muted by mute of this track
609 /* don't use Route::silence() here, because that causes
610 all outputs (sends, port inserts, etc. to be silent).
613 if (_meter_point == MeterPostFader) {
614 reset_peak_meters ();
617 IO::silence (nframes, offset);
621 if (_session.transport_speed() > 1.5f || _session.transport_speed() < -1.5f) {
622 pan (bufs, nbufs, nframes, offset, speed_quietning);
624 // cerr << _name << " panner state = " << _panner->automation_state() << endl;
625 if (!_panner->empty() &&
626 (_panner->automation_state() & Play ||
627 ((_panner->automation_state() & Touch) && !_panner->touching()))) {
628 pan_automated (bufs, nbufs, start_frame, end_frame, nframes, offset);
630 pan (bufs, nbufs, nframes, offset, 1.0);
637 /* ----------------------------------------------------------------------------------------------------
639 -------------------------------------------------------------------------------------------------- */
641 if (meter && (_meter_point == MeterPostFader)) {
642 // cerr << "meter post" << endl;
644 if ((_gain == 0 && !apply_gain_automation) || dmg == 0) {
645 uint32_t no = n_outputs();
646 for (n = 0; n < no; ++n) {
650 uint32_t no = n_outputs();
651 for (n = 0; n < no; ++n) {
652 _peak_power[n] = Session::compute_peak (output(n)->get_buffer (nframes) + offset, nframes, _peak_power[n]);
659 Route::n_process_buffers ()
661 return max (n_inputs(), redirect_max_outs);
666 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)
668 vector<Sample*>& bufs = _session.get_passthru_buffers();
669 uint32_t limit = n_process_buffers ();
673 collect_input (bufs, limit, nframes, offset);
675 #define meter_stream meter_first
678 for (uint32_t n = 0; n < limit; ++n) {
679 _peak_power[n] = Session::compute_peak (bufs[n], nframes, _peak_power[n]);
681 meter_stream = false;
686 process_output_buffers (bufs, limit, start_frame, end_frame, nframes, offset, true, declick, meter_stream);
692 Route::set_phase_invert (bool yn, void *src)
694 if (_phase_invert != yn) {
697 // phase_invert_changed (src); /* EMIT SIGNAL */
701 Route::set_solo (bool yn, void *src)
707 if (_mix_group && src != _mix_group && _mix_group->is_active()) {
708 _mix_group->apply (&Route::set_solo, yn, _mix_group);
714 solo_changed (src); /* EMIT SIGNAL */
715 _solo_control.Changed (); /* EMIT SIGNAL */
720 Route::set_solo_mute (bool yn)
722 Glib::Mutex::Lock lm (declick_lock);
724 /* Called by Session in response to another Route being soloed.
727 desired_solo_gain = (yn?0.0:1.0);
731 Route::set_solo_safe (bool yn, void *src)
733 if (_solo_safe != yn) {
735 solo_safe_changed (src); /* EMIT SIGNAL */
740 Route::set_mute (bool yn, void *src)
743 if (_mix_group && src != _mix_group && _mix_group->is_active()) {
744 _mix_group->apply (&Route::set_mute, yn, _mix_group);
750 mute_changed (src); /* EMIT SIGNAL */
752 _mute_control.Changed (); /* EMIT SIGNAL */
754 Glib::Mutex::Lock lm (declick_lock);
755 desired_mute_gain = (yn?0.0f:1.0f);
760 Route::add_redirect (Redirect *redirect, void *src, uint32_t* err_streams)
762 uint32_t old_rmo = redirect_max_outs;
764 if (!_session.engine().connected()) {
769 Glib::RWLock::WriterLock lm (redirect_lock);
774 uint32_t potential_max_streams = 0;
776 if ((pi = dynamic_cast<PluginInsert*>(redirect)) != 0) {
779 if (pi->input_streams() == 0) {
780 /* instrument plugin */
781 _have_internal_generator = true;
784 potential_max_streams = max(pi->input_streams(), pi->output_streams());
786 } else if ((porti = dynamic_cast<PortInsert*>(redirect)) != 0) {
788 /* force new port inserts to start out with an i/o configuration
789 that matches this route's i/o configuration.
791 the "inputs" for the port are supposed to match the output
794 the "outputs" of the route should match the inputs of this
795 route. XXX shouldn't they match the number of active signal
796 streams at the point of insertion?
800 porti->ensure_io (n_outputs (), n_inputs(), false, this);
803 // Ensure peak vector sizes before the plugin is activated
804 while (_peak_power.size() < potential_max_streams) {
805 _peak_power.push_back(0);
807 while (_visible_peak_power.size() < potential_max_streams) {
808 _visible_peak_power.push_back(0);
811 _redirects.push_back (redirect);
813 if (_reset_plugin_counts (err_streams)) {
814 _redirects.pop_back ();
815 _reset_plugin_counts (0); // it worked before we tried to add it ...
819 redirect->activate ();
820 redirect->active_changed.connect (mem_fun (*this, &Route::redirect_active_proxy));
823 if (redirect_max_outs != old_rmo || old_rmo == 0) {
828 redirects_changed (src); /* EMIT SIGNAL */
833 Route::add_redirects (const RedirectList& others, void *src, uint32_t* err_streams)
835 uint32_t old_rmo = redirect_max_outs;
837 if (!_session.engine().connected()) {
842 Glib::RWLock::WriterLock lm (redirect_lock);
844 RedirectList::iterator existing_end = _redirects.end();
847 uint32_t potential_max_streams = 0;
849 for (RedirectList::const_iterator i = others.begin(); i != others.end(); ++i) {
853 if ((pi = dynamic_cast<PluginInsert*>(*i)) != 0) {
856 uint32_t m = max(pi->input_streams(), pi->output_streams());
857 if (m > potential_max_streams)
858 potential_max_streams = m;
861 // Ensure peak vector sizes before the plugin is activated
862 while (_peak_power.size() < potential_max_streams) {
863 _peak_power.push_back(0);
865 while (_visible_peak_power.size() < potential_max_streams) {
866 _visible_peak_power.push_back(0);
869 _redirects.push_back (*i);
871 if (_reset_plugin_counts (err_streams)) {
873 _redirects.erase (existing_end, _redirects.end());
874 _reset_plugin_counts (0); // it worked before we tried to add it ...
879 (*i)->active_changed.connect (mem_fun (*this, &Route::redirect_active_proxy));
883 if (redirect_max_outs != old_rmo || old_rmo == 0) {
887 redirects_changed (src); /* EMIT SIGNAL */
892 Route::clear_redirects (void *src)
894 uint32_t old_rmo = redirect_max_outs;
896 if (!_session.engine().connected()) {
901 Glib::RWLock::WriterLock lm (redirect_lock);
903 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
910 if (redirect_max_outs != old_rmo) {
914 redirect_max_outs = 0;
915 _have_internal_generator = false;
916 redirects_changed (src); /* EMIT SIGNAL */
920 Route::remove_redirect (Redirect *redirect, void *src, uint32_t* err_streams)
922 uint32_t old_rmo = redirect_max_outs;
924 if (!_session.engine().connected()) {
928 redirect_max_outs = 0;
931 Glib::RWLock::WriterLock lm (redirect_lock);
932 RedirectList::iterator i;
933 bool removed = false;
935 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
936 if (*i == redirect) {
938 RedirectList::iterator tmp;
940 /* move along, see failure case for reset_plugin_counts()
941 where we may need to reinsert the redirect.
947 /* stop redirects that send signals to JACK ports
948 from causing noise as a result of no longer being
953 PortInsert* port_insert;
955 if ((send = dynamic_cast<Send*> (*i)) != 0) {
956 send->disconnect_inputs (this);
957 send->disconnect_outputs (this);
958 } else if ((port_insert = dynamic_cast<PortInsert*> (*i)) != 0) {
959 port_insert->disconnect_inputs (this);
960 port_insert->disconnect_outputs (this);
963 _redirects.erase (i);
976 if (_reset_plugin_counts (err_streams)) {
977 /* get back to where we where */
978 _redirects.insert (i, redirect);
979 /* we know this will work, because it worked before :) */
980 _reset_plugin_counts (0);
986 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
989 if ((pi = dynamic_cast<PluginInsert*>(*i)) != 0) {
990 if (pi->is_generator()) {
996 _have_internal_generator = foo;
999 if (old_rmo != redirect_max_outs) {
1003 redirects_changed (src); /* EMIT SIGNAL */
1008 Route::reset_plugin_counts (uint32_t* lpc)
1010 Glib::RWLock::WriterLock lm (redirect_lock);
1011 return _reset_plugin_counts (lpc);
1016 Route::_reset_plugin_counts (uint32_t* err_streams)
1018 RedirectList::iterator r;
1021 map<Placement,list<InsertCount> > insert_map;
1022 jack_nframes_t initial_streams;
1024 redirect_max_outs = 0;
1028 /* divide inserts up by placement so we get the signal flow
1029 properly modelled. we need to do this because the _redirects
1030 list is not sorted by placement, and because other reasons may
1031 exist now or in the future for this separate treatment.
1034 for (r = _redirects.begin(); r != _redirects.end(); ++r) {
1038 /* do this here in case we bomb out before we get to the end of
1042 redirect_max_outs = max ((*r)->output_streams (), redirect_max_outs);
1044 if ((insert = dynamic_cast<Insert*>(*r)) != 0) {
1046 insert_map[insert->placement()].push_back (InsertCount (*insert));
1048 /* reset plugin counts back to one for now so
1049 that we have a predictable, controlled
1050 state to try to configure.
1055 if ((pi = dynamic_cast<PluginInsert*>(insert)) != 0) {
1059 } else if (dynamic_cast<Send*> (*r) != 0) {
1072 /* Now process each placement in order, checking to see if we
1073 can really do what has been requested.
1078 if (check_some_plugin_counts (insert_map[PreFader], n_inputs (), err_streams)) {
1082 /* figure out the streams that will feed into PreFader */
1084 if (!insert_map[PreFader].empty()) {
1085 InsertCount& ic (insert_map[PreFader].back());
1086 initial_streams = ic.insert.compute_output_streams (ic.cnt);
1088 initial_streams = n_inputs ();
1093 if (check_some_plugin_counts (insert_map[PostFader], initial_streams, err_streams)) {
1097 /* OK, everything can be set up correctly, so lets do it */
1099 apply_some_plugin_counts (insert_map[PreFader]);
1100 apply_some_plugin_counts (insert_map[PostFader]);
1102 /* recompute max outs of any redirect */
1106 redirect_max_outs = 0;
1107 RedirectList::iterator prev = _redirects.end();
1109 for (r = _redirects.begin(); r != _redirects.end(); prev = r, ++r) {
1112 if ((s = dynamic_cast<Send*> (*r)) != 0) {
1113 if (r == _redirects.begin()) {
1114 s->expect_inputs (n_inputs());
1116 s->expect_inputs ((*prev)->output_streams());
1120 redirect_max_outs = max ((*r)->output_streams (), redirect_max_outs);
1129 Route::apply_some_plugin_counts (list<InsertCount>& iclist)
1131 list<InsertCount>::iterator i;
1133 for (i = iclist.begin(); i != iclist.end(); ++i) {
1135 if ((*i).insert.configure_io ((*i).cnt, (*i).in, (*i).out)) {
1138 /* make sure that however many we have, they are all active */
1139 (*i).insert.activate ();
1146 Route::check_some_plugin_counts (list<InsertCount>& iclist, int32_t required_inputs, uint32_t* err_streams)
1148 list<InsertCount>::iterator i;
1150 for (i = iclist.begin(); i != iclist.end(); ++i) {
1152 if (((*i).cnt = (*i).insert.can_support_input_configuration (required_inputs)) < 0) {
1154 *err_streams = required_inputs;
1159 (*i).in = required_inputs;
1160 (*i).out = (*i).insert.compute_output_streams ((*i).cnt);
1162 required_inputs = (*i).out;
1169 Route::copy_redirects (const Route& other, Placement placement, uint32_t* err_streams)
1171 uint32_t old_rmo = redirect_max_outs;
1177 RedirectList to_be_deleted;
1180 Glib::RWLock::WriterLock lm (redirect_lock);
1181 RedirectList::iterator tmp;
1182 RedirectList the_copy;
1184 the_copy = _redirects;
1186 /* remove all relevant redirects */
1188 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ) {
1192 if ((*i)->placement() == placement) {
1193 to_be_deleted.push_back (*i);
1194 _redirects.erase (i);
1200 /* now copy the relevant ones from "other" */
1202 for (RedirectList::const_iterator i = other._redirects.begin(); i != other._redirects.end(); ++i) {
1203 if ((*i)->placement() == placement) {
1204 _redirects.push_back (Redirect::clone (**i));
1208 /* reset plugin stream handling */
1210 if (_reset_plugin_counts (err_streams)) {
1212 /* FAILED COPY ATTEMPT: we have to restore order */
1214 /* delete all cloned redirects */
1216 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ) {
1221 if ((*i)->placement() == placement) {
1223 _redirects.erase (i);
1229 /* restore the natural order */
1231 _redirects = the_copy;
1232 redirect_max_outs = old_rmo;
1234 /* we failed, even though things are OK again */
1240 /* SUCCESSFUL COPY ATTEMPT: delete the redirects we removed pre-copy */
1242 for (RedirectList::iterator i = to_be_deleted.begin(); i != to_be_deleted.end(); ++i) {
1248 if (redirect_max_outs != old_rmo || old_rmo == 0) {
1252 redirects_changed (this); /* EMIT SIGNAL */
1257 Route::all_redirects_flip ()
1259 Glib::RWLock::ReaderLock lm (redirect_lock);
1261 if (_redirects.empty()) {
1265 bool first_is_on = _redirects.front()->active();
1267 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
1268 (*i)->set_active (!first_is_on, this);
1273 Route::all_redirects_active (bool state)
1275 Glib::RWLock::ReaderLock lm (redirect_lock);
1277 if (_redirects.empty()) {
1281 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
1282 (*i)->set_active (state, this);
1286 struct RedirectSorter {
1287 bool operator() (const Redirect *a, const Redirect *b) {
1288 return a->sort_key() < b->sort_key();
1293 Route::sort_redirects (uint32_t* err_streams)
1296 RedirectSorter comparator;
1297 Glib::RWLock::WriterLock lm (redirect_lock);
1298 uint32_t old_rmo = redirect_max_outs;
1300 /* the sweet power of C++ ... */
1302 RedirectList as_it_was_before = _redirects;
1304 _redirects.sort (comparator);
1306 if (_reset_plugin_counts (err_streams)) {
1307 _redirects = as_it_was_before;
1308 redirect_max_outs = old_rmo;
1314 redirects_changed (this); /* EMIT SIGNAL */
1326 Route::get_template()
1328 return state(false);
1332 Route::state(bool full_state)
1334 XMLNode *node = new XMLNode("Route");
1336 RedirectList:: iterator i;
1340 snprintf (buf, sizeof (buf), "0x%x", _flags);
1341 node->add_property("flags", buf);
1344 // FIXME: assumes there's only audio and MIDI types
1345 node->add_property("default-type", Buffer::type_to_string(_default_type));
1347 node->add_property("active", _active?"yes":"no");
1348 node->add_property("muted", _muted?"yes":"no");
1349 node->add_property("soloed", _soloed?"yes":"no");
1350 node->add_property("phase-invert", _phase_invert?"yes":"no");
1351 node->add_property("mute-affects-pre-fader", _mute_affects_pre_fader?"yes":"no");
1352 node->add_property("mute-affects-post-fader", _mute_affects_post_fader?"yes":"no");
1353 node->add_property("mute-affects-control-outs", _mute_affects_control_outs?"yes":"no");
1354 node->add_property("mute-affects-main-outs", _mute_affects_main_outs?"yes":"no");
1357 node->add_property("edit-group", _edit_group->name());
1360 node->add_property("mix-group", _mix_group->name());
1363 string order_string;
1364 OrderKeys::iterator x = order_keys.begin();
1366 while (x != order_keys.end()) {
1367 order_string += (*x).first;
1368 order_string += '=';
1369 snprintf (buf, sizeof(buf), "%ld", (*x).second);
1370 order_string += buf;
1374 if (x == order_keys.end()) {
1378 order_string += ':';
1380 node->add_property ("order-keys", order_string);
1382 node->add_child_nocopy (IO::state (full_state));
1384 if (_control_outs) {
1385 XMLNode* cnode = new XMLNode (X_("ControlOuts"));
1386 cnode->add_child_nocopy (_control_outs->state (full_state));
1387 node->add_child_nocopy (*cnode);
1390 if (_comment.length()) {
1391 XMLNode *cmt = node->add_child ("Comment");
1392 cmt->add_content (_comment);
1398 path = _session.snap_name();
1400 path += legalize_for_path (_name);
1401 path += ".automation";
1403 /* XXX we didn't ask for a state save, we asked for the current state.
1407 if (save_automation (path)) {
1408 error << _("Could not get state of route. Problem with save_automation") << endmsg;
1411 aevents = node->add_child ("Automation");
1412 aevents->add_property ("path", path);
1415 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
1416 node->add_child_nocopy((*i)->state (full_state));
1420 node->add_child_copy (*_extra_xml);
1427 Route::set_deferred_state ()
1430 XMLNodeConstIterator niter;
1432 if (!deferred_state) {
1436 nlist = deferred_state->children();
1438 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
1439 add_redirect_from_xml (**niter);
1442 delete deferred_state;
1447 Route::add_redirect_from_xml (const XMLNode& node)
1449 const XMLProperty *prop;
1453 if (node.name() == "Send") {
1456 send = new Send (_session, node);
1459 catch (failed_constructor &err) {
1460 error << _("Send construction failed") << endmsg;
1464 add_redirect (send, this);
1466 } else if (node.name() == "Insert") {
1469 if ((prop = node.property ("type")) != 0) {
1471 if (prop->value() == "ladspa" || prop->value() == "Ladspa" || prop->value() == "vst") {
1473 insert = new PluginInsert(_session, node);
1475 } else if (prop->value() == "port") {
1478 insert = new PortInsert (_session, node);
1482 error << string_compose(_("unknown Insert type \"%1\"; ignored"), prop->value()) << endmsg;
1485 add_redirect (insert, this);
1488 error << _("Insert XML node has no type property") << endmsg;
1492 catch (failed_constructor &err) {
1493 warning << _("insert could not be created. Ignored.") << endmsg;
1500 Route::set_state (const XMLNode& node)
1503 XMLNodeConstIterator niter;
1505 XMLPropertyList plist;
1506 const XMLProperty *prop;
1508 if (node.name() != "Route"){
1509 error << string_compose(_("Bad node sent to Route::set_state() [%1]"), node.name()) << endmsg;
1513 if ((prop = node.property ("flags")) != 0) {
1515 sscanf (prop->value().c_str(), "0x%x", &x);
1521 if ((prop = node.property ("default-type")) != 0) {
1522 _default_type = Buffer::type_from_string(prop->value());
1523 assert(_default_type != Buffer::NIL);
1526 if ((prop = node.property ("phase-invert")) != 0) {
1527 set_phase_invert(prop->value()=="yes"?true:false, this);
1530 if ((prop = node.property ("active")) != 0) {
1531 set_active (prop->value() == "yes");
1534 if ((prop = node.property ("muted")) != 0) {
1535 bool yn = prop->value()=="yes"?true:false;
1537 /* force reset of mute status */
1541 mute_gain = desired_mute_gain;
1544 if ((prop = node.property ("soloed")) != 0) {
1545 bool yn = prop->value()=="yes"?true:false;
1547 /* force reset of solo status */
1550 set_solo (yn, this);
1551 solo_gain = desired_solo_gain;
1554 if ((prop = node.property ("mute-affects-pre-fader")) != 0) {
1555 _mute_affects_pre_fader = (prop->value()=="yes")?true:false;
1558 if ((prop = node.property ("mute-affects-post-fader")) != 0) {
1559 _mute_affects_post_fader = (prop->value()=="yes")?true:false;
1562 if ((prop = node.property ("mute-affects-control-outs")) != 0) {
1563 _mute_affects_control_outs = (prop->value()=="yes")?true:false;
1566 if ((prop = node.property ("mute-affects-main-outs")) != 0) {
1567 _mute_affects_main_outs = (prop->value()=="yes")?true:false;
1570 if ((prop = node.property ("edit-group")) != 0) {
1571 RouteGroup* edit_group = _session.edit_group_by_name(prop->value());
1572 if(edit_group == 0) {
1573 error << string_compose(_("Route %1: unknown edit group \"%2 in saved state (ignored)"), _name, prop->value()) << endmsg;
1575 set_edit_group(edit_group, this);
1579 if ((prop = node.property ("order-keys")) != 0) {
1583 string::size_type colon, equal;
1584 string remaining = prop->value();
1586 while (remaining.length()) {
1588 if ((equal = remaining.find_first_of ('=')) == string::npos || equal == remaining.length()) {
1589 error << string_compose (_("badly formed order key string in state file! [%1] ... ignored."), remaining)
1592 if (sscanf (remaining.substr (equal+1).c_str(), "%ld", &n) != 1) {
1593 error << string_compose (_("badly formed order key string in state file! [%1] ... ignored."), remaining)
1596 set_order_key (remaining.substr (0, equal), n);
1600 colon = remaining.find_first_of (':');
1602 if (colon != string::npos) {
1603 remaining = remaining.substr (colon+1);
1610 nlist = node.children();
1612 if (deferred_state) {
1613 delete deferred_state;
1616 deferred_state = new XMLNode("deferred state");
1618 /* set parent class properties before anything else */
1620 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
1624 if (child->name() == IO::state_node_name) {
1626 IO::set_state (*child);
1631 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
1635 if (child->name() == "Send") {
1638 if (!IO::ports_legal) {
1640 deferred_state->add_child_copy (*child);
1643 add_redirect_from_xml (*child);
1646 } else if (child->name() == "Insert") {
1648 if (!IO::ports_legal) {
1650 deferred_state->add_child_copy (*child);
1654 add_redirect_from_xml (*child);
1657 } else if (child->name() == "Automation") {
1659 XMLPropertyList plist;
1660 XMLPropertyConstIterator piter;
1663 plist = child->properties();
1664 for (piter = plist.begin(); piter != plist.end(); ++piter) {
1666 if (prop->name() == "path") {
1667 load_automation (prop->value());
1671 } else if (child->name() == "ControlOuts") {
1673 string coutname = _name;
1674 coutname += _("[control]");
1676 _control_outs = new IO (_session, coutname);
1677 _control_outs->set_state (**(child->children().begin()));
1679 } else if (child->name() == "Comment") {
1681 /* XXX this is a terrible API design in libxml++ */
1683 XMLNode *cmt = *(child->children().begin());
1684 _comment = cmt->content();
1686 } else if (child->name() == "extra") {
1687 _extra_xml = new XMLNode (*child);
1691 if ((prop = node.property ("mix-group")) != 0) {
1692 RouteGroup* mix_group = _session.mix_group_by_name(prop->value());
1693 if (mix_group == 0) {
1694 error << string_compose(_("Route %1: unknown mix group \"%2 in saved state (ignored)"), _name, prop->value()) << endmsg;
1696 set_mix_group(mix_group, this);
1704 Route::curve_reallocate ()
1706 // _gain_automation_curve.finish_resize ();
1707 // _pan_automation_curve.finish_resize ();
1711 Route::silence (jack_nframes_t nframes, jack_nframes_t offset)
1715 // reset_peak_meters ();
1717 IO::silence (nframes, offset);
1719 if (_control_outs) {
1720 _control_outs->silence (nframes, offset);
1724 Glib::RWLock::ReaderLock lm (redirect_lock, Glib::TRY_LOCK);
1727 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
1729 if (!_active && (pi = dynamic_cast<PluginInsert*> (*i)) != 0) {
1730 // skip plugins, they don't need anything when we're not active
1734 (*i)->silence (nframes, offset);
1737 if (nframes == _session.get_block_size() && offset == 0) {
1747 Route::set_control_outs (const vector<string>& ports)
1749 Glib::Mutex::Lock lm (control_outs_lock);
1750 vector<string>::const_iterator i;
1752 if (_control_outs) {
1753 delete _control_outs;
1757 if (ports.empty()) {
1761 string coutname = _name;
1762 coutname += _("[control]");
1764 _control_outs = new IO (_session, coutname);
1766 /* our control outs need as many outputs as we
1767 have outputs. we track the changes in ::output_change_handler().
1770 _control_outs->ensure_io (0, n_outputs(), true, this);
1776 Route::set_edit_group (RouteGroup *eg, void *src)
1779 if (eg == _edit_group) {
1784 _edit_group->remove (this);
1787 if ((_edit_group = eg) != 0) {
1788 _edit_group->add (this);
1791 _session.set_dirty ();
1792 edit_group_changed (src); /* EMIT SIGNAL */
1796 Route::drop_edit_group (void *src)
1799 _session.set_dirty ();
1800 edit_group_changed (src); /* EMIT SIGNAL */
1804 Route::set_mix_group (RouteGroup *mg, void *src)
1807 if (mg == _mix_group) {
1812 _mix_group->remove (this);
1815 if ((_mix_group = mg) != 0) {
1816 _mix_group->add (this);
1819 _session.set_dirty ();
1820 mix_group_changed (src); /* EMIT SIGNAL */
1824 Route::drop_mix_group (void *src)
1827 _session.set_dirty ();
1828 mix_group_changed (src); /* EMIT SIGNAL */
1832 Route::set_comment (string cmt, void *src)
1835 comment_changed (src);
1836 _session.set_dirty ();
1840 Route::feeds (Route *o)
1846 uint32_t no = self.n_outputs();
1847 uint32_t ni = other.n_inputs ();
1849 for (i = 0; i < no; ++i) {
1850 for (j = 0; j < ni; ++j) {
1851 if (self.output(i)->connected_to (other.input(j)->name())) {
1857 /* check Redirects which may also interconnect Routes */
1859 for (RedirectList::iterator r = _redirects.begin(); r != _redirects.end(); r++) {
1861 no = (*r)->n_outputs();
1863 for (i = 0; i < no; ++i) {
1864 for (j = 0; j < ni; ++j) {
1865 if ((*r)->output(i)->connected_to (other.input (j)->name())) {
1872 /* check for control room outputs which may also interconnect Routes */
1874 if (_control_outs) {
1876 no = _control_outs->n_outputs();
1878 for (i = 0; i < no; ++i) {
1879 for (j = 0; j < ni; ++j) {
1880 if (_control_outs->output(i)->connected_to (other.input (j)->name())) {
1891 Route::set_mute_config (mute_type t, bool onoff, void *src)
1895 _mute_affects_pre_fader = onoff;
1896 pre_fader_changed(src); /* EMIT SIGNAL */
1900 _mute_affects_post_fader = onoff;
1901 post_fader_changed(src); /* EMIT SIGNAL */
1905 _mute_affects_control_outs = onoff;
1906 control_outs_changed(src); /* EMIT SIGNAL */
1910 _mute_affects_main_outs = onoff;
1911 main_outs_changed(src); /* EMIT SIGNAL */
1917 Route::get_mute_config (mute_type t)
1923 onoff = _mute_affects_pre_fader;
1926 onoff = _mute_affects_post_fader;
1929 onoff = _mute_affects_control_outs;
1932 onoff = _mute_affects_main_outs;
1940 Route::set_active (bool yn)
1943 active_changed(); /* EMIT SIGNAL */
1947 Route::handle_transport_stopped (bool abort_ignored, bool did_locate, bool can_flush_redirects)
1949 jack_nframes_t now = _session.transport_frame();
1952 Glib::RWLock::ReaderLock lm (redirect_lock);
1955 automation_snapshot (now);
1958 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
1960 if (Config->get_plugins_stop_with_transport() && can_flush_redirects) {
1961 (*i)->deactivate ();
1965 (*i)->transport_stopped (now);
1969 IO::transport_stopped (now);
1971 _roll_delay = _initial_delay;
1975 Route::get_memento() const
1977 void (Route::*pmf)(state_id_t) = &Route::set_state;
1978 return sigc::bind (mem_fun (*(const_cast<Route *>(this)), pmf), _current_state_id);
1982 Route::set_state (state_id_t id)
1988 Route::input_change_handler (IOChange change, void *ignored)
1990 if (change & ConfigurationChanged) {
1991 reset_plugin_counts (0);
1996 Route::output_change_handler (IOChange change, void *ignored)
1998 if (change & ConfigurationChanged) {
1999 if (_control_outs) {
2000 _control_outs->ensure_io (0, n_outputs(), true, this);
2003 reset_plugin_counts (0);
2008 Route::pans_required () const
2010 if (n_outputs() < 2) {
2014 return max (n_inputs (), redirect_max_outs);
2018 Route::no_roll (jack_nframes_t nframes, jack_nframes_t start_frame, jack_nframes_t end_frame, jack_nframes_t offset,
2019 bool session_state_changing, bool can_record, bool rec_monitors_input)
2021 if (n_outputs() == 0) {
2025 if (session_state_changing || !_active) {
2026 silence (nframes, offset);
2030 apply_gain_automation = false;
2033 passthru (start_frame, end_frame, nframes, offset, 0, false);
2035 silence (nframes, offset);
2042 Route::check_initial_delay (jack_nframes_t nframes, jack_nframes_t& offset, jack_nframes_t& transport_frame)
2044 if (_roll_delay > nframes) {
2046 _roll_delay -= nframes;
2047 silence (nframes, offset);
2048 /* transport frame is not legal for caller to use */
2051 } else if (_roll_delay > 0) {
2053 nframes -= _roll_delay;
2055 silence (_roll_delay, offset);
2057 offset += _roll_delay;
2058 transport_frame += _roll_delay;
2067 Route::roll (jack_nframes_t nframes, jack_nframes_t start_frame, jack_nframes_t end_frame, jack_nframes_t offset, int declick,
2068 bool can_record, bool rec_monitors_input)
2071 Glib::RWLock::ReaderLock lm (redirect_lock, Glib::TRY_LOCK);
2073 // automation snapshot can also be called from the non-rt context
2074 // and it uses the redirect list, so we take the lock out here
2075 automation_snapshot (_session.transport_frame());
2079 if ((n_outputs() == 0 && _redirects.empty()) || n_inputs() == 0 || !_active) {
2080 silence (nframes, offset);
2084 jack_nframes_t unused = 0;
2086 if ((nframes = check_initial_delay (nframes, offset, unused)) == 0) {
2092 apply_gain_automation = false;
2095 Glib::Mutex::Lock am (automation_lock, Glib::TRY_LOCK);
2097 if (am.locked() && _session.transport_rolling()) {
2099 jack_nframes_t start_frame = end_frame - nframes;
2101 if (gain_automation_playback()) {
2102 apply_gain_automation = _gain_automation_curve.rt_safe_get_vector (start_frame, end_frame, _session.gain_automation_buffer(), nframes);
2107 passthru (start_frame, end_frame, nframes, offset, declick, false);
2113 Route::silent_roll (jack_nframes_t nframes, jack_nframes_t start_frame, jack_nframes_t end_frame, jack_nframes_t offset,
2114 bool can_record, bool rec_monitors_input)
2116 silence (nframes, offset);
2121 Route::toggle_monitor_input ()
2123 for (vector<Port*>::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
2124 (*i)->request_monitor_input(!(*i)->monitoring_input());
2129 Route::has_external_redirects () const
2131 const PortInsert* pi;
2133 for (RedirectList::const_iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2134 if ((pi = dynamic_cast<const PortInsert*>(*i)) != 0) {
2136 uint32_t no = pi->n_outputs();
2138 for (uint32_t n = 0; n < no; ++n) {
2140 string port_name = pi->output(n)->name();
2141 string client_name = port_name.substr (0, port_name.find(':'));
2143 /* only say "yes" if the redirect is actually in use */
2145 if (client_name != "ardour" && pi->active()) {
2156 Route::flush_redirects ()
2158 /* XXX shouldn't really try to take this lock, since
2159 this is called from the RT audio thread.
2162 Glib::RWLock::ReaderLock lm (redirect_lock);
2164 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2165 (*i)->deactivate ();
2171 Route::set_meter_point (MeterPoint p, void *src)
2173 if (_meter_point != p) {
2175 meter_change (src); /* EMIT SIGNAL */
2176 _session.set_dirty ();
2181 Route::update_total_latency ()
2185 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2186 if ((*i)->active ()) {
2187 _own_latency += (*i)->latency ();
2191 set_port_latency (_own_latency);
2193 /* this (virtual) function is used for pure Routes,
2194 not derived classes like AudioTrack. this means
2195 that the data processed here comes from an input
2196 port, not prerecorded material, and therefore we
2197 have to take into account any input latency.
2200 _own_latency += input_latency ();
2202 return _own_latency;
2206 Route::set_latency_delay (jack_nframes_t longest_session_latency)
2208 _initial_delay = longest_session_latency - _own_latency;
2210 if (_session.transport_stopped()) {
2211 _roll_delay = _initial_delay;
2216 Route::automation_snapshot (jack_nframes_t now)
2218 IO::automation_snapshot (now);
2220 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2221 (*i)->automation_snapshot (now);
2225 Route::ToggleControllable::ToggleControllable (Route& s, ToggleType tp)
2226 : route (s), type(tp)
2232 Route::ToggleControllable::set_value (float val)
2234 bool bval = ((val >= 0.5f) ? true: false);
2238 route.set_mute (bval, this);
2241 route.set_solo (bval, this);
2249 Route::ToggleControllable::get_value (void) const
2255 val = route.muted() ? 1.0f : 0.0f;
2258 val = route.soloed() ? 1.0f : 0.0f;
2268 Route::set_block_size (jack_nframes_t nframes)
2270 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2271 (*i)->set_block_size (nframes);
2276 Route::redirect_active_proxy (Redirect* ignored, void* ignored_src)
2278 _session.update_latency_compensation (false, false);
2282 Route::protect_automation ()
2284 switch (gain_automation_state()) {
2287 set_gain_automation_state (Off);
2293 switch (panner().automation_state ()) {
2296 panner().set_automation_state (Off);
2302 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2304 if ((pi = dynamic_cast<PluginInsert*> (*i)) != 0) {
2305 pi->protect_automation ();
2311 Route::set_pending_declick (int declick)
2314 /* this call is not allowed to turn off a pending declick unless "force" is true */
2316 _pending_declick = declick;
2318 // cerr << _name << ": after setting to " << declick << " pending declick = " << _pending_declick << endl;
2320 _pending_declick = 0;