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