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 "pbd/failed_constructor.h"
23 #include "ardour/ardour.h"
24 #include "ardour/bundle.h"
25 #include "ardour/audioengine.h"
26 #include "ardour/port.h"
27 #include "pbd/xml++.h"
32 using namespace ARDOUR;
35 /** Construct an audio bundle.
36 * @param i true if ports are inputs, otherwise false.
38 Bundle::Bundle (bool i)
39 : _ports_are_inputs (i),
40 _signals_suspended (false),
41 _pending_change (Change (0))
47 /** Construct an audio bundle.
49 * @param i true if ports are inputs, otherwise false.
51 Bundle::Bundle (std::string const & n, bool i)
53 _ports_are_inputs (i),
54 _signals_suspended (false),
55 _pending_change (Change (0))
60 Bundle::Bundle (boost::shared_ptr<Bundle> other)
61 : _channel (other->_channel),
63 _ports_are_inputs (other->_ports_are_inputs),
64 _signals_suspended (other->_signals_suspended),
65 _pending_change (other->_pending_change)
71 Bundle::nchannels () const
73 Glib::Mutex::Lock lm (_channel_mutex);
76 for (vector<Channel>::const_iterator i = _channel.begin(); i != _channel.end(); ++i) {
77 c.set (i->type, c.get (i->type) + 1);
83 Bundle::PortList const &
84 Bundle::channel_ports (uint32_t c) const
86 assert (c < nchannels().n_total());
88 Glib::Mutex::Lock lm (_channel_mutex);
89 return _channel[c].ports;
92 /** Add an association between one of our channels and a port.
93 * @param ch Channel index.
94 * @param portname full port name to associate with (including prefix).
97 Bundle::add_port_to_channel (uint32_t ch, string portname)
99 assert (ch < nchannels().n_total());
100 assert (portname.find_first_of (':') != string::npos);
103 Glib::Mutex::Lock lm (_channel_mutex);
104 _channel[ch].ports.push_back (portname);
107 emit_changed (PortsChanged);
110 /** Disassociate a port from one of our channels.
111 * @param ch Channel index.
112 * @param portname port name to disassociate from.
115 Bundle::remove_port_from_channel (uint32_t ch, string portname)
117 assert (ch < nchannels().n_total());
119 bool changed = false;
122 Glib::Mutex::Lock lm (_channel_mutex);
123 PortList& pl = _channel[ch].ports;
124 PortList::iterator i = find (pl.begin(), pl.end(), portname);
133 emit_changed (PortsChanged);
137 /** Set a single port to be associated with a channel, removing any others.
139 * @param portname Full port name, including prefix.
142 Bundle::set_port (uint32_t ch, string portname)
144 assert (ch < nchannels().n_total());
145 assert (portname.find_first_of (':') != string::npos);
148 Glib::Mutex::Lock lm (_channel_mutex);
149 _channel[ch].ports.clear ();
150 _channel[ch].ports.push_back (portname);
153 emit_changed (PortsChanged);
156 /** @param n Channel name */
158 Bundle::add_channel (std::string const & n, DataType t)
161 Glib::Mutex::Lock lm (_channel_mutex);
162 _channel.push_back (Channel (n, t));
165 emit_changed (ConfigurationChanged);
168 /** @param n Channel name */
170 Bundle::add_channel (std::string const & n, DataType t, PortList p)
173 Glib::Mutex::Lock lm (_channel_mutex);
174 _channel.push_back (Channel (n, t, p));
177 emit_changed (ConfigurationChanged);
180 /** @param n Channel name */
182 Bundle::add_channel (std::string const & n, DataType t, std::string const & p)
185 Glib::Mutex::Lock lm (_channel_mutex);
186 _channel.push_back (Channel (n, t, p));
189 emit_changed (ConfigurationChanged);
193 Bundle::port_attached_to_channel (uint32_t ch, std::string portname)
195 assert (ch < nchannels().n_total());
197 Glib::Mutex::Lock lm (_channel_mutex);
198 return (std::find (_channel[ch].ports.begin (), _channel[ch].ports.end (), portname) != _channel[ch].ports.end ());
201 /** Remove a channel.
205 Bundle::remove_channel (uint32_t ch)
207 assert (ch < nchannels().n_total());
209 Glib::Mutex::Lock lm (_channel_mutex);
210 _channel.erase (_channel.begin () + ch);
213 /** Remove all channels */
215 Bundle::remove_channels ()
217 Glib::Mutex::Lock lm (_channel_mutex);
222 /** @param p Port name.
223 * @return true if any channel is associated with p.
226 Bundle::offers_port (std::string p) const
228 Glib::Mutex::Lock lm (_channel_mutex);
230 for (std::vector<Channel>::const_iterator i = _channel.begin(); i != _channel.end(); ++i) {
231 for (PortList::const_iterator j = i->ports.begin(); j != i->ports.end(); ++j) {
241 /** @param p Port name.
242 * @return true if this bundle offers this port on its own on a channel.
245 Bundle::offers_port_alone (std::string p) const
247 Glib::Mutex::Lock lm (_channel_mutex);
249 for (std::vector<Channel>::const_iterator i = _channel.begin(); i != _channel.end(); ++i) {
250 if (i->ports.size() == 1 && i->ports[0] == p) {
259 /** @param ch Channel.
260 * @return Channel name.
263 Bundle::channel_name (uint32_t ch) const
265 assert (ch < nchannels().n_total());
267 Glib::Mutex::Lock lm (_channel_mutex);
268 return _channel[ch].name;
271 /** Set the name of a channel.
276 Bundle::set_channel_name (uint32_t ch, std::string const & n)
278 assert (ch < nchannels().n_total());
281 Glib::Mutex::Lock lm (_channel_mutex);
282 _channel[ch].name = n;
285 emit_changed (NameChanged);
288 /** Take the channels from another bundle and add them to this bundle,
289 * so that channels from other are added to this (with their ports)
290 * and are named "<other_bundle_name> <other_channel_name>".
293 Bundle::add_channels_from_bundle (boost::shared_ptr<Bundle> other)
295 uint32_t const ch = nchannels().n_total();
297 for (uint32_t i = 0; i < other->nchannels().n_total(); ++i) {
300 s << other->name() << " " << other->channel_name(i);
302 add_channel (s.str(), other->channel_type(i));
304 PortList const& pl = other->channel_ports (i);
305 for (uint32_t j = 0; j < pl.size(); ++j) {
306 add_port_to_channel (ch + i, pl[j]);
311 /** Connect the ports associated with our channels to the ports associated
312 * with another bundle's channels.
313 * @param other Other bundle.
314 * @param engine AudioEngine to use to make the connections.
317 Bundle::connect (boost::shared_ptr<Bundle> other, AudioEngine & engine)
319 uint32_t const N = nchannels().n_total();
320 assert (N == other->nchannels().n_total());
322 for (uint32_t i = 0; i < N; ++i) {
323 Bundle::PortList const & our_ports = channel_ports (i);
324 Bundle::PortList const & other_ports = other->channel_ports (i);
326 for (Bundle::PortList::const_iterator j = our_ports.begin(); j != our_ports.end(); ++j) {
327 for (Bundle::PortList::const_iterator k = other_ports.begin(); k != other_ports.end(); ++k) {
328 engine.connect (*j, *k);
335 Bundle::disconnect (boost::shared_ptr<Bundle> other, AudioEngine & engine)
337 uint32_t const N = nchannels().n_total();
338 assert (N == other->nchannels().n_total());
340 for (uint32_t i = 0; i < N; ++i) {
341 Bundle::PortList const & our_ports = channel_ports (i);
342 Bundle::PortList const & other_ports = other->channel_ports (i);
344 for (Bundle::PortList::const_iterator j = our_ports.begin(); j != our_ports.end(); ++j) {
345 for (Bundle::PortList::const_iterator k = other_ports.begin(); k != other_ports.end(); ++k) {
346 engine.disconnect (*j, *k);
352 /** Remove all ports from all channels */
354 Bundle::remove_ports_from_channels ()
357 Glib::Mutex::Lock lm (_channel_mutex);
358 for (uint32_t c = 0; c < _channel.size(); ++c) {
359 _channel[c].ports.clear ();
364 emit_changed (PortsChanged);
367 /** Remove all ports from a given channel.
371 Bundle::remove_ports_from_channel (uint32_t ch)
373 assert (ch < nchannels().n_total());
376 Glib::Mutex::Lock lm (_channel_mutex);
377 _channel[ch].ports.clear ();
380 emit_changed (PortsChanged);
384 Bundle::suspend_signals ()
386 _signals_suspended = true;
390 Bundle::resume_signals ()
392 if (_pending_change) {
393 Changed (_pending_change);
394 _pending_change = Change (0);
397 _signals_suspended = false;
401 Bundle::emit_changed (Change c)
403 if (_signals_suspended) {
404 _pending_change = Change (int (_pending_change) | int (c));
411 Bundle::connected_to (boost::shared_ptr<Bundle> other, AudioEngine & engine)
413 if (_ports_are_inputs == other->_ports_are_inputs || nchannels() != other->nchannels()) {
417 for (uint32_t i = 0; i < nchannels().n_total(); ++i) {
418 Bundle::PortList const & A = channel_ports (i);
419 Bundle::PortList const & B = other->channel_ports (i);
421 for (uint32_t j = 0; j < A.size(); ++j) {
422 for (uint32_t k = 0; k < B.size(); ++k) {
424 boost::shared_ptr<Port> p = engine.get_port_by_name (A[j]);
425 boost::shared_ptr<Port> q = engine.get_port_by_name (B[k]);
431 if (p && !p->connected_to (B[k])) {
433 } else if (q && !q->connected_to (A[j])) {
444 Bundle::set_ports_are_inputs ()
446 _ports_are_inputs = true;
447 emit_changed (DirectionChanged);
451 Bundle::set_ports_are_outputs ()
453 _ports_are_inputs = false;
454 emit_changed (DirectionChanged);
461 Bundle::set_name (string const & n)
464 emit_changed (NameChanged);
467 /** @param b Other bundle.
468 * @return true if b has the same number of channels as this bundle, and those channels have corresponding ports.
471 Bundle::has_same_ports (boost::shared_ptr<Bundle> b) const
473 uint32_t const N = nchannels().n_total();
475 if (b->nchannels().n_total() != N) {
479 /* XXX: probably should sort channel port lists before comparing them */
481 for (uint32_t i = 0; i < N; ++i) {
482 if (channel_ports (i) != b->channel_ports (i)) {
491 Bundle::channel_type (uint32_t c) const
493 assert (c < nchannels().n_total());
495 Glib::Mutex::Lock lm (_channel_mutex);
496 return _channel[c].type;
500 operator<< (ostream& os, Bundle const & b)
502 os << "BUNDLE " << b.nchannels() << " channels: ";
503 for (uint32_t i = 0; i < b.nchannels().n_total(); ++i) {
505 Bundle::PortList const & pl = b.channel_ports (i);
506 for (Bundle::PortList::const_iterator j = pl.begin(); j != pl.end(); ++j) {
516 Bundle::operator== (Bundle const & other)
518 return _channel == other._channel;
521 /** Given an index of a channel as the nth channel of a particular type,
522 * return an index of that channel when considering channels of all types.
524 * e.g. given a bundle with channels:
529 * If t == MIDI and c == 0, then we would return 2, as 2 is the index of the
532 * If t == NIL, we just return c.
536 Bundle::type_channel_to_overall (DataType t, uint32_t c) const
538 if (t == DataType::NIL) {
542 Glib::Mutex::Lock lm (_channel_mutex);
544 vector<Channel>::const_iterator i = _channel.begin ();
550 assert (i != _channel.end ());
568 /** Perform the reverse of type_channel_to_overall */
570 Bundle::overall_channel_to_type (DataType t, uint32_t c) const
572 if (t == DataType::NIL) {
576 Glib::Mutex::Lock lm (_channel_mutex);
580 vector<Channel>::const_iterator i = _channel.begin ();
581 for (uint32_t j = 0; j < c; ++j) {