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