Fix incorrect VST callback fall-though (effective NOOP)
[ardour.git] / libs / ardour / bundle.cc
index f90b84ff2512e321a7ea107836e21aac6ed93e5f..65f0c8e127e468da830916ee799e27b4a235fe47 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2002 Paul Davis 
+    Copyright (C) 2002 Paul Davis
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
 
 #include <algorithm>
 
-#include <pbd/failed_constructor.h>
-#include <ardour/ardour.h>
-#include <ardour/bundle.h>
-#include <ardour/audioengine.h>
-#include <pbd/xml++.h>
+#include "ardour/bundle.h"
+#include "ardour/audioengine.h"
+#include "ardour/port.h"
 
-#include "i18n.h"
+#include "pbd/i18n.h"
 
+using namespace std;
 using namespace ARDOUR;
 using namespace PBD;
 
@@ -34,8 +33,7 @@ using namespace PBD;
  *  @param i true if ports are inputs, otherwise false.
  */
 Bundle::Bundle (bool i)
-       : _type (DataType::AUDIO),
-         _ports_are_inputs (i),
+       : _ports_are_inputs (i),
          _signals_suspended (false),
          _pending_change (Change (0))
 {
@@ -49,7 +47,6 @@ Bundle::Bundle (bool i)
  */
 Bundle::Bundle (std::string const & n, bool i)
        : _name (n),
-         _type (DataType::AUDIO),
          _ports_are_inputs (i),
          _signals_suspended (false),
          _pending_change (Change (0))
@@ -57,47 +54,42 @@ Bundle::Bundle (std::string const & n, bool i)
 
 }
 
-
-/** Construct a bundle.
- *  @param n Name.
- *  @param t Type.
- *  @param i true if ports are inputs, otherwise false.
- */
-Bundle::Bundle (std::string const & n, DataType t, bool i)
-       : _name (n),
-         _type (t),
-         _ports_are_inputs (i),
-         _signals_suspended (false),
-         _pending_change (Change (0))
-{
-
-}
-
-
 Bundle::Bundle (boost::shared_ptr<Bundle> other)
        : _channel (other->_channel),
          _name (other->_name),
-         _type (other->_type),
          _ports_are_inputs (other->_ports_are_inputs),
          _signals_suspended (other->_signals_suspended),
          _pending_change (other->_pending_change)
 {
-       
+
 }
 
-uint32_t
+ChanCount
 Bundle::nchannels () const
 {
-       Glib::Mutex::Lock lm (_channel_mutex);
-       return _channel.size ();
+       Glib::Threads::Mutex::Lock lm (_channel_mutex);
+
+       ChanCount c;
+       for (vector<Channel>::const_iterator i = _channel.begin(); i != _channel.end(); ++i) {
+               c.set (i->type, c.get (i->type) + 1);
+       }
+
+       return c;
+}
+
+uint32_t
+Bundle::n_total () const
+{
+    /* Simpler and far more efficient than nchannels.n_total() */
+    return _channel.size();
 }
 
 Bundle::PortList const &
 Bundle::channel_ports (uint32_t c) const
 {
-       assert (c < nchannels());
+       assert (c < n_total());
 
-       Glib::Mutex::Lock lm (_channel_mutex);
+       Glib::Threads::Mutex::Lock lm (_channel_mutex);
        return _channel[c].ports;
 }
 
