only the last step-edited note remains selected after each note addition; waf install...
[ardour.git] / gtk2_ardour / port_group.cc
1 /*
2     Copyright (C) 2002-2009 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 <cstring>
21 #include <boost/shared_ptr.hpp>
22
23 #include "midi++/manager.h"
24 #include "midi++/mmc.h"
25
26 #include "ardour/audio_track.h"
27 #include "ardour/audioengine.h"
28 #include "ardour/bundle.h"
29 #include "ardour/user_bundle.h"
30 #include "ardour/io_processor.h"
31 #include "ardour/midi_track.h"
32 #include "ardour/port.h"
33 #include "ardour/session.h"
34 #include "ardour/auditioner.h"
35 #include "ardour/control_protocol_manager.h"
36 #include "control_protocol/control_protocol.h"
37
38 #include "gui_thread.h"
39 #include "port_group.h"
40 #include "port_matrix.h"
41 #include "time_axis_view.h"
42 #include "public_editor.h"
43
44 #include "i18n.h"
45
46 using namespace std;
47 using namespace Gtk;
48 using namespace ARDOUR;
49
50 /** PortGroup constructor.
51  * @param n Name.
52  */
53 PortGroup::PortGroup (std::string const & n)
54         : name (n)
55 {
56
57 }
58
59 PortGroup::~PortGroup()
60 {
61         for (BundleList::iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
62                 delete *i;
63         }
64         _bundles.clear ();
65 }
66
67 /** Add a bundle to a group.
68  *  @param b Bundle.
69  *  @param allow_dups true to allow the group to contain more than one bundle with the same port, otherwise false.
70  */
71 void
72 PortGroup::add_bundle (boost::shared_ptr<Bundle> b, bool allow_dups)
73 {
74         add_bundle_internal (b, boost::shared_ptr<IO> (), false, Gdk::Color (), allow_dups);
75 }
76
77 /** Add a bundle to a group.
78  *  @param b Bundle.
79  *  @param io IO whose ports are in the bundle.
80  */
81 void
82 PortGroup::add_bundle (boost::shared_ptr<Bundle> b, boost::shared_ptr<IO> io)
83 {
84         add_bundle_internal (b, io, false, Gdk::Color (), false);
85 }
86
87 /** Add a bundle to a group.
88  *  @param b Bundle.
89  *  @param c Colour to represent the bundle with.
90  */
91 void
92 PortGroup::add_bundle (boost::shared_ptr<Bundle> b, boost::shared_ptr<IO> io, Gdk::Color c)
93 {
94         add_bundle_internal (b, io, true, c, false);
95 }
96
97 PortGroup::BundleRecord::BundleRecord (boost::shared_ptr<ARDOUR::Bundle> b, boost::shared_ptr<ARDOUR::IO> iop, Gdk::Color c, bool has_c)
98         : bundle (b)
99         , io (iop)
100         , colour (c)
101         , has_colour (has_c)
102 {
103 }
104
105 void
106 PortGroup::add_bundle_internal (boost::shared_ptr<Bundle> b, boost::shared_ptr<IO> io, bool has_colour, Gdk::Color colour, bool allow_dups)
107 {
108         assert (b.get());
109
110         if (!allow_dups) {
111                 
112                 /* don't add this bundle if we already have one with the same ports */
113                 
114                 BundleList::iterator i = _bundles.begin ();
115                 while (i != _bundles.end() && b->has_same_ports ((*i)->bundle) == false) {
116                         ++i;
117                 }
118                 
119                 if (i != _bundles.end ()) {
120                         return;
121                 }
122         }
123
124         BundleRecord* br = new BundleRecord (b, io, colour, has_colour);
125         b->Changed.connect (br->changed_connection, invalidator (*this), ui_bind (&PortGroup::bundle_changed, this, _1), gui_context());
126         _bundles.push_back (br);
127
128         Changed ();     
129 }
130
131 void
132 PortGroup::remove_bundle (boost::shared_ptr<Bundle> b)
133 {
134         assert (b.get());
135
136         BundleList::iterator i = _bundles.begin ();
137         while (i != _bundles.end() && (*i)->bundle != b) {
138                 ++i;
139         }
140
141         if (i == _bundles.end()) {
142                 return;
143         }
144
145         delete *i;
146         _bundles.erase (i);
147
148         Changed ();
149 }
150
151 void
152 PortGroup::bundle_changed (Bundle::Change c)
153 {
154         BundleChanged (c);
155 }
156
157
158 void
159 PortGroup::clear ()
160 {
161         for (BundleList::iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
162                 delete *i;
163         }
164
165         _bundles.clear ();
166         Changed ();
167 }
168
169 bool
170 PortGroup::has_port (std::string const& p) const
171 {
172         for (BundleList::const_iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
173                 if ((*i)->bundle->offers_port_alone (p)) {
174                         return true;
175                 }
176         }
177
178         return false;
179 }
180
181 boost::shared_ptr<Bundle>
182 PortGroup::only_bundle ()
183 {
184         assert (_bundles.size() == 1);
185         return _bundles.front()->bundle;
186 }
187
188
189 ChanCount
190 PortGroup::total_channels () const
191 {
192         ChanCount n;
193         for (BundleList::const_iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
194                 n += (*i)->bundle->nchannels ();
195         }
196
197         return n;
198 }
199
200 boost::shared_ptr<IO>
201 PortGroup::io_from_bundle (boost::shared_ptr<ARDOUR::Bundle> b) const
202 {
203         BundleList::const_iterator i = _bundles.begin ();
204         while (i != _bundles.end() && (*i)->bundle != b) {
205                 ++i;
206         }
207
208         if (i == _bundles.end()) {
209                 return boost::shared_ptr<IO> ();
210         }
211
212         return (*i)->io;
213 }
214
215 /** Remove bundles whose channels are already represented by other, larger bundles */
216 void
217 PortGroup::remove_duplicates ()
218 {
219         BundleList::iterator i = _bundles.begin();
220         while (i != _bundles.end()) {
221
222                 BundleList::iterator tmp = i;
223                 ++tmp;
224
225                 bool remove = false;
226
227                 for (BundleList::iterator j = _bundles.begin(); j != _bundles.end(); ++j) {
228
229                         if ((*j)->bundle->nchannels() > (*i)->bundle->nchannels()) {
230                                 /* this bundle is larger */
231
232                                 uint32_t k = 0;
233                                 while (k < (*i)->bundle->nchannels().n_total()) {
234                                         /* see if this channel on *i has an equivalent on *j */
235                                         uint32_t l = 0;
236                                         while (l < (*j)->bundle->nchannels().n_total() && (*i)->bundle->channel_ports (k) != (*j)->bundle->channel_ports (l)) {
237                                                 ++l;
238                                         }
239
240                                         if (l == (*j)->bundle->nchannels().n_total()) {
241                                                 /* it does not */
242                                                 break;
243                                         }
244
245                                         ++k;
246                                 }
247
248                                 if (k == (*i)->bundle->nchannels().n_total()) {
249                                         /* all channels on *i are represented by the larger bundle *j, so remove *i */
250                                         remove = true;
251                                         break;
252                                 }
253                         }
254                 }
255                 
256                 if (remove) {
257                         _bundles.erase (i);
258                 }
259                 
260                 i = tmp;
261         }
262 }
263
264
265 /** PortGroupList constructor.
266  */
267 PortGroupList::PortGroupList ()
268         : _signals_suspended (false), _pending_change (false), _pending_bundle_change ((Bundle::Change) 0)
269 {
270
271 }
272
273 PortGroupList::~PortGroupList() 
274 {
275         /* XXX need to clean up bundles, but ownership shared with PortGroups */
276 }
277
278 void
279 PortGroupList::maybe_add_processor_to_list (
280         boost::weak_ptr<Processor> wp, list<boost::shared_ptr<IO> >* route_ios, bool inputs, set<boost::shared_ptr<IO> >& used_io
281         )
282 {
283         boost::shared_ptr<Processor> p (wp.lock());
284
285         if (!p) {
286                 return;
287         }
288
289         boost::shared_ptr<IOProcessor> iop = boost::dynamic_pointer_cast<IOProcessor> (p);
290
291         if (iop) {
292
293                 boost::shared_ptr<IO> io = inputs ? iop->input() : iop->output();
294
295                 if (io && used_io.find (io) == used_io.end()) {
296                         route_ios->push_back (io);
297                         used_io.insert (io);
298                 }
299         }
300 }
301
302 struct RouteIOs {
303         RouteIOs (boost::shared_ptr<Route> r, boost::shared_ptr<IO> i) {
304                 route = r;
305                 ios.push_back (i);
306         }
307         
308         boost::shared_ptr<Route> route;
309         std::list<boost::shared_ptr<IO> > ios;
310 };
311
312 class RouteIOsComparator {
313 public:
314         bool operator() (RouteIOs const & a, RouteIOs const & b) {
315                 return a.route->order_key (X_("editor")) < b.route->order_key (X_("editor"));
316         }
317 };
318
319 /** Gather ports from around the system and put them in this PortGroupList.
320  *  @param type Type of ports to collect, or NIL for all types.
321  */
322 void
323 PortGroupList::gather (ARDOUR::Session* session, ARDOUR::DataType type, bool inputs, bool allow_dups)
324 {
325         clear ();
326
327         if (session == 0) {
328                 return;
329         }
330
331         boost::shared_ptr<PortGroup> bus (new PortGroup (_("Bus")));
332         boost::shared_ptr<PortGroup> track (new PortGroup (_("Track")));
333         boost::shared_ptr<PortGroup> system (new PortGroup (_("System")));
334         boost::shared_ptr<PortGroup> ardour (new PortGroup (_("Ardour")));
335         boost::shared_ptr<PortGroup> other (new PortGroup (_("Other")));
336
337         /* Find the IOs which have bundles for routes and their processors.  We store
338            these IOs in a RouteIOs class so that we can then sort the results by route
339            order key.
340         */
341
342         boost::shared_ptr<RouteList> routes = session->get_routes ();
343         list<RouteIOs> route_ios;
344
345         for (RouteList::const_iterator i = routes->begin(); i != routes->end(); ++i) {
346
347                 /* keep track of IOs that we have taken bundles from,
348                    so that we can avoid taking the same IO from both
349                    Route::output() and the main_outs Delivery */
350
351                 set<boost::shared_ptr<IO> > used_io;
352                 boost::shared_ptr<IO> io = inputs ? (*i)->input() : (*i)->output();
353                 used_io.insert (io);
354
355                 RouteIOs rb (*i, io);
356                 (*i)->foreach_processor (boost::bind (&PortGroupList::maybe_add_processor_to_list, this, _1, &rb.ios, inputs, used_io));
357
358                 route_ios.push_back (rb);
359         }
360
361         /* Sort RouteIOs by the routes' editor order keys */
362         route_ios.sort (RouteIOsComparator ());
363
364         /* Now put the bundles that belong to these sorted RouteIOs into the PortGroup.
365            Note that if the RouteIO's bundles are multi-type, we may make new Bundles
366            with only the ports of one type.
367         */
368         
369         for (list<RouteIOs>::iterator i = route_ios.begin(); i != route_ios.end(); ++i) {
370                 TimeAxisView* tv = PublicEditor::instance().axis_view_from_route (i->route);
371
372                 /* Work out which group to put these IOs' bundles in */
373                 boost::shared_ptr<PortGroup> g;
374                 if (boost::dynamic_pointer_cast<Track> (i->route)) {
375                         g = track;
376                 } else {
377                         g = bus;
378                 }
379
380                 for (list<boost::shared_ptr<IO> >::iterator j = i->ios.begin(); j != i->ios.end(); ++j) {
381                         boost::shared_ptr<Bundle> b = bundle_for_type ((*j)->bundle(), type);
382                         if (b->nchannels() != ChanCount::ZERO) {
383                                 if (tv) {
384                                         g->add_bundle (b, *j, tv->color ());
385                                 } else {
386                                         g->add_bundle (b, *j);
387                                 }
388                         }
389                 }
390         }
391
392         /* Bundles owned by the session; add user bundles first, then normal ones, so
393            that UserBundles that offer the same ports as a normal bundle get priority
394         */
395
396         boost::shared_ptr<BundleList> b = session->bundles ();
397
398         for (BundleList::iterator i = b->begin(); i != b->end(); ++i) {
399                 if (boost::dynamic_pointer_cast<UserBundle> (*i) && (*i)->ports_are_inputs() == inputs) {
400                         boost::shared_ptr<Bundle> b = bundle_for_type (*i, type);
401                         if (b->nchannels() != ChanCount::ZERO) {
402                                 system->add_bundle (b, allow_dups);
403                         }
404                 }
405         }
406
407         for (BundleList::iterator i = b->begin(); i != b->end(); ++i) {
408                 if (boost::dynamic_pointer_cast<UserBundle> (*i) == 0 && (*i)->ports_are_inputs() == inputs) {
409                         boost::shared_ptr<Bundle> b = bundle_for_type (*i, type);
410                         if (b->nchannels() != ChanCount::ZERO) {
411                                 system->add_bundle (b, allow_dups);
412                         }
413                 }
414         }
415         
416         /* Ardour stuff */
417
418         if (!inputs) {
419                 boost::shared_ptr<Bundle> b = bundle_for_type (session->the_auditioner()->output()->bundle(), type);
420                 if (b->nchannels() != ChanCount::ZERO) {
421                         ardour->add_bundle (b);
422                 }
423
424                 b = bundle_for_type (session->click_io()->bundle(), type);
425                 if (b->nchannels() != ChanCount::ZERO) {
426                         ardour->add_bundle (b);
427                 }
428         }
429
430         /* Ardour's surfaces */
431
432         ControlProtocolManager& m = ControlProtocolManager::instance ();
433         for (list<ControlProtocolInfo*>::iterator i = m.control_protocol_info.begin(); i != m.control_protocol_info.end(); ++i) {
434                 if ((*i)->protocol) {
435                         list<boost::shared_ptr<Bundle> > b = (*i)->protocol->bundles ();
436                         for (list<boost::shared_ptr<Bundle> >::iterator j = b.begin(); j != b.end(); ++j) {
437                                 if ((*j)->ports_are_inputs() == inputs) {
438                                         ardour->add_bundle (*j);
439                                 }
440                         }
441                 }
442         }
443
444         /* Ardour's sync ports */
445
446         MIDI::Manager* midi_manager = MIDI::Manager::instance ();
447         if (midi_manager && (type == DataType::MIDI || type == DataType::NIL)) {
448                 boost::shared_ptr<Bundle> sync (new Bundle (_("Sync"), inputs));
449                 MIDI::MachineControl* mmc = midi_manager->mmc ();
450                 AudioEngine& ae = session->engine ();
451                 if (inputs) {
452                         sync->add_channel (
453                                 _("MTC in"), DataType::MIDI, ae.make_port_name_non_relative (midi_manager->mtc_input_port()->name())
454                                 );
455                         sync->add_channel (
456                                 _("MIDI control in"), DataType::MIDI, ae.make_port_name_non_relative (midi_manager->midi_input_port()->name())
457                                 );
458                         sync->add_channel (
459                                 _("MIDI clock in"), DataType::MIDI, ae.make_port_name_non_relative (midi_manager->midi_clock_input_port()->name())
460                                 );
461                         sync->add_channel (
462                                 _("MMC in"), DataType::MIDI, ae.make_port_name_non_relative (mmc->input_port()->name())
463                                 );
464                 } else {
465                         sync->add_channel (
466                                 _("MTC out"), DataType::MIDI, ae.make_port_name_non_relative (midi_manager->mtc_output_port()->name())
467                                 );
468                         sync->add_channel (
469                                 _("MIDI control out"), DataType::MIDI, ae.make_port_name_non_relative (midi_manager->midi_output_port()->name())
470                                 );
471                         sync->add_channel (
472                                 _("MIDI clock out"), DataType::MIDI, ae.make_port_name_non_relative (midi_manager->midi_clock_output_port()->name())
473                                 );
474                         sync->add_channel (
475                                 _("MMC out"), DataType::MIDI, ae.make_port_name_non_relative (mmc->output_port()->name())
476                                 );
477                 }
478                 
479                 ardour->add_bundle (sync);
480         }
481
482         /* Now find all other ports that we haven't thought of yet */
483
484         std::vector<std::string> extra_system[DataType::num_types];
485         std::vector<std::string> extra_other[DataType::num_types];
486
487         const char ** ports = 0;
488         if (type == DataType::NIL) {
489                 ports = session->engine().get_ports ("", "", inputs ? JackPortIsInput : JackPortIsOutput);
490         } else {
491                 ports = session->engine().get_ports ("", type.to_jack_type(), inputs ? JackPortIsInput : JackPortIsOutput);
492         }
493         
494         if (ports) {
495
496                 int n = 0;
497
498                 while (ports[n]) {
499
500                         std::string const p = ports[n];
501
502                         if (!system->has_port(p) &&
503                             !bus->has_port(p) &&
504                             !track->has_port(p) &&
505                             !ardour->has_port(p) &&
506                             !other->has_port(p)) {
507                                 
508                                 /* special hack: ignore MIDI ports labelled Midi-Through. these
509                                    are basically useless and mess things up for default
510                                    connections.
511                                 */
512
513                                 if (p.find ("Midi-Through") != string::npos) {
514                                         ++n;
515                                         continue;
516                                 }
517
518                                 /* can't use the audio engine for this as we are looking at non-Ardour ports */
519
520                                 jack_port_t* jp = jack_port_by_name (session->engine().jack(), p.c_str());
521                                 if (jp) {
522                                         DataType t (jack_port_type (jp));
523                                         if (t != DataType::NIL) {
524                                                 if (port_has_prefix (p, "system:") || port_has_prefix (p, "alsa_pcm") || port_has_prefix (p, "ardour:")) {
525                                                         extra_system[t].push_back (p);
526                                                 } else {
527                                                         extra_other[t].push_back (p);
528                                                 }
529                                         }
530                                 }
531                         }
532
533                         ++n;
534                 }
535
536                 free (ports);
537         }
538
539         for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
540                 if (!extra_system[*i].empty()) {
541                         boost::shared_ptr<Bundle> b = make_bundle_from_ports (extra_system[*i], *i, inputs);
542                         boost::shared_ptr<Bundle> bt = bundle_for_type (b, type);
543                         if (bt->nchannels() != ChanCount::ZERO) {
544                                 system->add_bundle (bt);
545                         }
546                 }
547         }
548
549         for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
550                 if (!extra_other[*i].empty()) {
551                         boost::shared_ptr<Bundle> b = make_bundle_from_ports (extra_other[*i], *i, inputs);
552                         boost::shared_ptr<Bundle> bt = bundle_for_type (b, type);
553                         if (bt->nchannels() != ChanCount::ZERO) {
554                                 other->add_bundle (bt);
555                         }
556                 }
557         }
558
559         if (!allow_dups) {
560                 system->remove_duplicates ();
561         }
562
563         add_group_if_not_empty (other);
564         add_group_if_not_empty (bus);
565         add_group_if_not_empty (track);
566         add_group_if_not_empty (ardour);
567         add_group_if_not_empty (system);
568
569         emit_changed ();
570 }
571
572 boost::shared_ptr<Bundle>
573 PortGroupList::make_bundle_from_ports (std::vector<std::string> const & p, ARDOUR::DataType type, bool inputs) const
574 {
575         boost::shared_ptr<Bundle> b (new Bundle ("", inputs));
576
577         std::string const pre = common_prefix (p);
578         if (!pre.empty()) {
579                 b->set_name (pre.substr (0, pre.length() - 1));
580         }
581
582         for (uint32_t j = 0; j < p.size(); ++j) {
583                 b->add_channel (p[j].substr (pre.length()), type);
584                 b->set_port (j, p[j]);
585         }
586
587         return b;
588 }
589
590 bool
591 PortGroupList::port_has_prefix (const std::string& n, const std::string& p) const
592 {
593         return n.substr (0, p.length()) == p;
594 }
595
596 std::string
597 PortGroupList::common_prefix_before (std::vector<std::string> const & p, std::string const & s) const
598 {
599         /* we must have some strings and the first must contain the separator string */
600         if (p.empty() || p[0].find_first_of (s) == std::string::npos) {
601                 return "";
602         }
603
604         /* prefix of the first string */
605         std::string const fp = p[0].substr (0, p[0].find_first_of (s) + 1);
606
607         /* see if the other strings also start with fp */
608         uint32_t j = 1;
609         while (j < p.size()) {
610                 if (p[j].substr (0, fp.length()) != fp) {
611                         break;
612                 }
613                 ++j;
614         }
615
616         if (j != p.size()) {
617                 return "";
618         }
619
620         return fp;
621 }
622
623
624 std::string
625 PortGroupList::common_prefix (std::vector<std::string> const & p) const
626 {
627         /* common prefix before '/' ? */
628         std::string cp = common_prefix_before (p, "/");
629         if (!cp.empty()) {
630                 return cp;
631         }
632
633         cp = common_prefix_before (p, ":");
634         if (!cp.empty()) {
635                 return cp;
636         }
637
638         return "";
639 }
640
641 void
642 PortGroupList::clear ()
643 {
644         _groups.clear ();
645         _bundle_changed_connections.drop_connections ();
646         emit_changed ();
647 }
648
649
650 PortGroup::BundleList const &
651 PortGroupList::bundles () const
652 {
653         _bundles.clear ();
654
655         for (PortGroupList::List::const_iterator i = begin (); i != end (); ++i) {
656                 std::copy ((*i)->bundles().begin(), (*i)->bundles().end(), std::back_inserter (_bundles));
657         }
658
659         return _bundles;
660 }
661
662 ChanCount
663 PortGroupList::total_channels () const
664 {
665         ChanCount n;
666
667         for (PortGroupList::List::const_iterator i = begin(); i != end(); ++i) {
668                 n += (*i)->total_channels ();
669         }
670
671         return n;
672 }
673
674 void
675 PortGroupList::add_group_if_not_empty (boost::shared_ptr<PortGroup> g)
676 {
677         if (!g->bundles().empty ()) {
678                 add_group (g);
679         }
680 }
681
682 void
683 PortGroupList::add_group (boost::shared_ptr<PortGroup> g)
684 {
685         _groups.push_back (g);
686
687         g->Changed.connect (_changed_connections, invalidator (*this), boost::bind (&PortGroupList::emit_changed, this), gui_context());
688         g->BundleChanged.connect (_bundle_changed_connections, invalidator (*this), ui_bind (&PortGroupList::emit_bundle_changed, this, _1), gui_context());
689
690         emit_changed ();
691 }
692
693 void
694 PortGroupList::remove_bundle (boost::shared_ptr<Bundle> b)
695 {
696         for (List::iterator i = _groups.begin(); i != _groups.end(); ++i) {
697                 (*i)->remove_bundle (b);
698         }
699
700         emit_changed ();
701 }
702
703 void
704 PortGroupList::emit_changed ()
705 {
706         if (_signals_suspended) {
707                 _pending_change = true;
708         } else {
709                 Changed ();
710         }
711 }
712
713 void
714 PortGroupList::emit_bundle_changed (Bundle::Change c)
715 {
716         if (_signals_suspended) {
717                 _pending_bundle_change = c;
718         } else {
719                 BundleChanged (c);
720         }
721 }
722 void
723 PortGroupList::suspend_signals ()
724 {
725         _signals_suspended = true;
726 }
727
728 void
729 PortGroupList::resume_signals ()
730 {
731         if (_pending_change) {
732                 Changed ();
733                 _pending_change = false;
734         }
735
736         if (_pending_bundle_change != 0) {
737                 BundleChanged (_pending_bundle_change);
738                 _pending_bundle_change = (ARDOUR::Bundle::Change) 0;
739         }
740
741         _signals_suspended = false;
742 }
743
744 boost::shared_ptr<IO>
745 PortGroupList::io_from_bundle (boost::shared_ptr<ARDOUR::Bundle> b) const
746 {
747         List::const_iterator i = _groups.begin ();
748         while (i != _groups.end()) {
749                 boost::shared_ptr<IO> io = (*i)->io_from_bundle (b);
750                 if (io) {
751                         return io;
752                 }
753                 ++i;
754         }
755
756         return boost::shared_ptr<IO> ();
757 }
758
759 bool
760 PortGroupList::empty () const
761 {
762         List::const_iterator i = _groups.begin ();
763         while (i != _groups.end() && (*i)->total_channels() == ChanCount::ZERO) {
764                 ++i;
765         }
766
767         return (i == _groups.end());
768 }
769
770 /** Take a bundle, and either return it, if it contains only ports of type \a t,
771  *  or return a new bundle with those ports from \a b which are of type \a t.
772  *  Note that t == NIL is taken to mean "all types".
773  */
774 boost::shared_ptr<Bundle>
775 PortGroupList::bundle_for_type (boost::shared_ptr<Bundle> b, DataType t) const
776 {
777         /* We are asked for a bundle with all types, so that's easy */
778         if (t == DataType::NIL) {
779                 return b;
780         }
781
782         if (b->nchannels().get(t) == b->nchannels().n_total()) {
783                 /* All channels on b are of the correct type, so just return b */
784                 return b;
785         }
786
787         /* We must build a new bundle */
788         boost::shared_ptr<Bundle> n (new ARDOUR::Bundle (b->name(), b->ports_are_inputs()));
789         for (uint32_t i = 0; i < b->nchannels().n_total(); ++i) {
790                 if (b->channel_type(i) == t) {
791                         n->add_channel (b->channel_name (i), t, b->channel_ports (i));
792                 }
793         }
794
795         return n;
796 }