switch Evoral::Sequence debugging to use DEBUG_TRACE(); Sequence uses multiset<....
[ardour.git] / libs / ardour / io.cc
index 9811cfed3a04c9dcb84d397df2bb6e5790d717e7..f40cbafb2dca228e6a0e5c11d229e20e107db174 100644 (file)
 
 #include <fstream>
 #include <algorithm>
+#include <cmath>
+
 #include <unistd.h>
 #include <locale.h>
 #include <errno.h>
 
-#include <sigc++/bind.h>
-
 #include <glibmm.h>
 #include <glibmm/thread.h>
 
@@ -41,7 +41,6 @@
 #include "ardour/midi_port.h"
 #include "ardour/session.h"
 #include "ardour/cycle_timer.h"
-#include "ardour/panner.h"
 #include "ardour/buffer_set.h"
 #include "ardour/meter.h"
 #include "ardour/amp.h"
 
 #include "i18n.h"
 
-#include <cmath>
-
-/*
-  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;
@@ -69,8 +56,8 @@ using namespace PBD;
 
 const string                 IO::state_node_name = "IO";
 bool                         IO::connecting_legal = false;
-sigc::signal<int>            IO::ConnectingLegal;
-sigc::signal<void,ChanCount> IO::PortCountChanged;
+PBD::Signal0<int>            IO::ConnectingLegal;
+PBD::Signal1<void,ChanCount> 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).
@@ -82,7 +69,7 @@ IO::IO (Session& s, const string& name, Direction dir, DataType default_type)
 {
        _active = true;
        pending_state_node = 0;
-       setup_bundles ();
+       setup_bundle ();
 }
 
 IO::IO (Session& s, const XMLNode& node, DataType dt)
@@ -93,8 +80,8 @@ IO::IO (Session& s, const XMLNode& node, DataType dt)
        _active = true;
        pending_state_node = 0;
 
-       set_state (node);
-       setup_bundles ();
+       set_state (node, Stateful::loading_state_version);
+       setup_bundle ();
 }
 
 IO::~IO ()
@@ -125,13 +112,13 @@ IO::check_bundles_connected ()
 }
 
 void
-IO::check_bundles (std::vector<UserBundleInfo>& list, const PortSet& ports)
+IO::check_bundles (std::vector<UserBundleInfo*>& list, const PortSet& ports)
 {
-       std::vector<UserBundleInfo> new_list;
+       std::vector<UserBundleInfo*> new_list;
 
-       for (std::vector<UserBundleInfo>::iterator i = list.begin(); i != list.end(); ++i) {
+       for (std::vector<UserBundleInfo*>::iterator i = list.begin(); i != list.end(); ++i) {
 
-               uint32_t const N = i->bundle->nchannels ();
+               uint32_t const N = (*i)->bundle->nchannels ();
 
                if (_ports.num_ports (default_type()) < N) {
                        continue;
@@ -141,7 +128,7 @@ IO::check_bundles (std::vector<UserBundleInfo>& list, const PortSet& ports)
 
                for (uint32_t j = 0; j < N; ++j) {
                        /* Every port on bundle channel j must be connected to our input j */
-                       Bundle::PortList const pl = i->bundle->channel_ports (j);
+                       Bundle::PortList const pl = (*i)->bundle->channel_ports (j);
                        for (uint32_t k = 0; k < pl.size(); ++k) {
                                if (ports.port(j)->connected_to (pl[k]) == false) {
                                        ok = false;
@@ -157,7 +144,7 @@ IO::check_bundles (std::vector<UserBundleInfo>& list, const PortSet& ports)
                if (ok) {
                        new_list.push_back (*i);
                } else {
-                       i->changed.disconnect ();
+                       delete *i;
                }
        }
 
@@ -261,7 +248,7 @@ IO::remove_port (Port* port, void* src)
        }
 
        if (change & ConfigurationChanged) {
-               setup_bundles ();
+               setup_bundle ();
        }
 
        if (change != NoChange) {
@@ -325,7 +312,7 @@ IO::add_port (string destination, void* src, DataType type)
 
        // pan_changed (src); /* EMIT SIGNAL */
        changed (ConfigurationChanged, src); /* EMIT SIGNAL */
-       setup_bundles ();
+       setup_bundle ();
        _session.set_dirty ();
 
        return 0;
@@ -396,7 +383,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);
@@ -440,7 +427,7 @@ IO::ensure_ports (ChanCount count, bool clear, bool lockit, void* src)
 
        if (changed) {
                this->changed (ConfigurationChanged, src); /* EMIT SIGNAL */
-               setup_bundles ();
+               setup_bundle ();
                _session.set_dirty ();
        }
 
