add new sigc++2 directory
[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         if (is_active() == yn) {
154                 return;
155         }
156         if (yn) {
157                 _flags = Flag (_flags | Active);
158         } else {
159                 _flags = Flag (_flags & ~Active);
160         }
161         _session.set_dirty ();
162         FlagsChanged (src); /* EMIT SIGNAL */
163 }
164
165 void
166 RouteGroup::set_relative (bool yn, void *src)
167
168 {
169         if (is_relative() == yn) {
170                 return;
171         }
172         if (yn) {
173                 _flags = Flag (_flags | Relative);
174         } else {
175                 _flags = Flag (_flags & ~Relative);
176         }
177         _session.set_dirty ();
178         FlagsChanged (src); /* EMIT SIGNAL */
179 }
180
181 void
182 RouteGroup::set_hidden (bool yn, void *src)
183
184 {
185         if (is_hidden() == yn) {
186                 return;
187         }
188         if (yn) {
189                 _flags = Flag (_flags | Hidden);
190                 if (Config->get_hiding_groups_deactivates_groups()) {
191                         _flags = Flag (_flags & ~Active);
192                 }
193         } else {
194                 _flags = Flag (_flags & ~Hidden);
195                 if (Config->get_hiding_groups_deactivates_groups()) {
196                         _flags = Flag (_flags | Active);
197                 }
198         }
199         _session.set_dirty ();
200         FlagsChanged (src); /* EMIT SIGNAL */
201 }
202
203 void
204 RouteGroup::audio_track_group (set<AudioTrack*>& ats) 
205 {       
206         for (list<Route*>::iterator i = routes.begin(); i != routes.end(); ++i) {
207                 AudioTrack* at = dynamic_cast<AudioTrack*>(*i);
208                 if (at) {
209                         ats.insert (at);
210                 }
211         }
212 }
213