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.
20 #include "port_group.h"
21 #include "port_matrix.h"
23 #include "ardour/session.h"
24 #include "ardour/audio_track.h"
25 #include "ardour/midi_track.h"
26 #include "ardour/audioengine.h"
27 #include "ardour/port.h"
28 #include "ardour/bundle.h"
29 #include <boost/shared_ptr.hpp>
35 /** Add a bundle to a group.
39 PortGroup::add_bundle (boost::shared_ptr<ARDOUR::Bundle> b)
42 _bundles.push_back (b);
47 /** Add a port to a group.
51 PortGroup::add_port (std::string const &p)
68 PortGroup::has_port (std::string const& p) const
70 for (ARDOUR::BundleList::const_iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
71 if ((*i)->offers_port_alone (p)) {
76 for (vector<std::string>::const_iterator i = ports.begin(); i != ports.end(); ++i) {
85 boost::shared_ptr<ARDOUR::Bundle>
86 PortGroup::only_bundle ()
88 assert (_bundles.size() == 1);
89 return _bundles.front();
94 PortGroup::total_ports () const
97 for (ARDOUR::BundleList::const_iterator i = _bundles.begin(); i != _bundles.end(); ++i) {
98 n += (*i)->nchannels ();
107 /** PortGroupList constructor.
110 PortGroupList::PortGroupList ()
111 : _type (ARDOUR::DataType::AUDIO), _bundles_dirty (true)
117 PortGroupList::set_type (ARDOUR::DataType t)
123 /** Gather bundles from around the system and put them in this PortGroupList */
125 PortGroupList::gather (ARDOUR::Session& session, bool inputs)
129 boost::shared_ptr<PortGroup> buss (new PortGroup (_("Buss")));
130 boost::shared_ptr<PortGroup> track (new PortGroup (_("Track")));
131 boost::shared_ptr<PortGroup> system (new PortGroup (_("System")));
132 boost::shared_ptr<PortGroup> other (new PortGroup (_("Other")));
134 /* Find the bundles for routes. We take their bundles, copy them,
135 and add ports from the route's processors */
137 boost::shared_ptr<ARDOUR::RouteList> routes = session.get_routes ();
139 for (ARDOUR::RouteList::const_iterator i = routes->begin(); i != routes->end(); ++i) {
140 /* Copy the appropriate bundle from the route */
141 boost::shared_ptr<ARDOUR::Bundle> bundle (
143 inputs ? (*i)->bundle_for_inputs() : (*i)->bundle_for_outputs ()
147 /* Add ports from the route's processors */
150 boost::shared_ptr<ARDOUR::Processor> p = (*i)->nth_processor (n);
155 boost::shared_ptr<ARDOUR::IOProcessor> iop = boost::dynamic_pointer_cast<ARDOUR::IOProcessor> (p);
158 boost::shared_ptr<ARDOUR::Bundle> pb = inputs ?
159 iop->io()->bundle_for_inputs() : iop->io()->bundle_for_outputs();
160 bundle->add_channels_from_bundle (pb);
166 /* Work out which group to put this bundle in */
167 boost::shared_ptr<PortGroup> g;
168 if (_type == ARDOUR::DataType::AUDIO) {
170 if (boost::dynamic_pointer_cast<ARDOUR::AudioTrack> (*i)) {
172 } else if (!boost::dynamic_pointer_cast<ARDOUR::MidiTrack>(*i)) {
177 } else if (_type == ARDOUR::DataType::MIDI) {
179 if (boost::dynamic_pointer_cast<ARDOUR::MidiTrack> (*i)) {
183 /* No MIDI busses yet */
187 g->add_bundle (bundle);
191 /* Bundles created by the session */
193 boost::shared_ptr<ARDOUR::BundleList> b = session.bundles ();
194 for (ARDOUR::BundleList::iterator i = b->begin(); i != b->end(); ++i) {
195 if ((*i)->ports_are_inputs() == inputs && (*i)->type() == _type) {
196 system->add_bundle (*i);
200 /* Now find all other ports that we haven't thought of yet */
202 const char **ports = session.engine().get_ports ("", _type.to_jack_type(), inputs ?
203 JackPortIsInput : JackPortIsOutput);
207 string client_matching_string;
209 client_matching_string = session.engine().client_name();
210 client_matching_string += ':';
214 std::string const p = ports[n];
216 if (!system->has_port(p) && !buss->has_port(p) && !track->has_port(p) && !other->has_port(p)) {
218 if (port_has_prefix (p, "system:") ||
219 port_has_prefix (p, "alsa_pcm") ||
220 port_has_prefix (p, "ardour:")) {
221 system->add_port (p);
238 _bundles_dirty = true;
242 PortGroupList::port_has_prefix (const std::string& n, const std::string& p) const
244 return n.substr (0, p.length()) == p;
249 PortGroupList::update_bundles () const
253 for (PortGroupList::List::const_iterator i = begin (); i != end (); ++i) {
254 if ((*i)->visible()) {
256 std::copy ((*i)->bundles().begin(), (*i)->bundles().end(), std::back_inserter (_bundles));
258 /* make a bundle for the ports, if there are any */
259 if (!(*i)->ports.empty()) {
261 boost::shared_ptr<ARDOUR::Bundle> b (new ARDOUR::Bundle ("", _type, !_offer_inputs));
263 std::string const pre = common_prefix ((*i)->ports);
265 b->set_name (pre.substr (0, pre.length() - 1));
268 for (uint32_t j = 0; j < (*i)->ports.size(); ++j) {
269 std::string const p = (*i)->ports[j];
270 b->add_channel (p.substr (pre.length()));
274 _bundles.push_back (b);
279 _bundles_dirty = false;
283 PortGroupList::common_prefix (std::vector<std::string> const & p) const
285 /* common prefix before '/' ? */
286 if (p[0].find_first_of ("/") != std::string::npos) {
287 std::string const fp = p[0].substr (0, (p[0].find_first_of ("/") + 1));
289 while (j < p.size()) {
290 if (p[j].substr (0, fp.length()) != fp) {
301 /* or before ':' ? */
302 if (p[0].find_first_of (":") != std::string::npos) {
303 std::string const fp = p[0].substr (0, (p[0].find_first_of (":") + 1));
305 while (j < p.size()) {
306 if (p[j].substr (0, fp.length()) != fp) {
321 PortGroupList::clear ()
324 _bundles_dirty = true;
327 ARDOUR::BundleList const &
328 PortGroupList::bundles () const
330 if (_bundles_dirty) {
338 PortGroupList::total_visible_ports () const
342 for (PortGroupList::List::const_iterator i = begin(); i != end(); ++i) {
343 if ((*i)->visible()) {
344 n += (*i)->total_ports ();
352 PortGroupList::group_modified ()
354 _bundles_dirty = true;
358 PortGroupList::add_group (boost::shared_ptr<PortGroup> g)
360 _groups.push_back (g);
361 g->Modified.connect (sigc::mem_fun (*this, &PortGroupList::group_modified));
362 _bundles_dirty = true;