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/types_convert.h"
36 #include "ardour/audioengine.h"
37 #include "ardour/buffer.h"
38 #include "ardour/buffer_set.h"
39 #include "ardour/debug.h"
40 #include "ardour/io.h"
41 #include "ardour/port.h"
42 #include "ardour/profile.h"
43 #include "ardour/route.h"
44 #include "ardour/session.h"
45 #include "ardour/types_convert.h"
46 #include "ardour/user_bundle.h"
50 #define BLOCK_PROCESS_CALLBACK() Glib::Threads::Mutex::Lock em (AudioEngine::instance()->process_lock())
53 using namespace ARDOUR;
56 const string IO::state_node_name = "IO";
57 bool IO::connecting_legal = false;
58 PBD::Signal0<int> IO::ConnectingLegal;
59 PBD::Signal1<void,ChanCount> IO::PortCountChanged;
61 /** @param default_type The type of port that will be created by ensure_io
62 * and friends if no type is explicitly requested (to avoid breakage).
64 IO::IO (Session& s, const string& name, Direction dir, DataType default_type, bool sendish)
65 : SessionObject (s, name)
67 , _default_type (default_type)
71 Port::PostDisconnect.connect_same_thread (*this, boost::bind (&IO::disconnect_check, this, _1, _2));
72 pending_state_node = 0;
76 IO::IO (Session& s, const XMLNode& node, DataType dt, bool sendish)
77 : SessionObject(s, "unnamed io")
83 pending_state_node = 0;
84 Port::PostDisconnect.connect_same_thread (*this, boost::bind (&IO::disconnect_check, this, _1, _2));
86 set_state (node, Stateful::loading_state_version);
92 Glib::Threads::Mutex::Lock lm (io_lock);
94 DEBUG_TRACE (DEBUG::Ports, string_compose ("IO %1 unregisters %2 ports\n", name(), _ports.num_ports()));
96 BLOCK_PROCESS_CALLBACK ();
98 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
99 _session.engine().unregister_port (*i);
101 delete pending_state_node; pending_state_node = 0;
105 IO::disconnect_check (boost::shared_ptr<Port> a, boost::shared_ptr<Port> b)
107 if (_session.state_of_the_state () & Session::Deletion) {
110 /* this could be called from within our own ::disconnect() method(s)
111 or from somewhere that operates directly on a port. so, we don't
112 know for sure if we can take this lock or not. if we fail,
113 we assume that its safely locked by our own ::disconnect().
116 Glib::Threads::Mutex::Lock tm (io_lock, Glib::Threads::TRY_LOCK);
119 /* we took the lock, so we cannot be here from inside
122 if (_ports.contains (a) || _ports.contains (b)) {
123 changed (IOChange (IOChange::ConnectionsChanged), this); /* EMIT SIGNAL */
126 /* we didn't get the lock, so assume that we're inside
127 * ::disconnect(), and it will call changed() appropriately.
133 IO::increment_port_buffer_offset (pframes_t offset)
135 /* io_lock, not taken: function must be called from Session::process() calltree */
137 if (_direction == Output) {
138 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
139 i->increment_port_buffer_offset (offset);
145 IO::silence (framecnt_t nframes)
147 /* io_lock, not taken: function must be called from Session::process() calltree */
149 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
150 i->get_buffer(nframes).silence (nframes);
154 /** Set _bundles_connected to those bundles that are connected such that every
155 * port on every bundle channel x is connected to port x in _ports.
158 IO::check_bundles_connected ()
160 std::vector<UserBundleInfo*> new_list;
162 for (std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) {
164 uint32_t const N = (*i)->bundle->nchannels().n_total();
166 if (_ports.num_ports() < N) {
172 for (uint32_t j = 0; j < N; ++j) {
173 /* Every port on bundle channel j must be connected to our input j */
174 Bundle::PortList const pl = (*i)->bundle->channel_ports (j);
175 for (uint32_t k = 0; k < pl.size(); ++k) {
176 if (_ports.port(j)->connected_to (pl[k]) == false) {
188 new_list.push_back (*i);
194 _bundles_connected = new_list;
199 IO::disconnect (boost::shared_ptr<Port> our_port, string other_port, void* src)
201 if (other_port.length() == 0 || our_port == 0) {
206 Glib::Threads::Mutex::Lock lm (io_lock);
208 /* check that our_port is really one of ours */
210 if ( ! _ports.contains(our_port)) {
214 /* disconnect it from the source */
216 if (our_port->disconnect (other_port)) {
217 error << string_compose(_("IO: cannot disconnect port %1 from %2"), our_port->name(), other_port) << endmsg;
221 check_bundles_connected ();
224 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
226 _session.set_dirty ();
232 IO::connect (boost::shared_ptr<Port> our_port, string other_port, void* src)
234 if (other_port.length() == 0 || our_port == 0) {
239 Glib::Threads::Mutex::Lock lm (io_lock);
241 /* check that our_port is really one of ours */
243 if ( ! _ports.contains(our_port) ) {
247 /* connect it to the source */
249 if (our_port->connect (other_port)) {
253 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
254 _session.set_dirty ();
259 IO::remove_port (boost::shared_ptr<Port> port, void* src)
261 ChanCount before = _ports.count ();
262 ChanCount after = before;
263 after.set (port->type(), after.get (port->type()) - 1);
265 boost::optional<bool> const r = PortCountChanging (after); /* EMIT SIGNAL */
266 if (r.get_value_or (false)) {
273 BLOCK_PROCESS_CALLBACK ();
276 Glib::Threads::Mutex::Lock lm (io_lock);
278 if (_ports.remove(port)) {
279 change.type = IOChange::Type (change.type | IOChange::ConfigurationChanged);
280 change.before = before;
281 change.after = _ports.count ();
283 if (port->connected()) {
284 change.type = IOChange::Type (change.type | IOChange::ConnectionsChanged);
287 _session.engine().unregister_port (port);
288 check_bundles_connected ();
292 PortCountChanged (n_ports()); /* EMIT SIGNAL */
294 if (change.type != IOChange::NoChange) {
295 changed (change, src);
296 _buffers.attach_buffers (_ports);
300 if (change.type & IOChange::ConfigurationChanged) {
304 if (change.type == IOChange::NoChange) {
308 _session.set_dirty ();
315 * @param destination Name of port to connect new port to.
316 * @param src Source for emitted ConfigurationChanged signal.
317 * @param type Data type of port. Default value (NIL) will use this IO's default type.
320 IO::add_port (string destination, void* src, DataType type)
322 boost::shared_ptr<Port> our_port;
324 if (type == DataType::NIL) {
325 type = _default_type;
328 ChanCount before = _ports.count ();
329 ChanCount after = before;
330 after.set (type, after.get (type) + 1);
332 bool const r = PortCountChanging (after); /* EMIT SIGNAL */
340 BLOCK_PROCESS_CALLBACK ();
344 Glib::Threads::Mutex::Lock lm (io_lock);
346 /* Create a new port */
348 string portname = build_legal_port_name (type);
350 if (_direction == Input) {
351 if ((our_port = _session.engine().register_input_port (type, portname)) == 0) {
352 error << string_compose(_("IO: cannot register input port %1"), portname) << endmsg;
356 if ((our_port = _session.engine().register_output_port (type, portname)) == 0) {
357 error << string_compose(_("IO: cannot register output port %1"), portname) << endmsg;
362 change.before = _ports.count ();
363 _ports.add (our_port);
366 PortCountChanged (n_ports()); /* EMIT SIGNAL */
367 change.type = IOChange::ConfigurationChanged;
368 change.after = _ports.count ();
369 changed (change, src); /* EMIT SIGNAL */
370 _buffers.attach_buffers (_ports);
373 if (!destination.empty()) {
374 if (our_port->connect (destination)) {
379 apply_pretty_name ();
381 _session.set_dirty ();
387 IO::disconnect (void* src)
390 Glib::Threads::Mutex::Lock lm (io_lock);
392 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
393 i->disconnect_all ();
396 check_bundles_connected ();
399 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
404 /** Caller must hold process lock */
406 IO::ensure_ports_locked (ChanCount count, bool clear, bool& changed)
408 #ifndef PLATFORM_WINDOWS
409 assert (!AudioEngine::instance()->process_lock().trylock());
412 boost::shared_ptr<Port> port;
413 vector<boost::shared_ptr<Port> > deleted_ports;
417 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
419 const size_t n = count.get(*t);
421 /* remove unused ports */
422 for (size_t i = n_ports().get(*t); i > n; --i) {
423 port = _ports.port(*t, i-1);
428 /* hold a reference to the port so that we can ensure
429 * that this thread, and not a JACK notification thread,
430 * holds the final reference.
433 deleted_ports.push_back (port);
434 _session.engine().unregister_port (port);
439 /* this will drop the final reference to the deleted ports,
440 * which will in turn call their destructors, which will in
441 * turn call the backend to unregister them.
443 * There will no connect/disconnect or register/unregister
444 * callbacks from the backend until we get here, because
445 * they are driven by the Port destructor. The destructor
446 * will not execute until we drop the final reference,
447 * which all happens right .... here.
449 deleted_ports.clear ();
451 /* create any necessary new ports */
452 while (n_ports().get(*t) < n) {
454 string portname = build_legal_port_name (*t);
458 if (_direction == Input) {
459 if ((port = _session.engine().register_input_port (*t, portname)) == 0) {
460 error << string_compose(_("IO: cannot register input port %1"), portname) << endmsg;
464 if ((port = _session.engine().register_output_port (*t, portname)) == 0) {
465 error << string_compose(_("IO: cannot register output port %1"), portname) << endmsg;
471 catch (AudioEngine::PortRegistrationFailure& err) {
482 check_bundles_connected ();
483 PortCountChanged (n_ports()); /* EMIT SIGNAL */
484 _session.set_dirty ();
488 /* disconnect all existing ports so that we get a fresh start */
489 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
490 i->disconnect_all ();
497 /** Caller must hold process lock */
499 IO::ensure_ports (ChanCount count, bool clear, void* src)
501 #ifndef PLATFORM_WINDOWS
502 assert (!AudioEngine::instance()->process_lock().trylock());
505 bool changed = false;
507 if (count == n_ports() && !clear) {
513 change.before = _ports.count ();
516 Glib::Threads::Mutex::Lock im (io_lock);
517 if (ensure_ports_locked (count, clear, changed)) {
523 change.after = _ports.count ();
524 change.type = IOChange::ConfigurationChanged;
525 this->changed (change, src); /* EMIT SIGNAL */
526 _buffers.attach_buffers (_ports);
528 _session.set_dirty ();
534 /** Caller must hold process lock */
536 IO::ensure_io (ChanCount count, bool clear, void* src)
538 #ifndef PLATFORM_WINDOWS
539 assert (!AudioEngine::instance()->process_lock().trylock());
542 return ensure_ports (count, clear, src);
552 IO::state (bool /*full_state*/)
554 XMLNode* node = new XMLNode (state_node_name);
556 Glib::Threads::Mutex::Lock lm (io_lock);
558 node->set_property ("name", name());
559 node->set_property ("id", id ());
560 node->set_property ("direction", _direction);
561 node->set_property ("default-type", _default_type);
563 if (!_pretty_name_prefix.empty ()) {
564 node->set_property("pretty-name", _pretty_name_prefix);
567 for (std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) {
568 XMLNode* n = new XMLNode ("Bundle");
569 n->set_property ("name", (*i)->bundle->name ());
570 node->add_child_nocopy (*n);
573 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
575 vector<string> connections;
577 XMLNode* pnode = new XMLNode (X_("Port"));
578 pnode->set_property (X_("type"), i->type());
579 pnode->set_property (X_("name"), i->name());
581 if (i->get_connections (connections)) {
582 vector<string>::const_iterator ci;
583 std::sort (connections.begin(), connections.end());
585 for (n = 0, ci = connections.begin(); ci != connections.end(); ++ci, ++n) {
587 /* if its a connection to our own port,
588 return only the port name, not the
589 whole thing. this allows connections
590 to be re-established even when our
591 client name is different.
594 XMLNode* cnode = new XMLNode (X_("Connection"));
596 cnode->set_property (X_("other"), _session.engine().make_port_name_relative (*ci));
597 pnode->add_child_nocopy (*cnode);
601 node->add_child_nocopy (*pnode);
604 node->set_property (X_("user-latency"), _user_latency);
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 /* force use of non-localized representation of decimal point,
619 since we use it a lot in XML files and so forth.
622 if (node.name() != state_node_name) {
623 error << string_compose(_("incorrect XML node \"%1\" passed to IO object"), node.name()) << endmsg;
627 bool ignore_name = node.property ("ignore-name");
629 if (node.get_property ("name", name) && !ignore_name) {
633 if (node.get_property (X_("default-type"), _default_type)) {
634 assert(_default_type != DataType::NIL);
639 node.get_property ("direction", _direction);
641 if (create_ports (node, version)) {
645 // after create_ports, updates names
646 if (node.get_property ("pretty-name", name)) {
647 set_pretty_name (name);
650 if (connecting_legal) {
652 if (make_connections (node, version, false)) {
658 delete pending_state_node;
659 pending_state_node = new XMLNode (node);
660 pending_state_node_version = version;
661 pending_state_node_in = false;
662 ConnectingLegal.connect_same_thread (connection_legal_c, boost::bind (&IO::connecting_became_legal, this));
665 node.get_property ("user-latency", _user_latency);
671 IO::set_state_2X (const XMLNode& node, int version, bool in)
673 XMLProperty const * prop;
674 XMLNodeConstIterator iter;
677 /* force use of non-localized representation of decimal point,
678 since we use it a lot in XML files and so forth.
681 if (node.name() != state_node_name) {
682 error << string_compose(_("incorrect XML node \"%1\" passed to IO object"), node.name()) << endmsg;
686 if ((prop = node.property ("name")) != 0) {
687 set_name (prop->value());
690 if ((prop = node.property (X_("default-type"))) != 0) {
691 _default_type = DataType(prop->value());
692 assert(_default_type != DataType::NIL);
697 _direction = in ? Input : Output;
699 if (create_ports (node, version)) {
703 if (connecting_legal) {
705 if (make_connections_2X (node, version, in)) {
711 delete pending_state_node;
712 pending_state_node = new XMLNode (node);
713 pending_state_node_version = version;
714 pending_state_node_in = in;
715 ConnectingLegal.connect_same_thread (connection_legal_c, boost::bind (&IO::connecting_became_legal, this));
722 IO::connecting_became_legal ()
726 assert (pending_state_node);
728 connection_legal_c.disconnect ();
730 // it's not required for TracksLive, as long as TracksLive's session does all the connections when it's being loaded
731 if (!Profile->get_trx() ) {
732 ret = make_connections (*pending_state_node, pending_state_node_version, pending_state_node_in);
735 delete pending_state_node;
736 pending_state_node = 0;
741 boost::shared_ptr<Bundle>
742 IO::find_possible_bundle (const string &desired_name)
744 static const string digits = "0123456789";
745 const string &default_name = (_direction == Input ? _("in") : _("out"));
746 const string &bundle_type_name = (_direction == Input ? _("input") : _("output"));
748 boost::shared_ptr<Bundle> c = _session.bundle_by_name (desired_name);
751 int bundle_number, mask;
752 string possible_name;
754 string::size_type last_non_digit_pos;
755 std::string bundle_number_str;
757 error << string_compose(_("Unknown bundle \"%1\" listed for %2 of %3"), desired_name, bundle_type_name, _name)
760 // find numeric suffix of desired name
763 last_non_digit_pos = desired_name.find_last_not_of(digits);
765 if (last_non_digit_pos != string::npos) {
766 bundle_number_str = desired_name.substr(last_non_digit_pos);
767 bundle_number = string_to<int32_t>(bundle_number_str);
770 // see if it's a stereo connection e.g. "in 3+4"
772 if (last_non_digit_pos > 1 && desired_name[last_non_digit_pos] == '+') {
773 string::size_type left_last_non_digit_pos;
775 left_last_non_digit_pos = desired_name.find_last_not_of(digits, last_non_digit_pos-1);
777 if (left_last_non_digit_pos != string::npos) {
778 int left_bundle_number = 0;
779 bundle_number_str = desired_name.substr(left_last_non_digit_pos, last_non_digit_pos-1);
780 left_bundle_number = string_to<int32_t>(bundle_number_str);
782 if (left_bundle_number > 0 && left_bundle_number + 1 == bundle_number) {
793 // find highest set bit
795 while ((mask <= bundle_number) && (mask <<= 1)) {}
797 // "wrap" bundle number into largest possible power of 2
802 if (bundle_number & mask) {
803 bundle_number &= ~mask;
805 std::string possible_name = default_name + " " + to_string(bundle_number + 1);
808 possible_name += "+" + to_string(bundle_number + 2);
811 if ((c = _session.bundle_by_name (possible_name)) != 0) {
818 info << string_compose (_("Bundle %1 was not available - \"%2\" used instead"), desired_name, possible_name)
821 error << string_compose(_("No %1 bundles available as a replacement"), bundle_type_name)
832 IO::get_port_counts_2X (XMLNode const & node, int /*version*/, ChanCount& n, boost::shared_ptr<Bundle>& /*c*/)
834 XMLProperty const * prop;
835 XMLNodeList children = node.children ();
837 uint32_t n_audio = 0;
839 for (XMLNodeIterator i = children.begin(); i != children.end(); ++i) {
841 if ((prop = node.property ("inputs")) != 0 && _direction == Input) {
842 n_audio = count (prop->value().begin(), prop->value().end(), '{');
843 } else if ((prop = node.property ("input-connection")) != 0 && _direction == Input) {
845 } else if ((prop = node.property ("outputs")) != 0 && _direction == Output) {
846 n_audio = count (prop->value().begin(), prop->value().end(), '{');
847 } else if ((prop = node.property ("output-connection")) != 0 && _direction == Output) {
853 cnt.set_audio (n_audio);
854 n = ChanCount::max (n, cnt);
860 IO::get_port_counts (const XMLNode& node, int version, ChanCount& n, boost::shared_ptr<Bundle>& c)
862 if (version < 3000) {
863 return get_port_counts_2X (node, version, n, c);
866 XMLProperty const * prop;
867 XMLNodeConstIterator iter;
868 uint32_t n_audio = 0;
874 if ((prop = node.property ("connection")) != 0) {
876 if ((c = find_possible_bundle (prop->value())) != 0) {
877 n = ChanCount::max (n, c->nchannels());
882 for (iter = node.children().begin(); iter != node.children().end(); ++iter) {
884 if ((*iter)->name() == X_("Bundle")) {
885 prop = (*iter)->property ("name");
886 if ((c = find_possible_bundle (prop->value())) != 0) {
887 n = ChanCount::max (n, c->nchannels());
894 if ((*iter)->name() == X_("Port")) {
895 prop = (*iter)->property (X_("type"));
901 if (prop->value() == X_("audio")) {
902 cnt.set_audio (++n_audio);
903 } else if (prop->value() == X_("midi")) {
904 cnt.set_midi (++n_midi);
909 n = ChanCount::max (n, cnt);
914 IO::create_ports (const XMLNode& node, int version)
917 boost::shared_ptr<Bundle> c;
919 get_port_counts (node, version, n, c);
922 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
924 if (ensure_ports (n, true, this)) {
925 error << string_compose(_("%1: cannot create I/O ports"), _name) << endmsg;
936 IO::make_connections (const XMLNode& node, int version, bool in)
938 if (version < 3000) {
939 return make_connections_2X (node, version, in);
942 XMLProperty const * prop;
944 for (XMLNodeConstIterator i = node.children().begin(); i != node.children().end(); ++i) {
946 if ((*i)->name() == "Bundle") {
947 XMLProperty const * prop = (*i)->property ("name");
949 boost::shared_ptr<Bundle> b = find_possible_bundle (prop->value());
951 connect_ports_to_bundle (b, true, this);
958 if ((*i)->name() == "Port") {
960 prop = (*i)->property (X_("name"));
966 boost::shared_ptr<Port> p = port_by_name (prop->value());
969 for (XMLNodeConstIterator c = (*i)->children().begin(); c != (*i)->children().end(); ++c) {
971 XMLNode* cnode = (*c);
973 if (cnode->name() != X_("Connection")) {
977 if ((prop = cnode->property (X_("other"))) == 0) {
982 connect (p, prop->value(), this);
993 IO::prepare_for_reset (XMLNode& node, const std::string& name)
996 node.set_property ("name", name);
998 /* now find connections and reset the name of the port
999 in one so that when we re-use it it will match
1000 the name of the thing we're applying it to.
1004 XMLNodeList children = node.children();
1006 for (XMLNodeIterator i = children.begin(); i != children.end(); ++i) {
1008 if ((*i)->name() == "Port") {
1010 prop = (*i)->property (X_("name"));
1014 string old = prop->value();
1015 string::size_type slash = old.find ('/');
1017 if (slash != string::npos) {
1018 /* port name is of form: <IO-name>/<port-name> */
1021 new_name += old.substr (old.find ('/'));
1023 prop->set_value (new_name);
1032 IO::make_connections_2X (const XMLNode& node, int /*version*/, bool in)
1034 XMLProperty const * prop;
1036 /* XXX: bundles ("connections" as was) */
1038 if ((prop = node.property ("inputs")) != 0 && in) {
1040 string::size_type ostart = 0;
1041 string::size_type start = 0;
1042 string::size_type end = 0;
1045 vector<string> ports;
1047 string const str = prop->value ();
1049 while ((start = str.find_first_of ('{', ostart)) != string::npos) {
1052 if ((end = str.find_first_of ('}', start)) == string::npos) {
1053 error << string_compose(_("IO: badly formed string in XML node for inputs \"%1\""), str) << endmsg;
1057 if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
1058 error << string_compose(_("bad input string in XML node \"%1\""), str) << endmsg;
1065 for (int x = 0; x < n; ++x) {
1066 /* XXX: this is a bit of a hack; need to check if it's always valid */
1067 string::size_type const p = ports[x].find ("/out");
1068 if (p != string::npos) {
1069 ports[x].replace (p, 4, "/audio_out");
1071 if (NULL != nth(i).get())
1072 nth(i)->connect (ports[x]);
1082 if ((prop = node.property ("outputs")) != 0 && !in) {
1084 string::size_type ostart = 0;
1085 string::size_type start = 0;
1086 string::size_type end = 0;
1089 vector<string> ports;
1091 string const str = prop->value ();
1093 while ((start = str.find_first_of ('{', ostart)) != string::npos) {
1096 if ((end = str.find_first_of ('}', start)) == string::npos) {
1097 error << string_compose(_("IO: badly formed string in XML node for outputs \"%1\""), str) << endmsg;
1101 if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
1102 error << string_compose(_("IO: bad output string in XML node \"%1\""), str) << endmsg;
1108 for (int x = 0; x < n; ++x) {
1109 /* XXX: this is a bit of a hack; need to check if it's always valid */
1110 string::size_type const p = ports[x].find ("/in");
1111 if (p != string::npos) {
1112 ports[x].replace (p, 3, "/audio_in");
1114 if (NULL != nth(i).get())
1115 nth(i)->connect (ports[x]);
1128 IO::set_ports (const string& str)
1130 vector<string> ports;
1134 if ((nports = count (str.begin(), str.end(), '{')) == 0) {
1139 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
1141 // FIXME: audio-only
1142 if (ensure_ports (ChanCount(DataType::AUDIO, nports), true, this)) {
1147 string::size_type start = 0;
1148 string::size_type end = 0;
1149 string::size_type ostart = 0;
1150 for (int i = 0; (start = str.find_first_of ('{', ostart)) != string::npos; ++i) {
1153 if ((end = str.find_first_of ('}', start)) == string::npos) {
1154 error << string_compose(_("IO: badly formed string in XML node for inputs \"%1\""), str) << endmsg;
1158 if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
1159 error << string_compose(_("bad input string in XML node \"%1\""), str) << endmsg;
1165 for (int x = 0; x < n; ++x) {
1166 connect (nth (i), ports[x], this);
1177 IO::parse_io_string (const string& str, vector<string>& ports)
1179 string::size_type pos, opos;
1181 if (str.length() == 0) {
1189 while ((pos = str.find_first_of (',', opos)) != string::npos) {
1190 ports.push_back (str.substr (opos, pos - opos));
1194 if (opos < str.length()) {
1195 ports.push_back (str.substr(opos));
1198 return ports.size();
1202 IO::parse_gain_string (const string& str, vector<string>& ports)
1204 string::size_type pos, opos;
1209 while ((pos = str.find_first_of (',', opos)) != string::npos) {
1210 ports.push_back (str.substr (opos, pos - opos));
1214 if (opos < str.length()) {
1215 ports.push_back (str.substr(opos));
1218 return ports.size();
1222 IO::set_name (const string& requested_name)
1224 string name = requested_name;
1226 if (_name == name) {
1230 /* replace all colons in the name. i wish we didn't have to do this */
1232 replace_all (name, ":", "-");
1234 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
1235 string current_name = i->name();
1236 current_name.replace (current_name.find (_name), _name.val().length(), name);
1237 i->set_name (current_name);
1240 bool const r = SessionObject::set_name (name);
1248 IO::set_pretty_name (const std::string& str)
1250 if (_pretty_name_prefix == str) {
1253 _pretty_name_prefix = str;
1254 apply_pretty_name ();
1258 IO::apply_pretty_name ()
1261 if (_pretty_name_prefix.empty ()) {
1264 for (PortSet::iterator i = _ports.begin (); i != _ports.end(); ++i, ++pn) {
1265 (*i)->set_pretty_name (string_compose (("%1/%2 %3"),
1266 _pretty_name_prefix,
1267 _direction == Output ? _("Out") : _("In"),
1273 IO::latency () const
1275 framecnt_t max_latency;
1280 /* io lock not taken - must be protected by other means */
1282 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1283 if ((latency = i->private_latency_range (_direction == Output).max) > max_latency) {
1284 DEBUG_TRACE (DEBUG::Latency, string_compose ("port %1 has %2 latency of %3 - use\n",
1286 ((_direction == Output) ? "PLAYBACK" : "CAPTURE"),
1288 max_latency = latency;
1292 DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: max %4 latency from %2 ports = %3\n",
1293 name(), _ports.num_ports(), max_latency,
1294 ((_direction == Output) ? "PLAYBACK" : "CAPTURE")));
1299 IO::connect_ports_to_bundle (boost::shared_ptr<Bundle> c, bool exclusive, void* src)
1301 BLOCK_PROCESS_CALLBACK ();
1304 Glib::Threads::Mutex::Lock lm2 (io_lock);
1307 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
1308 i->disconnect_all ();
1312 c->connect (_bundle, _session.engine());
1314 /* If this is a UserBundle, make a note of what we've done */
1316 boost::shared_ptr<UserBundle> ub = boost::dynamic_pointer_cast<UserBundle> (c);
1319 /* See if we already know about this one */
1320 std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin();
1321 while (i != _bundles_connected.end() && (*i)->bundle != ub) {
1325 if (i == _bundles_connected.end()) {
1326 /* We don't, so make a note */
1327 _bundles_connected.push_back (new UserBundleInfo (this, ub));
1332 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
1337 IO::disconnect_ports_from_bundle (boost::shared_ptr<Bundle> c, void* src)
1339 BLOCK_PROCESS_CALLBACK ();
1342 Glib::Threads::Mutex::Lock lm2 (io_lock);
1344 c->disconnect (_bundle, _session.engine());
1346 /* If this is a UserBundle, make a note of what we've done */
1348 boost::shared_ptr<UserBundle> ub = boost::dynamic_pointer_cast<UserBundle> (c);
1351 std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin();
1352 while (i != _bundles_connected.end() && (*i)->bundle != ub) {
1356 if (i != _bundles_connected.end()) {
1358 _bundles_connected.erase (i);
1363 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
1369 IO::disable_connecting ()
1371 connecting_legal = false;
1376 IO::enable_connecting ()
1378 Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock());
1379 connecting_legal = true;
1380 boost::optional<int> r = ConnectingLegal ();
1381 return r.get_value_or (0);
1385 IO::bundle_changed (Bundle::Change /*c*/)
1388 // connect_input_ports_to_bundle (_input_bundle, this);
1393 IO::build_legal_port_name (DataType type)
1395 const int name_size = AudioEngine::instance()->port_name_size();
1399 if (type == DataType::AUDIO) {
1400 suffix = X_("audio");
1401 } else if (type == DataType::MIDI) {
1402 suffix = X_("midi");
1404 throw unknown_type();
1407 /* note that if "in" or "out" are translated it will break a session
1408 across locale switches because a port's connection list will
1409 show (old) translated names, but the current port name will
1410 use the (new) translated name.
1414 if (_direction == Input) {
1415 suffix += X_("_return");
1417 suffix += X_("_send");
1420 if (_direction == Input) {
1421 suffix += X_("_in");
1423 suffix += X_("_out");
1427 // allow up to 4 digits for the output port number, plus the slash, suffix and extra space
1429 limit = name_size - AudioEngine::instance()->my_name().length() - (suffix.length() + 5);
1431 std::vector<char> buf1(name_size+1);
1432 std::vector<char> buf2(name_size+1);
1434 /* colons are illegal in port names, so fix that */
1436 string nom = _name.val();
1437 replace_all (nom, ":", ";");
1439 snprintf (&buf1[0], name_size+1, ("%.*s/%s"), limit, nom.c_str(), suffix.c_str());
1441 int port_number = find_port_hole (&buf1[0]);
1442 snprintf (&buf2[0], name_size+1, "%s %d", &buf1[0], port_number);
1444 return string (&buf2[0]);
1448 IO::find_port_hole (const char* base)
1450 /* CALLER MUST HOLD IO LOCK */
1454 if (_ports.empty()) {
1458 /* we only allow up to 4 characters for the port number
1461 for (n = 1; n < 9999; ++n) {
1462 std::vector<char> buf (AudioEngine::instance()->port_name_size());
1463 PortSet::iterator i = _ports.begin();
1465 snprintf (&buf[0], buf.size()+1, _("%s %u"), base, n);
1467 for ( ; i != _ports.end(); ++i) {
1468 if (string(i->name()) == string(&buf[0])) {
1473 if (i == _ports.end()) {
1481 boost::shared_ptr<AudioPort>
1482 IO::audio(uint32_t n) const
1484 return _ports.nth_audio_port (n);
1488 boost::shared_ptr<MidiPort>
1489 IO::midi(uint32_t n) const
1491 return _ports.nth_midi_port (n);
1495 * Setup a bundle that describe our inputs or outputs. Also creates the bundle if necessary.
1504 _bundle.reset (new Bundle (_direction == Input));
1507 _bundle->suspend_signals ();
1509 _bundle->remove_channels ();
1511 if (_direction == Input) {
1512 snprintf(buf, sizeof (buf), _("%s in"), _name.val().c_str());
1514 snprintf(buf, sizeof (buf), _("%s out"), _name.val().c_str());
1516 _bundle->set_name (buf);
1519 for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
1521 uint32_t const N = _ports.count().get (*i);
1522 for (uint32_t j = 0; j < N; ++j) {
1523 _bundle->add_channel (bundle_channel_name (j, N, *i), *i);
1524 _bundle->set_port (c, _session.engine().make_port_name_non_relative (_ports.port(*i, j)->name()));
1530 _bundle->resume_signals ();
1533 /** @return Bundles connected to our ports */
1535 IO::bundles_connected ()
1540 for (std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) {
1541 bundles.push_back ((*i)->bundle);
1544 /* Session bundles */
1545 boost::shared_ptr<ARDOUR::BundleList> b = _session.bundles ();
1546 for (ARDOUR::BundleList::iterator i = b->begin(); i != b->end(); ++i) {
1547 if ((*i)->connected_to (_bundle, _session.engine())) {
1548 bundles.push_back (*i);
1554 boost::shared_ptr<ARDOUR::RouteList> r = _session.get_routes ();
1556 if (_direction == Input) {
1557 for (ARDOUR::RouteList::iterator i = r->begin(); i != r->end(); ++i) {
1558 if ((*i)->output()->bundle()->connected_to (_bundle, _session.engine())) {
1559 bundles.push_back ((*i)->output()->bundle());
1563 for (ARDOUR::RouteList::iterator i = r->begin(); i != r->end(); ++i) {
1564 if ((*i)->input()->bundle()->connected_to (_bundle, _session.engine())) {
1565 bundles.push_back ((*i)->input()->bundle());
1574 IO::UserBundleInfo::UserBundleInfo (IO* io, boost::shared_ptr<UserBundle> b)
1577 b->Changed.connect_same_thread (changed, boost::bind (&IO::bundle_changed, io, _1));
1581 IO::bundle_channel_name (uint32_t c, uint32_t n, DataType t) const
1585 if (t == DataType::AUDIO) {
1591 return c == 0 ? _("L") : _("R");
1593 snprintf (buf, sizeof(buf), "%d", (c + 1));
1599 snprintf (buf, sizeof(buf), "%d", (c + 1));
1608 IO::name_from_state (const XMLNode& node)
1610 XMLProperty const * prop;
1612 if ((prop = node.property ("name")) != 0) {
1613 return prop->value();
1620 IO::set_name_in_state (XMLNode& node, const string& new_name)
1622 node.set_property (X_("name"), new_name);
1623 XMLNodeList children = node.children ();
1624 for (XMLNodeIterator i = children.begin(); i != children.end(); ++i) {
1625 if ((*i)->name() == X_("Port")) {
1626 string const old_name = (*i)->property(X_("name"))->value();
1627 string const old_name_second_part = old_name.substr (old_name.find_first_of ("/") + 1);
1628 (*i)->set_property (X_("name"), string_compose ("%1/%2", new_name, old_name_second_part));
1634 IO::connected () const
1636 /* do we have any connections at all? */
1638 for (PortSet::const_iterator p = _ports.begin(); p != _ports.end(); ++p) {
1639 if (p->connected()) {
1648 IO::connected_to (boost::shared_ptr<const IO> other) const
1651 return connected ();
1654 assert (_direction != other->direction());
1657 uint32_t no = n_ports().n_total();
1658 uint32_t ni = other->n_ports ().n_total();
1660 for (i = 0; i < no; ++i) {
1661 for (j = 0; j < ni; ++j) {
1662 if ((NULL != nth(i).get()) && (NULL != other->nth(j).get())) {
1663 if (nth(i)->connected_to (other->nth(j)->name())) {
1674 IO::connected_to (const string& str) const
1676 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1677 if (i->connected_to (str)) {
1685 /** Call a processor's ::run() method, giving it our buffers
1686 * Caller must hold process lock.
1689 IO::process_input (boost::shared_ptr<Processor> proc, framepos_t start_frame, framepos_t end_frame, double speed, pframes_t nframes)
1691 /* don't read the data into new buffers - just use the port buffers directly */
1693 if (n_ports().n_total() == 0) {
1694 /* We have no ports, so nothing to process */
1698 _buffers.get_backend_port_addresses (_ports, nframes);
1700 proc->run (_buffers, start_frame, end_frame, speed, nframes, true);
1705 IO::collect_input (BufferSet& bufs, pframes_t nframes, ChanCount offset)
1707 assert(bufs.available() >= _ports.count());
1709 if (_ports.count() == ChanCount::ZERO) {
1713 bufs.set_count (_ports.count());
1715 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1716 PortSet::iterator i = _ports.begin(*t);
1717 BufferSet::iterator b = bufs.begin(*t);
1719 for (uint32_t off = 0; off < offset.get(*t); ++off, ++b) {
1720 if (b == bufs.end(*t)) {
1725 for ( ; i != _ports.end(*t); ++i, ++b) {
1726 const Buffer& bb (i->get_buffer (nframes));
1727 b->read_from (bb, nframes);
1733 IO::copy_to_outputs (BufferSet& bufs, DataType type, pframes_t nframes, framecnt_t offset)
1735 PortSet::iterator o = _ports.begin(type);
1736 BufferSet::iterator i = bufs.begin(type);
1737 BufferSet::iterator prev = i;
1739 assert(i != bufs.end(type)); // or second loop will crash
1741 // Copy any buffers 1:1 to outputs
1743 while (i != bufs.end(type) && o != _ports.end (type)) {
1744 Buffer& port_buffer (o->get_buffer (nframes));
1745 port_buffer.read_from (*i, nframes, offset);
1751 // Copy last buffer to any extra outputs
1753 while (o != _ports.end(type)) {
1754 Buffer& port_buffer (o->get_buffer (nframes));
1755 port_buffer.read_from (*prev, nframes, offset);
1760 boost::shared_ptr<Port>
1761 IO::port_by_name (const std::string& str) const
1763 /* to be called only from ::set_state() - no locking */
1765 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1767 if (i->name() == str) {
1768 return boost::const_pointer_cast<Port> (*i);
1772 return boost::shared_ptr<Port> ();
1776 IO::physically_connected () const
1778 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1779 if (i->physically_connected()) {
1788 IO::has_port (boost::shared_ptr<Port> p) const
1790 Glib::Threads::Mutex::Lock lm (io_lock);
1791 return _ports.contains (p);