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 : _type (DataType::AUDIO),
40 _ports_are_inputs (i),
41 _signals_suspended (false),
42 _pending_change (Change (0))
48 /** Construct an audio bundle.
50 * @param i true if ports are inputs, otherwise false.
52 Bundle::Bundle (std::string const & n, bool i)
54 _type (DataType::AUDIO),
55 _ports_are_inputs (i),
56 _signals_suspended (false),
57 _pending_change (Change (0))
63 /** Construct a bundle.
66 * @param i true if ports are inputs, otherwise false.
68 Bundle::Bundle (std::string const & n, DataType t, bool i)
71 _ports_are_inputs (i),
72 _signals_suspended (false),
73 _pending_change (Change (0))
79 Bundle::Bundle (boost::shared_ptr<Bundle> other)
80 : _channel (other->_channel),
83 _ports_are_inputs (other->_ports_are_inputs),
84 _signals_suspended (other->_signals_suspended),
85 _pending_change (other->_pending_change)
91 Bundle::nchannels () const
93 Glib::Mutex::Lock lm (_channel_mutex);
94 return _channel.size ();
97 Bundle::PortList const &
98 Bundle::channel_ports (uint32_t c) const
100 assert (c < nchannels());
102 Glib::Mutex::Lock lm (_channel_mutex);
103 return _channel[c].ports;
106 /** Add an association between one of our channels and a port.
107 * @param ch Channel index.
108 * @param portname full port name to associate with (including prefix).
111 Bundle::add_port_to_channel (uint32_t ch, string portname)
113 assert (ch < nchannels());
114 assert (portname.find_first_of (':') != string::npos);
117 Glib::Mutex::Lock lm (_channel_mutex);
118 _channel[ch].ports.push_back (portname);
121 emit_changed (PortsChanged);
124 /** Disassociate a port from one of our channels.
125 * @param ch Channel index.
126 * @param portname port name to disassociate from.
129 Bundle::remove_port_from_channel (uint32_t ch, string portname)
131 assert (ch < nchannels());
133 bool changed = false;
136 Glib::Mutex::Lock lm (_channel_mutex);
137 PortList& pl = _channel[ch].ports;
138 PortList::iterator i = find (pl.begin(), pl.end(), portname);
147 emit_changed (PortsChanged);
151 /** Set a single port to be associated with a channel, removing any others.
153 * @param portname Full port name, including prefix.
156 Bundle::set_port (uint32_t ch, string portname)
158 assert (ch < nchannels());
159 assert (portname.find_first_of (':') != string::npos);
162 Glib::Mutex::Lock lm (_channel_mutex);
163 _channel[ch].ports.clear ();
164 _channel[ch].ports.push_back (portname);
167 emit_changed (PortsChanged);
170 /** @param n Channel name */
172 Bundle::add_channel (std::string const & n)
175 Glib::Mutex::Lock lm (_channel_mutex);
176 _channel.push_back (Channel (n));
179 emit_changed (ConfigurationChanged);
183 Bundle::port_attached_to_channel (uint32_t ch, std::string portname)
185 assert (ch < nchannels());
187 Glib::Mutex::Lock lm (_channel_mutex);
188 return (std::find (_channel[ch].ports.begin (), _channel[ch].ports.end (), portname) != _channel[ch].ports.end ());
191 /** Remove a channel.
195 Bundle::remove_channel (uint32_t ch)
197 assert (ch < nchannels ());
199 Glib::Mutex::Lock lm (_channel_mutex);
200 _channel.erase (_channel.begin () + ch);
203 /** Remove all channels */
205 Bundle::remove_channels ()
207 Glib::Mutex::Lock lm (_channel_mutex);
212 /** @param p Port name.
213 * @return true if any channel is associated with p.
216 Bundle::uses_port (std::string p) const
218 Glib::Mutex::Lock lm (_channel_mutex);
220 for (std::vector<Channel>::const_iterator i = _channel.begin(); i != _channel.end(); ++i) {
221 for (PortList::const_iterator j = i->ports.begin(); j != i->ports.end(); ++j) {
231 /** @param p Port name.
232 * @return true if this bundle offers this port on its own on a channel.
235 Bundle::offers_port_alone (std::string p) const
237 Glib::Mutex::Lock lm (_channel_mutex);
239 for (std::vector<Channel>::const_iterator i = _channel.begin(); i != _channel.end(); ++i) {
240 if (i->ports.size() == 1 && i->ports[0] == p) {
249 /** @param ch Channel.
250 * @return Channel name.
253 Bundle::channel_name (uint32_t ch) const
255 assert (ch < nchannels());
257 Glib::Mutex::Lock lm (_channel_mutex);
258 return _channel[ch].name;
261 /** Set the name of a channel.
266 Bundle::set_channel_name (uint32_t ch, std::string const & n)
268 assert (ch < nchannels());
271 Glib::Mutex::Lock lm (_channel_mutex);
272 _channel[ch].name = n;
275 emit_changed (NameChanged);
278 /** Take the channels from another bundle and add them to this bundle,
279 * so that channels from other are added to this (with their ports)
280 * and are named "<other_bundle_name> <other_channel_name>".
283 Bundle::add_channels_from_bundle (boost::shared_ptr<Bundle> other)
285 uint32_t const ch = nchannels ();
287 for (uint32_t i = 0; i < other->nchannels(); ++i) {
290 s << other->name() << " " << other->channel_name(i);
292 add_channel (s.str());
294 PortList const& pl = other->channel_ports (i);
295 for (uint32_t j = 0; j < pl.size(); ++j) {
296 add_port_to_channel (ch + i, pl[j]);
301 /** Connect the ports associated with our channels to the ports associated
302 * with another bundle's channels.
303 * @param other Other bundle.
304 * @param engine AudioEngine to use to make the connections.
307 Bundle::connect (boost::shared_ptr<Bundle> other, AudioEngine & engine)
309 uint32_t const N = nchannels ();
310 assert (N == other->nchannels ());
312 for (uint32_t i = 0; i < N; ++i) {
313 Bundle::PortList const & our_ports = channel_ports (i);
314 Bundle::PortList const & other_ports = other->channel_ports (i);
316 for (Bundle::PortList::const_iterator j = our_ports.begin(); j != our_ports.end(); ++j) {
317 for (Bundle::PortList::const_iterator k = other_ports.begin(); k != other_ports.end(); ++k) {
318 engine.connect (*j, *k);
325 Bundle::disconnect (boost::shared_ptr<Bundle> other, AudioEngine & engine)
327 uint32_t const N = nchannels ();
328 assert (N == other->nchannels ());
330 for (uint32_t i = 0; i < N; ++i) {
331 Bundle::PortList const & our_ports = channel_ports (i);
332 Bundle::PortList const & other_ports = other->channel_ports (i);
334 for (Bundle::PortList::const_iterator j = our_ports.begin(); j != our_ports.end(); ++j) {
335 for (Bundle::PortList::const_iterator k = other_ports.begin(); k != other_ports.end(); ++k) {
336 engine.disconnect (*j, *k);
342 /** Remove all ports from all channels */
344 Bundle::remove_ports_from_channels ()
347 Glib::Mutex::Lock lm (_channel_mutex);
348 for (uint32_t c = 0; c < _channel.size(); ++c) {
349 _channel[c].ports.clear ();
354 emit_changed (PortsChanged);
357 /** Remove all ports from a given channel.
361 Bundle::remove_ports_from_channel (uint32_t ch)
363 assert (ch < nchannels ());
366 Glib::Mutex::Lock lm (_channel_mutex);
367 _channel[ch].ports.clear ();
370 emit_changed (PortsChanged);
374 Bundle::suspend_signals ()
376 _signals_suspended = true;
380 Bundle::resume_signals ()
382 if (_pending_change) {
383 Changed (_pending_change);
384 _pending_change = Change (0);
387 _signals_suspended = false;
391 Bundle::emit_changed (Change c)
393 if (_signals_suspended) {
394 _pending_change = Change (int (_pending_change) | int (c));
401 Bundle::connected_to (boost::shared_ptr<Bundle> other, AudioEngine & engine)
403 if (_ports_are_inputs == other->_ports_are_inputs ||
404 _type != other->_type ||
405 nchannels() != other->nchannels ()) {
410 for (uint32_t i = 0; i < nchannels(); ++i) {
411 Bundle::PortList const & A = channel_ports (i);
412 Bundle::PortList const & B = other->channel_ports (i);
414 for (uint32_t j = 0; j < A.size(); ++j) {
415 for (uint32_t k = 0; k < B.size(); ++k) {
417 Port* p = engine.get_port_by_name (A[j]);
418 Port* q = engine.get_port_by_name (B[k]);
424 if (p && !p->connected_to (B[k])) {
426 } else if (q && !q->connected_to (A[j])) {