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