2 Copyright (C) 2000-2006 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.
28 #include <glibmm/thread.h>
30 #include "pbd/xml++.h"
31 #include "pbd/replace_all.h"
32 #include "pbd/unknown_type.h"
33 #include "pbd/enumwriter.h"
35 #include "ardour/audioengine.h"
36 #include "ardour/buffer.h"
37 #include "ardour/debug.h"
38 #include "ardour/io.h"
39 #include "ardour/route.h"
40 #include "ardour/port.h"
41 #include "ardour/audio_port.h"
42 #include "ardour/midi_port.h"
43 #include "ardour/session.h"
44 #include "ardour/cycle_timer.h"
45 #include "ardour/buffer_set.h"
46 #include "ardour/meter.h"
47 #include "ardour/amp.h"
48 #include "ardour/user_bundle.h"
52 #define BLOCK_PROCESS_CALLBACK() Glib::Mutex::Lock em (AudioEngine::instance()->process_lock())
55 using namespace ARDOUR;
58 const string IO::state_node_name = "IO";
59 bool IO::connecting_legal = false;
60 PBD::Signal0<int> IO::ConnectingLegal;
61 PBD::Signal1<void,ChanCount> IO::PortCountChanged;
63 /** @param default_type The type of port that will be created by ensure_io
64 * and friends if no type is explicitly requested (to avoid breakage).
66 IO::IO (Session& s, const string& name, Direction dir, DataType default_type)
67 : SessionObject (s, name)
69 , _default_type (default_type)
72 pending_state_node = 0;
76 IO::IO (Session& s, const XMLNode& node, DataType dt)
77 : SessionObject(s, "unnamed io")
82 pending_state_node = 0;
84 set_state (node, Stateful::loading_state_version);
90 Glib::Mutex::Lock lm (io_lock);
92 BLOCK_PROCESS_CALLBACK ();
94 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
95 _session.engine().unregister_port (*i);
100 IO::increment_port_buffer_offset (pframes_t offset)
102 /* io_lock, not taken: function must be called from Session::process() calltree */
104 if (_direction == Output) {
105 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
106 i->increment_port_buffer_offset (offset);
112 IO::silence (framecnt_t nframes)
114 /* io_lock, not taken: function must be called from Session::process() calltree */
116 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
117 i->get_buffer(nframes).silence (nframes);
121 /** Set _bundles_connected to those bundles that are connected such that every
122 * port on every bundle channel x is connected to port x in _ports.
125 IO::check_bundles_connected ()
127 std::vector<UserBundleInfo*> new_list;
129 for (std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) {
131 uint32_t const N = (*i)->bundle->nchannels().n_total();
133 if (_ports.num_ports() < N) {
139 for (uint32_t j = 0; j < N; ++j) {
140 /* Every port on bundle channel j must be connected to our input j */
141 Bundle::PortList const pl = (*i)->bundle->channel_ports (j);
142 for (uint32_t k = 0; k < pl.size(); ++k) {
143 if (_ports.port(j)->connected_to (pl[k]) == false) {
155 new_list.push_back (*i);
161 _bundles_connected = new_list;
166 IO::disconnect (boost::shared_ptr<Port> our_port, string other_port, void* src)
168 if (other_port.length() == 0 || our_port == 0) {
173 Glib::Mutex::Lock lm (io_lock);
175 /* check that our_port is really one of ours */
177 if ( ! _ports.contains(our_port)) {
181 /* disconnect it from the source */
183 if (our_port->disconnect (other_port)) {
184 error << string_compose(_("IO: cannot disconnect port %1 from %2"), our_port->name(), other_port) << endmsg;
188 check_bundles_connected ();
191 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
193 _session.set_dirty ();
199 IO::connect (boost::shared_ptr<Port> our_port, string other_port, void* src)
201 if (other_port.length() == 0 || our_port == 0) {
206 Glib::Mutex::Lock lm (io_lock);
208 /* check that our_port is really one of ours */
210 if ( ! _ports.contains(our_port) ) {
214 /* connect it to the source */
216 if (our_port->connect (other_port)) {
220 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
221 _session.set_dirty ();
226 IO::remove_port (boost::shared_ptr<Port> port, void* src)
228 ChanCount before = _ports.count ();
229 ChanCount after = before;
230 after.set (port->type(), after.get (port->type()) - 1);
232 bool const r = PortCountChanging (after); /* EMIT SIGNAL */
240 BLOCK_PROCESS_CALLBACK ();
243 Glib::Mutex::Lock lm (io_lock);
245 if (_ports.remove(port)) {
246 change.type = IOChange::Type (change.type | IOChange::ConfigurationChanged);
247 change.before = before;
248 change.after = _ports.count ();
250 if (port->connected()) {
251 change.type = IOChange::Type (change.type | IOChange::ConnectionsChanged);
254 _session.engine().unregister_port (port);
255 check_bundles_connected ();
259 PortCountChanged (n_ports()); /* EMIT SIGNAL */
261 if (change.type != IOChange::NoChange) {
262 changed (change, src);
263 _buffers.attach_buffers (_ports);
267 if (change.type & IOChange::ConfigurationChanged) {
271 if (change.type == IOChange::NoChange) {
275 _session.set_dirty ();
282 * @param destination Name of port to connect new port to.
283 * @param src Source for emitted ConfigurationChanged signal.
284 * @param type Data type of port. Default value (NIL) will use this IO's default type.
287 IO::add_port (string destination, void* src, DataType type)
289 boost::shared_ptr<Port> our_port;
291 if (type == DataType::NIL) {
292 type = _default_type;
295 ChanCount before = _ports.count ();
296 ChanCount after = before;
297 after.set (type, after.get (type) + 1);
299 bool const r = PortCountChanging (after); /* EMIT SIGNAL */
307 BLOCK_PROCESS_CALLBACK ();
311 Glib::Mutex::Lock lm (io_lock);
313 /* Create a new port */
315 string portname = build_legal_port_name (type);
317 if (_direction == Input) {
318 if ((our_port = _session.engine().register_input_port (type, portname)) == 0) {
319 error << string_compose(_("IO: cannot register input port %1"), portname) << endmsg;
323 if ((our_port = _session.engine().register_output_port (type, portname)) == 0) {
324 error << string_compose(_("IO: cannot register output port %1"), portname) << endmsg;
329 change.before = _ports.count ();
330 _ports.add (our_port);
333 PortCountChanged (n_ports()); /* EMIT SIGNAL */
334 change.type = IOChange::ConfigurationChanged;
335 change.after = _ports.count ();
336 changed (change, src); /* EMIT SIGNAL */
337 _buffers.attach_buffers (_ports);
340 if (!destination.empty()) {
341 if (our_port->connect (destination)) {
347 _session.set_dirty ();
353 IO::disconnect (void* src)
356 Glib::Mutex::Lock lm (io_lock);
358 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
359 i->disconnect_all ();
362 check_bundles_connected ();
365 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
370 /** Caller must hold process lock */
372 IO::ensure_ports_locked (ChanCount count, bool clear, bool& changed)
374 assert (!AudioEngine::instance()->process_lock().trylock());
376 boost::shared_ptr<Port> port;
380 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
382 const size_t n = count.get(*t);
384 /* remove unused ports */
385 for (size_t i = n_ports().get(*t); i > n; --i) {
386 port = _ports.port(*t, i-1);
390 _session.engine().unregister_port (port);
395 /* create any necessary new ports */
396 while (n_ports().get(*t) < n) {
398 string portname = build_legal_port_name (*t);
402 if (_direction == Input) {
403 if ((port = _session.engine().register_input_port (*t, portname)) == 0) {
404 error << string_compose(_("IO: cannot register input port %1"), portname) << endmsg;
408 if ((port = _session.engine().register_output_port (*t, portname)) == 0) {
409 error << string_compose(_("IO: cannot register output port %1"), portname) << endmsg;
415 catch (AudioEngine::PortRegistrationFailure& err) {
426 check_bundles_connected ();
427 PortCountChanged (n_ports()); /* EMIT SIGNAL */
428 _session.set_dirty ();
432 /* disconnect all existing ports so that we get a fresh start */
433 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
434 i->disconnect_all ();
441 /** Caller must hold process lock */
443 IO::ensure_ports (ChanCount count, bool clear, void* src)
445 assert (!AudioEngine::instance()->process_lock().trylock());
447 bool changed = false;
449 if (count == n_ports() && !clear) {
455 change.before = _ports.count ();
458 Glib::Mutex::Lock im (io_lock);
459 if (ensure_ports_locked (count, clear, changed)) {
465 change.after = _ports.count ();
466 change.type = IOChange::ConfigurationChanged;
467 this->changed (change, src); /* EMIT SIGNAL */
468 _buffers.attach_buffers (_ports);
470 _session.set_dirty ();
476 /** Caller must hold process lock */
478 IO::ensure_io (ChanCount count, bool clear, void* src)
480 assert (!AudioEngine::instance()->process_lock().trylock());
482 return ensure_ports (count, clear, src);
492 IO::state (bool /*full_state*/)
494 XMLNode* node = new XMLNode (state_node_name);
497 vector<string>::iterator ci;
499 LocaleGuard lg (X_("POSIX"));
500 Glib::Mutex::Lock lm (io_lock);
502 node->add_property("name", _name);
503 id().print (buf, sizeof (buf));
504 node->add_property("id", buf);
505 node->add_property ("direction", enum_2_string (_direction));
506 node->add_property ("default-type", _default_type.to_string());
508 for (std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) {
509 XMLNode* n = new XMLNode ("Bundle");
510 n->add_property ("name", (*i)->bundle->name ());
511 node->add_child_nocopy (*n);
514 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
516 vector<string> connections;
518 XMLNode* pnode = new XMLNode (X_("Port"));
519 pnode->add_property (X_("type"), i->type().to_string());
520 pnode->add_property (X_("name"), i->name());
522 if (i->get_connections (connections)) {
524 for (n = 0, ci = connections.begin(); ci != connections.end(); ++ci, ++n) {
526 /* if its a connection to our own port,
527 return only the port name, not the
528 whole thing. this allows connections
529 to be re-established even when our
530 client name is different.
533 XMLNode* cnode = new XMLNode (X_("Connection"));
535 cnode->add_property (X_("other"), _session.engine().make_port_name_relative (*ci));
536 pnode->add_child_nocopy (*cnode);
540 node->add_child_nocopy (*pnode);
543 snprintf (buf, sizeof (buf), "%" PRId64, _user_latency);
544 node->add_property (X_("user-latency"), buf);
550 IO::set_state (const XMLNode& node, int version)
552 /* callers for version < 3000 need to call set_state_2X directly, as A3 IOs
553 * are input OR output, not both, so the direction needs to be specified
556 assert (version >= 3000);
558 const XMLProperty* prop;
559 XMLNodeConstIterator iter;
560 LocaleGuard lg (X_("POSIX"));
562 /* force use of non-localized representation of decimal point,
563 since we use it a lot in XML files and so forth.
566 if (node.name() != state_node_name) {
567 error << string_compose(_("incorrect XML node \"%1\" passed to IO object"), node.name()) << endmsg;
571 if ((prop = node.property ("name")) != 0) {
572 set_name (prop->value());
575 if ((prop = node.property (X_("default-type"))) != 0) {
576 _default_type = DataType(prop->value());
577 assert(_default_type != DataType::NIL);
582 if ((prop = node.property ("direction")) != 0) {
583 _direction = (Direction) string_2_enum (prop->value(), _direction);
586 if (create_ports (node, version)) {
590 if (connecting_legal) {
592 if (make_connections (node, version, false)) {
598 pending_state_node = new XMLNode (node);
599 pending_state_node_version = version;
600 pending_state_node_in = false;
601 ConnectingLegal.connect_same_thread (connection_legal_c, boost::bind (&IO::connecting_became_legal, this));
604 if ((prop = node.property ("user-latency")) != 0) {
605 _user_latency = atoi (prop->value ());
612 IO::set_state_2X (const XMLNode& node, int version, bool in)
614 const XMLProperty* prop;
615 XMLNodeConstIterator iter;
616 LocaleGuard lg (X_("POSIX"));
618 /* force use of non-localized representation of decimal point,
619 since we use it a lot in XML files and so forth.
622 if (node.name() != state_node_name) {
623 error << string_compose(_("incorrect XML node \"%1\" passed to IO object"), node.name()) << endmsg;
627 if ((prop = node.property ("name")) != 0) {
628 set_name (prop->value());
631 if ((prop = node.property (X_("default-type"))) != 0) {
632 _default_type = DataType(prop->value());
633 assert(_default_type != DataType::NIL);
638 _direction = in ? Input : Output;
640 if (create_ports (node, version)) {
644 if (connecting_legal) {
646 if (make_connections_2X (node, version, in)) {
652 pending_state_node = new XMLNode (node);
653 pending_state_node_version = version;
654 pending_state_node_in = in;
655 ConnectingLegal.connect_same_thread (connection_legal_c, boost::bind (&IO::connecting_became_legal, this));
662 IO::connecting_became_legal ()
666 assert (pending_state_node);
668 connection_legal_c.disconnect ();
670 ret = make_connections (*pending_state_node, pending_state_node_version, pending_state_node_in);
672 delete pending_state_node;
673 pending_state_node = 0;
678 boost::shared_ptr<Bundle>
679 IO::find_possible_bundle (const string &desired_name)
681 static const string digits = "0123456789";
682 const string &default_name = (_direction == Input ? _("in") : _("out"));
683 const string &bundle_type_name = (_direction == Input ? _("input") : _("output"));
685 boost::shared_ptr<Bundle> c = _session.bundle_by_name (desired_name);
688 int bundle_number, mask;
689 string possible_name;
691 string::size_type last_non_digit_pos;
693 error << string_compose(_("Unknown bundle \"%1\" listed for %2 of %3"), desired_name, bundle_type_name, _name)
696 // find numeric suffix of desired name
699 last_non_digit_pos = desired_name.find_last_not_of(digits);
701 if (last_non_digit_pos != string::npos) {
703 s << desired_name.substr(last_non_digit_pos);
707 // see if it's a stereo connection e.g. "in 3+4"
709 if (last_non_digit_pos > 1 && desired_name[last_non_digit_pos] == '+') {
710 string::size_type left_last_non_digit_pos;
712 left_last_non_digit_pos = desired_name.find_last_not_of(digits, last_non_digit_pos-1);
714 if (left_last_non_digit_pos != string::npos) {
715 int left_bundle_number = 0;
717 s << desired_name.substr(left_last_non_digit_pos, last_non_digit_pos-1);
718 s >> left_bundle_number;
720 if (left_bundle_number > 0 && left_bundle_number + 1 == bundle_number) {
731 // find highest set bit
733 while ((mask <= bundle_number) && (mask <<= 1)) {}
735 // "wrap" bundle number into largest possible power of 2
740 if (bundle_number & mask) {
741 bundle_number &= ~mask;
744 s << default_name << " " << bundle_number + 1;
747 s << "+" << bundle_number + 2;
750 possible_name = s.str();
752 if ((c = _session.bundle_by_name (possible_name)) != 0) {
759 info << string_compose (_("Bundle %1 was not available - \"%2\" used instead"), desired_name, possible_name)
762 error << string_compose(_("No %1 bundles available as a replacement"), bundle_type_name)
773 IO::get_port_counts_2X (XMLNode const & node, int /*version*/, ChanCount& n, boost::shared_ptr<Bundle>& /*c*/)
775 XMLProperty const * prop;
776 XMLNodeList children = node.children ();
778 uint32_t n_audio = 0;
780 for (XMLNodeIterator i = children.begin(); i != children.end(); ++i) {
782 if ((prop = node.property ("inputs")) != 0 && _direction == Input) {
783 n_audio = count (prop->value().begin(), prop->value().end(), '{');
784 } else if ((prop = node.property ("input-connection")) != 0 && _direction == Input) {
786 } else if ((prop = node.property ("outputs")) != 0 && _direction == Output) {
787 n_audio = count (prop->value().begin(), prop->value().end(), '{');
788 } else if ((prop = node.property ("output-connection")) != 0 && _direction == Output) {
794 cnt.set_audio (n_audio);
795 n = ChanCount::max (n, cnt);
801 IO::get_port_counts (const XMLNode& node, int version, ChanCount& n, boost::shared_ptr<Bundle>& c)
803 if (version < 3000) {
804 return get_port_counts_2X (node, version, n, c);
807 XMLProperty const * prop;
808 XMLNodeConstIterator iter;
809 uint32_t n_audio = 0;
815 if ((prop = node.property ("connection")) != 0) {
817 if ((c = find_possible_bundle (prop->value())) != 0) {
818 n = ChanCount::max (n, c->nchannels());
823 for (iter = node.children().begin(); iter != node.children().end(); ++iter) {
825 if ((*iter)->name() == X_("Bundle")) {
826 if ((c = find_possible_bundle (prop->value())) != 0) {
827 n = ChanCount::max (n, c->nchannels());
834 if ((*iter)->name() == X_("Port")) {
835 prop = (*iter)->property (X_("type"));
841 if (prop->value() == X_("audio")) {
842 cnt.set_audio (++n_audio);
843 } else if (prop->value() == X_("midi")) {
844 cnt.set_midi (++n_midi);
849 n = ChanCount::max (n, cnt);
854 IO::create_ports (const XMLNode& node, int version)
857 boost::shared_ptr<Bundle> c;
859 get_port_counts (node, version, n, c);
862 Glib::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
864 if (ensure_ports (n, true, this)) {
865 error << string_compose(_("%1: cannot create I/O ports"), _name) << endmsg;
876 IO::make_connections (const XMLNode& node, int version, bool in)
878 if (version < 3000) {
879 return make_connections_2X (node, version, in);
882 const XMLProperty* prop;
884 for (XMLNodeConstIterator i = node.children().begin(); i != node.children().end(); ++i) {
886 if ((*i)->name() == "Bundle") {
887 XMLProperty const * prop = (*i)->property ("name");
889 boost::shared_ptr<Bundle> b = find_possible_bundle (prop->value());
891 connect_ports_to_bundle (b, this);
898 if ((*i)->name() == "Port") {
900 prop = (*i)->property (X_("name"));
906 boost::shared_ptr<Port> p = port_by_name (prop->value());
909 for (XMLNodeConstIterator c = (*i)->children().begin(); c != (*i)->children().end(); ++c) {
911 XMLNode* cnode = (*c);
913 if (cnode->name() != X_("Connection")) {
917 if ((prop = cnode->property (X_("other"))) == 0) {
922 connect (p, prop->value(), this);
934 IO::make_connections_2X (const XMLNode& node, int /*version*/, bool in)
936 const XMLProperty* prop;
938 /* XXX: bundles ("connections" as was) */
940 if ((prop = node.property ("inputs")) != 0 && in) {
942 string::size_type ostart = 0;
943 string::size_type start = 0;
944 string::size_type end = 0;
947 vector<string> ports;
949 string const str = prop->value ();
951 while ((start = str.find_first_of ('{', ostart)) != string::npos) {
954 if ((end = str.find_first_of ('}', start)) == string::npos) {
955 error << string_compose(_("IO: badly formed string in XML node for inputs \"%1\""), str) << endmsg;
959 if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
960 error << string_compose(_("bad input string in XML node \"%1\""), str) << endmsg;
967 for (int x = 0; x < n; ++x) {
968 /* XXX: this is a bit of a hack; need to check if it's always valid */
969 string::size_type const p = ports[x].find ("/out");
970 if (p != string::npos) {
971 ports[x].replace (p, 4, "/audio_out");
973 nth(i)->connect (ports[x]);
983 if ((prop = node.property ("outputs")) != 0 && !in) {
985 string::size_type ostart = 0;
986 string::size_type start = 0;
987 string::size_type end = 0;
990 vector<string> ports;
992 string const str = prop->value ();
994 while ((start = str.find_first_of ('{', ostart)) != string::npos) {
997 if ((end = str.find_first_of ('}', start)) == string::npos) {
998 error << string_compose(_("IO: badly formed string in XML node for outputs \"%1\""), str) << endmsg;
1002 if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
1003 error << string_compose(_("IO: bad output string in XML node \"%1\""), str) << endmsg;
1009 for (int x = 0; x < n; ++x) {
1010 /* XXX: this is a bit of a hack; need to check if it's always valid */
1011 string::size_type const p = ports[x].find ("/in");
1012 if (p != string::npos) {
1013 ports[x].replace (p, 3, "/audio_in");
1015 nth(i)->connect (ports[x]);
1028 IO::set_ports (const string& str)
1030 vector<string> ports;
1035 if ((nports = count (str.begin(), str.end(), '{')) == 0) {
1040 Glib::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
1042 // FIXME: audio-only
1043 if (ensure_ports (ChanCount(DataType::AUDIO, nports), true, this)) {
1048 string::size_type start, end, ostart;
1055 while ((start = str.find_first_of ('{', ostart)) != string::npos) {
1058 if ((end = str.find_first_of ('}', start)) == string::npos) {
1059 error << string_compose(_("IO: badly formed string in XML node for inputs \"%1\""), str) << endmsg;
1063 if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
1064 error << string_compose(_("bad input string in XML node \"%1\""), str) << endmsg;
1070 for (int x = 0; x < n; ++x) {
1071 connect (nth (i), ports[x], this);
1083 IO::parse_io_string (const string& str, vector<string>& ports)
1085 string::size_type pos, opos;
1087 if (str.length() == 0) {
1096 while ((pos = str.find_first_of (',', opos)) != string::npos) {
1097 ports.push_back (str.substr (opos, pos - opos));
1101 if (opos < str.length()) {
1102 ports.push_back (str.substr(opos));
1105 return ports.size();
1109 IO::parse_gain_string (const string& str, vector<string>& ports)
1111 string::size_type pos, opos;
1117 while ((pos = str.find_first_of (',', opos)) != string::npos) {
1118 ports.push_back (str.substr (opos, pos - opos));
1122 if (opos < str.length()) {
1123 ports.push_back (str.substr(opos));
1126 return ports.size();
1130 IO::set_name (const string& requested_name)
1132 string name = requested_name;
1134 if (_name == name) {
1138 /* replace all colons in the name. i wish we didn't have to do this */
1140 replace_all (name, ":", "-");
1142 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
1143 string current_name = i->name();
1144 current_name.replace (current_name.find (_name), _name.val().length(), name);
1145 i->set_name (current_name);
1148 bool const r = SessionObject::set_name (name);
1156 IO::latency () const
1158 framecnt_t max_latency;
1163 /* io lock not taken - must be protected by other means */
1165 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1166 if ((latency = i->private_latency_range (_direction == Output).max) > max_latency) {
1167 DEBUG_TRACE (DEBUG::Latency, string_compose ("port %1 has %2 latency of %3 - use\n",
1169 ((_direction == Output) ? "PLAYBACK" : "CAPTURE"),
1171 max_latency = latency;
1175 DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: max %4 latency from %2 ports = %3\n",
1176 name(), _ports.num_ports(), max_latency,
1177 ((_direction == Output) ? "PLAYBACK" : "CAPTURE")));
1182 IO::connect_ports_to_bundle (boost::shared_ptr<Bundle> c, void* src)
1184 BLOCK_PROCESS_CALLBACK ();
1187 Glib::Mutex::Lock lm2 (io_lock);
1189 c->connect (_bundle, _session.engine());
1191 /* If this is a UserBundle, make a note of what we've done */
1193 boost::shared_ptr<UserBundle> ub = boost::dynamic_pointer_cast<UserBundle> (c);
1196 /* See if we already know about this one */
1197 std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin();
1198 while (i != _bundles_connected.end() && (*i)->bundle != ub) {
1202 if (i == _bundles_connected.end()) {
1203 /* We don't, so make a note */
1204 _bundles_connected.push_back (new UserBundleInfo (this, ub));
1209 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
1214 IO::disconnect_ports_from_bundle (boost::shared_ptr<Bundle> c, void* src)
1216 BLOCK_PROCESS_CALLBACK ();
1219 Glib::Mutex::Lock lm2 (io_lock);
1221 c->disconnect (_bundle, _session.engine());
1223 /* If this is a UserBundle, make a note of what we've done */
1225 boost::shared_ptr<UserBundle> ub = boost::dynamic_pointer_cast<UserBundle> (c);
1228 std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin();
1229 while (i != _bundles_connected.end() && (*i)->bundle != ub) {
1233 if (i != _bundles_connected.end()) {
1235 _bundles_connected.erase (i);
1240 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
1246 IO::disable_connecting ()
1248 connecting_legal = false;
1253 IO::enable_connecting ()
1255 connecting_legal = true;
1256 boost::optional<int> r = ConnectingLegal ();
1257 return r.get_value_or (0);
1261 IO::bundle_changed (Bundle::Change /*c*/)
1264 // connect_input_ports_to_bundle (_input_bundle, this);
1269 IO::build_legal_port_name (DataType type)
1271 const int name_size = jack_port_name_size();
1275 if (type == DataType::AUDIO) {
1276 suffix = _("audio");
1277 } else if (type == DataType::MIDI) {
1280 throw unknown_type();
1283 /* note that if "in" or "out" are translated it will break a session
1284 across locale switches because a port's connection list will
1285 show (old) translated names, but the current port name will
1286 use the (new) translated name.
1289 if (_direction == Input) {
1290 suffix += X_("_in");
1292 suffix += X_("_out");
1295 // allow up to 4 digits for the output port number, plus the slash, suffix and extra space
1297 limit = name_size - _session.engine().client_name().length() - (suffix.length() + 5);
1299 char buf1[name_size+1];
1300 char buf2[name_size+1];
1302 /* colons are illegal in port names, so fix that */
1304 string nom = _name.val();
1305 replace_all (nom, ":", ";");
1307 snprintf (buf1, name_size+1, ("%.*s/%s"), limit, nom.c_str(), suffix.c_str());
1309 int port_number = find_port_hole (buf1);
1310 snprintf (buf2, name_size+1, "%s %d", buf1, port_number);
1312 return string (buf2);
1316 IO::find_port_hole (const char* base)
1318 /* CALLER MUST HOLD IO LOCK */
1322 if (_ports.empty()) {
1326 /* we only allow up to 4 characters for the port number
1329 for (n = 1; n < 9999; ++n) {
1330 char buf[jack_port_name_size()];
1331 PortSet::iterator i = _ports.begin();
1333 snprintf (buf, jack_port_name_size(), _("%s %u"), base, n);
1335 for ( ; i != _ports.end(); ++i) {
1336 if (i->name() == buf) {
1341 if (i == _ports.end()) {
1349 boost::shared_ptr<AudioPort>
1350 IO::audio(uint32_t n) const
1352 return _ports.nth_audio_port (n);
1356 boost::shared_ptr<MidiPort>
1357 IO::midi(uint32_t n) const
1359 return _ports.nth_midi_port (n);
1363 * Setup a bundle that describe our inputs or outputs. Also creates the bundle if necessary.
1372 _bundle.reset (new Bundle (_direction == Input));
1375 _bundle->suspend_signals ();
1377 _bundle->remove_channels ();
1379 if (_direction == Input) {
1380 snprintf(buf, sizeof (buf), _("%s in"), _name.val().c_str());
1382 snprintf(buf, sizeof (buf), _("%s out"), _name.val().c_str());
1384 _bundle->set_name (buf);
1387 for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
1389 uint32_t const N = _ports.count().get (*i);
1390 for (uint32_t j = 0; j < N; ++j) {
1391 _bundle->add_channel (bundle_channel_name (j, N, *i), *i);
1392 _bundle->set_port (c, _session.engine().make_port_name_non_relative (_ports.port(*i, j)->name()));
1398 _bundle->resume_signals ();
1401 /** @return Bundles connected to our ports */
1403 IO::bundles_connected ()
1408 for (std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) {
1409 bundles.push_back ((*i)->bundle);
1412 /* Session bundles */
1413 boost::shared_ptr<ARDOUR::BundleList> b = _session.bundles ();
1414 for (ARDOUR::BundleList::iterator i = b->begin(); i != b->end(); ++i) {
1415 if ((*i)->connected_to (_bundle, _session.engine())) {
1416 bundles.push_back (*i);
1422 boost::shared_ptr<ARDOUR::RouteList> r = _session.get_routes ();
1424 if (_direction == Input) {
1425 for (ARDOUR::RouteList::iterator i = r->begin(); i != r->end(); ++i) {
1426 if ((*i)->output()->bundle()->connected_to (_bundle, _session.engine())) {
1427 bundles.push_back ((*i)->output()->bundle());
1431 for (ARDOUR::RouteList::iterator i = r->begin(); i != r->end(); ++i) {
1432 if ((*i)->input()->bundle()->connected_to (_bundle, _session.engine())) {
1433 bundles.push_back ((*i)->input()->bundle());
1442 IO::UserBundleInfo::UserBundleInfo (IO* io, boost::shared_ptr<UserBundle> b)
1445 b->Changed.connect_same_thread (changed, boost::bind (&IO::bundle_changed, io, _1));
1449 IO::bundle_channel_name (uint32_t c, uint32_t n, DataType t) const
1453 if (t == DataType::AUDIO) {
1459 return c == 0 ? _("L") : _("R");
1461 snprintf (buf, sizeof(buf), _("%d"), (c + 1));
1467 snprintf (buf, sizeof(buf), _("%d"), (c + 1));
1476 IO::name_from_state (const XMLNode& node)
1478 const XMLProperty* prop;
1480 if ((prop = node.property ("name")) != 0) {
1481 return prop->value();
1488 IO::set_name_in_state (XMLNode& node, const string& new_name)
1490 node.add_property (X_("name"), new_name);
1491 XMLNodeList children = node.children ();
1492 for (XMLNodeIterator i = children.begin(); i != children.end(); ++i) {
1493 if ((*i)->name() == X_("Port")) {
1494 string const old_name = (*i)->property(X_("name"))->value();
1495 string const old_name_second_part = old_name.substr (old_name.find_first_of ("/") + 1);
1496 (*i)->add_property (X_("name"), string_compose ("%1/%2", new_name, old_name_second_part));
1502 IO::connected () const
1504 /* do we have any connections at all? */
1506 for (PortSet::const_iterator p = _ports.begin(); p != _ports.end(); ++p) {
1507 if (p->connected()) {
1516 IO::connected_to (boost::shared_ptr<const IO> other) const
1519 return connected ();
1522 assert (_direction != other->direction());
1525 uint32_t no = n_ports().n_total();
1526 uint32_t ni = other->n_ports ().n_total();
1528 for (i = 0; i < no; ++i) {
1529 for (j = 0; j < ni; ++j) {
1530 if (nth(i)->connected_to (other->nth(j)->name())) {
1540 IO::connected_to (const string& str) const
1542 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1543 if (i->connected_to (str)) {
1551 /** Call a processor's ::run() method, giving it our buffers
1552 * Caller must hold process lock.
1555 IO::process_input (boost::shared_ptr<Processor> proc, framepos_t start_frame, framepos_t end_frame, pframes_t nframes)
1557 /* don't read the data into new buffers - just use the port buffers directly */
1559 if (n_ports().n_total() == 0) {
1560 /* We have no ports, so nothing to process */
1564 _buffers.get_jack_port_addresses (_ports, nframes);
1565 proc->run (_buffers, start_frame, end_frame, nframes, true);
1569 IO::collect_input (BufferSet& bufs, pframes_t nframes, ChanCount offset)
1571 assert(bufs.available() >= _ports.count());
1573 if (_ports.count() == ChanCount::ZERO) {
1577 bufs.set_count (_ports.count());
1579 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1580 PortSet::iterator i = _ports.begin(*t);
1581 BufferSet::iterator b = bufs.begin(*t);
1583 for (uint32_t off = 0; off < offset.get(*t); ++off, ++b) {
1584 if (b == bufs.end(*t)) {
1589 for ( ; i != _ports.end(*t); ++i, ++b) {
1590 Buffer& bb (i->get_buffer (nframes));
1591 b->read_from (bb, nframes);
1597 IO::copy_to_outputs (BufferSet& bufs, DataType type, pframes_t nframes, framecnt_t offset)
1599 // Copy any buffers 1:1 to outputs
1601 PortSet::iterator o = _ports.begin(type);
1602 BufferSet::iterator i = bufs.begin(type);
1603 BufferSet::iterator prev = i;
1605 while (i != bufs.end(type) && o != _ports.end (type)) {
1606 Buffer& port_buffer (o->get_buffer (nframes));
1607 port_buffer.read_from (*i, nframes, offset);
1613 // Copy last buffer to any extra outputs
1615 while (o != _ports.end(type)) {
1616 Buffer& port_buffer (o->get_buffer (nframes));
1617 port_buffer.read_from (*prev, nframes, offset);
1622 boost::shared_ptr<Port>
1623 IO::port_by_name (const std::string& str) const
1625 /* to be called only from ::set_state() - no locking */
1627 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1629 if (i->name() == str) {
1630 return boost::const_pointer_cast<Port> (*i);
1634 return boost::shared_ptr<Port> ();
1638 IO::physically_connected () const
1640 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1641 if (i->physically_connected()) {
1650 IO::has_port (boost::shared_ptr<Port> p) const
1652 Glib::Mutex::Lock lm (io_lock);
1653 return _ports.contains (p);