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