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