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