4f2198fd41c7500ae61df4695cc917cae2e77e3d
[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         : _type (DataType::AUDIO),
40           _ports_are_inputs (i),
41           _signals_suspended (false),
42           _pending_change (Change (0))
43 {
44
45 }
46
47
48 /** Construct an audio bundle.
49  *  @param n Name.
50  *  @param i true if ports are inputs, otherwise false.
51  */
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))
58 {
59
60 }
61
62
63 /** Construct a bundle.
64  *  @param n Name.
65  *  @param t Type.
66  *  @param i true if ports are inputs, otherwise false.
67  */
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))
74 {
75
76 }
77
78
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)
86 {
87
88 }
89
90 uint32_t
91 Bundle::nchannels () const
92 {
93         Glib::Mutex::Lock lm (_channel_mutex);
94         return _channel.size ();
95 }
96
97 Bundle::PortList const &
98 Bundle::channel_ports (uint32_t c) const
99 {
100         assert (c < nchannels());
101
102         Glib::Mutex::Lock lm (_channel_mutex);
103         return _channel[c].ports;
104 }
105
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).
109  */
110 void
111 Bundle::add_port_to_channel (uint32_t ch, string portname)
112 {
113         assert (ch < nchannels());
114         assert (portname.find_first_of (':') != string::npos);
115
116         {
117                 Glib::Mutex::Lock lm (_channel_mutex);
118                 _channel[ch].ports.push_back (portname);
119         }
120
121         emit_changed (PortsChanged);
122 }
123
124 /** Disassociate a port from one of our channels.
125  *  @param ch Channel index.
126  *  @param portname port name to disassociate from.
127  */
128 void
129 Bundle::remove_port_from_channel (uint32_t ch, string portname)
130 {
131         assert (ch < nchannels());
132
133         bool changed = false;
134
135         {
136                 Glib::Mutex::Lock lm (_channel_mutex);
137                 PortList& pl = _channel[ch].ports;
138                 PortList::iterator i = find (pl.begin(), pl.end(), portname);
139
140                 if (i != pl.end()) {
141                         pl.erase (i);
142                         changed = true;
143                 }
144         }
145
146         if (changed) {
147                 emit_changed (PortsChanged);
148         }
149 }
150
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.
154  */
155 void
156 Bundle::set_port (uint32_t ch, string portname)
157 {
158         assert (ch < nchannels());
159         assert (portname.find_first_of (':') != string::npos);
160
161         {
162                 Glib::Mutex::Lock lm (_channel_mutex);
163                 _channel[ch].ports.clear ();
164                 _channel[ch].ports.push_back (portname);
165         }
166
167         emit_changed (PortsChanged);
168 }
169
170 /** @param n Channel name */
171 void
172 Bundle::add_channel (std::string const & n)
173 {
174         {
175                 Glib::Mutex::Lock lm (_channel_mutex);
176                 _channel.push_back (Channel (n));
177         }
178
179         emit_changed (ConfigurationChanged);
180 }
181
182 bool
183 Bundle::port_attached_to_channel (uint32_t ch, std::string portname)
184 {
185         assert (ch < nchannels());
186
187         Glib::Mutex::Lock lm (_channel_mutex);
188         return (std::find (_channel[ch].ports.begin (), _channel[ch].ports.end (), portname) != _channel[ch].ports.end ());
189 }
190
191 /** Remove a channel.
192  *  @param ch Channel.
193  */
194 void
195 Bundle::remove_channel (uint32_t ch)
196 {
197         assert (ch < nchannels ());
198
199         Glib::Mutex::Lock lm (_channel_mutex);
200         _channel.erase (_channel.begin () + ch);
201 }
202
203 /** Remove all channels */
204 void
205 Bundle::remove_channels ()
206 {
207         Glib::Mutex::Lock lm (_channel_mutex);
208
209         _channel.clear ();
210 }
211
212 /** @param p Port name.
213  *  @return true if any channel is associated with p.
214  */
215 bool
216 Bundle::uses_port (std::string p) const
217 {
218         Glib::Mutex::Lock lm (_channel_mutex);
219
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;
224                         }
225                 }
226         }
227
228         return false;
229 }
230
231 /** @param p Port name.
232  *  @return true if this bundle offers this port on its own on a channel.
233  */
234 bool
235 Bundle::offers_port_alone (std::string p) const
236 {
237         Glib::Mutex::Lock lm (_channel_mutex);
238
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;
242                 }
243         }
244
245         return false;
246 }
247
248
249 /** @param ch Channel.
250  *  @return Channel name.
251  */
252 std::string
253 Bundle::channel_name (uint32_t ch) const
254 {
255         assert (ch < nchannels());
256
257         Glib::Mutex::Lock lm (_channel_mutex);
258         return _channel[ch].name;
259 }
260
261 /** Set the name of a channel.
262  *  @param ch Channel.
263  *  @param n New name.
264  */
265 void
266 Bundle::set_channel_name (uint32_t ch, std::string const & n)
267 {
268         assert (ch < nchannels());
269
270         {
271                 Glib::Mutex::Lock lm (_channel_mutex);
272                 _channel[ch].name = n;
273         }
274
275         emit_changed (NameChanged);
276 }
277
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>".
281  */
282 void
283 Bundle::add_channels_from_bundle (boost::shared_ptr<Bundle> other)
284 {
285         uint32_t const ch = nchannels ();
286
287         for (uint32_t i = 0; i < other->nchannels(); ++i) {
288
289                 std::stringstream s;
290                 s << other->name() << " " << other->channel_name(i);
291
292                 add_channel (s.str());
293
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]);
297                 }
298         }
299 }
300
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.
305  */
306 void
307 Bundle::connect (boost::shared_ptr<Bundle> other, AudioEngine & engine)
308 {
309         uint32_t const N = nchannels ();
310         assert (N == other->nchannels ());
311
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);
315
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);
319                         }
320                 }
321         }
322 }
323
324 void
325 Bundle::disconnect (boost::shared_ptr<Bundle> other, AudioEngine & engine)
326 {
327         uint32_t const N = nchannels ();
328         assert (N == other->nchannels ());
329
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);
333
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);
337                         }
338                 }
339         }
340 }
341
342 /** Remove all ports from all channels */
343 void
344 Bundle::remove_ports_from_channels ()
345 {
346         {
347                 Glib::Mutex::Lock lm (_channel_mutex);
348                 for (uint32_t c = 0; c < _channel.size(); ++c) {
349                         _channel[c].ports.clear ();
350                 }
351
352         }
353
354         emit_changed (PortsChanged);
355 }
356
357 /** Remove all ports from a given channel.
358  *  @param ch Channel.
359  */
360 void
361 Bundle::remove_ports_from_channel (uint32_t ch)
362 {
363         assert (ch < nchannels ());
364
365         {
366                 Glib::Mutex::Lock lm (_channel_mutex);
367                 _channel[ch].ports.clear ();
368         }
369
370         emit_changed (PortsChanged);
371 }
372
373 void
374 Bundle::suspend_signals ()
375 {
376         _signals_suspended = true;
377 }
378
379 void
380 Bundle::resume_signals ()
381 {
382         if (_pending_change) {
383                 Changed (_pending_change);
384                 _pending_change = Change (0);
385         }
386
387         _signals_suspended = false;
388 }
389
390 void
391 Bundle::emit_changed (Change c)
392 {
393         if (_signals_suspended) {
394                 _pending_change = Change (int (_pending_change) | int (c));
395         } else {
396                 Changed (c);
397         }
398 }
399
400 bool
401 Bundle::connected_to (boost::shared_ptr<Bundle> other, AudioEngine & engine)
402 {
403         if (_ports_are_inputs == other->_ports_are_inputs ||
404             _type != other->_type ||
405             nchannels() != other->nchannels ()) {
406
407                 return false;
408         }
409
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);
413
414                 for (uint32_t j = 0; j < A.size(); ++j) {
415                         for (uint32_t k = 0; k < B.size(); ++k) {
416
417                                 Port* p = engine.get_port_by_name (A[j]);
418                                 Port* q = engine.get_port_by_name (B[k]);
419
420                                 if (!p && !q) {
421                                         return false;
422                                 }
423
424                                 if (p && !p->connected_to (B[k])) {
425                                         return false;
426                                 } else if (q && !q->connected_to (A[j])) {
427                                         return false;
428                                 }
429                         }
430                 }
431         }
432
433         return true;
434 }
435
436 /** Set the type of the ports in this Bundle.
437  *  @param t New type.
438  */
439 void
440 Bundle::set_type (DataType t)
441 {
442         _type = t;
443         emit_changed (TypeChanged);
444 }
445
446 void
447 Bundle::set_ports_are_inputs ()
448 {
449         _ports_are_inputs = true;
450         emit_changed (DirectionChanged);
451 }
452
453 void
454 Bundle::set_ports_are_outputs ()
455 {
456         _ports_are_inputs = false;
457         emit_changed (DirectionChanged);
458 }
459
460 /** Set the name.
461  *  @param n New name.
462  */
463 void
464 Bundle::set_name (string const & n)
465 {
466         _name = n;
467         emit_changed (NameChanged);
468 }