Unify editor / mixer ordering.
[ardour.git] / libs / ardour / ardour / route.h
1 /*
2     Copyright (C) 2000-2002 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 #ifndef __ardour_route_h__
21 #define __ardour_route_h__
22
23 #include <cmath>
24 #include <cstring>
25 #include <list>
26 #include <map>
27 #include <set>
28 #include <string>
29
30 #include <boost/shared_ptr.hpp>
31 #include <boost/weak_ptr.hpp>
32 #include <boost/dynamic_bitset.hpp>
33 #include <boost/enable_shared_from_this.hpp>
34
35 #include <glibmm/threads.h>
36 #include "pbd/fastlog.h"
37 #include "pbd/xml++.h"
38 #include "pbd/undo.h"
39 #include "pbd/stateful.h"
40 #include "pbd/controllable.h"
41 #include "pbd/destructible.h"
42
43 #include "ardour/ardour.h"
44 #include "ardour/instrument_info.h"
45 #include "ardour/io.h"
46 #include "ardour/types.h"
47 #include "ardour/mute_master.h"
48 #include "ardour/route_group_member.h"
49 #include "ardour/graphnode.h"
50 #include "ardour/automatable.h"
51 #include "ardour/unknown_processor.h"
52
53 namespace ARDOUR {
54
55 class Amp;
56 class Delivery;
57 class IOProcessor;
58 class Panner;
59 class PannerShell;
60 class PortSet;
61 class Processor;
62 class RouteGroup;
63 class Send;
64 class InternalReturn;
65 class MonitorProcessor;
66 class Pannable;
67 class CapturingProcessor;
68 class InternalSend;
69
70 class Route : public SessionObject, public Automatable, public RouteGroupMember, public GraphNode, public boost::enable_shared_from_this<Route>
71 {
72   public:
73
74         typedef std::list<boost::shared_ptr<Processor> > ProcessorList;
75
76         enum Flag {
77                 Auditioner = 0x1,
78                 MasterOut = 0x2,
79                 MonitorOut = 0x4
80         };
81
82         Route (Session&, std::string name, Flag flags = Flag(0), DataType default_type = DataType::AUDIO);
83         virtual ~Route();
84
85         virtual int init ();
86
87         boost::shared_ptr<IO> input() const { return _input; }
88         boost::shared_ptr<IO> output() const { return _output; }
89
90         ChanCount n_inputs() const { return _input->n_ports(); }
91         ChanCount n_outputs() const { return _output->n_ports(); }
92
93         bool active() const { return _active; }
94         void set_active (bool yn, void *);
95
96         static std::string ensure_track_or_route_name(std::string, Session &);
97
98         std::string comment() { return _comment; }
99         void set_comment (std::string str, void *src);
100
101         bool set_name (const std::string& str);
102         static void set_name_in_state (XMLNode &, const std::string &);
103
104         uint32_t order_key () const;
105         bool has_order_key () const;
106         void set_order_key (uint32_t);
107
108         bool is_auditioner() const { return _flags & Auditioner; }
109         bool is_master() const { return _flags & MasterOut; }
110         bool is_monitor() const { return _flags & MonitorOut; }
111
112         virtual MonitorState monitoring_state () const;
113         virtual MeterState metering_state () const;
114         
115         /* these are the core of the API of a Route. see the protected sections as well */
116
117         virtual int roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame,
118                           int declick, bool& need_butler);
119
120         virtual int no_roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame,
121                              bool state_changing);
122
123         virtual int silent_roll (pframes_t nframes, framepos_t start_frame, framepos_t end_frame,
124                                  bool& need_butler);
125
126         virtual bool can_record() { return false; }
127
128         virtual void set_record_enabled (bool /*yn*/, void * /*src*/) {}
129         virtual bool record_enabled() const { return false; }
130         virtual void nonrealtime_handle_transport_stopped (bool abort, bool did_locate, bool flush_processors);
131         virtual void realtime_handle_transport_stopped () {}
132         virtual void realtime_locate () {}
133         virtual void non_realtime_locate (framepos_t);
134         virtual void set_pending_declick (int);
135
136         /* end of vfunc-based API */
137
138         void shift (framepos_t, framecnt_t);
139
140         void set_gain (gain_t val, void *src);
141         void inc_gain (gain_t delta, void *src);
142
143         void set_mute_points (MuteMaster::MutePoint);
144         MuteMaster::MutePoint mute_points () const;
145
146         bool muted () const;
147         void set_mute (bool yn, void* src);
148         
149         /* controls use set_solo() to modify this route's solo state
150          */
151
152         void set_solo (bool yn, void *src);
153         bool soloed () const { return self_soloed () || soloed_by_others (); }
154         void cancel_solo_after_disconnect (bool upstream);
155
156         bool soloed_by_others () const { return _soloed_by_others_upstream||_soloed_by_others_downstream; }
157         bool soloed_by_others_upstream () const { return _soloed_by_others_upstream; }
158         bool soloed_by_others_downstream () const { return _soloed_by_others_downstream; }
159         bool self_soloed () const { return _self_solo; }
160
161         void set_solo_isolated (bool yn, void *src);
162         bool solo_isolated() const;
163
164         void set_solo_safe (bool yn, void *src);
165         bool solo_safe() const;
166
167         void set_listen (bool yn, void* src);
168         bool listening_via_monitor () const;
169         void enable_monitor_send ();
170
171         void set_phase_invert (uint32_t, bool yn);
172         void set_phase_invert (boost::dynamic_bitset<>);
173         bool phase_invert (uint32_t) const;
174         boost::dynamic_bitset<> phase_invert () const;
175
176         void set_denormal_protection (bool yn);
177         bool denormal_protection() const;
178
179         void         set_meter_point (MeterPoint, bool force = false);
180         MeterPoint   meter_point() const { return _meter_point; }
181         void         meter ();
182
183         void         set_meter_type (MeterType t) { _meter_type = t; }
184         MeterType    meter_type() const { return _meter_type; }
185
186         /* Processors */
187
188         boost::shared_ptr<Amp> amp() const  { return _amp; }
189         PeakMeter&       peak_meter()       { return *_meter.get(); }
190         const PeakMeter& peak_meter() const { return *_meter.get(); }
191         boost::shared_ptr<PeakMeter> shared_peak_meter() const { return _meter; }
192
193         void flush_processors ();
194
195         void foreach_processor (boost::function<void(boost::weak_ptr<Processor>)> method) {
196                 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
197                 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
198                         if (boost::dynamic_pointer_cast<UnknownProcessor> (*i)) {
199                                 break;
200                         }
201                         method (boost::weak_ptr<Processor> (*i));
202                 }
203         }
204
205         boost::shared_ptr<Processor> nth_processor (uint32_t n) {
206                 Glib::Threads::RWLock::ReaderLock lm (_processor_lock);
207                 ProcessorList::iterator i;
208                 for (i = _processors.begin(); i != _processors.end() && n; ++i, --n) {}
209                 if (i == _processors.end()) {
210                         return boost::shared_ptr<Processor> ();
211                 } else {
212                         return *i;
213                 }
214         }
215
216         boost::shared_ptr<Processor> processor_by_id (PBD::ID) const;
217
218         boost::shared_ptr<Processor> nth_plugin (uint32_t n);
219         boost::shared_ptr<Processor> nth_send (uint32_t n);
220
221         bool has_io_processor_named (const std::string&);
222         ChanCount max_processor_streams () const { return processor_max_streams; }
223
224         std::list<std::string> unknown_processors () const;
225
226         /* special processors */
227
228         boost::shared_ptr<InternalSend>     monitor_send() const { return _monitor_send; }
229         boost::shared_ptr<Delivery>         main_outs() const { return _main_outs; }
230         boost::shared_ptr<InternalReturn>   internal_return() const { return _intreturn; }
231         boost::shared_ptr<MonitorProcessor> monitor_control() const { return _monitor_control; }
232         boost::shared_ptr<Send>             internal_send_for (boost::shared_ptr<const Route> target) const;
233         void add_internal_return ();
234         void add_send_to_internal_return (InternalSend *);
235         void remove_send_from_internal_return (InternalSend *);
236         void listen_position_changed ();
237         boost::shared_ptr<CapturingProcessor> add_export_point(/* Add some argument for placement later */);
238
239         /** A record of the stream configuration at some point in the processor list.
240          * Used to return where and why an processor list configuration request failed.
241          */
242         struct ProcessorStreams {
243                 ProcessorStreams(size_t i=0, ChanCount c=ChanCount()) : index(i), count(c) {}
244
245                 uint32_t  index; ///< Index of processor where configuration failed
246                 ChanCount count; ///< Input requested of processor
247         };
248
249         int add_processor (boost::shared_ptr<Processor>, Placement placement, ProcessorStreams* err = 0, bool activation_allowed = true);
250         int add_processor_by_index (boost::shared_ptr<Processor>, int, ProcessorStreams* err = 0, bool activation_allowed = true);
251         int add_processor (boost::shared_ptr<Processor>, boost::shared_ptr<Processor>, ProcessorStreams* err = 0, bool activation_allowed = true);
252         int add_processors (const ProcessorList&, boost::shared_ptr<Processor>, ProcessorStreams* err = 0);
253         boost::shared_ptr<Processor> before_processor_for_placement (Placement);
254         boost::shared_ptr<Processor> before_processor_for_index (int);
255         int remove_processor (boost::shared_ptr<Processor>, ProcessorStreams* err = 0, bool need_process_lock = true);
256         int remove_processors (const ProcessorList&, ProcessorStreams* err = 0);
257         int reorder_processors (const ProcessorList& new_order, ProcessorStreams* err = 0);
258         void disable_processors (Placement);
259         void disable_processors ();
260         void disable_plugins (Placement);
261         void disable_plugins ();
262         void ab_plugins (bool forward);
263         void clear_processors (Placement);
264         void all_visible_processors_active (bool);
265
266         framecnt_t set_private_port_latencies (bool playback) const;
267         void       set_public_port_latencies (framecnt_t, bool playback) const;
268
269         framecnt_t   update_signal_latency();
270         virtual void set_latency_compensation (framecnt_t);
271
272         void set_user_latency (framecnt_t);
273         framecnt_t initial_delay() const { return _initial_delay; }
274         framecnt_t signal_latency() const { return _signal_latency; }
275
276         PBD::Signal0<void>       active_changed;
277         PBD::Signal0<void>       phase_invert_changed;
278         PBD::Signal0<void>       denormal_protection_changed;
279         PBD::Signal1<void,void*> listen_changed;
280         PBD::Signal2<void,bool,void*> solo_changed;
281         PBD::Signal1<void,void*> solo_safe_changed;
282         PBD::Signal1<void,void*> solo_isolated_changed;
283         PBD::Signal1<void,void*> comment_changed;
284         PBD::Signal1<void,void*> mute_changed;
285         PBD::Signal0<void>       mute_points_changed;
286
287         /** the processors have changed; the parameter indicates what changed */
288         PBD::Signal1<void,RouteProcessorChange> processors_changed;
289         PBD::Signal1<void,void*> record_enable_changed;
290         /** the metering point has changed */
291         PBD::Signal0<void>       meter_change;
292         PBD::Signal0<void>       signal_latency_changed;
293         PBD::Signal0<void>       initial_delay_changed;
294
295         /** Emitted with the process lock held */
296         PBD::Signal0<void>       io_changed;
297
298         /* gui's call this for their own purposes. */
299
300         PBD::Signal2<void,std::string,void*> gui_changed;
301
302         /* stateful */
303
304         XMLNode& get_state();
305         virtual int set_state (const XMLNode&, int version);
306         virtual XMLNode& get_template();
307
308         XMLNode& get_processor_state ();
309         virtual void set_processor_state (const XMLNode&);
310
311         int save_as_template (const std::string& path, const std::string& name);
312
313         PBD::Signal1<void,void*> SelectedChanged;
314
315         int add_aux_send (boost::shared_ptr<Route>, boost::shared_ptr<Processor>);
316         void remove_aux_or_listen (boost::shared_ptr<Route>);
317
318         /**
319          * return true if this route feeds the first argument via at least one
320          * (arbitrarily long) signal pathway.
321          */
322         bool feeds (boost::shared_ptr<Route>, bool* via_send_only = 0);
323
324         /**
325          * return true if this route feeds the first argument directly, via
326          * either its main outs or a send.  This is checked by the actual
327          * connections, rather than by what the graph is currently doing.
328          */
329         bool direct_feeds_according_to_reality (boost::shared_ptr<Route>, bool* via_send_only = 0);
330
331         /**
332          * return true if this route feeds the first argument directly, via
333          * either its main outs or a send, according to the graph that
334          * is currently being processed.
335          */
336         bool direct_feeds_according_to_graph (boost::shared_ptr<Route>, bool* via_send_only = 0);
337
338         struct FeedRecord {
339                 boost::weak_ptr<Route> r;
340                 bool sends_only;
341
342                 FeedRecord (boost::shared_ptr<Route> rp, bool sendsonly)
343                 : r (rp)
344                 , sends_only (sendsonly) {}
345         };
346
347         struct FeedRecordCompare {
348                 bool operator() (const FeedRecord& a, const FeedRecord& b) const {
349                         return a.r < b.r;
350                 }
351         };
352
353         typedef std::set<FeedRecord,FeedRecordCompare> FedBy;
354
355         const FedBy& fed_by() const { return _fed_by; }
356         void clear_fed_by ();
357         bool add_fed_by (boost::shared_ptr<Route>, bool sends_only);
358
359         /* Controls (not all directly owned by the Route */
360
361         boost::shared_ptr<AutomationControl> get_control (const Evoral::Parameter& param);
362
363         class SoloControllable : public AutomationControl {
364         public:
365                 SoloControllable (std::string name, boost::shared_ptr<Route>);
366                 void set_value (double);
367                 double get_value () const;
368
369         private:
370                 boost::weak_ptr<Route> _route;
371         };
372
373         struct MuteControllable : public AutomationControl {
374         public:
375                 MuteControllable (std::string name, boost::shared_ptr<Route>);
376                 void set_value (double);
377                 double get_value () const;
378
379         private:
380                 boost::weak_ptr<Route> _route;
381         };
382
383         boost::shared_ptr<AutomationControl> solo_control() const {
384                 return _solo_control;
385         }
386
387         boost::shared_ptr<AutomationControl> mute_control() const {
388                 return _mute_control;
389         }
390
391         boost::shared_ptr<MuteMaster> mute_master() const {
392                 return _mute_master;
393         }
394
395         /* Route doesn't own these items, but sub-objects that it does own have them
396            and to make UI code a bit simpler, we provide direct access to them
397            here.
398         */
399
400         boost::shared_ptr<Panner> panner() const;  /* may return null */
401         boost::shared_ptr<PannerShell> panner_shell() const;
402         boost::shared_ptr<AutomationControl> gain_control() const;
403         boost::shared_ptr<Pannable> pannable() const;
404
405         /**
406            Return the first processor that accepts has at least one MIDI input
407            and at least one audio output. In the vast majority of cases, this
408            will be "the instrument". This does not preclude other MIDI->audio
409            processors later in the processing chain, but that would be a
410            special case not covered by this utility function.
411         */
412         boost::shared_ptr<Processor> the_instrument() const;
413         InstrumentInfo& instrument_info() { return _instrument_info; }
414
415         void protect_automation ();
416
417         enum { 
418                 /* These numbers are taken from MIDI Machine Control,
419                    which can only control up to 317 tracks without
420                    doing sysex segmentation.
421                 */
422                 MasterBusRemoteControlID = 318,
423                 MonitorBusRemoteControlID = 319,
424         };
425
426         void     set_remote_control_id (uint32_t id, bool notify_class_listeners = true);
427         uint32_t remote_control_id () const;
428         void     set_remote_control_id_explicit (uint32_t order_key);
429
430         /* for things concerned about *this* route's RID */
431
432         PBD::Signal0<void> RemoteControlIDChanged;
433
434         /* for things concerned about *any* route's RID changes */
435
436         static PBD::Signal0<void> RemoteControlIDChange;
437         static PBD::Signal0<void> SyncOrderKeys;
438
439         bool has_external_redirects() const;
440
441         /* can only be executed by a route for which is_monitor() is true
442            (i.e. the monitor out)
443         */
444         void monitor_run (framepos_t start_frame, framepos_t end_frame,
445                           pframes_t nframes, int declick);
446
447   protected:
448         friend class Session;
449
450         void catch_up_on_solo_mute_override ();
451         void mod_solo_by_others_upstream (int32_t);
452         void mod_solo_by_others_downstream (int32_t);
453         void curve_reallocate ();
454         virtual void set_block_size (pframes_t nframes);
455
456   protected:
457         virtual framecnt_t check_initial_delay (framecnt_t nframes, framepos_t&) { return nframes; }
458
459         void fill_buffers_with_input (BufferSet& bufs, boost::shared_ptr<IO> io, pframes_t nframes);
460
461         void passthru (BufferSet&, framepos_t start_frame, framepos_t end_frame,
462                         pframes_t nframes, int declick);
463
464         virtual void write_out_of_band_data (BufferSet& /* bufs */, framepos_t /* start_frame */, framepos_t /* end_frame */,
465                                              framecnt_t /* nframes */) {}
466
467         virtual void process_output_buffers (BufferSet& bufs,
468                                              framepos_t start_frame, framepos_t end_frame,
469                                              pframes_t nframes, int declick,
470                                              bool gain_automation_ok);
471
472         boost::shared_ptr<IO> _input;
473         boost::shared_ptr<IO> _output;
474
475         bool           _active;
476         framecnt_t     _signal_latency;
477         framecnt_t     _initial_delay;
478         framecnt_t     _roll_delay;
479
480         ProcessorList  _processors;
481         mutable Glib::Threads::RWLock   _processor_lock;
482         boost::shared_ptr<Delivery> _main_outs;
483         boost::shared_ptr<InternalSend> _monitor_send;
484         boost::shared_ptr<InternalReturn> _intreturn;
485         boost::shared_ptr<MonitorProcessor> _monitor_control;
486         boost::shared_ptr<Pannable> _pannable;
487
488         Flag           _flags;
489         int            _pending_declick;
490         MeterPoint     _meter_point;
491         MeterType      _meter_type;
492         boost::dynamic_bitset<> _phase_invert;
493         bool           _self_solo;
494         uint32_t       _soloed_by_others_upstream;
495         uint32_t       _soloed_by_others_downstream;
496         uint32_t       _solo_isolated;
497
498         bool           _denormal_protection;
499
500         bool _recordable : 1;
501         bool _silent : 1;
502         bool _declickable : 1;
503
504         boost::shared_ptr<SoloControllable> _solo_control;
505         boost::shared_ptr<MuteControllable> _mute_control;
506         boost::shared_ptr<MuteMaster> _mute_master;
507
508         virtual void act_on_mute () {}
509
510         std::string    _comment;
511         bool           _have_internal_generator;
512         bool           _solo_safe;
513         DataType       _default_type;
514         FedBy          _fed_by;
515
516         InstrumentInfo _instrument_info;
517
518         virtual ChanCount input_streams () const;
519
520   protected:
521         virtual XMLNode& state(bool);
522
523         int configure_processors (ProcessorStreams*);
524
525         void passthru_silence (framepos_t start_frame, framepos_t end_frame,
526                                pframes_t nframes, int declick);
527
528         void silence (framecnt_t);
529         void silence_unlocked (framecnt_t);
530
531         ChanCount processor_max_streams;
532         ChanCount processor_out_streams;
533
534         uint32_t pans_required() const;
535         ChanCount n_process_buffers ();
536
537         virtual void maybe_declick (BufferSet&, framecnt_t, int);
538
539         boost::shared_ptr<Amp>       _amp;
540         boost::shared_ptr<PeakMeter> _meter;
541
542         boost::shared_ptr<Processor> the_instrument_unlocked() const;
543
544   private:
545         int set_state_2X (const XMLNode&, int);
546         void set_processor_state_2X (XMLNodeList const &, int);
547
548         uint32_t _order_key;
549         bool _has_order_key;
550         uint32_t _remote_control_id;
551
552         void input_change_handler (IOChange, void *src);
553         void output_change_handler (IOChange, void *src);
554
555         bool input_port_count_changing (ChanCount);
556         bool output_port_count_changing (ChanCount);
557
558         bool _in_configure_processors;
559         bool _initial_io_setup;
560
561         int configure_processors_unlocked (ProcessorStreams*);
562         std::list<std::pair<ChanCount, ChanCount> > try_configure_processors (ChanCount, ProcessorStreams *);
563         std::list<std::pair<ChanCount, ChanCount> > try_configure_processors_unlocked (ChanCount, ProcessorStreams *);
564
565         bool add_processor_from_xml_2X (const XMLNode&, int);
566
567         void placement_range (Placement p, ProcessorList::iterator& start, ProcessorList::iterator& end);
568
569         void set_self_solo (bool yn);
570         void set_mute_master_solo ();
571
572         void set_processor_positions ();
573         framecnt_t update_port_latencies (PortSet& ports, PortSet& feeders, bool playback, framecnt_t) const;
574
575         void setup_invisible_processors ();
576         void unpan ();
577
578         boost::shared_ptr<CapturingProcessor> _capturing_processor;
579
580         /** A handy class to keep processor state while we attempt a reconfiguration
581          *  that may fail.
582          */
583         class ProcessorState {
584         public:
585                 ProcessorState (Route* r)
586                         : _route (r)
587                         , _processors (r->_processors)
588                         , _processor_max_streams (r->processor_max_streams)
589                 { }
590
591                 void restore () {
592                         _route->_processors = _processors;
593                         _route->processor_max_streams = _processor_max_streams;
594                 }
595
596         private:
597                 /* this should perhaps be a shared_ptr, but ProcessorStates will
598                    not hang around long enough for it to matter.
599                 */
600                 Route* _route;
601                 ProcessorList _processors;
602                 ChanCount _processor_max_streams;
603         };
604
605         friend class ProcessorState;
606
607         /* no copy construction */
608         Route (Route const &);
609
610         void maybe_note_meter_position ();
611         
612         /** true if we've made a note of a custom meter position in these variables */
613         bool _custom_meter_position_noted;
614         /** the processor that came after the meter when it was last set to a custom position,
615             or 0.
616         */
617         boost::weak_ptr<Processor> _processor_after_last_custom_meter;
618         /** true if the last custom meter position was at the end of the processor list */
619         bool _last_custom_meter_was_at_end;
620
621         void reset_instrument_info ();
622
623         void set_remote_control_id_internal (uint32_t id, bool notify_class_listeners = true);
624 };
625
626 } // namespace ARDOUR
627
628 #endif /* __ardour_route_h__ */