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
26 #include "pbd/error.h"
27 #include "pbd/enumwriter.h"
28 #include "pbd/strsplit.h"
30 #include "ardour/amp.h"
31 #include "ardour/route_group.h"
32 #include "ardour/audio_track.h"
33 #include "ardour/audio_diskstream.h"
34 #include "ardour/configuration.h"
35 #include "ardour/session.h"
39 using namespace ARDOUR;
42 RouteGroup::RouteGroup (Session& s, const string &n, Flag f, Property p)
44 , routes (new RouteList)
47 , _properties (Property (p))
51 RouteGroup::~RouteGroup ()
53 for (RouteList::iterator i = routes->begin(); i != routes->end();) {
54 RouteList::iterator tmp = i;
57 (*i)->leave_route_group ();
64 RouteGroup::set_name (string str)
67 _session.set_dirty ();
68 FlagsChanged (0); /* EMIT SIGNAL */
71 /** Add a route to a group. Adding a route which is already in the group is allowed; nothing will happen.
72 * @param r Route to add.
75 RouteGroup::add (boost::shared_ptr<Route> r)
77 if (find (routes->begin(), routes->end(), r) != routes->end()) {
81 r->leave_route_group ();
83 routes->push_back (r);
85 r->join_route_group (this);
86 r->DropReferences.connect_same_thread (*this, boost::bind (&RouteGroup::remove_when_going_away, this, boost::weak_ptr<Route> (r)));
88 _session.set_dirty ();
89 changed (); /* EMIT SIGNAL */
94 RouteGroup::remove_when_going_away (boost::weak_ptr<Route> wr)
96 boost::shared_ptr<Route> r (wr.lock());
104 RouteGroup::remove (boost::shared_ptr<Route> r)
106 RouteList::iterator i;
108 if ((i = find (routes->begin(), routes->end(), r)) != routes->end()) {
109 r->leave_route_group ();
111 _session.set_dirty ();
112 changed (); /* EMIT SIGNAL */
121 RouteGroup::get_min_factor(gain_t factor)
125 for (RouteList::iterator i = routes->begin(); i != routes->end(); i++) {
126 g = (*i)->amp()->gain();
128 if ( (g+g*factor) >= 0.0f)
131 if ( g <= 0.0000003f )
134 factor = 0.0000003f/g - 1.0f;
140 RouteGroup::get_max_factor(gain_t factor)
144 for (RouteList::iterator i = routes->begin(); i != routes->end(); i++) {
145 g = (*i)->amp()->gain();
147 // if the current factor woulnd't raise this route above maximum
148 if ( (g+g*factor) <= 1.99526231f)
151 // if route gain is already at peak, return 0.0f factor
155 // factor is calculated so that it would raise current route to max
156 factor = 1.99526231f/g - 1.0f;
163 RouteGroup::get_state (void)
165 XMLNode *node = new XMLNode ("RouteGroup");
166 node->add_property ("name", _name);
167 node->add_property ("flags", enum_2_string (_flags));
168 node->add_property ("properties", enum_2_string (_properties));
170 if (!routes->empty()) {
173 for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
174 str << (*i)->id () << ' ';
177 node->add_property ("routes", str.str());
184 RouteGroup::set_state (const XMLNode& node, int version)
186 if (version < 3000) {
187 return set_state_2X (node, version);
190 const XMLProperty *prop;
192 if ((prop = node.property ("name")) != 0) {
193 _name = prop->value();
196 if ((prop = node.property ("flags")) != 0) {
197 _flags = Flag (string_2_enum (prop->value(), _flags));
200 if ((prop = node.property ("properties")) != 0) {
201 _properties = Property (string_2_enum (prop->value(), _properties));
204 if ((prop = node.property ("routes")) != 0) {
205 stringstream str (prop->value());
207 split (str.str(), ids, ' ');
209 for (vector<string>::iterator i = ids.begin(); i != ids.end(); ++i) {
211 boost::shared_ptr<Route> r = _session.route_by_id (id);
223 RouteGroup::set_state_2X (const XMLNode& node, int /*version*/)
225 XMLProperty const * prop;
227 if ((prop = node.property ("name")) != 0) {
228 _name = prop->value();
231 if ((prop = node.property ("flags")) != 0) {
232 _flags = Flag (string_2_enum (prop->value(), _flags));
235 if (node.name() == "MixGroup") {
236 _properties = Property (Gain | Mute | Solo | RecEnable);
237 } else if (node.name() == "EditGroup") {
238 _properties = Property (Select | Edit);
245 RouteGroup::set_active (bool yn, void *src)
247 if (is_active() == yn) {
251 _flags = Flag (_flags | Active);
253 _flags = Flag (_flags & ~Active);
255 _session.set_dirty ();
256 FlagsChanged (src); /* EMIT SIGNAL */
260 RouteGroup::set_relative (bool yn, void *src)
263 if (is_relative() == yn) {
267 _flags = Flag (_flags | Relative);
269 _flags = Flag (_flags & ~Relative);
271 _session.set_dirty ();
272 FlagsChanged (src); /* EMIT SIGNAL */
276 RouteGroup::set_hidden (bool yn, void *src)
279 if (is_hidden() == yn) {
283 _flags = Flag (_flags | Hidden);
284 if (Config->get_hiding_groups_deactivates_groups()) {
285 _flags = Flag (_flags & ~Active);
288 _flags = Flag (_flags & ~Hidden);
289 if (Config->get_hiding_groups_deactivates_groups()) {
290 _flags = Flag (_flags | Active);
293 _session.set_dirty ();
294 FlagsChanged (src); /* EMIT SIGNAL */
298 RouteGroup::audio_track_group (set<boost::shared_ptr<AudioTrack> >& ats)
300 for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
301 boost::shared_ptr<AudioTrack> at = boost::dynamic_pointer_cast<AudioTrack>(*i);
309 RouteGroup::make_subgroup ()
314 /* since we don't do MIDI Busses yet, check quickly ... */
316 for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
317 if ((*i)->output()->n_ports().n_midi() != 0) {
318 PBD::info << _("You cannot subgroup MIDI tracks at this time") << endmsg;
323 for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
324 nin = max (nin, (*i)->output()->n_ports().n_audio());
328 /* use master bus etc. to determine default nouts */
329 rl = _session.new_audio_route (false, nin, 2, 0, 1);
334 subgroup_bus = rl.front();
335 subgroup_bus->set_name (_name);
337 boost::shared_ptr<Bundle> bundle = subgroup_bus->input()->bundle ();
339 for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
340 (*i)->output()->disconnect (this);
341 (*i)->output()->connect_ports_to_bundle (bundle, this);
346 RouteGroup::destroy_subgroup ()
352 for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
353 (*i)->output()->disconnect (this);
354 /* XXX find a new bundle to connect to */
357 _session.remove_route (subgroup_bus);
358 subgroup_bus.reset ();