@@ -476,9 +463,9 @@ IO::state (bool /*full_state*/)
        node->add_property ("direction", enum_2_string (_direction));
        node->add_property ("default-type", _default_type.to_string());
 
-       for (std::vector<UserBundleInfo>::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) {
+       for (std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) {
                XMLNode* n = new XMLNode ("Bundle");
-               n->add_property ("name", i->bundle->name ());
+               n->add_property ("name", (*i)->bundle->name ());
                node->add_child_nocopy (*n);
        }
 
@@ -515,8 +502,14 @@ IO::state (bool /*full_state*/)
 }
 
 int
-IO::set_state (const XMLNode& node)
+IO::set_state (const XMLNode& node, int version)
 {
+       /* callers for version < 3000 need to call set_state_2X directly, as A3 IOs
+        * are input OR output, not both, so the direction needs to be specified
+        * by the caller.
+        */
+       assert (version >= 3000);
+
        const XMLProperty* prop;
        XMLNodeConstIterator iter;
        LocaleGuard lg (X_("POSIX"));
@@ -547,22 +540,76 @@ IO::set_state (const XMLNode& node)
                _direction = (Direction) string_2_enum (prop->value(), _direction);
        }
 
-       if (create_ports (node)) {
+       if (create_ports (node, version)) {
                return -1;
        }
 
        if (connecting_legal) {
 
-               if (make_connections (node)) {
+               if (make_connections (node, version, false)) {
                        return -1;
                }
 
        } else {
 
                pending_state_node = new XMLNode (node);
-               connection_legal_c = ConnectingLegal.connect (mem_fun (*this, &IO::connecting_became_legal));
+               pending_state_node_version = version;
+               pending_state_node_in = false;
+               ConnectingLegal.connect_same_thread (connection_legal_c, boost::bind (&IO::connecting_became_legal, this));
+       }
+
+
+       return 0;
+}
+
+int
+IO::set_state_2X (const XMLNode& node, int version, bool in)
+{
+       const XMLProperty* prop;
+       XMLNodeConstIterator iter;
+       LocaleGuard lg (X_("POSIX"));
+
+       /* force use of non-localized representation of decimal point,
+          since we use it a lot in XML files and so forth.
+       */
+
+       if (node.name() != state_node_name) {
+               error << string_compose(_("incorrect XML node \"%1\" passed to IO object"), node.name()) << endmsg;
+               return -1;
+       }
+
+       if ((prop = node.property ("name")) != 0) {
+               set_name (prop->value());
+       }
+
+       if ((prop = node.property (X_("default-type"))) != 0) {
+               _default_type = DataType(prop->value());
+               assert(_default_type != DataType::NIL);
+       }
+
+       if ((prop = node.property ("id")) != 0) {
+               _id = prop->value ();
+       }
+
+       _direction = in ? Input : Output;
+
+       if (create_ports (node, version)) {
+               return -1;
        }
 
+       if (connecting_legal) {
+
+               if (make_connections_2X (node, version, in)) {
+                       return -1;
+               }
+
+       } else {
+
+               pending_state_node = new XMLNode (node);
+               pending_state_node_version = version;
+               pending_state_node_in = in;
+               ConnectingLegal.connect_same_thread (connection_legal_c, boost::bind (&IO::connecting_became_legal, this));
+       }
 
        return 0;
 }
@@ -576,7 +623,7 @@ IO::connecting_became_legal ()
 
        connection_legal_c.disconnect ();
 
-       ret = make_connections (*pending_state_node);
+       ret = make_connections (*pending_state_node, pending_state_node_version, pending_state_node_in);
 
        delete pending_state_node;
        pending_state_node = 0;
@@ -616,12 +663,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;
@@ -679,8 +726,40 @@ IO::find_possible_bundle (const string &desired_name)
 }
 
 int
