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