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>
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 "port_group.h"
34 #include "port_matrix.h"
35 #include "time_axis_view.h"
36 #include "public_editor.h"
42 using namespace ARDOUR;
44 /** PortGroup constructor.
47 PortGroup::PortGroup (std::string const & n)
53 /** Add a bundle to a group.
55 * @param allow_dups true to allow the group to contain more than one bundle with the same port, otherwise false.
58 PortGroup::add_bundle (boost::shared_ptr<Bundle> b, bool allow_dups)
60 add_bundle_internal (b, boost::shared_ptr<IO> (), false, Gdk::Color (), allow_dups);
63 /** Add a bundle to a group.
65 * @param io IO whose ports are in the bundle.
68 PortGroup::add_bundle (boost::shared_ptr<Bundle> b, boost::shared_ptr<IO> io)
70 add_bundle_internal (b, io, false, Gdk::Color (), false);
73 /** Add a bundle to a group.
75 * @param c Colour to represent the bundle with.
78 PortGroup::add_bundle (boost::shared_ptr<Bundle> b, boost::shared_ptr<IO> io, Gdk::Color c)
80 add_bundle_internal (b, io, true, c, false);
84 PortGroup::add_bundle_internal (boost::shared_ptr<Bundle> b, boost::shared_ptr<IO> io, bool has_colour, Gdk::Color colour, bool allow_dups)
90 /* don't add this bundle if we already have one with the same ports */
92 BundleList::iterator i = _bundles.begin ();
93 while (i != _bundles.end() && b->has_same_ports (i->bundle) == false) {
97 if (i != _bundles.end ()) {
106 r.has_colour = has_colour;
107 r.changed_connection = b->Changed.connect (sigc::mem_fun (*this, &PortGroup::bundle_changed));
109 _bundles.push_back (r);
115 PortGroup::remove_bundle (boost::shared_ptr<Bundle> b)
119 BundleList::iterator i = _bundles.begin ();
120 while (i != _bundles.end() && i->bundle != b) {
124 if (i == _bundles.end()) {
128 i->changed_connection.disconnect ();
135 PortGroup::bundle_changed (Bundle::Change c)
144 for (BundleList::iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
145 i->changed_connection.disconnect ();
153 PortGroup::has_port (std::string const& p) const
155 for (BundleList::const_iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
156 if (i->bundle->offers_port_alone (p)) {
164 boost::shared_ptr<Bundle>
165 PortGroup::only_bundle ()
167 assert (_bundles.size() == 1);
168 return _bundles.front().bundle;
173 PortGroup::total_channels () const
176 for (BundleList::const_iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
177 n += i->bundle->nchannels ();
183 boost::shared_ptr<IO>
184 PortGroup::io_from_bundle (boost::shared_ptr<ARDOUR::Bundle> b) const
186 BundleList::const_iterator i = _bundles.begin ();
187 while (i != _bundles.end() && i->bundle != b) {
191 if (i == _bundles.end()) {
192 return boost::shared_ptr<IO> ();
199 /** PortGroupList constructor.
201 PortGroupList::PortGroupList ()
202 : _type (DataType::AUDIO), _signals_suspended (false), _pending_change (false), _pending_bundle_change ((Bundle::Change) 0)
208 PortGroupList::set_type (DataType t)
215 PortGroupList::maybe_add_processor_to_list (
216 boost::weak_ptr<Processor> wp, list<boost::shared_ptr<Bundle> >* route_bundles, bool inputs, set<boost::shared_ptr<IO> >& used_io
219 boost::shared_ptr<Processor> p (wp.lock());
225 boost::shared_ptr<IOProcessor> iop = boost::dynamic_pointer_cast<IOProcessor> (p);
229 boost::shared_ptr<IO> io = inputs ? iop->input() : iop->output();
231 if (io && used_io.find (io) == used_io.end()) {
232 route_bundles->push_back (io->bundle ());
239 /** Gather bundles from around the system and put them in this PortGroupList */
241 PortGroupList::gather (ARDOUR::Session* session, bool inputs, bool allow_dups)
249 boost::shared_ptr<PortGroup> bus (new PortGroup (_("Bus")));
250 boost::shared_ptr<PortGroup> track (new PortGroup (_("Track")));
251 boost::shared_ptr<PortGroup> system (new PortGroup (_("System")));
252 boost::shared_ptr<PortGroup> ardour (new PortGroup (_("Ardour")));
253 boost::shared_ptr<PortGroup> other (new PortGroup (_("Other")));
255 /* Find the bundles for routes. We use the RouteBundle class to join
256 the route's input/output and processor bundles together so that they
257 are presented as one bundle in the matrix. */
259 boost::shared_ptr<RouteList> routes = session->get_routes ();
261 for (RouteList::const_iterator i = routes->begin(); i != routes->end(); ++i) {
263 list<boost::shared_ptr<Bundle> > route_bundles;
265 /* keep track of IOs that we have taken bundles from,
266 so that we can avoid taking the same IO from both
267 Route::output() and the main_outs Delivery */
269 set<boost::shared_ptr<IO> > used_io;
270 boost::shared_ptr<IO> io = inputs ? (*i)->input() : (*i)->output();
273 route_bundles.push_back (io->bundle ());
275 (*i)->foreach_processor (bind (mem_fun (*this, &PortGroupList::maybe_add_processor_to_list), &route_bundles, inputs, used_io));
277 /* Work out which group to put these bundles in */
278 boost::shared_ptr<PortGroup> g;
279 if (_type == DataType::AUDIO) {
281 if (boost::dynamic_pointer_cast<AudioTrack> (*i)) {
283 } else if (!boost::dynamic_pointer_cast<MidiTrack>(*i)) {
288 } else if (_type == DataType::MIDI) {
290 if (boost::dynamic_pointer_cast<MidiTrack> (*i)) {
294 /* No MIDI busses yet */
299 TimeAxisView* tv = PublicEditor::instance().axis_view_from_route (*i);
300 for (list<boost::shared_ptr<Bundle> >::iterator i = route_bundles.begin(); i != route_bundles.end(); ++i) {
302 g->add_bundle (*i, io, tv->color ());
304 g->add_bundle (*i, io);
310 /* Bundles owned by the session; add user bundles first, then normal ones, so
311 that UserBundles that offer the same ports as a normal bundle get priority
314 boost::shared_ptr<BundleList> b = session->bundles ();
316 for (BundleList::iterator i = b->begin(); i != b->end(); ++i) {
317 if (boost::dynamic_pointer_cast<UserBundle> (*i) && (*i)->ports_are_inputs() == inputs && (*i)->type() == _type) {
318 system->add_bundle (*i, allow_dups);
322 for (BundleList::iterator i = b->begin(); i != b->end(); ++i) {
323 if (boost::dynamic_pointer_cast<UserBundle> (*i) == 0 && (*i)->ports_are_inputs() == inputs && (*i)->type() == _type) {
324 system->add_bundle (*i, allow_dups);
330 if (!inputs && _type == DataType::AUDIO) {
331 ardour->add_bundle (session->the_auditioner()->output()->bundle());
332 ardour->add_bundle (session->click_io()->bundle());
335 /* Now find all other ports that we haven't thought of yet */
337 std::vector<std::string> extra_system;
338 std::vector<std::string> extra_other;
340 const char **ports = session->engine().get_ports ("", _type.to_jack_type(), inputs ?
341 JackPortIsInput : JackPortIsOutput);
345 string client_matching_string;
347 client_matching_string = session->engine().client_name();
348 client_matching_string += ':';
352 std::string const p = ports[n];
356 if (!system->has_port(p) &&
358 !track->has_port(p) &&
359 !ardour->has_port(p) &&
360 !other->has_port(p)) {
362 if (port_has_prefix (p, "system:") ||
363 port_has_prefix (p, "alsa_pcm") ||
364 port_has_prefix (p, "ardour:")) {
365 extra_system.push_back (p);
367 extra_other.push_back (p);
377 if (!extra_system.empty()) {
378 boost::shared_ptr<Bundle> b = make_bundle_from_ports (extra_system, inputs);
379 system->add_bundle (b);
382 if (!extra_other.empty()) {
383 other->add_bundle (make_bundle_from_ports (extra_other, inputs));
386 add_group_if_not_empty (system);
387 add_group_if_not_empty (bus);
388 add_group_if_not_empty (track);
389 add_group_if_not_empty (ardour);
390 add_group_if_not_empty (other);
395 boost::shared_ptr<Bundle>
396 PortGroupList::make_bundle_from_ports (std::vector<std::string> const & p, bool inputs) const
398 boost::shared_ptr<Bundle> b (new Bundle ("", _type, inputs));
400 std::string const pre = common_prefix (p);
402 b->set_name (pre.substr (0, pre.length() - 1));
405 for (uint32_t j = 0; j < p.size(); ++j) {
406 b->add_channel (p[j].substr (pre.length()));
407 b->set_port (j, p[j]);
414 PortGroupList::port_has_prefix (const std::string& n, const std::string& p) const
416 return n.substr (0, p.length()) == p;
420 PortGroupList::common_prefix_before (std::vector<std::string> const & p, std::string const & s) const
422 /* we must have some strings and the first must contain the separator string */
423 if (p.empty() || p[0].find_first_of (s) == std::string::npos) {
427 /* prefix of the first string */
428 std::string const fp = p[0].substr (0, p[0].find_first_of (s) + 1);
430 /* see if the other strings also start with fp */
432 while (j < p.size()) {
433 if (p[j].substr (0, fp.length()) != fp) {
448 PortGroupList::common_prefix (std::vector<std::string> const & p) const
450 /* common prefix before '/' ? */
451 std::string cp = common_prefix_before (p, "/");
456 cp = common_prefix_before (p, ":");
465 PortGroupList::clear ()
469 for (std::vector<sigc::connection>::iterator i = _bundle_changed_connections.begin(); i != _bundle_changed_connections.end(); ++i) {
473 _bundle_changed_connections.clear ();
479 PortGroup::BundleList const &
480 PortGroupList::bundles () const
484 for (PortGroupList::List::const_iterator i = begin (); i != end (); ++i) {
485 std::copy ((*i)->bundles().begin(), (*i)->bundles().end(), std::back_inserter (_bundles));
492 PortGroupList::total_channels () const
496 for (PortGroupList::List::const_iterator i = begin(); i != end(); ++i) {
497 n += (*i)->total_channels ();
504 PortGroupList::add_group_if_not_empty (boost::shared_ptr<PortGroup> g)
506 if (!g->bundles().empty ()) {
512 PortGroupList::add_group (boost::shared_ptr<PortGroup> g)
514 _groups.push_back (g);
516 g->Changed.connect (sigc::mem_fun (*this, &PortGroupList::emit_changed));
518 _bundle_changed_connections.push_back (
519 g->BundleChanged.connect (sigc::mem_fun (*this, &PortGroupList::emit_bundle_changed))
526 PortGroupList::remove_bundle (boost::shared_ptr<Bundle> b)
528 for (List::iterator i = _groups.begin(); i != _groups.end(); ++i) {
529 (*i)->remove_bundle (b);
536 PortGroupList::emit_changed ()
538 if (_signals_suspended) {
539 _pending_change = true;
546 PortGroupList::emit_bundle_changed (Bundle::Change c)
548 if (_signals_suspended) {
549 _pending_bundle_change = c;
555 PortGroupList::suspend_signals ()
557 _signals_suspended = true;
561 PortGroupList::resume_signals ()
563 if (_pending_change) {
565 _pending_change = false;
568 if (_pending_bundle_change != 0) {
569 BundleChanged (_pending_bundle_change);
570 _pending_bundle_change = (ARDOUR::Bundle::Change) 0;
573 _signals_suspended = false;
576 boost::shared_ptr<IO>
577 PortGroupList::io_from_bundle (boost::shared_ptr<ARDOUR::Bundle> b) const
579 List::const_iterator i = _groups.begin ();
580 while (i != _groups.end()) {
581 boost::shared_ptr<IO> io = (*i)->io_from_bundle (b);
588 return boost::shared_ptr<IO> ();
592 PortGroupList::empty () const
594 List::const_iterator i = _groups.begin ();
595 while (i != _groups.end() && (*i)->total_channels() == 0) {
599 return (i == _groups.end());