fix last commit
[ardour.git] / libs / ardour / route_group.cc
1 /*
2     Copyright (C) 2000-2009 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #define __STDC_FORMAT_MACROS
21 #include <inttypes.h>
22
23 #include <algorithm>
24
25
26 #include "pbd/error.h"
27 #include "pbd/enumwriter.h"
28 #include "pbd/strsplit.h"
29
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"
36
37 #include "i18n.h"
38
39 using namespace ARDOUR;
40 using namespace std;
41
42 RouteGroup::RouteGroup (Session& s, const string &n, Flag f, Property p)
43         : _session (s)
44         , routes (new RouteList)
45         , _name (n)
46         , _flags (f)
47         , _properties (Property (p))
48 {
49 }
50
51 RouteGroup::~RouteGroup ()
52 {
53         for (RouteList::iterator i = routes->begin(); i != routes->end();) {
54                 RouteList::iterator tmp = i;
55                 ++tmp;
56
57                 (*i)->leave_route_group ();
58                 
59                 i = tmp;
60         }
61 }
62
63 void
64 RouteGroup::set_name (string str)
65 {
66         _name = str;
67         _session.set_dirty ();
68         FlagsChanged (0); /* EMIT SIGNAL */
69 }
70
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.
73  */
74 int
75 RouteGroup::add (boost::shared_ptr<Route> r)
76 {
77         if (find (routes->begin(), routes->end(), r) != routes->end()) {
78                 return 0;
79         }
80         
81         r->leave_route_group ();
82
83         routes->push_back (r);
84
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)));
87         
88         _session.set_dirty ();
89         changed (); /* EMIT SIGNAL */
90         return 0;
91 }
92
93 void
94 RouteGroup::remove_when_going_away (boost::weak_ptr<Route> wr)
95 {
96         boost::shared_ptr<Route> r (wr.lock());
97
98         if (r) {
99                 remove (r);
100         }
101 }
102
103 int
104 RouteGroup::remove (boost::shared_ptr<Route> r)
105 {
106         RouteList::iterator i;
107
108         if ((i = find (routes->begin(), routes->end(), r)) != routes->end()) {
109                 r->leave_route_group ();
110                 routes->erase (i);
111                 _session.set_dirty ();
112                 changed (); /* EMIT SIGNAL */
113                 return 0;
114         }
115
116         return -1;
117 }
118
119
120 gain_t
121 RouteGroup::get_min_factor(gain_t factor)
122 {
123         gain_t g;
124
125         for (RouteList::iterator i = routes->begin(); i != routes->end(); i++) {
126                 g = (*i)->amp()->gain();
127
128                 if ( (g+g*factor) >= 0.0f)
129                         continue;
130
131                 if ( g <= 0.0000003f )
132                         return 0.0f;
133
134                 factor = 0.0000003f/g - 1.0f;
135         }
136         return factor;
137 }
138
139 gain_t
140 RouteGroup::get_max_factor(gain_t factor)
141 {
142         gain_t g;
143
144         for (RouteList::iterator i = routes->begin(); i != routes->end(); i++) {
145                 g = (*i)->amp()->gain();
146
147                 // if the current factor woulnd't raise this route above maximum
148                 if ( (g+g*factor) <= 1.99526231f)
149                         continue;
150
151                 // if route gain is already at peak, return 0.0f factor
152             if (g>=1.99526231f)
153                         return 0.0f;
154
155                 // factor is calculated so that it would raise current route to max
156                 factor = 1.99526231f/g - 1.0f;
157         }
158
159         return factor;
160 }
161
162 XMLNode&
163 RouteGroup::get_state (void)
164 {
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));
169
170         if (!routes->empty()) {
171                 stringstream str;
172                 
173                 for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
174                         str << (*i)->id () << ' ';
175                 }
176
177                 node->add_property ("routes", str.str());
178         }
179
180         return *node;
181 }
182
183 int
184 RouteGroup::set_state (const XMLNode& node, int version)
185 {
186         if (version < 3000) {
187                 return set_state_2X (node, version);
188         }
189
190         const XMLProperty *prop;
191
192         if ((prop = node.property ("name")) != 0) {
193                 _name = prop->value();
194         }
195
196         if ((prop = node.property ("flags")) != 0) {
197                 _flags = Flag (string_2_enum (prop->value(), _flags));
198         }
199
200         if ((prop = node.property ("properties")) != 0) {
201                 _properties = Property (string_2_enum (prop->value(), _properties));
202         }
203
204         if ((prop = node.property ("routes")) != 0) {
205                 stringstream str (prop->value());
206                 vector<string> ids;
207                 split (str.str(), ids, ' ');
208                 
209                 for (vector<string>::iterator i = ids.begin(); i != ids.end(); ++i) {
210                         PBD::ID id (*i);
211                         boost::shared_ptr<Route> r = _session.route_by_id (id);
212                         
213                         if (r) {
214                                 add (r);
215                         } 
216                 }
217         }
218
219         return 0;
220 }
221
222 int
223 RouteGroup::set_state_2X (const XMLNode& node, int /*version*/)
224 {
225         XMLProperty const * prop;
226
227         if ((prop = node.property ("name")) != 0) {
228                 _name = prop->value();
229         }
230
231         if ((prop = node.property ("flags")) != 0) {
232                 _flags = Flag (string_2_enum (prop->value(), _flags));
233         }
234
235         if (node.name() == "MixGroup") {
236                 _properties = Property (Gain | Mute | Solo | RecEnable);
237         } else if (node.name() == "EditGroup") {
238                 _properties = Property (Select | Edit);
239         }
240
241         return 0;
242 }
243
244 void
245 RouteGroup::set_active (bool yn, void *src)
246 {
247         if (is_active() == yn) {
248                 return;
249         }
250         if (yn) {
251                 _flags = Flag (_flags | Active);
252         } else {
253                 _flags = Flag (_flags & ~Active);
254         }
255         _session.set_dirty ();
256         FlagsChanged (src); /* EMIT SIGNAL */
257 }
258
259 void
260 RouteGroup::set_relative (bool yn, void *src)
261
262 {
263         if (is_relative() == yn) {
264                 return;
265         }
266         if (yn) {
267                 _flags = Flag (_flags | Relative);
268         } else {
269                 _flags = Flag (_flags & ~Relative);
270         }
271         _session.set_dirty ();
272         FlagsChanged (src); /* EMIT SIGNAL */
273 }
274
275 void
276 RouteGroup::set_hidden (bool yn, void *src)
277
278 {
279         if (is_hidden() == yn) {
280                 return;
281         }
282         if (yn) {
283                 _flags = Flag (_flags | Hidden);
284                 if (Config->get_hiding_groups_deactivates_groups()) {
285                         _flags = Flag (_flags & ~Active);
286                 }
287         } else {
288                 _flags = Flag (_flags & ~Hidden);
289                 if (Config->get_hiding_groups_deactivates_groups()) {
290                         _flags = Flag (_flags | Active);
291                 }
292         }
293         _session.set_dirty ();
294         FlagsChanged (src); /* EMIT SIGNAL */
295 }
296
297 void
298 RouteGroup::audio_track_group (set<boost::shared_ptr<AudioTrack> >& ats)
299 {
300         for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
301                 boost::shared_ptr<AudioTrack> at = boost::dynamic_pointer_cast<AudioTrack>(*i);
302                 if (at) {
303                         ats.insert (at);
304                 }
305         }
306 }
307
308 void
309 RouteGroup::make_subgroup ()
310 {
311         RouteList rl;
312         uint32_t nin = 0;
313
314         /* since we don't do MIDI Busses yet, check quickly ... */
315
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;
319                         return;
320                 }
321         }
322
323         for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
324                 nin = max (nin, (*i)->output()->n_ports().n_audio());
325         }
326
327         try {
328                 /* use master bus etc. to determine default nouts */
329                 rl = _session.new_audio_route (false, nin, 2, 0, 1);
330         } catch (...) {
331                 return;
332         }
333
334         subgroup_bus = rl.front();
335         subgroup_bus->set_name (_name);
336
337         boost::shared_ptr<Bundle> bundle = subgroup_bus->input()->bundle ();
338
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);
342         }
343 }
344
345 void
346 RouteGroup::destroy_subgroup ()
347 {
348         if (!subgroup_bus) {
349                 return;
350         }
351         
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 */
355         }
356
357         _session.remove_route (subgroup_bus);
358         subgroup_bus.reset ();
359 }