add route group support for monitoring choices
[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
25 #include "pbd/error.h"
26 #include "pbd/enumwriter.h"
27 #include "pbd/strsplit.h"
28
29 #include "ardour/amp.h"
30 #include "ardour/debug.h"
31 #include "ardour/route_group.h"
32 #include "ardour/audio_track.h"
33 #include "ardour/audio_diskstream.h"
34 #include "ardour/configuration.h"
35 #include "ardour/session.h"
36
37 #include "i18n.h"
38
39 using namespace ARDOUR;
40 using namespace PBD;
41 using namespace std;
42
43 namespace ARDOUR {
44         namespace Properties {
45                 PropertyDescriptor<bool> relative;
46                 PropertyDescriptor<bool> active;
47                 PropertyDescriptor<bool> gain;
48                 PropertyDescriptor<bool> mute;
49                 PropertyDescriptor<bool> solo;
50                 PropertyDescriptor<bool> recenable;
51                 PropertyDescriptor<bool> select;
52                 PropertyDescriptor<bool> edit;
53                 PropertyDescriptor<bool> route_active;
54                 PropertyDescriptor<bool> color;
55                 PropertyDescriptor<bool> monitoring;
56         }
57 }
58
59 void
60 RouteGroup::make_property_quarks ()
61 {
62         Properties::relative.property_id = g_quark_from_static_string (X_("relative"));
63         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for relative = %1\n",    Properties::relative.property_id));
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         Properties::hidden.property_id = g_quark_from_static_string (X_("hidden"));
67         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for hidden = %1\n",      Properties::hidden.property_id));
68         Properties::gain.property_id = g_quark_from_static_string (X_("gain"));
69         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for gain = %1\n",        Properties::gain.property_id));
70         Properties::mute.property_id = g_quark_from_static_string (X_("mute"));
71         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for mute = %1\n",        Properties::mute.property_id));
72         Properties::solo.property_id = g_quark_from_static_string (X_("solo"));
73         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for solo = %1\n",        Properties::solo.property_id));
74         Properties::recenable.property_id = g_quark_from_static_string (X_("recenable"));
75         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for recenable = %1\n",   Properties::recenable.property_id));
76         Properties::select.property_id = g_quark_from_static_string (X_("select"));
77         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for select = %1\n",      Properties::select.property_id));
78         Properties::edit.property_id = g_quark_from_static_string (X_("edit"));
79         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for edit = %1\n",        Properties::edit.property_id));
80         Properties::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::route_active.property_id));
82         Properties::color.property_id = g_quark_from_static_string (X_("color"));
83         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for color = %1\n",       Properties::color.property_id));
84         Properties::monitoring.property_id = g_quark_from_static_string (X_("monitoring"));
85         DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for monitoring = %1\n",       Properties::monitoring.property_id));
86 }
87
88 #define ROUTE_GROUP_DEFAULT_PROPERTIES  _relative (Properties::relative, false) \
89         , _active (Properties::active, false) \
90         , _hidden (Properties::hidden, false) \
91         , _gain (Properties::gain, false) \
92         , _mute (Properties::mute, false) \
93         , _solo (Properties::solo, false) \
94         , _recenable (Properties::recenable, false) \
95         , _select (Properties::select, false) \
96         , _edit (Properties::edit, false) \
97         , _route_active (Properties::route_active, false) \
98         , _color (Properties::color, false) \
99         , _monitoring (Properties::monitoring, false)
100
101 RouteGroup::RouteGroup (Session& s, const string &n)
102         : SessionObject (s, n)
103         , routes (new RouteList)
104         , ROUTE_GROUP_DEFAULT_PROPERTIES
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 (_edit);
117         add_property (_route_active);
118         add_property (_color);
119         add_property (_monitoring);
120 }
121
122 RouteGroup::~RouteGroup ()
123 {
124         for (RouteList::iterator i = routes->begin(); i != routes->end();) {
125                 RouteList::iterator tmp = i;
126                 ++tmp;
127
128                 (*i)->leave_route_group ();
129
130                 i = tmp;
131         }
132 }
133
134 /** Add a route to a group.  Adding a route which is already in the group is allowed; nothing will happen.
135  *  @param r Route to add.
136  */
137 int
138 RouteGroup::add (boost::shared_ptr<Route> r)
139 {
140         if (find (routes->begin(), routes->end(), r) != routes->end()) {
141                 return 0;
142         }
143
144         r->leave_route_group ();
145
146         routes->push_back (r);
147
148         r->join_route_group (this);
149         r->DropReferences.connect_same_thread (*this, boost::bind (&RouteGroup::remove_when_going_away, this, boost::weak_ptr<Route> (r)));
150
151         _session.set_dirty ();
152         RouteAdded (this, boost::weak_ptr<Route> (r)); /* EMIT SIGNAL */
153         return 0;
154 }
155
156 void
157 RouteGroup::remove_when_going_away (boost::weak_ptr<Route> wr)
158 {
159         boost::shared_ptr<Route> r (wr.lock());
160
161         if (r) {
162                 remove (r);
163         }
164 }
165
166 int
167 RouteGroup::remove (boost::shared_ptr<Route> r)
168 {
169         RouteList::iterator i;
170
171         if ((i = find (routes->begin(), routes->end(), r)) != routes->end()) {
172                 r->leave_route_group ();
173                 routes->erase (i);
174                 _session.set_dirty ();
175                 RouteRemoved (this, boost::weak_ptr<Route> (r)); /* EMIT SIGNAL */
176                 return 0;
177         }
178
179         return -1;
180 }
181
182
183 gain_t
184 RouteGroup::get_min_factor (gain_t factor)
185 {
186         for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
187                 gain_t const g = (*i)->amp()->gain();
188
189                 if ((g + g * factor) >= 0.0f) {
190                         continue;
191                 }
192
193                 if (g <= 0.0000003f) {
194                         return 0.0f;
195                 }
196
197                 factor = 0.0000003f / g - 1.0f;
198         }
199         
200         return factor;
201 }
202
203 gain_t
204 RouteGroup::get_max_factor (gain_t factor)
205 {
206         for (RouteList::iterator i = routes->begin(); i != routes->end(); i++) {
207                 gain_t const g = (*i)->amp()->gain();
208
209                 // if the current factor woulnd't raise this route above maximum
210                 if ((g + g * factor) <= 1.99526231f) {
211                         continue;
212                 }
213
214                 // if route gain is already at peak, return 0.0f factor
215                 if (g >= 1.99526231f) {
216                         return 0.0f;
217                 }
218
219                 // factor is calculated so that it would raise current route to max
220                 factor = 1.99526231f / g - 1.0f;
221         }
222
223         return factor;
224 }
225
226 XMLNode&
227 RouteGroup::get_state ()
228 {
229         XMLNode *node = new XMLNode ("RouteGroup");
230
231         char buf[64];
232         id().print (buf, sizeof (buf));
233         node->add_property ("id", buf);
234
235         add_properties (*node);
236
237         if (!routes->empty()) {
238                 stringstream str;
239
240                 for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
241                         str << (*i)->id () << ' ';
242                 }
243
244                 node->add_property ("routes", str.str());
245         }
246
247         return *node;
248 }
249
250 int
251 RouteGroup::set_state (const XMLNode& node, int version)
252 {
253         if (version < 3000) {
254                 return set_state_2X (node, version);
255         }
256
257         const XMLProperty *prop;
258
259         set_id (node);
260         set_values (node);
261
262         if ((prop = node.property ("routes")) != 0) {
263                 stringstream str (prop->value());
264                 vector<string> ids;
265                 split (str.str(), ids, ' ');
266
267                 for (vector<string>::iterator i = ids.begin(); i != ids.end(); ++i) {
268                         PBD::ID id (*i);
269                         boost::shared_ptr<Route> r = _session.route_by_id (id);
270
271                         if (r) {
272                                 add (r);
273                         }
274                 }
275         }
276
277         return 0;
278 }
279
280 int
281 RouteGroup::set_state_2X (const XMLNode& node, int /*version*/)
282 {
283         set_values (node);
284
285         if (node.name() == "MixGroup") {
286                 _gain = true;
287                 _mute = true;
288                 _solo = true;
289                 _recenable = true;
290                 _edit = false;
291                 _route_active = true;
292                 _color = false;
293         } else if (node.name() == "EditGroup") {
294                 _gain = false;
295                 _mute = false;
296                 _solo = false;
297                 _recenable = false;
298                 _edit = true;
299                 _route_active = false;
300                 _color = false;
301         }
302
303         return 0;
304 }
305
306 void
307 RouteGroup::set_gain (bool yn)
308 {
309         if (is_gain() == yn) {
310                 return;
311         }
312         _gain = yn;
313 }
314
315 void
316 RouteGroup::set_mute (bool yn)
317 {
318         if (is_mute() == yn) {
319                 return;
320         }
321         _mute = yn;
322 }
323
324 void
325 RouteGroup::set_solo (bool yn)
326 {
327         if (is_solo() == yn) {
328                 return;
329         }
330         _solo = yn;
331 }
332
333 void
334 RouteGroup::set_recenable (bool yn)
335 {
336         if (is_recenable() == yn) {
337                 return;
338         }
339         _recenable = yn;
340 }
341
342 void
343 RouteGroup::set_select (bool yn)
344 {
345         if (is_select() == yn) {
346                 return;
347         }
348         _select = yn;
349 }
350
351 void
352 RouteGroup::set_edit (bool yn)
353 {
354         if (is_edit() == yn) {
355                 return;
356         }
357         _edit = yn;
358 }
359
360 void
361 RouteGroup::set_route_active (bool yn)
362 {
363         if (is_route_active() == yn) {
364                 return;
365         }
366         _route_active = yn;
367 }
368
369 void
370 RouteGroup::set_color (bool yn)
371 {
372         if (is_color() == yn) {
373                 return;
374         }
375         _color = yn;
376
377         /* This is a bit of a hack, but this might change
378            our route's effective color, so emit gui_changed
379            for our routes.
380         */
381
382         for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
383                 (*i)->gui_changed (X_("color"), this);
384         }
385 }
386
387 void
388 RouteGroup::set_monitoring (bool yn) 
389 {
390         if (is_monitoring() == yn) {
391                 return;
392         }
393
394         _monitoring = yn;
395         send_change (PropertyChange (Properties::monitoring));
396
397         _session.set_dirty ();
398 }
399
400 void
401 RouteGroup::set_active (bool yn, void* /*src*/)
402 {
403         if (is_active() == yn) {
404                 return;
405         }
406
407         _active = yn;
408         send_change (PropertyChange (Properties::active));
409
410         _session.set_dirty ();
411 }
412
413 void
414 RouteGroup::set_relative (bool yn, void* /*src*/)
415 {
416         if (is_relative() == yn) {
417                 return;
418         }
419         _relative = yn;
420         _session.set_dirty ();
421 }
422
423 void
424 RouteGroup::set_hidden (bool yn, void* /*src*/)
425 {
426         if (is_hidden() == yn) {
427                 return;
428         }
429
430         if (yn) {
431                 _hidden = true;
432                 if (Config->get_hiding_groups_deactivates_groups()) {
433                         _active = false;
434                 }
435         } else {
436                 _hidden = false;
437                 if (Config->get_hiding_groups_deactivates_groups()) {
438                         _active = true;
439                 }
440         }
441
442         send_change (Properties::hidden);
443
444         _session.set_dirty ();
445 }
446
447 void
448 RouteGroup::audio_track_group (set<boost::shared_ptr<AudioTrack> >& ats)
449 {
450         for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
451                 boost::shared_ptr<AudioTrack> at = boost::dynamic_pointer_cast<AudioTrack>(*i);
452                 if (at) {
453                         ats.insert (at);
454                 }
455         }
456 }
457
458 void
459 RouteGroup::make_subgroup (bool aux, Placement placement)
460 {
461         RouteList rl;
462         uint32_t nin = 0;
463
464         /* since we don't do MIDI Busses yet, check quickly ... */
465
466         for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
467                 if ((*i)->output()->n_ports().n_midi() != 0) {
468                         PBD::info << _("You cannot subgroup MIDI tracks at this time") << endmsg;
469                         return;
470                 }
471         }
472
473         for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
474                 nin = max (nin, (*i)->output()->n_ports().n_audio());
475         }
476
477         try {
478                 /* use master bus etc. to determine default nouts */
479                 rl = _session.new_audio_route (nin, 2, 0, 1);
480         } catch (...) {
481                 return;
482         }
483
484         subgroup_bus = rl.front();
485         subgroup_bus->set_name (_name);
486
487         if (aux) {
488
489                 _session.add_internal_sends (subgroup_bus, placement, routes);
490
491         } else {
492
493                 boost::shared_ptr<Bundle> bundle = subgroup_bus->input()->bundle ();
494
495                 for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
496                         (*i)->output()->disconnect (this);
497                         (*i)->output()->connect_ports_to_bundle (bundle, this);
498                 }
499         }
500 }
501
502 void
503 RouteGroup::destroy_subgroup ()
504 {
505         if (!subgroup_bus) {
506                 return;
507         }
508
509         for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
510                 (*i)->output()->disconnect (this);
511                 /* XXX find a new bundle to connect to */
512         }
513
514         _session.remove_route (subgroup_bus);
515         subgroup_bus.reset ();
516 }
517
518 bool
519 RouteGroup::enabled_property (PBD::PropertyID prop)
520 {
521         OwnedPropertyList::iterator i = _properties->find (prop);
522         if (i == _properties->end()) {
523                 return false;
524         }
525
526         return dynamic_cast<const PropertyTemplate<bool>* > (i->second)->val ();
527 }