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