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)
48 : name (n), _visible (true)
53 /** Add a bundle to a group.
57 PortGroup::add_bundle (boost::shared_ptr<Bundle> b)
64 r.changed_connection = b->Changed.connect (sigc::mem_fun (*this, &PortGroup::bundle_changed));
66 _bundles.push_back (r);
71 /** Add a bundle to a group.
73 * @param c Colour to represent the group with.
76 PortGroup::add_bundle (boost::shared_ptr<Bundle> b, Gdk::Color c)
84 r.changed_connection = b->Changed.connect (sigc::mem_fun (*this, &PortGroup::bundle_changed));
86 _bundles.push_back (r);
92 PortGroup::remove_bundle (boost::shared_ptr<Bundle> b)
96 BundleList::iterator i = _bundles.begin ();
97 while (i != _bundles.end() && i->bundle != b) {
101 if (i == _bundles.end()) {
105 i->changed_connection.disconnect ();
112 PortGroup::bundle_changed (Bundle::Change c)
121 for (BundleList::iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
122 i->changed_connection.disconnect ();
130 PortGroup::has_port (std::string const& p) const
132 for (BundleList::const_iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
133 if (i->bundle->offers_port_alone (p)) {
141 boost::shared_ptr<Bundle>
142 PortGroup::only_bundle ()
144 assert (_bundles.size() == 1);
145 return _bundles.front().bundle;
150 PortGroup::total_channels () const
153 for (BundleList::const_iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
154 n += i->bundle->nchannels ();
160 /** PortGroupList constructor.
162 PortGroupList::PortGroupList ()
163 : _type (DataType::AUDIO), _signals_suspended (false), _pending_change (false)
169 PortGroupList::set_type (DataType t)
176 PortGroupList::maybe_add_processor_to_bundle (boost::weak_ptr<Processor> wp, boost::shared_ptr<RouteBundle> rb, bool inputs, set<boost::shared_ptr<IO> >& used_io)
178 boost::shared_ptr<Processor> p (wp.lock());
184 boost::shared_ptr<IOProcessor> iop = boost::dynamic_pointer_cast<IOProcessor> (p);
188 boost::shared_ptr<IO> io = inputs ? iop->input() : iop->output();
190 if (io && used_io.find (io) == used_io.end()) {
191 rb->add_processor_bundle (io->bundle ());
198 /** Gather bundles from around the system and put them in this PortGroupList */
200 PortGroupList::gather (ARDOUR::Session& session, bool inputs)
204 boost::shared_ptr<PortGroup> bus (new PortGroup (_("Bus")));
205 boost::shared_ptr<PortGroup> track (new PortGroup (_("Track")));
206 boost::shared_ptr<PortGroup> system (new PortGroup (_("System")));
207 boost::shared_ptr<PortGroup> ardour (new PortGroup (_("Ardour")));
208 boost::shared_ptr<PortGroup> other (new PortGroup (_("Other")));
210 /* Find the bundles for routes. We use the RouteBundle class to join
211 the route's input/output and processor bundles together so that they
212 are presented as one bundle in the matrix. */
214 boost::shared_ptr<RouteList> routes = session.get_routes ();
216 for (RouteList::const_iterator i = routes->begin(); i != routes->end(); ++i) {
218 /* keep track of IOs that we have taken bundles from, so that maybe_add_processor... below
219 can avoid taking the same IO from both Route::output() and the main_outs Delivery */
221 set<boost::shared_ptr<IO> > used_io;
222 boost::shared_ptr<IO> io = inputs ? (*i)->input() : (*i)->output();
225 boost::shared_ptr<RouteBundle> rb (new RouteBundle (io->bundle()));
227 (*i)->foreach_processor (bind (mem_fun (*this, &PortGroupList::maybe_add_processor_to_bundle), rb, inputs, used_io));
229 /* Work out which group to put this bundle in */
230 boost::shared_ptr<PortGroup> g;
231 if (_type == DataType::AUDIO) {
233 if (boost::dynamic_pointer_cast<AudioTrack> (*i)) {
235 } else if (!boost::dynamic_pointer_cast<MidiTrack>(*i)) {
240 } else if (_type == DataType::MIDI) {
242 if (boost::dynamic_pointer_cast<MidiTrack> (*i)) {
246 /* No MIDI busses yet */
251 TimeAxisView* tv = PublicEditor::instance().axis_view_from_route (i->get());
253 g->add_bundle (rb, tv->color ());
260 /* Bundles owned by the session. We only add the mono ones and the User ones
261 otherwise there is duplication of the same ports within the matrix */
263 boost::shared_ptr<BundleList> b = session.bundles ();
264 for (BundleList::iterator i = b->begin(); i != b->end(); ++i) {
265 if ((*i)->ports_are_inputs() == inputs && (*i)->type() == _type) {
267 if ((*i)->nchannels() == 1 || boost::dynamic_pointer_cast<UserBundle> (*i)) {
268 system->add_bundle (*i);
277 ardour->add_bundle (session.the_auditioner()->output()->bundle());
278 ardour->add_bundle (session.click_io()->bundle());
281 /* Now find all other ports that we haven't thought of yet */
283 std::vector<std::string> extra_system;
284 std::vector<std::string> extra_other;
286 const char **ports = session.engine().get_ports ("", _type.to_jack_type(), inputs ?
287 JackPortIsInput : JackPortIsOutput);
291 string client_matching_string;
293 client_matching_string = session.engine().client_name();
294 client_matching_string += ':';
298 std::string const p = ports[n];
300 if (!system->has_port(p) && !bus->has_port(p) && !track->has_port(p) && !ardour->has_port(p) && !other->has_port(p)) {
302 if (port_has_prefix (p, "system:") ||
303 port_has_prefix (p, "alsa_pcm") ||
304 port_has_prefix (p, "ardour:")) {
305 extra_system.push_back (p);
307 extra_other.push_back (p);
317 if (!extra_system.empty()) {
318 system->add_bundle (make_bundle_from_ports (extra_system, inputs));
321 if (!extra_other.empty()) {
322 other->add_bundle (make_bundle_from_ports (extra_other, inputs));
334 boost::shared_ptr<Bundle>
335 PortGroupList::make_bundle_from_ports (std::vector<std::string> const & p, bool inputs) const
337 boost::shared_ptr<Bundle> b (new Bundle ("", _type, inputs));
339 std::string const pre = common_prefix (p);
341 b->set_name (pre.substr (0, pre.length() - 1));
344 for (uint32_t j = 0; j < p.size(); ++j) {
345 b->add_channel (p[j].substr (pre.length()));
346 b->set_port (j, p[j]);
353 PortGroupList::port_has_prefix (const std::string& n, const std::string& p) const
355 return n.substr (0, p.length()) == p;
359 PortGroupList::common_prefix_before (std::vector<std::string> const & p, std::string const & s) const
361 /* we must have some strings and the first must contain the separator string */
362 if (p.empty() || p[0].find_first_of (s) == std::string::npos) {
366 /* prefix of the first string */
367 std::string const fp = p[0].substr (0, p[0].find_first_of (s) + 1);
369 /* see if the other strings also start with fp */
371 while (j < p.size()) {
372 if (p[j].substr (0, fp.length()) != fp) {
387 PortGroupList::common_prefix (std::vector<std::string> const & p) const
389 /* common prefix before '/' ? */
390 std::string cp = common_prefix_before (p, "/");
395 cp = common_prefix_before (p, ":");
404 PortGroupList::clear ()
408 for (std::vector<sigc::connection>::iterator i = _bundle_changed_connections.begin(); i != _bundle_changed_connections.end(); ++i) {
412 _bundle_changed_connections.clear ();
418 PortGroup::BundleList const &
419 PortGroupList::bundles () const
423 for (PortGroupList::List::const_iterator i = begin (); i != end (); ++i) {
424 if ((*i)->visible()) {
425 std::copy ((*i)->bundles().begin(), (*i)->bundles().end(), std::back_inserter (_bundles));
433 PortGroupList::total_visible_channels () const
437 for (PortGroupList::List::const_iterator i = begin(); i != end(); ++i) {
438 if ((*i)->visible()) {
439 n += (*i)->total_channels ();
448 PortGroupList::add_group (boost::shared_ptr<PortGroup> g)
450 _groups.push_back (g);
452 g->Changed.connect (sigc::mem_fun (*this, &PortGroupList::emit_changed));
454 _bundle_changed_connections.push_back (
455 g->BundleChanged.connect (sigc::hide (sigc::mem_fun (*this, &PortGroupList::emit_changed)))
462 PortGroupList::remove_bundle (boost::shared_ptr<Bundle> b)
464 for (List::iterator i = _groups.begin(); i != _groups.end(); ++i) {
465 (*i)->remove_bundle (b);
472 PortGroupList::emit_changed ()
474 if (_signals_suspended) {
475 _pending_change = true;
482 PortGroupList::suspend_signals ()
484 _signals_suspended = true;
488 PortGroupList::resume_signals ()
490 if (_pending_change) {
492 _pending_change = false;
495 _signals_suspended = false;
498 RouteBundle::RouteBundle (boost::shared_ptr<Bundle> r)
501 _route->Changed.connect (sigc::hide (sigc::mem_fun (*this, &RouteBundle::reread_component_bundles)));
502 reread_component_bundles ();
506 RouteBundle::reread_component_bundles ()
512 set_name (_route->name());
514 for (uint32_t i = 0; i < _route->nchannels(); ++i) {
515 add_channel (_route->channel_name (i));
516 PortList const & pl = _route->channel_ports (i);
517 for (uint32_t j = 0; j < pl.size(); ++j) {
518 add_port_to_channel (i, pl[j]);
522 for (std::vector<boost::shared_ptr<Bundle> >::iterator i = _processor.begin(); i != _processor.end(); ++i) {
523 add_channels_from_bundle (*i);
530 RouteBundle::add_processor_bundle (boost::shared_ptr<Bundle> p)
532 p->Changed.connect (sigc::hide (sigc::mem_fun (*this, &RouteBundle::reread_component_bundles)));
533 _processor.push_back (p);
535 reread_component_bundles ();