MIDI region forking, plus Playlist::regions_to_read() fix forward ported from 2.X...
[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/automatable_controls.h"
37 #include "ardour/automation_control.h"
38 #include "ardour/bundle.h"
39 #include "ardour/chan_count.h"
40 #include "ardour/data_type.h"
41 #include "ardour/latent.h"
42 #include "ardour/port_set.h"
43 #include "ardour/session_object.h"
44 #include "ardour/types.h"
45 #include "ardour/utils.h"
46
47 class XMLNode;
48
49 namespace ARDOUR {
50
51 class Amp;
52 class AudioEngine;
53 class AudioPort;
54 class BufferSet;
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 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);
79         IO (Session&, const XMLNode&, DataType default_type = DataType::AUDIO);
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
93         virtual void silence  (nframes_t);
94
95         int ensure_io (ChanCount cnt, bool clear, void *src);
96
97         int connect_ports_to_bundle (boost::shared_ptr<Bundle>, void *);
98         int disconnect_ports_from_bundle (boost::shared_ptr<Bundle>, void *);
99
100         BundleList bundles_connected ();
101
102         boost::shared_ptr<Bundle> bundle () { return _bundle; }
103
104         int add_port (std::string connection, void *src, DataType type = DataType::NIL);
105         int remove_port (Port *, void *src);
106         int connect (Port *our_port, std::string other_port, void *src);
107         int disconnect (Port *our_port, std::string other_port, void *src);
108         int disconnect (void *src);
109         bool connected_to (boost::shared_ptr<const IO>) const;
110         bool connected () const;
111         bool physically_connected () const;
112         
113         nframes_t signal_latency() const { return _own_latency; }
114         nframes_t latency() const;
115         void      set_port_latency (nframes_t);
116
117         void update_port_total_latencies ();
118
119         PortSet& ports() { return _ports; }
120         const PortSet& ports() const { return _ports; }
121
122         Port *nth (uint32_t n) const {
123                 if (n < _ports.num_ports()) {
124                         return _ports.port(n);
125                 } else {
126                         return 0;
127                 }
128         }
129
130         Port* port_by_name (const std::string& str) const;
131
132         AudioPort* audio(uint32_t n) const;
133         MidiPort*  midi(uint32_t n) const;
134
135         const ChanCount& n_ports ()  const { return _ports.count(); }
136
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         static int  disable_connecting (void);
145         static int  enable_connecting (void);
146         static int  disable_ports (void);
147         static int  enable_ports (void);
148
149         static PBD::Signal1<void,ChanCount> PortCountChanged; // emitted when the number of ports changes
150
151         static std::string name_from_state (const XMLNode&);
152         static void set_name_in_state (XMLNode&, const std::string&);
153
154         /* we have to defer/order port connection. this is how we do it.
155         */
156
157         static PBD::Signal0<int> ConnectingLegal;
158         static bool              connecting_legal;
159
160         XMLNode *pending_state_node;
161         int pending_state_node_version;
162         bool pending_state_node_in;
163
164         /* three utility functions - this just seems to be simplest place to put them */
165
166         void collect_input (BufferSet& bufs, nframes_t nframes, ChanCount offset);
167         void process_input (boost::shared_ptr<Processor>, sframes_t start_frame, sframes_t end_frame, nframes_t nframes);
168         void copy_to_outputs (BufferSet& bufs, DataType type, nframes_t nframes, nframes_t offset);
169
170         /* AudioTrack::deprecated_use_diskstream_connections() needs these */
171
172         int set_ports (const std::string& str);
173
174   private:
175         mutable Glib::Mutex io_lock;
176
177   protected:
178         PortSet   _ports;
179         Direction _direction;
180         DataType _default_type;
181         bool     _active;
182
183   private:
184         int connecting_became_legal ();
185         PBD::ScopedConnection 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             boost::shared_ptr<UserBundle> bundle;
192             PBD::ScopedConnection changed;
193         };
194         
195         std::vector<UserBundleInfo*> _bundles_connected; ///< user bundles connected to our ports
196
197         static int parse_io_string (const std::string&, std::vector<std::string>& chns);
198         static int parse_gain_string (const std::string&, std::vector<std::string>& chns);
199
200         int ensure_ports (ChanCount, bool clear, bool lockit, void *src);
201
202         void check_bundles_connected ();
203         void check_bundles (std::vector<UserBundleInfo*>&, const PortSet&);
204
205         void bundle_changed (Bundle::Change);
206
207         int get_port_counts (const XMLNode& node, int version, ChanCount& n, boost::shared_ptr<Bundle>& c);
208         int get_port_counts_2X (const XMLNode& node, int version, ChanCount& n, boost::shared_ptr<Bundle>& c);
209         int create_ports (const XMLNode&, int version);
210         int make_connections (const XMLNode&, int, bool);
211         int make_connections_2X (const XMLNode &, int, bool);
212
213         boost::shared_ptr<Bundle> find_possible_bundle (const std::string &desired_name);
214
215         bool ensure_ports_locked (ChanCount, bool clear, void *src);
216
217         std::string build_legal_port_name (DataType type);
218         int32_t find_port_hole (const char* base);
219
220         void setup_bundle ();
221         std::string bundle_channel_name (uint32_t, uint32_t) const;
222 };
223
224 } // namespace ARDOUR
225
226 #endif /*__ardour_io_h__ */