-IO::get_port_counts (const XMLNode& node, ChanCount& n, boost::shared_ptr<Bundle>& c)
+IO::get_port_counts_2X (XMLNode const & node, int /*version*/, ChanCount& n, boost::shared_ptr<Bundle>& /*c*/)
 {
+       XMLProperty const * prop;
+       XMLNodeList children = node.children ();
+
+       uint32_t n_audio = 0;
+
+       for (XMLNodeIterator i = children.begin(); i != children.end(); ++i) {
+
+               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;
+               }
+       }
+
+       ChanCount cnt;
+       cnt.set_audio (n_audio);
+       n = ChanCount::max (n, cnt);
+
+       return 0;
+}
+
+int
+IO::get_port_counts (const XMLNode& node, int version, ChanCount& n, boost::shared_ptr<Bundle>& c)
+{
+       if (version < 3000) {
+               return get_port_counts_2X (node, version, n, c);
+       }
+
        XMLProperty const * prop;
        XMLNodeConstIterator iter;
        uint32_t n_audio = 0;
@@ -728,12 +807,12 @@ IO::get_port_counts (const XMLNode& node, ChanCount& n, boost::shared_ptr<Bundle
 }
 
 int
-IO::create_ports (const XMLNode& node)
+IO::create_ports (const XMLNode& node, int version)
 {
        ChanCount n;
        boost::shared_ptr<Bundle> c;
 
-       get_port_counts (node, n, c);
+       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;
@@ -746,8 +825,12 @@ IO::create_ports (const XMLNode& node)
 }
 
 int
-IO::make_connections (const XMLNode& node)
+IO::make_connections (const XMLNode& node, int version, bool in)
 {
+       if (version < 3000) {
+               return make_connections_2X (node, version, in);
+       }
+
        const XMLProperty* prop;
 
        for (XMLNodeConstIterator i = node.children().begin(); i != node.children().end(); ++i) {
@@ -786,12 +869,107 @@ IO::make_connections (const XMLNode& node)
                                        if ((prop = cnode->property (X_("other"))) == 0) {
                                                continue;
                                        }
-
+                                        
                                        if (prop) {
                                                p->connect (prop->value());
                                        }
                                }
+                       } 
+               }
+       }
+
+       return 0;
+}
+
+
+int
+IO::make_connections_2X (const XMLNode& node, int /*version*/, bool in)
+{
+       const XMLProperty* prop;
+
+       /* XXX: bundles ("connections" as was) */
+
+       if ((prop = node.property ("inputs")) != 0 && in) {
+
+               string::size_type ostart = 0;
+               string::size_type start = 0;
+               string::size_type end = 0;
+               int i = 0;
+               int n;
+               vector<string> ports;
+
+               string const str = prop->value ();
+
+               while ((start = str.find_first_of ('{', ostart)) != string::npos) {
+                       start += 1;
+
+                       if ((end = str.find_first_of ('}', start)) == string::npos) {
+                               error << string_compose(_("IO: badly formed string in XML node for inputs \"%1\""), str) << endmsg;
+                               return -1;
                        }
+
+                       if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
+                               error << string_compose(_("bad input string in XML node \"%1\""), str) << endmsg;
+
+                               return -1;
+
+                       } else if (n > 0) {
+
+
+                               for (int x = 0; x < n; ++x) {
+                                       /* XXX: this is a bit of a hack; need to check if it's always valid */
+                                       string::size_type const p = ports[x].find ("/out");
+                                       if (p != string::npos) {
+                                               ports[x].replace (p, 4, "/audio_out");
+                                       }
+                                       nth(i)->connect (ports[x]);
+                               }
+                       }
+
+                       ostart = end+1;
+                       i++;
+               }
+
+       }
+
+       if ((prop = node.property ("outputs")) != 0 && !in) {
+
+               string::size_type ostart = 0;
+               string::size_type start = 0;
+               string::size_type end = 0;
+               int i = 0;
+               int n;
+               vector<string> ports;
+
+               string const str = prop->value ();
+
+               while ((start = str.find_first_of ('{', ostart)) != string::npos) {
+                       start += 1;
+
+                       if ((end = str.find_first_of ('}', start)) == string::npos) {
+                               error << string_compose(_("IO: badly formed string in XML node for outputs \"%1\""), str) << endmsg;
+                               return -1;
+                       }
+
+                       if ((n = parse_io_string (str.substr (start, end - start), ports)) < 0) {
+                               error << string_compose(_("IO: bad output string in XML node \"%1\""), str) << endmsg;
+
+                               return -1;
+
+                       } else if (n > 0) {
+
+                               for (int x = 0; x < n; ++x) {
+                                       /* XXX: this is a bit of a hack; need to check if it's always valid */
+                                       string::size_type const p = ports[x].find ("/in");
+                                       if (p != string::npos) {
+                                               ports[x].replace (p, 3, "/audio_in");
+                                       }
+                                       nth(i)->connect (ports[x]);
+                               }
+                       }
+
+                       ostart = end+1;
+                       i++;
                }
        }
 
