more work on the new all-Processor-all-The-Time redesign of Route - LOTS OF BREAKAGE...
[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/automatable_controls.h"
38 #include "ardour/automation_control.h"
39 #include "ardour/bundle.h"
40 #include "ardour/chan_count.h"
41 #include "ardour/data_type.h"
42 #include "ardour/latent.h"
43 #include "ardour/port_set.h"
44 #include "ardour/session_object.h"
45 #include "ardour/types.h"
46 #include "ardour/utils.h"
47
48 class XMLNode;
49
50 namespace ARDOUR {
51
52 class Amp;
53 class AudioEngine;
54 class AudioPort;
55 class BufferSet;
56 class Bundle;
57 class MidiPort;
58 class Panner;
59 class PeakMeter;
60 class Port;
61 class Session;
62 class UserBundle;
63
64 /** A collection of input and output ports with connections.
65  *
66  * An IO can contain ports of varying types, making routes/inserts/etc with
67  * varied combinations of types (eg MIDI and audio) possible.
68  */
69 class IO : public SessionObject, public AutomatableControls, public Latent
70 {
71   public:
72         static const std::string state_node_name;
73
74         IO (Session&, const std::string& name, DataType default_type = DataType::AUDIO,
75                 ChanCount in_min=ChanCount::ZERO, ChanCount in_max=ChanCount::INFINITE,
76                 ChanCount out_min=ChanCount::ZERO, ChanCount out_max=ChanCount::INFINITE);
77         
78         IO (Session&, const XMLNode&, DataType default_type = DataType::AUDIO);
79         
80         virtual ~IO();
81
82         ChanCount input_minimum() const { return _input_minimum; }
83         ChanCount input_maximum() const { return _input_maximum; }
84         ChanCount output_minimum() const { return _output_minimum; }
85         ChanCount output_maximum() const { return _output_maximum; }
86         
87         void set_input_minimum (ChanCount n);
88         void set_input_maximum (ChanCount n);
89         void set_output_minimum (ChanCount n);
90         void set_output_maximum (ChanCount n);
91         
92         bool active() const { return _active; }
93         void set_active (bool yn);
94         
95         DataType default_type() const         { return _default_type; }
96         void     set_default_type(DataType t) { _default_type = t; }
97         
98         bool set_name (const std::string& str);
99
100         virtual void silence  (nframes_t);
101
102         void collect_input  (BufferSet& bufs, nframes_t nframes, ChanCount offset=ChanCount::ZERO);
103         void deliver_output (BufferSet& bufs, sframes_t start_frame, sframes_t end_frame, nframes_t nframes);
104         void just_meter_input (sframes_t start_frame, sframes_t end_frame, nframes_t nframes);
105
106         BufferSet& output_buffers() { return *_output_buffers; }
107
108         gain_t         gain () const { return _gain_control->user_float(); }
109         virtual gain_t effective_gain () const;
110         
111         void set_denormal_protection (bool yn, void *src);
112         bool denormal_protection() const { return _denormal_protection; }
113         
114         void set_phase_invert (bool yn, void *src);
115         bool phase_invert() const { return _phase_invert; }
116
117         void reset_panner ();
118         
119         boost::shared_ptr<Amp> amp() const { return _amp; }
120
121         PeakMeter&       peak_meter()       { return *_meter.get(); }
122         const PeakMeter& peak_meter() const { return *_meter.get(); }
123         boost::shared_ptr<PeakMeter> shared_peak_meter() const { return _meter; }
124
125         boost::shared_ptr<Panner> panner() const { return _panner; }
126         
127         int ensure_io (ChanCount in, ChanCount out, bool clear, void *src);
128
129         int connect_input_ports_to_bundle (boost::shared_ptr<Bundle>, void *);
130         int disconnect_input_ports_from_bundle (boost::shared_ptr<Bundle>, void *);
131         int connect_output_ports_to_bundle (boost::shared_ptr<Bundle>, void *);
132         int disconnect_output_ports_from_bundle (boost::shared_ptr<Bundle>, void *);
133
134         BundleList bundles_connected_to_inputs ();
135         BundleList bundles_connected_to_outputs ();
136
137         boost::shared_ptr<Bundle> bundle_for_inputs () { return _bundle_for_inputs; }
138         boost::shared_ptr<Bundle> bundle_for_outputs () { return _bundle_for_outputs; }
139         
140         int add_input_port (std::string source, void *src, DataType type = DataType::NIL);
141         int add_output_port (std::string destination, void *src, DataType type = DataType::NIL);
142
143         int remove_input_port (Port *, void *src);
144         int remove_output_port (Port *, void *src);
145
146         int set_input (Port *, void *src);
147
148         int connect_input (Port *our_port, std::string other_port, void *src);
149         int connect_output (Port *our_port, std::string other_port, void *src);
150
151         int disconnect_input (Port *our_port, std::string other_port, void *src);
152         int disconnect_output (Port *our_port, std::string other_port, void *src);
153
154         int disconnect_inputs (void *src);
155         int disconnect_outputs (void *src);
156
157         nframes_t signal_latency() const { return _own_latency; }
158         nframes_t output_latency() const;
159         nframes_t input_latency() const;
160         void      set_port_latency (nframes_t);
161
162         void update_port_total_latencies ();
163
164         const PortSet& inputs()  const { return _inputs; }
165         const PortSet& outputs() const { return _outputs; }
166
167         Port *output (uint32_t n) const {
168                 if (n < _outputs.num_ports()) {
169                         return _outputs.port(n);
170                 } else {
171                         return 0;
172                 }
173         }
174
175         Port *input (uint32_t n) const {
176                 if (n < _inputs.num_ports()) {
177                         return _inputs.port(n);
178                 } else {
179                         return 0;
180                 }
181         }
182
183         AudioPort* audio_input(uint32_t n) const;
184         AudioPort* audio_output(uint32_t n) const;
185         MidiPort*  midi_input(uint32_t n) const;
186         MidiPort*  midi_output(uint32_t n) const;
187
188         const ChanCount& n_inputs ()  const { return _inputs.count(); }
189         const ChanCount& n_outputs () const { return _outputs.count(); }
190
191         void attach_buffers(ChanCount ignored);
192         
193         sigc::signal<void>                active_changed;
194
195         sigc::signal<void,IOChange,void*> input_changed;
196         sigc::signal<void,IOChange,void*> output_changed;
197
198         virtual XMLNode& state (bool full);
199         XMLNode& get_state (void);
200         int set_state (const XMLNode&);
201
202         static int  disable_connecting (void);
203         static int  enable_connecting (void);
204         static int  disable_ports (void);
205         static int  enable_ports (void);
206         static int  disable_panners (void);
207         static int  reset_panners (void);
208         
209         static sigc::signal<int>            PortsLegal;
210         static sigc::signal<int>            PannersLegal;
211         static sigc::signal<int>            ConnectingLegal;
212         /// raised when the number of input or output ports changes
213         static sigc::signal<void,ChanCount> PortCountChanged;
214         static sigc::signal<void,nframes_t> CycleStart;
215
216         static void update_meters();
217         static std::string name_from_state (const XMLNode&);
218         static void set_name_in_state (XMLNode&, const std::string&);
219
220   private: 
221         
222         static sigc::signal<void>   Meter;
223         static Glib::StaticMutex    m_meter_signal_lock;
224         sigc::connection            m_meter_connection;
225
226   public:
227     
228         /* automation */
229
230         struct GainControl : public AutomationControl {
231             GainControl (std::string name, IO* i, const Evoral::Parameter &param,
232                     boost::shared_ptr<AutomationList> al = boost::shared_ptr<AutomationList>() )
233                         : AutomationControl (i->_session, param, al, name )
234                         , _io (i)
235                 {}
236          
237             void set_value (float val);
238             float get_value (void) const;
239    
240             IO* _io;
241         };
242
243         boost::shared_ptr<GainControl> gain_control() {
244                 return _gain_control;
245         }
246         boost::shared_ptr<const GainControl> gain_control() const {
247                 return _gain_control;
248         }
249
250         void clear_automation ();
251         
252         void set_parameter_automation_state (Evoral::Parameter, AutoState);
253
254         virtual void transport_stopped (nframes_t now);
255         virtual void automation_snapshot (nframes_t now, bool force);
256
257         void start_pan_touch (uint32_t which);
258         void end_pan_touch (uint32_t which);
259
260         void defer_pan_reset ();
261         void allow_pan_reset ();
262
263         /* the session calls this for master outs before
264            anyone else. controls outs too, at some point.
265         */
266
267         XMLNode *pending_state_node;
268         int ports_became_legal ();
269
270   private:
271         mutable Glib::Mutex io_lock;
272
273   protected:
274         BufferSet*          _output_buffers; //< Set directly to output port buffers
275         bool                _active;
276         gain_t              _gain;
277         Glib::Mutex          declick_lock;
278         PortSet             _outputs;
279         PortSet             _inputs;
280         bool                 no_panner_reset;
281         bool                _phase_invert;
282         bool                _denormal_protection;
283         XMLNode*             deferred_state;
284         DataType            _default_type;
285         nframes_t           _output_offset;
286         ChanCount           _configured_inputs;
287         ChanCount           _configured_outputs;
288         
289         boost::shared_ptr<Amp>       _amp;
290         boost::shared_ptr<PeakMeter> _meter;
291         boost::shared_ptr<Panner>    _panner;
292
293         virtual void prepare_inputs (nframes_t nframes);
294         virtual void flush_outputs (nframes_t nframes);
295
296         virtual void set_deferred_state() {}
297
298         virtual uint32_t pans_required() const
299                 { return _inputs.count().n_audio(); }
300
301         boost::shared_ptr<GainControl> _gain_control;
302
303         virtual void   set_gain (gain_t g, void *src);
304         void           inc_gain (gain_t delta, void *src);
305
306         virtual int load_automation (std::string path);
307
308         /* AudioTrack::deprecated_use_diskstream_connections() needs these */
309
310         int set_inputs (const std::string& str);
311         int set_outputs (const std::string& str);
312
313         void increment_output_offset (nframes_t);
314         void cycle_start (nframes_t);
315
316         static bool connecting_legal;
317         static bool ports_legal;
318
319   private:
320         static bool panners_legal;
321
322         void copy_to_outputs (BufferSet& bufs, DataType type, nframes_t nframes);
323
324         int connecting_became_legal ();
325         int panners_became_legal ();
326         sigc::connection connection_legal_c;
327         sigc::connection port_legal_c;
328         sigc::connection panner_legal_c;
329
330         ChanCount _input_minimum; ///< minimum number of input channels (0 for no minimum)
331         ChanCount _input_maximum; ///< maximum number of input channels (ChanCount::INFINITE for no maximum)
332         ChanCount _output_minimum; ///< minimum number of output channels (0 for no minimum)
333         ChanCount _output_maximum; ///< maximum number of output channels (ChanCount::INFINITE for no maximum)
334
335         boost::shared_ptr<Bundle> _bundle_for_inputs; ///< a bundle representing our inputs
336         boost::shared_ptr<Bundle> _bundle_for_outputs; ///< a bundle representing our outputs
337
338         struct UserBundleInfo {
339                 UserBundleInfo (IO*, boost::shared_ptr<UserBundle> b);
340                 
341                 boost::shared_ptr<UserBundle> bundle;
342                 sigc::connection changed;
343         };
344         
345         std::vector<UserBundleInfo> _bundles_connected_to_outputs; ///< user bundles connected to our outputs
346         std::vector<UserBundleInfo> _bundles_connected_to_inputs; ///< user bundles connected to our inputs
347
348         static int parse_io_string (const std::string&, std::vector<std::string>& chns);
349
350         static int parse_gain_string (const std::string&, std::vector<std::string>& chns);
351         
352         int set_sources (std::vector<std::string>&, void *src, bool add);
353         int set_destinations (std::vector<std::string>&, void *src, bool add);
354
355         int ensure_inputs (ChanCount, bool clear, bool lockit, void *src);
356         int ensure_outputs (ChanCount, bool clear, bool lockit, void *src);
357
358         void check_bundles_connected_to_inputs ();
359         void check_bundles_connected_to_outputs ();
360         void check_bundles (std::vector<UserBundleInfo>&, const PortSet&);
361
362         void bundle_changed (Bundle::Change);
363
364         int get_port_counts (const XMLNode& node);
365         int create_ports (const XMLNode&);
366         int make_connections (const XMLNode&);
367         boost::shared_ptr<Bundle> find_possible_bundle (const std::string &desired_name, const std::string &default_name, const std::string &connection_type_name);
368
369         virtual void setup_peak_meters ();
370         void meter ();
371
372         bool ensure_inputs_locked (ChanCount, bool clear, void *src);
373         bool ensure_outputs_locked (ChanCount, bool clear, void *src);
374
375         std::string build_legal_port_name (DataType type, bool for_input);
376         int32_t find_input_port_hole (const char* base);
377         int32_t find_output_port_hole (const char* base);
378
379         void setup_bundles_for_inputs_and_outputs ();
380         void setup_bundle_for_inputs ();
381         void setup_bundle_for_outputs ();
382         std::string bundle_channel_name (uint32_t, uint32_t) const;
383 };
384
385 } // namespace ARDOUR
386
387 #endif /*__ardour_io_h__ */