Fixes to bundle manager to make it vaguely usable.
[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/session_object.h>
46 #include <ardour/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 UserBundle;
58 class Bundle;
59 class Panner;
60 class PeakMeter;
61 class Port;
62 class AudioPort;
63 class MidiPort;
64 class BufferSet;
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 SessionObject, public AutomatableControls, 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         BufferSet& output_buffers() { return *_output_buffers; }
114
115         gain_t         gain () const { return _desired_gain; }
116         virtual gain_t effective_gain () const;
117         
118         void set_denormal_protection (bool yn, void *src);
119         bool denormal_protection() const { return _denormal_protection; }
120         
121         void set_phase_invert (bool yn, void *src);
122         bool phase_invert() const { return _phase_invert; }
123
124         Panner& panner()        { return *_panner; }
125         PeakMeter& peak_meter() { return *_meter; }
126         const Panner& panner() const { return *_panner; }
127         void reset_panner ();
128         
129         int ensure_io (ChanCount in, ChanCount out, bool clear, void *src);
130
131         int connect_input_ports_to_bundle (boost::shared_ptr<Bundle>, void *);
132         int disconnect_input_ports_from_bundle (boost::shared_ptr<Bundle>, void *);
133         int connect_output_ports_to_bundle (boost::shared_ptr<Bundle>, void *);
134         int disconnect_output_ports_from_bundle (boost::shared_ptr<Bundle>, void *);
135
136         BundleList bundles_connected_to_inputs ();
137         BundleList bundles_connected_to_outputs ();
138
139         boost::shared_ptr<Bundle> bundle_for_inputs () { return _bundle_for_inputs; }
140         boost::shared_ptr<Bundle> bundle_for_outputs () { return _bundle_for_outputs; }
141         
142         int add_input_port (string source, void *src, DataType type = DataType::NIL);
143         int add_output_port (string destination, void *src, DataType type = DataType::NIL);
144
145         int remove_input_port (Port *, void *src);
146         int remove_output_port (Port *, void *src);
147
148         int set_input (Port *, void *src);
149
150         int connect_input (Port *our_port, string other_port, void *src);
151         int connect_output (Port *our_port, string other_port, void *src);
152
153         int disconnect_input (Port *our_port, string other_port, void *src);
154         int disconnect_output (Port *our_port, string other_port, void *src);
155
156         int disconnect_inputs (void *src);
157         int disconnect_outputs (void *src);
158
159         nframes_t signal_latency() const { return _own_latency; }
160         nframes_t output_latency() const;
161         nframes_t input_latency() const;
162         void      set_port_latency (nframes_t);
163
164         void update_port_total_latencies ();
165
166         const PortSet& inputs()  const { return _inputs; }
167         const PortSet& outputs() const { return _outputs; }
168
169         Port *output (uint32_t n) const {
170                 if (n < _outputs.num_ports()) {
171                         return _outputs.port(n);
172                 } else {
173                         return 0;
174                 }
175         }
176
177         Port *input (uint32_t n) const {
178                 if (n < _inputs.num_ports()) {
179                         return _inputs.port(n);
180                 } else {
181                         return 0;
182                 }
183         }
184
185         AudioPort* audio_input(uint32_t n) const;
186         AudioPort* audio_output(uint32_t n) const;
187         MidiPort*  midi_input(uint32_t n) const;
188         MidiPort*  midi_output(uint32_t n) const;
189
190         const ChanCount& n_inputs ()  const { return _inputs.count(); }
191         const ChanCount& n_outputs () const { return _outputs.count(); }
192
193         void attach_buffers(ChanCount ignored);
194         
195         sigc::signal<void>                active_changed;
196
197         sigc::signal<void,IOChange,void*> input_changed;
198         sigc::signal<void,IOChange,void*> output_changed;
199
200         virtual XMLNode& state (bool full);
201         XMLNode& get_state (void);
202         int set_state (const XMLNode&);
203
204         static int  disable_connecting (void);
205         static int  enable_connecting (void);
206         static int  disable_ports (void);
207         static int  enable_ports (void);
208         static int  disable_panners (void);
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, const Evoral::Parameter &param,
232                     boost::shared_ptr<AutomationList> al = boost::shared_ptr<AutomationList>() )
233                         : AutomationControl (i->_session, param, al, name )
234                         , _io (i)
235                 {}
236          
237             void set_value (float val);
238             float get_value (void) const;
239    
240             IO* _io;
241         };
242
243         boost::shared_ptr<GainControl> gain_control() {
244                 return _gain_control;
245         }
246         boost::shared_ptr<const GainControl> gain_control() const {
247                 return _gain_control;
248         }
249
250         void clear_automation ();
251         
252         void set_parameter_automation_state (Evoral::Parameter, AutoState);
253
254         virtual void transport_stopped (nframes_t now);
255         virtual void automation_snapshot (nframes_t now, bool force);
256
257         void start_pan_touch (uint32_t which);
258         void end_pan_touch (uint32_t which);
259
260         void defer_pan_reset ();
261         void allow_pan_reset ();
262
263         /* the session calls this for master outs before
264            anyone else. controls outs too, at some point.
265         */
266
267         XMLNode *pending_state_node;
268         int ports_became_legal ();
269
270   private:
271         mutable Glib::Mutex io_lock;
272
273   protected:
274         Panner*             _panner;
275         BufferSet*          _output_buffers; //< Set directly to output port buffers
276         bool                _active;
277         gain_t              _gain;
278         gain_t              _effective_gain;
279         gain_t              _desired_gain;
280         Glib::Mutex          declick_lock;
281         PortSet             _outputs;
282         PortSet             _inputs;
283         PeakMeter*          _meter;
284         bool                 no_panner_reset;
285         bool                _phase_invert;
286         bool                _denormal_protection;
287         XMLNode*             deferred_state;
288         DataType            _default_type;
289         bool                _public_ports;
290
291         virtual void prepare_inputs (nframes_t nframes, nframes_t offset);
292         virtual void flush_outputs (nframes_t nframes, nframes_t offset);
293
294         virtual void set_deferred_state() {}
295
296         virtual uint32_t pans_required() const
297                 { return _inputs.count().n_audio(); }
298
299         boost::shared_ptr<GainControl> _gain_control;
300
301         virtual void   set_gain (gain_t g, void *src);
302         void           inc_gain (gain_t delta, void *src);
303
304         bool apply_gain_automation;
305         
306         virtual int load_automation (std::string path);
307
308         /* AudioTrack::deprecated_use_diskstream_connections() needs these */
309
310         int set_inputs (const string& str);
311         int set_outputs (const string& str);
312
313         static bool connecting_legal;
314         static bool ports_legal;
315
316   private:
317         static bool panners_legal;
318
319         void copy_to_outputs (BufferSet& bufs, DataType type, nframes_t nframes, nframes_t offset);
320
321         int connecting_became_legal ();
322         int panners_became_legal ();
323         sigc::connection connection_legal_c;
324         sigc::connection port_legal_c;
325         sigc::connection panner_legal_c;
326
327         ChanCount _input_minimum; ///< minimum number of input channels (0 for no minimum)
328         ChanCount _input_maximum; ///< maximum number of input channels (ChanCount::INFINITE for no maximum)
329         ChanCount _output_minimum; ///< minimum number of output channels (0 for no minimum)
330         ChanCount _output_maximum; ///< maximum number of output channels (ChanCount::INFINITE for no maximum)
331
332         boost::shared_ptr<Bundle> _bundle_for_inputs; ///< a bundle representing our inputs
333         boost::shared_ptr<Bundle> _bundle_for_outputs; ///< a bundle representing our outputs
334
335         struct UserBundleInfo {
336                 UserBundleInfo (IO*, boost::shared_ptr<UserBundle> b);
337                 
338                 boost::shared_ptr<UserBundle> bundle;
339                 sigc::connection changed;
340         };
341         
342         std::vector<UserBundleInfo> _bundles_connected_to_outputs; ///< user bundles connected to our outputs
343         std::vector<UserBundleInfo> _bundles_connected_to_inputs; ///< user bundles connected to our inputs
344
345         static int parse_io_string (const string&, vector<string>& chns);
346
347         static int parse_gain_string (const string&, vector<string>& chns);
348         
349         int set_sources (vector<string>&, void *src, bool add);
350         int set_destinations (vector<string>&, void *src, bool add);
351
352         int ensure_inputs (ChanCount, bool clear, bool lockit, void *src);
353         int ensure_outputs (ChanCount, bool clear, bool lockit, void *src);
354
355         void check_bundles_connected_to_inputs ();
356         void check_bundles_connected_to_outputs ();
357         void check_bundles (std::vector<UserBundleInfo>&, const PortSet&);
358
359         void bundle_changed (Bundle::Change);
360
361         int create_ports (const XMLNode&);
362         int make_connections (const XMLNode&);
363         boost::shared_ptr<Bundle> find_possible_bundle (const string &desired_name, const string &default_name, const string &connection_type_name);
364
365         void setup_peak_meters ();
366         void meter ();
367
368         bool ensure_inputs_locked (ChanCount, bool clear, void *src);
369         bool ensure_outputs_locked (ChanCount, bool clear, void *src);
370
371         std::string build_legal_port_name (DataType type, bool for_input);
372         int32_t find_input_port_hole (const char* base);
373         int32_t find_output_port_hole (const char* base);
374
375         void setup_bundles_for_inputs_and_outputs ();
376         void setup_bundle_for_inputs ();
377         void setup_bundle_for_outputs ();
378         std::string bundle_channel_name (uint32_t, uint32_t) const;
379 };
380
381 } // namespace ARDOUR
382
383 #endif /*__ardour_io_h__ */