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 if (_session.state_of_the_state () & Session::Deletion) {
105 /* this could be called from within our own ::disconnect() method(s)
106 or from somewhere that operates directly on a port. so, we don't
107 know for sure if we can take this lock or not. if we fail,
108 we assume that its safely locked by our own ::disconnect().
111 Glib::Threads::Mutex::Lock tm (io_lock, Glib::Threads::TRY_LOCK);
114 /* we took the lock, so we cannot be here from inside
117 if (_ports.contains (a) || _ports.contains (b)) {
118 changed (IOChange (IOChange::ConnectionsChanged), this); /* EMIT SIGNAL */
121 /* we didn't get the lock, so assume that we're inside
122 * ::disconnect(), and it will call changed() appropriately.
128 IO::increment_port_buffer_offset (pframes_t offset)
130 /* io_lock, not taken: function must be called from Session::process() calltree */
132 if (_direction == Output) {
133 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
134 i->increment_port_buffer_offset (offset);
140 IO::silence (framecnt_t nframes)
142 /* io_lock, not taken: function must be called from Session::process() calltree */
144 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
145 i->get_buffer(nframes).silence (nframes);
149 /** Set _bundles_connected to those bundles that are connected such that every
150 * port on every bundle channel x is connected to port x in _ports.
153 IO::check_bundles_connected ()
155 std::vector<UserBundleInfo*> new_list;
157 for (std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) {
159 uint32_t const N = (*i)->bundle->nchannels().n_total();
161 if (_ports.num_ports() < N) {
167 for (uint32_t j = 0; j < N; ++j) {
168 /* Every port on bundle channel j must be connected to our input j */
169 Bundle::PortList const pl = (*i)->bundle->channel_ports (j);
170 for (uint32_t k = 0; k < pl.size(); ++k) {
171 if (_ports.port(j)->connected_to (pl[k]) == false) {
183 new_list.push_back (*i);
189 _bundles_connected = new_list;
194 IO::disconnect (boost::shared_ptr<Port> our_port, string other_port, void* src)
196 if (other_port.length() == 0 || our_port == 0) {
201 Glib::Threads::Mutex::Lock lm (io_lock);
203 /* check that our_port is really one of ours */
205 if ( ! _ports.contains(our_port)) {
209 /* disconnect it from the source */
211 if (our_port->disconnect (other_port)) {
212 error << string_compose(_("IO: cannot disconnect port %1 from %2"), our_port->name(), other_port) << endmsg;
216 check_bundles_connected ();
219 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
221 _session.set_dirty ();
227 IO::connect (boost::shared_ptr<Port> our_port, string other_port, void* src)
229 if (other_port.length() == 0 || our_port == 0) {
234 Glib::Threads::Mutex::Lock lm (io_lock);
236 /* check that our_port is really one of ours */
238 if ( ! _ports.contains(our_port) ) {
242 /* connect it to the source */
244 if (our_port->connect (other_port)) {
248 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
249 _session.set_dirty ();
254 IO::remove_port (boost::shared_ptr<Port> port, void* src)
256 ChanCount before = _ports.count ();
257 ChanCount after = before;
258 after.set (port->type(), after.get (port->type()) - 1);
260 boost::optional<bool> const r = PortCountChanging (after); /* EMIT SIGNAL */
261 if (r.get_value_or (false)) {
268 BLOCK_PROCESS_CALLBACK ();
271 Glib::Threads::Mutex::Lock lm (io_lock);
273 if (_ports.remove(port)) {
274 change.type = IOChange::Type (change.type | IOChange::ConfigurationChanged);
275 change.before = before;
276 change.after = _ports.count ();
278 if (port->connected()) {
279 change.type = IOChange::Type (change.type | IOChange::ConnectionsChanged);
282 _session.engine().unregister_port (port);
283 check_bundles_connected ();
287 PortCountChanged (n_ports()); /* EMIT SIGNAL */
289 if (change.type != IOChange::NoChange) {
290 changed (change, src);
291 _buffers.attach_buffers (_ports);
295 if (change.type & IOChange::ConfigurationChanged) {
299 if (change.type == IOChange::NoChange) {
303 _session.set_dirty ();
310 * @param destination Name of port to connect new port to.
311 * @param src Source for emitted ConfigurationChanged signal.
312 * @param type Data type of port. Default value (NIL) will use this IO's default type.
315 IO::add_port (string destination, void* src, DataType type)
317 boost::shared_ptr<Port> our_port;
319 if (type == DataType::NIL) {
320 type = _default_type;
323 ChanCount before = _ports.count ();
324 ChanCount after = before;
325 after.set (type, after.get (type) + 1);
327 bool const r = PortCountChanging (after); /* EMIT SIGNAL */
335 BLOCK_PROCESS_CALLBACK ();
339 Glib::Threads::Mutex::Lock lm (io_lock);
341 /* Create a new port */
343 string portname = build_legal_port_name (type);
345 if (_direction == Input) {
346 if ((our_port = _session.engine().register_input_port (type, portname)) == 0) {
347 error << string_compose(_("IO: cannot register input port %1"), portname) << endmsg;
351 if ((our_port = _session.engine().register_output_port (type, portname)) == 0) {
352 error << string_compose(_("IO: cannot register output port %1"), portname) << endmsg;
357 change.before = _ports.count ();
358 _ports.add (our_port);
361 PortCountChanged (n_ports()); /* EMIT SIGNAL */
362 change.type = IOChange::ConfigurationChanged;
363 change.after = _ports.count ();
364 changed (change, src); /* EMIT SIGNAL */
365 _buffers.attach_buffers (_ports);
368 if (!destination.empty()) {
369 if (our_port->connect (destination)) {
374 apply_pretty_name ();
376 _session.set_dirty ();
382 IO::disconnect (void* src)
385 Glib::Threads::Mutex::Lock lm (io_lock);
387 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
388 i->disconnect_all ();
391 check_bundles_connected ();
394 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
399 /** Caller must hold process lock */
401 IO::ensure_ports_locked (ChanCount count, bool clear, bool& changed)
403 #ifndef PLATFORM_WINDOWS
404 assert (!AudioEngine::instance()->process_lock().trylock());
407 boost::shared_ptr<Port> port;
408 vector<boost::shared_ptr<Port> > deleted_ports;
412 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
414 const size_t n = count.get(*t);
416 /* remove unused ports */
417 for (size_t i = n_ports().get(*t); i > n; --i) {
418 port = _ports.port(*t, i-1);
423 /* hold a reference to the port so that we can ensure
424 * that this thread, and not a JACK notification thread,
425 * holds the final reference.
428 deleted_ports.push_back (port);
429 _session.engine().unregister_port (port);
434 /* this will drop the final reference to the deleted ports,
435 * which will in turn call their destructors, which will in
436 * turn call the backend to unregister them.
438 * There will no connect/disconnect or register/unregister
439 * callbacks from the backend until we get here, because
440 * they are driven by the Port destructor. The destructor
441 * will not execute until we drop the final reference,
442 * which all happens right .... here.
444 deleted_ports.clear ();
446 /* create any necessary new ports */
447 while (n_ports().get(*t) < n) {
449 string portname = build_legal_port_name (*t);
453 if (_direction == Input) {
454 if ((port = _session.engine().register_input_port (*t, portname)) == 0) {
455 error << string_compose(_("IO: cannot register input port %1"), portname) << endmsg;
459 if ((port = _session.engine().register_output_port (*t, portname)) == 0) {
460 error << string_compose(_("IO: cannot register output port %1"), portname) << endmsg;
466 catch (AudioEngine::PortRegistrationFailure& err) {
477 check_bundles_connected ();
478 PortCountChanged (n_ports()); /* EMIT SIGNAL */
479 _session.set_dirty ();
483 /* disconnect all existing ports so that we get a fresh start */
484 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
485 i->disconnect_all ();
492 /** Caller must hold process lock */
494 IO::ensure_ports (ChanCount count, bool clear, void* src)
496 #ifndef PLATFORM_WINDOWS
497 assert (!AudioEngine::instance()->process_lock().trylock());
500 bool changed = false;
502 if (count == n_ports() && !clear) {
508 change.before = _ports.count ();
511 Glib::Threads::Mutex::Lock im (io_lock);
512 if (ensure_ports_locked (count, clear, changed)) {
518 change.after = _ports.count ();
519 change.type = IOChange::ConfigurationChanged;
520 this->changed (change, src); /* EMIT SIGNAL */
521 _buffers.attach_buffers (_ports);
523 _session.set_dirty ();
529 /** Caller must hold process lock */
531 IO::ensure_io (ChanCount count, bool clear, void* src)
533 #ifndef PLATFORM_WINDOWS
534 assert (!AudioEngine::instance()->process_lock().trylock());
537 return ensure_ports (count, clear, src);
547 IO::state (bool /*full_state*/)
549 XMLNode* node = new XMLNode (state_node_name);
554 Glib::Threads::Mutex::Lock lm (io_lock);
556 node->add_property("name", _name);
557 id().print (buf, sizeof (buf));
558 node->add_property("id", buf);
559 node->add_property ("direction", enum_2_string (_direction));
560 node->add_property ("default-type", _default_type.to_string());
562 if (!_pretty_name_prefix.empty ()) {
563 node->add_property("pretty-name", _pretty_name_prefix);
566 for (std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) {
567 XMLNode* n = new XMLNode ("Bundle");
568 n->add_property ("name", (*i)->bundle->name ());
569 node->add_child_nocopy (*n);
572 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
574 vector<string> connections;
576 XMLNode* pnode = new XMLNode (X_("Port"));
577 pnode->add_property (X_("type"), i->type().to_string());
578 pnode->add_property (X_("name"), i->name());
580 if (i->get_connections (connections)) {
581 vector<string>::const_iterator ci;
582 std::sort (connections.begin(), connections.end());
584 for (n = 0, ci = connections.begin(); ci != connections.end(); ++ci, ++n) {
586 /* if its a connection to our own port,
587 return only the port name, not the
588 whole thing. this allows connections
589 to be re-established even when our
590 client name is different.
593 XMLNode* cnode = new XMLNode (X_("Connection"));
595 cnode->add_property (X_("other"), _session.engine().make_port_name_relative (*ci));
596 pnode->add_child_nocopy (*cnode);
600 node->add_child_nocopy (*pnode);
603 snprintf (buf, sizeof (buf), "%" PRId64, _user_latency);
604 node->add_property (X_("user-latency"), buf);
610 IO::set_state (const XMLNode& node, int version)
612 /* callers for version < 3000 need to call set_state_2X directly, as A3 IOs
613 * are input OR output, not both, so the direction needs to be specified
616 assert (version >= 3000);
618 XMLProperty const * prop;
619 XMLNodeConstIterator iter;
622 /* force use of non-localized representation of decimal point,
623 since we use it a lot in XML files and so forth.
626 if (node.name() != state_node_name) {
627 error << string_compose(_("incorrect XML node \"%1\" passed to IO object"), node.name()) << endmsg;
631 if ((prop = node.property ("name")) != 0) {
632 set_name (prop->value());
635 if ((prop = node.property (X_("default-type"))) != 0) {
636 _default_type = DataType(prop->value());
637 assert(_default_type != DataType::NIL);
642 if ((prop = node.property ("direction")) != 0) {
643 _direction = (Direction) string_2_enum (prop->value(), _direction);
646 if (create_ports (node, version)) {
650 // after create_ports, updates names
651 if ((prop = node.property ("pretty-name")) != 0) {
652 set_pretty_name (prop->value());
655 if (connecting_legal) {
657 if (make_connections (node, version, false)) {
663 pending_state_node = new XMLNode (node);
664 pending_state_node_version = version;
665 pending_state_node_in = false;
666 ConnectingLegal.connect_same_thread (connection_legal_c, boost::bind (&IO::connecting_became_legal, this));
669 if ((prop = node.property ("user-latency")) != 0) {
670 _user_latency = atoi (prop->value ());
677 IO::set_state_2X (const XMLNode& node, int version, bool in)
679 XMLProperty const * prop;
680 XMLNodeConstIterator iter;
683 /* force use of non-localized representation of decimal point,
684 since we use it a lot in XML files and so forth.
687 if (node.name() != state_node_name) {
688 error << string_compose(_("incorrect XML node \"%1\" passed to IO object"), node.name()) << endmsg;
692 if ((prop = node.property ("name")) != 0) {
693 set_name (prop->value());
696 if ((prop = node.property (X_("default-type"))) != 0) {
697 _default_type = DataType(prop->value());
698 assert(_default_type != DataType::NIL);
703 _direction = in ? Input : Output;
705 if (create_ports (node, version)) {
709 if (connecting_legal) {
711 if (make_connections_2X (node, version, in)) {
717 pending_state_node = new XMLNode (node);
718 pending_state_node_version = version;
719 pending_state_node_in = in;
720 ConnectingLegal.connect_same_thread (connection_legal_c, boost::bind (&IO::connecting_became_legal, this));
727 IO::connecting_became_legal ()
731 assert (pending_state_node);
733 connection_legal_c.disconnect ();
735 // it's not required for TracksLive, as long as TracksLive's session does all the connections when it's being loaded
736 if (!Profile->get_trx() ) {
737 ret = make_connections (*pending_state_node, pending_state_node_version, pending_state_node_in);
740 delete pending_state_node;
741 pending_state_node = 0;
746 boost::shared_ptr<Bundle>
747 IO::find_possible_bundle (const string &desired_name)
749 static const string digits = "0123456789";
750 const string &default_name = (_direction == Input ? _("in") : _("out"));
751 const string &bundle_type_name = (_direction == Input ? _("input") : _("output"));
753 boost::shared_ptr<Bundle> c = _session.bundle_by_name (desired_name);
756 int bundle_number, mask;
757 string possible_name;
759 string::size_type last_non_digit_pos;
761 error << string_compose(_("Unknown bundle \"%1\" listed for %2 of %3"), desired_name, bundle_type_name, _name)
764 // find numeric suffix of desired name
767 last_non_digit_pos = desired_name.find_last_not_of(digits);
769 if (last_non_digit_pos != string::npos) {
771 s << desired_name.substr(last_non_digit_pos);
775 // see if it's a stereo connection e.g. "in 3+4"
777 if (last_non_digit_pos > 1 && desired_name[last_non_digit_pos] == '+') {
778 string::size_type left_last_non_digit_pos;
780 left_last_non_digit_pos = desired_name.find_last_not_of(digits, last_non_digit_pos-1);
782 if (left_last_non_digit_pos != string::npos) {
783 int left_bundle_number = 0;
785 s << desired_name.substr(left_last_non_digit_pos, last_non_digit_pos-1);
786 s >> left_bundle_number;
788 if (left_bundle_number > 0 && left_bundle_number + 1 == bundle_number) {
799 // find highest set bit
801 while ((mask <= bundle_number) && (mask <<= 1)) {}
803 // "wrap" bundle number into largest possible power of 2
808 if (bundle_number & mask) {
809 bundle_number &= ~mask;
812 s << default_name << " " << bundle_number + 1;
815 s << "+" << bundle_number + 2;
818 possible_name = s.str();
820 if ((c = _session.bundle_by_name (possible_name)) != 0) {
827 info << string_compose (_("Bundle %1 was not available - \"%2\" used instead"), desired_name, possible_name)
830 error << string_compose(_("No %1 bundles available as a replacement"), bundle_type_name)
841 IO::get_port_counts_2X (XMLNode const & node, int /*version*/, ChanCount& n, boost::shared_ptr<Bundle>& /*c*/)
843 XMLProperty const * prop;
844 XMLNodeList children = node.children ();
846 uint32_t n_audio = 0;
848 for (XMLNodeIterator i = children.begin(); i != children.end(); ++i) {
850 if ((prop = node.property ("inputs")) != 0 && _direction == Input) {
851 n_audio = count (prop->value().begin(), prop->value().end(), '{');
852 } else if ((prop = node.property ("input-connection")) != 0 && _direction == Input) {
854 } else if ((prop = node.property ("outputs")) != 0 && _direction == Output) {
855 n_audio = count (prop->value().begin(), prop->value().end(), '{');
856 } else if ((prop = node.property ("output-connection")) != 0 && _direction == Output) {
862 cnt.set_audio (n_audio);
863 n = ChanCount::max (n, cnt);
869 IO::get_port_counts (const XMLNode& node, int version, ChanCount& n, boost::shared_ptr<Bundle>& c)
871 if (version < 3000) {
872 return get_port_counts_2X (node, version, n, c);
875 XMLProperty const * prop;
876 XMLNodeConstIterator iter;
877 uint32_t n_audio = 0;
883 if ((prop = node.property ("connection")) != 0) {
885 if ((c = find_possible_bundle (prop->value())) != 0) {
886 n = ChanCount::max (n, c->nchannels());
891 for (iter = node.children().begin(); iter != node.children().end(); ++iter) {
893 if ((*iter)->name() == X_("Bundle")) {
894 prop = (*iter)->property ("name");
895 if ((c = find_possible_bundle (prop->value())) != 0) {
896 n = ChanCount::max (n, c->nchannels());
903 if ((*iter)->name() == X_("Port")) {
904 prop = (*iter)->property (X_("type"));
910 if (prop->value() == X_("audio")) {
911 cnt.set_audio (++n_audio);
912 } else if (prop->value() == X_("midi")) {
913 cnt.set_midi (++n_midi);
918 n = ChanCount::max (n, cnt);
923 IO::create_ports (const XMLNode& node, int version)
926 boost::shared_ptr<Bundle> c;
928 get_port_counts (node, version, n, c);
931 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
933 if (ensure_ports (n, true, this)) {
934 error << string_compose(_("%1: cannot create I/O ports"), _name) << endmsg;
945 IO::make_connections (const XMLNode& node, int version, bool in)
947 if (version < 3000) {
948 return make_connections_2X (node, version, in);
951 XMLProperty const * prop;
953 for (XMLNodeConstIterator i = node.children().begin(); i != node.children().end(); ++i) {
955 if ((*i)->name() == "Bundle") {
956 XMLProperty const * prop = (*i)->property ("name");
958 boost::shared_ptr<Bundle> b = find_possible_bundle (prop->value());
960 connect_ports_to_bundle (b, true, this);
967 if ((*i)->name() == "Port") {
969 prop = (*i)->property (X_("name"));
975 boost::shared_ptr<Port> p = port_by_name (prop->value());
978 for (XMLNodeConstIterator c = (*i)->children().begin(); c != (*i)->children().end(); ++c) {
980 XMLNode* cnode = (*c);
982 if (cnode->name() != X_("Connection")) {
986 if ((prop = cnode->property (X_("other"))) == 0) {
991 connect (p, prop->value(), this);
1002 IO::prepare_for_reset (XMLNode& node, const std::string& name)
1005 node.add_property ("name", name);
1007 /* now find connections and reset the name of the port
1008 in one so that when we re-use it it will match
1009 the name of the thing we're applying it to.
1013 XMLNodeList children = node.children();
1015 for (XMLNodeIterator i = children.begin(); i != children.end(); ++i) {
1017 if ((*i)->name() == "Port") {
1019 prop = (*i)->property (X_("name"));
1023 string old = prop->value();
1024 string::size_type slash = old.find ('/');
1026 if (slash != string::npos) {
1027 /* port name is of form: <IO-name>/<port-name> */
1030 new_name += old.substr (old.find ('/'));
1032 prop->set_value (new_name);
1041 IO::make_connections_2X (const XMLNode& node, int /*version*/, bool in)
1043 XMLProperty const * prop;
1045 /* XXX: bundles ("connections" as was) */
1047 if ((prop = node.property ("inputs")) != 0 && in) {
1049 string::size_type ostart = 0;
1050 string::size_type start = 0;
1051 string::size_type end = 0;
1054 vector<string> ports;
1056 string const str = prop->value ();
1058 while ((start = str.find_first_of ('{', ostart)) != string::npos) {
1061 if ((end = str.find_first_of ('}', start)) == string::npos) {
1062 error << string_compose(_("IO: badly formed string in XML node for inputs \"%1\""), str) << endmsg;
1066 if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
1067 error << string_compose(_("bad input string in XML node \"%1\""), str) << endmsg;
1074 for (int x = 0; x < n; ++x) {
1075 /* XXX: this is a bit of a hack; need to check if it's always valid */
1076 string::size_type const p = ports[x].find ("/out");
1077 if (p != string::npos) {
1078 ports[x].replace (p, 4, "/audio_out");
1080 if (NULL != nth(i).get())
1081 nth(i)->connect (ports[x]);
1091 if ((prop = node.property ("outputs")) != 0 && !in) {
1093 string::size_type ostart = 0;
1094 string::size_type start = 0;
1095 string::size_type end = 0;
1098 vector<string> ports;
1100 string const str = prop->value ();
1102 while ((start = str.find_first_of ('{', ostart)) != string::npos) {
1105 if ((end = str.find_first_of ('}', start)) == string::npos) {
1106 error << string_compose(_("IO: badly formed string in XML node for outputs \"%1\""), str) << endmsg;
1110 if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
1111 error << string_compose(_("IO: bad output string in XML node \"%1\""), str) << endmsg;
1117 for (int x = 0; x < n; ++x) {
1118 /* XXX: this is a bit of a hack; need to check if it's always valid */
1119 string::size_type const p = ports[x].find ("/in");
1120 if (p != string::npos) {
1121 ports[x].replace (p, 3, "/audio_in");
1123 if (NULL != nth(i).get())
1124 nth(i)->connect (ports[x]);
1137 IO::set_ports (const string& str)
1139 vector<string> ports;
1143 if ((nports = count (str.begin(), str.end(), '{')) == 0) {
1148 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
1150 // FIXME: audio-only
1151 if (ensure_ports (ChanCount(DataType::AUDIO, nports), true, this)) {
1156 string::size_type start = 0;
1157 string::size_type end = 0;
1158 string::size_type ostart = 0;
1159 for (int i = 0; (start = str.find_first_of ('{', ostart)) != string::npos; ++i) {
1162 if ((end = str.find_first_of ('}', start)) == string::npos) {
1163 error << string_compose(_("IO: badly formed string in XML node for inputs \"%1\""), str) << endmsg;
1167 if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
1168 error << string_compose(_("bad input string in XML node \"%1\""), str) << endmsg;
1174 for (int x = 0; x < n; ++x) {
1175 connect (nth (i), ports[x], this);
1186 IO::parse_io_string (const string& str, vector<string>& ports)
1188 string::size_type pos, opos;
1190 if (str.length() == 0) {
1198 while ((pos = str.find_first_of (',', opos)) != string::npos) {
1199 ports.push_back (str.substr (opos, pos - opos));
1203 if (opos < str.length()) {
1204 ports.push_back (str.substr(opos));
1207 return ports.size();
1211 IO::parse_gain_string (const string& str, vector<string>& ports)
1213 string::size_type pos, opos;
1218 while ((pos = str.find_first_of (',', opos)) != string::npos) {
1219 ports.push_back (str.substr (opos, pos - opos));
1223 if (opos < str.length()) {
1224 ports.push_back (str.substr(opos));
1227 return ports.size();
1231 IO::set_name (const string& requested_name)
1233 string name = requested_name;
1235 if (_name == name) {
1239 /* replace all colons in the name. i wish we didn't have to do this */
1241 replace_all (name, ":", "-");
1243 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
1244 string current_name = i->name();
1245 current_name.replace (current_name.find (_name), _name.val().length(), name);
1246 i->set_name (current_name);
1249 bool const r = SessionObject::set_name (name);
1257 IO::set_pretty_name (const std::string& str)
1259 if (_pretty_name_prefix == str) {
1262 _pretty_name_prefix = str;
1263 apply_pretty_name ();
1267 IO::apply_pretty_name ()
1270 if (_pretty_name_prefix.empty ()) {
1273 for (PortSet::iterator i = _ports.begin (); i != _ports.end(); ++i, ++pn) {
1274 (*i)->set_pretty_name (string_compose (("%1/%2 %3"),
1275 _pretty_name_prefix,
1276 _direction == Output ? _("Out") : _("In"),
1282 IO::latency () const
1284 framecnt_t max_latency;
1289 /* io lock not taken - must be protected by other means */
1291 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1292 if ((latency = i->private_latency_range (_direction == Output).max) > max_latency) {
1293 DEBUG_TRACE (DEBUG::Latency, string_compose ("port %1 has %2 latency of %3 - use\n",
1295 ((_direction == Output) ? "PLAYBACK" : "CAPTURE"),
1297 max_latency = latency;
1301 DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: max %4 latency from %2 ports = %3\n",
1302 name(), _ports.num_ports(), max_latency,
1303 ((_direction == Output) ? "PLAYBACK" : "CAPTURE")));
1308 IO::connect_ports_to_bundle (boost::shared_ptr<Bundle> c, bool exclusive, void* src)
1310 BLOCK_PROCESS_CALLBACK ();
1313 Glib::Threads::Mutex::Lock lm2 (io_lock);
1316 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
1317 i->disconnect_all ();
1321 c->connect (_bundle, _session.engine());
1323 /* If this is a UserBundle, make a note of what we've done */
1325 boost::shared_ptr<UserBundle> ub = boost::dynamic_pointer_cast<UserBundle> (c);
1328 /* See if we already know about this one */
1329 std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin();
1330 while (i != _bundles_connected.end() && (*i)->bundle != ub) {
1334 if (i == _bundles_connected.end()) {
1335 /* We don't, so make a note */
1336 _bundles_connected.push_back (new UserBundleInfo (this, ub));
1341 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
1346 IO::disconnect_ports_from_bundle (boost::shared_ptr<Bundle> c, void* src)
1348 BLOCK_PROCESS_CALLBACK ();
1351 Glib::Threads::Mutex::Lock lm2 (io_lock);
1353 c->disconnect (_bundle, _session.engine());
1355 /* If this is a UserBundle, make a note of what we've done */
1357 boost::shared_ptr<UserBundle> ub = boost::dynamic_pointer_cast<UserBundle> (c);
1360 std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin();
1361 while (i != _bundles_connected.end() && (*i)->bundle != ub) {
1365 if (i != _bundles_connected.end()) {
1367 _bundles_connected.erase (i);
1372 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
1378 IO::disable_connecting ()
1380 connecting_legal = false;
1385 IO::enable_connecting ()
1387 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock());
1388 connecting_legal = true;
1389 boost::optional<int> r = ConnectingLegal ();
1390 return r.get_value_or (0);
1394 IO::bundle_changed (Bundle::Change /*c*/)
1397 // connect_input_ports_to_bundle (_input_bundle, this);
1402 IO::build_legal_port_name (DataType type)
1404 const int name_size = AudioEngine::instance()->port_name_size();
1408 if (type == DataType::AUDIO) {
1409 suffix = X_("audio");
1410 } else if (type == DataType::MIDI) {
1411 suffix = X_("midi");
1413 throw unknown_type();
1416 /* note that if "in" or "out" are translated it will break a session
1417 across locale switches because a port's connection list will
1418 show (old) translated names, but the current port name will
1419 use the (new) translated name.
1423 if (_direction == Input) {
1424 suffix += X_("_return");
1426 suffix += X_("_send");
1429 if (_direction == Input) {
1430 suffix += X_("_in");
1432 suffix += X_("_out");
1436 // allow up to 4 digits for the output port number, plus the slash, suffix and extra space
1438 limit = name_size - AudioEngine::instance()->my_name().length() - (suffix.length() + 5);
1440 std::vector<char> buf1(name_size+1);
1441 std::vector<char> buf2(name_size+1);
1443 /* colons are illegal in port names, so fix that */
1445 string nom = _name.val();
1446 replace_all (nom, ":", ";");
1448 snprintf (&buf1[0], name_size+1, ("%.*s/%s"), limit, nom.c_str(), suffix.c_str());
1450 int port_number = find_port_hole (&buf1[0]);
1451 snprintf (&buf2[0], name_size+1, "%s %d", &buf1[0], port_number);
1453 return string (&buf2[0]);
1457 IO::find_port_hole (const char* base)
1459 /* CALLER MUST HOLD IO LOCK */
1463 if (_ports.empty()) {
1467 /* we only allow up to 4 characters for the port number
1470 for (n = 1; n < 9999; ++n) {
1471 std::vector<char> buf (AudioEngine::instance()->port_name_size());
1472 PortSet::iterator i = _ports.begin();
1474 snprintf (&buf[0], buf.size()+1, _("%s %u"), base, n);
1476 for ( ; i != _ports.end(); ++i) {
1477 if (string(i->name()) == string(&buf[0])) {
1482 if (i == _ports.end()) {
1490 boost::shared_ptr<AudioPort>
1491 IO::audio(uint32_t n) const
1493 return _ports.nth_audio_port (n);
1497 boost::shared_ptr<MidiPort>
1498 IO::midi(uint32_t n) const
1500 return _ports.nth_midi_port (n);
1504 * Setup a bundle that describe our inputs or outputs. Also creates the bundle if necessary.
1513 _bundle.reset (new Bundle (_direction == Input));
1516 _bundle->suspend_signals ();
1518 _bundle->remove_channels ();
1520 if (_direction == Input) {
1521 snprintf(buf, sizeof (buf), _("%s in"), _name.val().c_str());
1523 snprintf(buf, sizeof (buf), _("%s out"), _name.val().c_str());
1525 _bundle->set_name (buf);
1528 for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
1530 uint32_t const N = _ports.count().get (*i);
1531 for (uint32_t j = 0; j < N; ++j) {
1532 _bundle->add_channel (bundle_channel_name (j, N, *i), *i);
1533 _bundle->set_port (c, _session.engine().make_port_name_non_relative (_ports.port(*i, j)->name()));
1539 _bundle->resume_signals ();
1542 /** @return Bundles connected to our ports */
1544 IO::bundles_connected ()
1549 for (std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) {
1550 bundles.push_back ((*i)->bundle);
1553 /* Session bundles */
1554 boost::shared_ptr<ARDOUR::BundleList> b = _session.bundles ();
1555 for (ARDOUR::BundleList::iterator i = b->begin(); i != b->end(); ++i) {
1556 if ((*i)->connected_to (_bundle, _session.engine())) {
1557 bundles.push_back (*i);
1563 boost::shared_ptr<ARDOUR::RouteList> r = _session.get_routes ();
1565 if (_direction == Input) {
1566 for (ARDOUR::RouteList::iterator i = r->begin(); i != r->end(); ++i) {
1567 if ((*i)->output()->bundle()->connected_to (_bundle, _session.engine())) {
1568 bundles.push_back ((*i)->output()->bundle());
1572 for (ARDOUR::RouteList::iterator i = r->begin(); i != r->end(); ++i) {
1573 if ((*i)->input()->bundle()->connected_to (_bundle, _session.engine())) {
1574 bundles.push_back ((*i)->input()->bundle());
1583 IO::UserBundleInfo::UserBundleInfo (IO* io, boost::shared_ptr<UserBundle> b)
1586 b->Changed.connect_same_thread (changed, boost::bind (&IO::bundle_changed, io, _1));
1590 IO::bundle_channel_name (uint32_t c, uint32_t n, DataType t) const
1594 if (t == DataType::AUDIO) {
1600 return c == 0 ? _("L") : _("R");
1602 snprintf (buf, sizeof(buf), "%d", (c + 1));
1608 snprintf (buf, sizeof(buf), "%d", (c + 1));
1617 IO::name_from_state (const XMLNode& node)
1619 XMLProperty const * prop;
1621 if ((prop = node.property ("name")) != 0) {
1622 return prop->value();
1629 IO::set_name_in_state (XMLNode& node, const string& new_name)
1631 node.add_property (X_("name"), new_name);
1632 XMLNodeList children = node.children ();
1633 for (XMLNodeIterator i = children.begin(); i != children.end(); ++i) {
1634 if ((*i)->name() == X_("Port")) {
1635 string const old_name = (*i)->property(X_("name"))->value();
1636 string const old_name_second_part = old_name.substr (old_name.find_first_of ("/") + 1);
1637 (*i)->add_property (X_("name"), string_compose ("%1/%2", new_name, old_name_second_part));
1643 IO::connected () const
1645 /* do we have any connections at all? */
1647 for (PortSet::const_iterator p = _ports.begin(); p != _ports.end(); ++p) {
1648 if (p->connected()) {
1657 IO::connected_to (boost::shared_ptr<const IO> other) const
1660 return connected ();
1663 assert (_direction != other->direction());
1666 uint32_t no = n_ports().n_total();
1667 uint32_t ni = other->n_ports ().n_total();
1669 for (i = 0; i < no; ++i) {
1670 for (j = 0; j < ni; ++j) {
1671 if ((NULL != nth(i).get()) && (NULL != other->nth(j).get())) {
1672 if (nth(i)->connected_to (other->nth(j)->name())) {
1683 IO::connected_to (const string& str) const
1685 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1686 if (i->connected_to (str)) {
1694 /** Call a processor's ::run() method, giving it our buffers
1695 * Caller must hold process lock.
1698 IO::process_input (boost::shared_ptr<Processor> proc, framepos_t start_frame, framepos_t end_frame, double speed, pframes_t nframes)
1700 /* don't read the data into new buffers - just use the port buffers directly */
1702 if (n_ports().n_total() == 0) {
1703 /* We have no ports, so nothing to process */
1707 _buffers.get_backend_port_addresses (_ports, nframes);
1709 proc->run (_buffers, start_frame, end_frame, speed, nframes, true);
1714 IO::collect_input (BufferSet& bufs, pframes_t nframes, ChanCount offset)
1716 assert(bufs.available() >= _ports.count());
1718 if (_ports.count() == ChanCount::ZERO) {
1722 bufs.set_count (_ports.count());
1724 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1725 PortSet::iterator i = _ports.begin(*t);
1726 BufferSet::iterator b = bufs.begin(*t);
1728 for (uint32_t off = 0; off < offset.get(*t); ++off, ++b) {
1729 if (b == bufs.end(*t)) {
1734 for ( ; i != _ports.end(*t); ++i, ++b) {
1735 const Buffer& bb (i->get_buffer (nframes));
1736 b->read_from (bb, nframes);
1742 IO::copy_to_outputs (BufferSet& bufs, DataType type, pframes_t nframes, framecnt_t offset)
1744 PortSet::iterator o = _ports.begin(type);
1745 BufferSet::iterator i = bufs.begin(type);
1746 BufferSet::iterator prev = i;
1748 assert(i != bufs.end(type)); // or second loop will crash
1750 // Copy any buffers 1:1 to outputs
1752 while (i != bufs.end(type) && o != _ports.end (type)) {
1753 Buffer& port_buffer (o->get_buffer (nframes));
1754 port_buffer.read_from (*i, nframes, offset);
1760 // Copy last buffer to any extra outputs
1762 while (o != _ports.end(type)) {
1763 Buffer& port_buffer (o->get_buffer (nframes));
1764 port_buffer.read_from (*prev, nframes, offset);
1769 boost::shared_ptr<Port>
1770 IO::port_by_name (const std::string& str) const
1772 /* to be called only from ::set_state() - no locking */
1774 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1776 if (i->name() == str) {
1777 return boost::const_pointer_cast<Port> (*i);
1781 return boost::shared_ptr<Port> ();
1785 IO::physically_connected () const
1787 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1788 if (i->physically_connected()) {
1797 IO::has_port (boost::shared_ptr<Port> p) const
1799 Glib::Threads::Mutex::Lock lm (io_lock);
1800 return _ports.contains (p);