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");
1339 RedirectList:: iterator i;
1343 snprintf (buf, sizeof (buf), "0x%x", _flags);
1344 node->add_property("flags", buf);
1347 node->add_property("default-type", _default_type.to_string());
1349 node->add_property("active", _active?"yes":"no");
1350 node->add_property("muted", _muted?"yes":"no");
1351 node->add_property("soloed", _soloed?"yes":"no");
1352 node->add_property("phase-invert", _phase_invert?"yes":"no");
1353 node->add_property("mute-affects-pre-fader", _mute_affects_pre_fader?"yes":"no");
1354 node->add_property("mute-affects-post-fader", _mute_affects_post_fader?"yes":"no");
1355 node->add_property("mute-affects-control-outs", _mute_affects_control_outs?"yes":"no");
1356 node->add_property("mute-affects-main-outs", _mute_affects_main_outs?"yes":"no");
1359 node->add_property("edit-group", _edit_group->name());
1362 node->add_property("mix-group", _mix_group->name());
1365 string order_string;
1366 OrderKeys::iterator x = order_keys.begin();
1368 while (x != order_keys.end()) {
1369 order_string += (*x).first;
1370 order_string += '=';
1371 snprintf (buf, sizeof(buf), "%ld", (*x).second);
1372 order_string += buf;
1376 if (x == order_keys.end()) {
1380 order_string += ':';
1382 node->add_property ("order-keys", order_string);
1384 node->add_child_nocopy (IO::state (full_state));
1385 node->add_child_nocopy (_solo_control.get_state ());
1386 node->add_child_nocopy (_mute_control.get_state ());
1388 if (_control_outs) {
1389 XMLNode* cnode = new XMLNode (X_("ControlOuts"));
1390 cnode->add_child_nocopy (_control_outs->state (full_state));
1391 node->add_child_nocopy (*cnode);
1394 if (_comment.length()) {
1395 XMLNode *cmt = node->add_child ("Comment");
1396 cmt->add_content (_comment);
1402 path = _session.snap_name();
1404 path += legalize_for_path (_name);
1405 path += ".automation";
1407 /* XXX we didn't ask for a state save, we asked for the current state.
1411 if (save_automation (path)) {
1412 error << _("Could not get state of route. Problem with save_automation") << endmsg;
1415 aevents = node->add_child ("Automation");
1416 aevents->add_property ("path", path);
1419 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
1420 node->add_child_nocopy((*i)->state (full_state));
1424 node->add_child_copy (*_extra_xml);
1431 Route::set_deferred_state ()
1434 XMLNodeConstIterator niter;
1436 if (!deferred_state) {
1440 nlist = deferred_state->children();
1442 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
1443 add_redirect_from_xml (**niter);
1446 delete deferred_state;
1451 Route::add_redirect_from_xml (const XMLNode& node)
1453 const XMLProperty *prop;
1455 if (node.name() == "Send") {
1459 boost::shared_ptr<Send> send (new Send (_session, node));
1460 add_redirect (send, this);
1463 catch (failed_constructor &err) {
1464 error << _("Send construction failed") << endmsg;
1468 } else if (node.name() == "Insert") {
1471 if ((prop = node.property ("type")) != 0) {
1473 boost::shared_ptr<Insert> insert;
1475 if (prop->value() == "ladspa" || prop->value() == "Ladspa" || prop->value() == "vst") {
1477 insert.reset (new PluginInsert(_session, node));
1479 } else if (prop->value() == "port") {
1482 insert.reset (new PortInsert (_session, node));
1486 error << string_compose(_("unknown Insert type \"%1\"; ignored"), prop->value()) << endmsg;
1489 add_redirect (insert, this);
1492 error << _("Insert XML node has no type property") << endmsg;
1496 catch (failed_constructor &err) {
1497 warning << _("insert could not be created. Ignored.") << endmsg;
1504 Route::set_state (const XMLNode& node)
1507 XMLNodeConstIterator niter;
1509 XMLPropertyList plist;
1510 const XMLProperty *prop;
1512 if (node.name() != "Route"){
1513 error << string_compose(_("Bad node sent to Route::set_state() [%1]"), node.name()) << endmsg;
1517 if ((prop = node.property ("flags")) != 0) {
1519 sscanf (prop->value().c_str(), "0x%x", &x);
1525 if ((prop = node.property ("default-type")) != 0) {
1526 _default_type = DataType(prop->value());
1527 assert(_default_type != DataType::NIL);
1530 if ((prop = node.property ("phase-invert")) != 0) {
1531 set_phase_invert(prop->value()=="yes"?true:false, this);
1534 if ((prop = node.property ("active")) != 0) {
1535 set_active (prop->value() == "yes");
1538 if ((prop = node.property ("muted")) != 0) {
1539 bool yn = prop->value()=="yes"?true:false;
1541 /* force reset of mute status */
1545 mute_gain = desired_mute_gain;
1548 if ((prop = node.property ("soloed")) != 0) {
1549 bool yn = prop->value()=="yes"?true:false;
1551 /* force reset of solo status */
1554 set_solo (yn, this);
1555 solo_gain = desired_solo_gain;
1558 if ((prop = node.property ("mute-affects-pre-fader")) != 0) {
1559 _mute_affects_pre_fader = (prop->value()=="yes")?true:false;
1562 if ((prop = node.property ("mute-affects-post-fader")) != 0) {
1563 _mute_affects_post_fader = (prop->value()=="yes")?true:false;
1566 if ((prop = node.property ("mute-affects-control-outs")) != 0) {
1567 _mute_affects_control_outs = (prop->value()=="yes")?true:false;
1570 if ((prop = node.property ("mute-affects-main-outs")) != 0) {
1571 _mute_affects_main_outs = (prop->value()=="yes")?true:false;
1574 if ((prop = node.property ("edit-group")) != 0) {
1575 RouteGroup* edit_group = _session.edit_group_by_name(prop->value());
1576 if(edit_group == 0) {
1577 error << string_compose(_("Route %1: unknown edit group \"%2 in saved state (ignored)"), _name, prop->value()) << endmsg;
1579 set_edit_group(edit_group, this);
1583 if ((prop = node.property ("order-keys")) != 0) {
1587 string::size_type colon, equal;
1588 string remaining = prop->value();
1590 while (remaining.length()) {
1592 if ((equal = remaining.find_first_of ('=')) == string::npos || equal == remaining.length()) {
1593 error << string_compose (_("badly formed order key string in state file! [%1] ... ignored."), remaining)
1596 if (sscanf (remaining.substr (equal+1).c_str(), "%ld", &n) != 1) {
1597 error << string_compose (_("badly formed order key string in state file! [%1] ... ignored."), remaining)
1600 set_order_key (remaining.substr (0, equal), n);
1604 colon = remaining.find_first_of (':');
1606 if (colon != string::npos) {
1607 remaining = remaining.substr (colon+1);
1614 nlist = node.children();
1616 if (deferred_state) {
1617 delete deferred_state;
1620 deferred_state = new XMLNode("deferred state");
1622 /* set parent class properties before anything else */
1624 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
1628 if (child->name() == IO::state_node_name) {
1630 IO::set_state (*child);
1635 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
1639 if (child->name() == "Send") {
1642 if (!IO::ports_legal) {
1644 deferred_state->add_child_copy (*child);
1647 add_redirect_from_xml (*child);
1650 } else if (child->name() == "Insert") {
1652 if (!IO::ports_legal) {
1654 deferred_state->add_child_copy (*child);
1658 add_redirect_from_xml (*child);
1661 } else if (child->name() == "Automation") {
1663 XMLPropertyList plist;
1664 XMLPropertyConstIterator piter;
1667 plist = child->properties();
1668 for (piter = plist.begin(); piter != plist.end(); ++piter) {
1670 if (prop->name() == "path") {
1671 load_automation (prop->value());
1675 } else if (child->name() == "ControlOuts") {
1677 string coutname = _name;
1678 coutname += _("[control]");
1680 _control_outs = new IO (_session, coutname);
1681 _control_outs->set_state (**(child->children().begin()));
1683 } else if (child->name() == "Comment") {
1685 /* XXX this is a terrible API design in libxml++ */
1687 XMLNode *cmt = *(child->children().begin());
1688 _comment = cmt->content();
1690 } else if (child->name() == "extra") {
1691 _extra_xml = new XMLNode (*child);
1692 } else if (child->name() == "solo") {
1693 _solo_control.set_state (*child);
1694 _session.add_controllable (&_solo_control);
1695 } else if (child->name() == "mute") {
1696 _mute_control.set_state (*child);
1697 _session.add_controllable (&_mute_control);
1701 if ((prop = node.property ("mix-group")) != 0) {
1702 RouteGroup* mix_group = _session.mix_group_by_name(prop->value());
1703 if (mix_group == 0) {
1704 error << string_compose(_("Route %1: unknown mix group \"%2 in saved state (ignored)"), _name, prop->value()) << endmsg;
1706 set_mix_group(mix_group, this);
1714 Route::curve_reallocate ()
1716 // _gain_automation_curve.finish_resize ();
1717 // _pan_automation_curve.finish_resize ();
1721 Route::silence (nframes_t nframes, nframes_t offset)
1725 // reset_peak_meters ();
1727 IO::silence (nframes, offset);
1729 if (_control_outs) {
1730 _control_outs->silence (nframes, offset);
1734 Glib::RWLock::ReaderLock lm (redirect_lock, Glib::TRY_LOCK);
1737 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
1738 boost::shared_ptr<PluginInsert> pi;
1739 if (!_active && (pi = boost::dynamic_pointer_cast<PluginInsert> (*i)) != 0) {
1740 // skip plugins, they don't need anything when we're not active
1744 (*i)->silence (nframes, offset);
1747 if (nframes == _session.get_block_size() && offset == 0) {
1757 Route::set_control_outs (const vector<string>& ports)
1759 Glib::Mutex::Lock lm (control_outs_lock);
1760 vector<string>::const_iterator i;
1762 if (_control_outs) {
1763 delete _control_outs;
1767 if (ports.empty()) {
1771 string coutname = _name;
1772 coutname += _("[control]");
1774 _control_outs = new IO (_session, coutname);
1776 /* our control outs need as many outputs as we
1777 have outputs. we track the changes in ::output_change_handler().
1780 _control_outs->ensure_io (0, n_outputs(), true, this);
1786 Route::set_edit_group (RouteGroup *eg, void *src)
1789 if (eg == _edit_group) {
1794 _edit_group->remove (this);
1797 if ((_edit_group = eg) != 0) {
1798 _edit_group->add (this);
1801 _session.set_dirty ();
1802 edit_group_changed (src); /* EMIT SIGNAL */
1806 Route::drop_edit_group (void *src)
1809 _session.set_dirty ();
1810 edit_group_changed (src); /* EMIT SIGNAL */
1814 Route::set_mix_group (RouteGroup *mg, void *src)
1817 if (mg == _mix_group) {
1822 _mix_group->remove (this);
1825 if ((_mix_group = mg) != 0) {
1826 _mix_group->add (this);
1829 _session.set_dirty ();
1830 mix_group_changed (src); /* EMIT SIGNAL */
1834 Route::drop_mix_group (void *src)
1837 _session.set_dirty ();
1838 mix_group_changed (src); /* EMIT SIGNAL */
1842 Route::set_comment (string cmt, void *src)
1845 comment_changed (src);
1846 _session.set_dirty ();
1850 Route::feeds (boost::shared_ptr<Route> other)
1855 uint32_t no = self.n_outputs();
1856 uint32_t ni = other->n_inputs ();
1858 for (i = 0; i < no; ++i) {
1859 for (j = 0; j < ni; ++j) {
1860 if (self.output(i)->connected_to (other->input(j)->name())) {
1866 /* check Redirects which may also interconnect Routes */
1868 for (RedirectList::iterator r = _redirects.begin(); r != _redirects.end(); r++) {
1870 no = (*r)->n_outputs();
1872 for (i = 0; i < no; ++i) {
1873 for (j = 0; j < ni; ++j) {
1874 if ((*r)->output(i)->connected_to (other->input (j)->name())) {
1881 /* check for control room outputs which may also interconnect Routes */
1883 if (_control_outs) {
1885 no = _control_outs->n_outputs();
1887 for (i = 0; i < no; ++i) {
1888 for (j = 0; j < ni; ++j) {
1889 if (_control_outs->output(i)->connected_to (other->input (j)->name())) {
1900 Route::set_mute_config (mute_type t, bool onoff, void *src)
1904 _mute_affects_pre_fader = onoff;
1905 pre_fader_changed(src); /* EMIT SIGNAL */
1909 _mute_affects_post_fader = onoff;
1910 post_fader_changed(src); /* EMIT SIGNAL */
1914 _mute_affects_control_outs = onoff;
1915 control_outs_changed(src); /* EMIT SIGNAL */
1919 _mute_affects_main_outs = onoff;
1920 main_outs_changed(src); /* EMIT SIGNAL */
1926 Route::get_mute_config (mute_type t)
1932 onoff = _mute_affects_pre_fader;
1935 onoff = _mute_affects_post_fader;
1938 onoff = _mute_affects_control_outs;
1941 onoff = _mute_affects_main_outs;
1949 Route::set_active (bool yn)
1952 active_changed(); /* EMIT SIGNAL */
1956 Route::handle_transport_stopped (bool abort_ignored, bool did_locate, bool can_flush_redirects)
1958 nframes_t now = _session.transport_frame();
1961 Glib::RWLock::ReaderLock lm (redirect_lock);
1963 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
1965 if (Config->get_plugins_stop_with_transport() && can_flush_redirects) {
1966 (*i)->deactivate ();
1970 (*i)->transport_stopped (now);
1974 IO::transport_stopped (now);
1976 _roll_delay = _initial_delay;
1980 Route::input_change_handler (IOChange change, void *ignored)
1982 if (change & ConfigurationChanged) {
1983 reset_plugin_counts (0);
1988 Route::output_change_handler (IOChange change, void *ignored)
1990 if (change & ConfigurationChanged) {
1991 if (_control_outs) {
1992 _control_outs->ensure_io (0, n_outputs(), true, this);
1995 reset_plugin_counts (0);
2000 Route::pans_required () const
2002 if (n_outputs() < 2) {
2006 return max (n_inputs (), redirect_max_outs);
2010 Route::no_roll (nframes_t nframes, nframes_t start_frame, nframes_t end_frame, nframes_t offset,
2011 bool session_state_changing, bool can_record, bool rec_monitors_input)
2013 if (n_outputs() == 0) {
2017 if (session_state_changing || !_active) {
2018 silence (nframes, offset);
2022 apply_gain_automation = false;
2025 passthru (start_frame, end_frame, nframes, offset, 0, false);
2027 silence (nframes, offset);
2034 Route::check_initial_delay (nframes_t nframes, nframes_t& offset, nframes_t& transport_frame)
2036 if (_roll_delay > nframes) {
2038 _roll_delay -= nframes;
2039 silence (nframes, offset);
2040 /* transport frame is not legal for caller to use */
2043 } else if (_roll_delay > 0) {
2045 nframes -= _roll_delay;
2047 silence (_roll_delay, offset);
2049 offset += _roll_delay;
2050 transport_frame += _roll_delay;
2059 Route::roll (nframes_t nframes, nframes_t start_frame, nframes_t end_frame, nframes_t offset, int declick,
2060 bool can_record, bool rec_monitors_input)
2062 if ((n_outputs() == 0 && _redirects.empty()) || n_inputs() == 0 || !_active) {
2063 silence (nframes, offset);
2067 nframes_t unused = 0;
2069 if ((nframes = check_initial_delay (nframes, offset, unused)) == 0) {
2075 apply_gain_automation = false;
2078 Glib::Mutex::Lock am (automation_lock, Glib::TRY_LOCK);
2080 if (am.locked() && _session.transport_rolling()) {
2082 nframes_t start_frame = end_frame - nframes;
2084 if (gain_automation_playback()) {
2085 apply_gain_automation = _gain_automation_curve.rt_safe_get_vector (start_frame, end_frame, _session.gain_automation_buffer(), nframes);
2090 passthru (start_frame, end_frame, nframes, offset, declick, false);
2096 Route::silent_roll (nframes_t nframes, nframes_t start_frame, nframes_t end_frame, nframes_t offset,
2097 bool can_record, bool rec_monitors_input)
2099 silence (nframes, offset);
2104 Route::toggle_monitor_input ()
2106 for (vector<Port*>::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
2107 (*i)->ensure_monitor_input(!(*i)->monitoring_input());
2112 Route::has_external_redirects () const
2114 boost::shared_ptr<const PortInsert> pi;
2116 for (RedirectList::const_iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2117 if ((pi = boost::dynamic_pointer_cast<const PortInsert>(*i)) != 0) {
2119 uint32_t no = pi->n_outputs();
2121 for (uint32_t n = 0; n < no; ++n) {
2123 string port_name = pi->output(n)->name();
2124 string client_name = port_name.substr (0, port_name.find(':'));
2126 /* only say "yes" if the redirect is actually in use */
2128 if (client_name != "ardour" && pi->active()) {
2139 Route::flush_redirects ()
2141 /* XXX shouldn't really try to take this lock, since
2142 this is called from the RT audio thread.
2145 Glib::RWLock::ReaderLock lm (redirect_lock);
2147 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2148 (*i)->deactivate ();
2154 Route::set_meter_point (MeterPoint p, void *src)
2156 if (_meter_point != p) {
2158 meter_change (src); /* EMIT SIGNAL */
2159 _session.set_dirty ();
2164 Route::update_total_latency ()
2168 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2169 if ((*i)->active ()) {
2170 _own_latency += (*i)->latency ();
2174 set_port_latency (_own_latency);
2176 /* this (virtual) function is used for pure Routes,
2177 not derived classes like AudioTrack. this means
2178 that the data processed here comes from an input
2179 port, not prerecorded material, and therefore we
2180 have to take into account any input latency.
2183 _own_latency += input_latency ();
2185 return _own_latency;
2189 Route::set_latency_delay (nframes_t longest_session_latency)
2191 _initial_delay = longest_session_latency - _own_latency;
2193 if (_session.transport_stopped()) {
2194 _roll_delay = _initial_delay;
2198 Route::ToggleControllable::ToggleControllable (std::string name, Route& s, ToggleType tp)
2199 : Controllable (name), route (s), type(tp)
2205 Route::ToggleControllable::set_value (float val)
2207 bool bval = ((val >= 0.5f) ? true: false);
2211 route.set_mute (bval, this);
2214 route.set_solo (bval, this);
2222 Route::ToggleControllable::get_value (void) const
2228 val = route.muted() ? 1.0f : 0.0f;
2231 val = route.soloed() ? 1.0f : 0.0f;
2241 Route::set_block_size (nframes_t nframes)
2243 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2244 (*i)->set_block_size (nframes);
2249 Route::redirect_active_proxy (Redirect* ignored, void* ignored_src)
2251 _session.update_latency_compensation (false, false);
2255 Route::protect_automation ()
2257 switch (gain_automation_state()) {
2260 set_gain_automation_state (Off);
2266 switch (panner().automation_state ()) {
2269 panner().set_automation_state (Off);
2275 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2276 boost::shared_ptr<PluginInsert> pi;
2277 if ((pi = boost::dynamic_pointer_cast<PluginInsert> (*i)) != 0) {
2278 pi->protect_automation ();
2284 Route::set_pending_declick (int declick)
2287 /* this call is not allowed to turn off a pending declick unless "force" is true */
2289 _pending_declick = declick;
2291 // cerr << _name << ": after setting to " << declick << " pending declick = " << _pending_declick << endl;
2293 _pending_declick = 0;