ae807e66e15b3b8186fbeb401d0d2362067c1c65
[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     $Id$
19 */
20
21 #define __STDC_FORMAT_MACROS
22 #include <inttypes.h>
23
24 #include <algorithm>
25
26 #include <sigc++/bind.h>
27
28 #include <pbd/error.h>
29
30 #include <ardour/route_group.h>
31 #include <ardour/audio_track.h>
32 #include <ardour/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         char buf[32];
129         XMLNode *node = new XMLNode ("RouteGroup");
130         node->add_property ("name", _name);
131         snprintf (buf, sizeof (buf), "%" PRIu32, (uint32_t) _flags);
132         node->add_property ("flags", buf);
133         return *node;
134 }
135
136 int 
137 RouteGroup::set_state (const XMLNode& node)
138 {
139         const XMLProperty *prop;
140
141         if ((prop = node.property ("name")) != 0) {
142                 _name = prop->value();
143         }
144
145         if ((prop = node.property ("flags")) != 0) {
146                 _flags = atoi (prop->value().c_str());
147         }
148
149         return 0;
150 }
151
152 void
153 RouteGroup::set_active (bool yn, void *src)
154
155 {
156         if (is_active() == yn) {
157                 return;
158         }
159         if (yn) {
160                 _flags |= Active;
161         } else {
162                 _flags &= ~Active;
163         }
164         _session.set_dirty ();
165         FlagsChanged (src); /* EMIT SIGNAL */
166 }
167
168 void
169 RouteGroup::set_relative (bool yn, void *src)
170
171 {
172         if (is_relative() == yn) {
173                 return;
174         }
175         if (yn) {
176                 _flags |= Relative;
177         } else {
178                 _flags &= ~Relative;
179         }
180         _session.set_dirty ();
181         FlagsChanged (src); /* EMIT SIGNAL */
182 }
183
184 void
185 RouteGroup::set_hidden (bool yn, void *src)
186
187 {
188         if (is_hidden() == yn) {
189                 return;
190         }
191         if (yn) {
192                 _flags |= Hidden;
193                 if (Config->get_hiding_groups_deactivates_groups()) {
194                         _flags &= ~Active;
195                 }
196         } else {
197                 _flags &= ~Hidden;
198                 if (Config->get_hiding_groups_deactivates_groups()) {
199                         _flags |= Active;
200                 }
201         }
202         _session.set_dirty ();
203         FlagsChanged (src); /* EMIT SIGNAL */
204 }
205
206 void
207 RouteGroup::audio_track_group (set<AudioTrack*>& ats) 
208 {       
209         for (list<Route*>::iterator i = routes.begin(); i != routes.end(); ++i) {
210                 AudioTrack* at = dynamic_cast<AudioTrack*>(*i);
211                 if (at) {
212                         ats.insert (at);
213                 }
214         }
215 }
216