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/thread.h>
30 #include "pbd/xml++.h"
31 #include "pbd/replace_all.h"
32 #include "pbd/unknown_type.h"
33 #include "pbd/enumwriter.h"
35 #include "ardour/audioengine.h"
36 #include "ardour/buffer.h"
37 #include "ardour/debug.h"
38 #include "ardour/io.h"
39 #include "ardour/route.h"
40 #include "ardour/port.h"
41 #include "ardour/audio_port.h"
42 #include "ardour/midi_port.h"
43 #include "ardour/session.h"
44 #include "ardour/cycle_timer.h"
45 #include "ardour/buffer_set.h"
46 #include "ardour/meter.h"
47 #include "ardour/amp.h"
48 #include "ardour/user_bundle.h"
52 #define BLOCK_PROCESS_CALLBACK() Glib::Mutex::Lock em (AudioEngine::instance()->process_lock())
55 using namespace ARDOUR;
58 const string IO::state_node_name = "IO";
59 bool IO::connecting_legal = false;
60 PBD::Signal0<int> IO::ConnectingLegal;
61 PBD::Signal1<void,ChanCount> IO::PortCountChanged;
63 /** @param default_type The type of port that will be created by ensure_io
64 * and friends if no type is explicitly requested (to avoid breakage).
66 IO::IO (Session& s, const string& name, Direction dir, DataType default_type)
67 : SessionObject (s, name)
69 , _default_type (default_type)
72 pending_state_node = 0;
76 IO::IO (Session& s, const XMLNode& node, DataType dt)
77 : SessionObject(s, "unnamed io")
82 pending_state_node = 0;
84 set_state (node, Stateful::loading_state_version);
90 Glib::Mutex::Lock lm (io_lock);
92 BLOCK_PROCESS_CALLBACK ();
94 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
95 _session.engine().unregister_port (*i);
100 IO::increment_port_buffer_offset (pframes_t offset)
102 /* io_lock, not taken: function must be called from Session::process() calltree */
104 if (_direction == Output) {
105 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
106 i->increment_port_buffer_offset (offset);
112 IO::silence (framecnt_t nframes)
114 /* io_lock, not taken: function must be called from Session::process() calltree */
116 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
117 i->get_buffer(nframes).silence (nframes);
121 /** Set _bundles_connected to those bundles that are connected such that every
122 * port on every bundle channel x is connected to port x in _ports.
125 IO::check_bundles_connected ()
127 std::vector<UserBundleInfo*> new_list;
129 for (std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) {
131 uint32_t const N = (*i)->bundle->nchannels().n_total();
133 if (_ports.num_ports() < N) {
139 for (uint32_t j = 0; j < N; ++j) {
140 /* Every port on bundle channel j must be connected to our input j */
141 Bundle::PortList const pl = (*i)->bundle->channel_ports (j);
142 for (uint32_t k = 0; k < pl.size(); ++k) {
143 if (_ports.port(j)->connected_to (pl[k]) == false) {
155 new_list.push_back (*i);
161 _bundles_connected = new_list;
166 IO::disconnect (Port* our_port, string other_port, void* src)
168 if (other_port.length() == 0 || our_port == 0) {
173 Glib::Mutex::Lock lm (io_lock);
175 /* check that our_port is really one of ours */
177 if ( ! _ports.contains(our_port)) {
181 /* disconnect it from the source */
183 if (our_port->disconnect (other_port)) {
184 error << string_compose(_("IO: cannot disconnect port %1 from %2"), our_port->name(), other_port) << endmsg;
188 check_bundles_connected ();
191 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
193 _session.set_dirty ();
199 IO::connect (Port* our_port, string other_port, void* src)
201 if (other_port.length() == 0 || our_port == 0) {
206 Glib::Mutex::Lock lm (io_lock);
208 /* check that our_port is really one of ours */
210 if ( ! _ports.contains(our_port) ) {
214 /* connect it to the source */
216 if (our_port->connect (other_port)) {
220 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
221 _session.set_dirty ();
226 IO::remove_port (Port* port, void* src)
228 ChanCount before = _ports.count ();
229 ChanCount after = before;
230 after.set (port->type(), after.get (port->type()) - 1);
232 bool const r = PortCountChanging (after); /* EMIT SIGNAL */
240 BLOCK_PROCESS_CALLBACK ();
243 Glib::Mutex::Lock lm (io_lock);
245 if (_ports.remove(port)) {
246 change.type = IOChange::Type (change.type | IOChange::ConfigurationChanged);
247 change.before = before;
248 change.after = _ports.count ();
250 if (port->connected()) {
251 change.type = IOChange::Type (change.type | IOChange::ConnectionsChanged);
254 _session.engine().unregister_port (*port);
255 check_bundles_connected ();
259 PortCountChanged (n_ports()); /* EMIT SIGNAL */
261 if (change.type != IOChange::NoChange) {
262 changed (change, src);
263 _buffers.attach_buffers (_ports);
267 if (change.type & IOChange::ConfigurationChanged) {
271 if (change.type == IOChange::NoChange) {
275 _session.set_dirty ();
282 * @param destination Name of port to connect new port to.
283 * @param src Source for emitted ConfigurationChanged signal.
284 * @param type Data type of port. Default value (NIL) will use this IO's default type.
287 IO::add_port (string destination, void* src, DataType type)
291 if (type == DataType::NIL) {
292 type = _default_type;
298 BLOCK_PROCESS_CALLBACK ();
302 Glib::Mutex::Lock lm (io_lock);
304 /* Create a new port */
306 string portname = build_legal_port_name (type);
308 if (_direction == Input) {
309 if ((our_port = _session.engine().register_input_port (type, portname)) == 0) {
310 error << string_compose(_("IO: cannot register input port %1"), portname) << endmsg;
314 if ((our_port = _session.engine().register_output_port (type, portname)) == 0) {
315 error << string_compose(_("IO: cannot register output port %1"), portname) << endmsg;
320 change.before = _ports.count ();
321 _ports.add (our_port);
324 PortCountChanged (n_ports()); /* EMIT SIGNAL */
325 change.type = IOChange::ConfigurationChanged;
326 change.after = _ports.count ();
327 changed (change, src); /* EMIT SIGNAL */
328 _buffers.attach_buffers (_ports);
331 if (!destination.empty()) {
332 if (our_port->connect (destination)) {
338 _session.set_dirty ();
344 IO::disconnect (void* src)
347 Glib::Mutex::Lock lm (io_lock);
349 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
350 i->disconnect_all ();
353 check_bundles_connected ();
356 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
361 /** Caller must hold process lock */
363 IO::ensure_ports_locked (ChanCount count, bool clear, bool& changed)
365 assert (!AudioEngine::instance()->process_lock().trylock());
371 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
373 const size_t n = count.get(*t);
375 /* remove unused ports */
376 for (size_t i = n_ports().get(*t); i > n; --i) {
377 port = _ports.port(*t, i-1);
381 _session.engine().unregister_port (*port);
386 /* create any necessary new ports */
387 while (n_ports().get(*t) < n) {
389 string portname = build_legal_port_name (*t);
393 if (_direction == Input) {
394 if ((port = _session.engine().register_input_port (*t, portname)) == 0) {
395 error << string_compose(_("IO: cannot register input port %1"), portname) << endmsg;
399 if ((port = _session.engine().register_output_port (*t, portname)) == 0) {
400 error << string_compose(_("IO: cannot register output port %1"), portname) << endmsg;
406 catch (AudioEngine::PortRegistrationFailure& err) {
417 check_bundles_connected ();
418 PortCountChanged (n_ports()); /* EMIT SIGNAL */
419 _session.set_dirty ();
423 /* disconnect all existing ports so that we get a fresh start */
424 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
425 i->disconnect_all ();
432 /** Caller must hold process lock */
434 IO::ensure_ports (ChanCount count, bool clear, void* src)
436 assert (!AudioEngine::instance()->process_lock().trylock());
438 bool changed = false;
440 if (count == n_ports() && !clear) {
446 change.before = _ports.count ();
449 Glib::Mutex::Lock im (io_lock);
450 if (ensure_ports_locked (count, clear, changed)) {
456 change.after = _ports.count ();
457 change.type = IOChange::ConfigurationChanged;
458 this->changed (change, src); /* EMIT SIGNAL */
459 _buffers.attach_buffers (_ports);
461 _session.set_dirty ();
467 /** Caller must hold process lock */
469 IO::ensure_io (ChanCount count, bool clear, void* src)
471 assert (!AudioEngine::instance()->process_lock().trylock());
473 return ensure_ports (count, clear, src);
483 IO::state (bool /*full_state*/)
485 XMLNode* node = new XMLNode (state_node_name);
488 vector<string>::iterator ci;
490 LocaleGuard lg (X_("POSIX"));
491 Glib::Mutex::Lock lm (io_lock);
493 node->add_property("name", _name);
494 id().print (buf, sizeof (buf));
495 node->add_property("id", buf);
496 node->add_property ("direction", enum_2_string (_direction));
497 node->add_property ("default-type", _default_type.to_string());
499 for (std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) {
500 XMLNode* n = new XMLNode ("Bundle");
501 n->add_property ("name", (*i)->bundle->name ());
502 node->add_child_nocopy (*n);
505 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
507 vector<string> connections;
509 XMLNode* pnode = new XMLNode (X_("Port"));
510 pnode->add_property (X_("type"), i->type().to_string());
511 pnode->add_property (X_("name"), i->name());
513 if (i->get_connections (connections)) {
515 for (n = 0, ci = connections.begin(); ci != connections.end(); ++ci, ++n) {
517 /* if its a connection to our own port,
518 return only the port name, not the
519 whole thing. this allows connections
520 to be re-established even when our
521 client name is different.
524 XMLNode* cnode = new XMLNode (X_("Connection"));
526 cnode->add_property (X_("other"), _session.engine().make_port_name_relative (*ci));
527 pnode->add_child_nocopy (*cnode);
531 node->add_child_nocopy (*pnode);
534 snprintf (buf, sizeof (buf), "%" PRId64, _user_latency);
535 node->add_property (X_("user-latency"), buf);
541 IO::set_state (const XMLNode& node, int version)
543 /* callers for version < 3000 need to call set_state_2X directly, as A3 IOs
544 * are input OR output, not both, so the direction needs to be specified
547 assert (version >= 3000);
549 const XMLProperty* prop;
550 XMLNodeConstIterator iter;
551 LocaleGuard lg (X_("POSIX"));
553 /* force use of non-localized representation of decimal point,
554 since we use it a lot in XML files and so forth.
557 if (node.name() != state_node_name) {
558 error << string_compose(_("incorrect XML node \"%1\" passed to IO object"), node.name()) << endmsg;
562 if ((prop = node.property ("name")) != 0) {
563 set_name (prop->value());
566 if ((prop = node.property (X_("default-type"))) != 0) {
567 _default_type = DataType(prop->value());
568 assert(_default_type != DataType::NIL);
573 if ((prop = node.property ("direction")) != 0) {
574 _direction = (Direction) string_2_enum (prop->value(), _direction);
577 if (create_ports (node, version)) {
581 if (connecting_legal) {
583 if (make_connections (node, version, false)) {
589 pending_state_node = new XMLNode (node);
590 pending_state_node_version = version;
591 pending_state_node_in = false;
592 ConnectingLegal.connect_same_thread (connection_legal_c, boost::bind (&IO::connecting_became_legal, this));
595 if ((prop = node.property ("user-latency")) != 0) {
596 _user_latency = atoi (prop->value ());
603 IO::set_state_2X (const XMLNode& node, int version, bool in)
605 const XMLProperty* prop;
606 XMLNodeConstIterator iter;
607 LocaleGuard lg (X_("POSIX"));
609 /* force use of non-localized representation of decimal point,
610 since we use it a lot in XML files and so forth.
613 if (node.name() != state_node_name) {
614 error << string_compose(_("incorrect XML node \"%1\" passed to IO object"), node.name()) << endmsg;
618 if ((prop = node.property ("name")) != 0) {
619 set_name (prop->value());
622 if ((prop = node.property (X_("default-type"))) != 0) {
623 _default_type = DataType(prop->value());
624 assert(_default_type != DataType::NIL);
629 _direction = in ? Input : Output;
631 if (create_ports (node, version)) {
635 if (connecting_legal) {
637 if (make_connections_2X (node, version, in)) {
643 pending_state_node = new XMLNode (node);
644 pending_state_node_version = version;
645 pending_state_node_in = in;
646 ConnectingLegal.connect_same_thread (connection_legal_c, boost::bind (&IO::connecting_became_legal, this));
653 IO::connecting_became_legal ()
657 assert (pending_state_node);
659 connection_legal_c.disconnect ();
661 ret = make_connections (*pending_state_node, pending_state_node_version, pending_state_node_in);
663 delete pending_state_node;
664 pending_state_node = 0;
669 boost::shared_ptr<Bundle>
670 IO::find_possible_bundle (const string &desired_name)
672 static const string digits = "0123456789";
673 const string &default_name = (_direction == Input ? _("in") : _("out"));
674 const string &bundle_type_name = (_direction == Input ? _("input") : _("output"));
676 boost::shared_ptr<Bundle> c = _session.bundle_by_name (desired_name);
679 int bundle_number, mask;
680 string possible_name;
682 string::size_type last_non_digit_pos;
684 error << string_compose(_("Unknown bundle \"%1\" listed for %2 of %3"), desired_name, bundle_type_name, _name)
687 // find numeric suffix of desired name
690 last_non_digit_pos = desired_name.find_last_not_of(digits);
692 if (last_non_digit_pos != string::npos) {
694 s << desired_name.substr(last_non_digit_pos);
698 // see if it's a stereo connection e.g. "in 3+4"
700 if (last_non_digit_pos > 1 && desired_name[last_non_digit_pos] == '+') {
701 string::size_type left_last_non_digit_pos;
703 left_last_non_digit_pos = desired_name.find_last_not_of(digits, last_non_digit_pos-1);
705 if (left_last_non_digit_pos != string::npos) {
706 int left_bundle_number = 0;
708 s << desired_name.substr(left_last_non_digit_pos, last_non_digit_pos-1);
709 s >> left_bundle_number;
711 if (left_bundle_number > 0 && left_bundle_number + 1 == bundle_number) {
722 // find highest set bit
724 while ((mask <= bundle_number) && (mask <<= 1)) {}
726 // "wrap" bundle number into largest possible power of 2
731 if (bundle_number & mask) {
732 bundle_number &= ~mask;
735 s << default_name << " " << bundle_number + 1;
738 s << "+" << bundle_number + 2;
741 possible_name = s.str();
743 if ((c = _session.bundle_by_name (possible_name)) != 0) {
750 info << string_compose (_("Bundle %1 was not available - \"%2\" used instead"), desired_name, possible_name)
753 error << string_compose(_("No %1 bundles available as a replacement"), bundle_type_name)
764 IO::get_port_counts_2X (XMLNode const & node, int /*version*/, ChanCount& n, boost::shared_ptr<Bundle>& /*c*/)
766 XMLProperty const * prop;
767 XMLNodeList children = node.children ();
769 uint32_t n_audio = 0;
771 for (XMLNodeIterator i = children.begin(); i != children.end(); ++i) {
773 if ((prop = node.property ("inputs")) != 0 && _direction == Input) {
774 n_audio = count (prop->value().begin(), prop->value().end(), '{');
775 } else if ((prop = node.property ("input-connection")) != 0 && _direction == Input) {
777 } else if ((prop = node.property ("outputs")) != 0 && _direction == Output) {
778 n_audio = count (prop->value().begin(), prop->value().end(), '{');
779 } else if ((prop = node.property ("output-connection")) != 0 && _direction == Output) {
785 cnt.set_audio (n_audio);
786 n = ChanCount::max (n, cnt);
792 IO::get_port_counts (const XMLNode& node, int version, ChanCount& n, boost::shared_ptr<Bundle>& c)
794 if (version < 3000) {
795 return get_port_counts_2X (node, version, n, c);
798 XMLProperty const * prop;
799 XMLNodeConstIterator iter;
800 uint32_t n_audio = 0;
806 if ((prop = node.property ("connection")) != 0) {
808 if ((c = find_possible_bundle (prop->value())) != 0) {
809 n = ChanCount::max (n, c->nchannels());
814 for (iter = node.children().begin(); iter != node.children().end(); ++iter) {
816 if ((*iter)->name() == X_("Bundle")) {
817 if ((c = find_possible_bundle (prop->value())) != 0) {
818 n = ChanCount::max (n, c->nchannels());
825 if ((*iter)->name() == X_("Port")) {
826 prop = (*iter)->property (X_("type"));
832 if (prop->value() == X_("audio")) {
833 cnt.set_audio (++n_audio);
834 } else if (prop->value() == X_("midi")) {
835 cnt.set_midi (++n_midi);
840 n = ChanCount::max (n, cnt);
845 IO::create_ports (const XMLNode& node, int version)
848 boost::shared_ptr<Bundle> c;
850 get_port_counts (node, version, n, c);
853 Glib::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
855 if (ensure_ports (n, true, this)) {
856 error << string_compose(_("%1: cannot create I/O ports"), _name) << endmsg;
867 IO::make_connections (const XMLNode& node, int version, bool in)
869 if (version < 3000) {
870 return make_connections_2X (node, version, in);
873 const XMLProperty* prop;
875 for (XMLNodeConstIterator i = node.children().begin(); i != node.children().end(); ++i) {
877 if ((*i)->name() == "Bundle") {
878 XMLProperty const * prop = (*i)->property ("name");
880 boost::shared_ptr<Bundle> b = find_possible_bundle (prop->value());
882 connect_ports_to_bundle (b, this);
889 if ((*i)->name() == "Port") {
891 prop = (*i)->property (X_("name"));
897 Port* p = port_by_name (prop->value());
900 for (XMLNodeConstIterator c = (*i)->children().begin(); c != (*i)->children().end(); ++c) {
902 XMLNode* cnode = (*c);
904 if (cnode->name() != X_("Connection")) {
908 if ((prop = cnode->property (X_("other"))) == 0) {
913 connect (p, prop->value(), this);
925 IO::make_connections_2X (const XMLNode& node, int /*version*/, bool in)
927 const XMLProperty* prop;
929 /* XXX: bundles ("connections" as was) */
931 if ((prop = node.property ("inputs")) != 0 && in) {
933 string::size_type ostart = 0;
934 string::size_type start = 0;
935 string::size_type end = 0;
938 vector<string> ports;
940 string const str = prop->value ();
942 while ((start = str.find_first_of ('{', ostart)) != string::npos) {
945 if ((end = str.find_first_of ('}', start)) == string::npos) {
946 error << string_compose(_("IO: badly formed string in XML node for inputs \"%1\""), str) << endmsg;
950 if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
951 error << string_compose(_("bad input string in XML node \"%1\""), str) << endmsg;
958 for (int x = 0; x < n; ++x) {
959 /* XXX: this is a bit of a hack; need to check if it's always valid */
960 string::size_type const p = ports[x].find ("/out");
961 if (p != string::npos) {
962 ports[x].replace (p, 4, "/audio_out");
964 nth(i)->connect (ports[x]);
974 if ((prop = node.property ("outputs")) != 0 && !in) {
976 string::size_type ostart = 0;
977 string::size_type start = 0;
978 string::size_type end = 0;
981 vector<string> ports;
983 string const str = prop->value ();
985 while ((start = str.find_first_of ('{', ostart)) != string::npos) {
988 if ((end = str.find_first_of ('}', start)) == string::npos) {
989 error << string_compose(_("IO: badly formed string in XML node for outputs \"%1\""), str) << endmsg;
993 if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
994 error << string_compose(_("IO: bad output string in XML node \"%1\""), str) << endmsg;
1000 for (int x = 0; x < n; ++x) {
1001 /* XXX: this is a bit of a hack; need to check if it's always valid */
1002 string::size_type const p = ports[x].find ("/in");
1003 if (p != string::npos) {
1004 ports[x].replace (p, 3, "/audio_in");
1006 nth(i)->connect (ports[x]);
1019 IO::set_ports (const string& str)
1021 vector<string> ports;
1026 if ((nports = count (str.begin(), str.end(), '{')) == 0) {
1031 Glib::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
1033 // FIXME: audio-only
1034 if (ensure_ports (ChanCount(DataType::AUDIO, nports), true, this)) {
1039 string::size_type start, end, ostart;
1046 while ((start = str.find_first_of ('{', ostart)) != string::npos) {
1049 if ((end = str.find_first_of ('}', start)) == string::npos) {
1050 error << string_compose(_("IO: badly formed string in XML node for inputs \"%1\""), str) << endmsg;
1054 if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
1055 error << string_compose(_("bad input string in XML node \"%1\""), str) << endmsg;
1061 for (int x = 0; x < n; ++x) {
1062 connect (nth (i), ports[x], this);
1074 IO::parse_io_string (const string& str, vector<string>& ports)
1076 string::size_type pos, opos;
1078 if (str.length() == 0) {
1087 while ((pos = str.find_first_of (',', opos)) != string::npos) {
1088 ports.push_back (str.substr (opos, pos - opos));
1092 if (opos < str.length()) {
1093 ports.push_back (str.substr(opos));
1096 return ports.size();
1100 IO::parse_gain_string (const string& str, vector<string>& ports)
1102 string::size_type pos, opos;
1108 while ((pos = str.find_first_of (',', opos)) != string::npos) {
1109 ports.push_back (str.substr (opos, pos - opos));
1113 if (opos < str.length()) {
1114 ports.push_back (str.substr(opos));
1117 return ports.size();
1121 IO::set_name (const string& requested_name)
1123 string name = requested_name;
1125 if (_name == name) {
1129 /* replace all colons in the name. i wish we didn't have to do this */
1131 replace_all (name, ":", "-");
1133 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
1134 string current_name = i->name();
1135 current_name.replace (current_name.find (_name), _name.val().length(), name);
1136 i->set_name (current_name);
1139 bool const r = SessionObject::set_name (name);
1147 IO::latency () const
1149 framecnt_t max_latency;
1154 /* io lock not taken - must be protected by other means */
1156 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1157 if ((latency = i->private_latency_range (_direction == Output).max) > max_latency) {
1158 DEBUG_TRACE (DEBUG::Latency, string_compose ("port %1 has %2 latency of %3 - use\n",
1160 ((_direction == Output) ? "PLAYBACK" : "CAPTURE"),
1162 max_latency = latency;
1166 DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: max %4 latency from %2 ports = %3\n",
1167 name(), _ports.num_ports(), max_latency,
1168 ((_direction == Output) ? "PLAYBACK" : "CAPTURE")));
1173 IO::connect_ports_to_bundle (boost::shared_ptr<Bundle> c, void* src)
1175 BLOCK_PROCESS_CALLBACK ();
1178 Glib::Mutex::Lock lm2 (io_lock);
1180 c->connect (_bundle, _session.engine());
1182 /* If this is a UserBundle, make a note of what we've done */
1184 boost::shared_ptr<UserBundle> ub = boost::dynamic_pointer_cast<UserBundle> (c);
1187 /* See if we already know about this one */
1188 std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin();
1189 while (i != _bundles_connected.end() && (*i)->bundle != ub) {
1193 if (i == _bundles_connected.end()) {
1194 /* We don't, so make a note */
1195 _bundles_connected.push_back (new UserBundleInfo (this, ub));
1200 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
1205 IO::disconnect_ports_from_bundle (boost::shared_ptr<Bundle> c, void* src)
1207 BLOCK_PROCESS_CALLBACK ();
1210 Glib::Mutex::Lock lm2 (io_lock);
1212 c->disconnect (_bundle, _session.engine());
1214 /* If this is a UserBundle, make a note of what we've done */
1216 boost::shared_ptr<UserBundle> ub = boost::dynamic_pointer_cast<UserBundle> (c);
1219 std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin();
1220 while (i != _bundles_connected.end() && (*i)->bundle != ub) {
1224 if (i != _bundles_connected.end()) {
1226 _bundles_connected.erase (i);
1231 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
1237 IO::disable_connecting ()
1239 connecting_legal = false;
1244 IO::enable_connecting ()
1246 connecting_legal = true;
1247 boost::optional<int> r = ConnectingLegal ();
1248 return r.get_value_or (0);
1252 IO::bundle_changed (Bundle::Change /*c*/)
1255 // connect_input_ports_to_bundle (_input_bundle, this);
1260 IO::build_legal_port_name (DataType type)
1262 const int name_size = jack_port_name_size();
1266 if (type == DataType::AUDIO) {
1267 suffix = _("audio");
1268 } else if (type == DataType::MIDI) {
1271 throw unknown_type();
1274 /* note that if "in" or "out" are translated it will break a session
1275 across locale switches because a port's connection list will
1276 show (old) translated names, but the current port name will
1277 use the (new) translated name.
1280 if (_direction == Input) {
1281 suffix += X_("_in");
1283 suffix += X_("_out");
1286 // allow up to 4 digits for the output port number, plus the slash, suffix and extra space
1288 limit = name_size - _session.engine().client_name().length() - (suffix.length() + 5);
1290 char buf1[name_size+1];
1291 char buf2[name_size+1];
1293 /* colons are illegal in port names, so fix that */
1295 string nom = _name.val();
1296 replace_all (nom, ":", ";");
1298 snprintf (buf1, name_size+1, ("%.*s/%s"), limit, nom.c_str(), suffix.c_str());
1300 int port_number = find_port_hole (buf1);
1301 snprintf (buf2, name_size+1, "%s %d", buf1, port_number);
1303 return string (buf2);
1307 IO::find_port_hole (const char* base)
1309 /* CALLER MUST HOLD IO LOCK */
1313 if (_ports.empty()) {
1317 /* we only allow up to 4 characters for the port number
1320 for (n = 1; n < 9999; ++n) {
1321 char buf[jack_port_name_size()];
1322 PortSet::iterator i = _ports.begin();
1324 snprintf (buf, jack_port_name_size(), _("%s %u"), base, n);
1326 for ( ; i != _ports.end(); ++i) {
1327 if (i->name() == buf) {
1332 if (i == _ports.end()) {
1341 IO::audio(uint32_t n) const
1343 return _ports.nth_audio_port (n);
1348 IO::midi(uint32_t n) const
1350 return _ports.nth_midi_port (n);
1354 * Setup a bundle that describe our inputs or outputs. Also creates the bundle if necessary.
1363 _bundle.reset (new Bundle (_direction == Input));
1366 _bundle->suspend_signals ();
1368 _bundle->remove_channels ();
1370 if (_direction == Input) {
1371 snprintf(buf, sizeof (buf), _("%s in"), _name.val().c_str());
1373 snprintf(buf, sizeof (buf), _("%s out"), _name.val().c_str());
1375 _bundle->set_name (buf);
1378 for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
1380 uint32_t const N = _ports.count().get (*i);
1381 for (uint32_t j = 0; j < N; ++j) {
1382 _bundle->add_channel (bundle_channel_name (j, N, *i), *i);
1383 _bundle->set_port (c, _session.engine().make_port_name_non_relative (_ports.port(*i, j)->name()));
1389 _bundle->resume_signals ();
1392 /** @return Bundles connected to our ports */
1394 IO::bundles_connected ()
1399 for (std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) {
1400 bundles.push_back ((*i)->bundle);
1403 /* Session bundles */
1404 boost::shared_ptr<ARDOUR::BundleList> b = _session.bundles ();
1405 for (ARDOUR::BundleList::iterator i = b->begin(); i != b->end(); ++i) {
1406 if ((*i)->connected_to (_bundle, _session.engine())) {
1407 bundles.push_back (*i);
1413 boost::shared_ptr<ARDOUR::RouteList> r = _session.get_routes ();
1415 if (_direction == Input) {
1416 for (ARDOUR::RouteList::iterator i = r->begin(); i != r->end(); ++i) {
1417 if ((*i)->output()->bundle()->connected_to (_bundle, _session.engine())) {
1418 bundles.push_back ((*i)->output()->bundle());
1422 for (ARDOUR::RouteList::iterator i = r->begin(); i != r->end(); ++i) {
1423 if ((*i)->input()->bundle()->connected_to (_bundle, _session.engine())) {
1424 bundles.push_back ((*i)->input()->bundle());
1433 IO::UserBundleInfo::UserBundleInfo (IO* io, boost::shared_ptr<UserBundle> b)
1436 b->Changed.connect_same_thread (changed, boost::bind (&IO::bundle_changed, io, _1));
1440 IO::bundle_channel_name (uint32_t c, uint32_t n, DataType t) const
1444 if (t == DataType::AUDIO) {
1450 return c == 0 ? _("L") : _("R");
1452 snprintf (buf, sizeof(buf), _("%d"), (c + 1));
1458 snprintf (buf, sizeof(buf), _("%d"), (c + 1));
1467 IO::name_from_state (const XMLNode& node)
1469 const XMLProperty* prop;
1471 if ((prop = node.property ("name")) != 0) {
1472 return prop->value();
1479 IO::set_name_in_state (XMLNode& node, const string& new_name)
1481 const XMLProperty* prop;
1483 if ((prop = node.property ("name")) != 0) {
1484 node.add_property ("name", new_name);
1489 IO::connected () const
1491 /* do we have any connections at all? */
1493 for (PortSet::const_iterator p = _ports.begin(); p != _ports.end(); ++p) {
1494 if (p->connected()) {
1503 IO::connected_to (boost::shared_ptr<const IO> other) const
1506 return connected ();
1509 assert (_direction != other->direction());
1512 uint32_t no = n_ports().n_total();
1513 uint32_t ni = other->n_ports ().n_total();
1515 for (i = 0; i < no; ++i) {
1516 for (j = 0; j < ni; ++j) {
1517 if (nth(i)->connected_to (other->nth(j)->name())) {
1527 IO::connected_to (const string& str) const
1529 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1530 if (i->connected_to (str)) {
1538 /** Caller must hold process lock */
1540 IO::process_input (boost::shared_ptr<Processor> proc, framepos_t start_frame, framepos_t end_frame, pframes_t nframes)
1542 /* don't read the data into new buffers - just use the port buffers directly */
1544 _buffers.get_jack_port_addresses (_ports, nframes);
1545 proc->run (_buffers, start_frame, end_frame, nframes, true);
1549 IO::collect_input (BufferSet& bufs, pframes_t nframes, ChanCount offset)
1551 assert(bufs.available() >= _ports.count());
1553 if (_ports.count() == ChanCount::ZERO) {
1557 bufs.set_count (_ports.count());
1559 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1560 PortSet::iterator i = _ports.begin(*t);
1561 BufferSet::iterator b = bufs.begin(*t);
1563 for (uint32_t off = 0; off < offset.get(*t); ++off, ++b) {
1564 if (b == bufs.end(*t)) {
1569 for ( ; i != _ports.end(*t); ++i, ++b) {
1570 Buffer& bb (i->get_buffer (nframes));
1571 b->read_from (bb, nframes);
1577 IO::copy_to_outputs (BufferSet& bufs, DataType type, pframes_t nframes, framecnt_t offset)
1579 // Copy any buffers 1:1 to outputs
1581 PortSet::iterator o = _ports.begin(type);
1582 BufferSet::iterator i = bufs.begin(type);
1583 BufferSet::iterator prev = i;
1585 while (i != bufs.end(type) && o != _ports.end (type)) {
1586 Buffer& port_buffer (o->get_buffer (nframes));
1587 port_buffer.read_from (*i, nframes, offset);
1593 // Copy last buffer to any extra outputs
1595 while (o != _ports.end(type)) {
1596 Buffer& port_buffer (o->get_buffer (nframes));
1597 port_buffer.read_from (*prev, nframes, offset);
1603 IO::port_by_name (const std::string& str) const
1605 /* to be called only from ::set_state() - no locking */
1607 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1611 if (p.name() == str) {
1612 return const_cast<Port*>(&p);
1620 IO::physically_connected () const
1622 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1623 if (i->physically_connected()) {
1632 IO::has_port (Port* p) const
1634 Glib::Mutex::Lock lm (io_lock);
1635 return _ports.contains (p);