The great audio processing overhaul.
[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_controls.h"
38 #include "ardour/automation_control.h"
39 #include "ardour/bundle.h"
40 #include "ardour/chan_count.h"
41 #include "ardour/data_type.h"
42 #include "ardour/latent.h"
43 #include "ardour/port_set.h"
44 #include "ardour/session_object.h"
45 #include "ardour/types.h"
46 #include "ardour/utils.h"
47
48 using std::string;
49 using std::vector;
50
51 class XMLNode;
52
53 namespace ARDOUR {
54
55 class Amp;
56 class AudioEngine;
57 class AudioPort;
58 class BufferSet;
59 class Bundle;
60 class MidiPort;
61 class Panner;
62 class PeakMeter;
63 class Port;
64 class Session;
65 class UserBundle;
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 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, DataType default_type = DataType::AUDIO,
78                 ChanCount in_min=ChanCount::ZERO, ChanCount in_max=ChanCount::INFINITE,
79                 ChanCount out_min=ChanCount::ZERO, ChanCount out_max=ChanCount::INFINITE);
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         bool active() const { return _active; }
96         void set_active (bool yn);
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);
104
105         void collect_input  (BufferSet& bufs, nframes_t nframes);
106         void deliver_output (BufferSet& bufs, nframes_t start_frame, nframes_t end_frame, nframes_t nframes);
107         void just_meter_input (nframes_t start_frame, nframes_t end_frame, nframes_t nframes);
108
109         BufferSet& output_buffers() { return *_output_buffers; }
110
111         gain_t         gain () const { return _gain_control->user_float(); }
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         void reset_panner ();
121         
122         boost::shared_ptr<Amp> amp() const { return _amp; }
123
124         PeakMeter&       peak_meter()       { return *_meter.get(); }
125         const PeakMeter& peak_meter() const { return *_meter.get(); }
126         boost::shared_ptr<PeakMeter> shared_peak_meter() const { return _meter; }
127
128         boost::shared_ptr<Panner> panner() const { return _panner; }
129         
130         int ensure_io (ChanCount in, ChanCount out, bool clear, void *src);
131
132         int connect_input_ports_to_bundle (boost::shared_ptr<Bundle>, void *);
133         int disconnect_input_ports_from_bundle (boost::shared_ptr<Bundle>, void *);
134         int connect_output_ports_to_bundle (boost::shared_ptr<Bundle>, void *);
135         int disconnect_output_ports_from_bundle (boost::shared_ptr<Bundle>, void *);
136
137         BundleList bundles_connected_to_inputs ();
138         BundleList bundles_connected_to_outputs ();
139
140         boost::shared_ptr<Bundle> bundle_for_inputs () { return _bundle_for_inputs; }
141         boost::shared_ptr<Bundle> bundle_for_outputs () { return _bundle_for_outputs; }
142         
143         int add_input_port (string source, void *src, DataType type = DataType::NIL);
144         int add_output_port (string destination, void *src, DataType type = DataType::NIL);
145
146         int remove_input_port (Port *, void *src);
147         int remove_output_port (Port *, void *src);
148
149         int set_input (Port *, void *src);
150
151         int connect_input (Port *our_port, string other_port, void *src);
152         int connect_output (Port *our_port, string other_port, void *src);
153
154         int disconnect_input (Port *our_port, string other_port, void *src);
155         int disconnect_output (Port *our_port, string other_port, void *src);
156
157         int disconnect_inputs (void *src);
158         int disconnect_outputs (void *src);
159
160         nframes_t signal_latency() const { return _own_latency; }
161         nframes_t output_latency() const;
162         nframes_t input_latency() const;
163         void      set_port_latency (nframes_t);
164
165         void update_port_total_latencies ();
166
167         const PortSet& inputs()  const { return _inputs; }
168         const PortSet& outputs() const { return _outputs; }
169
170         Port *output (uint32_t n) const {
171                 if (n < _outputs.num_ports()) {
172                         return _outputs.port(n);
173                 } else {
174                         return 0;
175                 }
176         }
177
178         Port *input (uint32_t n) const {
179                 if (n < _inputs.num_ports()) {
180                         return _inputs.port(n);
181                 } else {
182                         return 0;
183                 }
184         }
185
186         AudioPort* audio_input(uint32_t n) const;
187         AudioPort* audio_output(uint32_t n) const;
188         MidiPort*  midi_input(uint32_t n) const;
189         MidiPort*  midi_output(uint32_t n) const;
190
191         const ChanCount& n_inputs ()  const { return _inputs.count(); }
192         const ChanCount& n_outputs () const { return _outputs.count(); }
193
194         void attach_buffers(ChanCount ignored);
195         
196         sigc::signal<void>                active_changed;
197
198         sigc::signal<void,IOChange,void*> input_changed;
199         sigc::signal<void,IOChange,void*> output_changed;
200
201         virtual XMLNode& state (bool full);
202         XMLNode& get_state (void);
203         int set_state (const XMLNode&);
204
205         static int  disable_connecting (void);
206         static int  enable_connecting (void);
207         static int  disable_ports (void);
208         static int  enable_ports (void);
209         static int  disable_panners (void);
210         static int  reset_panners (void);
211         
212         static sigc::signal<int>            PortsLegal;
213         static sigc::signal<int>            PannersLegal;
214         static sigc::signal<int>            ConnectingLegal;
215         /// raised when the number of input or output ports changes
216         static sigc::signal<void,ChanCount> PortCountChanged;
217         static sigc::signal<int>            PortsCreated;
218         static sigc::signal<void,nframes_t> CycleStart;
219
220         static void update_meters();
221         static std::string name_from_state (const XMLNode&);
222         static void set_name_in_state (XMLNode&, const std::string&);
223
224   private: 
225         
226         static sigc::signal<void>   Meter;
227         static Glib::StaticMutex    m_meter_signal_lock;
228         sigc::connection            m_meter_connection;
229
230   public:
231     
232         /* automation */
233
234         struct GainControl : public AutomationControl {
235             GainControl (std::string name, IO* i, const Evoral::Parameter &param,
236                     boost::shared_ptr<AutomationList> al = boost::shared_ptr<AutomationList>() )
237                         : AutomationControl (i->_session, param, al, name )
238                         , _io (i)
239                 {}
240          
241             void set_value (float val);
242             float get_value (void) const;
243    
244             IO* _io;
245         };
246
247         boost::shared_ptr<GainControl> gain_control() {
248                 return _gain_control;
249         }
250         boost::shared_ptr<const GainControl> gain_control() const {
251                 return _gain_control;
252         }
253
254         void clear_automation ();
255         
256         void set_parameter_automation_state (Evoral::Parameter, AutoState);
257
258         virtual void transport_stopped (nframes_t now);
259         virtual void automation_snapshot (nframes_t now, bool force);
260
261         void start_pan_touch (uint32_t which);
262         void end_pan_touch (uint32_t which);
263
264         void defer_pan_reset ();
265         void allow_pan_reset ();
266
267         /* the session calls this for master outs before
268            anyone else. controls outs too, at some point.
269         */
270
271         XMLNode *pending_state_node;
272         int ports_became_legal ();
273
274   private:
275         mutable Glib::Mutex io_lock;
276
277   protected:
278         BufferSet*          _output_buffers; //< Set directly to output port buffers
279         bool                _active;
280         gain_t              _gain;
281         Glib::Mutex          declick_lock;
282         PortSet             _outputs;
283         PortSet             _inputs;
284         bool                 no_panner_reset;
285         bool                _phase_invert;
286         bool                _denormal_protection;
287         XMLNode*             deferred_state;
288         DataType            _default_type;
289         nframes_t           _output_offset;
290         ChanCount           _configured_inputs;
291         ChanCount           _configured_outputs;
292         
293         boost::shared_ptr<Amp>       _amp;
294         boost::shared_ptr<PeakMeter> _meter;
295         boost::shared_ptr<Panner>    _panner;
296
297         virtual void prepare_inputs (nframes_t nframes);
298         virtual void flush_outputs (nframes_t nframes);
299
300         virtual void set_deferred_state() {}
301
302         virtual uint32_t pans_required() const
303                 { return _inputs.count().n_audio(); }
304
305         boost::shared_ptr<GainControl> _gain_control;
306
307         virtual void   set_gain (gain_t g, void *src);
308         void           inc_gain (gain_t delta, void *src);
309
310         virtual int load_automation (std::string path);
311
312         /* AudioTrack::deprecated_use_diskstream_connections() needs these */
313
314         int set_inputs (const string& str);
315         int set_outputs (const string& str);
316
317         void increment_output_offset (nframes_t);
318         void cycle_start (nframes_t);
319
320         static bool connecting_legal;
321         static bool ports_legal;
322
323   private:
324         static bool panners_legal;
325
326         void copy_to_outputs (BufferSet& bufs, DataType type, nframes_t nframes);
327
328         int connecting_became_legal ();
329         int panners_became_legal ();
330         sigc::connection connection_legal_c;
331         sigc::connection port_legal_c;
332         sigc::connection panner_legal_c;
333
334         ChanCount _input_minimum; ///< minimum number of input channels (0 for no minimum)
335         ChanCount _input_maximum; ///< maximum number of input channels (ChanCount::INFINITE for no maximum)
336         ChanCount _output_minimum; ///< minimum number of output channels (0 for no minimum)
337         ChanCount _output_maximum; ///< maximum number of output channels (ChanCount::INFINITE for no maximum)
338
339         boost::shared_ptr<Bundle> _bundle_for_inputs; ///< a bundle representing our inputs
340         boost::shared_ptr<Bundle> _bundle_for_outputs; ///< a bundle representing our outputs
341
342         struct UserBundleInfo {
343                 UserBundleInfo (IO*, boost::shared_ptr<UserBundle> b);
344                 
345                 boost::shared_ptr<UserBundle> bundle;
346                 sigc::connection changed;
347         };
348         
349         std::vector<UserBundleInfo> _bundles_connected_to_outputs; ///< user bundles connected to our outputs
350         std::vector<UserBundleInfo> _bundles_connected_to_inputs; ///< user bundles connected to our inputs
351
352         static int parse_io_string (const string&, vector<string>& chns);
353
354         static int parse_gain_string (const string&, vector<string>& chns);
355         
356         int set_sources (vector<string>&, void *src, bool add);
357         int set_destinations (vector<string>&, void *src, bool add);
358
359         int ensure_inputs (ChanCount, bool clear, bool lockit, void *src);
360         int ensure_outputs (ChanCount, bool clear, bool lockit, void *src);
361
362         void check_bundles_connected_to_inputs ();
363         void check_bundles_connected_to_outputs ();
364         void check_bundles (std::vector<UserBundleInfo>&, const PortSet&);
365
366         void bundle_changed (Bundle::Change);
367
368         int get_port_counts (const XMLNode& node);
369         int create_ports (const XMLNode&);
370         int make_connections (const XMLNode&);
371         boost::shared_ptr<Bundle> find_possible_bundle (const string &desired_name, const string &default_name, const string &connection_type_name);
372
373         virtual void setup_peak_meters ();
374         void meter ();
375
376         bool ensure_inputs_locked (ChanCount, bool clear, void *src);
377         bool ensure_outputs_locked (ChanCount, bool clear, void *src);
378
379         std::string build_legal_port_name (DataType type, bool for_input);
380         int32_t find_input_port_hole (const char* base);
381         int32_t find_output_port_hole (const char* base);
382
383         void setup_bundles_for_inputs_and_outputs ();
384         void setup_bundle_for_inputs ();
385         void setup_bundle_for_outputs ();
386         std::string bundle_channel_name (uint32_t, uint32_t) const;
387 };
388
389 } // namespace ARDOUR
390
391 #endif /*__ardour_io_h__ */