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.
21 #include <boost/shared_ptr.hpp>
22 #include <boost/algorithm/string.hpp>
24 #include "midi++/mmc.h"
26 #include "ardour/audioengine.h"
27 #include "ardour/auditioner.h"
28 #include "ardour/bundle.h"
29 #include "ardour/control_protocol_manager.h"
30 #include "ardour/io_processor.h"
31 #include "ardour/midi_port.h"
32 #include "ardour/midiport_manager.h"
33 #include "ardour/plugin_insert.h"
34 #include "ardour/port.h"
35 #include "ardour/profile.h"
36 #include "ardour/session.h"
37 #include "ardour/sidechain.h"
38 #include "ardour/user_bundle.h"
40 #include "control_protocol/control_protocol.h"
42 #include "gui_thread.h"
43 #include "port_group.h"
44 #include "port_matrix.h"
45 #include "time_axis_view.h"
46 #include "public_editor.h"
52 using namespace ARDOUR;
54 /** PortGroup constructor.
57 PortGroup::PortGroup (std::string const & n)
63 PortGroup::~PortGroup()
65 for (BundleList::iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
71 /** Add a bundle to a group.
73 * @param allow_dups true to allow the group to contain more than one bundle with the same port, otherwise false.
76 PortGroup::add_bundle (boost::shared_ptr<Bundle> b, bool allow_dups)
78 add_bundle_internal (b, boost::shared_ptr<IO> (), false, Gdk::Color (), allow_dups);
81 /** Add a bundle to a group.
83 * @param io IO whose ports are in the bundle.
86 PortGroup::add_bundle (boost::shared_ptr<Bundle> b, boost::shared_ptr<IO> io)
88 add_bundle_internal (b, io, false, Gdk::Color (), false);
91 /** Add a bundle to a group.
93 * @param c Colour to represent the bundle with.
96 PortGroup::add_bundle (boost::shared_ptr<Bundle> b, boost::shared_ptr<IO> io, Gdk::Color c)
98 add_bundle_internal (b, io, true, c, false);
101 PortGroup::BundleRecord::BundleRecord (boost::shared_ptr<ARDOUR::Bundle> b, boost::shared_ptr<ARDOUR::IO> iop, Gdk::Color c, bool has_c)
110 PortGroup::add_bundle_internal (boost::shared_ptr<Bundle> b, boost::shared_ptr<IO> io, bool has_colour, Gdk::Color colour, bool allow_dups)
116 /* don't add this bundle if we already have one with the same ports */
118 BundleList::iterator i = _bundles.begin ();
119 while (i != _bundles.end() && b->has_same_ports ((*i)->bundle) == false) {
123 if (i != _bundles.end ()) {
128 BundleRecord* br = new BundleRecord (b, io, colour, has_colour);
129 b->Changed.connect (br->changed_connection, invalidator (*this), boost::bind (&PortGroup::bundle_changed, this, _1), gui_context());
130 _bundles.push_back (br);
136 PortGroup::remove_bundle (boost::shared_ptr<Bundle> b)
140 BundleList::iterator i = _bundles.begin ();
141 while (i != _bundles.end() && (*i)->bundle != b) {
145 if (i == _bundles.end()) {
156 PortGroup::bundle_changed (Bundle::Change c)
165 for (BundleList::iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
174 PortGroup::has_port (std::string const& p) const
176 for (BundleList::const_iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
177 if ((*i)->bundle->offers_port_alone (p)) {
185 boost::shared_ptr<Bundle>
186 PortGroup::only_bundle ()
188 assert (_bundles.size() == 1);
189 return _bundles.front()->bundle;
194 PortGroup::total_channels () const
197 for (BundleList::const_iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
198 n += (*i)->bundle->nchannels ();
204 boost::shared_ptr<IO>
205 PortGroup::io_from_bundle (boost::shared_ptr<ARDOUR::Bundle> b) const
207 BundleList::const_iterator i = _bundles.begin ();
208 while (i != _bundles.end() && (*i)->bundle != b) {
212 if (i == _bundles.end()) {
213 return boost::shared_ptr<IO> ();
216 boost::shared_ptr<IO> io ((*i)->io.lock ());
220 /** Remove bundles whose channels are already represented by other, larger bundles */
222 PortGroup::remove_duplicates ()
224 BundleList::iterator i = _bundles.begin();
225 while (i != _bundles.end()) {
227 BundleList::iterator tmp = i;
232 for (BundleList::iterator j = _bundles.begin(); j != _bundles.end(); ++j) {
234 if ((*j)->bundle->nchannels() > (*i)->bundle->nchannels()) {
235 /* this bundle is larger */
238 while (k < (*i)->bundle->nchannels().n_total()) {
239 /* see if this channel on *i has an equivalent on *j */
241 while (l < (*j)->bundle->nchannels().n_total() && (*i)->bundle->channel_ports (k) != (*j)->bundle->channel_ports (l)) {
245 if (l == (*j)->bundle->nchannels().n_total()) {
253 if (k == (*i)->bundle->nchannels().n_total()) {
254 /* all channels on *i are represented by the larger bundle *j, so remove *i */
270 /** PortGroupList constructor.
272 PortGroupList::PortGroupList ()
273 : _signals_suspended (false), _pending_change (false), _pending_bundle_change ((Bundle::Change) 0)
278 PortGroupList::~PortGroupList()
280 /* XXX need to clean up bundles, but ownership shared with PortGroups */
284 PortGroupList::maybe_add_processor_to_list (
285 boost::weak_ptr<Processor> wp, list<boost::shared_ptr<IO> >* route_ios, bool inputs, set<boost::shared_ptr<IO> >& used_io
288 boost::shared_ptr<Processor> p (wp.lock());
294 boost::shared_ptr<IOProcessor> iop = boost::dynamic_pointer_cast<IOProcessor> (p);
298 boost::shared_ptr<IO> io = inputs ? iop->input() : iop->output();
300 if (io && used_io.find (io) == used_io.end()) {
301 route_ios->push_back (io);
308 RouteIOs (boost::shared_ptr<Route> r, boost::shared_ptr<IO> i) {
313 boost::shared_ptr<Route> route;
314 /* it's ok to use a shared_ptr here as RouteIOs structs are only used during ::gather () */
315 std::list<boost::shared_ptr<IO> > ios;
318 class RouteIOsComparator {
320 bool operator() (RouteIOs const & a, RouteIOs const & b) {
321 return a.route->presentation_info ().order() < b.route->presentation_info().order();
325 /** Gather ports from around the system and put them in this PortGroupList.
326 * @param type Type of ports to collect, or NIL for all types.
327 * @param use_session_bundles true to use the session's non-user bundles. Doing this will mean that
328 * hardware ports will be gathered into stereo pairs, as the session sets up bundles for these pairs.
329 * Not using the session bundles will mean that all hardware IO will be presented separately.
332 PortGroupList::gather (ARDOUR::Session* session, ARDOUR::DataType type, bool inputs, bool allow_dups, bool use_session_bundles)
340 boost::shared_ptr<PortGroup> bus (new PortGroup (string_compose (_("%1 Busses"), PROGRAM_NAME)));
341 boost::shared_ptr<PortGroup> track (new PortGroup (string_compose (_("%1 Tracks"), PROGRAM_NAME)));
342 boost::shared_ptr<PortGroup> sidechain (new PortGroup (string_compose (_("%1 Sidechains"), PROGRAM_NAME)));
343 boost::shared_ptr<PortGroup> system (new PortGroup (_("Hardware")));
344 boost::shared_ptr<PortGroup> program (new PortGroup (string_compose (_("%1 Misc"), PROGRAM_NAME)));
345 boost::shared_ptr<PortGroup> other (new PortGroup (_("Other")));
347 /* Find the IOs which have bundles for routes and their processors. We store
348 these IOs in a RouteIOs class so that we can then sort the results by route
352 boost::shared_ptr<RouteList> routes = session->get_routes ();
353 list<RouteIOs> route_ios;
355 for (RouteList::const_iterator i = routes->begin(); i != routes->end(); ++i) {
357 /* we never show the monitor bus inputs */
359 if (inputs && (*i)->is_monitor()) {
363 /* keep track of IOs that we have taken bundles from,
364 so that we can avoid taking the same IO from both
365 Route::output() and the main_outs Delivery
368 set<boost::shared_ptr<IO> > used_io;
369 boost::shared_ptr<IO> io = inputs ? (*i)->input() : (*i)->output();
372 RouteIOs rb (*i, io);
373 (*i)->foreach_processor (boost::bind (&PortGroupList::maybe_add_processor_to_list, this, _1, &rb.ios, inputs, used_io));
375 route_ios.push_back (rb);
378 /* Sort RouteIOs by the routes' editor order keys */
379 route_ios.sort (RouteIOsComparator ());
381 /* Now put the bundles that belong to these sorted RouteIOs into the PortGroup. */
383 for (list<RouteIOs>::iterator i = route_ios.begin(); i != route_ios.end(); ++i) {
384 TimeAxisView* tv = PublicEditor::instance().time_axis_view_from_stripable (i->route);
386 /* Work out which group to put these IOs' bundles in */
387 boost::shared_ptr<PortGroup> g;
388 if (boost::dynamic_pointer_cast<Track> (i->route)) {
394 for (list<boost::shared_ptr<IO> >::iterator j = i->ios.begin(); j != i->ios.end(); ++j) {
395 /* Only add the bundle if there is at least one port
396 * with a type that's been asked for */
397 if (type == DataType::NIL || (*j)->bundle()->nchannels().n(type) > 0) {
399 g->add_bundle ((*j)->bundle(), *j, tv->color ());
401 g->add_bundle ((*j)->bundle(), *j);
406 /* When on input side, let's look for sidechains in the route's plugins
407 to display them right next to their route */
408 for (uint32_t n = 0; inputs; ++n) {
409 boost::shared_ptr<Processor> p = (i->route)->nth_plugin (n);
413 boost::shared_ptr<SideChain> sc = boost::static_pointer_cast<PluginInsert> (p)->sidechain ();
416 boost::shared_ptr<IO> io = sc->input();
418 sidechain->add_bundle (io->bundle(), io, tv->color ());
420 sidechain->add_bundle (io->bundle(), io);
426 /* Bundles owned by the session; add user bundles first, then normal ones, so
427 that UserBundles that offer the same ports as a normal bundle get priority
430 boost::shared_ptr<BundleList> b = session->bundles ();
432 for (BundleList::iterator i = b->begin(); i != b->end(); ++i) {
433 if (boost::dynamic_pointer_cast<UserBundle> (*i) && (*i)->ports_are_inputs() == inputs) {
434 system->add_bundle (*i, allow_dups);
438 /* Only look for non-user bundles if instructed to do so */
439 if (use_session_bundles) {
440 for (BundleList::iterator i = b->begin(); i != b->end(); ++i) {
441 if (boost::dynamic_pointer_cast<UserBundle> (*i) == 0 && (*i)->ports_are_inputs() == inputs) {
442 system->add_bundle (*i, allow_dups);
449 if (type == DataType::AUDIO || type == DataType::NIL) {
451 program->add_bundle (session->the_auditioner()->output()->bundle());
452 program->add_bundle (session->click_io()->bundle());
453 /* Note: the LTC ports do not have the usual ":audio_out 1" postfix, so
454 * program->add_bundle (session->ltc_output_io()->bundle());
457 boost::shared_ptr<Bundle> ltc (new Bundle (_("LTC Out"), inputs));
458 ltc->add_channel (_("LTC Out"), DataType::AUDIO, session->engine().make_port_name_non_relative (session->ltc_output_port()->name()));
459 program->add_bundle (ltc);
461 // XXX TRANSPORTMASTERS
462 //boost::shared_ptr<Bundle> ltc (new Bundle (_("LTC In"), inputs));
463 // ltc->add_channel (_("LTC In"), DataType::AUDIO, session->engine().make_port_name_non_relative (session->ltc_input_port()->name()));
464 // program->add_bundle (ltc);
468 /* our control surfaces */
470 /* XXX assume for now that all control protocols with ports use
471 * MIDI. If anyone created a control protocol that used audio ports,
475 if ((type == DataType::MIDI || type == DataType::NIL)) {
476 ControlProtocolManager& m = ControlProtocolManager::instance ();
477 for (list<ControlProtocolInfo*>::iterator i = m.control_protocol_info.begin(); i != m.control_protocol_info.end(); ++i) {
478 if ((*i)->protocol) {
479 list<boost::shared_ptr<Bundle> > b = (*i)->protocol->bundles ();
480 for (list<boost::shared_ptr<Bundle> >::iterator j = b.begin(); j != b.end(); ++j) {
481 if ((*j)->ports_are_inputs() == inputs) {
482 program->add_bundle (*j);
491 if ((type == DataType::MIDI || type == DataType::NIL)) {
492 boost::shared_ptr<Bundle> sync (new Bundle (_("Sync"), inputs));
493 AudioEngine* ae = AudioEngine::instance();
496 // XXX TRANSPORTMASTER
497 // sync->add_channel (
498 // _("MTC in"), DataType::MIDI, ae->make_port_name_non_relative (session->mtc_input_port()->name())
500 // sync->add_channel (
501 // _("MIDI clock in"), DataType::MIDI, ae->make_port_name_non_relative (session->midi_clock_input_port()->name())
504 _("MMC in"), DataType::MIDI, ae->make_port_name_non_relative (session->mmc_input_port()->name())
508 _("MTC out"), DataType::MIDI, ae->make_port_name_non_relative (session->mtc_output_port()->name())
511 _("MIDI clock out"), DataType::MIDI, ae->make_port_name_non_relative (session->midi_clock_output_port()->name())
514 _("MMC out"), DataType::MIDI, ae->make_port_name_non_relative (session->mmc_output_port()->name())
518 program->add_bundle (sync);
521 /* Now find all other ports that we haven't thought of yet */
523 std::vector<std::string> extra_system[DataType::num_types];
524 std::vector<std::string> extra_program[DataType::num_types];
525 std::vector<std::string> extra_other[DataType::num_types];
527 string lpn (PROGRAM_NAME);
528 boost::to_lower (lpn);
532 vector<string> ports;
533 if (type == DataType::NIL) {
535 AudioEngine::instance()->get_ports ("", DataType::AUDIO, inputs ? IsInput : IsOutput, ports);
536 AudioEngine::instance()->get_ports ("", DataType::MIDI, inputs ? IsInput : IsOutput, p1);
537 for (vector<string>::const_iterator s = p1.begin(); s != p1.end(); ++s) {
538 ports.push_back (*s);
541 AudioEngine::instance()->get_ports ("", type, inputs ? IsInput : IsOutput, ports);
544 if (ports.size () > 0) {
546 for (vector<string>::const_iterator s = ports.begin(); s != ports.end(); ) {
548 std::string const p = *s;
550 if (!system->has_port(p) &&
552 !track->has_port(p) &&
553 !sidechain->has_port(p) &&
554 !program->has_port(p) &&
555 !other->has_port(p)) {
557 /* special hack: ignore MIDI ports labelled Midi-Through. these
558 are basically useless and mess things up for default
562 if (p.find ("Midi-Through") != string::npos || p.find ("Midi Through") != string::npos) {
567 /* special hack: ignore our monitor inputs (which show up here because
568 we excluded them earlier.
572 string monitor = _("Monitor");
574 boost::to_lower (lp);
575 boost::to_lower (monitor);
577 if ((lp.find (monitor) != string::npos) &&
578 (lp.find (lpn) != string::npos)) {
583 /* can't use the audio engine for this as we
584 * are looking at ports not owned by the
585 * application, and the audio engine/port
586 * manager doesn't seem them.
589 PortEngine::PortHandle ph = AudioEngine::instance()->port_engine().get_port_by_name (p);
595 DataType t (AudioEngine::instance()->port_engine().port_data_type (ph));
597 if (t != DataType::NIL) {
599 PortFlags flags (AudioEngine::instance()->port_engine().get_port_flags (ph));
601 if (port_has_prefix (p, lpnc)) {
603 /* we own this port (named after the program) */
605 /* Hide scene ports from non-Tracks Live builds */
606 if (!ARDOUR::Profile->get_trx()) {
607 if (p.find (_("Scene ")) != string::npos) {
613 extra_program[t].push_back (p);
615 } else if (flags & IsPhysical) {
617 extra_system[t].push_back (p);
620 extra_other[t].push_back (p);
629 for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
630 if (!extra_system[*i].empty()) {
631 boost::shared_ptr<Bundle> b = make_bundle_from_ports (extra_system[*i], *i, inputs);
632 system->add_bundle (b);
636 for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
637 if (!extra_program[*i].empty()) {
638 /* used program name as bundle name */
639 boost::shared_ptr<Bundle> b = make_bundle_from_ports (extra_program[*i], *i, inputs, lpn);
640 program->add_bundle (b);
644 for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
645 if (extra_other[*i].empty()) continue;
647 std::vector<std::string> nb;
648 for (uint32_t j = 0; j < extra_other[*i].size(); ++j) {
649 std::string nn = extra_other[*i][j];
650 std::string pf = nn.substr (0, nn.find_first_of (":") + 1);
651 if (pf != cp && !nb.empty()) {
652 boost::shared_ptr<Bundle> b = make_bundle_from_ports (nb, *i, inputs);
653 other->add_bundle (b);
657 nb.push_back(extra_other[*i][j]);
660 boost::shared_ptr<Bundle> b = make_bundle_from_ports (nb, *i, inputs);
661 other->add_bundle (b);
666 system->remove_duplicates ();
669 add_group_if_not_empty (other);
670 add_group_if_not_empty (bus);
671 add_group_if_not_empty (track);
672 add_group_if_not_empty (sidechain);
673 add_group_if_not_empty (program);
674 add_group_if_not_empty (system);
679 boost::shared_ptr<Bundle>
680 PortGroupList::make_bundle_from_ports (std::vector<std::string> const & p, ARDOUR::DataType type, bool inputs, std::string const& bundle_name) const
682 boost::shared_ptr<Bundle> b (new Bundle ("", inputs));
683 std::string const pre = common_prefix (p);
685 if (!bundle_name.empty()) {
686 b->set_name (bundle_name);
689 b->set_name (pre.substr (0, pre.length() - 1));
693 for (uint32_t j = 0; j < p.size(); ++j) {
694 std::string n = p[j].substr (pre.length());
695 std::string pn = AudioEngine::instance()->get_pretty_name_by_name (p[j]);
699 b->add_channel (n, type);
700 b->set_port (j, p[j]);
707 PortGroupList::port_has_prefix (const std::string& n, const std::string& p) const
709 return n.substr (0, p.length()) == p;
713 PortGroupList::common_prefix_before (std::vector<std::string> const & p, std::string const & s) const
715 /* we must have some strings and the first must contain the separator string */
716 if (p.empty() || p[0].find_first_of (s) == std::string::npos) {
720 /* prefix of the first string */
721 std::string const fp = p[0].substr (0, p[0].find_first_of (s) + 1);
723 /* see if the other strings also start with fp */
725 while (j < p.size()) {
726 if (p[j].substr (0, fp.length()) != fp) {
741 PortGroupList::common_prefix (std::vector<std::string> const & p) const
743 /* common prefix before '/' ? */
744 std::string cp = common_prefix_before (p, "/");
749 cp = common_prefix_before (p, ":");
758 PortGroupList::clear ()
761 _bundle_changed_connections.drop_connections ();
766 PortGroup::BundleList const &
767 PortGroupList::bundles () const
771 for (PortGroupList::List::const_iterator i = begin (); i != end (); ++i) {
772 std::copy ((*i)->bundles().begin(), (*i)->bundles().end(), std::back_inserter (_bundles));
779 PortGroupList::total_channels () const
783 for (PortGroupList::List::const_iterator i = begin(); i != end(); ++i) {
784 n += (*i)->total_channels ();
791 PortGroupList::add_group_if_not_empty (boost::shared_ptr<PortGroup> g)
793 if (!g->bundles().empty ()) {
799 PortGroupList::add_group (boost::shared_ptr<PortGroup> g)
801 _groups.push_back (g);
803 g->Changed.connect (_changed_connections, invalidator (*this), boost::bind (&PortGroupList::emit_changed, this), gui_context());
804 g->BundleChanged.connect (_bundle_changed_connections, invalidator (*this), boost::bind (&PortGroupList::emit_bundle_changed, this, _1), gui_context());
810 PortGroupList::remove_bundle (boost::shared_ptr<Bundle> b)
812 for (List::iterator i = _groups.begin(); i != _groups.end(); ++i) {
813 (*i)->remove_bundle (b);
820 PortGroupList::emit_changed ()
822 if (_signals_suspended) {
823 _pending_change = true;
830 PortGroupList::emit_bundle_changed (Bundle::Change c)
832 if (_signals_suspended) {
833 _pending_bundle_change = c;
839 PortGroupList::suspend_signals ()
841 _signals_suspended = true;
845 PortGroupList::resume_signals ()
847 if (_pending_change) {
849 _pending_change = false;
852 if (_pending_bundle_change != 0) {
853 BundleChanged (_pending_bundle_change);
854 _pending_bundle_change = (ARDOUR::Bundle::Change) 0;
857 _signals_suspended = false;
860 boost::shared_ptr<IO>
861 PortGroupList::io_from_bundle (boost::shared_ptr<ARDOUR::Bundle> b) const
863 List::const_iterator i = _groups.begin ();
864 while (i != _groups.end()) {
865 boost::shared_ptr<IO> io = (*i)->io_from_bundle (b);
872 return boost::shared_ptr<IO> ();
876 PortGroupList::empty () const
878 return _groups.empty ();