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;
54 uint32_t Route::order_key_cnt = 0;
57 Route::Route (Session& sess, string name, int input_min, int input_max, int output_min, int output_max, Flag flg, DataType default_type)
58 : IO (sess, name, input_min, input_max, output_min, output_max, default_type),
60 _solo_control (X_("solo"), *this, ToggleControllable::SoloControl),
61 _mute_control (X_("mute"), *this, ToggleControllable::MuteControl)
66 Route::Route (Session& sess, const XMLNode& node)
68 _solo_control (X_("solo"), *this, ToggleControllable::SoloControl),
69 _mute_control (X_("mute"), *this, ToggleControllable::MuteControl)
78 redirect_max_outs.reset();
82 _phase_invert = false;
83 order_keys[N_("signal")] = order_key_cnt++;
86 _meter_point = MeterPostFader;
90 _have_internal_generator = false;
92 _pending_declick = true;
93 _remote_control_id = 0;
98 _mute_affects_pre_fader = Config->get_mute_affects_pre_fader();
99 _mute_affects_post_fader = Config->get_mute_affects_post_fader();
100 _mute_affects_control_outs = Config->get_mute_affects_control_outs();
101 _mute_affects_main_outs = Config->get_mute_affects_main_outs();
104 desired_solo_gain = 1.0;
106 desired_mute_gain = 1.0;
110 input_changed.connect (mem_fun (this, &Route::input_change_handler));
111 output_changed.connect (mem_fun (this, &Route::output_change_handler));
116 clear_redirects (this);
119 delete _control_outs;
124 Route::set_remote_control_id (uint32_t id)
126 if (id != _remote_control_id) {
127 _remote_control_id = id;
128 RemoteControlIDChanged ();
133 Route::remote_control_id() const
135 return _remote_control_id;
139 Route::order_key (string name) const
141 OrderKeys::const_iterator i;
143 if ((i = order_keys.find (name)) == order_keys.end()) {
151 Route::set_order_key (string name, long n)
153 order_keys[name] = n;
154 _session.set_dirty ();
158 Route::inc_gain (gain_t fraction, void *src)
160 IO::inc_gain (fraction, src);
164 Route::set_gain (gain_t val, void *src)
166 if (src != 0 && _mix_group && src != _mix_group && _mix_group->is_active()) {
168 if (_mix_group->is_relative()) {
171 gain_t usable_gain = gain();
172 if (usable_gain < 0.000001f) {
173 usable_gain=0.000001f;
177 if (delta < 0.000001f) {
181 delta -= usable_gain;
183 if (delta == 0.0f) return;
185 gain_t factor = delta / usable_gain;
188 factor = _mix_group->get_max_factor(factor);
189 if (factor == 0.0f) {
194 factor = _mix_group->get_min_factor(factor);
195 if (factor == 0.0f) {
201 _mix_group->apply (&Route::inc_gain, factor, _mix_group);
205 _mix_group->apply (&Route::set_gain, val, _mix_group);
215 IO::set_gain (val, src);
218 /** Process this route for one (sub) cycle (process thread)
220 * @param bufs Scratch buffers to use for the signal path
221 * @param start_frame Initial transport frame
222 * @param end_frame Final transport frame
223 * @param nframes Number of frames to output (to ports)
224 * @param offset Output offset (of port buffers, for split cycles)
226 * Note that (end_frame - start_frame) may not be equal to nframes when the
227 * transport speed isn't 1.0 (eg varispeed).
230 Route::process_output_buffers (BufferSet& bufs,
231 nframes_t start_frame, nframes_t end_frame,
232 nframes_t nframes, nframes_t offset, bool with_redirects, int declick,
235 // This is definitely very audio-only for now
236 assert(_default_type == DataType::AUDIO);
238 RedirectList::iterator i;
239 bool post_fader_work = false;
240 bool mute_declick_applied = false;
246 gain_t* gab = _session.gain_automation_buffer();
248 switch (Config->get_monitoring_model()) {
249 case HardwareMonitoring:
250 case ExternalMonitoring:
257 declick = _pending_declick;
260 Glib::Mutex::Lock cm (control_outs_lock, Glib::TRY_LOCK);
270 Glib::Mutex::Lock dm (declick_lock, Glib::TRY_LOCK);
273 dmg = desired_mute_gain;
274 dsg = desired_solo_gain;
283 /* ----------------------------------------------------------------------------------------------------
284 GLOBAL DECLICK (for transport changes etc.)
285 -------------------------------------------------------------------------------------------------- */
288 Amp::run (bufs, nframes, 0.0, 1.0, false);
289 _pending_declick = 0;
290 } else if (declick < 0) {
291 Amp::run (bufs, nframes, 1.0, 0.0, false);
292 _pending_declick = 0;
295 /* no global declick */
297 if (solo_gain != dsg) {
298 Amp::run (bufs, nframes, solo_gain, dsg, false);
304 /* ----------------------------------------------------------------------------------------------------
305 INPUT METERING & MONITORING
306 -------------------------------------------------------------------------------------------------- */
308 if (meter && (_meter_point == MeterInput)) {
309 _meter->run(bufs, nframes);
312 if (!_soloed && _mute_affects_pre_fader && (mute_gain != dmg)) {
313 Amp::run (bufs, nframes, mute_gain, dmg, false);
315 mute_declick_applied = true;
318 if ((_meter_point == MeterInput) && co) {
320 solo_audible = dsg > 0;
321 mute_audible = dmg > 0;// || !_mute_affects_pre_fader;
323 if ( // muted by solo of another track
327 // muted by mute of this track
331 // rec-enabled but not s/w monitoring
333 // TODO: this is probably wrong
335 (no_monitor && record_enabled() && (!Config->get_auto_input() || _session.actively_recording()))
339 co->silence (nframes, offset);
343 co->deliver_output (bufs, start_frame, end_frame, nframes, offset);
348 /* ---------------------------------------------------------------------------------------------------
350 -------------------------------------------------------------------------------------------------- */
352 /* FIXME: Somewhere in these loops is where bufs.count() should go from n_inputs() to redirect_max_outs()
353 * (if they differ). Something explicit needs to be done here to make sure the list of redirects will
354 * give us what we need (possibly by inserting transparent 'translators' into the list to make it work) */
356 if (with_redirects) {
357 Glib::RWLock::ReaderLock rm (redirect_lock, Glib::TRY_LOCK);
359 if (mute_gain > 0 || !_mute_affects_pre_fader) {
360 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
361 switch ((*i)->placement()) {
363 (*i)->run (bufs, start_frame, end_frame, nframes, offset);
366 post_fader_work = true;
371 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
372 switch ((*i)->placement()) {
374 (*i)->silence (nframes, offset);
377 post_fader_work = true;
385 // FIXME: for now, just hope the redirects list did what it was supposed to
386 bufs.set_count(n_process_buffers());
389 if (!_soloed && (mute_gain != dmg) && !mute_declick_applied && _mute_affects_post_fader) {
390 Amp::run (bufs, nframes, mute_gain, dmg, false);
392 mute_declick_applied = true;
395 /* ----------------------------------------------------------------------------------------------------
396 PRE-FADER METERING & MONITORING
397 -------------------------------------------------------------------------------------------------- */
399 if (meter && (_meter_point == MeterPreFader)) {
400 _meter->run(bufs, nframes);
404 if ((_meter_point == MeterPreFader) && co) {
406 solo_audible = dsg > 0;
407 mute_audible = dmg > 0 || !_mute_affects_pre_fader;
409 if ( // muted by solo of another track
413 // muted by mute of this track
417 // rec-enabled but not s/w monitoring
419 (no_monitor && record_enabled() && (!Config->get_auto_input() || _session.actively_recording()))
423 co->silence (nframes, offset);
427 co->deliver_output (bufs, start_frame, end_frame, nframes, offset);
432 /* ----------------------------------------------------------------------------------------------------
434 -------------------------------------------------------------------------------------------------- */
436 /* if not recording or recording and requiring any monitor signal, then apply gain */
438 if ( // not recording
440 !(record_enabled() && _session.actively_recording()) ||
444 // h/w monitoring not in use
446 (!Config->get_monitoring_model() == HardwareMonitoring &&
448 // AND software monitoring required
450 Config->get_monitoring_model() == SoftwareMonitoring)) {
452 if (apply_gain_automation) {
455 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
456 Sample* const sp = i->data(nframes);
458 for (nframes_t nx = 0; nx < nframes; ++nx) {
463 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
464 Sample* const sp = i->data(nframes);
466 for (nframes_t nx = 0; nx < nframes; ++nx) {
472 if (apply_gain_automation && _session.transport_rolling() && nframes > 0) {
473 _effective_gain = gab[nframes-1];
478 /* manual (scalar) gain */
482 Amp::run (bufs, nframes, _gain, dg, _phase_invert);
485 } else if (_gain != 0 && (_phase_invert || _gain != 1.0)) {
487 /* no need to interpolate current gain value,
488 but its non-unity, so apply it. if the gain
489 is zero, do nothing because we'll ship silence
501 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
502 Sample* const sp = i->data(nframes);
503 Session::apply_gain_to_buffer(sp,nframes,this_gain);
506 } else if (_gain == 0) {
507 for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
515 /* actively recording, no monitoring required; leave buffers as-is to save CPU cycles */
519 /* ----------------------------------------------------------------------------------------------------
521 -------------------------------------------------------------------------------------------------- */
523 /* note that post_fader_work cannot be true unless with_redirects was also true, so don't test both */
525 if (post_fader_work) {
527 Glib::RWLock::ReaderLock rm (redirect_lock, Glib::TRY_LOCK);
529 if (mute_gain > 0 || !_mute_affects_post_fader) {
530 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
531 switch ((*i)->placement()) {
535 (*i)->run (bufs, start_frame, end_frame, nframes, offset);
540 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
541 switch ((*i)->placement()) {
545 (*i)->silence (nframes, offset);
553 if (!_soloed && (mute_gain != dmg) && !mute_declick_applied && _mute_affects_control_outs) {
554 Amp::run (bufs, nframes, mute_gain, dmg, false);
556 mute_declick_applied = true;
559 /* ----------------------------------------------------------------------------------------------------
561 -------------------------------------------------------------------------------------------------- */
563 if ((_meter_point == MeterPostFader) && co) {
565 solo_audible = solo_gain > 0;
566 mute_audible = dmg > 0 || !_mute_affects_control_outs;
568 if ( // silent anyway
570 (_gain == 0 && !apply_gain_automation) ||
572 // muted by solo of another track
576 // muted by mute of this track
580 // recording but not s/w monitoring
582 (no_monitor && record_enabled() && (!Config->get_auto_input() || _session.actively_recording()))
586 co->silence (nframes, offset);
590 co->deliver_output (bufs, start_frame, end_frame, nframes, offset);
594 /* ----------------------------------------------------------------------
596 ----------------------------------------------------------------------*/
598 if (!_soloed && (mute_gain != dmg) && !mute_declick_applied && _mute_affects_main_outs) {
599 Amp::run (bufs, nframes, mute_gain, dmg, false);
601 mute_declick_applied = true;
604 /* ----------------------------------------------------------------------------------------------------
606 -------------------------------------------------------------------------------------------------- */
608 solo_audible = dsg > 0;
609 mute_audible = dmg > 0 || !_mute_affects_main_outs;
611 if (n_outputs().get(_default_type) == 0) {
615 } else if (no_monitor && record_enabled() && (!Config->get_auto_input() || _session.actively_recording())) {
617 IO::silence (nframes, offset);
621 if ( // silent anyway
623 (_gain == 0 && !apply_gain_automation) ||
625 // muted by solo of another track, but not using control outs for solo
627 (!solo_audible && (Config->get_solo_model() != SoloBus)) ||
629 // muted by mute of this track
635 /* don't use Route::silence() here, because that causes
636 all outputs (sends, port inserts, etc. to be silent).
639 if (_meter_point == MeterPostFader) {
640 peak_meter().reset();
643 IO::silence (nframes, offset);
647 deliver_output(bufs, start_frame, end_frame, nframes, offset);
653 /* ----------------------------------------------------------------------------------------------------
655 -------------------------------------------------------------------------------------------------- */
657 if (meter && (_meter_point == MeterPostFader)) {
658 if ((_gain == 0 && !apply_gain_automation) || dmg == 0) {
661 _meter->run(output_buffers(), nframes, offset);
667 Route::n_process_buffers ()
669 return max (n_inputs(), redirect_max_outs);
673 Route::passthru (nframes_t start_frame, nframes_t end_frame, nframes_t nframes, nframes_t offset, int declick, bool meter_first)
675 BufferSet& bufs = _session.get_scratch_buffers(n_process_buffers());
679 collect_input (bufs, nframes, offset);
681 #define meter_stream meter_first
684 _meter->run(bufs, nframes);
685 meter_stream = false;
690 process_output_buffers (bufs, start_frame, end_frame, nframes, offset, true, declick, meter_stream);
696 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)
698 process_output_buffers (_session.get_silent_buffers (n_process_buffers()), start_frame, end_frame, nframes, offset, true, declick, meter);
702 Route::set_solo (bool yn, void *src)
708 if (_mix_group && src != _mix_group && _mix_group->is_active()) {
709 _mix_group->apply (&Route::set_solo, yn, _mix_group);
715 solo_changed (src); /* EMIT SIGNAL */
716 _solo_control.Changed (); /* EMIT SIGNAL */
721 Route::set_solo_mute (bool yn)
723 Glib::Mutex::Lock lm (declick_lock);
725 /* Called by Session in response to another Route being soloed.
728 desired_solo_gain = (yn?0.0:1.0);
732 Route::set_solo_safe (bool yn, void *src)
734 if (_solo_safe != yn) {
736 solo_safe_changed (src); /* EMIT SIGNAL */
741 Route::set_mute (bool yn, void *src)
744 if (_mix_group && src != _mix_group && _mix_group->is_active()) {
745 _mix_group->apply (&Route::set_mute, yn, _mix_group);
751 mute_changed (src); /* EMIT SIGNAL */
753 _mute_control.Changed (); /* EMIT SIGNAL */
755 Glib::Mutex::Lock lm (declick_lock);
756 desired_mute_gain = (yn?0.0f:1.0f);
761 Route::add_redirect (boost::shared_ptr<Redirect> redirect, void *src, uint32_t* err_streams)
763 ChanCount old_rmo = redirect_max_outs;
765 if (!_session.engine().connected()) {
770 Glib::RWLock::WriterLock lm (redirect_lock);
772 boost::shared_ptr<PluginInsert> pi;
773 boost::shared_ptr<PortInsert> porti;
775 redirect->set_default_type(_default_type);
777 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(redirect)) != 0) {
780 if (pi->input_streams() == ChanCount::ZERO) {
781 /* generator plugin */
782 _have_internal_generator = true;
785 } else if ((porti = boost::dynamic_pointer_cast<PortInsert>(redirect)) != 0) {
787 /* force new port inserts to start out with an i/o configuration
788 that matches this route's i/o configuration.
790 the "inputs" for the port are supposed to match the output
793 the "outputs" of the route should match the inputs of this
794 route. XXX shouldn't they match the number of active signal
795 streams at the point of insertion?
797 // FIXME: (yes, they should)
799 porti->ensure_io (n_outputs (), n_inputs(), false, this);
802 // Ensure peak vector sizes before the plugin is activated
803 ChanCount potential_max_streams = max(redirect->input_streams(), redirect->output_streams());
804 _meter->setup(potential_max_streams);
806 _redirects.push_back (redirect);
808 if (_reset_plugin_counts (err_streams)) {
809 _redirects.pop_back ();
810 _reset_plugin_counts (0); // it worked before we tried to add it ...
814 redirect->activate ();
815 redirect->active_changed.connect (mem_fun (*this, &Route::redirect_active_proxy));
818 if (redirect_max_outs != old_rmo || old_rmo == ChanCount::ZERO) {
823 redirects_changed (src); /* EMIT SIGNAL */
828 Route::add_redirects (const RedirectList& others, void *src, uint32_t* err_streams)
830 ChanCount old_rmo = redirect_max_outs;
832 if (!_session.engine().connected()) {
837 Glib::RWLock::WriterLock lm (redirect_lock);
839 RedirectList::iterator existing_end = _redirects.end();
842 ChanCount potential_max_streams;
844 for (RedirectList::const_iterator i = others.begin(); i != others.end(); ++i) {
846 boost::shared_ptr<PluginInsert> pi;
848 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(*i)) != 0) {
851 ChanCount m = max(pi->input_streams(), pi->output_streams());
852 if (m > potential_max_streams)
853 potential_max_streams = m;
856 // Ensure peak vector sizes before the plugin is activated
857 _meter->setup(potential_max_streams);
859 _redirects.push_back (*i);
861 if (_reset_plugin_counts (err_streams)) {
863 _redirects.erase (existing_end, _redirects.end());
864 _reset_plugin_counts (0); // it worked before we tried to add it ...
869 (*i)->active_changed.connect (mem_fun (*this, &Route::redirect_active_proxy));
873 if (redirect_max_outs != old_rmo || old_rmo == ChanCount::ZERO) {
877 redirects_changed (src); /* EMIT SIGNAL */
882 Route::clear_redirects (void *src)
884 ChanCount old_rmo = redirect_max_outs;
886 if (!_session.engine().connected()) {
891 Glib::RWLock::WriterLock lm (redirect_lock);
895 if (redirect_max_outs != old_rmo) {
899 redirect_max_outs.reset();
900 _have_internal_generator = false;
901 redirects_changed (src); /* EMIT SIGNAL */
905 Route::remove_redirect (boost::shared_ptr<Redirect> redirect, void *src, uint32_t* err_streams)
907 ChanCount old_rmo = redirect_max_outs;
909 if (!_session.engine().connected()) {
913 redirect_max_outs.reset();
916 Glib::RWLock::WriterLock lm (redirect_lock);
917 RedirectList::iterator i;
918 bool removed = false;
920 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
921 if (*i == redirect) {
923 RedirectList::iterator tmp;
925 /* move along, see failure case for reset_plugin_counts()
926 where we may need to reinsert the redirect.
932 /* stop redirects that send signals to JACK ports
933 from causing noise as a result of no longer being
937 boost::shared_ptr<Send> send;
938 boost::shared_ptr<PortInsert> port_insert;
940 if ((send = boost::dynamic_pointer_cast<Send> (*i)) != 0) {
941 send->disconnect_inputs (this);
942 send->disconnect_outputs (this);
943 } else if ((port_insert = boost::dynamic_pointer_cast<PortInsert> (*i)) != 0) {
944 port_insert->disconnect_inputs (this);
945 port_insert->disconnect_outputs (this);
948 _redirects.erase (i);
961 if (_reset_plugin_counts (err_streams)) {
962 /* get back to where we where */
963 _redirects.insert (i, redirect);
964 /* we know this will work, because it worked before :) */
965 _reset_plugin_counts (0);
971 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
972 boost::shared_ptr<PluginInsert> pi;
974 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(*i)) != 0) {
975 if (pi->is_generator()) {
981 _have_internal_generator = foo;
984 if (old_rmo != redirect_max_outs) {
988 redirects_changed (src); /* EMIT SIGNAL */
993 Route::reset_plugin_counts (uint32_t* lpc)
995 Glib::RWLock::WriterLock lm (redirect_lock);
996 return _reset_plugin_counts (lpc);
1001 Route::_reset_plugin_counts (uint32_t* err_streams)
1003 RedirectList::iterator r;
1006 map<Placement,list<InsertCount> > insert_map;
1007 nframes_t initial_streams;
1009 redirect_max_outs.reset();
1013 /* divide inserts up by placement so we get the signal flow
1014 properly modelled. we need to do this because the _redirects
1015 list is not sorted by placement, and because other reasons may
1016 exist now or in the future for this separate treatment.
1019 for (r = _redirects.begin(); r != _redirects.end(); ++r) {
1021 boost::shared_ptr<Insert> insert;
1023 /* do this here in case we bomb out before we get to the end of
1027 redirect_max_outs = max ((*r)->output_streams (), redirect_max_outs);
1029 if ((insert = boost::dynamic_pointer_cast<Insert>(*r)) != 0) {
1031 insert_map[insert->placement()].push_back (InsertCount (insert));
1033 /* reset plugin counts back to one for now so
1034 that we have a predictable, controlled
1035 state to try to configure.
1038 boost::shared_ptr<PluginInsert> pi;
1040 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(insert)) != 0) {
1044 } else if (boost::dynamic_pointer_cast<Send> (*r) != 0) {
1057 /* Now process each placement in order, checking to see if we
1058 can really do what has been requested.
1063 if (check_some_plugin_counts (insert_map[PreFader], n_inputs ().get(_default_type), err_streams)) {
1067 /* figure out the streams that will feed into PreFader */
1069 if (!insert_map[PreFader].empty()) {
1070 InsertCount& ic (insert_map[PreFader].back());
1071 initial_streams = ic.insert->compute_output_streams (ic.cnt);
1073 initial_streams = n_inputs ().get(_default_type);
1078 if (check_some_plugin_counts (insert_map[PostFader], initial_streams, err_streams)) {
1082 /* OK, everything can be set up correctly, so lets do it */
1084 apply_some_plugin_counts (insert_map[PreFader]);
1085 apply_some_plugin_counts (insert_map[PostFader]);
1087 /* recompute max outs of any redirect */
1091 redirect_max_outs.reset();
1092 RedirectList::iterator prev = _redirects.end();
1094 for (r = _redirects.begin(); r != _redirects.end(); prev = r, ++r) {
1095 boost::shared_ptr<Send> s;
1097 if ((s = boost::dynamic_pointer_cast<Send> (*r)) != 0) {
1098 if (r == _redirects.begin()) {
1099 s->expect_inputs (n_inputs());
1101 s->expect_inputs ((*prev)->output_streams());
1105 redirect_max_outs = max ((*r)->output_streams(), redirect_max_outs);
1114 Route::apply_some_plugin_counts (list<InsertCount>& iclist)
1116 list<InsertCount>::iterator i;
1118 for (i = iclist.begin(); i != iclist.end(); ++i) {
1120 if ((*i).insert->configure_io ((*i).cnt, (*i).in, (*i).out)) {
1123 /* make sure that however many we have, they are all active */
1124 (*i).insert->activate ();
1131 Route::check_some_plugin_counts (list<InsertCount>& iclist, int32_t required_inputs, uint32_t* err_streams)
1133 list<InsertCount>::iterator i;
1135 for (i = iclist.begin(); i != iclist.end(); ++i) {
1137 if (((*i).cnt = (*i).insert->can_support_input_configuration (required_inputs)) < 0) {
1139 *err_streams = required_inputs;
1144 (*i).in = required_inputs;
1145 (*i).out = (*i).insert->compute_output_streams ((*i).cnt);
1147 required_inputs = (*i).out;
1154 Route::copy_redirects (const Route& other, Placement placement, uint32_t* err_streams)
1156 ChanCount old_rmo = redirect_max_outs;
1162 RedirectList to_be_deleted;
1165 Glib::RWLock::WriterLock lm (redirect_lock);
1166 RedirectList::iterator tmp;
1167 RedirectList the_copy;
1169 the_copy = _redirects;
1171 /* remove all relevant redirects */
1173 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ) {
1177 if ((*i)->placement() == placement) {
1178 to_be_deleted.push_back (*i);
1179 _redirects.erase (i);
1185 /* now copy the relevant ones from "other" */
1187 for (RedirectList::const_iterator i = other._redirects.begin(); i != other._redirects.end(); ++i) {
1188 if ((*i)->placement() == placement) {
1189 _redirects.push_back (Redirect::clone (*i));
1193 /* reset plugin stream handling */
1195 if (_reset_plugin_counts (err_streams)) {
1197 /* FAILED COPY ATTEMPT: we have to restore order */
1199 /* delete all cloned redirects */
1201 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ) {
1206 if ((*i)->placement() == placement) {
1207 _redirects.erase (i);
1213 /* restore the natural order */
1215 _redirects = the_copy;
1216 redirect_max_outs = old_rmo;
1218 /* we failed, even though things are OK again */
1224 /* SUCCESSFUL COPY ATTEMPT: delete the redirects we removed pre-copy */
1225 to_be_deleted.clear ();
1229 if (redirect_max_outs != old_rmo || old_rmo == ChanCount::ZERO) {
1233 redirects_changed (this); /* EMIT SIGNAL */
1238 Route::all_redirects_flip ()
1240 Glib::RWLock::ReaderLock lm (redirect_lock);
1242 if (_redirects.empty()) {
1246 bool first_is_on = _redirects.front()->active();
1248 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
1249 (*i)->set_active (!first_is_on, this);
1254 Route::all_redirects_active (bool state)
1256 Glib::RWLock::ReaderLock lm (redirect_lock);
1258 if (_redirects.empty()) {
1262 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
1263 (*i)->set_active (state, this);
1267 struct RedirectSorter {
1268 bool operator() (boost::shared_ptr<const Redirect> a, boost::shared_ptr<const Redirect> b) {
1269 return a->sort_key() < b->sort_key();
1274 Route::sort_redirects (uint32_t* err_streams)
1277 RedirectSorter comparator;
1278 Glib::RWLock::WriterLock lm (redirect_lock);
1279 ChanCount old_rmo = redirect_max_outs;
1281 /* the sweet power of C++ ... */
1283 RedirectList as_it_was_before = _redirects;
1285 _redirects.sort (comparator);
1287 if (_reset_plugin_counts (err_streams)) {
1288 _redirects = as_it_was_before;
1289 redirect_max_outs = old_rmo;
1295 redirects_changed (this); /* EMIT SIGNAL */
1307 Route::get_template()
1309 return state(false);
1313 Route::state(bool full_state)
1315 XMLNode *node = new XMLNode("Route");
1317 RedirectList:: iterator i;
1321 snprintf (buf, sizeof (buf), "0x%x", _flags);
1322 node->add_property("flags", buf);
1325 node->add_property("default-type", _default_type.to_string());
1327 node->add_property("active", _active?"yes":"no");
1328 node->add_property("muted", _muted?"yes":"no");
1329 node->add_property("soloed", _soloed?"yes":"no");
1330 node->add_property("phase-invert", _phase_invert?"yes":"no");
1331 node->add_property("mute-affects-pre-fader", _mute_affects_pre_fader?"yes":"no");
1332 node->add_property("mute-affects-post-fader", _mute_affects_post_fader?"yes":"no");
1333 node->add_property("mute-affects-control-outs", _mute_affects_control_outs?"yes":"no");
1334 node->add_property("mute-affects-main-outs", _mute_affects_main_outs?"yes":"no");
1337 node->add_property("edit-group", _edit_group->name());
1340 node->add_property("mix-group", _mix_group->name());
1343 string order_string;
1344 OrderKeys::iterator x = order_keys.begin();
1346 while (x != order_keys.end()) {
1347 order_string += (*x).first;
1348 order_string += '=';
1349 snprintf (buf, sizeof(buf), "%ld", (*x).second);
1350 order_string += buf;
1354 if (x == order_keys.end()) {
1358 order_string += ':';
1360 node->add_property ("order-keys", order_string);
1362 node->add_child_nocopy (IO::state (full_state));
1363 node->add_child_nocopy (_solo_control.get_state ());
1364 node->add_child_nocopy (_mute_control.get_state ());
1366 if (_control_outs) {
1367 XMLNode* cnode = new XMLNode (X_("ControlOuts"));
1368 cnode->add_child_nocopy (_control_outs->state (full_state));
1369 node->add_child_nocopy (*cnode);
1372 if (_comment.length()) {
1373 XMLNode *cmt = node->add_child ("Comment");
1374 cmt->add_content (_comment);
1380 path = _session.snap_name();
1382 path += legalize_for_path (_name);
1383 path += ".automation";
1385 /* XXX we didn't ask for a state save, we asked for the current state.
1389 if (save_automation (path)) {
1390 error << _("Could not get state of route. Problem with save_automation") << endmsg;
1393 aevents = node->add_child ("Automation");
1394 aevents->add_property ("path", path);
1397 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
1398 node->add_child_nocopy((*i)->state (full_state));
1402 node->add_child_copy (*_extra_xml);
1409 Route::set_deferred_state ()
1412 XMLNodeConstIterator niter;
1414 if (!deferred_state) {
1418 nlist = deferred_state->children();
1420 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
1421 add_redirect_from_xml (**niter);
1424 delete deferred_state;
1429 Route::add_redirect_from_xml (const XMLNode& node)
1431 const XMLProperty *prop;
1433 if (node.name() == "Send") {
1437 boost::shared_ptr<Send> send (new Send (_session, node));
1438 add_redirect (send, this);
1441 catch (failed_constructor &err) {
1442 error << _("Send construction failed") << endmsg;
1446 } else if (node.name() == "Insert") {
1449 if ((prop = node.property ("type")) != 0) {
1451 boost::shared_ptr<Insert> insert;
1453 if (prop->value() == "ladspa" || prop->value() == "Ladspa" || prop->value() == "vst") {
1455 insert.reset (new PluginInsert(_session, node));
1457 } else if (prop->value() == "port") {
1460 insert.reset (new PortInsert (_session, node));
1464 error << string_compose(_("unknown Insert type \"%1\"; ignored"), prop->value()) << endmsg;
1467 add_redirect (insert, this);
1470 error << _("Insert XML node has no type property") << endmsg;
1474 catch (failed_constructor &err) {
1475 warning << _("insert could not be created. Ignored.") << endmsg;
1482 Route::set_state (const XMLNode& node)
1485 XMLNodeConstIterator niter;
1487 XMLPropertyList plist;
1488 const XMLProperty *prop;
1490 if (node.name() != "Route"){
1491 error << string_compose(_("Bad node sent to Route::set_state() [%1]"), node.name()) << endmsg;
1495 if ((prop = node.property ("flags")) != 0) {
1497 sscanf (prop->value().c_str(), "0x%x", &x);
1503 if ((prop = node.property ("default-type")) != 0) {
1504 _default_type = DataType(prop->value());
1505 assert(_default_type != DataType::NIL);
1508 if ((prop = node.property ("phase-invert")) != 0) {
1509 set_phase_invert(prop->value()=="yes"?true:false, this);
1512 if ((prop = node.property ("active")) != 0) {
1513 set_active (prop->value() == "yes");
1516 if ((prop = node.property ("muted")) != 0) {
1517 bool yn = prop->value()=="yes"?true:false;
1519 /* force reset of mute status */
1523 mute_gain = desired_mute_gain;
1526 if ((prop = node.property ("soloed")) != 0) {
1527 bool yn = prop->value()=="yes"?true:false;
1529 /* force reset of solo status */
1532 set_solo (yn, this);
1533 solo_gain = desired_solo_gain;
1536 if ((prop = node.property ("mute-affects-pre-fader")) != 0) {
1537 _mute_affects_pre_fader = (prop->value()=="yes")?true:false;
1540 if ((prop = node.property ("mute-affects-post-fader")) != 0) {
1541 _mute_affects_post_fader = (prop->value()=="yes")?true:false;
1544 if ((prop = node.property ("mute-affects-control-outs")) != 0) {
1545 _mute_affects_control_outs = (prop->value()=="yes")?true:false;
1548 if ((prop = node.property ("mute-affects-main-outs")) != 0) {
1549 _mute_affects_main_outs = (prop->value()=="yes")?true:false;
1552 if ((prop = node.property ("edit-group")) != 0) {
1553 RouteGroup* edit_group = _session.edit_group_by_name(prop->value());
1554 if(edit_group == 0) {
1555 error << string_compose(_("Route %1: unknown edit group \"%2 in saved state (ignored)"), _name, prop->value()) << endmsg;
1557 set_edit_group(edit_group, this);
1561 if ((prop = node.property ("order-keys")) != 0) {
1565 string::size_type colon, equal;
1566 string remaining = prop->value();
1568 while (remaining.length()) {
1570 if ((equal = remaining.find_first_of ('=')) == string::npos || equal == remaining.length()) {
1571 error << string_compose (_("badly formed order key string in state file! [%1] ... ignored."), remaining)
1574 if (sscanf (remaining.substr (equal+1).c_str(), "%ld", &n) != 1) {
1575 error << string_compose (_("badly formed order key string in state file! [%1] ... ignored."), remaining)
1578 set_order_key (remaining.substr (0, equal), n);
1582 colon = remaining.find_first_of (':');
1584 if (colon != string::npos) {
1585 remaining = remaining.substr (colon+1);
1592 nlist = node.children();
1594 if (deferred_state) {
1595 delete deferred_state;
1598 deferred_state = new XMLNode("deferred state");
1600 /* set parent class properties before anything else */
1602 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
1606 if (child->name() == IO::state_node_name) {
1608 IO::set_state (*child);
1613 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
1617 if (child->name() == "Send") {
1620 if (!IO::ports_legal) {
1622 deferred_state->add_child_copy (*child);
1625 add_redirect_from_xml (*child);
1628 } else if (child->name() == "Insert") {
1630 if (!IO::ports_legal) {
1632 deferred_state->add_child_copy (*child);
1636 add_redirect_from_xml (*child);
1639 } else if (child->name() == "Automation") {
1641 XMLPropertyList plist;
1642 XMLPropertyConstIterator piter;
1645 plist = child->properties();
1646 for (piter = plist.begin(); piter != plist.end(); ++piter) {
1648 if (prop->name() == "path") {
1649 load_automation (prop->value());
1653 } else if (child->name() == "ControlOuts") {
1655 string coutname = _name;
1656 coutname += _("[control]");
1658 _control_outs = new IO (_session, coutname);
1659 _control_outs->set_state (**(child->children().begin()));
1661 } else if (child->name() == "Comment") {
1663 /* XXX this is a terrible API design in libxml++ */
1665 XMLNode *cmt = *(child->children().begin());
1666 _comment = cmt->content();
1668 } else if (child->name() == "extra") {
1669 _extra_xml = new XMLNode (*child);
1670 } else if (child->name() == "solo") {
1671 _solo_control.set_state (*child);
1672 _session.add_controllable (&_solo_control);
1673 } else if (child->name() == "mute") {
1674 _mute_control.set_state (*child);
1675 _session.add_controllable (&_mute_control);
1679 if ((prop = node.property ("mix-group")) != 0) {
1680 RouteGroup* mix_group = _session.mix_group_by_name(prop->value());
1681 if (mix_group == 0) {
1682 error << string_compose(_("Route %1: unknown mix group \"%2 in saved state (ignored)"), _name, prop->value()) << endmsg;
1684 set_mix_group(mix_group, this);
1692 Route::curve_reallocate ()
1694 // _gain_automation_curve.finish_resize ();
1695 // _pan_automation_curve.finish_resize ();
1699 Route::silence (nframes_t nframes, nframes_t offset)
1703 IO::silence (nframes, offset);
1705 if (_control_outs) {
1706 _control_outs->silence (nframes, offset);
1710 Glib::RWLock::ReaderLock lm (redirect_lock, Glib::TRY_LOCK);
1713 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
1714 boost::shared_ptr<PluginInsert> pi;
1715 if (!_active && (pi = boost::dynamic_pointer_cast<PluginInsert> (*i)) != 0) {
1716 // skip plugins, they don't need anything when we're not active
1720 (*i)->silence (nframes, offset);
1723 if (nframes == _session.get_block_size() && offset == 0) {
1733 Route::set_control_outs (const vector<string>& ports)
1735 Glib::Mutex::Lock lm (control_outs_lock);
1736 vector<string>::const_iterator i;
1738 if (_control_outs) {
1739 delete _control_outs;
1743 if (ports.empty()) {
1747 string coutname = _name;
1748 coutname += _("[control]");
1750 _control_outs = new IO (_session, coutname);
1752 /* our control outs need as many outputs as we
1753 have audio outputs. we track the changes in ::output_change_handler().
1756 _control_outs->ensure_io (ChanCount::ZERO, ChanCount(DataType::AUDIO, n_outputs().get(DataType::AUDIO)), true, this);
1762 Route::set_edit_group (RouteGroup *eg, void *src)
1765 if (eg == _edit_group) {
1770 _edit_group->remove (this);
1773 if ((_edit_group = eg) != 0) {
1774 _edit_group->add (this);
1777 _session.set_dirty ();
1778 edit_group_changed (src); /* EMIT SIGNAL */
1782 Route::drop_edit_group (void *src)
1785 _session.set_dirty ();
1786 edit_group_changed (src); /* EMIT SIGNAL */
1790 Route::set_mix_group (RouteGroup *mg, void *src)
1793 if (mg == _mix_group) {
1798 _mix_group->remove (this);
1801 if ((_mix_group = mg) != 0) {
1802 _mix_group->add (this);
1805 _session.set_dirty ();
1806 mix_group_changed (src); /* EMIT SIGNAL */
1810 Route::drop_mix_group (void *src)
1813 _session.set_dirty ();
1814 mix_group_changed (src); /* EMIT SIGNAL */
1818 Route::set_comment (string cmt, void *src)
1821 comment_changed (src);
1822 _session.set_dirty ();
1826 Route::feeds (boost::shared_ptr<Route> other)
1831 uint32_t no = self.n_outputs().get_total();
1832 uint32_t ni = other->n_inputs ().get_total();
1834 for (i = 0; i < no; ++i) {
1835 for (j = 0; j < ni; ++j) {
1836 if (self.output(i)->connected_to (other->input(j)->name())) {
1842 /* check Redirects which may also interconnect Routes */
1844 for (RedirectList::iterator r = _redirects.begin(); r != _redirects.end(); r++) {
1846 no = (*r)->n_outputs().get_total();
1848 for (i = 0; i < no; ++i) {
1849 for (j = 0; j < ni; ++j) {
1850 if ((*r)->output(i)->connected_to (other->input (j)->name())) {
1857 /* check for control room outputs which may also interconnect Routes */
1859 if (_control_outs) {
1861 no = _control_outs->n_outputs().get_total();
1863 for (i = 0; i < no; ++i) {
1864 for (j = 0; j < ni; ++j) {
1865 if (_control_outs->output(i)->connected_to (other->input (j)->name())) {
1876 Route::set_mute_config (mute_type t, bool onoff, void *src)
1880 _mute_affects_pre_fader = onoff;
1881 pre_fader_changed(src); /* EMIT SIGNAL */
1885 _mute_affects_post_fader = onoff;
1886 post_fader_changed(src); /* EMIT SIGNAL */
1890 _mute_affects_control_outs = onoff;
1891 control_outs_changed(src); /* EMIT SIGNAL */
1895 _mute_affects_main_outs = onoff;
1896 main_outs_changed(src); /* EMIT SIGNAL */
1902 Route::get_mute_config (mute_type t)
1908 onoff = _mute_affects_pre_fader;
1911 onoff = _mute_affects_post_fader;
1914 onoff = _mute_affects_control_outs;
1917 onoff = _mute_affects_main_outs;
1925 Route::set_active (bool yn)
1928 active_changed(); /* EMIT SIGNAL */
1932 Route::handle_transport_stopped (bool abort_ignored, bool did_locate, bool can_flush_redirects)
1934 nframes_t now = _session.transport_frame();
1937 Glib::RWLock::ReaderLock lm (redirect_lock);
1939 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
1941 if (Config->get_plugins_stop_with_transport() && can_flush_redirects) {
1942 (*i)->deactivate ();
1946 (*i)->transport_stopped (now);
1950 IO::transport_stopped (now);
1952 _roll_delay = _initial_delay;
1956 Route::get_memento() const
1958 void (Route::*pmf)(state_id_t) = &Route::set_state;
1959 return sigc::bind (mem_fun (*(const_cast<Route *>(this)), pmf), _current_state_id);
1963 Route::set_state (state_id_t id)
1969 Route::input_change_handler (IOChange change, void *ignored)
1971 if (change & ConfigurationChanged) {
1972 reset_plugin_counts (0);
1977 Route::output_change_handler (IOChange change, void *ignored)
1979 if (change & ConfigurationChanged) {
1980 if (_control_outs) {
1981 _control_outs->ensure_io (ChanCount::ZERO, ChanCount(DataType::AUDIO, n_outputs().get(DataType::AUDIO)), true, this);
1984 reset_plugin_counts (0);
1989 Route::pans_required () const
1991 if (n_outputs().get(DataType::AUDIO) < 2) {
1995 return max (n_inputs ().get(DataType::AUDIO), static_cast<size_t>(redirect_max_outs.get(DataType::AUDIO)));
1999 Route::no_roll (nframes_t nframes, nframes_t start_frame, nframes_t end_frame, nframes_t offset,
2000 bool session_state_changing, bool can_record, bool rec_monitors_input)
2002 if (n_outputs().get_total() == 0) {
2006 if (session_state_changing || !_active) {
2007 silence (nframes, offset);
2011 apply_gain_automation = false;
2013 if (n_inputs().get_total()) {
2014 passthru (start_frame, end_frame, nframes, offset, 0, false);
2016 silence (nframes, offset);
2023 Route::check_initial_delay (nframes_t nframes, nframes_t& offset, nframes_t& transport_frame)
2025 if (_roll_delay > nframes) {
2027 _roll_delay -= nframes;
2028 silence (nframes, offset);
2029 /* transport frame is not legal for caller to use */
2032 } else if (_roll_delay > 0) {
2034 nframes -= _roll_delay;
2036 silence (_roll_delay, offset);
2038 offset += _roll_delay;
2039 transport_frame += _roll_delay;
2048 Route::roll (nframes_t nframes, nframes_t start_frame, nframes_t end_frame, nframes_t offset, int declick,
2049 bool can_record, bool rec_monitors_input)
2051 if ((n_outputs().get_total() == 0 && _redirects.empty()) || n_inputs().get_total() == 0 || !_active) {
2052 silence (nframes, offset);
2056 nframes_t unused = 0;
2058 if ((nframes = check_initial_delay (nframes, offset, unused)) == 0) {
2064 apply_gain_automation = false;
2067 Glib::Mutex::Lock am (automation_lock, Glib::TRY_LOCK);
2069 if (am.locked() && _session.transport_rolling()) {
2071 if (gain_automation_playback()) {
2072 apply_gain_automation = _gain_automation_curve.rt_safe_get_vector (start_frame, end_frame, _session.gain_automation_buffer(), nframes);
2077 passthru (start_frame, end_frame, nframes, offset, declick, false);
2083 Route::silent_roll (nframes_t nframes, nframes_t start_frame, nframes_t end_frame, nframes_t offset,
2084 bool can_record, bool rec_monitors_input)
2086 silence (nframes, offset);
2091 Route::toggle_monitor_input ()
2093 for (PortSet::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
2094 i->ensure_monitor_input( ! i->monitoring_input());
2099 Route::has_external_redirects () const
2101 boost::shared_ptr<const PortInsert> pi;
2103 for (RedirectList::const_iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2104 if ((pi = boost::dynamic_pointer_cast<const PortInsert>(*i)) != 0) {
2106 for (PortSet::const_iterator port = pi->outputs().begin();
2107 port != pi->outputs().end(); ++port) {
2109 string port_name = port->name();
2110 string client_name = port_name.substr (0, port_name.find(':'));
2112 /* only say "yes" if the redirect is actually in use */
2114 if (client_name != "ardour" && pi->active()) {
2125 Route::flush_redirects ()
2127 /* XXX shouldn't really try to take this lock, since
2128 this is called from the RT audio thread.
2131 Glib::RWLock::ReaderLock lm (redirect_lock);
2133 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2134 (*i)->deactivate ();
2140 Route::set_meter_point (MeterPoint p, void *src)
2142 if (_meter_point != p) {
2144 meter_change (src); /* EMIT SIGNAL */
2145 _session.set_dirty ();
2150 Route::update_total_latency ()
2154 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2155 if ((*i)->active ()) {
2156 _own_latency += (*i)->latency ();
2160 set_port_latency (_own_latency);
2162 /* this (virtual) function is used for pure Routes,
2163 not derived classes like AudioTrack. this means
2164 that the data processed here comes from an input
2165 port, not prerecorded material, and therefore we
2166 have to take into account any input latency.
2169 _own_latency += input_latency ();
2171 return _own_latency;
2175 Route::set_latency_delay (nframes_t longest_session_latency)
2177 _initial_delay = longest_session_latency - _own_latency;
2179 if (_session.transport_stopped()) {
2180 _roll_delay = _initial_delay;
2184 Route::ToggleControllable::ToggleControllable (std::string name, Route& s, ToggleType tp)
2185 : Controllable (name), route (s), type(tp)
2191 Route::ToggleControllable::set_value (float val)
2193 bool bval = ((val >= 0.5f) ? true: false);
2197 route.set_mute (bval, this);
2200 route.set_solo (bval, this);
2208 Route::ToggleControllable::get_value (void) const
2214 val = route.muted() ? 1.0f : 0.0f;
2217 val = route.soloed() ? 1.0f : 0.0f;
2227 Route::set_block_size (nframes_t nframes)
2229 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2230 (*i)->set_block_size (nframes);
2235 Route::redirect_active_proxy (Redirect* ignored, void* ignored_src)
2237 _session.update_latency_compensation (false, false);
2241 Route::protect_automation ()
2243 switch (gain_automation_state()) {
2246 set_gain_automation_state (Off);
2252 switch (panner().automation_state ()) {
2255 panner().set_automation_state (Off);
2261 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2262 boost::shared_ptr<PluginInsert> pi;
2263 if ((pi = boost::dynamic_pointer_cast<PluginInsert> (*i)) != 0) {
2264 pi->protect_automation ();
2270 Route::set_pending_declick (int declick)
2273 /* this call is not allowed to turn off a pending declick unless "force" is true */
2275 _pending_declick = declick;
2277 // cerr << _name << ": after setting to " << declick << " pending declick = " << _pending_declick << endl;
2279 _pending_declick = 0;