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