29cc32bf24996bd44388d6878243fb67218c16f3
[ardour.git] / libs / ardour / route.cc
1 /*
2     Copyright (C) 2000 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <cmath>
21 #include <fstream>
22 #include <cassert>
23 #include <algorithm>
24
25 #include "pbd/xml++.h"
26 #include "pbd/enumwriter.h"
27 #include "pbd/memento_command.h"
28 #include "pbd/stacktrace.h"
29
30 #include "evoral/Curve.hpp"
31
32 #include "ardour/amp.h"
33 #include "ardour/audio_port.h"
34 #include "ardour/audioengine.h"
35 #include "ardour/buffer.h"
36 #include "ardour/buffer_set.h"
37 #include "ardour/configuration.h"
38 #include "ardour/cycle_timer.h"
39 #include "ardour/debug.h"
40 #include "ardour/delivery.h"
41 #include "ardour/dB.h"
42 #include "ardour/internal_send.h"
43 #include "ardour/internal_return.h"
44 #include "ardour/ladspa_plugin.h"
45 #include "ardour/meter.h"
46 #include "ardour/mix.h"
47 #include "ardour/panner.h"
48 #include "ardour/plugin_insert.h"
49 #include "ardour/port.h"
50 #include "ardour/port_insert.h"
51 #include "ardour/processor.h"
52 #include "ardour/profile.h"
53 #include "ardour/route.h"
54 #include "ardour/route_group.h"
55 #include "ardour/send.h"
56 #include "ardour/session.h"
57 #include "ardour/timestamps.h"
58 #include "ardour/utils.h"
59
60 #include "i18n.h"
61
62 using namespace std;
63 using namespace ARDOUR;
64 using namespace PBD;
65
66 uint32_t Route::order_key_cnt = 0;
67 PBD::Signal1<void,string const&> Route::SyncOrderKeys;
68 PBD::Signal0<void> Route::RemoteControlIDChange;
69
70 Route::Route (Session& sess, string name, Flag flg, DataType default_type)
71         : SessionObject (sess, name)
72         , AutomatableControls (sess)
73         , _flags (flg)
74         , _solo_control (new SoloControllable (X_("solo"), *this))
75         , _mute_master (new MuteMaster (sess, name))
76         , _default_type (default_type)
77
78 {
79         init ();
80
81         /* add standard processors other than amp (added by ::init()) */
82
83         _meter.reset (new PeakMeter (_session));
84         _meter->set_display_to_user (false);
85         add_processor (_meter, PostFader);
86
87         if (_flags & ControlOut) {
88                 /* where we listen to tracks */
89                 _intreturn.reset (new InternalReturn (_session));
90                 add_processor (_intreturn, PreFader);
91         }
92
93         _main_outs.reset (new Delivery (_session, _output, _mute_master, _name, Delivery::Main));
94         add_processor (_main_outs, PostFader);
95
96         /* now that we have _meter, its safe to connect to this */
97
98         Metering::Meter.connect_same_thread (*this, (boost::bind (&Route::meter, this)));
99 }
100
101 Route::Route (Session& sess, const XMLNode& node, DataType default_type)
102         : SessionObject (sess, "toBeReset")
103         , AutomatableControls (sess)
104         , _solo_control (new SoloControllable (X_("solo"), *this))
105         , _mute_master (new MuteMaster (sess, "toBeReset"))
106         , _default_type (default_type)
107 {
108         init ();
109
110         _set_state (node, Stateful::loading_state_version, false);
111
112         /* now that we have _meter, its safe to connect to this */
113
114         Metering::Meter.connect_same_thread (*this, (boost::bind (&Route::meter, this)));
115 }
116
117 void
118 Route::init ()
119 {
120         _self_solo = false;
121         _soloed_by_others = 0;
122         _solo_isolated = 0;
123         _solo_safe = false;
124         _active = true;
125         processor_max_streams.reset();
126         _recordable = true;
127         order_keys[N_("signal")] = order_key_cnt++;
128         _silent = false;
129         _meter_point = MeterPostFader;
130         _initial_delay = 0;
131         _roll_delay = 0;
132         _have_internal_generator = false;
133         _declickable = false;
134         _pending_declick = true;
135         _remote_control_id = 0;
136         _in_configure_processors = false;
137         _mute_points = MuteMaster::AllPoints;
138
139         _phase_invert = 0;
140         _denormal_protection = false;
141
142         /* add standard controls */
143
144         _solo_control->set_flags (Controllable::Flag (_solo_control->flags() | Controllable::Toggle));
145         _mute_master->set_flags (Controllable::Flag (_solo_control->flags() | Controllable::Toggle));
146         
147         add_control (_solo_control);
148         add_control (_mute_master);
149
150         /* input and output objects */
151
152         _input.reset (new IO (_session, _name, IO::Input, _default_type));
153         _output.reset (new IO (_session, _name, IO::Output, _default_type));
154
155         _input->changed.connect_same_thread (*this, boost::bind (&Route::input_change_handler, this, _1, _2));
156         _output->changed.connect_same_thread (*this, boost::bind (&Route::output_change_handler, this, _1, _2));
157
158         /* add amp processor  */
159
160         _amp.reset (new Amp (_session, _mute_master));
161         add_processor (_amp, PostFader);
162 }
163
164 Route::~Route ()
165 {
166         DEBUG_TRACE (DEBUG::Destruction, string_compose ("route %1 destructor\n", _name));
167
168         /* do this early so that we don't get incoming signals as we are going through destruction 
169          */
170
171         drop_connections ();
172
173         /* don't use clear_processors here, as it depends on the session which may
174            be half-destroyed by now 
175         */
176
177         Glib::RWLock::WriterLock lm (_processor_lock);
178         for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
179                 (*i)->drop_references ();
180         }
181
182         _processors.clear ();
183 }
184
185 void
186 Route::set_remote_control_id (uint32_t id, bool notify_class_listeners)
187 {
188         if (id != _remote_control_id) {
189                 _remote_control_id = id;
190                 RemoteControlIDChanged ();
191                 if (notify_class_listeners) {
192                         RemoteControlIDChange ();
193                 }
194         }
195 }
196
197 uint32_t
198 Route::remote_control_id() const
199 {
200         return _remote_control_id;
201 }
202
203 long
204 Route::order_key (std::string const & name) const
205 {
206         OrderKeys::const_iterator i = order_keys.find (name);
207         if (i == order_keys.end()) {
208                 return -1;
209         }
210
211         return i->second;
212 }
213
214 void
215 Route::set_order_key (std::string const & name, long n)
216 {
217         order_keys[name] = n;
218
219         if (Config->get_sync_all_route_ordering()) {
220                 for (OrderKeys::iterator x = order_keys.begin(); x != order_keys.end(); ++x) {
221                         x->second = n;
222                 }
223         }
224
225         _session.set_dirty ();
226 }
227
228 /** Set all order keys to be the same as that for `base', if such a key
229  *  exists in this route.
230  *  @param base Base key.
231  */
232 void
233 Route::sync_order_keys (std::string const & base)
234 {
235         if (order_keys.empty()) {
236                 return;
237         }
238
239         OrderKeys::iterator i;
240         uint32_t key;
241
242         if ((i = order_keys.find (base)) == order_keys.end()) {
243                 /* key doesn't exist, use the first existing key (during session initialization) */
244                 i = order_keys.begin();
245                 key = i->second;
246                 ++i;
247         } else {
248                 /* key exists - use it and reset all others (actually, itself included) */
249                 key = i->second;
250                 i = order_keys.begin();
251         }
252
253         for (; i != order_keys.end(); ++i) {
254                 i->second = key;
255         }
256 }
257
258 string
259 Route::ensure_track_or_route_name(string name, Session &session)
260 {
261         string newname = name;
262
263         while (session.route_by_name (newname) != NULL) {
264                 newname = bump_name_once (newname);
265         }
266
267         return newname;
268 }
269
270
271 void
272 Route::inc_gain (gain_t fraction, void *src)
273 {
274         _amp->inc_gain (fraction, src);
275 }
276
277 void
278 Route::set_gain (gain_t val, void *src)
279 {
280         if (src != 0 && _route_group && src != _route_group && _route_group->active_property (RouteGroup::Gain)) {
281
282                 if (_route_group->is_relative()) {
283
284                         gain_t usable_gain = _amp->gain();
285                         if (usable_gain < 0.000001f) {
286                                 usable_gain = 0.000001f;
287                         }
288
289                         gain_t delta = val;
290                         if (delta < 0.000001f) {
291                                 delta = 0.000001f;
292                         }
293
294                         delta -= usable_gain;
295
296                         if (delta == 0.0f)
297                                 return;
298
299                         gain_t factor = delta / usable_gain;
300
301                         if (factor > 0.0f) {
302                                 factor = _route_group->get_max_factor(factor);
303                                 if (factor == 0.0f) {
304                                         _amp->gain_control()->Changed(); /* EMIT SIGNAL */
305                                         return;
306                                 }
307                         } else {
308                                 factor = _route_group->get_min_factor(factor);
309                                 if (factor == 0.0f) {
310                                         _amp->gain_control()->Changed(); /* EMIT SIGNAL */
311                                         return;
312                                 }
313                         }
314
315                         _route_group->apply (&Route::inc_gain, factor, _route_group);
316
317                 } else {
318
319                         _route_group->apply (&Route::set_gain, val, _route_group);
320                 }
321
322                 return;
323         }
324
325         if (val == _amp->gain()) {
326                 return;
327         }
328
329         _amp->set_gain (val, src);
330 }
331
332 /** Process this route for one (sub) cycle (process thread)
333  *
334  * @param bufs Scratch buffers to use for the signal path
335  * @param start_frame Initial transport frame
336  * @param end_frame Final transport frame
337  * @param nframes Number of frames to output (to ports)
338  *
339  * Note that (end_frame - start_frame) may not be equal to nframes when the
340  * transport speed isn't 1.0 (eg varispeed).
341  */
342 void
343 Route::process_output_buffers (BufferSet& bufs,
344                                sframes_t start_frame, sframes_t end_frame, nframes_t nframes,
345                                bool /*with_processors*/, int declick)
346 {
347         bool monitor;
348
349         bufs.is_silent (false);
350
351         switch (Config->get_monitoring_model()) {
352         case HardwareMonitoring:
353         case ExternalMonitoring:
354                 monitor = !record_enabled() || (_session.config.get_auto_input() && !_session.actively_recording());
355                 break;
356         default:
357                 monitor = true;
358         }
359
360         if (!declick) {
361                 declick = _pending_declick;
362         }
363
364         /* figure out if we're going to use gain automation */
365         _amp->setup_gain_automation (start_frame, end_frame, nframes);
366
367
368         /* tell main outs what to do about monitoring */
369         _main_outs->no_outs_cuz_we_no_monitor (!monitor);
370
371
372         /* -------------------------------------------------------------------------------------------
373            GLOBAL DECLICK (for transport changes etc.)
374            ----------------------------------------------------------------------------------------- */
375
376         if (declick > 0) {
377                 Amp::apply_gain (bufs, nframes, 0.0, 1.0);
378         } else if (declick < 0) {
379                 Amp::apply_gain (bufs, nframes, 1.0, 0.0);
380         }
381
382         _pending_declick = 0;
383
384         /* -------------------------------------------------------------------------------------------
385            DENORMAL CONTROL/PHASE INVERT
386            ----------------------------------------------------------------------------------------- */
387
388         if (_phase_invert) {
389
390                 int chn = 0;
391
392                 if (_denormal_protection || Config->get_denormal_protection()) {
393
394                         for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i, ++chn) {
395                                 Sample* const sp = i->data();
396
397                                 if (_phase_invert & chn) {
398                                         for (nframes_t nx = 0; nx < nframes; ++nx) {
399                                                 sp[nx]  = -sp[nx];
400                                                 sp[nx] += 1.0e-27f;
401                                         }
402                                 } else {
403                                         for (nframes_t nx = 0; nx < nframes; ++nx) {
404                                                 sp[nx] += 1.0e-27f;
405                                         }
406                                 }
407                         }
408
409                 } else {
410
411                         for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i, ++chn) {
412                                 Sample* const sp = i->data();
413
414                                 if (_phase_invert & chn) {
415                                         for (nframes_t nx = 0; nx < nframes; ++nx) {
416                                                 sp[nx] = -sp[nx];
417                                         }
418                                 }
419                         }
420                 }
421
422         } else {
423
424                 if (_denormal_protection || Config->get_denormal_protection()) {
425
426                         for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
427                                 Sample* const sp = i->data();
428                                 for (nframes_t nx = 0; nx < nframes; ++nx) {
429                                         sp[nx] += 1.0e-27f;
430                                 }
431                         }
432
433                 }
434         }
435
436         /* -------------------------------------------------------------------------------------------
437            and go ....
438            ----------------------------------------------------------------------------------------- */
439
440         Glib::RWLock::ReaderLock rm (_processor_lock, Glib::TRY_LOCK);
441
442         if (rm.locked()) {
443                 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
444
445                         if (bufs.count() != (*i)->input_streams()) {
446                                 cerr << _name << " bufs = " << bufs.count()
447                                      << " input for " << (*i)->name() << " = " << (*i)->input_streams()
448                                      << endl;
449                         }
450                         assert (bufs.count() == (*i)->input_streams());
451
452                         (*i)->run (bufs, start_frame, end_frame, nframes, *i != _processors.back());
453                         bufs.set_count (ChanCount::max(bufs.count(), (*i)->output_streams()));
454                 }
455
456                 if (!_processors.empty()) {
457                         bufs.set_count (ChanCount::max (bufs.count(), _processors.back()->output_streams()));
458                 }
459         }
460 }
461
462 ChanCount
463 Route::n_process_buffers ()
464 {
465         return max (_input->n_ports(), processor_max_streams);
466 }
467
468 void
469 Route::passthru (sframes_t start_frame, sframes_t end_frame, nframes_t nframes, int declick)
470 {
471         BufferSet& bufs = _session.get_scratch_buffers (n_process_buffers());
472
473         _silent = false;
474
475         assert (bufs.available() >= _input->n_ports());
476
477         if (_input->n_ports() == ChanCount::ZERO) {
478                 silence (nframes);
479         }
480
481         bufs.set_count (_input->n_ports());
482
483         if (is_control() && _session.listening()) {
484
485                 /* control/monitor bus ignores input ports when something is
486                    feeding the listen "stream". data will "arrive" into the
487                    route from the intreturn processor element.
488                 */
489
490                 bufs.silence (nframes, 0);
491
492         } else {
493
494                 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
495
496                         BufferSet::iterator o = bufs.begin(*t);
497                         PortSet& ports (_input->ports());
498
499                         for (PortSet::iterator i = ports.begin(*t); i != ports.end(*t); ++i, ++o) {
500                                 o->read_from (i->get_buffer(nframes), nframes);
501                         }
502                 }
503         }
504
505         write_out_of_band_data (bufs, start_frame, end_frame, nframes);
506         process_output_buffers (bufs, start_frame, end_frame, nframes, true, declick);
507 }
508
509 void
510 Route::passthru_silence (sframes_t start_frame, sframes_t end_frame, nframes_t nframes, int declick)
511 {
512         BufferSet& bufs (_session.get_silent_buffers (n_process_buffers()));
513         bufs.set_count (_input->n_ports());
514         write_out_of_band_data (bufs, start_frame, end_frame, nframes);
515         process_output_buffers (bufs, start_frame, end_frame, nframes, true, declick);
516 }
517
518 void
519 Route::set_listen (bool yn, void* src)
520 {
521         if (_control_outs) {
522                 if (yn != _control_outs->active()) {
523                         if (yn) {
524                                 _control_outs->activate ();
525                         } else {
526                                 _control_outs->deactivate ();
527                         }
528
529                         listen_changed (src); /* EMIT SIGNAL */
530                 }
531         }
532 }
533
534 bool
535 Route::listening () const
536 {
537         if (_control_outs) {
538                 return _control_outs->active ();
539         } else {
540                 return false;
541         }
542 }
543
544 void
545 Route::set_solo_safe (bool yn, void *src)
546 {
547         if (_solo_safe != yn) {
548                 _solo_safe = yn;
549                 solo_safe_changed (src);
550         } 
551 }
552
553 bool
554 Route::solo_safe() const
555 {
556         return _solo_safe;
557 }
558
559 void
560 Route::set_solo (bool yn, void *src)
561 {
562         if (_solo_safe) {
563                 return;
564         }
565
566         if (_route_group && src != _route_group && _route_group->active_property (RouteGroup::Solo)) {
567                 _route_group->apply (&Route::set_solo, yn, _route_group);
568                 return;
569         }
570
571         if (self_soloed() != yn) {
572                 set_self_solo (yn);
573                 set_delivery_solo ();
574                 solo_changed (src); /* EMIT SIGNAL */
575                 _solo_control->Changed (); /* EMIT SIGNAL */
576         }
577 }
578
579 void
580 Route::set_self_solo (bool yn)
581 {
582         _self_solo = yn;
583 }
584
585 void
586 Route::mod_solo_by_others (int32_t delta)
587 {
588         if (delta < 0) {
589                 if (_soloed_by_others >= (uint32_t) delta) {
590                         _soloed_by_others += delta;
591                 } else {
592                         _soloed_by_others = 0;
593                 }
594         } else {
595                 _soloed_by_others += delta;
596         }
597
598         set_delivery_solo ();
599 }
600
601 void
602 Route::set_delivery_solo ()
603 {
604         /* tell all delivery processors what the solo situation is, so that they keep
605            delivering even though Session::soloing() is true and they were not
606            explicitly soloed.
607         */
608
609         Glib::RWLock::ReaderLock rm (_processor_lock);
610         for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
611                 boost::shared_ptr<Delivery> d;
612                 
613                 if ((d = boost::dynamic_pointer_cast<Delivery> (*i)) != 0) {
614                         d->set_solo_level (soloed ());
615                         d->set_solo_isolated (solo_isolated());
616                 }
617         }
618 }
619
620 void
621 Route::set_solo_isolated (bool yn, void *src)
622 {
623         if (is_master() || is_control() || is_hidden()) {
624                 return;
625         }
626
627         if (_route_group && src != _route_group && _route_group->active_property (RouteGroup::Solo)) {
628                 _route_group->apply (&Route::set_solo_isolated, yn, _route_group);
629                 return;
630         }
631         
632         /* forward propagate solo-isolate status to everything fed by this route, but not those via sends only */
633
634         boost::shared_ptr<RouteList> routes = _session.get_routes ();
635         for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
636                 bool sends_only;
637                 bool does_feed = feeds (*i, &sends_only);
638                 
639                 if (does_feed && !sends_only) {
640                         (*i)->set_solo_isolated (yn, (*i)->route_group());
641                 }
642         }
643
644         bool changed = false;
645
646         if (yn) {
647                 if (_solo_isolated == 0) {
648                         changed = true;
649                 }
650                 _solo_isolated++;
651         } else {
652                 changed = (_solo_isolated == 1);
653                 if (_solo_isolated > 0) {
654                         _solo_isolated--;
655                 }
656         }
657
658         if (changed) {
659                 set_delivery_solo ();
660                 solo_isolated_changed (src);
661         }
662 }
663
664 bool
665 Route::solo_isolated () const
666 {
667         return _solo_isolated > 0;
668 }
669
670 void
671 Route::set_mute_points (MuteMaster::MutePoint mp)
672 {
673         _mute_points = mp;
674         mute_points_changed (); /* EMIT SIGNAL */
675
676         if (_mute_master->muted()) {
677                 _mute_master->mute_at (_mute_points);
678                 mute_changed (this); /* EMIT SIGNAL */
679         }
680 }
681
682 void
683 Route::set_mute (bool yn, void *src)
684 {
685         if (_route_group && src != _route_group && _route_group->active_property (RouteGroup::Mute)) {
686                 _route_group->apply (&Route::set_mute, yn, _route_group);
687                 return;
688         }
689
690         if (muted() != yn) {
691                 if (yn) {
692                         _mute_master->mute_at (_mute_points);
693                 } else {
694                         _mute_master->clear_mute ();
695                 }
696
697                 mute_changed (src); /* EMIT SIGNAL */
698         }
699 }
700
701 bool
702 Route::muted() const
703 {
704         return _mute_master->muted ();
705 }
706
707 #if 0
708 static void
709 dump_processors(const string& name, const list<boost::shared_ptr<Processor> >& procs)
710 {
711         cerr << name << " {" << endl;
712         for (list<boost::shared_ptr<Processor> >::const_iterator p = procs.begin();
713                         p != procs.end(); ++p) {
714                 cerr << "\t" << (*p)->name() << " ID = " << (*p)->id() << endl;
715         }
716         cerr << "}" << endl;
717 }
718 #endif
719
720 int
721 Route::add_processor (boost::shared_ptr<Processor> processor, Placement placement, ProcessorStreams* err)
722 {
723         ProcessorList::iterator loc;
724
725         /* XXX this is not thread safe - we don't hold the lock across determining the iter
726            to add before and actually doing the insertion. dammit.
727         */
728
729         if (placement == PreFader) {
730                 /* generic pre-fader: insert immediately before the amp */
731                 loc = find (_processors.begin(), _processors.end(), _amp);
732         } else {
733                 /* generic post-fader: insert right before the main outs */
734                 loc = find (_processors.begin(), _processors.end(), _main_outs);
735         }
736
737         return add_processor (processor, loc, err);
738 }
739
740
741 /** Add a processor to the route.
742  * If @a iter is not NULL, it must point to an iterator in _processors and the new
743  * processor will be inserted immediately before this location.  Otherwise,
744  * @a position is used.
745  */
746 int
747 Route::add_processor (boost::shared_ptr<Processor> processor, ProcessorList::iterator iter, ProcessorStreams* err)
748 {
749         ChanCount old_pms = processor_max_streams;
750
751         if (!_session.engine().connected() || !processor) {
752                 return 1;
753         }
754
755         {
756                 Glib::RWLock::WriterLock lm (_processor_lock);
757
758                 boost::shared_ptr<PluginInsert> pi;
759                 boost::shared_ptr<PortInsert> porti;
760
761                 ProcessorList::iterator loc = find(_processors.begin(), _processors.end(), processor);
762
763                 if (processor == _amp || processor == _meter || processor == _main_outs) {
764                         // Ensure only one of these are in the list at any time
765                         if (loc != _processors.end()) {
766                                 if (iter == loc) { // Already in place, do nothing
767                                         return 0;
768                                 } else { // New position given, relocate
769                                         _processors.erase (loc);
770                                 }
771                         }
772
773                 } else {
774                         if (loc != _processors.end()) {
775                                 cerr << "ERROR: Processor added to route twice!" << endl;
776                                 return 1;
777                         }
778
779                         loc = iter;
780                 }
781
782                 _processors.insert (loc, processor);
783
784                 // Set up processor list channels.  This will set processor->[input|output]_streams(),
785                 // configure redirect ports properly, etc.
786
787                 if (configure_processors_unlocked (err)) {
788                         ProcessorList::iterator ploc = loc;
789                         --ploc;
790                         _processors.erase(ploc);
791                         configure_processors_unlocked (0); // it worked before we tried to add it ...
792                         cerr << "configure failed\n";
793                         return -1;
794                 }
795
796                 if ((pi = boost::dynamic_pointer_cast<PluginInsert>(processor)) != 0) {
797
798                         if (pi->natural_input_streams() == ChanCount::ZERO) {
799                                 /* generator plugin */
800                                 _have_internal_generator = true;
801                         }
802
803                 }
804
805                 if (_control_outs != processor) {
806                         // XXX: do we want to emit the signal here ? change call order.
807                         processor->activate ();
808                 }
809
810                 processor->ActiveChanged.connect_same_thread (*this, boost::bind (&Session::update_latency_compensation, &_session, false, false));
811
812                 _output->set_user_latency (0);
813         }
814
815         processors_changed (RouteProcessorChange ()); /* EMIT SIGNAL */
816
817         return 0;
818 }
819
820 bool
821 Route::add_processor_from_xml (const XMLNode& node, ProcessorList::iterator iter)
822 {
823         const XMLProperty *prop;
824
825         if (node.name() != "Processor") {
826                 return false;
827         }
828
829         try {
830                 if ((prop = node.property ("type")) != 0) {
831
832                         boost::shared_ptr<Processor> processor;
833
834                         if (prop->value() == "ladspa" || prop->value() == "Ladspa" ||
835                             prop->value() == "lv2" ||
836                             prop->value() == "vst" ||
837                             prop->value() == "audiounit") {
838
839                                 processor.reset (new PluginInsert(_session, node));
840
841                         } else if (prop->value() == "port") {
842
843                                 processor.reset (new PortInsert (_session, _mute_master, node));
844
845                         } else if (prop->value() == "send") {
846
847                                 processor.reset (new Send (_session, _mute_master, node));
848
849                         } else if (prop->value() == "meter") {
850
851                                 if (_meter) {
852                                         if (_meter->set_state (node, Stateful::loading_state_version)) {
853                                                 return false;
854                                         } else {
855                                                 return true;
856                                         }
857                                 }
858
859                                 _meter.reset (new PeakMeter (_session, node));
860                                 _meter->set_display_to_user (_meter_point == MeterCustom);
861                                 processor = _meter;
862
863                         } else if (prop->value() == "amp") {
864
865                                 /* amp always exists */
866
867                                 processor = _amp;
868                                 if (processor->set_state (node, Stateful::loading_state_version)) {
869                                         return false;
870                                 } else {
871                                         /* never any reason to add it */
872                                         return true;
873                                 }
874
875                         } else if (prop->value() == "intsend") {
876
877                                 processor.reset (new InternalSend (_session, _mute_master, node));
878
879                         } else if (prop->value() == "intreturn") {
880
881                                 if (_intreturn) {
882                                         if (_intreturn->set_state (node, Stateful::loading_state_version)) {
883                                                 return false;
884                                         } else {
885                                                 return true;
886                                         }
887                                 }
888                                 _intreturn.reset (new InternalReturn (_session, node));
889                                 processor = _intreturn;
890
891                         } else if (prop->value() == "main-outs") {
892
893                                 if (_main_outs) {
894                                         if (_main_outs->set_state (node, Stateful::loading_state_version)) {
895                                                 return false;
896                                         } else {
897                                                 return true;
898                                         }
899                                 }
900
901                                 _main_outs.reset (new Delivery (_session, _output, _mute_master, node));
902                                 processor = _main_outs;
903
904                         } else {
905                                 error << string_compose(_("unknown Processor type \"%1\"; ignored"), prop->value()) << endmsg;
906                                 return false;
907                         }
908
909                         if (iter == _processors.end() && processor->display_to_user() && !_processors.empty()) {
910                                 /* check for invisible processors stacked at the end and leave them there */
911                                 ProcessorList::iterator p;
912                                 p = _processors.end();
913                                 --p;
914                                 while (!(*p)->display_to_user() && p != _processors.begin()) {
915                                         --p;
916                                 }
917                                 ++p;
918                                 iter = p;
919                         }
920
921                         return (add_processor (processor, iter) == 0);
922
923                 } else {
924                         error << _("Processor XML node has no type property") << endmsg;
925                         return false;
926                 }
927         }
928
929         catch (failed_constructor &err) {
930                 warning << _("processor could not be created. Ignored.") << endmsg;
931                 return false;
932         }
933 }
934
935
936 bool
937 Route::add_processor_from_xml_2X (const XMLNode& node, int version, ProcessorList::iterator iter)
938 {
939         const XMLProperty *prop;
940
941         try {
942                 boost::shared_ptr<Processor> processor;
943
944                 if (node.name() == "Insert") {
945
946                         if ((prop = node.property ("type")) != 0) {
947
948                                 if (prop->value() == "ladspa" || prop->value() == "Ladspa" || 
949                                                 prop->value() == "lv2" ||
950                                                 prop->value() == "vst" ||
951                                                 prop->value() == "audiounit") {
952
953                                         processor.reset (new PluginInsert (_session, node));
954
955                                 } else {
956
957                                         processor.reset (new PortInsert (_session, _mute_master, node));
958                                 }
959
960                         }
961
962                 } else if (node.name() == "Send") {
963
964                         processor.reset (new Send (_session, _mute_master, node, version));
965
966                 } else {
967
968                         error << string_compose(_("unknown Processor type \"%1\"; ignored"), node.name()) << endmsg;
969                         return false;
970                 }
971
972                 if (iter == _processors.end() && processor->display_to_user() && !_processors.empty()) {
973                         /* check for invisible processors stacked at the end and leave them there */
974                         ProcessorList::iterator p;
975                         p = _processors.end();
976                         --p;
977                         while (!(*p)->display_to_user() && p != _processors.begin()) {
978                                 --p;
979                         }
980                         ++p;
981                         iter = p;
982                 }
983
984                 return (add_processor (processor, iter) == 0);
985         }
986
987         catch (failed_constructor &err) {
988                 warning << _("processor could not be created. Ignored.") << endmsg;
989                 return false;
990         }
991 }
992
993 int
994 Route::add_processors (const ProcessorList& others, boost::shared_ptr<Processor> before, ProcessorStreams* err)
995 {
996         ProcessorList::iterator loc;
997
998         if (before) {
999                 loc = find(_processors.begin(), _processors.end(), before);
1000         } else {
1001                 /* nothing specified - at end but before main outs */
1002                 loc = find (_processors.begin(), _processors.end(), _main_outs);
1003         }
1004
1005         return add_processors (others, loc, err);
1006 }
1007
1008 int
1009 Route::add_processors (const ProcessorList& others, ProcessorList::iterator iter, ProcessorStreams* err)
1010 {
1011         /* NOTE: this is intended to be used ONLY when copying
1012            processors from another Route. Hence the subtle
1013            differences between this and ::add_processor()
1014         */
1015
1016         ChanCount old_pms = processor_max_streams;
1017
1018         if (!_session.engine().connected()) {
1019                 return 1;
1020         }
1021
1022         if (others.empty()) {
1023                 return 0;
1024         }
1025
1026         {
1027                 Glib::RWLock::WriterLock lm (_processor_lock);
1028
1029                 ChanCount potential_max_streams = ChanCount::max (_input->n_ports(), _output->n_ports());
1030
1031                 for (ProcessorList::const_iterator i = others.begin(); i != others.end(); ++i) {
1032
1033                         // Ensure meter only appears in the list once
1034                         if (*i == _meter) {
1035                                 ProcessorList::iterator m = find(_processors.begin(), _processors.end(), *i);
1036                                 if (m != _processors.end()) {
1037                                         _processors.erase(m);
1038                                 }
1039                         }
1040
1041                         boost::shared_ptr<PluginInsert> pi;
1042
1043                         if ((pi = boost::dynamic_pointer_cast<PluginInsert>(*i)) != 0) {
1044                                 pi->set_count (1);
1045
1046                                 ChanCount m = max (pi->input_streams(), pi->output_streams());
1047
1048                                 if (m > potential_max_streams) {
1049                                         potential_max_streams = m;
1050                                 }
1051                         }
1052
1053                         ProcessorList::iterator inserted = _processors.insert (iter, *i);
1054
1055                         if ((*i)->active()) {
1056                                 (*i)->activate ();
1057                         }
1058
1059                         if (configure_processors_unlocked (err)) {
1060                                 _processors.erase (inserted);
1061                                 configure_processors_unlocked (0); // it worked before we tried to add it ...
1062                                 return -1;
1063                         }
1064
1065                         (*i)->ActiveChanged.connect_same_thread (*this, boost::bind (&Session::update_latency_compensation, &_session, false, false));
1066                 }
1067
1068                 _output->set_user_latency (0);
1069         }
1070
1071         processors_changed (RouteProcessorChange ()); /* EMIT SIGNAL */
1072
1073         return 0;
1074 }
1075
1076 void
1077 Route::placement_range(Placement p, ProcessorList::iterator& start, ProcessorList::iterator& end)
1078 {
1079         if (p == PreFader) {
1080                 start = _processors.begin();
1081                 end = find(_processors.begin(), _processors.end(), _amp);
1082         } else {
1083                 start = find(_processors.begin(), _processors.end(), _amp);
1084                 ++start;
1085                 end = _processors.end();
1086         }
1087 }
1088
1089 /** Turn off all processors with a given placement
1090  * @param p Placement of processors to disable
1091  */
1092 void
1093 Route::disable_processors (Placement p)
1094 {
1095         Glib::RWLock::ReaderLock lm (_processor_lock);
1096
1097         ProcessorList::iterator start, end;
1098         placement_range(p, start, end);
1099
1100         for (ProcessorList::iterator i = start; i != end; ++i) {
1101                 (*i)->deactivate ();
1102         }
1103
1104         _session.set_dirty ();
1105 }
1106
1107 /** Turn off all redirects
1108  */
1109 void
1110 Route::disable_processors ()
1111 {
1112         Glib::RWLock::ReaderLock lm (_processor_lock);
1113
1114         for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1115                 (*i)->deactivate ();
1116         }
1117
1118         _session.set_dirty ();
1119 }
1120
1121 /** Turn off all redirects with a given placement
1122  * @param p Placement of redirects to disable
1123  */
1124 void
1125 Route::disable_plugins (Placement p)
1126 {
1127         Glib::RWLock::ReaderLock lm (_processor_lock);
1128
1129         ProcessorList::iterator start, end;
1130         placement_range(p, start, end);
1131
1132         for (ProcessorList::iterator i = start; i != end; ++i) {
1133                 if (boost::dynamic_pointer_cast<PluginInsert> (*i)) {
1134                         (*i)->deactivate ();
1135                 }
1136         }
1137
1138         _session.set_dirty ();
1139 }
1140
1141 /** Turn off all plugins
1142  */
1143 void
1144 Route::disable_plugins ()
1145 {
1146         Glib::RWLock::ReaderLock lm (_processor_lock);
1147
1148         for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1149                 if (boost::dynamic_pointer_cast<PluginInsert> (*i)) {
1150                         (*i)->deactivate ();
1151                 }
1152         }
1153
1154         _session.set_dirty ();
1155 }
1156
1157
1158 void
1159 Route::ab_plugins (bool forward)
1160 {
1161         Glib::RWLock::ReaderLock lm (_processor_lock);
1162
1163         if (forward) {
1164
1165                 /* forward = turn off all active redirects, and mark them so that the next time
1166                    we go the other way, we will revert them
1167                 */
1168
1169                 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1170                         if (!boost::dynamic_pointer_cast<PluginInsert> (*i)) {
1171                                 continue;
1172                         }
1173
1174                         if ((*i)->active()) {
1175                                 (*i)->deactivate ();
1176                                 (*i)->set_next_ab_is_active (true);
1177                         } else {
1178                                 (*i)->set_next_ab_is_active (false);
1179                         }
1180                 }
1181
1182         } else {
1183
1184                 /* backward = if the redirect was marked to go active on the next ab, do so */
1185
1186                 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1187
1188                         if (!boost::dynamic_pointer_cast<PluginInsert> (*i)) {
1189                                 continue;
1190                         }
1191
1192                         if ((*i)->get_next_ab_is_active()) {
1193                                 (*i)->activate ();
1194                         } else {
1195                                 (*i)->deactivate ();
1196                         }
1197                 }
1198         }
1199
1200         _session.set_dirty ();
1201 }
1202
1203
1204 /** Remove processors with a given placement.
1205  * @param p Placement of processors to remove.
1206  */
1207 void
1208 Route::clear_processors (Placement p)
1209 {
1210         const ChanCount old_pms = processor_max_streams;
1211
1212         if (!_session.engine().connected()) {
1213                 return;
1214         }
1215
1216         bool already_deleting = _session.deletion_in_progress();
1217         if (!already_deleting) {
1218                 _session.set_deletion_in_progress();
1219         }
1220
1221         {
1222                 Glib::RWLock::WriterLock lm (_processor_lock);
1223                 ProcessorList new_list;
1224                 ProcessorStreams err;
1225                 bool seen_amp = false;
1226
1227                 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1228
1229                         if (*i == _amp) {
1230                                 seen_amp = true;
1231                         }
1232
1233                         if ((*i) == _amp || (*i) == _meter || (*i) == _main_outs) {
1234
1235                                 /* you can't remove these */
1236
1237                                 new_list.push_back (*i);
1238
1239                         } else {
1240                                 if (seen_amp) {
1241
1242                                         switch (p) {
1243                                         case PreFader:
1244                                                 new_list.push_back (*i);
1245                                                 break;
1246                                         case PostFader:
1247                                                 (*i)->drop_references ();
1248                                                 break;
1249                                         }
1250
1251                                 } else {
1252
1253                                         switch (p) {
1254                                         case PreFader:
1255                                                 (*i)->drop_references ();
1256                                                 break;
1257                                         case PostFader:
1258                                                 new_list.push_back (*i);
1259                                                 break;
1260                                         }
1261                                 }
1262                         }
1263                 }
1264
1265                 _processors = new_list;
1266                 configure_processors_unlocked (&err); // this can't fail
1267         }
1268
1269         processor_max_streams.reset();
1270         _have_internal_generator = false;
1271         processors_changed (RouteProcessorChange ()); /* EMIT SIGNAL */
1272
1273         if (!already_deleting) {
1274                 _session.clear_deletion_in_progress();
1275         }
1276 }
1277
1278 int
1279 Route::remove_processor (boost::shared_ptr<Processor> processor, ProcessorStreams* err)
1280 {
1281         /* these can never be removed */
1282
1283         if (processor == _amp || processor == _meter || processor == _main_outs) {
1284                 return 0;
1285         }
1286
1287         ChanCount old_pms = processor_max_streams;
1288
1289         if (!_session.engine().connected()) {
1290                 return 1;
1291         }
1292
1293         processor_max_streams.reset();
1294
1295         {
1296                 Glib::RWLock::WriterLock lm (_processor_lock);
1297                 ProcessorList::iterator i;
1298                 bool removed = false;
1299
1300                 for (i = _processors.begin(); i != _processors.end(); ) {
1301                         if (*i == processor) {
1302
1303                                 /* move along, see failure case for configure_processors()
1304                                    where we may need to reconfigure the processor.
1305                                 */
1306
1307                                 /* stop redirects that send signals to JACK ports
1308                                    from causing noise as a result of no longer being
1309                                    run.
1310                                 */
1311
1312                                 boost::shared_ptr<IOProcessor> iop;
1313
1314                                 if ((iop = boost::dynamic_pointer_cast<IOProcessor> (*i)) != 0) {
1315                                         if (iop->input()) {
1316                                                 iop->input()->disconnect (this);
1317                                         }
1318                                         if (iop->output()) {
1319                                                 iop->output()->disconnect (this);
1320                                         }
1321                                 }
1322
1323                                 i = _processors.erase (i);
1324                                 removed = true;
1325                                 break;
1326
1327                         } else {
1328                                 ++i;
1329                         }
1330
1331                         _output->set_user_latency (0);
1332                 }
1333
1334                 if (!removed) {
1335                         /* what? */
1336                         return 1;
1337                 }
1338
1339                 if (configure_processors_unlocked (err)) {
1340                         /* get back to where we where */
1341                         _processors.insert (i, processor);
1342                         /* we know this will work, because it worked before :) */
1343                         configure_processors_unlocked (0);
1344                         return -1;
1345                 }
1346
1347                 _have_internal_generator = false;
1348
1349                 for (i = _processors.begin(); i != _processors.end(); ++i) {
1350                         boost::shared_ptr<PluginInsert> pi;
1351
1352                         if ((pi = boost::dynamic_pointer_cast<PluginInsert>(*i)) != 0) {
1353                                 if (pi->is_generator()) {
1354                                         _have_internal_generator = true;
1355                                         break;
1356                                 }
1357                         }
1358                 }
1359         }
1360
1361         processor->drop_references ();
1362         processors_changed (RouteProcessorChange ()); /* EMIT SIGNAL */
1363
1364         return 0;
1365 }
1366
1367 int
1368 Route::remove_processors (const ProcessorList& to_be_deleted, ProcessorStreams* err)
1369 {
1370         ProcessorList deleted;
1371         ProcessorList as_we_were;
1372
1373         if (!_session.engine().connected()) {
1374                 return 1;
1375         }
1376
1377         processor_max_streams.reset();
1378
1379         {
1380                 Glib::RWLock::WriterLock lm (_processor_lock);
1381                 ProcessorList::iterator i;
1382                 boost::shared_ptr<Processor> processor;
1383
1384                 as_we_were = _processors;
1385
1386                 for (i = _processors.begin(); i != _processors.end(); ) {
1387
1388                         processor = *i;
1389
1390                         /* these can never be removed */
1391
1392                         if (processor == _amp || processor == _meter || processor == _main_outs) {
1393                                 ++i;
1394                                 continue;
1395                         }
1396
1397                         /* see if its in the list of processors to delete */
1398
1399                         if (find (to_be_deleted.begin(), to_be_deleted.end(), processor) == to_be_deleted.end()) {
1400                                 ++i;
1401                                 continue;
1402                         }
1403
1404                         /* stop IOProcessors that send to JACK ports
1405                            from causing noise as a result of no longer being
1406                            run.
1407                         */
1408
1409                         boost::shared_ptr<IOProcessor> iop;
1410
1411                         if ((iop = boost::dynamic_pointer_cast<IOProcessor> (processor)) != 0) {
1412                                 iop->disconnect ();
1413                         }
1414
1415                         deleted.push_back (processor);
1416                         i = _processors.erase (i);
1417                 }
1418
1419                 if (deleted.empty()) {
1420                         /* none of those in the requested list were found */
1421                         return 0;
1422                 }
1423
1424                 _output->set_user_latency (0);
1425
1426                 if (configure_processors_unlocked (err)) {
1427                         /* get back to where we where */
1428                         _processors = as_we_were;
1429                         /* we know this will work, because it worked before :) */
1430                         configure_processors_unlocked (0);
1431                         return -1;
1432                 }
1433
1434                 _have_internal_generator = false;
1435
1436                 for (i = _processors.begin(); i != _processors.end(); ++i) {
1437                         boost::shared_ptr<PluginInsert> pi;
1438
1439                         if ((pi = boost::dynamic_pointer_cast<PluginInsert>(*i)) != 0) {
1440                                 if (pi->is_generator()) {
1441                                         _have_internal_generator = true;
1442                                         break;
1443                                 }
1444                         }
1445                 }
1446         }
1447
1448         /* now try to do what we need to so that those that were removed will be deleted */
1449
1450         for (ProcessorList::iterator i = deleted.begin(); i != deleted.end(); ++i) {
1451                 (*i)->drop_references ();
1452         }
1453
1454         processors_changed (RouteProcessorChange ()); /* EMIT SIGNAL */
1455
1456         return 0;
1457 }
1458
1459
1460 int
1461 Route::configure_processors (ProcessorStreams* err)
1462 {
1463         if (!_in_configure_processors) {
1464                 Glib::RWLock::WriterLock lm (_processor_lock);
1465                 return configure_processors_unlocked (err);
1466         }
1467         return 0;
1468 }
1469
1470 /** Configure the input/output configuration of each processor in the processors list.
1471  * Return 0 on success, otherwise configuration is impossible.
1472  */
1473 int
1474 Route::configure_processors_unlocked (ProcessorStreams* err)
1475 {
1476         if (_in_configure_processors) {
1477            return 0;
1478         }
1479
1480         _in_configure_processors = true;
1481
1482         // Check each processor in order to see if we can configure as requested
1483         ChanCount in = _input->n_ports ();
1484         ChanCount out;
1485         list< pair<ChanCount,ChanCount> > configuration;
1486         uint32_t index = 0;
1487
1488         DEBUG_TRACE (DEBUG::Processors, string_compose ("%1: configure processors\n", _name));
1489 #ifndef NDEBUG
1490         DEBUG_TRACE (DEBUG::Processors, "{\n");
1491         for (list<boost::shared_ptr<Processor> >::const_iterator p = _processors.begin(); p != _processors.end(); ++p) {
1492                 DEBUG_TRACE (DEBUG::Processors, string_compose ("\t%1 ID = %2\n", (*p)->name(), (*p)->id()));
1493         }
1494         DEBUG_TRACE (DEBUG::Processors, "}\n");
1495 #endif
1496
1497         for (ProcessorList::iterator p = _processors.begin(); p != _processors.end(); ++p, ++index) {
1498
1499                 if ((*p)->can_support_io_configuration(in, out)) {
1500                         DEBUG_TRACE (DEBUG::Processors, string_compose ("\t%1in = %2 out = %3\n",(*p)->name(), in, out));
1501                         configuration.push_back(make_pair(in, out));
1502                         in = out;
1503                 } else {
1504                         if (err) {
1505                                 err->index = index;
1506                                 err->count = in;
1507                         }
1508                         _in_configure_processors = false;
1509                         return -1;
1510                 }
1511         }
1512
1513         // We can, so configure everything
1514         list< pair<ChanCount,ChanCount> >::iterator c = configuration.begin();
1515         for (ProcessorList::iterator p = _processors.begin(); p != _processors.end(); ++p, ++c) {
1516                 (*p)->configure_io(c->first, c->second);
1517                 processor_max_streams = ChanCount::max(processor_max_streams, c->first);
1518                 processor_max_streams = ChanCount::max(processor_max_streams, c->second);
1519                 out = c->second;
1520         }
1521
1522         if (_meter) {
1523                 _meter->reset_max_channels (processor_max_streams);
1524         }
1525
1526         /* make sure we have sufficient scratch buffers to cope with the new processor
1527            configuration */
1528         _session.ensure_buffers (n_process_buffers ());
1529
1530         _in_configure_processors = false;
1531         return 0;
1532 }
1533
1534 void
1535 Route::all_processors_flip ()
1536 {
1537         Glib::RWLock::ReaderLock lm (_processor_lock);
1538
1539         if (_processors.empty()) {
1540                 return;
1541         }
1542
1543         bool first_is_on = _processors.front()->active();
1544
1545         for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1546                 if (first_is_on) {
1547                         (*i)->deactivate ();
1548                 } else {
1549                         (*i)->activate ();
1550                 }
1551         }
1552
1553         _session.set_dirty ();
1554 }
1555
1556 /** Set all processors with a given placement to a given active state.
1557  * @param p Placement of processors to change.
1558  * @param state New active state for those processors.
1559  */
1560 void
1561 Route::all_processors_active (Placement p, bool state)
1562 {
1563         Glib::RWLock::ReaderLock lm (_processor_lock);
1564
1565         if (_processors.empty()) {
1566                 return;
1567         }
1568         ProcessorList::iterator start, end;
1569         placement_range(p, start, end);
1570
1571         bool before_amp = true;
1572         for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1573                 if ((*i) == _amp) {
1574                         before_amp = false;
1575                         continue;
1576                 }
1577                 if (p == PreFader && before_amp) {
1578                         if (state) {
1579                                 (*i)->activate ();
1580                         } else {
1581                                 (*i)->deactivate ();
1582                         }
1583                 }
1584         }
1585
1586         _session.set_dirty ();
1587 }
1588
1589 bool
1590 Route::processor_is_prefader (boost::shared_ptr<Processor> p)
1591 {
1592         bool pre_fader = true;
1593         Glib::RWLock::ReaderLock lm (_processor_lock);
1594
1595         for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
1596
1597                 /* semantic note: if p == amp, we want to return true, so test
1598                    for equality before checking if this is the amp
1599                 */
1600
1601                 if ((*i) == p) {
1602                         break;
1603                 }
1604
1605                 if ((*i) == _amp) {
1606                         pre_fader = false;
1607                         break;
1608                 }
1609         }
1610
1611         return pre_fader;
1612 }
1613
1614 int
1615 Route::reorder_processors (const ProcessorList& new_order, ProcessorStreams* err)
1616 {
1617         /* "new_order" is an ordered list of processors to be positioned according to "placement".
1618            NOTE: all processors in "new_order" MUST be marked as display_to_user(). There maybe additional
1619            processors in the current actual processor list that are hidden. Any visible processors
1620            in the current list but not in "new_order" will be assumed to be deleted.
1621         */
1622
1623         {
1624                 Glib::RWLock::WriterLock lm (_processor_lock);
1625                 ChanCount old_pms = processor_max_streams;
1626                 ProcessorList::iterator oiter;
1627                 ProcessorList::const_iterator niter;
1628                 ProcessorList as_it_was_before = _processors;
1629                 ProcessorList as_it_will_be;
1630
1631                 oiter = _processors.begin();
1632                 niter = new_order.begin();
1633
1634                 while (niter !=  new_order.end()) {
1635
1636                         /* if the next processor in the old list is invisible (i.e. should not be in the new order)
1637                            then append it to the temp list.
1638
1639                            Otherwise, see if the next processor in the old list is in the new list. if not,
1640                            its been deleted. If its there, append it to the temp list.
1641                         */
1642
1643                         if (oiter == _processors.end()) {
1644
1645                                 /* no more elements in the old list, so just stick the rest of
1646                                    the new order onto the temp list.
1647                                 */
1648
1649                                 as_it_will_be.insert (as_it_will_be.end(), niter, new_order.end());
1650                                 while (niter != new_order.end()) {
1651                                         ++niter;
1652                                 }
1653                                 break;
1654
1655                         } else {
1656
1657                                 if (!(*oiter)->display_to_user()) {
1658
1659                                         as_it_will_be.push_back (*oiter);
1660
1661                                 } else {
1662
1663                                         /* visible processor: check that its in the new order */
1664
1665                                         if (find (new_order.begin(), new_order.end(), (*oiter)) == new_order.end()) {
1666                                                 /* deleted: do nothing, shared_ptr<> will clean up */
1667                                         } else {
1668                                                 /* ignore this one, and add the next item from the new order instead */
1669                                                 as_it_will_be.push_back (*niter);
1670                                                 ++niter;
1671                                         }
1672                                 }
1673
1674                                 /* now remove from old order - its taken care of no matter what */
1675                                 oiter = _processors.erase (oiter);
1676                         }
1677
1678                 }
1679
1680                 _processors.insert (oiter, as_it_will_be.begin(), as_it_will_be.end());
1681
1682                 if (configure_processors_unlocked (err)) {
1683                         _processors = as_it_was_before;
1684                         processor_max_streams = old_pms;
1685                         return -1;
1686                 }
1687         }
1688
1689         processors_changed (RouteProcessorChange ()); /* EMIT SIGNAL */
1690
1691         return 0;
1692 }
1693
1694 XMLNode&
1695 Route::get_state()
1696 {
1697         return state(true);
1698 }
1699
1700 XMLNode&
1701 Route::get_template()
1702 {
1703         return state(false);
1704 }
1705
1706 XMLNode&
1707 Route::state(bool full_state)
1708 {
1709         XMLNode *node = new XMLNode("Route");
1710         ProcessorList::iterator i;
1711         char buf[32];
1712
1713         id().print (buf, sizeof (buf));
1714         node->add_property("id", buf);
1715         node->add_property ("name", _name);
1716         node->add_property("default-type", _default_type.to_string());
1717
1718         if (_flags) {
1719                 node->add_property("flags", enum_2_string (_flags));
1720         }
1721
1722         node->add_property("active", _active?"yes":"no");
1723         node->add_property("phase-invert", _phase_invert?"yes":"no");
1724         node->add_property("denormal-protection", _denormal_protection?"yes":"no");
1725         node->add_property("meter-point", enum_2_string (_meter_point));
1726
1727         if (_route_group) {
1728                 node->add_property("route-group", _route_group->name());
1729         }
1730
1731         string order_string;
1732         OrderKeys::iterator x = order_keys.begin();
1733
1734         while (x != order_keys.end()) {
1735                 order_string += string ((*x).first);
1736                 order_string += '=';
1737                 snprintf (buf, sizeof(buf), "%ld", (*x).second);
1738                 order_string += buf;
1739
1740                 ++x;
1741
1742                 if (x == order_keys.end()) {
1743                         break;
1744                 }
1745
1746                 order_string += ':';
1747         }
1748         node->add_property ("order-keys", order_string);
1749         node->add_property ("self-solo", (_self_solo ? "yes" : "no"));
1750         snprintf (buf, sizeof (buf), "%d", _soloed_by_others);
1751         node->add_property ("soloed-by-others", buf);
1752
1753         node->add_child_nocopy (_input->state (full_state));
1754         node->add_child_nocopy (_output->state (full_state));
1755         node->add_child_nocopy (_solo_control->get_state ());
1756         node->add_child_nocopy (_mute_master->get_state ());
1757
1758         XMLNode* remote_control_node = new XMLNode (X_("RemoteControl"));
1759         snprintf (buf, sizeof (buf), "%d", _remote_control_id);
1760         remote_control_node->add_property (X_("id"), buf);
1761         node->add_child_nocopy (*remote_control_node);
1762
1763         if (_comment.length()) {
1764                 XMLNode *cmt = node->add_child ("Comment");
1765                 cmt->add_content (_comment);
1766         }
1767
1768         for (i = _processors.begin(); i != _processors.end(); ++i) {
1769                 node->add_child_nocopy((*i)->state (full_state));
1770         }
1771
1772         if (_extra_xml){
1773                 node->add_child_copy (*_extra_xml);
1774         }
1775
1776         return *node;
1777 }
1778
1779 int
1780 Route::set_state (const XMLNode& node, int version)
1781 {
1782         return _set_state (node, version, true);
1783 }
1784
1785 int
1786 Route::_set_state (const XMLNode& node, int version, bool /*call_base*/)
1787 {
1788         if (version < 3000) {
1789                 return _set_state_2X (node, version);
1790         }
1791
1792         XMLNodeList nlist;
1793         XMLNodeConstIterator niter;
1794         XMLNode *child;
1795         XMLPropertyList plist;
1796         const XMLProperty *prop;
1797
1798         if (node.name() != "Route"){
1799                 error << string_compose(_("Bad node sent to Route::set_state() [%1]"), node.name()) << endmsg;
1800                 return -1;
1801         }
1802
1803         if ((prop = node.property (X_("name"))) != 0) {
1804                 Route::set_name (prop->value());
1805         }
1806
1807         if ((prop = node.property ("id")) != 0) {
1808                 _id = prop->value ();
1809         }
1810
1811         if ((prop = node.property (X_("flags"))) != 0) {
1812                 _flags = Flag (string_2_enum (prop->value(), _flags));
1813         } else {
1814                 _flags = Flag (0);
1815         }
1816
1817         /* add all processors (except amp, which is always present) */
1818
1819         nlist = node.children();
1820         XMLNode processor_state (X_("processor_state"));
1821
1822         for (niter = nlist.begin(); niter != nlist.end(); ++niter){
1823
1824                 child = *niter;
1825
1826                 if (child->name() == IO::state_node_name) {
1827                         if ((prop = child->property (X_("direction"))) == 0) {
1828                                 continue;
1829                         }
1830
1831                         if (prop->value() == "Input") {
1832                                 _input->set_state (*child, version);
1833                         } else if (prop->value() == "Output") {
1834                                 _output->set_state (*child, version);
1835                         }
1836                 }
1837
1838                 if (child->name() == X_("Processor")) {
1839                         processor_state.add_child_copy (*child);
1840                 }
1841         }
1842
1843         set_processor_state (processor_state);
1844
1845         if ((prop = node.property ("self-solo")) != 0) {
1846                 set_self_solo (string_is_affirmative (prop->value()));
1847         }
1848
1849         if ((prop = node.property ("soloed-by-others")) != 0) {
1850                 _soloed_by_others = 0; // needed for mod_solo_by_others () to work
1851                 mod_solo_by_others (atoi (prop->value()));
1852         }
1853
1854         if ((prop = node.property ("solo-isolated")) != 0) {
1855                 set_solo_isolated (string_is_affirmative (prop->value()), this);
1856         }
1857
1858         if ((prop = node.property (X_("phase-invert"))) != 0) {
1859                 set_phase_invert (string_is_affirmative (prop->value()));
1860         }
1861
1862         if ((prop = node.property (X_("denormal-protection"))) != 0) {
1863                 set_denormal_protection (string_is_affirmative (prop->value()));
1864         }
1865
1866         if ((prop = node.property (X_("active"))) != 0) {
1867                 bool yn = string_is_affirmative (prop->value());
1868                 _active = !yn; // force switch
1869                 set_active (yn);
1870         }
1871
1872         if ((prop = node.property (X_("meter-point"))) != 0) {
1873                 _meter_point = MeterPoint (string_2_enum (prop->value (), _meter_point));
1874                 if (_meter) {
1875                         _meter->set_display_to_user (_meter_point == MeterCustom);
1876                 }
1877         }
1878
1879         if ((prop = node.property (X_("order-keys"))) != 0) {
1880
1881                 long n;
1882
1883                 string::size_type colon, equal;
1884                 string remaining = prop->value();
1885
1886                 while (remaining.length()) {
1887
1888                         if ((equal = remaining.find_first_of ('=')) == string::npos || equal == remaining.length()) {
1889                                 error << string_compose (_("badly formed order key string in state file! [%1] ... ignored."), remaining)
1890                                       << endmsg;
1891                         } else {
1892                                 if (sscanf (remaining.substr (equal+1).c_str(), "%ld", &n) != 1) {
1893                                         error << string_compose (_("badly formed order key string in state file! [%1] ... ignored."), remaining)
1894                                               << endmsg;
1895                                 } else {
1896                                         set_order_key (remaining.substr (0, equal), n);
1897                                 }
1898                         }
1899
1900                         colon = remaining.find_first_of (':');
1901
1902                         if (colon != string::npos) {
1903                                 remaining = remaining.substr (colon+1);
1904                         } else {
1905                                 break;
1906                         }
1907                 }
1908         }
1909
1910         for (niter = nlist.begin(); niter != nlist.end(); ++niter){
1911                 child = *niter;
1912
1913                 if (child->name() == X_("Comment")) {
1914
1915                         /* XXX this is a terrible API design in libxml++ */
1916
1917                         XMLNode *cmt = *(child->children().begin());
1918                         _comment = cmt->content();
1919
1920                 } else if (child->name() == X_("Extra")) {
1921
1922                         _extra_xml = new XMLNode (*child);
1923
1924                 } else if (child->name() == X_("Controllable") && (prop = child->property("name")) != 0) {
1925
1926                         if (prop->value() == "solo") {
1927                                 _solo_control->set_state (*child, version);
1928                                 _session.add_controllable (_solo_control);
1929                         }
1930
1931                 } else if (child->name() == X_("RemoteControl")) {
1932                         if ((prop = child->property (X_("id"))) != 0) {
1933                                 int32_t x;
1934                                 sscanf (prop->value().c_str(), "%d", &x);
1935                                 set_remote_control_id (x);
1936                         }
1937
1938                 } else if (child->name() == X_("MuteMaster")) {
1939                         _mute_master->set_state (*child, version);
1940                 }
1941         }
1942
1943         return 0;
1944 }
1945
1946 int
1947 Route::_set_state_2X (const XMLNode& node, int version)
1948 {
1949         XMLNodeList nlist;
1950         XMLNodeConstIterator niter;
1951         XMLNode *child;
1952         XMLPropertyList plist;
1953         const XMLProperty *prop;
1954
1955         /* 2X things which still remain to be handled:
1956          * default-type
1957          * muted
1958          * mute-affects-pre-fader
1959          * mute-affects-post-fader
1960          * mute-affects-control-outs
1961          * mute-affects-main-outs
1962          * automation
1963          * controlouts
1964          */
1965
1966         if (node.name() != "Route") {
1967                 error << string_compose(_("Bad node sent to Route::set_state() [%1]"), node.name()) << endmsg;
1968                 return -1;
1969         }
1970
1971         if ((prop = node.property (X_("flags"))) != 0) {
1972                 _flags = Flag (string_2_enum (prop->value(), _flags));
1973         } else {
1974                 _flags = Flag (0);
1975         }
1976
1977         /* add standard processors */
1978
1979         _meter.reset (new PeakMeter (_session));
1980         add_processor (_meter, PreFader);
1981
1982         if (_flags & ControlOut) {
1983                 /* where we listen to tracks */
1984                 _intreturn.reset (new InternalReturn (_session));
1985                 add_processor (_intreturn, PreFader);
1986         }
1987
1988         _main_outs.reset (new Delivery (_session, _output, _mute_master, _name, Delivery::Main));
1989         add_processor (_main_outs, PostFader);
1990
1991         /* IOs */
1992
1993         nlist = node.children ();
1994         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
1995
1996                 child = *niter;
1997
1998                 if (child->name() == IO::state_node_name) {
1999
2000                         /* there is a note in IO::set_state_2X() about why we have to call
2001                            this directly.
2002                            */
2003
2004                         _input->set_state_2X (*child, version, true);
2005                         _output->set_state_2X (*child, version, false);
2006
2007                         if ((prop = child->property (X_("name"))) != 0) {
2008                                 set_name (prop->value ());
2009                         }
2010
2011                         if ((prop = child->property (X_("id"))) != 0) {
2012                                 _id = prop->value ();
2013                         }
2014
2015                         if ((prop = child->property (X_("active"))) != 0) {
2016                                 bool yn = string_is_affirmative (prop->value());
2017                                 _active = !yn; // force switch
2018                                 set_active (yn);
2019                         }
2020                 }
2021
2022                 /* XXX: panners? */
2023         }
2024
2025         if ((prop = node.property (X_("phase-invert"))) != 0) {
2026                 set_phase_invert (string_is_affirmative (prop->value()));
2027         }
2028
2029         if ((prop = node.property (X_("denormal-protection"))) != 0) {
2030                 set_denormal_protection (string_is_affirmative (prop->value()));
2031         }
2032
2033         if ((prop = node.property (X_("soloed"))) != 0) {
2034                 bool yn = string_is_affirmative (prop->value());
2035
2036                 /* XXX force reset of solo status */
2037
2038                 set_solo (yn, this);
2039         }
2040
2041         if ((prop = node.property (X_("meter-point"))) != 0) {
2042                 _meter_point = MeterPoint (string_2_enum (prop->value (), _meter_point));
2043         }
2044
2045         /* do not carry over edit/mix groups from 2.X because (a) its hard (b) they
2046            don't mean the same thing.
2047         */
2048
2049         if ((prop = node.property (X_("order-keys"))) != 0) {
2050
2051                 long n;
2052
2053                 string::size_type colon, equal;
2054                 string remaining = prop->value();
2055
2056                 while (remaining.length()) {
2057
2058                         if ((equal = remaining.find_first_of ('=')) == string::npos || equal == remaining.length()) {
2059                                 error << string_compose (_("badly formed order key string in state file! [%1] ... ignored."), remaining)
2060                                         << endmsg;
2061                         } else {
2062                                 if (sscanf (remaining.substr (equal+1).c_str(), "%ld", &n) != 1) {
2063                                         error << string_compose (_("badly formed order key string in state file! [%1] ... ignored."), remaining)
2064                                                 << endmsg;
2065                                 } else {
2066                                         set_order_key (remaining.substr (0, equal), n);
2067                                 }
2068                         }
2069
2070                         colon = remaining.find_first_of (':');
2071
2072                         if (colon != string::npos) {
2073                                 remaining = remaining.substr (colon+1);
2074                         } else {
2075                                 break;
2076                         }
2077                 }
2078         }
2079
2080         XMLNodeList redirect_nodes;
2081
2082         for (niter = nlist.begin(); niter != nlist.end(); ++niter){
2083
2084                 child = *niter;
2085
2086                 if (child->name() == X_("Send") || child->name() == X_("Insert")) {
2087                         redirect_nodes.push_back(child);
2088                 }
2089
2090         }
2091
2092         set_processor_state_2X (redirect_nodes, version);
2093
2094         for (niter = nlist.begin(); niter != nlist.end(); ++niter){
2095                 child = *niter;
2096
2097                 if (child->name() == X_("Comment")) {
2098
2099                         /* XXX this is a terrible API design in libxml++ */
2100
2101                         XMLNode *cmt = *(child->children().begin());
2102                         _comment = cmt->content();
2103
2104                 } else if (child->name() == X_("Extra")) {
2105
2106                         _extra_xml = new XMLNode (*child);
2107
2108                 } else if (child->name() == X_("Controllable") && (prop = child->property("name")) != 0) {
2109
2110                         if (prop->value() == "solo") {
2111                                 _solo_control->set_state (*child, version);
2112                                 _session.add_controllable (_solo_control);
2113                         }
2114
2115                 } else if (child->name() == X_("RemoteControl")) {
2116                         if ((prop = child->property (X_("id"))) != 0) {
2117                                 int32_t x;
2118                                 sscanf (prop->value().c_str(), "%d", &x);
2119                                 set_remote_control_id (x);
2120                         }
2121
2122                 } 
2123         }
2124
2125         return 0;
2126 }
2127
2128 XMLNode&
2129 Route::get_processor_state ()
2130 {
2131         XMLNode* root = new XMLNode (X_("redirects"));
2132         for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
2133                 root->add_child_nocopy ((*i)->state (true));
2134         }
2135
2136         return *root;
2137 }
2138
2139 void
2140 Route::set_processor_state_2X (XMLNodeList const & nList, int version)
2141 {
2142         /* We don't bother removing existing processors not in nList, as this
2143            method will only be called when creating a Route from scratch, not
2144            for undo purposes.  Just put processors in at the appropriate place
2145            in the list.
2146         */
2147
2148         for (XMLNodeConstIterator i = nList.begin(); i != nList.end(); ++i) {
2149                 add_processor_from_xml_2X (**i, version, _processors.begin ());
2150         }
2151 }
2152
2153 void
2154 Route::set_processor_state (const XMLNode& node)
2155 {
2156         const XMLNodeList &nlist = node.children();
2157         XMLNodeConstIterator niter;
2158         ProcessorList::iterator i, o;
2159
2160         // Iterate through existing processors, remove those which are not in the state list
2161
2162         for (i = _processors.begin(); i != _processors.end(); ) {
2163
2164                 /* leave amp alone, always */
2165
2166                 if ((*i) == _amp) {
2167                         ++i;
2168                         continue;
2169                 }
2170
2171                 ProcessorList::iterator tmp = i;
2172                 ++tmp;
2173
2174                 bool processorInStateList = false;
2175
2176                 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
2177
2178                         XMLProperty* id_prop = (*niter)->property(X_("id"));
2179
2180                         if (id_prop && (*i)->id() == id_prop->value()) {
2181                                 processorInStateList = true;
2182                                 break;
2183                         }
2184                 }
2185
2186                 if (!processorInStateList) {
2187                         remove_processor (*i);
2188                 }
2189
2190                 i = tmp;
2191         }
2192
2193         // Iterate through state list and make sure all processors are on the track and in the correct order,
2194         // set the state of existing processors according to the new state on the same go
2195
2196         i = _processors.begin();
2197
2198         for (niter = nlist.begin(); niter != nlist.end(); ++niter, ++i) {
2199
2200                 XMLProperty* prop = (*niter)->property ("type");
2201
2202                 o = i;
2203
2204                 // Check whether the next processor in the list is the right one,
2205                 // except for "amp" which is always there and may not have the
2206                 // old ID since it is always created anew in every Route
2207
2208                 if (prop->value() != "amp") {
2209                         while (o != _processors.end()) {
2210                                 XMLProperty* id_prop = (*niter)->property(X_("id"));
2211                                 if (id_prop && (*o)->id() == id_prop->value()) {
2212                                         break;
2213                                 }
2214
2215                                 ++o;
2216                         }
2217                 }
2218
2219                 // If the processor (*niter) is not on the route,
2220                 // create it and move it to the correct location
2221
2222                 if (o == _processors.end()) {
2223
2224                         if (add_processor_from_xml (**niter, i)) {
2225                                 --i; // move iterator to the newly inserted processor
2226                         } else {
2227                                 cerr << "Error restoring route: unable to restore processor" << endl;
2228                         }
2229
2230                 } else {
2231
2232                         // Otherwise, the processor already exists; just
2233                         // ensure it is at the location provided in the XML state
2234
2235                         if (i != o) {
2236                                 boost::shared_ptr<Processor> tmp = (*o);
2237                                 _processors.erase (o); // remove the old copy
2238                                 _processors.insert (i, tmp); // insert the processor at the correct location
2239                                 --i; // move iterator to the correct processor
2240                         }
2241
2242                         // and make it (just) so
2243
2244                         (*i)->set_state (**niter, Stateful::current_state_version);
2245                 }
2246         }
2247
2248         /* note: there is no configure_processors() call because we figure that
2249            the XML state represents a working signal route.
2250         */
2251
2252         processors_changed (RouteProcessorChange ());
2253 }
2254
2255 void
2256 Route::curve_reallocate ()
2257 {
2258 //      _gain_automation_curve.finish_resize ();
2259 //      _pan_automation_curve.finish_resize ();
2260 }
2261
2262 void
2263 Route::silence (nframes_t nframes)
2264 {
2265         if (!_silent) {
2266
2267                 _output->silence (nframes);
2268
2269                 {
2270                         Glib::RWLock::ReaderLock lm (_processor_lock, Glib::TRY_LOCK);
2271
2272                         if (lm.locked()) {
2273                                 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
2274                                         boost::shared_ptr<PluginInsert> pi;
2275
2276                                         if (!_active && (pi = boost::dynamic_pointer_cast<PluginInsert> (*i)) != 0) {
2277                                                 // skip plugins, they don't need anything when we're not active
2278                                                 continue;
2279                                         }
2280
2281                                         (*i)->silence (nframes);
2282                                 }
2283
2284                                 if (nframes == _session.get_block_size()) {
2285                                         // _silent = true;
2286                                 }
2287                         }
2288                 }
2289
2290         }
2291 }
2292
2293 void
2294 Route::add_internal_return ()
2295 {
2296         if (!_intreturn) {
2297                 _intreturn.reset (new InternalReturn (_session));
2298                 add_processor (_intreturn, PreFader);
2299         }
2300 }
2301
2302 BufferSet*
2303 Route::get_return_buffer () const
2304 {
2305         Glib::RWLock::ReaderLock rm (_processor_lock);
2306
2307         for (ProcessorList::const_iterator x = _processors.begin(); x != _processors.end(); ++x) {
2308                 boost::shared_ptr<InternalReturn> d = boost::dynamic_pointer_cast<InternalReturn>(*x);
2309
2310                 if (d) {
2311                         BufferSet* bs = d->get_buffers ();
2312                         return bs;
2313                 }
2314         }
2315
2316         return 0;
2317 }
2318
2319 void
2320 Route::release_return_buffer () const
2321 {
2322         Glib::RWLock::ReaderLock rm (_processor_lock);
2323
2324         for (ProcessorList::const_iterator x = _processors.begin(); x != _processors.end(); ++x) {
2325                 boost::shared_ptr<InternalReturn> d = boost::dynamic_pointer_cast<InternalReturn>(*x);
2326
2327                 if (d) {
2328                         return d->release_buffers ();
2329                 }
2330         }
2331 }
2332
2333 int
2334 Route::listen_via (boost::shared_ptr<Route> route, Placement placement, bool /*active*/, bool aux)
2335 {
2336         vector<string> ports;
2337         vector<string>::const_iterator i;
2338
2339         {
2340                 Glib::RWLock::ReaderLock rm (_processor_lock);
2341
2342                 for (ProcessorList::iterator x = _processors.begin(); x != _processors.end(); ++x) {
2343
2344                         boost::shared_ptr<InternalSend> d = boost::dynamic_pointer_cast<InternalSend>(*x);
2345
2346                         if (d && d->target_route() == route) {
2347
2348                                 /* if the target is the control outs, then make sure
2349                                    we take note of which i-send is doing that.
2350                                 */
2351
2352                                 if (route == _session.control_out()) {
2353                                         _control_outs = boost::dynamic_pointer_cast<Delivery>(d);
2354                                 }
2355
2356                                 /* already listening via the specified IO: do nothing */
2357
2358                                 return 0;
2359                         }
2360                 }
2361         }
2362
2363         boost::shared_ptr<InternalSend> listener;
2364
2365         try {
2366                 listener.reset (new InternalSend (_session, _mute_master, route, (aux ? Delivery::Aux : Delivery::Listen)));
2367
2368         } catch (failed_constructor& err) {
2369                 return -1;
2370         }
2371
2372         if (route == _session.control_out()) {
2373                 _control_outs = listener;
2374         }
2375
2376         add_processor (listener, placement);
2377
2378         return 0;
2379 }
2380
2381 void
2382 Route::drop_listen (boost::shared_ptr<Route> route)
2383 {
2384         ProcessorStreams err;
2385         ProcessorList::iterator tmp;
2386
2387         Glib::RWLock::ReaderLock rl(_processor_lock);
2388         rl.acquire ();
2389
2390   again:
2391         for (ProcessorList::iterator x = _processors.begin(); x != _processors.end(); ) {
2392
2393                 boost::shared_ptr<InternalSend> d = boost::dynamic_pointer_cast<InternalSend>(*x);
2394
2395                 if (d && d->target_route() == route) {
2396                         rl.release ();
2397                         remove_processor (*x, &err);
2398                         rl.acquire ();
2399
2400                         /* list could have been demolished while we dropped the lock
2401                            so start over.
2402                         */
2403
2404                         goto again;
2405                 }
2406         }
2407
2408         rl.release ();
2409
2410         if (route == _session.control_out()) {
2411                 _control_outs.reset ();
2412         }
2413 }
2414
2415 void
2416 Route::set_comment (string cmt, void *src)
2417 {
2418         _comment = cmt;
2419         comment_changed (src);
2420         _session.set_dirty ();
2421 }
2422
2423 bool
2424 Route::feeds (boost::shared_ptr<Route> other, bool* only_send)
2425 {
2426         DEBUG_TRACE (DEBUG::Graph, string_compose ("Feeds? %1\n", _name));
2427
2428         if (_output->connected_to (other->input())) {
2429                 DEBUG_TRACE (DEBUG::Graph, string_compose ("\tdirect FEEDS %2\n", other->name()));
2430                 if (only_send) {
2431                         *only_send = false;
2432                 }
2433
2434                 return true;
2435         }
2436
2437         
2438         for (ProcessorList::iterator r = _processors.begin(); r != _processors.end(); r++) {
2439
2440                 boost::shared_ptr<IOProcessor> iop;
2441
2442                 if ((iop = boost::dynamic_pointer_cast<IOProcessor>(*r)) != 0) {
2443                         if (iop->feeds (other)) {
2444                                 DEBUG_TRACE (DEBUG::Graph,  string_compose ("\tIOP %1 does feed %2\n", iop->name(), other->name()));
2445                                 if (only_send) {
2446                                         *only_send = true;
2447                                 }
2448                                 return true;
2449                         } else {
2450                                 DEBUG_TRACE (DEBUG::Graph,  string_compose ("\tIOP %1 does NOT feed %2\n", iop->name(), other->name()));
2451                         }
2452                 } else {
2453                         DEBUG_TRACE (DEBUG::Graph,  string_compose ("\tPROC %1 is not an IOP\n", (*r)->name()));
2454                 }
2455                         
2456         }
2457
2458         DEBUG_TRACE (DEBUG::Graph,  string_compose ("\tdoes NOT feed %1\n", other->name()));
2459         return false;
2460 }
2461
2462 void
2463 Route::handle_transport_stopped (bool /*abort_ignored*/, bool did_locate, bool can_flush_processors)
2464 {
2465         nframes_t now = _session.transport_frame();
2466
2467         {
2468                 Glib::RWLock::ReaderLock lm (_processor_lock);
2469
2470                 if (!did_locate) {
2471                         automation_snapshot (now, true);
2472                 }
2473
2474                 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
2475
2476                         if (Config->get_plugins_stop_with_transport() && can_flush_processors) {
2477                                 (*i)->deactivate ();
2478                                 (*i)->activate ();
2479                         }
2480
2481                         (*i)->transport_stopped (now);
2482                 }
2483         }
2484
2485         _roll_delay = _initial_delay;
2486 }
2487
2488 void
2489 Route::input_change_handler (IOChange change, void * /*src*/)
2490 {
2491         if ((change & ConfigurationChanged)) {
2492                 configure_processors (0);
2493         }
2494 }
2495
2496 void
2497 Route::output_change_handler (IOChange change, void * /*src*/)
2498 {
2499         if ((change & ConfigurationChanged)) {
2500
2501                 /* XXX resize all listeners to match _main_outs? */
2502
2503                 // configure_processors (0);
2504         }
2505 }
2506
2507 uint32_t
2508 Route::pans_required () const
2509 {
2510         if (n_outputs().n_audio() < 2) {
2511                 return 0;
2512         }
2513
2514         return max (n_inputs ().n_audio(), processor_max_streams.n_audio());
2515 }
2516
2517 int
2518 Route::no_roll (nframes_t nframes, sframes_t start_frame, sframes_t end_frame,
2519                 bool session_state_changing, bool /*can_record*/, bool /*rec_monitors_input*/)
2520 {
2521         if (n_outputs().n_total() == 0) {
2522                 return 0;
2523         }
2524
2525         if (session_state_changing || !_active || n_inputs() == ChanCount::ZERO)  {
2526                 silence (nframes);
2527                 return 0;
2528         }
2529
2530         _amp->apply_gain_automation (false);
2531         passthru (start_frame, end_frame, nframes, 0);
2532
2533         return 0;
2534 }
2535
2536 nframes_t
2537 Route::check_initial_delay (nframes_t nframes, nframes_t& transport_frame)
2538 {
2539         if (_roll_delay > nframes) {
2540
2541                 _roll_delay -= nframes;
2542                 silence (nframes);
2543                 /* transport frame is not legal for caller to use */
2544                 return 0;
2545
2546         } else if (_roll_delay > 0) {
2547
2548                 nframes -= _roll_delay;
2549                 silence (_roll_delay);
2550                 /* we've written _roll_delay of samples into the
2551                    output ports, so make a note of that for
2552                    future reference.
2553                 */
2554                 _main_outs->increment_output_offset (_roll_delay);
2555                 transport_frame += _roll_delay;
2556
2557                 _roll_delay = 0;
2558         }
2559
2560         return nframes;
2561 }
2562
2563 int
2564 Route::roll (nframes_t nframes, sframes_t start_frame, sframes_t end_frame, int declick,
2565              bool /*can_record*/, bool /*rec_monitors_input*/)
2566 {
2567         {
2568                 // automation snapshot can also be called from the non-rt context
2569                 // and it uses the processor list, so we try to acquire the lock here
2570                 Glib::RWLock::ReaderLock lm (_processor_lock, Glib::TRY_LOCK);
2571
2572                 if (lm.locked()) {
2573                         automation_snapshot (_session.transport_frame(), false);
2574                 }
2575         }
2576
2577         if (n_outputs().n_total() == 0) {
2578                 return 0;
2579         }
2580
2581         if (!_active || n_inputs().n_total() == 0) {
2582                 silence (nframes);
2583                 return 0;
2584         }
2585
2586         nframes_t unused = 0;
2587
2588         if ((nframes = check_initial_delay (nframes, unused)) == 0) {
2589                 return 0;
2590         }
2591
2592         _silent = false;
2593
2594         passthru (start_frame, end_frame, nframes, declick);
2595
2596         return 0;
2597 }
2598
2599 int
2600 Route::silent_roll (nframes_t nframes, sframes_t /*start_frame*/, sframes_t /*end_frame*/,
2601                     bool /*can_record*/, bool /*rec_monitors_input*/)
2602 {
2603         silence (nframes);
2604         return 0;
2605 }
2606
2607 void
2608 Route::toggle_monitor_input ()
2609 {
2610         for (PortSet::iterator i = _input->ports().begin(); i != _input->ports().end(); ++i) {
2611                 i->ensure_monitor_input( ! i->monitoring_input());
2612         }
2613 }
2614
2615 bool
2616 Route::has_external_redirects () const
2617 {
2618         // FIXME: what about sends? - they don't return a signal back to ardour?
2619
2620         boost::shared_ptr<const PortInsert> pi;
2621
2622         for (ProcessorList::const_iterator i = _processors.begin(); i != _processors.end(); ++i) {
2623
2624                 if ((pi = boost::dynamic_pointer_cast<const PortInsert>(*i)) != 0) {
2625
2626                         for (PortSet::const_iterator port = pi->output()->ports().begin(); port != pi->output()->ports().end(); ++port) {
2627
2628                                 string port_name = port->name();
2629                                 string client_name = port_name.substr (0, port_name.find(':'));
2630
2631                                 /* only say "yes" if the redirect is actually in use */
2632
2633                                 if (client_name != "ardour" && pi->active()) {
2634                                         return true;
2635                                 }
2636                         }
2637                 }
2638         }
2639
2640         return false;
2641 }
2642
2643 void
2644 Route::flush_processors ()
2645 {
2646         /* XXX shouldn't really try to take this lock, since
2647            this is called from the RT audio thread.
2648         */
2649
2650         Glib::RWLock::ReaderLock lm (_processor_lock);
2651
2652         for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
2653                 (*i)->deactivate ();
2654                 (*i)->activate ();
2655         }
2656 }
2657
2658 void
2659 Route::set_meter_point (MeterPoint p, void *src)
2660 {
2661         /* CAN BE CALLED FROM PROCESS CONTEXT */
2662
2663         if (_meter_point == p) {
2664                 return;
2665         }
2666
2667         bool meter_was_visible_to_user = _meter->display_to_user ();
2668
2669         {
2670                 Glib::RWLock::WriterLock lm (_processor_lock);
2671         
2672                 if (p != MeterCustom) {
2673                         // Move meter in the processors list to reflect the new position
2674                         ProcessorList::iterator loc = find (_processors.begin(), _processors.end(), _meter);
2675                         _processors.erase(loc);
2676                         switch (p) {
2677                         case MeterInput:
2678                                 loc = _processors.begin();
2679                                 break;
2680                         case MeterPreFader:
2681                                 loc = find (_processors.begin(), _processors.end(), _amp);
2682                                 break;
2683                         case MeterPostFader:
2684                                 loc = _processors.end();
2685                                 break;
2686                         default:
2687                                 break;
2688                         }
2689                         
2690                         ChanCount m_in;
2691                         
2692                         if (loc == _processors.begin()) {
2693                                 m_in = _input->n_ports();
2694                         } else {
2695                                 ProcessorList::iterator before = loc;
2696                                 --before;
2697                                 m_in = (*before)->output_streams ();
2698                         }
2699                         
2700                         _meter->reflect_inputs (m_in);
2701                         
2702                         _processors.insert (loc, _meter);
2703                         
2704                         /* we do not need to reconfigure the processors, because the meter
2705                            (a) is always ready to handle processor_max_streams
2706                            (b) is always an N-in/N-out processor, and thus moving
2707                            it doesn't require any changes to the other processors.
2708                         */
2709                         
2710                         _meter->set_display_to_user (false);
2711                         
2712                 } else {
2713                         
2714                         // just make it visible and let the user move it
2715                         
2716                         _meter->set_display_to_user (true);
2717                 }
2718         }
2719
2720         _meter_point = p;
2721         meter_change (src); /* EMIT SIGNAL */
2722
2723         bool const meter_visibly_changed = (_meter->display_to_user() != meter_was_visible_to_user);
2724         
2725         processors_changed (RouteProcessorChange (RouteProcessorChange::MeterPointChange, meter_visibly_changed)); /* EMIT SIGNAL */
2726 }
2727
2728 void
2729 Route::put_control_outs_at (Placement p)
2730 {
2731         if (!_control_outs) {
2732                 return;
2733         }
2734
2735         {
2736                 Glib::RWLock::WriterLock lm (_processor_lock);
2737                 ProcessorList as_it_was (_processors);
2738                 // Move meter in the processors list
2739                 ProcessorList::iterator loc = find(_processors.begin(), _processors.end(), _control_outs);
2740                 _processors.erase(loc);
2741                 
2742                 switch (p) {
2743                 case PreFader:
2744                         loc = find(_processors.begin(), _processors.end(), _amp);
2745                         if (loc != _processors.begin()) {
2746                                 --loc;
2747                         }
2748                         break;
2749                 case PostFader:
2750                         loc = find(_processors.begin(), _processors.end(), _amp);
2751                         assert (loc != _processors.end());
2752                         loc++;
2753                         break;
2754                 }
2755                 
2756                 _processors.insert(loc, _control_outs);
2757
2758                 if (configure_processors_unlocked (0)) {
2759                         _processors = as_it_was;
2760                         configure_processors_unlocked (0); // it worked before we tried to add it ...
2761                         return;
2762                 }
2763         }
2764
2765         processors_changed (RouteProcessorChange ()); /* EMIT SIGNAL */
2766         _session.set_dirty ();
2767 }
2768
2769 nframes_t
2770 Route::update_total_latency ()
2771 {
2772         nframes_t old = _output->effective_latency();
2773         nframes_t own_latency = _output->user_latency();
2774
2775         for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
2776                 if ((*i)->active ()) {
2777                         own_latency += (*i)->signal_latency ();
2778                 }
2779         }
2780
2781         DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: internal redirect latency = %2\n", _name, own_latency));
2782
2783         _output->set_port_latency (own_latency);
2784
2785         if (_output->user_latency() == 0) {
2786
2787                 /* this (virtual) function is used for pure Routes,
2788                    not derived classes like AudioTrack.  this means
2789                    that the data processed here comes from an input
2790                    port, not prerecorded material, and therefore we
2791                    have to take into account any input latency.
2792                 */
2793
2794                 own_latency += _input->signal_latency ();
2795         }
2796
2797         if (old != own_latency) {
2798                 _output->set_latency_delay (own_latency);
2799                 signal_latency_changed (); /* EMIT SIGNAL */
2800         }
2801
2802         DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: input latency = %2 total = %3\n", _name, _input->signal_latency(), own_latency));
2803
2804         return _output->effective_latency ();
2805 }
2806
2807 void
2808 Route::set_user_latency (nframes_t nframes)
2809 {
2810         _output->set_user_latency (nframes);
2811         _session.update_latency_compensation (false, false);
2812 }
2813
2814 void
2815 Route::set_latency_delay (nframes_t longest_session_latency)
2816 {
2817         nframes_t old = _initial_delay;
2818
2819         if (_output->effective_latency() < longest_session_latency) {
2820                 _initial_delay = longest_session_latency - _output->effective_latency();
2821         } else {
2822                 _initial_delay = 0;
2823         }
2824
2825         if (_initial_delay != old) {
2826                 initial_delay_changed (); /* EMIT SIGNAL */
2827         }
2828
2829         if (_session.transport_stopped()) {
2830                 _roll_delay = _initial_delay;
2831         }
2832 }
2833
2834 void
2835 Route::automation_snapshot (nframes_t now, bool force)
2836 {
2837         for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
2838                 (*i)->automation_snapshot (now, force);
2839         }
2840 }
2841
2842 Route::SoloControllable::SoloControllable (std::string name, Route& r)
2843         : AutomationControl (r.session(), Evoral::Parameter (SoloAutomation),
2844                              boost::shared_ptr<AutomationList>(), name)
2845         , route (r)
2846 {
2847         boost::shared_ptr<AutomationList> gl(new AutomationList(Evoral::Parameter(SoloAutomation)));
2848         set_list (gl);
2849 }
2850
2851 void
2852 Route::SoloControllable::set_value (float val)
2853 {
2854         bool bval = ((val >= 0.5f) ? true: false);
2855
2856         route.set_solo (bval, this);
2857 }
2858
2859 float
2860 Route::SoloControllable::get_value (void) const
2861 {
2862         return route.self_soloed() ? 1.0f : 0.0f;
2863 }
2864
2865 void
2866 Route::set_block_size (nframes_t nframes)
2867 {
2868         for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
2869                 (*i)->set_block_size (nframes);
2870         }
2871         
2872         _session.ensure_buffers (n_process_buffers ());
2873 }
2874
2875 void
2876 Route::protect_automation ()
2877 {
2878         for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i)
2879                 (*i)->protect_automation();
2880 }
2881
2882 void
2883 Route::set_pending_declick (int declick)
2884 {
2885         if (_declickable) {
2886                 /* this call is not allowed to turn off a pending declick unless "force" is true */
2887                 if (declick) {
2888                         _pending_declick = declick;
2889                 }
2890                 // cerr << _name << ": after setting to " << declick << " pending declick = " << _pending_declick << endl;
2891         } else {
2892                 _pending_declick = 0;
2893         }
2894
2895 }
2896
2897 /** Shift automation forwards from a particular place, thereby inserting time.
2898  *  Adds undo commands for any shifts that are performed.
2899  *
2900  * @param pos Position to start shifting from.
2901  * @param frames Amount to shift forwards by.
2902  */
2903
2904 void
2905 Route::shift (nframes64_t /*pos*/, nframes64_t /*frames*/)
2906 {
2907 #ifdef THIS_NEEDS_FIXING_FOR_V3
2908
2909         /* gain automation */
2910         XMLNode &before = _gain_control->get_state ();
2911         _gain_control->shift (pos, frames);
2912         XMLNode &after = _gain_control->get_state ();
2913         _session.add_command (new MementoCommand<AutomationList> (_gain_automation_curve, &before, &after));
2914
2915         /* pan automation */
2916         for (std::vector<StreamPanner*>::iterator i = _panner->begin (); i != _panner->end (); ++i) {
2917                 Curve & c = (*i)->automation ();
2918                 XMLNode &before = c.get_state ();
2919                 c.shift (pos, frames);
2920                 XMLNode &after = c.get_state ();
2921                 _session.add_command (new MementoCommand<AutomationList> (c, &before, &after));
2922         }
2923
2924         /* redirect automation */
2925         {
2926                 Glib::RWLock::ReaderLock lm (redirect_lock);
2927                 for (RedirectList::iterator i = _redirects.begin (); i != _redirects.end (); ++i) {
2928
2929                         set<uint32_t> a;
2930                         (*i)->what_has_automation (a);
2931
2932                         for (set<uint32_t>::const_iterator j = a.begin (); j != a.end (); ++j) {
2933                                 AutomationList & al = (*i)->automation_list (*j);
2934                                 XMLNode &before = al.get_state ();
2935                                 al.shift (pos, frames);
2936                                 XMLNode &after = al.get_state ();
2937                                 _session.add_command (new MementoCommand<AutomationList> (al, &before, &after));
2938                         }
2939                 }
2940         }
2941 #endif
2942
2943 }
2944
2945
2946 int
2947 Route::save_as_template (const string& path, const string& name)
2948 {
2949         XMLNode& node (state (false));
2950         XMLTree tree;
2951
2952         IO::set_name_in_state (*node.children().front(), name);
2953
2954         tree.set_root (&node);
2955         return tree.write (path.c_str());
2956 }
2957
2958
2959 bool
2960 Route::set_name (const string& str)
2961 {
2962         bool ret;
2963         string ioproc_name;
2964         string name;
2965
2966         name = Route::ensure_track_or_route_name (str, _session);
2967         SessionObject::set_name (name);
2968
2969         ret = (_input->set_name(name) && _output->set_name(name));
2970
2971         if (ret) {
2972
2973                 Glib::RWLock::ReaderLock lm (_processor_lock);
2974
2975                 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
2976
2977                         /* rename all I/O processors that have inputs or outputs */
2978
2979                         boost::shared_ptr<IOProcessor> iop = boost::dynamic_pointer_cast<IOProcessor> (*i);
2980
2981                         if (iop && (iop->output() || iop->input())) {
2982                                 if (!iop->set_name (name)) {
2983                                         ret = false;
2984                                 }
2985                         }
2986                 }
2987
2988         }
2989
2990         return ret;
2991 }
2992
2993 boost::shared_ptr<Send>
2994 Route::internal_send_for (boost::shared_ptr<const Route> target) const
2995 {
2996         Glib::RWLock::ReaderLock lm (_processor_lock);
2997
2998         for (ProcessorList::const_iterator i = _processors.begin(); i != _processors.end(); ++i) {
2999                 boost::shared_ptr<InternalSend> send;
3000
3001                 if ((send = boost::dynamic_pointer_cast<InternalSend>(*i)) != 0) {
3002                         if (send->target_route() == target) {
3003                                 return send;
3004                         }
3005                 }
3006         }
3007
3008         return boost::shared_ptr<Send>();
3009 }
3010
3011 void
3012 Route::set_phase_invert (bool yn)
3013 {
3014         if (_phase_invert != yn) {
3015                 _phase_invert = 0xffff; // XXX all channels
3016                 phase_invert_changed (); /* EMIT SIGNAL */
3017         }
3018 }
3019
3020 bool
3021 Route::phase_invert () const
3022 {
3023         return _phase_invert != 0;
3024 }
3025
3026 void
3027 Route::set_denormal_protection (bool yn)
3028 {
3029         if (_denormal_protection != yn) {
3030                 _denormal_protection = yn;
3031                 denormal_protection_changed (); /* EMIT SIGNAL */
3032         }
3033 }
3034
3035 bool
3036 Route::denormal_protection () const
3037 {
3038         return _denormal_protection;
3039 }
3040
3041 void
3042 Route::set_active (bool yn)
3043 {
3044         if (_active != yn) {
3045                 _active = yn;
3046                 _input->set_active (yn);
3047                 _output->set_active (yn);
3048                 active_changed (); // EMIT SIGNAL
3049         }
3050 }
3051
3052 void
3053 Route::meter ()
3054 {
3055         Glib::RWLock::ReaderLock rm (_processor_lock, Glib::TRY_LOCK);
3056
3057         assert (_meter);
3058
3059         _meter->meter ();
3060
3061         for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
3062
3063                 boost::shared_ptr<Send> s;
3064                 boost::shared_ptr<Return> r;
3065
3066                 if ((s = boost::dynamic_pointer_cast<Send> (*i)) != 0) {
3067                         s->meter()->meter();
3068                 } else if ((r = boost::dynamic_pointer_cast<Return> (*i)) != 0) {
3069                         r->meter()->meter ();
3070                 }
3071         }
3072 }
3073
3074 boost::shared_ptr<Panner>
3075 Route::panner() const
3076 {
3077
3078         return _main_outs->panner();
3079 }
3080
3081 boost::shared_ptr<AutomationControl>
3082 Route::gain_control() const
3083 {
3084
3085         return _amp->gain_control();
3086 }
3087
3088 boost::shared_ptr<AutomationControl>
3089 Route::get_control (const Evoral::Parameter& param)
3090 {
3091         /* either we own the control or .... */
3092
3093         boost::shared_ptr<AutomationControl> c = boost::dynamic_pointer_cast<AutomationControl>(data().control (param));
3094
3095         if (!c) {
3096
3097                 /* maybe one of our processors does or ... */
3098
3099                 Glib::RWLock::ReaderLock rm (_processor_lock, Glib::TRY_LOCK);
3100                 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
3101                         if ((c = boost::dynamic_pointer_cast<AutomationControl>((*i)->data().control (param))) != 0) {
3102                                 break;
3103                         }
3104                 }
3105         }
3106
3107         if (!c) {
3108
3109                 /* nobody does so we'll make a new one */
3110
3111                 c = boost::dynamic_pointer_cast<AutomationControl>(control_factory(param));
3112                 add_control(c);
3113         }
3114
3115         return c;
3116 }
3117
3118 boost::shared_ptr<Processor>
3119 Route::nth_plugin (uint32_t n)
3120 {
3121         Glib::RWLock::ReaderLock lm (_processor_lock);
3122         ProcessorList::iterator i;
3123
3124         for (i = _processors.begin(); i != _processors.end(); ++i) {
3125                 if (boost::dynamic_pointer_cast<PluginInsert> (*i)) {
3126                         if (n-- == 0) {
3127                                 return *i;
3128                         }
3129                 }
3130         }
3131
3132         return boost::shared_ptr<Processor> ();
3133 }
3134
3135 boost::shared_ptr<Processor>
3136 Route::nth_send (uint32_t n)
3137 {
3138         Glib::RWLock::ReaderLock lm (_processor_lock);
3139         ProcessorList::iterator i;
3140
3141         for (i = _processors.begin(); i != _processors.end(); ++i) {
3142                 cerr << "check " << (*i)->name() << endl;
3143                 if (boost::dynamic_pointer_cast<Send> (*i)) {
3144                         if (n-- == 0) {
3145                                 return *i;
3146                         }
3147                 } else {
3148                         cerr << "\tnot a send\n";
3149                 }
3150         }
3151
3152         return boost::shared_ptr<Processor> ();
3153 }