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