Waf building of libardour (yay!).
[ardour.git] / libs / ardour / port.cc
1 /*
2     Copyright (C) 2009 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 #include "ardour/port.h"
21 #include "ardour/audioengine.h"
22 #include "pbd/failed_constructor.h"
23 #include "pbd/error.h"
24 #include "pbd/compose.h"
25 #include <stdexcept>
26
27 #include "i18n.h"
28
29 using namespace std;
30 using namespace ARDOUR;
31
32 AudioEngine* Port::_engine = 0;
33
34 /** @param n Port short name */
35 Port::Port (std::string const & n, DataType t, Flags f)
36         : _last_monitor (false)
37         , _name (n)
38         , _flags (f)
39 {
40
41         /* Unfortunately we have to pass the DataType into this constructor so that we can
42            create the right kind of JACK port; aside from this we'll use the virtual function type ()
43            to establish type. 
44         */
45
46         assert (_name.find_first_of (':') == std::string::npos);
47         
48         if ((_jack_port = jack_port_register (_engine->jack (), _name.c_str (), t.to_jack_type (), _flags, 0)) == 0) {
49                 throw failed_constructor ();
50         }
51 }
52
53 /** Port destructor */
54 Port::~Port ()
55 {
56         jack_port_unregister (_engine->jack (), _jack_port);
57 }
58
59 /** @return true if this port is connected to anything */
60 bool
61 Port::connected () const
62 {
63         return (jack_port_connected (_jack_port) != 0);
64 }
65
66 int
67 Port::disconnect_all ()
68 {
69         jack_port_disconnect (_engine->jack(), _jack_port);
70         _connections.clear ();
71
72         return 0;
73 }
74
75 /** @param o Port name
76  * @return true if this port is connected to o, otherwise false.
77  */
78 bool
79 Port::connected_to (std::string const & o) const
80 {
81         return jack_port_connected_to (_jack_port, _engine->make_port_name_non_relative(o).c_str ());
82 }
83
84 /** @param o Filled in with port full names of ports that we are connected to */
85 int
86 Port::get_connections (std::vector<std::string> & c) const
87 {
88         int n = 0;
89
90         const char** jc = jack_port_get_connections (_jack_port);
91         if (jc) {
92                 for (int i = 0; jc[i]; ++i) {
93                         c.push_back (jc[i]);
94                         ++n;
95                 }
96         }
97         
98         return n;
99 }
100
101 int
102 Port::connect (std::string const & other)
103 {
104         /* caller must hold process lock */
105
106         std::string const other_shrt = _engine->make_port_name_non_relative (other);
107         std::string const this_shrt = _engine->make_port_name_non_relative (_name);
108
109         int r = 0;
110                 
111         if (sends_output ()) {
112                 r = jack_connect (_engine->jack (), this_shrt.c_str (), other_shrt.c_str ());
113         } else {
114                 r = jack_connect (_engine->jack (), other_shrt.c_str (), this_shrt.c_str());
115         }
116         
117         if (r == 0) {
118                 _connections.insert (other);
119         }
120
121         return r;
122 }
123
124 int
125 Port::disconnect (std::string const & other)
126 {
127         /* caller must hold process lock */
128
129         std::string const other_shrt = _engine->make_port_name_non_relative (other);
130         std::string const this_shrt = _engine->make_port_name_non_relative (_name);
131
132         int r = 0;
133         
134         if (sends_output ()) {
135                 r = jack_disconnect (_engine->jack (), this_shrt.c_str (), other_shrt.c_str ());
136         } else {
137                 r = jack_disconnect (_engine->jack (), other_shrt.c_str (), this_shrt.c_str ());
138         }
139         
140         if (r == 0) {
141                 _connections.erase (other);
142         }
143         
144 return r;
145 }
146
147
148 bool
149 Port::connected_to (Port* o) const
150 {
151         return connected_to (o->name ());
152 }
153
154 int
155 Port::connect (Port* o)
156 {
157         return connect (o->name ());
158 }
159
160 int
161 Port::disconnect (Port* o)
162 {
163         return disconnect (o->name ());
164 }
165
166 void
167 Port::set_engine (AudioEngine* e)
168 {
169         _engine = e;
170 }
171
172 void
173 Port::ensure_monitor_input (bool yn)
174 {
175         jack_port_ensure_monitor (_jack_port, yn);
176 }
177
178 bool
179 Port::monitoring_input () const
180 {
181         return jack_port_monitoring_input (_jack_port);
182 }
183
184 void
185 Port::reset ()
186 {
187         _last_monitor = false;
188
189         // XXX
190         // _metering = 0;
191         // reset_meters ();
192 }
193
194 void
195 Port::recompute_total_latency () const
196 {
197 #ifdef HAVE_JACK_RECOMPUTE_LATENCY      
198         jack_recompute_total_latency (_engine->jack (), _jack_port);
199 #endif  
200 }
201
202 nframes_t
203 Port::total_latency () const
204 {
205         return jack_port_get_total_latency (_engine->jack (), _jack_port);
206 }
207
208 int
209 Port::reestablish ()
210 {
211         _jack_port = jack_port_register (_engine->jack(), _name.c_str(), type().to_jack_type(), _flags, 0);
212
213         if (_jack_port == 0) {
214                 PBD::error << string_compose (_("could not reregister %1"), _name) << endmsg;
215                 return -1;
216         }
217
218         reset ();
219
220         return 0;
221 }
222
223
224 int
225 Port::reconnect ()
226 {
227         /* caller must hold process lock; intended to be used only after reestablish() */
228
229         for (std::set<string>::iterator i = _connections.begin(); i != _connections.end(); ++i) {
230                 if (connect (*i)) {
231                         return -1;
232                 }
233         }
234
235         return 0;
236 }
237
238 /** @param n Short name */
239 int
240 Port::set_name (std::string const & n)
241 {
242         assert (_name.find_first_of (':') == std::string::npos);
243
244         int const r = jack_port_set_name (_jack_port, n.c_str());
245         if (r == 0) {
246                 _name = n;
247         }
248
249         return r;
250 }
251
252 void
253 Port::request_monitor_input (bool yn)
254 {
255         jack_port_request_monitor (_jack_port, yn);
256 }
257
258 void
259 Port::set_latency (nframes_t n)
260 {
261         jack_port_set_latency (_jack_port, n);
262 }