2 * Copyright (C) 2006-2017 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2007-2012 David Robillard <d@drobilla.net>
4 * Copyright (C) 2009-2011 Carl Hetherington <carl@carlh.net>
5 * Copyright (C) 2015-2019 Robin Gareus <robin@gareus.org>
6 * Copyright (C) 2016 Tim Mayberry <mojofunk@gmail.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "pbd/error.h"
28 #include "pbd/enumwriter.h"
29 #include "pbd/strsplit.h"
30 #include "pbd/types_convert.h"
31 #include "pbd/debug.h"
33 #include "ardour/amp.h"
34 #include "ardour/audio_track.h"
35 #include "ardour/debug.h"
36 #include "ardour/monitor_control.h"
37 #include "ardour/route.h"
38 #include "ardour/route_group.h"
39 #include "ardour/session.h"
40 #include "ardour/vca.h"
41 #include "ardour/vca_manager.h"
45 using namespace ARDOUR;
50 namespace Properties {
51 PropertyDescriptor<bool> active;
52 PropertyDescriptor<bool> group_relative;
53 PropertyDescriptor<bool> group_gain;
54 PropertyDescriptor<bool> group_mute;
55 PropertyDescriptor<bool> group_solo;
56 PropertyDescriptor<bool> group_recenable;
57 PropertyDescriptor<bool> group_select;
58 PropertyDescriptor<bool> group_route_active;
59 PropertyDescriptor<bool> group_color;
60 PropertyDescriptor<bool> group_monitoring;
61 PropertyDescriptor<int32_t> group_master_number;
66 RouteGroup::make_property_quarks ()
68 Properties::active.property_id = g_quark_from_static_string (X_("active"));
69 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for active = %1\n", Properties::active.property_id));
71 Properties::group_relative.property_id = g_quark_from_static_string (X_("relative"));
72 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for relative = %1\n", Properties::group_relative.property_id));
73 Properties::group_gain.property_id = g_quark_from_static_string (X_("gain"));
74 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for gain = %1\n", Properties::group_gain.property_id));
75 Properties::group_mute.property_id = g_quark_from_static_string (X_("mute"));
76 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for mute = %1\n", Properties::group_mute.property_id));
77 Properties::group_solo.property_id = g_quark_from_static_string (X_("solo"));
78 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for solo = %1\n", Properties::group_solo.property_id));
79 Properties::group_recenable.property_id = g_quark_from_static_string (X_("recenable"));
80 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for recenable = %1\n", Properties::group_recenable.property_id));
81 Properties::group_select.property_id = g_quark_from_static_string (X_("select"));
82 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for select = %1\n", Properties::group_select.property_id));
83 Properties::group_route_active.property_id = g_quark_from_static_string (X_("route-active"));
84 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for route-active = %1\n", Properties::group_route_active.property_id));
85 Properties::group_color.property_id = g_quark_from_static_string (X_("color"));
86 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for color = %1\n", Properties::group_color.property_id));
87 Properties::group_monitoring.property_id = g_quark_from_static_string (X_("monitoring"));
88 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for monitoring = %1\n", Properties::group_monitoring.property_id));
89 Properties::group_master_number.property_id = g_quark_from_static_string (X_("group-master-number"));
90 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for group-master-number = %1\n", Properties::group_master_number.property_id));
93 #define ROUTE_GROUP_DEFAULT_PROPERTIES _relative (Properties::group_relative, true) \
94 , _active (Properties::active, true) \
95 , _hidden (Properties::hidden, false) \
96 , _gain (Properties::group_gain, true) \
97 , _mute (Properties::group_mute, true) \
98 , _solo (Properties::group_solo, true) \
99 , _recenable (Properties::group_recenable, true) \
100 , _select (Properties::group_select, true) \
101 , _route_active (Properties::group_route_active, true) \
102 , _color (Properties::group_color, true) \
103 , _monitoring (Properties::group_monitoring, true) \
104 , _group_master_number (Properties::group_master_number, -1)
106 RouteGroup::RouteGroup (Session& s, const string &n)
107 : SessionObject (s, n)
108 , routes (new RouteList)
109 , ROUTE_GROUP_DEFAULT_PROPERTIES
110 , _solo_group (new ControlGroup (SoloAutomation))
111 , _mute_group (new ControlGroup (MuteAutomation))
112 , _rec_enable_group (new ControlGroup (RecEnableAutomation))
113 , _gain_group (new GainControlGroup ())
114 , _monitoring_group (new ControlGroup (MonitoringAutomation))
116 , _used_to_share_gain (false)
118 _xml_node_name = X_("RouteGroup");
120 add_property (_relative);
121 add_property (_active);
122 add_property (_hidden);
123 add_property (_gain);
124 add_property (_mute);
125 add_property (_solo);
126 add_property (_recenable);
127 add_property (_select);
128 add_property (_route_active);
129 add_property (_color);
130 add_property (_monitoring);
131 add_property (_group_master_number);
134 RouteGroup::~RouteGroup ()
136 _solo_group->clear ();
137 _mute_group->clear ();
138 _gain_group->clear ();
139 _rec_enable_group->clear ();
140 _monitoring_group->clear ();
142 boost::shared_ptr<VCA> vca (group_master.lock());
144 for (RouteList::iterator i = routes->begin(); i != routes->end();) {
145 RouteList::iterator tmp = i;
148 (*i)->set_route_group (0);
151 (*i)->unassign (vca);
158 /** Add a route to a group. Adding a route which is already in the group is allowed; nothing will happen.
159 * @param r Route to add.
162 RouteGroup::add (boost::shared_ptr<Route> r)
164 if (r->is_master()) {
168 if (find (routes->begin(), routes->end(), r) != routes->end()) {
172 if (r->route_group()) {
173 r->route_group()->remove (r);
176 routes->push_back (r);
178 _solo_group->add_control (r->solo_control());
179 _mute_group->add_control (r->mute_control());
180 _gain_group->add_control (r->gain_control());
181 boost::shared_ptr<Track> trk = boost::dynamic_pointer_cast<Track> (r);
183 _rec_enable_group->add_control (trk->rec_enable_control());
184 _monitoring_group->add_control (trk->monitoring_control());
187 r->set_route_group (this);
188 r->DropReferences.connect_same_thread (*this, boost::bind (&RouteGroup::remove_when_going_away, this, boost::weak_ptr<Route> (r)));
190 boost::shared_ptr<VCA> vca (group_master.lock());
196 _session.set_dirty ();
197 RouteAdded (this, boost::weak_ptr<Route> (r)); /* EMIT SIGNAL */
202 RouteGroup::remove_when_going_away (boost::weak_ptr<Route> wr)
204 boost::shared_ptr<Route> r (wr.lock());
212 RouteGroup::remove (boost::shared_ptr<Route> r)
214 RouteList::iterator i;
216 if ((i = find (routes->begin(), routes->end(), r)) != routes->end()) {
217 r->set_route_group (0);
219 boost::shared_ptr<VCA> vca = group_master.lock();
225 _solo_group->remove_control (r->solo_control());
226 _mute_group->remove_control (r->mute_control());
227 _gain_group->remove_control (r->gain_control());
228 boost::shared_ptr<Track> trk = boost::dynamic_pointer_cast<Track> (r);
230 _rec_enable_group->remove_control (trk->rec_enable_control());
231 _monitoring_group->remove_control (trk->monitoring_control());
234 _session.set_dirty ();
235 RouteRemoved (this, boost::weak_ptr<Route> (r)); /* EMIT SIGNAL */
243 RouteGroup::set_rgba (uint32_t color) {
246 PBD::PropertyChange change;
247 change.add (Properties::color);
248 PropertyChanged (change);
254 for (RouteList::const_iterator i = routes->begin(); i != routes->end(); ++i) {
255 (*i)->presentation_info().PropertyChanged (Properties::color);
260 RouteGroup::get_state ()
262 XMLNode *node = new XMLNode ("RouteGroup");
264 node->set_property ("id", id());
265 node->set_property ("rgba", _rgba);
266 node->set_property ("used-to-share-gain", _used_to_share_gain);
268 node->set_property ("subgroup-bus", _subgroup_bus->id ());
271 add_properties (*node);
273 if (!routes->empty()) {
276 for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
277 str << (*i)->id () << ' ';
280 node->set_property ("routes", str.str());
287 RouteGroup::set_state (const XMLNode& node, int version)
289 if (version < 3000) {
290 return set_state_2X (node, version);
295 node.get_property ("rgba", _rgba);
296 node.get_property ("used-to-share-gain", _used_to_share_gain);
299 if (node.get_property ("routes", routes)) {
300 stringstream str (routes);
302 split (str.str(), ids, ' ');
304 for (vector<string>::iterator i = ids.begin(); i != ids.end(); ++i) {
306 boost::shared_ptr<Route> r = _session.route_by_id (id);
314 PBD::ID subgroup_id (0);
315 if (node.get_property ("subgroup-bus", subgroup_id)) {
316 boost::shared_ptr<Route> r = _session.route_by_id (subgroup_id);
322 if (_group_master_number.val() > 0) {
323 boost::shared_ptr<VCA> vca = _session.vca_manager().vca_by_number (_group_master_number.val());
325 /* no need to do the assignment because slaves will
326 handle that themselves. But we can set group_master
327 to use with future assignments of newly added routes.
339 RouteGroup::set_state_2X (const XMLNode& node, int /*version*/)
343 if (node.name() == "MixGroup") {
348 _route_active = true;
350 } else if (node.name() == "EditGroup") {
355 _route_active = false;
365 RouteGroup::set_gain (bool yn)
367 if (is_gain() == yn) {
372 _gain_group->set_active (yn);
374 send_change (PropertyChange (Properties::group_gain));
378 RouteGroup::set_mute (bool yn)
380 if (is_mute() == yn) {
384 _mute_group->set_active (yn);
386 send_change (PropertyChange (Properties::group_mute));
390 RouteGroup::set_solo (bool yn)
392 if (is_solo() == yn) {
396 _solo_group->set_active (yn);
398 send_change (PropertyChange (Properties::group_solo));
402 RouteGroup::set_recenable (bool yn)
404 if (is_recenable() == yn) {
408 _rec_enable_group->set_active (yn);
409 send_change (PropertyChange (Properties::group_recenable));
413 RouteGroup::set_select (bool yn)
415 if (is_select() == yn) {
419 send_change (PropertyChange (Properties::group_select));
423 RouteGroup::set_route_active (bool yn)
425 if (is_route_active() == yn) {
429 send_change (PropertyChange (Properties::group_route_active));
433 RouteGroup::set_color (bool yn)
435 if (is_color() == yn) {
440 send_change (PropertyChange (Properties::group_color));
442 /* This is a bit of a hack, but this might change
443 our route's effective color, so emit gui_changed
447 for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
448 (*i)->gui_changed (X_("color"), this);
453 RouteGroup::set_monitoring (bool yn)
455 if (is_monitoring() == yn) {
460 _monitoring_group->set_active (yn);
462 send_change (PropertyChange (Properties::group_monitoring));
464 _session.set_dirty ();
468 RouteGroup::set_active (bool yn, void* /*src*/)
470 if (is_active() == yn) {
478 send_change (PropertyChange (Properties::active));
479 _session.set_dirty ();
483 RouteGroup::set_relative (bool yn, void* /*src*/)
485 if (is_relative() == yn) {
493 send_change (PropertyChange (Properties::group_relative));
494 _session.set_dirty ();
498 RouteGroup::set_hidden (bool yn, void* /*src*/)
500 if (is_hidden() == yn) {
506 if (Config->get_hiding_groups_deactivates_groups()) {
511 if (Config->get_hiding_groups_deactivates_groups()) {
516 send_change (Properties::hidden);
518 _session.set_dirty ();
522 RouteGroup::audio_track_group (set<boost::shared_ptr<AudioTrack> >& ats)
524 for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
525 boost::shared_ptr<AudioTrack> at = boost::dynamic_pointer_cast<AudioTrack>(*i);
533 RouteGroup::make_subgroup (bool aux, Placement placement)
538 /* since we don't do MIDI Busses yet, check quickly ... */
540 for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
541 if ((*i)->output()->n_ports().n_midi() != 0) {
542 PBD::warning << _("You cannot subgroup MIDI tracks at this time") << endmsg;
547 for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
548 if (!aux && nin != 0 && nin != (*i)->output()->n_ports().n_audio()) {
549 PBD::warning << _("You cannot subgroup tracks with different number of outputs at this time.") << endmsg;
552 nin = max (nin, (*i)->output()->n_ports().n_audio());
556 /* use master bus etc. to determine default nouts.
558 * (since tracks can't have fewer outs than ins,
559 * "nin" currently defines the number of outpus if nin > 2)
561 rl = _session.new_audio_route (nin, 2, 0, 1, string(), PresentationInfo::AudioBus, PresentationInfo::max_order);
566 _subgroup_bus = rl.front();
567 _subgroup_bus->set_name (_name);
571 _session.add_internal_sends (_subgroup_bus, placement, routes);
575 boost::shared_ptr<Bundle> bundle = _subgroup_bus->input()->bundle ();
577 for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
578 (*i)->output()->disconnect (this);
579 (*i)->output()->connect_ports_to_bundle (bundle, false, this);
585 RouteGroup::destroy_subgroup ()
587 if (!_subgroup_bus) {
591 for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
592 (*i)->output()->disconnect (this);
593 /* XXX find a new bundle to connect to */
596 _session.remove_route (_subgroup_bus);
597 _subgroup_bus.reset ();
601 RouteGroup::has_subgroup() const
603 return _subgroup_bus != 0;
607 RouteGroup::enabled_property (PBD::PropertyID prop)
609 OwnedPropertyList::iterator i = _properties->find (prop);
610 if (i == _properties->end()) {
614 return dynamic_cast<const PropertyTemplate<bool>* > (i->second)->val ();
618 RouteGroup::post_set (PBD::PropertyChange const &)
624 RouteGroup::push_to_groups ()
627 _gain_group->set_mode (ControlGroup::Mode (_gain_group->mode()|ControlGroup::Relative));
629 _gain_group->set_mode (ControlGroup::Mode (_gain_group->mode()&~ControlGroup::Relative));
633 _gain_group->set_active (is_gain());
634 _solo_group->set_active (is_solo());
635 _mute_group->set_active (is_mute());
636 _rec_enable_group->set_active (is_recenable());
637 _monitoring_group->set_active (is_monitoring());
639 _gain_group->set_active (false);
640 _solo_group->set_active (false);
641 _mute_group->set_active (false);
643 _rec_enable_group->set_active (false);
644 _monitoring_group->set_active (false);
649 RouteGroup::assign_master (boost::shared_ptr<VCA> master)
651 if (!routes || routes->empty()) {
655 boost::shared_ptr<Route> front = routes->front ();
657 if (front->slaved_to (master)) {
661 for (RouteList::iterator r = routes->begin(); r != routes->end(); ++r) {
662 (*r)->assign (master);
665 group_master = master;
666 _group_master_number = master->number();
668 _used_to_share_gain = is_gain ();
673 RouteGroup::unassign_master (boost::shared_ptr<VCA> master)
675 if (!routes || routes->empty()) {
679 boost::shared_ptr<Route> front = routes->front ();
681 if (!front->slaved_to (master)) {
685 for (RouteList::iterator r = routes->begin(); r != routes->end(); ++r) {
686 (*r)->unassign (master);
689 group_master.reset ();
690 _group_master_number = -1;
692 set_gain (_used_to_share_gain);
696 RouteGroup::slaved () const
698 if (!routes || routes->empty()) {
702 return routes->front()->slaved ();
706 RouteGroup::has_control_master() const
708 return group_master.lock() != 0;