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/buffer.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/audio_port.h>
41 #include <ardour/ladspa_plugin.h>
42 #include <ardour/panner.h>
43 #include <ardour/dB.h>
44 #include <ardour/mix.h>
45 #include <ardour/amp.h>
46 #include <ardour/meter.h>
47 #include <ardour/buffer_set.h>
51 using namespace ARDOUR;
55 uint32_t Route::order_key_cnt = 0;
58 Route::Route (Session& sess, string name, int input_min, int input_max, int output_min, int output_max, Flag flg, DataType default_type)
59 : IO (sess, name, input_min, input_max, output_min, output_max, default_type),
61 _solo_control (*this, ToggleControllable::SoloControl),
62 _mute_control (*this, ToggleControllable::MuteControl)
67 Route::Route (Session& sess, const XMLNode& node)
69 _solo_control (*this, ToggleControllable::SoloControl),
70 _mute_control (*this, ToggleControllable::MuteControl)
79 redirect_max_outs.reset();
83 _phase_invert = false;
84 order_keys[N_("signal")] = order_key_cnt++;
87 _meter_point = MeterPostFader;
91 _have_internal_generator = false;
93 _pending_declick = true;
94 _remote_control_id = 0;
99 _mute_affects_pre_fader = Config->get_mute_affects_pre_fader();
100 _mute_affects_post_fader = Config->get_mute_affects_post_fader();
101 _mute_affects_control_outs = Config->get_mute_affects_control_outs();
102 _mute_affects_main_outs = Config->get_mute_affects_main_outs();
105 desired_solo_gain = 1.0;
107 desired_mute_gain = 1.0;
111 input_changed.connect (mem_fun (this, &Route::input_change_handler));
112 output_changed.connect (mem_fun (this, &Route::output_change_handler));
117 clear_redirects (this);
120 delete _control_outs;
125 Route::set_remote_control_id (uint32_t id)
127 if (id != _remote_control_id) {
128 _remote_control_id = id;
129 RemoteControlIDChanged ();
134 Route::remote_control_id() const
136 return _remote_control_id;
140 Route::order_key (string name) const
142 OrderKeys::const_iterator i;
144 if ((i = order_keys.find (name)) == order_keys.end()) {
152 Route::set_order_key (string name, long n)
154 order_keys[name] = n;
155 _session.set_dirty ();
159 Route::inc_gain (gain_t fraction, void *src)
161 IO::inc_gain (fraction, src);
165 Route::set_gain (gain_t val, void *src)
167 if (src != 0 && _mix_group && src != _mix_group && _mix_group->is_active()) {
169 if (_mix_group->is_relative()) {
172 gain_t usable_gain = gain();
173 if (usable_gain < 0.000001f) {
174 usable_gain=0.000001f;
178 if (delta < 0.000001f) {
182 delta -= usable_gain;
184 if (delta == 0.0f) return;
186 gain_t factor = delta / usable_gain;
189 factor = _mix_group->get_max_factor(factor);
190 if (factor == 0.0f) {
195 factor = _mix_group->get_min_factor(factor);
196 if (factor == 0.0f) {
202 _mix_group->apply (&Route::inc_gain, factor, _mix_group);
206 _mix_group->apply (&Route::set_gain, val, _mix_group);
216 IO::set_gain (val, src);
219 /** Process this route for one (sub) cycle (process thread)
221 * @param bufs Scratch buffers to use for the signal path
222 * @param start_frame Initial transport frame
223 * @param end_frame Final transport frame
224 * @param nframes Number of frames to output (to ports)
225 * @param offset Output offset (of port buffers, for split cycles)
227 * Note that (end_frame - start_frame) may not be equal to nframes when the
228 * transport speed isn't 1.0 (eg varispeed).
231 Route::process_output_buffers (BufferSet& bufs,
232 jack_nframes_t start_frame, jack_nframes_t end_frame,
233 jack_nframes_t nframes, jack_nframes_t offset, bool with_redirects, int declick,
236 // This is definitely very audio-only for now
237 assert(_default_type == DataType::AUDIO);
239 RedirectList::iterator i;
240 bool post_fader_work = false;
241 bool mute_declick_applied = false;
243 vector<Sample*>::iterator bufiter;
247 bool no_monitor = (Config->get_use_hardware_monitoring() || !Config->get_use_sw_monitoring ());
248 gain_t* gab = _session.gain_automation_buffer();
250 declick = _pending_declick;
253 Glib::Mutex::Lock cm (control_outs_lock, Glib::TRY_LOCK);
263 Glib::Mutex::Lock dm (declick_lock, Glib::TRY_LOCK);
266 dmg = desired_mute_gain;
267 dsg = desired_solo_gain;
276 /* ----------------------------------------------------------------------------------------------------
277 GLOBAL DECLICK (for transport changes etc.)
278 -------------------------------------------------------------------------------------------------- */
281 Amp::run (bufs, nframes, 0.0, 1.0, _phase_invert);
282 _pending_declick = 0;
283 } else if (declick < 0) {
284 Amp::run (bufs, nframes, 1.0, 0.0, _phase_invert);
285 _pending_declick = 0;
288 /* no global declick */
290 if (solo_gain != dsg) {
291 Amp::run (bufs, nframes, solo_gain, dsg, _phase_invert);
297 /* ----------------------------------------------------------------------------------------------------
298 INPUT METERING & MONITORING
299 -------------------------------------------------------------------------------------------------- */
301 if (meter && (_meter_point == MeterInput)) {
302 _meter->run(bufs, nframes);
305 if (!_soloed && _mute_affects_pre_fader && (mute_gain != dmg)) {
306 Amp::run (bufs, nframes, mute_gain, dmg, _phase_invert);
308 mute_declick_applied = true;
311 if ((_meter_point == MeterInput) && co) {
313 solo_audible = dsg > 0;
314 mute_audible = dmg > 0;// || !_mute_affects_pre_fader;
316 if ( // muted by solo of another track
320 // muted by mute of this track
324 // rec-enabled but not s/w monitoring
326 // TODO: this is probably wrong
328 (no_monitor && record_enabled() && (!_session.get_auto_input() || _session.actively_recording()))
332 co->silence (nframes, offset);
336 co->deliver_output (bufs, start_frame, end_frame, nframes, offset);
341 /* ----------------------------------------------------------------------------------------------------
343 -------------------------------------------------------------------------------------------------- */
345 if (with_redirects) {
346 Glib::RWLock::ReaderLock rm (redirect_lock, Glib::TRY_LOCK);
348 if (mute_gain > 0 || !_mute_affects_pre_fader) {
349 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
350 switch ((*i)->placement()) {
352 (*i)->run (bufs, start_frame, end_frame, nframes, offset);
355 post_fader_work = true;
360 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
361 switch ((*i)->placement()) {
363 (*i)->silence (nframes, offset);
366 post_fader_work = true;
375 if (!_soloed && (mute_gain != dmg) && !mute_declick_applied && _mute_affects_post_fader) {
376 Amp::run (bufs, nframes, mute_gain, dmg, _phase_invert);
378 mute_declick_applied = true;
381 /* ----------------------------------------------------------------------------------------------------
382 PRE-FADER METERING & MONITORING
383 -------------------------------------------------------------------------------------------------- */
385 if (meter && (_meter_point == MeterPreFader)) {
386 _meter->run(bufs, nframes);
390 if ((_meter_point == MeterPreFader) && co) {
392 solo_audible = dsg > 0;
393 mute_audible = dmg > 0 || !_mute_affects_pre_fader;
395 if ( // muted by solo of another track
399 // muted by mute of this track
403 // rec-enabled but not s/w monitoring
405 (no_monitor && record_enabled() && (!_session.get_auto_input() || _session.actively_recording()))
409 co->silence (nframes, offset);
413 co->deliver_output (bufs, start_frame, end_frame, nframes, offset);
418 /* ----------------------------------------------------------------------------------------------------
420 -------------------------------------------------------------------------------------------------- */
422 /* if not recording or recording and requiring any monitor signal, then apply gain */
424 if ( // not recording
426 !(record_enabled() && _session.actively_recording()) ||
430 // h/w monitoring not in use
432 (!Config->get_use_hardware_monitoring() &&
434 // AND software monitoring required
436 Config->get_use_sw_monitoring())) {
438 if (apply_gain_automation) {
441 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
442 Sample* const sp = i->data(nframes);
444 for (jack_nframes_t nx = 0; nx < nframes; ++nx) {
449 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
450 Sample* const sp = i->data(nframes);
452 for (jack_nframes_t nx = 0; nx < nframes; ++nx) {
458 if (apply_gain_automation && _session.transport_rolling() && nframes > 0) {
459 _effective_gain = gab[nframes-1];
464 /* manual (scalar) gain */
468 Amp::run (bufs, nframes, _gain, dg, _phase_invert);
471 } else if (_gain != 0 && (_phase_invert || _gain != 1.0)) {
473 /* no need to interpolate current gain value,
474 but its non-unity, so apply it. if the gain
475 is zero, do nothing because we'll ship silence
487 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
488 Sample* const sp = i->data(nframes);
489 apply_gain_to_buffer(sp,nframes,this_gain);
492 } else if (_gain == 0) {
493 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
501 /* actively recording, no monitoring required; leave buffers as-is to save CPU cycles */
505 /* ----------------------------------------------------------------------------------------------------
507 -------------------------------------------------------------------------------------------------- */
509 /* note that post_fader_work cannot be true unless with_redirects was also true, so don't test both */
511 if (post_fader_work) {
513 Glib::RWLock::ReaderLock rm (redirect_lock, Glib::TRY_LOCK);
515 if (mute_gain > 0 || !_mute_affects_post_fader) {
516 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
517 switch ((*i)->placement()) {
521 (*i)->run (bufs, start_frame, end_frame, nframes, offset);
526 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
527 switch ((*i)->placement()) {
531 (*i)->silence (nframes, offset);
539 if (!_soloed && (mute_gain != dmg) && !mute_declick_applied && _mute_affects_control_outs) {
540 Amp::run (bufs, nframes, mute_gain, dmg, _phase_invert);
542 mute_declick_applied = true;
545 /* ----------------------------------------------------------------------------------------------------
547 -------------------------------------------------------------------------------------------------- */
549 if ((_meter_point == MeterPostFader) && co) {
551 solo_audible = solo_gain > 0;
552 mute_audible = dmg > 0 || !_mute_affects_control_outs;
554 if ( // silent anyway
556 (_gain == 0 && !apply_gain_automation) ||
558 // muted by solo of another track
562 // muted by mute of this track
566 // recording but not s/w monitoring
568 (no_monitor && record_enabled() && (!_session.get_auto_input() || _session.actively_recording()))
572 co->silence (nframes, offset);
576 co->deliver_output (bufs, start_frame, end_frame, nframes, offset);
580 /* ----------------------------------------------------------------------
582 ----------------------------------------------------------------------*/
584 if (!_soloed && (mute_gain != dmg) && !mute_declick_applied && _mute_affects_main_outs) {
585 Amp::run (bufs, nframes, mute_gain, dmg, _phase_invert);
587 mute_declick_applied = true;
590 /* ----------------------------------------------------------------------------------------------------
592 -------------------------------------------------------------------------------------------------- */
594 solo_audible = dsg > 0;
595 mute_audible = dmg > 0 || !_mute_affects_main_outs;
597 if (n_outputs().get(_default_type) == 0) {
601 } else if (no_monitor && record_enabled() && (!_session.get_auto_input() || _session.actively_recording())) {
603 IO::silence (nframes, offset);
607 if ( // silent anyway
609 (_gain == 0 && !apply_gain_automation) ||
611 // muted by solo of another track, but not using control outs for solo
613 (!solo_audible && (_session.solo_model() != Session::SoloBus)) ||
615 // muted by mute of this track
621 /* don't use Route::silence() here, because that causes
622 all outputs (sends, port inserts, etc. to be silent).
625 if (_meter_point == MeterPostFader) {
626 peak_meter().reset();
629 IO::silence (nframes, offset);
633 deliver_output(bufs, start_frame, end_frame, nframes, offset);
639 /* ----------------------------------------------------------------------------------------------------
641 -------------------------------------------------------------------------------------------------- */
643 if (meter && (_meter_point == MeterPostFader)) {
644 if ((_gain == 0 && !apply_gain_automation) || dmg == 0) {
647 _meter->run(output_buffers(), nframes, offset);
653 Route::n_process_buffers ()
655 //return max (n_inputs(), redirect_max_outs);
656 return n_inputs(); // FIXME?
660 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)
662 BufferSet& bufs = _session.get_scratch_buffers(n_process_buffers());
666 collect_input (bufs, nframes, offset);
668 #define meter_stream meter_first
671 _meter->run(bufs, nframes);
672 meter_stream = false;
677 process_output_buffers (bufs, start_frame, end_frame, nframes, offset, true, declick, meter_stream);
683 Route::passthru_silence (jack_nframes_t start_frame, jack_nframes_t end_frame, jack_nframes_t nframes, jack_nframes_t offset, int declick, bool meter)
685 process_output_buffers (_session.get_silent_buffers (n_process_buffers()), start_frame, end_frame, nframes, offset, true, declick, meter);
689 Route::set_solo (bool yn, void *src)
695 if (_mix_group && src != _mix_group && _mix_group->is_active()) {
696 _mix_group->apply (&Route::set_solo, yn, _mix_group);
702 solo_changed (src); /* EMIT SIGNAL */
703 _solo_control.Changed (); /* EMIT SIGNAL */
708 Route::set_solo_mute (bool yn)
710 Glib::Mutex::Lock lm (declick_lock);
712 /* Called by Session in response to another Route being soloed.
715 desired_solo_gain = (yn?0.0:1.0);
719 Route::set_solo_safe (bool yn, void *src)
721 if (_solo_safe != yn) {
723 solo_safe_changed (src); /* EMIT SIGNAL */
728 Route::set_mute (bool yn, void *src)
731 if (_mix_group && src != _mix_group && _mix_group->is_active()) {
732 _mix_group->apply (&Route::set_mute, yn, _mix_group);
738 mute_changed (src); /* EMIT SIGNAL */
740 _mute_control.Changed (); /* EMIT SIGNAL */
742 Glib::Mutex::Lock lm (declick_lock);
743 desired_mute_gain = (yn?0.0f:1.0f);
748 Route::add_redirect (boost::shared_ptr<Redirect> redirect, void *src, uint32_t* err_streams)
750 ChanCount old_rmo = redirect_max_outs;
752 if (!_session.engine().connected()) {
757 Glib::RWLock::WriterLock lm (redirect_lock);
759 boost::shared_ptr<PluginInsert> pi;
760 boost::shared_ptr<PortInsert> porti;
762 ChanCount potential_max_streams;
764 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(redirect)) != 0) {
767 if (pi->input_streams() == ChanCount::ZERO) {
768 /* generator plugin */
769 _have_internal_generator = true;
772 potential_max_streams = max(pi->input_streams(), pi->output_streams());
774 } else if ((porti = boost::dynamic_pointer_cast<PortInsert>(redirect)) != 0) {
776 /* force new port inserts to start out with an i/o configuration
777 that matches this route's i/o configuration.
779 the "inputs" for the port are supposed to match the output
782 the "outputs" of the route should match the inputs of this
783 route. XXX shouldn't they match the number of active signal
784 streams at the point of insertion?
788 porti->ensure_io (n_outputs (), n_inputs(), false, this);
791 // Ensure peak vector sizes before the plugin is activated
792 _meter->setup(potential_max_streams);
794 _redirects.push_back (redirect);
796 if (_reset_plugin_counts (err_streams)) {
797 _redirects.pop_back ();
798 _reset_plugin_counts (0); // it worked before we tried to add it ...
802 redirect->activate ();
803 redirect->active_changed.connect (mem_fun (*this, &Route::redirect_active_proxy));
806 if (redirect_max_outs != old_rmo || old_rmo == ChanCount::ZERO) {
811 redirects_changed (src); /* EMIT SIGNAL */
816 Route::add_redirects (const RedirectList& others, void *src, uint32_t* err_streams)
818 ChanCount old_rmo = redirect_max_outs;
820 if (!_session.engine().connected()) {
825 Glib::RWLock::WriterLock lm (redirect_lock);
827 RedirectList::iterator existing_end = _redirects.end();
830 ChanCount potential_max_streams;
832 for (RedirectList::const_iterator i = others.begin(); i != others.end(); ++i) {
834 boost::shared_ptr<PluginInsert> pi;
836 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(*i)) != 0) {
839 ChanCount m = max(pi->input_streams(), pi->output_streams());
840 if (m > potential_max_streams)
841 potential_max_streams = m;
844 // Ensure peak vector sizes before the plugin is activated
845 _meter->setup(potential_max_streams);
847 _redirects.push_back (*i);
849 if (_reset_plugin_counts (err_streams)) {
851 _redirects.erase (existing_end, _redirects.end());
852 _reset_plugin_counts (0); // it worked before we tried to add it ...
857 (*i)->active_changed.connect (mem_fun (*this, &Route::redirect_active_proxy));
861 if (redirect_max_outs != old_rmo || old_rmo == ChanCount::ZERO) {
865 redirects_changed (src); /* EMIT SIGNAL */
870 Route::clear_redirects (void *src)
872 ChanCount old_rmo = redirect_max_outs;
874 if (!_session.engine().connected()) {
879 Glib::RWLock::WriterLock lm (redirect_lock);
883 if (redirect_max_outs != old_rmo) {
887 redirect_max_outs.reset();
888 _have_internal_generator = false;
889 redirects_changed (src); /* EMIT SIGNAL */
893 Route::remove_redirect (boost::shared_ptr<Redirect> redirect, void *src, uint32_t* err_streams)
895 ChanCount old_rmo = redirect_max_outs;
897 if (!_session.engine().connected()) {
901 redirect_max_outs.reset();
904 Glib::RWLock::WriterLock lm (redirect_lock);
905 RedirectList::iterator i;
906 bool removed = false;
908 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
909 if (*i == redirect) {
911 RedirectList::iterator tmp;
913 /* move along, see failure case for reset_plugin_counts()
914 where we may need to reinsert the redirect.
920 /* stop redirects that send signals to JACK ports
921 from causing noise as a result of no longer being
925 boost::shared_ptr<Send> send;
926 boost::shared_ptr<PortInsert> port_insert;
928 if ((send = boost::dynamic_pointer_cast<Send> (*i)) != 0) {
929 send->disconnect_inputs (this);
930 send->disconnect_outputs (this);
931 } else if ((port_insert = boost::dynamic_pointer_cast<PortInsert> (*i)) != 0) {
932 port_insert->disconnect_inputs (this);
933 port_insert->disconnect_outputs (this);
936 _redirects.erase (i);
949 if (_reset_plugin_counts (err_streams)) {
950 /* get back to where we where */
951 _redirects.insert (i, redirect);
952 /* we know this will work, because it worked before :) */
953 _reset_plugin_counts (0);
959 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
960 boost::shared_ptr<PluginInsert> pi;
962 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(*i)) != 0) {
963 if (pi->is_generator()) {
969 _have_internal_generator = foo;
972 if (old_rmo != redirect_max_outs) {
976 redirects_changed (src); /* EMIT SIGNAL */
981 Route::reset_plugin_counts (uint32_t* lpc)
983 Glib::RWLock::WriterLock lm (redirect_lock);
984 return _reset_plugin_counts (lpc);
989 Route::_reset_plugin_counts (uint32_t* err_streams)
991 RedirectList::iterator r;
994 map<Placement,list<InsertCount> > insert_map;
995 jack_nframes_t initial_streams;
997 redirect_max_outs.reset();
1001 /* divide inserts up by placement so we get the signal flow
1002 properly modelled. we need to do this because the _redirects
1003 list is not sorted by placement, and because other reasons may
1004 exist now or in the future for this separate treatment.
1007 for (r = _redirects.begin(); r != _redirects.end(); ++r) {
1009 boost::shared_ptr<Insert> insert;
1011 /* do this here in case we bomb out before we get to the end of
1015 redirect_max_outs = max ((*r)->output_streams (), redirect_max_outs);
1017 if ((insert = boost::dynamic_pointer_cast<Insert>(*r)) != 0) {
1019 insert_map[insert->placement()].push_back (InsertCount (insert));
1021 /* reset plugin counts back to one for now so
1022 that we have a predictable, controlled
1023 state to try to configure.
1026 boost::shared_ptr<PluginInsert> pi;
1028 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(insert)) != 0) {
1032 } else if (boost::dynamic_pointer_cast<Send> (*r) != 0) {
1045 /* Now process each placement in order, checking to see if we
1046 can really do what has been requested.
1051 if (check_some_plugin_counts (insert_map[PreFader], n_inputs ().get(_default_type), err_streams)) {
1055 /* figure out the streams that will feed into PreFader */
1057 if (!insert_map[PreFader].empty()) {
1058 InsertCount& ic (insert_map[PreFader].back());
1059 initial_streams = ic.insert->compute_output_streams (ic.cnt);
1061 initial_streams = n_inputs ().get(_default_type);
1066 if (check_some_plugin_counts (insert_map[PostFader], initial_streams, err_streams)) {
1070 /* OK, everything can be set up correctly, so lets do it */
1072 apply_some_plugin_counts (insert_map[PreFader]);
1073 apply_some_plugin_counts (insert_map[PostFader]);
1075 /* recompute max outs of any redirect */
1079 redirect_max_outs.reset();
1080 RedirectList::iterator prev = _redirects.end();
1082 for (r = _redirects.begin(); r != _redirects.end(); prev = r, ++r) {
1083 boost::shared_ptr<Send> s;
1085 if ((s = boost::dynamic_pointer_cast<Send> (*r)) != 0) {
1086 if (r == _redirects.begin()) {
1087 s->expect_inputs (n_inputs());
1089 s->expect_inputs ((*prev)->output_streams());
1093 redirect_max_outs = max ((*r)->output_streams(), redirect_max_outs);
1102 Route::apply_some_plugin_counts (list<InsertCount>& iclist)
1104 list<InsertCount>::iterator i;
1106 for (i = iclist.begin(); i != iclist.end(); ++i) {
1108 if ((*i).insert->configure_io ((*i).cnt, (*i).in, (*i).out)) {
1111 /* make sure that however many we have, they are all active */
1112 (*i).insert->activate ();
1119 Route::check_some_plugin_counts (list<InsertCount>& iclist, int32_t required_inputs, uint32_t* err_streams)
1121 list<InsertCount>::iterator i;
1123 for (i = iclist.begin(); i != iclist.end(); ++i) {
1125 if (((*i).cnt = (*i).insert->can_support_input_configuration (required_inputs)) < 0) {
1127 *err_streams = required_inputs;
1132 (*i).in = required_inputs;
1133 (*i).out = (*i).insert->compute_output_streams ((*i).cnt);
1135 required_inputs = (*i).out;
1142 Route::copy_redirects (const Route& other, Placement placement, uint32_t* err_streams)
1144 ChanCount old_rmo = redirect_max_outs;
1150 RedirectList to_be_deleted;
1153 Glib::RWLock::WriterLock lm (redirect_lock);
1154 RedirectList::iterator tmp;
1155 RedirectList the_copy;
1157 the_copy = _redirects;
1159 /* remove all relevant redirects */
1161 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ) {
1165 if ((*i)->placement() == placement) {
1166 to_be_deleted.push_back (*i);
1167 _redirects.erase (i);
1173 /* now copy the relevant ones from "other" */
1175 for (RedirectList::const_iterator i = other._redirects.begin(); i != other._redirects.end(); ++i) {
1176 if ((*i)->placement() == placement) {
1177 _redirects.push_back (Redirect::clone (*i));
1181 /* reset plugin stream handling */
1183 if (_reset_plugin_counts (err_streams)) {
1185 /* FAILED COPY ATTEMPT: we have to restore order */
1187 /* delete all cloned redirects */
1189 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ) {
1194 if ((*i)->placement() == placement) {
1195 _redirects.erase (i);
1201 /* restore the natural order */
1203 _redirects = the_copy;
1204 redirect_max_outs = old_rmo;
1206 /* we failed, even though things are OK again */
1212 /* SUCCESSFUL COPY ATTEMPT: delete the redirects we removed pre-copy */
1213 to_be_deleted.clear ();
1217 if (redirect_max_outs != old_rmo || old_rmo == ChanCount::ZERO) {
1221 redirects_changed (this); /* EMIT SIGNAL */
1226 Route::all_redirects_flip ()
1228 Glib::RWLock::ReaderLock lm (redirect_lock);
1230 if (_redirects.empty()) {
1234 bool first_is_on = _redirects.front()->active();
1236 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
1237 (*i)->set_active (!first_is_on, this);
1242 Route::all_redirects_active (bool state)
1244 Glib::RWLock::ReaderLock lm (redirect_lock);
1246 if (_redirects.empty()) {
1250 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
1251 (*i)->set_active (state, this);
1255 struct RedirectSorter {
1256 bool operator() (boost::shared_ptr<const Redirect> a, boost::shared_ptr<const Redirect> b) {
1257 return a->sort_key() < b->sort_key();
1262 Route::sort_redirects (uint32_t* err_streams)
1265 RedirectSorter comparator;
1266 Glib::RWLock::WriterLock lm (redirect_lock);
1267 ChanCount old_rmo = redirect_max_outs;
1269 /* the sweet power of C++ ... */
1271 RedirectList as_it_was_before = _redirects;
1273 _redirects.sort (comparator);
1275 if (_reset_plugin_counts (err_streams)) {
1276 _redirects = as_it_was_before;
1277 redirect_max_outs = old_rmo;
1283 redirects_changed (this); /* EMIT SIGNAL */
1295 Route::get_template()
1297 return state(false);
1301 Route::state(bool full_state)
1303 XMLNode *node = new XMLNode("Route");
1305 RedirectList:: iterator i;
1309 snprintf (buf, sizeof (buf), "0x%x", _flags);
1310 node->add_property("flags", buf);
1313 node->add_property("default-type", _default_type.to_string());
1315 node->add_property("active", _active?"yes":"no");
1316 node->add_property("muted", _muted?"yes":"no");
1317 node->add_property("soloed", _soloed?"yes":"no");
1318 node->add_property("phase-invert", _phase_invert?"yes":"no");
1319 node->add_property("mute-affects-pre-fader", _mute_affects_pre_fader?"yes":"no");
1320 node->add_property("mute-affects-post-fader", _mute_affects_post_fader?"yes":"no");
1321 node->add_property("mute-affects-control-outs", _mute_affects_control_outs?"yes":"no");
1322 node->add_property("mute-affects-main-outs", _mute_affects_main_outs?"yes":"no");
1325 node->add_property("edit-group", _edit_group->name());
1328 node->add_property("mix-group", _mix_group->name());
1331 string order_string;
1332 OrderKeys::iterator x = order_keys.begin();
1334 while (x != order_keys.end()) {
1335 order_string += (*x).first;
1336 order_string += '=';
1337 snprintf (buf, sizeof(buf), "%ld", (*x).second);
1338 order_string += buf;
1342 if (x == order_keys.end()) {
1346 order_string += ':';
1348 node->add_property ("order-keys", order_string);
1350 node->add_child_nocopy (IO::state (full_state));
1352 if (_control_outs) {
1353 XMLNode* cnode = new XMLNode (X_("ControlOuts"));
1354 cnode->add_child_nocopy (_control_outs->state (full_state));
1355 node->add_child_nocopy (*cnode);
1358 if (_comment.length()) {
1359 XMLNode *cmt = node->add_child ("Comment");
1360 cmt->add_content (_comment);
1366 path = _session.snap_name();
1368 path += legalize_for_path (_name);
1369 path += ".automation";
1371 /* XXX we didn't ask for a state save, we asked for the current state.
1375 if (save_automation (path)) {
1376 error << _("Could not get state of route. Problem with save_automation") << endmsg;
1379 aevents = node->add_child ("Automation");
1380 aevents->add_property ("path", path);
1383 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
1384 node->add_child_nocopy((*i)->state (full_state));
1388 node->add_child_copy (*_extra_xml);
1395 Route::set_deferred_state ()
1398 XMLNodeConstIterator niter;
1400 if (!deferred_state) {
1404 nlist = deferred_state->children();
1406 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
1407 add_redirect_from_xml (**niter);
1410 delete deferred_state;
1415 Route::add_redirect_from_xml (const XMLNode& node)
1417 const XMLProperty *prop;
1419 if (node.name() == "Send") {
1423 boost::shared_ptr<Send> send (new Send (_session, node));
1424 add_redirect (send, this);
1427 catch (failed_constructor &err) {
1428 error << _("Send construction failed") << endmsg;
1432 } else if (node.name() == "Insert") {
1435 if ((prop = node.property ("type")) != 0) {
1437 boost::shared_ptr<Insert> insert;
1439 if (prop->value() == "ladspa" || prop->value() == "Ladspa" || prop->value() == "vst") {
1441 insert.reset (new PluginInsert(_session, node));
1443 } else if (prop->value() == "port") {
1446 insert.reset (new PortInsert (_session, node));
1450 error << string_compose(_("unknown Insert type \"%1\"; ignored"), prop->value()) << endmsg;
1453 add_redirect (insert, this);
1456 error << _("Insert XML node has no type property") << endmsg;
1460 catch (failed_constructor &err) {
1461 warning << _("insert could not be created. Ignored.") << endmsg;
1468 Route::set_state (const XMLNode& node)
1471 XMLNodeConstIterator niter;
1473 XMLPropertyList plist;
1474 const XMLProperty *prop;
1476 if (node.name() != "Route"){
1477 error << string_compose(_("Bad node sent to Route::set_state() [%1]"), node.name()) << endmsg;
1481 if ((prop = node.property ("flags")) != 0) {
1483 sscanf (prop->value().c_str(), "0x%x", &x);
1489 if ((prop = node.property ("default-type")) != 0) {
1490 _default_type = DataType(prop->value());
1491 assert(_default_type != DataType::NIL);
1494 if ((prop = node.property ("phase-invert")) != 0) {
1495 set_phase_invert(prop->value()=="yes"?true:false, this);
1498 if ((prop = node.property ("active")) != 0) {
1499 set_active (prop->value() == "yes");
1502 if ((prop = node.property ("muted")) != 0) {
1503 bool yn = prop->value()=="yes"?true:false;
1505 /* force reset of mute status */
1509 mute_gain = desired_mute_gain;
1512 if ((prop = node.property ("soloed")) != 0) {
1513 bool yn = prop->value()=="yes"?true:false;
1515 /* force reset of solo status */
1518 set_solo (yn, this);
1519 solo_gain = desired_solo_gain;
1522 if ((prop = node.property ("mute-affects-pre-fader")) != 0) {
1523 _mute_affects_pre_fader = (prop->value()=="yes")?true:false;
1526 if ((prop = node.property ("mute-affects-post-fader")) != 0) {
1527 _mute_affects_post_fader = (prop->value()=="yes")?true:false;
1530 if ((prop = node.property ("mute-affects-control-outs")) != 0) {
1531 _mute_affects_control_outs = (prop->value()=="yes")?true:false;
1534 if ((prop = node.property ("mute-affects-main-outs")) != 0) {
1535 _mute_affects_main_outs = (prop->value()=="yes")?true:false;
1538 if ((prop = node.property ("edit-group")) != 0) {
1539 RouteGroup* edit_group = _session.edit_group_by_name(prop->value());
1540 if(edit_group == 0) {
1541 error << string_compose(_("Route %1: unknown edit group \"%2 in saved state (ignored)"), _name, prop->value()) << endmsg;
1543 set_edit_group(edit_group, this);
1547 if ((prop = node.property ("order-keys")) != 0) {
1551 string::size_type colon, equal;
1552 string remaining = prop->value();
1554 while (remaining.length()) {
1556 if ((equal = remaining.find_first_of ('=')) == string::npos || equal == remaining.length()) {
1557 error << string_compose (_("badly formed order key string in state file! [%1] ... ignored."), remaining)
1560 if (sscanf (remaining.substr (equal+1).c_str(), "%ld", &n) != 1) {
1561 error << string_compose (_("badly formed order key string in state file! [%1] ... ignored."), remaining)
1564 set_order_key (remaining.substr (0, equal), n);
1568 colon = remaining.find_first_of (':');
1570 if (colon != string::npos) {
1571 remaining = remaining.substr (colon+1);
1578 nlist = node.children();
1580 if (deferred_state) {
1581 delete deferred_state;
1584 deferred_state = new XMLNode("deferred state");
1586 /* set parent class properties before anything else */
1588 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
1592 if (child->name() == IO::state_node_name) {
1594 IO::set_state (*child);
1599 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
1603 if (child->name() == "Send") {
1606 if (!IO::ports_legal) {
1608 deferred_state->add_child_copy (*child);
1611 add_redirect_from_xml (*child);
1614 } else if (child->name() == "Insert") {
1616 if (!IO::ports_legal) {
1618 deferred_state->add_child_copy (*child);
1622 add_redirect_from_xml (*child);
1625 } else if (child->name() == "Automation") {
1627 XMLPropertyList plist;
1628 XMLPropertyConstIterator piter;
1631 plist = child->properties();
1632 for (piter = plist.begin(); piter != plist.end(); ++piter) {
1634 if (prop->name() == "path") {
1635 load_automation (prop->value());
1639 } else if (child->name() == "ControlOuts") {
1641 string coutname = _name;
1642 coutname += _("[control]");
1644 _control_outs = new IO (_session, coutname);
1645 _control_outs->set_state (**(child->children().begin()));
1647 } else if (child->name() == "Comment") {
1649 /* XXX this is a terrible API design in libxml++ */
1651 XMLNode *cmt = *(child->children().begin());
1652 _comment = cmt->content();
1654 } else if (child->name() == "extra") {
1655 _extra_xml = new XMLNode (*child);
1659 if ((prop = node.property ("mix-group")) != 0) {
1660 RouteGroup* mix_group = _session.mix_group_by_name(prop->value());
1661 if (mix_group == 0) {
1662 error << string_compose(_("Route %1: unknown mix group \"%2 in saved state (ignored)"), _name, prop->value()) << endmsg;
1664 set_mix_group(mix_group, this);
1672 Route::curve_reallocate ()
1674 // _gain_automation_curve.finish_resize ();
1675 // _pan_automation_curve.finish_resize ();
1679 Route::silence (jack_nframes_t nframes, jack_nframes_t offset)
1683 IO::silence (nframes, offset);
1685 if (_control_outs) {
1686 _control_outs->silence (nframes, offset);
1690 Glib::RWLock::ReaderLock lm (redirect_lock, Glib::TRY_LOCK);
1693 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
1694 boost::shared_ptr<PluginInsert> pi;
1695 if (!_active && (pi = boost::dynamic_pointer_cast<PluginInsert> (*i)) != 0) {
1696 // skip plugins, they don't need anything when we're not active
1700 (*i)->silence (nframes, offset);
1703 if (nframes == _session.get_block_size() && offset == 0) {
1713 Route::set_control_outs (const vector<string>& ports)
1715 Glib::Mutex::Lock lm (control_outs_lock);
1716 vector<string>::const_iterator i;
1718 if (_control_outs) {
1719 delete _control_outs;
1723 if (ports.empty()) {
1727 string coutname = _name;
1728 coutname += _("[control]");
1730 _control_outs = new IO (_session, coutname);
1732 /* our control outs need as many outputs as we
1733 have audio outputs. we track the changes in ::output_change_handler().
1736 _control_outs->ensure_io (ChanCount::ZERO, ChanCount(DataType::AUDIO, n_outputs().get(DataType::AUDIO)), true, this);
1742 Route::set_edit_group (RouteGroup *eg, void *src)
1745 if (eg == _edit_group) {
1750 _edit_group->remove (this);
1753 if ((_edit_group = eg) != 0) {
1754 _edit_group->add (this);
1757 _session.set_dirty ();
1758 edit_group_changed (src); /* EMIT SIGNAL */
1762 Route::drop_edit_group (void *src)
1765 _session.set_dirty ();
1766 edit_group_changed (src); /* EMIT SIGNAL */
1770 Route::set_mix_group (RouteGroup *mg, void *src)
1773 if (mg == _mix_group) {
1778 _mix_group->remove (this);
1781 if ((_mix_group = mg) != 0) {
1782 _mix_group->add (this);
1785 _session.set_dirty ();
1786 mix_group_changed (src); /* EMIT SIGNAL */
1790 Route::drop_mix_group (void *src)
1793 _session.set_dirty ();
1794 mix_group_changed (src); /* EMIT SIGNAL */
1798 Route::set_comment (string cmt, void *src)
1801 comment_changed (src);
1802 _session.set_dirty ();
1806 Route::feeds (boost::shared_ptr<Route> other)
1811 uint32_t no = self.n_outputs().get_total();
1812 uint32_t ni = other->n_inputs ().get_total();
1814 for (i = 0; i < no; ++i) {
1815 for (j = 0; j < ni; ++j) {
1816 if (self.output(i)->connected_to (other->input(j)->name())) {
1822 /* check Redirects which may also interconnect Routes */
1824 for (RedirectList::iterator r = _redirects.begin(); r != _redirects.end(); r++) {
1826 no = (*r)->n_outputs().get_total();
1828 for (i = 0; i < no; ++i) {
1829 for (j = 0; j < ni; ++j) {
1830 if ((*r)->output(i)->connected_to (other->input (j)->name())) {
1837 /* check for control room outputs which may also interconnect Routes */
1839 if (_control_outs) {
1841 no = _control_outs->n_outputs().get_total();
1843 for (i = 0; i < no; ++i) {
1844 for (j = 0; j < ni; ++j) {
1845 if (_control_outs->output(i)->connected_to (other->input (j)->name())) {
1856 Route::set_mute_config (mute_type t, bool onoff, void *src)
1860 _mute_affects_pre_fader = onoff;
1861 pre_fader_changed(src); /* EMIT SIGNAL */
1865 _mute_affects_post_fader = onoff;
1866 post_fader_changed(src); /* EMIT SIGNAL */
1870 _mute_affects_control_outs = onoff;
1871 control_outs_changed(src); /* EMIT SIGNAL */
1875 _mute_affects_main_outs = onoff;
1876 main_outs_changed(src); /* EMIT SIGNAL */
1882 Route::get_mute_config (mute_type t)
1888 onoff = _mute_affects_pre_fader;
1891 onoff = _mute_affects_post_fader;
1894 onoff = _mute_affects_control_outs;
1897 onoff = _mute_affects_main_outs;
1905 Route::set_active (bool yn)
1908 active_changed(); /* EMIT SIGNAL */
1912 Route::handle_transport_stopped (bool abort_ignored, bool did_locate, bool can_flush_redirects)
1914 jack_nframes_t now = _session.transport_frame();
1917 Glib::RWLock::ReaderLock lm (redirect_lock);
1920 automation_snapshot (now);
1923 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
1925 if (Config->get_plugins_stop_with_transport() && can_flush_redirects) {
1926 (*i)->deactivate ();
1930 (*i)->transport_stopped (now);
1934 IO::transport_stopped (now);
1936 _roll_delay = _initial_delay;
1940 Route::get_memento() const
1942 void (Route::*pmf)(state_id_t) = &Route::set_state;
1943 return sigc::bind (mem_fun (*(const_cast<Route *>(this)), pmf), _current_state_id);
1947 Route::set_state (state_id_t id)
1953 Route::input_change_handler (IOChange change, void *ignored)
1955 if (change & ConfigurationChanged) {
1956 reset_plugin_counts (0);
1961 Route::output_change_handler (IOChange change, void *ignored)
1963 if (change & ConfigurationChanged) {
1964 if (_control_outs) {
1965 _control_outs->ensure_io (ChanCount::ZERO, ChanCount(DataType::AUDIO, n_outputs().get(DataType::AUDIO)), true, this);
1968 reset_plugin_counts (0);
1973 Route::pans_required () const
1975 if (n_outputs().get(DataType::AUDIO) < 2) {
1979 return max (n_inputs ().get(DataType::AUDIO), static_cast<size_t>(redirect_max_outs.get(DataType::AUDIO)));
1983 Route::no_roll (jack_nframes_t nframes, jack_nframes_t start_frame, jack_nframes_t end_frame, jack_nframes_t offset,
1984 bool session_state_changing, bool can_record, bool rec_monitors_input)
1986 if (n_outputs().get_total() == 0) {
1990 if (session_state_changing || !_active) {
1991 silence (nframes, offset);
1995 apply_gain_automation = false;
1997 if (n_inputs().get_total()) {
1998 passthru (start_frame, end_frame, nframes, offset, 0, false);
2000 silence (nframes, offset);
2007 Route::check_initial_delay (jack_nframes_t nframes, jack_nframes_t& offset, jack_nframes_t& transport_frame)
2009 if (_roll_delay > nframes) {
2011 _roll_delay -= nframes;
2012 silence (nframes, offset);
2013 /* transport frame is not legal for caller to use */
2016 } else if (_roll_delay > 0) {
2018 nframes -= _roll_delay;
2020 silence (_roll_delay, offset);
2022 offset += _roll_delay;
2023 transport_frame += _roll_delay;
2032 Route::roll (jack_nframes_t nframes, jack_nframes_t start_frame, jack_nframes_t end_frame, jack_nframes_t offset, int declick,
2033 bool can_record, bool rec_monitors_input)
2036 Glib::RWLock::ReaderLock lm (redirect_lock, Glib::TRY_LOCK);
2038 // automation snapshot can also be called from the non-rt context
2039 // and it uses the redirect list, so we take the lock out here
2040 automation_snapshot (_session.transport_frame());
2044 if ((n_outputs().get_total() == 0 && _redirects.empty()) || n_inputs().get_total() == 0 || !_active) {
2045 silence (nframes, offset);
2049 jack_nframes_t unused = 0;
2051 if ((nframes = check_initial_delay (nframes, offset, unused)) == 0) {
2057 apply_gain_automation = false;
2060 Glib::Mutex::Lock am (automation_lock, Glib::TRY_LOCK);
2062 if (am.locked() && _session.transport_rolling()) {
2064 if (gain_automation_playback()) {
2065 apply_gain_automation = _gain_automation_curve.rt_safe_get_vector (start_frame, end_frame, _session.gain_automation_buffer(), nframes);
2070 passthru (start_frame, end_frame, nframes, offset, declick, false);
2076 Route::silent_roll (jack_nframes_t nframes, jack_nframes_t start_frame, jack_nframes_t end_frame, jack_nframes_t offset,
2077 bool can_record, bool rec_monitors_input)
2079 silence (nframes, offset);
2084 Route::toggle_monitor_input ()
2086 for (PortSet::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
2087 i->request_monitor_input( ! i->monitoring_input());
2092 Route::has_external_redirects () const
2094 boost::shared_ptr<const PortInsert> pi;
2096 for (RedirectList::const_iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2097 if ((pi = boost::dynamic_pointer_cast<const PortInsert>(*i)) != 0) {
2099 for (PortSet::const_iterator port = pi->outputs().begin();
2100 port != pi->outputs().end(); ++port) {
2102 string port_name = port->name();
2103 string client_name = port_name.substr (0, port_name.find(':'));
2105 /* only say "yes" if the redirect is actually in use */
2107 if (client_name != "ardour" && pi->active()) {
2118 Route::flush_redirects ()
2120 /* XXX shouldn't really try to take this lock, since
2121 this is called from the RT audio thread.
2124 Glib::RWLock::ReaderLock lm (redirect_lock);
2126 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2127 (*i)->deactivate ();
2133 Route::set_meter_point (MeterPoint p, void *src)
2135 if (_meter_point != p) {
2137 meter_change (src); /* EMIT SIGNAL */
2138 _session.set_dirty ();
2143 Route::update_total_latency ()
2147 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2148 if ((*i)->active ()) {
2149 _own_latency += (*i)->latency ();
2153 set_port_latency (_own_latency);
2155 /* this (virtual) function is used for pure Routes,
2156 not derived classes like AudioTrack. this means
2157 that the data processed here comes from an input
2158 port, not prerecorded material, and therefore we
2159 have to take into account any input latency.
2162 _own_latency += input_latency ();
2164 return _own_latency;
2168 Route::set_latency_delay (jack_nframes_t longest_session_latency)
2170 _initial_delay = longest_session_latency - _own_latency;
2172 if (_session.transport_stopped()) {
2173 _roll_delay = _initial_delay;
2178 Route::automation_snapshot (jack_nframes_t now)
2180 IO::automation_snapshot (now);
2182 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2183 (*i)->automation_snapshot (now);
2187 Route::ToggleControllable::ToggleControllable (Route& s, ToggleType tp)
2188 : route (s), type(tp)
2194 Route::ToggleControllable::set_value (float val)
2196 bool bval = ((val >= 0.5f) ? true: false);
2200 route.set_mute (bval, this);
2203 route.set_solo (bval, this);
2211 Route::ToggleControllable::get_value (void) const
2217 val = route.muted() ? 1.0f : 0.0f;
2220 val = route.soloed() ? 1.0f : 0.0f;
2230 Route::set_block_size (jack_nframes_t nframes)
2232 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2233 (*i)->set_block_size (nframes);
2238 Route::redirect_active_proxy (Redirect* ignored, void* ignored_src)
2240 _session.update_latency_compensation (false, false);
2244 Route::protect_automation ()
2246 switch (gain_automation_state()) {
2249 set_gain_automation_state (Off);
2255 switch (panner().automation_state ()) {
2258 panner().set_automation_state (Off);
2264 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2265 boost::shared_ptr<PluginInsert> pi;
2266 if ((pi = boost::dynamic_pointer_cast<PluginInsert> (*i)) != 0) {
2267 pi->protect_automation ();
2273 Route::set_pending_declick (int declick)
2276 /* this call is not allowed to turn off a pending declick unless "force" is true */
2278 _pending_declick = declick;
2280 // cerr << _name << ": after setting to " << declick << " pending declick = " << _pending_declick << endl;
2282 _pending_declick = 0;