Part 1 of loading 2.X sessions; some things work, some things don't, hacks a-plenty.
[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 PeakMeter;
59 class Port;
60 class Processor;
61 class Session;
62 class UserBundle;
63
64 /** A collection of ports (all input or all output) 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 Latent
70 {
71   public:
72         static const std::string state_node_name;
73
74         enum Direction {
75                 Input,
76                 Output
77         };
78
79         IO (Session&, const std::string& name, Direction, DataType default_type = DataType::AUDIO);
80         IO (Session&, const XMLNode&, DataType default_type = DataType::AUDIO);
81
82         virtual ~IO();
83
84         Direction direction() const { return _direction; }
85
86         DataType default_type() const         { return _default_type; }
87         void     set_default_type(DataType t) { _default_type = t; }
88
89         bool active() const { return _active; }
90         void set_active(bool yn) { _active = yn; }
91
92         bool set_name (const std::string& str);
93
94         virtual void silence  (nframes_t);
95
96         int ensure_io (ChanCount cnt, bool clear, void *src);
97
98         int connect_ports_to_bundle (boost::shared_ptr<Bundle>, void *);
99         int disconnect_ports_from_bundle (boost::shared_ptr<Bundle>, void *);
100
101         BundleList bundles_connected ();
102
103         boost::shared_ptr<Bundle> bundle () { return _bundle; }
104
105         int add_port (std::string connection, void *src, DataType type = DataType::NIL);
106         int remove_port (Port *, void *src);
107         int connect (Port *our_port, std::string other_port, void *src);
108         int disconnect (Port *our_port, std::string other_port, void *src);
109         int disconnect (void *src);
110         bool connected_to (boost::shared_ptr<const IO>) 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         sigc::signal<void,IOChange,void*> changed;
137
138         virtual XMLNode& state (bool full);
139         XMLNode& get_state (void);
140         int set_state (const XMLNode&, int version = 3000);
141         int set_state_2X (const XMLNode&, int, bool);
142
143         static int  disable_connecting (void);
144         static int  enable_connecting (void);
145         static int  disable_ports (void);
146         static int  enable_ports (void);
147
148         static sigc::signal<void,ChanCount> PortCountChanged; // emitted when the number of ports changes
149
150         static std::string name_from_state (const XMLNode&);
151         static void set_name_in_state (XMLNode&, const std::string&);
152
153         /* we have to defer/order port connection. this is how we do it.
154         */
155
156         static sigc::signal<int> ConnectingLegal;
157         static bool              connecting_legal;
158
159         XMLNode *pending_state_node;
160         int pending_state_node_version;
161         bool pending_state_node_in;
162
163         /* three utility functions - this just seems to be simplest place to put them */
164
165         void collect_input (BufferSet& bufs, nframes_t nframes, ChanCount offset);
166         void process_input (boost::shared_ptr<Processor>, sframes_t start_frame, sframes_t end_frame, nframes_t nframes);
167         void copy_to_outputs (BufferSet& bufs, DataType type, nframes_t nframes, nframes_t offset);
168
169         /* AudioTrack::deprecated_use_diskstream_connections() needs these */
170
171         int set_ports (const std::string& str);
172
173   private:
174         mutable Glib::Mutex io_lock;
175
176   protected:
177         PortSet   _ports;
178         Direction _direction;
179         DataType _default_type;
180         bool     _active;
181
182   private:
183
184         int connecting_became_legal ();
185         sigc::connection connection_legal_c;
186
187         boost::shared_ptr<Bundle> _bundle; ///< a bundle representing our ports
188
189         struct UserBundleInfo {
190                 UserBundleInfo (IO*, boost::shared_ptr<UserBundle> b);
191
192                 boost::shared_ptr<UserBundle> bundle;
193                 sigc::connection changed;
194         };
195
196         std::vector<UserBundleInfo> _bundles_connected; ///< user bundles connected to our ports
197
198         static int parse_io_string (const std::string&, std::vector<std::string>& chns);
199         static int parse_gain_string (const std::string&, std::vector<std::string>& chns);
200
201         int ensure_ports (ChanCount, bool clear, bool lockit, void *src);
202
203         void check_bundles_connected ();
204         void check_bundles (std::vector<UserBundleInfo>&, const PortSet&);
205
206         void bundle_changed (Bundle::Change);
207
208
209         int get_port_counts (const XMLNode& node, int version, ChanCount& n, boost::shared_ptr<Bundle>& c);
210         int get_port_counts_2X (const XMLNode& node, int version, ChanCount& n, boost::shared_ptr<Bundle>& c);
211         int create_ports (const XMLNode&, int version);
212         int make_connections (const XMLNode&, int, bool);
213         int make_connections_2X (const XMLNode &, int, bool);
214
215         boost::shared_ptr<Bundle> find_possible_bundle (const std::string &desired_name);
216
217         bool ensure_ports_locked (ChanCount, bool clear, void *src);
218
219         std::string build_legal_port_name (DataType type);
220         int32_t find_port_hole (const char* base);
221
222         void setup_bundles ();
223         std::string bundle_channel_name (uint32_t, uint32_t) const;
224 };
225
226 } // namespace ARDOUR
227
228 #endif /*__ardour_io_h__ */