- Replaced integer port counts (and input/output maximum/minimum) with ChanCount...
[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     $Id$
19 */
20
21 #ifndef __ardour_io_h__
22 #define __ardour_io_h__
23
24 #include <string>
25 #include <vector>
26 #include <cmath>
27 #include <sigc++/signal.h>
28 #include <jack/jack.h>
29
30 #include <glibmm/thread.h>
31
32 #include <pbd/fastlog.h>
33 #include <pbd/undo.h>
34 #include <pbd/stateful.h> 
35 #include <pbd/controllable.h>
36
37 #include <ardour/ardour.h>
38 #include <ardour/utils.h>
39 #include <ardour/state_manager.h>
40 #include <ardour/curve.h>
41 #include <ardour/types.h>
42 #include <ardour/data_type.h>
43 #include <ardour/port_set.h>
44 #include <ardour/chan_count.h>
45
46 using std::string;
47 using std::vector;
48
49 class XMLNode;
50
51 namespace ARDOUR {
52
53 class Session;
54 class AudioEngine;
55 class Connection;
56 class Panner;
57 class Port;
58 class AudioPort;
59 class MidiPort;
60
61 /** A collection of input and output ports with connections.
62  *
63  * An IO can contain ports of varying types, making routes/inserts/etc with
64  * varied combinations of types (eg MIDI and audio) possible.
65  */
66 class IO : public Stateful, public ARDOUR::StateManager
67 {
68
69   public:
70         static const string state_node_name;
71
72         IO (Session&, string name, 
73             int input_min = -1, int input_max = -1, 
74             int output_min = -1, int output_max = -1,
75                 DataType default_type = DataType::AUDIO);
76         
77         virtual ~IO();
78
79         ChanCount input_minimum() const { return _input_minimum; }
80         ChanCount input_maximum() const { return _input_maximum; }
81         ChanCount output_minimum() const { return _output_minimum; }
82         ChanCount output_maximum() const { return _output_maximum; }
83
84         void set_input_minimum (ChanCount n);
85         void set_input_maximum (ChanCount n);
86         void set_output_minimum (ChanCount n);
87         void set_output_maximum (ChanCount n);
88         
89         // Do not write any new code using these
90         void set_input_minimum (int n);
91         void set_input_maximum (int n);
92         void set_output_minimum (int n);
93         void set_output_maximum (int n);
94
95         DataType default_type() const { return _default_type; }
96
97         const string& name() const { return _name; }
98         virtual int set_name (string str, void *src);
99         
100         virtual void silence  (jack_nframes_t, jack_nframes_t offset);
101
102         // These should be moved in to a separate object that manipulates an IO
103         
104         void pan (vector<Sample*>& bufs, uint32_t nbufs, jack_nframes_t nframes, jack_nframes_t offset, gain_t gain_coeff);
105         void pan_automated (vector<Sample*>& bufs, uint32_t nbufs, jack_nframes_t start_frame, jack_nframes_t end_frame, 
106                             jack_nframes_t nframes, jack_nframes_t offset);
107         void collect_input  (vector<Sample*>&, uint32_t nbufs, jack_nframes_t nframes, jack_nframes_t offset);
108         void deliver_output (vector<Sample*>&, uint32_t nbufs, jack_nframes_t nframes, jack_nframes_t offset);
109         void deliver_output_no_pan (vector<Sample*>&, uint32_t nbufs, jack_nframes_t nframes, jack_nframes_t offset);
110         void just_meter_input (jack_nframes_t start_frame, jack_nframes_t end_frame, 
111                                jack_nframes_t nframes, jack_nframes_t offset);
112
113         virtual uint32_t n_process_buffers () { return 0; }
114
115         virtual void   set_gain (gain_t g, void *src);
116         void           inc_gain (gain_t delta, void *src);
117         gain_t         gain () const { return _desired_gain; }
118         virtual gain_t effective_gain () const;
119
120         Panner& panner() { return *_panner; }
121
122         int ensure_io (uint32_t, uint32_t, bool clear, void *src);
123
124         int use_input_connection (Connection&, void *src);
125         int use_output_connection (Connection&, void *src);
126
127         Connection *input_connection() const { return _input_connection; }
128         Connection *output_connection() const { return _output_connection; }
129
130         int add_input_port (string source, void *src, DataType type = DataType::NIL);
131         int add_output_port (string destination, void *src, DataType type = DataType::NIL);
132
133         int remove_input_port (Port *, void *src);
134         int remove_output_port (Port *, void *src);
135
136         int set_input (Port *, void *src);
137
138         int connect_input (Port *our_port, string other_port, void *src);
139         int connect_output (Port *our_port, string other_port, void *src);
140
141         int disconnect_input (Port *our_port, string other_port, void *src);
142         int disconnect_output (Port *our_port, string other_port, void *src);
143
144         int disconnect_inputs (void *src);
145         int disconnect_outputs (void *src);
146
147         jack_nframes_t output_latency() const;
148         jack_nframes_t input_latency() const;
149         void           set_port_latency (jack_nframes_t);
150
151         Port *output (uint32_t n) const {
152                 if (n < _outputs.num_ports()) {
153                         return _outputs.port(n);
154                 } else {
155                         return 0;
156                 }
157         }
158
159         Port *input (uint32_t n) const {
160                 if (n < _inputs.num_ports()) {
161                         return _inputs.port(n);
162                 } else {
163                         return 0;
164                 }
165         }
166
167         AudioPort* audio_input(uint32_t n) const;
168         AudioPort* audio_output(uint32_t n) const;
169         MidiPort*  midi_input(uint32_t n) const;
170         MidiPort*  midi_output(uint32_t n) const;
171
172         const ChanCount& n_inputs ()  const { return _inputs.chan_count(); }
173         const ChanCount& n_outputs () const { return _outputs.chan_count(); }
174
175         sigc::signal<void,IOChange,void*> input_changed;
176         sigc::signal<void,IOChange,void*> output_changed;
177
178         sigc::signal<void,void*> gain_changed;
179         sigc::signal<void,void*> name_changed;
180
181         virtual XMLNode& state (bool full);
182         XMLNode& get_state (void);
183         int set_state (const XMLNode&);
184
185         virtual UndoAction get_memento() const;
186
187
188         static int  disable_connecting (void);
189
190         static int  enable_connecting (void);
191
192         static int  disable_ports (void);
193
194         static int  enable_ports (void);
195
196         static int  disable_panners (void);
197
198         static int  reset_panners (void);
199         
200         static sigc::signal<int> PortsLegal;
201         static sigc::signal<int> PannersLegal;
202         static sigc::signal<int> ConnectingLegal;
203         static sigc::signal<void,ChanCount> MoreOutputs;
204         static sigc::signal<int> PortsCreated;
205
206         PBD::Controllable& gain_control() {
207                 return _gain_control;
208         }
209
210         /* Peak metering */
211
212         float peak_input_power (uint32_t n) { 
213                 if (n < std::max (_inputs.chan_count().get(DataType::AUDIO),
214                                   _outputs.chan_count().get(DataType::AUDIO))) {
215                         return _visible_peak_power[n];
216                 } else {
217                         return minus_infinity();
218                 }
219         }
220
221     static void update_meters();
222
223 private: 
224
225     static sigc::signal<void>   Meter;
226     static Glib::StaticMutex    m_meter_signal_lock;
227     sigc::connection            m_meter_connection;
228
229 public:
230
231         /* automation */
232
233         void clear_automation ();
234
235         bool gain_automation_recording() const { 
236                 return (_gain_automation_curve.automation_state() & (Write|Touch));
237         }
238
239         bool gain_automation_playback() const {
240                 return (_gain_automation_curve.automation_state() & Play) ||
241                         ((_gain_automation_curve.automation_state() & Touch) && 
242                          !_gain_automation_curve.touching());
243         }
244
245         virtual void set_gain_automation_state (AutoState);
246         AutoState gain_automation_state() const { return _gain_automation_curve.automation_state(); }
247         sigc::signal<void> gain_automation_state_changed;
248
249         virtual void set_gain_automation_style (AutoStyle);
250         AutoStyle gain_automation_style () const { return _gain_automation_curve.automation_style(); }
251         sigc::signal<void> gain_automation_style_changed;
252
253         static void set_automation_interval (jack_nframes_t frames) {
254                 _automation_interval = frames;
255         }
256
257         static jack_nframes_t automation_interval() { 
258                 return _automation_interval;
259         }
260
261         virtual void transport_stopped (jack_nframes_t now);
262         virtual void automation_snapshot (jack_nframes_t now);
263
264         ARDOUR::Curve& gain_automation_curve () { return _gain_automation_curve; }
265
266         void start_gain_touch ();
267         void end_gain_touch ();
268
269         void start_pan_touch (uint32_t which);
270         void end_pan_touch (uint32_t which);
271
272         const PBD::ID& id() const { return _id; }
273
274         void defer_pan_reset ();
275         void allow_pan_reset ();
276
277         /* the session calls this for master outs before
278            anyone else. controls outs too, at some point.
279         */
280
281         XMLNode *pending_state_node;
282         int ports_became_legal ();
283
284   private:
285         mutable Glib::Mutex io_lock;
286
287   protected:
288         Session&      _session;
289         Panner*       _panner;
290         gain_t        _gain;
291         gain_t        _effective_gain;
292         gain_t        _desired_gain;
293         Glib::Mutex   declick_lock;
294         PortSet      _outputs;
295         PortSet      _inputs;
296         vector<float> _peak_power;
297         vector<float> _visible_peak_power;
298         string        _name;
299         Connection*   _input_connection;
300         Connection*   _output_connection;
301         PBD::ID       _id;
302         bool           no_panner_reset;
303         XMLNode*       deferred_state;
304         DataType      _default_type;
305
306         virtual void set_deferred_state() {}
307
308         void reset_peak_meters();
309         void reset_panner ();
310
311         virtual uint32_t pans_required() const
312                 { return _inputs.chan_count().get(DataType::AUDIO); }
313
314         static void apply_declick (vector<Sample*>&, uint32_t nbufs, jack_nframes_t nframes, 
315                                    gain_t initial, gain_t target, bool invert_polarity);
316
317         struct GainControllable : public PBD::Controllable {
318             GainControllable (IO& i) : io (i) {}
319          
320             void set_value (float val);
321             float get_value (void) const;
322    
323             IO& io;
324         };
325
326         GainControllable _gain_control;
327
328         /* state management */
329
330         Change               restore_state (State&);
331         StateManager::State* state_factory (std::string why) const;
332
333         /* automation */
334
335         jack_nframes_t last_automation_snapshot;
336         static jack_nframes_t _automation_interval;
337
338     AutoState      _gain_automation_state;
339         AutoStyle      _gain_automation_style;
340
341         bool     apply_gain_automation;
342         Curve    _gain_automation_curve;
343         
344         int  save_automation (const string&);
345         int  load_automation (const string&);
346         
347         Glib::Mutex automation_lock;
348
349         /* AudioTrack::deprecated_use_diskstream_connections() needs these */
350
351         int set_inputs (const string& str);
352         int set_outputs (const string& str);
353
354         static bool connecting_legal;
355         static bool ports_legal;
356
357   private:
358
359
360         /* are these the best variable names ever, or what? */
361
362         sigc::connection input_connection_configuration_connection;
363         sigc::connection output_connection_configuration_connection;
364         sigc::connection input_connection_connection_connection;
365         sigc::connection output_connection_connection_connection;
366
367         static bool panners_legal;
368         
369         int connecting_became_legal ();
370         int panners_became_legal ();
371         sigc::connection connection_legal_c;
372         sigc::connection port_legal_c;
373         sigc::connection panner_legal_c;
374
375         ChanCount _input_minimum;
376         ChanCount _input_maximum;
377         ChanCount _output_minimum;
378         ChanCount _output_maximum;
379
380
381         static int parse_io_string (const string&, vector<string>& chns);
382
383         static int parse_gain_string (const string&, vector<string>& chns);
384         
385         int set_sources (vector<string>&, void *src, bool add);
386         int set_destinations (vector<string>&, void *src, bool add);
387
388         int ensure_inputs (uint32_t, bool clear, bool lockit, void *src);
389         int ensure_outputs (uint32_t, bool clear, bool lockit, void *src);
390
391         void drop_input_connection ();
392         void drop_output_connection ();
393
394         void input_connection_configuration_changed ();
395         void input_connection_connection_changed (int);
396         void output_connection_configuration_changed ();
397         void output_connection_connection_changed (int);
398
399         int create_ports (const XMLNode&);
400         int make_connections (const XMLNode&);
401
402         void setup_peak_meters ();
403         void meter ();
404
405         bool ensure_inputs_locked (uint32_t, bool clear, void *src);
406         bool ensure_outputs_locked (uint32_t, bool clear, void *src);
407
408         int32_t find_input_port_hole ();
409         int32_t find_output_port_hole ();
410 };
411
412 } // namespace ARDOUR
413
414 #endif /*__ardour_io_h__ */