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/buffer_set.h"
38 #include "ardour/debug.h"
39 #include "ardour/io.h"
40 #include "ardour/port.h"
41 #include "ardour/route.h"
42 #include "ardour/session.h"
43 #include "ardour/user_bundle.h"
47 #define BLOCK_PROCESS_CALLBACK() Glib::Mutex::Lock em (AudioEngine::instance()->process_lock())
50 using namespace ARDOUR;
53 const string IO::state_node_name = "IO";
54 bool IO::connecting_legal = false;
55 PBD::Signal0<int> IO::ConnectingLegal;
56 PBD::Signal1<void,ChanCount> IO::PortCountChanged;
58 /** @param default_type The type of port that will be created by ensure_io
59 * and friends if no type is explicitly requested (to avoid breakage).
61 IO::IO (Session& s, const string& name, Direction dir, DataType default_type)
62 : SessionObject (s, name)
64 , _default_type (default_type)
67 Port::PostDisconnect.connect_same_thread (*this, boost::bind (&IO::disconnect_check, this, _1, _2));
68 pending_state_node = 0;
72 IO::IO (Session& s, const XMLNode& node, DataType dt)
73 : SessionObject(s, "unnamed io")
78 pending_state_node = 0;
79 Port::PostDisconnect.connect_same_thread (*this, boost::bind (&IO::disconnect_check, this, _1, _2));
81 set_state (node, Stateful::loading_state_version);
87 Glib::Mutex::Lock lm (io_lock);
89 BLOCK_PROCESS_CALLBACK ();
91 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
92 _session.engine().unregister_port (*i);
97 IO::disconnect_check (boost::shared_ptr<Port> a, boost::shared_ptr<Port> b)
99 /* this could be called from within our own ::disconnect() method(s)
100 or from somewhere that operates directly on a port. so, we don't
101 know for sure if we can take this lock or not. if we fail,
102 we assume that its safely locked by our own ::disconnect().
105 Glib::Mutex::Lock tm (io_lock, Glib::TRY_LOCK);
108 /* we took the lock, so we cannot be here from inside
111 if (_ports.contains (a) || _ports.contains (b)) {
112 changed (IOChange (IOChange::ConnectionsChanged), this); /* EMIT SIGNAL */
115 /* we didn't get the lock, so assume that we're inside
116 * ::disconnect(), and it will call changed() appropriately.
122 IO::increment_port_buffer_offset (pframes_t offset)
124 /* io_lock, not taken: function must be called from Session::process() calltree */
126 if (_direction == Output) {
127 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
128 i->increment_port_buffer_offset (offset);
134 IO::silence (framecnt_t nframes)
136 /* io_lock, not taken: function must be called from Session::process() calltree */
138 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
139 i->get_buffer(nframes).silence (nframes);
143 /** Set _bundles_connected to those bundles that are connected such that every
144 * port on every bundle channel x is connected to port x in _ports.
147 IO::check_bundles_connected ()
149 std::vector<UserBundleInfo*> new_list;
151 for (std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) {
153 uint32_t const N = (*i)->bundle->nchannels().n_total();
155 if (_ports.num_ports() < N) {
161 for (uint32_t j = 0; j < N; ++j) {
162 /* Every port on bundle channel j must be connected to our input j */
163 Bundle::PortList const pl = (*i)->bundle->channel_ports (j);
164 for (uint32_t k = 0; k < pl.size(); ++k) {
165 if (_ports.port(j)->connected_to (pl[k]) == false) {
177 new_list.push_back (*i);
183 _bundles_connected = new_list;
188 IO::disconnect (boost::shared_ptr<Port> our_port, string other_port, void* src)
190 if (other_port.length() == 0 || our_port == 0) {
195 Glib::Mutex::Lock lm (io_lock);
197 /* check that our_port is really one of ours */
199 if ( ! _ports.contains(our_port)) {
203 /* disconnect it from the source */
205 if (our_port->disconnect (other_port)) {
206 error << string_compose(_("IO: cannot disconnect port %1 from %2"), our_port->name(), other_port) << endmsg;
210 check_bundles_connected ();
213 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
215 _session.set_dirty ();
221 IO::connect (boost::shared_ptr<Port> our_port, string other_port, void* src)
223 if (other_port.length() == 0 || our_port == 0) {
228 Glib::Mutex::Lock lm (io_lock);
230 /* check that our_port is really one of ours */
232 if ( ! _ports.contains(our_port) ) {
236 /* connect it to the source */
238 if (our_port->connect (other_port)) {
242 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
243 _session.set_dirty ();
248 IO::remove_port (boost::shared_ptr<Port> port, void* src)
250 ChanCount before = _ports.count ();
251 ChanCount after = before;
252 after.set (port->type(), after.get (port->type()) - 1);
254 boost::optional<bool> const r = PortCountChanging (after); /* EMIT SIGNAL */
255 if (r.get_value_or (false)) {
262 BLOCK_PROCESS_CALLBACK ();
265 Glib::Mutex::Lock lm (io_lock);
267 if (_ports.remove(port)) {
268 change.type = IOChange::Type (change.type | IOChange::ConfigurationChanged);
269 change.before = before;
270 change.after = _ports.count ();
272 if (port->connected()) {
273 change.type = IOChange::Type (change.type | IOChange::ConnectionsChanged);
276 _session.engine().unregister_port (port);
277 check_bundles_connected ();
281 PortCountChanged (n_ports()); /* EMIT SIGNAL */
283 if (change.type != IOChange::NoChange) {
284 changed (change, src);
285 _buffers.attach_buffers (_ports);
289 if (change.type & IOChange::ConfigurationChanged) {
293 if (change.type == IOChange::NoChange) {
297 _session.set_dirty ();
304 * @param destination Name of port to connect new port to.
305 * @param src Source for emitted ConfigurationChanged signal.
306 * @param type Data type of port. Default value (NIL) will use this IO's default type.
309 IO::add_port (string destination, void* src, DataType type)
311 boost::shared_ptr<Port> our_port;
313 if (type == DataType::NIL) {
314 type = _default_type;
317 ChanCount before = _ports.count ();
318 ChanCount after = before;
319 after.set (type, after.get (type) + 1);
321 bool const r = PortCountChanging (after); /* EMIT SIGNAL */
329 BLOCK_PROCESS_CALLBACK ();
333 Glib::Mutex::Lock lm (io_lock);
335 /* Create a new port */
337 string portname = build_legal_port_name (type);
339 if (_direction == Input) {
340 if ((our_port = _session.engine().register_input_port (type, portname)) == 0) {
341 error << string_compose(_("IO: cannot register input port %1"), portname) << endmsg;
345 if ((our_port = _session.engine().register_output_port (type, portname)) == 0) {
346 error << string_compose(_("IO: cannot register output port %1"), portname) << endmsg;
351 change.before = _ports.count ();
352 _ports.add (our_port);
355 PortCountChanged (n_ports()); /* EMIT SIGNAL */
356 change.type = IOChange::ConfigurationChanged;
357 change.after = _ports.count ();
358 changed (change, src); /* EMIT SIGNAL */
359 _buffers.attach_buffers (_ports);
362 if (!destination.empty()) {
363 if (our_port->connect (destination)) {
369 _session.set_dirty ();
375 IO::disconnect (void* src)
378 Glib::Mutex::Lock lm (io_lock);
380 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
381 i->disconnect_all ();
384 check_bundles_connected ();
387 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
392 /** Caller must hold process lock */
394 IO::ensure_ports_locked (ChanCount count, bool clear, bool& changed)
396 assert (!AudioEngine::instance()->process_lock().trylock());
398 boost::shared_ptr<Port> port;
402 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
404 const size_t n = count.get(*t);
406 /* remove unused ports */
407 for (size_t i = n_ports().get(*t); i > n; --i) {
408 port = _ports.port(*t, i-1);
412 _session.engine().unregister_port (port);
417 /* create any necessary new ports */
418 while (n_ports().get(*t) < n) {
420 string portname = build_legal_port_name (*t);
424 if (_direction == Input) {
425 if ((port = _session.engine().register_input_port (*t, portname)) == 0) {
426 error << string_compose(_("IO: cannot register input port %1"), portname) << endmsg;
430 if ((port = _session.engine().register_output_port (*t, portname)) == 0) {
431 error << string_compose(_("IO: cannot register output port %1"), portname) << endmsg;
437 catch (AudioEngine::PortRegistrationFailure& err) {
448 check_bundles_connected ();
449 PortCountChanged (n_ports()); /* EMIT SIGNAL */
450 _session.set_dirty ();
454 /* disconnect all existing ports so that we get a fresh start */
455 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
456 i->disconnect_all ();
463 /** Caller must hold process lock */
465 IO::ensure_ports (ChanCount count, bool clear, void* src)
467 assert (!AudioEngine::instance()->process_lock().trylock());
469 bool changed = false;
471 if (count == n_ports() && !clear) {
477 change.before = _ports.count ();
480 Glib::Mutex::Lock im (io_lock);
481 if (ensure_ports_locked (count, clear, changed)) {
487 change.after = _ports.count ();
488 change.type = IOChange::ConfigurationChanged;
489 this->changed (change, src); /* EMIT SIGNAL */
490 _buffers.attach_buffers (_ports);
492 _session.set_dirty ();
498 /** Caller must hold process lock */
500 IO::ensure_io (ChanCount count, bool clear, void* src)
502 assert (!AudioEngine::instance()->process_lock().trylock());
504 return ensure_ports (count, clear, src);
514 IO::state (bool /*full_state*/)
516 XMLNode* node = new XMLNode (state_node_name);
519 vector<string>::iterator ci;
521 LocaleGuard lg (X_("POSIX"));
522 Glib::Mutex::Lock lm (io_lock);
524 node->add_property("name", _name);
525 id().print (buf, sizeof (buf));
526 node->add_property("id", buf);
527 node->add_property ("direction", enum_2_string (_direction));
528 node->add_property ("default-type", _default_type.to_string());
530 for (std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) {
531 XMLNode* n = new XMLNode ("Bundle");
532 n->add_property ("name", (*i)->bundle->name ());
533 node->add_child_nocopy (*n);
536 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
538 vector<string> connections;
540 XMLNode* pnode = new XMLNode (X_("Port"));
541 pnode->add_property (X_("type"), i->type().to_string());
542 pnode->add_property (X_("name"), i->name());
544 if (i->get_connections (connections)) {
546 for (n = 0, ci = connections.begin(); ci != connections.end(); ++ci, ++n) {
548 /* if its a connection to our own port,
549 return only the port name, not the
550 whole thing. this allows connections
551 to be re-established even when our
552 client name is different.
555 XMLNode* cnode = new XMLNode (X_("Connection"));
557 cnode->add_property (X_("other"), _session.engine().make_port_name_relative (*ci));
558 pnode->add_child_nocopy (*cnode);
562 node->add_child_nocopy (*pnode);
565 snprintf (buf, sizeof (buf), "%" PRId64, _user_latency);
566 node->add_property (X_("user-latency"), buf);
572 IO::set_state (const XMLNode& node, int version)
574 /* callers for version < 3000 need to call set_state_2X directly, as A3 IOs
575 * are input OR output, not both, so the direction needs to be specified
578 assert (version >= 3000);
580 const XMLProperty* prop;
581 XMLNodeConstIterator iter;
582 LocaleGuard lg (X_("POSIX"));
584 /* force use of non-localized representation of decimal point,
585 since we use it a lot in XML files and so forth.
588 if (node.name() != state_node_name) {
589 error << string_compose(_("incorrect XML node \"%1\" passed to IO object"), node.name()) << endmsg;
593 if ((prop = node.property ("name")) != 0) {
594 set_name (prop->value());
597 if ((prop = node.property (X_("default-type"))) != 0) {
598 _default_type = DataType(prop->value());
599 assert(_default_type != DataType::NIL);
604 if ((prop = node.property ("direction")) != 0) {
605 _direction = (Direction) string_2_enum (prop->value(), _direction);
608 if (create_ports (node, version)) {
612 if (connecting_legal) {
614 if (make_connections (node, version, false)) {
620 pending_state_node = new XMLNode (node);
621 pending_state_node_version = version;
622 pending_state_node_in = false;
623 ConnectingLegal.connect_same_thread (connection_legal_c, boost::bind (&IO::connecting_became_legal, this));
626 if ((prop = node.property ("user-latency")) != 0) {
627 _user_latency = atoi (prop->value ());
634 IO::set_state_2X (const XMLNode& node, int version, bool in)
636 const XMLProperty* prop;
637 XMLNodeConstIterator iter;
638 LocaleGuard lg (X_("POSIX"));
640 /* force use of non-localized representation of decimal point,
641 since we use it a lot in XML files and so forth.
644 if (node.name() != state_node_name) {
645 error << string_compose(_("incorrect XML node \"%1\" passed to IO object"), node.name()) << endmsg;
649 if ((prop = node.property ("name")) != 0) {
650 set_name (prop->value());
653 if ((prop = node.property (X_("default-type"))) != 0) {
654 _default_type = DataType(prop->value());
655 assert(_default_type != DataType::NIL);
660 _direction = in ? Input : Output;
662 if (create_ports (node, version)) {
666 if (connecting_legal) {
668 if (make_connections_2X (node, version, in)) {
674 pending_state_node = new XMLNode (node);
675 pending_state_node_version = version;
676 pending_state_node_in = in;
677 ConnectingLegal.connect_same_thread (connection_legal_c, boost::bind (&IO::connecting_became_legal, this));
684 IO::connecting_became_legal ()
688 assert (pending_state_node);
690 connection_legal_c.disconnect ();
692 ret = make_connections (*pending_state_node, pending_state_node_version, pending_state_node_in);
694 delete pending_state_node;
695 pending_state_node = 0;
700 boost::shared_ptr<Bundle>
701 IO::find_possible_bundle (const string &desired_name)
703 static const string digits = "0123456789";
704 const string &default_name = (_direction == Input ? _("in") : _("out"));
705 const string &bundle_type_name = (_direction == Input ? _("input") : _("output"));
707 boost::shared_ptr<Bundle> c = _session.bundle_by_name (desired_name);
710 int bundle_number, mask;
711 string possible_name;
713 string::size_type last_non_digit_pos;
715 error << string_compose(_("Unknown bundle \"%1\" listed for %2 of %3"), desired_name, bundle_type_name, _name)
718 // find numeric suffix of desired name
721 last_non_digit_pos = desired_name.find_last_not_of(digits);
723 if (last_non_digit_pos != string::npos) {
725 s << desired_name.substr(last_non_digit_pos);
729 // see if it's a stereo connection e.g. "in 3+4"
731 if (last_non_digit_pos > 1 && desired_name[last_non_digit_pos] == '+') {
732 string::size_type left_last_non_digit_pos;
734 left_last_non_digit_pos = desired_name.find_last_not_of(digits, last_non_digit_pos-1);
736 if (left_last_non_digit_pos != string::npos) {
737 int left_bundle_number = 0;
739 s << desired_name.substr(left_last_non_digit_pos, last_non_digit_pos-1);
740 s >> left_bundle_number;
742 if (left_bundle_number > 0 && left_bundle_number + 1 == bundle_number) {
753 // find highest set bit
755 while ((mask <= bundle_number) && (mask <<= 1)) {}
757 // "wrap" bundle number into largest possible power of 2
762 if (bundle_number & mask) {
763 bundle_number &= ~mask;
766 s << default_name << " " << bundle_number + 1;
769 s << "+" << bundle_number + 2;
772 possible_name = s.str();
774 if ((c = _session.bundle_by_name (possible_name)) != 0) {
781 info << string_compose (_("Bundle %1 was not available - \"%2\" used instead"), desired_name, possible_name)
784 error << string_compose(_("No %1 bundles available as a replacement"), bundle_type_name)
795 IO::get_port_counts_2X (XMLNode const & node, int /*version*/, ChanCount& n, boost::shared_ptr<Bundle>& /*c*/)
797 XMLProperty const * prop;
798 XMLNodeList children = node.children ();
800 uint32_t n_audio = 0;
802 for (XMLNodeIterator i = children.begin(); i != children.end(); ++i) {
804 if ((prop = node.property ("inputs")) != 0 && _direction == Input) {
805 n_audio = count (prop->value().begin(), prop->value().end(), '{');
806 } else if ((prop = node.property ("input-connection")) != 0 && _direction == Input) {
808 } else if ((prop = node.property ("outputs")) != 0 && _direction == Output) {
809 n_audio = count (prop->value().begin(), prop->value().end(), '{');
810 } else if ((prop = node.property ("output-connection")) != 0 && _direction == Output) {
816 cnt.set_audio (n_audio);
817 n = ChanCount::max (n, cnt);
823 IO::get_port_counts (const XMLNode& node, int version, ChanCount& n, boost::shared_ptr<Bundle>& c)
825 if (version < 3000) {
826 return get_port_counts_2X (node, version, n, c);
829 XMLProperty const * prop;
830 XMLNodeConstIterator iter;
831 uint32_t n_audio = 0;
837 if ((prop = node.property ("connection")) != 0) {
839 if ((c = find_possible_bundle (prop->value())) != 0) {
840 n = ChanCount::max (n, c->nchannels());
845 for (iter = node.children().begin(); iter != node.children().end(); ++iter) {
847 if ((*iter)->name() == X_("Bundle")) {
848 if ((c = find_possible_bundle (prop->value())) != 0) {
849 n = ChanCount::max (n, c->nchannels());
856 if ((*iter)->name() == X_("Port")) {
857 prop = (*iter)->property (X_("type"));
863 if (prop->value() == X_("audio")) {
864 cnt.set_audio (++n_audio);
865 } else if (prop->value() == X_("midi")) {
866 cnt.set_midi (++n_midi);
871 n = ChanCount::max (n, cnt);
876 IO::create_ports (const XMLNode& node, int version)
879 boost::shared_ptr<Bundle> c;
881 get_port_counts (node, version, n, c);
884 Glib::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
886 if (ensure_ports (n, true, this)) {
887 error << string_compose(_("%1: cannot create I/O ports"), _name) << endmsg;
898 IO::make_connections (const XMLNode& node, int version, bool in)
900 if (version < 3000) {
901 return make_connections_2X (node, version, in);
904 const XMLProperty* prop;
906 for (XMLNodeConstIterator i = node.children().begin(); i != node.children().end(); ++i) {
908 if ((*i)->name() == "Bundle") {
909 XMLProperty const * prop = (*i)->property ("name");
911 boost::shared_ptr<Bundle> b = find_possible_bundle (prop->value());
913 connect_ports_to_bundle (b, this);
920 if ((*i)->name() == "Port") {
922 prop = (*i)->property (X_("name"));
928 boost::shared_ptr<Port> p = port_by_name (prop->value());
931 for (XMLNodeConstIterator c = (*i)->children().begin(); c != (*i)->children().end(); ++c) {
933 XMLNode* cnode = (*c);
935 if (cnode->name() != X_("Connection")) {
939 if ((prop = cnode->property (X_("other"))) == 0) {
944 connect (p, prop->value(), this);
955 IO::prepare_for_reset (XMLNode& node, const std::string& name)
958 node.add_property ("name", name);
960 /* now find connections and reset the name of the port
961 in one so that when we re-use it it will match
962 the name of the thing we're applying it to.
966 XMLNodeList children = node.children();
968 for (XMLNodeIterator i = children.begin(); i != children.end(); ++i) {
970 if ((*i)->name() == "Port") {
972 prop = (*i)->property (X_("name"));
976 string old = prop->value();
977 string::size_type slash = old.find ('/');
979 if (slash != string::npos) {
980 /* port name is of form: <IO-name>/<port-name> */
983 new_name += old.substr (old.find ('/'));
985 prop->set_value (new_name);
994 IO::make_connections_2X (const XMLNode& node, int /*version*/, bool in)
996 const XMLProperty* prop;
998 /* XXX: bundles ("connections" as was) */
1000 if ((prop = node.property ("inputs")) != 0 && in) {
1002 string::size_type ostart = 0;
1003 string::size_type start = 0;
1004 string::size_type end = 0;
1007 vector<string> ports;
1009 string const str = prop->value ();
1011 while ((start = str.find_first_of ('{', ostart)) != string::npos) {
1014 if ((end = str.find_first_of ('}', start)) == string::npos) {
1015 error << string_compose(_("IO: badly formed string in XML node for inputs \"%1\""), str) << endmsg;
1019 if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
1020 error << string_compose(_("bad input string in XML node \"%1\""), str) << endmsg;
1027 for (int x = 0; x < n; ++x) {
1028 /* XXX: this is a bit of a hack; need to check if it's always valid */
1029 string::size_type const p = ports[x].find ("/out");
1030 if (p != string::npos) {
1031 ports[x].replace (p, 4, "/audio_out");
1033 nth(i)->connect (ports[x]);
1043 if ((prop = node.property ("outputs")) != 0 && !in) {
1045 string::size_type ostart = 0;
1046 string::size_type start = 0;
1047 string::size_type end = 0;
1050 vector<string> ports;
1052 string const str = prop->value ();
1054 while ((start = str.find_first_of ('{', ostart)) != string::npos) {
1057 if ((end = str.find_first_of ('}', start)) == string::npos) {
1058 error << string_compose(_("IO: badly formed string in XML node for outputs \"%1\""), str) << endmsg;
1062 if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
1063 error << string_compose(_("IO: bad output string in XML node \"%1\""), str) << endmsg;
1069 for (int x = 0; x < n; ++x) {
1070 /* XXX: this is a bit of a hack; need to check if it's always valid */
1071 string::size_type const p = ports[x].find ("/in");
1072 if (p != string::npos) {
1073 ports[x].replace (p, 3, "/audio_in");
1075 nth(i)->connect (ports[x]);
1088 IO::set_ports (const string& str)
1090 vector<string> ports;
1095 if ((nports = count (str.begin(), str.end(), '{')) == 0) {
1100 Glib::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
1102 // FIXME: audio-only
1103 if (ensure_ports (ChanCount(DataType::AUDIO, nports), true, this)) {
1108 string::size_type start, end, ostart;
1115 while ((start = str.find_first_of ('{', ostart)) != string::npos) {
1118 if ((end = str.find_first_of ('}', start)) == string::npos) {
1119 error << string_compose(_("IO: badly formed string in XML node for inputs \"%1\""), str) << endmsg;
1123 if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
1124 error << string_compose(_("bad input string in XML node \"%1\""), str) << endmsg;
1130 for (int x = 0; x < n; ++x) {
1131 connect (nth (i), ports[x], this);
1143 IO::parse_io_string (const string& str, vector<string>& ports)
1145 string::size_type pos, opos;
1147 if (str.length() == 0) {
1156 while ((pos = str.find_first_of (',', opos)) != string::npos) {
1157 ports.push_back (str.substr (opos, pos - opos));
1161 if (opos < str.length()) {
1162 ports.push_back (str.substr(opos));
1165 return ports.size();
1169 IO::parse_gain_string (const string& str, vector<string>& ports)
1171 string::size_type pos, opos;
1177 while ((pos = str.find_first_of (',', opos)) != string::npos) {
1178 ports.push_back (str.substr (opos, pos - opos));
1182 if (opos < str.length()) {
1183 ports.push_back (str.substr(opos));
1186 return ports.size();
1190 IO::set_name (const string& requested_name)
1192 string name = requested_name;
1194 if (_name == name) {
1198 /* replace all colons in the name. i wish we didn't have to do this */
1200 replace_all (name, ":", "-");
1202 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
1203 string current_name = i->name();
1204 current_name.replace (current_name.find (_name), _name.val().length(), name);
1205 i->set_name (current_name);
1208 bool const r = SessionObject::set_name (name);
1216 IO::latency () const
1218 framecnt_t max_latency;
1223 /* io lock not taken - must be protected by other means */
1225 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1226 if ((latency = i->private_latency_range (_direction == Output).max) > max_latency) {
1227 DEBUG_TRACE (DEBUG::Latency, string_compose ("port %1 has %2 latency of %3 - use\n",
1229 ((_direction == Output) ? "PLAYBACK" : "CAPTURE"),
1231 max_latency = latency;
1235 DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: max %4 latency from %2 ports = %3\n",
1236 name(), _ports.num_ports(), max_latency,
1237 ((_direction == Output) ? "PLAYBACK" : "CAPTURE")));
1242 IO::connect_ports_to_bundle (boost::shared_ptr<Bundle> c, void* src)
1244 BLOCK_PROCESS_CALLBACK ();
1247 Glib::Mutex::Lock lm2 (io_lock);
1249 c->connect (_bundle, _session.engine());
1251 /* If this is a UserBundle, make a note of what we've done */
1253 boost::shared_ptr<UserBundle> ub = boost::dynamic_pointer_cast<UserBundle> (c);
1256 /* See if we already know about this one */
1257 std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin();
1258 while (i != _bundles_connected.end() && (*i)->bundle != ub) {
1262 if (i == _bundles_connected.end()) {
1263 /* We don't, so make a note */
1264 _bundles_connected.push_back (new UserBundleInfo (this, ub));
1269 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
1274 IO::disconnect_ports_from_bundle (boost::shared_ptr<Bundle> c, void* src)
1276 BLOCK_PROCESS_CALLBACK ();
1279 Glib::Mutex::Lock lm2 (io_lock);
1281 c->disconnect (_bundle, _session.engine());
1283 /* If this is a UserBundle, make a note of what we've done */
1285 boost::shared_ptr<UserBundle> ub = boost::dynamic_pointer_cast<UserBundle> (c);
1288 std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin();
1289 while (i != _bundles_connected.end() && (*i)->bundle != ub) {
1293 if (i != _bundles_connected.end()) {
1295 _bundles_connected.erase (i);
1300 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
1306 IO::disable_connecting ()
1308 connecting_legal = false;
1313 IO::enable_connecting ()
1315 Glib::Mutex::Lock lm (AudioEngine::instance()->process_lock());
1316 connecting_legal = true;
1317 boost::optional<int> r = ConnectingLegal ();
1318 return r.get_value_or (0);
1322 IO::bundle_changed (Bundle::Change /*c*/)
1325 // connect_input_ports_to_bundle (_input_bundle, this);
1330 IO::build_legal_port_name (DataType type)
1332 const int name_size = jack_port_name_size();
1336 if (type == DataType::AUDIO) {
1337 suffix = _("audio");
1338 } else if (type == DataType::MIDI) {
1341 throw unknown_type();
1344 /* note that if "in" or "out" are translated it will break a session
1345 across locale switches because a port's connection list will
1346 show (old) translated names, but the current port name will
1347 use the (new) translated name.
1350 if (_direction == Input) {
1351 suffix += X_("_in");
1353 suffix += X_("_out");
1356 // allow up to 4 digits for the output port number, plus the slash, suffix and extra space
1358 limit = name_size - _session.engine().client_name().length() - (suffix.length() + 5);
1360 char buf1[name_size+1];
1361 char buf2[name_size+1];
1363 /* colons are illegal in port names, so fix that */
1365 string nom = _name.val();
1366 replace_all (nom, ":", ";");
1368 snprintf (buf1, name_size+1, ("%.*s/%s"), limit, nom.c_str(), suffix.c_str());
1370 int port_number = find_port_hole (buf1);
1371 snprintf (buf2, name_size+1, "%s %d", buf1, port_number);
1373 return string (buf2);
1377 IO::find_port_hole (const char* base)
1379 /* CALLER MUST HOLD IO LOCK */
1383 if (_ports.empty()) {
1387 /* we only allow up to 4 characters for the port number
1390 for (n = 1; n < 9999; ++n) {
1391 char buf[jack_port_name_size()];
1392 PortSet::iterator i = _ports.begin();
1394 snprintf (buf, jack_port_name_size(), _("%s %u"), base, n);
1396 for ( ; i != _ports.end(); ++i) {
1397 if (i->name() == buf) {
1402 if (i == _ports.end()) {
1410 boost::shared_ptr<AudioPort>
1411 IO::audio(uint32_t n) const
1413 return _ports.nth_audio_port (n);
1417 boost::shared_ptr<MidiPort>
1418 IO::midi(uint32_t n) const
1420 return _ports.nth_midi_port (n);
1424 * Setup a bundle that describe our inputs or outputs. Also creates the bundle if necessary.
1433 _bundle.reset (new Bundle (_direction == Input));
1436 _bundle->suspend_signals ();
1438 _bundle->remove_channels ();
1440 if (_direction == Input) {
1441 snprintf(buf, sizeof (buf), _("%s in"), _name.val().c_str());
1443 snprintf(buf, sizeof (buf), _("%s out"), _name.val().c_str());
1445 _bundle->set_name (buf);
1448 for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
1450 uint32_t const N = _ports.count().get (*i);
1451 for (uint32_t j = 0; j < N; ++j) {
1452 _bundle->add_channel (bundle_channel_name (j, N, *i), *i);
1453 _bundle->set_port (c, _session.engine().make_port_name_non_relative (_ports.port(*i, j)->name()));
1459 _bundle->resume_signals ();
1462 /** @return Bundles connected to our ports */
1464 IO::bundles_connected ()
1469 for (std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) {
1470 bundles.push_back ((*i)->bundle);
1473 /* Session bundles */
1474 boost::shared_ptr<ARDOUR::BundleList> b = _session.bundles ();
1475 for (ARDOUR::BundleList::iterator i = b->begin(); i != b->end(); ++i) {
1476 if ((*i)->connected_to (_bundle, _session.engine())) {
1477 bundles.push_back (*i);
1483 boost::shared_ptr<ARDOUR::RouteList> r = _session.get_routes ();
1485 if (_direction == Input) {
1486 for (ARDOUR::RouteList::iterator i = r->begin(); i != r->end(); ++i) {
1487 if ((*i)->output()->bundle()->connected_to (_bundle, _session.engine())) {
1488 bundles.push_back ((*i)->output()->bundle());
1492 for (ARDOUR::RouteList::iterator i = r->begin(); i != r->end(); ++i) {
1493 if ((*i)->input()->bundle()->connected_to (_bundle, _session.engine())) {
1494 bundles.push_back ((*i)->input()->bundle());
1503 IO::UserBundleInfo::UserBundleInfo (IO* io, boost::shared_ptr<UserBundle> b)
1506 b->Changed.connect_same_thread (changed, boost::bind (&IO::bundle_changed, io, _1));
1510 IO::bundle_channel_name (uint32_t c, uint32_t n, DataType t) const
1514 if (t == DataType::AUDIO) {
1520 return c == 0 ? _("L") : _("R");
1522 snprintf (buf, sizeof(buf), _("%d"), (c + 1));
1528 snprintf (buf, sizeof(buf), _("%d"), (c + 1));
1537 IO::name_from_state (const XMLNode& node)
1539 const XMLProperty* prop;
1541 if ((prop = node.property ("name")) != 0) {
1542 return prop->value();
1549 IO::set_name_in_state (XMLNode& node, const string& new_name)
1551 node.add_property (X_("name"), new_name);
1552 XMLNodeList children = node.children ();
1553 for (XMLNodeIterator i = children.begin(); i != children.end(); ++i) {
1554 if ((*i)->name() == X_("Port")) {
1555 string const old_name = (*i)->property(X_("name"))->value();
1556 string const old_name_second_part = old_name.substr (old_name.find_first_of ("/") + 1);
1557 (*i)->add_property (X_("name"), string_compose ("%1/%2", new_name, old_name_second_part));
1563 IO::connected () const
1565 /* do we have any connections at all? */
1567 for (PortSet::const_iterator p = _ports.begin(); p != _ports.end(); ++p) {
1568 if (p->connected()) {
1577 IO::connected_to (boost::shared_ptr<const IO> other) const
1580 return connected ();
1583 assert (_direction != other->direction());
1586 uint32_t no = n_ports().n_total();
1587 uint32_t ni = other->n_ports ().n_total();
1589 for (i = 0; i < no; ++i) {
1590 for (j = 0; j < ni; ++j) {
1591 if (nth(i)->connected_to (other->nth(j)->name())) {
1601 IO::connected_to (const string& str) const
1603 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1604 if (i->connected_to (str)) {
1612 /** Call a processor's ::run() method, giving it our buffers
1613 * Caller must hold process lock.
1616 IO::process_input (boost::shared_ptr<Processor> proc, framepos_t start_frame, framepos_t end_frame, pframes_t nframes)
1618 /* don't read the data into new buffers - just use the port buffers directly */
1620 if (n_ports().n_total() == 0) {
1621 /* We have no ports, so nothing to process */
1625 _buffers.get_jack_port_addresses (_ports, nframes);
1626 proc->run (_buffers, start_frame, end_frame, nframes, true);
1630 IO::collect_input (BufferSet& bufs, pframes_t nframes, ChanCount offset)
1632 assert(bufs.available() >= _ports.count());
1634 if (_ports.count() == ChanCount::ZERO) {
1638 bufs.set_count (_ports.count());
1640 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1641 PortSet::iterator i = _ports.begin(*t);
1642 BufferSet::iterator b = bufs.begin(*t);
1644 for (uint32_t off = 0; off < offset.get(*t); ++off, ++b) {
1645 if (b == bufs.end(*t)) {
1650 for ( ; i != _ports.end(*t); ++i, ++b) {
1651 Buffer& bb (i->get_buffer (nframes));
1652 b->read_from (bb, nframes);
1658 IO::copy_to_outputs (BufferSet& bufs, DataType type, pframes_t nframes, framecnt_t offset)
1660 // Copy any buffers 1:1 to outputs
1662 PortSet::iterator o = _ports.begin(type);
1663 BufferSet::iterator i = bufs.begin(type);
1664 BufferSet::iterator prev = i;
1666 while (i != bufs.end(type) && o != _ports.end (type)) {
1667 Buffer& port_buffer (o->get_buffer (nframes));
1668 port_buffer.read_from (*i, nframes, offset);
1674 // Copy last buffer to any extra outputs
1676 while (o != _ports.end(type)) {
1677 Buffer& port_buffer (o->get_buffer (nframes));
1678 port_buffer.read_from (*prev, nframes, offset);
1683 boost::shared_ptr<Port>
1684 IO::port_by_name (const std::string& str) const
1686 /* to be called only from ::set_state() - no locking */
1688 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1690 if (i->name() == str) {
1691 return boost::const_pointer_cast<Port> (*i);
1695 return boost::shared_ptr<Port> ();
1699 IO::physically_connected () const
1701 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1702 if (i->physically_connected()) {
1711 IO::has_port (boost::shared_ptr<Port> p) const
1713 Glib::Mutex::Lock lm (io_lock);
1714 return _ports.contains (p);