summaryrefslogtreecommitdiff
path: root/libs/ardour/bundle.cc
blob: b165155e9057491b6dbd65000e898ae2872f058c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
/*
 * Copyright (C) 2007-2012 Carl Hetherington <carl@carlh.net>
 * Copyright (C) 2008-2016 Paul Davis <paul@linuxaudiosystems.com>
 * Copyright (C) 2009-2012 David Robillard <d@drobilla.net>
 * Copyright (C) 2013-2015 Robin Gareus <robin@gareus.org>
 * Copyright (C) 2017 Julien "_FrnchFrgg_" RIVAUD <frnchfrgg@free.fr>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <algorithm>

#include "ardour/bundle.h"
#include "ardour/audioengine.h"
#include "ardour/port.h"

#include "pbd/i18n.h"

using namespace std;
using namespace ARDOUR;
using namespace PBD;

/** Construct an audio bundle.
 *  @param i true if ports are inputs, otherwise false.
 */
Bundle::Bundle (bool i)
	: _ports_are_inputs (i),
	  _signals_suspended (false),
	  _pending_change (Change (0))
{

}


/** Construct an audio bundle.
 *  @param n Name.
 *  @param i true if ports are inputs, otherwise false.
 */
Bundle::Bundle (std::string const & n, bool i)
	: _name (n),
	  _ports_are_inputs (i),
	  _signals_suspended (false),
	  _pending_change (Change (0))
{

}

Bundle::Bundle (boost::shared_ptr<Bundle> other)
	: _channel (other->_channel),
	  _name (other->_name),
	  _ports_are_inputs (other->_ports_are_inputs),
	  _signals_suspended (other->_signals_suspended),
	  _pending_change (other->_pending_change)
{

}

ChanCount
Bundle::nchannels () const
{
	Glib::Threads::Mutex::Lock lm (_channel_mutex);

	ChanCount c;
	for (vector<Channel>::const_iterator i = _channel.begin(); i != _channel.end(); ++i) {
		c.set (i->type, c.get (i->type) + 1);
	}

	return c;
}

uint32_t
Bundle::n_total () const
{
    /* Simpler and far more efficient than nchannels.n_total() */
    return _channel.size();
}

Bundle::PortList const &
Bundle::channel_ports (uint32_t c) const
{
	assert (c < n_total());

	Glib::Threads::Mutex::Lock lm (_channel_mutex);
	return _channel[c].ports;
}

/** Add an association between one of our channels and a port.
 *  @param ch Channel index.
 *  @param portname full port name to associate with (including prefix).
 */
void
Bundle::add_port_to_channel (uint32_t ch, string portname)
{
	assert (ch < n_total());
	assert (portname.find_first_of (':') != string::npos);

	{
		Glib::Threads::Mutex::Lock lm (_channel_mutex);
		_channel[ch].ports.push_back (portname);
	}

	emit_changed (PortsChanged);
}

/** Disassociate a port from one of our channels.
 *  @param ch Channel index.
 *  @param portname port name to disassociate from.
 */
void
Bundle::remove_port_from_channel (uint32_t ch, string portname)
{
	assert (ch < n_total());

	bool changed = false;

	{
		Glib::Threads::Mutex::Lock lm (_channel_mutex);
		PortList& pl = _channel[ch].ports;
		PortList::iterator i = find (pl.begin(), pl.end(), portname);

		if (i != pl.end()) {
			pl.erase (i);
			changed = true;
		}
	}

	if (changed) {
		emit_changed (PortsChanged);
	}
}

/** Set a single port to be associated with a channel, removing any others.
 *  @param ch Channel.
 *  @param portname Full port name, including prefix.
 */
void
Bundle::set_port (uint32_t ch, string portname)
{
	assert (ch < n_total());
	assert (portname.find_first_of (':') != string::npos);

	{
		Glib::Threads::Mutex::Lock lm (_channel_mutex);
		_channel[ch].ports.clear ();
		_channel[ch].ports.push_back (portname);
	}

	emit_changed (PortsChanged);
}

/** @param n Channel name */
void
Bundle::add_channel (std::string const & n, DataType t)
{
	{
		Glib::Threads::Mutex::Lock lm (_channel_mutex);
		_channel.push_back (Channel (n, t));
	}

	emit_changed (ConfigurationChanged);
}

