switch MIDI Clock slave code to use DEBUG_TRACE; don't make it require start/stop...
[ardour2.git] / libs / ardour / bundle.cc
blobf409e0beee4eb1493e9696a3f772a4564213539f
1 /*
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.
20 #include <algorithm>
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"
29 #include "i18n.h"
31 using namespace std;
32 using namespace ARDOUR;
33 using namespace PBD;
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.
49 * @param n Name.
50 * @param i true if ports are inputs, otherwise false.
52 Bundle::Bundle (std::string const & n, bool i)
53 : _name (n),
54 _type (DataType::AUDIO),
55 _ports_are_inputs (i),
56 _signals_suspended (false),
57 _pending_change (Change (0))
63 /** Construct a bundle.
64 * @param n Name.
65 * @param t Type.
66 * @param i true if ports are inputs, otherwise false.
68 Bundle::Bundle (std::string const & n, DataType t, bool i)
69 : _name (n),
70 _type (t),
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),
81 _name (other->_name),
82 _type (other->_type),
83 _ports_are_inputs (other->_ports_are_inputs),
84 _signals_suspended (other->_signals_suspended),
85 _pending_change (other->_pending_change)
90 uint32_t
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).
110 void
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.
128 void
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);
140 if (i != pl.end()) {
141 pl.erase (i);
142 changed = true;
146 if (changed) {
147 emit_changed (PortsChanged);
151 /** Set a single port to be associated with a channel, removing any others.
152 * @param ch Channel.
153 * @param portname Full port name, including prefix.
155 void
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 */
171 void
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);
182 bool
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.
192 * @param ch Channel.
194 void
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 */
204 void
205 Bundle::remove_channels ()
207 Glib::Mutex::Lock lm (_channel_mutex);
209 _channel.clear ();
212 /** @param p Port name.
213 * @return true if any channel is associated with p.
215 bool
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) {
222 if (*j == p) {
223 return true;
228 return false;
231 /** @param p Port name.
232 * @return true if this bundle offers this port on its own on a channel.
234 bool
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) {
241 return true;
245 return false;
249 /** @param ch Channel.
250 * @return Channel name.
252 std::string
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.
262 * @param ch Channel.
263 * @param n New name.
265 void
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>".
282 void
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) {
289 std::stringstream s;
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.
306 void
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);
324 void
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 */
343 void
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.
358 * @param ch Channel.
360 void
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);
373 void
374 Bundle::suspend_signals ()
376 _signals_suspended = true;
379 void
380 Bundle::resume_signals ()
382 if (_pending_change) {
383 Changed (_pending_change);
384 _pending_change = Change (0);
387 _signals_suspended = false;
390 void
391 Bundle::emit_changed (Change c)
393 if (_signals_suspended) {
394 _pending_change = Change (int (_pending_change) | int (c));
395 } else {
396 Changed (c);
400 bool
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 ()) {
407 return false;
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]);
420 if (!p && !q) {
421 return false;
424 if (p && !p->connected_to (B[k])) {
425 return false;
426 } else if (q && !q->connected_to (A[j])) {
427 return false;
433 return true;
436 /** Set the type of the ports in this Bundle.
437 * @param t New type.
439 void
440 Bundle::set_type (DataType t)
442 _type = t;
443 emit_changed (TypeChanged);
446 void
447 Bundle::set_ports_are_inputs ()
449 _ports_are_inputs = true;
450 emit_changed (DirectionChanged);
453 void
454 Bundle::set_ports_are_outputs ()
456 _ports_are_inputs = false;
457 emit_changed (DirectionChanged);
460 /** Set the name.
461 * @param n New name.
463 void
464 Bundle::set_name (string const & n)
466 _name = n;
467 emit_changed (NameChanged);
470 /** @param b Other bundle.
471 * @return true if b has the same number of channels as this bundle, and those channels have corresponding ports.
473 bool
474 Bundle::has_same_ports (boost::shared_ptr<Bundle> b) const
476 uint32_t const N = nchannels ();
478 if (b->nchannels() != N) {
479 return false;
482 /* XXX: probably should sort channel port lists before comparing them */
484 for (uint32_t i = 0; i < N; ++i) {
485 if (channel_ports (i) != b->channel_ports (i)) {
486 return false;
490 return true;