try not thinning when loading old-school automation lists
[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 #include "pbd/error.h"
25 #include "pbd/enumwriter.h"
26 #include "pbd/strsplit.h"
27
28 #include "ardour/amp.h"
29 #include "ardour/audio_track.h"
30 #include "ardour/route.h"
31 #include "ardour/route_group.h"
32 #include "ardour/session.h"
33
34 #include "i18n.h"
35
36 using namespace ARDOUR;
37 using namespace PBD;
38 using namespace std;
39
40 namespace ARDOUR {
41         namespace Properties {
42                 PropertyDescriptor<bool> relative;
43                 PropertyDescriptor<bool> active;
44                 PropertyDescriptor<bool> gain;
45                 PropertyDescriptor<bool> mute;
46                 PropertyDescriptor<bool> solo;
47                 PropertyDescriptor<bool> recenable;
48                 PropertyDescriptor<bool> select;
49                 PropertyDescriptor<bool> edit;
50                 PropertyDescriptor<bool> route_active;
51                 PropertyDescriptor<bool> color;
52                 PropertyDescriptor<bool> monitoring;
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         Properties::route_active.property_id = g_quark_from_static_string (X_("route-active"));
78         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for route-active = %1\n", Properties::route_active.property_id));
79         Properties::color.property_id = g_quark_from_static_string (X_("color"));
80         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for color = %1\n",       Properties::color.property_id));
81         Properties::monitoring.property_id = g_quark_from_static_string (X_("monitoring"));
82         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for monitoring = %1\n",       Properties::monitoring.property_id));
83 }
84
85 #define ROUTE_GROUP_DEFAULT_PROPERTIES  _relative (Properties::relative, false) \
86         , _active (Properties::active, false) \
87         , _hidden (Properties::hidden, false) \
88         , _gain (Properties::gain, false) \
89         , _mute (Properties::mute, false) \
90         , _solo (Properties::solo, false) \
91         , _recenable (Properties::recenable, false) \
92         , _select (Properties::select, false) \
93         , _edit (Properties::edit, false) \
94         , _route_active (Properties::route_active, false) \
95         , _color (Properties::color, false) \
96         , _monitoring (Properties::monitoring, false)
97
98 RouteGroup::RouteGroup (Session& s, const string &n)
99         : SessionObject (s, n)
100         , routes (new RouteList)
101         , ROUTE_GROUP_DEFAULT_PROPERTIES
102 {
103         _xml_node_name = X_("RouteGroup");
104
105         add_property (_relative);
106         add_property (_active);
107         add_property (_hidden);
108         add_property (_gain);
109         add_property (_mute);
110         add_property (_solo);
111         add_property (_recenable);
112         add_property (_select);
113         add_property (_edit);
114         add_property (_route_active);
115         add_property (_color);
116         add_property (_monitoring);
117 }
118
119 RouteGroup::~RouteGroup ()
120 {
121         for (RouteList::iterator i = routes->begin(); i != routes->end();) {
122                 RouteList::iterator tmp = i;
123                 ++tmp;
124
125                 (*i)->set_route_group (0);
126
127                 i = tmp;
128         }
129 }
130
131 /** Add a route to a group.  Adding a route which is already in the group is allowed; nothing will happen.
132  *  @param r Route to add.
133  */
134 int
135 RouteGroup::add (boost::shared_ptr<Route> r)
136 {
137         if (find (routes->begin(), routes->end(), r) != routes->end()) {
138                 return 0;
139         }
140
141         if (r->route_group()) {
142                 r->route_group()->remove (r);
143         }
144         
145         routes->push_back (r);
146
147         r->set_route_group (this);
148         r->DropReferences.connect_same_thread (*this, boost::bind (&RouteGroup::remove_when_going_away, this, boost::weak_ptr<Route> (r)));
149
150         _session.set_dirty ();
151         RouteAdded (this, boost::weak_ptr<Route> (r)); /* EMIT SIGNAL */
152         return 0;
153 }
154
155 void
156 RouteGroup::remove_when_going_away (boost::weak_ptr<Route> wr)
157 {
158         boost::shared_ptr<Route> r (wr.lock());
159
160         if (r) {
161                 remove (r);
162         }
163 }
164
165 int
166 RouteGroup::remove (boost::shared_ptr<Route> r)
167 {
168         RouteList::iterator i;
169
170         if ((i = find (routes->begin(), routes->end(), r)) != routes->end()) {
171                 r->set_route_group (0);
172                 routes->erase (i);
173                 _session.set_dirty ();
174                 RouteRemoved (this, boost::weak_ptr<Route> (r)); /* EMIT SIGNAL */
175                 return 0;
176         }
177
178         return -1;
179 }
180
181
182 gain_t
183 RouteGroup::get_min_factor (gain_t factor)
184 {
185         for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
186                 gain_t const g = (*i)->amp()->gain();
187
188                 if ((g + g * factor) >= 0.0f) {
189                         continue;
190                 }
191
192                 if (g <= 0.0000003f) {
193                         return 0.0f;
194                 }
195
196                 factor = 0.0000003f / g - 1.0f;
197         }
198         
199         return factor;
200 }
201
202 gain_t
203 RouteGroup::get_max_factor (gain_t factor)
204 {
205         for (RouteList::iterator i = routes->begin(); i != routes->end(); i++) {
206                 gain_t const g = (*i)->amp()->gain();
207
208                 // if the current factor woulnd't raise this route above maximum
209                 if ((g + g * factor) <= 1.99526231f) {
210                         continue;
211                 }
212
213                 // if route gain is already at peak, return 0.0f factor
214                 if (g >= 1.99526231f) {
215                         return 0.0f;
216                 }
217
218                 // factor is calculated so that it would raise current route to max
219                 factor = 1.99526231f / g - 1.0f;
220         }
221
222         return factor;
223 }
224
225 XMLNode&
226 RouteGroup::get_state ()
227 {
228         XMLNode *node = new XMLNode ("RouteGroup");
229
230         char buf[64];
231         id().print (buf, sizeof (buf));
232         node->add_property ("id", buf);
233
234         add_properties (*node);
235
236         if (!routes->empty()) {
237                 stringstream str;
238
239                 for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
240                         str << (*i)->id () << ' ';
241                 }
242
243                 node->add_property ("routes", str.str());
244         }
245
246         return *node;
247 }
248
249 int
250 RouteGroup::set_state (const XMLNode& node, int version)
251 {
252         if (version < 3000) {
253                 return set_state_2X (node, version);
254         }
255
256         const XMLProperty *prop;
257
258         set_id (node);
259         set_values (node);
260
261         if ((prop = node.property ("routes")) != 0) {
262                 stringstream str (prop->value());
263                 vector<string> ids;
264                 split (str.str(), ids, ' ');
265
266                 for (vector<string>::iterator i = ids.begin(); i != ids.end(); ++i) {
267                         PBD::ID id (*i);
268                         boost::shared_ptr<Route> r = _session.route_by_id (id);
269
270                         if (r) {
271                                 add (r);
272                         }
273                 }
274         }
275
276         return 0;
277 }
278
279 int
280 RouteGroup::set_state_2X (const XMLNode& node, int /*version*/)
281 {
282         set_values (node);
283
284         if (node.name() == "MixGroup") {
285                 _gain = true;
286                 _mute = true;
287                 _solo = true;
288                 _recenable = true;
289                 _edit = false;
290                 _route_active = true;
291                 _color = false;
292         } else if (node.name() == "EditGroup") {
293                 _gain = false;
294                 _mute = false;
295                 _solo = false;
296                 _recenable = false;
297                 _edit = true;
298                 _route_active = false;
299                 _color = false;
300         }
301
302         return 0;
303 }
304
305 void
306 RouteGroup::set_gain (bool yn)
307 {
308         if (is_gain() == yn) {
309                 return;
310         }
311         _gain = yn;
312         send_change (PropertyChange (Properties::gain));
313 }
314
315 void
316 RouteGroup::set_mute (bool yn)
317 {
318         if (is_mute() == yn) {
319                 return;
320         }
321         _mute = yn;
322         send_change (PropertyChange (Properties::mute));
323 }
324
325 void
326 RouteGroup::set_solo (bool yn)
327 {
328         if (is_solo() == yn) {
329                 return;
330         }
331         _solo = yn;
332         send_change (PropertyChange (Properties::solo));
333 }
334
335 void
336 RouteGroup::set_recenable (bool yn)
337 {
338         if (is_recenable() == yn) {
339                 return;
340         }
341         _recenable = yn;
342         send_change (PropertyChange (Properties::recenable));
343 }
344
345 void
346 RouteGroup::set_select (bool yn)
347 {
348         if (is_select() == yn) {
349                 return;
350         }
351         _select = yn;
352         send_change (PropertyChange (Properties::select));
353 }
354
355 void
356 RouteGroup::set_edit (bool yn)
357 {
358         if (is_edit() == yn) {
359                 return;
360         }
361         _edit = yn;
362         send_change (PropertyChange (Properties::edit));
363 }
364
365 void
366 RouteGroup::set_route_active (bool yn)
367 {
368         if (is_route_active() == yn) {
369                 return;
370         }
371         _route_active = yn;
372         send_change (PropertyChange (Properties::route_active));
373 }
374
375 void
376 RouteGroup::set_color (bool yn)
377 {
378         if (is_color() == yn) {
379                 return;
380         }
381         _color = yn;
382
383         send_change (PropertyChange (Properties::color));
384
385         /* This is a bit of a hack, but this might change
386            our route's effective color, so emit gui_changed
387            for our routes.
388         */
389
390         for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
391                 (*i)->gui_changed (X_("color"), this);
392         }
393 }
394
395 void
396 RouteGroup::set_monitoring (bool yn) 
397 {
398         if (is_monitoring() == yn) {
399                 return;
400         }
401
402         _monitoring = yn;
403         send_change (PropertyChange (Properties::monitoring));
404
405         _session.set_dirty ();
406 }
407
408 void
409 RouteGroup::set_active (bool yn, void* /*src*/)
410 {
411         if (is_active() == yn) {
412                 return;
413         }
414
415         _active = yn;
416         send_change (PropertyChange (Properties::active));
417         _session.set_dirty ();
418 }
419
420 void
421 RouteGroup::set_relative (bool yn, void* /*src*/)
422 {
423         if (is_relative() == yn) {
424                 return;
425         }
426         _relative = yn;
427         send_change (PropertyChange (Properties::relative));
428         _session.set_dirty ();
429 }
430
431 void
432 RouteGroup::set_hidden (bool yn, void* /*src*/)
433 {
434         if (is_hidden() == yn) {
435                 return;
436         }
437
438         if (yn) {
439                 _hidden = true;
440                 if (Config->get_hiding_groups_deactivates_groups()) {
441                         _active = false;
442                 }
443         } else {
444                 _hidden = false;
445                 if (Config->get_hiding_groups_deactivates_groups()) {
446                         _active = true;
447                 }
448         }
449
450         send_change (Properties::hidden);
451
452         _session.set_dirty ();
453 }
454
455 void
456 RouteGroup::audio_track_group (set<boost::shared_ptr<AudioTrack> >& ats)
457 {
458         for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
459                 boost::shared_ptr<AudioTrack> at = boost::dynamic_pointer_cast<AudioTrack>(*i);
460                 if (at) {
461                         ats.insert (at);
462                 }
463         }
464 }
465
466 void
467 RouteGroup::make_subgroup (bool aux, Placement placement)
468 {
469         RouteList rl;
470         uint32_t nin = 0;
471
472         /* since we don't do MIDI Busses yet, check quickly ... */
473
474         for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
475                 if ((*i)->output()->n_ports().n_midi() != 0) {
476                         PBD::info << _("You cannot subgroup MIDI tracks at this time") << endmsg;
477                         return;
478                 }
479         }
480
481         for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
482                 nin = max (nin, (*i)->output()->n_ports().n_audio());
483         }
484
485         try {
486                 /* use master bus etc. to determine default nouts */
487                 rl = _session.new_audio_route (nin, 2, 0, 1);
488         } catch (...) {
489                 return;
490         }
491
492         subgroup_bus = rl.front();
493         subgroup_bus->set_name (_name);
494
495         if (aux) {
496
497                 _session.add_internal_sends (subgroup_bus, placement, routes);
498
499         } else {
500
501                 boost::shared_ptr<Bundle> bundle = subgroup_bus->input()->bundle ();
502
503                 for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
504                         (*i)->output()->disconnect (this);
505                         (*i)->output()->connect_ports_to_bundle (bundle, this);
506                 }
507         }
508 }
509
510 void
511 RouteGroup::destroy_subgroup ()
512 {
513         if (!subgroup_bus) {
514                 return;
515         }
516
517         for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
518                 (*i)->output()->disconnect (this);
519                 /* XXX find a new bundle to connect to */
520         }
521
522         _session.remove_route (subgroup_bus);
523         subgroup_bus.reset ();
524 }
525
526 bool
527 RouteGroup::enabled_property (PBD::PropertyID prop)
528 {
529         OwnedPropertyList::iterator i = _properties->find (prop);
530         if (i == _properties->end()) {
531                 return false;
532         }
533
534         return dynamic_cast<const PropertyTemplate<bool>* > (i->second)->val ();
535 }