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 id().print (buf, sizeof (buf));
561 node->add_property("id", buf);
562 node->add_property ("direction", enum_2_string (_direction));
563 node->add_property ("default-type", _default_type.to_string());
565 if (!_pretty_name_prefix.empty ()) {
566 node->add_property("pretty-name", _pretty_name_prefix);
569 for (std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) {
570 XMLNode* n = new XMLNode ("Bundle");
571 n->add_property ("name", (*i)->bundle->name ());
572 node->add_child_nocopy (*n);
575 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
577 vector<string> connections;
579 XMLNode* pnode = new XMLNode (X_("Port"));
580 pnode->add_property (X_("type"), i->type().to_string());
581 pnode->add_property (X_("name"), i->name());
583 if (i->get_connections (connections)) {
584 vector<string>::const_iterator ci;
585 std::sort (connections.begin(), connections.end());
587 for (n = 0, ci = connections.begin(); ci != connections.end(); ++ci, ++n) {
589 /* if its a connection to our own port,
590 return only the port name, not the
591 whole thing. this allows connections
592 to be re-established even when our
593 client name is different.
596 XMLNode* cnode = new XMLNode (X_("Connection"));
598 cnode->add_property (X_("other"), _session.engine().make_port_name_relative (*ci));
599 pnode->add_child_nocopy (*cnode);
603 node->add_child_nocopy (*pnode);
606 snprintf (buf, sizeof (buf), "%" PRId64, _user_latency);
607 node->add_property (X_("user-latency"), buf);
613 IO::set_state (const XMLNode& node, int version)
615 /* callers for version < 3000 need to call set_state_2X directly, as A3 IOs
616 * are input OR output, not both, so the direction needs to be specified
619 assert (version >= 3000);
621 XMLProperty const * prop;
622 XMLNodeConstIterator iter;
625 /* force use of non-localized representation of decimal point,
626 since we use it a lot in XML files and so forth.
629 if (node.name() != state_node_name) {
630 error << string_compose(_("incorrect XML node \"%1\" passed to IO object"), node.name()) << endmsg;
634 bool ignore_name = node.property ("ignore-name");
635 if ((prop = node.property ("name")) != 0 && !ignore_name) {
636 set_name (prop->value());
639 if ((prop = node.property (X_("default-type"))) != 0) {
640 _default_type = DataType(prop->value());
641 assert(_default_type != DataType::NIL);
646 if ((prop = node.property ("direction")) != 0) {
647 _direction = (Direction) string_2_enum (prop->value(), _direction);
650 if (create_ports (node, version)) {
654 // after create_ports, updates names
655 if ((prop = node.property ("pretty-name")) != 0) {
656 set_pretty_name (prop->value());
659 if (connecting_legal) {
661 if (make_connections (node, version, false)) {
667 delete pending_state_node;
668 pending_state_node = new XMLNode (node);
669 pending_state_node_version = version;
670 pending_state_node_in = false;
671 ConnectingLegal.connect_same_thread (connection_legal_c, boost::bind (&IO::connecting_became_legal, this));
674 if ((prop = node.property ("user-latency")) != 0) {
675 _user_latency = atoi (prop->value ());
682 IO::set_state_2X (const XMLNode& node, int version, bool in)
684 XMLProperty const * prop;
685 XMLNodeConstIterator iter;
688 /* force use of non-localized representation of decimal point,
689 since we use it a lot in XML files and so forth.
692 if (node.name() != state_node_name) {
693 error << string_compose(_("incorrect XML node \"%1\" passed to IO object"), node.name()) << endmsg;
697 if ((prop = node.property ("name")) != 0) {
698 set_name (prop->value());
701 if ((prop = node.property (X_("default-type"))) != 0) {
702 _default_type = DataType(prop->value());
703 assert(_default_type != DataType::NIL);
708 _direction = in ? Input : Output;
710 if (create_ports (node, version)) {
714 if (connecting_legal) {
716 if (make_connections_2X (node, version, in)) {
722 delete pending_state_node;
723 pending_state_node = new XMLNode (node);
724 pending_state_node_version = version;
725 pending_state_node_in = in;
726 ConnectingLegal.connect_same_thread (connection_legal_c, boost::bind (&IO::connecting_became_legal, this));
733 IO::connecting_became_legal ()
737 assert (pending_state_node);
739 connection_legal_c.disconnect ();
741 // it's not required for TracksLive, as long as TracksLive's session does all the connections when it's being loaded
742 if (!Profile->get_trx() ) {
743 ret = make_connections (*pending_state_node, pending_state_node_version, pending_state_node_in);
746 delete pending_state_node;
747 pending_state_node = 0;
752 boost::shared_ptr<Bundle>
753 IO::find_possible_bundle (const string &desired_name)
755 static const string digits = "0123456789";
756 const string &default_name = (_direction == Input ? _("in") : _("out"));
757 const string &bundle_type_name = (_direction == Input ? _("input") : _("output"));
759 boost::shared_ptr<Bundle> c = _session.bundle_by_name (desired_name);
762 int bundle_number, mask;
763 string possible_name;
765 string::size_type last_non_digit_pos;
767 error << string_compose(_("Unknown bundle \"%1\" listed for %2 of %3"), desired_name, bundle_type_name, _name)
770 // find numeric suffix of desired name
773 last_non_digit_pos = desired_name.find_last_not_of(digits);
775 if (last_non_digit_pos != string::npos) {
777 s << desired_name.substr(last_non_digit_pos);
781 // see if it's a stereo connection e.g. "in 3+4"
783 if (last_non_digit_pos > 1 && desired_name[last_non_digit_pos] == '+') {
784 string::size_type left_last_non_digit_pos;
786 left_last_non_digit_pos = desired_name.find_last_not_of(digits, last_non_digit_pos-1);
788 if (left_last_non_digit_pos != string::npos) {
789 int left_bundle_number = 0;
791 s << desired_name.substr(left_last_non_digit_pos, last_non_digit_pos-1);
792 s >> left_bundle_number;
794 if (left_bundle_number > 0 && left_bundle_number + 1 == bundle_number) {
805 // find highest set bit
807 while ((mask <= bundle_number) && (mask <<= 1)) {}
809 // "wrap" bundle number into largest possible power of 2
814 if (bundle_number & mask) {
815 bundle_number &= ~mask;
818 s << default_name << " " << bundle_number + 1;
821 s << "+" << bundle_number + 2;
824 possible_name = s.str();
826 if ((c = _session.bundle_by_name (possible_name)) != 0) {
833 info << string_compose (_("Bundle %1 was not available - \"%2\" used instead"), desired_name, possible_name)
836 error << string_compose(_("No %1 bundles available as a replacement"), bundle_type_name)
847 IO::get_port_counts_2X (XMLNode const & node, int /*version*/, ChanCount& n, boost::shared_ptr<Bundle>& /*c*/)
849 XMLProperty const * prop;
850 XMLNodeList children = node.children ();
852 uint32_t n_audio = 0;
854 for (XMLNodeIterator i = children.begin(); i != children.end(); ++i) {
856 if ((prop = node.property ("inputs")) != 0 && _direction == Input) {
857 n_audio = count (prop->value().begin(), prop->value().end(), '{');
858 } else if ((prop = node.property ("input-connection")) != 0 && _direction == Input) {
860 } else if ((prop = node.property ("outputs")) != 0 && _direction == Output) {
861 n_audio = count (prop->value().begin(), prop->value().end(), '{');
862 } else if ((prop = node.property ("output-connection")) != 0 && _direction == Output) {
868 cnt.set_audio (n_audio);
869 n = ChanCount::max (n, cnt);
875 IO::get_port_counts (const XMLNode& node, int version, ChanCount& n, boost::shared_ptr<Bundle>& c)
877 if (version < 3000) {
878 return get_port_counts_2X (node, version, n, c);
881 XMLProperty const * prop;
882 XMLNodeConstIterator iter;
883 uint32_t n_audio = 0;
889 if ((prop = node.property ("connection")) != 0) {
891 if ((c = find_possible_bundle (prop->value())) != 0) {
892 n = ChanCount::max (n, c->nchannels());
897 for (iter = node.children().begin(); iter != node.children().end(); ++iter) {
899 if ((*iter)->name() == X_("Bundle")) {
900 prop = (*iter)->property ("name");
901 if ((c = find_possible_bundle (prop->value())) != 0) {
902 n = ChanCount::max (n, c->nchannels());
909 if ((*iter)->name() == X_("Port")) {
910 prop = (*iter)->property (X_("type"));
916 if (prop->value() == X_("audio")) {
917 cnt.set_audio (++n_audio);
918 } else if (prop->value() == X_("midi")) {
919 cnt.set_midi (++n_midi);
924 n = ChanCount::max (n, cnt);
929 IO::create_ports (const XMLNode& node, int version)
932 boost::shared_ptr<Bundle> c;
934 get_port_counts (node, version, n, c);
937 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
939 if (ensure_ports (n, true, this)) {
940 error << string_compose(_("%1: cannot create I/O ports"), _name) << endmsg;
951 IO::make_connections (const XMLNode& node, int version, bool in)
953 if (version < 3000) {
954 return make_connections_2X (node, version, in);
957 XMLProperty const * prop;
959 for (XMLNodeConstIterator i = node.children().begin(); i != node.children().end(); ++i) {
961 if ((*i)->name() == "Bundle") {
962 XMLProperty const * prop = (*i)->property ("name");
964 boost::shared_ptr<Bundle> b = find_possible_bundle (prop->value());
966 connect_ports_to_bundle (b, true, this);
973 if ((*i)->name() == "Port") {
975 prop = (*i)->property (X_("name"));
981 boost::shared_ptr<Port> p = port_by_name (prop->value());
984 for (XMLNodeConstIterator c = (*i)->children().begin(); c != (*i)->children().end(); ++c) {
986 XMLNode* cnode = (*c);
988 if (cnode->name() != X_("Connection")) {
992 if ((prop = cnode->property (X_("other"))) == 0) {
997 connect (p, prop->value(), this);
1008 IO::prepare_for_reset (XMLNode& node, const std::string& name)
1011 node.add_property ("name", name);
1013 /* now find connections and reset the name of the port
1014 in one so that when we re-use it it will match
1015 the name of the thing we're applying it to.
1019 XMLNodeList children = node.children();
1021 for (XMLNodeIterator i = children.begin(); i != children.end(); ++i) {
1023 if ((*i)->name() == "Port") {
1025 prop = (*i)->property (X_("name"));
1029 string old = prop->value();
1030 string::size_type slash = old.find ('/');
1032 if (slash != string::npos) {
1033 /* port name is of form: <IO-name>/<port-name> */
1036 new_name += old.substr (old.find ('/'));
1038 prop->set_value (new_name);
1047 IO::make_connections_2X (const XMLNode& node, int /*version*/, bool in)
1049 XMLProperty const * prop;
1051 /* XXX: bundles ("connections" as was) */
1053 if ((prop = node.property ("inputs")) != 0 && in) {
1055 string::size_type ostart = 0;
1056 string::size_type start = 0;
1057 string::size_type end = 0;
1060 vector<string> ports;
1062 string const str = prop->value ();
1064 while ((start = str.find_first_of ('{', ostart)) != string::npos) {
1067 if ((end = str.find_first_of ('}', start)) == string::npos) {
1068 error << string_compose(_("IO: badly formed string in XML node for inputs \"%1\""), str) << endmsg;
1072 if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
1073 error << string_compose(_("bad input string in XML node \"%1\""), str) << endmsg;
1080 for (int x = 0; x < n; ++x) {
1081 /* XXX: this is a bit of a hack; need to check if it's always valid */
1082 string::size_type const p = ports[x].find ("/out");
1083 if (p != string::npos) {
1084 ports[x].replace (p, 4, "/audio_out");
1086 if (NULL != nth(i).get())
1087 nth(i)->connect (ports[x]);
1097 if ((prop = node.property ("outputs")) != 0 && !in) {
1099 string::size_type ostart = 0;
1100 string::size_type start = 0;
1101 string::size_type end = 0;
1104 vector<string> ports;
1106 string const str = prop->value ();
1108 while ((start = str.find_first_of ('{', ostart)) != string::npos) {
1111 if ((end = str.find_first_of ('}', start)) == string::npos) {
1112 error << string_compose(_("IO: badly formed string in XML node for outputs \"%1\""), str) << endmsg;
1116 if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
1117 error << string_compose(_("IO: bad output string in XML node \"%1\""), str) << endmsg;
1123 for (int x = 0; x < n; ++x) {
1124 /* XXX: this is a bit of a hack; need to check if it's always valid */
1125 string::size_type const p = ports[x].find ("/in");
1126 if (p != string::npos) {
1127 ports[x].replace (p, 3, "/audio_in");
1129 if (NULL != nth(i).get())
1130 nth(i)->connect (ports[x]);
1143 IO::set_ports (const string& str)
1145 vector<string> ports;
1149 if ((nports = count (str.begin(), str.end(), '{')) == 0) {
1154 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
1156 // FIXME: audio-only
1157 if (ensure_ports (ChanCount(DataType::AUDIO, nports), true, this)) {
1162 string::size_type start = 0;
1163 string::size_type end = 0;
1164 string::size_type ostart = 0;
1165 for (int i = 0; (start = str.find_first_of ('{', ostart)) != string::npos; ++i) {
1168 if ((end = str.find_first_of ('}', start)) == string::npos) {
1169 error << string_compose(_("IO: badly formed string in XML node for inputs \"%1\""), str) << endmsg;
1173 if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
1174 error << string_compose(_("bad input string in XML node \"%1\""), str) << endmsg;
1180 for (int x = 0; x < n; ++x) {
1181 connect (nth (i), ports[x], this);
1192 IO::parse_io_string (const string& str, vector<string>& ports)
1194 string::size_type pos, opos;
1196 if (str.length() == 0) {
1204 while ((pos = str.find_first_of (',', opos)) != string::npos) {
1205 ports.push_back (str.substr (opos, pos - opos));
1209 if (opos < str.length()) {
1210 ports.push_back (str.substr(opos));
1213 return ports.size();
1217 IO::parse_gain_string (const string& str, vector<string>& ports)
1219 string::size_type pos, opos;
1224 while ((pos = str.find_first_of (',', opos)) != string::npos) {
1225 ports.push_back (str.substr (opos, pos - opos));
1229 if (opos < str.length()) {
1230 ports.push_back (str.substr(opos));
1233 return ports.size();
1237 IO::set_name (const string& requested_name)
1239 string name = requested_name;
1241 if (_name == name) {
1245 /* replace all colons in the name. i wish we didn't have to do this */
1247 replace_all (name, ":", "-");
1249 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
1250 string current_name = i->name();
1251 current_name.replace (current_name.find (_name), _name.val().length(), name);
1252 i->set_name (current_name);
1255 bool const r = SessionObject::set_name (name);
1263 IO::set_pretty_name (const std::string& str)
1265 if (_pretty_name_prefix == str) {
1268 _pretty_name_prefix = str;
1269 apply_pretty_name ();
1273 IO::apply_pretty_name ()
1276 if (_pretty_name_prefix.empty ()) {
1279 for (PortSet::iterator i = _ports.begin (); i != _ports.end(); ++i, ++pn) {
1280 (*i)->set_pretty_name (string_compose (("%1/%2 %3"),
1281 _pretty_name_prefix,
1282 _direction == Output ? _("Out") : _("In"),
1288 IO::latency () const
1290 framecnt_t max_latency;
1295 /* io lock not taken - must be protected by other means */
1297 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1298 if ((latency = i->private_latency_range (_direction == Output).max) > max_latency) {
1299 DEBUG_TRACE (DEBUG::Latency, string_compose ("port %1 has %2 latency of %3 - use\n",
1301 ((_direction == Output) ? "PLAYBACK" : "CAPTURE"),
1303 max_latency = latency;
1307 DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: max %4 latency from %2 ports = %3\n",
1308 name(), _ports.num_ports(), max_latency,
1309 ((_direction == Output) ? "PLAYBACK" : "CAPTURE")));
1314 IO::connect_ports_to_bundle (boost::shared_ptr<Bundle> c, bool exclusive, void* src)
1316 BLOCK_PROCESS_CALLBACK ();
1319 Glib::Threads::Mutex::Lock lm2 (io_lock);
1322 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
1323 i->disconnect_all ();
1327 c->connect (_bundle, _session.engine());
1329 /* If this is a UserBundle, make a note of what we've done */
1331 boost::shared_ptr<UserBundle> ub = boost::dynamic_pointer_cast<UserBundle> (c);
1334 /* See if we already know about this one */
1335 std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin();
1336 while (i != _bundles_connected.end() && (*i)->bundle != ub) {
1340 if (i == _bundles_connected.end()) {
1341 /* We don't, so make a note */
1342 _bundles_connected.push_back (new UserBundleInfo (this, ub));
1347 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
1352 IO::disconnect_ports_from_bundle (boost::shared_ptr<Bundle> c, void* src)
1354 BLOCK_PROCESS_CALLBACK ();
1357 Glib::Threads::Mutex::Lock lm2 (io_lock);
1359 c->disconnect (_bundle, _session.engine());
1361 /* If this is a UserBundle, make a note of what we've done */
1363 boost::shared_ptr<UserBundle> ub = boost::dynamic_pointer_cast<UserBundle> (c);
1366 std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin();
1367 while (i != _bundles_connected.end() && (*i)->bundle != ub) {
1371 if (i != _bundles_connected.end()) {
1373 _bundles_connected.erase (i);
1378 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
1384 IO::disable_connecting ()
1386 connecting_legal = false;
1391 IO::enable_connecting ()
1393 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock());
1394 connecting_legal = true;
1395 boost::optional<int> r = ConnectingLegal ();
1396 return r.get_value_or (0);
1400 IO::bundle_changed (Bundle::Change /*c*/)
1403 // connect_input_ports_to_bundle (_input_bundle, this);
1408 IO::build_legal_port_name (DataType type)
1410 const int name_size = AudioEngine::instance()->port_name_size();
1414 if (type == DataType::AUDIO) {
1415 suffix = X_("audio");
1416 } else if (type == DataType::MIDI) {
1417 suffix = X_("midi");
1419 throw unknown_type();
1422 /* note that if "in" or "out" are translated it will break a session
1423 across locale switches because a port's connection list will
1424 show (old) translated names, but the current port name will
1425 use the (new) translated name.
1429 if (_direction == Input) {
1430 suffix += X_("_return");
1432 suffix += X_("_send");
1435 if (_direction == Input) {
1436 suffix += X_("_in");
1438 suffix += X_("_out");
1442 // allow up to 4 digits for the output port number, plus the slash, suffix and extra space
1444 limit = name_size - AudioEngine::instance()->my_name().length() - (suffix.length() + 5);
1446 std::vector<char> buf1(name_size+1);
1447 std::vector<char> buf2(name_size+1);
1449 /* colons are illegal in port names, so fix that */
1451 string nom = _name.val();
1452 replace_all (nom, ":", ";");
1454 snprintf (&buf1[0], name_size+1, ("%.*s/%s"), limit, nom.c_str(), suffix.c_str());
1456 int port_number = find_port_hole (&buf1[0]);
1457 snprintf (&buf2[0], name_size+1, "%s %d", &buf1[0], port_number);
1459 return string (&buf2[0]);
1463 IO::find_port_hole (const char* base)
1465 /* CALLER MUST HOLD IO LOCK */
1469 if (_ports.empty()) {
1473 /* we only allow up to 4 characters for the port number
1476 for (n = 1; n < 9999; ++n) {
1477 std::vector<char> buf (AudioEngine::instance()->port_name_size());
1478 PortSet::iterator i = _ports.begin();
1480 snprintf (&buf[0], buf.size()+1, _("%s %u"), base, n);
1482 for ( ; i != _ports.end(); ++i) {
1483 if (string(i->name()) == string(&buf[0])) {
1488 if (i == _ports.end()) {
1496 boost::shared_ptr<AudioPort>
1497 IO::audio(uint32_t n) const
1499 return _ports.nth_audio_port (n);
1503 boost::shared_ptr<MidiPort>
1504 IO::midi(uint32_t n) const
1506 return _ports.nth_midi_port (n);
1510 * Setup a bundle that describe our inputs or outputs. Also creates the bundle if necessary.
1519 _bundle.reset (new Bundle (_direction == Input));
1522 _bundle->suspend_signals ();
1524 _bundle->remove_channels ();
1526 if (_direction == Input) {
1527 snprintf(buf, sizeof (buf), _("%s in"), _name.val().c_str());
1529 snprintf(buf, sizeof (buf), _("%s out"), _name.val().c_str());
1531 _bundle->set_name (buf);
1534 for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
1536 uint32_t const N = _ports.count().get (*i);
1537 for (uint32_t j = 0; j < N; ++j) {
1538 _bundle->add_channel (bundle_channel_name (j, N, *i), *i);
1539 _bundle->set_port (c, _session.engine().make_port_name_non_relative (_ports.port(*i, j)->name()));
1545 _bundle->resume_signals ();
1548 /** @return Bundles connected to our ports */
1550 IO::bundles_connected ()
1555 for (std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) {
1556 bundles.push_back ((*i)->bundle);
1559 /* Session bundles */
1560 boost::shared_ptr<ARDOUR::BundleList> b = _session.bundles ();
1561 for (ARDOUR::BundleList::iterator i = b->begin(); i != b->end(); ++i) {
1562 if ((*i)->connected_to (_bundle, _session.engine())) {
1563 bundles.push_back (*i);
1569 boost::shared_ptr<ARDOUR::RouteList> r = _session.get_routes ();
1571 if (_direction == Input) {
1572 for (ARDOUR::RouteList::iterator i = r->begin(); i != r->end(); ++i) {
1573 if ((*i)->output()->bundle()->connected_to (_bundle, _session.engine())) {
1574 bundles.push_back ((*i)->output()->bundle());
1578 for (ARDOUR::RouteList::iterator i = r->begin(); i != r->end(); ++i) {
1579 if ((*i)->input()->bundle()->connected_to (_bundle, _session.engine())) {
1580 bundles.push_back ((*i)->input()->bundle());
1589 IO::UserBundleInfo::UserBundleInfo (IO* io, boost::shared_ptr<UserBundle> b)
1592 b->Changed.connect_same_thread (changed, boost::bind (&IO::bundle_changed, io, _1));
1596 IO::bundle_channel_name (uint32_t c, uint32_t n, DataType t) const
1600 if (t == DataType::AUDIO) {
1606 return c == 0 ? _("L") : _("R");
1608 snprintf (buf, sizeof(buf), "%d", (c + 1));
1614 snprintf (buf, sizeof(buf), "%d", (c + 1));
1623 IO::name_from_state (const XMLNode& node)
1625 XMLProperty const * prop;
1627 if ((prop = node.property ("name")) != 0) {
1628 return prop->value();
1635 IO::set_name_in_state (XMLNode& node, const string& new_name)
1637 node.add_property (X_("name"), new_name);
1638 XMLNodeList children = node.children ();
1639 for (XMLNodeIterator i = children.begin(); i != children.end(); ++i) {
1640 if ((*i)->name() == X_("Port")) {
1641 string const old_name = (*i)->property(X_("name"))->value();
1642 string const old_name_second_part = old_name.substr (old_name.find_first_of ("/") + 1);
1643 (*i)->add_property (X_("name"), string_compose ("%1/%2", new_name, old_name_second_part));
1649 IO::connected () const
1651 /* do we have any connections at all? */
1653 for (PortSet::const_iterator p = _ports.begin(); p != _ports.end(); ++p) {
1654 if (p->connected()) {
1663 IO::connected_to (boost::shared_ptr<const IO> other) const
1666 return connected ();
1669 assert (_direction != other->direction());
1672 uint32_t no = n_ports().n_total();
1673 uint32_t ni = other->n_ports ().n_total();
1675 for (i = 0; i < no; ++i) {
1676 for (j = 0; j < ni; ++j) {
1677 if ((NULL != nth(i).get()) && (NULL != other->nth(j).get())) {
1678 if (nth(i)->connected_to (other->nth(j)->name())) {
1689 IO::connected_to (const string& str) const
1691 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1692 if (i->connected_to (str)) {
1700 /** Call a processor's ::run() method, giving it our buffers
1701 * Caller must hold process lock.
1704 IO::process_input (boost::shared_ptr<Processor> proc, framepos_t start_frame, framepos_t end_frame, double speed, pframes_t nframes)
1706 /* don't read the data into new buffers - just use the port buffers directly */
1708 if (n_ports().n_total() == 0) {
1709 /* We have no ports, so nothing to process */
1713 _buffers.get_backend_port_addresses (_ports, nframes);
1715 proc->run (_buffers, start_frame, end_frame, speed, nframes, true);
1720 IO::collect_input (BufferSet& bufs, pframes_t nframes, ChanCount offset)
1722 assert(bufs.available() >= _ports.count());
1724 if (_ports.count() == ChanCount::ZERO) {
1728 bufs.set_count (_ports.count());
1730 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1731 PortSet::iterator i = _ports.begin(*t);
1732 BufferSet::iterator b = bufs.begin(*t);
1734 for (uint32_t off = 0; off < offset.get(*t); ++off, ++b) {
1735 if (b == bufs.end(*t)) {
1740 for ( ; i != _ports.end(*t); ++i, ++b) {
1741 const Buffer& bb (i->get_buffer (nframes));
1742 b->read_from (bb, nframes);
1748 IO::copy_to_outputs (BufferSet& bufs, DataType type, pframes_t nframes, framecnt_t offset)
1750 PortSet::iterator o = _ports.begin(type);
1751 BufferSet::iterator i = bufs.begin(type);
1752 BufferSet::iterator prev = i;
1754 assert(i != bufs.end(type)); // or second loop will crash
1756 // Copy any buffers 1:1 to outputs
1758 while (i != bufs.end(type) && o != _ports.end (type)) {
1759 Buffer& port_buffer (o->get_buffer (nframes));
1760 port_buffer.read_from (*i, nframes, offset);
1766 // Copy last buffer to any extra outputs
1768 while (o != _ports.end(type)) {
1769 Buffer& port_buffer (o->get_buffer (nframes));
1770 port_buffer.read_from (*prev, nframes, offset);
1775 boost::shared_ptr<Port>
1776 IO::port_by_name (const std::string& str) const
1778 /* to be called only from ::set_state() - no locking */
1780 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1782 if (i->name() == str) {
1783 return boost::const_pointer_cast<Port> (*i);
1787 return boost::shared_ptr<Port> ();
1791 IO::physically_connected () const
1793 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1794 if (i->physically_connected()) {
1803 IO::has_port (boost::shared_ptr<Port> p) const
1805 Glib::Threads::Mutex::Lock lm (io_lock);
1806 return _ports.contains (p);