@@ -901,25 +1079,23 @@ 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);
        }
 
        bool const r = SessionObject::set_name (name);
 
-       setup_bundles ();
+       setup_bundle ();
 
        return r;
 }
@@ -978,14 +1154,14 @@ IO::connect_ports_to_bundle (boost::shared_ptr<Bundle> c, void* src)
                if (ub) {
 
                        /* See if we already know about this one */
-                       std::vector<UserBundleInfo>::iterator i = _bundles_connected.begin();
-                       while (i != _bundles_connected.end() && i->bundle != ub) {
+                       std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin();
+                       while (i != _bundles_connected.end() && (*i)->bundle != ub) {
                                ++i;
                        }
 
                        if (i == _bundles_connected.end()) {
                                /* We don't, so make a note */
-                               _bundles_connected.push_back (UserBundleInfo (this, ub));
+                               _bundles_connected.push_back (new UserBundleInfo (this, ub));
                        }
                }
        }
@@ -1008,12 +1184,13 @@ IO::disconnect_ports_from_bundle (boost::shared_ptr<Bundle> c, void* src)
                boost::shared_ptr<UserBundle> ub = boost::dynamic_pointer_cast<UserBundle> (c);
                if (ub) {
 
-                       std::vector<UserBundleInfo>::iterator i = _bundles_connected.begin();
-                       while (i != _bundles_connected.end() && i->bundle != ub) {
+                       std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin();
+                       while (i != _bundles_connected.end() && (*i)->bundle != ub) {
                                ++i;
                        }
 
                        if (i != _bundles_connected.end()) {
+                               delete *i;
                                _bundles_connected.erase (i);
                        }
                }
@@ -1035,7 +1212,8 @@ int
 IO::enable_connecting ()
 {
        connecting_legal = true;
-       return ConnectingLegal ();
+       boost::optional<int> r = ConnectingLegal ();
+       return r.get_value_or (0);
 }
 
 void
@@ -1080,7 +1258,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);
@@ -1136,11 +1314,11 @@ IO::midi(uint32_t n) const
 }
 
 /**
- *  Setup bundles that describe our inputs and outputs. Also creates bundles if necessary.
+ *  Setup a bundle that describe our inputs or outputs. Also creates the bundle if necessary.
  */
 
 void
-IO::setup_bundles ()
+IO::setup_bundle ()
 {
         char buf[32];
 
@@ -1155,9 +1333,9 @@ IO::setup_bundles ()
        _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();
@@ -1176,8 +1354,8 @@ IO::bundles_connected ()
        BundleList bundles;
 
        /* User bundles */
-       for (std::vector<UserBundleInfo>::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) {
-               bundles.push_back (i->bundle);
+       for (std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) {
+               bundles.push_back ((*i)->bundle);
        }
 
        /* Session bundles */
@@ -1213,9 +1391,7 @@ IO::bundles_connected ()
 IO::UserBundleInfo::UserBundleInfo (IO* io, boost::shared_ptr<UserBundle> b)
 {
        bundle = b;
-       changed = b->Changed.connect (
-               sigc::mem_fun (*io, &IO::bundle_changed)
-               );
+       b->Changed.connect_same_thread (changed, boost::bind (&IO::bundle_changed, io, _1));
 }
 
 std::string
@@ -1258,19 +1434,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<const IO> 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());
@@ -1298,7 +1480,7 @@ IO::process_input (boost::shared_ptr<Processor> proc, sframes_t start_frame, sfr
        /* 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);
+       proc->run (bufs, start_frame, end_frame, nframes, true);
 }
 
 void
@@ -1371,3 +1553,15 @@ 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;
+}