/** @param n Channel name */
void
Bundle::add_channel (std::string const & n, DataType t, PortList p)
{
	{
		Glib::Threads::Mutex::Lock lm (_channel_mutex);
		_channel.push_back (Channel (n, t, p));
	}

	emit_changed (ConfigurationChanged);
}

/** @param n Channel name */
void
Bundle::add_channel (std::string const & n, DataType t, std::string const & p)
{
	{
		Glib::Threads::Mutex::Lock lm (_channel_mutex);
		_channel.push_back (Channel (n, t, p));
	}

	emit_changed (ConfigurationChanged);
}

bool
Bundle::port_attached_to_channel (uint32_t ch, std::string portname)
{
	assert (ch < n_total());

	Glib::Threads::Mutex::Lock lm (_channel_mutex);
	return (std::find (_channel[ch].ports.begin (), _channel[ch].ports.end (), portname) != _channel[ch].ports.end ());
}

/** Remove a channel.
 *  @param ch Channel.
 */
void
Bundle::remove_channel (uint32_t ch)
{
	assert (ch < n_total());

	Glib::Threads::Mutex::Lock lm (_channel_mutex);
	_channel.erase (_channel.begin () + ch);

	lm.release();
	emit_changed (ConfigurationChanged);
}

/** Remove all channels */
void
Bundle::remove_channels ()
{
	Glib::Threads::Mutex::Lock lm (_channel_mutex);

	_channel.clear ();

	lm.release();
	emit_changed (ConfigurationChanged);
}

/** @param p Port name.
 *  @return true if any channel is associated with p.
 */
bool
Bundle::offers_port (std::string p) const
{
	Glib::Threads::Mutex::Lock lm (_channel_mutex);

	for (std::vector<Channel>::const_iterator i = _channel.begin(); i != _channel.end(); ++i) {
		for (PortList::const_iterator j = i->ports.begin(); j != i->ports.end(); ++j) {
			if (*j == p) {
				return true;
			}
		}
	}

	return false;
}

/** @param p Port name.
 *  @return true if this bundle offers this port on its own on a channel.
 */
bool
Bundle::offers_port_alone (std::string p) const
{
	Glib::Threads::Mutex::Lock lm (_channel_mutex);

	for (std::vector<Channel>::const_iterator i = _channel.begin(); i != _channel.end(); ++i) {
		if (i->ports.size() == 1 && i->ports[0] == p) {
			return true;
		}
	}

	return false;
}


/** @param ch Channel.
 *  @return Channel name.
 */
std::string
Bundle::channel_name (uint32_t ch) const
{
	assert (ch < n_total());

	Glib::Threads::Mutex::Lock lm (_channel_mutex);
	return _channel[ch].name;
}

/** Set the name of a channel.
 *  @param ch Channel.
 *  @param n New name.
 */
void
Bundle::set_channel_name (uint32_t ch, std::string const & n)
{
	assert (ch < n_total());

	{
		Glib::Threads::Mutex::Lock lm (_channel_mutex);
		_channel[ch].name = n;
	}

	emit_changed (NameChanged);
}

/** Take the channels from another bundle and add them to this bundle,
 *  so that channels from other are added to this (with their ports)
 *  and are named "<other_bundle_name> <other_channel_name>".
 */
void
Bundle::add_channels_from_bundle (boost::shared_ptr<Bundle> other)
{
	uint32_t const ch = n_total();

	for (uint32_t i = 0; i < other->n_total(); ++i) {

		std::stringstream s;
		s << other->name() << " " << other->channel_name(i);

		add_channel (s.str(), other->channel_type(i));

		PortList const& pl = other->channel_ports (i);
		for (uint32_t j = 0; j < pl.size(); ++j) {
			add_port_to_channel (ch + i, pl[j]);
		}
	}
}

/** Connect the ports associated with our channels to the ports associated
 *  with another bundle's channels.
 *  @param other Other bundle.
 *  @param engine AudioEngine to use to make the connections.
 *  @param allow_partial whether to allow leaving unconnected channels types,
 *              or require that the ChanCounts match exactly (default false).
 */
