lots of details relating to MIDI file management; try to ignore ALSA sequencer MIDI...
[ardour2.git] / gtk2_ardour / port_group.cc
bloba010b3c184b4be91743ca6820d020e178142c003
1 /*
2 Copyright (C) 2002-2009 Paul Davis
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.
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.
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.
20 #include <cstring>
21 #include <boost/shared_ptr.hpp>
23 #include "ardour/audio_track.h"
24 #include "ardour/audioengine.h"
25 #include "ardour/bundle.h"
26 #include "ardour/user_bundle.h"
27 #include "ardour/io_processor.h"
28 #include "ardour/midi_track.h"
29 #include "ardour/port.h"
30 #include "ardour/session.h"
31 #include "ardour/auditioner.h"
33 #include "gui_thread.h"
34 #include "port_group.h"
35 #include "port_matrix.h"
36 #include "time_axis_view.h"
37 #include "public_editor.h"
39 #include "i18n.h"
41 using namespace std;
42 using namespace Gtk;
43 using namespace ARDOUR;
45 /** PortGroup constructor.
46 * @param n Name.
48 PortGroup::PortGroup (std::string const & n)
49 : name (n)
54 PortGroup::~PortGroup()
56 for (BundleList::iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
57 delete *i;
59 _bundles.clear ();
62 /** Add a bundle to a group.
63 * @param b Bundle.
64 * @param allow_dups true to allow the group to contain more than one bundle with the same port, otherwise false.
66 void
67 PortGroup::add_bundle (boost::shared_ptr<Bundle> b, bool allow_dups)
69 add_bundle_internal (b, boost::shared_ptr<IO> (), false, Gdk::Color (), allow_dups);
72 /** Add a bundle to a group.
73 * @param b Bundle.
74 * @param io IO whose ports are in the bundle.
76 void
77 PortGroup::add_bundle (boost::shared_ptr<Bundle> b, boost::shared_ptr<IO> io)
79 add_bundle_internal (b, io, false, Gdk::Color (), false);
82 /** Add a bundle to a group.
83 * @param b Bundle.
84 * @param c Colour to represent the bundle with.
86 void
87 PortGroup::add_bundle (boost::shared_ptr<Bundle> b, boost::shared_ptr<IO> io, Gdk::Color c)
89 add_bundle_internal (b, io, true, c, false);
92 PortGroup::BundleRecord::BundleRecord (boost::shared_ptr<ARDOUR::Bundle> b, boost::shared_ptr<ARDOUR::IO> iop, Gdk::Color c, bool has_c)
93 : bundle (b)
94 , io (iop)
95 , colour (c)
96 , has_colour (has_c)
100 void
101 PortGroup::add_bundle_internal (boost::shared_ptr<Bundle> b, boost::shared_ptr<IO> io, bool has_colour, Gdk::Color colour, bool allow_dups)
103 assert (b.get());
105 if (!allow_dups) {
107 /* don't add this bundle if we already have one with the same ports */
109 BundleList::iterator i = _bundles.begin ();
110 while (i != _bundles.end() && b->has_same_ports ((*i)->bundle) == false) {
111 ++i;
114 if (i != _bundles.end ()) {
115 return;
119 BundleRecord* br = new BundleRecord (b, io, colour, has_colour);
120 b->Changed.connect (br->changed_connection, invalidator (*this), ui_bind (&PortGroup::bundle_changed, this, _1), gui_context());
121 _bundles.push_back (br);
123 Changed ();
126 void
127 PortGroup::remove_bundle (boost::shared_ptr<Bundle> b)
129 assert (b.get());
131 BundleList::iterator i = _bundles.begin ();
132 while (i != _bundles.end() && (*i)->bundle != b) {
133 ++i;
136 if (i == _bundles.end()) {
137 return;
140 delete *i;
141 _bundles.erase (i);
143 Changed ();
146 void
147 PortGroup::bundle_changed (Bundle::Change c)
149 BundleChanged (c);
153 void
154 PortGroup::clear ()
156 for (BundleList::iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
157 delete *i;
160 _bundles.clear ();
161 Changed ();
164 bool
165 PortGroup::has_port (std::string const& p) const
167 for (BundleList::const_iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
168 if ((*i)->bundle->offers_port_alone (p)) {
169 return true;
173 return false;
176 boost::shared_ptr<Bundle>
177 PortGroup::only_bundle ()
179 assert (_bundles.size() == 1);
180 return _bundles.front()->bundle;
184 uint32_t
185 PortGroup::total_channels () const
187 uint32_t n = 0;
188 for (BundleList::const_iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
189 n += (*i)->bundle->nchannels ();
192 return n;
195 boost::shared_ptr<IO>
196 PortGroup::io_from_bundle (boost::shared_ptr<ARDOUR::Bundle> b) const
198 BundleList::const_iterator i = _bundles.begin ();
199 while (i != _bundles.end() && (*i)->bundle != b) {
200 ++i;
203 if (i == _bundles.end()) {
204 return boost::shared_ptr<IO> ();
207 return (*i)->io;
210 /** Remove bundles whose channels are already represented by other, larger bundles */
211 void
212 PortGroup::remove_duplicates ()
214 BundleList::iterator i = _bundles.begin();
215 while (i != _bundles.end()) {
217 BundleList::iterator tmp = i;
218 ++tmp;
220 bool remove = false;
222 for (BundleList::iterator j = _bundles.begin(); j != _bundles.end(); ++j) {
224 if ((*j)->bundle->nchannels() > (*i)->bundle->nchannels()) {
225 /* this bundle is larger */
227 uint32_t k = 0;
228 while (k < (*i)->bundle->nchannels()) {
229 /* see if this channel on *i has an equivalent on *j */
230 uint32_t l = 0;
231 while (l < (*j)->bundle->nchannels() && (*i)->bundle->channel_ports (k) != (*j)->bundle->channel_ports (l)) {
232 ++l;
235 if (l == (*j)->bundle->nchannels()) {
236 /* it does not */
237 break;
240 ++k;
243 if (k == (*i)->bundle->nchannels ()) {
244 /* all channels on *i are represented by the larger bundle *j, so remove *i */
245 remove = true;
246 break;
251 if (remove) {
252 _bundles.erase (i);
255 i = tmp;
260 /** PortGroupList constructor.
262 PortGroupList::PortGroupList ()
263 : _type (DataType::AUDIO), _signals_suspended (false), _pending_change (false), _pending_bundle_change ((Bundle::Change) 0)
268 PortGroupList::~PortGroupList()
270 /* XXX need to clean up bundles, but ownership shared with PortGroups */
273 void
274 PortGroupList::set_type (DataType t)
276 _type = t;
277 clear ();
280 void
281 PortGroupList::maybe_add_processor_to_list (
282 boost::weak_ptr<Processor> wp, list<boost::shared_ptr<Bundle> >* route_bundles, bool inputs, set<boost::shared_ptr<IO> >& used_io
285 boost::shared_ptr<Processor> p (wp.lock());
287 if (!p) {
288 return;
291 boost::shared_ptr<IOProcessor> iop = boost::dynamic_pointer_cast<IOProcessor> (p);
293 if (iop) {
295 boost::shared_ptr<IO> io = inputs ? iop->input() : iop->output();
297 if (io && used_io.find (io) == used_io.end()) {
298 route_bundles->push_back (io->bundle ());
299 used_io.insert (io);
305 /** Gather bundles from around the system and put them in this PortGroupList */
306 void
307 PortGroupList::gather (ARDOUR::Session* session, bool inputs, bool allow_dups)
309 clear ();
311 if (session == 0) {
312 return;
315 boost::shared_ptr<PortGroup> bus (new PortGroup (_("Bus")));
316 boost::shared_ptr<PortGroup> track (new PortGroup (_("Track")));
317 boost::shared_ptr<PortGroup> system (new PortGroup (_("System")));
318 boost::shared_ptr<PortGroup> ardour (new PortGroup (_("Ardour")));
319 boost::shared_ptr<PortGroup> other (new PortGroup (_("Other")));
321 /* Find the bundles for routes. We use the RouteBundle class to join
322 the route's input/output and processor bundles together so that they
323 are presented as one bundle in the matrix. */
325 boost::shared_ptr<RouteList> routes = session->get_routes ();
327 for (RouteList::const_iterator i = routes->begin(); i != routes->end(); ++i) {
329 list<boost::shared_ptr<Bundle> > route_bundles;
331 /* keep track of IOs that we have taken bundles from,
332 so that we can avoid taking the same IO from both
333 Route::output() and the main_outs Delivery */
335 set<boost::shared_ptr<IO> > used_io;
336 boost::shared_ptr<IO> io = inputs ? (*i)->input() : (*i)->output();
337 used_io.insert (io);
339 route_bundles.push_back (io->bundle ());
341 (*i)->foreach_processor (boost::bind (&PortGroupList::maybe_add_processor_to_list, this, _1, &route_bundles, inputs, used_io));
343 /* Work out which group to put these bundles in */
344 boost::shared_ptr<PortGroup> g;
345 if (_type == DataType::AUDIO) {
347 if (boost::dynamic_pointer_cast<AudioTrack> (*i)) {
348 g = track;
349 } else if (!boost::dynamic_pointer_cast<MidiTrack>(*i)) {
350 g = bus;
354 } else if (_type == DataType::MIDI) {
356 if (boost::dynamic_pointer_cast<MidiTrack> (*i)) {
357 g = track;
360 /* No MIDI busses yet */
363 if (g) {
365 TimeAxisView* tv = PublicEditor::instance().axis_view_from_route (*i);
366 for (list<boost::shared_ptr<Bundle> >::iterator i = route_bundles.begin(); i != route_bundles.end(); ++i) {
367 if (tv) {
368 g->add_bundle (*i, io, tv->color ());
369 } else {
370 g->add_bundle (*i, io);
376 /* Bundles owned by the session; add user bundles first, then normal ones, so
377 that UserBundles that offer the same ports as a normal bundle get priority
380 boost::shared_ptr<BundleList> b = session->bundles ();
382 for (BundleList::iterator i = b->begin(); i != b->end(); ++i) {
383 if (boost::dynamic_pointer_cast<UserBundle> (*i) && (*i)->ports_are_inputs() == inputs && (*i)->type() == _type) {
384 system->add_bundle (*i, allow_dups);
388 for (BundleList::iterator i = b->begin(); i != b->end(); ++i) {
389 if (boost::dynamic_pointer_cast<UserBundle> (*i) == 0 && (*i)->ports_are_inputs() == inputs && (*i)->type() == _type) {
390 system->add_bundle (*i, allow_dups);
394 /* Ardour stuff */
396 if (!inputs && _type == DataType::AUDIO) {
397 ardour->add_bundle (session->the_auditioner()->output()->bundle());
398 ardour->add_bundle (session->click_io()->bundle());
401 /* Now find all other ports that we haven't thought of yet */
403 std::vector<std::string> extra_system;
404 std::vector<std::string> extra_other;
406 const char **ports = session->engine().get_ports ("", _type.to_jack_type(), inputs ?
407 JackPortIsInput : JackPortIsOutput);
408 if (ports) {
410 int n = 0;
411 string client_matching_string;
413 client_matching_string = session->engine().client_name();
414 client_matching_string += ':';
416 while (ports[n]) {
418 std::string const p = ports[n];
420 if (!system->has_port(p) &&
421 !bus->has_port(p) &&
422 !track->has_port(p) &&
423 !ardour->has_port(p) &&
424 !other->has_port(p)) {
426 /* special hack: ignore MIDI ports labelled Midi-Through. these
427 are basically useless and mess things up for default
428 connections.
431 if (p.find ("MIDI-Through") != string::npos) {
432 ++n;
433 continue;
436 if (port_has_prefix (p, "system:") ||
437 port_has_prefix (p, "alsa_pcm") ||
438 port_has_prefix (p, "ardour:")) {
439 extra_system.push_back (p);
440 } else {
441 extra_other.push_back (p);
445 ++n;
448 free (ports);
451 if (!extra_system.empty()) {
452 boost::shared_ptr<Bundle> b = make_bundle_from_ports (extra_system, inputs);
453 system->add_bundle (b);
456 if (!extra_other.empty()) {
457 other->add_bundle (make_bundle_from_ports (extra_other, inputs));
460 if (!allow_dups) {
461 system->remove_duplicates ();
464 add_group_if_not_empty (other);
465 add_group_if_not_empty (bus);
466 add_group_if_not_empty (track);
467 add_group_if_not_empty (ardour);
468 add_group_if_not_empty (system);
470 emit_changed ();
473 boost::shared_ptr<Bundle>
474 PortGroupList::make_bundle_from_ports (std::vector<std::string> const & p, bool inputs) const
476 boost::shared_ptr<Bundle> b (new Bundle ("", _type, inputs));
478 std::string const pre = common_prefix (p);
479 if (!pre.empty()) {
480 b->set_name (pre.substr (0, pre.length() - 1));
483 for (uint32_t j = 0; j < p.size(); ++j) {
484 b->add_channel (p[j].substr (pre.length()));
485 b->set_port (j, p[j]);
488 return b;
491 bool
492 PortGroupList::port_has_prefix (const std::string& n, const std::string& p) const
494 return n.substr (0, p.length()) == p;
497 std::string
498 PortGroupList::common_prefix_before (std::vector<std::string> const & p, std::string const & s) const
500 /* we must have some strings and the first must contain the separator string */
501 if (p.empty() || p[0].find_first_of (s) == std::string::npos) {
502 return "";
505 /* prefix of the first string */
506 std::string const fp = p[0].substr (0, p[0].find_first_of (s) + 1);
508 /* see if the other strings also start with fp */
509 uint32_t j = 1;
510 while (j < p.size()) {
511 if (p[j].substr (0, fp.length()) != fp) {
512 break;
514 ++j;
517 if (j != p.size()) {
518 return "";
521 return fp;
525 std::string
526 PortGroupList::common_prefix (std::vector<std::string> const & p) const
528 /* common prefix before '/' ? */
529 std::string cp = common_prefix_before (p, "/");
530 if (!cp.empty()) {
531 return cp;
534 cp = common_prefix_before (p, ":");
535 if (!cp.empty()) {
536 return cp;
539 return "";
542 void
543 PortGroupList::clear ()
545 _groups.clear ();
546 _bundle_changed_connections.drop_connections ();
547 emit_changed ();
551 PortGroup::BundleList const &
552 PortGroupList::bundles () const
554 _bundles.clear ();
556 for (PortGroupList::List::const_iterator i = begin (); i != end (); ++i) {
557 std::copy ((*i)->bundles().begin(), (*i)->bundles().end(), std::back_inserter (_bundles));
560 return _bundles;
563 uint32_t
564 PortGroupList::total_channels () const
566 uint32_t n = 0;
568 for (PortGroupList::List::const_iterator i = begin(); i != end(); ++i) {
569 n += (*i)->total_channels ();
572 return n;
575 void
576 PortGroupList::add_group_if_not_empty (boost::shared_ptr<PortGroup> g)
578 if (!g->bundles().empty ()) {
579 add_group (g);
583 void
584 PortGroupList::add_group (boost::shared_ptr<PortGroup> g)
586 _groups.push_back (g);
588 g->Changed.connect (_changed_connections, invalidator (*this), boost::bind (&PortGroupList::emit_changed, this), gui_context());
589 g->BundleChanged.connect (_bundle_changed_connections, invalidator (*this), ui_bind (&PortGroupList::emit_bundle_changed, this, _1), gui_context());
591 emit_changed ();
594 void
595 PortGroupList::remove_bundle (boost::shared_ptr<Bundle> b)
597 for (List::iterator i = _groups.begin(); i != _groups.end(); ++i) {
598 (*i)->remove_bundle (b);
601 emit_changed ();
604 void
605 PortGroupList::emit_changed ()
607 if (_signals_suspended) {
608 _pending_change = true;
609 } else {
610 Changed ();
614 void
615 PortGroupList::emit_bundle_changed (Bundle::Change c)
617 if (_signals_suspended) {
618 _pending_bundle_change = c;
619 } else {
620 BundleChanged (c);
623 void
624 PortGroupList::suspend_signals ()
626 _signals_suspended = true;
629 void
630 PortGroupList::resume_signals ()
632 if (_pending_change) {
633 Changed ();
634 _pending_change = false;
637 if (_pending_bundle_change != 0) {
638 BundleChanged (_pending_bundle_change);
639 _pending_bundle_change = (ARDOUR::Bundle::Change) 0;
642 _signals_suspended = false;
645 boost::shared_ptr<IO>
646 PortGroupList::io_from_bundle (boost::shared_ptr<ARDOUR::Bundle> b) const
648 List::const_iterator i = _groups.begin ();
649 while (i != _groups.end()) {
650 boost::shared_ptr<IO> io = (*i)->io_from_bundle (b);
651 if (io) {
652 return io;
654 ++i;
657 return boost::shared_ptr<IO> ();
660 bool
661 PortGroupList::empty () const
663 List::const_iterator i = _groups.begin ();
664 while (i != _groups.end() && (*i)->total_channels() == 0) {
665 ++i;
668 return (i == _groups.end());