Tidy.
[ardour.git] / libs / ardour / ardour / io.h
1 /*
2     Copyright (C) 2000 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_io_h__
21 #define __ardour_io_h__
22
23 #include <string>
24 #include <vector>
25 #include <cmath>
26 #include <sigc++/signal.h>
27 #include <jack/jack.h>
28
29 #include <glibmm/thread.h>
30
31 #include <pbd/fastlog.h>
32 #include <pbd/undo.h>
33 #include <pbd/statefuldestructible.h> 
34 #include <pbd/controllable.h>
35
36 #include <ardour/ardour.h>
37 #include <ardour/automatable.h>
38 #include <ardour/utils.h>
39 #include <ardour/types.h>
40 #include <ardour/data_type.h>
41 #include <ardour/port_set.h>
42 #include <ardour/chan_count.h>
43 #include <ardour/latent.h>
44 #include <ardour/automation_control.h>
45 #include <ardour/user_bundle.h>
46
47 using std::string;
48 using std::vector;
49
50 class XMLNode;
51
52 namespace ARDOUR {
53
54 class Session;
55 class AudioEngine;
56 class Bundle;
57 class AutoBundle;
58 class Panner;
59 class PeakMeter;
60 class Port;
61 class AudioPort;
62 class MidiPort;
63 class BufferSet;
64
65
66 /** A collection of input and output ports with connections.
67  *
68  * An IO can contain ports of varying types, making routes/inserts/etc with
69  * varied combinations of types (eg MIDI and audio) possible.
70  */
71
72 class IO : public Automatable, public Latent
73 {
74   public:
75         static const string state_node_name;
76
77         IO (Session&, const string& name, 
78             int input_min = -1, int input_max = -1, 
79             int output_min = -1, int output_max = -1,
80             DataType default_type = DataType::AUDIO,
81             bool public_ports = true);
82         
83         IO (Session&, const XMLNode&, DataType default_type = DataType::AUDIO);
84         
85         virtual ~IO();
86
87         ChanCount input_minimum() const { return _input_minimum; }
88         ChanCount input_maximum() const { return _input_maximum; }
89         ChanCount output_minimum() const { return _output_minimum; }
90         ChanCount output_maximum() const { return _output_maximum; }
91         
92         void set_input_minimum (ChanCount n);
93         void set_input_maximum (ChanCount n);
94         void set_output_minimum (ChanCount n);
95         void set_output_maximum (ChanCount n);
96         
97         bool active() const { return _active; }
98         void set_active (bool yn);
99         
100         DataType default_type() const         { return _default_type; }
101         void     set_default_type(DataType t) { _default_type = t; }
102         
103         bool set_name (const string& str);
104
105         virtual void silence  (nframes_t, nframes_t offset);
106
107         void collect_input  (BufferSet& bufs, nframes_t nframes, nframes_t offset);
108         void deliver_output (BufferSet& bufs, nframes_t start_frame, nframes_t end_frame,
109                                               nframes_t nframes, nframes_t offset);
110         void just_meter_input (nframes_t start_frame, nframes_t end_frame, 
111                                nframes_t nframes, nframes_t offset);
112
113         gain_t         gain () const { return _desired_gain; }
114         virtual gain_t effective_gain () const;
115         
116         void set_denormal_protection (bool yn, void *src);
117         bool denormal_protection() const { return _denormal_protection; }
118         
119         void set_phase_invert (bool yn, void *src);
120         bool phase_invert() const { return _phase_invert; }
121
122         Panner& panner()        { return *_panner; }
123         PeakMeter& peak_meter() { return *_meter; }
124         const Panner& panner() const { return *_panner; }
125         
126         int ensure_io (ChanCount in, ChanCount out, bool clear, void *src);
127
128         int connect_input_ports_to_bundle (boost::shared_ptr<Bundle>, void *src);
129         int connect_output_ports_to_bundle (boost::shared_ptr<Bundle>, void *src);
130
131         std::vector<boost::shared_ptr<Bundle> > bundles_connected_to_inputs ();
132         std::vector<boost::shared_ptr<Bundle> > bundles_connected_to_outputs ();
133
134         boost::shared_ptr<AutoBundle> bundle_for_inputs () { return _bundle_for_inputs; }
135         boost::shared_ptr<AutoBundle> bundle_for_outputs () { return _bundle_for_outputs; }
136         
137         int add_input_port (string source, void *src, DataType type = DataType::NIL);
138         int add_output_port (string destination, void *src, DataType type = DataType::NIL);
139
140         int remove_input_port (Port *, void *src);
141         int remove_output_port (Port *, void *src);
142
143         int set_input (Port *, void *src);
144
145         int connect_input (Port *our_port, string other_port, void *src);
146         int connect_output (Port *our_port, string other_port, void *src);
147
148         int disconnect_input (Port *our_port, string other_port, void *src);
149         int disconnect_output (Port *our_port, string other_port, void *src);
150
151         int disconnect_inputs (void *src);
152         int disconnect_outputs (void *src);
153
154         nframes_t signal_latency() const { return _own_latency; }
155         nframes_t output_latency() const;
156         nframes_t input_latency() const;
157         void      set_port_latency (nframes_t);
158
159         void update_port_total_latencies ();
160
161         const PortSet& inputs()  const { return _inputs; }
162         const PortSet& outputs() const { return _outputs; }
163
164         Port *output (uint32_t n) const {
165                 if (n < _outputs.num_ports()) {
166                         return _outputs.port(n);
167                 } else {
168                         return 0;
169                 }
170         }
171
172         Port *input (uint32_t n) const {
173                 if (n < _inputs.num_ports()) {
174                         return _inputs.port(n);
175                 } else {
176                         return 0;
177                 }
178         }
179
180         AudioPort* audio_input(uint32_t n) const;
181         AudioPort* audio_output(uint32_t n) const;
182         MidiPort*  midi_input(uint32_t n) const;
183         MidiPort*  midi_output(uint32_t n) const;
184
185         const ChanCount& n_inputs ()  const { return _inputs.count(); }
186         const ChanCount& n_outputs () const { return _outputs.count(); }
187
188         void attach_buffers(ChanCount ignored);
189         
190         sigc::signal<void>                active_changed;
191
192         sigc::signal<void,IOChange,void*> input_changed;
193         sigc::signal<void,IOChange,void*> output_changed;
194
195         virtual XMLNode& state (bool full);
196         XMLNode& get_state (void);
197         int set_state (const XMLNode&);
198
199         static int  disable_connecting (void);
200
201         static int  enable_connecting (void);
202
203         static int  disable_ports (void);
204
205         static int  enable_ports (void);
206
207         static int  disable_panners (void);
208
209         static int  reset_panners (void);
210         
211         static sigc::signal<int>            PortsLegal;
212         static sigc::signal<int>            PannersLegal;
213         static sigc::signal<int>            ConnectingLegal;
214         /// raised when the number of input or output ports changes
215         static sigc::signal<void,ChanCount> PortCountChanged;
216         static sigc::signal<int>            PortsCreated;
217
218     static void update_meters();
219
220   private: 
221
222     static sigc::signal<void>   Meter;
223     static Glib::StaticMutex    m_meter_signal_lock;
224     sigc::connection            m_meter_connection;
225
226   public:
227
228         /* automation */
229
230         struct GainControl : public AutomationControl {
231             GainControl (std::string name, IO& i, boost::shared_ptr<AutomationList> al)
232                         : AutomationControl (i._session, al, name)
233                         , _io (i)
234                 {}
235          
236             void set_value (float val);
237             float get_value (void) const;
238    
239             IO& _io;
240         };
241
242         boost::shared_ptr<GainControl> gain_control() {
243                 return _gain_control;
244         }
245         boost::shared_ptr<const GainControl> gain_control() const {
246                 return _gain_control;
247         }
248
249         void clear_automation ();
250         
251         void set_parameter_automation_state (Parameter, AutoState);
252
253         virtual void transport_stopped (nframes_t now);
254         virtual void automation_snapshot (nframes_t now, bool force);
255
256         void start_pan_touch (uint32_t which);
257         void end_pan_touch (uint32_t which);
258
259         void defer_pan_reset ();
260         void allow_pan_reset ();
261
262         /* the session calls this for master outs before
263            anyone else. controls outs too, at some point.
264         */
265
266         XMLNode *pending_state_node;
267         int ports_became_legal ();
268
269   private:
270         mutable Glib::Mutex io_lock;
271
272   protected:
273         Panner*             _panner;
274         BufferSet*          _output_buffers; //< Set directly to output port buffers
275         bool                _active;
276         gain_t              _gain;
277         gain_t              _effective_gain;
278         gain_t              _desired_gain;
279         Glib::Mutex          declick_lock;
280         PortSet             _outputs;
281         PortSet             _inputs;
282         PeakMeter*          _meter;
283         bool                 no_panner_reset;
284         bool                _phase_invert;
285         bool                _denormal_protection;
286         XMLNode*             deferred_state;
287         DataType            _default_type;
288         bool                _public_ports;
289
290         virtual void set_deferred_state() {}
291
292         void reset_panner ();
293
294         virtual uint32_t pans_required() const
295                 { return _inputs.count().n_audio(); }
296
297         boost::shared_ptr<GainControl> _gain_control;
298
299         virtual void   set_gain (gain_t g, void *src);
300         void           inc_gain (gain_t delta, void *src);
301
302         bool apply_gain_automation;
303         
304         virtual int load_automation (std::string path);
305
306         /* AudioTrack::deprecated_use_diskstream_connections() needs these */
307
308         int set_inputs (const string& str);
309         int set_outputs (const string& str);
310
311         static bool connecting_legal;
312         static bool ports_legal;
313
314         BufferSet& output_buffers() { return *_output_buffers; }
315
316   private:
317
318         friend class Send;
319
320         static bool panners_legal;
321         
322         int connecting_became_legal ();
323         int panners_became_legal ();
324         sigc::connection connection_legal_c;
325         sigc::connection port_legal_c;
326         sigc::connection panner_legal_c;
327
328         ChanCount _input_minimum; ///< minimum number of input channels (0 for no minimum)
329         ChanCount _input_maximum; ///< maximum number of input channels (ChanCount::INFINITE for no maximum)
330         ChanCount _output_minimum; ///< minimum number of output channels (0 for no minimum)
331         ChanCount _output_maximum; ///< maximum number of output channels (ChanCount::INFINITE for no maximum)
332
333         boost::shared_ptr<AutoBundle> _bundle_for_inputs; ///< a bundle representing our inputs
334         boost::shared_ptr<AutoBundle> _bundle_for_outputs; ///< a bundle representing our outputs
335
336         struct UserBundleInfo {
337                 UserBundleInfo (IO*, boost::shared_ptr<UserBundle> b);
338                 
339                 boost::shared_ptr<UserBundle> bundle;
340                 sigc::connection configuration_will_change;
341                 sigc::connection configuration_has_changed;
342                 sigc::connection ports_will_change;
343                 sigc::connection ports_have_changed;
344         };
345         
346         std::vector<UserBundleInfo> _bundles_connected_to_outputs; ///< user bundles connected to our outputs
347         std::vector<UserBundleInfo> _bundles_connected_to_inputs; ///< user bundles connected to our inputs
348
349         static int parse_io_string (const string&, vector<string>& chns);
350
351         static int parse_gain_string (const string&, vector<string>& chns);
352         
353         int set_sources (vector<string>&, void *src, bool add);
354         int set_destinations (vector<string>&, void *src, bool add);
355
356         int ensure_inputs (ChanCount, bool clear, bool lockit, void *src);
357         int ensure_outputs (ChanCount, bool clear, bool lockit, void *src);
358
359         void check_bundles_connected_to_inputs ();
360         void check_bundles_connected_to_outputs ();
361         void check_bundles (std::vector<UserBundleInfo>&, const PortSet&);
362
363         void bundle_configuration_will_change ();
364         void bundle_configuration_has_changed ();
365         void bundle_ports_will_change (int);
366         void bundle_ports_have_changed (int);
367
368         int create_ports (const XMLNode&);
369         int make_connections (const XMLNode&);
370         boost::shared_ptr<Bundle> find_possible_bundle (const string &desired_name, const string &default_name, const string &connection_type_name);
371
372         void setup_peak_meters ();
373         void meter ();
374
375         bool ensure_inputs_locked (ChanCount, bool clear, void *src);
376         bool ensure_outputs_locked (ChanCount, bool clear, void *src);
377
378         std::string build_legal_port_name (DataType type, bool for_input);
379         int32_t find_input_port_hole (const char* base);
380         int32_t find_output_port_hole (const char* base);
381
382         void create_bundles_for_inputs_and_outputs ();
383         void setup_bundles_for_inputs_and_outputs ();
384
385         void maybe_add_input_bundle_to_list (boost::shared_ptr<Bundle>, std::vector<boost::shared_ptr<Bundle> >*);
386         void maybe_add_output_bundle_to_list (boost::shared_ptr<Bundle>, std::vector<boost::shared_ptr<Bundle> >*);
387 };
388
389 } // namespace ARDOUR
390
391 #endif /*__ardour_io_h__ */