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