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