optionally tie together editor+mixer display orders; provide GUI control for timecode...
[ardour.git] / libs / ardour / route.cc
1 /*
2     Copyright (C) 2000 Paul Davis 
3
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.
8
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.
13
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.
17
18 */
19
20 #include <cmath>
21 #include <fstream>
22 #include <cassert>
23
24 #include <sigc++/bind.h>
25 #include <pbd/xml++.h>
26 #include <pbd/enumwriter.h>
27
28 #include <ardour/timestamps.h>
29 #include <ardour/buffer.h>
30 #include <ardour/audioengine.h>
31 #include <ardour/route.h>
32 #include <ardour/insert.h>
33 #include <ardour/send.h>
34 #include <ardour/session.h>
35 #include <ardour/utils.h>
36 #include <ardour/configuration.h>
37 #include <ardour/cycle_timer.h>
38 #include <ardour/route_group.h>
39 #include <ardour/port.h>
40 #include <ardour/ladspa_plugin.h>
41 #include <ardour/panner.h>
42 #include <ardour/dB.h>
43 #include <ardour/mix.h>
44
45 #include "i18n.h"
46
47 using namespace std;
48 using namespace ARDOUR;
49 using namespace PBD;
50
51 uint32_t Route::order_key_cnt = 0;
52 sigc::signal<void> Route::SyncOrderKeys;
53
54 Route::Route (Session& sess, string name, int input_min, int input_max, int output_min, int output_max, Flag flg, DataType default_type)
55         : IO (sess, name, input_min, input_max, output_min, output_max, default_type),
56           _flags (flg),
57           _solo_control (X_("solo"), *this, ToggleControllable::SoloControl),
58           _mute_control (X_("mute"), *this, ToggleControllable::MuteControl)
59 {
60         init ();
61 }
62
63 Route::Route (Session& sess, const XMLNode& node, DataType default_type)
64         : IO (sess, *node.child ("IO"), default_type),
65           _solo_control (X_("solo"), *this, ToggleControllable::SoloControl),
66           _mute_control (X_("mute"), *this, ToggleControllable::MuteControl)
67 {
68         init ();
69         _set_state (node, false);
70 }
71
72 void
73 Route::init ()
74 {
75         redirect_max_outs = 0;
76         _muted = false;
77         _soloed = false;
78         _solo_safe = false;
79         _phase_invert = false;
80         _denormal_protection = false;
81         order_keys[strdup (N_("signal"))] = order_key_cnt++;
82         _active = true;
83         _silent = false;
84         _meter_point = MeterPostFader;
85         _initial_delay = 0;
86         _roll_delay = 0;
87         _own_latency = 0;
88         _have_internal_generator = false;
89         _declickable = false;
90         _pending_declick = true;
91         _remote_control_id = 0;
92         _ignore_gain_on_deliver = true;
93         
94         _edit_group = 0;
95         _mix_group = 0;
96
97         _mute_affects_pre_fader = Config->get_mute_affects_pre_fader();
98         _mute_affects_post_fader = Config->get_mute_affects_post_fader();
99         _mute_affects_control_outs = Config->get_mute_affects_control_outs();
100         _mute_affects_main_outs = Config->get_mute_affects_main_outs();
101         
102         solo_gain = 1.0;
103         desired_solo_gain = 1.0;
104         mute_gain = 1.0;
105         desired_mute_gain = 1.0;
106
107         _control_outs = 0;
108
109         input_changed.connect (mem_fun (this, &Route::input_change_handler));
110         output_changed.connect (mem_fun (this, &Route::output_change_handler));
111 }
112
113 Route::~Route ()
114 {
115         clear_redirects (PreFader, this);
116         clear_redirects (PostFader, this);
117
118         for (OrderKeys::iterator i = order_keys.begin(); i != order_keys.end(); ++i) {
119                 free ((void*)(i->first));
120         }
121
122         if (_control_outs) {
123                 delete _control_outs;
124         }
125 }
126
127 void
128 Route::set_remote_control_id (uint32_t id)
129 {
130         if (id != _remote_control_id) {
131                 _remote_control_id = id;
132                 RemoteControlIDChanged ();
133         }
134 }
135
136 uint32_t
137 Route::remote_control_id() const
138 {
139         return _remote_control_id;
140 }
141
142 long
143 Route::order_key (const char* name) const
144 {
145         OrderKeys::const_iterator i;
146         
147         for (i = order_keys.begin(); i != order_keys.end(); ++i) {
148                 if (!strcmp (name, i->first)) {
149                         return i->second;
150                 }
151         }
152
153         return -1;
154 }
155
156 void
157 Route::set_order_key (const char* name, long n)
158 {
159         order_keys[strdup(name)] = n;
160
161         if (Config->get_sync_all_route_ordering()) {
162                 for (OrderKeys::iterator x = order_keys.begin(); x != order_keys.end(); ++x) {
163                         x->second = n;
164                 }
165         } 
166
167         _session.set_dirty ();
168 }
169
170 void
171 Route::sync_order_keys ()
172 {
173         uint32_t key;
174         
175         if (order_keys.empty()) {
176                 return;
177         }
178         
179         OrderKeys::iterator x = order_keys.begin();
180         key = x->second;
181         ++x;
182
183         for (; x != order_keys.end(); ++x) {
184                 x->second = key;
185         }
186 }
187
188 void
189 Route::inc_gain (gain_t fraction, void *src)
190 {
191         IO::inc_gain (fraction, src);
192 }
193
194 void
195 Route::set_gain (gain_t val, void *src)
196 {
197         if (src != 0 && _mix_group && src != _mix_group && _mix_group->is_active()) {
198                 
199                 if (_mix_group->is_relative()) {
200                         
201                         
202                         gain_t usable_gain  = gain();
203                         if (usable_gain < 0.000001f) {
204                                 usable_gain=0.000001f;
205                         }
206                                                 
207                         gain_t delta = val;
208                         if (delta < 0.000001f) {
209                                 delta=0.000001f;
210                         }
211
212                         delta -= usable_gain;
213
214                         if (delta == 0.0f) return;
215
216                         gain_t factor = delta / usable_gain;
217
218                         if (factor > 0.0f) {
219                                 factor = _mix_group->get_max_factor(factor);
220                                 if (factor == 0.0f) {
221                                         gain_changed (src);
222                                         return;
223                                 }
224                         } else {
225                                 factor = _mix_group->get_min_factor(factor);
226                                 if (factor == 0.0f) {
227                                         gain_changed (src);
228                                         return;
229                                 }
230                         }
231                                         
232                         _mix_group->apply (&Route::inc_gain, factor, _mix_group);
233
234                 } else {
235                         
236                         _mix_group->apply (&Route::set_gain, val, _mix_group);
237                 }
238
239                 return;
240         } 
241
242         if (val == gain()) {
243                 return;
244         }
245
246         IO::set_gain (val, src);
247 }
248
249 void
250 Route::process_output_buffers (vector<Sample*>& bufs, uint32_t nbufs,
251                                nframes_t start_frame, nframes_t end_frame, 
252                                nframes_t nframes, nframes_t offset, bool with_redirects, int declick,
253                                bool meter)
254 {
255         uint32_t n;
256         RedirectList::iterator i;
257         bool post_fader_work = false;
258         bool mute_declick_applied = false;
259         gain_t dmg, dsg, dg;
260         vector<Sample*>::iterator bufiter;
261         IO *co;
262         bool mute_audible;
263         bool solo_audible;
264         bool no_monitor;
265         gain_t* gab = _session.gain_automation_buffer();
266
267         switch (Config->get_monitoring_model()) {
268         case HardwareMonitoring:
269         case ExternalMonitoring:
270                 no_monitor = true;
271                 break;
272         default:
273                 no_monitor = false;
274         }
275
276         declick = _pending_declick;
277
278         {
279                 Glib::Mutex::Lock cm (control_outs_lock, Glib::TRY_LOCK);
280                 
281                 if (cm.locked()) {
282                         co = _control_outs;
283                 } else {
284                         co = 0;
285                 }
286         }
287         
288         { 
289                 Glib::Mutex::Lock dm (declick_lock, Glib::TRY_LOCK);
290                 
291                 if (dm.locked()) {
292                         dmg = desired_mute_gain;
293                         dsg = desired_solo_gain;
294                         dg = _desired_gain;
295                 } else {
296                         dmg = mute_gain;
297                         dsg = solo_gain;
298                         dg = _gain;
299                 }
300         }
301
302         /* ----------------------------------------------------------------------------------------------------
303            GLOBAL DECLICK (for transport changes etc.)
304            -------------------------------------------------------------------------------------------------- */
305
306         if (declick > 0) {
307                 apply_declick (bufs, nbufs, nframes, 0.0, 1.0, false);
308                 _pending_declick = 0;
309         } else if (declick < 0) {
310                 apply_declick (bufs, nbufs, nframes, 1.0, 0.0, false);
311                 _pending_declick = 0;
312         } else {
313
314                 /* no global declick */
315
316                 if (solo_gain != dsg) {
317                         apply_declick (bufs, nbufs, nframes, solo_gain, dsg, false);
318                         solo_gain = dsg;
319                 }
320         }
321
322
323         /* ----------------------------------------------------------------------------------------------------
324            INPUT METERING & MONITORING
325            -------------------------------------------------------------------------------------------------- */
326
327         if (meter && (_meter_point == MeterInput)) {
328                 for (n = 0; n < nbufs; ++n) {
329                         _peak_power[n] = Session::compute_peak (bufs[n], nframes, _peak_power[n]); 
330                 }
331         }
332
333         if (!_soloed && _mute_affects_pre_fader && (mute_gain != dmg)) {
334                 apply_declick (bufs, nbufs, nframes, mute_gain, dmg, false);
335                 mute_gain = dmg;
336                 mute_declick_applied = true;
337         }
338
339         if ((_meter_point == MeterInput) && co) {
340                 
341                 solo_audible = dsg > 0;
342                 mute_audible = dmg > 0;// || !_mute_affects_pre_fader;
343                 
344                 if (    // muted by solo of another track
345                         
346                         !solo_audible || 
347                         
348                         // muted by mute of this track 
349                         
350                         !mute_audible ||
351                         
352                         // rec-enabled but not s/w monitoring 
353                         
354                         // TODO: this is probably wrong
355
356                         (no_monitor && record_enabled() && (!Config->get_auto_input() || _session.actively_recording()))
357
358                         ) {
359                         
360                         co->silence (nframes, offset);
361                         
362                 } else {
363
364                         co->deliver_output (bufs, nbufs, nframes, offset);
365                         
366                 } 
367         } 
368
369         /* -----------------------------------------------------------------------------------------------------
370            DENORMAL CONTROL
371            -------------------------------------------------------------------------------------------------- */
372
373         if (_denormal_protection || Config->get_denormal_protection()) {
374
375                 for (n = 0; n < nbufs; ++n)  {
376                         Sample *sp = bufs[n];
377                         
378                         for (nframes_t nx = offset; nx < nframes + offset; ++nx) {
379                                 sp[nx] += 1.0e-27f;
380                         }
381                 }
382         }
383
384
385         /* ----------------------------------------------------------------------------------------------------
386            PRE-FADER REDIRECTS
387            -------------------------------------------------------------------------------------------------- */
388
389         if (with_redirects) {
390                 Glib::RWLock::ReaderLock rm (redirect_lock, Glib::TRY_LOCK);
391                 if (rm.locked()) {
392                         if (mute_gain > 0 || !_mute_affects_pre_fader) {
393                                 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
394                                         switch ((*i)->placement()) {
395                                         case PreFader:
396                                                 if (dsg == 0) {
397                                                         if (boost::dynamic_pointer_cast<Send>(*i) || boost::dynamic_pointer_cast<PortInsert>(*i)) {
398                                                                 (*i)->silence (nframes, offset);
399                                                         }
400                                                 } else {
401                                                         (*i)->run (bufs, nbufs, nframes, offset);
402                                                 }
403                                                 break;
404                                         case PostFader:
405                                                 post_fader_work = true;
406                                                 break;
407                                         }
408                                 }
409                         } else {
410                                 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
411                                         switch ((*i)->placement()) {
412                                         case PreFader:
413                                                 (*i)->silence (nframes, offset);
414                                                 break;
415                                         case PostFader:
416                                                 post_fader_work = true;
417                                                 break;
418                                         }
419                                 }
420                         }
421                 } 
422         }
423
424
425         if (!_soloed && (mute_gain != dmg) && !mute_declick_applied && _mute_affects_post_fader) {
426                 apply_declick (bufs, nbufs, nframes, mute_gain, dmg, false);
427                 mute_gain = dmg;
428                 mute_declick_applied = true;
429         }
430
431         /* ----------------------------------------------------------------------------------------------------
432            PRE-FADER METERING & MONITORING
433            -------------------------------------------------------------------------------------------------- */
434
435         if (meter && (_meter_point == MeterPreFader)) {
436                 for (n = 0; n < nbufs; ++n) {
437                         _peak_power[n] = Session::compute_peak (bufs[n], nframes, _peak_power[n]);
438                 }
439         }
440
441         
442         if ((_meter_point == MeterPreFader) && co) {
443                 
444                 solo_audible = dsg > 0;
445                 mute_audible = dmg > 0 || !_mute_affects_pre_fader;
446                 
447                 if ( // muted by solo of another track
448                         
449                         !solo_audible || 
450                         
451                         // muted by mute of this track 
452                         
453                         !mute_audible ||
454                         
455                         // rec-enabled but not s/w monitoring 
456                         
457                         (no_monitor && record_enabled() && (!Config->get_auto_input() || _session.actively_recording()))
458
459                         ) {
460                         
461                         co->silence (nframes, offset);
462                         
463                 } else {
464
465                         co->deliver_output_no_pan (bufs, nbufs, nframes, offset);
466                         
467                 } 
468         } 
469         
470         /* ----------------------------------------------------------------------------------------------------
471            GAIN STAGE
472            -------------------------------------------------------------------------------------------------- */
473
474         /* if not recording or recording and requiring any monitor signal, then apply gain */
475
476         if ( // not recording 
477
478                 !(record_enabled() && _session.actively_recording()) || 
479                 
480             // OR recording 
481                 
482                 // h/w monitoring not in use 
483                 
484                 (!Config->get_monitoring_model() == HardwareMonitoring && 
485
486                  // AND software monitoring required
487
488                  Config->get_monitoring_model() == SoftwareMonitoring)) { 
489                 
490                 if (apply_gain_automation) {
491                         
492                         if (_phase_invert) {
493                                 for (n = 0; n < nbufs; ++n)  {
494                                         Sample *sp = bufs[n];
495                                         
496                                         for (nframes_t nx = 0; nx < nframes; ++nx) {
497                                                 sp[nx] *= -gab[nx];
498                                         }
499                                 }
500                         } else {
501                                 for (n = 0; n < nbufs; ++n) {
502                                         Sample *sp = bufs[n];
503                                         
504                                         for (nframes_t nx = 0; nx < nframes; ++nx) {
505                                                 sp[nx] *= gab[nx];
506                                         }
507                                 }
508                         }
509                         
510                         if (apply_gain_automation && _session.transport_rolling() && nframes > 0) {
511                                 _effective_gain = gab[nframes-1];
512                         }
513                         
514                 } else {
515                         
516                         /* manual (scalar) gain */
517                         
518                         if (_gain != dg) {
519                                 
520                                 apply_declick (bufs, nbufs, nframes, _gain, dg, _phase_invert);
521                                 _gain = dg;
522                                 
523                         } else if (_gain != 0 && (_phase_invert || _gain != 1.0)) {
524                                 
525                                 /* no need to interpolate current gain value,
526                                    but its non-unity, so apply it. if the gain
527                                    is zero, do nothing because we'll ship silence
528                                    below.
529                                 */
530
531                                 gain_t this_gain;
532                                 
533                                 if (_phase_invert) {
534                                         this_gain = -_gain;
535                                 } else {
536                                         this_gain = _gain;
537                                 }
538                                 
539                                 for (n = 0; n < nbufs; ++n) {
540                                         Sample *sp = bufs[n];
541                                         Session::apply_gain_to_buffer(sp,nframes,this_gain);
542                                 }
543
544                         } else if (_gain == 0) {
545                                 for (n = 0; n < nbufs; ++n) {
546                                         memset (bufs[n], 0, sizeof (Sample) * nframes);
547                                 }
548                         }
549                 }
550
551         } else {
552
553                 /* actively recording, no monitoring required; leave buffers as-is to save CPU cycles */
554
555         }
556
557         /* ----------------------------------------------------------------------------------------------------
558            POST-FADER REDIRECTS
559            -------------------------------------------------------------------------------------------------- */
560
561         /* note that post_fader_work cannot be true unless with_redirects was also true, so don't test both */
562
563         if (post_fader_work) {
564
565                 Glib::RWLock::ReaderLock rm (redirect_lock, Glib::TRY_LOCK);
566                 if (rm.locked()) {
567                         if (mute_gain > 0 || !_mute_affects_post_fader) {
568                                 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
569                                         switch ((*i)->placement()) {
570                                         case PreFader:
571                                                 break;
572                                         case PostFader:
573                                                 if (dsg == 0) {
574                                                         if (boost::dynamic_pointer_cast<Send>(*i) || boost::dynamic_pointer_cast<PortInsert>(*i)) {
575                                                                 (*i)->silence (nframes, offset);
576                                                         }
577                                                 } else {
578                                                         (*i)->run (bufs, nbufs, nframes, offset);
579                                                 }
580                                                 break;
581                                         }
582                                 }
583                         } else {
584                                 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
585                                         switch ((*i)->placement()) {
586                                         case PreFader:
587                                                 break;
588                                         case PostFader:
589                                                 (*i)->silence (nframes, offset);
590                                                 break;
591                                         }
592                                 }
593                         }
594                 } 
595         }
596
597         if (!_soloed && (mute_gain != dmg) && !mute_declick_applied && _mute_affects_control_outs) {
598                 apply_declick (bufs, nbufs, nframes, mute_gain, dmg, false);
599                 mute_gain = dmg;
600                 mute_declick_applied = true;
601         }
602
603         /* ----------------------------------------------------------------------------------------------------
604            CONTROL OUTPUT STAGE
605            -------------------------------------------------------------------------------------------------- */
606
607         if ((_meter_point == MeterPostFader) && co) {
608                 
609                 solo_audible = solo_gain > 0;
610                 mute_audible = dmg > 0 || !_mute_affects_control_outs;
611
612                 if ( // silent anyway
613
614                         (_gain == 0 && !apply_gain_automation) || 
615                     
616                      // muted by solo of another track
617
618                         !solo_audible || 
619                     
620                      // muted by mute of this track 
621
622                         !mute_audible ||
623
624                     // recording but not s/w monitoring 
625                         
626                         (no_monitor && record_enabled() && (!Config->get_auto_input() || _session.actively_recording()))
627
628                         ) {
629                         
630                         co->silence (nframes, offset);
631                         
632                 } else {
633
634                         co->deliver_output_no_pan (bufs, nbufs, nframes, offset);
635                 } 
636         } 
637
638         /* ----------------------------------------------------------------------
639            GLOBAL MUTE 
640            ----------------------------------------------------------------------*/
641
642         if (!_soloed && (mute_gain != dmg) && !mute_declick_applied && _mute_affects_main_outs) {
643                 apply_declick (bufs, nbufs, nframes, mute_gain, dmg, false);
644                 mute_gain = dmg;
645                 mute_declick_applied = true;
646         }
647         
648         /* ----------------------------------------------------------------------------------------------------
649            MAIN OUTPUT STAGE
650            -------------------------------------------------------------------------------------------------- */
651
652         solo_audible = dsg > 0;
653         mute_audible = dmg > 0 || !_mute_affects_main_outs;
654         
655         if (n_outputs() == 0) {
656             
657             /* relax */
658
659         } else if (no_monitor && record_enabled() && (!Config->get_auto_input() || _session.actively_recording())) {
660                 
661                 IO::silence (nframes, offset);
662                 
663         } else {
664
665                 if ( // silent anyway
666
667                     (_gain == 0 && !apply_gain_automation) ||
668                     
669                     // muted by solo of another track, but not using control outs for solo
670
671                     (!solo_audible && (Config->get_solo_model() != SoloBus)) ||
672                     
673                     // muted by mute of this track
674
675                     !mute_audible
676
677                         ) {
678
679                         /* don't use Route::silence() here, because that causes
680                            all outputs (sends, port inserts, etc. to be silent).
681                         */
682                         
683                         if (_meter_point == MeterPostFader) {
684                                 reset_peak_meters ();
685                         }
686
687                         IO::silence (nframes, offset);
688                         
689                 } else {
690                         
691                         if ((_session.transport_speed() > 1.5f || 
692                              _session.transport_speed() < -1.5f) &&
693                             Config->get_quieten_at_speed()) {
694                                 pan (bufs, nbufs, nframes, offset, speed_quietning); 
695                         } else {
696                                 // cerr << _name << " panner state = " << _panner->automation_state() << endl;
697                                 if (!_panner->empty() &&
698                                     (_panner->automation_state() & Play ||
699                                      ((_panner->automation_state() & Touch) && !_panner->touching()))) {
700                                         pan_automated (bufs, nbufs, start_frame, end_frame, nframes, offset);
701                                 } else {
702                                         pan (bufs, nbufs, nframes, offset, 1.0); 
703                                 }
704                         }
705                 }
706
707         }
708
709         /* ----------------------------------------------------------------------------------------------------
710            POST-FADER METERING
711            -------------------------------------------------------------------------------------------------- */
712
713         if (meter && (_meter_point == MeterPostFader)) {
714 //              cerr << "meter post" << endl;
715
716                 if ((_gain == 0 && !apply_gain_automation) || dmg == 0) {
717                         uint32_t no = n_outputs();
718                         for (n = 0; n < no; ++n) {
719                                 _peak_power[n] = 0;
720                         } 
721                 } else {
722                         uint32_t no = n_outputs();
723                         for (n = 0; n < no; ++n) {
724                                 _peak_power[n] = Session::compute_peak (output(n)->get_buffer (nframes) + offset, nframes, _peak_power[n]);
725                         }
726                 }
727         }
728 }
729
730 uint32_t
731 Route::n_process_buffers ()
732 {
733         return max (n_inputs(), redirect_max_outs);
734 }
735
736 void
737
738 Route::passthru (nframes_t start_frame, nframes_t end_frame, nframes_t nframes, nframes_t offset, int declick, bool meter_first)
739 {
740         vector<Sample*>& bufs = _session.get_passthru_buffers();
741         uint32_t limit = n_process_buffers ();
742
743         _silent = false;
744
745         collect_input (bufs, limit, nframes, offset);
746
747 #define meter_stream meter_first
748
749         if (meter_first) {
750                 for (uint32_t n = 0; n < limit; ++n) {
751                         _peak_power[n] = Session::compute_peak (bufs[n], nframes, _peak_power[n]);
752                 }
753                 meter_stream = false;
754         } else {
755                 meter_stream = true;
756         }
757                 
758         process_output_buffers (bufs, limit, start_frame, end_frame, nframes, offset, true, declick, meter_stream);
759
760 #undef meter_stream
761 }
762
763 void
764 Route::set_phase_invert (bool yn, void *src)
765 {
766         if (_phase_invert != yn) {
767                 _phase_invert = yn;
768                 //  phase_invert_changed (src); /* EMIT SIGNAL */
769         }
770 }
771
772 void
773 Route::set_denormal_protection (bool yn, void *src)
774 {
775         if (_denormal_protection != yn) {
776                 _denormal_protection = yn;
777                 //  denormal_protection_changed (src); /* EMIT SIGNAL */
778         }
779 }
780
781 void
782 Route::set_solo (bool yn, void *src)
783 {
784         if (_solo_safe) {
785                 return;
786         }
787
788         if (_mix_group && src != _mix_group && _mix_group->is_active()) {
789                 _mix_group->apply (&Route::set_solo, yn, _mix_group);
790                 return;
791         }
792
793         if (_soloed != yn) {
794                 _soloed = yn;
795                 solo_changed (src); /* EMIT SIGNAL */
796                 _solo_control.Changed (); /* EMIT SIGNAL */
797         }
798 }
799
800 void
801 Route::set_solo_mute (bool yn)
802 {
803         Glib::Mutex::Lock lm (declick_lock);
804
805         /* Called by Session in response to another Route being soloed.
806          */
807            
808         desired_solo_gain = (yn?0.0:1.0);
809 }
810
811 void
812 Route::set_solo_safe (bool yn, void *src)
813 {
814         if (_solo_safe != yn) {
815                 _solo_safe = yn;
816                  solo_safe_changed (src); /* EMIT SIGNAL */
817         }
818 }
819
820 void
821 Route::set_mute (bool yn, void *src)
822
823 {
824         if (_mix_group && src != _mix_group && _mix_group->is_active()) {
825                 _mix_group->apply (&Route::set_mute, yn, _mix_group);
826                 return;
827         }
828
829         if (_muted != yn) {
830                 _muted = yn;
831                 mute_changed (src); /* EMIT SIGNAL */
832                 
833                 _mute_control.Changed (); /* EMIT SIGNAL */
834                 
835                 Glib::Mutex::Lock lm (declick_lock);
836                 desired_mute_gain = (yn?0.0f:1.0f);
837         }
838 }
839
840 int
841 Route::add_redirect (boost::shared_ptr<Redirect> redirect, void *src, uint32_t* err_streams)
842 {
843         uint32_t old_rmo = redirect_max_outs;
844
845         if (!_session.engine().connected()) {
846                 return 1;
847         }
848
849         {
850                 Glib::RWLock::WriterLock lm (redirect_lock);
851
852                 boost::shared_ptr<PluginInsert> pi;
853                 boost::shared_ptr<PortInsert> porti;
854
855                 uint32_t potential_max_streams = 0;
856
857                 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(redirect)) != 0) {
858                         pi->set_count (1);
859
860                         if (pi->input_streams() == 0) {
861                                 /* instrument plugin */
862                                 _have_internal_generator = true;
863                         }
864
865                         potential_max_streams = max(pi->input_streams(), pi->output_streams());
866                         
867                 } else if ((porti = boost::dynamic_pointer_cast<PortInsert>(redirect)) != 0) {
868
869                         /* force new port inserts to start out with an i/o configuration
870                            that matches this route's i/o configuration.
871
872                            the "inputs" for the port are supposed to match the output
873                            of this route.
874
875                            the "outputs" of the route should match the inputs of this
876                            route. XXX shouldn't they match the number of active signal
877                            streams at the point of insertion?
878                            
879                         */
880
881                         porti->ensure_io (n_outputs (), n_inputs(), false, this);
882                 }
883
884                 // Ensure peak vector sizes before the plugin is activated
885                 while (_peak_power.size() < potential_max_streams) {
886                         _peak_power.push_back(0);
887                 }
888                 while (_visible_peak_power.size() < potential_max_streams) {
889                         _visible_peak_power.push_back(-INFINITY);
890                 }
891                 while (_max_peak_power.size() < potential_max_streams) {
892                         _max_peak_power.push_back(-INFINITY);
893                 }
894
895                 _redirects.push_back (redirect);
896
897                 if (_reset_plugin_counts (err_streams)) {
898                         _redirects.pop_back ();
899                         _reset_plugin_counts (0); // it worked before we tried to add it ...
900                         return -1;
901                 }
902
903                 redirect->activate ();
904                 redirect->active_changed.connect (mem_fun (*this, &Route::redirect_active_proxy));
905         }
906         
907         if (redirect_max_outs != old_rmo || old_rmo == 0) {
908                 reset_panner ();
909         }
910
911
912         redirects_changed (src); /* EMIT SIGNAL */
913         return 0;
914 }
915
916 int
917 Route::add_redirects (const RedirectList& others, void *src, uint32_t* err_streams)
918 {
919         uint32_t old_rmo = redirect_max_outs;
920
921         assert (ports_legal);
922
923         if (!_session.engine().connected()) {
924                 return 1;
925         }
926
927         {
928                 Glib::RWLock::WriterLock lm (redirect_lock);
929
930                 RedirectList::iterator existing_end = _redirects.end();
931                 --existing_end;
932
933                 uint32_t potential_max_streams = 0;
934
935                 for (RedirectList::const_iterator i = others.begin(); i != others.end(); ++i) {
936                         
937                         boost::shared_ptr<PluginInsert> pi;
938                         
939                         if ((pi = boost::dynamic_pointer_cast<PluginInsert>(*i)) != 0) {
940                                 pi->set_count (1);
941                                 
942                                 uint32_t m = max(pi->input_streams(), pi->output_streams());
943                                 if (m > potential_max_streams)
944                                         potential_max_streams = m;
945                         }
946
947                         // Ensure peak vector sizes before the plugin is activated
948                         while (_peak_power.size() < potential_max_streams) {
949                                 _peak_power.push_back(0);
950                         }
951                         while (_visible_peak_power.size() < potential_max_streams) {
952                                 _visible_peak_power.push_back(-INFINITY);
953                         }
954                         while (_max_peak_power.size() < potential_max_streams) {
955                                 _max_peak_power.push_back(-INFINITY);
956                         }
957
958                         _redirects.push_back (*i);
959                         
960                         if (_reset_plugin_counts (err_streams)) {
961                                 ++existing_end;
962                                 _redirects.erase (existing_end, _redirects.end());
963                                 _reset_plugin_counts (0); // it worked before we tried to add it ...
964                                 return -1;
965                         }
966                         
967                         (*i)->activate ();
968                         (*i)->active_changed.connect (mem_fun (*this, &Route::redirect_active_proxy));
969                 }
970         }
971         
972         if (redirect_max_outs != old_rmo || old_rmo == 0) {
973                 reset_panner ();
974         }
975
976         redirects_changed (src); /* EMIT SIGNAL */
977         return 0;
978 }
979
980 /** Remove redirects with a given placement.
981  * @param p Placement of redirects to remove.
982  */
983 void
984 Route::clear_redirects (Placement p, void *src)
985 {
986         const uint32_t old_rmo = redirect_max_outs;
987
988         if (!_session.engine().connected()) {
989                 return;
990         }
991
992         {
993                 Glib::RWLock::WriterLock lm (redirect_lock);
994                 RedirectList new_list;
995                 
996                 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
997                         if ((*i)->placement() == p) {
998                                 /* it's the placement we want to get rid of */
999                                 (*i)->drop_references ();
1000                         } else {
1001                                 /* it's a different placement, so keep it */
1002                                 new_list.push_back (*i);
1003                         }
1004                 }
1005                 
1006                 _redirects = new_list;
1007         }
1008
1009         /* FIXME: can't see how this test can ever fire */
1010         if (redirect_max_outs != old_rmo) {
1011                 reset_panner ();
1012         }
1013         
1014         redirect_max_outs = 0;
1015         _have_internal_generator = false;
1016         redirects_changed (src); /* EMIT SIGNAL */
1017 }
1018
1019 int
1020 Route::remove_redirect (boost::shared_ptr<Redirect> redirect, void *src, uint32_t* err_streams)
1021 {
1022         uint32_t old_rmo = redirect_max_outs;
1023
1024         assert (ports_legal);
1025
1026         if (!_session.engine().connected()) {
1027                 return 1;
1028         }
1029
1030         redirect_max_outs = 0;
1031
1032         {
1033                 Glib::RWLock::WriterLock lm (redirect_lock);
1034                 RedirectList::iterator i;
1035                 bool removed = false;
1036
1037                 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
1038                         if (*i == redirect) {
1039
1040                                 RedirectList::iterator tmp;
1041
1042                                 /* move along, see failure case for reset_plugin_counts()
1043                                    where we may need to reinsert the redirect.
1044                                 */
1045
1046                                 tmp = i;
1047                                 ++tmp;
1048
1049                                 /* stop redirects that send signals to JACK ports
1050                                    from causing noise as a result of no longer being
1051                                    run.
1052                                 */
1053
1054                                 boost::shared_ptr<Send> send;
1055                                 boost::shared_ptr<PortInsert> port_insert;
1056                                 
1057                                 if ((send = boost::dynamic_pointer_cast<Send> (*i)) != 0) {
1058                                         send->disconnect_inputs (this);
1059                                         send->disconnect_outputs (this);
1060                                 } else if ((port_insert = boost::dynamic_pointer_cast<PortInsert> (*i)) != 0) {
1061                                         port_insert->disconnect_inputs (this);
1062                                         port_insert->disconnect_outputs (this);
1063                                 }
1064
1065                                 _redirects.erase (i);
1066
1067                                 i = tmp;
1068                                 removed = true;
1069                                 break;
1070                         }
1071                 }
1072
1073                 if (!removed) {
1074                         /* what? */
1075                         return 1;
1076                 }
1077
1078                 if (_reset_plugin_counts (err_streams)) {
1079                         /* get back to where we where */
1080                         _redirects.insert (i, redirect);
1081                         /* we know this will work, because it worked before :) */
1082                         _reset_plugin_counts (0);
1083                         return -1;
1084                 }
1085
1086                 bool foo = false;
1087
1088                 for (i = _redirects.begin(); i != _redirects.end(); ++i) {
1089                         boost::shared_ptr<PluginInsert> pi;
1090                         
1091                         if ((pi = boost::dynamic_pointer_cast<PluginInsert>(*i)) != 0) {
1092                                 if (pi->is_generator()) {
1093                                         foo = true;
1094                                 }
1095                         }
1096                 }
1097
1098                 _have_internal_generator = foo;
1099         }
1100
1101         if (old_rmo != redirect_max_outs) {
1102                 reset_panner ();
1103         }
1104
1105         redirect->drop_references ();
1106
1107         redirects_changed (src); /* EMIT SIGNAL */
1108         return 0;
1109 }
1110
1111 int
1112 Route::reset_plugin_counts (uint32_t* lpc)
1113 {
1114         Glib::RWLock::WriterLock lm (redirect_lock);
1115         return _reset_plugin_counts (lpc);
1116 }
1117
1118
1119 int
1120 Route::_reset_plugin_counts (uint32_t* err_streams)
1121 {
1122         RedirectList::iterator r;
1123         uint32_t i_cnt;
1124         uint32_t s_cnt;
1125         map<Placement,list<InsertCount> > insert_map;
1126         nframes_t initial_streams;
1127
1128         redirect_max_outs = 0;
1129         i_cnt = 0;
1130         s_cnt = 0;
1131
1132         /* divide inserts up by placement so we get the signal flow
1133            properly modelled. we need to do this because the _redirects
1134            list is not sorted by placement, and because other reasons may 
1135            exist now or in the future for this separate treatment.
1136         */
1137         
1138         for (r = _redirects.begin(); r != _redirects.end(); ++r) {
1139
1140                 boost::shared_ptr<Insert> insert;
1141
1142                 /* do this here in case we bomb out before we get to the end of
1143                    this function.
1144                 */
1145
1146                 redirect_max_outs = max ((*r)->output_streams (), redirect_max_outs);
1147
1148                 if ((insert = boost::dynamic_pointer_cast<Insert>(*r)) != 0) {
1149                         ++i_cnt;
1150                         insert_map[insert->placement()].push_back (InsertCount (insert));
1151
1152                         /* reset plugin counts back to one for now so
1153                            that we have a predictable, controlled
1154                            state to try to configure.
1155                         */
1156
1157                         boost::shared_ptr<PluginInsert> pi;
1158                 
1159                         if ((pi = boost::dynamic_pointer_cast<PluginInsert>(insert)) != 0) {
1160                                 pi->set_count (1);
1161                         }
1162
1163                 } else if (boost::dynamic_pointer_cast<Send> (*r) != 0) {
1164                         ++s_cnt;
1165                 }
1166         }
1167         
1168         if (i_cnt == 0) {
1169                 if (s_cnt) {
1170                         goto recompute;
1171                 } else {
1172                         return 0;
1173                 }
1174         }
1175
1176         /* Now process each placement in order, checking to see if we 
1177            can really do what has been requested.
1178         */
1179
1180         /* A: PreFader */
1181         
1182         if (check_some_plugin_counts (insert_map[PreFader], n_inputs (), err_streams)) {
1183                 return -1;
1184         }
1185
1186         /* figure out the streams that will feed into PreFader */
1187
1188         if (!insert_map[PreFader].empty()) {
1189                 InsertCount& ic (insert_map[PreFader].back());
1190                 initial_streams = ic.insert->compute_output_streams (ic.cnt);
1191         } else {
1192                 initial_streams = n_inputs ();
1193         }
1194
1195         /* B: PostFader */
1196
1197         if (check_some_plugin_counts (insert_map[PostFader], initial_streams, err_streams)) {
1198                 return -1;
1199         }
1200
1201         /* OK, everything can be set up correctly, so lets do it */
1202
1203         apply_some_plugin_counts (insert_map[PreFader]);
1204         apply_some_plugin_counts (insert_map[PostFader]);
1205
1206         /* recompute max outs of any redirect */
1207
1208   recompute:
1209
1210         redirect_max_outs = 0;
1211         RedirectList::iterator prev = _redirects.end();
1212
1213         for (r = _redirects.begin(); r != _redirects.end(); prev = r, ++r) {
1214                 boost::shared_ptr<Send> s;
1215
1216                 if ((s = boost::dynamic_pointer_cast<Send> (*r)) != 0) {
1217                         if (r == _redirects.begin()) {
1218                                 s->expect_inputs (n_inputs());
1219                         } else {
1220                                 s->expect_inputs ((*prev)->output_streams());
1221                         }
1222
1223                 } else {
1224                         
1225                         /* don't pay any attention to send output configuration, since it doesn't
1226                            affect the route.
1227                          */
1228
1229                         redirect_max_outs = max ((*r)->output_streams (), redirect_max_outs);
1230                         
1231                 }
1232         }
1233
1234         /* we're done */
1235
1236         return 0;
1237 }                                  
1238
1239 int32_t
1240 Route::apply_some_plugin_counts (list<InsertCount>& iclist)
1241 {
1242         list<InsertCount>::iterator i;
1243
1244         for (i = iclist.begin(); i != iclist.end(); ++i) {
1245                 
1246                 if ((*i).insert->configure_io ((*i).cnt, (*i).in, (*i).out)) {
1247                         return -1;
1248                 }
1249                 /* make sure that however many we have, they are all active */
1250                 (*i).insert->activate ();
1251         }
1252
1253         return 0;
1254 }
1255
1256 int32_t
1257 Route::check_some_plugin_counts (list<InsertCount>& iclist, int32_t required_inputs, uint32_t* err_streams)
1258 {
1259         list<InsertCount>::iterator i;
1260         
1261         for (i = iclist.begin(); i != iclist.end(); ++i) {
1262
1263                 if (((*i).cnt = (*i).insert->can_support_input_configuration (required_inputs)) < 0) {
1264                         if (err_streams) {
1265                                 *err_streams = required_inputs;
1266                         }
1267                         return -1;
1268                 }
1269                 
1270                 (*i).in = required_inputs;
1271                 (*i).out = (*i).insert->compute_output_streams ((*i).cnt);
1272
1273                 required_inputs = (*i).out;
1274         }
1275
1276         return 0;
1277 }
1278
1279 int
1280 Route::copy_redirects (const Route& other, Placement placement, uint32_t* err_streams)
1281 {
1282         uint32_t old_rmo = redirect_max_outs;
1283
1284         if (err_streams) {
1285                 *err_streams = 0;
1286         }
1287
1288         RedirectList to_be_deleted;
1289
1290         {
1291                 Glib::RWLock::WriterLock lm (redirect_lock);
1292                 RedirectList::iterator tmp;
1293                 RedirectList the_copy;
1294
1295                 the_copy = _redirects;
1296                 
1297                 /* remove all relevant redirects */
1298
1299                 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ) {
1300                         tmp = i;
1301                         ++tmp;
1302
1303                         if ((*i)->placement() == placement) {
1304                                 to_be_deleted.push_back (*i);
1305                                 _redirects.erase (i);
1306                         }
1307
1308                         i = tmp;
1309                 }
1310
1311                 /* now copy the relevant ones from "other" */
1312                 
1313                 for (RedirectList::const_iterator i = other._redirects.begin(); i != other._redirects.end(); ++i) {
1314                         if ((*i)->placement() == placement) {
1315                                 _redirects.push_back (Redirect::clone (*i));
1316                         }
1317                 }
1318
1319                 /* reset plugin stream handling */
1320
1321                 if (_reset_plugin_counts (err_streams)) {
1322
1323                         /* FAILED COPY ATTEMPT: we have to restore order */
1324
1325                         /* delete all cloned redirects */
1326
1327                         for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ) {
1328
1329                                 tmp = i;
1330                                 ++tmp;
1331
1332                                 if ((*i)->placement() == placement) {
1333                                         _redirects.erase (i);
1334                                 }
1335                                 
1336                                 i = tmp;
1337                         }
1338
1339                         /* restore the natural order */
1340
1341                         _redirects = the_copy;
1342                         redirect_max_outs = old_rmo;
1343
1344                         /* we failed, even though things are OK again */
1345
1346                         return -1;
1347
1348                 } else {
1349                         
1350                         /* SUCCESSFUL COPY ATTEMPT: delete the redirects we removed pre-copy */
1351                         to_be_deleted.clear ();
1352                 }
1353         }
1354
1355         if (redirect_max_outs != old_rmo || old_rmo == 0) {
1356                 reset_panner ();
1357         }
1358
1359         redirects_changed (this); /* EMIT SIGNAL */
1360         return 0;
1361 }
1362
1363 void
1364 Route::all_redirects_flip ()
1365 {
1366         Glib::RWLock::ReaderLock lm (redirect_lock);
1367
1368         if (_redirects.empty()) {
1369                 return;
1370         }
1371
1372         bool first_is_on = _redirects.front()->active();
1373         
1374         for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
1375                 (*i)->set_active (!first_is_on, this);
1376         }
1377 }
1378
1379 /** Set all redirects with a given placement to a given active state.
1380  * @param p Placement of redirects to change.
1381  * @param state New active state for those redirects.
1382  */
1383 void
1384 Route::all_redirects_active (Placement p, bool state)
1385 {
1386         Glib::RWLock::ReaderLock lm (redirect_lock);
1387
1388         if (_redirects.empty()) {
1389                 return;
1390         }
1391
1392         for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
1393                 if ((*i)->placement() == p) {
1394                         (*i)->set_active (state, this);
1395                 }
1396         }
1397 }
1398
1399 struct RedirectSorter {
1400     bool operator() (boost::shared_ptr<const Redirect> a, boost::shared_ptr<const Redirect> b) {
1401             return a->sort_key() < b->sort_key();
1402     }
1403 };
1404
1405 int
1406 Route::sort_redirects (uint32_t* err_streams)
1407 {
1408         {
1409                 RedirectSorter comparator;
1410                 Glib::RWLock::WriterLock lm (redirect_lock);
1411                 uint32_t old_rmo = redirect_max_outs;
1412
1413                 /* the sweet power of C++ ... */
1414
1415                 RedirectList as_it_was_before = _redirects;
1416
1417                 _redirects.sort (comparator);
1418         
1419                 if (_reset_plugin_counts (err_streams)) {
1420                         _redirects = as_it_was_before;
1421                         redirect_max_outs = old_rmo;
1422                         return -1;
1423                 } 
1424         } 
1425
1426         reset_panner ();
1427         redirects_changed (this); /* EMIT SIGNAL */
1428
1429         return 0;
1430 }
1431
1432 XMLNode&
1433 Route::get_state()
1434 {
1435         return state(true);
1436 }
1437
1438 XMLNode&
1439 Route::get_template()
1440 {
1441         return state(false);
1442 }
1443
1444 XMLNode&
1445 Route::state(bool full_state)
1446 {
1447         XMLNode *node = new XMLNode("Route");
1448         RedirectList:: iterator i;
1449         char buf[32];
1450
1451         if (_flags) {
1452                 node->add_property("flags", enum_2_string (_flags));
1453         }
1454         
1455         node->add_property("default-type", _default_type.to_string());
1456
1457         node->add_property("active", _active?"yes":"no");
1458         node->add_property("muted", _muted?"yes":"no");
1459         node->add_property("soloed", _soloed?"yes":"no");
1460         node->add_property("phase-invert", _phase_invert?"yes":"no");
1461         node->add_property("denormal-protection", _denormal_protection?"yes":"no");
1462         node->add_property("mute-affects-pre-fader", _mute_affects_pre_fader?"yes":"no"); 
1463         node->add_property("mute-affects-post-fader", _mute_affects_post_fader?"yes":"no"); 
1464         node->add_property("mute-affects-control-outs", _mute_affects_control_outs?"yes":"no"); 
1465         node->add_property("mute-affects-main-outs", _mute_affects_main_outs?"yes":"no"); 
1466
1467         if (_edit_group) {
1468                 node->add_property("edit-group", _edit_group->name());
1469         }
1470         if (_mix_group) {
1471                 node->add_property("mix-group", _mix_group->name());
1472         }
1473
1474         string order_string;
1475         OrderKeys::iterator x = order_keys.begin(); 
1476
1477         while (x != order_keys.end()) {
1478                 order_string += string ((*x).first);
1479                 order_string += '=';
1480                 snprintf (buf, sizeof(buf), "%ld", (*x).second);
1481                 order_string += buf;
1482                 
1483                 ++x;
1484
1485                 if (x == order_keys.end()) {
1486                         break;
1487                 }
1488
1489                 order_string += ':';
1490         }
1491         node->add_property ("order-keys", order_string);
1492
1493         node->add_child_nocopy (IO::state (full_state));
1494         node->add_child_nocopy (_solo_control.get_state ());
1495         node->add_child_nocopy (_mute_control.get_state ());
1496
1497         XMLNode* remote_control_node = new XMLNode (X_("remote_control"));
1498         snprintf (buf, sizeof (buf), "%d", _remote_control_id);
1499         remote_control_node->add_property (X_("id"), buf);
1500         node->add_child_nocopy (*remote_control_node);
1501
1502         if (_control_outs) {
1503                 XMLNode* cnode = new XMLNode (X_("ControlOuts"));
1504                 cnode->add_child_nocopy (_control_outs->state (full_state));
1505                 node->add_child_nocopy (*cnode);
1506         }
1507
1508         if (_comment.length()) {
1509                 XMLNode *cmt = node->add_child ("Comment");
1510                 cmt->add_content (_comment);
1511         }
1512
1513         for (i = _redirects.begin(); i != _redirects.end(); ++i) {
1514                 node->add_child_nocopy((*i)->state (full_state));
1515         }
1516
1517         if (_extra_xml){
1518                 node->add_child_copy (*_extra_xml);
1519         }
1520         
1521         return *node;
1522 }
1523
1524 void
1525 Route::set_deferred_state ()
1526 {
1527         XMLNodeList nlist;
1528         XMLNodeConstIterator niter;
1529
1530         if (!deferred_state) {
1531                 return;
1532         }
1533
1534         nlist = deferred_state->children();
1535
1536         for (niter = nlist.begin(); niter != nlist.end(); ++niter){
1537                 add_redirect_from_xml (**niter);
1538         }
1539
1540         delete deferred_state;
1541         deferred_state = 0;
1542 }
1543
1544 void
1545 Route::add_redirect_from_xml (const XMLNode& node)
1546 {
1547         const XMLProperty *prop;
1548
1549         if (node.name() == "Send") {
1550                 
1551
1552                 try {
1553                         boost::shared_ptr<Send> send (new Send (_session, node));
1554                         add_redirect (send, this);
1555                 } 
1556                 
1557                 catch (failed_constructor &err) {
1558                         error << _("Send construction failed") << endmsg;
1559                         return;
1560                 }
1561                 
1562         } else if (node.name() == "Insert") {
1563                 
1564                 try {
1565                         if ((prop = node.property ("type")) != 0) {
1566
1567                                 boost::shared_ptr<Insert> insert;
1568
1569                                 if (prop->value() == "ladspa" || prop->value() == "Ladspa" || prop->value() == "vst") {
1570
1571                                         insert.reset (new PluginInsert(_session, node));
1572                                         
1573                                 } else if (prop->value() == "port") {
1574
1575
1576                                         insert.reset (new PortInsert (_session, node));
1577
1578                                 } else {
1579
1580                                         error << string_compose(_("unknown Insert type \"%1\"; ignored"), prop->value()) << endmsg;
1581                                 }
1582
1583                                 add_redirect (insert, this);
1584                                 
1585                         } else {
1586                                 error << _("Insert XML node has no type property") << endmsg;
1587                         }
1588                 }
1589                 
1590                 catch (failed_constructor &err) {
1591                         warning << _("insert could not be created. Ignored.") << endmsg;
1592                         return;
1593                 }
1594         }
1595 }
1596
1597 int
1598 Route::set_state (const XMLNode& node)
1599 {
1600         return _set_state (node, true);
1601 }
1602
1603 int
1604 Route::_set_state (const XMLNode& node, bool call_base)
1605 {
1606         XMLNodeList nlist;
1607         XMLNodeConstIterator niter;
1608         XMLNode *child;
1609         XMLPropertyList plist;
1610         const XMLProperty *prop;
1611
1612         if (node.name() != "Route"){
1613                 error << string_compose(_("Bad node sent to Route::set_state() [%1]"), node.name()) << endmsg;
1614                 return -1;
1615         }
1616
1617         if ((prop = node.property (X_("flags"))) != 0) {
1618                 _flags = Flag (string_2_enum (prop->value(), _flags));
1619         } else {
1620                 _flags = Flag (0);
1621         }
1622         
1623         if ((prop = node.property (X_("default-type"))) != 0) {
1624                 _default_type = DataType(prop->value());
1625                 assert(_default_type != DataType::NIL);
1626         }
1627
1628         if ((prop = node.property (X_("phase-invert"))) != 0) {
1629                 set_phase_invert (prop->value()=="yes"?true:false, this);
1630         }
1631
1632         if ((prop = node.property (X_("denormal-protection"))) != 0) {
1633                 set_denormal_protection (prop->value()=="yes"?true:false, this);
1634         }
1635
1636         if ((prop = node.property (X_("active"))) != 0) {
1637                 set_active (prop->value() == "yes");
1638         }
1639
1640         if ((prop = node.property (X_("muted"))) != 0) {
1641                 bool yn = prop->value()=="yes"?true:false; 
1642
1643                 /* force reset of mute status */
1644
1645                 _muted = !yn;
1646                 set_mute(yn, this);
1647                 mute_gain = desired_mute_gain;
1648         }
1649
1650         if ((prop = node.property (X_("soloed"))) != 0) {
1651                 bool yn = prop->value()=="yes"?true:false; 
1652
1653                 /* force reset of solo status */
1654
1655                 _soloed = !yn;
1656                 set_solo (yn, this);
1657                 solo_gain = desired_solo_gain;
1658         }
1659
1660         if ((prop = node.property (X_("mute-affects-pre-fader"))) != 0) {
1661                 _mute_affects_pre_fader = (prop->value()=="yes")?true:false;
1662         }
1663
1664         if ((prop = node.property (X_("mute-affects-post-fader"))) != 0) {
1665                 _mute_affects_post_fader = (prop->value()=="yes")?true:false;
1666         }
1667
1668         if ((prop = node.property (X_("mute-affects-control-outs"))) != 0) {
1669                 _mute_affects_control_outs = (prop->value()=="yes")?true:false;
1670         }
1671
1672         if ((prop = node.property (X_("mute-affects-main-outs"))) != 0) {
1673                 _mute_affects_main_outs = (prop->value()=="yes")?true:false;
1674         }
1675
1676         if ((prop = node.property (X_("edit-group"))) != 0) {
1677                 RouteGroup* edit_group = _session.edit_group_by_name(prop->value());
1678                 if(edit_group == 0) {
1679                         error << string_compose(_("Route %1: unknown edit group \"%2 in saved state (ignored)"), _name, prop->value()) << endmsg;
1680                 } else {
1681                         set_edit_group(edit_group, this);
1682                 }
1683         }
1684
1685         if ((prop = node.property (X_("order-keys"))) != 0) {
1686
1687                 long n;
1688
1689                 string::size_type colon, equal;
1690                 string remaining = prop->value();
1691
1692                 while (remaining.length()) {
1693
1694                         if ((equal = remaining.find_first_of ('=')) == string::npos || equal == remaining.length()) {
1695                                 error << string_compose (_("badly formed order key string in state file! [%1] ... ignored."), remaining)
1696                                       << endmsg;
1697                         } else {
1698                                 if (sscanf (remaining.substr (equal+1).c_str(), "%ld", &n) != 1) {
1699                                         error << string_compose (_("badly formed order key string in state file! [%1] ... ignored."), remaining)
1700                                               << endmsg;
1701                                 } else {
1702                                         set_order_key (remaining.substr (0, equal).c_str(), n);
1703                                 }
1704                         }
1705
1706                         colon = remaining.find_first_of (':');
1707
1708                         if (colon != string::npos) {
1709                                 remaining = remaining.substr (colon+1);
1710                         } else {
1711                                 break;
1712                         }
1713                 }
1714         }
1715
1716         nlist = node.children();
1717
1718         if (deferred_state) {
1719                 delete deferred_state;
1720         }
1721
1722         deferred_state = new XMLNode(X_("deferred state"));
1723
1724         /* set parent class properties before anything else */
1725
1726         for (niter = nlist.begin(); niter != nlist.end(); ++niter){
1727
1728                 child = *niter;
1729
1730                 if (child->name() == IO::state_node_name && call_base) {
1731
1732                         IO::set_state (*child);
1733                         break;
1734                 }
1735         }
1736
1737
1738         XMLNodeList redirect_nodes;
1739         
1740         for (niter = nlist.begin(); niter != nlist.end(); ++niter){
1741                 
1742                 child = *niter;
1743                 
1744                 if (child->name() == X_("Send") || child->name() == X_("Insert")) {
1745                         redirect_nodes.push_back(child);
1746                 }
1747                 
1748         }
1749         
1750         _set_redirect_states (redirect_nodes);
1751
1752         for (niter = nlist.begin(); niter != nlist.end(); ++niter){
1753                 child = *niter;
1754                 // All redirects (sends and inserts) have been applied already
1755
1756                 if (child->name() == X_("Automation")) {
1757                         
1758                         if ((prop = child->property (X_("path"))) != 0)  {
1759                                 load_automation (prop->value());
1760                         }
1761
1762                 } else if (child->name() == X_("ControlOuts")) {
1763                         
1764                         string coutname = _name;
1765                         coutname += _("[control]");
1766
1767                         _control_outs = new IO (_session, coutname);
1768                         _control_outs->set_state (**(child->children().begin()));
1769
1770                 } else if (child->name() == X_("Comment")) {
1771
1772                         /* XXX this is a terrible API design in libxml++ */
1773
1774                         XMLNode *cmt = *(child->children().begin());
1775                         _comment = cmt->content();
1776
1777                 } else if (child->name() == X_("extra")) {
1778
1779                         _extra_xml = new XMLNode (*child);
1780
1781                 } else if (child->name() == X_("controllable") && (prop = child->property("name")) != 0) {
1782                         
1783                         if (prop->value() == "solo") {
1784                                 _solo_control.set_state (*child);
1785                                 _session.add_controllable (&_solo_control);
1786                         }
1787                         else if (prop->value() == "mute") {
1788                                 _mute_control.set_state (*child);
1789                                 _session.add_controllable (&_mute_control);
1790                         }
1791                 }
1792                 else if (child->name() == X_("remote_control")) {
1793                         if ((prop = child->property (X_("id"))) != 0) {
1794                                 int32_t x;
1795                                 sscanf (prop->value().c_str(), "%d", &x);
1796                                 set_remote_control_id (x);
1797                         }
1798                 }
1799         }
1800
1801         if ((prop = node.property (X_("mix-group"))) != 0) {
1802                 RouteGroup* mix_group = _session.mix_group_by_name(prop->value());
1803                 if (mix_group == 0) {
1804                         error << string_compose(_("Route %1: unknown mix group \"%2 in saved state (ignored)"), _name, prop->value()) << endmsg;
1805                 }  else {
1806                         set_mix_group(mix_group, this);
1807                 }
1808         }
1809
1810         return 0;
1811 }
1812
1813 void
1814 Route::_set_redirect_states(const XMLNodeList &nlist)
1815 {
1816         XMLNodeConstIterator niter;
1817         char buf[64];
1818
1819         RedirectList::iterator i, o;
1820
1821         if (!ports_legal) {
1822
1823                 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1824                         deferred_state->add_child_copy (**niter);
1825                 }
1826
1827                 return;
1828         }
1829
1830         // Iterate through existing redirects, remove those which are not in the state list
1831         for (i = _redirects.begin(); i != _redirects.end(); ) {
1832                 RedirectList::iterator tmp = i;
1833                 ++tmp;
1834
1835                 bool redirectInStateList = false;
1836
1837                 (*i)->id().print (buf, sizeof (buf));
1838
1839                 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1840
1841                         if (strncmp (buf,(*niter)->child(X_("Redirect"))->child(X_("IO"))->property(X_("id"))->value().c_str(), sizeof(buf)) == 0) {
1842                                 redirectInStateList = true;
1843                                 break;
1844                         }
1845                 }
1846                 
1847                 if (!redirectInStateList) {
1848                         remove_redirect ( *i, this);
1849                 }
1850
1851
1852                 i = tmp;
1853         }
1854
1855
1856         // Iterate through state list and make sure all redirects are on the track and in the correct order,
1857         // set the state of existing redirects according to the new state on the same go
1858         i = _redirects.begin();
1859         for (niter = nlist.begin(); niter != nlist.end(); ++niter, ++i) {
1860
1861                 // Check whether the next redirect in the list 
1862                 o = i;
1863
1864                 while (o != _redirects.end()) {
1865                         (*o)->id().print (buf, sizeof (buf));
1866                         if ( strncmp(buf, (*niter)->child(X_("Redirect"))->child(X_("IO"))->property(X_("id"))->value().c_str(), sizeof(buf)) == 0)
1867                                 break;
1868                         ++o;
1869                 }
1870
1871                 if (o == _redirects.end()) {
1872                         // If the redirect (*niter) is not on the route, we need to create it
1873                         // and move it to the correct location
1874
1875                         RedirectList::iterator prev_last = _redirects.end();
1876                         --prev_last; // We need this to check whether adding succeeded
1877                         
1878                         add_redirect_from_xml (**niter);
1879
1880                         RedirectList::iterator last = _redirects.end();
1881                         --last;
1882
1883                         if (prev_last == last) {
1884                                 warning << _name << ": could not fully restore state as some redirects were not possible to create" << endmsg;
1885                                 continue;
1886
1887                         }
1888
1889                         boost::shared_ptr<Redirect> tmp = (*last);
1890                         // remove the redirect from the wrong location
1891                         _redirects.erase(last);
1892                         // insert the new redirect at the current location
1893                         _redirects.insert(i, tmp);
1894
1895                         --i; // move pointer to the newly inserted redirect
1896                         continue;
1897                 }
1898
1899                 // We found the redirect (*niter) on the route, first we must make sure the redirect
1900                 // is at the location provided in the XML state
1901                 if (i != o) {
1902                         boost::shared_ptr<Redirect> tmp = (*o);
1903                         // remove the old copy
1904                         _redirects.erase(o);
1905                         // insert the redirect at the correct location
1906                         _redirects.insert(i, tmp);
1907
1908                         --i; // move pointer so it points to the right redirect
1909                 }
1910
1911                 (*i)->set_state( (**niter) );
1912         }
1913         
1914         redirects_changed(this);
1915 }
1916
1917 void
1918 Route::curve_reallocate ()
1919 {
1920 //      _gain_automation_curve.finish_resize ();
1921 //      _pan_automation_curve.finish_resize ();
1922 }
1923
1924 void
1925 Route::silence (nframes_t nframes, nframes_t offset)
1926 {
1927         if (!_silent) {
1928
1929                 // reset_peak_meters ();
1930                 
1931                 IO::silence (nframes, offset);
1932
1933                 if (_control_outs) {
1934                         _control_outs->silence (nframes, offset);
1935                 }
1936
1937                 { 
1938                         Glib::RWLock::ReaderLock lm (redirect_lock, Glib::TRY_LOCK);
1939                         
1940                         if (lm.locked()) {
1941                                 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
1942                                         boost::shared_ptr<PluginInsert> pi;
1943                                         if (!_active && (pi = boost::dynamic_pointer_cast<PluginInsert> (*i)) != 0) {
1944                                                 // skip plugins, they don't need anything when we're not active
1945                                                 continue;
1946                                         }
1947
1948                                         (*i)->silence (nframes, offset);
1949                                 }
1950
1951                                 if (nframes == _session.get_block_size() && offset == 0) {
1952                                         // _silent = true;
1953                                 }
1954                         }
1955                 }
1956                 
1957         }
1958 }       
1959
1960 int
1961 Route::set_control_outs (const vector<string>& ports)
1962 {
1963         Glib::Mutex::Lock lm (control_outs_lock);
1964         vector<string>::const_iterator i;
1965         uint32_t limit;
1966
1967         if (_control_outs) {
1968                 delete _control_outs;
1969                 _control_outs = 0;
1970         }
1971
1972         if (control() || master()) {
1973                 /* no control outs for these two special busses */
1974                 return 0;
1975         }
1976         
1977         if (ports.empty()) {
1978                 return 0;
1979         }
1980  
1981         string coutname = _name;
1982         coutname += _("[control]");
1983         
1984         _control_outs = new IO (_session, coutname);
1985
1986         /* our control outs need as many outputs as we
1987            have outputs. we track the changes in ::output_change_handler().
1988         */
1989
1990         limit = n_outputs ();
1991
1992         if (_control_outs->ensure_io (0, limit, true, this)) {
1993                 return -1;
1994         }
1995
1996         /* now connect to the named ports */
1997
1998         for (uint32_t n = 0; n < limit; ++n) {
1999                 if (_control_outs->connect_output (_control_outs->output (n), ports[n], this)) {
2000                         error << string_compose (_("could not connect %1 to %2"), _control_outs->output(n)->name(), ports[n]) << endmsg;
2001                         return -1;
2002                 }
2003         }
2004  
2005         return 0;
2006 }       
2007
2008 void
2009 Route::set_edit_group (RouteGroup *eg, void *src)
2010
2011 {
2012         if (eg == _edit_group) {
2013                 return;
2014         }
2015
2016         if (_edit_group) {
2017                 _edit_group->remove (this);
2018         }
2019
2020         if ((_edit_group = eg) != 0) {
2021                 _edit_group->add (this);
2022         }
2023
2024         _session.set_dirty ();
2025         edit_group_changed (src); /* EMIT SIGNAL */
2026 }
2027
2028 void
2029 Route::drop_edit_group (void *src)
2030 {
2031         _edit_group = 0;
2032         _session.set_dirty ();
2033         edit_group_changed (src); /* EMIT SIGNAL */
2034 }
2035
2036 void
2037 Route::set_mix_group (RouteGroup *mg, void *src)
2038
2039 {
2040         if (mg == _mix_group) {
2041                 return;
2042         }
2043
2044         if (_mix_group) {
2045                 _mix_group->remove (this);
2046         }
2047
2048         if ((_mix_group = mg) != 0) {
2049                 _mix_group->add (this);
2050         }
2051
2052         _session.set_dirty ();
2053         mix_group_changed (src); /* EMIT SIGNAL */
2054 }
2055
2056 void
2057 Route::drop_mix_group (void *src)
2058 {
2059         _mix_group = 0;
2060         _session.set_dirty ();
2061         mix_group_changed (src); /* EMIT SIGNAL */
2062 }
2063
2064 void
2065 Route::set_comment (string cmt, void *src)
2066 {
2067         _comment = cmt;
2068         comment_changed (src);
2069         _session.set_dirty ();
2070 }
2071
2072 bool
2073 Route::feeds (boost::shared_ptr<Route> other)
2074 {
2075         uint32_t i, j;
2076
2077         IO& self = *this;
2078         uint32_t no = self.n_outputs();
2079         uint32_t ni = other->n_inputs ();
2080
2081         for (i = 0; i < no; ++i) {
2082                 for (j = 0; j < ni; ++j) {
2083                         if (self.output(i)->connected_to (other->input(j)->name())) {
2084                                 return true;
2085                         }
2086                 }
2087         }
2088
2089         /* check Redirects which may also interconnect Routes */
2090
2091         for (RedirectList::iterator r = _redirects.begin(); r != _redirects.end(); r++) {
2092
2093                 no = (*r)->n_outputs();
2094
2095                 for (i = 0; i < no; ++i) {
2096                         for (j = 0; j < ni; ++j) {
2097                                 if ((*r)->output(i)->connected_to (other->input (j)->name())) {
2098                                         return true;
2099                                 }
2100                         }
2101                 }
2102         }
2103
2104         /* check for control room outputs which may also interconnect Routes */
2105
2106         if (_control_outs) {
2107
2108                 no = _control_outs->n_outputs();
2109                 
2110                 for (i = 0; i < no; ++i) {
2111                         for (j = 0; j < ni; ++j) {
2112                                 if (_control_outs->output(i)->connected_to (other->input (j)->name())) {
2113                                         return true;
2114                                 }
2115                         }
2116                 }
2117         }
2118
2119         return false;
2120 }
2121
2122 void
2123 Route::set_mute_config (mute_type t, bool onoff, void *src)
2124 {
2125         switch (t) {
2126         case PRE_FADER:
2127                 _mute_affects_pre_fader = onoff;
2128                  pre_fader_changed(src); /* EMIT SIGNAL */
2129                 break;
2130
2131         case POST_FADER:
2132                 _mute_affects_post_fader = onoff;
2133                  post_fader_changed(src); /* EMIT SIGNAL */
2134                 break;
2135
2136         case CONTROL_OUTS:
2137                 _mute_affects_control_outs = onoff;
2138                  control_outs_changed(src); /* EMIT SIGNAL */
2139                 break;
2140
2141         case MAIN_OUTS:
2142                 _mute_affects_main_outs = onoff;
2143                  main_outs_changed(src); /* EMIT SIGNAL */
2144                 break;
2145         }
2146 }
2147
2148 bool
2149 Route::get_mute_config (mute_type t)
2150 {
2151         bool onoff = false;
2152         
2153         switch (t){
2154         case PRE_FADER:
2155                 onoff = _mute_affects_pre_fader; 
2156                 break;
2157         case POST_FADER:
2158                 onoff = _mute_affects_post_fader;
2159                 break;
2160         case CONTROL_OUTS:
2161                 onoff = _mute_affects_control_outs;
2162                 break;
2163         case MAIN_OUTS:
2164                 onoff = _mute_affects_main_outs;
2165                 break;
2166         }
2167         
2168         return onoff;
2169 }
2170
2171 void
2172 Route::set_active (bool yn)
2173 {
2174         _active = yn; 
2175          active_changed(); /* EMIT SIGNAL */
2176 }
2177
2178 void
2179 Route::handle_transport_stopped (bool abort_ignored, bool did_locate, bool can_flush_redirects)
2180 {
2181         nframes_t now = _session.transport_frame();
2182
2183         {
2184                 Glib::RWLock::ReaderLock lm (redirect_lock);
2185
2186                 if (!did_locate) {
2187                         automation_snapshot (now);
2188                 }
2189
2190                 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2191                         
2192                         if (Config->get_plugins_stop_with_transport() && can_flush_redirects) {
2193                                 (*i)->deactivate ();
2194                                 (*i)->activate ();
2195                         }
2196                         
2197                         (*i)->transport_stopped (now);
2198                 }
2199         }
2200
2201         IO::transport_stopped (now);
2202  
2203         _roll_delay = _initial_delay;
2204 }
2205
2206 void
2207 Route::input_change_handler (IOChange change, void *ignored)
2208 {
2209         if (change & ConfigurationChanged) {
2210                 reset_plugin_counts (0);
2211         }
2212 }
2213
2214 void
2215 Route::output_change_handler (IOChange change, void *ignored)
2216 {
2217         if (change & ConfigurationChanged) {
2218                 if (_control_outs) {
2219                         _control_outs->ensure_io (0, n_outputs(), true, this);
2220                 }
2221                 
2222                 reset_plugin_counts (0);
2223         }
2224 }
2225
2226 uint32_t
2227 Route::pans_required () const
2228 {
2229         if (n_outputs() < 2) {
2230                 return 0;
2231         }
2232         
2233         return max (n_inputs (), redirect_max_outs);
2234 }
2235
2236 int 
2237 Route::no_roll (nframes_t nframes, nframes_t start_frame, nframes_t end_frame, nframes_t offset, 
2238                    bool session_state_changing, bool can_record, bool rec_monitors_input)
2239 {
2240         if (n_outputs() == 0) {
2241                 return 0;
2242         }
2243
2244         if (session_state_changing || !_active)  {
2245                 silence (nframes, offset);
2246                 return 0;
2247         }
2248
2249         apply_gain_automation = false;
2250         
2251         if (n_inputs()) {
2252                 passthru (start_frame, end_frame, nframes, offset, 0, false);
2253         } else {
2254                 silence (nframes, offset);
2255         }
2256
2257         return 0;
2258 }
2259
2260 nframes_t
2261 Route::check_initial_delay (nframes_t nframes, nframes_t& offset, nframes_t& transport_frame)
2262 {
2263         if (_roll_delay > nframes) {
2264
2265                 _roll_delay -= nframes;
2266                 silence (nframes, offset);
2267                 /* transport frame is not legal for caller to use */
2268                 return 0;
2269
2270         } else if (_roll_delay > 0) {
2271
2272                 nframes -= _roll_delay;
2273
2274                 silence (_roll_delay, offset);
2275
2276                 offset += _roll_delay;
2277                 transport_frame += _roll_delay;
2278
2279                 _roll_delay = 0;
2280         }
2281
2282         return nframes;
2283 }
2284
2285 int
2286 Route::roll (nframes_t nframes, nframes_t start_frame, nframes_t end_frame, nframes_t offset, int declick,
2287              bool can_record, bool rec_monitors_input)
2288 {
2289         {
2290                 Glib::RWLock::ReaderLock lm (redirect_lock, Glib::TRY_LOCK);
2291                 if (lm.locked()) {
2292                         // automation snapshot can also be called from the non-rt context
2293                         // and it uses the redirect list, so we take the lock out here
2294                         automation_snapshot (_session.transport_frame());
2295                 }
2296         }
2297
2298         if ((n_outputs() == 0 && _redirects.empty()) || n_inputs() == 0 || !_active) {
2299                 silence (nframes, offset);
2300                 return 0;
2301         }
2302         
2303         nframes_t unused = 0;
2304
2305         if ((nframes = check_initial_delay (nframes, offset, unused)) == 0) {
2306                 return 0;
2307         }
2308
2309         _silent = false;
2310
2311         apply_gain_automation = false;
2312
2313         { 
2314                 Glib::Mutex::Lock am (automation_lock, Glib::TRY_LOCK);
2315                 
2316                 if (am.locked() && _session.transport_rolling()) {
2317                         
2318                         nframes_t start_frame = end_frame - nframes;
2319                         
2320                         if (gain_automation_playback()) {
2321                                 apply_gain_automation = _gain_automation_curve.rt_safe_get_vector (start_frame, end_frame, _session.gain_automation_buffer(), nframes);
2322                         }
2323                 }
2324         }
2325
2326         passthru (start_frame, end_frame, nframes, offset, declick, false);
2327
2328         return 0;
2329 }
2330
2331 int
2332 Route::silent_roll (nframes_t nframes, nframes_t start_frame, nframes_t end_frame, nframes_t offset, 
2333                     bool can_record, bool rec_monitors_input)
2334 {
2335         silence (nframes, offset);
2336         return 0;
2337 }
2338
2339 void
2340 Route::toggle_monitor_input ()
2341 {
2342         for (vector<Port*>::iterator i = _inputs.begin(); i != _inputs.end(); ++i) {
2343                 (*i)->ensure_monitor_input(!(*i)->monitoring_input());
2344         }
2345 }
2346
2347 bool
2348 Route::has_external_redirects () const
2349 {
2350         boost::shared_ptr<const PortInsert> pi;
2351         
2352         for (RedirectList::const_iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2353                 if ((pi = boost::dynamic_pointer_cast<const PortInsert>(*i)) != 0) {
2354
2355                         uint32_t no = pi->n_outputs();
2356
2357                         for (uint32_t n = 0; n < no; ++n) {
2358                                 
2359                                 string port_name = pi->output(n)->name();
2360                                 string client_name = port_name.substr (0, port_name.find(':'));
2361
2362                                 /* only say "yes" if the redirect is actually in use */
2363                                 
2364                                 if (client_name != "ardour" && pi->active()) {
2365                                         return true;
2366                                 }
2367                         }
2368                 }
2369         }
2370
2371         return false;
2372 }
2373
2374 void
2375 Route::flush_redirects ()
2376 {
2377         /* XXX shouldn't really try to take this lock, since
2378            this is called from the RT audio thread.
2379         */
2380
2381         Glib::RWLock::ReaderLock lm (redirect_lock);
2382
2383         for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2384                 (*i)->deactivate ();
2385                 (*i)->activate ();
2386         }
2387 }
2388
2389 void
2390 Route::set_meter_point (MeterPoint p, void *src)
2391 {
2392         if (_meter_point != p) {
2393                 _meter_point = p;
2394                  meter_change (src); /* EMIT SIGNAL */
2395                 _session.set_dirty ();
2396         }
2397 }
2398
2399 nframes_t
2400 Route::update_total_latency ()
2401 {
2402         _own_latency = 0;
2403
2404         for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2405                 if ((*i)->active ()) {
2406                         _own_latency += (*i)->latency ();
2407                 }
2408         }
2409
2410         set_port_latency (_own_latency);
2411
2412         /* this (virtual) function is used for pure Routes,
2413            not derived classes like AudioTrack.  this means
2414            that the data processed here comes from an input
2415            port, not prerecorded material, and therefore we
2416            have to take into account any input latency.
2417         */
2418
2419         _own_latency += input_latency ();
2420
2421         return _own_latency;
2422 }
2423
2424 void
2425 Route::set_latency_delay (nframes_t longest_session_latency)
2426 {
2427         _initial_delay = longest_session_latency - _own_latency;
2428
2429         if (_session.transport_stopped()) {
2430                 _roll_delay = _initial_delay;
2431         }
2432 }
2433
2434 void
2435 Route::automation_snapshot (nframes_t now)
2436 {
2437         IO::automation_snapshot (now);
2438
2439         for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2440                 (*i)->automation_snapshot (now);
2441         }
2442 }
2443
2444 Route::ToggleControllable::ToggleControllable (std::string name, Route& s, ToggleType tp)
2445         : Controllable (name), route (s), type(tp)
2446 {
2447         
2448 }
2449
2450 void
2451 Route::ToggleControllable::set_value (float val)
2452 {
2453         bool bval = ((val >= 0.5f) ? true: false);
2454         
2455         switch (type) {
2456         case MuteControl:
2457                 route.set_mute (bval, this);
2458                 break;
2459         case SoloControl:
2460                 route.set_solo (bval, this);
2461                 break;
2462         default:
2463                 break;
2464         }
2465 }
2466
2467 float
2468 Route::ToggleControllable::get_value (void) const
2469 {
2470         float val = 0.0f;
2471         
2472         switch (type) {
2473         case MuteControl:
2474                 val = route.muted() ? 1.0f : 0.0f;
2475                 break;
2476         case SoloControl:
2477                 val = route.soloed() ? 1.0f : 0.0f;
2478                 break;
2479         default:
2480                 break;
2481         }
2482
2483         return val;
2484 }
2485
2486 void 
2487 Route::set_block_size (nframes_t nframes)
2488 {
2489         for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2490                 (*i)->set_block_size (nframes);
2491         }
2492 }
2493
2494 void
2495 Route::redirect_active_proxy (Redirect* ignored, void* ignored_src)
2496 {
2497         _session.update_latency_compensation (false, false);
2498 }
2499
2500 void
2501 Route::protect_automation ()
2502 {
2503         switch (gain_automation_state()) {
2504         case Write:
2505                 set_gain_automation_state (Off);
2506         case Touch:
2507                 set_gain_automation_state (Play);
2508                 break;
2509         default:
2510                 break;
2511         }
2512
2513         switch (panner().automation_state ()) {
2514         case Write:
2515                 panner().set_automation_state (Off);
2516                 break;
2517         case Touch:
2518                 panner().set_automation_state (Play);
2519                 break;
2520         default:
2521                 break;
2522         }
2523         
2524         for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
2525                 boost::shared_ptr<PluginInsert> pi;
2526                 if ((pi = boost::dynamic_pointer_cast<PluginInsert> (*i)) != 0) {
2527                         pi->protect_automation ();
2528                 }
2529         }
2530 }
2531
2532 void
2533 Route::set_pending_declick (int declick)
2534 {
2535         if (_declickable) {
2536                 /* this call is not allowed to turn off a pending declick unless "force" is true */
2537                 if (declick) {
2538                         _pending_declick = declick;
2539                 }
2540                 // cerr << _name << ": after setting to " << declick << " pending declick = " << _pending_declick << endl;
2541         } else {
2542                 _pending_declick = 0;
2543         }
2544
2545 }