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