remove duplicated gain_to_slider functions
[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/utils.h>
38 #include <ardour/curve.h>
39 #include <ardour/types.h>
40 #include <ardour/data_type.h>
41
42 using std::string;
43 using std::vector;
44
45 class XMLNode;
46
47 namespace ARDOUR {
48
49 class Session;
50 class AudioEngine;
51 class Port;
52 class Connection;
53 class Panner;
54
55 /** A collection of input and output ports with connections.
56  *
57  * An IO can contain ports of varying types, making routes/inserts/etc with
58  * varied combinations of types (eg MIDI and audio) possible.
59  */
60 class IO : public PBD::StatefulDestructible
61 {
62
63   public:
64         static const string state_node_name;
65
66         IO (Session&, string name, 
67             int input_min = -1, int input_max = -1, 
68             int output_min = -1, int output_max = -1,
69             DataType default_type = DataType::AUDIO);
70
71         IO (Session&, const XMLNode&, DataType default_type = DataType::AUDIO);
72
73         virtual ~IO();
74
75         int input_minimum() const { return _input_minimum; }
76         int input_maximum() const { return _input_maximum; }
77         int output_minimum() const { return _output_minimum; }
78         int output_maximum() const { return _output_maximum; }
79
80         void set_input_minimum (int n);
81         void set_input_maximum (int n);
82         void set_output_minimum (int n);
83         void set_output_maximum (int n);
84
85         DataType default_type() const { return _default_type; }
86
87         const string& name() const { return _name; }
88         virtual int set_name (string str, void *src);
89         
90         virtual void silence  (nframes_t, nframes_t offset);
91
92         // These should be moved in to a separate object that manipulates an IO
93         
94         void pan (vector<Sample*>& bufs, uint32_t nbufs, nframes_t nframes, nframes_t offset, gain_t gain_coeff);
95         void pan_automated (vector<Sample*>& bufs, uint32_t nbufs, nframes_t start_frame, nframes_t end_frame, 
96                             nframes_t nframes, nframes_t offset);
97         void collect_input  (vector<Sample*>&, uint32_t nbufs, nframes_t nframes, nframes_t offset);
98         void deliver_output (vector<Sample*>&, uint32_t nbufs, nframes_t nframes, nframes_t offset);
99         void deliver_output_no_pan (vector<Sample*>&, uint32_t nbufs, nframes_t nframes, nframes_t offset);
100         void just_meter_input (nframes_t start_frame, nframes_t end_frame, 
101                                nframes_t nframes, nframes_t offset);
102
103         virtual uint32_t n_process_buffers () { return 0; }
104
105         virtual void   set_gain (gain_t g, void *src);
106         void           inc_gain (gain_t delta, void *src);
107         gain_t         gain () const { return _desired_gain; }
108         virtual gain_t effective_gain () const;
109
110         Panner& panner() { return *_panner; }
111         const Panner& panner() const { 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         const PBD::Controllable& gain_control() const {
189                 return _gain_control;
190         }
191         
192         /* Peak metering */
193
194         float peak_input_power (uint32_t n) { 
195                 if (_ninputs == 0) {
196                         return minus_infinity();
197                 } else if (n < std::max (_ninputs, _noutputs)) {
198                         return _visible_peak_power[n];
199                 } else {
200                         return minus_infinity();
201                 }
202         }
203
204         float max_peak_power (uint32_t n) {
205                 if (_ninputs == 0) {
206                         return minus_infinity();
207                 } else if (n < std::max (_ninputs, _noutputs)) {
208                         return _max_peak_power[n];
209                 } else {
210                         return minus_infinity();
211                 }
212         }
213
214         void reset_max_peak_meters ();
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         static void set_automation_interval (jack_nframes_t frames) {
230                 _automation_interval = frames;
231         }
232
233         static jack_nframes_t automation_interval() { 
234                 return _automation_interval;
235         }
236
237         void clear_automation ();
238
239         bool gain_automation_recording() const { 
240                 return (_gain_automation_curve.automation_state() & (Write|Touch));
241         }
242
243         bool gain_automation_playback() const {
244                 return (_gain_automation_curve.automation_state() & Play) ||
245                         ((_gain_automation_curve.automation_state() & Touch) && 
246                          !_gain_automation_curve.touching());
247         }
248
249         virtual void set_gain_automation_state (AutoState);
250         AutoState gain_automation_state() const { return _gain_automation_curve.automation_state(); }
251         sigc::signal<void> gain_automation_state_changed;
252
253         virtual void set_gain_automation_style (AutoStyle);
254         AutoStyle gain_automation_style () const { return _gain_automation_curve.automation_style(); }
255         sigc::signal<void> gain_automation_style_changed;
256
257         virtual void transport_stopped (nframes_t now);
258         void automation_snapshot (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         void defer_pan_reset ();
269         void allow_pan_reset ();
270
271         /* the session calls this for master outs before
272            anyone else. controls outs too, at some point.
273         */
274
275         XMLNode *pending_state_node;
276         int ports_became_legal ();
277
278   private:
279         mutable Glib::Mutex io_lock;
280
281   protected:
282         Session&            _session;
283         Panner*             _panner;
284         gain_t              _gain;
285         gain_t              _effective_gain;
286         gain_t              _desired_gain;
287         Glib::Mutex         declick_lock;
288         vector<Port*>       _outputs;
289         vector<Port*>       _inputs;
290         vector<float>       _peak_power;
291         vector<float>       _visible_peak_power;
292         vector<float>       _max_peak_power;
293         string              _name;
294         Connection*         _input_connection;
295         Connection*         _output_connection;
296         bool                 no_panner_reset;
297         XMLNode*             deferred_state;
298         DataType            _default_type;
299         bool                _ignore_gain_on_deliver;
300         
301
302         virtual void set_deferred_state() {}
303
304         void reset_peak_meters();
305         void reset_panner ();
306
307         virtual uint32_t pans_required() const { return _ninputs; }
308
309         static void apply_declick (vector<Sample*>&, uint32_t nbufs, nframes_t nframes, 
310                                    gain_t initial, gain_t target, bool invert_polarity);
311
312         struct GainControllable : public PBD::Controllable {
313             GainControllable (std::string name, IO& i) : Controllable (name), io (i) {}
314          
315             void set_value (float val);
316             float get_value (void) const;
317    
318             IO& io;
319         };
320
321         GainControllable _gain_control;
322
323         nframes_t last_automation_snapshot;
324         static 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         Glib::Mutex automation_lock;
333
334         virtual int set_automation_state (const XMLNode&);
335         virtual XMLNode& get_automation_state ();
336         virtual int load_automation (std::string path);
337
338         /* AudioTrack::deprecated_use_diskstream_connections() needs these */
339
340         int set_inputs (const string& str);
341         int set_outputs (const string& str);
342
343         static bool connecting_legal;
344         static bool ports_legal;
345
346   private:
347
348         uint32_t _ninputs;
349         uint32_t _noutputs;
350
351         /* are these the best variable names ever, or what? */
352
353         sigc::connection input_connection_configuration_connection;
354         sigc::connection output_connection_configuration_connection;
355         sigc::connection input_connection_connection_connection;
356         sigc::connection output_connection_connection_connection;
357
358         static bool panners_legal;
359         
360         int connecting_became_legal ();
361         int panners_became_legal ();
362         sigc::connection connection_legal_c;
363         sigc::connection port_legal_c;
364         sigc::connection panner_legal_c;
365
366         int _input_minimum;
367         int _input_maximum;
368         int _output_minimum;
369         int _output_maximum;
370
371
372         static int parse_io_string (const string&, vector<string>& chns);
373
374         static int parse_gain_string (const string&, vector<string>& chns);
375         
376         int set_sources (vector<string>&, void *src, bool add);
377         int set_destinations (vector<string>&, void *src, bool add);
378
379         int ensure_inputs (uint32_t, bool clear, bool lockit, void *src);
380         int ensure_outputs (uint32_t, bool clear, bool lockit, void *src);
381
382         void drop_input_connection ();
383         void drop_output_connection ();
384
385         void input_connection_configuration_changed ();
386         void input_connection_connection_changed (int);
387         void output_connection_configuration_changed ();
388         void output_connection_connection_changed (int);
389
390         int create_ports (const XMLNode&);
391         int make_connections (const XMLNode&);
392
393         void setup_peak_meters ();
394         void meter ();
395
396         bool ensure_inputs_locked (uint32_t, bool clear, void *src);
397         bool ensure_outputs_locked (uint32_t, bool clear, void *src);
398
399         int32_t find_input_port_hole ();
400         int32_t find_output_port_hole ();
401 };
402
403 } // namespace ARDOUR
404
405 #endif /*__ardour_io_h__ */