when renaming redirects, scan all routes AND sends AND port inserts for the name...
[ardour2.git] / libs / ardour / route.cc
blob4e19b9c23eb36f2ed564da6108d52696b3f5aceb
1 /*
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.
20 #include <cmath>
21 #include <fstream>
22 #include <cassert>
24 #include <sigc++/bind.h>
25 #include <pbd/xml++.h>
26 #include <pbd/enumwriter.h>
27 #include <pbd/stacktrace.h>
28 #include <pbd/memento_command.h>
30 #include <ardour/timestamps.h>
31 #include <ardour/buffer.h>
32 #include <ardour/audioengine.h>
33 #include <ardour/route.h>
34 #include <ardour/insert.h>
35 #include <ardour/send.h>
36 #include <ardour/session.h>
37 #include <ardour/utils.h>
38 #include <ardour/configuration.h>
39 #include <ardour/cycle_timer.h>
40 #include <ardour/route_group.h>
41 #include <ardour/port.h>
42 #include <ardour/ladspa_plugin.h>
43 #include <ardour/panner.h>
44 #include <ardour/dB.h>
45 #include <ardour/mix.h>
46 #include <ardour/profile.h>
48 #include "i18n.h"
50 using namespace std;
51 using namespace ARDOUR;
52 using namespace PBD;
54 uint32_t Route::order_key_cnt = 0;
55 sigc::signal<void,const char*> Route::SyncOrderKeys;
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),
59 _flags (flg),
60 _solo_control (X_("solo"), *this, ToggleControllable::SoloControl),
61 _mute_control (X_("mute"), *this, ToggleControllable::MuteControl)
63 init ();
66 Route::Route (Session& sess, const XMLNode& node, DataType default_type)
67 : IO (sess, *node.child ("IO"), default_type),
68 _solo_control (X_("solo"), *this, ToggleControllable::SoloControl),
69 _mute_control (X_("mute"), *this, ToggleControllable::MuteControl)
71 init ();
72 _set_state (node, false);
75 void
76 Route::init ()
78 redirect_max_outs = 0;
79 _muted = false;
80 _soloed = false;
81 _solo_safe = false;
82 _phase_invert = false;
83 _denormal_protection = false;
84 order_keys[strdup (N_("signal"))] = order_key_cnt++;
85 _silent = false;
86 _meter_point = MeterPostFader;
87 _initial_delay = 0;
88 _roll_delay = 0;
89 _own_latency = 0;
90 _have_internal_generator = false;
91 _declickable = false;
92 _pending_declick = true;
93 _remote_control_id = 0;
94 _ignore_gain_on_deliver = true;
96 _edit_group = 0;
97 _mix_group = 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();
104 solo_gain = 1.0;
105 desired_solo_gain = 1.0;
106 mute_gain = 1.0;
107 desired_mute_gain = 1.0;
109 _control_outs = 0;
111 input_changed.connect (mem_fun (this, &Route::input_change_handler));
112 output_changed.connect (mem_fun (this, &Route::output_change_handler));
115 Route::~Route ()
117 clear_redirects (PreFader, this);
118 clear_redirects (PostFader, this);
120 for (OrderKeys::iterator i = order_keys.begin(); i != order_keys.end(); ++i) {
121 free ((void*)(i->first));
124 if (_control_outs) {
125 delete _control_outs;
129 void
130 Route::set_remote_control_id (uint32_t id)
132 if (id != _remote_control_id) {
133 _remote_control_id = id;
134 RemoteControlIDChanged ();
138 uint32_t
139 Route::remote_control_id() const
141 return _remote_control_id;
144 long
145 Route::order_key (const char* name) const
147 OrderKeys::const_iterator i;
149 for (i = order_keys.begin(); i != order_keys.end(); ++i) {
150 if (!strcmp (name, i->first)) {
151 return i->second;
155 return -1;
158 void
159 Route::set_order_key (const char* name, long n)
161 order_keys[strdup(name)] = n;
163 if (Config->get_sync_all_route_ordering()) {
164 for (OrderKeys::iterator x = order_keys.begin(); x != order_keys.end(); ++x) {
165 x->second = n;
169 _session.set_dirty ();
172 void
173 Route::sync_order_keys (const char* base)
175 if (order_keys.empty()) {
176 return;
179 OrderKeys::iterator i;
180 uint32_t key;
182 if ((i = order_keys.find (base)) == order_keys.end()) {
183 /* key doesn't exist, use the first existing
184 key (this is done during session initialization)
186 i = order_keys.begin();
187 key = i->second;
188 ++i;
189 } else {
190 /* key exists - use it and reset all others
191 (actually, itself included)
193 i = order_keys.begin();
194 key = i->second;
197 for (; i != order_keys.end(); ++i) {
198 i->second = key;
202 string
203 Route::ensure_track_or_route_name(string name, Session &session)
205 string newname = name;
207 while (session.route_by_name (newname)!=NULL)
209 newname = bump_name_once (newname);
212 return newname;
216 void
217 Route::inc_gain (gain_t fraction, void *src)
219 IO::inc_gain (fraction, src);
222 void
223 Route::set_gain (gain_t val, void *src)
225 if (src != 0 && _mix_group && src != _mix_group && _mix_group->is_active()) {
227 if (_mix_group->is_relative()) {
230 gain_t usable_gain = gain();
231 if (usable_gain < 0.000001f) {
232 usable_gain=0.000001f;
235 gain_t delta = val;
236 if (delta < 0.000001f) {
237 delta=0.000001f;
240 delta -= usable_gain;
242 if (delta == 0.0f) return;
244 gain_t factor = delta / usable_gain;
246 if (factor > 0.0f) {
247 factor = _mix_group->get_max_factor(factor);
248 if (factor == 0.0f) {
249 gain_changed (src);
250 return;
252 } else {
253 factor = _mix_group->get_min_factor(factor);
254 if (factor == 0.0f) {
255 gain_changed (src);
256 return;
260 _mix_group->apply (&Route::inc_gain, factor, _mix_group);
262 } else {
264 _mix_group->apply (&Route::set_gain, val, _mix_group);
267 return;
270 if (val == gain()) {
271 return;
274 IO::set_gain (val, src);
277 void
278 Route::process_output_buffers (vector<Sample*>& bufs, uint32_t nbufs,
279 nframes_t start_frame, nframes_t end_frame,
280 nframes_t nframes, bool with_redirects, int declick,
281 bool meter)
283 uint32_t n;
284 RedirectList::iterator i;
285 bool post_fader_work = false;
286 bool mute_declick_applied = false;
287 gain_t dmg, dsg, dg;
288 vector<Sample*>::iterator bufiter;
289 IO *co;
290 bool mute_audible;
291 bool solo_audible;
292 bool no_monitor;
293 gain_t* gab = _session.gain_automation_buffer();
295 switch (Config->get_monitoring_model()) {
296 case HardwareMonitoring:
297 case ExternalMonitoring:
298 no_monitor = true;
299 break;
300 default:
301 no_monitor = false;
304 declick = _pending_declick;
307 Glib::Mutex::Lock cm (control_outs_lock, Glib::TRY_LOCK);
309 if (cm.locked()) {
310 co = _control_outs;
311 } else {
312 co = 0;
317 Glib::Mutex::Lock dm (declick_lock, Glib::TRY_LOCK);
319 if (dm.locked()) {
320 dmg = desired_mute_gain;
321 dsg = desired_solo_gain;
322 dg = _desired_gain;
323 } else {
324 dmg = mute_gain;
325 dsg = solo_gain;
326 dg = _gain;
330 /* ----------------------------------------------------------------------------------------------------
331 GLOBAL DECLICK (for transport changes etc.)
332 -------------------------------------------------------------------------------------------------- */
334 if (declick > 0) {
335 apply_declick (bufs, nbufs, nframes, 0.0, 1.0, false);
336 _pending_declick = 0;
337 } else if (declick < 0) {
338 apply_declick (bufs, nbufs, nframes, 1.0, 0.0, false);
339 _pending_declick = 0;
340 } else {
342 /* no global declick */
344 if (solo_gain != dsg) {
345 apply_declick (bufs, nbufs, nframes, solo_gain, dsg, false);
346 solo_gain = dsg;
351 /* ----------------------------------------------------------------------------------------------------
352 INPUT METERING & MONITORING
353 -------------------------------------------------------------------------------------------------- */
355 if (meter && (_meter_point == MeterInput)) {
356 for (n = 0; n < nbufs; ++n) {
357 _peak_power[n] = Session::compute_peak (bufs[n], nframes, _peak_power[n]);
361 if (!_soloed && _mute_affects_pre_fader && (mute_gain != dmg)) {
362 apply_declick (bufs, nbufs, nframes, mute_gain, dmg, false);
363 mute_gain = dmg;
364 mute_declick_applied = true;
367 if ((_meter_point == MeterInput) && co) {
369 solo_audible = dsg > 0;
370 mute_audible = dmg > 0;// || !_mute_affects_pre_fader;
372 if ( // muted by solo of another track
374 !solo_audible ||
376 // muted by mute of this track
378 !mute_audible ||
380 // rec-enabled but not s/w monitoring
382 // TODO: this is probably wrong
384 (no_monitor && record_enabled() && (!Config->get_auto_input() || _session.actively_recording()))
388 co->silence (nframes);
390 } else {
392 co->deliver_output (bufs, nbufs, nframes);
397 /* -----------------------------------------------------------------------------------------------------
398 DENORMAL CONTROL
399 -------------------------------------------------------------------------------------------------- */
401 if (_denormal_protection || Config->get_denormal_protection()) {
403 for (n = 0; n < nbufs; ++n) {
404 Sample *sp = bufs[n];
406 for (nframes_t nx = 0; nx < nframes; ++nx) {
407 sp[nx] += 1.0e-27f;
413 /* ----------------------------------------------------------------------------------------------------
414 PRE-FADER REDIRECTS
415 -------------------------------------------------------------------------------------------------- */
417 if (with_redirects) {
418 Glib::RWLock::ReaderLock rm (redirect_lock, Glib::TRY_LOCK);
419 if (rm.locked()) {
420 if (mute_gain > 0 || !_mute_affects_pre_fader) {
421 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
422 switch ((*i)->placement()) {
423 case PreFader:
424 if (dsg == 0) {
425 if (boost::dynamic_pointer_cast<Send>(*i) || boost::dynamic_pointer_cast<PortInsert>(*i)) {
426 (*i)->silence (nframes);
428 } else {
429 (*i)->run (bufs, nbufs, nframes);
431 break;
432 case PostFader:
433 post_fader_work = true;
434 break;
437 } else {
438 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
439 switch ((*i)->placement()) {
440 case PreFader:
441 (*i)->silence (nframes);
442 break;
443 case PostFader:
444 post_fader_work = true;
445 break;
453 if (!_soloed && (mute_gain != dmg) && !mute_declick_applied && _mute_affects_post_fader) {
454 apply_declick (bufs, nbufs, nframes, mute_gain, dmg, false);
455 mute_gain = dmg;
456 mute_declick_applied = true;
459 /* ----------------------------------------------------------------------------------------------------
460 PRE-FADER METERING & MONITORING
461 -------------------------------------------------------------------------------------------------- */
463 if (meter && (_meter_point == MeterPreFader)) {
464 for (n = 0; n < nbufs; ++n) {
465 _peak_power[n] = Session::compute_peak (bufs[n], nframes, _peak_power[n]);
470 if ((_meter_point == MeterPreFader) && co) {
472 solo_audible = dsg > 0;
473 mute_audible = dmg > 0 || !_mute_affects_pre_fader;
475 if ( // muted by solo of another track
477 !solo_audible ||
479 // muted by mute of this track
481 !mute_audible ||
483 // rec-enabled but not s/w monitoring
485 (no_monitor && record_enabled() && (!Config->get_auto_input() || _session.actively_recording()))
489 co->silence (nframes);
491 } else {
493 co->deliver_output_no_pan (bufs, nbufs, nframes);
498 /* ----------------------------------------------------------------------------------------------------
499 GAIN STAGE
500 -------------------------------------------------------------------------------------------------- */
502 /* if not recording or recording and requiring any monitor signal, then apply gain */
504 if ( // not recording
506 !(record_enabled() && _session.actively_recording()) ||
508 // OR recording
510 // AND software monitoring required
512 (Config->get_monitoring_model() == SoftwareMonitoring)) {
514 if (apply_gain_automation) {
516 if (_phase_invert) {
517 for (n = 0; n < nbufs; ++n) {
518 Sample *sp = bufs[n];
520 for (nframes_t nx = 0; nx < nframes; ++nx) {
521 sp[nx] *= -gab[nx];
524 } else {
525 for (n = 0; n < nbufs; ++n) {
526 Sample *sp = bufs[n];
528 for (nframes_t nx = 0; nx < nframes; ++nx) {
529 sp[nx] *= gab[nx];
534 if (apply_gain_automation && _session.transport_rolling() && nframes > 0) {
535 _effective_gain = gab[nframes-1];
538 } else {
540 /* manual (scalar) gain */
542 if (_gain != dg) {
544 apply_declick (bufs, nbufs, nframes, _gain, dg, _phase_invert);
545 _gain = dg;
547 } else if (_gain != 0 && (_phase_invert || _gain != 1.0)) {
549 /* no need to interpolate current gain value,
550 but its non-unity, so apply it. if the gain
551 is zero, do nothing because we'll ship silence
552 below.
555 gain_t this_gain;
557 if (_phase_invert) {
558 this_gain = -_gain;
559 } else {
560 this_gain = _gain;
563 for (n = 0; n < nbufs; ++n) {
564 Sample *sp = bufs[n];
565 Session::apply_gain_to_buffer(sp,nframes,this_gain);
568 } else if (_gain == 0) {
569 for (n = 0; n < nbufs; ++n) {
570 memset (bufs[n], 0, sizeof (Sample) * nframes);
575 } else {
577 /* actively recording, no monitoring required; leave buffers as-is to save CPU cycles */
581 /* ----------------------------------------------------------------------------------------------------
582 POST-FADER REDIRECTS
583 -------------------------------------------------------------------------------------------------- */
585 /* note that post_fader_work cannot be true unless with_redirects was also true, so don't test both */
587 if (post_fader_work) {
589 Glib::RWLock::ReaderLock rm (redirect_lock, Glib::TRY_LOCK);
590 if (rm.locked()) {
591 if (mute_gain > 0 || !_mute_affects_post_fader) {
592 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
593 switch ((*i)->placement()) {
594 case PreFader:
595 break;
596 case PostFader:
597 if (dsg == 0) {
598 if (boost::dynamic_pointer_cast<Send>(*i) || boost::dynamic_pointer_cast<PortInsert>(*i)) {
599 (*i)->silence (nframes);
601 } else {
602 (*i)->run (bufs, nbufs, nframes);
604 break;
607 } else {
608 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
609 switch ((*i)->placement()) {
610 case PreFader:
611 break;
612 case PostFader:
613 (*i)->silence (nframes);
614 break;
621 if (!_soloed && (mute_gain != dmg) && !mute_declick_applied && _mute_affects_control_outs) {
622 apply_declick (bufs, nbufs, nframes, mute_gain, dmg, false);
623 mute_gain = dmg;
624 mute_declick_applied = true;
627 /* ----------------------------------------------------------------------------------------------------
628 CONTROL OUTPUT STAGE
629 -------------------------------------------------------------------------------------------------- */
631 if ((_meter_point == MeterPostFader) && co) {
633 solo_audible = solo_gain > 0;
634 mute_audible = dmg > 0 || !_mute_affects_control_outs;
636 if ( // silent anyway
638 (_gain == 0 && !apply_gain_automation) ||
640 // muted by solo of another track
642 !solo_audible ||
644 // muted by mute of this track
646 !mute_audible ||
648 // recording but not s/w monitoring
650 (no_monitor && record_enabled() && (!Config->get_auto_input() || _session.actively_recording()))
654 co->silence (nframes);
656 } else {
658 co->deliver_output_no_pan (bufs, nbufs, nframes);
662 /* ----------------------------------------------------------------------
663 GLOBAL MUTE
664 ----------------------------------------------------------------------*/
666 if (!_soloed && (mute_gain != dmg) && !mute_declick_applied && _mute_affects_main_outs) {
667 apply_declick (bufs, nbufs, nframes, mute_gain, dmg, false);
668 mute_gain = dmg;
669 mute_declick_applied = true;
672 /* ----------------------------------------------------------------------------------------------------
673 MAIN OUTPUT STAGE
674 -------------------------------------------------------------------------------------------------- */
676 solo_audible = dsg > 0;
677 mute_audible = dmg > 0 || !_mute_affects_main_outs;
679 if (n_outputs() == 0) {
681 /* relax */
683 } else if (no_monitor && record_enabled() && (!Config->get_auto_input() || _session.actively_recording())) {
685 IO::silence (nframes);
687 } else {
689 if ( // silent anyway
691 (_gain == 0 && !apply_gain_automation) ||
693 // muted by solo of another track, but not using control outs for solo
695 (!solo_audible && (Config->get_solo_model() != SoloBus)) ||
697 // muted by mute of this track
699 !mute_audible
703 /* don't use Route::silence() here, because that causes
704 all outputs (sends, port inserts, etc. to be silent).
707 if (_meter_point == MeterPostFader) {
708 reset_peak_meters ();
711 IO::silence (nframes);
713 } else {
715 if ((_session.transport_speed() > 1.5f ||
716 _session.transport_speed() < -1.5f) &&
717 Config->get_quieten_at_speed()) {
718 pan (bufs, nbufs, nframes, speed_quietning);
719 } else {
720 // cerr << _name << " panner state = " << _panner->automation_state() << endl;
721 if (!_panner->empty() &&
722 (_panner->automation_state() & Play ||
723 ((_panner->automation_state() & Touch) && !_panner->touching()))) {
724 pan_automated (bufs, nbufs, start_frame, end_frame, nframes);
725 } else {
726 pan (bufs, nbufs, nframes, 1.0);
733 /* ----------------------------------------------------------------------------------------------------
734 POST-FADER METERING
735 -------------------------------------------------------------------------------------------------- */
737 if (meter && (_meter_point == MeterPostFader)) {
739 if ((_gain == 0 && !apply_gain_automation) || dmg == 0) {
740 uint32_t no = n_outputs();
741 for (n = 0; n < no; ++n) {
742 _peak_power[n] = 0;
744 } else {
745 uint32_t no = n_outputs();
746 for (n = 0; n < no; ++n) {
747 _peak_power[n] = Session::compute_peak (get_output_buffer (n, nframes), nframes, _peak_power[n]);
753 uint32_t
754 Route::n_process_buffers ()
756 return max (n_inputs(), redirect_max_outs);
759 void
761 Route::passthru (nframes_t start_frame, nframes_t end_frame, nframes_t nframes, int declick, bool meter_first)
763 vector<Sample*>& bufs = _session.get_passthru_buffers();
764 uint32_t limit = n_process_buffers ();
766 _silent = false;
768 collect_input (bufs, limit, nframes);
770 #define meter_stream meter_first
772 if (meter_first) {
773 for (uint32_t n = 0; n < limit; ++n) {
774 _peak_power[n] = Session::compute_peak (bufs[n], nframes, _peak_power[n]);
776 meter_stream = false;
777 } else {
778 meter_stream = true;
781 process_output_buffers (bufs, limit, start_frame, end_frame, nframes, true, declick, meter_stream);
783 #undef meter_stream
786 void
787 Route::set_phase_invert (bool yn, void *src)
789 if (_phase_invert != yn) {
790 _phase_invert = yn;
791 // phase_invert_changed (src); /* EMIT SIGNAL */
795 void
796 Route::set_denormal_protection (bool yn, void *src)
798 if (_denormal_protection != yn) {
799 _denormal_protection = yn;
800 // denormal_protection_changed (src); /* EMIT SIGNAL */
804 void
805 Route::set_solo (bool yn, void *src)
807 if (_solo_safe) {
808 return;
811 if (_mix_group && src != _mix_group && _mix_group->is_active()) {
812 _mix_group->apply (&Route::set_solo, yn, _mix_group);
813 return;
816 if (_soloed != yn) {
817 _soloed = yn;
818 solo_changed (src); /* EMIT SIGNAL */
819 _solo_control.Changed (); /* EMIT SIGNAL */
822 catch_up_on_solo_mute_override ();
825 void
826 Route::catch_up_on_solo_mute_override ()
828 if (Config->get_solo_model() != InverseMute) {
829 return;
834 Glib::Mutex::Lock lm (declick_lock);
836 if (_muted) {
837 if (Config->get_solo_mute_override()) {
838 desired_mute_gain = (_soloed?1.0:0.0);
839 } else {
840 desired_mute_gain = 0.0;
842 } else {
843 desired_mute_gain = 1.0;
848 void
849 Route::set_solo_mute (bool yn)
851 Glib::Mutex::Lock lm (declick_lock);
853 /* Called by Session in response to another Route being soloed.
856 desired_solo_gain = (yn?0.0:1.0);
859 void
860 Route::set_solo_safe (bool yn, void *src)
862 if (_solo_safe != yn) {
863 _solo_safe = yn;
864 solo_safe_changed (src); /* EMIT SIGNAL */
868 void
869 Route::set_mute (bool yn, void *src)
872 if (_mix_group && src != _mix_group && _mix_group->is_active()) {
873 _mix_group->apply (&Route::set_mute, yn, _mix_group);
874 return;
877 if (_muted != yn) {
878 _muted = yn;
879 mute_changed (src); /* EMIT SIGNAL */
881 _mute_control.Changed (); /* EMIT SIGNAL */
883 Glib::Mutex::Lock lm (declick_lock);
885 if (_soloed && Config->get_solo_mute_override()){
886 desired_mute_gain = 1.0f;
887 } else {
888 desired_mute_gain = (yn?0.0f:1.0f);
894 Route::add_redirect (boost::shared_ptr<Redirect> redirect, void *src, uint32_t* err_streams)
896 uint32_t old_rmo = redirect_max_outs;
898 if (!_session.engine().connected()) {
899 return 1;
903 Glib::RWLock::WriterLock lm (redirect_lock);
905 boost::shared_ptr<PluginInsert> pi;
906 boost::shared_ptr<PortInsert> porti;
908 _redirects.push_back (redirect);
910 if (_reset_plugin_counts (err_streams)) {
911 _redirects.pop_back ();
912 _reset_plugin_counts (0); // it worked before we tried to add it ...
913 return -1;
916 uint32_t potential_max_streams = 0;
918 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(redirect)) != 0) {
920 if (pi->input_streams() == 0) {
921 /* instrument plugin */
922 _have_internal_generator = true;
925 potential_max_streams = max(pi->input_streams(), pi->output_streams());
927 } else if ((porti = boost::dynamic_pointer_cast<PortInsert>(redirect)) != 0) {
929 /* force new port inserts to start out with an i/o configuration
930 that matches this route's i/o configuration.
932 the "inputs" for the port are supposed to match the output
933 of this route.
935 the "outputs" of the route should match the inputs of this
936 route. XXX shouldn't they match the number of active signal
937 streams at the point of insertion?
941 porti->ensure_io (n_outputs (), n_inputs(), false, this);
944 // Ensure peak vector sizes before the plugin is activated
945 while (_peak_power.size() < potential_max_streams) {
946 _peak_power.push_back(0);
948 while (_visible_peak_power.size() < potential_max_streams) {
949 _visible_peak_power.push_back(-INFINITY);
951 while (_max_peak_power.size() < potential_max_streams) {
952 _max_peak_power.push_back(-INFINITY);
955 redirect->active_changed.connect (mem_fun (*this, &Route::redirect_active_proxy));
958 if (redirect_max_outs != old_rmo || old_rmo == 0) {
959 reset_panner ();
962 redirects_changed (src); /* EMIT SIGNAL */
964 return 0;
968 Route::add_redirects (const RedirectList& others, void *src, uint32_t* err_streams)
970 uint32_t old_rmo = redirect_max_outs;
972 assert (ports_legal);
974 if (!_session.engine().connected()) {
975 return 1;
979 Glib::RWLock::WriterLock lm (redirect_lock);
981 RedirectList::iterator existing_end = _redirects.end();
982 --existing_end;
984 uint32_t potential_max_streams = 0;
986 for (RedirectList::const_iterator i = others.begin(); i != others.end(); ++i) {
988 boost::shared_ptr<PluginInsert> pi;
990 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(*i)) != 0) {
991 pi->set_count (1);
993 uint32_t m = max(pi->input_streams(), pi->output_streams());
994 if (m > potential_max_streams)
995 potential_max_streams = m;
998 // Ensure peak vector sizes before the plugin is activated
999 while (_peak_power.size() < potential_max_streams) {
1000 _peak_power.push_back(0);
1002 while (_visible_peak_power.size() < potential_max_streams) {
1003 _visible_peak_power.push_back(-INFINITY);
1005 while (_max_peak_power.size() < potential_max_streams) {
1006 _max_peak_power.push_back(-INFINITY);
1009 _redirects.push_back (*i);
1011 if (_reset_plugin_counts (err_streams)) {
1012 ++existing_end;
1013 _redirects.erase (existing_end, _redirects.end());
1014 _reset_plugin_counts (0); // it worked before we tried to add it ...
1015 return -1;
1018 (*i)->active_changed.connect (mem_fun (*this, &Route::redirect_active_proxy));
1022 if (redirect_max_outs != old_rmo || old_rmo == 0) {
1023 reset_panner ();
1026 redirects_changed (src); /* EMIT SIGNAL */
1027 return 0;
1030 /** Remove redirects with a given placement.
1031 * @param p Placement of redirects to remove.
1033 void
1034 Route::clear_redirects (Placement p, void *src)
1036 const uint32_t old_rmo = redirect_max_outs;
1038 if (!_session.engine().connected()) {
1039 return;
1043 Glib::RWLock::WriterLock lm (redirect_lock);
1044 RedirectList new_list;
1046 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
1047 if ((*i)->placement() == p) {
1048 /* it's the placement we want to get rid of */
1049 (*i)->drop_references ();
1050 } else {
1051 /* it's a different placement, so keep it */
1052 new_list.push_back (*i);
1056 _redirects = new_list;
1059 /* FIXME: can't see how this test can ever fire */
1060 if (redirect_max_outs != old_rmo) {
1061 reset_panner ();
1064 redirect_max_outs = 0;
1065 _have_internal_generator = false;
1066 redirects_changed (src); /* EMIT SIGNAL */
1070 Route::remove_redirect (boost::shared_ptr<Redirect> redirect, void *src, uint32_t* err_streams)
1072 uint32_t old_rmo = redirect_max_outs;
1074 assert (ports_legal);
1076 if (!_session.engine().connected()) {
1077 return 1;
1080 redirect_max_outs = 0;
1083 Glib::RWLock::WriterLock lm (redirect_lock);
1084 RedirectList::iterator i;
1085 bool removed = false;
1087 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
1088 if (*i == redirect) {
1090 RedirectList::iterator tmp;
1092 /* move along, see failure case for reset_plugin_counts()
1093 where we may need to reinsert the redirect.
1096 tmp = i;
1097 ++tmp;
1099 /* stop redirects that send signals to JACK ports
1100 from causing noise as a result of no longer being
1101 run.
1104 boost::shared_ptr<Send> send;
1105 boost::shared_ptr<PortInsert> port_insert;
1107 if ((send = boost::dynamic_pointer_cast<Send> (*i)) != 0) {
1108 send->disconnect_inputs (this);
1109 send->disconnect_outputs (this);
1110 } else if ((port_insert = boost::dynamic_pointer_cast<PortInsert> (*i)) != 0) {
1111 port_insert->disconnect_inputs (this);
1112 port_insert->disconnect_outputs (this);
1115 _redirects.erase (i);
1117 i = tmp;
1118 removed = true;
1119 break;
1123 if (!removed) {
1124 /* what? */
1125 return 1;
1128 if (_reset_plugin_counts (err_streams)) {
1129 /* get back to where we where */
1130 _redirects.insert (i, redirect);
1131 /* we know this will work, because it worked before :) */
1132 _reset_plugin_counts (0);
1133 return -1;
1136 _have_internal_generator = false;
1138 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
1139 boost::shared_ptr<PluginInsert> pi;
1141 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(*i)) != 0) {
1142 if (pi->is_generator()) {
1143 _have_internal_generator = true;
1144 break;
1150 if (old_rmo != redirect_max_outs) {
1151 reset_panner ();
1154 redirect->drop_references ();
1155 redirects_changed (src); /* EMIT SIGNAL */
1157 return 0;
1161 Route::reset_plugin_counts (uint32_t* lpc)
1163 Glib::RWLock::WriterLock lm (redirect_lock);
1164 return _reset_plugin_counts (lpc);
1169 Route::_reset_plugin_counts (uint32_t* err_streams)
1171 RedirectList::iterator r;
1172 uint32_t insert_cnt = 0;
1173 uint32_t send_cnt = 0;
1174 map<Placement,list<InsertCount> > insert_map;
1175 RedirectList::iterator prev;
1176 int32_t initial_streams = n_inputs ();;
1177 int32_t previous_initial_streams = n_inputs ();
1178 int ret = -1;
1180 redirect_max_outs = 0;
1182 /* Step 1: build a map that links each insert to an in/out channel count
1184 Divide inserts up by placement so we get the signal flow
1185 properly modelled. we need to do this because the _redirects
1186 list is not sorted by placement, and because other reasons may
1187 exist now or in the future for this separate treatment.
1190 for (r = _redirects.begin(); r != _redirects.end(); ++r) {
1192 boost::shared_ptr<Insert> insert;
1194 if ((insert = boost::dynamic_pointer_cast<Insert>(*r)) != 0) {
1195 ++insert_cnt;
1196 insert_map[insert->placement()].push_back (InsertCount (insert));
1198 /* reset plugin counts back to one for now so
1199 that we have a predictable, controlled
1200 state to try to configure.
1203 boost::shared_ptr<PluginInsert> pi;
1205 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(insert)) != 0) {
1206 pi->set_count (1);
1209 } else if (boost::dynamic_pointer_cast<Send> (*r) != 0) {
1210 ++send_cnt;
1214 if (insert_cnt == 0) {
1215 if (send_cnt) {
1216 goto recompute;
1217 } else {
1218 ret = 0;
1219 goto streamcount;
1223 /* Now process each placement in order, checking to see if we
1224 can really do what has been requested.
1227 /* A: PreFader */
1229 if (check_some_plugin_counts (insert_map[PreFader], n_inputs (), err_streams)) {
1230 goto streamcount;
1233 /* figure out the streams that will feed into PreFader */
1235 if (!insert_map[PreFader].empty()) {
1236 previous_initial_streams = n_inputs ();
1237 for (list<InsertCount>::iterator i = insert_map[PreFader].begin(); i != insert_map[PreFader].end(); i++) {
1238 if (i->insert->can_do (previous_initial_streams, initial_streams) < 0) {
1239 goto streamcount;
1241 previous_initial_streams = initial_streams;
1245 /* B: PostFader */
1247 if (check_some_plugin_counts (insert_map[PostFader], initial_streams, err_streams)) {
1248 goto streamcount;
1251 if (!insert_map[PostFader].empty()) {
1252 for (list<InsertCount>::iterator i = insert_map[PostFader].begin(); i != insert_map[PostFader].end(); i++) {
1253 if (i->insert->can_do (previous_initial_streams, initial_streams) < 0) {
1254 goto streamcount;
1256 previous_initial_streams = initial_streams;
1260 /* OK, everything can be set up correctly, so lets do it */
1262 apply_some_plugin_counts (insert_map[PreFader]);
1263 apply_some_plugin_counts (insert_map[PostFader]);
1265 /* recompute max outs of any redirect */
1267 recompute:
1269 redirect_max_outs = 0;
1270 prev = _redirects.end();
1272 for (r = _redirects.begin(); r != _redirects.end(); prev = r, ++r) {
1273 boost::shared_ptr<Send> s;
1275 if ((s = boost::dynamic_pointer_cast<Send> (*r)) != 0) {
1276 if (r == _redirects.begin()) {
1277 s->expect_inputs (n_inputs());
1278 } else {
1279 s->expect_inputs ((*prev)->output_streams());
1282 } else {
1284 /* don't pay any attention to send output configuration, since it doesn't
1285 affect the route.
1288 redirect_max_outs = max ((*r)->output_streams (), redirect_max_outs);
1293 /* we're done */
1294 return 0;
1296 streamcount:
1297 for (r = _redirects.begin(); r != _redirects.end(); ++r) {
1298 redirect_max_outs = max ((*r)->output_streams (), redirect_max_outs);
1300 return ret;
1303 int32_t
1304 Route::apply_some_plugin_counts (list<InsertCount>& iclist)
1306 list<InsertCount>::iterator i;
1308 for (i = iclist.begin(); i != iclist.end(); ++i) {
1310 if ((*i).insert->configure_io ((*i).cnt, (*i).in, (*i).out)) {
1311 return -1;
1313 /* make sure that however many we have, they are all active */
1314 (*i).insert->activate ();
1317 return 0;
1320 int32_t
1321 Route::check_some_plugin_counts (list<InsertCount>& iclist, int32_t required_inputs, uint32_t* err_streams)
1323 list<InsertCount>::iterator i;
1325 for (i = iclist.begin(); i != iclist.end(); ++i) {
1327 if (((*i).cnt = (*i).insert->can_do (required_inputs, (*i).out)) < 0) {
1328 if (err_streams) {
1329 *err_streams = required_inputs;
1331 return -1;
1334 (*i).in = required_inputs;
1335 required_inputs = (*i).out;
1338 return 0;
1342 Route::copy_redirects (const Route& other, Placement placement, uint32_t* err_streams)
1344 uint32_t old_rmo = redirect_max_outs;
1346 if (err_streams) {
1347 *err_streams = 0;
1350 RedirectList to_be_deleted;
1353 Glib::RWLock::WriterLock lm (redirect_lock);
1354 RedirectList::iterator tmp;
1355 RedirectList the_copy;
1357 the_copy = _redirects;
1359 /* remove all relevant redirects */
1361 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ) {
1362 tmp = i;
1363 ++tmp;
1365 if ((*i)->placement() == placement) {
1366 to_be_deleted.push_back (*i);
1367 _redirects.erase (i);
1370 i = tmp;
1373 /* now copy the relevant ones from "other" */
1375 for (RedirectList::const_iterator i = other._redirects.begin(); i != other._redirects.end(); ++i) {
1376 if ((*i)->placement() == placement) {
1377 _redirects.push_back (Redirect::clone (*i));
1381 /* reset plugin stream handling */
1383 if (_reset_plugin_counts (err_streams)) {
1385 /* FAILED COPY ATTEMPT: we have to restore order */
1387 /* delete all cloned redirects */
1389 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ) {
1391 tmp = i;
1392 ++tmp;
1394 if ((*i)->placement() == placement) {
1395 _redirects.erase (i);
1398 i = tmp;
1401 /* restore the natural order */
1403 _redirects = the_copy;
1404 redirect_max_outs = old_rmo;
1406 /* we failed, even though things are OK again */
1408 return -1;
1410 } else {
1412 /* SUCCESSFUL COPY ATTEMPT: delete the redirects we removed pre-copy */
1413 to_be_deleted.clear ();
1417 if (redirect_max_outs != old_rmo || old_rmo == 0) {
1418 reset_panner ();
1421 redirects_changed (this); /* EMIT SIGNAL */
1422 return 0;
1425 void
1426 Route::all_redirects_flip ()
1428 Glib::RWLock::ReaderLock lm (redirect_lock);
1430 if (_redirects.empty()) {
1431 return;
1434 bool first_is_on = _redirects.front()->active();
1436 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
1437 (*i)->set_active (!first_is_on, this);
1441 /** Set all redirects with a given placement to a given active state.
1442 * @param p Placement of redirects to change.
1443 * @param state New active state for those redirects.
1445 void
1446 Route::all_redirects_active (Placement p, bool state)
1448 Glib::RWLock::ReaderLock lm (redirect_lock);
1450 if (_redirects.empty()) {
1451 return;
1454 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
1455 if ((*i)->placement() == p) {
1456 (*i)->set_active (state, this);
1461 struct RedirectSorter {
1462 bool operator() (boost::shared_ptr<const Redirect> a, boost::shared_ptr<const Redirect> b) {
1463 return a->sort_key() < b->sort_key();
1468 Route::sort_redirects (uint32_t* err_streams)
1471 RedirectSorter comparator;
1472 Glib::RWLock::WriterLock lm (redirect_lock);
1473 uint32_t old_rmo = redirect_max_outs;
1475 /* the sweet power of C++ ... */
1477 RedirectList as_it_was_before = _redirects;
1479 _redirects.sort (comparator);
1481 if (_reset_plugin_counts (err_streams)) {
1482 _redirects = as_it_was_before;
1483 redirect_max_outs = old_rmo;
1484 return -1;
1488 reset_panner ();
1489 redirects_changed (this); /* EMIT SIGNAL */
1491 return 0;
1494 XMLNode&
1495 Route::get_state()
1497 return state(true);
1500 XMLNode&
1501 Route::get_template()
1503 return state(false);
1506 XMLNode&
1507 Route::state(bool full_state)
1509 XMLNode *node = new XMLNode("Route");
1510 RedirectList:: iterator i;
1511 char buf[32];
1513 if (_flags) {
1514 node->add_property("flags", enum_2_string (_flags));
1517 node->add_property("default-type", _default_type.to_string());
1519 node->add_property("muted", _muted?"yes":"no");
1520 node->add_property("soloed", _soloed?"yes":"no");
1521 node->add_property("phase-invert", _phase_invert?"yes":"no");
1522 node->add_property("denormal-protection", _denormal_protection?"yes":"no");
1523 node->add_property("mute-affects-pre-fader", _mute_affects_pre_fader?"yes":"no");
1524 node->add_property("mute-affects-post-fader", _mute_affects_post_fader?"yes":"no");
1525 node->add_property("mute-affects-control-outs", _mute_affects_control_outs?"yes":"no");
1526 node->add_property("mute-affects-main-outs", _mute_affects_main_outs?"yes":"no");
1527 node->add_property("meter-point", enum_2_string (_meter_point));
1529 if (_edit_group) {
1530 node->add_property("edit-group", _edit_group->name());
1532 if (_mix_group) {
1533 node->add_property("mix-group", _mix_group->name());
1536 string order_string;
1537 OrderKeys::iterator x = order_keys.begin();
1539 while (x != order_keys.end()) {
1540 order_string += string ((*x).first);
1541 order_string += '=';
1542 snprintf (buf, sizeof(buf), "%ld", (*x).second);
1543 order_string += buf;
1545 ++x;
1547 if (x == order_keys.end()) {
1548 break;
1551 order_string += ':';
1553 node->add_property ("order-keys", order_string);
1555 node->add_child_nocopy (IO::state (full_state));
1556 node->add_child_nocopy (_solo_control.get_state ());
1557 node->add_child_nocopy (_mute_control.get_state ());
1559 XMLNode* remote_control_node = new XMLNode (X_("remote_control"));
1560 snprintf (buf, sizeof (buf), "%d", _remote_control_id);
1561 remote_control_node->add_property (X_("id"), buf);
1562 node->add_child_nocopy (*remote_control_node);
1564 if (_control_outs) {
1565 XMLNode* cnode = new XMLNode (X_("ControlOuts"));
1566 cnode->add_child_nocopy (_control_outs->state (full_state));
1567 node->add_child_nocopy (*cnode);
1570 if (_comment.length()) {
1571 XMLNode *cmt = node->add_child ("Comment");
1572 cmt->add_content (_comment);
1575 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
1576 node->add_child_nocopy((*i)->state (full_state));
1579 if (_extra_xml){
1580 node->add_child_copy (*_extra_xml);
1583 return *node;
1586 void
1587 Route::set_deferred_state ()
1589 XMLNodeList nlist;
1590 XMLNodeConstIterator niter;
1592 if (!deferred_state) {
1593 return;
1596 nlist = deferred_state->children();
1598 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
1599 add_redirect_from_xml (**niter);
1602 delete deferred_state;
1603 deferred_state = 0;
1606 void
1607 Route::add_redirect_from_xml (const XMLNode& node)
1609 const XMLProperty *prop;
1611 if (node.name() == "Send") {
1614 try {
1615 boost::shared_ptr<Send> send (new Send (_session, node));
1616 add_redirect (send, this);
1619 catch (failed_constructor &err) {
1620 error << _("Send construction failed") << endmsg;
1621 return;
1624 } else if (node.name() == "Insert") {
1626 try {
1627 if ((prop = node.property ("type")) != 0) {
1629 boost::shared_ptr<Insert> insert;
1630 bool have_insert = false;
1632 if (prop->value() == "ladspa" || prop->value() == "Ladspa" ||
1633 prop->value() == "lv2" ||
1634 prop->value() == "vst" ||
1635 prop->value() == "audiounit") {
1637 insert.reset (new PluginInsert(_session, node));
1638 have_insert = true;
1640 } else if (prop->value() == "port") {
1643 insert.reset (new PortInsert (_session, node));
1644 have_insert = true;
1646 } else {
1648 error << string_compose(_("unknown Insert type \"%1\"; ignored"), prop->value()) << endmsg;
1651 if (have_insert) {
1652 add_redirect (insert, this);
1655 } else {
1656 error << _("Insert XML node has no type property") << endmsg;
1660 catch (failed_constructor &err) {
1661 warning << _("insert could not be created. Ignored.") << endmsg;
1662 return;
1668 Route::set_state (const XMLNode& node)
1670 return _set_state (node, true);
1674 Route::_set_state (const XMLNode& node, bool call_base)
1676 XMLNodeList nlist;
1677 XMLNodeConstIterator niter;
1678 XMLNode *child;
1679 XMLPropertyList plist;
1680 const XMLProperty *prop;
1682 if (node.name() != "Route"){
1683 error << string_compose(_("Bad node sent to Route::set_state() [%1]"), node.name()) << endmsg;
1684 return -1;
1687 if ((prop = node.property (X_("flags"))) != 0) {
1688 _flags = Flag (string_2_enum (prop->value(), _flags));
1689 } else {
1690 _flags = Flag (0);
1693 if ((prop = node.property (X_("default-type"))) != 0) {
1694 _default_type = DataType(prop->value());
1695 assert(_default_type != DataType::NIL);
1698 if ((prop = node.property (X_("phase-invert"))) != 0) {
1699 set_phase_invert (string_is_affirmative (prop->value()), this);
1702 if ((prop = node.property (X_("denormal-protection"))) != 0) {
1703 set_denormal_protection (string_is_affirmative (prop->value()), this);
1706 if ((prop = node.property (X_("muted"))) != 0) {
1707 bool yn = string_is_affirmative (prop->value());
1709 /* force reset of mute status */
1711 _muted = !yn;
1712 set_mute(yn, this);
1713 mute_gain = desired_mute_gain;
1716 if ((prop = node.property (X_("soloed"))) != 0) {
1717 bool yn = string_is_affirmative (prop->value());
1719 /* force reset of solo status */
1721 _soloed = !yn;
1722 set_solo (yn, this);
1723 solo_gain = desired_solo_gain;
1726 if ((prop = node.property (X_("mute-affects-pre-fader"))) != 0) {
1727 _mute_affects_pre_fader = string_is_affirmative (prop->value());
1730 if ((prop = node.property (X_("mute-affects-post-fader"))) != 0) {
1731 _mute_affects_post_fader = string_is_affirmative (prop->value());
1734 if ((prop = node.property (X_("mute-affects-control-outs"))) != 0) {
1735 _mute_affects_control_outs = string_is_affirmative (prop->value());
1738 if ((prop = node.property (X_("mute-affects-main-outs"))) != 0) {
1739 _mute_affects_main_outs = string_is_affirmative (prop->value());
1742 if ((prop = node.property (X_("meter-point"))) != 0) {
1743 _meter_point = MeterPoint (string_2_enum (prop->value (), _meter_point));
1746 if ((prop = node.property (X_("edit-group"))) != 0) {
1747 RouteGroup* edit_group = _session.edit_group_by_name(prop->value());
1748 if(edit_group == 0) {
1749 error << string_compose(_("Route %1: unknown edit group \"%2 in saved state (ignored)"), _name, prop->value()) << endmsg;
1750 } else {
1751 set_edit_group(edit_group, this);
1755 if ((prop = node.property (X_("order-keys"))) != 0) {
1757 long n;
1759 string::size_type colon, equal;
1760 string remaining = prop->value();
1762 while (remaining.length()) {
1764 if ((equal = remaining.find_first_of ('=')) == string::npos || equal == remaining.length()) {
1765 error << string_compose (_("badly formed order key string in state file! [%1] ... ignored."), remaining)
1766 << endmsg;
1767 } else {
1768 if (sscanf (remaining.substr (equal+1).c_str(), "%ld", &n) != 1) {
1769 error << string_compose (_("badly formed order key string in state file! [%1] ... ignored."), remaining)
1770 << endmsg;
1771 } else {
1772 set_order_key (remaining.substr (0, equal).c_str(), n);
1776 colon = remaining.find_first_of (':');
1778 if (colon != string::npos) {
1779 remaining = remaining.substr (colon+1);
1780 } else {
1781 break;
1786 nlist = node.children();
1788 if (deferred_state) {
1789 delete deferred_state;
1792 deferred_state = new XMLNode(X_("deferred state"));
1794 /* set parent class properties before anything else */
1796 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
1798 child = *niter;
1800 if (child->name() == IO::state_node_name && call_base) {
1802 IO::set_state (*child);
1803 break;
1808 XMLNodeList redirect_nodes;
1810 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
1812 child = *niter;
1814 if (child->name() == X_("Send") || child->name() == X_("Insert")) {
1815 redirect_nodes.push_back(child);
1820 _set_redirect_states (redirect_nodes);
1822 for (niter = nlist.begin(); niter != nlist.end(); ++niter){
1823 child = *niter;
1824 // All redirects (sends and inserts) have been applied already
1826 if (child->name() == X_("Automation")) {
1828 if ((prop = child->property (X_("path"))) != 0) {
1829 load_automation (prop->value());
1832 } else if (child->name() == X_("ControlOuts")) {
1834 string coutname = _name;
1835 coutname += _("[control]");
1837 delete _control_outs;
1838 _control_outs = new IO (_session, coutname);
1840 /* fix up the control out name in the XML before setting it.
1841 Otherwise track templates don't work because the control
1842 outs end up with the stored template name, rather than
1843 the new name of the track based on the template.
1846 XMLProperty* prop = (*child->children().begin())->property ("name");
1847 if (prop) {
1848 prop->set_value (coutname);
1851 _control_outs->set_state (**(child->children().begin()));
1853 } else if (child->name() == X_("Comment")) {
1855 /* XXX this is a terrible API design in libxml++ */
1857 XMLNode *cmt = *(child->children().begin());
1858 _comment = cmt->content();
1860 } else if (child->name() == X_("extra")) {
1862 _extra_xml = new XMLNode (*child);
1864 } else if (child->name() == X_("controllable") && (prop = child->property("name")) != 0) {
1866 if (prop->value() == "solo") {
1867 _solo_control.set_state (*child);
1868 _session.add_controllable (&_solo_control);
1870 else if (prop->value() == "mute") {
1871 _mute_control.set_state (*child);
1872 _session.add_controllable (&_mute_control);
1875 else if (child->name() == X_("remote_control")) {
1876 if ((prop = child->property (X_("id"))) != 0) {
1877 int32_t x;
1878 sscanf (prop->value().c_str(), "%d", &x);
1879 set_remote_control_id (x);
1884 if ((prop = node.property (X_("mix-group"))) != 0) {
1885 RouteGroup* mix_group = _session.mix_group_by_name(prop->value());
1886 if (mix_group == 0) {
1887 error << string_compose(_("Route %1: unknown mix group \"%2 in saved state (ignored)"), _name, prop->value()) << endmsg;
1888 } else {
1889 set_mix_group(mix_group, this);
1893 return 0;
1896 void
1897 Route::_set_redirect_states(const XMLNodeList &nlist)
1899 XMLNodeConstIterator niter;
1900 char buf[64];
1902 RedirectList::iterator i, o;
1904 if (!ports_legal) {
1906 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1907 deferred_state->add_child_copy (**niter);
1910 return;
1913 // Iterate through existing redirects, remove those which are not in the state list
1914 for (i = _redirects.begin(); i != _redirects.end(); ) {
1915 RedirectList::iterator tmp = i;
1916 ++tmp;
1918 bool redirectInStateList = false;
1920 (*i)->id().print (buf, sizeof (buf));
1922 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1924 if (strncmp (buf,(*niter)->child(X_("Redirect"))->child(X_("IO"))->property(X_("id"))->value().c_str(), sizeof(buf)) == 0) {
1925 redirectInStateList = true;
1926 break;
1930 if (!redirectInStateList) {
1931 remove_redirect ( *i, this);
1935 i = tmp;
1939 // Iterate through state list and make sure all redirects are on the track and in the correct order,
1940 // set the state of existing redirects according to the new state on the same go
1941 i = _redirects.begin();
1942 for (niter = nlist.begin(); niter != nlist.end(); ++niter, ++i) {
1944 // Check whether the next redirect in the list
1945 o = i;
1947 while (o != _redirects.end()) {
1948 (*o)->id().print (buf, sizeof (buf));
1949 if ( strncmp(buf, (*niter)->child(X_("Redirect"))->child(X_("IO"))->property(X_("id"))->value().c_str(), sizeof(buf)) == 0)
1950 break;
1951 ++o;
1954 if (o == _redirects.end()) {
1955 // If the redirect (*niter) is not on the route, we need to create it
1956 // and move it to the correct location
1958 RedirectList::iterator prev_last = _redirects.end();
1959 --prev_last; // We need this to check whether adding succeeded
1961 add_redirect_from_xml (**niter);
1963 RedirectList::iterator last = _redirects.end();
1964 --last;
1966 if (prev_last == last) {
1967 warning << _name << ": could not fully restore state as some redirects were not possible to create" << endmsg;
1968 continue;
1972 boost::shared_ptr<Redirect> tmp = (*last);
1973 // remove the redirect from the wrong location
1974 _redirects.erase(last);
1975 // insert the new redirect at the current location
1976 _redirects.insert(i, tmp);
1978 --i; // move pointer to the newly inserted redirect
1979 continue;
1982 // We found the redirect (*niter) on the route, first we must make sure the redirect
1983 // is at the location provided in the XML state
1984 if (i != o) {
1985 boost::shared_ptr<Redirect> tmp = (*o);
1986 // remove the old copy
1987 _redirects.erase(o);
1988 // insert the redirect at the correct location
1989 _redirects.insert(i, tmp);
1991 --i; // move pointer so it points to the right redirect
1994 (*i)->set_state( (**niter) );
1997 redirects_changed(this);
2000 void
2001 Route::curve_reallocate ()
2003 // _gain_automation_curve.finish_resize ();
2004 // _pan_automation_curve.finish_resize ();
2007 void
2008 Route::silence (nframes_t nframes)
2010 if (!_silent) {
2012 // reset_peak_meters ();
2014 IO::silence (nframes);
2016 if (_control_outs) {
2017 _control_outs->silence (nframes);
2021 Glib::RWLock::ReaderLock lm (redirect_lock, Glib::TRY_LOCK);
2023 if (lm.locked()) {
2024 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2025 boost::shared_ptr<PluginInsert> pi;
2026 if (!_active && (pi = boost::dynamic_pointer_cast<PluginInsert> (*i)) != 0) {
2027 // skip plugins, they don't need anything when we're not active
2028 continue;
2031 (*i)->silence (nframes);
2034 if (nframes == _session.get_block_size()) {
2035 // _silent = true;
2044 Route::set_control_outs (const vector<string>& ports)
2046 Glib::Mutex::Lock lm (control_outs_lock);
2047 vector<string>::const_iterator i;
2048 uint32_t limit;
2050 if (_control_outs) {
2051 delete _control_outs;
2052 _control_outs = 0;
2055 if (control() || master()) {
2056 /* no control outs for these two special busses */
2057 return 0;
2060 if (ports.empty()) {
2061 return 0;
2064 string coutname = _name;
2065 coutname += _("[control]");
2067 _control_outs = new IO (_session, coutname);
2069 /* our control outs need as many outputs as we
2070 have outputs. we track the changes in ::output_change_handler().
2073 limit = n_outputs ();
2075 if (_control_outs->ensure_io (0, limit, true, this)) {
2076 return -1;
2079 /* now connect to the named ports */
2081 for (uint32_t n = 0; n < limit; ++n) {
2082 if (_control_outs->connect_output (_control_outs->output (n), ports[n % ports.size()], this)) {
2083 error << string_compose (_("could not connect %1 to %2"), _control_outs->output(n)->name(), ports[n]) << endmsg;
2084 return -1;
2088 return 0;
2091 void
2092 Route::set_edit_group (RouteGroup *eg, void *src)
2095 if (eg == _edit_group) {
2096 return;
2099 if (_edit_group) {
2100 _edit_group->remove (this);
2103 if ((_edit_group = eg) != 0) {
2104 _edit_group->add (this);
2107 _session.set_dirty ();
2108 edit_group_changed (src); /* EMIT SIGNAL */
2111 void
2112 Route::drop_edit_group (void *src)
2114 _edit_group = 0;
2115 _session.set_dirty ();
2116 edit_group_changed (src); /* EMIT SIGNAL */
2119 void
2120 Route::set_mix_group (RouteGroup *mg, void *src)
2123 if (mg == _mix_group) {
2124 return;
2127 if (_mix_group) {
2128 _mix_group->remove (this);
2131 if ((_mix_group = mg) != 0) {
2132 _mix_group->add (this);
2135 _session.set_dirty ();
2136 mix_group_changed (src); /* EMIT SIGNAL */
2139 void
2140 Route::drop_mix_group (void *src)
2142 _mix_group = 0;
2143 _session.set_dirty ();
2144 mix_group_changed (src); /* EMIT SIGNAL */
2147 void
2148 Route::set_comment (string cmt, void *src)
2150 _comment = cmt;
2151 comment_changed (src);
2152 _session.set_dirty ();
2155 bool
2156 Route::feeds (boost::shared_ptr<Route> other)
2158 uint32_t i, j;
2160 IO& self = *this;
2161 uint32_t no = self.n_outputs();
2162 uint32_t ni = other->n_inputs ();
2164 for (i = 0; i < no; ++i) {
2165 for (j = 0; j < ni; ++j) {
2166 if (self.output(i)->connected_to (other->input(j)->name())) {
2167 return true;
2172 /* check Redirects which may also interconnect Routes */
2174 for (RedirectList::iterator r = _redirects.begin(); r != _redirects.end(); r++) {
2176 no = (*r)->n_outputs();
2178 for (i = 0; i < no; ++i) {
2179 for (j = 0; j < ni; ++j) {
2180 if ((*r)->output(i)->connected_to (other->input (j)->name())) {
2181 return true;
2187 /* check for control room outputs which may also interconnect Routes */
2189 if (_control_outs) {
2191 no = _control_outs->n_outputs();
2193 for (i = 0; i < no; ++i) {
2194 for (j = 0; j < ni; ++j) {
2195 if (_control_outs->output(i)->connected_to (other->input (j)->name())) {
2196 return true;
2202 return false;
2205 void
2206 Route::set_mute_config (mute_type t, bool onoff, void *src)
2208 switch (t) {
2209 case PRE_FADER:
2210 _mute_affects_pre_fader = onoff;
2211 pre_fader_changed(src); /* EMIT SIGNAL */
2212 break;
2214 case POST_FADER:
2215 _mute_affects_post_fader = onoff;
2216 post_fader_changed(src); /* EMIT SIGNAL */
2217 break;
2219 case CONTROL_OUTS:
2220 _mute_affects_control_outs = onoff;
2221 control_outs_changed(src); /* EMIT SIGNAL */
2222 break;
2224 case MAIN_OUTS:
2225 _mute_affects_main_outs = onoff;
2226 main_outs_changed(src); /* EMIT SIGNAL */
2227 break;
2231 bool
2232 Route::get_mute_config (mute_type t)
2234 bool onoff = false;
2236 switch (t){
2237 case PRE_FADER:
2238 onoff = _mute_affects_pre_fader;
2239 break;
2240 case POST_FADER:
2241 onoff = _mute_affects_post_fader;
2242 break;
2243 case CONTROL_OUTS:
2244 onoff = _mute_affects_control_outs;
2245 break;
2246 case MAIN_OUTS:
2247 onoff = _mute_affects_main_outs;
2248 break;
2251 return onoff;
2254 void
2255 Route::handle_transport_stopped (bool abort_ignored, bool did_locate, bool can_flush_redirects)
2257 nframes_t now = _session.transport_frame();
2260 Glib::RWLock::ReaderLock lm (redirect_lock);
2262 if (!did_locate) {
2263 automation_snapshot (now, true);
2266 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2268 if (Config->get_plugins_stop_with_transport() && can_flush_redirects) {
2269 (*i)->deactivate ();
2270 (*i)->activate ();
2273 (*i)->transport_stopped (now);
2277 IO::transport_stopped (now);
2279 _roll_delay = _initial_delay;
2282 void
2283 Route::input_change_handler (IOChange change, void *ignored)
2285 if (change & ConfigurationChanged) {
2286 reset_plugin_counts (0);
2290 void
2291 Route::output_change_handler (IOChange change, void *ignored)
2293 if (change & ConfigurationChanged) {
2294 if (_control_outs) {
2295 _control_outs->ensure_io (0, n_outputs(), true, this);
2298 reset_plugin_counts (0);
2302 uint32_t
2303 Route::pans_required () const
2305 if (n_outputs() < 2) {
2306 return 0;
2309 return max (n_inputs (), redirect_max_outs);
2312 int
2313 Route::no_roll (nframes_t nframes, nframes_t start_frame, nframes_t end_frame,
2314 bool session_state_changing, bool can_record, bool rec_monitors_input)
2316 if (n_outputs() == 0) {
2317 return 0;
2320 if (session_state_changing || !_active) {
2321 silence (nframes);
2322 return 0;
2325 apply_gain_automation = false;
2327 if (n_inputs()) {
2328 passthru (start_frame, end_frame, nframes, 0, false);
2329 } else {
2330 silence (nframes);
2333 return 0;
2336 nframes_t
2337 Route::check_initial_delay (nframes_t nframes, nframes_t& transport_frame)
2339 if (_roll_delay > nframes) {
2341 _roll_delay -= nframes;
2342 silence (nframes);
2343 /* transport frame is not legal for caller to use */
2344 return 0;
2346 } else if (_roll_delay > 0) {
2348 nframes -= _roll_delay;
2350 silence (_roll_delay);
2352 /* we've written _roll_delay of samples into the
2353 output ports, so make a note of that for
2354 future reference.
2357 increment_output_offset (_roll_delay);
2359 transport_frame += _roll_delay;
2361 _roll_delay = 0;
2364 return nframes;
2368 Route::roll (nframes_t nframes, nframes_t start_frame, nframes_t end_frame, int declick,
2369 bool can_record, bool rec_monitors_input)
2372 Glib::RWLock::ReaderLock lm (redirect_lock, Glib::TRY_LOCK);
2373 if (lm.locked()) {
2374 // automation snapshot can also be called from the non-rt context
2375 // and it uses the redirect list, so we take the lock out here
2376 automation_snapshot (_session.transport_frame(), false);
2380 if ((n_outputs() == 0 && _redirects.empty()) || n_inputs() == 0 || !_active) {
2381 silence (nframes);
2382 return 0;
2385 nframes_t unused = 0;
2387 if ((nframes = check_initial_delay (nframes, unused)) == 0) {
2388 return 0;
2391 _silent = false;
2393 apply_gain_automation = false;
2396 Glib::Mutex::Lock am (automation_lock, Glib::TRY_LOCK);
2398 if (am.locked() && _session.transport_rolling()) {
2400 nframes_t start_frame = end_frame - nframes;
2402 if (gain_automation_playback()) {
2403 apply_gain_automation = _gain_automation_curve.rt_safe_get_vector (start_frame, end_frame, _session.gain_automation_buffer(), nframes);
2408 passthru (start_frame, end_frame, nframes, declick, false);
2410 return 0;
2414 Route::silent_roll (nframes_t nframes, nframes_t start_frame, nframes_t end_frame,
2415 bool can_record, bool rec_monitors_input)
2417 silence (nframes);
2418 return 0;
2421 void
2422 Route::toggle_monitor_input ()
2424 for (vector<Port*>::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
2425 (*i)->ensure_monitor_input(!(*i)->monitoring_input());
2429 bool
2430 Route::has_external_redirects () const
2432 boost::shared_ptr<const PortInsert> pi;
2434 for (RedirectList::const_iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2435 if ((pi = boost::dynamic_pointer_cast<const PortInsert>(*i)) != 0) {
2437 uint32_t no = pi->n_outputs();
2439 for (uint32_t n = 0; n < no; ++n) {
2441 string port_name = pi->output(n)->name();
2442 string client_name = port_name.substr (0, port_name.find(':'));
2444 /* only say "yes" if the redirect is actually in use */
2446 if (client_name != "ardour" && pi->active()) {
2447 return true;
2453 return false;
2456 void
2457 Route::flush_redirects ()
2459 /* XXX shouldn't really try to take this lock, since
2460 this is called from the RT audio thread.
2463 Glib::RWLock::ReaderLock lm (redirect_lock);
2465 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2466 (*i)->deactivate ();
2467 (*i)->activate ();
2471 void
2472 Route::set_meter_point (MeterPoint p, void *src)
2474 if (_meter_point != p) {
2475 _meter_point = p;
2476 meter_change (src); /* EMIT SIGNAL */
2477 _session.set_dirty ();
2481 nframes_t
2482 Route::update_total_latency ()
2484 _own_latency = 0;
2486 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2487 if ((*i)->active ()) {
2488 _own_latency += (*i)->latency ();
2492 #undef DEBUG_LATENCY
2493 #ifdef DEBUG_LATENCY
2494 cerr << _name << ": internal redirect latency = " << _own_latency << endl;
2495 #endif
2497 set_port_latency (_own_latency);
2499 /* this (virtual) function is used for pure Routes,
2500 not derived classes like AudioTrack. this means
2501 that the data processed here comes from an input
2502 port, not prerecorded material, and therefore we
2503 have to take into account any input latency.
2506 _own_latency += input_latency ();
2508 #ifdef DEBUG_LATENCY
2509 cerr << _name << ": input latency = " << input_latency() << " total = "
2510 << _own_latency << endl;
2511 #endif
2513 return _own_latency;
2516 void
2517 Route::set_latency_delay (nframes_t longest_session_latency)
2519 _initial_delay = longest_session_latency - _own_latency;
2521 if (_session.transport_stopped()) {
2522 _roll_delay = _initial_delay;
2526 void
2527 Route::automation_snapshot (nframes_t now, bool force)
2529 if (!force && !should_snapshot(now)) {
2530 return;
2533 IO::automation_snapshot (now, force);
2535 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2536 (*i)->automation_snapshot (now, force);
2540 Route::ToggleControllable::ToggleControllable (std::string name, Route& s, ToggleType tp)
2541 : Controllable (name), route (s), type(tp)
2546 void
2547 Route::ToggleControllable::set_value (float val)
2549 bool bval = ((val >= 0.5f) ? true: false);
2551 switch (type) {
2552 case MuteControl:
2553 route.set_mute (bval, this);
2554 break;
2555 case SoloControl:
2556 route.set_solo (bval, this);
2557 break;
2558 default:
2559 break;
2563 float
2564 Route::ToggleControllable::get_value (void) const
2566 float val = 0.0f;
2568 switch (type) {
2569 case MuteControl:
2570 val = route.muted() ? 1.0f : 0.0f;
2571 break;
2572 case SoloControl:
2573 val = route.soloed() ? 1.0f : 0.0f;
2574 break;
2575 default:
2576 break;
2579 return val;
2582 void
2583 Route::set_block_size (nframes_t nframes)
2585 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2586 (*i)->set_block_size (nframes);
2590 void
2591 Route::redirect_active_proxy (Redirect* ignored, void* ignored_src)
2593 _session.update_latency_compensation (false, false);
2596 void
2597 Route::protect_automation ()
2599 switch (gain_automation_state()) {
2600 case Write:
2601 set_gain_automation_state (Off);
2602 case Touch:
2603 set_gain_automation_state (Play);
2604 break;
2605 default:
2606 break;
2609 switch (panner().automation_state ()) {
2610 case Write:
2611 panner().set_automation_state (Off);
2612 break;
2613 case Touch:
2614 panner().set_automation_state (Play);
2615 break;
2616 default:
2617 break;
2620 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2621 boost::shared_ptr<PluginInsert> pi;
2622 if ((pi = boost::dynamic_pointer_cast<PluginInsert> (*i)) != 0) {
2623 pi->protect_automation ();
2628 void
2629 Route::set_pending_declick (int declick)
2631 if (_declickable) {
2632 /* this call is not allowed to turn off a pending declick unless "force" is true */
2633 if (declick) {
2634 _pending_declick = declick;
2636 // cerr << _name << ": after setting to " << declick << " pending declick = " << _pending_declick << endl;
2637 } else {
2638 _pending_declick = 0;
2643 /** Shift automation forwards from a particular place, thereby inserting time.
2644 * Adds undo commands for any shifts that are performed.
2646 * @param pos Position to start shifting from.
2647 * @param frames Amount to shift forwards by.
2650 void
2651 Route::shift (nframes64_t pos, nframes64_t frames)
2653 /* gain automation */
2654 XMLNode &before = _gain_automation_curve.get_state ();
2655 _gain_automation_curve.shift (pos, frames);
2656 XMLNode &after = _gain_automation_curve.get_state ();
2657 _session.add_command (new MementoCommand<AutomationList> (_gain_automation_curve, &before, &after));
2659 /* pan automation */
2660 for (std::vector<StreamPanner*>::iterator i = _panner->begin (); i != _panner->end (); ++i) {
2661 Curve & c = (*i)->automation ();
2662 XMLNode &before = c.get_state ();
2663 c.shift (pos, frames);
2664 XMLNode &after = c.get_state ();
2665 _session.add_command (new MementoCommand<AutomationList> (c, &before, &after));
2668 /* redirect automation */
2670 Glib::RWLock::ReaderLock lm (redirect_lock);
2671 for (RedirectList::iterator i = _redirects.begin (); i != _redirects.end (); ++i) {
2673 set<uint32_t> a;
2674 (*i)->what_has_automation (a);
2676 for (set<uint32_t>::const_iterator j = a.begin (); j != a.end (); ++j) {
2677 AutomationList & al = (*i)->automation_list (*j);
2678 XMLNode &before = al.get_state ();
2679 al.shift (pos, frames);
2680 XMLNode &after = al.get_state ();
2681 _session.add_command (new MementoCommand<AutomationList> (al, &before, &after));
2688 Route::save_as_template (const string& path, const string& name)
2690 XMLNode& node (state (false));
2691 XMLTree tree;
2693 IO::set_name_in_state (*node.children().front(), name);
2695 tree.set_root (&node);
2696 return tree.write (path.c_str());
2700 Route::set_name (string str, void* src)
2702 int ret;
2704 if ((ret = IO::set_name (str, src)) == 0) {
2705 if (_control_outs) {
2706 string coutname = _name;
2707 coutname += _("[control]");
2708 cerr << _name << " reset control outs to " << coutname << endl;
2709 return _control_outs->set_name (coutname, src);
2711 return 0;
2713 return ret;
2716 bool
2717 Route::has_io_redirect_named (const string& name)
2719 Glib::RWLock::ReaderLock lm (redirect_lock);
2720 RedirectList::iterator i;
2722 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
2723 if (boost::dynamic_pointer_cast<Send> (*i) ||
2724 boost::dynamic_pointer_cast<PortInsert> (*i)) {
2725 if ((*i)->name() == name) {
2726 return true;
2731 return false;