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.
20 #include "ardour/port.h"
21 #include "ardour/audioengine.h"
22 #include "ardour/i18n.h"
23 #include "pbd/failed_constructor.h"
24 #include "pbd/error.h"
25 #include "pbd/compose.h"
28 ARDOUR::AudioEngine* ARDOUR::Port::_engine = 0;
30 /** @param n Port short name */
31 ARDOUR::Port::Port (std::string const & n, DataType t, Flags f, bool e) : _jack_port (0), _last_monitor (false), _latency (0), _name (n), _flags (f)
33 /* Unfortunately we have to pass the DataType into this constructor so that we can
34 create the right kind of JACK port; aside from this we'll use the virtual function type ()
37 assert (_name.find_first_of (':') == std::string::npos);
44 throw failed_constructor ();
49 /** Port destructor */
50 ARDOUR::Port::~Port ()
53 jack_port_unregister (_engine->jack (), _jack_port);
57 /** Make this port externally visible by setting it up to use a JACK port.
58 * @param t Data type, so that we can call this method from the constructor.
61 ARDOUR::Port::do_make_external (DataType t)
64 /* already external */
68 _jack_port = jack_port_register (_engine->jack (), _name.c_str (), t.to_jack_type (), _flags, 0);
69 if (_jack_port == 0) {
70 throw std::runtime_error ("Could not register JACK port");
75 ARDOUR::Port::make_external ()
77 do_make_external (type ());
80 /** @return true if this port is connected to anything */
82 ARDOUR::Port::connected () const
84 if (!_connections.empty ()) {
85 /* connected to a Port* */
89 if (_jack_port == 0) {
90 /* not using a JACK port, so can't be connected to anything else */
94 return (jack_port_connected (_jack_port) != 0);
98 ARDOUR::Port::disconnect_all ()
100 /* Disconnect from Port* connections */
101 for (std::set<Port*>::iterator i = _connections.begin (); i != _connections.end (); ++i) {
102 (*i)->_connections.erase (this);
105 _connections.clear ();
107 /* And JACK connections */
108 jack_port_disconnect (_engine->jack(), _jack_port);
109 _named_connections.clear ();
114 /** @param o Port name
115 * @return true if this port is connected to o, otherwise false.
118 ARDOUR::Port::connected_to (std::string const & o) const
120 std::string const full = _engine->make_port_name_non_relative (o);
121 std::string const shrt = _engine->make_port_name_non_relative (o);
123 if (_jack_port && jack_port_connected_to (_jack_port, full.c_str ())) {
124 /* connected via JACK */
128 for (std::set<Port*>::iterator i = _connections.begin (); i != _connections.end (); ++i) {
129 if ((*i)->name () == shrt) {
130 /* connected internally */
138 /** @param o Filled in with port full names of ports that we are connected to */
140 ARDOUR::Port::get_connections (std::vector<std::string> & c) const
144 /* JACK connections */
146 const char** jc = jack_port_get_connections (_jack_port);
148 for (int i = 0; jc[i]; ++i) {
155 /* Internal connections */
156 for (std::set<Port*>::iterator i = _connections.begin (); i != _connections.end (); ++i) {
157 std::string const full = _engine->make_port_name_non_relative ((*i)->name());
166 ARDOUR::Port::connect (std::string const & other)
168 /* caller must hold process lock */
170 std::string const other_shrt = _engine->make_port_name_non_relative (other);
172 Port* p = _engine->get_port_by_name_locked (other_shrt);
176 if (p && !p->external ()) {
177 /* non-external Ardour port; connect using Port* */
180 /* connect using name */
182 /* for this to work, we must be an external port */
187 std::string const this_shrt = _engine->make_port_name_non_relative (_name);
189 if (sends_output ()) {
190 r = jack_connect (_engine->jack (), this_shrt.c_str (), other_shrt.c_str ());
192 r = jack_connect (_engine->jack (), other_shrt.c_str (), this_shrt.c_str());
196 _named_connections.insert (other);
204 ARDOUR::Port::disconnect (std::string const & other)
206 /* caller must hold process lock */
208 std::string const other_shrt = _engine->make_port_name_non_relative (other);
210 Port* p = _engine->get_port_by_name_locked (other_shrt);
213 if (p && !p->external ()) {
214 /* non-external Ardour port; disconnect using Port* */
217 /* disconnect using name */
219 std::string const this_shrt = _engine->make_port_name_non_relative (_name);
221 if (sends_output ()) {
222 r = jack_disconnect (_engine->jack (), this_shrt.c_str (), other_shrt.c_str ());
224 r = jack_disconnect (_engine->jack (), other_shrt.c_str (), this_shrt.c_str ());
228 _named_connections.erase (other);
237 ARDOUR::Port::connected_to (Port* o) const
239 return connected_to (o->name ());
243 ARDOUR::Port::connect (Port* o)
245 /* caller must hold process lock */
247 if (external () && o->external ()) {
248 /* we're both external; connect using name */
249 return connect (o->name ());
252 /* otherwise connect by Port* */
253 _connections.insert (o);
254 o->_connections.insert (this);
260 ARDOUR::Port::disconnect (Port* o)
262 if (external () && o->external ()) {
263 /* we're both external; try disconnecting using name */
264 int const r = disconnect (o->name ());
270 _connections.erase (o);
271 o->_connections.erase (this);
277 ARDOUR::Port::set_engine (AudioEngine* e)
283 ARDOUR::Port::ensure_monitor_input (bool yn)
286 jack_port_ensure_monitor (_jack_port, yn);
291 ARDOUR::Port::monitoring_input () const
294 return jack_port_monitoring_input (_jack_port);
301 ARDOUR::Port::reset ()
303 _last_monitor = false;
311 ARDOUR::Port::recompute_total_latency () const
313 #ifdef HAVE_JACK_RECOMPUTE_LATENCY
315 jack_recompute_total_latency (_engine->jack (), _jack_port);
321 ARDOUR::Port::total_latency () const
324 return jack_port_get_total_latency (_engine->jack (), _jack_port);
331 ARDOUR::Port::reestablish ()
337 _jack_port = jack_port_register (_engine->jack(), _name.c_str(), type().to_jack_type(), _flags, 0);
339 if (_jack_port == 0) {
340 PBD::error << string_compose (_("could not reregister %1"), _name) << endmsg;
351 ARDOUR::Port::reconnect ()
353 /* caller must hold process lock; intended to be used only after reestablish() */
359 for (std::set<string>::iterator i = _named_connections.begin(); i != _named_connections.end(); ++i) {
368 /** @param n Short name */
370 ARDOUR::Port::set_name (std::string const & n)
372 assert (_name.find_first_of (':') == std::string::npos);
377 r = jack_port_set_name (_jack_port, n.c_str());
389 ARDOUR::Port::set_latency (nframes_t n)
395 ARDOUR::Port::request_monitor_input (bool yn)
398 jack_port_request_monitor (_jack_port, yn);