void
Bundle::connect (boost::shared_ptr<Bundle> other, AudioEngine & engine,
                 bool allow_partial)
{
	ChanCount our_count = nchannels();
	ChanCount other_count = other->nchannels();

	if (!allow_partial && our_count != other_count) {
		assert (our_count == other_count);
		return;
	}

	for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
		uint32_t N = our_count.n(*t);
		if (N != other_count.n(*t))
			continue;
		for (uint32_t i = 0; i < N; ++i) {
			Bundle::PortList const & our_ports =
				channel_ports (type_channel_to_overall(*t, i));
			Bundle::PortList const & other_ports =
				other->channel_ports (other->type_channel_to_overall(*t, i));

			for (Bundle::PortList::const_iterator j = our_ports.begin();
						j != our_ports.end(); ++j) {
				for (Bundle::PortList::const_iterator k = other_ports.begin();
							k != other_ports.end(); ++k) {
					engine.connect (*j, *k);
				}
			}
		}
	}
}

void
Bundle::disconnect (boost::shared_ptr<Bundle> other, AudioEngine & engine)
{
	ChanCount our_count = nchannels();
	ChanCount other_count = other->nchannels();

	for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
		uint32_t N = min(our_count.n(*t), other_count.n(*t));
		for (uint32_t i = 0; i < N; ++i) {
			Bundle::PortList const & our_ports =
				channel_ports (type_channel_to_overall(*t, i));
			Bundle::PortList const & other_ports =
				other->channel_ports (other->type_channel_to_overall(*t, i));

			for (Bundle::PortList::const_iterator j = our_ports.begin();
						j != our_ports.end(); ++j) {
				for (Bundle::PortList::const_iterator k = other_ports.begin();
							k != other_ports.end(); ++k) {
					engine.disconnect (*j, *k);
				}
			}
		}
	}
}

/** Remove all ports from all channels */
void
Bundle::remove_ports_from_channels ()
{
	{
		Glib::Threads::Mutex::Lock lm (_channel_mutex);
		for (uint32_t c = 0; c < n_total(); ++c) {
			_channel[c].ports.clear ();
		}

	}

	emit_changed (PortsChanged);
}

/** Remove all ports from a given channel.
 *  @param ch Channel.
 */
void
Bundle::remove_ports_from_channel (uint32_t ch)
{
	assert (ch < n_total());

	{
		Glib::Threads::Mutex::Lock lm (_channel_mutex);
		_channel[ch].ports.clear ();
	}

	emit_changed (PortsChanged);
}

void
Bundle::suspend_signals ()
{
	_signals_suspended = true;
}

void
Bundle::resume_signals ()
{
	if (_pending_change) {
		Changed (_pending_change);
		_pending_change = Change (0);
	}

	_signals_suspended = false;
}

void
Bundle::emit_changed (Change c)
{
	if (_signals_suspended) {
		_pending_change = Change (int (_pending_change) | int (c));
	} else {
		Changed (c);
	}
}

/** This must not be called in code executed as a response to a backend event,
 *  as it may query the backend in the same thread where it's waiting for us.
 * @return true if a Bundle is connected to another.
 * @param type: if not NIL, restrict the check to channels of that type.
 * @param exclusive: if true, additionally check if the bundle is connected
 *                   only to |other|, and return false if not. */
bool
Bundle::connected_to (boost::shared_ptr<Bundle> other, AudioEngine & engine,
                      DataType type, bool exclusive)
{
	if (_ports_are_inputs == other->_ports_are_inputs)
		return false;

	if (type == DataType::NIL) {
		for (DataType::iterator t = DataType::begin();
		                        t != DataType::end(); ++t) {
			if (!connected_to(other, engine, *t, exclusive))
				return false;
		}
		return true;
	}

	uint32_t N = nchannels().n(type);
	if (other->nchannels().n(type) != N)
		return false;

	vector<string> port_connections;

	for (uint32_t i = 0; i < N; ++i) {
		Bundle::PortList const & our_ports =
			channel_ports (type_channel_to_overall(type, i));
		Bundle::PortList const & other_ports =
			other->channel_ports (other->type_channel_to_overall(type, i));

		for (Bundle::PortList::const_iterator j = our_ports.begin(); j != our_ports.end(); ++j) {

			boost::shared_ptr<Port> p = engine.get_port_by_name(*j);

			for (Bundle::PortList::const_iterator k = other_ports.begin();
			                                   k != other_ports.end(); ++k) {
				boost::shared_ptr<Port> q = engine.get_port_by_name(*k);

				if (!p && !q) {
					return false;
				}

				if (p && !p->connected_to (*k)) {
					return false;
				} else if (q && !q->connected_to (*j)) {
					return false;
				}
			}

			if (exclusive && p) {
				port_connections.clear();
				p->get_connections(port_connections);
				if (port_connections.size() != other_ports.size())
					return false;
			}
		}
	}

	return true;
}

