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