s/ParamID/Parameter/
[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 use_input_bundle (Bundle&, void *src);
124         int use_output_bundle (Bundle&, void *src);
125
126         Bundle *input_bundle() const { return _input_bundle; }
127         Bundle *output_bundle() const { return _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         sigc::signal<void,IOChange,void*> input_changed;
183         sigc::signal<void,IOChange,void*> output_changed;
184
185         virtual XMLNode& state (bool full);
186         XMLNode& get_state (void);
187         int set_state (const XMLNode&);
188
189         static int  disable_connecting (void);
190
191         static int  enable_connecting (void);
192
193         static int  disable_ports (void);
194
195         static int  enable_ports (void);
196
197         static int  disable_panners (void);
198
199         static int  reset_panners (void);
200         
201         static sigc::signal<int>            PortsLegal;
202         static sigc::signal<int>            PannersLegal;
203         static sigc::signal<int>            ConnectingLegal;
204         static sigc::signal<void,ChanCount> MoreChannels;
205         static sigc::signal<int>            PortsCreated;
206
207     static void update_meters();
208
209   private: 
210
211     static sigc::signal<void>   Meter;
212     static Glib::StaticMutex    m_meter_signal_lock;
213     sigc::connection            m_meter_connection;
214
215   public:
216
217         /* automation */
218         
219         struct GainControl : public AutomationControl {
220             GainControl (std::string name, IO& i, boost::shared_ptr<AutomationList> al)
221                         : AutomationControl (i._session, al, name)
222                         , _io (i)
223                 {}
224          
225             void set_value (float val);
226             float get_value (void) const;
227    
228             IO& _io;
229         };
230
231         boost::shared_ptr<GainControl> gain_control() {
232                 return _gain_control;
233         }
234         boost::shared_ptr<const GainControl> gain_control() const {
235                 return _gain_control;
236         }
237
238         void clear_automation ();
239         
240         void set_parameter_automation_state (Parameter, AutoState);
241
242         virtual void transport_stopped (nframes_t now); // interface: matches Insert
243         void automation_snapshot (nframes_t now); // interface: matches Automatable
244
245         void start_pan_touch (uint32_t which);
246         void end_pan_touch (uint32_t which);
247
248         void defer_pan_reset ();
249         void allow_pan_reset ();
250
251         /* the session calls this for master outs before
252            anyone else. controls outs too, at some point.
253         */
254
255         XMLNode *pending_state_node;
256         int ports_became_legal ();
257
258   private:
259         mutable Glib::Mutex io_lock;
260
261   protected:
262         Panner*             _panner;
263         BufferSet*          _output_buffers; //< Set directly to output port buffers
264         gain_t              _gain;
265         gain_t              _effective_gain;
266         gain_t              _desired_gain;
267         Glib::Mutex         declick_lock;
268         PortSet             _outputs;
269         PortSet             _inputs;
270         PeakMeter*          _meter;
271         Bundle*             _input_bundle;
272         Bundle*             _output_bundle;
273         bool                 no_panner_reset;
274         bool                _phase_invert;
275         bool                _denormal_protection;
276         XMLNode*             deferred_state;
277         DataType            _default_type;
278         
279         virtual void set_deferred_state() {}
280
281         void reset_panner ();
282
283         virtual uint32_t pans_required() const
284                 { return _inputs.count().n_audio(); }
285
286         boost::shared_ptr<GainControl> _gain_control;
287
288         virtual void   set_gain (gain_t g, void *src);
289         void           inc_gain (gain_t delta, void *src);
290
291         bool apply_gain_automation;
292         
293         virtual int load_automation (std::string path);
294
295         /* AudioTrack::deprecated_use_diskstream_connections() needs these */
296
297         int set_inputs (const string& str);
298         int set_outputs (const string& str);
299
300         static bool connecting_legal;
301         static bool ports_legal;
302
303         BufferSet& output_buffers() { return *_output_buffers; }
304
305   private:
306
307         friend class Send;
308
309         /* are these the best variable names ever, or what? */
310
311         sigc::connection input_bundle_configuration_connection;
312         sigc::connection output_bundle_configuration_connection;
313         sigc::connection input_bundle_connection_connection;
314         sigc::connection output_bundle_connection_connection;
315
316         static bool panners_legal;
317         
318         int connecting_became_legal ();
319         int panners_became_legal ();
320         sigc::connection connection_legal_c;
321         sigc::connection port_legal_c;
322         sigc::connection panner_legal_c;
323
324         ChanCount _input_minimum;
325         ChanCount _input_maximum;
326         ChanCount _output_minimum;
327         ChanCount _output_maximum;
328
329
330         static int parse_io_string (const string&, vector<string>& chns);
331
332         static int parse_gain_string (const string&, vector<string>& chns);
333         
334         int set_sources (vector<string>&, void *src, bool add);
335         int set_destinations (vector<string>&, void *src, bool add);
336
337         int ensure_inputs (ChanCount, bool clear, bool lockit, void *src);
338         int ensure_outputs (ChanCount, bool clear, bool lockit, void *src);
339
340         void drop_input_bundle ();
341         void drop_output_bundle ();
342
343         void input_bundle_configuration_changed ();
344         void input_bundle_connection_changed (int);
345         void output_bundle_configuration_changed ();
346         void output_bundle_connection_changed (int);
347
348         int create_ports (const XMLNode&);
349         int make_connections (const XMLNode&);
350
351         void setup_peak_meters ();
352         void meter ();
353
354         bool ensure_inputs_locked (ChanCount, bool clear, void *src);
355         bool ensure_outputs_locked (ChanCount, bool clear, void *src);
356
357         int32_t find_input_port_hole ();
358         int32_t find_output_port_hole ();
359 };
360
361 } // namespace ARDOUR
362
363 #endif /*__ardour_io_h__ */