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/profile.h"
42 #include "ardour/route.h"
43 #include "ardour/session.h"
44 #include "ardour/user_bundle.h"
48 #define BLOCK_PROCESS_CALLBACK() Glib::Threads::Mutex::Lock em (AudioEngine::instance()->process_lock())
51 using namespace ARDOUR;
54 const string IO::state_node_name = "IO";
55 bool IO::connecting_legal = false;
56 PBD::Signal0<int> IO::ConnectingLegal;
57 PBD::Signal1<void,ChanCount> IO::PortCountChanged;
59 /** @param default_type The type of port that will be created by ensure_io
60 * and friends if no type is explicitly requested (to avoid breakage).
62 IO::IO (Session& s, const string& name, Direction dir, DataType default_type, bool sendish)
63 : SessionObject (s, name)
65 , _default_type (default_type)
69 Port::PostDisconnect.connect_same_thread (*this, boost::bind (&IO::disconnect_check, this, _1, _2));
70 pending_state_node = 0;
74 IO::IO (Session& s, const XMLNode& node, DataType dt, bool sendish)
75 : SessionObject(s, "unnamed io")
81 pending_state_node = 0;
82 Port::PostDisconnect.connect_same_thread (*this, boost::bind (&IO::disconnect_check, this, _1, _2));
84 set_state (node, Stateful::loading_state_version);
90 Glib::Threads::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::disconnect_check (boost::shared_ptr<Port> a, boost::shared_ptr<Port> b)
102 /* this could be called from within our own ::disconnect() method(s)
103 or from somewhere that operates directly on a port. so, we don't
104 know for sure if we can take this lock or not. if we fail,
105 we assume that its safely locked by our own ::disconnect().
108 Glib::Threads::Mutex::Lock tm (io_lock, Glib::Threads::TRY_LOCK);
111 /* we took the lock, so we cannot be here from inside
114 if (_ports.contains (a) || _ports.contains (b)) {
115 changed (IOChange (IOChange::ConnectionsChanged), this); /* EMIT SIGNAL */
118 /* we didn't get the lock, so assume that we're inside
119 * ::disconnect(), and it will call changed() appropriately.
125 IO::increment_port_buffer_offset (pframes_t offset)
127 /* io_lock, not taken: function must be called from Session::process() calltree */
129 if (_direction == Output) {
130 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
131 i->increment_port_buffer_offset (offset);
137 IO::silence (framecnt_t nframes)
139 /* io_lock, not taken: function must be called from Session::process() calltree */
141 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
142 i->get_buffer(nframes).silence (nframes);
146 /** Set _bundles_connected to those bundles that are connected such that every
147 * port on every bundle channel x is connected to port x in _ports.
150 IO::check_bundles_connected ()
152 std::vector<UserBundleInfo*> new_list;
154 for (std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) {
156 uint32_t const N = (*i)->bundle->nchannels().n_total();
158 if (_ports.num_ports() < N) {
164 for (uint32_t j = 0; j < N; ++j) {
165 /* Every port on bundle channel j must be connected to our input j */
166 Bundle::PortList const pl = (*i)->bundle->channel_ports (j);
167 for (uint32_t k = 0; k < pl.size(); ++k) {
168 if (_ports.port(j)->connected_to (pl[k]) == false) {
180 new_list.push_back (*i);
186 _bundles_connected = new_list;
191 IO::disconnect (boost::shared_ptr<Port> our_port, string other_port, void* src)
193 if (other_port.length() == 0 || our_port == 0) {
198 Glib::Threads::Mutex::Lock lm (io_lock);
200 /* check that our_port is really one of ours */
202 if ( ! _ports.contains(our_port)) {
206 /* disconnect it from the source */
208 if (our_port->disconnect (other_port)) {
209 error << string_compose(_("IO: cannot disconnect port %1 from %2"), our_port->name(), other_port) << endmsg;
213 check_bundles_connected ();
216 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
218 _session.set_dirty ();
224 IO::connect (boost::shared_ptr<Port> our_port, string other_port, void* src)
226 if (other_port.length() == 0 || our_port == 0) {
231 Glib::Threads::Mutex::Lock lm (io_lock);
233 /* check that our_port is really one of ours */
235 if ( ! _ports.contains(our_port) ) {
239 /* connect it to the source */
241 if (our_port->connect (other_port)) {
245 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
246 _session.set_dirty ();
251 IO::remove_port (boost::shared_ptr<Port> port, void* src)
253 ChanCount before = _ports.count ();
254 ChanCount after = before;
255 after.set (port->type(), after.get (port->type()) - 1);
257 boost::optional<bool> const r = PortCountChanging (after); /* EMIT SIGNAL */
258 if (r.get_value_or (false)) {
265 BLOCK_PROCESS_CALLBACK ();
268 Glib::Threads::Mutex::Lock lm (io_lock);
270 if (_ports.remove(port)) {
271 change.type = IOChange::Type (change.type | IOChange::ConfigurationChanged);
272 change.before = before;
273 change.after = _ports.count ();
275 if (port->connected()) {
276 change.type = IOChange::Type (change.type | IOChange::ConnectionsChanged);
279 _session.engine().unregister_port (port);
280 check_bundles_connected ();
284 PortCountChanged (n_ports()); /* EMIT SIGNAL */
286 if (change.type != IOChange::NoChange) {
287 changed (change, src);
288 _buffers.attach_buffers (_ports);
292 if (change.type & IOChange::ConfigurationChanged) {
296 if (change.type == IOChange::NoChange) {
300 _session.set_dirty ();
307 * @param destination Name of port to connect new port to.
308 * @param src Source for emitted ConfigurationChanged signal.
309 * @param type Data type of port. Default value (NIL) will use this IO's default type.
312 IO::add_port (string destination, void* src, DataType type)
314 boost::shared_ptr<Port> our_port;
316 if (type == DataType::NIL) {
317 type = _default_type;
320 ChanCount before = _ports.count ();
321 ChanCount after = before;
322 after.set (type, after.get (type) + 1);
324 bool const r = PortCountChanging (after); /* EMIT SIGNAL */
332 BLOCK_PROCESS_CALLBACK ();
336 Glib::Threads::Mutex::Lock lm (io_lock);
338 /* Create a new port */
340 string portname = build_legal_port_name (type);
342 if (_direction == Input) {
343 if ((our_port = _session.engine().register_input_port (type, portname)) == 0) {
344 error << string_compose(_("IO: cannot register input port %1"), portname) << endmsg;
348 if ((our_port = _session.engine().register_output_port (type, portname)) == 0) {
349 error << string_compose(_("IO: cannot register output port %1"), portname) << endmsg;
354 change.before = _ports.count ();
355 _ports.add (our_port);
358 PortCountChanged (n_ports()); /* EMIT SIGNAL */
359 change.type = IOChange::ConfigurationChanged;
360 change.after = _ports.count ();
361 changed (change, src); /* EMIT SIGNAL */
362 _buffers.attach_buffers (_ports);
365 if (!destination.empty()) {
366 if (our_port->connect (destination)) {
372 _session.set_dirty ();
378 IO::disconnect (void* src)
381 Glib::Threads::Mutex::Lock lm (io_lock);
383 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
384 i->disconnect_all ();
387 check_bundles_connected ();
390 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
395 /** Caller must hold process lock */
397 IO::ensure_ports_locked (ChanCount count, bool clear, bool& changed)
399 #ifndef PLATFORM_WINDOWS
400 assert (!AudioEngine::instance()->process_lock().trylock());
403 boost::shared_ptr<Port> port;
407 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
409 const size_t n = count.get(*t);
411 /* remove unused ports */
412 for (size_t i = n_ports().get(*t); i > n; --i) {
413 port = _ports.port(*t, i-1);
417 _session.engine().unregister_port (port);
422 /* create any necessary new ports */
423 while (n_ports().get(*t) < n) {
425 string portname = build_legal_port_name (*t);
429 if (_direction == Input) {
430 if ((port = _session.engine().register_input_port (*t, portname)) == 0) {
431 error << string_compose(_("IO: cannot register input port %1"), portname) << endmsg;
435 if ((port = _session.engine().register_output_port (*t, portname)) == 0) {
436 error << string_compose(_("IO: cannot register output port %1"), portname) << endmsg;
442 catch (AudioEngine::PortRegistrationFailure& err) {
453 check_bundles_connected ();
454 PortCountChanged (n_ports()); /* EMIT SIGNAL */
455 _session.set_dirty ();
459 /* disconnect all existing ports so that we get a fresh start */
460 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
461 i->disconnect_all ();
468 /** Caller must hold process lock */
470 IO::ensure_ports (ChanCount count, bool clear, void* src)
472 #ifndef PLATFORM_WINDOWS
473 assert (!AudioEngine::instance()->process_lock().trylock());
476 bool changed = false;
478 if (count == n_ports() && !clear) {
484 change.before = _ports.count ();
487 Glib::Threads::Mutex::Lock im (io_lock);
488 if (ensure_ports_locked (count, clear, changed)) {
494 change.after = _ports.count ();
495 change.type = IOChange::ConfigurationChanged;
496 this->changed (change, src); /* EMIT SIGNAL */
497 _buffers.attach_buffers (_ports);
499 _session.set_dirty ();
505 /** Caller must hold process lock */
507 IO::ensure_io (ChanCount count, bool clear, void* src)
509 #ifndef PLATFORM_WINDOWS
510 assert (!AudioEngine::instance()->process_lock().trylock());
513 return ensure_ports (count, clear, src);
523 IO::state (bool /*full_state*/)
525 XMLNode* node = new XMLNode (state_node_name);
529 LocaleGuard lg (X_("C"));
530 Glib::Threads::Mutex::Lock lm (io_lock);
532 node->add_property("name", _name);
533 id().print (buf, sizeof (buf));
534 node->add_property("id", buf);
535 node->add_property ("direction", enum_2_string (_direction));
536 node->add_property ("default-type", _default_type.to_string());
538 for (std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) {
539 XMLNode* n = new XMLNode ("Bundle");
540 n->add_property ("name", (*i)->bundle->name ());
541 node->add_child_nocopy (*n);
544 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
546 vector<string> connections;
548 XMLNode* pnode = new XMLNode (X_("Port"));
549 pnode->add_property (X_("type"), i->type().to_string());
550 pnode->add_property (X_("name"), i->name());
552 if (i->get_connections (connections)) {
553 vector<string>::const_iterator ci;
554 std::sort (connections.begin(), connections.end());
556 for (n = 0, ci = connections.begin(); ci != connections.end(); ++ci, ++n) {
558 /* if its a connection to our own port,
559 return only the port name, not the
560 whole thing. this allows connections
561 to be re-established even when our
562 client name is different.
565 XMLNode* cnode = new XMLNode (X_("Connection"));
567 cnode->add_property (X_("other"), _session.engine().make_port_name_relative (*ci));
568 pnode->add_child_nocopy (*cnode);
572 node->add_child_nocopy (*pnode);
575 snprintf (buf, sizeof (buf), "%" PRId64, _user_latency);
576 node->add_property (X_("user-latency"), buf);
582 IO::set_state (const XMLNode& node, int version)
584 /* callers for version < 3000 need to call set_state_2X directly, as A3 IOs
585 * are input OR output, not both, so the direction needs to be specified
588 assert (version >= 3000);
590 const XMLProperty* prop;
591 XMLNodeConstIterator iter;
592 LocaleGuard lg (X_("C"));
594 /* force use of non-localized representation of decimal point,
595 since we use it a lot in XML files and so forth.
598 if (node.name() != state_node_name) {
599 error << string_compose(_("incorrect XML node \"%1\" passed to IO object"), node.name()) << endmsg;
603 if ((prop = node.property ("name")) != 0) {
604 set_name (prop->value());
607 if ((prop = node.property (X_("default-type"))) != 0) {
608 _default_type = DataType(prop->value());
609 assert(_default_type != DataType::NIL);
614 if ((prop = node.property ("direction")) != 0) {
615 _direction = (Direction) string_2_enum (prop->value(), _direction);
618 if (create_ports (node, version)) {
622 if (connecting_legal) {
624 if (make_connections (node, version, false)) {
630 pending_state_node = new XMLNode (node);
631 pending_state_node_version = version;
632 pending_state_node_in = false;
633 ConnectingLegal.connect_same_thread (connection_legal_c, boost::bind (&IO::connecting_became_legal, this));
636 if ((prop = node.property ("user-latency")) != 0) {
637 _user_latency = atoi (prop->value ());
644 IO::set_state_2X (const XMLNode& node, int version, bool in)
646 const XMLProperty* prop;
647 XMLNodeConstIterator iter;
648 LocaleGuard lg (X_("C"));
650 /* force use of non-localized representation of decimal point,
651 since we use it a lot in XML files and so forth.
654 if (node.name() != state_node_name) {
655 error << string_compose(_("incorrect XML node \"%1\" passed to IO object"), node.name()) << endmsg;
659 if ((prop = node.property ("name")) != 0) {
660 set_name (prop->value());
663 if ((prop = node.property (X_("default-type"))) != 0) {
664 _default_type = DataType(prop->value());
665 assert(_default_type != DataType::NIL);
670 _direction = in ? Input : Output;
672 if (create_ports (node, version)) {
676 if (connecting_legal) {
678 if (make_connections_2X (node, version, in)) {
684 pending_state_node = new XMLNode (node);
685 pending_state_node_version = version;
686 pending_state_node_in = in;
687 ConnectingLegal.connect_same_thread (connection_legal_c, boost::bind (&IO::connecting_became_legal, this));
694 IO::connecting_became_legal ()
698 assert (pending_state_node);
700 connection_legal_c.disconnect ();
702 // it's not required for TracksLive, as long as TracksLive's session does all the connections when it's being loaded
703 if (!Profile->get_trx() ) {
704 ret = make_connections (*pending_state_node, pending_state_node_version, pending_state_node_in);
707 delete pending_state_node;
708 pending_state_node = 0;
713 boost::shared_ptr<Bundle>
714 IO::find_possible_bundle (const string &desired_name)
716 static const string digits = "0123456789";
717 const string &default_name = (_direction == Input ? _("in") : _("out"));
718 const string &bundle_type_name = (_direction == Input ? _("input") : _("output"));
720 boost::shared_ptr<Bundle> c = _session.bundle_by_name (desired_name);
723 int bundle_number, mask;
724 string possible_name;
726 string::size_type last_non_digit_pos;
728 error << string_compose(_("Unknown bundle \"%1\" listed for %2 of %3"), desired_name, bundle_type_name, _name)
731 // find numeric suffix of desired name
734 last_non_digit_pos = desired_name.find_last_not_of(digits);
736 if (last_non_digit_pos != string::npos) {
738 s << desired_name.substr(last_non_digit_pos);
742 // see if it's a stereo connection e.g. "in 3+4"
744 if (last_non_digit_pos > 1 && desired_name[last_non_digit_pos] == '+') {
745 string::size_type left_last_non_digit_pos;
747 left_last_non_digit_pos = desired_name.find_last_not_of(digits, last_non_digit_pos-1);
749 if (left_last_non_digit_pos != string::npos) {
750 int left_bundle_number = 0;
752 s << desired_name.substr(left_last_non_digit_pos, last_non_digit_pos-1);
753 s >> left_bundle_number;
755 if (left_bundle_number > 0 && left_bundle_number + 1 == bundle_number) {
766 // find highest set bit
768 while ((mask <= bundle_number) && (mask <<= 1)) {}
770 // "wrap" bundle number into largest possible power of 2
775 if (bundle_number & mask) {
776 bundle_number &= ~mask;
779 s << default_name << " " << bundle_number + 1;
782 s << "+" << bundle_number + 2;
785 possible_name = s.str();
787 if ((c = _session.bundle_by_name (possible_name)) != 0) {
794 info << string_compose (_("Bundle %1 was not available - \"%2\" used instead"), desired_name, possible_name)
797 error << string_compose(_("No %1 bundles available as a replacement"), bundle_type_name)
808 IO::get_port_counts_2X (XMLNode const & node, int /*version*/, ChanCount& n, boost::shared_ptr<Bundle>& /*c*/)
810 XMLProperty const * prop;
811 XMLNodeList children = node.children ();
813 uint32_t n_audio = 0;
815 for (XMLNodeIterator i = children.begin(); i != children.end(); ++i) {
817 if ((prop = node.property ("inputs")) != 0 && _direction == Input) {
818 n_audio = count (prop->value().begin(), prop->value().end(), '{');
819 } else if ((prop = node.property ("input-connection")) != 0 && _direction == Input) {
821 } else if ((prop = node.property ("outputs")) != 0 && _direction == Output) {
822 n_audio = count (prop->value().begin(), prop->value().end(), '{');
823 } else if ((prop = node.property ("output-connection")) != 0 && _direction == Output) {
829 cnt.set_audio (n_audio);
830 n = ChanCount::max (n, cnt);
836 IO::get_port_counts (const XMLNode& node, int version, ChanCount& n, boost::shared_ptr<Bundle>& c)
838 if (version < 3000) {
839 return get_port_counts_2X (node, version, n, c);
842 XMLProperty const * prop;
843 XMLNodeConstIterator iter;
844 uint32_t n_audio = 0;
850 if ((prop = node.property ("connection")) != 0) {
852 if ((c = find_possible_bundle (prop->value())) != 0) {
853 n = ChanCount::max (n, c->nchannels());
858 for (iter = node.children().begin(); iter != node.children().end(); ++iter) {
860 if ((*iter)->name() == X_("Bundle")) {
861 prop = (*iter)->property ("name");
862 if ((c = find_possible_bundle (prop->value())) != 0) {
863 n = ChanCount::max (n, c->nchannels());
870 if ((*iter)->name() == X_("Port")) {
871 prop = (*iter)->property (X_("type"));
877 if (prop->value() == X_("audio")) {
878 cnt.set_audio (++n_audio);
879 } else if (prop->value() == X_("midi")) {
880 cnt.set_midi (++n_midi);
885 n = ChanCount::max (n, cnt);
890 IO::create_ports (const XMLNode& node, int version)
893 boost::shared_ptr<Bundle> c;
895 get_port_counts (node, version, n, c);
898 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
900 if (ensure_ports (n, true, this)) {
901 error << string_compose(_("%1: cannot create I/O ports"), _name) << endmsg;
912 IO::make_connections (const XMLNode& node, int version, bool in)
914 if (version < 3000) {
915 return make_connections_2X (node, version, in);
918 const XMLProperty* prop;
920 for (XMLNodeConstIterator i = node.children().begin(); i != node.children().end(); ++i) {
922 if ((*i)->name() == "Bundle") {
923 XMLProperty const * prop = (*i)->property ("name");
925 boost::shared_ptr<Bundle> b = find_possible_bundle (prop->value());
927 connect_ports_to_bundle (b, true, this);
934 if ((*i)->name() == "Port") {
936 prop = (*i)->property (X_("name"));
942 boost::shared_ptr<Port> p = port_by_name (prop->value());
945 for (XMLNodeConstIterator c = (*i)->children().begin(); c != (*i)->children().end(); ++c) {
947 XMLNode* cnode = (*c);
949 if (cnode->name() != X_("Connection")) {
953 if ((prop = cnode->property (X_("other"))) == 0) {
958 connect (p, prop->value(), this);
969 IO::prepare_for_reset (XMLNode& node, const std::string& name)
972 node.add_property ("name", name);
974 /* now find connections and reset the name of the port
975 in one so that when we re-use it it will match
976 the name of the thing we're applying it to.
980 XMLNodeList children = node.children();
982 for (XMLNodeIterator i = children.begin(); i != children.end(); ++i) {
984 if ((*i)->name() == "Port") {
986 prop = (*i)->property (X_("name"));
990 string old = prop->value();
991 string::size_type slash = old.find ('/');
993 if (slash != string::npos) {
994 /* port name is of form: <IO-name>/<port-name> */
997 new_name += old.substr (old.find ('/'));
999 prop->set_value (new_name);
1008 IO::make_connections_2X (const XMLNode& node, int /*version*/, bool in)
1010 const XMLProperty* prop;
1012 /* XXX: bundles ("connections" as was) */
1014 if ((prop = node.property ("inputs")) != 0 && in) {
1016 string::size_type ostart = 0;
1017 string::size_type start = 0;
1018 string::size_type end = 0;
1021 vector<string> ports;
1023 string const str = prop->value ();
1025 while ((start = str.find_first_of ('{', ostart)) != string::npos) {
1028 if ((end = str.find_first_of ('}', start)) == string::npos) {
1029 error << string_compose(_("IO: badly formed string in XML node for inputs \"%1\""), str) << endmsg;
1033 if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
1034 error << string_compose(_("bad input string in XML node \"%1\""), str) << endmsg;
1041 for (int x = 0; x < n; ++x) {
1042 /* XXX: this is a bit of a hack; need to check if it's always valid */
1043 string::size_type const p = ports[x].find ("/out");
1044 if (p != string::npos) {
1045 ports[x].replace (p, 4, "/audio_out");
1047 if (NULL != nth(i).get())
1048 nth(i)->connect (ports[x]);
1058 if ((prop = node.property ("outputs")) != 0 && !in) {
1060 string::size_type ostart = 0;
1061 string::size_type start = 0;
1062 string::size_type end = 0;
1065 vector<string> ports;
1067 string const str = prop->value ();
1069 while ((start = str.find_first_of ('{', ostart)) != string::npos) {
1072 if ((end = str.find_first_of ('}', start)) == string::npos) {
1073 error << string_compose(_("IO: badly formed string in XML node for outputs \"%1\""), str) << endmsg;
1077 if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
1078 error << string_compose(_("IO: bad output string in XML node \"%1\""), str) << endmsg;
1084 for (int x = 0; x < n; ++x) {
1085 /* XXX: this is a bit of a hack; need to check if it's always valid */
1086 string::size_type const p = ports[x].find ("/in");
1087 if (p != string::npos) {
1088 ports[x].replace (p, 3, "/audio_in");
1090 if (NULL != nth(i).get())
1091 nth(i)->connect (ports[x]);
1104 IO::set_ports (const string& str)
1106 vector<string> ports;
1110 if ((nports = count (str.begin(), str.end(), '{')) == 0) {
1115 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
1117 // FIXME: audio-only
1118 if (ensure_ports (ChanCount(DataType::AUDIO, nports), true, this)) {
1123 string::size_type start = 0;
1124 string::size_type end = 0;
1125 string::size_type ostart = 0;
1126 for (int i = 0; (start = str.find_first_of ('{', ostart)) != string::npos; ++i) {
1129 if ((end = str.find_first_of ('}', start)) == string::npos) {
1130 error << string_compose(_("IO: badly formed string in XML node for inputs \"%1\""), str) << endmsg;
1134 if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
1135 error << string_compose(_("bad input string in XML node \"%1\""), str) << endmsg;
1141 for (int x = 0; x < n; ++x) {
1142 connect (nth (i), ports[x], this);
1153 IO::parse_io_string (const string& str, vector<string>& ports)
1155 string::size_type pos, opos;
1157 if (str.length() == 0) {
1165 while ((pos = str.find_first_of (',', opos)) != string::npos) {
1166 ports.push_back (str.substr (opos, pos - opos));
1170 if (opos < str.length()) {
1171 ports.push_back (str.substr(opos));
1174 return ports.size();
1178 IO::parse_gain_string (const string& str, vector<string>& ports)
1180 string::size_type pos, opos;
1185 while ((pos = str.find_first_of (',', opos)) != string::npos) {
1186 ports.push_back (str.substr (opos, pos - opos));
1190 if (opos < str.length()) {
1191 ports.push_back (str.substr(opos));
1194 return ports.size();
1198 IO::set_name (const string& requested_name)
1200 string name = requested_name;
1202 if (_name == name) {
1206 /* replace all colons in the name. i wish we didn't have to do this */
1208 replace_all (name, ":", "-");
1210 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
1211 string current_name = i->name();
1212 current_name.replace (current_name.find (_name), _name.val().length(), name);
1213 i->set_name (current_name);
1216 bool const r = SessionObject::set_name (name);
1224 IO::latency () const
1226 framecnt_t max_latency;
1231 /* io lock not taken - must be protected by other means */
1233 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1234 if ((latency = i->private_latency_range (_direction == Output).max) > max_latency) {
1235 DEBUG_TRACE (DEBUG::Latency, string_compose ("port %1 has %2 latency of %3 - use\n",
1237 ((_direction == Output) ? "PLAYBACK" : "CAPTURE"),
1239 max_latency = latency;
1243 DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: max %4 latency from %2 ports = %3\n",
1244 name(), _ports.num_ports(), max_latency,
1245 ((_direction == Output) ? "PLAYBACK" : "CAPTURE")));
1250 IO::connect_ports_to_bundle (boost::shared_ptr<Bundle> c, bool exclusive, void* src)
1252 BLOCK_PROCESS_CALLBACK ();
1255 Glib::Threads::Mutex::Lock lm2 (io_lock);
1258 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
1259 i->disconnect_all ();
1263 c->connect (_bundle, _session.engine());
1265 /* If this is a UserBundle, make a note of what we've done */
1267 boost::shared_ptr<UserBundle> ub = boost::dynamic_pointer_cast<UserBundle> (c);
1270 /* See if we already know about this one */
1271 std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin();
1272 while (i != _bundles_connected.end() && (*i)->bundle != ub) {
1276 if (i == _bundles_connected.end()) {
1277 /* We don't, so make a note */
1278 _bundles_connected.push_back (new UserBundleInfo (this, ub));
1283 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
1288 IO::disconnect_ports_from_bundle (boost::shared_ptr<Bundle> c, void* src)
1290 BLOCK_PROCESS_CALLBACK ();
1293 Glib::Threads::Mutex::Lock lm2 (io_lock);
1295 c->disconnect (_bundle, _session.engine());
1297 /* If this is a UserBundle, make a note of what we've done */
1299 boost::shared_ptr<UserBundle> ub = boost::dynamic_pointer_cast<UserBundle> (c);
1302 std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin();
1303 while (i != _bundles_connected.end() && (*i)->bundle != ub) {
1307 if (i != _bundles_connected.end()) {
1309 _bundles_connected.erase (i);
1314 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
1320 IO::disable_connecting ()
1322 connecting_legal = false;
1327 IO::enable_connecting ()
1329 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock());
1330 connecting_legal = true;
1331 boost::optional<int> r = ConnectingLegal ();
1332 return r.get_value_or (0);
1336 IO::bundle_changed (Bundle::Change /*c*/)
1339 // connect_input_ports_to_bundle (_input_bundle, this);
1344 IO::build_legal_port_name (DataType type)
1346 const int name_size = AudioEngine::instance()->port_name_size();
1350 if (type == DataType::AUDIO) {
1351 suffix = X_("audio");
1352 } else if (type == DataType::MIDI) {
1353 suffix = X_("midi");
1355 throw unknown_type();
1358 /* note that if "in" or "out" are translated it will break a session
1359 across locale switches because a port's connection list will
1360 show (old) translated names, but the current port name will
1361 use the (new) translated name.
1365 if (_direction == Input) {
1366 suffix += X_("_return");
1368 suffix += X_("_send");
1371 if (_direction == Input) {
1372 suffix += X_("_in");
1374 suffix += X_("_out");
1378 // allow up to 4 digits for the output port number, plus the slash, suffix and extra space
1380 limit = name_size - AudioEngine::instance()->my_name().length() - (suffix.length() + 5);
1382 std::vector<char> buf1(name_size+1);
1383 std::vector<char> buf2(name_size+1);
1385 /* colons are illegal in port names, so fix that */
1387 string nom = _name.val();
1388 replace_all (nom, ":", ";");
1390 snprintf (&buf1[0], name_size+1, ("%.*s/%s"), limit, nom.c_str(), suffix.c_str());
1392 int port_number = find_port_hole (&buf1[0]);
1393 snprintf (&buf2[0], name_size+1, "%s %d", &buf1[0], port_number);
1395 return string (&buf2[0]);
1399 IO::find_port_hole (const char* base)
1401 /* CALLER MUST HOLD IO LOCK */
1405 if (_ports.empty()) {
1409 /* we only allow up to 4 characters for the port number
1412 for (n = 1; n < 9999; ++n) {
1413 std::vector<char> buf (AudioEngine::instance()->port_name_size());
1414 PortSet::iterator i = _ports.begin();
1416 snprintf (&buf[0], buf.size()+1, _("%s %u"), base, n);
1418 for ( ; i != _ports.end(); ++i) {
1419 if (string(i->name()) == string(&buf[0])) {
1424 if (i == _ports.end()) {
1432 boost::shared_ptr<AudioPort>
1433 IO::audio(uint32_t n) const
1435 return _ports.nth_audio_port (n);
1439 boost::shared_ptr<MidiPort>
1440 IO::midi(uint32_t n) const
1442 return _ports.nth_midi_port (n);
1446 * Setup a bundle that describe our inputs or outputs. Also creates the bundle if necessary.
1455 _bundle.reset (new Bundle (_direction == Input));
1458 _bundle->suspend_signals ();
1460 _bundle->remove_channels ();
1462 if (_direction == Input) {
1463 snprintf(buf, sizeof (buf), _("%s in"), _name.val().c_str());
1465 snprintf(buf, sizeof (buf), _("%s out"), _name.val().c_str());
1467 _bundle->set_name (buf);
1470 for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
1472 uint32_t const N = _ports.count().get (*i);
1473 for (uint32_t j = 0; j < N; ++j) {
1474 _bundle->add_channel (bundle_channel_name (j, N, *i), *i);
1475 _bundle->set_port (c, _session.engine().make_port_name_non_relative (_ports.port(*i, j)->name()));
1481 _bundle->resume_signals ();
1484 /** @return Bundles connected to our ports */
1486 IO::bundles_connected ()
1491 for (std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) {
1492 bundles.push_back ((*i)->bundle);
1495 /* Session bundles */
1496 boost::shared_ptr<ARDOUR::BundleList> b = _session.bundles ();
1497 for (ARDOUR::BundleList::iterator i = b->begin(); i != b->end(); ++i) {
1498 if ((*i)->connected_to (_bundle, _session.engine())) {
1499 bundles.push_back (*i);
1505 boost::shared_ptr<ARDOUR::RouteList> r = _session.get_routes ();
1507 if (_direction == Input) {
1508 for (ARDOUR::RouteList::iterator i = r->begin(); i != r->end(); ++i) {
1509 if ((*i)->output()->bundle()->connected_to (_bundle, _session.engine())) {
1510 bundles.push_back ((*i)->output()->bundle());
1514 for (ARDOUR::RouteList::iterator i = r->begin(); i != r->end(); ++i) {
1515 if ((*i)->input()->bundle()->connected_to (_bundle, _session.engine())) {
1516 bundles.push_back ((*i)->input()->bundle());
1525 IO::UserBundleInfo::UserBundleInfo (IO* io, boost::shared_ptr<UserBundle> b)
1528 b->Changed.connect_same_thread (changed, boost::bind (&IO::bundle_changed, io, _1));
1532 IO::bundle_channel_name (uint32_t c, uint32_t n, DataType t) const
1536 if (t == DataType::AUDIO) {
1542 return c == 0 ? _("L") : _("R");
1544 snprintf (buf, sizeof(buf), _("%d"), (c + 1));
1550 snprintf (buf, sizeof(buf), _("%d"), (c + 1));
1559 IO::name_from_state (const XMLNode& node)
1561 const XMLProperty* prop;
1563 if ((prop = node.property ("name")) != 0) {
1564 return prop->value();
1571 IO::set_name_in_state (XMLNode& node, const string& new_name)
1573 node.add_property (X_("name"), new_name);
1574 XMLNodeList children = node.children ();
1575 for (XMLNodeIterator i = children.begin(); i != children.end(); ++i) {
1576 if ((*i)->name() == X_("Port")) {
1577 string const old_name = (*i)->property(X_("name"))->value();
1578 string const old_name_second_part = old_name.substr (old_name.find_first_of ("/") + 1);
1579 (*i)->add_property (X_("name"), string_compose ("%1/%2", new_name, old_name_second_part));
1585 IO::connected () const
1587 /* do we have any connections at all? */
1589 for (PortSet::const_iterator p = _ports.begin(); p != _ports.end(); ++p) {
1590 if (p->connected()) {
1599 IO::connected_to (boost::shared_ptr<const IO> other) const
1602 return connected ();
1605 assert (_direction != other->direction());
1608 uint32_t no = n_ports().n_total();
1609 uint32_t ni = other->n_ports ().n_total();
1611 for (i = 0; i < no; ++i) {
1612 for (j = 0; j < ni; ++j) {
1613 if ((NULL != nth(i).get()) && (NULL != other->nth(j).get())) {
1614 if (nth(i)->connected_to (other->nth(j)->name())) {
1625 IO::connected_to (const string& str) const
1627 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1628 if (i->connected_to (str)) {
1636 /** Call a processor's ::run() method, giving it our buffers
1637 * Caller must hold process lock.
1640 IO::process_input (boost::shared_ptr<Processor> proc, framepos_t start_frame, framepos_t end_frame, pframes_t nframes)
1642 /* don't read the data into new buffers - just use the port buffers directly */
1644 if (n_ports().n_total() == 0) {
1645 /* We have no ports, so nothing to process */
1649 _buffers.get_backend_port_addresses (_ports, nframes);
1651 proc->run (_buffers, start_frame, end_frame, nframes, true);
1656 IO::collect_input (BufferSet& bufs, pframes_t nframes, ChanCount offset)
1658 assert(bufs.available() >= _ports.count());
1660 if (_ports.count() == ChanCount::ZERO) {
1664 bufs.set_count (_ports.count());
1666 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1667 PortSet::iterator i = _ports.begin(*t);
1668 BufferSet::iterator b = bufs.begin(*t);
1670 for (uint32_t off = 0; off < offset.get(*t); ++off, ++b) {
1671 if (b == bufs.end(*t)) {
1676 for ( ; i != _ports.end(*t); ++i, ++b) {
1677 Buffer& bb (i->get_buffer (nframes));
1678 b->read_from (bb, nframes);
1684 IO::copy_to_outputs (BufferSet& bufs, DataType type, pframes_t nframes, framecnt_t offset)
1686 // Copy any buffers 1:1 to outputs
1688 PortSet::iterator o = _ports.begin(type);
1689 BufferSet::iterator i = bufs.begin(type);
1690 BufferSet::iterator prev = i;
1692 while (i != bufs.end(type) && o != _ports.end (type)) {
1693 Buffer& port_buffer (o->get_buffer (nframes));
1694 port_buffer.read_from (*i, nframes, offset);
1700 // Copy last buffer to any extra outputs
1702 while (o != _ports.end(type)) {
1703 Buffer& port_buffer (o->get_buffer (nframes));
1704 port_buffer.read_from (*prev, nframes, offset);
1709 boost::shared_ptr<Port>
1710 IO::port_by_name (const std::string& str) const
1712 /* to be called only from ::set_state() - no locking */
1714 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1716 if (i->name() == str) {
1717 return boost::const_pointer_cast<Port> (*i);
1721 return boost::shared_ptr<Port> ();
1725 IO::physically_connected () const
1727 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1728 if (i->physically_connected()) {
1737 IO::has_port (boost::shared_ptr<Port> p) const
1739 Glib::Threads::Mutex::Lock lm (io_lock);
1740 return _ports.contains (p);