Add operator<< for bundles.
[ardour.git] / libs / ardour / bundle.cc
1 /*
2     Copyright (C) 2002 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #include <algorithm>
21
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"
28
29 #include "i18n.h"
30
31 using namespace std;
32 using namespace ARDOUR;
33 using namespace PBD;
34
35 /** Construct an audio bundle.
36  *  @param i true if ports are inputs, otherwise false.
37  */
38 Bundle::Bundle (bool i)
39         : _ports_are_inputs (i),
40           _signals_suspended (false),
41           _pending_change (Change (0))
42 {
43
44 }
45
46
47 /** Construct an audio bundle.
48  *  @param n Name.
49  *  @param i true if ports are inputs, otherwise false.
50  */
51 Bundle::Bundle (std::string const & n, bool i)
52         : _name (n),
53           _ports_are_inputs (i),
54           _signals_suspended (false),
55           _pending_change (Change (0))
56 {
57
58 }
59
60 Bundle::Bundle (boost::shared_ptr<Bundle> other)
61         : _channel (other->_channel),
62           _name (other->_name),
63           _ports_are_inputs (other->_ports_are_inputs),
64           _signals_suspended (other->_signals_suspended),
65           _pending_change (other->_pending_change)
66 {
67
68 }
69
70 ChanCount
71 Bundle::nchannels () const
72 {
73         Glib::Mutex::Lock lm (_channel_mutex);
74
75         ChanCount c;
76         for (vector<Channel>::const_iterator i = _channel.begin(); i != _channel.end(); ++i) {
77                 c.set (i->type, c.get (i->type) + 1);
78         }
79
80         return c;
81 }
82
83 Bundle::PortList const &
84 Bundle::channel_ports (uint32_t c) const
85 {
86         assert (c < nchannels().n_total());
87
88         Glib::Mutex::Lock lm (_channel_mutex);
89         return _channel[c].ports;
90 }
91
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).
95  */
96 void
97 Bundle::add_port_to_channel (uint32_t ch, string portname)
98 {
99         assert (ch < nchannels().n_total());
100         assert (portname.find_first_of (':') != string::npos);
101
102         {
103                 Glib::Mutex::Lock lm (_channel_mutex);
104                 _channel[ch].ports.push_back (portname);
105         }
106
107         emit_changed (PortsChanged);
108 }
109
110 /** Disassociate a port from one of our channels.
111  *  @param ch Channel index.
112  *  @param portname port name to disassociate from.
113  */
114 void
115 Bundle::remove_port_from_channel (uint32_t ch, string portname)
116 {
117         assert (ch < nchannels().n_total());
118
119         bool changed = false;
120
121         {
122                 Glib::Mutex::Lock lm (_channel_mutex);
123                 PortList& pl = _channel[ch].ports;
124                 PortList::iterator i = find (pl.begin(), pl.end(), portname);
125
126                 if (i != pl.end()) {
127                         pl.erase (i);
128                         changed = true;
129                 }
130         }
131
132         if (changed) {
133                 emit_changed (PortsChanged);
134         }
135 }
136
137 /** Set a single port to be associated with a channel, removing any others.
138  *  @param ch Channel.
139  *  @param portname Full port name, including prefix.
140  */
141 void
142 Bundle::set_port (uint32_t ch, string portname)
143 {
144         assert (ch < nchannels().n_total());
145         assert (portname.find_first_of (':') != string::npos);
146
147         {
148                 Glib::Mutex::Lock lm (_channel_mutex);
149                 _channel[ch].ports.clear ();
150                 _channel[ch].ports.push_back (portname);
151         }
152
153         emit_changed (PortsChanged);
154 }
155
156 /** @param n Channel name */
157 void
158 Bundle::add_channel (std::string const & n, DataType t)
159 {
160         {
161                 Glib::Mutex::Lock lm (_channel_mutex);
162                 _channel.push_back (Channel (n, t));
163         }
164
165         emit_changed (ConfigurationChanged);
166 }
167
168 /** @param n Channel name */
169 void
170 Bundle::add_channel (std::string const & n, DataType t, PortList p)
171 {
172         {
173                 Glib::Mutex::Lock lm (_channel_mutex);
174                 _channel.push_back (Channel (n, t, p));
175         }
176
177         emit_changed (ConfigurationChanged);
178 }
179
180 /** @param n Channel name */
181 void
182 Bundle::add_channel (std::string const & n, DataType t, std::string const & p)
183 {
184         {
185                 Glib::Mutex::Lock lm (_channel_mutex);
186                 _channel.push_back (Channel (n, t, p));
187         }
188
189         emit_changed (ConfigurationChanged);
190 }
191
192 bool
193 Bundle::port_attached_to_channel (uint32_t ch, std::string portname)
194 {
195         assert (ch < nchannels().n_total());
196
197         Glib::Mutex::Lock lm (_channel_mutex);
198         return (std::find (_channel[ch].ports.begin (), _channel[ch].ports.end (), portname) != _channel[ch].ports.end ());
199 }
200
201 /** Remove a channel.
202  *  @param ch Channel.
203  */
204 void
205 Bundle::remove_channel (uint32_t ch)
206 {
207         assert (ch < nchannels().n_total());
208
209         Glib::Mutex::Lock lm (_channel_mutex);
210         _channel.erase (_channel.begin () + ch);
211 }
212
213 /** Remove all channels */
214 void
215 Bundle::remove_channels ()
216 {
217         Glib::Mutex::Lock lm (_channel_mutex);
218
219         _channel.clear ();
220 }
221
222 /** @param p Port name.
223  *  @return true if any channel is associated with p.
224  */
225 bool
226 Bundle::offers_port (std::string p) const
227 {
228         Glib::Mutex::Lock lm (_channel_mutex);
229
230         for (std::vector<Channel>::const_iterator i = _channel.begin(); i != _channel.end(); ++i) {
231                 for (PortList::const_iterator j = i->ports.begin(); j != i->ports.end(); ++j) {
232                         if (*j == p) {
233                                 return true;
234                         }
235                 }
236         }
237
238         return false;
239 }
240
241 /** @param p Port name.
242  *  @return true if this bundle offers this port on its own on a channel.
243  */
244 bool
245 Bundle::offers_port_alone (std::string p) const
246 {
247         Glib::Mutex::Lock lm (_channel_mutex);
248
249         for (std::vector<Channel>::const_iterator i = _channel.begin(); i != _channel.end(); ++i) {
250                 if (i->ports.size() == 1 && i->ports[0] == p) {
251                         return true;
252                 }
253         }
254
255         return false;
256 }
257
258
259 /** @param ch Channel.
260  *  @return Channel name.
261  */
262 std::string
263 Bundle::channel_name (uint32_t ch) const
264 {
265         assert (ch < nchannels().n_total());
266
267         Glib::Mutex::Lock lm (_channel_mutex);
268         return _channel[ch].name;
269 }
270
271 /** Set the name of a channel.
272  *  @param ch Channel.
273  *  @param n New name.
274  */
275 void
276 Bundle::set_channel_name (uint32_t ch, std::string const & n)
277 {
278         assert (ch < nchannels().n_total());
279
280         {
281                 Glib::Mutex::Lock lm (_channel_mutex);
282                 _channel[ch].name = n;
283         }
284
285         emit_changed (NameChanged);
286 }
287
288 /** Take the channels from another bundle and add them to this bundle,
289  *  so that channels from other are added to this (with their ports)
290  *  and are named "<other_bundle_name> <other_channel_name>".
291  */
292 void
293 Bundle::add_channels_from_bundle (boost::shared_ptr<Bundle> other)
294 {
295         uint32_t const ch = nchannels().n_total();
296
297         for (uint32_t i = 0; i < other->nchannels().n_total(); ++i) {
298
299                 std::stringstream s;
300                 s << other->name() << " " << other->channel_name(i);
301
302                 add_channel (s.str(), other->channel_type(i));
303
304                 PortList const& pl = other->channel_ports (i);
305                 for (uint32_t j = 0; j < pl.size(); ++j) {
306                         add_port_to_channel (ch + i, pl[j]);
307                 }
308         }
309 }
310
311 /** Connect the ports associated with our channels to the ports associated
312  *  with another bundle's channels.
313  *  @param other Other bundle.
314  *  @param engine AudioEngine to use to make the connections.
315  */
316 void
317 Bundle::connect (boost::shared_ptr<Bundle> other, AudioEngine & engine)
318 {
319         uint32_t const N = nchannels().n_total();
320         assert (N == other->nchannels().n_total());
321
322         for (uint32_t i = 0; i < N; ++i) {
323                 Bundle::PortList const & our_ports = channel_ports (i);
324                 Bundle::PortList const & other_ports = other->channel_ports (i);
325
326                 for (Bundle::PortList::const_iterator j = our_ports.begin(); j != our_ports.end(); ++j) {
327                         for (Bundle::PortList::const_iterator k = other_ports.begin(); k != other_ports.end(); ++k) {
328                                 engine.connect (*j, *k);
329                         }
330                 }
331         }
332 }
333
334 void
335 Bundle::disconnect (boost::shared_ptr<Bundle> other, AudioEngine & engine)
336 {
337         uint32_t const N = nchannels().n_total();
338         assert (N == other->nchannels().n_total());
339
340         for (uint32_t i = 0; i < N; ++i) {
341                 Bundle::PortList const & our_ports = channel_ports (i);
342                 Bundle::PortList const & other_ports = other->channel_ports (i);
343
344                 for (Bundle::PortList::const_iterator j = our_ports.begin(); j != our_ports.end(); ++j) {
345                         for (Bundle::PortList::const_iterator k = other_ports.begin(); k != other_ports.end(); ++k) {
346                                 engine.disconnect (*j, *k);
347                         }
348                 }
349         }
350 }
351
352 /** Remove all ports from all channels */
353 void
354 Bundle::remove_ports_from_channels ()
355 {
356         {
357                 Glib::Mutex::Lock lm (_channel_mutex);
358                 for (uint32_t c = 0; c < _channel.size(); ++c) {
359                         _channel[c].ports.clear ();
360                 }
361
362         }
363
364         emit_changed (PortsChanged);
365 }
366
367 /** Remove all ports from a given channel.
368  *  @param ch Channel.
369  */
370 void
371 Bundle::remove_ports_from_channel (uint32_t ch)
372 {
373         assert (ch < nchannels().n_total());
374
375         {
376                 Glib::Mutex::Lock lm (_channel_mutex);
377                 _channel[ch].ports.clear ();
378         }
379
380         emit_changed (PortsChanged);
381 }
382
383 void
384 Bundle::suspend_signals ()
385 {
386         _signals_suspended = true;
387 }
388
389 void
390 Bundle::resume_signals ()
391 {
392         if (_pending_change) {
393                 Changed (_pending_change);
394                 _pending_change = Change (0);
395         }
396
397         _signals_suspended = false;
398 }
399
400 void
401 Bundle::emit_changed (Change c)
402 {
403         if (_signals_suspended) {
404                 _pending_change = Change (int (_pending_change) | int (c));
405         } else {
406                 Changed (c);
407         }
408 }
409
410 bool
411 Bundle::connected_to (boost::shared_ptr<Bundle> other, AudioEngine & engine)
412 {
413         if (_ports_are_inputs == other->_ports_are_inputs || nchannels() != other->nchannels()) {
414                 return false;
415         }
416
417         for (uint32_t i = 0; i < nchannels().n_total(); ++i) {
418                 Bundle::PortList const & A = channel_ports (i);
419                 Bundle::PortList const & B = other->channel_ports (i);
420
421                 for (uint32_t j = 0; j < A.size(); ++j) {
422                         for (uint32_t k = 0; k < B.size(); ++k) {
423
424                                 Port* p = engine.get_port_by_name (A[j]);
425                                 Port* q = engine.get_port_by_name (B[k]);
426
427                                 if (!p && !q) {
428                                         return false;
429                                 }
430
431                                 if (p && !p->connected_to (B[k])) {
432                                         return false;
433                                 } else if (q && !q->connected_to (A[j])) {
434                                         return false;
435                                 }
436                         }
437                 }
438         }
439
440         return true;
441 }
442
443 void
444 Bundle::set_ports_are_inputs ()
445 {
446         _ports_are_inputs = true;
447         emit_changed (DirectionChanged);
448 }
449
450 void
451 Bundle::set_ports_are_outputs ()
452 {
453         _ports_are_inputs = false;
454         emit_changed (DirectionChanged);
455 }
456
457 /** Set the name.
458  *  @param n New name.
459  */
460 void
461 Bundle::set_name (string const & n)
462 {
463         _name = n;
464         emit_changed (NameChanged);
465 }
466
467 /** @param b Other bundle.
468  *  @return true if b has the same number of channels as this bundle, and those channels have corresponding ports.
469  */
470 bool
471 Bundle::has_same_ports (boost::shared_ptr<Bundle> b) const
472 {
473         uint32_t const N = nchannels().n_total();
474
475         if (b->nchannels().n_total() != N) {
476                 return false;
477         }
478
479         /* XXX: probably should sort channel port lists before comparing them */
480
481         for (uint32_t i = 0; i < N; ++i) {
482                 if (channel_ports (i) != b->channel_ports (i)) {
483                         return false;
484                 }
485         }
486
487         return true;
488 }
489
490 DataType
491 Bundle::channel_type (uint32_t c) const
492 {
493         assert (c < nchannels().n_total());
494
495         Glib::Mutex::Lock lm (_channel_mutex);
496         return _channel[c].type;
497 }       
498
499 ostream &
500 operator<< (ostream& os, Bundle const & b)
501 {
502         os << "BUNDLE " << b.nchannels() << " channels: ";
503         for (uint32_t i = 0; i < b.nchannels().n_total(); ++i) {
504                 os << "( ";
505                 Bundle::PortList const & pl = b.channel_ports (i);
506                 for (Bundle::PortList::const_iterator j = pl.begin(); j != pl.end(); ++j) {
507                         os << *j << " ";
508                 }
509                 os << ") ";
510         }
511
512         return os;
513 }