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>
31 using namespace ARDOUR;
34 /** Construct an audio bundle.
35 * @param i true if ports are inputs, otherwise false.
37 Bundle::Bundle (bool i)
38 : _type (DataType::AUDIO),
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 _type (DataType::AUDIO),
54 _ports_are_inputs (i),
55 _signals_suspended (false),
56 _pending_change (Change (0))
62 /** Construct a bundle.
65 * @param i true if ports are inputs, otherwise false.
67 Bundle::Bundle (std::string const & n, DataType t, bool i)
70 _ports_are_inputs (i),
71 _signals_suspended (false),
72 _pending_change (Change (0))
78 Bundle::Bundle (boost::shared_ptr<Bundle> other)
79 : _channel (other->_channel),
82 _ports_are_inputs (other->_ports_are_inputs),
83 _signals_suspended (other->_signals_suspended),
84 _pending_change (other->_pending_change)
90 Bundle::nchannels () const
92 Glib::Mutex::Lock lm (_channel_mutex);
93 return _channel.size ();
96 Bundle::PortList const &
97 Bundle::channel_ports (uint32_t c) const
99 assert (c < nchannels());
101 Glib::Mutex::Lock lm (_channel_mutex);
102 return _channel[c].ports;
105 /** Add an association between one of our channels and a port.
106 * @param ch Channel index.
107 * @param portname full port name to associate with (including prefix).
110 Bundle::add_port_to_channel (uint32_t ch, string portname)
112 assert (ch < nchannels());
113 assert (portname.find_first_of (':') != string::npos);
116 Glib::Mutex::Lock lm (_channel_mutex);
117 _channel[ch].ports.push_back (portname);
120 emit_changed (PortsChanged);
123 /** Disassociate a port from one of our channels.
124 * @param ch Channel index.
125 * @param portname port name to disassociate from.
128 Bundle::remove_port_from_channel (uint32_t ch, string portname)
130 assert (ch < nchannels());
132 bool changed = false;
135 Glib::Mutex::Lock lm (_channel_mutex);
136 PortList& pl = _channel[ch].ports;
137 PortList::iterator i = find (pl.begin(), pl.end(), portname);
146 emit_changed (PortsChanged);
150 /** Set a single port to be associated with a channel, removing any others.
152 * @param portname Full port name, including prefix.
155 Bundle::set_port (uint32_t ch, string portname)
157 assert (ch < nchannels());
158 assert (portname.find_first_of (':') != string::npos);
161 Glib::Mutex::Lock lm (_channel_mutex);
162 _channel[ch].ports.clear ();
163 _channel[ch].ports.push_back (portname);
166 emit_changed (PortsChanged);
169 /** @param n Channel name */
171 Bundle::add_channel (std::string const & n)
174 Glib::Mutex::Lock lm (_channel_mutex);
175 _channel.push_back (Channel (n));
178 emit_changed (ConfigurationChanged);
182 Bundle::port_attached_to_channel (uint32_t ch, std::string portname)
184 assert (ch < nchannels());
186 Glib::Mutex::Lock lm (_channel_mutex);
187 return (std::find (_channel[ch].ports.begin (), _channel[ch].ports.end (), portname) != _channel[ch].ports.end ());
190 /** Remove a channel.
194 Bundle::remove_channel (uint32_t ch)
196 assert (ch < nchannels ());
198 Glib::Mutex::Lock lm (_channel_mutex);
199 _channel.erase (_channel.begin () + ch);
202 /** Remove all channels */
204 Bundle::remove_channels ()
206 Glib::Mutex::Lock lm (_channel_mutex);
211 /** @param p Port name.
212 * @return true if any channel is associated with p.
215 Bundle::uses_port (std::string p) const
217 Glib::Mutex::Lock lm (_channel_mutex);
219 for (std::vector<Channel>::const_iterator i = _channel.begin(); i != _channel.end(); ++i) {
220 for (PortList::const_iterator j = i->ports.begin(); j != i->ports.end(); ++j) {
230 /** @param p Port name.
231 * @return true if this bundle offers this port on its own on a channel.
234 Bundle::offers_port_alone (std::string p) const
236 Glib::Mutex::Lock lm (_channel_mutex);
238 for (std::vector<Channel>::const_iterator i = _channel.begin(); i != _channel.end(); ++i) {
239 if (i->ports.size() == 1 && i->ports[0] == p) {
248 /** @param ch Channel.
249 * @return Channel name.
252 Bundle::channel_name (uint32_t ch) const
254 assert (ch < nchannels());
256 Glib::Mutex::Lock lm (_channel_mutex);
257 return _channel[ch].name;
260 /** Set the name of a channel.
265 Bundle::set_channel_name (uint32_t ch, std::string const & n)
267 assert (ch < nchannels());
270 Glib::Mutex::Lock lm (_channel_mutex);
271 _channel[ch].name = n;
274 emit_changed (NameChanged);
277 /** Take the channels from another bundle and add them to this bundle,
278 * so that channels from other are added to this (with their ports)
279 * and are named "<other_bundle_name> <other_channel_name>".
282 Bundle::add_channels_from_bundle (boost::shared_ptr<Bundle> other)
284 uint32_t const ch = nchannels ();
286 for (uint32_t i = 0; i < other->nchannels(); ++i) {
289 s << other->name() << " " << other->channel_name(i);
291 add_channel (s.str());
293 PortList const& pl = other->channel_ports (i);
294 for (uint32_t j = 0; j < pl.size(); ++j) {
295 add_port_to_channel (ch + i, pl[j]);
300 /** Connect the ports associated with our channels to the ports associated
301 * with another bundle's channels.
302 * @param other Other bundle.
303 * @param engine AudioEngine to use to make the connections.
306 Bundle::connect (boost::shared_ptr<Bundle> other, AudioEngine & engine)
308 uint32_t const N = nchannels ();
309 assert (N == other->nchannels ());
311 for (uint32_t i = 0; i < N; ++i) {
312 Bundle::PortList const & our_ports = channel_ports (i);
313 Bundle::PortList const & other_ports = other->channel_ports (i);
315 for (Bundle::PortList::const_iterator j = our_ports.begin(); j != our_ports.end(); ++j) {
316 for (Bundle::PortList::const_iterator k = other_ports.begin(); k != other_ports.end(); ++k) {
317 engine.connect (*j, *k);
324 Bundle::disconnect (boost::shared_ptr<Bundle> other, AudioEngine & engine)
326 uint32_t const N = nchannels ();
327 assert (N == other->nchannels ());
329 for (uint32_t i = 0; i < N; ++i) {
330 Bundle::PortList const & our_ports = channel_ports (i);
331 Bundle::PortList const & other_ports = other->channel_ports (i);
333 for (Bundle::PortList::const_iterator j = our_ports.begin(); j != our_ports.end(); ++j) {
334 for (Bundle::PortList::const_iterator k = other_ports.begin(); k != other_ports.end(); ++k) {
335 engine.disconnect (*j, *k);
341 /** Remove all ports from all channels */
343 Bundle::remove_ports_from_channels ()
346 Glib::Mutex::Lock lm (_channel_mutex);
347 for (uint32_t c = 0; c < _channel.size(); ++c) {
348 _channel[c].ports.clear ();
353 emit_changed (PortsChanged);
356 /** Remove all ports from a given channel.
360 Bundle::remove_ports_from_channel (uint32_t ch)
362 assert (ch < nchannels ());
365 Glib::Mutex::Lock lm (_channel_mutex);
366 _channel[ch].ports.clear ();
369 emit_changed (PortsChanged);
373 Bundle::suspend_signals ()
375 _signals_suspended = true;
379 Bundle::resume_signals ()
381 if (_pending_change) {
382 Changed (_pending_change);
383 _pending_change = Change (0);
386 _signals_suspended = false;
390 Bundle::emit_changed (Change c)
392 if (_signals_suspended) {
393 _pending_change = Change (int (_pending_change) | int (c));
400 Bundle::connected_to (boost::shared_ptr<Bundle> other, AudioEngine & engine)
402 if (_ports_are_inputs == other->_ports_are_inputs ||
403 _type != other->_type ||
404 nchannels() != other->nchannels ()) {
409 for (uint32_t i = 0; i < nchannels(); ++i) {
410 Bundle::PortList const & A = channel_ports (i);
411 Bundle::PortList const & B = other->channel_ports (i);
413 for (uint32_t j = 0; j < A.size(); ++j) {
414 for (uint32_t k = 0; k < B.size(); ++k) {
416 Port* p = engine.get_port_by_name (A[j]);
417 Port* q = engine.get_port_by_name (B[k]);
423 if (p && !p->connected_to (B[k])) {
425 } else if (q && !q->connected_to (A[j])) {