Various work on bundles. We now have a Bundle Manager dialogue, and hopefully things...
[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         
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         DataType default_type() const         { return _default_type; }
98         void     set_default_type(DataType t) { _default_type = t; }
99         
100         bool set_name (const string& str);
101
102         virtual void silence  (nframes_t, nframes_t offset);
103
104         void collect_input  (BufferSet& bufs, nframes_t nframes, nframes_t offset);
105         void deliver_output (BufferSet& bufs, nframes_t start_frame, nframes_t end_frame,
106                                               nframes_t nframes, nframes_t offset);
107         void just_meter_input (nframes_t start_frame, nframes_t end_frame, 
108                                nframes_t nframes, nframes_t offset);
109
110         gain_t         gain () const { return _desired_gain; }
111         virtual gain_t effective_gain () const;
112         
113         void set_denormal_protection (bool yn, void *src);
114         bool denormal_protection() const { return _denormal_protection; }
115         
116         void set_phase_invert (bool yn, void *src);
117         bool phase_invert() const { return _phase_invert; }
118
119         Panner& panner()        { return *_panner; }
120         PeakMeter& peak_meter() { return *_meter; }
121         const Panner& panner() const { return *_panner; }
122         
123         int ensure_io (ChanCount in, ChanCount out, bool clear, void *src);
124
125         int connect_input_ports_to_bundle (boost::shared_ptr<Bundle>, void *src);
126         int connect_output_ports_to_bundle (boost::shared_ptr<Bundle>, void *src);
127
128         std::vector<boost::shared_ptr<Bundle> > bundles_connected_to_inputs ();
129         std::vector<boost::shared_ptr<Bundle> > bundles_connected_to_outputs ();
130
131         boost::shared_ptr<AutoBundle> bundle_for_inputs () { return _bundle_for_inputs; }
132         boost::shared_ptr<AutoBundle> bundle_for_outputs () { return _bundle_for_outputs; }
133         
134         int add_input_port (string source, void *src, DataType type = DataType::NIL);
135         int add_output_port (string destination, void *src, DataType type = DataType::NIL);
136
137         int remove_input_port (Port *, void *src);
138         int remove_output_port (Port *, void *src);
139
140         int set_input (Port *, void *src);
141
142         int connect_input (Port *our_port, string other_port, void *src);
143         int connect_output (Port *our_port, string other_port, void *src);
144
145         int disconnect_input (Port *our_port, string other_port, void *src);
146         int disconnect_output (Port *our_port, string other_port, void *src);
147
148         int disconnect_inputs (void *src);
149         int disconnect_outputs (void *src);
150
151         nframes_t signal_latency() const { return _own_latency; }
152         nframes_t output_latency() const;
153         nframes_t input_latency() const;
154         void      set_port_latency (nframes_t);
155
156         void update_port_total_latencies ();
157
158         const PortSet& inputs()  const { return _inputs; }
159         const PortSet& outputs() const { return _outputs; }
160
161         Port *output (uint32_t n) const {
162                 if (n < _outputs.num_ports()) {
163                         return _outputs.port(n);
164                 } else {
165                         return 0;
166                 }
167         }
168
169         Port *input (uint32_t n) const {
170                 if (n < _inputs.num_ports()) {
171                         return _inputs.port(n);
172                 } else {
173                         return 0;
174                 }
175         }
176
177         AudioPort* audio_input(uint32_t n) const;
178         AudioPort* audio_output(uint32_t n) const;
179         MidiPort*  midi_input(uint32_t n) const;
180         MidiPort*  midi_output(uint32_t n) const;
181
182         const ChanCount& n_inputs ()  const { return _inputs.count(); }
183         const ChanCount& n_outputs () const { return _outputs.count(); }
184
185         void attach_buffers(ChanCount ignored);
186
187         sigc::signal<void,IOChange,void*> input_changed;
188         sigc::signal<void,IOChange,void*> output_changed;
189
190         virtual XMLNode& state (bool full);
191         XMLNode& get_state (void);
192         int set_state (const XMLNode&);
193
194         static int  disable_connecting (void);
195
196         static int  enable_connecting (void);
197
198         static int  disable_ports (void);
199
200         static int  enable_ports (void);
201
202         static int  disable_panners (void);
203
204         static int  reset_panners (void);
205         
206         static sigc::signal<int>            PortsLegal;
207         static sigc::signal<int>            PannersLegal;
208         static sigc::signal<int>            ConnectingLegal;
209         /// raised when the number of input or output ports changes
210         static sigc::signal<void,ChanCount> PortCountChanged;
211         static sigc::signal<int>            PortsCreated;
212
213     static void update_meters();
214
215   private: 
216
217     static sigc::signal<void>   Meter;
218     static Glib::StaticMutex    m_meter_signal_lock;
219     sigc::connection            m_meter_connection;
220
221   public:
222
223         /* automation */
224
225         struct GainControl : public AutomationControl {
226             GainControl (std::string name, IO& i, boost::shared_ptr<AutomationList> al)
227                         : AutomationControl (i._session, al, name)
228                         , _io (i)
229                 {}
230          
231             void set_value (float val);
232             float get_value (void) const;
233    
234             IO& _io;
235         };
236
237         boost::shared_ptr<GainControl> gain_control() {
238                 return _gain_control;
239         }
240         boost::shared_ptr<const GainControl> gain_control() const {
241                 return _gain_control;
242         }
243
244         void clear_automation ();
245         
246         void set_parameter_automation_state (Parameter, AutoState);
247
248         virtual void transport_stopped (nframes_t now); // interface: matches Insert
249         void automation_snapshot (nframes_t now); // interface: matches Automatable
250
251         void start_pan_touch (uint32_t which);
252         void end_pan_touch (uint32_t which);
253
254         void defer_pan_reset ();
255         void allow_pan_reset ();
256
257         /* the session calls this for master outs before
258            anyone else. controls outs too, at some point.
259         */
260
261         XMLNode *pending_state_node;
262         int ports_became_legal ();
263
264   private:
265         mutable Glib::Mutex io_lock;
266
267   protected:
268         Panner*             _panner;
269         BufferSet*          _output_buffers; //< Set directly to output port buffers
270         gain_t              _gain;
271         gain_t              _effective_gain;
272         gain_t              _desired_gain;
273         Glib::Mutex         declick_lock;
274         PortSet             _outputs;
275         PortSet             _inputs;
276         PeakMeter*          _meter;
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         static bool panners_legal;
314         
315         int connecting_became_legal ();
316         int panners_became_legal ();
317         sigc::connection connection_legal_c;
318         sigc::connection port_legal_c;
319         sigc::connection panner_legal_c;
320
321         ChanCount _input_minimum; ///< minimum number of input channels (0 for no minimum)
322         ChanCount _input_maximum; ///< maximum number of input channels (ChanCount::INFINITE for no maximum)
323         ChanCount _output_minimum; ///< minimum number of output channels (0 for no minimum)
324         ChanCount _output_maximum; ///< maximum number of output channels (ChanCount::INFINITE for no maximum)
325
326         boost::shared_ptr<AutoBundle> _bundle_for_inputs; ///< a bundle representing our inputs
327         boost::shared_ptr<AutoBundle> _bundle_for_outputs; ///< a bundle representing our outputs
328
329         struct UserBundleInfo {
330                 UserBundleInfo (IO*, boost::shared_ptr<UserBundle> b);
331                 
332                 boost::shared_ptr<UserBundle> bundle;
333                 sigc::connection configuration_will_change;
334                 sigc::connection configuration_has_changed;
335                 sigc::connection ports_will_change;
336                 sigc::connection ports_have_changed;
337         };
338         
339         std::vector<UserBundleInfo> _bundles_connected_to_outputs; ///< user bundles connected to our outputs
340         std::vector<UserBundleInfo> _bundles_connected_to_inputs; ///< user bundles connected to our inputs
341
342         static int parse_io_string (const string&, vector<string>& chns);
343
344         static int parse_gain_string (const string&, vector<string>& chns);
345         
346         int set_sources (vector<string>&, void *src, bool add);
347         int set_destinations (vector<string>&, void *src, bool add);
348
349         int ensure_inputs (ChanCount, bool clear, bool lockit, void *src);
350         int ensure_outputs (ChanCount, bool clear, bool lockit, void *src);
351
352         void check_bundles_connected_to_inputs ();
353         void check_bundles_connected_to_outputs ();
354         void check_bundles (std::vector<UserBundleInfo>&, const PortSet&);
355
356         void bundle_configuration_will_change ();
357         void bundle_configuration_has_changed ();
358         void bundle_ports_will_change (int);
359         void bundle_ports_have_changed (int);
360
361         int create_ports (const XMLNode&);
362         int make_connections (const XMLNode&);
363
364         void setup_peak_meters ();
365         void meter ();
366
367         bool ensure_inputs_locked (ChanCount, bool clear, void *src);
368         bool ensure_outputs_locked (ChanCount, bool clear, void *src);
369
370         int32_t find_input_port_hole ();
371         int32_t find_output_port_hole ();
372
373         void create_bundles_for_inputs_and_outputs ();
374         void setup_bundles_for_inputs_and_outputs ();
375
376         void maybe_add_input_bundle_to_list (boost::shared_ptr<Bundle>, std::vector<boost::shared_ptr<Bundle> >*);
377         void maybe_add_output_bundle_to_list (boost::shared_ptr<Bundle>, std::vector<boost::shared_ptr<Bundle> >*);
378 };
379
380 } // namespace ARDOUR
381
382 #endif /*__ardour_io_h__ */