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