@@ -108,11 +100,11 @@ Bundle::channel_ports (uint32_t c) const
 void
 Bundle::add_port_to_channel (uint32_t ch, string portname)
 {
-       assert (ch < nchannels());
+       assert (ch < n_total());
        assert (portname.find_first_of (':') != string::npos);
 
        {
-               Glib::Mutex::Lock lm (_channel_mutex);
+               Glib::Threads::Mutex::Lock lm (_channel_mutex);
                _channel[ch].ports.push_back (portname);
        }
 
@@ -126,15 +118,15 @@ Bundle::add_port_to_channel (uint32_t ch, string portname)
 void
 Bundle::remove_port_from_channel (uint32_t ch, string portname)
 {
-       assert (ch < nchannels());
+       assert (ch < n_total());
 
        bool changed = false;
 
        {
-               Glib::Mutex::Lock lm (_channel_mutex);
+               Glib::Threads::Mutex::Lock lm (_channel_mutex);
                PortList& pl = _channel[ch].ports;
                PortList::iterator i = find (pl.begin(), pl.end(), portname);
-               
+
                if (i != pl.end()) {
                        pl.erase (i);
                        changed = true;
@@ -153,11 +145,11 @@ Bundle::remove_port_from_channel (uint32_t ch, string portname)
 void
 Bundle::set_port (uint32_t ch, string portname)
 {
-       assert (ch < nchannels());
+       assert (ch < n_total());
        assert (portname.find_first_of (':') != string::npos);
 
        {
-               Glib::Mutex::Lock lm (_channel_mutex);
+               Glib::Threads::Mutex::Lock lm (_channel_mutex);
                _channel[ch].ports.clear ();
                _channel[ch].ports.push_back (portname);
        }
@@ -167,11 +159,35 @@ Bundle::set_port (uint32_t ch, string portname)
 
 /** @param n Channel name */
 void
-Bundle::add_channel (std::string const & n)
+Bundle::add_channel (std::string const & n, DataType t)
+{
+       {
+               Glib::Threads::Mutex::Lock lm (_channel_mutex);
+               _channel.push_back (Channel (n, t));
+       }
+
+       emit_changed (ConfigurationChanged);
+}
+
+/** @param n Channel name */
+void
+Bundle::add_channel (std::string const & n, DataType t, PortList p)
+{
+       {
+               Glib::Threads::Mutex::Lock lm (_channel_mutex);
+               _channel.push_back (Channel (n, t, p));
+       }
+
+       emit_changed (ConfigurationChanged);
+}
+
+/** @param n Channel name */
+void
+Bundle::add_channel (std::string const & n, DataType t, std::string const & p)
 {
        {
-               Glib::Mutex::Lock lm (_channel_mutex);
-               _channel.push_back (Channel (n));
+               Glib::Threads::Mutex::Lock lm (_channel_mutex);
+               _channel.push_back (Channel (n, t, p));
        }
 
        emit_changed (ConfigurationChanged);
@@ -180,9 +196,9 @@ Bundle::add_channel (std::string const & n)
 bool
 Bundle::port_attached_to_channel (uint32_t ch, std::string portname)
 {
-       assert (ch < nchannels());
-       
-       Glib::Mutex::Lock lm (_channel_mutex);
+       assert (ch < n_total());
+
+       Glib::Threads::Mutex::Lock lm (_channel_mutex);
        return (std::find (_channel[ch].ports.begin (), _channel[ch].ports.end (), portname) != _channel[ch].ports.end ());
 }
 
@@ -192,28 +208,34 @@ Bundle::port_attached_to_channel (uint32_t ch, std::string portname)
 void
 Bundle::remove_channel (uint32_t ch)
 {
-       assert (ch < nchannels ());
+       assert (ch < n_total());
 
-       Glib::Mutex::Lock lm (_channel_mutex);
+       Glib::Threads::Mutex::Lock lm (_channel_mutex);
        _channel.erase (_channel.begin () + ch);
+
+       lm.release();
+       emit_changed (ConfigurationChanged);
 }
 
 /** Remove all channels */
 void
 Bundle::remove_channels ()
 {
-       Glib::Mutex::Lock lm (_channel_mutex);
+       Glib::Threads::Mutex::Lock lm (_channel_mutex);
 
        _channel.clear ();
+
+       lm.release();
+       emit_changed (ConfigurationChanged);
 }
 
 /** @param p Port name.
  *  @return true if any channel is associated with p.
  */
 bool
-Bundle::uses_port (std::string p) const
+Bundle::offers_port (std::string p) const
 {
-       Glib::Mutex::Lock lm (_channel_mutex);
+       Glib::Threads::Mutex::Lock lm (_channel_mutex);
 
        for (std::vector<Channel>::const_iterator i = _channel.begin(); i != _channel.end(); ++i) {
                for (PortList::const_iterator j = i->ports.begin(); j != i->ports.end(); ++j) {
@@ -232,7 +254,7 @@ Bundle::uses_port (std::string p) const
 bool
 Bundle::offers_port_alone (std::string p) const
 {
-       Glib::Mutex::Lock lm (_channel_mutex);
+       Glib::Threads::Mutex::Lock lm (_channel_mutex);
 
        for (std::vector<Channel>::const_iterator i = _channel.begin(); i != _channel.end(); ++i) {
                if (i->ports.size() == 1 && i->ports[0] == p) {
@@ -250,9 +272,9 @@ Bundle::offers_port_alone (std::string p) const
 std::string
 Bundle::channel_name (uint32_t ch) const
 {
-       assert (ch < nchannels());
+       assert (ch < n_total());
 
-       Glib::Mutex::Lock lm (_channel_mutex);
+       Glib::Threads::Mutex::Lock lm (_channel_mutex);
        return _channel[ch].name;
 }
 
@@ -263,10 +285,10 @@ Bundle::channel_name (uint32_t ch) const
 void
 Bundle::set_channel_name (uint32_t ch, std::string const & n)
 {
-       assert (ch < nchannels());
+       assert (ch < n_total());
 
        {
-               Glib::Mutex::Lock lm (_channel_mutex);
+               Glib::Threads::Mutex::Lock lm (_channel_mutex);
                _channel[ch].name = n;
        }
 
@@ -280,14 +302,14 @@ Bundle::set_channel_name (uint32_t ch, std::string const & n)
 void
 Bundle::add_channels_from_bundle (boost::shared_ptr<Bundle> other)
 {
-       uint32_t const ch = nchannels ();
-       
-       for (uint32_t i = 0; i < other->nchannels(); ++i) {
+       uint32_t const ch = n_total();
+
+       for (uint32_t i = 0; i < other->n_total(); ++i) {
 
                std::stringstream s;
                s << other->name() << " " << other->channel_name(i);
 
-               add_channel (s.str());
+               add_channel (s.str(), other->channel_type(i));
 
                PortList const& pl = other->channel_ports (i);
                for (uint32_t j = 0; j < pl.size(); ++j) {
@@ -300,20 +322,37 @@ Bundle::add_channels_from_bundle (boost::shared_ptr<Bundle> other)
  *  with another bundle's channels.
  *  @param other Other bundle.
  *  @param engine AudioEngine to use to make the connections.
+ *  @param allow_partial whether to allow leaving unconnected channels types,
+ *              or require that the ChanCounts match exactly (default false).
  */
 void
-Bundle::connect (boost::shared_ptr<Bundle> other, AudioEngine & engine)
+Bundle::connect (boost::shared_ptr<Bundle> other, AudioEngine & engine,
+                 bool allow_partial)
 {
-       uint32_t const N = nchannels ();
-       assert (N == other->nchannels ());
+       ChanCount our_count = nchannels();
+       ChanCount other_count = other->nchannels();
 
-       for (uint32_t i = 0; i < N; ++i) {
-               Bundle::PortList const & our_ports = channel_ports (i);
-               Bundle::PortList const & other_ports = other->channel_ports (i);
+       if (!allow_partial && our_count != other_count) {
+               assert (our_count == other_count);
+               return;
+       }
 
-               for (Bundle::PortList::const_iterator j = our_ports.begin(); j != our_ports.end(); ++j) {
-                       for (Bundle::PortList::const_iterator k = other_ports.begin(); k != other_ports.end(); ++k) {
-                               engine.connect (*j, *k);
+       for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
+               uint32_t N = our_count.n(*t);
+               if (N != other_count.n(*t))
+                       continue;
+               for (uint32_t i = 0; i < N; ++i) {
+                       Bundle::PortList const & our_ports =
+                               channel_ports (type_channel_to_overall(*t, i));
+                       Bundle::PortList const & other_ports =
+                               other->channel_ports (other->type_channel_to_overall(*t, i));
+
+                       for (Bundle::PortList::const_iterator j = our_ports.begin();
+                                               j != our_ports.end(); ++j) {
+                               for (Bundle::PortList::const_iterator k = other_ports.begin();
+                                                       k != other_ports.end(); ++k) {
+                                       engine.connect (*j, *k);
+                               }
                        }
                }
        }
@@ -322,16 +361,23 @@ Bundle::connect (boost::shared_ptr<Bundle> other, AudioEngine & engine)
 void
 Bundle::disconnect (boost::shared_ptr<Bundle> other, AudioEngine & engine)
 {
-       uint32_t const N = nchannels ();
-       assert (N == other->nchannels ());
-
-       for (uint32_t i = 0; i < N; ++i) {
-               Bundle::PortList const & our_ports = channel_ports (i);
-               Bundle::PortList const & other_ports = other->channel_ports (i);
-
-               for (Bundle::PortList::const_iterator j = our_ports.begin(); j != our_ports.end(); ++j) {
-                       for (Bundle::PortList::const_iterator k = other_ports.begin(); k != other_ports.end(); ++k) {
-                               engine.disconnect (*j, *k);
+       ChanCount our_count = nchannels();
+       ChanCount other_count = other->nchannels();
+
+       for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
+               uint32_t N = min(our_count.n(*t), other_count.n(*t));
+               for (uint32_t i = 0; i < N; ++i) {
+                       Bundle::PortList const & our_ports =
+                               channel_ports (type_channel_to_overall(*t, i));
+                       Bundle::PortList const & other_ports =
+                               other->channel_ports (other->type_channel_to_overall(*t, i));
+
+                       for (Bundle::PortList::const_iterator j = our_ports.begin();
+                                               j != our_ports.end(); ++j) {
+                               for (Bundle::PortList::const_iterator k = other_ports.begin();
+                                                       k != other_ports.end(); ++k) {
+                                       engine.disconnect (*j, *k);
+                               }
                        }
                }
        }
@@ -342,8 +388,8 @@ void
 Bundle::remove_ports_from_channels ()
 {
        {
-               Glib::Mutex::Lock lm (_channel_mutex);
-               for (uint32_t c = 0; c < _channel.size(); ++c) {
+               Glib::Threads::Mutex::Lock lm (_channel_mutex);
+               for (uint32_t c = 0; c < n_total(); ++c) {
                        _channel[c].ports.clear ();
                }
 
@@ -358,10 +404,10 @@ Bundle::remove_ports_from_channels ()
 void
 Bundle::remove_ports_from_channel (uint32_t ch)
 {
-       assert (ch < nchannels ());
-       
+       assert (ch < n_total());
+
        {
-               Glib::Mutex::Lock lm (_channel_mutex);
+               Glib::Threads::Mutex::Lock lm (_channel_mutex);
                _channel[ch].ports.clear ();
        }
 
@@ -394,4 +440,248 @@ Bundle::emit_changed (Change c)
                Changed (c);
        }
 }
-               
+
+/** This must not be called in code executed as a response to a backend event,
+ *  as it may query the backend in the same thread where it's waiting for us.
+ * @return true if a Bundle is connected to another.
+ * @param type: if not NIL, restrict the check to channels of that type.
+ * @param exclusive: if true, additionally check if the bundle is connected
+ *                   only to |other|, and return false if not. */
+bool
+Bundle::connected_to (boost::shared_ptr<Bundle> other, AudioEngine & engine,
+                      DataType type, bool exclusive)
+{
+       if (_ports_are_inputs == other->_ports_are_inputs)
+               return false;
+
+       if (type == DataType::NIL) {
+               for (DataType::iterator t = DataType::begin();
+                                       t != DataType::end(); ++t) {
+                       if (!connected_to(other, engine, *t, exclusive))
+                               return false;
+               }
+               return true;
+       }
+
+       uint32_t N = nchannels().n(type);
+       if (other->nchannels().n(type) != N)
+               return false;
+
+       vector<string> port_connections;
+
+       for (uint32_t i = 0; i < N; ++i) {
+               Bundle::PortList const & our_ports =
+                       channel_ports (type_channel_to_overall(type, i));
+               Bundle::PortList const & other_ports =
+                       other->channel_ports (other->type_channel_to_overall(type, i));
+
+               for (Bundle::PortList::const_iterator j = our_ports.begin();
+                                                     j != our_ports.end(); ++j) {
+                       boost::shared_ptr<Port> p = engine.get_port_by_name(*j);
+
+                       for (Bundle::PortList::const_iterator k = other_ports.begin();
+                                                          k != other_ports.end(); ++k) {
+                               boost::shared_ptr<Port> q = engine.get_port_by_name(*k);
+
+                               if (!p && !q) {
+                                       return false;
+                               }
+
+                               if (p && !p->connected_to (*k)) {
+                                       return false;
+                               } else if (q && !q->connected_to (*j)) {
+                                       return false;
+                               }
+                       }
+
+                       if (exclusive && p) {
+                               port_connections.clear();
+                               p->get_connections(port_connections);
+                               if (port_connections.size() != other_ports.size())
+                                       return false;
+                       }
+               }
+       }
+
+       return true;
+}
+
+/** This must not be called in code executed as a response to a backend event,
+ *  as it uses the backend port_get_all_connections().
+ *  @return true if any of this bundle's channels are connected to anything.
+ */
+bool
+Bundle::connected_to_anything (AudioEngine& engine)
+{
+       PortManager& pm (engine);
+
+       for (uint32_t i = 0; i < n_total(); ++i) {
+               Bundle::PortList const & ports = channel_ports (i);
+
+               for (uint32_t j = 0; j < ports.size(); ++j) {
+
+                       /* ports[j] may not be an Ardour port, so use the port manager directly
+                          rather than doing it with Port.
+                       */
+
+                       if (pm.connected (ports[j])) {
+                               return true;
+                       }
+               }
+       }
+
+       return false;
+}
+
+void
+Bundle::set_ports_are_inputs ()
+{
+       _ports_are_inputs = true;
+       emit_changed (DirectionChanged);
+}
+
+void
+Bundle::set_ports_are_outputs ()
+{
+       _ports_are_inputs = false;
+       emit_changed (DirectionChanged);
+}
+
+/** Set the name.
+ *  @param n New name.
+ */
+void
+Bundle::set_name (string const & n)
+{
+       _name = n;
+       emit_changed (NameChanged);
+}
+
+/** @param b Other bundle.
+ *  @return true if b has the same number of channels as this bundle, and those channels have corresponding ports.
+ */
+bool
+Bundle::has_same_ports (boost::shared_ptr<Bundle> b) const
+{
+       ChanCount our_count = nchannels();
+       ChanCount other_count = b->nchannels();
+
+       if (our_count != other_count)
+               return false;
+
+       for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
+               uint32_t N = our_count.n(*t);
+               for (uint32_t i = 0; i < N; ++i) {
+                       Bundle::PortList const & our_ports =
+                               channel_ports (type_channel_to_overall(*t, i));
+                       Bundle::PortList const & other_ports =
+                               b->channel_ports (b->type_channel_to_overall(*t, i));
+
+                       if (our_ports != other_ports)
+                               return false;
+               }
+       }
+
+       return true;
+}
+
+DataType
+Bundle::channel_type (uint32_t c) const
+{
+       assert (c < n_total());
+
+       Glib::Threads::Mutex::Lock lm (_channel_mutex);
+       return _channel[c].type;
+}
+
+ostream &
+operator<< (ostream& os, Bundle const & b)
+{
+       os << "BUNDLE " << b.nchannels() << " channels: ";
+       for (uint32_t i = 0; i < b.n_total(); ++i) {
+               os << "( ";
+               Bundle::PortList const & pl = b.channel_ports (i);
+               for (Bundle::PortList::const_iterator j = pl.begin(); j != pl.end(); ++j) {
+                       os << *j << " ";
+               }
+               os << ") ";
+       }
+
+       return os;
+}
+
+bool
+Bundle::operator== (Bundle const & other)
+{
+       return _channel == other._channel;
+}
+
+/** Given an index of a channel as the nth channel of a particular type,
+ *  return an index of that channel when considering channels of all types.
+ *
+ *  e.g. given a bundle with channels:
+ *          fred   [audio]
+ *          jim    [audio]
+ *          sheila [midi]
+ *
+ * If t == MIDI and c == 0, then we would return 2, as 2 is the index of the
+ * 0th MIDI channel.
+ *
+ * If t == NIL, we just return c.
+ */
+
+uint32_t
+Bundle::type_channel_to_overall (DataType t, uint32_t c) const
+{
+       if (t == DataType::NIL) {
+               return c;
+       }
+
+       Glib::Threads::Mutex::Lock lm (_channel_mutex);
+
+       vector<Channel>::const_iterator i = _channel.begin ();
+
+       uint32_t o = 0;
+
+       while (1) {
+
+               assert (i != _channel.end ());
+
+               if (i->type != t) {
+                       ++i;
+               } else {
+                       if (c == 0) {
+                               return o;
+                       }
+                       --c;
+               }
+
+               ++o;
+       }
+
+       abort(); /* NOTREACHED */
+       return -1;
+}
+
+/** Perform the reverse of type_channel_to_overall */
+uint32_t
+Bundle::overall_channel_to_type (DataType t, uint32_t c) const
+{
+       if (t == DataType::NIL) {
+               return c;
+       }
+
+       Glib::Threads::Mutex::Lock lm (_channel_mutex);
+
+       uint32_t s = 0;
+
+       vector<Channel>::const_iterator i = _channel.begin ();
+       for (uint32_t j = 0; j < c; ++j) {
+               if (i->type == t) {
+                       ++s;
+               }
+               ++i;
+       }
+
+       return s;
+}