/** This must not be called in code executed as a response to a backend event,
 *  as it uses the backend port_get_all_connections().
 *  @return true if any of this bundle's channels are connected to anything.
 */
bool
Bundle::connected_to_anything (AudioEngine& engine)
{
	PortManager& pm (engine);

	for (uint32_t i = 0; i < n_total(); ++i) {
		Bundle::PortList const & ports = channel_ports (i);

		for (uint32_t j = 0; j < ports.size(); ++j) {

			/* ports[j] may not be an Ardour port, so use the port manager directly
			   rather than doing it with Port.
			*/

			if (pm.connected (ports[j])) {
				return true;
			}
		}
	}

	return false;
}

void
Bundle::set_ports_are_inputs ()
{
	_ports_are_inputs = true;
	emit_changed (DirectionChanged);
}

void
Bundle::set_ports_are_outputs ()
{
	_ports_are_inputs = false;
	emit_changed (DirectionChanged);
}

/** Set the name.
 *  @param n New name.
 */
void
Bundle::set_name (string const & n)
{
	_name = n;
	emit_changed (NameChanged);
}

/** @param b Other bundle.
 *  @return true if b has the same number of channels as this bundle, and those channels have corresponding ports.
 */
bool
Bundle::has_same_ports (boost::shared_ptr<Bundle> b) const
{
	ChanCount our_count = nchannels();
	ChanCount other_count = b->nchannels();

	if (our_count != other_count)
		return false;

	for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
		uint32_t N = our_count.n(*t);
		for (uint32_t i = 0; i < N; ++i) {
			Bundle::PortList const & our_ports =
				channel_ports (type_channel_to_overall(*t, i));
			Bundle::PortList const & other_ports =
				b->channel_ports (b->type_channel_to_overall(*t, i));

			if (our_ports != other_ports)
				return false;
		}
	}

	return true;
}

DataType
Bundle::channel_type (uint32_t c) const
{
	assert (c < n_total());

	Glib::Threads::Mutex::Lock lm (_channel_mutex);
	return _channel[c].type;
}

ostream &
operator<< (ostream& os, Bundle const & b)
{
	os << "BUNDLE " << b.nchannels() << " channels: ";
	for (uint32_t i = 0; i < b.n_total(); ++i) {
		os << "( ";
		Bundle::PortList const & pl = b.channel_ports (i);
		for (Bundle::PortList::const_iterator j = pl.begin(); j != pl.end(); ++j) {
			os << *j << " ";
		}
		os << ") ";
	}

	return os;
}

bool
Bundle::operator== (Bundle const & other)
{
	return _channel == other._channel;
}

/** Given an index of a channel as the nth channel of a particular type,
 *  return an index of that channel when considering channels of all types.
 *
 *  e.g. given a bundle with channels:
 *          fred   [audio]
 *          jim    [audio]
 *          sheila [midi]
 *
 * If t == MIDI and c == 0, then we would return 2, as 2 is the index of the
 * 0th MIDI channel.
 *
 * If t == NIL, we just return c.
 */

uint32_t
Bundle::type_channel_to_overall (DataType t, uint32_t c) const
{
	if (t == DataType::NIL) {
		return c;
	}

	Glib::Threads::Mutex::Lock lm (_channel_mutex);

	vector<Channel>::const_iterator i = _channel.begin ();

	uint32_t o = 0;

	while (1) {

		assert (i != _channel.end ());

		if (i->type != t) {
			++i;
		} else {
			if (c == 0) {
				return o;
			}
			--c;
		}

		++o;
	}

	abort(); /* NOTREACHED */
	return -1;
}

/** Perform the reverse of type_channel_to_overall */
uint32_t
Bundle::overall_channel_to_type (DataType t, uint32_t c) const
{
	if (t == DataType::NIL) {
		return c;
	}

	Glib::Threads::Mutex::Lock lm (_channel_mutex);

	uint32_t s = 0;

	vector<Channel>::const_iterator i = _channel.begin ();
	for (uint32_t j = 0; j < c; ++j) {
		if (i->type == t) {
			++s;
		}
		++i;
	}

	return s;
}