Move RouteGroup color into libardour.
[ardour.git] / libs / ardour / route_group.cc
1 /*
2     Copyright (C) 2000-2016 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/types_convert.h"
28 #include "pbd/debug.h"
29
30 #include "ardour/amp.h"
31 #include "ardour/audio_track.h"
32 #include "ardour/debug.h"
33 #include "ardour/monitor_control.h"
34 #include "ardour/route.h"
35 #include "ardour/route_group.h"
36 #include "ardour/session.h"
37 #include "ardour/vca.h"
38 #include "ardour/vca_manager.h"
39
40 #include "pbd/i18n.h"
41
42 using namespace ARDOUR;
43 using namespace PBD;
44 using namespace std;
45
46 namespace ARDOUR {
47         namespace Properties {
48                 PropertyDescriptor<bool> active;
49                 PropertyDescriptor<bool> group_relative;
50                 PropertyDescriptor<bool> group_gain;
51                 PropertyDescriptor<bool> group_mute;
52                 PropertyDescriptor<bool> group_solo;
53                 PropertyDescriptor<bool> group_recenable;
54                 PropertyDescriptor<bool> group_select;
55                 PropertyDescriptor<bool> group_route_active;
56                 PropertyDescriptor<bool> group_color;
57                 PropertyDescriptor<bool> group_monitoring;
58                 PropertyDescriptor<int32_t> group_master_number;
59         }
60 }
61
62 void
63 RouteGroup::make_property_quarks ()
64 {
65         Properties::active.property_id = g_quark_from_static_string (X_("active"));
66         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for active = %1\n",      Properties::active.property_id));
67
68         Properties::group_relative.property_id = g_quark_from_static_string (X_("relative"));
69         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for relative = %1\n",    Properties::group_relative.property_id));
70         Properties::group_gain.property_id = g_quark_from_static_string (X_("gain"));
71         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for gain = %1\n",        Properties::group_gain.property_id));
72         Properties::group_mute.property_id = g_quark_from_static_string (X_("mute"));
73         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for mute = %1\n",        Properties::group_mute.property_id));
74         Properties::group_solo.property_id = g_quark_from_static_string (X_("solo"));
75         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for solo = %1\n",        Properties::group_solo.property_id));
76         Properties::group_recenable.property_id = g_quark_from_static_string (X_("recenable"));
77         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for recenable = %1\n",   Properties::group_recenable.property_id));
78         Properties::group_select.property_id = g_quark_from_static_string (X_("select"));
79         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for select = %1\n",      Properties::group_select.property_id));
80         Properties::group_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::group_route_active.property_id));
82         Properties::group_color.property_id = g_quark_from_static_string (X_("color"));
83         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for color = %1\n", Properties::group_color.property_id));
84         Properties::group_monitoring.property_id = g_quark_from_static_string (X_("monitoring"));
85         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for monitoring = %1\n", Properties::group_monitoring.property_id));
86         Properties::group_master_number.property_id = g_quark_from_static_string (X_("group-master-number"));
87         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for group-master-number = %1\n", Properties::group_master_number.property_id));
88 }
89
90 #define ROUTE_GROUP_DEFAULT_PROPERTIES  _relative (Properties::group_relative, true) \
91         , _active (Properties::active, true) \
92         , _hidden (Properties::hidden, false) \
93         , _gain (Properties::group_gain, true) \
94         , _mute (Properties::group_mute, true) \
95         , _solo (Properties::group_solo, true) \
96         , _recenable (Properties::group_recenable, true) \
97         , _select (Properties::group_select, true) \
98         , _route_active (Properties::group_route_active, true) \
99         , _color (Properties::group_color, true) \
100         , _monitoring (Properties::group_monitoring, true) \
101         , _group_master_number (Properties::group_master_number, -1)
102
103 RouteGroup::RouteGroup (Session& s, const string &n)
104         : SessionObject (s, n)
105         , routes (new RouteList)
106         , ROUTE_GROUP_DEFAULT_PROPERTIES
107         , _solo_group (new ControlGroup (SoloAutomation))
108         , _mute_group (new ControlGroup (MuteAutomation))
109         , _rec_enable_group (new ControlGroup (RecEnableAutomation))
110         , _gain_group (new GainControlGroup ())
111         , _monitoring_group (new ControlGroup (MonitoringAutomation))
112         , _rgba (0)
113 {
114         _xml_node_name = X_("RouteGroup");
115
116         add_property (_relative);
117         add_property (_active);
118         add_property (_hidden);
119         add_property (_gain);
120         add_property (_mute);
121         add_property (_solo);
122         add_property (_recenable);
123         add_property (_select);
124         add_property (_route_active);
125         add_property (_color);
126         add_property (_monitoring);
127         add_property (_group_master_number);
128 }
129
130 RouteGroup::~RouteGroup ()
131 {
132         _solo_group->clear ();
133         _mute_group->clear ();
134         _gain_group->clear ();
135         _rec_enable_group->clear ();
136         _monitoring_group->clear ();
137
138         boost::shared_ptr<VCA> vca (group_master.lock());
139
140         for (RouteList::iterator i = routes->begin(); i != routes->end();) {
141                 RouteList::iterator tmp = i;
142                 ++tmp;
143
144                 (*i)->set_route_group (0);
145
146                 if (vca) {
147                         (*i)->unassign (vca);
148                 }
149
150                 i = tmp;
151         }
152 }
153
154 /** Add a route to a group.  Adding a route which is already in the group is allowed; nothing will happen.
155  *  @param r Route to add.
156  */
157 int
158 RouteGroup::add (boost::shared_ptr<Route> r)
159 {
160         if (r->is_master()) {
161                 return 0;
162         }
163
164         if (find (routes->begin(), routes->end(), r) != routes->end()) {
165                 return 0;
166         }
167
168         if (r->route_group()) {
169                 r->route_group()->remove (r);
170         }
171
172         routes->push_back (r);
173
174         _solo_group->add_control (r->solo_control());
175         _mute_group->add_control (r->mute_control());
176         _gain_group->add_control (r->gain_control());
177         boost::shared_ptr<Track> trk = boost::dynamic_pointer_cast<Track> (r);
178         if (trk) {
179                 _rec_enable_group->add_control (trk->rec_enable_control());
180                 _monitoring_group->add_control (trk->monitoring_control());
181         }
182
183         r->set_route_group (this);
184         r->DropReferences.connect_same_thread (*this, boost::bind (&RouteGroup::remove_when_going_away, this, boost::weak_ptr<Route> (r)));
185
186         boost::shared_ptr<VCA> vca (group_master.lock());
187
188         if (vca) {
189                 r->assign  (vca, false);
190         }
191
192         _session.set_dirty ();
193         RouteAdded (this, boost::weak_ptr<Route> (r)); /* EMIT SIGNAL */
194         return 0;
195 }
196
197 void
198 RouteGroup::remove_when_going_away (boost::weak_ptr<Route> wr)
199 {
200         boost::shared_ptr<Route> r (wr.lock());
201
202         if (r) {
203                 remove (r);
204         }
205 }
206
207 int
208 RouteGroup::remove (boost::shared_ptr<Route> r)
209 {
210         RouteList::iterator i;
211
212         if ((i = find (routes->begin(), routes->end(), r)) != routes->end()) {
213                 r->set_route_group (0);
214
215                 boost::shared_ptr<VCA> vca = group_master.lock();
216
217                 if (vca) {
218                         r->unassign (vca);
219                 }
220
221                 _solo_group->remove_control (r->solo_control());
222                 _mute_group->remove_control (r->mute_control());
223                 _gain_group->remove_control (r->gain_control());
224                 boost::shared_ptr<Track> trk = boost::dynamic_pointer_cast<Track> (r);
225                 if (trk) {
226                         _rec_enable_group->remove_control (trk->rec_enable_control());
227                         _monitoring_group->remove_control (trk->monitoring_control());
228                 }
229                 routes->erase (i);
230                 _session.set_dirty ();
231                 RouteRemoved (this, boost::weak_ptr<Route> (r)); /* EMIT SIGNAL */
232                 return 0;
233         }
234
235         return -1;
236 }
237
238 void
239 RouteGroup::set_rgba (uint32_t color) {
240         _rgba = color;
241
242         PBD::PropertyChange change;
243         change.add (Properties::color);
244         PropertyChanged (change);
245
246         if (!is_color ()) {
247                 return;
248         }
249
250         for (RouteList::const_iterator i = routes->begin(); i != routes->end(); ++i) {
251                 (*i)->presentation_info().PropertyChanged (Properties::color);
252         }
253 }
254
255 XMLNode&
256 RouteGroup::get_state ()
257 {
258         XMLNode *node = new XMLNode ("RouteGroup");
259
260         node->set_property ("id", id());
261         node->set_property ("rgba", _rgba);
262
263         add_properties (*node);
264
265         if (!routes->empty()) {
266                 stringstream str;
267
268                 for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
269                         str << (*i)->id () << ' ';
270                 }
271
272                 node->set_property ("routes", str.str());
273         }
274
275         return *node;
276 }
277
278 int
279 RouteGroup::set_state (const XMLNode& node, int version)
280 {
281         if (version < 3000) {
282                 return set_state_2X (node, version);
283         }
284
285         set_id (node);
286         set_values (node);
287         node.get_property ("rgba", _rgba);
288
289         std::string routes;
290         if (node.get_property ("routes", routes)) {
291                 stringstream str (routes);
292                 vector<string> ids;
293                 split (str.str(), ids, ' ');
294
295                 for (vector<string>::iterator i = ids.begin(); i != ids.end(); ++i) {
296                         PBD::ID id (*i);
297                         boost::shared_ptr<Route> r = _session.route_by_id (id);
298
299                         if (r) {
300                                 add (r);
301                         }
302                 }
303         }
304
305         if (_group_master_number.val() > 0) {
306                 boost::shared_ptr<VCA> vca = _session.vca_manager().vca_by_number (_group_master_number.val());
307                 if (vca) {
308                         /* no need to do the assignment because slaves will
309                            handle that themselves. But we can set group_master
310                            to use with future assignments of newly added routes.
311                         */
312                         group_master = vca;
313                 }
314         }
315
316         push_to_groups ();
317
318         return 0;
319 }
320
321 int
322 RouteGroup::set_state_2X (const XMLNode& node, int /*version*/)
323 {
324         set_values (node);
325
326         if (node.name() == "MixGroup") {
327                 _gain = true;
328                 _mute = true;
329                 _solo = true;
330                 _recenable = true;
331                 _route_active = true;
332                 _color = false;
333         } else if (node.name() == "EditGroup") {
334                 _gain = false;
335                 _mute = false;
336                 _solo = false;
337                 _recenable = false;
338                 _route_active = false;
339                 _color = false;
340         }
341
342         push_to_groups ();
343
344         return 0;
345 }
346
347 void
348 RouteGroup::set_gain (bool yn)
349 {
350         if (is_gain() == yn) {
351                 return;
352         }
353         if (has_control_master()) {
354                 return;
355         }
356
357         _gain = yn;
358         _gain_group->set_active (yn);
359
360         send_change (PropertyChange (Properties::group_gain));
361 }
362
363 void
364 RouteGroup::set_mute (bool yn)
365 {
366         if (is_mute() == yn) {
367                 return;
368         }
369         _mute = yn;
370         _mute_group->set_active (yn);
371
372         send_change (PropertyChange (Properties::group_mute));
373 }
374
375 void
376 RouteGroup::set_solo (bool yn)
377 {
378         if (is_solo() == yn) {
379                 return;
380         }
381         _solo = yn;
382         _solo_group->set_active (yn);
383
384         send_change (PropertyChange (Properties::group_solo));
385 }
386
387 void
388 RouteGroup::set_recenable (bool yn)
389 {
390         if (is_recenable() == yn) {
391                 return;
392         }
393         _recenable = yn;
394         _rec_enable_group->set_active (yn);
395         send_change (PropertyChange (Properties::group_recenable));
396 }
397
398 void
399 RouteGroup::set_select (bool yn)
400 {
401         if (is_select() == yn) {
402                 return;
403         }
404         _select = yn;
405         send_change (PropertyChange (Properties::group_select));
406 }
407
408 void
409 RouteGroup::set_route_active (bool yn)
410 {
411         if (is_route_active() == yn) {
412                 return;
413         }
414         _route_active = yn;
415         send_change (PropertyChange (Properties::group_route_active));
416 }
417
418 void
419 RouteGroup::set_color (bool yn)
420 {
421         if (is_color() == yn) {
422                 return;
423         }
424         _color = yn;
425
426         send_change (PropertyChange (Properties::group_color));
427
428         /* This is a bit of a hack, but this might change
429            our route's effective color, so emit gui_changed
430            for our routes.
431         */
432
433         for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
434                 (*i)->gui_changed (X_("color"), this);
435         }
436 }
437
438 void
439 RouteGroup::set_monitoring (bool yn)
440 {
441         if (is_monitoring() == yn) {
442                 return;
443         }
444
445         _monitoring = yn;
446         _monitoring_group->set_active (yn);
447
448         send_change (PropertyChange (Properties::group_monitoring));
449
450         _session.set_dirty ();
451 }
452
453 void
454 RouteGroup::set_active (bool yn, void* /*src*/)
455 {
456         if (is_active() == yn) {
457                 return;
458         }
459
460         _active = yn;
461
462         push_to_groups ();
463
464         send_change (PropertyChange (Properties::active));
465         _session.set_dirty ();
466 }
467
468 void
469 RouteGroup::set_relative (bool yn, void* /*src*/)
470 {
471         if (is_relative() == yn) {
472                 return;
473         }
474
475         _relative = yn;
476
477         push_to_groups ();
478
479         send_change (PropertyChange (Properties::group_relative));
480         _session.set_dirty ();
481 }
482
483 void
484 RouteGroup::set_hidden (bool yn, void* /*src*/)
485 {
486         if (is_hidden() == yn) {
487                 return;
488         }
489
490         if (yn) {
491                 _hidden = true;
492                 if (Config->get_hiding_groups_deactivates_groups()) {
493                         _active = false;
494                 }
495         } else {
496                 _hidden = false;
497                 if (Config->get_hiding_groups_deactivates_groups()) {
498                         _active = true;
499                 }
500         }
501
502         send_change (Properties::hidden);
503
504         _session.set_dirty ();
505 }
506
507 void
508 RouteGroup::audio_track_group (set<boost::shared_ptr<AudioTrack> >& ats)
509 {
510         for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
511                 boost::shared_ptr<AudioTrack> at = boost::dynamic_pointer_cast<AudioTrack>(*i);
512                 if (at) {
513                         ats.insert (at);
514                 }
515         }
516 }
517
518 void
519 RouteGroup::make_subgroup (bool aux, Placement placement)
520 {
521         RouteList rl;
522         uint32_t nin = 0;
523
524         /* since we don't do MIDI Busses yet, check quickly ... */
525
526         for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
527                 if ((*i)->output()->n_ports().n_midi() != 0) {
528                         PBD::warning << _("You cannot subgroup MIDI tracks at this time") << endmsg;
529                         return;
530                 }
531         }
532
533         for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
534                 if (!aux && nin != 0 && nin != (*i)->output()->n_ports().n_audio()) {
535                         PBD::warning << _("You cannot subgroup tracks with different number of outputs at this time.") << endmsg;
536                         return;
537                 }
538                 nin = max (nin, (*i)->output()->n_ports().n_audio());
539         }
540
541         try {
542                 /* use master bus etc. to determine default nouts.
543                  *
544                  * (since tracks can't have fewer outs than ins,
545                  * "nin" currently defines the number of outpus if nin > 2)
546                  */
547                 rl = _session.new_audio_route (nin, 2, 0, 1, string(), PresentationInfo::AudioBus, PresentationInfo::max_order);
548         } catch (...) {
549                 return;
550         }
551
552         subgroup_bus = rl.front();
553         subgroup_bus->set_name (_name);
554
555         if (aux) {
556
557                 _session.add_internal_sends (subgroup_bus, placement, routes);
558
559         } else {
560
561                 boost::shared_ptr<Bundle> bundle = subgroup_bus->input()->bundle ();
562
563                 for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
564                         (*i)->output()->disconnect (this);
565                         (*i)->output()->connect_ports_to_bundle (bundle, false, this);
566                 }
567         }
568 }
569
570 void
571 RouteGroup::destroy_subgroup ()
572 {
573         if (!subgroup_bus) {
574                 return;
575         }
576
577         for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
578                 (*i)->output()->disconnect (this);
579                 /* XXX find a new bundle to connect to */
580         }
581
582         _session.remove_route (subgroup_bus);
583         subgroup_bus.reset ();
584 }
585
586 bool
587 RouteGroup::has_subgroup() const
588 {
589         return subgroup_bus != 0;
590 }
591
592 bool
593 RouteGroup::enabled_property (PBD::PropertyID prop)
594 {
595         OwnedPropertyList::iterator i = _properties->find (prop);
596         if (i == _properties->end()) {
597                 return false;
598         }
599
600         return dynamic_cast<const PropertyTemplate<bool>* > (i->second)->val ();
601 }
602
603 void
604 RouteGroup::post_set (PBD::PropertyChange const &)
605 {
606         push_to_groups ();
607 }
608
609 void
610 RouteGroup::push_to_groups ()
611 {
612         if (is_relative()) {
613                 _gain_group->set_mode (ControlGroup::Mode (_gain_group->mode()|ControlGroup::Relative));
614         } else {
615                 _gain_group->set_mode (ControlGroup::Mode (_gain_group->mode()&~ControlGroup::Relative));
616         }
617
618         if (_active) {
619                 _gain_group->set_active (is_gain());
620                 _solo_group->set_active (is_solo());
621                 _mute_group->set_active (is_mute());
622                 _rec_enable_group->set_active (is_recenable());
623                 _monitoring_group->set_active (is_monitoring());
624         } else {
625                 _gain_group->set_active (false);
626                 _solo_group->set_active (false);
627                 _mute_group->set_active (false);
628
629                 _rec_enable_group->set_active (false);
630                 _monitoring_group->set_active (false);
631         }
632 }
633
634 void
635 RouteGroup::assign_master (boost::shared_ptr<VCA> master)
636 {
637         if (!routes || routes->empty()) {
638                 return;
639         }
640
641         boost::shared_ptr<Route> front = routes->front ();
642
643         if (front->slaved_to (master)) {
644                 return;
645         }
646
647         for (RouteList::iterator r = routes->begin(); r != routes->end(); ++r) {
648                 (*r)->assign (master, false);
649         }
650
651         bool used_to_share_gain = false;
652
653         if (is_gain()) {
654                 used_to_share_gain = true;
655         }
656
657         group_master = master;
658         _group_master_number = master->number();
659         _gain_group->set_active (false);
660
661         if (used_to_share_gain) {
662                 send_change (PropertyChange (Properties::group_gain));
663         }
664 }
665
666 void
667 RouteGroup::unassign_master (boost::shared_ptr<VCA> master)
668 {
669         if (!routes || routes->empty()) {
670                 return;
671         }
672
673         boost::shared_ptr<Route> front = routes->front ();
674
675         if (!front->slaved_to (master)) {
676                 return;
677         }
678
679         for (RouteList::iterator r = routes->begin(); r != routes->end(); ++r) {
680                 (*r)->unassign (master);
681         }
682
683         group_master.reset ();
684         _group_master_number = -1;
685
686         /* this is slightly tricky: is_gain() will return whether or not
687            the group is supposed to be sharing gain adjustment, and now that
688            we've reset _group_master_number to -1, it will reflect the user's
689            intentions correctly. Since there was a master before, and now there
690            is not, we are going to reactivate gain sharing ... and then tell
691            the world about it.
692         */
693         if (is_gain()) {
694                 _gain_group->set_active (true);
695                 send_change (PropertyChange (Properties::group_gain));
696         }
697 }
698
699 bool
700 RouteGroup::slaved () const
701 {
702         if (!routes || routes->empty()) {
703                 return false;
704         }
705
706         return routes->front()->slaved ();
707 }
708
709 bool
710 RouteGroup::has_control_master() const
711 {
712         return group_master.lock() != 0;
713 }