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.
27 #include <glibmm/thread.h>
29 #include "pbd/xml++.h"
30 #include "pbd/replace_all.h"
31 #include "pbd/unknown_type.h"
32 #include "pbd/enumwriter.h"
34 #include "ardour/audioengine.h"
35 #include "ardour/buffer.h"
36 #include "ardour/io.h"
37 #include "ardour/route.h"
38 #include "ardour/port.h"
39 #include "ardour/audio_port.h"
40 #include "ardour/midi_port.h"
41 #include "ardour/session.h"
42 #include "ardour/cycle_timer.h"
43 #include "ardour/buffer_set.h"
44 #include "ardour/meter.h"
45 #include "ardour/amp.h"
46 #include "ardour/user_bundle.h"
53 A bug in OS X's cmath that causes isnan() and isinf() to be
54 "undeclared". the following works around that
57 #if defined(__APPLE__) && defined(__MACH__)
58 extern "C" int isnan (double);
59 extern "C" int isinf (double);
62 #define BLOCK_PROCESS_CALLBACK() Glib::Mutex::Lock em (_session.engine().process_lock())
65 using namespace ARDOUR;
68 const string IO::state_node_name = "IO";
69 bool IO::connecting_legal = false;
70 PBD::Signal0<int> IO::ConnectingLegal;
71 PBD::Signal1<void,ChanCount> IO::PortCountChanged;
73 /** @param default_type The type of port that will be created by ensure_io
74 * and friends if no type is explicitly requested (to avoid breakage).
76 IO::IO (Session& s, const string& name, Direction dir, DataType default_type)
77 : SessionObject (s, name)
79 , _default_type (default_type)
82 pending_state_node = 0;
86 IO::IO (Session& s, const XMLNode& node, DataType dt)
87 : SessionObject(s, "unnamed io")
92 pending_state_node = 0;
94 set_state (node, Stateful::loading_state_version);
100 Glib::Mutex::Lock lm (io_lock);
102 BLOCK_PROCESS_CALLBACK ();
104 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
105 _session.engine().unregister_port (*i);
110 IO::silence (nframes_t nframes)
112 /* io_lock, not taken: function must be called from Session::process() calltree */
114 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
115 i->get_buffer(nframes).silence (nframes);
120 IO::check_bundles_connected ()
122 check_bundles (_bundles_connected, ports());
126 IO::check_bundles (std::vector<UserBundleInfo*>& list, const PortSet& ports)
128 std::vector<UserBundleInfo*> new_list;
130 for (std::vector<UserBundleInfo*>::iterator i = list.begin(); i != list.end(); ++i) {
132 uint32_t const N = (*i)->bundle->nchannels ();
134 if (_ports.num_ports (default_type()) < N) {
140 for (uint32_t j = 0; j < N; ++j) {
141 /* Every port on bundle channel j must be connected to our input j */
142 Bundle::PortList const pl = (*i)->bundle->channel_ports (j);
143 for (uint32_t k = 0; k < pl.size(); ++k) {
144 if (ports.port(j)->connected_to (pl[k]) == false) {
156 new_list.push_back (*i);
167 IO::disconnect (Port* our_port, string other_port, void* src)
169 if (other_port.length() == 0 || our_port == 0) {
174 BLOCK_PROCESS_CALLBACK ();
177 Glib::Mutex::Lock lm (io_lock);
179 /* check that our_port is really one of ours */
181 if ( ! _ports.contains(our_port)) {
185 /* disconnect it from the source */
187 if (our_port->disconnect (other_port)) {
188 error << string_compose(_("IO: cannot disconnect port %1 from %2"), our_port->name(), other_port) << endmsg;
192 check_bundles_connected ();
196 changed (ConnectionsChanged, src); /* EMIT SIGNAL */
197 _session.set_dirty ();
203 IO::connect (Port* our_port, string other_port, void* src)
205 if (other_port.length() == 0 || our_port == 0) {
210 BLOCK_PROCESS_CALLBACK ();
213 Glib::Mutex::Lock lm (io_lock);
215 /* check that our_port is really one of ours */
217 if ( ! _ports.contains(our_port) ) {
221 /* connect it to the source */
223 if (our_port->connect (other_port)) {
229 changed (ConnectionsChanged, src); /* EMIT SIGNAL */
230 _session.set_dirty ();
235 IO::remove_port (Port* port, void* src)
237 IOChange change (NoChange);
240 BLOCK_PROCESS_CALLBACK ();
244 Glib::Mutex::Lock lm (io_lock);
246 if (_ports.remove(port)) {
247 change = IOChange (change|ConfigurationChanged);
249 if (port->connected()) {
250 change = IOChange (change|ConnectionsChanged);
253 _session.engine().unregister_port (*port);
254 check_bundles_connected ();
258 PortCountChanged (n_ports()); /* EMIT SIGNAL */
261 if (change & ConfigurationChanged) {
265 if (change != NoChange) {
266 changed (change, src);
267 _session.set_dirty ();
274 /** Add an output port.
276 * @param destination Name of input port to connect new port to.
277 * @param src Source for emitted ConfigurationChanged signal.
278 * @param type Data type of port. Default value (NIL) will use this IO's default type.
281 IO::add_port (string destination, void* src, DataType type)
285 if (type == DataType::NIL) {
286 type = _default_type;
290 BLOCK_PROCESS_CALLBACK ();
294 Glib::Mutex::Lock lm (io_lock);
296 /* Create a new output port */
298 string portname = build_legal_port_name (type);
300 if (_direction == Input) {
301 if ((our_port = _session.engine().register_input_port (type, portname)) == 0) {
302 error << string_compose(_("IO: cannot register input port %1"), portname) << endmsg;
306 if ((our_port = _session.engine().register_output_port (type, portname)) == 0) {
307 error << string_compose(_("IO: cannot register output port %1"), portname) << endmsg;
312 _ports.add (our_port);
315 PortCountChanged (n_ports()); /* EMIT SIGNAL */
318 if (destination.length()) {
319 if (our_port->connect (destination)) {
324 // pan_changed (src); /* EMIT SIGNAL */
325 changed (ConfigurationChanged, src); /* EMIT SIGNAL */
327 _session.set_dirty ();
333 IO::disconnect (void* src)
336 BLOCK_PROCESS_CALLBACK ();
339 Glib::Mutex::Lock lm (io_lock);
341 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
342 i->disconnect_all ();
345 check_bundles_connected ();
349 changed (ConnectionsChanged, src); /* EMIT SIGNAL */
355 IO::ensure_ports_locked (ChanCount count, bool clear, void* /*src*/)
358 bool changed = false;
360 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
362 const size_t n = count.get(*t);
364 /* remove unused ports */
365 for (size_t i = n_ports().get(*t); i > n; --i) {
366 port = _ports.port(*t, i-1);
370 _session.engine().unregister_port (*port);
375 /* create any necessary new ports */
376 while (n_ports().get(*t) < n) {
378 string portname = build_legal_port_name (*t);
382 if (_direction == Input) {
383 if ((port = _session.engine().register_input_port (*t, portname)) == 0) {
384 error << string_compose(_("IO: cannot register input port %1"), portname) << endmsg;
388 if ((port = _session.engine().register_output_port (*t, portname)) == 0) {
389 error << string_compose(_("IO: cannot register output port %1"), portname) << endmsg;
395 catch (AudioEngine::PortRegistrationFailure& err) {
397 throw AudioEngine::PortRegistrationFailure();
406 check_bundles_connected ();
407 PortCountChanged (n_ports()); /* EMIT SIGNAL */
408 _session.set_dirty ();
412 /* disconnect all existing ports so that we get a fresh start */
413 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
414 i->disconnect_all ();
423 IO::ensure_ports (ChanCount count, bool clear, bool lockit, void* src)
425 bool changed = false;
427 if (count == n_ports() && !clear) {
432 BLOCK_PROCESS_CALLBACK ();
433 Glib::Mutex::Lock im (io_lock);
434 changed = ensure_ports_locked (count, clear, src);
436 changed = ensure_ports_locked (count, clear, src);
440 this->changed (ConfigurationChanged, src); /* EMIT SIGNAL */
442 _session.set_dirty ();
449 IO::ensure_io (ChanCount count, bool clear, void* src)
451 return ensure_ports (count, clear, true, src);
461 IO::state (bool /*full_state*/)
463 XMLNode* node = new XMLNode (state_node_name);
466 vector<string>::iterator ci;
468 LocaleGuard lg (X_("POSIX"));
469 Glib::Mutex::Lock lm (io_lock);
471 node->add_property("name", _name);
472 id().print (buf, sizeof (buf));
473 node->add_property("id", buf);
474 node->add_property ("direction", enum_2_string (_direction));
475 node->add_property ("default-type", _default_type.to_string());
477 for (std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) {
478 XMLNode* n = new XMLNode ("Bundle");
479 n->add_property ("name", (*i)->bundle->name ());
480 node->add_child_nocopy (*n);
483 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
485 vector<string> connections;
487 XMLNode* pnode = new XMLNode (X_("Port"));
488 pnode->add_property (X_("type"), i->type().to_string());
489 pnode->add_property (X_("name"), i->name());
491 if (i->get_connections (connections)) {
493 for (n = 0, ci = connections.begin(); ci != connections.end(); ++ci, ++n) {
495 /* if its a connection to our own port,
496 return only the port name, not the
497 whole thing. this allows connections
498 to be re-established even when our
499 client name is different.
502 XMLNode* cnode = new XMLNode (X_("Connection"));
504 cnode->add_property (X_("other"), _session.engine().make_port_name_relative (*ci));
505 pnode->add_child_nocopy (*cnode);
509 node->add_child_nocopy (*pnode);
516 IO::set_state (const XMLNode& node, int version)
518 /* callers for version < 3000 need to call set_state_2X directly, as A3 IOs
519 * are input OR output, not both, so the direction needs to be specified
522 assert (version >= 3000);
524 const XMLProperty* prop;
525 XMLNodeConstIterator iter;
526 LocaleGuard lg (X_("POSIX"));
528 /* force use of non-localized representation of decimal point,
529 since we use it a lot in XML files and so forth.
532 if (node.name() != state_node_name) {
533 error << string_compose(_("incorrect XML node \"%1\" passed to IO object"), node.name()) << endmsg;
537 if ((prop = node.property ("name")) != 0) {
538 set_name (prop->value());
541 if ((prop = node.property (X_("default-type"))) != 0) {
542 _default_type = DataType(prop->value());
543 assert(_default_type != DataType::NIL);
546 if ((prop = node.property ("id")) != 0) {
547 _id = prop->value ();
550 if ((prop = node.property ("direction")) != 0) {
551 _direction = (Direction) string_2_enum (prop->value(), _direction);
554 if (create_ports (node, version)) {
558 if (connecting_legal) {
560 if (make_connections (node, version, false)) {
566 pending_state_node = new XMLNode (node);
567 pending_state_node_version = version;
568 pending_state_node_in = false;
569 ConnectingLegal.connect_same_thread (connection_legal_c, boost::bind (&IO::connecting_became_legal, this));
577 IO::set_state_2X (const XMLNode& node, int version, bool in)
579 const XMLProperty* prop;
580 XMLNodeConstIterator iter;
581 LocaleGuard lg (X_("POSIX"));
583 /* force use of non-localized representation of decimal point,
584 since we use it a lot in XML files and so forth.
587 if (node.name() != state_node_name) {
588 error << string_compose(_("incorrect XML node \"%1\" passed to IO object"), node.name()) << endmsg;
592 if ((prop = node.property ("name")) != 0) {
593 set_name (prop->value());
596 if ((prop = node.property (X_("default-type"))) != 0) {
597 _default_type = DataType(prop->value());
598 assert(_default_type != DataType::NIL);
601 if ((prop = node.property ("id")) != 0) {
602 _id = prop->value ();
605 _direction = in ? Input : Output;
607 if (create_ports (node, version)) {
611 if (connecting_legal) {
613 if (make_connections_2X (node, version, in)) {
619 pending_state_node = new XMLNode (node);
620 pending_state_node_version = version;
621 pending_state_node_in = in;
622 ConnectingLegal.connect_same_thread (connection_legal_c, boost::bind (&IO::connecting_became_legal, this));
629 IO::connecting_became_legal ()
633 assert (pending_state_node);
635 connection_legal_c.disconnect ();
637 ret = make_connections (*pending_state_node, pending_state_node_version, pending_state_node_in);
639 delete pending_state_node;
640 pending_state_node = 0;
645 boost::shared_ptr<Bundle>
646 IO::find_possible_bundle (const string &desired_name)
648 static const string digits = "0123456789";
649 const string &default_name = (_direction == Input ? _("in") : _("out"));
650 const string &bundle_type_name = (_direction == Input ? _("input") : _("output"));
652 boost::shared_ptr<Bundle> c = _session.bundle_by_name (desired_name);
655 int bundle_number, mask;
656 string possible_name;
658 string::size_type last_non_digit_pos;
660 error << string_compose(_("Unknown bundle \"%1\" listed for %2 of %3"), desired_name, bundle_type_name, _name)
663 // find numeric suffix of desired name
666 last_non_digit_pos = desired_name.find_last_not_of(digits);
668 if (last_non_digit_pos != string::npos) {
670 s << desired_name.substr(last_non_digit_pos);
674 // see if it's a stereo connection e.g. "in 3+4"
676 if (last_non_digit_pos > 1 && desired_name[last_non_digit_pos] == '+') {
677 int left_bundle_number = 0;
678 string::size_type left_last_non_digit_pos;
680 left_last_non_digit_pos = desired_name.find_last_not_of(digits, last_non_digit_pos-1);
682 if (left_last_non_digit_pos != string::npos) {
684 s << desired_name.substr(left_last_non_digit_pos, last_non_digit_pos-1);
685 s >> left_bundle_number;
687 if (left_bundle_number > 0 && left_bundle_number + 1 == bundle_number) {
698 // find highest set bit
700 while ((mask <= bundle_number) && (mask <<= 1)) {}
702 // "wrap" bundle number into largest possible power of 2
707 if (bundle_number & mask) {
708 bundle_number &= ~mask;
711 s << default_name << " " << bundle_number + 1;
714 s << "+" << bundle_number + 2;
717 possible_name = s.str();
719 if ((c = _session.bundle_by_name (possible_name)) != 0) {
726 info << string_compose (_("Bundle %1 was not available - \"%2\" used instead"), desired_name, possible_name)
729 error << string_compose(_("No %1 bundles available as a replacement"), bundle_type_name)
740 IO::get_port_counts_2X (XMLNode const & node, int /*version*/, ChanCount& n, boost::shared_ptr<Bundle>& /*c*/)
742 XMLProperty const * prop;
743 XMLNodeList children = node.children ();
745 uint32_t n_audio = 0;
747 for (XMLNodeIterator i = children.begin(); i != children.end(); ++i) {
749 if ((prop = node.property ("inputs")) != 0 && _direction == Input) {
750 n_audio = count (prop->value().begin(), prop->value().end(), '{');
751 } else if ((prop = node.property ("outputs")) != 0 && _direction == Output) {
752 n_audio = count (prop->value().begin(), prop->value().end(), '{');
757 cnt.set_audio (n_audio);
758 n = ChanCount::max (n, cnt);
764 IO::get_port_counts (const XMLNode& node, int version, ChanCount& n, boost::shared_ptr<Bundle>& c)
766 if (version < 3000) {
767 return get_port_counts_2X (node, version, n, c);
770 XMLProperty const * prop;
771 XMLNodeConstIterator iter;
772 uint32_t n_audio = 0;
778 if ((prop = node.property ("connection")) != 0) {
780 if ((c = find_possible_bundle (prop->value())) != 0) {
781 n = ChanCount::max (n, ChanCount(c->type(), c->nchannels()));
786 for (iter = node.children().begin(); iter != node.children().end(); ++iter) {
788 if ((*iter)->name() == X_("Bundle")) {
789 if ((c = find_possible_bundle (prop->value())) != 0) {
790 n = ChanCount::max (n, ChanCount(c->type(), c->nchannels()));
797 if ((*iter)->name() == X_("Port")) {
798 prop = (*iter)->property (X_("type"));
804 if (prop->value() == X_("audio")) {
805 cnt.set_audio (++n_audio);
806 } else if (prop->value() == X_("midi")) {
807 cnt.set_midi (++n_midi);
812 n = ChanCount::max (n, cnt);
817 IO::create_ports (const XMLNode& node, int version)
820 boost::shared_ptr<Bundle> c;
822 get_port_counts (node, version, n, c);
824 if (ensure_ports (n, true, true, this)) {
825 error << string_compose(_("%1: cannot create I/O ports"), _name) << endmsg;
835 IO::make_connections (const XMLNode& node, int version, bool in)
837 if (version < 3000) {
838 return make_connections_2X (node, version, in);
841 const XMLProperty* prop;
843 for (XMLNodeConstIterator i = node.children().begin(); i != node.children().end(); ++i) {
845 if ((*i)->name() == "Bundle") {
846 XMLProperty const * prop = (*i)->property ("name");
848 boost::shared_ptr<Bundle> b = find_possible_bundle (prop->value());
850 connect_ports_to_bundle (b, this);
857 if ((*i)->name() == "Port") {
859 prop = (*i)->property (X_("name"));
865 Port* p = port_by_name (prop->value());
868 for (XMLNodeConstIterator c = (*i)->children().begin(); c != (*i)->children().end(); ++c) {
870 XMLNode* cnode = (*c);
872 if (cnode->name() != X_("Connection")) {
876 if ((prop = cnode->property (X_("other"))) == 0) {
881 p->connect (prop->value());
893 IO::make_connections_2X (const XMLNode& node, int /*version*/, bool in)
895 const XMLProperty* prop;
897 /* XXX: bundles ("connections" as was) */
899 if ((prop = node.property ("inputs")) != 0 && in) {
901 string::size_type ostart = 0;
902 string::size_type start = 0;
903 string::size_type end = 0;
906 vector<string> ports;
908 string const str = prop->value ();
910 while ((start = str.find_first_of ('{', ostart)) != string::npos) {
913 if ((end = str.find_first_of ('}', start)) == string::npos) {
914 error << string_compose(_("IO: badly formed string in XML node for inputs \"%1\""), str) << endmsg;
918 if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
919 error << string_compose(_("bad input string in XML node \"%1\""), str) << endmsg;
926 for (int x = 0; x < n; ++x) {
927 /* XXX: this is a bit of a hack; need to check if it's always valid */
928 string::size_type const p = ports[x].find ("/out");
929 if (p != string::npos) {
930 ports[x].replace (p, 4, "/audio_out");
932 nth(i)->connect (ports[x]);
942 if ((prop = node.property ("outputs")) != 0 && !in) {
944 string::size_type ostart = 0;
945 string::size_type start = 0;
946 string::size_type end = 0;
949 vector<string> ports;
951 string const str = prop->value ();
953 while ((start = str.find_first_of ('{', ostart)) != string::npos) {
956 if ((end = str.find_first_of ('}', start)) == string::npos) {
957 error << string_compose(_("IO: badly formed string in XML node for outputs \"%1\""), str) << endmsg;
961 if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
962 error << string_compose(_("IO: bad output string in XML node \"%1\""), str) << endmsg;
968 for (int x = 0; x < n; ++x) {
969 /* XXX: this is a bit of a hack; need to check if it's always valid */
970 string::size_type const p = ports[x].find ("/in");
971 if (p != string::npos) {
972 ports[x].replace (p, 3, "/audio_in");
974 nth(i)->connect (ports[x]);
987 IO::set_ports (const string& str)
989 vector<string> ports;
994 if ((nports = count (str.begin(), str.end(), '{')) == 0) {
999 if (ensure_ports (ChanCount(DataType::AUDIO, nports), true, true, this)) {
1003 string::size_type start, end, ostart;
1010 while ((start = str.find_first_of ('{', ostart)) != string::npos) {
1013 if ((end = str.find_first_of ('}', start)) == string::npos) {
1014 error << string_compose(_("IO: badly formed string in XML node for inputs \"%1\""), str) << endmsg;
1018 if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
1019 error << string_compose(_("bad input string in XML node \"%1\""), str) << endmsg;
1025 for (int x = 0; x < n; ++x) {
1026 connect (nth (i), ports[x], this);
1038 IO::parse_io_string (const string& str, vector<string>& ports)
1040 string::size_type pos, opos;
1042 if (str.length() == 0) {
1051 while ((pos = str.find_first_of (',', opos)) != string::npos) {
1052 ports.push_back (str.substr (opos, pos - opos));
1056 if (opos < str.length()) {
1057 ports.push_back (str.substr(opos));
1060 return ports.size();
1064 IO::parse_gain_string (const string& str, vector<string>& ports)
1066 string::size_type pos, opos;
1072 while ((pos = str.find_first_of (',', opos)) != string::npos) {
1073 ports.push_back (str.substr (opos, pos - opos));
1077 if (opos < str.length()) {
1078 ports.push_back (str.substr(opos));
1081 return ports.size();
1085 IO::set_name (const string& requested_name)
1087 string name = requested_name;
1089 if (_name == name) {
1093 /* replace all colons in the name. i wish we didn't have to do this */
1095 if (replace_all (name, ":", "-")) {
1096 warning << _("you cannot use colons to name objects with I/O connections") << endmsg;
1099 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
1100 string current_name = i->name();
1101 current_name.replace (current_name.find (_name), _name.val().length(), name);
1102 i->set_name (current_name);
1105 bool const r = SessionObject::set_name (name);
1113 IO::set_port_latency (nframes_t nframes)
1115 Glib::Mutex::Lock lm (io_lock);
1117 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
1118 i->set_latency (nframes);
1123 IO::latency () const
1125 nframes_t max_latency;
1130 /* io lock not taken - must be protected by other means */
1132 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1133 if ((latency = i->total_latency ()) > max_latency) {
1134 max_latency = latency;
1142 IO::update_port_total_latencies ()
1144 /* io_lock, not taken: function must be called from Session::process() calltree */
1146 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
1147 _session.engine().update_total_latency (*i);
1152 IO::connect_ports_to_bundle (boost::shared_ptr<Bundle> c, void* src)
1155 BLOCK_PROCESS_CALLBACK ();
1156 Glib::Mutex::Lock lm2 (io_lock);
1158 c->connect (_bundle, _session.engine());
1160 /* If this is a UserBundle, make a note of what we've done */
1162 boost::shared_ptr<UserBundle> ub = boost::dynamic_pointer_cast<UserBundle> (c);
1165 /* See if we already know about this one */
1166 std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin();
1167 while (i != _bundles_connected.end() && (*i)->bundle != ub) {
1171 if (i == _bundles_connected.end()) {
1172 /* We don't, so make a note */
1173 _bundles_connected.push_back (new UserBundleInfo (this, ub));
1178 changed (IOChange (ConfigurationChanged|ConnectionsChanged), src); /* EMIT SIGNAL */
1183 IO::disconnect_ports_from_bundle (boost::shared_ptr<Bundle> c, void* src)
1186 BLOCK_PROCESS_CALLBACK ();
1187 Glib::Mutex::Lock lm2 (io_lock);
1189 c->disconnect (_bundle, _session.engine());
1191 /* If this is a UserBundle, make a note of what we've done */
1193 boost::shared_ptr<UserBundle> ub = boost::dynamic_pointer_cast<UserBundle> (c);
1196 std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin();
1197 while (i != _bundles_connected.end() && (*i)->bundle != ub) {
1201 if (i != _bundles_connected.end()) {
1203 _bundles_connected.erase (i);
1208 changed (IOChange (ConfigurationChanged|ConnectionsChanged), src); /* EMIT SIGNAL */
1214 IO::disable_connecting ()
1216 connecting_legal = false;
1221 IO::enable_connecting ()
1223 connecting_legal = true;
1224 boost::optional<int> r = ConnectingLegal ();
1225 return r.get_value_or (0);
1229 IO::bundle_changed (Bundle::Change /*c*/)
1232 // connect_input_ports_to_bundle (_input_bundle, this);
1237 IO::build_legal_port_name (DataType type)
1239 const int name_size = jack_port_name_size();
1243 if (type == DataType::AUDIO) {
1244 suffix = _("audio");
1245 } else if (type == DataType::MIDI) {
1248 throw unknown_type();
1251 /* note that if "in" or "out" are translated it will break a session
1252 across locale switches because a port's connection list will
1253 show (old) translated names, but the current port name will
1254 use the (new) translated name.
1257 if (_direction == Input) {
1258 suffix += X_("_in");
1260 suffix += X_("_out");
1263 // allow up to 4 digits for the output port number, plus the slash, suffix and extra space
1265 limit = name_size - _session.engine().client_name().length() - (suffix.length() + 5);
1267 char buf1[name_size+1];
1268 char buf2[name_size+1];
1270 snprintf (buf1, name_size+1, ("%.*s/%s"), limit, _name.val().c_str(), suffix.c_str());
1272 int port_number = find_port_hole (buf1);
1273 snprintf (buf2, name_size+1, "%s %d", buf1, port_number);
1275 return string (buf2);
1279 IO::find_port_hole (const char* base)
1281 /* CALLER MUST HOLD IO LOCK */
1285 if (_ports.empty()) {
1289 /* we only allow up to 4 characters for the port number
1292 for (n = 1; n < 9999; ++n) {
1293 char buf[jack_port_name_size()];
1294 PortSet::iterator i = _ports.begin();
1296 snprintf (buf, jack_port_name_size(), _("%s %u"), base, n);
1298 for ( ; i != _ports.end(); ++i) {
1299 if (i->name() == buf) {
1304 if (i == _ports.end()) {
1313 IO::audio(uint32_t n) const
1315 return _ports.nth_audio_port (n);
1320 IO::midi(uint32_t n) const
1322 return _ports.nth_midi_port (n);
1326 * Setup a bundle that describe our inputs or outputs. Also creates the bundle if necessary.
1335 _bundle.reset (new Bundle (_direction == Input));
1338 _bundle->suspend_signals ();
1340 _bundle->set_type (default_type ());
1342 _bundle->remove_channels ();
1344 if (_direction == Input) {
1345 snprintf(buf, sizeof (buf), _("%s in"), _name.val().c_str());
1347 snprintf(buf, sizeof (buf), _("%s out"), _name.val().c_str());
1349 _bundle->set_name (buf);
1350 uint32_t const ni = _ports.num_ports();
1351 for (uint32_t i = 0; i < ni; ++i) {
1352 _bundle->add_channel (bundle_channel_name (i, ni));
1353 _bundle->set_port (i, _session.engine().make_port_name_non_relative (_ports.port(i)->name()));
1356 _bundle->resume_signals ();
1359 /** @return Bundles connected to our ports */
1361 IO::bundles_connected ()
1366 for (std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) {
1367 bundles.push_back ((*i)->bundle);
1370 /* Session bundles */
1371 boost::shared_ptr<ARDOUR::BundleList> b = _session.bundles ();
1372 for (ARDOUR::BundleList::iterator i = b->begin(); i != b->end(); ++i) {
1373 if ((*i)->connected_to (_bundle, _session.engine())) {
1374 bundles.push_back (*i);
1380 boost::shared_ptr<ARDOUR::RouteList> r = _session.get_routes ();
1382 if (_direction == Input) {
1383 for (ARDOUR::RouteList::iterator i = r->begin(); i != r->end(); ++i) {
1384 if ((*i)->output()->bundle()->connected_to (_bundle, _session.engine())) {
1385 bundles.push_back ((*i)->output()->bundle());
1389 for (ARDOUR::RouteList::iterator i = r->begin(); i != r->end(); ++i) {
1390 if ((*i)->input()->bundle()->connected_to (_bundle, _session.engine())) {
1391 bundles.push_back ((*i)->input()->bundle());
1400 IO::UserBundleInfo::UserBundleInfo (IO* io, boost::shared_ptr<UserBundle> b)
1403 b->Changed.connect_same_thread (changed, boost::bind (&IO::bundle_changed, io, _1));
1407 IO::bundle_channel_name (uint32_t c, uint32_t n) const
1415 return c == 0 ? _("L") : _("R");
1417 snprintf (buf, sizeof(buf), _("%d"), (c + 1));
1425 IO::name_from_state (const XMLNode& node)
1427 const XMLProperty* prop;
1429 if ((prop = node.property ("name")) != 0) {
1430 return prop->value();
1437 IO::set_name_in_state (XMLNode& node, const string& new_name)
1439 const XMLProperty* prop;
1441 if ((prop = node.property ("name")) != 0) {
1442 node.add_property ("name", new_name);
1447 IO::connected_to (boost::shared_ptr<const IO> other) const
1450 /* do we have any connections at all? */
1452 for (PortSet::const_iterator p = _ports.begin(); p != _ports.end(); ++p) {
1453 if (p->connected()) {
1461 assert (_direction != other->direction());
1464 uint32_t no = n_ports().n_total();
1465 uint32_t ni = other->n_ports ().n_total();
1467 for (i = 0; i < no; ++i) {
1468 for (j = 0; j < ni; ++j) {
1469 if (nth(i)->connected_to (other->nth(j)->name())) {
1479 IO::process_input (boost::shared_ptr<Processor> proc, sframes_t start_frame, sframes_t end_frame, nframes_t nframes)
1483 /* don't read the data into new buffers - just use the port buffers directly */
1485 bufs.attach_buffers (_ports, nframes, 0);
1486 proc->run (bufs, start_frame, end_frame, nframes, true);
1490 IO::collect_input (BufferSet& bufs, nframes_t nframes, ChanCount offset)
1492 assert(bufs.available() >= _ports.count());
1494 if (_ports.count() == ChanCount::ZERO) {
1498 bufs.set_count (_ports.count());
1500 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1501 PortSet::iterator i = _ports.begin(*t);
1502 BufferSet::iterator b = bufs.begin(*t);
1504 for (uint32_t off = 0; off < offset.get(*t); ++off, ++b) {
1505 if (b == bufs.end(*t)) {
1510 for ( ; i != _ports.end(*t); ++i, ++b) {
1511 Buffer& bb (i->get_buffer (nframes));
1512 b->read_from (bb, nframes);
1518 IO::copy_to_outputs (BufferSet& bufs, DataType type, nframes_t nframes, nframes_t offset)
1520 // Copy any buffers 1:1 to outputs
1522 PortSet::iterator o = _ports.begin(type);
1523 BufferSet::iterator i = bufs.begin(type);
1524 BufferSet::iterator prev = i;
1526 while (i != bufs.end(type) && o != _ports.end (type)) {
1527 Buffer& port_buffer (o->get_buffer (nframes));
1528 port_buffer.read_from (*i, nframes, offset);
1534 // Copy last buffer to any extra outputs
1536 while (o != _ports.end(type)) {
1537 Buffer& port_buffer (o->get_buffer (nframes));
1538 port_buffer.read_from (*prev, nframes, offset);
1544 IO::port_by_name (const std::string& str) const
1546 /* to be called only from ::set_state() - no locking */
1548 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1552 if (p.name() == str) {
1553 return const_cast<Port*>(&p);