US2400: send_pan_azimuth lookup function (currently mb-only)
[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 #include "pbd/enum_convert.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/libardour_visibility.h"
44 #include "ardour/types.h"
45 #include "ardour/utils.h"
46 #include "ardour/buffer_set.h"
47
48 class XMLNode;
49
50 namespace ARDOUR {
51
52 class Amp;
53 class AudioEngine;
54 class AudioPort;
55 class Bundle;
56 class MidiPort;
57 class PeakMeter;
58 class Port;
59 class Processor;
60 class Session;
61 class UserBundle;
62
63 /** A collection of ports (all input or all output) with connections.
64  *
65  * An IO can contain ports of varying types, making routes/inserts/etc with
66  * varied combinations of types (eg MIDI and audio) possible.
67  */
68 class LIBARDOUR_API IO : public SessionObject, public Latent
69 {
70 public:
71         static const std::string state_node_name;
72
73         enum Direction {
74                 Input,
75                 Output
76         };
77
78         IO (Session&, const std::string& name, Direction, DataType default_type = DataType::AUDIO, bool sendish = false);
79         IO (Session&, const XMLNode&, DataType default_type = DataType::AUDIO, bool sendish = false);
80
81         virtual ~IO();
82
83         Direction direction() const { return _direction; }
84
85         DataType default_type() const         { return _default_type; }
86         void     set_default_type(DataType t) { _default_type = t; }
87
88         bool active() const { return _active; }
89         void set_active(bool yn) { _active = yn; }
90
91         bool set_name (const std::string& str);
92         void set_pretty_name (const std::string& str);
93         std::string pretty_name () const { return _pretty_name_prefix; }
94
95         virtual void silence (samplecnt_t);
96         void increment_port_buffer_offset (pframes_t offset);
97
98         int ensure_io (ChanCount cnt, bool clear, void *src);
99
100         int connect_ports_to_bundle (boost::shared_ptr<Bundle>, bool exclusive, void *);
101         int connect_ports_to_bundle (boost::shared_ptr<Bundle>, bool, bool, void *);
102         int disconnect_ports_from_bundle (boost::shared_ptr<Bundle>, void *);
103
104         BundleList bundles_connected ();
105
106         boost::shared_ptr<Bundle> bundle () { return _bundle; }
107
108         int add_port (std::string connection, void *src, DataType type = DataType::NIL);
109         int remove_port (boost::shared_ptr<Port>, void *src);
110         int connect (boost::shared_ptr<Port> our_port, std::string other_port, void *src);
111         int disconnect (boost::shared_ptr<Port> our_port, std::string other_port, void *src);
112         int disconnect (void *src);
113         bool connected_to (boost::shared_ptr<const IO>) const;
114         bool connected_to (const std::string&) const;
115         bool connected () const;
116         bool physically_connected () const;
117
118         samplecnt_t signal_latency () const { return 0; }
119
120         samplecnt_t latency () const;
121         samplecnt_t public_latency () const;
122         samplecnt_t connected_latency (bool for_playback) const;
123
124         PortSet& ports() { return _ports; }
125         const PortSet& ports() const { return _ports; }
126
127         bool has_port (boost::shared_ptr<Port>) const;
128
129         boost::shared_ptr<Port> nth (uint32_t n) const {
130                 if (n < _ports.num_ports()) {
131                         return _ports.port(n);
132                 } else {
133                         return boost::shared_ptr<Port> ();
134                 }
135         }
136
137         boost::shared_ptr<Port> port_by_name (const std::string& str) const;
138
139         boost::shared_ptr<AudioPort> audio(uint32_t n) const;
140         boost::shared_ptr<MidiPort>  midi(uint32_t n) const;
141
142         const ChanCount& n_ports ()  const { return _ports.count(); }
143
144         /* The process lock will be held on emission of this signal if
145          * IOChange contains ConfigurationChanged.  In other cases,
146          * the process lock status is undefined.
147          */
148         PBD::Signal2<void, IOChange, void *> changed;
149
150         XMLNode& get_state (void);
151
152         int set_state (const XMLNode&, int version);
153         int set_state_2X (const XMLNode&, int, bool);
154         static void prepare_for_reset (XMLNode&, const std::string&);
155
156         class BoolCombiner {
157         public:
158
159                 typedef bool result_type;
160
161                 template <typename Iter>
162                 result_type operator() (Iter first, Iter last) const {
163                         bool r = false;
164                         while (first != last) {
165                                 if (*first) {
166                                         r = true;
167                                 }
168                                 ++first;
169                         }
170
171                         return r;
172                 }
173         };
174
175         /** Emitted when the port count is about to change.  Objects
176          *  can attach to this, and return `true' if they want to prevent
177          *  the change from happening.
178          */
179         PBD::Signal1<bool, ChanCount, BoolCombiner> PortCountChanging;
180
181         static int disable_connecting ();
182         static int enable_connecting ();
183
184         static PBD::Signal1<void, ChanCount> PortCountChanged; // emitted when the number of ports changes
185
186         static std::string name_from_state (const XMLNode&);
187         static void set_name_in_state (XMLNode&, const std::string&);
188
189         /* we have to defer/order port connection. this is how we do it.
190         */
191
192         static PBD::Signal0<int> ConnectingLegal;
193         static bool              connecting_legal;
194
195         XMLNode *pending_state_node;
196         int pending_state_node_version;
197         bool pending_state_node_in;
198
199         /* three utility functions - this just seems to be simplest place to put them */
200
201         void collect_input (BufferSet& bufs, pframes_t nframes, ChanCount offset);
202         void copy_to_outputs (BufferSet& bufs, DataType type, pframes_t nframes, samplecnt_t offset);
203
204         /* AudioTrack::deprecated_use_diskstream_connections() needs these */
205
206         int set_ports (const std::string& str);
207
208 protected:
209         virtual XMLNode& state ();
210
211         PortSet   _ports;
212         Direction _direction;
213         DataType _default_type;
214         bool     _active;
215         bool     _sendish;
216
217 private:
218         mutable Glib::Threads::Mutex io_lock;
219         int connecting_became_legal ();
220         PBD::ScopedConnection connection_legal_c;
221
222         boost::shared_ptr<Bundle> _bundle; ///< a bundle representing our ports
223
224         struct UserBundleInfo {
225                 UserBundleInfo (IO*, boost::shared_ptr<UserBundle> b);
226                 boost::shared_ptr<UserBundle> bundle;
227                 PBD::ScopedConnection changed;
228         };
229
230         static int parse_io_string (const std::string&, std::vector<std::string>& chns);
231         static int parse_gain_string (const std::string&, std::vector<std::string>& chns);
232
233         int ensure_ports (ChanCount, bool clear, void *src);
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 namespace PBD {
262         DEFINE_ENUM_CONVERT (ARDOUR::IO::Direction)
263 }
264
265 #endif /*__ardour_io_h__ */