meet rhythm ferret: cute, furry and always on time (ardour build now requires fftw3...
[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         bool active() const { return _active; }
76         void set_active (bool yn);
77
78         int input_minimum() const { return _input_minimum; }
79         int input_maximum() const { return _input_maximum; }
80         int output_minimum() const { return _output_minimum; }
81         int output_maximum() const { return _output_maximum; }
82
83         void set_input_minimum (int n);
84         void set_input_maximum (int n);
85         void set_output_minimum (int n);
86         void set_output_maximum (int n);
87
88         DataType default_type() const { return _default_type; }
89
90         const string& name() const { return _name; }
91         virtual int set_name (string str, void *src);
92         
93         virtual void silence  (nframes_t, nframes_t offset);
94
95         // These should be moved in to a separate object that manipulates an IO
96         
97         void pan (vector<Sample*>& bufs, uint32_t nbufs, nframes_t nframes, nframes_t offset, gain_t gain_coeff);
98         void pan_automated (vector<Sample*>& bufs, uint32_t nbufs, nframes_t start_frame, nframes_t end_frame, 
99                             nframes_t nframes, nframes_t offset);
100         void collect_input  (vector<Sample*>&, uint32_t nbufs, nframes_t nframes, nframes_t offset);
101         void deliver_output (vector<Sample*>&, uint32_t nbufs, nframes_t nframes, nframes_t offset);
102         void deliver_output_no_pan (vector<Sample*>&, uint32_t nbufs, nframes_t nframes, nframes_t offset);
103         void just_meter_input (nframes_t start_frame, nframes_t end_frame, 
104                                nframes_t nframes, nframes_t offset);
105
106         virtual uint32_t n_process_buffers () { return 0; }
107
108         virtual void   set_gain (gain_t g, void *src);
109         void           inc_gain (gain_t delta, void *src);
110         gain_t         gain () const { return _desired_gain; }
111         virtual gain_t effective_gain () const;
112
113         Panner& panner() { return *_panner; }
114         const Panner& panner() const { return *_panner; }
115         
116         int ensure_io (uint32_t, uint32_t, bool clear, void *src);
117
118         int use_input_connection (Connection&, void *src);
119         int use_output_connection (Connection&, void *src);
120
121         Connection *input_connection() const { return _input_connection; }
122         Connection *output_connection() const { return _output_connection; }
123
124         int add_input_port (string source, void *src, DataType type = DataType::NIL);
125         int add_output_port (string destination, void *src, DataType type = DataType::NIL);
126
127         int remove_input_port (Port *, void *src);
128         int remove_output_port (Port *, void *src);
129
130         int set_input (Port *, void *src);
131
132         int connect_input (Port *our_port, string other_port, void *src);
133         int connect_output (Port *our_port, string other_port, void *src);
134
135         int disconnect_input (Port *our_port, string other_port, void *src);
136         int disconnect_output (Port *our_port, string other_port, void *src);
137
138         int disconnect_inputs (void *src);
139         int disconnect_outputs (void *src);
140
141         nframes_t output_latency() const;
142         nframes_t input_latency() const;
143         void           set_port_latency (nframes_t);
144
145         Port *output (uint32_t n) const {
146                 if (n < _noutputs) {
147                         return _outputs[n];
148                 } else {
149                         return 0;
150                 }
151         }
152
153         Port *input (uint32_t n) const {
154                 if (n < _ninputs) {
155                         return _inputs[n];
156                 } else {
157                         return 0;
158                 }
159         }
160
161         uint32_t n_inputs () const { return _ninputs; }
162         uint32_t n_outputs () const { return _noutputs; }
163
164         sigc::signal<void>                active_changed;
165
166         sigc::signal<void,IOChange,void*> input_changed;
167         sigc::signal<void,IOChange,void*> output_changed;
168
169         sigc::signal<void,void*> gain_changed;
170         sigc::signal<void,void*> name_changed;
171
172         virtual XMLNode& state (bool full);
173         XMLNode& get_state (void);
174         int set_state (const XMLNode&);
175
176         static int  disable_connecting (void);
177         static int  enable_connecting (void);
178         static int  disable_ports (void);
179         static int  enable_ports (void);
180         static int  disable_panners (void);
181         static int  reset_panners (void);
182         
183         static sigc::signal<int> PortsLegal;
184         static sigc::signal<int> PannersLegal;
185         static sigc::signal<int> ConnectingLegal;
186         static sigc::signal<void,uint32_t> MoreOutputs;
187         static sigc::signal<int> PortsCreated;
188
189         PBD::Controllable& gain_control() {
190                 return _gain_control;
191         }
192
193         const PBD::Controllable& gain_control() const {
194                 return _gain_control;
195         }
196         
197         /* Peak metering */
198
199         float peak_input_power (uint32_t n) { 
200                 if (n < std::max (_ninputs, _noutputs)) {
201                         return _visible_peak_power[n];
202                 } else {
203                         return minus_infinity();
204                 }
205         }
206
207         float max_peak_power (uint32_t n) {
208                 if (n < std::max (_ninputs, _noutputs)) {
209                         return _max_peak_power[n];
210                 } else {
211                         return minus_infinity();
212                 }
213         }
214
215         void reset_max_peak_meters ();
216
217         
218         static void update_meters();
219
220   private: 
221
222         static sigc::signal<void>   Meter;
223         static Glib::StaticMutex    m_meter_signal_lock;
224         sigc::connection            m_meter_connection;
225
226   public:
227
228          bool                     _active;
229
230          /* automation */
231          
232          static void set_automation_interval (jack_nframes_t frames) {
233                  _automation_interval = frames;
234          }
235          
236          static jack_nframes_t automation_interval() { 
237                  return _automation_interval;
238          }
239          
240          void clear_automation ();
241          
242          bool gain_automation_recording() const { 
243                  return (_gain_automation_curve.automation_state() & (Write|Touch));
244          }
245          
246          bool gain_automation_playback() const {
247                  return (_gain_automation_curve.automation_state() & Play) ||
248                          ((_gain_automation_curve.automation_state() & Touch) && 
249                           !_gain_automation_curve.touching());
250          }
251
252          virtual void set_gain_automation_state (AutoState);
253         AutoState gain_automation_state() const { return _gain_automation_curve.automation_state(); }
254         sigc::signal<void> gain_automation_state_changed;
255
256         virtual void set_gain_automation_style (AutoStyle);
257         AutoStyle gain_automation_style () const { return _gain_automation_curve.automation_style(); }
258         sigc::signal<void> gain_automation_style_changed;
259
260         virtual void transport_stopped (nframes_t now);
261         void automation_snapshot (nframes_t now);
262
263         ARDOUR::Curve& gain_automation_curve () { return _gain_automation_curve; }
264
265         void start_gain_touch ();
266         void end_gain_touch ();
267
268         void start_pan_touch (uint32_t which);
269         void end_pan_touch (uint32_t which);
270
271         void defer_pan_reset ();
272         void allow_pan_reset ();
273
274         /* the session calls this for master outs before
275            anyone else. controls outs too, at some point.
276         */
277
278         XMLNode *pending_state_node;
279         int ports_became_legal ();
280
281   private:
282         mutable Glib::Mutex io_lock;
283
284   protected:
285         Session&            _session;
286         Panner*             _panner;
287         gain_t              _gain;
288         gain_t              _effective_gain;
289         gain_t              _desired_gain;
290         Glib::Mutex         declick_lock;
291         vector<Port*>       _outputs;
292         vector<Port*>       _inputs;
293         vector<float>       _peak_power;
294         vector<float>       _visible_peak_power;
295         vector<float>       _max_peak_power;
296         string              _name;
297         Connection*         _input_connection;
298         Connection*         _output_connection;
299         bool                 no_panner_reset;
300         XMLNode*             deferred_state;
301         DataType            _default_type;
302         bool                _ignore_gain_on_deliver;
303         
304
305         virtual void set_deferred_state() {}
306
307         void reset_peak_meters();
308         void reset_panner ();
309
310         virtual uint32_t pans_required() const { return _ninputs; }
311
312         static void apply_declick (vector<Sample*>&, uint32_t nbufs, nframes_t nframes, 
313                                    gain_t initial, gain_t target, bool invert_polarity);
314
315         struct GainControllable : public PBD::Controllable {
316             GainControllable (std::string name, IO& i) : Controllable (name), io (i) {}
317          
318             void set_value (float val);
319             float get_value (void) const;
320    
321             IO& io;
322         };
323
324         GainControllable _gain_control;
325
326         nframes_t last_automation_snapshot;
327         static nframes_t _automation_interval;
328
329         AutoState      _gain_automation_state;
330         AutoStyle      _gain_automation_style;
331
332         bool     apply_gain_automation;
333         Curve    _gain_automation_curve;
334         
335         Glib::Mutex automation_lock;
336
337         virtual int set_automation_state (const XMLNode&);
338         virtual XMLNode& get_automation_state ();
339         virtual int load_automation (std::string path);
340
341         /* AudioTrack::deprecated_use_diskstream_connections() needs these */
342
343         int set_inputs (const string& str);
344         int set_outputs (const string& str);
345
346         static bool connecting_legal;
347         static bool ports_legal;
348
349   private:
350
351         uint32_t _ninputs;
352         uint32_t _noutputs;
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         int _input_minimum;
370         int _input_maximum;
371         int _output_minimum;
372         int _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         std::string build_legal_port_name (bool for_input);
403         int32_t find_input_port_hole (const char* base);
404         int32_t find_output_port_hole (const char* base);
405 };
406
407 } // namespace ARDOUR
408
409 #endif /*__ardour_io_h__ */