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