5e57eb3e10a4d1eee884219a892d01e429280c7b
[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 "ardour/bundle.h"
23 #include "ardour/audioengine.h"
24 #include "ardour/port.h"
25
26 #include "pbd/i18n.h"
27
28 using namespace std;
29 using namespace ARDOUR;
30 using namespace PBD;
31
32 /** Construct an audio bundle.
33  *  @param i true if ports are inputs, otherwise false.
34  */
35 Bundle::Bundle (bool i)
36         : _ports_are_inputs (i),
37           _signals_suspended (false),
38           _pending_change (Change (0))
39 {
40
41 }
42
43
44 /** Construct an audio bundle.
45  *  @param n Name.
46  *  @param i true if ports are inputs, otherwise false.
47  */
48 Bundle::Bundle (std::string const & n, bool i)
49         : _name (n),
50           _ports_are_inputs (i),
51           _signals_suspended (false),
52           _pending_change (Change (0))
53 {
54
55 }
56
57 Bundle::Bundle (boost::shared_ptr<Bundle> other)
58         : _channel (other->_channel),
59           _name (other->_name),
60           _ports_are_inputs (other->_ports_are_inputs),
61           _signals_suspended (other->_signals_suspended),
62           _pending_change (other->_pending_change)
63 {
64
65 }
66
67 ChanCount
68 Bundle::nchannels () const
69 {
70         Glib::Threads::Mutex::Lock lm (_channel_mutex);
71
72         ChanCount c;
73         for (vector<Channel>::const_iterator i = _channel.begin(); i != _channel.end(); ++i) {
74                 c.set (i->type, c.get (i->type) + 1);
75         }
76
77         return c;
78 }
79
80 uint32_t
81 Bundle::n_total () const
82 {
83     /* Simpler and far more efficient than nchannels.n_total() */
84     return _channel.size();
85 }
86
87 Bundle::PortList const &
88 Bundle::channel_ports (uint32_t c) const
89 {
90         assert (c < n_total());
91
92         Glib::Threads::Mutex::Lock lm (_channel_mutex);
93         return _channel[c].ports;
94 }
95
96 /** Add an association between one of our channels and a port.
97  *  @param ch Channel index.
98  *  @param portname full port name to associate with (including prefix).
99  */
100 void
101 Bundle::add_port_to_channel (uint32_t ch, string portname)
102 {
103         assert (ch < n_total());
104         assert (portname.find_first_of (':') != string::npos);
105
106         {
107                 Glib::Threads::Mutex::Lock lm (_channel_mutex);
108                 _channel[ch].ports.push_back (portname);
109         }
110
111         emit_changed (PortsChanged);
112 }
113
114 /** Disassociate a port from one of our channels.
115  *  @param ch Channel index.
116  *  @param portname port name to disassociate from.
117  */
118 void
119 Bundle::remove_port_from_channel (uint32_t ch, string portname)
120 {
121         assert (ch < n_total());
122
123         bool changed = false;
124
125         {
126                 Glib::Threads::Mutex::Lock lm (_channel_mutex);
127                 PortList& pl = _channel[ch].ports;
128                 PortList::iterator i = find (pl.begin(), pl.end(), portname);
129
130                 if (i != pl.end()) {
131                         pl.erase (i);
132                         changed = true;
133                 }
134         }
135
136         if (changed) {
137                 emit_changed (PortsChanged);
138         }
139 }
140
141 /** Set a single port to be associated with a channel, removing any others.
142  *  @param ch Channel.
143  *  @param portname Full port name, including prefix.
144  */
145 void
146 Bundle::set_port (uint32_t ch, string portname)
147 {
148         assert (ch < n_total());
149         assert (portname.find_first_of (':') != string::npos);
150
151         {
152                 Glib::Threads::Mutex::Lock lm (_channel_mutex);
153                 _channel[ch].ports.clear ();
154                 _channel[ch].ports.push_back (portname);
155         }
156
157         emit_changed (PortsChanged);
158 }
159
160 /** @param n Channel name */
161 void
162 Bundle::add_channel (std::string const & n, DataType t)
163 {
164         {
165                 Glib::Threads::Mutex::Lock lm (_channel_mutex);
166                 _channel.push_back (Channel (n, t));
167         }
168
169         emit_changed (ConfigurationChanged);
170 }
171
172 /** @param n Channel name */
173 void
174 Bundle::add_channel (std::string const & n, DataType t, PortList p)
175 {
176         {
177                 Glib::Threads::Mutex::Lock lm (_channel_mutex);
178                 _channel.push_back (Channel (n, t, p));
179         }
180
181         emit_changed (ConfigurationChanged);
182 }
183
184 /** @param n Channel name */
185 void
186 Bundle::add_channel (std::string const & n, DataType t, std::string const & p)
187 {
188         {
189                 Glib::Threads::Mutex::Lock lm (_channel_mutex);
190                 _channel.push_back (Channel (n, t, p));
191         }
192
193         emit_changed (ConfigurationChanged);
194 }
195
196 bool
197 Bundle::port_attached_to_channel (uint32_t ch, std::string portname)
198 {
199         assert (ch < n_total());
200
201         Glib::Threads::Mutex::Lock lm (_channel_mutex);
202         return (std::find (_channel[ch].ports.begin (), _channel[ch].ports.end (), portname) != _channel[ch].ports.end ());
203 }
204
205 /** Remove a channel.
206  *  @param ch Channel.
207  */
208 void
209 Bundle::remove_channel (uint32_t ch)
210 {
211         assert (ch < n_total());
212
213         Glib::Threads::Mutex::Lock lm (_channel_mutex);
214         _channel.erase (_channel.begin () + ch);
215
216         lm.release();
217         emit_changed (ConfigurationChanged);
218 }
219
220 /** Remove all channels */
221 void
222 Bundle::remove_channels ()
223 {
224         Glib::Threads::Mutex::Lock lm (_channel_mutex);
225
226         _channel.clear ();
227
228         lm.release();
229         emit_changed (ConfigurationChanged);
230 }
231
232 /** @param p Port name.
233  *  @return true if any channel is associated with p.
234  */
235 bool
236 Bundle::offers_port (std::string p) const
237 {
238         Glib::Threads::Mutex::Lock lm (_channel_mutex);
239
240         for (std::vector<Channel>::const_iterator i = _channel.begin(); i != _channel.end(); ++i) {
241                 for (PortList::const_iterator j = i->ports.begin(); j != i->ports.end(); ++j) {
242                         if (*j == p) {
243                                 return true;
244                         }
245                 }
246         }
247
248         return false;
249 }
250
251 /** @param p Port name.
252  *  @return true if this bundle offers this port on its own on a channel.
253  */
254 bool
255 Bundle::offers_port_alone (std::string p) const
256 {
257         Glib::Threads::Mutex::Lock lm (_channel_mutex);
258
259         for (std::vector<Channel>::const_iterator i = _channel.begin(); i != _channel.end(); ++i) {
260                 if (i->ports.size() == 1 && i->ports[0] == p) {
261                         return true;
262                 }
263         }
264
265         return false;
266 }
267
268
269 /** @param ch Channel.
270  *  @return Channel name.
271  */
272 std::string
273 Bundle::channel_name (uint32_t ch) const
274 {
275         assert (ch < n_total());
276
277         Glib::Threads::Mutex::Lock lm (_channel_mutex);
278         return _channel[ch].name;
279 }
280
281 /** Set the name of a channel.
282  *  @param ch Channel.
283  *  @param n New name.
284  */
285 void
286 Bundle::set_channel_name (uint32_t ch, std::string const & n)
287 {
288         assert (ch < n_total());
289
290         {
291                 Glib::Threads::Mutex::Lock lm (_channel_mutex);
292                 _channel[ch].name = n;
293         }
294
295         emit_changed (NameChanged);
296 }
297
298 /** Take the channels from another bundle and add them to this bundle,
299  *  so that channels from other are added to this (with their ports)
300  *  and are named "<other_bundle_name> <other_channel_name>".
301  */
302 void
303 Bundle::add_channels_from_bundle (boost::shared_ptr<Bundle> other)
304 {
305         uint32_t const ch = n_total();
306
307         for (uint32_t i = 0; i < other->n_total(); ++i) {
308
309                 std::stringstream s;
310                 s << other->name() << " " << other->channel_name(i);
311
312                 add_channel (s.str(), other->channel_type(i));
313
314                 PortList const& pl = other->channel_ports (i);
315                 for (uint32_t j = 0; j < pl.size(); ++j) {
316                         add_port_to_channel (ch + i, pl[j]);
317                 }
318         }
319 }
320
321 /** Connect the ports associated with our channels to the ports associated
322  *  with another bundle's channels.
323  *  @param other Other bundle.
324  *  @param engine AudioEngine to use to make the connections.
325  */
326 void
327 Bundle::connect (boost::shared_ptr<Bundle> other, AudioEngine & engine)
328 {
329         uint32_t const N = n_total();
330         assert (N == other->n_total());
331
332         for (uint32_t i = 0; i < N; ++i) {
333                 Bundle::PortList const & our_ports = channel_ports (i);
334                 Bundle::PortList const & other_ports = other->channel_ports (i);
335
336                 for (Bundle::PortList::const_iterator j = our_ports.begin(); j != our_ports.end(); ++j) {
337                         for (Bundle::PortList::const_iterator k = other_ports.begin(); k != other_ports.end(); ++k) {
338                                 engine.connect (*j, *k);
339                         }
340                 }
341         }
342 }
343
344 void
345 Bundle::disconnect (boost::shared_ptr<Bundle> other, AudioEngine & engine)
346 {
347         uint32_t const N = n_total();
348         assert (N == other->n_total());
349
350         for (uint32_t i = 0; i < N; ++i) {
351                 Bundle::PortList const & our_ports = channel_ports (i);
352                 Bundle::PortList const & other_ports = other->channel_ports (i);
353
354                 for (Bundle::PortList::const_iterator j = our_ports.begin(); j != our_ports.end(); ++j) {
355                         for (Bundle::PortList::const_iterator k = other_ports.begin(); k != other_ports.end(); ++k) {
356                                 engine.disconnect (*j, *k);
357                         }
358                 }
359         }
360 }
361
362 /** Remove all ports from all channels */
363 void
364 Bundle::remove_ports_from_channels ()
365 {
366         {
367                 Glib::Threads::Mutex::Lock lm (_channel_mutex);
368                 for (uint32_t c = 0; c < n_total(); ++c) {
369                         _channel[c].ports.clear ();
370                 }
371
372         }
373
374         emit_changed (PortsChanged);
375 }
376
377 /** Remove all ports from a given channel.
378  *  @param ch Channel.
379  */
380 void
381 Bundle::remove_ports_from_channel (uint32_t ch)
382 {
383         assert (ch < n_total());
384
385         {
386                 Glib::Threads::Mutex::Lock lm (_channel_mutex);
387                 _channel[ch].ports.clear ();
388         }
389
390         emit_changed (PortsChanged);
391 }
392
393 void
394 Bundle::suspend_signals ()
395 {
396         _signals_suspended = true;
397 }
398
399 void
400 Bundle::resume_signals ()
401 {
402         if (_pending_change) {
403                 Changed (_pending_change);
404                 _pending_change = Change (0);
405         }
406
407         _signals_suspended = false;
408 }
409
410 void
411 Bundle::emit_changed (Change c)
412 {
413         if (_signals_suspended) {
414                 _pending_change = Change (int (_pending_change) | int (c));
415         } else {
416                 Changed (c);
417         }
418 }
419
420 bool
421 Bundle::connected_to (boost::shared_ptr<Bundle> other, AudioEngine & engine)
422 {
423         if (_ports_are_inputs == other->_ports_are_inputs || nchannels() != other->nchannels()) {
424                 return false;
425         }
426
427         for (uint32_t i = 0; i < n_total(); ++i) {
428                 Bundle::PortList const & A = channel_ports (i);
429                 Bundle::PortList const & B = other->channel_ports (i);
430
431                 for (uint32_t j = 0; j < A.size(); ++j) {
432                         for (uint32_t k = 0; k < B.size(); ++k) {
433
434                                 boost::shared_ptr<Port> p = engine.get_port_by_name (A[j]);
435                                 boost::shared_ptr<Port> q = engine.get_port_by_name (B[k]);
436
437                                 if (!p && !q) {
438                                         return false;
439                                 }
440
441                                 if (p && !p->connected_to (B[k])) {
442                                         return false;
443                                 } else if (q && !q->connected_to (A[j])) {
444                                         return false;
445                                 }
446                         }
447                 }
448         }
449
450         return true;
451 }
452
453 /** This must not be called in code executed as a response to a backend event,
454  *  as it uses the backend port_get_all_connections().
455  *  @return true if any of this bundle's channels are connected to anything.
456  */
457 bool
458 Bundle::connected_to_anything (AudioEngine& engine)
459 {
460         PortManager& pm (engine);
461
462         for (uint32_t i = 0; i < n_total(); ++i) {
463                 Bundle::PortList const & ports = channel_ports (i);
464
465                 for (uint32_t j = 0; j < ports.size(); ++j) {
466
467                         /* ports[j] may not be an Ardour port, so use the port manager directly
468                            rather than doing it with Port.
469                         */
470
471                         if (pm.connected (ports[j])) {
472                                 return true;
473                         }
474                 }
475         }
476
477         return false;
478 }
479
480 void
481 Bundle::set_ports_are_inputs ()
482 {
483         _ports_are_inputs = true;
484         emit_changed (DirectionChanged);
485 }
486
487 void
488 Bundle::set_ports_are_outputs ()
489 {
490         _ports_are_inputs = false;
491         emit_changed (DirectionChanged);
492 }
493
494 /** Set the name.
495  *  @param n New name.
496  */
497 void
498 Bundle::set_name (string const & n)
499 {
500         _name = n;
501         emit_changed (NameChanged);
502 }
503
504 /** @param b Other bundle.
505  *  @return true if b has the same number of channels as this bundle, and those channels have corresponding ports.
506  */
507 bool
508 Bundle::has_same_ports (boost::shared_ptr<Bundle> b) const
509 {
510         uint32_t const N = n_total();
511
512         if (b->n_total() != N) {
513                 return false;
514         }
515
516         /* XXX: probably should sort channel port lists before comparing them */
517
518         for (uint32_t i = 0; i < N; ++i) {
519                 if (channel_ports (i) != b->channel_ports (i)) {
520                         return false;
521                 }
522         }
523
524         return true;
525 }
526
527 DataType
528 Bundle::channel_type (uint32_t c) const
529 {
530         assert (c < n_total());
531
532         Glib::Threads::Mutex::Lock lm (_channel_mutex);
533         return _channel[c].type;
534 }
535
536 ostream &
537 operator<< (ostream& os, Bundle const & b)
538 {
539         os << "BUNDLE " << b.nchannels() << " channels: ";
540         for (uint32_t i = 0; i < b.n_total(); ++i) {
541                 os << "( ";
542                 Bundle::PortList const & pl = b.channel_ports (i);
543                 for (Bundle::PortList::const_iterator j = pl.begin(); j != pl.end(); ++j) {
544                         os << *j << " ";
545                 }
546                 os << ") ";
547         }
548
549         return os;
550 }
551
552 bool
553 Bundle::operator== (Bundle const & other)
554 {
555         return _channel == other._channel;
556 }
557
558 /** Given an index of a channel as the nth channel of a particular type,
559  *  return an index of that channel when considering channels of all types.
560  *
561  *  e.g. given a bundle with channels:
562  *          fred   [audio]
563  *          jim    [audio]
564  *          sheila [midi]
565  *
566  * If t == MIDI and c == 0, then we would return 2, as 2 is the index of the
567  * 0th MIDI channel.
568  *
569  * If t == NIL, we just return c.
570  */
571
572 uint32_t
573 Bundle::type_channel_to_overall (DataType t, uint32_t c) const
574 {
575         if (t == DataType::NIL) {
576                 return c;
577         }
578
579         Glib::Threads::Mutex::Lock lm (_channel_mutex);
580
581         vector<Channel>::const_iterator i = _channel.begin ();
582
583         uint32_t o = 0;
584
585         while (1) {
586
587                 assert (i != _channel.end ());
588
589                 if (i->type != t) {
590                         ++i;
591                 } else {
592                         if (c == 0) {
593                                 return o;
594                         }
595                         --c;
596                 }
597
598                 ++o;
599         }
600
601         abort(); /* NOTREACHED */
602         return -1;
603 }
604
605 /** Perform the reverse of type_channel_to_overall */
606 uint32_t
607 Bundle::overall_channel_to_type (DataType t, uint32_t c) const
608 {
609         if (t == DataType::NIL) {
610                 return c;
611         }
612
613         Glib::Threads::Mutex::Lock lm (_channel_mutex);
614
615         uint32_t s = 0;
616
617         vector<Channel>::const_iterator i = _channel.begin ();
618         for (uint32_t j = 0; j < c; ++j) {
619                 if (i->type == t) {
620                         ++s;
621                 }
622                 ++i;
623         }
624
625         return s;
626 }