Merged with trunk R920.
[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/statefuldestructible.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 PeakMeter;
58 class Port;
59 class AudioPort;
60 class MidiPort;
61 class BufferSet;
62
63 /** A collection of input and output ports with connections.
64  *
65  * An IO can contain ports of varying types, making routes/inserts/etc with
66  * varied combinations of types (eg MIDI and audio) possible.
67  */
68 class IO : public PBD::StatefulDestructible, public ARDOUR::StateManager
69 {
70
71   public:
72         static const string state_node_name;
73
74         IO (Session&, string name, 
75             int input_min = -1, int input_max = -1, 
76             int output_min = -1, int output_max = -1,
77                 DataType default_type = DataType::AUDIO);
78         
79         virtual ~IO();
80
81         ChanCount input_minimum() const { return _input_minimum; }
82         ChanCount input_maximum() const { return _input_maximum; }
83         ChanCount output_minimum() const { return _output_minimum; }
84         ChanCount output_maximum() const { return _output_maximum; }
85
86         void set_input_minimum (ChanCount n);
87         void set_input_maximum (ChanCount n);
88         void set_output_minimum (ChanCount n);
89         void set_output_maximum (ChanCount n);
90         
91         // Do not write any new code using these
92         void set_input_minimum (int n);
93         void set_input_maximum (int n);
94         void set_output_minimum (int n);
95         void set_output_maximum (int n);
96
97         DataType default_type() const         { return _default_type; }
98         void     set_default_type(DataType t) { _default_type = t; }
99
100         const string& name() const { return _name; }
101         virtual int set_name (string str, void *src);
102         
103         virtual void silence  (jack_nframes_t, jack_nframes_t offset);
104
105         void collect_input  (BufferSet& bufs, jack_nframes_t nframes, jack_nframes_t offset);
106         void deliver_output (BufferSet& bufs, jack_nframes_t start_frame, jack_nframes_t end_frame,
107                                               jack_nframes_t nframes, jack_nframes_t offset);
108         void just_meter_input (jack_nframes_t start_frame, jack_nframes_t end_frame, 
109                                jack_nframes_t nframes, jack_nframes_t offset);
110
111         virtual void   set_gain (gain_t g, void *src);
112         void           inc_gain (gain_t delta, void *src);
113         gain_t         gain () const { return _desired_gain; }
114         virtual gain_t effective_gain () const;
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
122         int ensure_io (ChanCount in, ChanCount out, 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         const PortSet& inputs()  const { return _inputs; }
152         const PortSet& outputs() const { return _outputs; }
153
154         Port *output (uint32_t n) const {
155                 if (n < _outputs.num_ports()) {
156                         return _outputs.port(n);
157                 } else {
158                         return 0;
159                 }
160         }
161
162         Port *input (uint32_t n) const {
163                 if (n < _inputs.num_ports()) {
164                         return _inputs.port(n);
165                 } else {
166                         return 0;
167                 }
168         }
169
170         AudioPort* audio_input(uint32_t n) const;
171         AudioPort* audio_output(uint32_t n) const;
172         MidiPort*  midi_input(uint32_t n) const;
173         MidiPort*  midi_output(uint32_t n) const;
174
175         const ChanCount& n_inputs ()  const { return _inputs.count(); }
176         const ChanCount& n_outputs () const { return _outputs.count(); }
177
178         void attach_buffers(ChanCount ignored);
179
180         sigc::signal<void,IOChange,void*> input_changed;
181         sigc::signal<void,IOChange,void*> output_changed;
182
183         sigc::signal<void,void*> gain_changed;
184         sigc::signal<void,void*> name_changed;
185
186         virtual XMLNode& state (bool full);
187         XMLNode& get_state (void);
188         int set_state (const XMLNode&);
189
190         virtual UndoAction get_memento() const;
191
192
193         static int  disable_connecting (void);
194
195         static int  enable_connecting (void);
196
197         static int  disable_ports (void);
198
199         static int  enable_ports (void);
200
201         static int  disable_panners (void);
202
203         static int  reset_panners (void);
204         
205         static sigc::signal<int>            PortsLegal;
206         static sigc::signal<int>            PannersLegal;
207         static sigc::signal<int>            ConnectingLegal;
208         static sigc::signal<void,ChanCount> MoreChannels;
209         static sigc::signal<int>            PortsCreated;
210
211         PBD::Controllable& gain_control() {
212                 return _gain_control;
213         }
214
215     static void update_meters();
216
217 private: 
218
219     static sigc::signal<void>   Meter;
220     static Glib::StaticMutex    m_meter_signal_lock;
221     sigc::connection            m_meter_connection;
222
223 public:
224
225         /* automation */
226
227         void clear_automation ();
228
229         bool gain_automation_recording() const { 
230                 return (_gain_automation_curve.automation_state() & (Write|Touch));
231         }
232
233         bool gain_automation_playback() const {
234                 return (_gain_automation_curve.automation_state() & Play) ||
235                         ((_gain_automation_curve.automation_state() & Touch) && 
236                          !_gain_automation_curve.touching());
237         }
238
239         virtual void set_gain_automation_state (AutoState);
240         AutoState gain_automation_state() const { return _gain_automation_curve.automation_state(); }
241         sigc::signal<void> gain_automation_state_changed;
242
243         virtual void set_gain_automation_style (AutoStyle);
244         AutoStyle gain_automation_style () const { return _gain_automation_curve.automation_style(); }
245         sigc::signal<void> gain_automation_style_changed;
246
247         static void set_automation_interval (jack_nframes_t frames) {
248                 _automation_interval = frames;
249         }
250
251         static jack_nframes_t automation_interval() { 
252                 return _automation_interval;
253         }
254
255         virtual void transport_stopped (jack_nframes_t now);
256         virtual void automation_snapshot (jack_nframes_t now);
257
258         ARDOUR::Curve& gain_automation_curve () { return _gain_automation_curve; }
259
260         void start_gain_touch ();
261         void end_gain_touch ();
262
263         void start_pan_touch (uint32_t which);
264         void end_pan_touch (uint32_t which);
265
266         void defer_pan_reset ();
267         void allow_pan_reset ();
268
269         /* the session calls this for master outs before
270            anyone else. controls outs too, at some point.
271         */
272
273         XMLNode *pending_state_node;
274         int ports_became_legal ();
275
276   private:
277         mutable Glib::Mutex io_lock;
278
279   protected:
280         Session&    _session;
281         Panner*     _panner;
282         BufferSet*  _output_buffers; //< Set directly to our output port buffers
283         gain_t      _gain;
284         gain_t      _effective_gain;
285         gain_t      _desired_gain;
286         Glib::Mutex declick_lock;
287         PortSet     _outputs;
288         PortSet     _inputs;
289         PeakMeter*  _meter;
290         string      _name;
291         Connection* _input_connection;
292         Connection* _output_connection;
293         bool         no_panner_reset;
294         bool        _phase_invert;
295         XMLNode*     deferred_state;
296         DataType    _default_type;
297
298         virtual void set_deferred_state() {}
299
300         void reset_panner ();
301
302         virtual uint32_t pans_required() const
303                 { return _inputs.count().get(DataType::AUDIO); }
304
305         struct GainControllable : public PBD::Controllable {
306             GainControllable (IO& i) : io (i) {}
307          
308             void set_value (float val);
309             float get_value (void) const;
310    
311             IO& io;
312         };
313
314         GainControllable _gain_control;
315
316         /* state management */
317
318         Change               restore_state (State&);
319         StateManager::State* state_factory (std::string why) const;
320
321         /* automation */
322
323         jack_nframes_t last_automation_snapshot;
324         static jack_nframes_t _automation_interval;
325
326     AutoState      _gain_automation_state;
327         AutoStyle      _gain_automation_style;
328
329         bool     apply_gain_automation;
330         Curve    _gain_automation_curve;
331         
332         int  save_automation (const string&);
333         int  load_automation (const string&);
334         
335         Glib::Mutex automation_lock;
336
337         /* AudioTrack::deprecated_use_diskstream_connections() needs these */
338
339         int set_inputs (const string& str);
340         int set_outputs (const string& str);
341
342         static bool connecting_legal;
343         static bool ports_legal;
344
345         BufferSet& output_buffers() { return *_output_buffers; }
346
347   private:
348
349         /* are these the best variable names ever, or what? */
350
351         sigc::connection input_connection_configuration_connection;
352         sigc::connection output_connection_configuration_connection;
353         sigc::connection input_connection_connection_connection;
354         sigc::connection output_connection_connection_connection;
355
356         static bool panners_legal;
357         
358         int connecting_became_legal ();
359         int panners_became_legal ();
360         sigc::connection connection_legal_c;
361         sigc::connection port_legal_c;
362         sigc::connection panner_legal_c;
363
364         ChanCount _input_minimum;
365         ChanCount _input_maximum;
366         ChanCount _output_minimum;
367         ChanCount _output_maximum;
368
369
370         static int parse_io_string (const string&, vector<string>& chns);
371
372         static int parse_gain_string (const string&, vector<string>& chns);
373         
374         int set_sources (vector<string>&, void *src, bool add);
375         int set_destinations (vector<string>&, void *src, bool add);
376
377         int ensure_inputs (ChanCount, bool clear, bool lockit, void *src);
378         int ensure_outputs (ChanCount, bool clear, bool lockit, void *src);
379
380         void drop_input_connection ();
381         void drop_output_connection ();
382
383         void input_connection_configuration_changed ();
384         void input_connection_connection_changed (int);
385         void output_connection_configuration_changed ();
386         void output_connection_connection_changed (int);
387
388         int create_ports (const XMLNode&);
389         int make_connections (const XMLNode&);
390
391         void setup_peak_meters ();
392         void meter ();
393
394         bool ensure_inputs_locked (ChanCount, bool clear, void *src);
395         bool ensure_outputs_locked (ChanCount, bool clear, void *src);
396
397         int32_t find_input_port_hole ();
398         int32_t find_output_port_hole ();
399 };
400
401 } // namespace ARDOUR
402
403 #endif /*__ardour_io_h__ */