Merged with trunk R1612.
[ardour.git] / libs / ardour / route_group.cc
1 /*
2     Copyright (C) 2000-2002 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 #include <sigc++/bind.h>
26
27 #include <pbd/error.h>
28 #include <pbd/enumwriter.h>
29
30 #include <ardour/route_group.h>
31 #include <ardour/audio_track.h>
32 #include <ardour/audio_diskstream.h>
33 #include <ardour/configuration.h>
34
35 using namespace ARDOUR;
36 using namespace sigc;
37 using namespace std;
38
39 RouteGroup::RouteGroup (Session& s, const string &n, Flag f)
40         : _session (s), _name (n), _flags (f) 
41 {
42 }
43
44 void
45 RouteGroup::set_name (string str)
46 {
47         _name = str;
48         _session.set_dirty ();
49         FlagsChanged (0); /* EMIT SIGNAL */
50 }
51
52 int
53 RouteGroup::add (Route *r)
54 {
55         routes.push_back (r);
56         r->GoingAway.connect (sigc::bind (mem_fun (*this, &RouteGroup::remove_when_going_away), r));
57         _session.set_dirty ();
58         changed (); /* EMIT SIGNAL */
59         return 0;
60 }
61
62 void
63 RouteGroup::remove_when_going_away (Route *r)
64 {
65         remove (r);
66 }
67     
68 int
69 RouteGroup::remove (Route *r) 
70 {
71         list<Route *>::iterator i;
72
73         if ((i = find (routes.begin(), routes.end(), r)) != routes.end()) {
74                 routes.erase (i);
75                 _session.set_dirty ();
76                 changed (); /* EMIT SIGNAL */
77                 return 0;
78         }
79         return -1;
80 }
81
82
83 gain_t
84 RouteGroup::get_min_factor(gain_t factor)
85 {
86         gain_t g;
87         
88         for (list<Route *>::iterator i = routes.begin(); i != routes.end(); i++) {
89                 g = (*i)->gain();
90
91                 if ( (g+g*factor) >= 0.0f)
92                         continue;
93
94                 if ( g <= 0.0000003f )
95                         return 0.0f;
96                 
97                 factor = 0.0000003f/g - 1.0f;
98         }       
99         return factor;
100 }
101
102 gain_t
103 RouteGroup::get_max_factor(gain_t factor)
104 {
105         gain_t g;
106         
107         for (list<Route *>::iterator i = routes.begin(); i != routes.end(); i++) {
108                 g = (*i)->gain();
109                 
110                 // if the current factor woulnd't raise this route above maximum
111                 if ( (g+g*factor) <= 1.99526231f) 
112                         continue;
113                 
114                 // if route gain is already at peak, return 0.0f factor
115             if (g>=1.99526231f)
116                         return 0.0f;
117
118                 // factor is calculated so that it would raise current route to max
119                 factor = 1.99526231f/g - 1.0f;
120         }
121
122         return factor;
123 }
124
125 XMLNode&
126 RouteGroup::get_state (void)
127 {
128         XMLNode *node = new XMLNode ("RouteGroup");
129         node->add_property ("name", _name);
130         node->add_property ("flags", enum_2_string (_flags));
131         return *node;
132 }
133
134 int 
135 RouteGroup::set_state (const XMLNode& node)
136 {
137         const XMLProperty *prop;
138
139         if ((prop = node.property ("name")) != 0) {
140                 _name = prop->value();
141         }
142
143         if ((prop = node.property ("flags")) != 0) {
144                 _flags = Flag (string_2_enum (prop->value(), _flags));
145         }
146
147         return 0;
148 }
149
150 void
151 RouteGroup::set_active (bool yn, void *src)
152
153 {
154         if (is_active() == yn) {
155                 return;
156         }
157         if (yn) {
158                 _flags = Flag (_flags | Active);
159         } else {
160                 _flags = Flag (_flags & ~Active);
161         }
162         _session.set_dirty ();
163         FlagsChanged (src); /* EMIT SIGNAL */
164 }
165
166 void
167 RouteGroup::set_relative (bool yn, void *src)
168
169 {
170         if (is_relative() == yn) {
171                 return;
172         }
173         if (yn) {
174                 _flags = Flag (_flags | Relative);
175         } else {
176                 _flags = Flag (_flags & ~Relative);
177         }
178         _session.set_dirty ();
179         FlagsChanged (src); /* EMIT SIGNAL */
180 }
181
182 void
183 RouteGroup::set_hidden (bool yn, void *src)
184
185 {
186         if (is_hidden() == yn) {
187                 return;
188         }
189         if (yn) {
190                 _flags = Flag (_flags | Hidden);
191                 if (Config->get_hiding_groups_deactivates_groups()) {
192                         _flags = Flag (_flags & ~Active);
193                 }
194         } else {
195                 _flags = Flag (_flags & ~Hidden);
196                 if (Config->get_hiding_groups_deactivates_groups()) {
197                         _flags = Flag (_flags | Active);
198                 }
199         }
200         _session.set_dirty ();
201         FlagsChanged (src); /* EMIT SIGNAL */
202 }
203
204 void
205 RouteGroup::audio_track_group (set<AudioTrack*>& ats) 
206 {       
207         for (list<Route*>::iterator i = routes.begin(); i != routes.end(); ++i) {
208                 AudioTrack* at = dynamic_cast<AudioTrack*>(*i);
209                 if (at) {
210                         ats.insert (at);
211                 }
212         }
213 }
214