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