6e68c01d8c2039463dc92c93ef6f8138ee0bfe8b
[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/curve.h>
40 #include <ardour/types.h>
41 #include <ardour/data_type.h>
42 #include <ardour/port_set.h>
43 #include <ardour/chan_count.h>
44 #include <ardour/latent.h>
45 #include <ardour/automation_control.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 Panner;
58 class PeakMeter;
59 class Port;
60 class AudioPort;
61 class MidiPort;
62 class BufferSet;
63
64
65 /** A collection of input and output ports with connections.
66  *
67  * An IO can contain ports of varying types, making routes/inserts/etc with
68  * varied combinations of types (eg MIDI and audio) possible.
69  */
70
71 class IO : public Automatable, public Latent
72 {
73   public:
74         static const string state_node_name;
75
76         IO (Session&, const string& name, 
77             int input_min = -1, int input_max = -1, 
78             int output_min = -1, int output_max = -1,
79             DataType default_type = DataType::AUDIO);
80         
81         IO (Session&, const XMLNode&, DataType default_type = DataType::AUDIO);
82         
83         virtual ~IO();
84
85         ChanCount input_minimum() const { return _input_minimum; }
86         ChanCount input_maximum() const { return _input_maximum; }
87         ChanCount output_minimum() const { return _output_minimum; }
88         ChanCount output_maximum() const { return _output_maximum; }
89         
90         void set_input_minimum (ChanCount n);
91         void set_input_maximum (ChanCount n);
92         void set_output_minimum (ChanCount n);
93         void set_output_maximum (ChanCount n);
94         
95         DataType default_type() const         { return _default_type; }
96         void     set_default_type(DataType t) { _default_type = t; }
97         
98         bool set_name (const string& str);
99
100         virtual void silence  (nframes_t, nframes_t offset);
101
102         void collect_input  (BufferSet& bufs, nframes_t nframes, nframes_t offset);
103         void deliver_output (BufferSet& bufs, nframes_t start_frame, nframes_t end_frame,
104                                               nframes_t nframes, nframes_t offset);
105         void just_meter_input (nframes_t start_frame, nframes_t end_frame, 
106                                nframes_t nframes, nframes_t offset);
107
108         gain_t         gain () const { return _desired_gain; }
109         virtual gain_t effective_gain () const;
110         
111         void set_denormal_protection (bool yn, void *src);
112         bool denormal_protection() const { return _denormal_protection; }
113         
114         void set_phase_invert (bool yn, void *src);
115         bool phase_invert() const { return _phase_invert; }
116
117         Panner& panner()        { return *_panner; }
118         PeakMeter& peak_meter() { return *_meter; }
119         const Panner& panner() const { return *_panner; }
120         
121         int ensure_io (ChanCount in, ChanCount out, bool clear, void *src);
122
123         int connect_input_ports_to_bundle (boost::shared_ptr<Bundle>, void *src);
124         int connect_output_ports_to_bundle (boost::shared_ptr<Bundle>, void *src);
125
126         boost::shared_ptr<Bundle> input_bundle();
127         boost::shared_ptr<Bundle> output_bundle();
128
129         int add_input_port (string source, void *src, DataType type = DataType::NIL);
130         int add_output_port (string destination, void *src, DataType type = DataType::NIL);
131
132         int remove_input_port (Port *, void *src);
133         int remove_output_port (Port *, void *src);
134
135         int set_input (Port *, void *src);
136
137         int connect_input (Port *our_port, string other_port, void *src);
138         int connect_output (Port *our_port, string other_port, void *src);
139
140         int disconnect_input (Port *our_port, string other_port, void *src);
141         int disconnect_output (Port *our_port, string other_port, void *src);
142
143         int disconnect_inputs (void *src);
144         int disconnect_outputs (void *src);
145
146         nframes_t signal_latency() const { return _own_latency; }
147         nframes_t output_latency() const;
148         nframes_t input_latency() const;
149         void      set_port_latency (nframes_t);
150
151         void update_port_total_latencies ();
152
153         const PortSet& inputs()  const { return _inputs; }
154         const PortSet& outputs() const { return _outputs; }
155
156         Port *output (uint32_t n) const {
157                 if (n < _outputs.num_ports()) {
158                         return _outputs.port(n);
159                 } else {
160                         return 0;
161                 }
162         }
163
164         Port *input (uint32_t n) const {
165                 if (n < _inputs.num_ports()) {
166                         return _inputs.port(n);
167                 } else {
168                         return 0;
169                 }
170         }
171
172         AudioPort* audio_input(uint32_t n) const;
173         AudioPort* audio_output(uint32_t n) const;
174         MidiPort*  midi_input(uint32_t n) const;
175         MidiPort*  midi_output(uint32_t n) const;
176
177         const ChanCount& n_inputs ()  const { return _inputs.count(); }
178         const ChanCount& n_outputs () const { return _outputs.count(); }
179
180         void attach_buffers(ChanCount ignored);
181
182         boost::shared_ptr<Bundle> bundle_for_inputs () const { return _bundle_for_inputs; }
183         boost::shared_ptr<Bundle> bundle_for_outputs () const { return _bundle_for_outputs; }
184
185         sigc::signal<void,IOChange,void*> input_changed;
186         sigc::signal<void,IOChange,void*> output_changed;
187
188         virtual XMLNode& state (bool full);
189         XMLNode& get_state (void);
190         int set_state (const XMLNode&);
191
192         static int  disable_connecting (void);
193
194         static int  enable_connecting (void);
195
196         static int  disable_ports (void);
197
198         static int  enable_ports (void);
199
200         static int  disable_panners (void);
201
202         static int  reset_panners (void);
203         
204         static sigc::signal<int>            PortsLegal;
205         static sigc::signal<int>            PannersLegal;
206         static sigc::signal<int>            ConnectingLegal;
207         /// raised when the number of input or output ports changes
208         static sigc::signal<void,ChanCount> PortCountChanged;
209         static sigc::signal<int>            PortsCreated;
210
211     static void update_meters();
212
213   private: 
214
215     static sigc::signal<void>   Meter;
216     static Glib::StaticMutex    m_meter_signal_lock;
217     sigc::connection            m_meter_connection;
218
219   public:
220
221         /* automation */
222
223         struct GainControl : public AutomationControl {
224             GainControl (std::string name, IO& i, boost::shared_ptr<AutomationList> al)
225                         : AutomationControl (i._session, al, name)
226                         , _io (i)
227                 {}
228          
229             void set_value (float val);
230             float get_value (void) const;
231    
232             IO& _io;
233         };
234
235         boost::shared_ptr<GainControl> gain_control() {
236                 return _gain_control;
237         }
238         boost::shared_ptr<const GainControl> gain_control() const {
239                 return _gain_control;
240         }
241
242         void clear_automation ();
243         
244         void set_parameter_automation_state (Parameter, AutoState);
245
246         virtual void transport_stopped (nframes_t now); // interface: matches Insert
247         void automation_snapshot (nframes_t now); // interface: matches Automatable
248
249         void start_pan_touch (uint32_t which);
250         void end_pan_touch (uint32_t which);
251
252         void defer_pan_reset ();
253         void allow_pan_reset ();
254
255         /* the session calls this for master outs before
256            anyone else. controls outs too, at some point.
257         */
258
259         XMLNode *pending_state_node;
260         int ports_became_legal ();
261
262   private:
263         mutable Glib::Mutex io_lock;
264
265   protected:
266         Panner*             _panner;
267         BufferSet*          _output_buffers; //< Set directly to output port buffers
268         gain_t              _gain;
269         gain_t              _effective_gain;
270         gain_t              _desired_gain;
271         Glib::Mutex         declick_lock;
272         PortSet             _outputs;
273         PortSet             _inputs;
274         PeakMeter*          _meter;
275         boost::shared_ptr<Bundle> _input_bundle; ///< bundle connected to our inputs
276         boost::shared_ptr<Bundle> _output_bundle; ///< bundle connected to our outputs
277         bool                 no_panner_reset;
278         bool                _phase_invert;
279         bool                _denormal_protection;
280         XMLNode*             deferred_state;
281         DataType            _default_type;
282         
283         virtual void set_deferred_state() {}
284
285         void reset_panner ();
286
287         virtual uint32_t pans_required() const
288                 { return _inputs.count().n_audio(); }
289
290         boost::shared_ptr<GainControl> _gain_control;
291
292         virtual void   set_gain (gain_t g, void *src);
293         void           inc_gain (gain_t delta, void *src);
294
295         bool apply_gain_automation;
296         
297         virtual int load_automation (std::string path);
298
299         /* AudioTrack::deprecated_use_diskstream_connections() needs these */
300
301         int set_inputs (const string& str);
302         int set_outputs (const string& str);
303
304         static bool connecting_legal;
305         static bool ports_legal;
306
307         BufferSet& output_buffers() { return *_output_buffers; }
308
309   private:
310
311         friend class Send;
312
313         /* are these the best variable names ever, or what? */
314
315         sigc::connection input_bundle_configuration_connection;
316         sigc::connection output_bundle_configuration_connection;
317         sigc::connection input_bundle_connection_connection;
318         sigc::connection output_bundle_connection_connection;
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<Bundle> _bundle_for_inputs;
334         boost::shared_ptr<Bundle> _bundle_for_outputs;
335
336         static int parse_io_string (const string&, vector<string>& chns);
337
338         static int parse_gain_string (const string&, vector<string>& chns);
339         
340         int set_sources (vector<string>&, void *src, bool add);
341         int set_destinations (vector<string>&, void *src, bool add);
342
343         int ensure_inputs (ChanCount, bool clear, bool lockit, void *src);
344         int ensure_outputs (ChanCount, bool clear, bool lockit, void *src);
345
346         void drop_input_bundle ();
347         void drop_output_bundle ();
348
349         void input_bundle_configuration_changed ();
350         void input_bundle_connection_changed (int);
351         void output_bundle_configuration_changed ();
352         void output_bundle_connection_changed (int);
353
354         int create_ports (const XMLNode&);
355         int make_connections (const XMLNode&);
356
357         void setup_peak_meters ();
358         void meter ();
359
360         bool ensure_inputs_locked (ChanCount, bool clear, void *src);
361         bool ensure_outputs_locked (ChanCount, bool clear, void *src);
362
363         int32_t find_input_port_hole ();
364         int32_t find_output_port_hole ();
365
366         void create_bundles ();
367         void setup_bundles ();
368 };
369
370 } // namespace ARDOUR
371
372 #endif /*__ardour_io_h__ */