2b54810577db3699e084eaa13cacd7ed65c20ce2
[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
33 #include <glibmm/thread.h>
34 #include "pbd/fastlog.h"
35 #include "pbd/xml++.h"
36 #include "pbd/undo.h"
37 #include "pbd/stateful.h" 
38 #include "pbd/controllable.h"
39 #include "pbd/destructible.h"
40
41 #include "ardour/ardour.h"
42 #include "ardour/io.h"
43 #include "ardour/types.h"
44
45 namespace ARDOUR {
46
47 class Amp;
48 class ControlOutputs;
49 class IOProcessor;
50 class Processor;
51 class RouteGroup;
52 class Send;
53
54 enum mute_type {
55     PRE_FADER =    0x1,
56     POST_FADER =   0x2,
57     CONTROL_OUTS = 0x4,
58     MAIN_OUTS =    0x8
59 };
60
61 class Route : public IO
62 {
63   protected:
64
65         typedef list<boost::shared_ptr<Processor> > ProcessorList;
66
67   public:
68
69         enum Flag {
70                 Hidden = 0x1,
71                 MasterOut = 0x2,
72                 ControlOut = 0x4
73         };
74
75         Route (Session&, std::string name, Flag flags = Flag(0),
76                         DataType default_type = DataType::AUDIO,
77                         ChanCount in=ChanCount::ZERO, ChanCount out=ChanCount::ZERO);
78
79         Route (Session&, const XMLNode&, DataType default_type = DataType::AUDIO);
80         virtual ~Route();
81
82         static std::string ensure_track_or_route_name(std::string, Session &);
83
84         std::string comment() { return _comment; }
85         void set_comment (std::string str, void *src);
86
87         long order_key (const char* name) const;
88         void set_order_key (const char* name, long n);
89
90         bool is_hidden() const { return _flags & Hidden; }
91         bool is_master() const { return _flags & MasterOut; }
92         bool is_control() const { return _flags & ControlOut; }
93
94         /* these are the core of the API of a Route. see the protected sections as well */
95
96
97         virtual int roll (nframes_t nframes, nframes_t start_frame, nframes_t end_frame, 
98                           int declick, bool can_record, bool rec_monitors_input);
99
100         virtual int no_roll (nframes_t nframes, nframes_t start_frame, nframes_t end_frame, 
101                              bool state_changing, bool can_record, bool rec_monitors_input);
102
103         virtual int silent_roll (nframes_t nframes, nframes_t start_frame, nframes_t end_frame, 
104                                  bool can_record, bool rec_monitors_input);
105
106         virtual void toggle_monitor_input ();
107         virtual bool can_record() { return false; }
108         virtual void set_record_enable (bool yn, void *src) {}
109         virtual bool record_enabled() const { return false; }
110         virtual void handle_transport_stopped (bool abort, bool did_locate, bool flush_processors);
111         virtual void set_pending_declick (int);
112
113         /* end of vfunc-based API */
114
115         void shift (nframes64_t, nframes64_t);
116
117         /* override IO::set_gain() to provide group control */
118
119         void set_gain (gain_t val, void *src);
120         void inc_gain (gain_t delta, void *src);
121         
122         void set_solo (bool yn, void *src);
123         bool soloed() const { return _soloed; }
124
125         void set_solo_safe (bool yn, void *src);
126         bool solo_safe() const { return _solo_safe; }
127         
128         void set_mute (bool yn, void *src);
129         bool muted() const { return _muted; }
130         bool solo_muted() const { return desired_solo_gain == 0.0; }
131
132         void set_mute_config (mute_type, bool, void *src);
133         bool get_mute_config (mute_type);
134
135         void       set_edit_group (RouteGroup *, void *);
136         void       drop_edit_group (void *);
137         RouteGroup *edit_group () { return _edit_group; }
138
139         void       set_mix_group (RouteGroup *, void *);
140         void       drop_mix_group (void *);
141         RouteGroup *mix_group () { return _mix_group; }
142
143         virtual void set_meter_point (MeterPoint, void *src);
144         MeterPoint   meter_point() const { return _meter_point; }
145
146         /* Processors */
147
148         void flush_processors ();
149
150         void foreach_processor (sigc::slot<void, boost::weak_ptr<Processor> > method) {
151                 Glib::RWLock::ReaderLock lm (_processor_lock);
152                 for (ProcessorList::iterator i = _processors.begin(); i != _processors.end(); ++i) {
153                         method (boost::weak_ptr<Processor> (*i));
154                 }
155         }
156         
157         void foreach_processor (Placement p, sigc::slot<void, boost::weak_ptr<Processor> > method) {
158                 Glib::RWLock::ReaderLock lm (_processor_lock);
159                 ProcessorList::iterator start, end;
160                 placement_range(p, start, end);
161                 for (ProcessorList::iterator i = start; i != end; ++i) {
162                         method (boost::weak_ptr<Processor> (*i));
163                 }
164         }
165
166         boost::shared_ptr<Processor> nth_processor (uint32_t n) {
167                 Glib::RWLock::ReaderLock lm (_processor_lock);
168                 ProcessorList::iterator i;
169                 for (i = _processors.begin(); i != _processors.end() && n; ++i, --n) {}
170                 if (i == _processors.end()) {
171                         return boost::shared_ptr<Processor> ();
172                 } else {
173                         return *i;
174                 }
175         }
176         
177         ChanCount max_processor_streams () const { return processor_max_streams; }
178         ChanCount pre_fader_streams() const;
179         
180         /** A record of the stream configuration at some point in the processor list.
181          * Used to return where and why an processor list configuration request failed.
182          */
183         struct ProcessorStreams {
184                 ProcessorStreams(size_t i=0, ChanCount c=ChanCount()) : index(i), count(c) {}
185
186                 uint32_t  index; ///< Index of processor where configuration failed
187                 ChanCount count; ///< Input requested of processor
188         };
189
190         int add_processor (boost::shared_ptr<Processor>, ProcessorStreams* err = 0, ProcessorList::iterator* iter=0, Placement=PreFader);
191         int add_processors (const ProcessorList&, ProcessorStreams* err = 0, Placement placement=PreFader);
192         int remove_processor (boost::shared_ptr<Processor>, ProcessorStreams* err = 0);
193         int sort_processors (ProcessorStreams* err = 0);
194         void disable_processors (Placement);
195         void disable_processors ();
196         void disable_plugins (Placement);
197         void disable_plugins ();
198         void ab_plugins (bool forward);
199         void clear_processors (Placement);
200         void all_processors_flip();
201         void all_processors_active (Placement, bool state);
202
203         virtual nframes_t update_total_latency();
204         void set_latency_delay (nframes_t);
205         void set_user_latency (nframes_t);
206         nframes_t initial_delay() const { return _initial_delay; }
207
208         sigc::signal<void,void*> solo_changed;
209         sigc::signal<void,void*> solo_safe_changed;
210         sigc::signal<void,void*> comment_changed;
211         sigc::signal<void,void*> mute_changed;
212         sigc::signal<void,void*> pre_fader_changed;
213         sigc::signal<void,void*> post_fader_changed;
214         sigc::signal<void,void*> control_outs_changed;
215         sigc::signal<void,void*> main_outs_changed;
216         sigc::signal<void>       processors_changed;
217         sigc::signal<void,void*> record_enable_changed;
218         sigc::signal<void,void*> edit_group_changed;
219         sigc::signal<void,void*> mix_group_changed;
220         sigc::signal<void,void*> meter_change;
221         sigc::signal<void>       signal_latency_changed;
222         sigc::signal<void>       initial_delay_changed;
223
224         /* gui's call this for their own purposes. */
225
226         sigc::signal<void,std::string,void*> gui_changed;
227
228         /* stateful */
229
230         XMLNode& get_state();
231         int set_state(const XMLNode& node);
232         virtual XMLNode& get_template();
233
234         XMLNode& get_processor_state ();
235         int set_processor_state (const XMLNode&);
236
237         int save_as_template (const std::string& path, const std::string& name);
238
239         sigc::signal<void,void*> SelectedChanged;
240
241         int set_control_outs (const vector<std::string>& ports);
242         boost::shared_ptr<ControlOutputs> control_outs() { return _control_outs; }
243
244         bool feeds (boost::shared_ptr<Route>);
245         std::set<boost::shared_ptr<Route> > fed_by;
246
247         struct ToggleControllable : public PBD::Controllable {
248             enum ToggleType {
249                     MuteControl = 0,
250                     SoloControl
251             };
252             
253             ToggleControllable (std::string name, Route&, ToggleType);
254             void set_value (float);
255             float get_value (void) const;
256
257             Route& route;
258             ToggleType type;
259         };
260
261         boost::shared_ptr<PBD::Controllable> solo_control() {
262                 return _solo_control;
263         }
264
265         boost::shared_ptr<PBD::Controllable> mute_control() {
266                 return _mute_control;
267         }
268         
269         void automation_snapshot (nframes_t now, bool force=false);
270         void protect_automation ();
271         
272         void set_remote_control_id (uint32_t id);
273         uint32_t remote_control_id () const;
274         sigc::signal<void> RemoteControlIDChanged;
275
276         void sync_order_keys (const char* base);
277         static sigc::signal<void,const char*> SyncOrderKeys;
278
279   protected:
280         friend class Session;
281
282         void catch_up_on_solo_mute_override ();
283         void set_solo_mute (bool yn);
284         void set_block_size (nframes_t nframes);
285         bool has_external_redirects() const;
286         void curve_reallocate ();
287
288   protected:
289         nframes_t check_initial_delay (nframes_t, nframes_t&);
290         
291         void passthru (nframes_t start_frame, nframes_t end_frame,
292                         nframes_t nframes, int declick);
293
294         virtual void process_output_buffers (BufferSet& bufs,
295                                              nframes_t start_frame, nframes_t end_frame,
296                                              nframes_t nframes, bool with_processors, int declick);
297         
298         Flag           _flags;
299         int            _pending_declick;
300         MeterPoint     _meter_point;
301
302         gain_t          solo_gain;
303         gain_t          mute_gain;
304         gain_t          desired_solo_gain;
305         gain_t          desired_mute_gain;
306         
307         nframes_t      _initial_delay;
308         nframes_t      _roll_delay;
309         ProcessorList  _processors;
310         Glib::RWLock   _processor_lock;
311         boost::shared_ptr<ControlOutputs> _control_outs;
312         RouteGroup    *_edit_group;
313         RouteGroup    *_mix_group;
314         std::string    _comment;
315         bool           _have_internal_generator;
316         
317         boost::shared_ptr<ToggleControllable> _solo_control;
318         boost::shared_ptr<ToggleControllable> _mute_control;
319
320         /* tight cache-line access here is more important than sheer speed of access.
321            keep these after things that should be aligned
322         */
323
324         bool _muted : 1;
325         bool _soloed : 1;
326         bool _solo_safe : 1;
327         bool _recordable : 1;
328         bool _mute_affects_pre_fader : 1;
329         bool _mute_affects_post_fader : 1;
330         bool _mute_affects_control_outs : 1;
331         bool _mute_affects_main_outs : 1;
332         bool _silent : 1;
333         bool _declickable : 1;
334
335   protected:
336
337         virtual XMLNode& state(bool);
338
339         void passthru_silence (nframes_t start_frame, nframes_t end_frame,
340                                nframes_t nframes, int declick);
341         
342         void silence (nframes_t nframes);
343         
344         sigc::connection input_signal_connection;
345
346         ChanCount processor_max_streams;
347         uint32_t _remote_control_id;
348
349         uint32_t pans_required() const;
350         ChanCount n_process_buffers ();
351         
352         void setup_peak_meters ();
353
354         virtual int  _set_state (const XMLNode&, bool call_base);
355         virtual void _set_processor_states (const XMLNodeList&);
356
357   private:
358         void init ();
359
360         static uint32_t order_key_cnt;
361
362         struct ltstr {
363             bool operator()(const char* s1, const char* s2) const {
364                     return strcmp(s1, s2) < 0;
365             }
366         };
367
368         typedef std::map<const char*,long,ltstr> OrderKeys;
369         OrderKeys order_keys;
370
371         void input_change_handler (IOChange, void *src);
372         void output_change_handler (IOChange, void *src);
373
374         bool _in_configure_processors;
375
376         int configure_processors (ProcessorStreams*);
377         int configure_processors_unlocked (ProcessorStreams*);
378         
379         void set_deferred_state ();
380         bool add_processor_from_xml (const XMLNode&, ProcessorList::iterator* iter=0);  
381
382         void placement_range(
383                         Placement                p,
384                         ProcessorList::iterator& start,
385                         ProcessorList::iterator& end);
386 };
387
388 } // namespace ARDOUR
389
390 #endif /* __ardour_route_h__ */