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