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