fix issues with route group assignment to control master
[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 #include "pbd/debug.h"
28
29 #include "ardour/amp.h"
30 #include "ardour/audio_track.h"
31 #include "ardour/monitor_control.h"
32 #include "ardour/route.h"
33 #include "ardour/route_group.h"
34 #include "ardour/session.h"
35
36 #include "i18n.h"
37
38 using namespace ARDOUR;
39 using namespace PBD;
40 using namespace std;
41
42 namespace ARDOUR {
43         namespace Properties {
44                 PropertyDescriptor<bool> relative;
45                 PropertyDescriptor<bool> active;
46                 PropertyDescriptor<bool> gain;
47                 PropertyDescriptor<bool> mute;
48                 PropertyDescriptor<bool> solo;
49                 PropertyDescriptor<bool> recenable;
50                 PropertyDescriptor<bool> select;
51                 PropertyDescriptor<bool> route_active;
52                 PropertyDescriptor<bool> color;
53                 PropertyDescriptor<bool> monitoring;
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::route_active.property_id = g_quark_from_static_string (X_("route-active"));
77         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for route-active = %1\n", Properties::route_active.property_id));
78         Properties::color.property_id = g_quark_from_static_string (X_("color"));
79         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for color = %1\n",       Properties::color.property_id));
80         Properties::monitoring.property_id = g_quark_from_static_string (X_("monitoring"));
81         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for monitoring = %1\n",       Properties::monitoring.property_id));
82 }
83
84 #define ROUTE_GROUP_DEFAULT_PROPERTIES  _relative (Properties::relative, true) \
85         , _active (Properties::active, true) \
86         , _hidden (Properties::hidden, false) \
87         , _gain (Properties::gain, true) \
88         , _mute (Properties::mute, true) \
89         , _solo (Properties::solo, true) \
90         , _recenable (Properties::recenable, true) \
91         , _select (Properties::select, true) \
92         , _route_active (Properties::route_active, true) \
93         , _color (Properties::color, true) \
94         , _monitoring (Properties::monitoring, true)
95
96 RouteGroup::RouteGroup (Session& s, const string &n)
97         : SessionObject (s, n)
98         , routes (new RouteList)
99         , ROUTE_GROUP_DEFAULT_PROPERTIES
100         , _solo_group (new ControlGroup (SoloAutomation))
101         , _mute_group (new ControlGroup (MuteAutomation))
102         , _rec_enable_group (new ControlGroup (RecEnableAutomation))
103         , _gain_group (new ControlGroup (GainAutomation))
104         , _monitoring_group (new ControlGroup (MonitoringAutomation))
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 (_route_active);
117         add_property (_color);
118         add_property (_monitoring);
119 }
120
121 RouteGroup::~RouteGroup ()
122 {
123         _solo_group->clear ();
124         _mute_group->clear ();
125         _gain_group->clear ();
126         _rec_enable_group->clear ();
127         _monitoring_group->clear ();
128
129         for (RouteList::iterator i = routes->begin(); i != routes->end();) {
130                 RouteList::iterator tmp = i;
131                 ++tmp;
132
133                 (*i)->set_route_group (0);
134
135                 i = tmp;
136         }
137 }
138
139 /** Add a route to a group.  Adding a route which is already in the group is allowed; nothing will happen.
140  *  @param r Route to add.
141  */
142 int
143 RouteGroup::add (boost::shared_ptr<Route> r)
144 {
145         if (find (routes->begin(), routes->end(), r) != routes->end()) {
146                 return 0;
147         }
148
149         if (r->route_group()) {
150                 r->route_group()->remove (r);
151         }
152
153         routes->push_back (r);
154
155         _solo_group->add_control (r->solo_control());
156         _mute_group->add_control (r->mute_control());
157         _gain_group->add_control (r->gain_control());
158         boost::shared_ptr<Track> trk = boost::dynamic_pointer_cast<Track> (r);
159         if (trk) {
160                 _rec_enable_group->add_control (trk->rec_enable_control());
161                 _monitoring_group->add_control (trk->monitoring_control());
162         }
163
164         r->set_route_group (this);
165         r->DropReferences.connect_same_thread (*this, boost::bind (&RouteGroup::remove_when_going_away, this, boost::weak_ptr<Route> (r)));
166
167         _session.set_dirty ();
168         RouteAdded (this, boost::weak_ptr<Route> (r)); /* EMIT SIGNAL */
169         return 0;
170 }
171
172 void
173 RouteGroup::remove_when_going_away (boost::weak_ptr<Route> wr)
174 {
175         boost::shared_ptr<Route> r (wr.lock());
176
177         if (r) {
178                 remove (r);
179         }
180 }
181
182 int
183 RouteGroup::remove (boost::shared_ptr<Route> r)
184 {
185         RouteList::iterator i;
186
187         if ((i = find (routes->begin(), routes->end(), r)) != routes->end()) {
188                 r->set_route_group (0);
189                 _solo_group->remove_control (r->solo_control());
190                 _mute_group->remove_control (r->mute_control());
191                 _gain_group->remove_control (r->gain_control());
192                 boost::shared_ptr<Track> trk = boost::dynamic_pointer_cast<Track> (r);
193                 if (trk) {
194                         _rec_enable_group->remove_control (trk->rec_enable_control());
195                         _monitoring_group->remove_control (trk->monitoring_control());
196                 }
197                 routes->erase (i);
198                 _session.set_dirty ();
199                 RouteRemoved (this, boost::weak_ptr<Route> (r)); /* EMIT SIGNAL */
200                 return 0;
201         }
202
203         return -1;
204 }
205
206
207 XMLNode&
208 RouteGroup::get_state ()
209 {
210         XMLNode *node = new XMLNode ("RouteGroup");
211
212         char buf[64];
213         id().print (buf, sizeof (buf));
214         node->add_property ("id", buf);
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         XMLProperty const * prop;
239
240         set_id (node);
241         set_values (node);
242
243         if ((prop = node.property ("routes")) != 0) {
244                 stringstream str (prop->value());
245                 vector<string> ids;
246                 split (str.str(), ids, ' ');
247
248                 for (vector<string>::iterator i = ids.begin(); i != ids.end(); ++i) {
249                         PBD::ID id (*i);
250                         boost::shared_ptr<Route> r = _session.route_by_id (id);
251
252                         if (r) {
253                                 add (r);
254                         }
255                 }
256         }
257
258         push_to_groups ();
259
260         return 0;
261 }
262
263 int
264 RouteGroup::set_state_2X (const XMLNode& node, int /*version*/)
265 {
266         set_values (node);
267
268         if (node.name() == "MixGroup") {
269                 _gain = true;
270                 _mute = true;
271                 _solo = true;
272                 _recenable = true;
273                 _route_active = true;
274                 _color = false;
275         } else if (node.name() == "EditGroup") {
276                 _gain = false;
277                 _mute = false;
278                 _solo = false;
279                 _recenable = false;
280                 _route_active = false;
281                 _color = false;
282         }
283
284         push_to_groups ();
285
286         return 0;
287 }
288
289 void
290 RouteGroup::set_gain (bool yn)
291 {
292         if (is_gain() == yn) {
293                 return;
294         }
295         _gain = yn;
296         _gain_group->set_active (yn);
297
298         if (routes->empty() || !routes->front()->slaved()) {
299                 pre_master_gain = yn;
300         }
301
302         send_change (PropertyChange (Properties::gain));
303 }
304
305 void
306 RouteGroup::set_mute (bool yn)
307 {
308         if (is_mute() == yn) {
309                 return;
310         }
311         _mute = yn;
312         _mute_group->set_active (yn);
313
314         if (routes->empty() || !routes->front()->slaved()) {
315                 pre_master_mute = yn;
316         }
317
318         send_change (PropertyChange (Properties::mute));
319 }
320
321 void
322 RouteGroup::set_solo (bool yn)
323 {
324         if (is_solo() == yn) {
325                 return;
326         }
327         _solo = yn;
328         _solo_group->set_active (yn);
329
330         if (routes->empty() || !routes->front()->slaved()) {
331                 pre_master_solo = yn;
332         }
333
334         send_change (PropertyChange (Properties::solo));
335 }
336
337 void
338 RouteGroup::set_recenable (bool yn)
339 {
340         if (is_recenable() == yn) {
341                 return;
342         }
343         _recenable = yn;
344         _rec_enable_group->set_active (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_route_active (bool yn)
360 {
361         if (is_route_active() == yn) {
362                 return;
363         }
364         _route_active = yn;
365         send_change (PropertyChange (Properties::route_active));
366 }
367
368 void
369 RouteGroup::set_color (bool yn)
370 {
371         if (is_color() == yn) {
372                 return;
373         }
374         _color = yn;
375
376         send_change (PropertyChange (Properties::color));
377
378         /* This is a bit of a hack, but this might change
379            our route's effective color, so emit gui_changed
380            for our routes.
381         */
382
383         for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
384                 (*i)->gui_changed (X_("color"), this);
385         }
386 }
387
388 void
389 RouteGroup::set_monitoring (bool yn)
390 {
391         if (is_monitoring() == yn) {
392                 return;
393         }
394
395         _monitoring = yn;
396         _monitoring_group->set_active (yn);
397
398         send_change (PropertyChange (Properties::monitoring));
399
400         _session.set_dirty ();
401 }
402
403 void
404 RouteGroup::set_active (bool yn, void* /*src*/)
405 {
406         if (is_active() == yn) {
407                 return;
408         }
409
410         _active = yn;
411         send_change (PropertyChange (Properties::active));
412         _session.set_dirty ();
413 }
414
415 void
416 RouteGroup::set_relative (bool yn, void* /*src*/)
417 {
418         if (is_relative() == yn) {
419                 return;
420         }
421         _relative = yn;
422         send_change (PropertyChange (Properties::relative));
423         _session.set_dirty ();
424 }
425
426 void
427 RouteGroup::set_hidden (bool yn, void* /*src*/)
428 {
429         if (is_hidden() == yn) {
430                 return;
431         }
432
433         if (yn) {
434                 _hidden = true;
435                 if (Config->get_hiding_groups_deactivates_groups()) {
436                         _active = false;
437                 }
438         } else {
439                 _hidden = false;
440                 if (Config->get_hiding_groups_deactivates_groups()) {
441                         _active = true;
442                 }
443         }
444
445         send_change (Properties::hidden);
446
447         _session.set_dirty ();
448 }
449
450 void
451 RouteGroup::audio_track_group (set<boost::shared_ptr<AudioTrack> >& ats)
452 {
453         for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
454                 boost::shared_ptr<AudioTrack> at = boost::dynamic_pointer_cast<AudioTrack>(*i);
455                 if (at) {
456                         ats.insert (at);
457                 }
458         }
459 }
460
461 void
462 RouteGroup::make_subgroup (bool aux, Placement placement)
463 {
464         RouteList rl;
465         uint32_t nin = 0;
466
467         /* since we don't do MIDI Busses yet, check quickly ... */
468
469         for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
470                 if ((*i)->output()->n_ports().n_midi() != 0) {
471                         PBD::warning << _("You cannot subgroup MIDI tracks at this time") << endmsg;
472                         return;
473                 }
474         }
475
476         for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
477                 if (!aux && nin != 0 && nin != (*i)->output()->n_ports().n_audio()) {
478                         PBD::warning << _("You cannot subgroup tracks with different number of outputs at this time.") << endmsg;
479                         return;
480                 }
481                 nin = max (nin, (*i)->output()->n_ports().n_audio());
482         }
483
484         try {
485                 /* use master bus etc. to determine default nouts.
486                  *
487                  * (since tracks can't have fewer outs than ins,
488                  * "nin" currently defines the number of outpus if nin > 2)
489                  */
490                 rl = _session.new_audio_route (nin, 2, 0, 1, string(), PresentationInfo::AudioBus, PresentationInfo::max_order);
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, false, 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::has_subgroup() const
531 {
532         return subgroup_bus != 0;
533 }
534
535 bool
536 RouteGroup::enabled_property (PBD::PropertyID prop)
537 {
538         OwnedPropertyList::iterator i = _properties->find (prop);
539         if (i == _properties->end()) {
540                 return false;
541         }
542
543         return dynamic_cast<const PropertyTemplate<bool>* > (i->second)->val ();
544 }
545
546 void
547 RouteGroup::post_set (PBD::PropertyChange const &)
548 {
549         push_to_groups ();
550 }
551
552 void
553 RouteGroup::push_to_groups ()
554 {
555         _gain_group->set_active (_gain);
556         _solo_group->set_active (_solo);
557         _mute_group->set_active (_mute);
558         _rec_enable_group->set_active (_recenable);
559         _monitoring_group->set_active (_monitoring);
560 }
561
562 void
563 RouteGroup::assign_master (boost::shared_ptr<VCA> master)
564 {
565         if (!routes || routes->empty()) {
566                 return;
567         }
568
569         boost::shared_ptr<Route> front = routes->front ();
570
571         if (!front) {
572                 return;
573         }
574
575         if (front->slaved_to (master)) {
576                 return;
577         }
578
579         bool cancel_master_controls = false;
580
581         if (!front->slaved()) {
582                 pre_master_gain = is_gain ();
583                 pre_master_solo = is_solo ();
584                 pre_master_mute = is_mute ();
585                 cancel_master_controls = true;
586         }
587
588         for (RouteList::iterator r = routes->begin(); r != routes->end(); ++r) {
589                 (*r)->assign (master);
590         }
591
592         if (cancel_master_controls) {
593                 set_gain (false);
594                 set_solo (false);
595                 set_mute (false);
596         }
597 }
598
599 void
600 RouteGroup::unassign_master (boost::shared_ptr<VCA> master)
601 {
602         if (!routes || routes->empty()) {
603                 return;
604         }
605
606         boost::shared_ptr<Route> front = routes->front ();
607
608         if (!front) {
609                 return;
610         }
611
612         if (!front->slaved_to (master)) {
613                 return;
614         }
615
616         for (RouteList::iterator r = routes->begin(); r != routes->end(); ++r) {
617                 (*r)->unassign (master);
618         }
619
620         if (!front->slaved()) {
621                 set_gain (pre_master_gain);
622                 set_solo (pre_master_solo);
623                 set_mute (pre_master_mute);
624         }
625 }
626
627 bool
628 RouteGroup::slaved () const
629 {
630         if (!routes || routes->empty()) {
631                 return false;
632         }
633
634         return routes->front()->slaved ();
635 }