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"
32 #include "port_group.h"
33 #include "port_matrix.h"
34 #include "time_axis_view.h"
35 #include "public_editor.h"
41 using namespace ARDOUR;
43 /** PortGroup constructor.
46 PortGroup::PortGroup (std::string const & n)
47 : name (n), _visible (true)
52 /** Add a bundle to a group.
56 PortGroup::add_bundle (boost::shared_ptr<Bundle> b)
63 r.changed_connection = b->Changed.connect (sigc::mem_fun (*this, &PortGroup::bundle_changed));
65 _bundles.push_back (r);
70 /** Add a bundle to a group.
72 * @param c Colour to represent the group with.
75 PortGroup::add_bundle (boost::shared_ptr<Bundle> b, Gdk::Color c)
83 r.changed_connection = b->Changed.connect (sigc::mem_fun (*this, &PortGroup::bundle_changed));
85 _bundles.push_back (r);
91 PortGroup::remove_bundle (boost::shared_ptr<Bundle> b)
95 BundleList::iterator i = _bundles.begin ();
96 while (i != _bundles.end() && i->bundle != b) {
100 if (i == _bundles.end()) {
104 i->changed_connection.disconnect ();
111 PortGroup::bundle_changed (Bundle::Change c)
120 for (BundleList::iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
121 i->changed_connection.disconnect ();
129 PortGroup::has_port (std::string const& p) const
131 for (BundleList::const_iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
132 if (i->bundle->offers_port_alone (p)) {
140 boost::shared_ptr<Bundle>
141 PortGroup::only_bundle ()
143 assert (_bundles.size() == 1);
144 return _bundles.front().bundle;
149 PortGroup::total_channels () const
152 for (BundleList::const_iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
153 n += i->bundle->nchannels ();
159 /** PortGroupList constructor.
161 PortGroupList::PortGroupList ()
162 : _type (DataType::AUDIO), _signals_suspended (false), _pending_change (false)
168 PortGroupList::set_type (DataType t)
175 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)
177 boost::shared_ptr<Processor> p (wp.lock());
183 boost::shared_ptr<IOProcessor> iop = boost::dynamic_pointer_cast<IOProcessor> (p);
187 boost::shared_ptr<IO> io = inputs ? iop->input() : iop->output();
189 if (io && used_io.find (io) == used_io.end()) {
190 rb->add_processor_bundle (io->bundle ());
197 /** Gather bundles from around the system and put them in this PortGroupList */
199 PortGroupList::gather (ARDOUR::Session& session, bool inputs)
203 boost::shared_ptr<PortGroup> bus (new PortGroup (_("Bus")));
204 boost::shared_ptr<PortGroup> track (new PortGroup (_("Track")));
205 boost::shared_ptr<PortGroup> system (new PortGroup (_("System")));
206 boost::shared_ptr<PortGroup> other (new PortGroup (_("Other")));
208 /* Find the bundles for routes. We use the RouteBundle class to join
209 the route's input/output and processor bundles together so that they
210 are presented as one bundle in the matrix. */
212 boost::shared_ptr<RouteList> routes = session.get_routes ();
214 for (RouteList::const_iterator i = routes->begin(); i != routes->end(); ++i) {
216 /* keep track of IOs that we have taken bundles from, so that maybe_add_processor... below
217 can avoid taking the same IO from both Route::output() and the main_outs Delivery */
219 set<boost::shared_ptr<IO> > used_io;
220 boost::shared_ptr<IO> io = inputs ? (*i)->input() : (*i)->output();
223 boost::shared_ptr<RouteBundle> rb (new RouteBundle (io->bundle()));
225 (*i)->foreach_processor (bind (mem_fun (*this, &PortGroupList::maybe_add_processor_to_bundle), rb, inputs, used_io));
227 /* Work out which group to put this bundle in */
228 boost::shared_ptr<PortGroup> g;
229 if (_type == DataType::AUDIO) {
231 if (boost::dynamic_pointer_cast<AudioTrack> (*i)) {
233 } else if (!boost::dynamic_pointer_cast<MidiTrack>(*i)) {
238 } else if (_type == DataType::MIDI) {
240 if (boost::dynamic_pointer_cast<MidiTrack> (*i)) {
244 /* No MIDI busses yet */
249 TimeAxisView* tv = PublicEditor::instance().axis_view_from_route (i->get());
251 g->add_bundle (rb, tv->color ());
258 /* Bundles owned by the session. We only add the mono ones and the User ones
259 otherwise there is duplication of the same ports within the matrix */
261 boost::shared_ptr<BundleList> b = session.bundles ();
262 for (BundleList::iterator i = b->begin(); i != b->end(); ++i) {
263 if ((*i)->ports_are_inputs() == inputs && (*i)->type() == _type) {
265 if ((*i)->nchannels() == 1 || boost::dynamic_pointer_cast<UserBundle> (*i)) {
266 system->add_bundle (*i);
272 /* Now find all other ports that we haven't thought of yet */
274 std::vector<std::string> extra_system;
275 std::vector<std::string> extra_other;
277 const char **ports = session.engine().get_ports ("", _type.to_jack_type(), inputs ?
278 JackPortIsInput : JackPortIsOutput);
282 string client_matching_string;
284 client_matching_string = session.engine().client_name();
285 client_matching_string += ':';
289 std::string const p = ports[n];
291 if (!system->has_port(p) && !bus->has_port(p) && !track->has_port(p) && !other->has_port(p)) {
293 if (port_has_prefix (p, "system:") ||
294 port_has_prefix (p, "alsa_pcm") ||
295 port_has_prefix (p, "ardour:")) {
296 extra_system.push_back (p);
298 extra_other.push_back (p);
308 if (!extra_system.empty()) {
309 system->add_bundle (make_bundle_from_ports (extra_system, inputs));
312 if (!extra_other.empty()) {
313 other->add_bundle (make_bundle_from_ports (extra_other, inputs));
324 boost::shared_ptr<Bundle>
325 PortGroupList::make_bundle_from_ports (std::vector<std::string> const & p, bool inputs) const
327 boost::shared_ptr<Bundle> b (new Bundle ("", _type, inputs));
329 std::string const pre = common_prefix (p);
331 b->set_name (pre.substr (0, pre.length() - 1));
334 for (uint32_t j = 0; j < p.size(); ++j) {
335 b->add_channel (p[j].substr (pre.length()));
336 b->set_port (j, p[j]);
343 PortGroupList::port_has_prefix (const std::string& n, const std::string& p) const
345 return n.substr (0, p.length()) == p;
349 PortGroupList::common_prefix_before (std::vector<std::string> const & p, std::string const & s) const
351 /* we must have some strings and the first must contain the separator string */
352 if (p.empty() || p[0].find_first_of (s) == std::string::npos) {
356 /* prefix of the first string */
357 std::string const fp = p[0].substr (0, p[0].find_first_of (s) + 1);
359 /* see if the other strings also start with fp */
361 while (j < p.size()) {
362 if (p[j].substr (0, fp.length()) != fp) {
377 PortGroupList::common_prefix (std::vector<std::string> const & p) const
379 /* common prefix before '/' ? */
380 std::string cp = common_prefix_before (p, "/");
385 cp = common_prefix_before (p, ":");
394 PortGroupList::clear ()
398 for (std::vector<sigc::connection>::iterator i = _bundle_changed_connections.begin(); i != _bundle_changed_connections.end(); ++i) {
402 _bundle_changed_connections.clear ();
408 PortGroup::BundleList const &
409 PortGroupList::bundles () const
413 for (PortGroupList::List::const_iterator i = begin (); i != end (); ++i) {
414 if ((*i)->visible()) {
415 std::copy ((*i)->bundles().begin(), (*i)->bundles().end(), std::back_inserter (_bundles));
423 PortGroupList::total_visible_channels () const
427 for (PortGroupList::List::const_iterator i = begin(); i != end(); ++i) {
428 if ((*i)->visible()) {
429 n += (*i)->total_channels ();
438 PortGroupList::add_group (boost::shared_ptr<PortGroup> g)
440 _groups.push_back (g);
442 g->Changed.connect (sigc::mem_fun (*this, &PortGroupList::emit_changed));
444 _bundle_changed_connections.push_back (
445 g->BundleChanged.connect (sigc::hide (sigc::mem_fun (*this, &PortGroupList::emit_changed)))
452 PortGroupList::remove_bundle (boost::shared_ptr<Bundle> b)
454 for (List::iterator i = _groups.begin(); i != _groups.end(); ++i) {
455 (*i)->remove_bundle (b);
462 PortGroupList::emit_changed ()
464 if (_signals_suspended) {
465 _pending_change = true;
472 PortGroupList::suspend_signals ()
474 _signals_suspended = true;
478 PortGroupList::resume_signals ()
480 if (_pending_change) {
482 _pending_change = false;
485 _signals_suspended = false;
488 RouteBundle::RouteBundle (boost::shared_ptr<Bundle> r)
491 _route->Changed.connect (sigc::hide (sigc::mem_fun (*this, &RouteBundle::reread_component_bundles)));
492 reread_component_bundles ();
496 RouteBundle::reread_component_bundles ()
502 set_name (_route->name());
504 for (uint32_t i = 0; i < _route->nchannels(); ++i) {
505 add_channel (_route->channel_name (i));
506 PortList const & pl = _route->channel_ports (i);
507 for (uint32_t j = 0; j < pl.size(); ++j) {
508 add_port_to_channel (i, pl[j]);
512 for (std::vector<boost::shared_ptr<Bundle> >::iterator i = _processor.begin(); i != _processor.end(); ++i) {
513 add_channels_from_bundle (*i);
520 RouteBundle::add_processor_bundle (boost::shared_ptr<Bundle> p)
522 p->Changed.connect (sigc::hide (sigc::mem_fun (*this, &RouteBundle::reread_component_bundles)));
523 _processor.push_back (p);
525 reread_component_bundles ();