5511f64dd683b3becf62f58ac146134ab12135f4
[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
27 #include <glibmm/threads.h>
28
29 #include "pbd/fastlog.h"
30 #include "pbd/undo.h"
31 #include "pbd/statefuldestructible.h"
32 #include "pbd/controllable.h"
33
34 #include "ardour/ardour.h"
35 #include "ardour/automation_control.h"
36 #include "ardour/bundle.h"
37 #include "ardour/chan_count.h"
38 #include "ardour/data_type.h"
39 #include "ardour/latent.h"
40 #include "ardour/port_set.h"
41 #include "ardour/session_object.h"
42 #include "ardour/libardour_visibility.h"
43 #include "ardour/types.h"
44 #include "ardour/utils.h"
45 #include "ardour/buffer_set.h"
46
47 class XMLNode;
48
49 namespace ARDOUR {
50
51 class Amp;
52 class AudioEngine;
53 class AudioPort;
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 LIBARDOUR_API 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, bool sendish = false);
78         IO (Session&, const XMLNode&, DataType default_type = DataType::AUDIO, bool sendish = false);
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         void set_pretty_name (const std::string& str);
92         std::string pretty_name () const { return _pretty_name_prefix; }
93
94         virtual void silence (framecnt_t);
95         void increment_port_buffer_offset (pframes_t offset);
96
97         int ensure_io (ChanCount cnt, bool clear, void *src);
98
99         int connect_ports_to_bundle (boost::shared_ptr<Bundle>, bool exclusive, void *);
100         int disconnect_ports_from_bundle (boost::shared_ptr<Bundle>, void *);
101
102         BundleList bundles_connected ();
103
104         boost::shared_ptr<Bundle> bundle () { return _bundle; }
105
106         int add_port (std::string connection, void *src, DataType type = DataType::NIL);
107         int remove_port (boost::shared_ptr<Port>, void *src);
108         int connect (boost::shared_ptr<Port> our_port, std::string other_port, void *src);
109         int disconnect (boost::shared_ptr<Port> our_port, std::string other_port, void *src);
110         int disconnect (void *src);
111         bool connected_to (boost::shared_ptr<const IO>) const;
112         bool connected_to (const std::string&) const;
113         bool connected () const;
114         bool physically_connected () const;
115
116         framecnt_t signal_latency () const { return 0; }
117         framecnt_t latency () const;
118
119         PortSet& ports() { return _ports; }
120         const PortSet& ports() const { return _ports; }
121
122         bool has_port (boost::shared_ptr<Port>) const;
123
124         boost::shared_ptr<Port> nth (uint32_t n) const {
125                 if (n < _ports.num_ports()) {
126                         return _ports.port(n);
127                 } else {
128                         return boost::shared_ptr<Port> ();
129                 }
130         }
131
132         boost::shared_ptr<Port> port_by_name (const std::string& str) const;
133
134         boost::shared_ptr<AudioPort> audio(uint32_t n) const;
135         boost::shared_ptr<MidiPort>  midi(uint32_t n) const;
136
137         const ChanCount& n_ports ()  const { return _ports.count(); }
138
139         /* The process lock will be held on emission of this signal if
140          * IOChange contains ConfigurationChanged.  In other cases,
141          * the process lock status is undefined.
142          */
143         PBD::Signal2<void, IOChange, void *> changed;
144
145         virtual XMLNode& state (bool full);
146         XMLNode& get_state (void);
147         int set_state (const XMLNode&, int version);
148         int set_state_2X (const XMLNode&, int, bool);
149         static void prepare_for_reset (XMLNode&, const std::string&);
150
151         class BoolCombiner {
152         public:
153
154                 typedef bool result_type;
155
156                 template <typename Iter>
157                 result_type operator() (Iter first, Iter last) const {
158                         bool r = false;
159                         while (first != last) {
160                                 if (*first) {
161                                         r = true;
162                                 }
163                                 ++first;
164                         }
165
166                         return r;
167                 }
168         };
169
170         /** Emitted when the port count is about to change.  Objects
171          *  can attach to this, and return `true' if they want to prevent
172          *  the change from happening.
173          */
174         PBD::Signal1<bool, ChanCount, BoolCombiner> PortCountChanging;
175
176         static int disable_connecting ();
177         static int enable_connecting ();
178
179         static PBD::Signal1<void, ChanCount> PortCountChanged; // emitted when the number of ports changes
180
181         static std::string name_from_state (const XMLNode&);
182         static void set_name_in_state (XMLNode&, const std::string&);
183
184         /* we have to defer/order port connection. this is how we do it.
185         */
186
187         static PBD::Signal0<int> ConnectingLegal;
188         static bool              connecting_legal;
189
190         XMLNode *pending_state_node;
191         int pending_state_node_version;
192         bool pending_state_node_in;
193
194         /* three utility functions - this just seems to be simplest place to put them */
195
196         void collect_input (BufferSet& bufs, pframes_t nframes, ChanCount offset);
197         void process_input (boost::shared_ptr<Processor>, framepos_t start_frame, framepos_t end_frame, pframes_t nframes);
198         void copy_to_outputs (BufferSet& bufs, DataType type, pframes_t nframes, framecnt_t offset);
199
200         /* AudioTrack::deprecated_use_diskstream_connections() needs these */
201
202         int set_ports (const std::string& str);
203
204   private:
205         mutable Glib::Threads::Mutex io_lock;
206
207   protected:
208         PortSet   _ports;
209         Direction _direction;
210         DataType _default_type;
211         bool     _active;
212         bool     _sendish;
213
214   private:
215         int connecting_became_legal ();
216         PBD::ScopedConnection connection_legal_c;
217
218         boost::shared_ptr<Bundle> _bundle; ///< a bundle representing our ports
219
220         struct UserBundleInfo {
221                 UserBundleInfo (IO*, boost::shared_ptr<UserBundle> b);
222                 boost::shared_ptr<UserBundle> bundle;
223                 PBD::ScopedConnection changed;
224         };
225
226         std::vector<UserBundleInfo*> _bundles_connected; ///< user bundles connected to our ports
227
228         static int parse_io_string (const std::string&, std::vector<std::string>& chns);
229         static int parse_gain_string (const std::string&, std::vector<std::string>& chns);
230
231         int ensure_ports (ChanCount, bool clear, void *src);
232
233         void check_bundles_connected ();
234
235         void bundle_changed (Bundle::Change);
236
237         int get_port_counts (const XMLNode& node, int version, ChanCount& n, boost::shared_ptr<Bundle>& c);
238         int get_port_counts_2X (const XMLNode& node, int version, ChanCount& n, boost::shared_ptr<Bundle>& c);
239         int create_ports (const XMLNode&, int version);
240         int make_connections (const XMLNode&, int, bool);
241         int make_connections_2X (const XMLNode &, int, bool);
242
243         boost::shared_ptr<Bundle> find_possible_bundle (const std::string &desired_name);
244
245         int ensure_ports_locked (ChanCount, bool clear, bool& changed);
246
247         std::string build_legal_port_name (DataType type);
248         int32_t find_port_hole (const char* base);
249
250         void setup_bundle ();
251         std::string bundle_channel_name (uint32_t, uint32_t, DataType) const;
252
253         void apply_pretty_name ();
254         std::string _pretty_name_prefix;
255         BufferSet _buffers;
256         void disconnect_check (boost::shared_ptr<ARDOUR::Port>, boost::shared_ptr<ARDOUR::Port>);
257 };
258
259 } // namespace ARDOUR
260
261 #endif /*__ardour_io_h__ */