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;
96 _mute_affects_pre_fader = Config->get_mute_affects_pre_fader();
97 _mute_affects_post_fader = Config->get_mute_affects_post_fader();
98 _mute_affects_control_outs = Config->get_mute_affects_control_outs();
99 _mute_affects_main_outs = Config->get_mute_affects_main_outs();
102 desired_solo_gain = 1.0;
104 desired_mute_gain = 1.0;
108 input_changed.connect (mem_fun (this, &Route::input_change_handler));
109 output_changed.connect (mem_fun (this, &Route::output_change_handler));
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 nframes_t start_frame, nframes_t end_frame,
219 nframes_t nframes, 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;
232 gain_t* gab = _session.gain_automation_buffer();
234 switch (Config->get_monitoring_model()) {
235 case HardwareMonitoring:
236 case ExternalMonitoring:
243 declick = _pending_declick;
246 Glib::Mutex::Lock cm (control_outs_lock, Glib::TRY_LOCK);
256 Glib::Mutex::Lock dm (declick_lock, Glib::TRY_LOCK);
259 dmg = desired_mute_gain;
260 dsg = desired_solo_gain;
269 /* ----------------------------------------------------------------------------------------------------
270 GLOBAL DECLICK (for transport changes etc.)
271 -------------------------------------------------------------------------------------------------- */
274 apply_declick (bufs, nbufs, nframes, 0.0, 1.0, false);
275 _pending_declick = 0;
276 } else if (declick < 0) {
277 apply_declick (bufs, nbufs, nframes, 1.0, 0.0, false);
278 _pending_declick = 0;
281 /* no global declick */
283 if (solo_gain != dsg) {
284 apply_declick (bufs, nbufs, nframes, solo_gain, dsg, false);
290 /* ----------------------------------------------------------------------------------------------------
291 INPUT METERING & MONITORING
292 -------------------------------------------------------------------------------------------------- */
294 if (meter && (_meter_point == MeterInput)) {
295 for (n = 0; n < nbufs; ++n) {
296 _peak_power[n] = Session::compute_peak (bufs[n], nframes, _peak_power[n]);
300 if (!_soloed && _mute_affects_pre_fader && (mute_gain != dmg)) {
301 apply_declick (bufs, nbufs, nframes, mute_gain, dmg, false);
303 mute_declick_applied = true;
306 if ((_meter_point == MeterInput) && co) {
308 solo_audible = dsg > 0;
309 mute_audible = dmg > 0;// || !_mute_affects_pre_fader;
311 if ( // muted by solo of another track
315 // muted by mute of this track
319 // rec-enabled but not s/w monitoring
321 // TODO: this is probably wrong
323 (no_monitor && record_enabled() && (!Config->get_auto_input() || _session.actively_recording()))
327 co->silence (nframes, offset);
331 co->deliver_output (bufs, nbufs, nframes, offset);
336 /* ----------------------------------------------------------------------------------------------------
338 -------------------------------------------------------------------------------------------------- */
340 if (with_redirects) {
341 Glib::RWLock::ReaderLock rm (redirect_lock, Glib::TRY_LOCK);
343 if (mute_gain > 0 || !_mute_affects_pre_fader) {
344 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
345 switch ((*i)->placement()) {
347 (*i)->run (bufs, nbufs, nframes, offset);
350 post_fader_work = true;
355 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
356 switch ((*i)->placement()) {
358 (*i)->silence (nframes, offset);
361 post_fader_work = true;
370 if (!_soloed && (mute_gain != dmg) && !mute_declick_applied && _mute_affects_post_fader) {
371 apply_declick (bufs, nbufs, nframes, mute_gain, dmg, false);
373 mute_declick_applied = true;
376 /* ----------------------------------------------------------------------------------------------------
377 PRE-FADER METERING & MONITORING
378 -------------------------------------------------------------------------------------------------- */
380 if (meter && (_meter_point == MeterPreFader)) {
381 for (n = 0; n < nbufs; ++n) {
382 _peak_power[n] = Session::compute_peak (bufs[n], nframes, _peak_power[n]);
387 if ((_meter_point == MeterPreFader) && co) {
389 solo_audible = dsg > 0;
390 mute_audible = dmg > 0 || !_mute_affects_pre_fader;
392 if ( // muted by solo of another track
396 // muted by mute of this track
400 // rec-enabled but not s/w monitoring
402 (no_monitor && record_enabled() && (!Config->get_auto_input() || _session.actively_recording()))
406 co->silence (nframes, offset);
410 co->deliver_output (bufs, nbufs, nframes, offset);
415 /* ----------------------------------------------------------------------------------------------------
417 -------------------------------------------------------------------------------------------------- */
419 /* if not recording or recording and requiring any monitor signal, then apply gain */
421 if ( // not recording
423 !(record_enabled() && _session.actively_recording()) ||
427 // h/w monitoring not in use
429 (!Config->get_monitoring_model() == HardwareMonitoring &&
431 // AND software monitoring required
433 Config->get_monitoring_model() == SoftwareMonitoring)) {
435 if (apply_gain_automation) {
438 for (n = 0; n < nbufs; ++n) {
439 Sample *sp = bufs[n];
441 for (nframes_t nx = 0; nx < nframes; ++nx) {
446 for (n = 0; n < nbufs; ++n) {
447 Sample *sp = bufs[n];
449 for (nframes_t nx = 0; nx < nframes; ++nx) {
455 if (apply_gain_automation && _session.transport_rolling() && nframes > 0) {
456 _effective_gain = gab[nframes-1];
461 /* manual (scalar) gain */
465 apply_declick (bufs, nbufs, nframes, _gain, dg, _phase_invert);
468 } else if (_gain != 0 && (_phase_invert || _gain != 1.0)) {
470 /* no need to interpolate current gain value,
471 but its non-unity, so apply it. if the gain
472 is zero, do nothing because we'll ship silence
484 for (n = 0; n < nbufs; ++n) {
485 Sample *sp = bufs[n];
486 apply_gain_to_buffer(sp,nframes,this_gain);
489 } else if (_gain == 0) {
490 for (n = 0; n < nbufs; ++n) {
491 memset (bufs[n], 0, sizeof (Sample) * nframes);
498 /* actively recording, no monitoring required; leave buffers as-is to save CPU cycles */
502 /* ----------------------------------------------------------------------------------------------------
504 -------------------------------------------------------------------------------------------------- */
506 /* note that post_fader_work cannot be true unless with_redirects was also true, so don't test both */
508 if (post_fader_work) {
510 Glib::RWLock::ReaderLock rm (redirect_lock, Glib::TRY_LOCK);
512 if (mute_gain > 0 || !_mute_affects_post_fader) {
513 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
514 switch ((*i)->placement()) {
518 (*i)->run (bufs, nbufs, nframes, offset);
523 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
524 switch ((*i)->placement()) {
528 (*i)->silence (nframes, offset);
536 if (!_soloed && (mute_gain != dmg) && !mute_declick_applied && _mute_affects_control_outs) {
537 apply_declick (bufs, nbufs, nframes, mute_gain, dmg, false);
539 mute_declick_applied = true;
542 /* ----------------------------------------------------------------------------------------------------
544 -------------------------------------------------------------------------------------------------- */
546 if ((_meter_point == MeterPostFader) && co) {
548 solo_audible = solo_gain > 0;
549 mute_audible = dmg > 0 || !_mute_affects_control_outs;
551 if ( // silent anyway
553 (_gain == 0 && !apply_gain_automation) ||
555 // muted by solo of another track
559 // muted by mute of this track
563 // recording but not s/w monitoring
565 (no_monitor && record_enabled() && (!Config->get_auto_input() || _session.actively_recording()))
569 co->silence (nframes, offset);
573 co->deliver_output_no_pan (bufs, nbufs, nframes, offset);
577 /* ----------------------------------------------------------------------
579 ----------------------------------------------------------------------*/
581 if (!_soloed && (mute_gain != dmg) && !mute_declick_applied && _mute_affects_main_outs) {
582 apply_declick (bufs, nbufs, nframes, mute_gain, dmg, false);
584 mute_declick_applied = true;
587 /* ----------------------------------------------------------------------------------------------------
589 -------------------------------------------------------------------------------------------------- */
591 solo_audible = dsg > 0;
592 mute_audible = dmg > 0 || !_mute_affects_main_outs;
594 if (n_outputs() == 0) {
598 } else if (no_monitor && record_enabled() && (!Config->get_auto_input() || _session.actively_recording())) {
600 IO::silence (nframes, offset);
604 if ( // silent anyway
606 (_gain == 0 && !apply_gain_automation) ||
608 // muted by solo of another track, but not using control outs for solo
610 (!solo_audible && (Config->get_solo_model() != SoloBus)) ||
612 // muted by mute of this track
618 /* don't use Route::silence() here, because that causes
619 all outputs (sends, port inserts, etc. to be silent).
622 if (_meter_point == MeterPostFader) {
623 reset_peak_meters ();
626 IO::silence (nframes, offset);
630 if ((_session.transport_speed() > 1.5f ||
631 _session.transport_speed() < -1.5f) &&
632 Config->get_quieten_at_speed()) {
633 pan (bufs, nbufs, nframes, offset, speed_quietning);
635 // cerr << _name << " panner state = " << _panner->automation_state() << endl;
636 if (!_panner->empty() &&
637 (_panner->automation_state() & Play ||
638 ((_panner->automation_state() & Touch) && !_panner->touching()))) {
639 pan_automated (bufs, nbufs, start_frame, end_frame, nframes, offset);
641 pan (bufs, nbufs, nframes, offset, 1.0);
648 /* ----------------------------------------------------------------------------------------------------
650 -------------------------------------------------------------------------------------------------- */
652 if (meter && (_meter_point == MeterPostFader)) {
653 // cerr << "meter post" << endl;
655 if ((_gain == 0 && !apply_gain_automation) || dmg == 0) {
656 uint32_t no = n_outputs();
657 for (n = 0; n < no; ++n) {
661 uint32_t no = n_outputs();
662 for (n = 0; n < no; ++n) {
663 _peak_power[n] = Session::compute_peak (output(n)->get_buffer (nframes) + offset, nframes, _peak_power[n]);
670 Route::n_process_buffers ()
672 return max (n_inputs(), redirect_max_outs);
677 Route::passthru (nframes_t start_frame, nframes_t end_frame, nframes_t nframes, nframes_t offset, int declick, bool meter_first)
679 vector<Sample*>& bufs = _session.get_passthru_buffers();
680 uint32_t limit = n_process_buffers ();
684 collect_input (bufs, limit, nframes, offset);
686 #define meter_stream meter_first
689 for (uint32_t n = 0; n < limit; ++n) {
690 _peak_power[n] = Session::compute_peak (bufs[n], nframes, _peak_power[n]);
692 meter_stream = false;
697 process_output_buffers (bufs, limit, start_frame, end_frame, nframes, offset, true, declick, meter_stream);
703 Route::set_phase_invert (bool yn, void *src)
705 if (_phase_invert != yn) {
708 // phase_invert_changed (src); /* EMIT SIGNAL */
712 Route::set_solo (bool yn, void *src)
718 if (_mix_group && src != _mix_group && _mix_group->is_active()) {
719 _mix_group->apply (&Route::set_solo, yn, _mix_group);
725 solo_changed (src); /* EMIT SIGNAL */
726 _solo_control.Changed (); /* EMIT SIGNAL */
731 Route::set_solo_mute (bool yn)
733 Glib::Mutex::Lock lm (declick_lock);
735 /* Called by Session in response to another Route being soloed.
738 desired_solo_gain = (yn?0.0:1.0);
742 Route::set_solo_safe (bool yn, void *src)
744 if (_solo_safe != yn) {
746 solo_safe_changed (src); /* EMIT SIGNAL */
751 Route::set_mute (bool yn, void *src)
754 if (_mix_group && src != _mix_group && _mix_group->is_active()) {
755 _mix_group->apply (&Route::set_mute, yn, _mix_group);
761 mute_changed (src); /* EMIT SIGNAL */
763 _mute_control.Changed (); /* EMIT SIGNAL */
765 Glib::Mutex::Lock lm (declick_lock);
766 desired_mute_gain = (yn?0.0f:1.0f);
771 Route::add_redirect (boost::shared_ptr<Redirect> redirect, void *src, uint32_t* err_streams)
773 uint32_t old_rmo = redirect_max_outs;
775 if (!_session.engine().connected()) {
780 Glib::RWLock::WriterLock lm (redirect_lock);
782 boost::shared_ptr<PluginInsert> pi;
783 boost::shared_ptr<PortInsert> porti;
785 uint32_t potential_max_streams = 0;
787 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(redirect)) != 0) {
790 if (pi->input_streams() == 0) {
791 /* instrument plugin */
792 _have_internal_generator = true;
795 potential_max_streams = max(pi->input_streams(), pi->output_streams());
797 } else if ((porti = boost::dynamic_pointer_cast<PortInsert>(redirect)) != 0) {
799 /* force new port inserts to start out with an i/o configuration
800 that matches this route's i/o configuration.
802 the "inputs" for the port are supposed to match the output
805 the "outputs" of the route should match the inputs of this
806 route. XXX shouldn't they match the number of active signal
807 streams at the point of insertion?
811 porti->ensure_io (n_outputs (), n_inputs(), false, this);
814 // Ensure peak vector sizes before the plugin is activated
815 while (_peak_power.size() < potential_max_streams) {
816 _peak_power.push_back(0);
818 while (_visible_peak_power.size() < potential_max_streams) {
819 _visible_peak_power.push_back(0);
822 _redirects.push_back (redirect);
824 if (_reset_plugin_counts (err_streams)) {
825 _redirects.pop_back ();
826 _reset_plugin_counts (0); // it worked before we tried to add it ...
830 redirect->activate ();
831 redirect->active_changed.connect (mem_fun (*this, &Route::redirect_active_proxy));
834 if (redirect_max_outs != old_rmo || old_rmo == 0) {
839 redirects_changed (src); /* EMIT SIGNAL */
844 Route::add_redirects (const RedirectList& others, void *src, uint32_t* err_streams)
846 uint32_t old_rmo = redirect_max_outs;
848 if (!_session.engine().connected()) {
853 Glib::RWLock::WriterLock lm (redirect_lock);
855 RedirectList::iterator existing_end = _redirects.end();
858 uint32_t potential_max_streams = 0;
860 for (RedirectList::const_iterator i = others.begin(); i != others.end(); ++i) {
862 boost::shared_ptr<PluginInsert> pi;
864 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(*i)) != 0) {
867 uint32_t m = max(pi->input_streams(), pi->output_streams());
868 if (m > potential_max_streams)
869 potential_max_streams = m;
872 // Ensure peak vector sizes before the plugin is activated
873 while (_peak_power.size() < potential_max_streams) {
874 _peak_power.push_back(0);
876 while (_visible_peak_power.size() < potential_max_streams) {
877 _visible_peak_power.push_back(0);
880 _redirects.push_back (*i);
882 if (_reset_plugin_counts (err_streams)) {
884 _redirects.erase (existing_end, _redirects.end());
885 _reset_plugin_counts (0); // it worked before we tried to add it ...
890 (*i)->active_changed.connect (mem_fun (*this, &Route::redirect_active_proxy));
894 if (redirect_max_outs != old_rmo || old_rmo == 0) {
898 redirects_changed (src); /* EMIT SIGNAL */
903 Route::clear_redirects (void *src)
905 uint32_t old_rmo = redirect_max_outs;
907 if (!_session.engine().connected()) {
912 Glib::RWLock::WriterLock lm (redirect_lock);
916 if (redirect_max_outs != old_rmo) {
920 redirect_max_outs = 0;
921 _have_internal_generator = false;
922 redirects_changed (src); /* EMIT SIGNAL */
926 Route::remove_redirect (boost::shared_ptr<Redirect> redirect, void *src, uint32_t* err_streams)
928 uint32_t old_rmo = redirect_max_outs;
930 if (!_session.engine().connected()) {
934 redirect_max_outs = 0;
937 Glib::RWLock::WriterLock lm (redirect_lock);
938 RedirectList::iterator i;
939 bool removed = false;
941 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
942 if (*i == redirect) {
944 RedirectList::iterator tmp;
946 /* move along, see failure case for reset_plugin_counts()
947 where we may need to reinsert the redirect.
953 /* stop redirects that send signals to JACK ports
954 from causing noise as a result of no longer being
958 boost::shared_ptr<Send> send;
959 boost::shared_ptr<PortInsert> port_insert;
961 if ((send = boost::dynamic_pointer_cast<Send> (*i)) != 0) {
962 send->disconnect_inputs (this);
963 send->disconnect_outputs (this);
964 } else if ((port_insert = boost::dynamic_pointer_cast<PortInsert> (*i)) != 0) {
965 port_insert->disconnect_inputs (this);
966 port_insert->disconnect_outputs (this);
969 _redirects.erase (i);
982 if (_reset_plugin_counts (err_streams)) {
983 /* get back to where we where */
984 _redirects.insert (i, redirect);
985 /* we know this will work, because it worked before :) */
986 _reset_plugin_counts (0);
992 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
993 boost::shared_ptr<PluginInsert> pi;
995 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(*i)) != 0) {
996 if (pi->is_generator()) {
1002 _have_internal_generator = foo;
1005 if (old_rmo != redirect_max_outs) {
1009 redirects_changed (src); /* EMIT SIGNAL */
1014 Route::reset_plugin_counts (uint32_t* lpc)
1016 Glib::RWLock::WriterLock lm (redirect_lock);
1017 return _reset_plugin_counts (lpc);
1022 Route::_reset_plugin_counts (uint32_t* err_streams)
1024 RedirectList::iterator r;
1027 map<Placement,list<InsertCount> > insert_map;
1028 nframes_t initial_streams;
1030 redirect_max_outs = 0;
1034 /* divide inserts up by placement so we get the signal flow
1035 properly modelled. we need to do this because the _redirects
1036 list is not sorted by placement, and because other reasons may
1037 exist now or in the future for this separate treatment.
1040 for (r = _redirects.begin(); r != _redirects.end(); ++r) {
1042 boost::shared_ptr<Insert> insert;
1044 /* do this here in case we bomb out before we get to the end of
1048 redirect_max_outs = max ((*r)->output_streams (), redirect_max_outs);
1050 if ((insert = boost::dynamic_pointer_cast<Insert>(*r)) != 0) {
1052 insert_map[insert->placement()].push_back (InsertCount (insert));
1054 /* reset plugin counts back to one for now so
1055 that we have a predictable, controlled
1056 state to try to configure.
1059 boost::shared_ptr<PluginInsert> pi;
1061 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(insert)) != 0) {
1065 } else if (boost::dynamic_pointer_cast<Send> (*r) != 0) {
1078 /* Now process each placement in order, checking to see if we
1079 can really do what has been requested.
1084 if (check_some_plugin_counts (insert_map[PreFader], n_inputs (), err_streams)) {
1088 /* figure out the streams that will feed into PreFader */
1090 if (!insert_map[PreFader].empty()) {
1091 InsertCount& ic (insert_map[PreFader].back());
1092 initial_streams = ic.insert->compute_output_streams (ic.cnt);
1094 initial_streams = n_inputs ();
1099 if (check_some_plugin_counts (insert_map[PostFader], initial_streams, err_streams)) {
1103 /* OK, everything can be set up correctly, so lets do it */
1105 apply_some_plugin_counts (insert_map[PreFader]);
1106 apply_some_plugin_counts (insert_map[PostFader]);
1108 /* recompute max outs of any redirect */
1112 redirect_max_outs = 0;
1113 RedirectList::iterator prev = _redirects.end();
1115 for (r = _redirects.begin(); r != _redirects.end(); prev = r, ++r) {
1116 boost::shared_ptr<Send> s;
1118 if ((s = boost::dynamic_pointer_cast<Send> (*r)) != 0) {
1119 if (r == _redirects.begin()) {
1120 s->expect_inputs (n_inputs());
1122 s->expect_inputs ((*prev)->output_streams());
1126 redirect_max_outs = max ((*r)->output_streams (), redirect_max_outs);
1135 Route::apply_some_plugin_counts (list<InsertCount>& iclist)
1137 list<InsertCount>::iterator i;
1139 for (i = iclist.begin(); i != iclist.end(); ++i) {
1141 if ((*i).insert->configure_io ((*i).cnt, (*i).in, (*i).out)) {
1144 /* make sure that however many we have, they are all active */
1145 (*i).insert->activate ();
1152 Route::check_some_plugin_counts (list<InsertCount>& iclist, int32_t required_inputs, uint32_t* err_streams)
1154 list<InsertCount>::iterator i;
1156 for (i = iclist.begin(); i != iclist.end(); ++i) {
1158 if (((*i).cnt = (*i).insert->can_support_input_configuration (required_inputs)) < 0) {
1160 *err_streams = required_inputs;
1165 (*i).in = required_inputs;
1166 (*i).out = (*i).insert->compute_output_streams ((*i).cnt);
1168 required_inputs = (*i).out;
1175 Route::copy_redirects (const Route& other, Placement placement, uint32_t* err_streams)
1177 uint32_t old_rmo = redirect_max_outs;
1183 RedirectList to_be_deleted;
1186 Glib::RWLock::WriterLock lm (redirect_lock);
1187 RedirectList::iterator tmp;
1188 RedirectList the_copy;
1190 the_copy = _redirects;
1192 /* remove all relevant redirects */
1194 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ) {
1198 if ((*i)->placement() == placement) {
1199 to_be_deleted.push_back (*i);
1200 _redirects.erase (i);
1206 /* now copy the relevant ones from "other" */
1208 for (RedirectList::const_iterator i = other._redirects.begin(); i != other._redirects.end(); ++i) {
1209 if ((*i)->placement() == placement) {
1210 _redirects.push_back (Redirect::clone (*i));
1214 /* reset plugin stream handling */
1216 if (_reset_plugin_counts (err_streams)) {
1218 /* FAILED COPY ATTEMPT: we have to restore order */
1220 /* delete all cloned redirects */
1222 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ) {
1227 if ((*i)->placement() == placement) {
1228 _redirects.erase (i);
1234 /* restore the natural order */
1236 _redirects = the_copy;
1237 redirect_max_outs = old_rmo;
1239 /* we failed, even though things are OK again */
1245 /* SUCCESSFUL COPY ATTEMPT: delete the redirects we removed pre-copy */
1246 to_be_deleted.clear ();
1250 if (redirect_max_outs != old_rmo || old_rmo == 0) {
1254 redirects_changed (this); /* EMIT SIGNAL */
1259 Route::all_redirects_flip ()
1261 Glib::RWLock::ReaderLock lm (redirect_lock);
1263 if (_redirects.empty()) {
1267 bool first_is_on = _redirects.front()->active();
1269 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
1270 (*i)->set_active (!first_is_on, this);
1275 Route::all_redirects_active (bool state)
1277 Glib::RWLock::ReaderLock lm (redirect_lock);
1279 if (_redirects.empty()) {
1283 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
1284 (*i)->set_active (state, this);
1288 struct RedirectSorter {
1289 bool operator() (boost::shared_ptr<const Redirect> a, boost::shared_ptr<const Redirect> b) {
1290 return a->sort_key() < b->sort_key();
1295 Route::sort_redirects (uint32_t* err_streams)
1298 RedirectSorter comparator;
1299 Glib::RWLock::WriterLock lm (redirect_lock);
1300 uint32_t old_rmo = redirect_max_outs;
1302 /* the sweet power of C++ ... */
1304 RedirectList as_it_was_before = _redirects;
1306 _redirects.sort (comparator);
1308 if (_reset_plugin_counts (err_streams)) {
1309 _redirects = as_it_was_before;
1310 redirect_max_outs = old_rmo;
1316 redirects_changed (this); /* EMIT SIGNAL */
1328 Route::get_template()
1330 return state(false);
1334 Route::state(bool full_state)
1336 XMLNode *node = new XMLNode("Route");
1338 RedirectList:: iterator i;
1342 snprintf (buf, sizeof (buf), "0x%x", _flags);
1343 node->add_property("flags", buf);
1346 node->add_property("default-type", _default_type.to_string());
1348 node->add_property("active", _active?"yes":"no");
1349 node->add_property("muted", _muted?"yes":"no");
1350 node->add_property("soloed", _soloed?"yes":"no");
1351 node->add_property("phase-invert", _phase_invert?"yes":"no");
1352 node->add_property("mute-affects-pre-fader", _mute_affects_pre_fader?"yes":"no");
1353 node->add_property("mute-affects-post-fader", _mute_affects_post_fader?"yes":"no");
1354 node->add_property("mute-affects-control-outs", _mute_affects_control_outs?"yes":"no");
1355 node->add_property("mute-affects-main-outs", _mute_affects_main_outs?"yes":"no");
1358 node->add_property("edit-group", _edit_group->name());
1361 node->add_property("mix-group", _mix_group->name());
1364 string order_string;
1365 OrderKeys::iterator x = order_keys.begin();
1367 while (x != order_keys.end()) {
1368 order_string += (*x).first;
1369 order_string += '=';
1370 snprintf (buf, sizeof(buf), "%ld", (*x).second);
1371 order_string += buf;
1375 if (x == order_keys.end()) {
1379 order_string += ':';
1381 node->add_property ("order-keys", order_string);
1383 node->add_child_nocopy (IO::state (full_state));
1384 node->add_child_nocopy (_solo_control.get_state ());
1385 node->add_child_nocopy (_mute_control.get_state ());
1387 if (_control_outs) {
1388 XMLNode* cnode = new XMLNode (X_("ControlOuts"));
1389 cnode->add_child_nocopy (_control_outs->state (full_state));
1390 node->add_child_nocopy (*cnode);
1393 if (_comment.length()) {
1394 XMLNode *cmt = node->add_child ("Comment");
1395 cmt->add_content (_comment);
1401 path = _session.snap_name();
1403 path += legalize_for_path (_name);
1404 path += ".automation";
1406 /* XXX we didn't ask for a state save, we asked for the current state.
1410 if (save_automation (path)) {
1411 error << _("Could not get state of route. Problem with save_automation") << endmsg;
1414 aevents = node->add_child ("Automation");
1415 aevents->add_property ("path", path);
1418 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
1419 node->add_child_nocopy((*i)->state (full_state));
1423 node->add_child_copy (*_extra_xml);
1430 Route::set_deferred_state ()
1433 XMLNodeConstIterator niter;
1435 if (!deferred_state) {
1439 nlist = deferred_state->children();
1441 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
1442 add_redirect_from_xml (**niter);
1445 delete deferred_state;
1450 Route::add_redirect_from_xml (const XMLNode& node)
1452 const XMLProperty *prop;
1454 if (node.name() == "Send") {
1458 boost::shared_ptr<Send> send (new Send (_session, node));
1459 add_redirect (send, this);
1462 catch (failed_constructor &err) {
1463 error << _("Send construction failed") << endmsg;
1467 } else if (node.name() == "Insert") {
1470 if ((prop = node.property ("type")) != 0) {
1472 boost::shared_ptr<Insert> insert;
1474 if (prop->value() == "ladspa" || prop->value() == "Ladspa" || prop->value() == "vst") {
1476 insert.reset (new PluginInsert(_session, node));
1478 } else if (prop->value() == "port") {
1481 insert.reset (new PortInsert (_session, node));
1485 error << string_compose(_("unknown Insert type \"%1\"; ignored"), prop->value()) << endmsg;
1488 add_redirect (insert, this);
1491 error << _("Insert XML node has no type property") << endmsg;
1495 catch (failed_constructor &err) {
1496 warning << _("insert could not be created. Ignored.") << endmsg;
1503 Route::set_state (const XMLNode& node)
1506 XMLNodeConstIterator niter;
1508 XMLPropertyList plist;
1509 const XMLProperty *prop;
1511 if (node.name() != "Route"){
1512 error << string_compose(_("Bad node sent to Route::set_state() [%1]"), node.name()) << endmsg;
1516 if ((prop = node.property ("flags")) != 0) {
1518 sscanf (prop->value().c_str(), "0x%x", &x);
1524 if ((prop = node.property ("default-type")) != 0) {
1525 _default_type = DataType(prop->value());
1526 assert(_default_type != DataType::NIL);
1529 if ((prop = node.property ("phase-invert")) != 0) {
1530 set_phase_invert(prop->value()=="yes"?true:false, this);
1533 if ((prop = node.property ("active")) != 0) {
1534 set_active (prop->value() == "yes");
1537 if ((prop = node.property ("muted")) != 0) {
1538 bool yn = prop->value()=="yes"?true:false;
1540 /* force reset of mute status */
1544 mute_gain = desired_mute_gain;
1547 if ((prop = node.property ("soloed")) != 0) {
1548 bool yn = prop->value()=="yes"?true:false;
1550 /* force reset of solo status */
1553 set_solo (yn, this);
1554 solo_gain = desired_solo_gain;
1557 if ((prop = node.property ("mute-affects-pre-fader")) != 0) {
1558 _mute_affects_pre_fader = (prop->value()=="yes")?true:false;
1561 if ((prop = node.property ("mute-affects-post-fader")) != 0) {
1562 _mute_affects_post_fader = (prop->value()=="yes")?true:false;
1565 if ((prop = node.property ("mute-affects-control-outs")) != 0) {
1566 _mute_affects_control_outs = (prop->value()=="yes")?true:false;
1569 if ((prop = node.property ("mute-affects-main-outs")) != 0) {
1570 _mute_affects_main_outs = (prop->value()=="yes")?true:false;
1573 if ((prop = node.property ("edit-group")) != 0) {
1574 RouteGroup* edit_group = _session.edit_group_by_name(prop->value());
1575 if(edit_group == 0) {
1576 error << string_compose(_("Route %1: unknown edit group \"%2 in saved state (ignored)"), _name, prop->value()) << endmsg;
1578 set_edit_group(edit_group, this);
1582 if ((prop = node.property ("order-keys")) != 0) {
1586 string::size_type colon, equal;
1587 string remaining = prop->value();
1589 while (remaining.length()) {
1591 if ((equal = remaining.find_first_of ('=')) == string::npos || equal == remaining.length()) {
1592 error << string_compose (_("badly formed order key string in state file! [%1] ... ignored."), remaining)
1595 if (sscanf (remaining.substr (equal+1).c_str(), "%ld", &n) != 1) {
1596 error << string_compose (_("badly formed order key string in state file! [%1] ... ignored."), remaining)
1599 set_order_key (remaining.substr (0, equal), n);
1603 colon = remaining.find_first_of (':');
1605 if (colon != string::npos) {
1606 remaining = remaining.substr (colon+1);
1613 nlist = node.children();
1615 if (deferred_state) {
1616 delete deferred_state;
1619 deferred_state = new XMLNode("deferred state");
1621 /* set parent class properties before anything else */
1623 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
1627 if (child->name() == IO::state_node_name) {
1629 IO::set_state (*child);
1634 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
1638 if (child->name() == "Send") {
1641 if (!IO::ports_legal) {
1643 deferred_state->add_child_copy (*child);
1646 add_redirect_from_xml (*child);
1649 } else if (child->name() == "Insert") {
1651 if (!IO::ports_legal) {
1653 deferred_state->add_child_copy (*child);
1657 add_redirect_from_xml (*child);
1660 } else if (child->name() == "Automation") {
1662 XMLPropertyList plist;
1663 XMLPropertyConstIterator piter;
1666 plist = child->properties();
1667 for (piter = plist.begin(); piter != plist.end(); ++piter) {
1669 if (prop->name() == "path") {
1670 load_automation (prop->value());
1674 } else if (child->name() == "ControlOuts") {
1676 string coutname = _name;
1677 coutname += _("[control]");
1679 _control_outs = new IO (_session, coutname);
1680 _control_outs->set_state (**(child->children().begin()));
1682 } else if (child->name() == "Comment") {
1684 /* XXX this is a terrible API design in libxml++ */
1686 XMLNode *cmt = *(child->children().begin());
1687 _comment = cmt->content();
1689 } else if (child->name() == "extra") {
1690 _extra_xml = new XMLNode (*child);
1691 } else if (child->name() == "solo") {
1692 _solo_control.set_state (*child);
1693 _session.add_controllable (&_solo_control);
1694 } else if (child->name() == "mute") {
1695 _mute_control.set_state (*child);
1696 _session.add_controllable (&_mute_control);
1700 if ((prop = node.property ("mix-group")) != 0) {
1701 RouteGroup* mix_group = _session.mix_group_by_name(prop->value());
1702 if (mix_group == 0) {
1703 error << string_compose(_("Route %1: unknown mix group \"%2 in saved state (ignored)"), _name, prop->value()) << endmsg;
1705 set_mix_group(mix_group, this);
1713 Route::curve_reallocate ()
1715 // _gain_automation_curve.finish_resize ();
1716 // _pan_automation_curve.finish_resize ();
1720 Route::silence (nframes_t nframes, nframes_t offset)
1724 // reset_peak_meters ();
1726 IO::silence (nframes, offset);
1728 if (_control_outs) {
1729 _control_outs->silence (nframes, offset);
1733 Glib::RWLock::ReaderLock lm (redirect_lock, Glib::TRY_LOCK);
1736 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
1737 boost::shared_ptr<PluginInsert> pi;
1738 if (!_active && (pi = boost::dynamic_pointer_cast<PluginInsert> (*i)) != 0) {
1739 // skip plugins, they don't need anything when we're not active
1743 (*i)->silence (nframes, offset);
1746 if (nframes == _session.get_block_size() && offset == 0) {
1756 Route::set_control_outs (const vector<string>& ports)
1758 Glib::Mutex::Lock lm (control_outs_lock);
1759 vector<string>::const_iterator i;
1761 if (_control_outs) {
1762 delete _control_outs;
1766 if (ports.empty()) {
1770 string coutname = _name;
1771 coutname += _("[control]");
1773 _control_outs = new IO (_session, coutname);
1775 /* our control outs need as many outputs as we
1776 have outputs. we track the changes in ::output_change_handler().
1779 _control_outs->ensure_io (0, n_outputs(), true, this);
1785 Route::set_edit_group (RouteGroup *eg, void *src)
1788 if (eg == _edit_group) {
1793 _edit_group->remove (this);
1796 if ((_edit_group = eg) != 0) {
1797 _edit_group->add (this);
1800 _session.set_dirty ();
1801 edit_group_changed (src); /* EMIT SIGNAL */
1805 Route::drop_edit_group (void *src)
1808 _session.set_dirty ();
1809 edit_group_changed (src); /* EMIT SIGNAL */
1813 Route::set_mix_group (RouteGroup *mg, void *src)
1816 if (mg == _mix_group) {
1821 _mix_group->remove (this);
1824 if ((_mix_group = mg) != 0) {
1825 _mix_group->add (this);
1828 _session.set_dirty ();
1829 mix_group_changed (src); /* EMIT SIGNAL */
1833 Route::drop_mix_group (void *src)
1836 _session.set_dirty ();
1837 mix_group_changed (src); /* EMIT SIGNAL */
1841 Route::set_comment (string cmt, void *src)
1844 comment_changed (src);
1845 _session.set_dirty ();
1849 Route::feeds (boost::shared_ptr<Route> other)
1854 uint32_t no = self.n_outputs();
1855 uint32_t ni = other->n_inputs ();
1857 for (i = 0; i < no; ++i) {
1858 for (j = 0; j < ni; ++j) {
1859 if (self.output(i)->connected_to (other->input(j)->name())) {
1865 /* check Redirects which may also interconnect Routes */
1867 for (RedirectList::iterator r = _redirects.begin(); r != _redirects.end(); r++) {
1869 no = (*r)->n_outputs();
1871 for (i = 0; i < no; ++i) {
1872 for (j = 0; j < ni; ++j) {
1873 if ((*r)->output(i)->connected_to (other->input (j)->name())) {
1880 /* check for control room outputs which may also interconnect Routes */
1882 if (_control_outs) {
1884 no = _control_outs->n_outputs();
1886 for (i = 0; i < no; ++i) {
1887 for (j = 0; j < ni; ++j) {
1888 if (_control_outs->output(i)->connected_to (other->input (j)->name())) {
1899 Route::set_mute_config (mute_type t, bool onoff, void *src)
1903 _mute_affects_pre_fader = onoff;
1904 pre_fader_changed(src); /* EMIT SIGNAL */
1908 _mute_affects_post_fader = onoff;
1909 post_fader_changed(src); /* EMIT SIGNAL */
1913 _mute_affects_control_outs = onoff;
1914 control_outs_changed(src); /* EMIT SIGNAL */
1918 _mute_affects_main_outs = onoff;
1919 main_outs_changed(src); /* EMIT SIGNAL */
1925 Route::get_mute_config (mute_type t)
1931 onoff = _mute_affects_pre_fader;
1934 onoff = _mute_affects_post_fader;
1937 onoff = _mute_affects_control_outs;
1940 onoff = _mute_affects_main_outs;
1948 Route::set_active (bool yn)
1951 active_changed(); /* EMIT SIGNAL */
1955 Route::handle_transport_stopped (bool abort_ignored, bool did_locate, bool can_flush_redirects)
1957 nframes_t now = _session.transport_frame();
1960 Glib::RWLock::ReaderLock lm (redirect_lock);
1962 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
1964 if (Config->get_plugins_stop_with_transport() && can_flush_redirects) {
1965 (*i)->deactivate ();
1969 (*i)->transport_stopped (now);
1973 IO::transport_stopped (now);
1975 _roll_delay = _initial_delay;
1979 Route::get_memento() const
1981 void (Route::*pmf)(state_id_t) = &Route::set_state;
1982 return sigc::bind (mem_fun (*(const_cast<Route *>(this)), pmf), _current_state_id);
1986 Route::set_state (state_id_t id)
1992 Route::input_change_handler (IOChange change, void *ignored)
1994 if (change & ConfigurationChanged) {
1995 reset_plugin_counts (0);
2000 Route::output_change_handler (IOChange change, void *ignored)
2002 if (change & ConfigurationChanged) {
2003 if (_control_outs) {
2004 _control_outs->ensure_io (0, n_outputs(), true, this);
2007 reset_plugin_counts (0);
2012 Route::pans_required () const
2014 if (n_outputs() < 2) {
2018 return max (n_inputs (), redirect_max_outs);
2022 Route::no_roll (nframes_t nframes, nframes_t start_frame, nframes_t end_frame, nframes_t offset,
2023 bool session_state_changing, bool can_record, bool rec_monitors_input)
2025 if (n_outputs() == 0) {
2029 if (session_state_changing || !_active) {
2030 silence (nframes, offset);
2034 apply_gain_automation = false;
2037 passthru (start_frame, end_frame, nframes, offset, 0, false);
2039 silence (nframes, offset);
2046 Route::check_initial_delay (nframes_t nframes, nframes_t& offset, nframes_t& transport_frame)
2048 if (_roll_delay > nframes) {
2050 _roll_delay -= nframes;
2051 silence (nframes, offset);
2052 /* transport frame is not legal for caller to use */
2055 } else if (_roll_delay > 0) {
2057 nframes -= _roll_delay;
2059 silence (_roll_delay, offset);
2061 offset += _roll_delay;
2062 transport_frame += _roll_delay;
2071 Route::roll (nframes_t nframes, nframes_t start_frame, nframes_t end_frame, nframes_t offset, int declick,
2072 bool can_record, bool rec_monitors_input)
2074 if ((n_outputs() == 0 && _redirects.empty()) || n_inputs() == 0 || !_active) {
2075 silence (nframes, offset);
2079 nframes_t unused = 0;
2081 if ((nframes = check_initial_delay (nframes, offset, unused)) == 0) {
2087 apply_gain_automation = false;
2090 Glib::Mutex::Lock am (automation_lock, Glib::TRY_LOCK);
2092 if (am.locked() && _session.transport_rolling()) {
2094 nframes_t start_frame = end_frame - nframes;
2096 if (gain_automation_playback()) {
2097 apply_gain_automation = _gain_automation_curve.rt_safe_get_vector (start_frame, end_frame, _session.gain_automation_buffer(), nframes);
2102 passthru (start_frame, end_frame, nframes, offset, declick, false);
2108 Route::silent_roll (nframes_t nframes, nframes_t start_frame, nframes_t end_frame, nframes_t offset,
2109 bool can_record, bool rec_monitors_input)
2111 silence (nframes, offset);
2116 Route::toggle_monitor_input ()
2118 for (vector<Port*>::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
2119 (*i)->ensure_monitor_input(!(*i)->monitoring_input());
2124 Route::has_external_redirects () const
2126 boost::shared_ptr<const PortInsert> pi;
2128 for (RedirectList::const_iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2129 if ((pi = boost::dynamic_pointer_cast<const PortInsert>(*i)) != 0) {
2131 uint32_t no = pi->n_outputs();
2133 for (uint32_t n = 0; n < no; ++n) {
2135 string port_name = pi->output(n)->name();
2136 string client_name = port_name.substr (0, port_name.find(':'));
2138 /* only say "yes" if the redirect is actually in use */
2140 if (client_name != "ardour" && pi->active()) {
2151 Route::flush_redirects ()
2153 /* XXX shouldn't really try to take this lock, since
2154 this is called from the RT audio thread.
2157 Glib::RWLock::ReaderLock lm (redirect_lock);
2159 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2160 (*i)->deactivate ();
2166 Route::set_meter_point (MeterPoint p, void *src)
2168 if (_meter_point != p) {
2170 meter_change (src); /* EMIT SIGNAL */
2171 _session.set_dirty ();
2176 Route::update_total_latency ()
2180 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2181 if ((*i)->active ()) {
2182 _own_latency += (*i)->latency ();
2186 set_port_latency (_own_latency);
2188 /* this (virtual) function is used for pure Routes,
2189 not derived classes like AudioTrack. this means
2190 that the data processed here comes from an input
2191 port, not prerecorded material, and therefore we
2192 have to take into account any input latency.
2195 _own_latency += input_latency ();
2197 return _own_latency;
2201 Route::set_latency_delay (nframes_t longest_session_latency)
2203 _initial_delay = longest_session_latency - _own_latency;
2205 if (_session.transport_stopped()) {
2206 _roll_delay = _initial_delay;
2210 Route::ToggleControllable::ToggleControllable (std::string name, Route& s, ToggleType tp)
2211 : Controllable (name), route (s), type(tp)
2217 Route::ToggleControllable::set_value (float val)
2219 bool bval = ((val >= 0.5f) ? true: false);
2223 route.set_mute (bval, this);
2226 route.set_solo (bval, this);
2234 Route::ToggleControllable::get_value (void) const
2240 val = route.muted() ? 1.0f : 0.0f;
2243 val = route.soloed() ? 1.0f : 0.0f;
2253 Route::set_block_size (nframes_t nframes)
2255 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2256 (*i)->set_block_size (nframes);
2261 Route::redirect_active_proxy (Redirect* ignored, void* ignored_src)
2263 _session.update_latency_compensation (false, false);
2267 Route::protect_automation ()
2269 switch (gain_automation_state()) {
2272 set_gain_automation_state (Off);
2278 switch (panner().automation_state ()) {
2281 panner().set_automation_state (Off);
2287 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2288 boost::shared_ptr<PluginInsert> pi;
2289 if ((pi = boost::dynamic_pointer_cast<PluginInsert> (*i)) != 0) {
2290 pi->protect_automation ();
2296 Route::set_pending_declick (int declick)
2299 /* this call is not allowed to turn off a pending declick unless "force" is true */
2301 _pending_declick = declick;
2303 // cerr << _name << ": after setting to " << declick << " pending declick = " << _pending_declick << endl;
2305 _pending_declick = 0;