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/io_processor.h"
27 #include "ardour/midi_track.h"
28 #include "ardour/port.h"
29 #include "ardour/session.h"
31 #include "port_group.h"
32 #include "port_matrix.h"
39 /** Add a bundle to a group.
43 PortGroup::add_bundle (boost::shared_ptr<ARDOUR::Bundle> b)
46 _bundles.push_back (b);
51 /** Add a port to a group.
55 PortGroup::add_port (std::string const &p)
72 PortGroup::has_port (std::string const& p) const
74 for (ARDOUR::BundleList::const_iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
75 if ((*i)->offers_port_alone (p)) {
80 for (vector<std::string>::const_iterator i = ports.begin(); i != ports.end(); ++i) {
89 boost::shared_ptr<ARDOUR::Bundle>
90 PortGroup::only_bundle ()
92 assert (_bundles.size() == 1);
93 return _bundles.front();
98 PortGroup::total_ports () const
101 for (ARDOUR::BundleList::const_iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
102 n += (*i)->nchannels ();
111 /** PortGroupList constructor.
114 PortGroupList::PortGroupList ()
115 : _type (ARDOUR::DataType::AUDIO), _bundles_dirty (true)
121 PortGroupList::set_type (ARDOUR::DataType t)
127 /** Gather bundles from around the system and put them in this PortGroupList */
129 PortGroupList::gather (ARDOUR::Session& session, bool inputs)
133 boost::shared_ptr<PortGroup> bus (new PortGroup (_("Bus")));
134 boost::shared_ptr<PortGroup> track (new PortGroup (_("Track")));
135 boost::shared_ptr<PortGroup> system (new PortGroup (_("System")));
136 boost::shared_ptr<PortGroup> other (new PortGroup (_("Other")));
138 /* Find the bundles for routes. We take their bundles, copy them,
139 and add ports from the route's processors */
141 boost::shared_ptr<ARDOUR::RouteList> routes = session.get_routes ();
143 for (ARDOUR::RouteList::const_iterator i = routes->begin(); i != routes->end(); ++i) {
144 /* Copy the appropriate bundle from the route */
145 boost::shared_ptr<ARDOUR::Bundle> bundle (
147 inputs ? (*i)->bundle_for_inputs() : (*i)->bundle_for_outputs ()
151 /* Add ports from the route's processors */
154 boost::shared_ptr<ARDOUR::Processor> p = (*i)->nth_processor (n);
159 boost::shared_ptr<ARDOUR::IOProcessor> iop = boost::dynamic_pointer_cast<ARDOUR::IOProcessor> (p);
162 boost::shared_ptr<ARDOUR::Bundle> pb = inputs ?
163 iop->io()->bundle_for_inputs() : iop->io()->bundle_for_outputs();
164 bundle->add_channels_from_bundle (pb);
170 /* Work out which group to put this bundle in */
171 boost::shared_ptr<PortGroup> g;
172 if (_type == ARDOUR::DataType::AUDIO) {
174 if (boost::dynamic_pointer_cast<ARDOUR::AudioTrack> (*i)) {
176 } else if (!boost::dynamic_pointer_cast<ARDOUR::MidiTrack>(*i)) {
181 } else if (_type == ARDOUR::DataType::MIDI) {
183 if (boost::dynamic_pointer_cast<ARDOUR::MidiTrack> (*i)) {
187 /* No MIDI busses yet */
191 g->add_bundle (bundle);
195 /* Bundles created by the session. We only add the mono ones,
196 otherwise there is duplication of the same ports within the matrix */
198 boost::shared_ptr<ARDOUR::BundleList> b = session.bundles ();
199 for (ARDOUR::BundleList::iterator i = b->begin(); i != b->end(); ++i) {
200 if ((*i)->nchannels() == 1 && (*i)->ports_are_inputs() == inputs && (*i)->type() == _type) {
201 system->add_bundle (*i);
205 /* Now find all other ports that we haven't thought of yet */
207 const char **ports = session.engine().get_ports ("", _type.to_jack_type(), inputs ?
208 JackPortIsInput : JackPortIsOutput);
212 string client_matching_string;
214 client_matching_string = session.engine().client_name();
215 client_matching_string += ':';
219 std::string const p = ports[n];
221 if (!system->has_port(p) && !bus->has_port(p) && !track->has_port(p) && !other->has_port(p)) {
223 if (port_has_prefix (p, "system:") ||
224 port_has_prefix (p, "alsa_pcm") ||
225 port_has_prefix (p, "ardour:")) {
226 system->add_port (p);
243 _bundles_dirty = true;
247 PortGroupList::port_has_prefix (const std::string& n, const std::string& p) const
249 return n.substr (0, p.length()) == p;
254 PortGroupList::update_bundles () const
258 for (PortGroupList::List::const_iterator i = begin (); i != end (); ++i) {
259 if ((*i)->visible()) {
261 std::copy ((*i)->bundles().begin(), (*i)->bundles().end(), std::back_inserter (_bundles));
263 /* make a bundle for the ports, if there are any */
264 if (!(*i)->ports.empty()) {
266 boost::shared_ptr<ARDOUR::Bundle> b (new ARDOUR::Bundle ("", _type, !_offer_inputs));
268 std::string const pre = common_prefix ((*i)->ports);
270 b->set_name (pre.substr (0, pre.length() - 1));
273 for (uint32_t j = 0; j < (*i)->ports.size(); ++j) {
274 std::string const p = (*i)->ports[j];
275 b->add_channel (p.substr (pre.length()));
279 _bundles.push_back (b);
284 _bundles_dirty = false;
288 PortGroupList::common_prefix (std::vector<std::string> const & p) const
290 /* common prefix before '/' ? */
291 if (p[0].find_first_of ("/") != std::string::npos) {
292 std::string const fp = p[0].substr (0, (p[0].find_first_of ("/") + 1));
294 while (j < p.size()) {
295 if (p[j].substr (0, fp.length()) != fp) {
306 /* or before ':' ? */
307 if (p[0].find_first_of (":") != std::string::npos) {
308 std::string const fp = p[0].substr (0, (p[0].find_first_of (":") + 1));
310 while (j < p.size()) {
311 if (p[j].substr (0, fp.length()) != fp) {
326 PortGroupList::clear ()
329 _bundles_dirty = true;
332 ARDOUR::BundleList const &
333 PortGroupList::bundles () const
335 if (_bundles_dirty) {
343 PortGroupList::total_visible_ports () const
347 for (PortGroupList::List::const_iterator i = begin(); i != end(); ++i) {
348 if ((*i)->visible()) {
349 n += (*i)->total_ports ();
357 PortGroupList::group_modified ()
359 _bundles_dirty = true;
363 PortGroupList::add_group (boost::shared_ptr<PortGroup> g)
365 _groups.push_back (g);
366 g->Modified.connect (sigc::mem_fun (*this, &PortGroupList::group_modified));
367 _bundles_dirty = true;