X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fardour%2Fio.cc;h=21afdbae5657d1be5cd266fa1139f7928ba15965;hb=d8657a4ab83ee96c21ce4620e9daca789d76e164;hp=b9fd9ccbc66ed4bbfcbcad79e98a729f64972709;hpb=f6fdd8dcbf41f864e9f0cc32dabe81fe3533ddfe;p=ardour.git diff --git a/libs/ardour/io.cc b/libs/ardour/io.cc index b9fd9ccbc6..21afdbae56 100644 --- a/libs/ardour/io.cc +++ b/libs/ardour/io.cc @@ -18,11 +18,12 @@ #include #include +#include + #include #include #include - #include #include @@ -47,19 +48,7 @@ #include "i18n.h" -#include - -/* - A bug in OS X's cmath that causes isnan() and isinf() to be - "undeclared". the following works around that -*/ - -#if defined(__APPLE__) && defined(__MACH__) -extern "C" int isnan (double); -extern "C" int isinf (double); -#endif - -#define BLOCK_PROCESS_CALLBACK() Glib::Mutex::Lock em (_session.engine().process_lock()) +#define BLOCK_PROCESS_CALLBACK() Glib::Mutex::Lock em (AudioEngine::instance()->process_lock()) using namespace std; using namespace ARDOUR; @@ -67,8 +56,8 @@ using namespace PBD; const string IO::state_node_name = "IO"; bool IO::connecting_legal = false; -boost::signals2::signal IO::ConnectingLegal; -boost::signals2::signal IO::PortCountChanged; +PBD::Signal0 IO::ConnectingLegal; +PBD::Signal1 IO::PortCountChanged; /** @param default_type The type of port that will be created by ensure_io * and friends if no type is explicitly requested (to avoid breakage). @@ -107,7 +96,7 @@ IO::~IO () } void -IO::silence (nframes_t nframes) +IO::silence (framecnt_t nframes) { /* io_lock, not taken: function must be called from Session::process() calltree */ @@ -122,6 +111,10 @@ IO::check_bundles_connected () check_bundles (_bundles_connected, ports()); } +/** Check the bundles in list to see which are connected to a given PortSet, + * and update list with those that are connected such that every port on every + * bundle channel x is connected to port x in ports. + */ void IO::check_bundles (std::vector& list, const PortSet& ports) { @@ -129,9 +122,9 @@ IO::check_bundles (std::vector& list, const PortSet& ports) for (std::vector::iterator i = list.begin(); i != list.end(); ++i) { - uint32_t const N = (*i)->bundle->nchannels (); + uint32_t const N = (*i)->bundle->nchannels().n_total(); - if (_ports.num_ports (default_type()) < N) { + if (_ports.num_ports() < N) { continue; } @@ -170,30 +163,27 @@ IO::disconnect (Port* our_port, string other_port, void* src) return 0; } - { - BLOCK_PROCESS_CALLBACK (); - - { - Glib::Mutex::Lock lm (io_lock); - - /* check that our_port is really one of ours */ - - if ( ! _ports.contains(our_port)) { - return -1; - } - - /* disconnect it from the source */ - - if (our_port->disconnect (other_port)) { - error << string_compose(_("IO: cannot disconnect port %1 from %2"), our_port->name(), other_port) << endmsg; - return -1; - } + { + Glib::Mutex::Lock lm (io_lock); + + /* check that our_port is really one of ours */ + + if ( ! _ports.contains(our_port)) { + return -1; + } + + /* disconnect it from the source */ + + if (our_port->disconnect (other_port)) { + error << string_compose(_("IO: cannot disconnect port %1 from %2"), our_port->name(), other_port) << endmsg; + return -1; + } + + check_bundles_connected (); + } + + changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */ - check_bundles_connected (); - } - } - - changed (ConnectionsChanged, src); /* EMIT SIGNAL */ _session.set_dirty (); return 0; @@ -207,26 +197,22 @@ IO::connect (Port* our_port, string other_port, void* src) } { - BLOCK_PROCESS_CALLBACK (); - - { - Glib::Mutex::Lock lm (io_lock); - - /* check that our_port is really one of ours */ - - if ( ! _ports.contains(our_port) ) { - return -1; - } - - /* connect it to the source */ - - if (our_port->connect (other_port)) { - return -1; - } + Glib::Mutex::Lock lm (io_lock); + + /* check that our_port is really one of ours */ + + if ( ! _ports.contains(our_port) ) { + return -1; + } + + /* connect it to the source */ + + if (our_port->connect (other_port)) { + return -1; } } - changed (ConnectionsChanged, src); /* EMIT SIGNAL */ + changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */ _session.set_dirty (); return 0; } @@ -234,20 +220,30 @@ IO::connect (Port* our_port, string other_port, void* src) int IO::remove_port (Port* port, void* src) { - IOChange change (NoChange); + ChanCount before = _ports.count (); + ChanCount after = before; + after.set (port->type(), after.get (port->type()) - 1); + + bool const r = PortCountChanging (after); /* EMIT SIGNAL */ + if (r) { + return -1; + } + + IOChange change; { BLOCK_PROCESS_CALLBACK (); - { Glib::Mutex::Lock lm (io_lock); if (_ports.remove(port)) { - change = IOChange (change|ConfigurationChanged); + change.type = IOChange::Type (change.type | IOChange::ConfigurationChanged); + change.before = before; + change.after = _ports.count (); if (port->connected()) { - change = IOChange (change|ConnectionsChanged); + change.type = IOChange::Type (change.type | IOChange::ConnectionsChanged); } _session.engine().unregister_port (*port); @@ -256,24 +252,27 @@ IO::remove_port (Port* port, void* src) } PortCountChanged (n_ports()); /* EMIT SIGNAL */ + + if (change.type != IOChange::NoChange) { + changed (change, src); + _session.set_dirty (); + } } - if (change & ConfigurationChanged) { + if (change.type & IOChange::ConfigurationChanged) { setup_bundle (); } - if (change != NoChange) { - changed (change, src); - _session.set_dirty (); - return 0; + if (change.type == IOChange::NoChange) { + return -1; } - return -1; + return 0; } -/** Add an output port. +/** Add a port. * - * @param destination Name of input port to connect new port to. + * @param destination Name of port to connect new port to. * @param src Source for emitted ConfigurationChanged signal. * @param type Data type of port. Default value (NIL) will use this IO's default type. */ @@ -286,6 +285,8 @@ IO::add_port (string destination, void* src, DataType type) type = _default_type; } + IOChange change; + { BLOCK_PROCESS_CALLBACK (); @@ -293,7 +294,7 @@ IO::add_port (string destination, void* src, DataType type) { Glib::Mutex::Lock lm (io_lock); - /* Create a new output port */ + /* Create a new port */ string portname = build_legal_port_name (type); @@ -309,10 +310,16 @@ IO::add_port (string destination, void* src, DataType type) } } + change.before = _ports.count (); _ports.add (our_port); } PortCountChanged (n_ports()); /* EMIT SIGNAL */ + + // pan_changed (src); /* EMIT SIGNAL */ + change.type = IOChange::ConfigurationChanged; + change.after = _ports.count (); + changed (change, src); /* EMIT SIGNAL */ } if (destination.length()) { @@ -321,8 +328,6 @@ IO::add_port (string destination, void* src, DataType type) } } - // pan_changed (src); /* EMIT SIGNAL */ - changed (ConfigurationChanged, src); /* EMIT SIGNAL */ setup_bundle (); _session.set_dirty (); @@ -333,27 +338,26 @@ int IO::disconnect (void* src) { { - BLOCK_PROCESS_CALLBACK (); - - { - Glib::Mutex::Lock lm (io_lock); - - for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) { - i->disconnect_all (); - } - - check_bundles_connected (); + Glib::Mutex::Lock lm (io_lock); + + for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) { + i->disconnect_all (); } + + check_bundles_connected (); } - - changed (ConnectionsChanged, src); /* EMIT SIGNAL */ + + changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */ return 0; } +/** Caller must hold process lock */ bool IO::ensure_ports_locked (ChanCount count, bool clear, void* /*src*/) { + assert (!AudioEngine::instance()->process_lock().trylock()); + Port* port = 0; bool changed = false; @@ -394,7 +398,7 @@ IO::ensure_ports_locked (ChanCount count, bool clear, void* /*src*/) catch (AudioEngine::PortRegistrationFailure& err) { /* pass it on */ - throw AudioEngine::PortRegistrationFailure(); + throw; } _ports.add (port); @@ -418,26 +422,32 @@ IO::ensure_ports_locked (ChanCount count, bool clear, void* /*src*/) return changed; } - +/** Caller must hold process lock */ int -IO::ensure_ports (ChanCount count, bool clear, bool lockit, void* src) +IO::ensure_ports (ChanCount count, bool clear, void* src) { + assert (!AudioEngine::instance()->process_lock().trylock()); + bool changed = false; if (count == n_ports() && !clear) { return 0; } - if (lockit) { - BLOCK_PROCESS_CALLBACK (); + IOChange change; + + change.before = _ports.count (); + + { Glib::Mutex::Lock im (io_lock); changed = ensure_ports_locked (count, clear, src); - } else { - changed = ensure_ports_locked (count, clear, src); } if (changed) { - this->changed (ConfigurationChanged, src); /* EMIT SIGNAL */ + change.after = _ports.count (); + change.type = IOChange::ConfigurationChanged; + this->changed (change, src); /* EMIT SIGNAL */ + _buffers.attach_buffers (_ports); setup_bundle (); _session.set_dirty (); } @@ -445,10 +455,13 @@ IO::ensure_ports (ChanCount count, bool clear, bool lockit, void* src) return 0; } +/** Caller must hold process lock */ int IO::ensure_io (ChanCount count, bool clear, void* src) { - return ensure_ports (count, clear, true, src); + assert (!AudioEngine::instance()->process_lock().trylock()); + + return ensure_ports (count, clear, src); } XMLNode& @@ -566,7 +579,7 @@ IO::set_state (const XMLNode& node, int version) pending_state_node = new XMLNode (node); pending_state_node_version = version; pending_state_node_in = false; - connection_legal_c = ConnectingLegal.connect (boost::bind (&IO::connecting_became_legal, this)); + ConnectingLegal.connect_same_thread (connection_legal_c, boost::bind (&IO::connecting_became_legal, this)); } @@ -619,7 +632,7 @@ IO::set_state_2X (const XMLNode& node, int version, bool in) pending_state_node = new XMLNode (node); pending_state_node_version = version; pending_state_node_in = in; - connection_legal_c = ConnectingLegal.connect (boost::bind (&IO::connecting_became_legal, this)); + ConnectingLegal.connect_same_thread (connection_legal_c, boost::bind (&IO::connecting_became_legal, this)); } return 0; @@ -674,12 +687,12 @@ IO::find_possible_bundle (const string &desired_name) // see if it's a stereo connection e.g. "in 3+4" if (last_non_digit_pos > 1 && desired_name[last_non_digit_pos] == '+') { - int left_bundle_number = 0; string::size_type left_last_non_digit_pos; left_last_non_digit_pos = desired_name.find_last_not_of(digits, last_non_digit_pos-1); if (left_last_non_digit_pos != string::npos) { + int left_bundle_number = 0; stringstream s; s << desired_name.substr(left_last_non_digit_pos, last_non_digit_pos-1); s >> left_bundle_number; @@ -748,8 +761,12 @@ IO::get_port_counts_2X (XMLNode const & node, int /*version*/, ChanCount& n, boo if ((prop = node.property ("inputs")) != 0 && _direction == Input) { n_audio = count (prop->value().begin(), prop->value().end(), '{'); + } else if ((prop = node.property ("input-connection")) != 0 && _direction == Input) { + n_audio = 1; } else if ((prop = node.property ("outputs")) != 0 && _direction == Output) { n_audio = count (prop->value().begin(), prop->value().end(), '{'); + } else if ((prop = node.property ("output-connection")) != 0 && _direction == Output) { + n_audio = 2; } } @@ -778,7 +795,7 @@ IO::get_port_counts (const XMLNode& node, int version, ChanCount& n, boost::shar if ((prop = node.property ("connection")) != 0) { if ((c = find_possible_bundle (prop->value())) != 0) { - n = ChanCount::max (n, ChanCount(c->type(), c->nchannels())); + n = ChanCount::max (n, c->nchannels()); } return 0; } @@ -787,7 +804,7 @@ IO::get_port_counts (const XMLNode& node, int version, ChanCount& n, boost::shar if ((*iter)->name() == X_("Bundle")) { if ((c = find_possible_bundle (prop->value())) != 0) { - n = ChanCount::max (n, ChanCount(c->type(), c->nchannels())); + n = ChanCount::max (n, c->nchannels()); return 0; } else { return -1; @@ -821,9 +838,13 @@ IO::create_ports (const XMLNode& node, int version) get_port_counts (node, version, n, c); - if (ensure_ports (n, true, true, this)) { - error << string_compose(_("%1: cannot create I/O ports"), _name) << endmsg; - return -1; + { + Glib::Mutex::Lock lm (AudioEngine::instance()->process_lock ()); + + if (ensure_ports (n, true, this)) { + error << string_compose(_("%1: cannot create I/O ports"), _name) << endmsg; + return -1; + } } /* XXX use c */ @@ -876,12 +897,12 @@ IO::make_connections (const XMLNode& node, int version, bool in) if ((prop = cnode->property (X_("other"))) == 0) { continue; } - + if (prop) { p->connect (prop->value()); } } - } + } } } @@ -995,9 +1016,13 @@ IO::set_ports (const string& str) return 0; } - // FIXME: audio-only - if (ensure_ports (ChanCount(DataType::AUDIO, nports), true, true, this)) { - return -1; + { + Glib::Mutex::Lock lm (AudioEngine::instance()->process_lock ()); + + // FIXME: audio-only + if (ensure_ports (ChanCount(DataType::AUDIO, nports), true, this)) { + return -1; + } } string::size_type start, end, ostart; @@ -1086,19 +1111,17 @@ IO::set_name (const string& requested_name) { string name = requested_name; - if (name == _name) { + if (_name == name) { return true; } /* replace all colons in the name. i wish we didn't have to do this */ - if (replace_all (name, ":", "-")) { - warning << _("you cannot use colons to name objects with I/O connections") << endmsg; - } + replace_all (name, ":", "-"); for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) { string current_name = i->name(); - current_name.replace (current_name.find (_name), _name.length(), name); + current_name.replace (current_name.find (_name), _name.val().length(), name); i->set_name (current_name); } @@ -1110,7 +1133,7 @@ IO::set_name (const string& requested_name) } void -IO::set_port_latency (nframes_t nframes) +IO::set_port_latency (framecnt_t nframes) { Glib::Mutex::Lock lm (io_lock); @@ -1119,11 +1142,11 @@ IO::set_port_latency (nframes_t nframes) } } -nframes_t +framecnt_t IO::latency () const { - nframes_t max_latency; - nframes_t latency; + framecnt_t max_latency; + framecnt_t latency; max_latency = 0; @@ -1151,8 +1174,9 @@ IO::update_port_total_latencies () int IO::connect_ports_to_bundle (boost::shared_ptr c, void* src) { + BLOCK_PROCESS_CALLBACK (); + { - BLOCK_PROCESS_CALLBACK (); Glib::Mutex::Lock lm2 (io_lock); c->connect (_bundle, _session.engine()); @@ -1175,15 +1199,16 @@ IO::connect_ports_to_bundle (boost::shared_ptr c, void* src) } } - changed (IOChange (ConfigurationChanged|ConnectionsChanged), src); /* EMIT SIGNAL */ + changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */ return 0; } int IO::disconnect_ports_from_bundle (boost::shared_ptr c, void* src) { + BLOCK_PROCESS_CALLBACK (); + { - BLOCK_PROCESS_CALLBACK (); Glib::Mutex::Lock lm2 (io_lock); c->disconnect (_bundle, _session.engine()); @@ -1205,7 +1230,7 @@ IO::disconnect_ports_from_bundle (boost::shared_ptr c, void* src) } } - changed (IOChange (ConfigurationChanged|ConnectionsChanged), src); /* EMIT SIGNAL */ + changed (IOChange (IOChange::ConnectionsChanged), src); /* EMIT SIGNAL */ return 0; } @@ -1221,7 +1246,8 @@ int IO::enable_connecting () { connecting_legal = true; - return *ConnectingLegal (); + boost::optional r = ConnectingLegal (); + return r.get_value_or (0); } void @@ -1266,7 +1292,7 @@ IO::build_legal_port_name (DataType type) char buf1[name_size+1]; char buf2[name_size+1]; - snprintf (buf1, name_size+1, ("%.*s/%s"), limit, _name.c_str(), suffix.c_str()); + snprintf (buf1, name_size+1, ("%.*s/%s"), limit, _name.val().c_str(), suffix.c_str()); int port_number = find_port_hole (buf1); snprintf (buf2, name_size+1, "%s %d", buf1, port_number); @@ -1336,20 +1362,25 @@ IO::setup_bundle () _bundle->suspend_signals (); - _bundle->set_type (default_type ()); - _bundle->remove_channels (); if (_direction == Input) { - snprintf(buf, sizeof (buf), _("%s in"), _name.c_str()); + snprintf(buf, sizeof (buf), _("%s in"), _name.val().c_str()); } else { - snprintf(buf, sizeof (buf), _("%s out"), _name.c_str()); + snprintf(buf, sizeof (buf), _("%s out"), _name.val().c_str()); } _bundle->set_name (buf); - uint32_t const ni = _ports.num_ports(); - for (uint32_t i = 0; i < ni; ++i) { - _bundle->add_channel (bundle_channel_name (i, ni)); - _bundle->set_port (i, _session.engine().make_port_name_non_relative (_ports.port(i)->name())); + + int c = 0; + for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) { + + uint32_t const N = _ports.count().get (*i); + for (uint32_t j = 0; j < N; ++j) { + _bundle->add_channel (bundle_channel_name (j, N, *i), *i); + _bundle->set_port (c, _session.engine().make_port_name_non_relative (_ports.port(*i, j)->name())); + ++c; + } + } _bundle->resume_signals (); @@ -1399,22 +1430,31 @@ IO::bundles_connected () IO::UserBundleInfo::UserBundleInfo (IO* io, boost::shared_ptr b) { bundle = b; - changed = b->Changed.connect (boost::bind (&IO::bundle_changed, io, _1)); + b->Changed.connect_same_thread (changed, boost::bind (&IO::bundle_changed, io, _1)); } std::string -IO::bundle_channel_name (uint32_t c, uint32_t n) const +IO::bundle_channel_name (uint32_t c, uint32_t n, DataType t) const { char buf[32]; - switch (n) { - case 1: - return _("mono"); - case 2: - return c == 0 ? _("L") : _("R"); - default: + if (t == DataType::AUDIO) { + + switch (n) { + case 1: + return _("mono"); + case 2: + return c == 0 ? _("L") : _("R"); + default: + snprintf (buf, sizeof(buf), _("%d"), (c + 1)); + return buf; + } + + } else { + snprintf (buf, sizeof(buf), _("%d"), (c + 1)); return buf; + } return ""; @@ -1442,19 +1482,25 @@ IO::set_name_in_state (XMLNode& node, const string& new_name) } } +bool +IO::connected () const +{ + /* do we have any connections at all? */ + + for (PortSet::const_iterator p = _ports.begin(); p != _ports.end(); ++p) { + if (p->connected()) { + return true; + } + } + + return false; +} + bool IO::connected_to (boost::shared_ptr other) const { if (!other) { - /* do we have any connections at all? */ - - for (PortSet::const_iterator p = _ports.begin(); p != _ports.end(); ++p) { - if (p->connected()) { - return true; - } - } - - return false; + return connected (); } assert (_direction != other->direction()); @@ -1475,18 +1521,16 @@ IO::connected_to (boost::shared_ptr other) const } void -IO::process_input (boost::shared_ptr proc, sframes_t start_frame, sframes_t end_frame, nframes_t nframes) +IO::process_input (boost::shared_ptr proc, framepos_t start_frame, framepos_t end_frame, pframes_t nframes) { - BufferSet bufs; - /* don't read the data into new buffers - just use the port buffers directly */ - bufs.attach_buffers (_ports, nframes, 0); - proc->run (bufs, start_frame, end_frame, nframes, true); + _buffers.get_jack_port_addresses (_ports, nframes, 0); + proc->run (_buffers, start_frame, end_frame, nframes, true); } void -IO::collect_input (BufferSet& bufs, nframes_t nframes, ChanCount offset) +IO::collect_input (BufferSet& bufs, pframes_t nframes, ChanCount offset) { assert(bufs.available() >= _ports.count()); @@ -1514,7 +1558,7 @@ IO::collect_input (BufferSet& bufs, nframes_t nframes, ChanCount offset) } void -IO::copy_to_outputs (BufferSet& bufs, DataType type, nframes_t nframes, nframes_t offset) +IO::copy_to_outputs (BufferSet& bufs, DataType type, pframes_t nframes, framecnt_t offset) { // Copy any buffers 1:1 to outputs @@ -1555,3 +1599,22 @@ IO::port_by_name (const std::string& str) const return 0; } + +bool +IO::physically_connected () const +{ + for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) { + if (i->physically_connected()) { + return true; + } + } + + return false; +} + +bool +IO::has_port (Port* p) const +{ + Glib::Mutex::Lock lm (io_lock); + return _ports.contains (p); +}