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);
169 Bundle::port_attached_to_channel (uint32_t ch, std::string portname)
171 assert (ch < nchannels().n_total());
173 Glib::Mutex::Lock lm (_channel_mutex);
174 return (std::find (_channel[ch].ports.begin (), _channel[ch].ports.end (), portname) != _channel[ch].ports.end ());
177 /** Remove a channel.
181 Bundle::remove_channel (uint32_t ch)
183 assert (ch < nchannels().n_total());
185 Glib::Mutex::Lock lm (_channel_mutex);
186 _channel.erase (_channel.begin () + ch);
189 /** Remove all channels */
191 Bundle::remove_channels ()
193 Glib::Mutex::Lock lm (_channel_mutex);
198 /** @param p Port name.
199 * @return true if any channel is associated with p.
202 Bundle::uses_port (std::string p) const
204 Glib::Mutex::Lock lm (_channel_mutex);
206 for (std::vector<Channel>::const_iterator i = _channel.begin(); i != _channel.end(); ++i) {
207 for (PortList::const_iterator j = i->ports.begin(); j != i->ports.end(); ++j) {
217 /** @param p Port name.
218 * @return true if this bundle offers this port on its own on a channel.
221 Bundle::offers_port_alone (std::string p) const
223 Glib::Mutex::Lock lm (_channel_mutex);
225 for (std::vector<Channel>::const_iterator i = _channel.begin(); i != _channel.end(); ++i) {
226 if (i->ports.size() == 1 && i->ports[0] == p) {
235 /** @param ch Channel.
236 * @return Channel name.
239 Bundle::channel_name (uint32_t ch) const
241 assert (ch < nchannels().n_total());
243 Glib::Mutex::Lock lm (_channel_mutex);
244 return _channel[ch].name;
247 /** Set the name of a channel.
252 Bundle::set_channel_name (uint32_t ch, std::string const & n)
254 assert (ch < nchannels().n_total());
257 Glib::Mutex::Lock lm (_channel_mutex);
258 _channel[ch].name = n;
261 emit_changed (NameChanged);
264 /** Take the channels from another bundle and add them to this bundle,
265 * so that channels from other are added to this (with their ports)
266 * and are named "<other_bundle_name> <other_channel_name>".
269 Bundle::add_channels_from_bundle (boost::shared_ptr<Bundle> other)
271 uint32_t const ch = nchannels().n_total();
273 for (uint32_t i = 0; i < other->nchannels().n_total(); ++i) {
276 s << other->name() << " " << other->channel_name(i);
278 add_channel (s.str(), other->channel_type(i));
280 PortList const& pl = other->channel_ports (i);
281 for (uint32_t j = 0; j < pl.size(); ++j) {
282 add_port_to_channel (ch + i, pl[j]);
287 /** Connect the ports associated with our channels to the ports associated
288 * with another bundle's channels.
289 * @param other Other bundle.
290 * @param engine AudioEngine to use to make the connections.
293 Bundle::connect (boost::shared_ptr<Bundle> other, AudioEngine & engine)
295 uint32_t const N = nchannels().n_total();
296 assert (N == other->nchannels().n_total());
298 for (uint32_t i = 0; i < N; ++i) {
299 Bundle::PortList const & our_ports = channel_ports (i);
300 Bundle::PortList const & other_ports = other->channel_ports (i);
302 for (Bundle::PortList::const_iterator j = our_ports.begin(); j != our_ports.end(); ++j) {
303 for (Bundle::PortList::const_iterator k = other_ports.begin(); k != other_ports.end(); ++k) {
304 engine.connect (*j, *k);
311 Bundle::disconnect (boost::shared_ptr<Bundle> other, AudioEngine & engine)
313 uint32_t const N = nchannels().n_total();
314 assert (N == other->nchannels().n_total());
316 for (uint32_t i = 0; i < N; ++i) {
317 Bundle::PortList const & our_ports = channel_ports (i);
318 Bundle::PortList const & other_ports = other->channel_ports (i);
320 for (Bundle::PortList::const_iterator j = our_ports.begin(); j != our_ports.end(); ++j) {
321 for (Bundle::PortList::const_iterator k = other_ports.begin(); k != other_ports.end(); ++k) {
322 engine.disconnect (*j, *k);
328 /** Remove all ports from all channels */
330 Bundle::remove_ports_from_channels ()
333 Glib::Mutex::Lock lm (_channel_mutex);
334 for (uint32_t c = 0; c < _channel.size(); ++c) {
335 _channel[c].ports.clear ();
340 emit_changed (PortsChanged);
343 /** Remove all ports from a given channel.
347 Bundle::remove_ports_from_channel (uint32_t ch)
349 assert (ch < nchannels().n_total());
352 Glib::Mutex::Lock lm (_channel_mutex);
353 _channel[ch].ports.clear ();
356 emit_changed (PortsChanged);
360 Bundle::suspend_signals ()
362 _signals_suspended = true;
366 Bundle::resume_signals ()
368 if (_pending_change) {
369 Changed (_pending_change);
370 _pending_change = Change (0);
373 _signals_suspended = false;
377 Bundle::emit_changed (Change c)
379 if (_signals_suspended) {
380 _pending_change = Change (int (_pending_change) | int (c));
387 Bundle::connected_to (boost::shared_ptr<Bundle> other, AudioEngine & engine)
389 if (_ports_are_inputs == other->_ports_are_inputs || nchannels() != other->nchannels()) {
393 for (uint32_t i = 0; i < nchannels().n_total(); ++i) {
394 Bundle::PortList const & A = channel_ports (i);
395 Bundle::PortList const & B = other->channel_ports (i);
397 for (uint32_t j = 0; j < A.size(); ++j) {
398 for (uint32_t k = 0; k < B.size(); ++k) {
400 Port* p = engine.get_port_by_name (A[j]);
401 Port* q = engine.get_port_by_name (B[k]);
407 if (p && !p->connected_to (B[k])) {
409 } else if (q && !q->connected_to (A[j])) {
420 Bundle::set_ports_are_inputs ()
422 _ports_are_inputs = true;
423 emit_changed (DirectionChanged);
427 Bundle::set_ports_are_outputs ()
429 _ports_are_inputs = false;
430 emit_changed (DirectionChanged);
437 Bundle::set_name (string const & n)
440 emit_changed (NameChanged);
443 /** @param b Other bundle.
444 * @return true if b has the same number of channels as this bundle, and those channels have corresponding ports.
447 Bundle::has_same_ports (boost::shared_ptr<Bundle> b) const
449 uint32_t const N = nchannels().n_total();
451 if (b->nchannels().n_total() != N) {
455 /* XXX: probably should sort channel port lists before comparing them */
457 for (uint32_t i = 0; i < N; ++i) {
458 if (channel_ports (i) != b->channel_ports (i)) {
467 Bundle::channel_type (uint32_t c) const
469 assert (c < nchannels().n_total());
471 Glib::Mutex::Lock lm (_channel_mutex);
472 return _channel[c].type;