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