f31820e9244cb75c7ded816221acc9641347b0ba
[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 PBD;
41 using namespace std;
42
43 namespace ARDOUR {
44         namespace Properties {
45                 PropertyDescriptor<bool> relative;
46                 PropertyDescriptor<bool> active;
47                 PropertyDescriptor<bool> gain;
48                 PropertyDescriptor<bool> mute;
49                 PropertyDescriptor<bool> solo;
50                 PropertyDescriptor<bool> recenable;
51                 PropertyDescriptor<bool> select;
52                 PropertyDescriptor<bool> edit;
53         }       
54 }
55
56 void
57 RouteGroup::make_property_quarks ()
58 {
59         Properties::relative.id = g_quark_from_static_string (X_("relative"));
60         Properties::active.id = g_quark_from_static_string (X_("active"));
61         Properties::hidden.id = g_quark_from_static_string (X_("hidden"));
62         Properties::gain.id = g_quark_from_static_string (X_("gain"));
63         Properties::mute.id = g_quark_from_static_string (X_("mute"));
64         Properties::solo.id = g_quark_from_static_string (X_("solo"));
65         Properties::recenable.id = g_quark_from_static_string (X_("recenable"));
66         Properties::select.id = g_quark_from_static_string (X_("select"));
67         Properties::edit.id = g_quark_from_static_string (X_("edit"));
68 }
69
70 #define ROUTE_GROUP_DEFAULT_PROPERTIES  _relative (Properties::relative, false) \
71         , _active (Properties::active, false) \
72         , _hidden (Properties::hidden, false) \
73         , _gain (Properties::gain, false) \
74         , _mute (Properties::mute, false) \
75         , _solo (Properties::solo, false) \
76         , _recenable (Properties::recenable, false) \
77         , _select (Properties::select, false) \
78         , _edit (Properties::edit, false)
79
80 RouteGroup::RouteGroup (Session& s, const string &n)
81         : SessionObject (s, n)
82         , routes (new RouteList)
83         , ROUTE_GROUP_DEFAULT_PROPERTIES
84 {
85         _xml_node_name = X_("RegionGroup");
86
87         add_property (_relative);
88         add_property (_active);
89         add_property (_hidden);
90         add_property (_gain);
91         add_property (_mute);
92         add_property (_solo);
93         add_property (_recenable);
94         add_property (_select);
95         add_property (_edit);
96 }
97
98 RouteGroup::~RouteGroup ()
99 {
100         for (RouteList::iterator i = routes->begin(); i != routes->end();) {
101                 RouteList::iterator tmp = i;
102                 ++tmp;
103
104                 (*i)->leave_route_group ();
105                 
106                 i = tmp;
107         }
108 }
109
110 /** Add a route to a group.  Adding a route which is already in the group is allowed; nothing will happen.
111  *  @param r Route to add.
112  */
113 int
114 RouteGroup::add (boost::shared_ptr<Route> r)
115 {
116         if (find (routes->begin(), routes->end(), r) != routes->end()) {
117                 return 0;
118         }
119         
120         r->leave_route_group ();
121
122         routes->push_back (r);
123
124         r->join_route_group (this);
125         r->DropReferences.connect_same_thread (*this, boost::bind (&RouteGroup::remove_when_going_away, this, boost::weak_ptr<Route> (r)));
126         
127         _session.set_dirty ();
128         changed (); /* EMIT SIGNAL */
129         return 0;
130 }
131
132 void
133 RouteGroup::remove_when_going_away (boost::weak_ptr<Route> wr)
134 {
135         boost::shared_ptr<Route> r (wr.lock());
136
137         if (r) {
138                 remove (r);
139         }
140 }
141
142 int
143 RouteGroup::remove (boost::shared_ptr<Route> r)
144 {
145         RouteList::iterator i;
146
147         if ((i = find (routes->begin(), routes->end(), r)) != routes->end()) {
148                 r->leave_route_group ();
149                 routes->erase (i);
150                 _session.set_dirty ();
151                 changed (); /* EMIT SIGNAL */
152                 return 0;
153         }
154
155         return -1;
156 }
157
158
159 gain_t
160 RouteGroup::get_min_factor(gain_t factor)
161 {
162         gain_t g;
163
164         for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
165                 g = (*i)->amp()->gain();
166
167                 if ( (g+g*factor) >= 0.0f)
168                         continue;
169
170                 if ( g <= 0.0000003f )
171                         return 0.0f;
172
173                 factor = 0.0000003f/g - 1.0f;
174         }
175         return factor;
176 }
177
178 gain_t
179 RouteGroup::get_max_factor(gain_t factor)
180 {
181         gain_t g;
182
183         for (RouteList::iterator i = routes->begin(); i != routes->end(); i++) {
184                 g = (*i)->amp()->gain();
185
186                 // if the current factor woulnd't raise this route above maximum
187                 if ( (g+g*factor) <= 1.99526231f)
188                         continue;
189
190                 // if route gain is already at peak, return 0.0f factor
191             if (g>=1.99526231f)
192                         return 0.0f;
193
194                 // factor is calculated so that it would raise current route to max
195                 factor = 1.99526231f/g - 1.0f;
196         }
197
198         return factor;
199 }
200
201 XMLNode&
202 RouteGroup::get_state (void)
203 {
204         XMLNode *node = new XMLNode ("RouteGroup");
205         
206         add_properties (*node);
207
208         if (!routes->empty()) {
209                 stringstream str;
210                 
211                 for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
212                         str << (*i)->id () << ' ';
213                 }
214
215                 node->add_property ("routes", str.str());
216         }
217
218         return *node;
219 }
220
221 int
222 RouteGroup::set_state (const XMLNode& node, int version)
223 {
224         if (version < 3000) {
225                 return set_state_2X (node, version);
226         }
227
228         const XMLProperty *prop;
229
230         if ((prop = node.property ("routes")) != 0) {
231                 stringstream str (prop->value());
232                 vector<string> ids;
233                 split (str.str(), ids, ' ');
234                 
235                 for (vector<string>::iterator i = ids.begin(); i != ids.end(); ++i) {
236                         PBD::ID id (*i);
237                         boost::shared_ptr<Route> r = _session.route_by_id (id);
238                         
239                         if (r) {
240                                 add (r);
241                         } 
242                 }
243         }
244
245         return 0;
246 }
247
248 int
249 RouteGroup::set_state_2X (const XMLNode& node, int /*version*/)
250 {
251         set_properties (node);
252
253         if (node.name() == "MixGroup") {
254                 _gain = true;
255                 _mute = true;
256                 _solo = true;
257                 _recenable = true;
258                 _edit = false;
259         } else if (node.name() == "EditGroup") {
260                 _gain = false;
261                 _mute = false;
262                 _solo = false;
263                 _recenable = false;
264                 _edit = true;
265         }
266
267         return 0;
268 }
269
270 void
271 RouteGroup::set_gain (bool yn)
272 {
273         if (is_gain() == yn) {
274                 return;
275         }
276         _gain = yn;
277 }
278
279 void
280 RouteGroup::set_mute (bool yn)
281 {
282         if (is_mute() == yn) {
283                 return;
284         }
285         _mute = yn;
286 }
287
288 void
289 RouteGroup::set_solo (bool yn)
290 {
291         if (is_solo() == yn) {
292                 return;
293         }
294         _solo = yn;
295 }
296
297 void
298 RouteGroup::set_recenable (bool yn)
299 {
300         if (is_recenable() == yn) {
301                 return;
302         }
303         _recenable = yn;
304 }
305
306 void
307 RouteGroup::set_select (bool yn)
308 {
309         if (is_select() == yn) {
310                 return;
311         }
312         _select = yn;
313 }
314
315 void
316 RouteGroup::set_edit (bool yn)
317 {
318         if (is_edit() == yn) {
319                 return;
320         }
321         _edit = yn;
322 }
323
324 void
325 RouteGroup::set_active (bool yn, void *src)
326 {
327         if (is_active() == yn) {
328                 return;
329         }
330         _active = yn;
331         _session.set_dirty ();
332         FlagsChanged (src); /* EMIT SIGNAL */
333 }
334
335 void
336 RouteGroup::set_relative (bool yn, void *src)
337
338 {
339         if (is_relative() == yn) {
340                 return;
341         }
342         _relative = yn;
343         _session.set_dirty ();
344         FlagsChanged (src); /* EMIT SIGNAL */
345 }
346
347 void
348 RouteGroup::set_hidden (bool yn, void *src)
349 {
350         if (is_hidden() == yn) {
351                 return;
352         }
353         if (yn) {
354                 _hidden = true;
355                 if (Config->get_hiding_groups_deactivates_groups()) {
356                         _active = false;
357                 }
358         } else {
359                 _hidden = false;
360                 if (Config->get_hiding_groups_deactivates_groups()) {
361                         _active = true;
362                 }
363         }
364         _session.set_dirty ();
365         FlagsChanged (src); /* EMIT SIGNAL */
366 }
367
368 void
369 RouteGroup::audio_track_group (set<boost::shared_ptr<AudioTrack> >& ats)
370 {
371         for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
372                 boost::shared_ptr<AudioTrack> at = boost::dynamic_pointer_cast<AudioTrack>(*i);
373                 if (at) {
374                         ats.insert (at);
375                 }
376         }
377 }
378
379 void
380 RouteGroup::make_subgroup ()
381 {
382         RouteList rl;
383         uint32_t nin = 0;
384
385         /* since we don't do MIDI Busses yet, check quickly ... */
386
387         for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
388                 if ((*i)->output()->n_ports().n_midi() != 0) {
389                         PBD::info << _("You cannot subgroup MIDI tracks at this time") << endmsg;
390                         return;
391                 }
392         }
393
394         for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
395                 nin = max (nin, (*i)->output()->n_ports().n_audio());
396         }
397
398         try {
399                 /* use master bus etc. to determine default nouts */
400                 rl = _session.new_audio_route (false, nin, 2, 0, 1);
401         } catch (...) {
402                 return;
403         }
404
405         subgroup_bus = rl.front();
406         subgroup_bus->set_name (_name);
407
408         boost::shared_ptr<Bundle> bundle = subgroup_bus->input()->bundle ();
409
410         for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
411                 (*i)->output()->disconnect (this);
412                 (*i)->output()->connect_ports_to_bundle (bundle, this);
413         }
414 }
415
416 void
417 RouteGroup::destroy_subgroup ()
418 {
419         if (!subgroup_bus) {
420                 return;
421         }
422         
423         for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
424                 (*i)->output()->disconnect (this);
425                 /* XXX find a new bundle to connect to */
426         }
427
428         _session.remove_route (subgroup_bus);
429         subgroup_bus.reset ();
430 }
431
432 bool
433 RouteGroup::enabled_property (PBD::PropertyID prop)
434 {
435         if (Properties::relative.id == prop) {
436                 return is_relative();
437         } else if (Properties::active.id == prop) {
438                 return is_active();
439         } else if (Properties::hidden.id == prop) {
440                 return is_hidden();
441         } else if (Properties::gain.id == prop) {
442                 return is_gain();
443         } else if (Properties::mute.id == prop) {
444                 return is_mute();
445         } else if (Properties::solo.id == prop) {
446                 return is_solo();
447         } else if (Properties::recenable.id == prop) {
448                 return is_recenable();
449         } else if (Properties::select.id == prop) {
450                 return is_select();
451         } else if (Properties::edit.id == prop) {
452                 return is_edit();
453         }
454
455         return false;
456 }