Prevent removal of route inputs when the plugins cannot be configured with the new...
[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 <jack/jack.h>
27
28 #include <glibmm/thread.h>
29
30 #include "pbd/fastlog.h"
31 #include "pbd/undo.h"
32 #include "pbd/statefuldestructible.h"
33 #include "pbd/controllable.h"
34
35 #include "ardour/ardour.h"
36 #include "ardour/automation_control.h"
37 #include "ardour/bundle.h"
38 #include "ardour/chan_count.h"
39 #include "ardour/data_type.h"
40 #include "ardour/latent.h"
41 #include "ardour/port_set.h"
42 #include "ardour/session_object.h"
43 #include "ardour/types.h"
44 #include "ardour/utils.h"
45
46 class XMLNode;
47
48 namespace ARDOUR {
49
50 class Amp;
51 class AudioEngine;
52 class AudioPort;
53 class BufferSet;
54 class Bundle;
55 class MidiPort;
56 class PeakMeter;
57 class Port;
58 class Processor;
59 class Session;
60 class UserBundle;
61
62 /** A collection of ports (all input or all output) with connections.
63  *
64  * An IO can contain ports of varying types, making routes/inserts/etc with
65  * varied combinations of types (eg MIDI and audio) possible.
66  */
67 class IO : public SessionObject, public Latent
68 {
69   public:
70         static const std::string state_node_name;
71
72         enum Direction {
73                 Input,
74                 Output
75         };
76
77         IO (Session&, const std::string& name, Direction, DataType default_type = DataType::AUDIO);
78         IO (Session&, const XMLNode&, DataType default_type = DataType::AUDIO);
79
80         virtual ~IO();
81
82         Direction direction() const { return _direction; }
83
84         DataType default_type() const         { return _default_type; }
85         void     set_default_type(DataType t) { _default_type = t; }
86
87         bool active() const { return _active; }
88         void set_active(bool yn) { _active = yn; }
89
90         bool set_name (const std::string& str);
91
92         virtual void silence  (nframes_t);
93
94         int ensure_io (ChanCount cnt, bool clear, void *src);
95
96         int connect_ports_to_bundle (boost::shared_ptr<Bundle>, void *);
97         int disconnect_ports_from_bundle (boost::shared_ptr<Bundle>, void *);
98
99         BundleList bundles_connected ();
100
101         boost::shared_ptr<Bundle> bundle () { return _bundle; }
102
103         int add_port (std::string connection, void *src, DataType type = DataType::NIL);
104         int remove_port (Port *, void *src);
105         int connect (Port *our_port, std::string other_port, void *src);
106         int disconnect (Port *our_port, std::string other_port, void *src);
107         int disconnect (void *src);
108         bool connected_to (boost::shared_ptr<const IO>) const;
109         bool connected () const;
110         bool physically_connected () const;
111         
112         nframes_t signal_latency() const { return _own_latency; }
113         nframes_t latency() const;
114         void      set_port_latency (nframes_t);
115
116         void update_port_total_latencies ();
117
118         PortSet& ports() { return _ports; }
119         const PortSet& ports() const { return _ports; }
120
121         Port *nth (uint32_t n) const {
122                 if (n < _ports.num_ports()) {
123                         return _ports.port(n);
124                 } else {
125                         return 0;
126                 }
127         }
128
129         Port* port_by_name (const std::string& str) const;
130
131         AudioPort* audio(uint32_t n) const;
132         MidiPort*  midi(uint32_t n) const;
133
134         const ChanCount& n_ports ()  const { return _ports.count(); }
135
136         /** Emitted with the process lock held */
137         PBD::Signal2<void,IOChange,void*> changed;
138
139         virtual XMLNode& state (bool full);
140         XMLNode& get_state (void);
141         int set_state (const XMLNode&, int version);
142         int set_state_2X (const XMLNode&, int, bool);
143
144         class BoolCombiner {
145         public:
146
147                 typedef bool result_type;
148
149                 template <typename Iter>
150                 bool operator() (Iter first, Iter last) const {
151                         bool r = false;
152                         while (first != last) {
153                                 if (*first) {
154                                         r = true;
155                                 }
156                                 ++first;
157                         }
158
159                         return r;
160                 }
161         };
162
163         /** Emitted when the port count is about to change.  Objects
164          *  can attach to this, and return `true' if they want to prevent
165          *  the change from happening.
166          */
167         PBD::Signal1<bool, ChanCount, BoolCombiner> PortCountChanging;
168         
169         static int  disable_connecting (void);
170         static int  enable_connecting (void);
171         static int  disable_ports (void);
172         static int  enable_ports (void);
173
174         static PBD::Signal1<void,ChanCount> PortCountChanged; // emitted when the number of ports changes
175
176         static std::string name_from_state (const XMLNode&);
177         static void set_name_in_state (XMLNode&, const std::string&);
178
179         /* we have to defer/order port connection. this is how we do it.
180         */
181
182         static PBD::Signal0<int> ConnectingLegal;
183         static bool              connecting_legal;
184
185         XMLNode *pending_state_node;
186         int pending_state_node_version;
187         bool pending_state_node_in;
188
189         /* three utility functions - this just seems to be simplest place to put them */
190
191         void collect_input (BufferSet& bufs, nframes_t nframes, ChanCount offset);
192         void process_input (boost::shared_ptr<Processor>, framepos_t start_frame, framepos_t end_frame, nframes_t nframes);
193         void copy_to_outputs (BufferSet& bufs, DataType type, nframes_t nframes, nframes_t offset);
194
195         /* AudioTrack::deprecated_use_diskstream_connections() needs these */
196
197         int set_ports (const std::string& str);
198
199   private:
200         mutable Glib::Mutex io_lock;
201
202   protected:
203         PortSet   _ports;
204         Direction _direction;
205         DataType _default_type;
206         bool     _active;
207
208   private:
209         int connecting_became_legal ();
210         PBD::ScopedConnection connection_legal_c;
211
212         boost::shared_ptr<Bundle> _bundle; ///< a bundle representing our ports
213
214         struct UserBundleInfo {
215             UserBundleInfo (IO*, boost::shared_ptr<UserBundle> b);
216             boost::shared_ptr<UserBundle> bundle;
217             PBD::ScopedConnection changed;
218         };
219         
220         std::vector<UserBundleInfo*> _bundles_connected; ///< user bundles connected to our ports
221
222         static int parse_io_string (const std::string&, std::vector<std::string>& chns);
223         static int parse_gain_string (const std::string&, std::vector<std::string>& chns);
224
225         int ensure_ports (ChanCount, bool clear, void *src);
226
227         void check_bundles_connected ();
228         void check_bundles (std::vector<UserBundleInfo*>&, const PortSet&);
229
230         void bundle_changed (Bundle::Change);
231
232         int get_port_counts (const XMLNode& node, int version, ChanCount& n, boost::shared_ptr<Bundle>& c);
233         int get_port_counts_2X (const XMLNode& node, int version, ChanCount& n, boost::shared_ptr<Bundle>& c);
234         int create_ports (const XMLNode&, int version);
235         int make_connections (const XMLNode&, int, bool);
236         int make_connections_2X (const XMLNode &, int, bool);
237
238         boost::shared_ptr<Bundle> find_possible_bundle (const std::string &desired_name);
239
240         bool ensure_ports_locked (ChanCount, bool clear, void *src);
241
242         std::string build_legal_port_name (DataType type);
243         int32_t find_port_hole (const char* base);
244
245         void setup_bundle ();
246         std::string bundle_channel_name (uint32_t, uint32_t, DataType) const;
247 };
248
249 } // namespace ARDOUR
250
251 #endif /*__ardour_io_h__ */