2 Copyright (C) 2009 Paul Davis
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.
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.
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.
21 #include "libardour-config.h"
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"
34 using namespace ARDOUR;
36 AudioEngine* Port::_engine = 0;
37 nframes_t Port::_buffer_size = 0;
38 bool Port::_connecting_blocked = false;
40 /** @param n Port short name */
41 Port::Port (std::string const & n, DataType t, Flags f)
42 : _last_monitor (false)
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 ()
52 assert (_name.find_first_of (':') == std::string::npos);
54 if (!_engine->connected()) {
55 throw failed_constructor ();
58 if ((_jack_port = jack_port_register (_engine->jack (), _name.c_str (), t.to_jack_type (), _flags, 0)) == 0) {
59 throw failed_constructor ();
63 /** Port destructor */
66 jack_port_unregister (_engine->jack (), _jack_port);
69 /** @return true if this port is connected to anything */
71 Port::connected () const
73 return (jack_port_connected (_jack_port) != 0);
77 Port::disconnect_all ()
79 jack_port_disconnect (_engine->jack(), _jack_port);
80 _connections.clear ();
85 /** @param o Port name
86 * @return true if this port is connected to o, otherwise false.
89 Port::connected_to (std::string const & o) const
91 return jack_port_connected_to (_jack_port, _engine->make_port_name_non_relative(o).c_str ());
94 /** @param o Filled in with port full names of ports that we are connected to */
96 Port::get_connections (std::vector<std::string> & c) const
100 const char** jc = jack_port_get_connections (_jack_port);
102 for (int i = 0; jc[i]; ++i) {
114 Port::connect (std::string const & other)
116 /* caller must hold process lock */
118 std::string const other_shrt = _engine->make_port_name_non_relative (other);
119 std::string const this_shrt = _engine->make_port_name_non_relative (_name);
123 if (_connecting_blocked) {
127 if (sends_output ()) {
128 r = jack_connect (_engine->jack (), this_shrt.c_str (), other_shrt.c_str ());
130 r = jack_connect (_engine->jack (), other_shrt.c_str (), this_shrt.c_str());
134 _connections.insert (other);
141 Port::disconnect (std::string const & other)
143 /* caller must hold process lock */
145 std::string const other_shrt = _engine->make_port_name_non_relative (other);
146 std::string const this_shrt = _engine->make_port_name_non_relative (_name);
150 if (sends_output ()) {
151 r = jack_disconnect (_engine->jack (), this_shrt.c_str (), other_shrt.c_str ());
153 r = jack_disconnect (_engine->jack (), other_shrt.c_str (), this_shrt.c_str ());
157 _connections.erase (other);
165 Port::connected_to (Port* o) const
167 return connected_to (o->name ());
171 Port::connect (Port* o)
173 return connect (o->name ());
177 Port::disconnect (Port* o)
179 return disconnect (o->name ());
183 Port::set_engine (AudioEngine* e)
189 Port::ensure_monitor_input (bool yn)
191 jack_port_ensure_monitor (_jack_port, yn);
195 Port::monitoring_input () const
197 return jack_port_monitoring_input (_jack_port);
203 _last_monitor = false;
211 Port::recompute_total_latency () const
213 #ifdef HAVE_JACK_RECOMPUTE_LATENCY
214 jack_client_t* jack = _engine->jack();
220 jack_recompute_total_latency (jack, _jack_port);
225 Port::total_latency () const
227 jack_client_t* jack = _engine->jack();
233 return jack_port_get_total_latency (jack, _jack_port);
239 jack_client_t* jack = _engine->jack();
245 cerr << "RE-REGISTER: " << _name.c_str() << endl;
246 _jack_port = jack_port_register (jack, _name.c_str(), type().to_jack_type(), _flags, 0);
248 if (_jack_port == 0) {
249 PBD::error << string_compose (_("could not reregister %1"), _name) << endmsg;
262 /* caller must hold process lock; intended to be used only after reestablish() */
264 for (std::set<string>::iterator i = _connections.begin(); i != _connections.end(); ++i) {
273 /** @param n Short port name (no JACK client name) */
275 Port::set_name (std::string const & n)
281 int const r = jack_port_set_name (_jack_port, n.c_str());
291 Port::request_monitor_input (bool yn)
293 jack_port_request_monitor (_jack_port, yn);
297 Port::set_latency (nframes_t n)
299 jack_port_set_latency (_jack_port, n);
303 Port::physically_connected () const
305 const char** jc = jack_port_get_connections (_jack_port);
308 for (int i = 0; jc[i]; ++i) {
310 jack_port_t* port = jack_port_by_name (_engine->jack(), jc[i]);
312 if (port && (jack_port_flags (port) & JackPortIsPhysical)) {