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