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 Port::PostDisconnect.connect_same_thread (*this, boost::bind (&IO::disconnect_check, this, _1, _2));
73 pending_state_node = 0;
77 IO::IO (Session& s, const XMLNode& node, DataType dt)
78 : 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::Mutex::Lock lm (io_lock);
94 BLOCK_PROCESS_CALLBACK ();
96 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
97 _session.engine().unregister_port (*i);
102 IO::disconnect_check (boost::shared_ptr<Port> a, boost::shared_ptr<Port> b)
104 /* this could be called from within our own ::disconnect() method(s)
105 or from somewhere that operates directly on a port. so, we don't
106 know for sure if we can take this lock or not. if we fail,
107 we assume that its safely locked by our own ::disconnect().
110 Glib::Mutex::Lock tm (io_lock, Glib::TRY_LOCK);
113 /* we took the lock, so we cannot be here from inside
116 if (_ports.contains (a) || _ports.contains (b)) {
117 changed (IOChange (IOChange::ConnectionsChanged), this); /* EMIT SIGNAL */
120 /* we didn't get the lock, so assume that we're inside
121 * ::disconnect(), and it will call changed() appropriately.
127 IO::increment_port_buffer_offset (pframes_t offset)
129 /* io_lock, not taken: function must be called from Session::process() calltree */
131 if (_direction == Output) {
132 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
133 i->increment_port_buffer_offset (offset);
139 IO::silence (framecnt_t nframes)
141 /* io_lock, not taken: function must be called from Session::process() calltree */
143 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
144 i->get_buffer(nframes).silence (nframes);
148 /** Set _bundles_connected to those bundles that are connected such that every
149 * port on every bundle channel x is connected to port x in _ports.
152 IO::check_bundles_connected ()
154 std::vector<UserBundleInfo*> new_list;
156 for (std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) {
158 uint32_t const N = (*i)->bundle->nchannels().n_total();
160 if (_ports.num_ports() < N) {
166 for (uint32_t j = 0; j < N; ++j) {
167 /* Every port on bundle channel j must be connected to our input j */
168 Bundle::PortList const pl = (*i)->bundle->channel_ports (j);
169 for (uint32_t k = 0; k < pl.size(); ++k) {
170 if (_ports.port(j)->connected_to (pl[k]) == false) {
182 new_list.push_back (*i);
188 _bundles_connected = new_list;
193 IO::disconnect (boost::shared_ptr<Port> our_port, string other_port, void* src)
195 if (other_port.length() == 0 || our_port == 0) {
200 Glib::Mutex::Lock lm (io_lock);
202 /* check that our_port is really one of ours */
204 if ( ! _ports.contains(our_port)) {
208 /* disconnect it from the source */
210 if (our_port->disconnect (other_port)) {
211 error << string_compose(_("IO: cannot disconnect port %1 from %2"), our_port->name(), other_port) << endmsg;
215 check_bundles_connected ();
218 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
220 _session.set_dirty ();
226 IO::connect (boost::shared_ptr<Port> our_port, string other_port, void* src)
228 if (other_port.length() == 0 || our_port == 0) {
233 Glib::Mutex::Lock lm (io_lock);
235 /* check that our_port is really one of ours */
237 if ( ! _ports.contains(our_port) ) {
241 /* connect it to the source */
243 if (our_port->connect (other_port)) {
247 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
248 _session.set_dirty ();
253 IO::remove_port (boost::shared_ptr<Port> port, void* src)
255 ChanCount before = _ports.count ();
256 ChanCount after = before;
257 after.set (port->type(), after.get (port->type()) - 1);
259 bool const r = PortCountChanging (after); /* EMIT SIGNAL */
267 BLOCK_PROCESS_CALLBACK ();
270 Glib::Mutex::Lock lm (io_lock);
272 if (_ports.remove(port)) {
273 change.type = IOChange::Type (change.type | IOChange::ConfigurationChanged);
274 change.before = before;
275 change.after = _ports.count ();
277 if (port->connected()) {
278 change.type = IOChange::Type (change.type | IOChange::ConnectionsChanged);
281 _session.engine().unregister_port (port);
282 check_bundles_connected ();
286 PortCountChanged (n_ports()); /* EMIT SIGNAL */
288 if (change.type != IOChange::NoChange) {
289 changed (change, src);
290 _buffers.attach_buffers (_ports);
294 if (change.type & IOChange::ConfigurationChanged) {
298 if (change.type == IOChange::NoChange) {
302 _session.set_dirty ();
309 * @param destination Name of port to connect new port to.
310 * @param src Source for emitted ConfigurationChanged signal.
311 * @param type Data type of port. Default value (NIL) will use this IO's default type.
314 IO::add_port (string destination, void* src, DataType type)
316 boost::shared_ptr<Port> our_port;
318 if (type == DataType::NIL) {
319 type = _default_type;
322 ChanCount before = _ports.count ();
323 ChanCount after = before;
324 after.set (type, after.get (type) + 1);
326 bool const r = PortCountChanging (after); /* EMIT SIGNAL */
334 BLOCK_PROCESS_CALLBACK ();
338 Glib::Mutex::Lock lm (io_lock);
340 /* Create a new port */
342 string portname = build_legal_port_name (type);
344 if (_direction == Input) {
345 if ((our_port = _session.engine().register_input_port (type, portname)) == 0) {
346 error << string_compose(_("IO: cannot register input port %1"), portname) << endmsg;
350 if ((our_port = _session.engine().register_output_port (type, portname)) == 0) {
351 error << string_compose(_("IO: cannot register output port %1"), portname) << endmsg;
356 change.before = _ports.count ();
357 _ports.add (our_port);
360 PortCountChanged (n_ports()); /* EMIT SIGNAL */
361 change.type = IOChange::ConfigurationChanged;
362 change.after = _ports.count ();
363 changed (change, src); /* EMIT SIGNAL */
364 _buffers.attach_buffers (_ports);
367 if (!destination.empty()) {
368 if (our_port->connect (destination)) {
374 _session.set_dirty ();
380 IO::disconnect (void* src)
383 Glib::Mutex::Lock lm (io_lock);
385 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
386 i->disconnect_all ();
389 check_bundles_connected ();
392 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
397 /** Caller must hold process lock */
399 IO::ensure_ports_locked (ChanCount count, bool clear, bool& changed)
401 assert (!AudioEngine::instance()->process_lock().trylock());
403 boost::shared_ptr<Port> port;
407 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
409 const size_t n = count.get(*t);
411 /* remove unused ports */
412 for (size_t i = n_ports().get(*t); i > n; --i) {
413 port = _ports.port(*t, i-1);
417 _session.engine().unregister_port (port);
422 /* create any necessary new ports */
423 while (n_ports().get(*t) < n) {
425 string portname = build_legal_port_name (*t);
429 if (_direction == Input) {
430 if ((port = _session.engine().register_input_port (*t, portname)) == 0) {
431 error << string_compose(_("IO: cannot register input port %1"), portname) << endmsg;
435 if ((port = _session.engine().register_output_port (*t, portname)) == 0) {
436 error << string_compose(_("IO: cannot register output port %1"), portname) << endmsg;
442 catch (AudioEngine::PortRegistrationFailure& err) {
453 check_bundles_connected ();
454 PortCountChanged (n_ports()); /* EMIT SIGNAL */
455 _session.set_dirty ();
459 /* disconnect all existing ports so that we get a fresh start */
460 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
461 i->disconnect_all ();
468 /** Caller must hold process lock */
470 IO::ensure_ports (ChanCount count, bool clear, void* src)
472 assert (!AudioEngine::instance()->process_lock().trylock());
474 bool changed = false;
476 if (count == n_ports() && !clear) {
482 change.before = _ports.count ();
485 Glib::Mutex::Lock im (io_lock);
486 if (ensure_ports_locked (count, clear, changed)) {
492 change.after = _ports.count ();
493 change.type = IOChange::ConfigurationChanged;
494 this->changed (change, src); /* EMIT SIGNAL */
495 _buffers.attach_buffers (_ports);
497 _session.set_dirty ();
503 /** Caller must hold process lock */
505 IO::ensure_io (ChanCount count, bool clear, void* src)
507 assert (!AudioEngine::instance()->process_lock().trylock());
509 return ensure_ports (count, clear, src);
519 IO::state (bool /*full_state*/)
521 XMLNode* node = new XMLNode (state_node_name);
524 vector<string>::iterator ci;
526 LocaleGuard lg (X_("POSIX"));
527 Glib::Mutex::Lock lm (io_lock);
529 node->add_property("name", _name);
530 id().print (buf, sizeof (buf));
531 node->add_property("id", buf);
532 node->add_property ("direction", enum_2_string (_direction));
533 node->add_property ("default-type", _default_type.to_string());
535 for (std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) {
536 XMLNode* n = new XMLNode ("Bundle");
537 n->add_property ("name", (*i)->bundle->name ());
538 node->add_child_nocopy (*n);
541 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
543 vector<string> connections;
545 XMLNode* pnode = new XMLNode (X_("Port"));
546 pnode->add_property (X_("type"), i->type().to_string());
547 pnode->add_property (X_("name"), i->name());
549 if (i->get_connections (connections)) {
551 for (n = 0, ci = connections.begin(); ci != connections.end(); ++ci, ++n) {
553 /* if its a connection to our own port,
554 return only the port name, not the
555 whole thing. this allows connections
556 to be re-established even when our
557 client name is different.
560 XMLNode* cnode = new XMLNode (X_("Connection"));
562 cnode->add_property (X_("other"), _session.engine().make_port_name_relative (*ci));
563 pnode->add_child_nocopy (*cnode);
567 node->add_child_nocopy (*pnode);
570 snprintf (buf, sizeof (buf), "%" PRId64, _user_latency);
571 node->add_property (X_("user-latency"), buf);
577 IO::set_state (const XMLNode& node, int version)
579 /* callers for version < 3000 need to call set_state_2X directly, as A3 IOs
580 * are input OR output, not both, so the direction needs to be specified
583 assert (version >= 3000);
585 const XMLProperty* prop;
586 XMLNodeConstIterator iter;
587 LocaleGuard lg (X_("POSIX"));
589 /* force use of non-localized representation of decimal point,
590 since we use it a lot in XML files and so forth.
593 if (node.name() != state_node_name) {
594 error << string_compose(_("incorrect XML node \"%1\" passed to IO object"), node.name()) << endmsg;
598 if ((prop = node.property ("name")) != 0) {
599 set_name (prop->value());
602 if ((prop = node.property (X_("default-type"))) != 0) {
603 _default_type = DataType(prop->value());
604 assert(_default_type != DataType::NIL);
609 if ((prop = node.property ("direction")) != 0) {
610 _direction = (Direction) string_2_enum (prop->value(), _direction);
613 if (create_ports (node, version)) {
617 if (connecting_legal) {
619 if (make_connections (node, version, false)) {
625 pending_state_node = new XMLNode (node);
626 pending_state_node_version = version;
627 pending_state_node_in = false;
628 ConnectingLegal.connect_same_thread (connection_legal_c, boost::bind (&IO::connecting_became_legal, this));
631 if ((prop = node.property ("user-latency")) != 0) {
632 _user_latency = atoi (prop->value ());
639 IO::set_state_2X (const XMLNode& node, int version, bool in)
641 const XMLProperty* prop;
642 XMLNodeConstIterator iter;
643 LocaleGuard lg (X_("POSIX"));
645 /* force use of non-localized representation of decimal point,
646 since we use it a lot in XML files and so forth.
649 if (node.name() != state_node_name) {
650 error << string_compose(_("incorrect XML node \"%1\" passed to IO object"), node.name()) << endmsg;
654 if ((prop = node.property ("name")) != 0) {
655 set_name (prop->value());
658 if ((prop = node.property (X_("default-type"))) != 0) {
659 _default_type = DataType(prop->value());
660 assert(_default_type != DataType::NIL);
665 _direction = in ? Input : Output;
667 if (create_ports (node, version)) {
671 if (connecting_legal) {
673 if (make_connections_2X (node, version, in)) {
679 pending_state_node = new XMLNode (node);
680 pending_state_node_version = version;
681 pending_state_node_in = in;
682 ConnectingLegal.connect_same_thread (connection_legal_c, boost::bind (&IO::connecting_became_legal, this));
689 IO::connecting_became_legal ()
693 assert (pending_state_node);
695 connection_legal_c.disconnect ();
697 ret = make_connections (*pending_state_node, pending_state_node_version, pending_state_node_in);
699 delete pending_state_node;
700 pending_state_node = 0;
705 boost::shared_ptr<Bundle>
706 IO::find_possible_bundle (const string &desired_name)
708 static const string digits = "0123456789";
709 const string &default_name = (_direction == Input ? _("in") : _("out"));
710 const string &bundle_type_name = (_direction == Input ? _("input") : _("output"));
712 boost::shared_ptr<Bundle> c = _session.bundle_by_name (desired_name);
715 int bundle_number, mask;
716 string possible_name;
718 string::size_type last_non_digit_pos;
720 error << string_compose(_("Unknown bundle \"%1\" listed for %2 of %3"), desired_name, bundle_type_name, _name)
723 // find numeric suffix of desired name
726 last_non_digit_pos = desired_name.find_last_not_of(digits);
728 if (last_non_digit_pos != string::npos) {
730 s << desired_name.substr(last_non_digit_pos);
734 // see if it's a stereo connection e.g. "in 3+4"
736 if (last_non_digit_pos > 1 && desired_name[last_non_digit_pos] == '+') {
737 string::size_type left_last_non_digit_pos;
739 left_last_non_digit_pos = desired_name.find_last_not_of(digits, last_non_digit_pos-1);
741 if (left_last_non_digit_pos != string::npos) {
742 int left_bundle_number = 0;
744 s << desired_name.substr(left_last_non_digit_pos, last_non_digit_pos-1);
745 s >> left_bundle_number;
747 if (left_bundle_number > 0 && left_bundle_number + 1 == bundle_number) {
758 // find highest set bit
760 while ((mask <= bundle_number) && (mask <<= 1)) {}
762 // "wrap" bundle number into largest possible power of 2
767 if (bundle_number & mask) {
768 bundle_number &= ~mask;
771 s << default_name << " " << bundle_number + 1;
774 s << "+" << bundle_number + 2;
777 possible_name = s.str();
779 if ((c = _session.bundle_by_name (possible_name)) != 0) {
786 info << string_compose (_("Bundle %1 was not available - \"%2\" used instead"), desired_name, possible_name)
789 error << string_compose(_("No %1 bundles available as a replacement"), bundle_type_name)
800 IO::get_port_counts_2X (XMLNode const & node, int /*version*/, ChanCount& n, boost::shared_ptr<Bundle>& /*c*/)
802 XMLProperty const * prop;
803 XMLNodeList children = node.children ();
805 uint32_t n_audio = 0;
807 for (XMLNodeIterator i = children.begin(); i != children.end(); ++i) {
809 if ((prop = node.property ("inputs")) != 0 && _direction == Input) {
810 n_audio = count (prop->value().begin(), prop->value().end(), '{');
811 } else if ((prop = node.property ("input-connection")) != 0 && _direction == Input) {
813 } else if ((prop = node.property ("outputs")) != 0 && _direction == Output) {
814 n_audio = count (prop->value().begin(), prop->value().end(), '{');
815 } else if ((prop = node.property ("output-connection")) != 0 && _direction == Output) {
821 cnt.set_audio (n_audio);
822 n = ChanCount::max (n, cnt);
828 IO::get_port_counts (const XMLNode& node, int version, ChanCount& n, boost::shared_ptr<Bundle>& c)
830 if (version < 3000) {
831 return get_port_counts_2X (node, version, n, c);
834 XMLProperty const * prop;
835 XMLNodeConstIterator iter;
836 uint32_t n_audio = 0;
842 if ((prop = node.property ("connection")) != 0) {
844 if ((c = find_possible_bundle (prop->value())) != 0) {
845 n = ChanCount::max (n, c->nchannels());
850 for (iter = node.children().begin(); iter != node.children().end(); ++iter) {
852 if ((*iter)->name() == X_("Bundle")) {
853 if ((c = find_possible_bundle (prop->value())) != 0) {
854 n = ChanCount::max (n, c->nchannels());
861 if ((*iter)->name() == X_("Port")) {
862 prop = (*iter)->property (X_("type"));
868 if (prop->value() == X_("audio")) {
869 cnt.set_audio (++n_audio);
870 } else if (prop->value() == X_("midi")) {
871 cnt.set_midi (++n_midi);
876 n = ChanCount::max (n, cnt);
881 IO::create_ports (const XMLNode& node, int version)
884 boost::shared_ptr<Bundle> c;
886 get_port_counts (node, version, n, c);
889 Glib::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
891 if (ensure_ports (n, true, this)) {
892 error << string_compose(_("%1: cannot create I/O ports"), _name) << endmsg;
903 IO::make_connections (const XMLNode& node, int version, bool in)
905 if (version < 3000) {
906 return make_connections_2X (node, version, in);
909 const XMLProperty* prop;
911 for (XMLNodeConstIterator i = node.children().begin(); i != node.children().end(); ++i) {
913 if ((*i)->name() == "Bundle") {
914 XMLProperty const * prop = (*i)->property ("name");
916 boost::shared_ptr<Bundle> b = find_possible_bundle (prop->value());
918 connect_ports_to_bundle (b, this);
925 if ((*i)->name() == "Port") {
927 prop = (*i)->property (X_("name"));
933 boost::shared_ptr<Port> p = port_by_name (prop->value());
936 for (XMLNodeConstIterator c = (*i)->children().begin(); c != (*i)->children().end(); ++c) {
938 XMLNode* cnode = (*c);
940 if (cnode->name() != X_("Connection")) {
944 if ((prop = cnode->property (X_("other"))) == 0) {
949 connect (p, prop->value(), this);
961 IO::make_connections_2X (const XMLNode& node, int /*version*/, bool in)
963 const XMLProperty* prop;
965 /* XXX: bundles ("connections" as was) */
967 if ((prop = node.property ("inputs")) != 0 && in) {
969 string::size_type ostart = 0;
970 string::size_type start = 0;
971 string::size_type end = 0;
974 vector<string> ports;
976 string const str = prop->value ();
978 while ((start = str.find_first_of ('{', ostart)) != string::npos) {
981 if ((end = str.find_first_of ('}', start)) == string::npos) {
982 error << string_compose(_("IO: badly formed string in XML node for inputs \"%1\""), str) << endmsg;
986 if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
987 error << string_compose(_("bad input string in XML node \"%1\""), str) << endmsg;
994 for (int x = 0; x < n; ++x) {
995 /* XXX: this is a bit of a hack; need to check if it's always valid */
996 string::size_type const p = ports[x].find ("/out");
997 if (p != string::npos) {
998 ports[x].replace (p, 4, "/audio_out");
1000 nth(i)->connect (ports[x]);
1010 if ((prop = node.property ("outputs")) != 0 && !in) {
1012 string::size_type ostart = 0;
1013 string::size_type start = 0;
1014 string::size_type end = 0;
1017 vector<string> ports;
1019 string const str = prop->value ();
1021 while ((start = str.find_first_of ('{', ostart)) != string::npos) {
1024 if ((end = str.find_first_of ('}', start)) == string::npos) {
1025 error << string_compose(_("IO: badly formed string in XML node for outputs \"%1\""), str) << endmsg;
1029 if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
1030 error << string_compose(_("IO: bad output string in XML node \"%1\""), str) << endmsg;
1036 for (int x = 0; x < n; ++x) {
1037 /* XXX: this is a bit of a hack; need to check if it's always valid */
1038 string::size_type const p = ports[x].find ("/in");
1039 if (p != string::npos) {
1040 ports[x].replace (p, 3, "/audio_in");
1042 nth(i)->connect (ports[x]);
1055 IO::set_ports (const string& str)
1057 vector<string> ports;
1062 if ((nports = count (str.begin(), str.end(), '{')) == 0) {
1067 Glib::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
1069 // FIXME: audio-only
1070 if (ensure_ports (ChanCount(DataType::AUDIO, nports), true, this)) {
1075 string::size_type start, end, ostart;
1082 while ((start = str.find_first_of ('{', ostart)) != string::npos) {
1085 if ((end = str.find_first_of ('}', start)) == string::npos) {
1086 error << string_compose(_("IO: badly formed string in XML node for inputs \"%1\""), str) << endmsg;
1090 if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
1091 error << string_compose(_("bad input string in XML node \"%1\""), str) << endmsg;
1097 for (int x = 0; x < n; ++x) {
1098 connect (nth (i), ports[x], this);
1110 IO::parse_io_string (const string& str, vector<string>& ports)
1112 string::size_type pos, opos;
1114 if (str.length() == 0) {
1123 while ((pos = str.find_first_of (',', opos)) != string::npos) {
1124 ports.push_back (str.substr (opos, pos - opos));
1128 if (opos < str.length()) {
1129 ports.push_back (str.substr(opos));
1132 return ports.size();
1136 IO::parse_gain_string (const string& str, vector<string>& ports)
1138 string::size_type pos, opos;
1144 while ((pos = str.find_first_of (',', opos)) != string::npos) {
1145 ports.push_back (str.substr (opos, pos - opos));
1149 if (opos < str.length()) {
1150 ports.push_back (str.substr(opos));
1153 return ports.size();
1157 IO::set_name (const string& requested_name)
1159 string name = requested_name;
1161 if (_name == name) {
1165 /* replace all colons in the name. i wish we didn't have to do this */
1167 replace_all (name, ":", "-");
1169 for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
1170 string current_name = i->name();
1171 current_name.replace (current_name.find (_name), _name.val().length(), name);
1172 i->set_name (current_name);
1175 bool const r = SessionObject::set_name (name);
1183 IO::latency () const
1185 framecnt_t max_latency;
1190 /* io lock not taken - must be protected by other means */
1192 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1193 if ((latency = i->private_latency_range (_direction == Output).max) > max_latency) {
1194 DEBUG_TRACE (DEBUG::Latency, string_compose ("port %1 has %2 latency of %3 - use\n",
1196 ((_direction == Output) ? "PLAYBACK" : "CAPTURE"),
1198 max_latency = latency;
1202 DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: max %4 latency from %2 ports = %3\n",
1203 name(), _ports.num_ports(), max_latency,
1204 ((_direction == Output) ? "PLAYBACK" : "CAPTURE")));
1209 IO::connect_ports_to_bundle (boost::shared_ptr<Bundle> c, void* src)
1211 BLOCK_PROCESS_CALLBACK ();
1214 Glib::Mutex::Lock lm2 (io_lock);
1216 c->connect (_bundle, _session.engine());
1218 /* If this is a UserBundle, make a note of what we've done */
1220 boost::shared_ptr<UserBundle> ub = boost::dynamic_pointer_cast<UserBundle> (c);
1223 /* See if we already know about this one */
1224 std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin();
1225 while (i != _bundles_connected.end() && (*i)->bundle != ub) {
1229 if (i == _bundles_connected.end()) {
1230 /* We don't, so make a note */
1231 _bundles_connected.push_back (new UserBundleInfo (this, ub));
1236 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
1241 IO::disconnect_ports_from_bundle (boost::shared_ptr<Bundle> c, void* src)
1243 BLOCK_PROCESS_CALLBACK ();
1246 Glib::Mutex::Lock lm2 (io_lock);
1248 c->disconnect (_bundle, _session.engine());
1250 /* If this is a UserBundle, make a note of what we've done */
1252 boost::shared_ptr<UserBundle> ub = boost::dynamic_pointer_cast<UserBundle> (c);
1255 std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin();
1256 while (i != _bundles_connected.end() && (*i)->bundle != ub) {
1260 if (i != _bundles_connected.end()) {
1262 _bundles_connected.erase (i);
1267 changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */
1273 IO::disable_connecting ()
1275 connecting_legal = false;
1280 IO::enable_connecting ()
1282 Glib::Mutex::Lock lm (AudioEngine::instance()->process_lock());
1283 connecting_legal = true;
1284 boost::optional<int> r = ConnectingLegal ();
1285 return r.get_value_or (0);
1289 IO::bundle_changed (Bundle::Change /*c*/)
1292 // connect_input_ports_to_bundle (_input_bundle, this);
1297 IO::build_legal_port_name (DataType type)
1299 const int name_size = jack_port_name_size();
1303 if (type == DataType::AUDIO) {
1304 suffix = _("audio");
1305 } else if (type == DataType::MIDI) {
1308 throw unknown_type();
1311 /* note that if "in" or "out" are translated it will break a session
1312 across locale switches because a port's connection list will
1313 show (old) translated names, but the current port name will
1314 use the (new) translated name.
1317 if (_direction == Input) {
1318 suffix += X_("_in");
1320 suffix += X_("_out");
1323 // allow up to 4 digits for the output port number, plus the slash, suffix and extra space
1325 limit = name_size - _session.engine().client_name().length() - (suffix.length() + 5);
1327 char buf1[name_size+1];
1328 char buf2[name_size+1];
1330 /* colons are illegal in port names, so fix that */
1332 string nom = _name.val();
1333 replace_all (nom, ":", ";");
1335 snprintf (buf1, name_size+1, ("%.*s/%s"), limit, nom.c_str(), suffix.c_str());
1337 int port_number = find_port_hole (buf1);
1338 snprintf (buf2, name_size+1, "%s %d", buf1, port_number);
1340 return string (buf2);
1344 IO::find_port_hole (const char* base)
1346 /* CALLER MUST HOLD IO LOCK */
1350 if (_ports.empty()) {
1354 /* we only allow up to 4 characters for the port number
1357 for (n = 1; n < 9999; ++n) {
1358 char buf[jack_port_name_size()];
1359 PortSet::iterator i = _ports.begin();
1361 snprintf (buf, jack_port_name_size(), _("%s %u"), base, n);
1363 for ( ; i != _ports.end(); ++i) {
1364 if (i->name() == buf) {
1369 if (i == _ports.end()) {
1377 boost::shared_ptr<AudioPort>
1378 IO::audio(uint32_t n) const
1380 return _ports.nth_audio_port (n);
1384 boost::shared_ptr<MidiPort>
1385 IO::midi(uint32_t n) const
1387 return _ports.nth_midi_port (n);
1391 * Setup a bundle that describe our inputs or outputs. Also creates the bundle if necessary.
1400 _bundle.reset (new Bundle (_direction == Input));
1403 _bundle->suspend_signals ();
1405 _bundle->remove_channels ();
1407 if (_direction == Input) {
1408 snprintf(buf, sizeof (buf), _("%s in"), _name.val().c_str());
1410 snprintf(buf, sizeof (buf), _("%s out"), _name.val().c_str());
1412 _bundle->set_name (buf);
1415 for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
1417 uint32_t const N = _ports.count().get (*i);
1418 for (uint32_t j = 0; j < N; ++j) {
1419 _bundle->add_channel (bundle_channel_name (j, N, *i), *i);
1420 _bundle->set_port (c, _session.engine().make_port_name_non_relative (_ports.port(*i, j)->name()));
1426 _bundle->resume_signals ();
1429 /** @return Bundles connected to our ports */
1431 IO::bundles_connected ()
1436 for (std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) {
1437 bundles.push_back ((*i)->bundle);
1440 /* Session bundles */
1441 boost::shared_ptr<ARDOUR::BundleList> b = _session.bundles ();
1442 for (ARDOUR::BundleList::iterator i = b->begin(); i != b->end(); ++i) {
1443 if ((*i)->connected_to (_bundle, _session.engine())) {
1444 bundles.push_back (*i);
1450 boost::shared_ptr<ARDOUR::RouteList> r = _session.get_routes ();
1452 if (_direction == Input) {
1453 for (ARDOUR::RouteList::iterator i = r->begin(); i != r->end(); ++i) {
1454 if ((*i)->output()->bundle()->connected_to (_bundle, _session.engine())) {
1455 bundles.push_back ((*i)->output()->bundle());
1459 for (ARDOUR::RouteList::iterator i = r->begin(); i != r->end(); ++i) {
1460 if ((*i)->input()->bundle()->connected_to (_bundle, _session.engine())) {
1461 bundles.push_back ((*i)->input()->bundle());
1470 IO::UserBundleInfo::UserBundleInfo (IO* io, boost::shared_ptr<UserBundle> b)
1473 b->Changed.connect_same_thread (changed, boost::bind (&IO::bundle_changed, io, _1));
1477 IO::bundle_channel_name (uint32_t c, uint32_t n, DataType t) const
1481 if (t == DataType::AUDIO) {
1487 return c == 0 ? _("L") : _("R");
1489 snprintf (buf, sizeof(buf), _("%d"), (c + 1));
1495 snprintf (buf, sizeof(buf), _("%d"), (c + 1));
1504 IO::name_from_state (const XMLNode& node)
1506 const XMLProperty* prop;
1508 if ((prop = node.property ("name")) != 0) {
1509 return prop->value();
1516 IO::set_name_in_state (XMLNode& node, const string& new_name)
1518 node.add_property (X_("name"), new_name);
1519 XMLNodeList children = node.children ();
1520 for (XMLNodeIterator i = children.begin(); i != children.end(); ++i) {
1521 if ((*i)->name() == X_("Port")) {
1522 string const old_name = (*i)->property(X_("name"))->value();
1523 string const old_name_second_part = old_name.substr (old_name.find_first_of ("/") + 1);
1524 (*i)->add_property (X_("name"), string_compose ("%1/%2", new_name, old_name_second_part));
1530 IO::connected () const
1532 /* do we have any connections at all? */
1534 for (PortSet::const_iterator p = _ports.begin(); p != _ports.end(); ++p) {
1535 if (p->connected()) {
1544 IO::connected_to (boost::shared_ptr<const IO> other) const
1547 return connected ();
1550 assert (_direction != other->direction());
1553 uint32_t no = n_ports().n_total();
1554 uint32_t ni = other->n_ports ().n_total();
1556 for (i = 0; i < no; ++i) {
1557 for (j = 0; j < ni; ++j) {
1558 if (nth(i)->connected_to (other->nth(j)->name())) {
1568 IO::connected_to (const string& str) const
1570 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1571 if (i->connected_to (str)) {
1579 /** Call a processor's ::run() method, giving it our buffers
1580 * Caller must hold process lock.
1583 IO::process_input (boost::shared_ptr<Processor> proc, framepos_t start_frame, framepos_t end_frame, pframes_t nframes)
1585 /* don't read the data into new buffers - just use the port buffers directly */
1587 if (n_ports().n_total() == 0) {
1588 /* We have no ports, so nothing to process */
1592 _buffers.get_jack_port_addresses (_ports, nframes);
1593 proc->run (_buffers, start_frame, end_frame, nframes, true);
1597 IO::collect_input (BufferSet& bufs, pframes_t nframes, ChanCount offset)
1599 assert(bufs.available() >= _ports.count());
1601 if (_ports.count() == ChanCount::ZERO) {
1605 bufs.set_count (_ports.count());
1607 for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
1608 PortSet::iterator i = _ports.begin(*t);
1609 BufferSet::iterator b = bufs.begin(*t);
1611 for (uint32_t off = 0; off < offset.get(*t); ++off, ++b) {
1612 if (b == bufs.end(*t)) {
1617 for ( ; i != _ports.end(*t); ++i, ++b) {
1618 Buffer& bb (i->get_buffer (nframes));
1619 b->read_from (bb, nframes);
1625 IO::copy_to_outputs (BufferSet& bufs, DataType type, pframes_t nframes, framecnt_t offset)
1627 // Copy any buffers 1:1 to outputs
1629 PortSet::iterator o = _ports.begin(type);
1630 BufferSet::iterator i = bufs.begin(type);
1631 BufferSet::iterator prev = i;
1633 while (i != bufs.end(type) && o != _ports.end (type)) {
1634 Buffer& port_buffer (o->get_buffer (nframes));
1635 port_buffer.read_from (*i, nframes, offset);
1641 // Copy last buffer to any extra outputs
1643 while (o != _ports.end(type)) {
1644 Buffer& port_buffer (o->get_buffer (nframes));
1645 port_buffer.read_from (*prev, nframes, offset);
1650 boost::shared_ptr<Port>
1651 IO::port_by_name (const std::string& str) const
1653 /* to be called only from ::set_state() - no locking */
1655 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1657 if (i->name() == str) {
1658 return boost::const_pointer_cast<Port> (*i);
1662 return boost::shared_ptr<Port> ();
1666 IO::physically_connected () const
1668 for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
1669 if (i->physically_connected()) {
1678 IO::has_port (boost::shared_ptr<Port> p) const
1680 Glib::Mutex::Lock lm (io_lock);
1681 return _ports.contains (p);