7b5e72742df05066d18749dabcf715d28298ba69
[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 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 Stateful, 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
99         const string& name() const { return _name; }
100         virtual int set_name (string str, void *src);
101         
102         virtual void silence  (jack_nframes_t, jack_nframes_t offset);
103
104         void collect_input  (BufferSet& bufs, jack_nframes_t nframes, jack_nframes_t offset);
105         void deliver_output (BufferSet& bufs, jack_nframes_t start_frame, jack_nframes_t end_frame,
106                                               jack_nframes_t nframes, jack_nframes_t offset);
107         void just_meter_input (jack_nframes_t start_frame, jack_nframes_t end_frame, 
108                                jack_nframes_t nframes, jack_nframes_t offset);
109
110         virtual ChanCount n_process_buffers () { return ChanCount::ZERO; }
111
112         virtual void   set_gain (gain_t g, void *src);
113         void           inc_gain (gain_t delta, void *src);
114         gain_t         gain () const { return _desired_gain; }
115         virtual gain_t effective_gain () const;
116         
117         void set_phase_invert (bool yn, void *src);
118         bool phase_invert() const { return _phase_invert; }
119
120         Panner& panner()        { return *_panner; }
121         PeakMeter& peak_meter() { return *_meter; }
122
123         int ensure_io (uint32_t, uint32_t, bool clear, void *src);
124         int ensure_io (const ChanCount& in, const ChanCount& out, bool clear, void *src);
125
126         int use_input_connection (Connection&, void *src);
127         int use_output_connection (Connection&, void *src);
128
129         Connection *input_connection() const { return _input_connection; }
130         Connection *output_connection() const { return _output_connection; }
131
132         int add_input_port (string source, void *src, DataType type = DataType::NIL);
133         int add_output_port (string destination, void *src, DataType type = DataType::NIL);
134
135         int remove_input_port (Port *, void *src);
136         int remove_output_port (Port *, void *src);
137
138         int set_input (Port *, void *src);
139
140         int connect_input (Port *our_port, string other_port, void *src);
141         int connect_output (Port *our_port, string other_port, void *src);
142
143         int disconnect_input (Port *our_port, string other_port, void *src);
144         int disconnect_output (Port *our_port, string other_port, void *src);
145
146         int disconnect_inputs (void *src);
147         int disconnect_outputs (void *src);
148
149         jack_nframes_t output_latency() const;
150         jack_nframes_t input_latency() const;
151         void           set_port_latency (jack_nframes_t);
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         sigc::signal<void,void*> gain_changed;
186         sigc::signal<void,void*> name_changed;
187
188         virtual XMLNode& state (bool full);
189         XMLNode& get_state (void);
190         int set_state (const XMLNode&);
191
192         virtual UndoAction get_memento() const;
193
194
195         static int  disable_connecting (void);
196
197         static int  enable_connecting (void);
198
199         static int  disable_ports (void);
200
201         static int  enable_ports (void);
202
203         static int  disable_panners (void);
204
205         static int  reset_panners (void);
206         
207         static sigc::signal<int>            PortsLegal;
208         static sigc::signal<int>            PannersLegal;
209         static sigc::signal<int>            ConnectingLegal;
210         static sigc::signal<void,ChanCount> MoreChannels;
211         static sigc::signal<int>            PortsCreated;
212
213         PBD::Controllable& gain_control() {
214                 return _gain_control;
215         }
216
217     static void update_meters();
218
219 private: 
220
221     static sigc::signal<void>   Meter;
222     static Glib::StaticMutex    m_meter_signal_lock;
223     sigc::connection            m_meter_connection;
224
225 public:
226
227         /* automation */
228
229         void clear_automation ();
230
231         bool gain_automation_recording() const { 
232                 return (_gain_automation_curve.automation_state() & (Write|Touch));
233         }
234
235         bool gain_automation_playback() const {
236                 return (_gain_automation_curve.automation_state() & Play) ||
237                         ((_gain_automation_curve.automation_state() & Touch) && 
238                          !_gain_automation_curve.touching());
239         }
240
241         virtual void set_gain_automation_state (AutoState);
242         AutoState gain_automation_state() const { return _gain_automation_curve.automation_state(); }
243         sigc::signal<void> gain_automation_state_changed;
244
245         virtual void set_gain_automation_style (AutoStyle);
246         AutoStyle gain_automation_style () const { return _gain_automation_curve.automation_style(); }
247         sigc::signal<void> gain_automation_style_changed;
248
249         static void set_automation_interval (jack_nframes_t frames) {
250                 _automation_interval = frames;
251         }
252
253         static jack_nframes_t automation_interval() { 
254                 return _automation_interval;
255         }
256
257         virtual void transport_stopped (jack_nframes_t now);
258         virtual void automation_snapshot (jack_nframes_t now);
259
260         ARDOUR::Curve& gain_automation_curve () { return _gain_automation_curve; }
261
262         void start_gain_touch ();
263         void end_gain_touch ();
264
265         void start_pan_touch (uint32_t which);
266         void end_pan_touch (uint32_t which);
267
268         const PBD::ID& id() const { return _id; }
269
270         void defer_pan_reset ();
271         void allow_pan_reset ();
272
273         /* the session calls this for master outs before
274            anyone else. controls outs too, at some point.
275         */
276
277         XMLNode *pending_state_node;
278         int ports_became_legal ();
279
280   private:
281         mutable Glib::Mutex io_lock;
282
283   protected:
284         Session&    _session;
285         Panner*     _panner;
286         BufferSet*  _output_buffers; //< Set directly to our output port buffers
287         gain_t      _gain;
288         gain_t      _effective_gain;
289         gain_t      _desired_gain;
290         Glib::Mutex declick_lock;
291         PortSet     _outputs;
292         PortSet     _inputs;
293         PeakMeter*  _meter;
294         string      _name;
295         Connection* _input_connection;
296         Connection* _output_connection;
297         PBD::ID     _id;
298         bool         no_panner_reset;
299         bool        _phase_invert;
300         XMLNode*     deferred_state;
301         DataType    _default_type;
302
303         virtual void set_deferred_state() {}
304
305         void reset_panner ();
306
307         virtual uint32_t pans_required() const
308                 { return _inputs.count().get(DataType::AUDIO); }
309
310         struct GainControllable : public PBD::Controllable {
311             GainControllable (IO& i) : io (i) {}
312          
313             void set_value (float val);
314             float get_value (void) const;
315    
316             IO& io;
317         };
318
319         GainControllable _gain_control;
320
321         /* state management */
322
323         Change               restore_state (State&);
324         StateManager::State* state_factory (std::string why) const;
325
326         /* automation */
327
328         jack_nframes_t last_automation_snapshot;
329         static jack_nframes_t _automation_interval;
330
331     AutoState      _gain_automation_state;
332         AutoStyle      _gain_automation_style;
333
334         bool     apply_gain_automation;
335         Curve    _gain_automation_curve;
336         
337         int  save_automation (const string&);
338         int  load_automation (const string&);
339         
340         Glib::Mutex automation_lock;
341
342         /* AudioTrack::deprecated_use_diskstream_connections() needs these */
343
344         int set_inputs (const string& str);
345         int set_outputs (const string& str);
346
347         static bool connecting_legal;
348         static bool ports_legal;
349
350         BufferSet& output_buffers() { return *_output_buffers; }
351
352   private:
353
354         /* are these the best variable names ever, or what? */
355
356         sigc::connection input_connection_configuration_connection;
357         sigc::connection output_connection_configuration_connection;
358         sigc::connection input_connection_connection_connection;
359         sigc::connection output_connection_connection_connection;
360
361         static bool panners_legal;
362         
363         int connecting_became_legal ();
364         int panners_became_legal ();
365         sigc::connection connection_legal_c;
366         sigc::connection port_legal_c;
367         sigc::connection panner_legal_c;
368
369         ChanCount _input_minimum;
370         ChanCount _input_maximum;
371         ChanCount _output_minimum;
372         ChanCount _output_maximum;
373
374
375         static int parse_io_string (const string&, vector<string>& chns);
376
377         static int parse_gain_string (const string&, vector<string>& chns);
378         
379         int set_sources (vector<string>&, void *src, bool add);
380         int set_destinations (vector<string>&, void *src, bool add);
381
382         int ensure_inputs (uint32_t, bool clear, bool lockit, void *src);
383         int ensure_outputs (uint32_t, bool clear, bool lockit, void *src);
384
385         void drop_input_connection ();
386         void drop_output_connection ();
387
388         void input_connection_configuration_changed ();
389         void input_connection_connection_changed (int);
390         void output_connection_configuration_changed ();
391         void output_connection_connection_changed (int);
392
393         int create_ports (const XMLNode&);
394         int make_connections (const XMLNode&);
395
396         void setup_peak_meters ();
397         void meter ();
398
399         bool ensure_inputs_locked (uint32_t, bool clear, void *src);
400         bool ensure_outputs_locked (uint32_t, bool clear, void *src);
401
402         int32_t find_input_port_hole ();
403         int32_t find_output_port_hole ();
404 };
405
406 } // namespace ARDOUR
407
408 #endif /*__ardour_io_h__ */