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