2 Copyright (C) 2002 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.
22 #include "ardour/bundle.h"
23 #include "ardour/audioengine.h"
24 #include "ardour/port.h"
29 using namespace ARDOUR;
32 /** Construct an audio bundle.
33 * @param i true if ports are inputs, otherwise false.
35 Bundle::Bundle (bool i)
36 : _ports_are_inputs (i),
37 _signals_suspended (false),
38 _pending_change (Change (0))
44 /** Construct an audio bundle.
46 * @param i true if ports are inputs, otherwise false.
48 Bundle::Bundle (std::string const & n, bool i)
50 _ports_are_inputs (i),
51 _signals_suspended (false),
52 _pending_change (Change (0))
57 Bundle::Bundle (boost::shared_ptr<Bundle> other)
58 : _channel (other->_channel),
60 _ports_are_inputs (other->_ports_are_inputs),
61 _signals_suspended (other->_signals_suspended),
62 _pending_change (other->_pending_change)
68 Bundle::nchannels () const
70 Glib::Threads::Mutex::Lock lm (_channel_mutex);
73 for (vector<Channel>::const_iterator i = _channel.begin(); i != _channel.end(); ++i) {
74 c.set (i->type, c.get (i->type) + 1);
80 Bundle::PortList const &
81 Bundle::channel_ports (uint32_t c) const
83 assert (c < nchannels().n_total());
85 Glib::Threads::Mutex::Lock lm (_channel_mutex);
86 return _channel[c].ports;
89 /** Add an association between one of our channels and a port.
90 * @param ch Channel index.
91 * @param portname full port name to associate with (including prefix).
94 Bundle::add_port_to_channel (uint32_t ch, string portname)
96 assert (ch < nchannels().n_total());
97 assert (portname.find_first_of (':') != string::npos);
100 Glib::Threads::Mutex::Lock lm (_channel_mutex);
101 _channel[ch].ports.push_back (portname);
104 emit_changed (PortsChanged);
107 /** Disassociate a port from one of our channels.
108 * @param ch Channel index.
109 * @param portname port name to disassociate from.
112 Bundle::remove_port_from_channel (uint32_t ch, string portname)
114 assert (ch < nchannels().n_total());
116 bool changed = false;
119 Glib::Threads::Mutex::Lock lm (_channel_mutex);
120 PortList& pl = _channel[ch].ports;
121 PortList::iterator i = find (pl.begin(), pl.end(), portname);
130 emit_changed (PortsChanged);
134 /** Set a single port to be associated with a channel, removing any others.
136 * @param portname Full port name, including prefix.
139 Bundle::set_port (uint32_t ch, string portname)
141 assert (ch < nchannels().n_total());
142 assert (portname.find_first_of (':') != string::npos);
145 Glib::Threads::Mutex::Lock lm (_channel_mutex);
146 _channel[ch].ports.clear ();
147 _channel[ch].ports.push_back (portname);
150 emit_changed (PortsChanged);
153 /** @param n Channel name */
155 Bundle::add_channel (std::string const & n, DataType t)
158 Glib::Threads::Mutex::Lock lm (_channel_mutex);
159 _channel.push_back (Channel (n, t));
162 emit_changed (ConfigurationChanged);
165 /** @param n Channel name */
167 Bundle::add_channel (std::string const & n, DataType t, PortList p)
170 Glib::Threads::Mutex::Lock lm (_channel_mutex);
171 _channel.push_back (Channel (n, t, p));
174 emit_changed (ConfigurationChanged);
177 /** @param n Channel name */
179 Bundle::add_channel (std::string const & n, DataType t, std::string const & p)
182 Glib::Threads::Mutex::Lock lm (_channel_mutex);
183 _channel.push_back (Channel (n, t, p));
186 emit_changed (ConfigurationChanged);
190 Bundle::port_attached_to_channel (uint32_t ch, std::string portname)
192 assert (ch < nchannels().n_total());
194 Glib::Threads::Mutex::Lock lm (_channel_mutex);
195 return (std::find (_channel[ch].ports.begin (), _channel[ch].ports.end (), portname) != _channel[ch].ports.end ());
198 /** Remove a channel.
202 Bundle::remove_channel (uint32_t ch)
204 assert (ch < nchannels().n_total());
206 Glib::Threads::Mutex::Lock lm (_channel_mutex);
207 _channel.erase (_channel.begin () + ch);
210 emit_changed (ConfigurationChanged);
213 /** Remove all channels */
215 Bundle::remove_channels ()
217 Glib::Threads::Mutex::Lock lm (_channel_mutex);
222 emit_changed (ConfigurationChanged);
225 /** @param p Port name.
226 * @return true if any channel is associated with p.
229 Bundle::offers_port (std::string p) const
231 Glib::Threads::Mutex::Lock lm (_channel_mutex);
233 for (std::vector<Channel>::const_iterator i = _channel.begin(); i != _channel.end(); ++i) {
234 for (PortList::const_iterator j = i->ports.begin(); j != i->ports.end(); ++j) {
244 /** @param p Port name.
245 * @return true if this bundle offers this port on its own on a channel.
248 Bundle::offers_port_alone (std::string p) const
250 Glib::Threads::Mutex::Lock lm (_channel_mutex);
252 for (std::vector<Channel>::const_iterator i = _channel.begin(); i != _channel.end(); ++i) {
253 if (i->ports.size() == 1 && i->ports[0] == p) {
262 /** @param ch Channel.
263 * @return Channel name.
266 Bundle::channel_name (uint32_t ch) const
268 assert (ch < nchannels().n_total());
270 Glib::Threads::Mutex::Lock lm (_channel_mutex);
271 return _channel[ch].name;
274 /** Set the name of a channel.
279 Bundle::set_channel_name (uint32_t ch, std::string const & n)
281 assert (ch < nchannels().n_total());
284 Glib::Threads::Mutex::Lock lm (_channel_mutex);
285 _channel[ch].name = n;
288 emit_changed (NameChanged);
291 /** Take the channels from another bundle and add them to this bundle,
292 * so that channels from other are added to this (with their ports)
293 * and are named "<other_bundle_name> <other_channel_name>".
296 Bundle::add_channels_from_bundle (boost::shared_ptr<Bundle> other)
298 uint32_t const ch = nchannels().n_total();
300 for (uint32_t i = 0; i < other->nchannels().n_total(); ++i) {
303 s << other->name() << " " << other->channel_name(i);
305 add_channel (s.str(), other->channel_type(i));
307 PortList const& pl = other->channel_ports (i);
308 for (uint32_t j = 0; j < pl.size(); ++j) {
309 add_port_to_channel (ch + i, pl[j]);
314 /** Connect the ports associated with our channels to the ports associated
315 * with another bundle's channels.
316 * @param other Other bundle.
317 * @param engine AudioEngine to use to make the connections.
320 Bundle::connect (boost::shared_ptr<Bundle> other, AudioEngine & engine)
322 uint32_t const N = nchannels().n_total();
323 assert (N == other->nchannels().n_total());
325 for (uint32_t i = 0; i < N; ++i) {
326 Bundle::PortList const & our_ports = channel_ports (i);
327 Bundle::PortList const & other_ports = other->channel_ports (i);
329 for (Bundle::PortList::const_iterator j = our_ports.begin(); j != our_ports.end(); ++j) {
330 for (Bundle::PortList::const_iterator k = other_ports.begin(); k != other_ports.end(); ++k) {
331 engine.connect (*j, *k);
338 Bundle::disconnect (boost::shared_ptr<Bundle> other, AudioEngine & engine)
340 uint32_t const N = nchannels().n_total();
341 assert (N == other->nchannels().n_total());
343 for (uint32_t i = 0; i < N; ++i) {
344 Bundle::PortList const & our_ports = channel_ports (i);
345 Bundle::PortList const & other_ports = other->channel_ports (i);
347 for (Bundle::PortList::const_iterator j = our_ports.begin(); j != our_ports.end(); ++j) {
348 for (Bundle::PortList::const_iterator k = other_ports.begin(); k != other_ports.end(); ++k) {
349 engine.disconnect (*j, *k);
355 /** Remove all ports from all channels */
357 Bundle::remove_ports_from_channels ()
360 Glib::Threads::Mutex::Lock lm (_channel_mutex);
361 for (uint32_t c = 0; c < _channel.size(); ++c) {
362 _channel[c].ports.clear ();
367 emit_changed (PortsChanged);
370 /** Remove all ports from a given channel.
374 Bundle::remove_ports_from_channel (uint32_t ch)
376 assert (ch < nchannels().n_total());
379 Glib::Threads::Mutex::Lock lm (_channel_mutex);
380 _channel[ch].ports.clear ();
383 emit_changed (PortsChanged);
387 Bundle::suspend_signals ()
389 _signals_suspended = true;
393 Bundle::resume_signals ()
395 if (_pending_change) {
396 Changed (_pending_change);
397 _pending_change = Change (0);
400 _signals_suspended = false;
404 Bundle::emit_changed (Change c)
406 if (_signals_suspended) {
407 _pending_change = Change (int (_pending_change) | int (c));
414 Bundle::connected_to (boost::shared_ptr<Bundle> other, AudioEngine & engine)
416 if (_ports_are_inputs == other->_ports_are_inputs || nchannels() != other->nchannels()) {
420 for (uint32_t i = 0; i < nchannels().n_total(); ++i) {
421 Bundle::PortList const & A = channel_ports (i);
422 Bundle::PortList const & B = other->channel_ports (i);
424 for (uint32_t j = 0; j < A.size(); ++j) {
425 for (uint32_t k = 0; k < B.size(); ++k) {
427 boost::shared_ptr<Port> p = engine.get_port_by_name (A[j]);
428 boost::shared_ptr<Port> q = engine.get_port_by_name (B[k]);
434 if (p && !p->connected_to (B[k])) {
436 } else if (q && !q->connected_to (A[j])) {
446 /** This must not be called in code executed as a response to a backend event,
447 * as it uses the backend port_get_all_connections().
448 * @return true if any of this bundle's channels are connected to anything.
451 Bundle::connected_to_anything (AudioEngine& engine)
453 PortManager& pm (engine);
455 for (uint32_t i = 0; i < nchannels().n_total(); ++i) {
456 Bundle::PortList const & ports = channel_ports (i);
458 for (uint32_t j = 0; j < ports.size(); ++j) {
460 /* ports[j] may not be an Ardour port, so use the port manager directly
461 rather than doing it with Port.
464 if (pm.connected (ports[j])) {
474 Bundle::set_ports_are_inputs ()
476 _ports_are_inputs = true;
477 emit_changed (DirectionChanged);
481 Bundle::set_ports_are_outputs ()
483 _ports_are_inputs = false;
484 emit_changed (DirectionChanged);
491 Bundle::set_name (string const & n)
494 emit_changed (NameChanged);
497 /** @param b Other bundle.
498 * @return true if b has the same number of channels as this bundle, and those channels have corresponding ports.
501 Bundle::has_same_ports (boost::shared_ptr<Bundle> b) const
503 uint32_t const N = nchannels().n_total();
505 if (b->nchannels().n_total() != N) {
509 /* XXX: probably should sort channel port lists before comparing them */
511 for (uint32_t i = 0; i < N; ++i) {
512 if (channel_ports (i) != b->channel_ports (i)) {
521 Bundle::channel_type (uint32_t c) const
523 assert (c < nchannels().n_total());
525 Glib::Threads::Mutex::Lock lm (_channel_mutex);
526 return _channel[c].type;
530 operator<< (ostream& os, Bundle const & b)
532 os << "BUNDLE " << b.nchannels() << " channels: ";
533 for (uint32_t i = 0; i < b.nchannels().n_total(); ++i) {
535 Bundle::PortList const & pl = b.channel_ports (i);
536 for (Bundle::PortList::const_iterator j = pl.begin(); j != pl.end(); ++j) {
546 Bundle::operator== (Bundle const & other)
548 return _channel == other._channel;
551 /** Given an index of a channel as the nth channel of a particular type,
552 * return an index of that channel when considering channels of all types.
554 * e.g. given a bundle with channels:
559 * If t == MIDI and c == 0, then we would return 2, as 2 is the index of the
562 * If t == NIL, we just return c.
566 Bundle::type_channel_to_overall (DataType t, uint32_t c) const
568 if (t == DataType::NIL) {
572 Glib::Threads::Mutex::Lock lm (_channel_mutex);
574 vector<Channel>::const_iterator i = _channel.begin ();
580 assert (i != _channel.end ());
598 /** Perform the reverse of type_channel_to_overall */
600 Bundle::overall_channel_to_type (DataType t, uint32_t c) const
602 if (t == DataType::NIL) {
606 Glib::Threads::Mutex::Lock lm (_channel_mutex);
610 vector<Channel>::const_iterator i = _channel.begin ();
611 for (uint32_t j = 0; j < c; ++j) {