2 * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
3 * Copyright (C) 2009-2012 David Robillard <d@drobilla.net>
4 * Copyright (C) 2009-2018 Paul Davis <paul@linuxaudiosystems.com>
5 * Copyright (C) 2012-2016 Robin Gareus <robin@gareus.org>
6 * Copyright (C) 2016 Julien "_FrnchFrgg_" RIVAUD <frnchfrgg@free.fr>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <boost/shared_ptr.hpp>
25 #include <boost/algorithm/string.hpp>
27 #include "midi++/mmc.h"
29 #include "ardour/audioengine.h"
30 #include "ardour/auditioner.h"
31 #include "ardour/bundle.h"
32 #include "ardour/control_protocol_manager.h"
33 #include "ardour/io_processor.h"
34 #include "ardour/midi_port.h"
35 #include "ardour/midiport_manager.h"
36 #include "ardour/plugin_insert.h"
37 #include "ardour/port.h"
38 #include "ardour/profile.h"
39 #include "ardour/session.h"
40 #include "ardour/sidechain.h"
41 #include "ardour/user_bundle.h"
43 #include "control_protocol/control_protocol.h"
45 #include "gui_thread.h"
46 #include "port_group.h"
47 #include "port_matrix.h"
48 #include "time_axis_view.h"
49 #include "public_editor.h"
55 using namespace ARDOUR;
57 /** PortGroup constructor.
60 PortGroup::PortGroup (std::string const & n)
66 PortGroup::~PortGroup()
68 for (BundleList::iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
74 /** Add a bundle to a group.
76 * @param allow_dups true to allow the group to contain more than one bundle with the same port, otherwise false.
79 PortGroup::add_bundle (boost::shared_ptr<Bundle> b, bool allow_dups)
81 add_bundle_internal (b, boost::shared_ptr<IO> (), false, Gdk::Color (), allow_dups);
84 /** Add a bundle to a group.
86 * @param io IO whose ports are in the bundle.
89 PortGroup::add_bundle (boost::shared_ptr<Bundle> b, boost::shared_ptr<IO> io)
91 add_bundle_internal (b, io, false, Gdk::Color (), false);
94 /** Add a bundle to a group.
96 * @param c Colour to represent the bundle with.
99 PortGroup::add_bundle (boost::shared_ptr<Bundle> b, boost::shared_ptr<IO> io, Gdk::Color c)
101 add_bundle_internal (b, io, true, c, false);
104 PortGroup::BundleRecord::BundleRecord (boost::shared_ptr<ARDOUR::Bundle> b, boost::shared_ptr<ARDOUR::IO> iop, Gdk::Color c, bool has_c)
113 PortGroup::add_bundle_internal (boost::shared_ptr<Bundle> b, boost::shared_ptr<IO> io, bool has_colour, Gdk::Color colour, bool allow_dups)
119 /* don't add this bundle if we already have one with the same ports */
121 BundleList::iterator i = _bundles.begin ();
122 while (i != _bundles.end() && b->has_same_ports ((*i)->bundle) == false) {
126 if (i != _bundles.end ()) {
131 BundleRecord* br = new BundleRecord (b, io, colour, has_colour);
132 b->Changed.connect (br->changed_connection, invalidator (*this), boost::bind (&PortGroup::bundle_changed, this, _1), gui_context());
133 _bundles.push_back (br);
139 PortGroup::remove_bundle (boost::shared_ptr<Bundle> b)
143 BundleList::iterator i = _bundles.begin ();
144 while (i != _bundles.end() && (*i)->bundle != b) {
148 if (i == _bundles.end()) {
159 PortGroup::bundle_changed (Bundle::Change c)
168 for (BundleList::iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
177 PortGroup::has_port (std::string const& p) const
179 for (BundleList::const_iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
180 if ((*i)->bundle->offers_port_alone (p)) {
188 boost::shared_ptr<Bundle>
189 PortGroup::only_bundle ()
191 assert (_bundles.size() == 1);
192 return _bundles.front()->bundle;
197 PortGroup::total_channels () const
200 for (BundleList::const_iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
201 n += (*i)->bundle->nchannels ();
207 boost::shared_ptr<IO>
208 PortGroup::io_from_bundle (boost::shared_ptr<ARDOUR::Bundle> b) const
210 BundleList::const_iterator i = _bundles.begin ();
211 while (i != _bundles.end() && (*i)->bundle != b) {
215 if (i == _bundles.end()) {
216 return boost::shared_ptr<IO> ();
219 boost::shared_ptr<IO> io ((*i)->io.lock ());
223 /** Remove bundles whose channels are already represented by other, larger bundles */
225 PortGroup::remove_duplicates ()
227 BundleList::iterator i = _bundles.begin();
228 while (i != _bundles.end()) {
230 BundleList::iterator tmp = i;
235 for (BundleList::iterator j = _bundles.begin(); j != _bundles.end(); ++j) {
237 if ((*j)->bundle->nchannels() > (*i)->bundle->nchannels()) {
238 /* this bundle is larger */
241 while (k < (*i)->bundle->nchannels().n_total()) {
242 /* see if this channel on *i has an equivalent on *j */
244 while (l < (*j)->bundle->nchannels().n_total() && (*i)->bundle->channel_ports (k) != (*j)->bundle->channel_ports (l)) {
248 if (l == (*j)->bundle->nchannels().n_total()) {
256 if (k == (*i)->bundle->nchannels().n_total()) {
257 /* all channels on *i are represented by the larger bundle *j, so remove *i */
273 /** PortGroupList constructor.
275 PortGroupList::PortGroupList ()
276 : _signals_suspended (false), _pending_change (false), _pending_bundle_change ((Bundle::Change) 0)
281 PortGroupList::~PortGroupList()
283 /* XXX need to clean up bundles, but ownership shared with PortGroups */
287 PortGroupList::maybe_add_processor_to_list (
288 boost::weak_ptr<Processor> wp, list<boost::shared_ptr<IO> >* route_ios, bool inputs, set<boost::shared_ptr<IO> >& used_io
291 boost::shared_ptr<Processor> p (wp.lock());
297 boost::shared_ptr<IOProcessor> iop = boost::dynamic_pointer_cast<IOProcessor> (p);
301 boost::shared_ptr<IO> io = inputs ? iop->input() : iop->output();
303 if (io && used_io.find (io) == used_io.end()) {
304 route_ios->push_back (io);
311 RouteIOs (boost::shared_ptr<Route> r, boost::shared_ptr<IO> i) {
316 boost::shared_ptr<Route> route;
317 /* it's ok to use a shared_ptr here as RouteIOs structs are only used during ::gather () */
318 std::list<boost::shared_ptr<IO> > ios;
321 class RouteIOsComparator {
323 bool operator() (RouteIOs const & a, RouteIOs const & b) {
324 return a.route->presentation_info ().order() < b.route->presentation_info().order();
328 /** Gather ports from around the system and put them in this PortGroupList.
329 * @param type Type of ports to collect, or NIL for all types.
330 * @param use_session_bundles true to use the session's non-user bundles. Doing this will mean that
331 * hardware ports will be gathered into stereo pairs, as the session sets up bundles for these pairs.
332 * Not using the session bundles will mean that all hardware IO will be presented separately.
335 PortGroupList::gather (ARDOUR::Session* session, ARDOUR::DataType type, bool inputs, bool allow_dups, bool use_session_bundles)
343 boost::shared_ptr<PortGroup> bus (new PortGroup (string_compose (_("%1 Busses"), PROGRAM_NAME)));
344 boost::shared_ptr<PortGroup> track (new PortGroup (string_compose (_("%1 Tracks"), PROGRAM_NAME)));
345 boost::shared_ptr<PortGroup> sidechain (new PortGroup (string_compose (_("%1 Sidechains"), PROGRAM_NAME)));
346 boost::shared_ptr<PortGroup> system (new PortGroup (_("Hardware")));
347 boost::shared_ptr<PortGroup> program (new PortGroup (string_compose (_("%1 Misc"), PROGRAM_NAME)));
348 boost::shared_ptr<PortGroup> other (new PortGroup (_("Other")));
350 /* Find the IOs which have bundles for routes and their processors. We store
351 these IOs in a RouteIOs class so that we can then sort the results by route
355 boost::shared_ptr<RouteList> routes = session->get_routes ();
356 list<RouteIOs> route_ios;
358 for (RouteList::const_iterator i = routes->begin(); i != routes->end(); ++i) {
360 /* we never show the monitor bus inputs */
362 if (inputs && (*i)->is_monitor()) {
366 /* keep track of IOs that we have taken bundles from,
367 so that we can avoid taking the same IO from both
368 Route::output() and the main_outs Delivery
371 set<boost::shared_ptr<IO> > used_io;
372 boost::shared_ptr<IO> io = inputs ? (*i)->input() : (*i)->output();
375 RouteIOs rb (*i, io);
376 (*i)->foreach_processor (boost::bind (&PortGroupList::maybe_add_processor_to_list, this, _1, &rb.ios, inputs, used_io));
378 route_ios.push_back (rb);
381 /* Sort RouteIOs by the routes' editor order keys */
382 route_ios.sort (RouteIOsComparator ());
384 /* Now put the bundles that belong to these sorted RouteIOs into the PortGroup. */
386 for (list<RouteIOs>::iterator i = route_ios.begin(); i != route_ios.end(); ++i) {
387 TimeAxisView* tv = PublicEditor::instance().time_axis_view_from_stripable (i->route);
389 /* Work out which group to put these IOs' bundles in */
390 boost::shared_ptr<PortGroup> g;
391 if (boost::dynamic_pointer_cast<Track> (i->route)) {
397 for (list<boost::shared_ptr<IO> >::iterator j = i->ios.begin(); j != i->ios.end(); ++j) {
398 /* Only add the bundle if there is at least one port
399 * with a type that's been asked for */
400 if (type == DataType::NIL || (*j)->bundle()->nchannels().n(type) > 0) {
402 g->add_bundle ((*j)->bundle(), *j, tv->color ());
404 g->add_bundle ((*j)->bundle(), *j);
409 /* When on input side, let's look for sidechains in the route's plugins
410 to display them right next to their route */
411 for (uint32_t n = 0; inputs; ++n) {
412 boost::shared_ptr<Processor> p = (i->route)->nth_plugin (n);
416 boost::shared_ptr<SideChain> sc = boost::static_pointer_cast<PluginInsert> (p)->sidechain ();
419 boost::shared_ptr<IO> io = sc->input();
421 sidechain->add_bundle (io->bundle(), io, tv->color ());
423 sidechain->add_bundle (io->bundle(), io);
429 /* Bundles owned by the session; add user bundles first, then normal ones, so
430 that UserBundles that offer the same ports as a normal bundle get priority
433 boost::shared_ptr<BundleList> b = session->bundles ();
435 for (BundleList::iterator i = b->begin(); i != b->end(); ++i) {
436 if (boost::dynamic_pointer_cast<UserBundle> (*i) && (*i)->ports_are_inputs() == inputs) {
437 system->add_bundle (*i, allow_dups);
441 /* Only look for non-user bundles if instructed to do so */
442 if (use_session_bundles) {
443 for (BundleList::iterator i = b->begin(); i != b->end(); ++i) {
444 if (boost::dynamic_pointer_cast<UserBundle> (*i) == 0 && (*i)->ports_are_inputs() == inputs) {
445 system->add_bundle (*i, allow_dups);
452 if (type == DataType::AUDIO || type == DataType::NIL) {
454 program->add_bundle (session->the_auditioner()->output()->bundle());
455 program->add_bundle (session->click_io()->bundle());
456 /* Note: the LTC ports do not have the usual ":audio_out 1" postfix, so
457 * program->add_bundle (session->ltc_output_io()->bundle());
460 boost::shared_ptr<Bundle> ltc (new Bundle (_("LTC Out"), inputs));
461 ltc->add_channel (_("LTC Out"), DataType::AUDIO, session->engine().make_port_name_non_relative (session->ltc_output_port()->name()));
462 program->add_bundle (ltc);
464 // XXX TRANSPORTMASTERS
465 //boost::shared_ptr<Bundle> ltc (new Bundle (_("LTC In"), inputs));
466 // ltc->add_channel (_("LTC In"), DataType::AUDIO, session->engine().make_port_name_non_relative (session->ltc_input_port()->name()));
467 // program->add_bundle (ltc);
471 /* our control surfaces */
473 /* XXX assume for now that all control protocols with ports use
474 * MIDI. If anyone created a control protocol that used audio ports,
478 if ((type == DataType::MIDI || type == DataType::NIL)) {
479 ControlProtocolManager& m = ControlProtocolManager::instance ();
480 for (list<ControlProtocolInfo*>::iterator i = m.control_protocol_info.begin(); i != m.control_protocol_info.end(); ++i) {
481 if ((*i)->protocol) {
482 list<boost::shared_ptr<Bundle> > b = (*i)->protocol->bundles ();
483 for (list<boost::shared_ptr<Bundle> >::iterator j = b.begin(); j != b.end(); ++j) {
484 if ((*j)->ports_are_inputs() == inputs) {
485 program->add_bundle (*j);
494 if ((type == DataType::MIDI || type == DataType::NIL)) {
495 boost::shared_ptr<Bundle> sync (new Bundle (_("Sync"), inputs));
496 AudioEngine* ae = AudioEngine::instance();
499 // XXX TRANSPORTMASTER
500 // sync->add_channel (
501 // _("MTC in"), DataType::MIDI, ae->make_port_name_non_relative (session->mtc_input_port()->name())
503 // sync->add_channel (
504 // _("MIDI clock in"), DataType::MIDI, ae->make_port_name_non_relative (session->midi_clock_input_port()->name())
507 _("MMC in"), DataType::MIDI, ae->make_port_name_non_relative (session->mmc_input_port()->name())
511 _("MTC out"), DataType::MIDI, ae->make_port_name_non_relative (session->mtc_output_port()->name())
514 _("MIDI clock out"), DataType::MIDI, ae->make_port_name_non_relative (session->midi_clock_output_port()->name())
517 _("MMC out"), DataType::MIDI, ae->make_port_name_non_relative (session->mmc_output_port()->name())
521 program->add_bundle (sync);
524 /* Now find all other ports that we haven't thought of yet */
526 std::vector<std::string> extra_system[DataType::num_types];
527 std::vector<std::string> extra_program[DataType::num_types];
528 std::vector<std::string> extra_other[DataType::num_types];
530 string lpn (PROGRAM_NAME);
531 boost::to_lower (lpn);
535 vector<string> ports;
536 if (type == DataType::NIL) {
538 AudioEngine::instance()->get_ports ("", DataType::AUDIO, inputs ? IsInput : IsOutput, ports);
539 AudioEngine::instance()->get_ports ("", DataType::MIDI, inputs ? IsInput : IsOutput, p1);
540 for (vector<string>::const_iterator s = p1.begin(); s != p1.end(); ++s) {
541 ports.push_back (*s);
544 AudioEngine::instance()->get_ports ("", type, inputs ? IsInput : IsOutput, ports);
547 if (ports.size () > 0) {
549 for (vector<string>::const_iterator s = ports.begin(); s != ports.end(); ) {
551 std::string const p = *s;
553 if (!system->has_port(p) &&
555 !track->has_port(p) &&
556 !sidechain->has_port(p) &&
557 !program->has_port(p) &&
558 !other->has_port(p)) {
560 /* special hack: ignore MIDI ports labelled Midi-Through. these
561 are basically useless and mess things up for default
565 if (p.find ("Midi-Through") != string::npos || p.find ("Midi Through") != string::npos) {
570 /* special hack: ignore our monitor inputs (which show up here because
571 we excluded them earlier.
575 string monitor = _("Monitor");
577 boost::to_lower (lp);
578 boost::to_lower (monitor);
580 if ((lp.find (monitor) != string::npos) &&
581 (lp.find (lpn) != string::npos)) {
586 /* can't use the audio engine for this as we
587 * are looking at ports not owned by the
588 * application, and the audio engine/port
589 * manager doesn't seem them.
592 PortEngine::PortHandle ph = AudioEngine::instance()->port_engine().get_port_by_name (p);
598 DataType t (AudioEngine::instance()->port_engine().port_data_type (ph));
600 if (t != DataType::NIL) {
602 PortFlags flags (AudioEngine::instance()->port_engine().get_port_flags (ph));
604 if (port_has_prefix (p, lpnc)) {
606 /* we own this port (named after the program) */
608 /* Hide scene ports from non-Tracks Live builds */
609 if (!ARDOUR::Profile->get_trx()) {
610 if (p.find (_("Scene ")) != string::npos) {
616 extra_program[t].push_back (p);
618 } else if (flags & IsPhysical) {
620 extra_system[t].push_back (p);
623 extra_other[t].push_back (p);
632 for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
633 if (!extra_system[*i].empty()) {
634 boost::shared_ptr<Bundle> b = make_bundle_from_ports (extra_system[*i], *i, inputs);
635 system->add_bundle (b);
639 for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
640 if (!extra_program[*i].empty()) {
641 /* used program name as bundle name */
642 boost::shared_ptr<Bundle> b = make_bundle_from_ports (extra_program[*i], *i, inputs, lpn);
643 program->add_bundle (b);
647 for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
648 if (extra_other[*i].empty()) continue;
650 std::vector<std::string> nb;
651 for (uint32_t j = 0; j < extra_other[*i].size(); ++j) {
652 std::string nn = extra_other[*i][j];
653 std::string pf = nn.substr (0, nn.find_first_of (":") + 1);
654 if (pf != cp && !nb.empty()) {
655 boost::shared_ptr<Bundle> b = make_bundle_from_ports (nb, *i, inputs);
656 other->add_bundle (b);
660 nb.push_back(extra_other[*i][j]);
663 boost::shared_ptr<Bundle> b = make_bundle_from_ports (nb, *i, inputs);
664 other->add_bundle (b);
669 system->remove_duplicates ();
672 add_group_if_not_empty (other);
673 add_group_if_not_empty (bus);
674 add_group_if_not_empty (track);
675 add_group_if_not_empty (sidechain);
676 add_group_if_not_empty (program);
677 add_group_if_not_empty (system);
682 boost::shared_ptr<Bundle>
683 PortGroupList::make_bundle_from_ports (std::vector<std::string> const & p, ARDOUR::DataType type, bool inputs, std::string const& bundle_name) const
685 boost::shared_ptr<Bundle> b (new Bundle ("", inputs));
686 std::string const pre = common_prefix (p);
688 if (!bundle_name.empty()) {
689 b->set_name (bundle_name);
692 b->set_name (pre.substr (0, pre.length() - 1));
696 for (uint32_t j = 0; j < p.size(); ++j) {
697 std::string n = p[j].substr (pre.length());
698 std::string pn = AudioEngine::instance()->get_pretty_name_by_name (p[j]);
702 b->add_channel (n, type);
703 b->set_port (j, p[j]);
710 PortGroupList::port_has_prefix (const std::string& n, const std::string& p) const
712 return n.substr (0, p.length()) == p;
716 PortGroupList::common_prefix_before (std::vector<std::string> const & p, std::string const & s) const
718 /* we must have some strings and the first must contain the separator string */
719 if (p.empty() || p[0].find_first_of (s) == std::string::npos) {
723 /* prefix of the first string */
724 std::string const fp = p[0].substr (0, p[0].find_first_of (s) + 1);
726 /* see if the other strings also start with fp */
728 while (j < p.size()) {
729 if (p[j].substr (0, fp.length()) != fp) {
744 PortGroupList::common_prefix (std::vector<std::string> const & p) const
746 /* common prefix before '/' ? */
747 std::string cp = common_prefix_before (p, "/");
752 cp = common_prefix_before (p, ":");
761 PortGroupList::clear ()
764 _bundle_changed_connections.drop_connections ();
769 PortGroup::BundleList const &
770 PortGroupList::bundles () const
774 for (PortGroupList::List::const_iterator i = begin (); i != end (); ++i) {
775 std::copy ((*i)->bundles().begin(), (*i)->bundles().end(), std::back_inserter (_bundles));
782 PortGroupList::total_channels () const
786 for (PortGroupList::List::const_iterator i = begin(); i != end(); ++i) {
787 n += (*i)->total_channels ();
794 PortGroupList::add_group_if_not_empty (boost::shared_ptr<PortGroup> g)
796 if (!g->bundles().empty ()) {
802 PortGroupList::add_group (boost::shared_ptr<PortGroup> g)
804 _groups.push_back (g);
806 g->Changed.connect (_changed_connections, invalidator (*this), boost::bind (&PortGroupList::emit_changed, this), gui_context());
807 g->BundleChanged.connect (_bundle_changed_connections, invalidator (*this), boost::bind (&PortGroupList::emit_bundle_changed, this, _1), gui_context());
813 PortGroupList::remove_bundle (boost::shared_ptr<Bundle> b)
815 for (List::iterator i = _groups.begin(); i != _groups.end(); ++i) {
816 (*i)->remove_bundle (b);
823 PortGroupList::emit_changed ()
825 if (_signals_suspended) {
826 _pending_change = true;
833 PortGroupList::emit_bundle_changed (Bundle::Change c)
835 if (_signals_suspended) {
836 _pending_bundle_change = c;
842 PortGroupList::suspend_signals ()
844 _signals_suspended = true;
848 PortGroupList::resume_signals ()
850 if (_pending_change) {
852 _pending_change = false;
855 if (_pending_bundle_change != 0) {
856 BundleChanged (_pending_bundle_change);
857 _pending_bundle_change = (ARDOUR::Bundle::Change) 0;
860 _signals_suspended = false;
863 boost::shared_ptr<IO>
864 PortGroupList::io_from_bundle (boost::shared_ptr<ARDOUR::Bundle> b) const
866 List::const_iterator i = _groups.begin ();
867 while (i != _groups.end()) {
868 boost::shared_ptr<IO> io = (*i)->io_from_bundle (b);
875 return boost::shared_ptr<IO> ();
879 PortGroupList::empty () const
881 return _groups.empty ();