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/threads.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::Threads::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, bool sendish)
62 : SessionObject (s, name)
64 , _default_type (default_type)
68 Port::PostDisconnect.connect_same_thread (*this, boost::bind (&IO::disconnect_check, this, _1, _2));
69 pending_state_node = 0;
73 IO::IO (Session& s, const XMLNode& node, DataType dt, bool sendish)
74 : SessionObject(s, "unnamed io")
80 pending_state_node = 0;
81 Port::PostDisconnect.connect_same_thread (*this, boost::bind (&IO::disconnect_check, this, _1, _2));
83 set_state (node, Stateful::loading_state_version);
89 Glib::Threads::Mutex::Lock lm (io_lock);
91 BLOCK_PROCESS_CALLBACK ();
93 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
94 _session.engine().unregister_port (*i);
99 IO::disconnect_check (boost::shared_ptr<Port> a, boost::shared_ptr<Port> b)
101 /* this could be called from within our own ::disconnect() method(s)
102 or from somewhere that operates directly on a port. so, we don't
103 know for sure if we can take this lock or not. if we fail,
104 we assume that its safely locked by our own ::disconnect().
107 Glib::Threads::Mutex::Lock tm (io_lock, Glib::Threads::TRY_LOCK);
110 /* we took the lock, so we cannot be here from inside
113 if (_ports.contains (a) || _ports.contains (b)) {
114 changed (IOChange (IOChange::ConnectionsChanged), this); /* EMIT SIGNAL */
117 /* we didn't get the lock, so assume that we're inside
118 * ::disconnect(), and it will call changed() appropriately.
124 IO::increment_port_buffer_offset (pframes_t offset)
126 /* io_lock, not taken: function must be called from Session::process() calltree */
128 if (_direction == Output) {
129 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
130 i->increment_port_buffer_offset (offset);
136 IO::silence (framecnt_t nframes)
138 /* io_lock, not taken: function must be called from Session::process() calltree */
140 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
141 i->get_buffer(nframes).silence (nframes);
145 /** Set _bundles_connected to those bundles that are connected such that every
146 * port on every bundle channel x is connected to port x in _ports.
149 IO::check_bundles_connected ()
151 std::vector<UserBundleInfo*> new_list;
153 for (std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) {
155 uint32_t const N = (*i)->bundle->nchannels().n_total();
157 if (_ports.num_ports() < N) {
163 for (uint32_t j = 0; j < N; ++j) {
164 /* Every port on bundle channel j must be connected to our input j */
165 Bundle::PortList const pl = (*i)->bundle->channel_ports (j);
166 for (uint32_t k = 0; k < pl.size(); ++k) {
167 if (_ports.port(j)->connected_to (pl[k]) == false) {
179 new_list.push_back (*i);
185 _bundles_connected = new_list;
190 IO::disconnect (boost::shared_ptr<Port> our_port, string other_port, void* src)
192 if (other_port.length() == 0 || our_port == 0) {
197 Glib::Threads::Mutex::Lock lm (io_lock);
199 /* check that our_port is really one of ours */
201 if ( ! _ports.contains(our_port)) {
205 /* disconnect it from the source */
207 if (our_port->disconnect (other_port)) {
208 error << string_compose(_("IO: cannot disconnect port %1 from %2"), our_port->name(), other_port) << endmsg;
212 check_bundles_connected ();
215 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
217 _session.set_dirty ();
223 IO::connect (boost::shared_ptr<Port> our_port, string other_port, void* src)
225 if (other_port.length() == 0 || our_port == 0) {
230 Glib::Threads::Mutex::Lock lm (io_lock);
232 /* check that our_port is really one of ours */
234 if ( ! _ports.contains(our_port) ) {
238 /* connect it to the source */
240 if (our_port->connect (other_port)) {
244 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
245 _session.set_dirty ();
250 IO::remove_port (boost::shared_ptr<Port> port, void* src)
252 ChanCount before = _ports.count ();
253 ChanCount after = before;
254 after.set (port->type(), after.get (port->type()) - 1);
256 boost::optional<bool> const r = PortCountChanging (after); /* EMIT SIGNAL */
257 if (r.get_value_or (false)) {
264 BLOCK_PROCESS_CALLBACK ();
267 Glib::Threads::Mutex::Lock lm (io_lock);
269 if (_ports.remove(port)) {
270 change.type = IOChange::Type (change.type | IOChange::ConfigurationChanged);
271 change.before = before;
272 change.after = _ports.count ();
274 if (port->connected()) {
275 change.type = IOChange::Type (change.type | IOChange::ConnectionsChanged);
278 _session.engine().unregister_port (port);
279 check_bundles_connected ();
283 PortCountChanged (n_ports()); /* EMIT SIGNAL */
285 if (change.type != IOChange::NoChange) {
286 changed (change, src);
287 _buffers.attach_buffers (_ports);
291 if (change.type & IOChange::ConfigurationChanged) {
295 if (change.type == IOChange::NoChange) {
299 _session.set_dirty ();
306 * @param destination Name of port to connect new port to.
307 * @param src Source for emitted ConfigurationChanged signal.
308 * @param type Data type of port. Default value (NIL) will use this IO's default type.
311 IO::add_port (string destination, void* src, DataType type)
313 boost::shared_ptr<Port> our_port;
315 if (type == DataType::NIL) {
316 type = _default_type;
319 ChanCount before = _ports.count ();
320 ChanCount after = before;
321 after.set (type, after.get (type) + 1);
323 bool const r = PortCountChanging (after); /* EMIT SIGNAL */
331 BLOCK_PROCESS_CALLBACK ();
335 Glib::Threads::Mutex::Lock lm (io_lock);
337 /* Create a new port */
339 string portname = build_legal_port_name (type);
341 if (_direction == Input) {
342 if ((our_port = _session.engine().register_input_port (type, portname)) == 0) {
343 error << string_compose(_("IO: cannot register input port %1"), portname) << endmsg;
347 if ((our_port = _session.engine().register_output_port (type, portname)) == 0) {
348 error << string_compose(_("IO: cannot register output port %1"), portname) << endmsg;
353 change.before = _ports.count ();
354 _ports.add (our_port);
357 PortCountChanged (n_ports()); /* EMIT SIGNAL */
358 change.type = IOChange::ConfigurationChanged;
359 change.after = _ports.count ();
360 changed (change, src); /* EMIT SIGNAL */
361 _buffers.attach_buffers (_ports);
364 if (!destination.empty()) {
365 if (our_port->connect (destination)) {
371 _session.set_dirty ();
377 IO::disconnect (void* src)
380 Glib::Threads::Mutex::Lock lm (io_lock);
382 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
383 i->disconnect_all ();
386 check_bundles_connected ();
389 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
394 /** Caller must hold process lock */
396 IO::ensure_ports_locked (ChanCount count, bool clear, bool& changed)
398 assert (!AudioEngine::instance()->process_lock().trylock());
400 boost::shared_ptr<Port> port;
404 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
406 const size_t n = count.get(*t);
408 /* remove unused ports */
409 for (size_t i = n_ports().get(*t); i > n; --i) {
410 port = _ports.port(*t, i-1);
414 _session.engine().unregister_port (port);
419 /* create any necessary new ports */
420 while (n_ports().get(*t) < n) {
422 string portname = build_legal_port_name (*t);
426 if (_direction == Input) {
427 if ((port = _session.engine().register_input_port (*t, portname)) == 0) {
428 error << string_compose(_("IO: cannot register input port %1"), portname) << endmsg;
432 if ((port = _session.engine().register_output_port (*t, portname)) == 0) {
433 error << string_compose(_("IO: cannot register output port %1"), portname) << endmsg;
439 catch (AudioEngine::PortRegistrationFailure& err) {
450 check_bundles_connected ();
451 PortCountChanged (n_ports()); /* EMIT SIGNAL */
452 _session.set_dirty ();
456 /* disconnect all existing ports so that we get a fresh start */
457 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
458 i->disconnect_all ();
465 /** Caller must hold process lock */
467 IO::ensure_ports (ChanCount count, bool clear, void* src)
469 assert (!AudioEngine::instance()->process_lock().trylock());
471 bool changed = false;
473 if (count == n_ports() && !clear) {
479 change.before = _ports.count ();
482 Glib::Threads::Mutex::Lock im (io_lock);
483 if (ensure_ports_locked (count, clear, changed)) {
489 change.after = _ports.count ();
490 change.type = IOChange::ConfigurationChanged;
491 this->changed (change, src); /* EMIT SIGNAL */
492 _buffers.attach_buffers (_ports);
494 _session.set_dirty ();
500 /** Caller must hold process lock */
502 IO::ensure_io (ChanCount count, bool clear, void* src)
504 assert (!AudioEngine::instance()->process_lock().trylock());
506 return ensure_ports (count, clear, src);
516 IO::state (bool /*full_state*/)
518 XMLNode* node = new XMLNode (state_node_name);
521 vector<string>::iterator ci;
523 LocaleGuard lg (X_("POSIX"));
524 Glib::Threads::Mutex::Lock lm (io_lock);
526 node->add_property("name", _name);
527 id().print (buf, sizeof (buf));
528 node->add_property("id", buf);
529 node->add_property ("direction", enum_2_string (_direction));
530 node->add_property ("default-type", _default_type.to_string());
532 for (std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) {
533 XMLNode* n = new XMLNode ("Bundle");
534 n->add_property ("name", (*i)->bundle->name ());
535 node->add_child_nocopy (*n);
538 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
540 vector<string> connections;
542 XMLNode* pnode = new XMLNode (X_("Port"));
543 pnode->add_property (X_("type"), i->type().to_string());
544 pnode->add_property (X_("name"), i->name());
546 if (i->get_connections (connections)) {
548 for (n = 0, ci = connections.begin(); ci != connections.end(); ++ci, ++n) {
550 /* if its a connection to our own port,
551 return only the port name, not the
552 whole thing. this allows connections
553 to be re-established even when our
554 client name is different.
557 XMLNode* cnode = new XMLNode (X_("Connection"));
559 cnode->add_property (X_("other"), _session.engine().make_port_name_relative (*ci));
560 pnode->add_child_nocopy (*cnode);
564 node->add_child_nocopy (*pnode);
567 snprintf (buf, sizeof (buf), "%" PRId64, _user_latency);
568 node->add_property (X_("user-latency"), buf);
574 IO::set_state (const XMLNode& node, int version)
576 /* callers for version < 3000 need to call set_state_2X directly, as A3 IOs
577 * are input OR output, not both, so the direction needs to be specified
580 assert (version >= 3000);
582 const XMLProperty* prop;
583 XMLNodeConstIterator iter;
584 LocaleGuard lg (X_("POSIX"));
586 /* force use of non-localized representation of decimal point,
587 since we use it a lot in XML files and so forth.
590 if (node.name() != state_node_name) {
591 error << string_compose(_("incorrect XML node \"%1\" passed to IO object"), node.name()) << endmsg;
595 if ((prop = node.property ("name")) != 0) {
596 set_name (prop->value());
599 if ((prop = node.property (X_("default-type"))) != 0) {
600 _default_type = DataType(prop->value());
601 assert(_default_type != DataType::NIL);
606 if ((prop = node.property ("direction")) != 0) {
607 _direction = (Direction) string_2_enum (prop->value(), _direction);
610 if (create_ports (node, version)) {
614 if (connecting_legal) {
616 if (make_connections (node, version, false)) {
622 pending_state_node = new XMLNode (node);
623 pending_state_node_version = version;
624 pending_state_node_in = false;
625 ConnectingLegal.connect_same_thread (connection_legal_c, boost::bind (&IO::connecting_became_legal, this));
628 if ((prop = node.property ("user-latency")) != 0) {
629 _user_latency = atoi (prop->value ());
636 IO::set_state_2X (const XMLNode& node, int version, bool in)
638 const XMLProperty* prop;
639 XMLNodeConstIterator iter;
640 LocaleGuard lg (X_("POSIX"));
642 /* force use of non-localized representation of decimal point,
643 since we use it a lot in XML files and so forth.
646 if (node.name() != state_node_name) {
647 error << string_compose(_("incorrect XML node \"%1\" passed to IO object"), node.name()) << endmsg;
651 if ((prop = node.property ("name")) != 0) {
652 set_name (prop->value());
655 if ((prop = node.property (X_("default-type"))) != 0) {
656 _default_type = DataType(prop->value());
657 assert(_default_type != DataType::NIL);
662 _direction = in ? Input : Output;
664 if (create_ports (node, version)) {
668 if (connecting_legal) {
670 if (make_connections_2X (node, version, in)) {
676 pending_state_node = new XMLNode (node);
677 pending_state_node_version = version;
678 pending_state_node_in = in;
679 ConnectingLegal.connect_same_thread (connection_legal_c, boost::bind (&IO::connecting_became_legal, this));
686 IO::connecting_became_legal ()
690 assert (pending_state_node);
692 connection_legal_c.disconnect ();
694 ret = make_connections (*pending_state_node, pending_state_node_version, pending_state_node_in);
696 delete pending_state_node;
697 pending_state_node = 0;
702 boost::shared_ptr<Bundle>
703 IO::find_possible_bundle (const string &desired_name)
705 static const string digits = "0123456789";
706 const string &default_name = (_direction == Input ? _("in") : _("out"));
707 const string &bundle_type_name = (_direction == Input ? _("input") : _("output"));
709 boost::shared_ptr<Bundle> c = _session.bundle_by_name (desired_name);
712 int bundle_number, mask;
713 string possible_name;
715 string::size_type last_non_digit_pos;
717 error << string_compose(_("Unknown bundle \"%1\" listed for %2 of %3"), desired_name, bundle_type_name, _name)
720 // find numeric suffix of desired name
723 last_non_digit_pos = desired_name.find_last_not_of(digits);
725 if (last_non_digit_pos != string::npos) {
727 s << desired_name.substr(last_non_digit_pos);
731 // see if it's a stereo connection e.g. "in 3+4"
733 if (last_non_digit_pos > 1 && desired_name[last_non_digit_pos] == '+') {
734 string::size_type left_last_non_digit_pos;
736 left_last_non_digit_pos = desired_name.find_last_not_of(digits, last_non_digit_pos-1);
738 if (left_last_non_digit_pos != string::npos) {
739 int left_bundle_number = 0;
741 s << desired_name.substr(left_last_non_digit_pos, last_non_digit_pos-1);
742 s >> left_bundle_number;
744 if (left_bundle_number > 0 && left_bundle_number + 1 == bundle_number) {
755 // find highest set bit
757 while ((mask <= bundle_number) && (mask <<= 1)) {}
759 // "wrap" bundle number into largest possible power of 2
764 if (bundle_number & mask) {
765 bundle_number &= ~mask;
768 s << default_name << " " << bundle_number + 1;
771 s << "+" << bundle_number + 2;
774 possible_name = s.str();
776 if ((c = _session.bundle_by_name (possible_name)) != 0) {
783 info << string_compose (_("Bundle %1 was not available - \"%2\" used instead"), desired_name, possible_name)
786 error << string_compose(_("No %1 bundles available as a replacement"), bundle_type_name)
797 IO::get_port_counts_2X (XMLNode const & node, int /*version*/, ChanCount& n, boost::shared_ptr<Bundle>& /*c*/)
799 XMLProperty const * prop;
800 XMLNodeList children = node.children ();
802 uint32_t n_audio = 0;
804 for (XMLNodeIterator i = children.begin(); i != children.end(); ++i) {
806 if ((prop = node.property ("inputs")) != 0 && _direction == Input) {
807 n_audio = count (prop->value().begin(), prop->value().end(), '{');
808 } else if ((prop = node.property ("input-connection")) != 0 && _direction == Input) {
810 } else if ((prop = node.property ("outputs")) != 0 && _direction == Output) {
811 n_audio = count (prop->value().begin(), prop->value().end(), '{');
812 } else if ((prop = node.property ("output-connection")) != 0 && _direction == Output) {
818 cnt.set_audio (n_audio);
819 n = ChanCount::max (n, cnt);
825 IO::get_port_counts (const XMLNode& node, int version, ChanCount& n, boost::shared_ptr<Bundle>& c)
827 if (version < 3000) {
828 return get_port_counts_2X (node, version, n, c);
831 XMLProperty const * prop;
832 XMLNodeConstIterator iter;
833 uint32_t n_audio = 0;
839 if ((prop = node.property ("connection")) != 0) {
841 if ((c = find_possible_bundle (prop->value())) != 0) {
842 n = ChanCount::max (n, c->nchannels());
847 for (iter = node.children().begin(); iter != node.children().end(); ++iter) {
849 if ((*iter)->name() == X_("Bundle")) {
850 if ((c = find_possible_bundle (prop->value())) != 0) {
851 n = ChanCount::max (n, c->nchannels());
858 if ((*iter)->name() == X_("Port")) {
859 prop = (*iter)->property (X_("type"));
865 if (prop->value() == X_("audio")) {
866 cnt.set_audio (++n_audio);
867 } else if (prop->value() == X_("midi")) {
868 cnt.set_midi (++n_midi);
873 n = ChanCount::max (n, cnt);
878 IO::create_ports (const XMLNode& node, int version)
881 boost::shared_ptr<Bundle> c;
883 get_port_counts (node, version, n, c);
886 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
888 if (ensure_ports (n, true, this)) {
889 error << string_compose(_("%1: cannot create I/O ports"), _name) << endmsg;
900 IO::make_connections (const XMLNode& node, int version, bool in)
902 if (version < 3000) {
903 return make_connections_2X (node, version, in);
906 const XMLProperty* prop;
908 for (XMLNodeConstIterator i = node.children().begin(); i != node.children().end(); ++i) {
910 if ((*i)->name() == "Bundle") {
911 XMLProperty const * prop = (*i)->property ("name");
913 boost::shared_ptr<Bundle> b = find_possible_bundle (prop->value());
915 connect_ports_to_bundle (b, true, this);
922 if ((*i)->name() == "Port") {
924 prop = (*i)->property (X_("name"));
930 boost::shared_ptr<Port> p = port_by_name (prop->value());
933 for (XMLNodeConstIterator c = (*i)->children().begin(); c != (*i)->children().end(); ++c) {
935 XMLNode* cnode = (*c);
937 if (cnode->name() != X_("Connection")) {
941 if ((prop = cnode->property (X_("other"))) == 0) {
946 connect (p, prop->value(), this);
957 IO::prepare_for_reset (XMLNode& node, const std::string& name)
960 node.add_property ("name", name);
962 /* now find connections and reset the name of the port
963 in one so that when we re-use it it will match
964 the name of the thing we're applying it to.
968 XMLNodeList children = node.children();
970 for (XMLNodeIterator i = children.begin(); i != children.end(); ++i) {
972 if ((*i)->name() == "Port") {
974 prop = (*i)->property (X_("name"));
978 string old = prop->value();
979 string::size_type slash = old.find ('/');
981 if (slash != string::npos) {
982 /* port name is of form: <IO-name>/<port-name> */
985 new_name += old.substr (old.find ('/'));
987 prop->set_value (new_name);
996 IO::make_connections_2X (const XMLNode& node, int /*version*/, bool in)
998 const XMLProperty* prop;
1000 /* XXX: bundles ("connections" as was) */
1002 if ((prop = node.property ("inputs")) != 0 && in) {
1004 string::size_type ostart = 0;
1005 string::size_type start = 0;
1006 string::size_type end = 0;
1009 vector<string> ports;
1011 string const str = prop->value ();
1013 while ((start = str.find_first_of ('{', ostart)) != string::npos) {
1016 if ((end = str.find_first_of ('}', start)) == string::npos) {
1017 error << string_compose(_("IO: badly formed string in XML node for inputs \"%1\""), str) << endmsg;
1021 if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
1022 error << string_compose(_("bad input string in XML node \"%1\""), str) << endmsg;
1029 for (int x = 0; x < n; ++x) {
1030 /* XXX: this is a bit of a hack; need to check if it's always valid */
1031 string::size_type const p = ports[x].find ("/out");
1032 if (p != string::npos) {
1033 ports[x].replace (p, 4, "/audio_out");
1035 nth(i)->connect (ports[x]);
1045 if ((prop = node.property ("outputs")) != 0 && !in) {
1047 string::size_type ostart = 0;
1048 string::size_type start = 0;
1049 string::size_type end = 0;
1052 vector<string> ports;
1054 string const str = prop->value ();
1056 while ((start = str.find_first_of ('{', ostart)) != string::npos) {
1059 if ((end = str.find_first_of ('}', start)) == string::npos) {
1060 error << string_compose(_("IO: badly formed string in XML node for outputs \"%1\""), str) << endmsg;
1064 if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
1065 error << string_compose(_("IO: bad output string in XML node \"%1\""), str) << endmsg;
1071 for (int x = 0; x < n; ++x) {
1072 /* XXX: this is a bit of a hack; need to check if it's always valid */
1073 string::size_type const p = ports[x].find ("/in");
1074 if (p != string::npos) {
1075 ports[x].replace (p, 3, "/audio_in");
1077 nth(i)->connect (ports[x]);
1090 IO::set_ports (const string& str)
1092 vector<string> ports;
1097 if ((nports = count (str.begin(), str.end(), '{')) == 0) {
1102 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
1104 // FIXME: audio-only
1105 if (ensure_ports (ChanCount(DataType::AUDIO, nports), true, this)) {
1110 string::size_type start, end, ostart;
1117 while ((start = str.find_first_of ('{', ostart)) != string::npos) {
1120 if ((end = str.find_first_of ('}', start)) == string::npos) {
1121 error << string_compose(_("IO: badly formed string in XML node for inputs \"%1\""), str) << endmsg;
1125 if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
1126 error << string_compose(_("bad input string in XML node \"%1\""), str) << endmsg;
1132 for (int x = 0; x < n; ++x) {
1133 connect (nth (i), ports[x], this);
1145 IO::parse_io_string (const string& str, vector<string>& ports)
1147 string::size_type pos, opos;
1149 if (str.length() == 0) {
1158 while ((pos = str.find_first_of (',', opos)) != string::npos) {
1159 ports.push_back (str.substr (opos, pos - opos));
1163 if (opos < str.length()) {
1164 ports.push_back (str.substr(opos));
1167 return ports.size();
1171 IO::parse_gain_string (const string& str, vector<string>& ports)
1173 string::size_type pos, opos;
1179 while ((pos = str.find_first_of (',', opos)) != string::npos) {
1180 ports.push_back (str.substr (opos, pos - opos));
1184 if (opos < str.length()) {
1185 ports.push_back (str.substr(opos));
1188 return ports.size();
1192 IO::set_name (const string& requested_name)
1194 string name = requested_name;
1196 if (_name == name) {
1200 /* replace all colons in the name. i wish we didn't have to do this */
1202 replace_all (name, ":", "-");
1204 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
1205 string current_name = i->name();
1206 current_name.replace (current_name.find (_name), _name.val().length(), name);
1207 i->set_name (current_name);
1210 bool const r = SessionObject::set_name (name);
1218 IO::latency () const
1220 framecnt_t max_latency;
1225 /* io lock not taken - must be protected by other means */
1227 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1228 if ((latency = i->private_latency_range (_direction == Output).max) > max_latency) {
1229 DEBUG_TRACE (DEBUG::Latency, string_compose ("port %1 has %2 latency of %3 - use\n",
1231 ((_direction == Output) ? "PLAYBACK" : "CAPTURE"),
1233 max_latency = latency;
1237 DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: max %4 latency from %2 ports = %3\n",
1238 name(), _ports.num_ports(), max_latency,
1239 ((_direction == Output) ? "PLAYBACK" : "CAPTURE")));
1244 IO::connect_ports_to_bundle (boost::shared_ptr<Bundle> c, bool exclusive, void* src)
1246 BLOCK_PROCESS_CALLBACK ();
1249 Glib::Threads::Mutex::Lock lm2 (io_lock);
1252 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
1253 i->disconnect_all ();
1257 c->connect (_bundle, _session.engine());
1259 /* If this is a UserBundle, make a note of what we've done */
1261 boost::shared_ptr<UserBundle> ub = boost::dynamic_pointer_cast<UserBundle> (c);
1264 /* See if we already know about this one */
1265 std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin();
1266 while (i != _bundles_connected.end() && (*i)->bundle != ub) {
1270 if (i == _bundles_connected.end()) {
1271 /* We don't, so make a note */
1272 _bundles_connected.push_back (new UserBundleInfo (this, ub));
1277 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
1282 IO::disconnect_ports_from_bundle (boost::shared_ptr<Bundle> c, void* src)
1284 BLOCK_PROCESS_CALLBACK ();
1287 Glib::Threads::Mutex::Lock lm2 (io_lock);
1289 c->disconnect (_bundle, _session.engine());
1291 /* If this is a UserBundle, make a note of what we've done */
1293 boost::shared_ptr<UserBundle> ub = boost::dynamic_pointer_cast<UserBundle> (c);
1296 std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin();
1297 while (i != _bundles_connected.end() && (*i)->bundle != ub) {
1301 if (i != _bundles_connected.end()) {
1303 _bundles_connected.erase (i);
1308 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
1314 IO::disable_connecting ()
1316 connecting_legal = false;
1321 IO::enable_connecting ()
1323 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock());
1324 connecting_legal = true;
1325 boost::optional<int> r = ConnectingLegal ();
1326 return r.get_value_or (0);
1330 IO::bundle_changed (Bundle::Change /*c*/)
1333 // connect_input_ports_to_bundle (_input_bundle, this);
1338 IO::build_legal_port_name (DataType type)
1340 const int name_size = AudioEngine::instance()->port_name_size();
1344 if (type == DataType::AUDIO) {
1345 suffix = X_("audio");
1346 } else if (type == DataType::MIDI) {
1347 suffix = X_("midi");
1349 throw unknown_type();
1352 /* note that if "in" or "out" are translated it will break a session
1353 across locale switches because a port's connection list will
1354 show (old) translated names, but the current port name will
1355 use the (new) translated name.
1359 if (_direction == Input) {
1360 suffix += X_("_return");
1362 suffix += X_("_send");
1365 if (_direction == Input) {
1366 suffix += X_("_in");
1368 suffix += X_("_out");
1372 // allow up to 4 digits for the output port number, plus the slash, suffix and extra space
1374 limit = name_size - AudioEngine::instance()->my_name().length() - (suffix.length() + 5);
1376 char buf1[name_size+1];
1377 char buf2[name_size+1];
1379 /* colons are illegal in port names, so fix that */
1381 string nom = _name.val();
1382 replace_all (nom, ":", ";");
1384 snprintf (buf1, name_size+1, ("%.*s/%s"), limit, nom.c_str(), suffix.c_str());
1386 int port_number = find_port_hole (buf1);
1387 snprintf (buf2, name_size+1, "%s %d", buf1, port_number);
1389 return string (buf2);
1393 IO::find_port_hole (const char* base)
1395 /* CALLER MUST HOLD IO LOCK */
1399 if (_ports.empty()) {
1403 /* we only allow up to 4 characters for the port number
1406 for (n = 1; n < 9999; ++n) {
1407 size_t size = AudioEngine::instance()->port_name_size() + 1;
1409 PortSet::iterator i = _ports.begin();
1411 snprintf (buf, size, _("%s %u"), base, n);
1413 for ( ; i != _ports.end(); ++i) {
1414 if (i->name() == buf) {
1419 if (i == _ports.end()) {
1427 boost::shared_ptr<AudioPort>
1428 IO::audio(uint32_t n) const
1430 return _ports.nth_audio_port (n);
1434 boost::shared_ptr<MidiPort>
1435 IO::midi(uint32_t n) const
1437 return _ports.nth_midi_port (n);
1441 * Setup a bundle that describe our inputs or outputs. Also creates the bundle if necessary.
1450 _bundle.reset (new Bundle (_direction == Input));
1453 _bundle->suspend_signals ();
1455 _bundle->remove_channels ();
1457 if (_direction == Input) {
1458 snprintf(buf, sizeof (buf), _("%s in"), _name.val().c_str());
1460 snprintf(buf, sizeof (buf), _("%s out"), _name.val().c_str());
1462 _bundle->set_name (buf);
1465 for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
1467 uint32_t const N = _ports.count().get (*i);
1468 for (uint32_t j = 0; j < N; ++j) {
1469 _bundle->add_channel (bundle_channel_name (j, N, *i), *i);
1470 _bundle->set_port (c, _session.engine().make_port_name_non_relative (_ports.port(*i, j)->name()));
1476 _bundle->resume_signals ();
1479 /** @return Bundles connected to our ports */
1481 IO::bundles_connected ()
1486 for (std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) {
1487 bundles.push_back ((*i)->bundle);
1490 /* Session bundles */
1491 boost::shared_ptr<ARDOUR::BundleList> b = _session.bundles ();
1492 for (ARDOUR::BundleList::iterator i = b->begin(); i != b->end(); ++i) {
1493 if ((*i)->connected_to (_bundle, _session.engine())) {
1494 bundles.push_back (*i);
1500 boost::shared_ptr<ARDOUR::RouteList> r = _session.get_routes ();
1502 if (_direction == Input) {
1503 for (ARDOUR::RouteList::iterator i = r->begin(); i != r->end(); ++i) {
1504 if ((*i)->output()->bundle()->connected_to (_bundle, _session.engine())) {
1505 bundles.push_back ((*i)->output()->bundle());
1509 for (ARDOUR::RouteList::iterator i = r->begin(); i != r->end(); ++i) {
1510 if ((*i)->input()->bundle()->connected_to (_bundle, _session.engine())) {
1511 bundles.push_back ((*i)->input()->bundle());
1520 IO::UserBundleInfo::UserBundleInfo (IO* io, boost::shared_ptr<UserBundle> b)
1523 b->Changed.connect_same_thread (changed, boost::bind (&IO::bundle_changed, io, _1));
1527 IO::bundle_channel_name (uint32_t c, uint32_t n, DataType t) const
1531 if (t == DataType::AUDIO) {
1537 return c == 0 ? _("L") : _("R");
1539 snprintf (buf, sizeof(buf), _("%d"), (c + 1));
1545 snprintf (buf, sizeof(buf), _("%d"), (c + 1));
1554 IO::name_from_state (const XMLNode& node)
1556 const XMLProperty* prop;
1558 if ((prop = node.property ("name")) != 0) {
1559 return prop->value();
1566 IO::set_name_in_state (XMLNode& node, const string& new_name)
1568 node.add_property (X_("name"), new_name);
1569 XMLNodeList children = node.children ();
1570 for (XMLNodeIterator i = children.begin(); i != children.end(); ++i) {
1571 if ((*i)->name() == X_("Port")) {
1572 string const old_name = (*i)->property(X_("name"))->value();
1573 string const old_name_second_part = old_name.substr (old_name.find_first_of ("/") + 1);
1574 (*i)->add_property (X_("name"), string_compose ("%1/%2", new_name, old_name_second_part));
1580 IO::connected () const
1582 /* do we have any connections at all? */
1584 for (PortSet::const_iterator p = _ports.begin(); p != _ports.end(); ++p) {
1585 if (p->connected()) {
1594 IO::connected_to (boost::shared_ptr<const IO> other) const
1597 return connected ();
1600 assert (_direction != other->direction());
1603 uint32_t no = n_ports().n_total();
1604 uint32_t ni = other->n_ports ().n_total();
1606 for (i = 0; i < no; ++i) {
1607 for (j = 0; j < ni; ++j) {
1608 if (nth(i)->connected_to (other->nth(j)->name())) {
1618 IO::connected_to (const string& str) const
1620 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1621 if (i->connected_to (str)) {
1629 /** Call a processor's ::run() method, giving it our buffers
1630 * Caller must hold process lock.
1633 IO::process_input (boost::shared_ptr<Processor> proc, framepos_t start_frame, framepos_t end_frame, pframes_t nframes)
1635 /* don't read the data into new buffers - just use the port buffers directly */
1637 if (n_ports().n_total() == 0) {
1638 /* We have no ports, so nothing to process */
1642 _buffers.get_backend_port_addresses (_ports, nframes);
1644 proc->run (_buffers, start_frame, end_frame, nframes, true);
1649 IO::collect_input (BufferSet& bufs, pframes_t nframes, ChanCount offset)
1651 assert(bufs.available() >= _ports.count());
1653 if (_ports.count() == ChanCount::ZERO) {
1657 bufs.set_count (_ports.count());
1659 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1660 PortSet::iterator i = _ports.begin(*t);
1661 BufferSet::iterator b = bufs.begin(*t);
1663 for (uint32_t off = 0; off < offset.get(*t); ++off, ++b) {
1664 if (b == bufs.end(*t)) {
1669 for ( ; i != _ports.end(*t); ++i, ++b) {
1670 Buffer& bb (i->get_buffer (nframes));
1671 b->read_from (bb, nframes);
1677 IO::copy_to_outputs (BufferSet& bufs, DataType type, pframes_t nframes, framecnt_t offset)
1679 // Copy any buffers 1:1 to outputs
1681 PortSet::iterator o = _ports.begin(type);
1682 BufferSet::iterator i = bufs.begin(type);
1683 BufferSet::iterator prev = i;
1685 while (i != bufs.end(type) && o != _ports.end (type)) {
1686 Buffer& port_buffer (o->get_buffer (nframes));
1687 port_buffer.read_from (*i, nframes, offset);
1693 // Copy last buffer to any extra outputs
1695 while (o != _ports.end(type)) {
1696 Buffer& port_buffer (o->get_buffer (nframes));
1697 port_buffer.read_from (*prev, nframes, offset);
1702 boost::shared_ptr<Port>
1703 IO::port_by_name (const std::string& str) const
1705 /* to be called only from ::set_state() - no locking */
1707 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1709 if (i->name() == str) {
1710 return boost::const_pointer_cast<Port> (*i);
1714 return boost::shared_ptr<Port> ();
1718 IO::physically_connected () const
1720 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1721 if (i->physically_connected()) {
1730 IO::has_port (boost::shared_ptr<Port> p) const
1732 Glib::Threads::Mutex::Lock lm (io_lock);
1733 return _ports.contains (p);