Make sure buses and tracks have unique names.
[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 <list>
25 #include <set>
26 #include <map>
27 #include <string>
28
29 #include <boost/shared_ptr.hpp>
30
31 #include <pbd/fastlog.h>
32 #include <glibmm/thread.h>
33 #include <pbd/xml++.h>
34 #include <pbd/undo.h>
35 #include <pbd/stateful.h> 
36 #include <pbd/controllable.h>
37 #include <pbd/destructible.h>
38
39 #include <ardour/ardour.h>
40 #include <ardour/io.h>
41 #include <ardour/session.h>
42 #include <ardour/redirect.h>
43 #include <ardour/types.h>
44
45 namespace ARDOUR {
46
47 class Insert;
48 class Send;
49 class RouteGroup;
50
51 enum mute_type {
52     PRE_FADER =    0x1,
53     POST_FADER =   0x2,
54     CONTROL_OUTS = 0x4,
55     MAIN_OUTS =    0x8
56 };
57
58 class Route : public IO
59 {
60   protected:
61
62         typedef list<boost::shared_ptr<Redirect> > RedirectList;
63   public:
64
65         enum Flag {
66                 Hidden = 0x1,
67                 MasterOut = 0x2,
68                 ControlOut = 0x4
69         };
70
71
72         Route (Session&, std::string name, int input_min, int input_max, int output_min, int output_max,
73                Flag flags = Flag(0), DataType default_type = DataType::AUDIO);
74         Route (Session&, const XMLNode&, DataType default_type = DataType::AUDIO);
75         virtual ~Route();
76
77         static std::string ensure_track_or_route_name(std::string, Session &);
78
79         std::string comment() { return _comment; }
80         void set_comment (std::string str, void *src);
81
82         long order_key (const char* name) const;
83         void set_order_key (const char* name, long n);
84
85         bool hidden() const { return _flags & Hidden; }
86         bool master() const { return _flags & MasterOut; }
87         bool control() const { return _flags & ControlOut; }
88
89         /* these are the core of the API of a Route. see the protected sections as well */
90
91
92         virtual int  roll (nframes_t nframes, nframes_t start_frame, nframes_t end_frame, 
93                            nframes_t offset, int declick, bool can_record, bool rec_monitors_input);
94
95         virtual int  no_roll (nframes_t nframes, nframes_t start_frame, nframes_t end_frame, 
96                               nframes_t offset, bool state_changing, bool can_record, bool rec_monitors_input);
97
98         virtual int  silent_roll (nframes_t nframes, nframes_t start_frame, nframes_t end_frame, 
99                                   nframes_t offset, bool can_record, bool rec_monitors_input);
100         virtual void toggle_monitor_input ();
101         virtual bool can_record() { return false; }
102         virtual void set_record_enable (bool yn, void *src) {}
103         virtual bool record_enabled() const { return false; }
104         virtual void handle_transport_stopped (bool abort, bool did_locate, bool flush_redirects);
105         virtual void set_pending_declick (int);
106
107         /* end of vfunc-based API */
108
109         /* override IO::set_gain() to provide group control */
110
111         void set_gain (gain_t val, void *src);
112         void inc_gain (gain_t delta, void *src);
113
114         void set_solo (bool yn, void *src);
115         bool soloed() const { return _soloed; }
116
117         void set_solo_safe (bool yn, void *src);
118         bool solo_safe() const { return _solo_safe; }
119
120         void set_mute (bool yn, void *src);
121         bool muted() const { return _muted; }
122         bool solo_muted() const { return desired_solo_gain == 0.0; }
123
124         void set_mute_config (mute_type, bool, void *src);
125         bool get_mute_config (mute_type);
126
127         void set_phase_invert (bool yn, void *src);
128         bool phase_invert() const { return _phase_invert; }
129
130         void set_denormal_protection (bool yn, void *src);
131         bool denormal_protection() const { return _denormal_protection; }
132
133         
134         void       set_edit_group (RouteGroup *, void *);
135         void       drop_edit_group (void *);
136         RouteGroup *edit_group () { return _edit_group; }
137
138         void       set_mix_group (RouteGroup *, void *);
139         void       drop_mix_group (void *);
140         RouteGroup *mix_group () { return _mix_group; }
141
142         virtual void  set_meter_point (MeterPoint, void *src);
143         MeterPoint  meter_point() const { return _meter_point; }
144
145         /* Redirects */
146
147         void flush_redirects ();
148
149         template<class T> void foreach_redirect (T *obj, void (T::*func)(boost::shared_ptr<Redirect>)) {
150                 Glib::RWLock::ReaderLock lm (redirect_lock);
151                 for (RedirectList::iterator i = _redirects.begin(); i != _redirects.end(); ++i) {
152                         (obj->*func) (*i);
153                 }
154         }
155
156         boost::shared_ptr<Redirect> nth_redirect (uint32_t n) {
157                 Glib::RWLock::ReaderLock lm (redirect_lock);
158                 RedirectList::iterator i;
159                 for (i = _redirects.begin(); i != _redirects.end() && n; ++i, --n);
160                 if (i == _redirects.end()) {
161                         return boost::shared_ptr<Redirect> ();
162                 } else {
163                         return *i;
164                 }
165         }
166         
167         uint32_t max_redirect_outs () const { return redirect_max_outs; }
168                 
169         int add_redirect (boost::shared_ptr<Redirect>, void *src, uint32_t* err_streams = 0);
170         int add_redirects (const RedirectList&, void *src, uint32_t* err_streams = 0);
171         int remove_redirect (boost::shared_ptr<Redirect>, void *src, uint32_t* err_streams = 0);
172         int copy_redirects (const Route&, Placement, uint32_t* err_streams = 0);
173         int sort_redirects (uint32_t* err_streams = 0);
174
175         void clear_redirects (Placement, void *src);
176         void all_redirects_flip();
177         void all_redirects_active (Placement, bool state);
178
179         virtual nframes_t update_total_latency();
180         nframes_t signal_latency() const { return _own_latency; }
181         virtual void set_latency_delay (nframes_t);
182
183         sigc::signal<void,void*> solo_changed;
184         sigc::signal<void,void*> solo_safe_changed;
185         sigc::signal<void,void*> comment_changed;
186         sigc::signal<void,void*> mute_changed;
187         sigc::signal<void,void*> pre_fader_changed;
188         sigc::signal<void,void*> post_fader_changed;
189         sigc::signal<void,void*> control_outs_changed;
190         sigc::signal<void,void*> main_outs_changed;
191         sigc::signal<void,void*> redirects_changed;
192         sigc::signal<void,void*> record_enable_changed;
193         sigc::signal<void,void*> edit_group_changed;
194         sigc::signal<void,void*> mix_group_changed;
195         sigc::signal<void,void*> meter_change;
196
197         /* gui's call this for their own purposes. */
198
199         sigc::signal<void,std::string,void*> gui_changed;
200
201         /* stateful */
202
203         XMLNode& get_state();
204         int set_state(const XMLNode& node);
205         virtual XMLNode& get_template();
206
207         sigc::signal<void,void*> SelectedChanged;
208
209         int set_control_outs (const vector<std::string>& ports);
210         IO* control_outs() { return _control_outs; }
211
212         bool feeds (boost::shared_ptr<Route>);
213         set<boost::shared_ptr<Route> > fed_by;
214
215         struct ToggleControllable : public PBD::Controllable {
216             enum ToggleType {
217                     MuteControl = 0,
218                     SoloControl
219             };
220             
221             ToggleControllable (std::string name, Route&, ToggleType);
222             void set_value (float);
223             float get_value (void) const;
224
225             Route& route;
226             ToggleType type;
227         };
228
229         PBD::Controllable& solo_control() {
230                 return _solo_control;
231         }
232
233         PBD::Controllable& mute_control() {
234                 return _mute_control;
235         }
236         
237         void automation_snapshot (nframes_t now);
238         void protect_automation ();
239         
240         void set_remote_control_id (uint32_t id);
241         uint32_t remote_control_id () const;
242         sigc::signal<void> RemoteControlIDChanged;
243
244         void sync_order_keys ();
245         static sigc::signal<void> SyncOrderKeys;
246
247   protected:
248         friend class Session;
249
250         void set_solo_mute (bool yn);
251         void set_block_size (nframes_t nframes);
252         bool has_external_redirects() const;
253         void curve_reallocate ();
254
255   protected:
256         Flag _flags;
257
258         /* tight cache-line access here is more important than sheer speed of
259            access.
260         */
261
262         bool                     _muted : 1;
263         bool                     _soloed : 1;
264         bool                     _solo_safe : 1;
265         bool                     _phase_invert : 1;
266         bool                     _denormal_protection : 1;
267         bool                     _recordable : 1;
268         bool                     _mute_affects_pre_fader : 1;
269         bool                     _mute_affects_post_fader : 1;
270         bool                     _mute_affects_control_outs : 1;
271         bool                     _mute_affects_main_outs : 1;
272         bool                     _silent : 1;
273         bool                     _declickable : 1;
274         int                      _pending_declick;
275         
276         MeterPoint               _meter_point;
277
278         gain_t                    solo_gain;
279         gain_t                    mute_gain;
280         gain_t                    desired_solo_gain;
281         gain_t                    desired_mute_gain;
282
283         nframes_t            check_initial_delay (nframes_t, nframes_t&, nframes_t&);
284
285         nframes_t           _initial_delay;
286         nframes_t           _roll_delay;
287         nframes_t           _own_latency;
288         RedirectList             _redirects;
289         Glib::RWLock      redirect_lock;
290         IO                      *_control_outs;
291         Glib::Mutex      control_outs_lock;
292         RouteGroup              *_edit_group;
293         RouteGroup              *_mix_group;
294         std::string              _comment;
295         bool                     _have_internal_generator;
296
297         ToggleControllable _solo_control;
298         ToggleControllable _mute_control;
299         
300         void passthru (nframes_t start_frame, nframes_t end_frame, 
301                        nframes_t nframes, nframes_t offset, int declick, bool meter_inputs);
302
303         void process_output_buffers (vector<Sample*>& bufs, uint32_t nbufs,
304                                      nframes_t start_frame, nframes_t end_frame,
305                                      nframes_t nframes, nframes_t offset, bool with_redirects, int declick,
306                                      bool meter);
307
308   protected:
309         /* for derived classes */
310
311         virtual XMLNode& state(bool);
312
313         void silence (nframes_t nframes, nframes_t offset);
314         sigc::connection input_signal_connection;
315
316         uint32_t redirect_max_outs;
317         uint32_t _remote_control_id;
318
319         uint32_t pans_required() const;
320         uint32_t n_process_buffers ();
321
322         virtual int  _set_state (const XMLNode&, bool call_base);
323         virtual void _set_redirect_states (const XMLNodeList&);
324
325   private:
326         void init ();
327
328         static uint32_t order_key_cnt;
329
330         struct ltstr
331         {
332             bool operator()(const char* s1, const char* s2) const
333             {
334                     return strcmp(s1, s2) < 0;
335             }
336         };
337
338         typedef std::map<const char*,long,ltstr> OrderKeys;
339         OrderKeys order_keys;
340
341         void input_change_handler (IOChange, void *src);
342         void output_change_handler (IOChange, void *src);
343
344         bool legal_redirect (Redirect&);
345         int reset_plugin_counts (uint32_t*); /* locked */
346         int _reset_plugin_counts (uint32_t*); /* unlocked */
347
348         /* plugin count handling */
349
350         struct InsertCount {
351             boost::shared_ptr<ARDOUR::Insert> insert;
352             int32_t cnt;
353             int32_t in;
354             int32_t out;
355
356             InsertCount (boost::shared_ptr<ARDOUR::Insert> ins) : insert (ins), cnt (-1) {}
357         };
358         
359         int32_t apply_some_plugin_counts (std::list<InsertCount>& iclist);
360         int32_t check_some_plugin_counts (std::list<InsertCount>& iclist, int32_t required_inputs, uint32_t* err_streams);
361
362         void set_deferred_state ();
363         void add_redirect_from_xml (const XMLNode&);
364         void redirect_active_proxy (Redirect*, void*);
365 };
366
367 } // namespace ARDOUR
368
369 #endif /* __ardour_route_h__ */