Insert/Redirect refactoring, towards better MIDI support in mixer strip, and
[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<Insert> > InsertList;
63
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_inserts);
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         bool solo_muted() const { return desired_solo_gain == 0.0; }
125
126         void set_mute_config (mute_type, bool, void *src);
127         bool get_mute_config (mute_type);
128
129         void       set_edit_group (RouteGroup *, void *);
130         void       drop_edit_group (void *);
131         RouteGroup *edit_group () { return _edit_group; }
132
133         void       set_mix_group (RouteGroup *, void *);
134         void       drop_mix_group (void *);
135         RouteGroup *mix_group () { return _mix_group; }
136
137         virtual void  set_meter_point (MeterPoint, void *src);
138         MeterPoint  meter_point() const { return _meter_point; }
139
140         /* Inserts */
141
142         void flush_inserts ();
143
144         template<class T> void foreach_insert (T *obj, void (T::*func)(boost::shared_ptr<Insert>)) {
145                 Glib::RWLock::ReaderLock lm (insert_lock);
146                 for (InsertList::iterator i = _inserts.begin(); i != _inserts.end(); ++i) {
147                         (obj->*func) (*i);
148                 }
149         }
150
151         boost::shared_ptr<Insert> nth_insert (uint32_t n) {
152                 Glib::RWLock::ReaderLock lm (insert_lock);
153                 InsertList::iterator i;
154                 for (i = _inserts.begin(); i != _inserts.end() && n; ++i, --n);
155                 if (i == _inserts.end()) {
156                         return boost::shared_ptr<Redirect> ();
157                 } else {
158                         return *i;
159                 }
160         }
161         
162         ChanCount max_insert_outs () const { return insert_max_outs; }
163         ChanCount pre_fader_streams() const;
164         
165         /** A record of the stream configuration at some point in the insert list.
166          * Used to return where and why an insert list configuration request failed.
167          */
168         struct InsertStreams {
169                 InsertStreams(size_t i=0, ChanCount c=ChanCount()) : index(i), count(c) {}
170
171                 size_t    index; ///< Index of insert where configuration failed
172                 ChanCount count; ///< Input requested of insert
173         };
174
175         int add_insert (boost::shared_ptr<Insert>, InsertStreams* err = 0);
176         int add_inserts (const InsertList&, InsertStreams* err = 0);
177         int remove_insert (boost::shared_ptr<Insert>, InsertStreams* err = 0);
178         int copy_inserts (const Route&, Placement, InsertStreams* err = 0);
179         int sort_inserts (InsertStreams* err = 0);
180         void disable_inserts (Placement);
181         void disable_inserts ();
182         void disable_plugins (Placement);
183         void disable_plugins ();
184         void ab_plugins (bool forward);
185         void clear_inserts (Placement);
186         void all_inserts_flip();
187         void all_inserts_active (Placement, bool state);
188
189         virtual nframes_t update_total_latency();
190         nframes_t signal_latency() const { return _own_latency; }
191         virtual void set_latency_delay (nframes_t);
192
193         sigc::signal<void,void*> solo_changed;
194         sigc::signal<void,void*> solo_safe_changed;
195         sigc::signal<void,void*> comment_changed;
196         sigc::signal<void,void*> mute_changed;
197         sigc::signal<void,void*> pre_fader_changed;
198         sigc::signal<void,void*> post_fader_changed;
199         sigc::signal<void,void*> control_outs_changed;
200         sigc::signal<void,void*> main_outs_changed;
201         sigc::signal<void>       inserts_changed;
202         sigc::signal<void,void*> record_enable_changed;
203         sigc::signal<void,void*> edit_group_changed;
204         sigc::signal<void,void*> mix_group_changed;
205         sigc::signal<void>       active_changed;
206         sigc::signal<void,void*> meter_change;
207
208         /* gui's call this for their own purposes. */
209
210         sigc::signal<void,std::string,void*> gui_changed;
211
212         /* stateful */
213
214         XMLNode& get_state();
215         int set_state(const XMLNode& node);
216         virtual XMLNode& get_template();
217
218         XMLNode& get_insert_state ();
219         int set_insert_state (const XMLNode&);
220
221         sigc::signal<void,void*> SelectedChanged;
222
223         int set_control_outs (const vector<std::string>& ports);
224         IO* control_outs() { return _control_outs; }
225
226         bool feeds (boost::shared_ptr<Route>);
227         set<boost::shared_ptr<Route> > fed_by;
228
229         struct ToggleControllable : public PBD::Controllable {
230             enum ToggleType {
231                     MuteControl = 0,
232                     SoloControl
233             };
234             
235             ToggleControllable (std::string name, Route&, ToggleType);
236             void set_value (float);
237             float get_value (void) const;
238
239             Route& route;
240             ToggleType type;
241         };
242
243         PBD::Controllable& solo_control() {
244                 return _solo_control;
245         }
246
247         PBD::Controllable& mute_control() {
248                 return _mute_control;
249         }
250         
251         void automation_snapshot (nframes_t now);
252         void protect_automation ();
253         
254         void set_remote_control_id (uint32_t id);
255         uint32_t remote_control_id () const;
256         sigc::signal<void> RemoteControlIDChanged;
257
258   protected:
259         friend class Session;
260
261         void set_solo_mute (bool yn);
262         void set_block_size (nframes_t nframes);
263         bool has_external_redirects() const;
264         void curve_reallocate ();
265
266   protected:
267         Flag _flags;
268
269         /* tight cache-line access here is more important than sheer speed of
270            access.
271         */
272
273         bool                     _muted : 1;
274         bool                     _soloed : 1;
275         bool                     _solo_safe : 1;
276         bool                     _recordable : 1;
277         bool                     _active : 1;
278         bool                     _mute_affects_pre_fader : 1;
279         bool                     _mute_affects_post_fader : 1;
280         bool                     _mute_affects_control_outs : 1;
281         bool                     _mute_affects_main_outs : 1;
282         bool                     _silent : 1;
283         bool                     _declickable : 1;
284         int                      _pending_declick;
285         
286         MeterPoint               _meter_point;
287
288         gain_t                    solo_gain;
289         gain_t                    mute_gain;
290         gain_t                    desired_solo_gain;
291         gain_t                    desired_mute_gain;
292
293         nframes_t            check_initial_delay (nframes_t, nframes_t&, nframes_t&);
294
295         nframes_t           _initial_delay;
296         nframes_t           _roll_delay;
297         nframes_t           _own_latency;
298         InsertList             _inserts;
299         Glib::RWLock      insert_lock;
300         IO                      *_control_outs;
301         Glib::Mutex      control_outs_lock;
302         RouteGroup              *_edit_group;
303         RouteGroup              *_mix_group;
304         std::string              _comment;
305         bool                     _have_internal_generator;
306
307         ToggleControllable _solo_control;
308         ToggleControllable _mute_control;
309         
310         void passthru (nframes_t start_frame, nframes_t end_frame, 
311                        nframes_t nframes, nframes_t offset, int declick, bool meter_inputs);
312
313         virtual void process_output_buffers (BufferSet& bufs,
314                                      nframes_t start_frame, nframes_t end_frame,
315                                      nframes_t nframes, nframes_t offset, bool with_inserts, int declick,
316                                      bool meter);
317
318   protected:
319
320         virtual XMLNode& state(bool);
321
322         void passthru_silence (nframes_t start_frame, nframes_t end_frame,
323                                nframes_t nframes, nframes_t offset, int declick,
324                                bool meter);
325         
326         void silence (nframes_t nframes, nframes_t offset);
327         
328         sigc::connection input_signal_connection;
329
330         ChanCount insert_max_outs;
331         uint32_t _remote_control_id;
332
333         uint32_t pans_required() const;
334         ChanCount n_process_buffers ();
335
336         virtual int  _set_state (const XMLNode&, bool call_base);
337         virtual void _set_insert_states (const XMLNodeList&);
338
339   private:
340         void init ();
341
342         static uint32_t order_key_cnt;
343
344         struct ltstr
345         {
346             bool operator()(const char* s1, const char* s2) const
347             {
348                     return strcmp(s1, s2) < 0;
349             }
350         };
351
352         typedef std::map<const char*,long,ltstr> OrderKeys;
353         OrderKeys order_keys;
354
355         void input_change_handler (IOChange, void *src);
356         void output_change_handler (IOChange, void *src);
357
358         int reset_plugin_counts (InsertStreams*); /* locked */
359         int _reset_plugin_counts (InsertStreams*); /* unlocked */
360
361         /* insert I/O channels and plugin count handling */
362
363         struct InsertCount {
364             boost::shared_ptr<ARDOUR::Insert> insert;
365             ChanCount in;
366             ChanCount out;
367
368             InsertCount (boost::shared_ptr<ARDOUR::Insert> ins) : insert(ins) {}
369         };
370         
371         int32_t apply_some_plugin_counts (std::list<InsertCount>& iclist);
372         bool    check_some_plugin_counts (std::list<InsertCount>& iclist, ChanCount required_inputs, InsertStreams* err_streams);
373
374         void set_deferred_state ();
375         void add_insert_from_xml (const XMLNode&);
376 };
377
378 } // namespace ARDOUR
379
380 #endif /* __ardour_route_h__ */