Finish automation polling. Get rid of strip_from_route.
[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         /* Peak metering */
189
190         float peak_input_power (uint32_t n) { 
191                 if (_ninputs == 0) {
192                         return minus_infinity();
193                 } else if (n < std::max (_ninputs, _noutputs)) {
194                         return _visible_peak_power[n];
195                 } else {
196                         return minus_infinity();
197                 }
198         }
199
200         float max_peak_power (uint32_t n) {
201                 if (_ninputs == 0) {
202                         return minus_infinity();
203                 } else if (n < std::max (_ninputs, _noutputs)) {
204                         return _max_peak_power[n];
205                 } else {
206                         return minus_infinity();
207                 }
208         }
209
210         void reset_max_peak_meters ();
211
212         
213     static void update_meters();
214
215   private: 
216
217     static sigc::signal<void>   Meter;
218     static Glib::StaticMutex    m_meter_signal_lock;
219     sigc::connection            m_meter_connection;
220
221   public:
222
223         /* automation */
224
225         static void set_automation_interval (jack_nframes_t frames) {
226                 _automation_interval = frames;
227         }
228
229         static jack_nframes_t automation_interval() { 
230                 return _automation_interval;
231         }
232
233         void clear_automation ();
234
235         bool gain_automation_recording() const { 
236                 return (_gain_automation_curve.automation_state() & (Write|Touch));
237         }
238
239         bool gain_automation_playback() const {
240                 return (_gain_automation_curve.automation_state() & Play) ||
241                         ((_gain_automation_curve.automation_state() & Touch) && 
242                          !_gain_automation_curve.touching());
243         }
244
245         virtual void set_gain_automation_state (AutoState);
246         AutoState gain_automation_state() const { return _gain_automation_curve.automation_state(); }
247         sigc::signal<void> gain_automation_state_changed;
248
249         virtual void set_gain_automation_style (AutoStyle);
250         AutoStyle gain_automation_style () const { return _gain_automation_curve.automation_style(); }
251         sigc::signal<void> gain_automation_style_changed;
252
253         virtual void transport_stopped (nframes_t now);
254         void automation_snapshot (nframes_t now);
255
256         ARDOUR::Curve& gain_automation_curve () { return _gain_automation_curve; }
257
258         void start_gain_touch ();
259         void end_gain_touch ();
260
261         void start_pan_touch (uint32_t which);
262         void end_pan_touch (uint32_t which);
263
264         void defer_pan_reset ();
265         void allow_pan_reset ();
266
267         /* the session calls this for master outs before
268            anyone else. controls outs too, at some point.
269         */
270
271         XMLNode *pending_state_node;
272         int ports_became_legal ();
273
274   private:
275         mutable Glib::Mutex io_lock;
276
277   protected:
278         Session&            _session;
279         Panner*             _panner;
280         gain_t              _gain;
281         gain_t              _effective_gain;
282         gain_t              _desired_gain;
283         Glib::Mutex         declick_lock;
284         vector<Port*>       _outputs;
285         vector<Port*>       _inputs;
286         vector<float>       _peak_power;
287         vector<float>       _visible_peak_power;
288         vector<float>       _max_peak_power;
289         string              _name;
290         Connection*         _input_connection;
291         Connection*         _output_connection;
292         bool                 no_panner_reset;
293         XMLNode*             deferred_state;
294         DataType            _default_type;
295         bool                _ignore_gain_on_deliver;
296         
297
298         virtual void set_deferred_state() {}
299
300         void reset_peak_meters();
301         void reset_panner ();
302
303         virtual uint32_t pans_required() const { return _ninputs; }
304
305         static void apply_declick (vector<Sample*>&, uint32_t nbufs, nframes_t nframes, 
306                                    gain_t initial, gain_t target, bool invert_polarity);
307
308         struct GainControllable : public PBD::Controllable {
309             GainControllable (std::string name, IO& i) : Controllable (name), io (i) {}
310          
311             void set_value (float val);
312             float get_value (void) const;
313    
314             IO& io;
315         };
316
317         GainControllable _gain_control;
318
319         nframes_t last_automation_snapshot;
320         static nframes_t _automation_interval;
321
322         AutoState      _gain_automation_state;
323         AutoStyle      _gain_automation_style;
324
325         bool     apply_gain_automation;
326         Curve    _gain_automation_curve;
327         
328         Glib::Mutex automation_lock;
329
330         virtual int set_automation_state (const XMLNode&);
331         virtual XMLNode& get_automation_state ();
332         virtual int load_automation (std::string path);
333
334         /* AudioTrack::deprecated_use_diskstream_connections() needs these */
335
336         int set_inputs (const string& str);
337         int set_outputs (const string& str);
338
339         static bool connecting_legal;
340         static bool ports_legal;
341
342   private:
343
344         uint32_t _ninputs;
345         uint32_t _noutputs;
346
347         /* are these the best variable names ever, or what? */
348
349         sigc::connection input_connection_configuration_connection;
350         sigc::connection output_connection_configuration_connection;
351         sigc::connection input_connection_connection_connection;
352         sigc::connection output_connection_connection_connection;
353
354         static bool panners_legal;
355         
356         int connecting_became_legal ();
357         int panners_became_legal ();
358         sigc::connection connection_legal_c;
359         sigc::connection port_legal_c;
360         sigc::connection panner_legal_c;
361
362         int _input_minimum;
363         int _input_maximum;
364         int _output_minimum;
365         int _output_maximum;
366
367
368         static int parse_io_string (const string&, vector<string>& chns);
369
370         static int parse_gain_string (const string&, vector<string>& chns);
371         
372         int set_sources (vector<string>&, void *src, bool add);
373         int set_destinations (vector<string>&, void *src, bool add);
374
375         int ensure_inputs (uint32_t, bool clear, bool lockit, void *src);
376         int ensure_outputs (uint32_t, bool clear, bool lockit, void *src);
377
378         void drop_input_connection ();
379         void drop_output_connection ();
380
381         void input_connection_configuration_changed ();
382         void input_connection_connection_changed (int);
383         void output_connection_configuration_changed ();
384         void output_connection_connection_changed (int);
385
386         int create_ports (const XMLNode&);
387         int make_connections (const XMLNode&);
388
389         void setup_peak_meters ();
390         void meter ();
391
392         bool ensure_inputs_locked (uint32_t, bool clear, void *src);
393         bool ensure_outputs_locked (uint32_t, bool clear, void *src);
394
395         int32_t find_input_port_hole ();
396         int32_t find_output_port_hole ();
397 };
398
399 } // namespace ARDOUR
400
401 #endif /*__ardour_io_h__ */