2 Copyright (C) 2000-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 #define __STDC_FORMAT_MACROS
25 #include <sigc++/bind.h>
27 #include "pbd/error.h"
28 #include "pbd/enumwriter.h"
29 #include "pbd/strsplit.h"
31 #include "ardour/amp.h"
32 #include "ardour/route_group.h"
33 #include "ardour/audio_track.h"
34 #include "ardour/audio_diskstream.h"
35 #include "ardour/configuration.h"
36 #include "ardour/session.h"
40 using namespace ARDOUR;
43 RouteGroup::RouteGroup (Session& s, const string &n, Flag f, Property p)
45 , routes (new RouteList)
48 , _properties (Property (p))
52 RouteGroup::~RouteGroup ()
54 for (RouteList::iterator i = routes->begin(); i != routes->end();) {
55 RouteList::iterator tmp = i;
58 (*i)->leave_route_group ();
65 RouteGroup::set_name (string str)
68 _session.set_dirty ();
69 FlagsChanged (0); /* EMIT SIGNAL */
73 RouteGroup::add (boost::shared_ptr<Route> r)
75 r->leave_route_group ();
77 routes->push_back (r);
79 r->join_route_group (this);
80 r->GoingAway.connect (sigc::bind (sigc::mem_fun (*this, &RouteGroup::remove_when_going_away), boost::weak_ptr<Route> (r)));
82 _session.set_dirty ();
83 changed (); /* EMIT SIGNAL */
88 RouteGroup::remove_when_going_away (boost::weak_ptr<Route> wr)
90 boost::shared_ptr<Route> r (wr.lock());
98 RouteGroup::remove (boost::shared_ptr<Route> r)
100 RouteList::iterator i;
102 if ((i = find (routes->begin(), routes->end(), r)) != routes->end()) {
103 r->leave_route_group ();
105 _session.set_dirty ();
106 changed (); /* EMIT SIGNAL */
115 RouteGroup::get_min_factor(gain_t factor)
119 for (RouteList::iterator i = routes->begin(); i != routes->end(); i++) {
120 g = (*i)->amp()->gain();
122 if ( (g+g*factor) >= 0.0f)
125 if ( g <= 0.0000003f )
128 factor = 0.0000003f/g - 1.0f;
134 RouteGroup::get_max_factor(gain_t factor)
138 for (RouteList::iterator i = routes->begin(); i != routes->end(); i++) {
139 g = (*i)->amp()->gain();
141 // if the current factor woulnd't raise this route above maximum
142 if ( (g+g*factor) <= 1.99526231f)
145 // if route gain is already at peak, return 0.0f factor
149 // factor is calculated so that it would raise current route to max
150 factor = 1.99526231f/g - 1.0f;
157 RouteGroup::get_state (void)
159 XMLNode *node = new XMLNode ("RouteGroup");
160 node->add_property ("name", _name);
161 node->add_property ("flags", enum_2_string (_flags));
162 node->add_property ("properties", enum_2_string (_properties));
164 if (!routes->empty()) {
167 for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
168 str << (*i)->id () << ' ';
171 node->add_property ("routes", str.str());
178 RouteGroup::set_state (const XMLNode& node, int version)
180 if (version < 3000) {
181 return set_state_2X (node, version);
184 const XMLProperty *prop;
186 if ((prop = node.property ("name")) != 0) {
187 _name = prop->value();
190 if ((prop = node.property ("flags")) != 0) {
191 _flags = Flag (string_2_enum (prop->value(), _flags));
194 if ((prop = node.property ("properties")) != 0) {
195 _properties = Property (string_2_enum (prop->value(), _properties));
198 if ((prop = node.property ("routes")) != 0) {
199 stringstream str (prop->value());
201 split (str.str(), ids, ' ');
203 for (vector<string>::iterator i = ids.begin(); i != ids.end(); ++i) {
205 boost::shared_ptr<Route> r = _session.route_by_id (id);
217 RouteGroup::set_state_2X (const XMLNode& node, int /*version*/)
219 XMLProperty const * prop;
221 if ((prop = node.property ("name")) != 0) {
222 _name = prop->value();
225 if ((prop = node.property ("flags")) != 0) {
226 _flags = Flag (string_2_enum (prop->value(), _flags));
229 if (node.name() == "MixGroup") {
230 _properties = Property (Gain | Mute | Solo | RecEnable);
231 } else if (node.name() == "EditGroup") {
232 _properties = Property (Select | Edit);
239 RouteGroup::set_active (bool yn, void *src)
241 if (is_active() == yn) {
245 _flags = Flag (_flags | Active);
247 _flags = Flag (_flags & ~Active);
249 _session.set_dirty ();
250 FlagsChanged (src); /* EMIT SIGNAL */
254 RouteGroup::set_relative (bool yn, void *src)
257 if (is_relative() == yn) {
261 _flags = Flag (_flags | Relative);
263 _flags = Flag (_flags & ~Relative);
265 _session.set_dirty ();
266 FlagsChanged (src); /* EMIT SIGNAL */
270 RouteGroup::set_hidden (bool yn, void *src)
273 if (is_hidden() == yn) {
277 _flags = Flag (_flags | Hidden);
278 if (Config->get_hiding_groups_deactivates_groups()) {
279 _flags = Flag (_flags & ~Active);
282 _flags = Flag (_flags & ~Hidden);
283 if (Config->get_hiding_groups_deactivates_groups()) {
284 _flags = Flag (_flags | Active);
287 _session.set_dirty ();
288 FlagsChanged (src); /* EMIT SIGNAL */
292 RouteGroup::audio_track_group (set<boost::shared_ptr<AudioTrack> >& ats)
294 for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
295 boost::shared_ptr<AudioTrack> at = boost::dynamic_pointer_cast<AudioTrack>(*i);
303 RouteGroup::make_subgroup ()
308 /* since we don't do MIDI Busses yet, check quickly ... */
310 for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
311 if ((*i)->output()->n_ports().n_midi() != 0) {
312 PBD::info << _("You cannot subgroup MIDI tracks at this time") << endmsg;
317 for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
318 nin = max (nin, (*i)->output()->n_ports().n_audio());
322 /* use master bus etc. to determine default nouts */
323 rl = _session.new_audio_route (false, nin, 2, 0, 1);
328 subgroup_bus = rl.front();
329 subgroup_bus->set_name (_name);
331 boost::shared_ptr<Bundle> bundle = subgroup_bus->input()->bundle ();
333 for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
334 (*i)->output()->disconnect (this);
335 (*i)->output()->connect_ports_to_bundle (bundle, this);
340 RouteGroup::destroy_subgroup ()
346 for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
347 (*i)->output()->disconnect (this);
348 /* XXX find a new bundle to connect to */
351 _session.remove_route (subgroup_bus);
352 subgroup_bus.reset ();