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"
34 #include "pbd/locale_guard.h"
35 #include "pbd/types_convert.h"
37 #include "ardour/audioengine.h"
38 #include "ardour/buffer.h"
39 #include "ardour/buffer_set.h"
40 #include "ardour/debug.h"
41 #include "ardour/io.h"
42 #include "ardour/port.h"
43 #include "ardour/profile.h"
44 #include "ardour/route.h"
45 #include "ardour/session.h"
46 #include "ardour/types_convert.h"
47 #include "ardour/user_bundle.h"
51 #define BLOCK_PROCESS_CALLBACK() Glib::Threads::Mutex::Lock em (AudioEngine::instance()->process_lock())
54 using namespace ARDOUR;
57 const string IO::state_node_name = "IO";
58 bool IO::connecting_legal = false;
59 PBD::Signal0<int> IO::ConnectingLegal;
60 PBD::Signal1<void,ChanCount> IO::PortCountChanged;
62 /** @param default_type The type of port that will be created by ensure_io
63 * and friends if no type is explicitly requested (to avoid breakage).
65 IO::IO (Session& s, const string& name, Direction dir, DataType default_type, bool sendish)
66 : SessionObject (s, name)
68 , _default_type (default_type)
72 Port::PostDisconnect.connect_same_thread (*this, boost::bind (&IO::disconnect_check, this, _1, _2));
73 pending_state_node = 0;
77 IO::IO (Session& s, const XMLNode& node, DataType dt, bool sendish)
78 : SessionObject(s, "unnamed io")
84 pending_state_node = 0;
85 Port::PostDisconnect.connect_same_thread (*this, boost::bind (&IO::disconnect_check, this, _1, _2));
87 set_state (node, Stateful::loading_state_version);
93 Glib::Threads::Mutex::Lock lm (io_lock);
95 DEBUG_TRACE (DEBUG::Ports, string_compose ("IO %1 unregisters %2 ports\n", name(), _ports.num_ports()));
97 BLOCK_PROCESS_CALLBACK ();
99 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
100 _session.engine().unregister_port (*i);
102 delete pending_state_node; pending_state_node = 0;
106 IO::disconnect_check (boost::shared_ptr<Port> a, boost::shared_ptr<Port> b)
108 if (_session.state_of_the_state () & Session::Deletion) {
111 /* this could be called from within our own ::disconnect() method(s)
112 or from somewhere that operates directly on a port. so, we don't
113 know for sure if we can take this lock or not. if we fail,
114 we assume that its safely locked by our own ::disconnect().
117 Glib::Threads::Mutex::Lock tm (io_lock, Glib::Threads::TRY_LOCK);
120 /* we took the lock, so we cannot be here from inside
123 if (_ports.contains (a) || _ports.contains (b)) {
124 changed (IOChange (IOChange::ConnectionsChanged), this); /* EMIT SIGNAL */
127 /* we didn't get the lock, so assume that we're inside
128 * ::disconnect(), and it will call changed() appropriately.
134 IO::increment_port_buffer_offset (pframes_t offset)
136 /* io_lock, not taken: function must be called from Session::process() calltree */
138 if (_direction == Output) {
139 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
140 i->increment_port_buffer_offset (offset);
146 IO::silence (framecnt_t nframes)
148 /* io_lock, not taken: function must be called from Session::process() calltree */
150 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
151 i->get_buffer(nframes).silence (nframes);
155 /** Set _bundles_connected to those bundles that are connected such that every
156 * port on every bundle channel x is connected to port x in _ports.
159 IO::check_bundles_connected ()
161 std::vector<UserBundleInfo*> new_list;
163 for (std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) {
165 uint32_t const N = (*i)->bundle->nchannels().n_total();
167 if (_ports.num_ports() < N) {
173 for (uint32_t j = 0; j < N; ++j) {
174 /* Every port on bundle channel j must be connected to our input j */
175 Bundle::PortList const pl = (*i)->bundle->channel_ports (j);
176 for (uint32_t k = 0; k < pl.size(); ++k) {
177 if (_ports.port(j)->connected_to (pl[k]) == false) {
189 new_list.push_back (*i);
195 _bundles_connected = new_list;
200 IO::disconnect (boost::shared_ptr<Port> our_port, string other_port, void* src)
202 if (other_port.length() == 0 || our_port == 0) {
207 Glib::Threads::Mutex::Lock lm (io_lock);
209 /* check that our_port is really one of ours */
211 if ( ! _ports.contains(our_port)) {
215 /* disconnect it from the source */
217 if (our_port->disconnect (other_port)) {
218 error << string_compose(_("IO: cannot disconnect port %1 from %2"), our_port->name(), other_port) << endmsg;
222 check_bundles_connected ();
225 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
227 _session.set_dirty ();
233 IO::connect (boost::shared_ptr<Port> our_port, string other_port, void* src)
235 if (other_port.length() == 0 || our_port == 0) {
240 Glib::Threads::Mutex::Lock lm (io_lock);
242 /* check that our_port is really one of ours */
244 if ( ! _ports.contains(our_port) ) {
248 /* connect it to the source */
250 if (our_port->connect (other_port)) {
254 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
255 _session.set_dirty ();
260 IO::remove_port (boost::shared_ptr<Port> port, void* src)
262 ChanCount before = _ports.count ();
263 ChanCount after = before;
264 after.set (port->type(), after.get (port->type()) - 1);
266 boost::optional<bool> const r = PortCountChanging (after); /* EMIT SIGNAL */
267 if (r.get_value_or (false)) {
274 BLOCK_PROCESS_CALLBACK ();
277 Glib::Threads::Mutex::Lock lm (io_lock);
279 if (_ports.remove(port)) {
280 change.type = IOChange::Type (change.type | IOChange::ConfigurationChanged);
281 change.before = before;
282 change.after = _ports.count ();
284 if (port->connected()) {
285 change.type = IOChange::Type (change.type | IOChange::ConnectionsChanged);
288 _session.engine().unregister_port (port);
289 check_bundles_connected ();
293 PortCountChanged (n_ports()); /* EMIT SIGNAL */
295 if (change.type != IOChange::NoChange) {
296 changed (change, src);
297 _buffers.attach_buffers (_ports);
301 if (change.type & IOChange::ConfigurationChanged) {
305 if (change.type == IOChange::NoChange) {
309 _session.set_dirty ();
316 * @param destination Name of port to connect new port to.
317 * @param src Source for emitted ConfigurationChanged signal.
318 * @param type Data type of port. Default value (NIL) will use this IO's default type.
321 IO::add_port (string destination, void* src, DataType type)
323 boost::shared_ptr<Port> our_port;
325 if (type == DataType::NIL) {
326 type = _default_type;
329 ChanCount before = _ports.count ();
330 ChanCount after = before;
331 after.set (type, after.get (type) + 1);
333 bool const r = PortCountChanging (after); /* EMIT SIGNAL */
341 BLOCK_PROCESS_CALLBACK ();
345 Glib::Threads::Mutex::Lock lm (io_lock);
347 /* Create a new port */
349 string portname = build_legal_port_name (type);
351 if (_direction == Input) {
352 if ((our_port = _session.engine().register_input_port (type, portname)) == 0) {
353 error << string_compose(_("IO: cannot register input port %1"), portname) << endmsg;
357 if ((our_port = _session.engine().register_output_port (type, portname)) == 0) {
358 error << string_compose(_("IO: cannot register output port %1"), portname) << endmsg;
363 change.before = _ports.count ();
364 _ports.add (our_port);
367 PortCountChanged (n_ports()); /* EMIT SIGNAL */
368 change.type = IOChange::ConfigurationChanged;
369 change.after = _ports.count ();
370 changed (change, src); /* EMIT SIGNAL */
371 _buffers.attach_buffers (_ports);
374 if (!destination.empty()) {
375 if (our_port->connect (destination)) {
380 apply_pretty_name ();
382 _session.set_dirty ();
388 IO::disconnect (void* src)
391 Glib::Threads::Mutex::Lock lm (io_lock);
393 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
394 i->disconnect_all ();
397 check_bundles_connected ();
400 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
405 /** Caller must hold process lock */
407 IO::ensure_ports_locked (ChanCount count, bool clear, bool& changed)
409 #ifndef PLATFORM_WINDOWS
410 assert (!AudioEngine::instance()->process_lock().trylock());
413 boost::shared_ptr<Port> port;
414 vector<boost::shared_ptr<Port> > deleted_ports;
418 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
420 const size_t n = count.get(*t);
422 /* remove unused ports */
423 for (size_t i = n_ports().get(*t); i > n; --i) {
424 port = _ports.port(*t, i-1);
429 /* hold a reference to the port so that we can ensure
430 * that this thread, and not a JACK notification thread,
431 * holds the final reference.
434 deleted_ports.push_back (port);
435 _session.engine().unregister_port (port);
440 /* this will drop the final reference to the deleted ports,
441 * which will in turn call their destructors, which will in
442 * turn call the backend to unregister them.
444 * There will no connect/disconnect or register/unregister
445 * callbacks from the backend until we get here, because
446 * they are driven by the Port destructor. The destructor
447 * will not execute until we drop the final reference,
448 * which all happens right .... here.
450 deleted_ports.clear ();
452 /* create any necessary new ports */
453 while (n_ports().get(*t) < n) {
455 string portname = build_legal_port_name (*t);
459 if (_direction == Input) {
460 if ((port = _session.engine().register_input_port (*t, portname)) == 0) {
461 error << string_compose(_("IO: cannot register input port %1"), portname) << endmsg;
465 if ((port = _session.engine().register_output_port (*t, portname)) == 0) {
466 error << string_compose(_("IO: cannot register output port %1"), portname) << endmsg;
472 catch (AudioEngine::PortRegistrationFailure& err) {
483 check_bundles_connected ();
484 PortCountChanged (n_ports()); /* EMIT SIGNAL */
485 _session.set_dirty ();
489 /* disconnect all existing ports so that we get a fresh start */
490 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
491 i->disconnect_all ();
498 /** Caller must hold process lock */
500 IO::ensure_ports (ChanCount count, bool clear, void* src)
502 #ifndef PLATFORM_WINDOWS
503 assert (!AudioEngine::instance()->process_lock().trylock());
506 bool changed = false;
508 if (count == n_ports() && !clear) {
514 change.before = _ports.count ();
517 Glib::Threads::Mutex::Lock im (io_lock);
518 if (ensure_ports_locked (count, clear, changed)) {
524 change.after = _ports.count ();
525 change.type = IOChange::ConfigurationChanged;
526 this->changed (change, src); /* EMIT SIGNAL */
527 _buffers.attach_buffers (_ports);
529 _session.set_dirty ();
535 /** Caller must hold process lock */
537 IO::ensure_io (ChanCount count, bool clear, void* src)
539 #ifndef PLATFORM_WINDOWS
540 assert (!AudioEngine::instance()->process_lock().trylock());
543 return ensure_ports (count, clear, src);
553 IO::state (bool /*full_state*/)
555 XMLNode* node = new XMLNode (state_node_name);
557 Glib::Threads::Mutex::Lock lm (io_lock);
559 node->set_property ("name", name());
560 node->set_property ("id", id ());
561 node->set_property ("direction", _direction);
562 node->set_property ("default-type", _default_type);
564 if (!_pretty_name_prefix.empty ()) {
565 node->set_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->set_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->set_property (X_("type"), i->type());
580 pnode->set_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->set_property (X_("other"), _session.engine().make_port_name_relative (*ci));
598 pnode->add_child_nocopy (*cnode);
602 node->add_child_nocopy (*pnode);
605 node->set_property (X_("user-latency"), _user_latency);
611 IO::set_state (const XMLNode& node, int version)
613 /* callers for version < 3000 need to call set_state_2X directly, as A3 IOs
614 * are input OR output, not both, so the direction needs to be specified
617 assert (version >= 3000);
619 /* force use of non-localized representation of decimal point,
620 since we use it a lot in XML files and so forth.
623 if (node.name() != state_node_name) {
624 error << string_compose(_("incorrect XML node \"%1\" passed to IO object"), node.name()) << endmsg;
628 bool ignore_name = node.property ("ignore-name");
630 if (node.get_property ("name", name) && !ignore_name) {
634 if (node.get_property (X_("default-type"), _default_type)) {
635 assert(_default_type != DataType::NIL);
640 node.get_property ("direction", _direction);
642 if (create_ports (node, version)) {
646 // after create_ports, updates names
647 if (node.get_property ("pretty-name", name)) {
648 set_pretty_name (name);
651 if (connecting_legal) {
653 if (make_connections (node, version, false)) {
659 delete pending_state_node;
660 pending_state_node = new XMLNode (node);
661 pending_state_node_version = version;
662 pending_state_node_in = false;
663 ConnectingLegal.connect_same_thread (connection_legal_c, boost::bind (&IO::connecting_became_legal, this));
666 node.get_property ("user-latency", _user_latency);
672 IO::set_state_2X (const XMLNode& node, int version, bool in)
674 XMLProperty const * prop;
675 XMLNodeConstIterator iter;
678 /* force use of non-localized representation of decimal point,
679 since we use it a lot in XML files and so forth.
682 if (node.name() != state_node_name) {
683 error << string_compose(_("incorrect XML node \"%1\" passed to IO object"), node.name()) << endmsg;
687 if ((prop = node.property ("name")) != 0) {
688 set_name (prop->value());
691 if ((prop = node.property (X_("default-type"))) != 0) {
692 _default_type = DataType(prop->value());
693 assert(_default_type != DataType::NIL);
698 _direction = in ? Input : Output;
700 if (create_ports (node, version)) {
704 if (connecting_legal) {
706 if (make_connections_2X (node, version, in)) {
712 delete pending_state_node;
713 pending_state_node = new XMLNode (node);
714 pending_state_node_version = version;
715 pending_state_node_in = in;
716 ConnectingLegal.connect_same_thread (connection_legal_c, boost::bind (&IO::connecting_became_legal, this));
723 IO::connecting_became_legal ()
727 assert (pending_state_node);
729 connection_legal_c.disconnect ();
731 // it's not required for TracksLive, as long as TracksLive's session does all the connections when it's being loaded
732 if (!Profile->get_trx() ) {
733 ret = make_connections (*pending_state_node, pending_state_node_version, pending_state_node_in);
736 delete pending_state_node;
737 pending_state_node = 0;
742 boost::shared_ptr<Bundle>
743 IO::find_possible_bundle (const string &desired_name)
745 static const string digits = "0123456789";
746 const string &default_name = (_direction == Input ? _("in") : _("out"));
747 const string &bundle_type_name = (_direction == Input ? _("input") : _("output"));
749 boost::shared_ptr<Bundle> c = _session.bundle_by_name (desired_name);
752 int bundle_number, mask;
753 string possible_name;
755 string::size_type last_non_digit_pos;
756 std::string bundle_number_str;
758 error << string_compose(_("Unknown bundle \"%1\" listed for %2 of %3"), desired_name, bundle_type_name, _name)
761 // find numeric suffix of desired name
764 last_non_digit_pos = desired_name.find_last_not_of(digits);
766 if (last_non_digit_pos != string::npos) {
767 bundle_number_str = desired_name.substr(last_non_digit_pos);
768 bundle_number = string_to<int32_t>(bundle_number_str);
771 // see if it's a stereo connection e.g. "in 3+4"
773 if (last_non_digit_pos > 1 && desired_name[last_non_digit_pos] == '+') {
774 string::size_type left_last_non_digit_pos;
776 left_last_non_digit_pos = desired_name.find_last_not_of(digits, last_non_digit_pos-1);
778 if (left_last_non_digit_pos != string::npos) {
779 int left_bundle_number = 0;
780 bundle_number_str = desired_name.substr(left_last_non_digit_pos, last_non_digit_pos-1);
781 left_bundle_number = string_to<int32_t>(bundle_number_str);
783 if (left_bundle_number > 0 && left_bundle_number + 1 == bundle_number) {
794 // find highest set bit
796 while ((mask <= bundle_number) && (mask <<= 1)) {}
798 // "wrap" bundle number into largest possible power of 2
803 if (bundle_number & mask) {
804 bundle_number &= ~mask;
806 std::string possible_name = default_name + " " + to_string(bundle_number + 1);
809 possible_name += "+" + to_string(bundle_number + 2);
812 if ((c = _session.bundle_by_name (possible_name)) != 0) {
819 info << string_compose (_("Bundle %1 was not available - \"%2\" used instead"), desired_name, possible_name)
822 error << string_compose(_("No %1 bundles available as a replacement"), bundle_type_name)
833 IO::get_port_counts_2X (XMLNode const & node, int /*version*/, ChanCount& n, boost::shared_ptr<Bundle>& /*c*/)
835 XMLProperty const * prop;
836 XMLNodeList children = node.children ();
838 uint32_t n_audio = 0;
840 for (XMLNodeIterator i = children.begin(); i != children.end(); ++i) {
842 if ((prop = node.property ("inputs")) != 0 && _direction == Input) {
843 n_audio = count (prop->value().begin(), prop->value().end(), '{');
844 } else if ((prop = node.property ("input-connection")) != 0 && _direction == Input) {
846 } else if ((prop = node.property ("outputs")) != 0 && _direction == Output) {
847 n_audio = count (prop->value().begin(), prop->value().end(), '{');
848 } else if ((prop = node.property ("output-connection")) != 0 && _direction == Output) {
854 cnt.set_audio (n_audio);
855 n = ChanCount::max (n, cnt);
861 IO::get_port_counts (const XMLNode& node, int version, ChanCount& n, boost::shared_ptr<Bundle>& c)
863 if (version < 3000) {
864 return get_port_counts_2X (node, version, n, c);
867 XMLProperty const * prop;
868 XMLNodeConstIterator iter;
869 uint32_t n_audio = 0;
875 if ((prop = node.property ("connection")) != 0) {
877 if ((c = find_possible_bundle (prop->value())) != 0) {
878 n = ChanCount::max (n, c->nchannels());
883 for (iter = node.children().begin(); iter != node.children().end(); ++iter) {
885 if ((*iter)->name() == X_("Bundle")) {
886 prop = (*iter)->property ("name");
887 if ((c = find_possible_bundle (prop->value())) != 0) {
888 n = ChanCount::max (n, c->nchannels());
895 if ((*iter)->name() == X_("Port")) {
896 prop = (*iter)->property (X_("type"));
902 if (prop->value() == X_("audio")) {
903 cnt.set_audio (++n_audio);
904 } else if (prop->value() == X_("midi")) {
905 cnt.set_midi (++n_midi);
910 n = ChanCount::max (n, cnt);
915 IO::create_ports (const XMLNode& node, int version)
918 boost::shared_ptr<Bundle> c;
920 get_port_counts (node, version, n, c);
923 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
925 if (ensure_ports (n, true, this)) {
926 error << string_compose(_("%1: cannot create I/O ports"), _name) << endmsg;
937 IO::make_connections (const XMLNode& node, int version, bool in)
939 if (version < 3000) {
940 return make_connections_2X (node, version, in);
943 XMLProperty const * prop;
945 for (XMLNodeConstIterator i = node.children().begin(); i != node.children().end(); ++i) {
947 if ((*i)->name() == "Bundle") {
948 XMLProperty const * prop = (*i)->property ("name");
950 boost::shared_ptr<Bundle> b = find_possible_bundle (prop->value());
952 connect_ports_to_bundle (b, true, this);
959 if ((*i)->name() == "Port") {
961 prop = (*i)->property (X_("name"));
967 boost::shared_ptr<Port> p = port_by_name (prop->value());
970 for (XMLNodeConstIterator c = (*i)->children().begin(); c != (*i)->children().end(); ++c) {
972 XMLNode* cnode = (*c);
974 if (cnode->name() != X_("Connection")) {
978 if ((prop = cnode->property (X_("other"))) == 0) {
983 connect (p, prop->value(), this);
994 IO::prepare_for_reset (XMLNode& node, const std::string& name)
997 node.set_property ("name", name);
999 /* now find connections and reset the name of the port
1000 in one so that when we re-use it it will match
1001 the name of the thing we're applying it to.
1005 XMLNodeList children = node.children();
1007 for (XMLNodeIterator i = children.begin(); i != children.end(); ++i) {
1009 if ((*i)->name() == "Port") {
1011 prop = (*i)->property (X_("name"));
1015 string old = prop->value();
1016 string::size_type slash = old.find ('/');
1018 if (slash != string::npos) {
1019 /* port name is of form: <IO-name>/<port-name> */
1022 new_name += old.substr (old.find ('/'));
1024 prop->set_value (new_name);
1033 IO::make_connections_2X (const XMLNode& node, int /*version*/, bool in)
1035 XMLProperty const * prop;
1037 /* XXX: bundles ("connections" as was) */
1039 if ((prop = node.property ("inputs")) != 0 && in) {
1041 string::size_type ostart = 0;
1042 string::size_type start = 0;
1043 string::size_type end = 0;
1046 vector<string> ports;
1048 string const str = prop->value ();
1050 while ((start = str.find_first_of ('{', ostart)) != string::npos) {
1053 if ((end = str.find_first_of ('}', start)) == string::npos) {
1054 error << string_compose(_("IO: badly formed string in XML node for inputs \"%1\""), str) << endmsg;
1058 if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
1059 error << string_compose(_("bad input string in XML node \"%1\""), str) << endmsg;
1066 for (int x = 0; x < n; ++x) {
1067 /* XXX: this is a bit of a hack; need to check if it's always valid */
1068 string::size_type const p = ports[x].find ("/out");
1069 if (p != string::npos) {
1070 ports[x].replace (p, 4, "/audio_out");
1072 if (NULL != nth(i).get())
1073 nth(i)->connect (ports[x]);
1083 if ((prop = node.property ("outputs")) != 0 && !in) {
1085 string::size_type ostart = 0;
1086 string::size_type start = 0;
1087 string::size_type end = 0;
1090 vector<string> ports;
1092 string const str = prop->value ();
1094 while ((start = str.find_first_of ('{', ostart)) != string::npos) {
1097 if ((end = str.find_first_of ('}', start)) == string::npos) {
1098 error << string_compose(_("IO: badly formed string in XML node for outputs \"%1\""), str) << endmsg;
1102 if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
1103 error << string_compose(_("IO: bad output string in XML node \"%1\""), str) << endmsg;
1109 for (int x = 0; x < n; ++x) {
1110 /* XXX: this is a bit of a hack; need to check if it's always valid */
1111 string::size_type const p = ports[x].find ("/in");
1112 if (p != string::npos) {
1113 ports[x].replace (p, 3, "/audio_in");
1115 if (NULL != nth(i).get())
1116 nth(i)->connect (ports[x]);
1129 IO::set_ports (const string& str)
1131 vector<string> ports;
1135 if ((nports = count (str.begin(), str.end(), '{')) == 0) {
1140 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
1142 // FIXME: audio-only
1143 if (ensure_ports (ChanCount(DataType::AUDIO, nports), true, this)) {
1148 string::size_type start = 0;
1149 string::size_type end = 0;
1150 string::size_type ostart = 0;
1151 for (int i = 0; (start = str.find_first_of ('{', ostart)) != string::npos; ++i) {
1154 if ((end = str.find_first_of ('}', start)) == string::npos) {
1155 error << string_compose(_("IO: badly formed string in XML node for inputs \"%1\""), str) << endmsg;
1159 if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
1160 error << string_compose(_("bad input string in XML node \"%1\""), str) << endmsg;
1166 for (int x = 0; x < n; ++x) {
1167 connect (nth (i), ports[x], this);
1178 IO::parse_io_string (const string& str, vector<string>& ports)
1180 string::size_type pos, opos;
1182 if (str.length() == 0) {
1190 while ((pos = str.find_first_of (',', opos)) != string::npos) {
1191 ports.push_back (str.substr (opos, pos - opos));
1195 if (opos < str.length()) {
1196 ports.push_back (str.substr(opos));
1199 return ports.size();
1203 IO::parse_gain_string (const string& str, vector<string>& ports)
1205 string::size_type pos, opos;
1210 while ((pos = str.find_first_of (',', opos)) != string::npos) {
1211 ports.push_back (str.substr (opos, pos - opos));
1215 if (opos < str.length()) {
1216 ports.push_back (str.substr(opos));
1219 return ports.size();
1223 IO::set_name (const string& requested_name)
1225 string name = requested_name;
1227 if (_name == name) {
1231 /* replace all colons in the name. i wish we didn't have to do this */
1233 replace_all (name, ":", "-");
1235 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
1236 string current_name = i->name();
1237 current_name.replace (current_name.find (_name), _name.val().length(), name);
1238 i->set_name (current_name);
1241 bool const r = SessionObject::set_name (name);
1249 IO::set_pretty_name (const std::string& str)
1251 if (_pretty_name_prefix == str) {
1254 _pretty_name_prefix = str;
1255 apply_pretty_name ();
1259 IO::apply_pretty_name ()
1262 if (_pretty_name_prefix.empty ()) {
1265 for (PortSet::iterator i = _ports.begin (); i != _ports.end(); ++i, ++pn) {
1266 (*i)->set_pretty_name (string_compose (("%1/%2 %3"),
1267 _pretty_name_prefix,
1268 _direction == Output ? _("Out") : _("In"),
1274 IO::latency () const
1276 framecnt_t max_latency;
1281 /* io lock not taken - must be protected by other means */
1283 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1284 if ((latency = i->private_latency_range (_direction == Output).max) > max_latency) {
1285 DEBUG_TRACE (DEBUG::Latency, string_compose ("port %1 has %2 latency of %3 - use\n",
1287 ((_direction == Output) ? "PLAYBACK" : "CAPTURE"),
1289 max_latency = latency;
1293 DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: max %4 latency from %2 ports = %3\n",
1294 name(), _ports.num_ports(), max_latency,
1295 ((_direction == Output) ? "PLAYBACK" : "CAPTURE")));
1300 IO::connect_ports_to_bundle (boost::shared_ptr<Bundle> c, bool exclusive, void* src)
1302 BLOCK_PROCESS_CALLBACK ();
1305 Glib::Threads::Mutex::Lock lm2 (io_lock);
1308 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
1309 i->disconnect_all ();
1313 c->connect (_bundle, _session.engine());
1315 /* If this is a UserBundle, make a note of what we've done */
1317 boost::shared_ptr<UserBundle> ub = boost::dynamic_pointer_cast<UserBundle> (c);
1320 /* See if we already know about this one */
1321 std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin();
1322 while (i != _bundles_connected.end() && (*i)->bundle != ub) {
1326 if (i == _bundles_connected.end()) {
1327 /* We don't, so make a note */
1328 _bundles_connected.push_back (new UserBundleInfo (this, ub));
1333 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
1338 IO::disconnect_ports_from_bundle (boost::shared_ptr<Bundle> c, void* src)
1340 BLOCK_PROCESS_CALLBACK ();
1343 Glib::Threads::Mutex::Lock lm2 (io_lock);
1345 c->disconnect (_bundle, _session.engine());
1347 /* If this is a UserBundle, make a note of what we've done */
1349 boost::shared_ptr<UserBundle> ub = boost::dynamic_pointer_cast<UserBundle> (c);
1352 std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin();
1353 while (i != _bundles_connected.end() && (*i)->bundle != ub) {
1357 if (i != _bundles_connected.end()) {
1359 _bundles_connected.erase (i);
1364 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
1370 IO::disable_connecting ()
1372 connecting_legal = false;
1377 IO::enable_connecting ()
1379 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock());
1380 connecting_legal = true;
1381 boost::optional<int> r = ConnectingLegal ();
1382 return r.get_value_or (0);
1386 IO::bundle_changed (Bundle::Change /*c*/)
1389 // connect_input_ports_to_bundle (_input_bundle, this);
1394 IO::build_legal_port_name (DataType type)
1396 const int name_size = AudioEngine::instance()->port_name_size();
1400 if (type == DataType::AUDIO) {
1401 suffix = X_("audio");
1402 } else if (type == DataType::MIDI) {
1403 suffix = X_("midi");
1405 throw unknown_type();
1408 /* note that if "in" or "out" are translated it will break a session
1409 across locale switches because a port's connection list will
1410 show (old) translated names, but the current port name will
1411 use the (new) translated name.
1415 if (_direction == Input) {
1416 suffix += X_("_return");
1418 suffix += X_("_send");
1421 if (_direction == Input) {
1422 suffix += X_("_in");
1424 suffix += X_("_out");
1428 // allow up to 4 digits for the output port number, plus the slash, suffix and extra space
1430 limit = name_size - AudioEngine::instance()->my_name().length() - (suffix.length() + 5);
1432 std::vector<char> buf1(name_size+1);
1433 std::vector<char> buf2(name_size+1);
1435 /* colons are illegal in port names, so fix that */
1437 string nom = _name.val();
1438 replace_all (nom, ":", ";");
1440 snprintf (&buf1[0], name_size+1, ("%.*s/%s"), limit, nom.c_str(), suffix.c_str());
1442 int port_number = find_port_hole (&buf1[0]);
1443 snprintf (&buf2[0], name_size+1, "%s %d", &buf1[0], port_number);
1445 return string (&buf2[0]);
1449 IO::find_port_hole (const char* base)
1451 /* CALLER MUST HOLD IO LOCK */
1455 if (_ports.empty()) {
1459 /* we only allow up to 4 characters for the port number
1462 for (n = 1; n < 9999; ++n) {
1463 std::vector<char> buf (AudioEngine::instance()->port_name_size());
1464 PortSet::iterator i = _ports.begin();
1466 snprintf (&buf[0], buf.size()+1, _("%s %u"), base, n);
1468 for ( ; i != _ports.end(); ++i) {
1469 if (string(i->name()) == string(&buf[0])) {
1474 if (i == _ports.end()) {
1482 boost::shared_ptr<AudioPort>
1483 IO::audio(uint32_t n) const
1485 return _ports.nth_audio_port (n);
1489 boost::shared_ptr<MidiPort>
1490 IO::midi(uint32_t n) const
1492 return _ports.nth_midi_port (n);
1496 * Setup a bundle that describe our inputs or outputs. Also creates the bundle if necessary.
1505 _bundle.reset (new Bundle (_direction == Input));
1508 _bundle->suspend_signals ();
1510 _bundle->remove_channels ();
1512 if (_direction == Input) {
1513 snprintf(buf, sizeof (buf), _("%s in"), _name.val().c_str());
1515 snprintf(buf, sizeof (buf), _("%s out"), _name.val().c_str());
1517 _bundle->set_name (buf);
1520 for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
1522 uint32_t const N = _ports.count().get (*i);
1523 for (uint32_t j = 0; j < N; ++j) {
1524 _bundle->add_channel (bundle_channel_name (j, N, *i), *i);
1525 _bundle->set_port (c, _session.engine().make_port_name_non_relative (_ports.port(*i, j)->name()));
1531 _bundle->resume_signals ();
1534 /** @return Bundles connected to our ports */
1536 IO::bundles_connected ()
1541 for (std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) {
1542 bundles.push_back ((*i)->bundle);
1545 /* Session bundles */
1546 boost::shared_ptr<ARDOUR::BundleList> b = _session.bundles ();
1547 for (ARDOUR::BundleList::iterator i = b->begin(); i != b->end(); ++i) {
1548 if ((*i)->connected_to (_bundle, _session.engine())) {
1549 bundles.push_back (*i);
1555 boost::shared_ptr<ARDOUR::RouteList> r = _session.get_routes ();
1557 if (_direction == Input) {
1558 for (ARDOUR::RouteList::iterator i = r->begin(); i != r->end(); ++i) {
1559 if ((*i)->output()->bundle()->connected_to (_bundle, _session.engine())) {
1560 bundles.push_back ((*i)->output()->bundle());
1564 for (ARDOUR::RouteList::iterator i = r->begin(); i != r->end(); ++i) {
1565 if ((*i)->input()->bundle()->connected_to (_bundle, _session.engine())) {
1566 bundles.push_back ((*i)->input()->bundle());
1575 IO::UserBundleInfo::UserBundleInfo (IO* io, boost::shared_ptr<UserBundle> b)
1578 b->Changed.connect_same_thread (changed, boost::bind (&IO::bundle_changed, io, _1));
1582 IO::bundle_channel_name (uint32_t c, uint32_t n, DataType t) const
1586 if (t == DataType::AUDIO) {
1592 return c == 0 ? _("L") : _("R");
1594 snprintf (buf, sizeof(buf), "%d", (c + 1));
1600 snprintf (buf, sizeof(buf), "%d", (c + 1));
1609 IO::name_from_state (const XMLNode& node)
1611 XMLProperty const * prop;
1613 if ((prop = node.property ("name")) != 0) {
1614 return prop->value();
1621 IO::set_name_in_state (XMLNode& node, const string& new_name)
1623 node.set_property (X_("name"), new_name);
1624 XMLNodeList children = node.children ();
1625 for (XMLNodeIterator i = children.begin(); i != children.end(); ++i) {
1626 if ((*i)->name() == X_("Port")) {
1627 string const old_name = (*i)->property(X_("name"))->value();
1628 string const old_name_second_part = old_name.substr (old_name.find_first_of ("/") + 1);
1629 (*i)->set_property (X_("name"), string_compose ("%1/%2", new_name, old_name_second_part));
1635 IO::connected () const
1637 /* do we have any connections at all? */
1639 for (PortSet::const_iterator p = _ports.begin(); p != _ports.end(); ++p) {
1640 if (p->connected()) {
1649 IO::connected_to (boost::shared_ptr<const IO> other) const
1652 return connected ();
1655 assert (_direction != other->direction());
1658 uint32_t no = n_ports().n_total();
1659 uint32_t ni = other->n_ports ().n_total();
1661 for (i = 0; i < no; ++i) {
1662 for (j = 0; j < ni; ++j) {
1663 if ((NULL != nth(i).get()) && (NULL != other->nth(j).get())) {
1664 if (nth(i)->connected_to (other->nth(j)->name())) {
1675 IO::connected_to (const string& str) const
1677 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1678 if (i->connected_to (str)) {
1686 /** Call a processor's ::run() method, giving it our buffers
1687 * Caller must hold process lock.
1690 IO::process_input (boost::shared_ptr<Processor> proc, framepos_t start_frame, framepos_t end_frame, double speed, pframes_t nframes)
1692 /* don't read the data into new buffers - just use the port buffers directly */
1694 if (n_ports().n_total() == 0) {
1695 /* We have no ports, so nothing to process */
1699 _buffers.get_backend_port_addresses (_ports, nframes);
1701 proc->run (_buffers, start_frame, end_frame, speed, nframes, true);
1706 IO::collect_input (BufferSet& bufs, pframes_t nframes, ChanCount offset)
1708 assert(bufs.available() >= _ports.count());
1710 if (_ports.count() == ChanCount::ZERO) {
1714 bufs.set_count (_ports.count());
1716 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1717 PortSet::iterator i = _ports.begin(*t);
1718 BufferSet::iterator b = bufs.begin(*t);
1720 for (uint32_t off = 0; off < offset.get(*t); ++off, ++b) {
1721 if (b == bufs.end(*t)) {
1726 for ( ; i != _ports.end(*t); ++i, ++b) {
1727 const Buffer& bb (i->get_buffer (nframes));
1728 b->read_from (bb, nframes);
1734 IO::copy_to_outputs (BufferSet& bufs, DataType type, pframes_t nframes, framecnt_t offset)
1736 PortSet::iterator o = _ports.begin(type);
1737 BufferSet::iterator i = bufs.begin(type);
1738 BufferSet::iterator prev = i;
1740 assert(i != bufs.end(type)); // or second loop will crash
1742 // Copy any buffers 1:1 to outputs
1744 while (i != bufs.end(type) && o != _ports.end (type)) {
1745 Buffer& port_buffer (o->get_buffer (nframes));
1746 port_buffer.read_from (*i, nframes, offset);
1752 // Copy last buffer to any extra outputs
1754 while (o != _ports.end(type)) {
1755 Buffer& port_buffer (o->get_buffer (nframes));
1756 port_buffer.read_from (*prev, nframes, offset);
1761 boost::shared_ptr<Port>
1762 IO::port_by_name (const std::string& str) const
1764 /* to be called only from ::set_state() - no locking */
1766 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1768 if (i->name() == str) {
1769 return boost::const_pointer_cast<Port> (*i);
1773 return boost::shared_ptr<Port> ();
1777 IO::physically_connected () const
1779 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1780 if (i->physically_connected()) {
1789 IO::has_port (boost::shared_ptr<Port> p) const
1791 Glib::Threads::Mutex::Lock lm (io_lock);
1792 return _ports.contains (p);