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> buss (new PortGroup (_("Buss")));
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 */
197 boost::shared_ptr<ARDOUR::BundleList> b = session.bundles ();
198 for (ARDOUR::BundleList::iterator i = b->begin(); i != b->end(); ++i) {
199 if ((*i)->ports_are_inputs() == inputs && (*i)->type() == _type) {
200 system->add_bundle (*i);
204 /* Now find all other ports that we haven't thought of yet */
206 const char **ports = session.engine().get_ports ("", _type.to_jack_type(), inputs ?
207 JackPortIsInput : JackPortIsOutput);
211 string client_matching_string;
213 client_matching_string = session.engine().client_name();
214 client_matching_string += ':';
218 std::string const p = ports[n];
220 if (!system->has_port(p) && !buss->has_port(p) && !track->has_port(p) && !other->has_port(p)) {
222 if (port_has_prefix (p, "system:") ||
223 port_has_prefix (p, "alsa_pcm") ||
224 port_has_prefix (p, "ardour:")) {
225 system->add_port (p);
242 _bundles_dirty = true;
246 PortGroupList::port_has_prefix (const std::string& n, const std::string& p) const
248 return n.substr (0, p.length()) == p;
253 PortGroupList::update_bundles () const
257 for (PortGroupList::List::const_iterator i = begin (); i != end (); ++i) {
258 if ((*i)->visible()) {
260 std::copy ((*i)->bundles().begin(), (*i)->bundles().end(), std::back_inserter (_bundles));
262 /* make a bundle for the ports, if there are any */
263 if (!(*i)->ports.empty()) {
265 boost::shared_ptr<ARDOUR::Bundle> b (new ARDOUR::Bundle ("", _type, !_offer_inputs));
267 std::string const pre = common_prefix ((*i)->ports);
269 b->set_name (pre.substr (0, pre.length() - 1));
272 for (uint32_t j = 0; j < (*i)->ports.size(); ++j) {
273 std::string const p = (*i)->ports[j];
274 b->add_channel (p.substr (pre.length()));
278 _bundles.push_back (b);
283 _bundles_dirty = false;
287 PortGroupList::common_prefix (std::vector<std::string> const & p) const
289 /* common prefix before '/' ? */
290 if (p[0].find_first_of ("/") != std::string::npos) {
291 std::string const fp = p[0].substr (0, (p[0].find_first_of ("/") + 1));
293 while (j < p.size()) {
294 if (p[j].substr (0, fp.length()) != fp) {
305 /* or before ':' ? */
306 if (p[0].find_first_of (":") != std::string::npos) {
307 std::string const fp = p[0].substr (0, (p[0].find_first_of (":") + 1));
309 while (j < p.size()) {
310 if (p[j].substr (0, fp.length()) != fp) {
325 PortGroupList::clear ()
328 _bundles_dirty = true;
331 ARDOUR::BundleList const &
332 PortGroupList::bundles () const
334 if (_bundles_dirty) {
342 PortGroupList::total_visible_ports () const
346 for (PortGroupList::List::const_iterator i = begin(); i != end(); ++i) {
347 if ((*i)->visible()) {
348 n += (*i)->total_ports ();
356 PortGroupList::group_modified ()
358 _bundles_dirty = true;
362 PortGroupList::add_group (boost::shared_ptr<PortGroup> g)
364 _groups.push_back (g);
365 g->Modified.connect (sigc::mem_fun (*this, &PortGroupList::group_modified));
366 _bundles_dirty = true;