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