move ownership of an RT MIDI buffer from DiskIO to MidiPlaylist
[ardour.git] / libs / ardour / route_group.cc
1 /*
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>
7  *
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.
12  *
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.
17  *
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.
21  */
22
23 #include <inttypes.h>
24
25 #include <algorithm>
26
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"
32
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"
42
43 #include "pbd/i18n.h"
44
45 using namespace ARDOUR;
46 using namespace PBD;
47 using namespace std;
48
49 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;
62         }
63 }
64
65 void
66 RouteGroup::make_property_quarks ()
67 {
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));
70
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));
91 }
92
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)
105
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))
115         , _rgba (0)
116         , _used_to_share_gain (false)
117 {
118         _xml_node_name = X_("RouteGroup");
119
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);
132 }
133
134 RouteGroup::~RouteGroup ()
135 {
136         _solo_group->clear ();
137         _mute_group->clear ();
138         _gain_group->clear ();
139         _rec_enable_group->clear ();
140         _monitoring_group->clear ();
141
142         boost::shared_ptr<VCA> vca (group_master.lock());
143
144         for (RouteList::iterator i = routes->begin(); i != routes->end();) {
145                 RouteList::iterator tmp = i;
146                 ++tmp;
147
148                 (*i)->set_route_group (0);
149
150                 if (vca) {
151                         (*i)->unassign (vca);
152                 }
153
154                 i = tmp;
155         }
156 }
157
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.
160  */
161 int
162 RouteGroup::add (boost::shared_ptr<Route> r)
163 {
164         if (r->is_master()) {
165                 return 0;
166         }
167
168         if (find (routes->begin(), routes->end(), r) != routes->end()) {
169                 return 0;
170         }
171
172         if (r->route_group()) {
173                 r->route_group()->remove (r);
174         }
175
176         routes->push_back (r);
177
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);
182         if (trk) {
183                 _rec_enable_group->add_control (trk->rec_enable_control());
184                 _monitoring_group->add_control (trk->monitoring_control());
185         }
186
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)));
189
190         boost::shared_ptr<VCA> vca (group_master.lock());
191
192         if (vca) {
193                 r->assign (vca);
194         }
195
196         _session.set_dirty ();
197         RouteAdded (this, boost::weak_ptr<Route> (r)); /* EMIT SIGNAL */
198         return 0;
199 }
200
201 void
202 RouteGroup::remove_when_going_away (boost::weak_ptr<Route> wr)
203 {
204         boost::shared_ptr<Route> r (wr.lock());
205
206         if (r) {
207                 remove (r);
208         }
209 }
210
211 int
212 RouteGroup::remove (boost::shared_ptr<Route> r)
213 {
214         RouteList::iterator i;
215
216         if ((i = find (routes->begin(), routes->end(), r)) != routes->end()) {
217                 r->set_route_group (0);
218
219                 boost::shared_ptr<VCA> vca = group_master.lock();
220
221                 if (vca) {
222                         r->unassign (vca);
223                 }
224
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);
229                 if (trk) {
230                         _rec_enable_group->remove_control (trk->rec_enable_control());
231                         _monitoring_group->remove_control (trk->monitoring_control());
232                 }
233                 routes->erase (i);
234                 _session.set_dirty ();
235                 RouteRemoved (this, boost::weak_ptr<Route> (r)); /* EMIT SIGNAL */
236                 return 0;
237         }
238
239         return -1;
240 }
241
242 void
243 RouteGroup::set_rgba (uint32_t color) {
244         _rgba = color;
245
246         PBD::PropertyChange change;
247         change.add (Properties::color);
248         PropertyChanged (change);
249
250         if (!is_color ()) {
251                 return;
252         }
253
254         for (RouteList::const_iterator i = routes->begin(); i != routes->end(); ++i) {
255                 (*i)->presentation_info().PropertyChanged (Properties::color);
256         }
257 }
258
259 XMLNode&
260 RouteGroup::get_state ()
261 {
262         XMLNode *node = new XMLNode ("RouteGroup");
263
264         node->set_property ("id", id());
265         node->set_property ("rgba", _rgba);
266         node->set_property ("used-to-share-gain", _used_to_share_gain);
267         if (_subgroup_bus) {
268                 node->set_property ("subgroup-bus", _subgroup_bus->id ());
269         }
270
271         add_properties (*node);
272
273         if (!routes->empty()) {
274                 stringstream str;
275
276                 for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
277                         str << (*i)->id () << ' ';
278                 }
279
280                 node->set_property ("routes", str.str());
281         }
282
283         return *node;
284 }
285
286 int
287 RouteGroup::set_state (const XMLNode& node, int version)
288 {
289         if (version < 3000) {
290                 return set_state_2X (node, version);
291         }
292
293         set_id (node);
294         set_values (node);
295         node.get_property ("rgba", _rgba);
296         node.get_property ("used-to-share-gain", _used_to_share_gain);
297
298         std::string routes;
299         if (node.get_property ("routes", routes)) {
300                 stringstream str (routes);
301                 vector<string> ids;
302                 split (str.str(), ids, ' ');
303
304                 for (vector<string>::iterator i = ids.begin(); i != ids.end(); ++i) {
305                         PBD::ID id (*i);
306                         boost::shared_ptr<Route> r = _session.route_by_id (id);
307
308                         if (r) {
309                                 add (r);
310                         }
311                 }
312         }
313
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);
317                 if (r) {
318                         _subgroup_bus = r;
319                 }
320         }
321
322         if (_group_master_number.val() > 0) {
323                 boost::shared_ptr<VCA> vca = _session.vca_manager().vca_by_number (_group_master_number.val());
324                 if (vca) {
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.
328                         */
329                         group_master = vca;
330                 }
331         }
332
333         push_to_groups ();
334
335         return 0;
336 }
337
338 int
339 RouteGroup::set_state_2X (const XMLNode& node, int /*version*/)
340 {
341         set_values (node);
342
343         if (node.name() == "MixGroup") {
344                 _gain = true;
345                 _mute = true;
346                 _solo = true;
347                 _recenable = true;
348                 _route_active = true;
349                 _color = false;
350         } else if (node.name() == "EditGroup") {
351                 _gain = false;
352                 _mute = false;
353                 _solo = false;
354                 _recenable = false;
355                 _route_active = false;
356                 _color = false;
357         }
358
359         push_to_groups ();
360
361         return 0;
362 }
363
364 void
365 RouteGroup::set_gain (bool yn)
366 {
367         if (is_gain() == yn) {
368                 return;
369         }
370
371         _gain = yn;
372         _gain_group->set_active (yn);
373
374         send_change (PropertyChange (Properties::group_gain));
375 }
376
377 void
378 RouteGroup::set_mute (bool yn)
379 {
380         if (is_mute() == yn) {
381                 return;
382         }
383         _mute = yn;
384         _mute_group->set_active (yn);
385
386         send_change (PropertyChange (Properties::group_mute));
387 }
388
389 void
390 RouteGroup::set_solo (bool yn)
391 {
392         if (is_solo() == yn) {
393                 return;
394         }
395         _solo = yn;
396         _solo_group->set_active (yn);
397
398         send_change (PropertyChange (Properties::group_solo));
399 }
400
401 void
402 RouteGroup::set_recenable (bool yn)
403 {
404         if (is_recenable() == yn) {
405                 return;
406         }
407         _recenable = yn;
408         _rec_enable_group->set_active (yn);
409         send_change (PropertyChange (Properties::group_recenable));
410 }
411
412 void
413 RouteGroup::set_select (bool yn)
414 {
415         if (is_select() == yn) {
416                 return;
417         }
418         _select = yn;
419         send_change (PropertyChange (Properties::group_select));
420 }
421
422 void
423 RouteGroup::set_route_active (bool yn)
424 {
425         if (is_route_active() == yn) {
426                 return;
427         }
428         _route_active = yn;
429         send_change (PropertyChange (Properties::group_route_active));
430 }
431
432 void
433 RouteGroup::set_color (bool yn)
434 {
435         if (is_color() == yn) {
436                 return;
437         }
438         _color = yn;
439
440         send_change (PropertyChange (Properties::group_color));
441
442         /* This is a bit of a hack, but this might change
443            our route's effective color, so emit gui_changed
444            for our routes.
445         */
446
447         for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
448                 (*i)->gui_changed (X_("color"), this);
449         }
450 }
451
452 void
453 RouteGroup::set_monitoring (bool yn)
454 {
455         if (is_monitoring() == yn) {
456                 return;
457         }
458
459         _monitoring = yn;
460         _monitoring_group->set_active (yn);
461
462         send_change (PropertyChange (Properties::group_monitoring));
463
464         _session.set_dirty ();
465 }
466
467 void
468 RouteGroup::set_active (bool yn, void* /*src*/)
469 {
470         if (is_active() == yn) {
471                 return;
472         }
473
474         _active = yn;
475
476         push_to_groups ();
477
478         send_change (PropertyChange (Properties::active));
479         _session.set_dirty ();
480 }
481
482 void
483 RouteGroup::set_relative (bool yn, void* /*src*/)
484 {
485         if (is_relative() == yn) {
486                 return;
487         }
488
489         _relative = yn;
490
491         push_to_groups ();
492
493         send_change (PropertyChange (Properties::group_relative));
494         _session.set_dirty ();
495 }
496
497 void
498 RouteGroup::set_hidden (bool yn, void* /*src*/)
499 {
500         if (is_hidden() == yn) {
501                 return;
502         }
503
504         if (yn) {
505                 _hidden = true;
506                 if (Config->get_hiding_groups_deactivates_groups()) {
507                         _active = false;
508                 }
509         } else {
510                 _hidden = false;
511                 if (Config->get_hiding_groups_deactivates_groups()) {
512                         _active = true;
513                 }
514         }
515
516         send_change (Properties::hidden);
517
518         _session.set_dirty ();
519 }
520
521 void
522 RouteGroup::audio_track_group (set<boost::shared_ptr<AudioTrack> >& ats)
523 {
524         for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
525                 boost::shared_ptr<AudioTrack> at = boost::dynamic_pointer_cast<AudioTrack>(*i);
526                 if (at) {
527                         ats.insert (at);
528                 }
529         }
530 }
531
532 void
533 RouteGroup::make_subgroup (bool aux, Placement placement)
534 {
535         RouteList rl;
536         uint32_t nin = 0;
537
538         /* since we don't do MIDI Busses yet, check quickly ... */
539
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;
543                         return;
544                 }
545         }
546
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;
550                         return;
551                 }
552                 nin = max (nin, (*i)->output()->n_ports().n_audio());
553         }
554
555         try {
556                 /* use master bus etc. to determine default nouts.
557                  *
558                  * (since tracks can't have fewer outs than ins,
559                  * "nin" currently defines the number of outpus if nin > 2)
560                  */
561                 rl = _session.new_audio_route (nin, 2, 0, 1, string(), PresentationInfo::AudioBus, PresentationInfo::max_order);
562         } catch (...) {
563                 return;
564         }
565
566         _subgroup_bus = rl.front();
567         _subgroup_bus->set_name (_name);
568
569         if (aux) {
570
571                 _session.add_internal_sends (_subgroup_bus, placement, routes);
572
573         } else {
574
575                 boost::shared_ptr<Bundle> bundle = _subgroup_bus->input()->bundle ();
576
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);
580                 }
581         }
582 }
583
584 void
585 RouteGroup::destroy_subgroup ()
586 {
587         if (!_subgroup_bus) {
588                 return;
589         }
590
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 */
594         }
595
596         _session.remove_route (_subgroup_bus);
597         _subgroup_bus.reset ();
598 }
599
600 bool
601 RouteGroup::has_subgroup() const
602 {
603         return _subgroup_bus != 0;
604 }
605
606 bool
607 RouteGroup::enabled_property (PBD::PropertyID prop)
608 {
609         OwnedPropertyList::iterator i = _properties->find (prop);
610         if (i == _properties->end()) {
611                 return false;
612         }
613
614         return dynamic_cast<const PropertyTemplate<bool>* > (i->second)->val ();
615 }
616
617 void
618 RouteGroup::post_set (PBD::PropertyChange const &)
619 {
620         push_to_groups ();
621 }
622
623 void
624 RouteGroup::push_to_groups ()
625 {
626         if (is_relative()) {
627                 _gain_group->set_mode (ControlGroup::Mode (_gain_group->mode()|ControlGroup::Relative));
628         } else {
629                 _gain_group->set_mode (ControlGroup::Mode (_gain_group->mode()&~ControlGroup::Relative));
630         }
631
632         if (_active) {
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());
638         } else {
639                 _gain_group->set_active (false);
640                 _solo_group->set_active (false);
641                 _mute_group->set_active (false);
642
643                 _rec_enable_group->set_active (false);
644                 _monitoring_group->set_active (false);
645         }
646 }
647
648 void
649 RouteGroup::assign_master (boost::shared_ptr<VCA> master)
650 {
651         if (!routes || routes->empty()) {
652                 return;
653         }
654
655         boost::shared_ptr<Route> front = routes->front ();
656
657         if (front->slaved_to (master)) {
658                 return;
659         }
660
661         for (RouteList::iterator r = routes->begin(); r != routes->end(); ++r) {
662                 (*r)->assign (master);
663         }
664
665         group_master = master;
666         _group_master_number = master->number();
667
668         _used_to_share_gain = is_gain ();
669         set_gain (false);
670 }
671
672 void
673 RouteGroup::unassign_master (boost::shared_ptr<VCA> master)
674 {
675         if (!routes || routes->empty()) {
676                 return;
677         }
678
679         boost::shared_ptr<Route> front = routes->front ();
680
681         if (!front->slaved_to (master)) {
682                 return;
683         }
684
685         for (RouteList::iterator r = routes->begin(); r != routes->end(); ++r) {
686                 (*r)->unassign (master);
687         }
688
689         group_master.reset ();
690         _group_master_number = -1;
691
692         set_gain (_used_to_share_gain);
693 }
694
695 bool
696 RouteGroup::slaved () const
697 {
698         if (!routes || routes->empty()) {
699                 return false;
700         }
701
702         return routes->front()->slaved ();
703 }
704
705 bool
706 RouteGroup::has_control_master() const
707 {
708         return group_master.lock() != 0;
709 }