Allow choice of direct or aux busses when subgrouping route groups. Fixes #3658.
[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 #include <inttypes.h>
21
22 #include <algorithm>
23
24
25 #include "pbd/error.h"
26 #include "pbd/enumwriter.h"
27 #include "pbd/strsplit.h"
28
29 #include "ardour/amp.h"
30 #include "ardour/debug.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.property_id = g_quark_from_static_string (X_("relative"));
60         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for relative = %1\n",    Properties::relative.property_id));
61         Properties::active.property_id = g_quark_from_static_string (X_("active"));
62         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for active = %1\n",      Properties::active.property_id));
63         Properties::hidden.property_id = g_quark_from_static_string (X_("hidden"));
64         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for hidden = %1\n",      Properties::hidden.property_id));
65         Properties::gain.property_id = g_quark_from_static_string (X_("gain"));
66         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for gain = %1\n",        Properties::gain.property_id));
67         Properties::mute.property_id = g_quark_from_static_string (X_("mute"));
68         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for mute = %1\n",        Properties::mute.property_id));
69         Properties::solo.property_id = g_quark_from_static_string (X_("solo"));
70         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for solo = %1\n",        Properties::solo.property_id));
71         Properties::recenable.property_id = g_quark_from_static_string (X_("recenable"));
72         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for recenable = %1\n",   Properties::recenable.property_id));
73         Properties::select.property_id = g_quark_from_static_string (X_("select"));
74         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for select = %1\n",      Properties::select.property_id));
75         Properties::edit.property_id = g_quark_from_static_string (X_("edit"));
76         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for edit = %1\n",        Properties::edit.property_id));
77 }
78
79 #define ROUTE_GROUP_DEFAULT_PROPERTIES  _relative (Properties::relative, false) \
80         , _active (Properties::active, false) \
81         , _hidden (Properties::hidden, false) \
82         , _gain (Properties::gain, false) \
83         , _mute (Properties::mute, false) \
84         , _solo (Properties::solo, false) \
85         , _recenable (Properties::recenable, false) \
86         , _select (Properties::select, false) \
87         , _edit (Properties::edit, false)
88
89 RouteGroup::RouteGroup (Session& s, const string &n)
90         : SessionObject (s, n)
91         , routes (new RouteList)
92         , ROUTE_GROUP_DEFAULT_PROPERTIES
93 {
94         _xml_node_name = X_("RouteGroup");
95
96         add_property (_relative);
97         add_property (_active);
98         add_property (_hidden);
99         add_property (_gain);
100         add_property (_mute);
101         add_property (_solo);
102         add_property (_recenable);
103         add_property (_select);
104         add_property (_edit);
105 }
106
107 RouteGroup::~RouteGroup ()
108 {
109         for (RouteList::iterator i = routes->begin(); i != routes->end();) {
110                 RouteList::iterator tmp = i;
111                 ++tmp;
112
113                 (*i)->leave_route_group ();
114                 
115                 i = tmp;
116         }
117 }
118
119 /** Add a route to a group.  Adding a route which is already in the group is allowed; nothing will happen.
120  *  @param r Route to add.
121  */
122 int
123 RouteGroup::add (boost::shared_ptr<Route> r)
124 {
125         if (find (routes->begin(), routes->end(), r) != routes->end()) {
126                 return 0;
127         }
128         
129         r->leave_route_group ();
130
131         routes->push_back (r);
132
133         r->join_route_group (this);
134         r->DropReferences.connect_same_thread (*this, boost::bind (&RouteGroup::remove_when_going_away, this, boost::weak_ptr<Route> (r)));
135         
136         _session.set_dirty ();
137         MembershipChanged (); /* EMIT SIGNAL */
138         return 0;
139 }
140
141 void
142 RouteGroup::remove_when_going_away (boost::weak_ptr<Route> wr)
143 {
144         boost::shared_ptr<Route> r (wr.lock());
145
146         if (r) {
147                 remove (r);
148         }
149 }
150
151 int
152 RouteGroup::remove (boost::shared_ptr<Route> r)
153 {
154         RouteList::iterator i;
155
156         if ((i = find (routes->begin(), routes->end(), r)) != routes->end()) {
157                 r->leave_route_group ();
158                 routes->erase (i);
159                 _session.set_dirty ();
160                 MembershipChanged (); /* EMIT SIGNAL */
161                 return 0;
162         }
163
164         return -1;
165 }
166
167
168 gain_t
169 RouteGroup::get_min_factor(gain_t factor)
170 {
171         gain_t g;
172
173         for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
174                 g = (*i)->amp()->gain();
175
176                 if ( (g+g*factor) >= 0.0f)
177                         continue;
178
179                 if ( g <= 0.0000003f )
180                         return 0.0f;
181
182                 factor = 0.0000003f/g - 1.0f;
183         }
184         return factor;
185 }
186
187 gain_t
188 RouteGroup::get_max_factor(gain_t factor)
189 {
190         gain_t g;
191
192         for (RouteList::iterator i = routes->begin(); i != routes->end(); i++) {
193                 g = (*i)->amp()->gain();
194
195                 // if the current factor woulnd't raise this route above maximum
196                 if ( (g+g*factor) <= 1.99526231f)
197                         continue;
198
199                 // if route gain is already at peak, return 0.0f factor
200             if (g>=1.99526231f)
201                         return 0.0f;
202
203                 // factor is calculated so that it would raise current route to max
204                 factor = 1.99526231f/g - 1.0f;
205         }
206
207         return factor;
208 }
209
210 XMLNode&
211 RouteGroup::get_state (void)
212 {
213         XMLNode *node = new XMLNode ("RouteGroup");
214         
215         add_properties (*node);
216
217         if (!routes->empty()) {
218                 stringstream str;
219                 
220                 for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
221                         str << (*i)->id () << ' ';
222                 }
223
224                 node->add_property ("routes", str.str());
225         }
226
227         return *node;
228 }
229
230 int
231 RouteGroup::set_state (const XMLNode& node, int version)
232 {
233         if (version < 3000) {
234                 return set_state_2X (node, version);
235         }
236
237         set_values (node);
238
239         const XMLProperty *prop;
240
241         if ((prop = node.property ("routes")) != 0) {
242                 stringstream str (prop->value());
243                 vector<string> ids;
244                 split (str.str(), ids, ' ');
245                 
246                 for (vector<string>::iterator i = ids.begin(); i != ids.end(); ++i) {
247                         PBD::ID id (*i);
248                         boost::shared_ptr<Route> r = _session.route_by_id (id);
249                         
250                         if (r) {
251                                 add (r);
252                         } 
253                 }
254         }
255
256         return 0;
257 }
258
259 int
260 RouteGroup::set_state_2X (const XMLNode& node, int /*version*/)
261 {
262         set_values (node);
263
264         if (node.name() == "MixGroup") {
265                 _gain = true;
266                 _mute = true;
267                 _solo = true;
268                 _recenable = true;
269                 _edit = false;
270         } else if (node.name() == "EditGroup") {
271                 _gain = false;
272                 _mute = false;
273                 _solo = false;
274                 _recenable = false;
275                 _edit = true;
276         }
277
278         return 0;
279 }
280
281 void
282 RouteGroup::set_gain (bool yn)
283 {
284         if (is_gain() == yn) {
285                 return;
286         }
287         _gain = yn;
288 }
289
290 void
291 RouteGroup::set_mute (bool yn)
292 {
293         if (is_mute() == yn) {
294                 return;
295         }
296         _mute = yn;
297 }
298
299 void
300 RouteGroup::set_solo (bool yn)
301 {
302         if (is_solo() == yn) {
303                 return;
304         }
305         _solo = yn;
306 }
307
308 void
309 RouteGroup::set_recenable (bool yn)
310 {
311         if (is_recenable() == yn) {
312                 return;
313         }
314         _recenable = yn;
315 }
316
317 void
318 RouteGroup::set_select (bool yn)
319 {
320         if (is_select() == yn) {
321                 return;
322         }
323         _select = yn;
324 }
325
326 void
327 RouteGroup::set_edit (bool yn)
328 {
329         if (is_edit() == yn) {
330                 return;
331         }
332         _edit = yn;
333 }
334
335 void
336 RouteGroup::set_active (bool yn, void* /*src*/)
337 {
338         if (is_active() == yn) {
339                 return;
340         }
341
342         _active = yn;
343         send_change (PropertyChange (Properties::active));
344                 
345         _session.set_dirty ();
346 }
347
348 void
349 RouteGroup::set_relative (bool yn, void* /*src*/)
350 {
351         if (is_relative() == yn) {
352                 return;
353         }
354         _relative = yn;
355         _session.set_dirty ();
356 }
357
358 void
359 RouteGroup::set_hidden (bool yn, void* /*src*/)
360 {
361         if (is_hidden() == yn) {
362                 return;
363         }
364         if (yn) {
365                 _hidden = true;
366                 if (Config->get_hiding_groups_deactivates_groups()) {
367                         _active = false;
368                 }
369         } else {
370                 _hidden = false;
371                 if (Config->get_hiding_groups_deactivates_groups()) {
372                         _active = true;
373                 }
374         }
375         _session.set_dirty ();
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 (bool aux, Placement placement)
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 (aux, nin, 2, 0, 1);
411         } catch (...) {
412                 return;
413         }
414
415         subgroup_bus = rl.front();
416         subgroup_bus->set_name (_name);
417
418         if (aux) {
419
420                 _session.add_internal_sends (subgroup_bus, placement, routes);
421
422         } else {
423
424                 boost::shared_ptr<Bundle> bundle = subgroup_bus->input()->bundle ();
425                 
426                 for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
427                         (*i)->output()->disconnect (this);
428                         (*i)->output()->connect_ports_to_bundle (bundle, this);
429                 }
430         }
431 }
432
433 void
434 RouteGroup::destroy_subgroup ()
435 {
436         if (!subgroup_bus) {
437                 return;
438         }
439         
440         for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
441                 (*i)->output()->disconnect (this);
442                 /* XXX find a new bundle to connect to */
443         }
444
445         _session.remove_route (subgroup_bus);
446         subgroup_bus.reset ();
447 }
448
449 bool
450 RouteGroup::enabled_property (PBD::PropertyID prop)
451 {
452         OwnedPropertyList::iterator i = _properties->find (prop);
453         if (i == _properties->end()) {
454                 return false;
455         }
456
457         return dynamic_cast<const PropertyTemplate<bool>* > (i->second)->val ();
458 }