Use AudioFile.h instead of ExtAudioFile.h.
[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 <pbd/lockmonitor.h>
31 #include <pbd/fastlog.h>
32 #include <pbd/undo.h>
33 #include <pbd/atomic.h>
34 #include <midi++/controllable.h>
35
36 #include <ardour/ardour.h>
37 #include <ardour/stateful.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         /* MIDI control */
180
181         void set_midi_to_gain_function (gain_t (*function)(double val)) {
182                 _midi_gain_control.midi_to_gain = function;
183         }
184
185         void set_gain_to_midi_function (double (*function)(gain_t gain)) {
186                 _midi_gain_control.gain_to_midi = function;
187         }
188
189         MIDI::Controllable& midi_gain_control() {
190                 return _midi_gain_control;
191         }
192
193         virtual void reset_midi_control (MIDI::Port*, bool on);
194
195         virtual void send_all_midi_feedback ();
196         virtual MIDI::byte* write_midi_feedback (MIDI::byte*, int32_t& bufsize);
197         
198         /* Peak metering */
199
200         float peak_input_power (uint32_t n) { 
201                 if (n < std::max(_ninputs, _noutputs)) {
202                         float x = _stored_peak_power[n];
203                         if(x > 0.0) {
204                                 return 20 * fast_log10(x);
205                         } else {
206                                 return minus_infinity();
207                         }
208                 } else {
209                         return minus_infinity();
210                 }
211         }
212
213         static sigc::signal<void> GrabPeakPower;
214
215         /* automation */
216
217         void clear_automation ();
218
219         bool gain_automation_recording() const { 
220                 return (_gain_automation_curve.automation_state() & (Write|Touch));
221         }
222
223         bool gain_automation_playback() const {
224                 return (_gain_automation_curve.automation_state() & Play) ||
225                         ((_gain_automation_curve.automation_state() & Touch) && 
226                          !_gain_automation_curve.touching());
227         }
228
229         virtual void set_gain_automation_state (AutoState);
230         AutoState gain_automation_state() const { return _gain_automation_curve.automation_state(); }
231         sigc::signal<void> gain_automation_state_changed;
232
233         virtual void set_gain_automation_style (AutoStyle);
234         AutoStyle gain_automation_style () const { return _gain_automation_curve.automation_style(); }
235         sigc::signal<void> gain_automation_style_changed;
236
237         static void set_automation_interval (jack_nframes_t frames) {
238                 _automation_interval = frames;
239         }
240
241         static jack_nframes_t automation_interval() { 
242                 return _automation_interval;
243         }
244
245         virtual void transport_stopped (jack_nframes_t now);
246         virtual void automation_snapshot (jack_nframes_t now);
247
248         ARDOUR::Curve& gain_automation_curve () { return _gain_automation_curve; }
249
250         void start_gain_touch ();
251         void end_gain_touch ();
252
253         void start_pan_touch (uint32_t which);
254         void end_pan_touch (uint32_t which);
255
256         id_t id() const { return _id; }
257
258         void defer_pan_reset ();
259         void allow_pan_reset ();
260
261         /* the session calls this for master outs before
262            anyone else. controls outs too, at some point.
263         */
264
265         XMLNode *pending_state_node;
266         int ports_became_legal ();
267
268   private:
269         mutable PBD::Lock io_lock;
270
271   protected:
272         Session&            _session;
273         Panner*             _panner;
274         gain_t              _gain;
275         gain_t              _effective_gain;
276         gain_t              _desired_gain;
277         PBD::NonBlockingLock declick_lock;
278         vector<Port*>       _outputs;
279         vector<Port*>       _inputs;
280         vector<float>       _peak_power;
281         vector<float>       _stored_peak_power;
282         string              _name;
283         Connection*         _input_connection;
284         Connection*         _output_connection;
285         id_t                _id;
286         bool                 no_panner_reset;
287         XMLNode*             deferred_state;
288
289         virtual void set_deferred_state() {}
290
291         void reset_peak_meters();
292         void reset_panner ();
293
294         virtual uint32_t pans_required() const { return _ninputs; }
295
296         static void apply_declick (vector<Sample*>&, uint32_t nbufs, jack_nframes_t nframes, 
297                                    gain_t initial, gain_t target, bool invert_polarity);
298
299         struct MIDIGainControl : public MIDI::Controllable {
300             MIDIGainControl (IO&, MIDI::Port *);
301             void set_value (float);
302
303             void send_feedback (gain_t);
304             MIDI::byte* write_feedback (MIDI::byte* buf, int32_t& bufsize, gain_t val, bool force = false);
305
306             IO& io;
307             bool setting;
308             MIDI::byte last_written;
309
310             gain_t (*midi_to_gain) (double val);
311             double (*gain_to_midi) (gain_t gain);
312         };
313
314         MIDIGainControl _midi_gain_control;
315
316         /* state management */
317
318         Change               restore_state (State&);
319         StateManager::State* state_factory (std::string why) const;
320         void                 send_state_changed();
321
322         bool get_midi_node_info (XMLNode * node, MIDI::eventType & ev, MIDI::channel_t & chan, MIDI::byte & additional);
323         bool set_midi_node_info (XMLNode * node, MIDI::eventType ev, MIDI::channel_t chan, MIDI::byte additional);
324         
325         /* automation */
326
327         jack_nframes_t last_automation_snapshot;
328         static jack_nframes_t _automation_interval;
329
330         AutoState      _gain_automation_state;
331         AutoStyle      _gain_automation_style;
332
333         bool     apply_gain_automation;
334         Curve    _gain_automation_curve;
335         
336         int  save_automation (const string&);
337         int  load_automation (const string&);
338         
339         PBD::NonBlockingLock automation_lock;
340
341         /* AudioTrack::deprecated_use_diskstream_connections() needs these */
342
343         int set_inputs (const string& str);
344         int set_outputs (const string& str);
345
346         static bool connecting_legal;
347         static bool ports_legal;
348
349   private:
350
351         uint32_t _ninputs;
352         uint32_t _noutputs;
353
354         /* are these the best variable names ever, or what? */
355
356         sigc::connection input_connection_configuration_connection;
357         sigc::connection output_connection_configuration_connection;
358         sigc::connection input_connection_connection_connection;
359         sigc::connection output_connection_connection_connection;
360
361         static bool panners_legal;
362         
363         int connecting_became_legal ();
364         int panners_became_legal ();
365         sigc::connection connection_legal_c;
366         sigc::connection port_legal_c;
367         sigc::connection panner_legal_c;
368
369         int _input_minimum;
370         int _input_maximum;
371         int _output_minimum;
372         int _output_maximum;
373
374
375         static int parse_io_string (const string&, vector<string>& chns);
376
377         static int parse_gain_string (const string&, vector<string>& chns);
378         
379         int set_sources (vector<string>&, void *src, bool add);
380         int set_destinations (vector<string>&, void *src, bool add);
381
382         int ensure_inputs (uint32_t, bool clear, bool lockit, void *src);
383         int ensure_outputs (uint32_t, bool clear, bool lockit, void *src);
384
385         void drop_input_connection ();
386         void drop_output_connection ();
387
388         void input_connection_configuration_changed ();
389         void input_connection_connection_changed (int);
390         void output_connection_configuration_changed ();
391         void output_connection_connection_changed (int);
392
393         int create_ports (const XMLNode&);
394         int make_connections (const XMLNode&);
395
396         void setup_peak_meters ();
397         void grab_peak_power ();
398
399         bool ensure_inputs_locked (uint32_t, bool clear, void *src);
400         bool ensure_outputs_locked (uint32_t, bool clear, void *src);
401
402         int32_t find_input_port_hole ();
403         int32_t find_output_port_hole ();
404 };
405
406 }; /* namespace ARDOUR */
407
408 #endif /*__ardour_io_h__ */