Stub implementation of LV2 persist extension.
[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
34 #include <glibmm/thread.h>
35 #include "pbd/fastlog.h"
36 #include "pbd/xml++.h"
37 #include "pbd/undo.h"
38 #include "pbd/stateful.h"
39 #include "pbd/controllable.h"
40 #include "pbd/destructible.h"
41
42 #include "ardour/ardour.h"
43 #include "ardour/io.h"
44 #include "ardour/types.h"
45 #include "ardour/mute_master.h"
46 #include "ardour/route_group_member.h"
47 #include "ardour/graphnode.h"
48 #include "ardour/automatable.h"
49
50 namespace ARDOUR {
51
52 class Amp;
53 class Delivery;
54 class IOProcessor;
55 class Panner;
56 class Processor;
57 class RouteGroup;
58 class Send;
59 class InternalReturn;
60 class MonitorProcessor;
61
62 class Route : public SessionObject, public Automatable, public RouteGroupMember, public GraphNode
63 {
64   public:
65
66         typedef std::list<boost::shared_ptr<Processor> > ProcessorList;
67
68         enum Flag {
69                 Hidden = 0x1,
70                 MasterOut = 0x2,
71                 MonitorOut = 0x4
72         };
73
74         Route (Session&, std::string name, Flag flags = Flag(0), DataType default_type = DataType::AUDIO);
75         virtual ~Route();
76
77         virtual int init ();
78
79         boost::shared_ptr<IO> input() const { return _input; }
80         boost::shared_ptr<IO> output() const { return _output; }
81
82         ChanCount n_inputs() const { return _input->n_ports(); }
83         ChanCount n_outputs() const { return _output->n_ports(); }
84
85         bool active() const { return _active; }
86         void set_active (bool yn);
87
88         static std::string ensure_track_or_route_name(std::string, Session &);
89
90         std::string comment() { return _comment; }
91         void set_comment (std::string str, void *src);
92
93         bool set_name (const std::string& str);
94
95         int32_t order_key (std::string const &) const;
96         void set_order_key (std::string const &, int32_t);
97
98         bool is_hidden() const { return _flags & Hidden; }
99         bool is_master() const { return _flags & MasterOut; }
100         bool is_monitor() const { return _flags & MonitorOut; }
101
102         /* these are the core of the API of a Route. see the protected sections as well */
103
104         virtual int roll (nframes_t nframes, framepos_t start_frame, framepos_t end_frame,
105                           int declick, bool can_record, bool rec_monitors_input, bool& need_butler);
106
107         virtual int no_roll (nframes_t nframes, framepos_t start_frame, framepos_t end_frame,
108                         bool state_changing, bool can_record, bool rec_monitors_input);
109
110         virtual int silent_roll (nframes_t nframes, framepos_t start_frame, framepos_t end_frame,
111                                  bool can_record, bool rec_monitors_input, bool& need_butler);
112
113         virtual void toggle_monitor_input ();
114         virtual bool can_record() { return false; }
115
116         virtual void set_record_enabled (bool /*yn*/, void * /*src*/) {}
117         virtual bool record_enabled() const { return false; }
118         virtual void handle_transport_stopped (bool abort, bool did_locate, bool flush_processors);
119         virtual void set_pending_declick (int);
120
121         /* end of vfunc-based API */
122
123         void shift (framepos_t, framepos_t);
124
125         void set_gain (gain_t val, void *src);
126         void inc_gain (gain_t delta, void *src);
127
128         void set_mute_points (MuteMaster::MutePoint);
129         MuteMaster::MutePoint mute_points () const;
130
131         bool muted () const;
132         void set_mute (bool yn, void* src);
133
134         /* controls use set_solo() to modify this route's solo state
135          */
136
137         void set_solo (bool yn, void *src);
138         bool soloed () const { return self_soloed () || soloed_by_others (); }
139
140         bool soloed_by_others () const { return _soloed_by_others_upstream||_soloed_by_others_downstream; }
141         bool soloed_by_others_upstream () const { return _soloed_by_others_upstream; }
142         bool soloed_by_others_downstream () const { return _soloed_by_others_downstream; }
143         bool self_soloed () const { return _self_solo; }
144         
145         void set_solo_isolated (bool yn, void *src);
146         bool solo_isolated() const;
147
148         void set_solo_safe (bool yn, void *src);
149         bool solo_safe() const;
150
151         void set_listen (bool yn, void* src);
152         bool listening () const;
153
154         void set_phase_invert (uint32_t, bool yn);
155         void set_phase_invert (boost::dynamic_bitset<>);
156         bool phase_invert (uint32_t) const;
157         boost::dynamic_bitset<> phase_invert () const;
158
159         void set_denormal_protection (bool yn);
160         bool denormal_protection() const;
161
162         void         set_meter_point (MeterPoint);
163         void         infer_meter_point () const;
164         MeterPoint   meter_point() const { return _meter_point; }
165         void         meter ();
166
167         /* Processors */
168
169         boost::shared_ptr<Amp> amp() const  { return _amp; }
170         PeakMeter&       peak_meter()       { return *_meter.get(); }
171         const PeakMeter& peak_meter() const { return *_meter.get(); }
172         boost::shared_ptr<PeakMeter> shared_peak_meter() const { return _meter; }
173
174         void flush_processors ();
175
176         void foreach_processor (boost::function<void(boost::weak_ptr<Processor>)> method) {
177                 Glib::RWLock::ReaderLock lm (_processor_lock);
178                 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
179                         method (boost::weak_ptr<Processor> (*i));
180                 }
181         }
182
183         boost::shared_ptr<Processor> nth_processor (uint32_t n) {
184                 Glib::RWLock::ReaderLock lm (_processor_lock);
185                 ProcessorList::iterator i;
186                 for (i = _processors.begin(); i != _processors.end() && n; ++i, --n) {}
187                 if (i == _processors.end()) {
188                         return boost::shared_ptr<Processor> ();
189                 } else {
190                         return *i;
191                 }
192         }
193
194         boost::shared_ptr<Processor> nth_plugin (uint32_t n);
195         boost::shared_ptr<Processor> nth_send (uint32_t n);
196
197         bool processor_is_prefader (boost::shared_ptr<Processor> p);
198
199         bool has_io_processor_named (const std::string&);
200         ChanCount max_processor_streams () const { return processor_max_streams; }
201
202         /* special processors */
203
204         boost::shared_ptr<Delivery>         monitor_send() const { return _monitor_send; }
205         boost::shared_ptr<Delivery>         main_outs() const { return _main_outs; }
206         boost::shared_ptr<InternalReturn>   internal_return() const { return _intreturn; }
207         boost::shared_ptr<MonitorProcessor> monitor_control() const { return _monitor_control; }
208         boost::shared_ptr<Send>             internal_send_for (boost::shared_ptr<const Route> target) const;
209         void add_internal_return ();
210         BufferSet* get_return_buffer () const;
211         void release_return_buffer () const;
212         void put_monitor_send_at (Placement);
213
214         /** A record of the stream configuration at some point in the processor list.
215          * Used to return where and why an processor list configuration request failed.
216          */
217         struct ProcessorStreams {
218                 ProcessorStreams(size_t i=0, ChanCount c=ChanCount()) : index(i), count(c) {}
219
220                 uint32_t  index; ///< Index of processor where configuration failed
221                 ChanCount count; ///< Input requested of processor
222         };
223
224         int add_processor (boost::shared_ptr<Processor>, Placement placement, ProcessorStreams* err = 0);
225         int add_processor (boost::shared_ptr<Processor>, ProcessorList::iterator iter, ProcessorStreams* err = 0, bool activation_allowed = true);
226         int add_processors (const ProcessorList&, boost::shared_ptr<Processor> before, ProcessorStreams* err = 0);
227         int add_processors (const ProcessorList&, ProcessorList::iterator iter, ProcessorStreams* err = 0);
228         int remove_processor (boost::shared_ptr<Processor>, ProcessorStreams* err = 0);
229         int remove_processors (const ProcessorList&, ProcessorStreams* err = 0);
230         int reorder_processors (const ProcessorList& new_order, ProcessorStreams* err = 0);
231         void disable_processors (Placement);
232         void disable_processors ();
233         void disable_plugins (Placement);
234         void disable_plugins ();
235         void ab_plugins (bool forward);
236         void clear_processors (Placement);
237         void all_processors_flip();
238         void all_processors_active (Placement, bool state);
239
240         virtual nframes_t update_total_latency();
241         void set_latency_delay (nframes_t);
242         void set_user_latency (nframes_t);
243         nframes_t initial_delay() const { return _initial_delay; }
244
245         PBD::Signal0<void>       active_changed;
246         PBD::Signal0<void>       phase_invert_changed;
247         PBD::Signal0<void>       denormal_protection_changed;
248         PBD::Signal1<void,void*> listen_changed;
249         PBD::Signal2<void,bool,void*> solo_changed;
250         PBD::Signal1<void,void*> solo_safe_changed;
251         PBD::Signal1<void,void*> solo_isolated_changed;
252         PBD::Signal1<void,void*> comment_changed;
253         PBD::Signal1<void,void*> mute_changed;
254         PBD::Signal0<void>       mute_points_changed;
255
256         /** the processors have changed; the parameter indicates what changed */
257         PBD::Signal1<void,RouteProcessorChange> processors_changed;
258         PBD::Signal1<void,void*> record_enable_changed;
259         /** the metering point has changed */
260         PBD::Signal0<void>       meter_change; 
261         PBD::Signal0<void>       signal_latency_changed;
262         PBD::Signal0<void>       initial_delay_changed;
263         PBD::Signal0<void>       order_key_changed;
264         PBD::Signal0<void>       io_changed;
265
266         /* gui's call this for their own purposes. */
267
268         PBD::Signal2<void,std::string,void*> gui_changed;
269
270         /* stateful */
271
272         XMLNode& get_state();
273         int set_state (const XMLNode&, int version);
274         virtual XMLNode& get_template();
275
276         XMLNode& get_processor_state ();
277         virtual void set_processor_state (const XMLNode&);
278
279         int save_as_template (const std::string& path, const std::string& name);
280
281         PBD::Signal1<void,void*> SelectedChanged;
282
283         int listen_via (boost::shared_ptr<Route>, Placement p, bool active, bool aux);
284         void drop_listen (boost::shared_ptr<Route>);
285
286        /** 
287         * return true if this route feeds the first argument via at least one
288         * (arbitrarily long) signal pathway.
289         */
290         bool feeds (boost::shared_ptr<Route>, bool* via_send_only = 0);
291
292        /** 
293         * return true if this route feeds the first argument directly, via
294         * either its main outs or a send.
295         */
296         bool direct_feeds (boost::shared_ptr<Route>, bool* via_send_only = 0);
297
298         struct FeedRecord {
299             boost::weak_ptr<Route> r;
300             bool sends_only;
301
302             FeedRecord (boost::shared_ptr<Route> rp, bool sendsonly)
303                     : r (rp)
304                     , sends_only (sendsonly) {}
305         };
306
307         struct FeedRecordCompare {
308             bool operator() (const FeedRecord& a, const FeedRecord& b) const {
309                     return a.r < b.r;
310             }
311         };
312
313         typedef std::set<FeedRecord,FeedRecordCompare> FedBy;
314
315         const FedBy& fed_by() const { return _fed_by; }
316         void clear_fed_by ();
317         bool add_fed_by (boost::shared_ptr<Route>, bool sends_only);
318         bool not_fed() const { return _fed_by.empty(); }
319
320         /* Controls (not all directly owned by the Route */
321
322         boost::shared_ptr<AutomationControl> get_control (const Evoral::Parameter& param);
323
324         struct SoloControllable : public AutomationControl {
325                 SoloControllable (std::string name, Route&);
326                 void set_value (double);
327                 double get_value (void) const;
328
329                 Route& route;
330         };
331
332         struct MuteControllable : public AutomationControl {
333                 MuteControllable (std::string name, Route&);
334                 void set_value (double);
335                 double get_value (void) const;
336
337                 Route& route;
338         };
339
340         boost::shared_ptr<AutomationControl> solo_control() const {
341                 return _solo_control;
342         }
343
344         boost::shared_ptr<AutomationControl> mute_control() const {
345                 return _mute_control;
346         }
347
348         boost::shared_ptr<MuteMaster> mute_master() const {
349                 return _mute_master;
350         }
351
352         /* Route doesn't own these items, but sub-objects that it does own have them
353            and to make UI code a bit simpler, we provide direct access to them
354            here.
355         */
356
357         boost::shared_ptr<Panner> panner() const;
358         boost::shared_ptr<AutomationControl> gain_control() const;
359
360         void automation_snapshot (nframes_t now, bool force=false);
361         void protect_automation ();
362
363         void set_remote_control_id (uint32_t id, bool notify_class_listeners = true);
364         uint32_t remote_control_id () const;
365
366         /* for things concerned about *this* route's RID */
367
368         PBD::Signal0<void> RemoteControlIDChanged;
369
370         /* for things concerned about any route's RID changes */
371
372         static PBD::Signal0<void> RemoteControlIDChange;
373
374         void sync_order_keys (std::string const &);
375         static PBD::Signal1<void,std::string const &> SyncOrderKeys;
376
377   protected:
378         friend class Session;
379
380         void catch_up_on_solo_mute_override ();
381         void mod_solo_by_others_upstream (int32_t);
382         void mod_solo_by_others_downstream (int32_t);
383         bool has_external_redirects() const;
384         void curve_reallocate ();
385         void just_meter_input (framepos_t start_frame, framepos_t end_frame, nframes_t nframes);
386         virtual void set_block_size (nframes_t nframes);
387
388   protected:
389         nframes_t check_initial_delay (nframes_t, nframes_t&);
390
391         void passthru (framepos_t start_frame, framepos_t end_frame,
392                         nframes_t nframes, int declick);
393
394         virtual void write_out_of_band_data (BufferSet& /* bufs */, framepos_t /* start_frame */, framepos_t /* end_frame */,
395                         nframes_t /* nframes */) {}
396
397         virtual void process_output_buffers (BufferSet& bufs,
398                         framepos_t start_frame, framepos_t end_frame,
399                         nframes_t nframes, bool with_processors, int declick);
400
401         boost::shared_ptr<IO> _input;
402         boost::shared_ptr<IO> _output;
403
404         bool           _active;
405         nframes_t      _initial_delay;
406         nframes_t      _roll_delay;
407
408         ProcessorList  _processors;
409         mutable Glib::RWLock   _processor_lock;
410         boost::shared_ptr<Delivery> _main_outs;
411         boost::shared_ptr<Delivery> _monitor_send;
412         boost::shared_ptr<InternalReturn> _intreturn;
413         boost::shared_ptr<MonitorProcessor> _monitor_control;
414
415         Flag           _flags;
416         int            _pending_declick;
417         MeterPoint     _meter_point;
418         boost::dynamic_bitset<> _phase_invert;
419         bool           _self_solo;
420         uint32_t       _soloed_by_others_upstream;
421         uint32_t       _soloed_by_others_downstream;
422         uint32_t       _solo_isolated;
423
424         bool           _denormal_protection;
425
426         bool _recordable : 1;
427         bool _silent : 1;
428         bool _declickable : 1;
429
430         boost::shared_ptr<SoloControllable> _solo_control;
431         boost::shared_ptr<MuteControllable> _mute_control;
432         boost::shared_ptr<MuteMaster> _mute_master;
433     
434         std::string    _comment;
435         bool           _have_internal_generator;
436         bool           _solo_safe;
437         DataType       _default_type;
438         FedBy          _fed_by;
439
440         virtual ChanCount input_streams () const;
441
442   protected:
443         virtual XMLNode& state(bool);
444
445         int configure_processors (ProcessorStreams*);
446
447         void passthru_silence (framepos_t start_frame, framepos_t end_frame,
448                                nframes_t nframes, int declick);
449
450         void silence (nframes_t);
451         void silence_unlocked (nframes_t);
452
453         ChanCount processor_max_streams;
454         uint32_t _remote_control_id;
455
456         uint32_t pans_required() const;
457         ChanCount n_process_buffers ();
458         
459         virtual int  _set_state (const XMLNode&, int, bool call_base);
460
461         boost::shared_ptr<Amp>       _amp;
462         boost::shared_ptr<PeakMeter> _meter;
463
464   private:
465         int _set_state_2X (const XMLNode&, int);
466         void set_processor_state_2X (XMLNodeList const &, int);
467
468         static uint32_t order_key_cnt;
469
470         typedef std::map<std::string, long> OrderKeys;
471         OrderKeys order_keys;
472
473         void input_change_handler (IOChange, void *src);
474         void output_change_handler (IOChange, void *src);
475
476         bool _in_configure_processors;
477
478         int configure_processors_unlocked (ProcessorStreams*);
479
480         bool add_processor_from_xml_2X (const XMLNode&, int);
481
482         void placement_range (Placement p, ProcessorList::iterator& start, ProcessorList::iterator& end);
483
484         void set_self_solo (bool yn);
485         void set_mute_master_solo ();
486
487         void set_processor_positions ();
488 };
489
490 } // namespace ARDOUR
491
492 #endif /* __ardour_route_h__ */