X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fardour%2Fbundle.cc;h=0ac62d70762d6a78fbccd8b692bfecee97728810;hb=ef0c4ed0e6907ff7cc0c9f139495619a9f242ff5;hp=cd416c6a6775ce3935a7cf2bb64365d635669df6;hpb=bb9cc45cd22af67ac275a5e73accbe14fee664d8;p=ardour.git diff --git a/libs/ardour/bundle.cc b/libs/ardour/bundle.cc index cd416c6a67..0ac62d7076 100644 --- a/libs/ardour/bundle.cc +++ b/libs/ardour/bundle.cc @@ -19,12 +19,9 @@ #include -#include "pbd/failed_constructor.h" -#include "ardour/ardour.h" #include "ardour/bundle.h" #include "ardour/audioengine.h" #include "ardour/port.h" -#include "pbd/xml++.h" #include "i18n.h" @@ -36,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)) { @@ -51,23 +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)) -{ - -} - - -/** 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)) @@ -75,11 +54,9 @@ Bundle::Bundle (std::string const & n, DataType t, bool i) } - Bundle::Bundle (boost::shared_ptr 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) @@ -87,19 +64,25 @@ Bundle::Bundle (boost::shared_ptr other) } -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::const_iterator i = _channel.begin(); i != _channel.end(); ++i) { + c.set (i->type, c.get (i->type) + 1); + } + + return c; } Bundle::PortList const & Bundle::channel_ports (uint32_t c) const { - assert (c < nchannels()); + assert (c < nchannels().n_total()); - Glib::Mutex::Lock lm (_channel_mutex); + Glib::Threads::Mutex::Lock lm (_channel_mutex); return _channel[c].ports; } @@ -110,11 +93,11 @@ Bundle::channel_ports (uint32_t c) const void Bundle::add_port_to_channel (uint32_t ch, string portname) { - assert (ch < nchannels()); + assert (ch < nchannels().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); } @@ -128,12 +111,12 @@ 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 < nchannels().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); @@ -155,11 +138,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 < nchannels().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); } @@ -169,11 +152,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::Mutex::Lock lm (_channel_mutex); - _channel.push_back (Channel (n)); + 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::Threads::Mutex::Lock lm (_channel_mutex); + _channel.push_back (Channel (n, t, p)); } emit_changed (ConfigurationChanged); @@ -182,9 +189,9 @@ Bundle::add_channel (std::string const & n) bool Bundle::port_attached_to_channel (uint32_t ch, std::string portname) { - assert (ch < nchannels()); + assert (ch < nchannels().n_total()); - Glib::Mutex::Lock lm (_channel_mutex); + Glib::Threads::Mutex::Lock lm (_channel_mutex); return (std::find (_channel[ch].ports.begin (), _channel[ch].ports.end (), portname) != _channel[ch].ports.end ()); } @@ -194,28 +201,34 @@ Bundle::port_attached_to_channel (uint32_t ch, std::string portname) void Bundle::remove_channel (uint32_t ch) { - assert (ch < nchannels ()); + assert (ch < nchannels().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::const_iterator i = _channel.begin(); i != _channel.end(); ++i) { for (PortList::const_iterator j = i->ports.begin(); j != i->ports.end(); ++j) { @@ -234,7 +247,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::const_iterator i = _channel.begin(); i != _channel.end(); ++i) { if (i->ports.size() == 1 && i->ports[0] == p) { @@ -252,9 +265,9 @@ Bundle::offers_port_alone (std::string p) const std::string Bundle::channel_name (uint32_t ch) const { - assert (ch < nchannels()); + assert (ch < nchannels().n_total()); - Glib::Mutex::Lock lm (_channel_mutex); + Glib::Threads::Mutex::Lock lm (_channel_mutex); return _channel[ch].name; } @@ -265,10 +278,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 < nchannels().n_total()); { - Glib::Mutex::Lock lm (_channel_mutex); + Glib::Threads::Mutex::Lock lm (_channel_mutex); _channel[ch].name = n; } @@ -282,14 +295,14 @@ Bundle::set_channel_name (uint32_t ch, std::string const & n) void Bundle::add_channels_from_bundle (boost::shared_ptr other) { - uint32_t const ch = nchannels (); + uint32_t const ch = nchannels().n_total(); - for (uint32_t i = 0; i < other->nchannels(); ++i) { + for (uint32_t i = 0; i < other->nchannels().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) { @@ -306,8 +319,8 @@ Bundle::add_channels_from_bundle (boost::shared_ptr other) void Bundle::connect (boost::shared_ptr other, AudioEngine & engine) { - uint32_t const N = nchannels (); - assert (N == other->nchannels ()); + uint32_t const N = nchannels().n_total(); + assert (N == other->nchannels().n_total()); for (uint32_t i = 0; i < N; ++i) { Bundle::PortList const & our_ports = channel_ports (i); @@ -324,8 +337,8 @@ Bundle::connect (boost::shared_ptr other, AudioEngine & engine) void Bundle::disconnect (boost::shared_ptr other, AudioEngine & engine) { - uint32_t const N = nchannels (); - assert (N == other->nchannels ()); + uint32_t const N = nchannels().n_total(); + assert (N == other->nchannels().n_total()); for (uint32_t i = 0; i < N; ++i) { Bundle::PortList const & our_ports = channel_ports (i); @@ -344,7 +357,7 @@ void Bundle::remove_ports_from_channels () { { - Glib::Mutex::Lock lm (_channel_mutex); + Glib::Threads::Mutex::Lock lm (_channel_mutex); for (uint32_t c = 0; c < _channel.size(); ++c) { _channel[c].ports.clear (); } @@ -360,10 +373,10 @@ Bundle::remove_ports_from_channels () void Bundle::remove_ports_from_channel (uint32_t ch) { - assert (ch < nchannels ()); + assert (ch < nchannels().n_total()); { - Glib::Mutex::Lock lm (_channel_mutex); + Glib::Threads::Mutex::Lock lm (_channel_mutex); _channel[ch].ports.clear (); } @@ -400,22 +413,19 @@ Bundle::emit_changed (Change c) bool Bundle::connected_to (boost::shared_ptr other, AudioEngine & engine) { - if (_ports_are_inputs == other->_ports_are_inputs || - _type != other->_type || - nchannels() != other->nchannels ()) { - + if (_ports_are_inputs == other->_ports_are_inputs || nchannels() != other->nchannels()) { return false; } - for (uint32_t i = 0; i < nchannels(); ++i) { + for (uint32_t i = 0; i < nchannels().n_total(); ++i) { Bundle::PortList const & A = channel_ports (i); Bundle::PortList const & B = other->channel_ports (i); for (uint32_t j = 0; j < A.size(); ++j) { for (uint32_t k = 0; k < B.size(); ++k) { - Port* p = engine.get_port_by_name (A[j]); - Port* q = engine.get_port_by_name (B[k]); + boost::shared_ptr p = engine.get_port_by_name (A[j]); + boost::shared_ptr q = engine.get_port_by_name (B[k]); if (!p && !q) { return false; @@ -432,3 +442,179 @@ Bundle::connected_to (boost::shared_ptr other, AudioEngine & engine) return true; } + +/** This must not be called in code executed as a response to a JACK event, + * as it uses jack_port_get_all_connections(). + * @return true if any of this bundle's channels are connected to anything. + */ +bool +Bundle::connected_to_anything (AudioEngine& engine) +{ + for (uint32_t i = 0; i < nchannels().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 JACK directly + rather than doing it with Port. + */ + jack_port_t* jp = jack_port_by_name (engine.jack(), ports[j].c_str()); + if (jp) { + const char ** c = jack_port_get_all_connections (engine.jack(), jp); + if (c) { + jack_free (c); + 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 b) const +{ + uint32_t const N = nchannels().n_total(); + + if (b->nchannels().n_total() != N) { + return false; + } + + /* XXX: probably should sort channel port lists before comparing them */ + + for (uint32_t i = 0; i < N; ++i) { + if (channel_ports (i) != b->channel_ports (i)) { + return false; + } + } + + return true; +} + +DataType +Bundle::channel_type (uint32_t c) const +{ + assert (c < nchannels().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.nchannels().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::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; + } + + /* 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::const_iterator i = _channel.begin (); + for (uint32_t j = 0; j < c; ++j) { + if (i->type == t) { + ++s; + } + ++i; + } + + return s; +}