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 <pbd/xml++.h>
30 using namespace ARDOUR;
33 /** Construct an audio bundle.
34 * @param i true if ports are inputs, otherwise false.
36 Bundle::Bundle (bool i)
37 : _type (DataType::AUDIO),
38 _ports_are_inputs (i),
39 _signals_suspended (false),
40 _pending_change (Change (0))
46 /** Construct an audio bundle.
48 * @param i true if ports are inputs, otherwise false.
50 Bundle::Bundle (std::string const & n, bool i)
52 _type (DataType::AUDIO),
53 _ports_are_inputs (i),
54 _signals_suspended (false),
55 _pending_change (Change (0))
61 /** Construct a bundle.
64 * @param i true if ports are inputs, otherwise false.
66 Bundle::Bundle (std::string const & n, DataType t, bool i)
69 _ports_are_inputs (i),
70 _signals_suspended (false),
71 _pending_change (Change (0))
77 Bundle::Bundle (boost::shared_ptr<Bundle> other)
78 : _channel (other->_channel),
81 _ports_are_inputs (other->_ports_are_inputs),
82 _signals_suspended (other->_signals_suspended),
83 _pending_change (other->_pending_change)
89 Bundle::nchannels () const
91 Glib::Mutex::Lock lm (_channel_mutex);
92 return _channel.size ();
95 Bundle::PortList const &
96 Bundle::channel_ports (uint32_t c) const
98 assert (c < nchannels());
100 Glib::Mutex::Lock lm (_channel_mutex);
101 return _channel[c].ports;
104 /** Add an association between one of our channels and a port.
105 * @param ch Channel index.
106 * @param portname full port name to associate with (including prefix).
109 Bundle::add_port_to_channel (uint32_t ch, string portname)
111 assert (ch < nchannels());
112 assert (portname.find_first_of (':') != string::npos);
115 Glib::Mutex::Lock lm (_channel_mutex);
116 _channel[ch].ports.push_back (portname);
119 emit_changed (PortsChanged);
122 /** Disassociate a port from one of our channels.
123 * @param ch Channel index.
124 * @param portname port name to disassociate from.
127 Bundle::remove_port_from_channel (uint32_t ch, string portname)
129 assert (ch < nchannels());
131 bool changed = false;
134 Glib::Mutex::Lock lm (_channel_mutex);
135 PortList& pl = _channel[ch].ports;
136 PortList::iterator i = find (pl.begin(), pl.end(), portname);
145 emit_changed (PortsChanged);
149 /** Set a single port to be associated with a channel, removing any others.
151 * @param portname Full port name, including prefix.
154 Bundle::set_port (uint32_t ch, string portname)
156 assert (ch < nchannels());
157 assert (portname.find_first_of (':') != string::npos);
160 Glib::Mutex::Lock lm (_channel_mutex);
161 _channel[ch].ports.clear ();
162 _channel[ch].ports.push_back (portname);
165 emit_changed (PortsChanged);
168 /** @param n Channel name */
170 Bundle::add_channel (std::string const & n)
173 Glib::Mutex::Lock lm (_channel_mutex);
174 _channel.push_back (Channel (n));
177 emit_changed (ConfigurationChanged);
181 Bundle::port_attached_to_channel (uint32_t ch, std::string portname)
183 assert (ch < nchannels());
185 Glib::Mutex::Lock lm (_channel_mutex);
186 return (std::find (_channel[ch].ports.begin (), _channel[ch].ports.end (), portname) != _channel[ch].ports.end ());
189 /** Remove a channel.
193 Bundle::remove_channel (uint32_t ch)
195 assert (ch < nchannels ());
197 Glib::Mutex::Lock lm (_channel_mutex);
198 _channel.erase (_channel.begin () + ch);
201 /** Remove all channels */
203 Bundle::remove_channels ()
205 Glib::Mutex::Lock lm (_channel_mutex);
210 /** @param p Port name.
211 * @return true if any channel is associated with p.
214 Bundle::uses_port (std::string p) const
216 Glib::Mutex::Lock lm (_channel_mutex);
218 for (std::vector<Channel>::const_iterator i = _channel.begin(); i != _channel.end(); ++i) {
219 for (PortList::const_iterator j = i->ports.begin(); j != i->ports.end(); ++j) {
229 /** @param p Port name.
230 * @return true if this bundle offers this port on its own on a channel.
233 Bundle::offers_port_alone (std::string p) const
235 Glib::Mutex::Lock lm (_channel_mutex);
237 for (std::vector<Channel>::const_iterator i = _channel.begin(); i != _channel.end(); ++i) {
238 if (i->ports.size() == 1 && i->ports[0] == p) {
247 /** @param ch Channel.
248 * @return Channel name.
251 Bundle::channel_name (uint32_t ch) const
253 assert (ch < nchannels());
255 Glib::Mutex::Lock lm (_channel_mutex);
256 return _channel[ch].name;
259 /** Set the name of a channel.
264 Bundle::set_channel_name (uint32_t ch, std::string const & n)
266 assert (ch < nchannels());
269 Glib::Mutex::Lock lm (_channel_mutex);
270 _channel[ch].name = n;
273 emit_changed (NameChanged);
276 /** Take the channels from another bundle and add them to this bundle,
277 * so that channels from other are added to this (with their ports)
278 * and are named "<other_bundle_name> <other_channel_name>".
281 Bundle::add_channels_from_bundle (boost::shared_ptr<Bundle> other)
283 uint32_t const ch = nchannels ();
285 for (uint32_t i = 0; i < other->nchannels(); ++i) {
288 s << other->name() << " " << other->channel_name(i);
290 add_channel (s.str());
292 PortList const& pl = other->channel_ports (i);
293 for (uint32_t j = 0; j < pl.size(); ++j) {
294 add_port_to_channel (ch + i, pl[j]);
299 /** Connect the ports associated with our channels to the ports associated
300 * with another bundle's channels.
301 * @param other Other bundle.
302 * @param engine AudioEngine to use to make the connections.
305 Bundle::connect (boost::shared_ptr<Bundle> other, AudioEngine & engine)
307 uint32_t const N = nchannels ();
308 assert (N == other->nchannels ());
310 for (uint32_t i = 0; i < N; ++i) {
311 Bundle::PortList const & our_ports = channel_ports (i);
312 Bundle::PortList const & other_ports = other->channel_ports (i);
314 for (Bundle::PortList::const_iterator j = our_ports.begin(); j != our_ports.end(); ++j) {
315 for (Bundle::PortList::const_iterator k = other_ports.begin(); k != other_ports.end(); ++k) {
316 engine.connect (*j, *k);
323 Bundle::disconnect (boost::shared_ptr<Bundle> other, AudioEngine & engine)
325 uint32_t const N = nchannels ();
326 assert (N == other->nchannels ());
328 for (uint32_t i = 0; i < N; ++i) {
329 Bundle::PortList const & our_ports = channel_ports (i);
330 Bundle::PortList const & other_ports = other->channel_ports (i);
332 for (Bundle::PortList::const_iterator j = our_ports.begin(); j != our_ports.end(); ++j) {
333 for (Bundle::PortList::const_iterator k = other_ports.begin(); k != other_ports.end(); ++k) {
334 engine.disconnect (*j, *k);
340 /** Remove all ports from all channels */
342 Bundle::remove_ports_from_channels ()
345 Glib::Mutex::Lock lm (_channel_mutex);
346 for (uint32_t c = 0; c < _channel.size(); ++c) {
347 _channel[c].ports.clear ();
352 emit_changed (PortsChanged);
355 /** Remove all ports from a given channel.
359 Bundle::remove_ports_from_channel (uint32_t ch)
361 assert (ch < nchannels ());
364 Glib::Mutex::Lock lm (_channel_mutex);
365 _channel[ch].ports.clear ();
368 emit_changed (PortsChanged);
372 Bundle::suspend_signals ()
374 _signals_suspended = true;
378 Bundle::resume_signals ()
380 if (_pending_change) {
381 Changed (_pending_change);
382 _pending_change = Change (0);
385 _signals_suspended = false;
389 Bundle::emit_changed (Change c)
391 if (_signals_suspended) {
392 _pending_change = Change (int (_pending_change) | int (c));