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