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 DEBUG_TRACE (DEBUG::Ports, string_compose ("IO %1 unregisters %2 ports\n", name(), _ports.num_ports()));
94 BLOCK_PROCESS_CALLBACK ();
96 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
97 _session.engine().unregister_port (*i);
99 delete pending_state_node; pending_state_node = 0;
103 IO::disconnect_check (boost::shared_ptr<Port> a, boost::shared_ptr<Port> b)
105 if (_session.state_of_the_state () & Session::Deletion) {
108 /* this could be called from within our own ::disconnect() method(s)
109 or from somewhere that operates directly on a port. so, we don't
110 know for sure if we can take this lock or not. if we fail,
111 we assume that its safely locked by our own ::disconnect().
114 Glib::Threads::Mutex::Lock tm (io_lock, Glib::Threads::TRY_LOCK);
117 /* we took the lock, so we cannot be here from inside
120 if (_ports.contains (a) || _ports.contains (b)) {
121 changed (IOChange (IOChange::ConnectionsChanged), this); /* EMIT SIGNAL */
124 /* we didn't get the lock, so assume that we're inside
125 * ::disconnect(), and it will call changed() appropriately.
131 IO::increment_port_buffer_offset (pframes_t offset)
133 /* io_lock, not taken: function must be called from Session::process() calltree */
135 if (_direction == Output) {
136 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
137 i->increment_port_buffer_offset (offset);
143 IO::silence (framecnt_t nframes)
145 /* io_lock, not taken: function must be called from Session::process() calltree */
147 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
148 i->get_buffer(nframes).silence (nframes);
152 /** Set _bundles_connected to those bundles that are connected such that every
153 * port on every bundle channel x is connected to port x in _ports.
156 IO::check_bundles_connected ()
158 std::vector<UserBundleInfo*> new_list;
160 for (std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) {
162 uint32_t const N = (*i)->bundle->nchannels().n_total();
164 if (_ports.num_ports() < N) {
170 for (uint32_t j = 0; j < N; ++j) {
171 /* Every port on bundle channel j must be connected to our input j */
172 Bundle::PortList const pl = (*i)->bundle->channel_ports (j);
173 for (uint32_t k = 0; k < pl.size(); ++k) {
174 if (_ports.port(j)->connected_to (pl[k]) == false) {
186 new_list.push_back (*i);
192 _bundles_connected = new_list;
197 IO::disconnect (boost::shared_ptr<Port> our_port, string other_port, void* src)
199 if (other_port.length() == 0 || our_port == 0) {
204 Glib::Threads::Mutex::Lock lm (io_lock);
206 /* check that our_port is really one of ours */
208 if ( ! _ports.contains(our_port)) {
212 /* disconnect it from the source */
214 if (our_port->disconnect (other_port)) {
215 error << string_compose(_("IO: cannot disconnect port %1 from %2"), our_port->name(), other_port) << endmsg;
219 check_bundles_connected ();
222 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
224 _session.set_dirty ();
230 IO::connect (boost::shared_ptr<Port> our_port, string other_port, void* src)
232 if (other_port.length() == 0 || our_port == 0) {
237 Glib::Threads::Mutex::Lock lm (io_lock);
239 /* check that our_port is really one of ours */
241 if ( ! _ports.contains(our_port) ) {
245 /* connect it to the source */
247 if (our_port->connect (other_port)) {
251 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
252 _session.set_dirty ();
257 IO::remove_port (boost::shared_ptr<Port> port, void* src)
259 ChanCount before = _ports.count ();
260 ChanCount after = before;
261 after.set (port->type(), after.get (port->type()) - 1);
263 boost::optional<bool> const r = PortCountChanging (after); /* EMIT SIGNAL */
264 if (r.get_value_or (false)) {
271 BLOCK_PROCESS_CALLBACK ();
274 Glib::Threads::Mutex::Lock lm (io_lock);
276 if (_ports.remove(port)) {
277 change.type = IOChange::Type (change.type | IOChange::ConfigurationChanged);
278 change.before = before;
279 change.after = _ports.count ();
281 if (port->connected()) {
282 change.type = IOChange::Type (change.type | IOChange::ConnectionsChanged);
285 _session.engine().unregister_port (port);
286 check_bundles_connected ();
290 PortCountChanged (n_ports()); /* EMIT SIGNAL */
292 if (change.type != IOChange::NoChange) {
293 changed (change, src);
294 _buffers.attach_buffers (_ports);
298 if (change.type & IOChange::ConfigurationChanged) {
302 if (change.type == IOChange::NoChange) {
306 _session.set_dirty ();
313 * @param destination Name of port to connect new port to.
314 * @param src Source for emitted ConfigurationChanged signal.
315 * @param type Data type of port. Default value (NIL) will use this IO's default type.
318 IO::add_port (string destination, void* src, DataType type)
320 boost::shared_ptr<Port> our_port;
322 if (type == DataType::NIL) {
323 type = _default_type;
326 ChanCount before = _ports.count ();
327 ChanCount after = before;
328 after.set (type, after.get (type) + 1);
330 bool const r = PortCountChanging (after); /* EMIT SIGNAL */
338 BLOCK_PROCESS_CALLBACK ();
342 Glib::Threads::Mutex::Lock lm (io_lock);
344 /* Create a new port */
346 string portname = build_legal_port_name (type);
348 if (_direction == Input) {
349 if ((our_port = _session.engine().register_input_port (type, portname)) == 0) {
350 error << string_compose(_("IO: cannot register input port %1"), portname) << endmsg;
354 if ((our_port = _session.engine().register_output_port (type, portname)) == 0) {
355 error << string_compose(_("IO: cannot register output port %1"), portname) << endmsg;
360 change.before = _ports.count ();
361 _ports.add (our_port);
364 PortCountChanged (n_ports()); /* EMIT SIGNAL */
365 change.type = IOChange::ConfigurationChanged;
366 change.after = _ports.count ();
367 changed (change, src); /* EMIT SIGNAL */
368 _buffers.attach_buffers (_ports);
371 if (!destination.empty()) {
372 if (our_port->connect (destination)) {
377 apply_pretty_name ();
379 _session.set_dirty ();
385 IO::disconnect (void* src)
388 Glib::Threads::Mutex::Lock lm (io_lock);
390 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
391 i->disconnect_all ();
394 check_bundles_connected ();
397 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
402 /** Caller must hold process lock */
404 IO::ensure_ports_locked (ChanCount count, bool clear, bool& changed)
406 #ifndef PLATFORM_WINDOWS
407 assert (!AudioEngine::instance()->process_lock().trylock());
410 boost::shared_ptr<Port> port;
411 vector<boost::shared_ptr<Port> > deleted_ports;
415 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
417 const size_t n = count.get(*t);
419 /* remove unused ports */
420 for (size_t i = n_ports().get(*t); i > n; --i) {
421 port = _ports.port(*t, i-1);
426 /* hold a reference to the port so that we can ensure
427 * that this thread, and not a JACK notification thread,
428 * holds the final reference.
431 deleted_ports.push_back (port);
432 _session.engine().unregister_port (port);
437 /* this will drop the final reference to the deleted ports,
438 * which will in turn call their destructors, which will in
439 * turn call the backend to unregister them.
441 * There will no connect/disconnect or register/unregister
442 * callbacks from the backend until we get here, because
443 * they are driven by the Port destructor. The destructor
444 * will not execute until we drop the final reference,
445 * which all happens right .... here.
447 deleted_ports.clear ();
449 /* create any necessary new ports */
450 while (n_ports().get(*t) < n) {
452 string portname = build_legal_port_name (*t);
456 if (_direction == Input) {
457 if ((port = _session.engine().register_input_port (*t, portname)) == 0) {
458 error << string_compose(_("IO: cannot register input port %1"), portname) << endmsg;
462 if ((port = _session.engine().register_output_port (*t, portname)) == 0) {
463 error << string_compose(_("IO: cannot register output port %1"), portname) << endmsg;
469 catch (AudioEngine::PortRegistrationFailure& err) {
480 check_bundles_connected ();
481 PortCountChanged (n_ports()); /* EMIT SIGNAL */
482 _session.set_dirty ();
486 /* disconnect all existing ports so that we get a fresh start */
487 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
488 i->disconnect_all ();
495 /** Caller must hold process lock */
497 IO::ensure_ports (ChanCount count, bool clear, void* src)
499 #ifndef PLATFORM_WINDOWS
500 assert (!AudioEngine::instance()->process_lock().trylock());
503 bool changed = false;
505 if (count == n_ports() && !clear) {
511 change.before = _ports.count ();
514 Glib::Threads::Mutex::Lock im (io_lock);
515 if (ensure_ports_locked (count, clear, changed)) {
521 change.after = _ports.count ();
522 change.type = IOChange::ConfigurationChanged;
523 this->changed (change, src); /* EMIT SIGNAL */
524 _buffers.attach_buffers (_ports);
526 _session.set_dirty ();
532 /** Caller must hold process lock */
534 IO::ensure_io (ChanCount count, bool clear, void* src)
536 #ifndef PLATFORM_WINDOWS
537 assert (!AudioEngine::instance()->process_lock().trylock());
540 return ensure_ports (count, clear, src);
550 IO::state (bool /*full_state*/)
552 XMLNode* node = new XMLNode (state_node_name);
557 Glib::Threads::Mutex::Lock lm (io_lock);
559 node->add_property("name", _name);
560 node->add_property ("id", id ().to_s ());
561 node->add_property ("direction", enum_2_string (_direction));
562 node->add_property ("default-type", _default_type.to_string());
564 if (!_pretty_name_prefix.empty ()) {
565 node->add_property("pretty-name", _pretty_name_prefix);
568 for (std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) {
569 XMLNode* n = new XMLNode ("Bundle");
570 n->add_property ("name", (*i)->bundle->name ());
571 node->add_child_nocopy (*n);
574 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
576 vector<string> connections;
578 XMLNode* pnode = new XMLNode (X_("Port"));
579 pnode->add_property (X_("type"), i->type().to_string());
580 pnode->add_property (X_("name"), i->name());
582 if (i->get_connections (connections)) {
583 vector<string>::const_iterator ci;
584 std::sort (connections.begin(), connections.end());
586 for (n = 0, ci = connections.begin(); ci != connections.end(); ++ci, ++n) {
588 /* if its a connection to our own port,
589 return only the port name, not the
590 whole thing. this allows connections
591 to be re-established even when our
592 client name is different.
595 XMLNode* cnode = new XMLNode (X_("Connection"));
597 cnode->add_property (X_("other"), _session.engine().make_port_name_relative (*ci));
598 pnode->add_child_nocopy (*cnode);
602 node->add_child_nocopy (*pnode);
605 snprintf (buf, sizeof (buf), "%" PRId64, _user_latency);
606 node->add_property (X_("user-latency"), buf);
612 IO::set_state (const XMLNode& node, int version)
614 /* callers for version < 3000 need to call set_state_2X directly, as A3 IOs
615 * are input OR output, not both, so the direction needs to be specified
618 assert (version >= 3000);
620 XMLProperty const * prop;
621 XMLNodeConstIterator iter;
624 /* force use of non-localized representation of decimal point,
625 since we use it a lot in XML files and so forth.
628 if (node.name() != state_node_name) {
629 error << string_compose(_("incorrect XML node \"%1\" passed to IO object"), node.name()) << endmsg;
633 bool ignore_name = node.property ("ignore-name");
634 if ((prop = node.property ("name")) != 0 && !ignore_name) {
635 set_name (prop->value());
638 if ((prop = node.property (X_("default-type"))) != 0) {
639 _default_type = DataType(prop->value());
640 assert(_default_type != DataType::NIL);
645 if ((prop = node.property ("direction")) != 0) {
646 _direction = (Direction) string_2_enum (prop->value(), _direction);
649 if (create_ports (node, version)) {
653 // after create_ports, updates names
654 if ((prop = node.property ("pretty-name")) != 0) {
655 set_pretty_name (prop->value());
658 if (connecting_legal) {
660 if (make_connections (node, version, false)) {
666 delete pending_state_node;
667 pending_state_node = new XMLNode (node);
668 pending_state_node_version = version;
669 pending_state_node_in = false;
670 ConnectingLegal.connect_same_thread (connection_legal_c, boost::bind (&IO::connecting_became_legal, this));
673 if ((prop = node.property ("user-latency")) != 0) {
674 _user_latency = atoi (prop->value ());
681 IO::set_state_2X (const XMLNode& node, int version, bool in)
683 XMLProperty const * prop;
684 XMLNodeConstIterator iter;
687 /* force use of non-localized representation of decimal point,
688 since we use it a lot in XML files and so forth.
691 if (node.name() != state_node_name) {
692 error << string_compose(_("incorrect XML node \"%1\" passed to IO object"), node.name()) << endmsg;
696 if ((prop = node.property ("name")) != 0) {
697 set_name (prop->value());
700 if ((prop = node.property (X_("default-type"))) != 0) {
701 _default_type = DataType(prop->value());
702 assert(_default_type != DataType::NIL);
707 _direction = in ? Input : Output;
709 if (create_ports (node, version)) {
713 if (connecting_legal) {
715 if (make_connections_2X (node, version, in)) {
721 delete pending_state_node;
722 pending_state_node = new XMLNode (node);
723 pending_state_node_version = version;
724 pending_state_node_in = in;
725 ConnectingLegal.connect_same_thread (connection_legal_c, boost::bind (&IO::connecting_became_legal, this));
732 IO::connecting_became_legal ()
736 assert (pending_state_node);
738 connection_legal_c.disconnect ();
740 // it's not required for TracksLive, as long as TracksLive's session does all the connections when it's being loaded
741 if (!Profile->get_trx() ) {
742 ret = make_connections (*pending_state_node, pending_state_node_version, pending_state_node_in);
745 delete pending_state_node;
746 pending_state_node = 0;
751 boost::shared_ptr<Bundle>
752 IO::find_possible_bundle (const string &desired_name)
754 static const string digits = "0123456789";
755 const string &default_name = (_direction == Input ? _("in") : _("out"));
756 const string &bundle_type_name = (_direction == Input ? _("input") : _("output"));
758 boost::shared_ptr<Bundle> c = _session.bundle_by_name (desired_name);
761 int bundle_number, mask;
762 string possible_name;
764 string::size_type last_non_digit_pos;
766 error << string_compose(_("Unknown bundle \"%1\" listed for %2 of %3"), desired_name, bundle_type_name, _name)
769 // find numeric suffix of desired name
772 last_non_digit_pos = desired_name.find_last_not_of(digits);
774 if (last_non_digit_pos != string::npos) {
776 s << desired_name.substr(last_non_digit_pos);
780 // see if it's a stereo connection e.g. "in 3+4"
782 if (last_non_digit_pos > 1 && desired_name[last_non_digit_pos] == '+') {
783 string::size_type left_last_non_digit_pos;
785 left_last_non_digit_pos = desired_name.find_last_not_of(digits, last_non_digit_pos-1);
787 if (left_last_non_digit_pos != string::npos) {
788 int left_bundle_number = 0;
790 s << desired_name.substr(left_last_non_digit_pos, last_non_digit_pos-1);
791 s >> left_bundle_number;
793 if (left_bundle_number > 0 && left_bundle_number + 1 == bundle_number) {
804 // find highest set bit
806 while ((mask <= bundle_number) && (mask <<= 1)) {}
808 // "wrap" bundle number into largest possible power of 2
813 if (bundle_number & mask) {
814 bundle_number &= ~mask;
817 s << default_name << " " << bundle_number + 1;
820 s << "+" << bundle_number + 2;
823 possible_name = s.str();
825 if ((c = _session.bundle_by_name (possible_name)) != 0) {
832 info << string_compose (_("Bundle %1 was not available - \"%2\" used instead"), desired_name, possible_name)
835 error << string_compose(_("No %1 bundles available as a replacement"), bundle_type_name)
846 IO::get_port_counts_2X (XMLNode const & node, int /*version*/, ChanCount& n, boost::shared_ptr<Bundle>& /*c*/)
848 XMLProperty const * prop;
849 XMLNodeList children = node.children ();
851 uint32_t n_audio = 0;
853 for (XMLNodeIterator i = children.begin(); i != children.end(); ++i) {
855 if ((prop = node.property ("inputs")) != 0 && _direction == Input) {
856 n_audio = count (prop->value().begin(), prop->value().end(), '{');
857 } else if ((prop = node.property ("input-connection")) != 0 && _direction == Input) {
859 } else if ((prop = node.property ("outputs")) != 0 && _direction == Output) {
860 n_audio = count (prop->value().begin(), prop->value().end(), '{');
861 } else if ((prop = node.property ("output-connection")) != 0 && _direction == Output) {
867 cnt.set_audio (n_audio);
868 n = ChanCount::max (n, cnt);
874 IO::get_port_counts (const XMLNode& node, int version, ChanCount& n, boost::shared_ptr<Bundle>& c)
876 if (version < 3000) {
877 return get_port_counts_2X (node, version, n, c);
880 XMLProperty const * prop;
881 XMLNodeConstIterator iter;
882 uint32_t n_audio = 0;
888 if ((prop = node.property ("connection")) != 0) {
890 if ((c = find_possible_bundle (prop->value())) != 0) {
891 n = ChanCount::max (n, c->nchannels());
896 for (iter = node.children().begin(); iter != node.children().end(); ++iter) {
898 if ((*iter)->name() == X_("Bundle")) {
899 prop = (*iter)->property ("name");
900 if ((c = find_possible_bundle (prop->value())) != 0) {
901 n = ChanCount::max (n, c->nchannels());
908 if ((*iter)->name() == X_("Port")) {
909 prop = (*iter)->property (X_("type"));
915 if (prop->value() == X_("audio")) {
916 cnt.set_audio (++n_audio);
917 } else if (prop->value() == X_("midi")) {
918 cnt.set_midi (++n_midi);
923 n = ChanCount::max (n, cnt);
928 IO::create_ports (const XMLNode& node, int version)
931 boost::shared_ptr<Bundle> c;
933 get_port_counts (node, version, n, c);
936 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
938 if (ensure_ports (n, true, this)) {
939 error << string_compose(_("%1: cannot create I/O ports"), _name) << endmsg;
950 IO::make_connections (const XMLNode& node, int version, bool in)
952 if (version < 3000) {
953 return make_connections_2X (node, version, in);
956 XMLProperty const * prop;
958 for (XMLNodeConstIterator i = node.children().begin(); i != node.children().end(); ++i) {
960 if ((*i)->name() == "Bundle") {
961 XMLProperty const * prop = (*i)->property ("name");
963 boost::shared_ptr<Bundle> b = find_possible_bundle (prop->value());
965 connect_ports_to_bundle (b, true, this);
972 if ((*i)->name() == "Port") {
974 prop = (*i)->property (X_("name"));
980 boost::shared_ptr<Port> p = port_by_name (prop->value());
983 for (XMLNodeConstIterator c = (*i)->children().begin(); c != (*i)->children().end(); ++c) {
985 XMLNode* cnode = (*c);
987 if (cnode->name() != X_("Connection")) {
991 if ((prop = cnode->property (X_("other"))) == 0) {
996 connect (p, prop->value(), this);
1007 IO::prepare_for_reset (XMLNode& node, const std::string& name)
1010 node.add_property ("name", name);
1012 /* now find connections and reset the name of the port
1013 in one so that when we re-use it it will match
1014 the name of the thing we're applying it to.
1018 XMLNodeList children = node.children();
1020 for (XMLNodeIterator i = children.begin(); i != children.end(); ++i) {
1022 if ((*i)->name() == "Port") {
1024 prop = (*i)->property (X_("name"));
1028 string old = prop->value();
1029 string::size_type slash = old.find ('/');
1031 if (slash != string::npos) {
1032 /* port name is of form: <IO-name>/<port-name> */
1035 new_name += old.substr (old.find ('/'));
1037 prop->set_value (new_name);
1046 IO::make_connections_2X (const XMLNode& node, int /*version*/, bool in)
1048 XMLProperty const * prop;
1050 /* XXX: bundles ("connections" as was) */
1052 if ((prop = node.property ("inputs")) != 0 && in) {
1054 string::size_type ostart = 0;
1055 string::size_type start = 0;
1056 string::size_type end = 0;
1059 vector<string> ports;
1061 string const str = prop->value ();
1063 while ((start = str.find_first_of ('{', ostart)) != string::npos) {
1066 if ((end = str.find_first_of ('}', start)) == string::npos) {
1067 error << string_compose(_("IO: badly formed string in XML node for inputs \"%1\""), str) << endmsg;
1071 if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
1072 error << string_compose(_("bad input string in XML node \"%1\""), str) << endmsg;
1079 for (int x = 0; x < n; ++x) {
1080 /* XXX: this is a bit of a hack; need to check if it's always valid */
1081 string::size_type const p = ports[x].find ("/out");
1082 if (p != string::npos) {
1083 ports[x].replace (p, 4, "/audio_out");
1085 if (NULL != nth(i).get())
1086 nth(i)->connect (ports[x]);
1096 if ((prop = node.property ("outputs")) != 0 && !in) {
1098 string::size_type ostart = 0;
1099 string::size_type start = 0;
1100 string::size_type end = 0;
1103 vector<string> ports;
1105 string const str = prop->value ();
1107 while ((start = str.find_first_of ('{', ostart)) != string::npos) {
1110 if ((end = str.find_first_of ('}', start)) == string::npos) {
1111 error << string_compose(_("IO: badly formed string in XML node for outputs \"%1\""), str) << endmsg;
1115 if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
1116 error << string_compose(_("IO: bad output string in XML node \"%1\""), str) << endmsg;
1122 for (int x = 0; x < n; ++x) {
1123 /* XXX: this is a bit of a hack; need to check if it's always valid */
1124 string::size_type const p = ports[x].find ("/in");
1125 if (p != string::npos) {
1126 ports[x].replace (p, 3, "/audio_in");
1128 if (NULL != nth(i).get())
1129 nth(i)->connect (ports[x]);
1142 IO::set_ports (const string& str)
1144 vector<string> ports;
1148 if ((nports = count (str.begin(), str.end(), '{')) == 0) {
1153 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
1155 // FIXME: audio-only
1156 if (ensure_ports (ChanCount(DataType::AUDIO, nports), true, this)) {
1161 string::size_type start = 0;
1162 string::size_type end = 0;
1163 string::size_type ostart = 0;
1164 for (int i = 0; (start = str.find_first_of ('{', ostart)) != string::npos; ++i) {
1167 if ((end = str.find_first_of ('}', start)) == string::npos) {
1168 error << string_compose(_("IO: badly formed string in XML node for inputs \"%1\""), str) << endmsg;
1172 if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
1173 error << string_compose(_("bad input string in XML node \"%1\""), str) << endmsg;
1179 for (int x = 0; x < n; ++x) {
1180 connect (nth (i), ports[x], this);
1191 IO::parse_io_string (const string& str, vector<string>& ports)
1193 string::size_type pos, opos;
1195 if (str.length() == 0) {
1203 while ((pos = str.find_first_of (',', opos)) != string::npos) {
1204 ports.push_back (str.substr (opos, pos - opos));
1208 if (opos < str.length()) {
1209 ports.push_back (str.substr(opos));
1212 return ports.size();
1216 IO::parse_gain_string (const string& str, vector<string>& ports)
1218 string::size_type pos, opos;
1223 while ((pos = str.find_first_of (',', opos)) != string::npos) {
1224 ports.push_back (str.substr (opos, pos - opos));
1228 if (opos < str.length()) {
1229 ports.push_back (str.substr(opos));
1232 return ports.size();
1236 IO::set_name (const string& requested_name)
1238 string name = requested_name;
1240 if (_name == name) {
1244 /* replace all colons in the name. i wish we didn't have to do this */
1246 replace_all (name, ":", "-");
1248 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
1249 string current_name = i->name();
1250 current_name.replace (current_name.find (_name), _name.val().length(), name);
1251 i->set_name (current_name);
1254 bool const r = SessionObject::set_name (name);
1262 IO::set_pretty_name (const std::string& str)
1264 if (_pretty_name_prefix == str) {
1267 _pretty_name_prefix = str;
1268 apply_pretty_name ();
1272 IO::apply_pretty_name ()
1275 if (_pretty_name_prefix.empty ()) {
1278 for (PortSet::iterator i = _ports.begin (); i != _ports.end(); ++i, ++pn) {
1279 (*i)->set_pretty_name (string_compose (("%1/%2 %3"),
1280 _pretty_name_prefix,
1281 _direction == Output ? _("Out") : _("In"),
1287 IO::latency () const
1289 framecnt_t max_latency;
1294 /* io lock not taken - must be protected by other means */
1296 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1297 if ((latency = i->private_latency_range (_direction == Output).max) > max_latency) {
1298 DEBUG_TRACE (DEBUG::Latency, string_compose ("port %1 has %2 latency of %3 - use\n",
1300 ((_direction == Output) ? "PLAYBACK" : "CAPTURE"),
1302 max_latency = latency;
1306 DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: max %4 latency from %2 ports = %3\n",
1307 name(), _ports.num_ports(), max_latency,
1308 ((_direction == Output) ? "PLAYBACK" : "CAPTURE")));
1313 IO::connect_ports_to_bundle (boost::shared_ptr<Bundle> c, bool exclusive, void* src)
1315 BLOCK_PROCESS_CALLBACK ();
1318 Glib::Threads::Mutex::Lock lm2 (io_lock);
1321 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
1322 i->disconnect_all ();
1326 c->connect (_bundle, _session.engine());
1328 /* If this is a UserBundle, make a note of what we've done */
1330 boost::shared_ptr<UserBundle> ub = boost::dynamic_pointer_cast<UserBundle> (c);
1333 /* See if we already know about this one */
1334 std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin();
1335 while (i != _bundles_connected.end() && (*i)->bundle != ub) {
1339 if (i == _bundles_connected.end()) {
1340 /* We don't, so make a note */
1341 _bundles_connected.push_back (new UserBundleInfo (this, ub));
1346 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
1351 IO::disconnect_ports_from_bundle (boost::shared_ptr<Bundle> c, void* src)
1353 BLOCK_PROCESS_CALLBACK ();
1356 Glib::Threads::Mutex::Lock lm2 (io_lock);
1358 c->disconnect (_bundle, _session.engine());
1360 /* If this is a UserBundle, make a note of what we've done */
1362 boost::shared_ptr<UserBundle> ub = boost::dynamic_pointer_cast<UserBundle> (c);
1365 std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin();
1366 while (i != _bundles_connected.end() && (*i)->bundle != ub) {
1370 if (i != _bundles_connected.end()) {
1372 _bundles_connected.erase (i);
1377 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
1383 IO::disable_connecting ()
1385 connecting_legal = false;
1390 IO::enable_connecting ()
1392 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock());
1393 connecting_legal = true;
1394 boost::optional<int> r = ConnectingLegal ();
1395 return r.get_value_or (0);
1399 IO::bundle_changed (Bundle::Change /*c*/)
1402 // connect_input_ports_to_bundle (_input_bundle, this);
1407 IO::build_legal_port_name (DataType type)
1409 const int name_size = AudioEngine::instance()->port_name_size();
1413 if (type == DataType::AUDIO) {
1414 suffix = X_("audio");
1415 } else if (type == DataType::MIDI) {
1416 suffix = X_("midi");
1418 throw unknown_type();
1421 /* note that if "in" or "out" are translated it will break a session
1422 across locale switches because a port's connection list will
1423 show (old) translated names, but the current port name will
1424 use the (new) translated name.
1428 if (_direction == Input) {
1429 suffix += X_("_return");
1431 suffix += X_("_send");
1434 if (_direction == Input) {
1435 suffix += X_("_in");
1437 suffix += X_("_out");
1441 // allow up to 4 digits for the output port number, plus the slash, suffix and extra space
1443 limit = name_size - AudioEngine::instance()->my_name().length() - (suffix.length() + 5);
1445 std::vector<char> buf1(name_size+1);
1446 std::vector<char> buf2(name_size+1);
1448 /* colons are illegal in port names, so fix that */
1450 string nom = _name.val();
1451 replace_all (nom, ":", ";");
1453 snprintf (&buf1[0], name_size+1, ("%.*s/%s"), limit, nom.c_str(), suffix.c_str());
1455 int port_number = find_port_hole (&buf1[0]);
1456 snprintf (&buf2[0], name_size+1, "%s %d", &buf1[0], port_number);
1458 return string (&buf2[0]);
1462 IO::find_port_hole (const char* base)
1464 /* CALLER MUST HOLD IO LOCK */
1468 if (_ports.empty()) {
1472 /* we only allow up to 4 characters for the port number
1475 for (n = 1; n < 9999; ++n) {
1476 std::vector<char> buf (AudioEngine::instance()->port_name_size());
1477 PortSet::iterator i = _ports.begin();
1479 snprintf (&buf[0], buf.size()+1, _("%s %u"), base, n);
1481 for ( ; i != _ports.end(); ++i) {
1482 if (string(i->name()) == string(&buf[0])) {
1487 if (i == _ports.end()) {
1495 boost::shared_ptr<AudioPort>
1496 IO::audio(uint32_t n) const
1498 return _ports.nth_audio_port (n);
1502 boost::shared_ptr<MidiPort>
1503 IO::midi(uint32_t n) const
1505 return _ports.nth_midi_port (n);
1509 * Setup a bundle that describe our inputs or outputs. Also creates the bundle if necessary.
1518 _bundle.reset (new Bundle (_direction == Input));
1521 _bundle->suspend_signals ();
1523 _bundle->remove_channels ();
1525 if (_direction == Input) {
1526 snprintf(buf, sizeof (buf), _("%s in"), _name.val().c_str());
1528 snprintf(buf, sizeof (buf), _("%s out"), _name.val().c_str());
1530 _bundle->set_name (buf);
1533 for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
1535 uint32_t const N = _ports.count().get (*i);
1536 for (uint32_t j = 0; j < N; ++j) {
1537 _bundle->add_channel (bundle_channel_name (j, N, *i), *i);
1538 _bundle->set_port (c, _session.engine().make_port_name_non_relative (_ports.port(*i, j)->name()));
1544 _bundle->resume_signals ();
1547 /** @return Bundles connected to our ports */
1549 IO::bundles_connected ()
1554 for (std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) {
1555 bundles.push_back ((*i)->bundle);
1558 /* Session bundles */
1559 boost::shared_ptr<ARDOUR::BundleList> b = _session.bundles ();
1560 for (ARDOUR::BundleList::iterator i = b->begin(); i != b->end(); ++i) {
1561 if ((*i)->connected_to (_bundle, _session.engine())) {
1562 bundles.push_back (*i);
1568 boost::shared_ptr<ARDOUR::RouteList> r = _session.get_routes ();
1570 if (_direction == Input) {
1571 for (ARDOUR::RouteList::iterator i = r->begin(); i != r->end(); ++i) {
1572 if ((*i)->output()->bundle()->connected_to (_bundle, _session.engine())) {
1573 bundles.push_back ((*i)->output()->bundle());
1577 for (ARDOUR::RouteList::iterator i = r->begin(); i != r->end(); ++i) {
1578 if ((*i)->input()->bundle()->connected_to (_bundle, _session.engine())) {
1579 bundles.push_back ((*i)->input()->bundle());
1588 IO::UserBundleInfo::UserBundleInfo (IO* io, boost::shared_ptr<UserBundle> b)
1591 b->Changed.connect_same_thread (changed, boost::bind (&IO::bundle_changed, io, _1));
1595 IO::bundle_channel_name (uint32_t c, uint32_t n, DataType t) const
1599 if (t == DataType::AUDIO) {
1605 return c == 0 ? _("L") : _("R");
1607 snprintf (buf, sizeof(buf), "%d", (c + 1));
1613 snprintf (buf, sizeof(buf), "%d", (c + 1));
1622 IO::name_from_state (const XMLNode& node)
1624 XMLProperty const * prop;
1626 if ((prop = node.property ("name")) != 0) {
1627 return prop->value();
1634 IO::set_name_in_state (XMLNode& node, const string& new_name)
1636 node.add_property (X_("name"), new_name);
1637 XMLNodeList children = node.children ();
1638 for (XMLNodeIterator i = children.begin(); i != children.end(); ++i) {
1639 if ((*i)->name() == X_("Port")) {
1640 string const old_name = (*i)->property(X_("name"))->value();
1641 string const old_name_second_part = old_name.substr (old_name.find_first_of ("/") + 1);
1642 (*i)->add_property (X_("name"), string_compose ("%1/%2", new_name, old_name_second_part));
1648 IO::connected () const
1650 /* do we have any connections at all? */
1652 for (PortSet::const_iterator p = _ports.begin(); p != _ports.end(); ++p) {
1653 if (p->connected()) {
1662 IO::connected_to (boost::shared_ptr<const IO> other) const
1665 return connected ();
1668 assert (_direction != other->direction());
1671 uint32_t no = n_ports().n_total();
1672 uint32_t ni = other->n_ports ().n_total();
1674 for (i = 0; i < no; ++i) {
1675 for (j = 0; j < ni; ++j) {
1676 if ((NULL != nth(i).get()) && (NULL != other->nth(j).get())) {
1677 if (nth(i)->connected_to (other->nth(j)->name())) {
1688 IO::connected_to (const string& str) const
1690 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1691 if (i->connected_to (str)) {
1699 /** Call a processor's ::run() method, giving it our buffers
1700 * Caller must hold process lock.
1703 IO::process_input (boost::shared_ptr<Processor> proc, framepos_t start_frame, framepos_t end_frame, double speed, pframes_t nframes)
1705 /* don't read the data into new buffers - just use the port buffers directly */
1707 if (n_ports().n_total() == 0) {
1708 /* We have no ports, so nothing to process */
1712 _buffers.get_backend_port_addresses (_ports, nframes);
1714 proc->run (_buffers, start_frame, end_frame, speed, nframes, true);
1719 IO::collect_input (BufferSet& bufs, pframes_t nframes, ChanCount offset)
1721 assert(bufs.available() >= _ports.count());
1723 if (_ports.count() == ChanCount::ZERO) {
1727 bufs.set_count (_ports.count());
1729 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1730 PortSet::iterator i = _ports.begin(*t);
1731 BufferSet::iterator b = bufs.begin(*t);
1733 for (uint32_t off = 0; off < offset.get(*t); ++off, ++b) {
1734 if (b == bufs.end(*t)) {
1739 for ( ; i != _ports.end(*t); ++i, ++b) {
1740 const Buffer& bb (i->get_buffer (nframes));
1741 b->read_from (bb, nframes);
1747 IO::copy_to_outputs (BufferSet& bufs, DataType type, pframes_t nframes, framecnt_t offset)
1749 PortSet::iterator o = _ports.begin(type);
1750 BufferSet::iterator i = bufs.begin(type);
1751 BufferSet::iterator prev = i;
1753 assert(i != bufs.end(type)); // or second loop will crash
1755 // Copy any buffers 1:1 to outputs
1757 while (i != bufs.end(type) && o != _ports.end (type)) {
1758 Buffer& port_buffer (o->get_buffer (nframes));
1759 port_buffer.read_from (*i, nframes, offset);
1765 // Copy last buffer to any extra outputs
1767 while (o != _ports.end(type)) {
1768 Buffer& port_buffer (o->get_buffer (nframes));
1769 port_buffer.read_from (*prev, nframes, offset);
1774 boost::shared_ptr<Port>
1775 IO::port_by_name (const std::string& str) const
1777 /* to be called only from ::set_state() - no locking */
1779 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1781 if (i->name() == str) {
1782 return boost::const_pointer_cast<Port> (*i);
1786 return boost::shared_ptr<Port> ();
1790 IO::physically_connected () const
1792 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1793 if (i->physically_connected()) {
1802 IO::has_port (boost::shared_ptr<Port> p) const
1804 Glib::Threads::Mutex::Lock lm (io_lock);
1805 return _ports.contains (p);