5e49a11875ad7fbd4b7f742d21fa42841f98bb3d
[ardour.git] / libs / ardour / mute_master.cc
1 /*
2
3     Copyright (C) 2009 Paul Davis
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #include "pbd/enumwriter.h"
22 #include "pbd/xml++.h"
23
24 #include "ardour/types.h"
25 #include "ardour/mute_master.h"
26 #include "ardour/session.h"
27
28 #include "i18n.h"
29
30 using namespace ARDOUR;
31 using namespace std;
32
33 const MuteMaster::MutePoint MuteMaster::AllPoints = MutePoint (MuteMaster::PreFader|
34                                                                MuteMaster::PostFader|
35                                                                MuteMaster::Listen|
36                                                                MuteMaster::Main);
37
38 MuteMaster::MuteMaster (Session& s, const std::string&)
39         : SessionHandleRef (s) 
40         , _mute_point (AllPoints)
41         , _self_muted (false)
42         , _muted_by_others (0)
43         , _solo_ignore (false)
44 {
45 }
46
47 void
48 MuteMaster::mute_at (MutePoint mp)
49 {
50         if ((_mute_point & mp) != mp) {
51                 _mute_point = MutePoint (_mute_point | mp);
52                 cerr << "Mute point set, now " << _mute_point << endl;
53                 MutePointChanged (); // EMIT SIGNAL
54         }
55 }
56
57 void
58 MuteMaster::unmute_at (MutePoint mp)
59 {
60         if ((_mute_point & mp) == mp) {
61                 _mute_point = MutePoint (_mute_point & ~mp);
62                 cerr << "Mute point unset, now " << _mute_point << endl;
63                 MutePointChanged (); // EMIT SIGNAL
64         }
65 }
66
67 void
68 MuteMaster::clear_muted_by_others ()
69 {
70         _muted_by_others = 0;
71 }
72
73 void
74 MuteMaster::mod_muted_by_others (int32_t delta)
75 {
76         if (delta < 0) {
77                 if (_muted_by_others >= (uint32_t) abs (delta)) {
78                         _muted_by_others += delta;
79                 } else {
80                         _muted_by_others = 0;
81                 }
82         } else {
83                 _muted_by_others += delta;
84         }
85 }
86
87 void
88 MuteMaster::set_solo_level (SoloLevel l)
89 {
90         _solo_level = l;
91 }
92
93 gain_t
94 MuteMaster::mute_gain_at (MutePoint mp) const
95 {
96         gain_t gain;
97         const SoloLevel l = _solo_level;
98
99         // cerr << "solo level = " << _solo_level << " selfmuted " <<  self_muted_at (mp) << " omute " << muted_by_others_at (mp) << endl;
100         
101         if (Config->get_solo_mute_override()) {
102                 if ((l == SelfSoloed) || (l == DownstreamSoloed)) { 
103                         gain = 1.0;
104                 } else if (self_muted_at (mp)) { // self-muted 
105                         gain = Config->get_solo_mute_gain ();
106                 } else if (l == UpstreamSoloed) {
107                         gain = 1.0;
108                 } else if (muted_by_others_at (mp)) { // muted by others
109                         gain = Config->get_solo_mute_gain ();
110                 } else {
111                         if (!_solo_ignore && _session.soloing()) {
112                                 gain = 0.0;
113                         } else {
114                                 gain = 1.0;
115                         }
116                 }
117         } else {
118                 if (self_muted_at (mp)) { // self-muted 
119                         gain = Config->get_solo_mute_gain ();
120                 } else if ((l == SelfSoloed) || (l == DownstreamSoloed)) {
121                         gain = 1.0;
122                 } else if (muted_by_others_at (mp)) { // muted by others
123                         gain = Config->get_solo_mute_gain ();
124                 } else if (l == UpstreamSoloed) { // soloed by others
125                         gain = 1.0;
126                 } else {
127                         if (!_solo_ignore && _session.soloing()) {
128                                 gain = 0.0;
129                         } else {
130                                 gain = 1.0;
131                         }
132                 }
133         }
134         
135         // cerr << "\tgain = " << gain << endl;
136         
137         return gain;
138 }
139
140 void
141 MuteMaster::set_mute_points (const std::string& mute_point)
142 {
143         MutePoint old = _mute_point;
144
145         _mute_point = (MutePoint) string_2_enum (mute_point, _mute_point);
146         cerr << "Mute point set from string, now " << _mute_point << endl;
147         
148         if (old != _mute_point) {
149                 MutePointChanged(); /* EMIT SIGNAL */
150         }
151 }
152
153 void
154 MuteMaster::set_mute_points (MutePoint mp) 
155 {
156         if (_mute_point != mp) {
157                 _mute_point = mp;
158                 cerr << "Mute point set from mp, now " << _mute_point << endl;
159                 MutePointChanged (); /* EMIT SIGNAL */
160         }
161 }
162
163 int
164 MuteMaster::set_state (const XMLNode& node, int /*version*/)
165 {
166         const XMLProperty* prop;
167
168         if ((prop = node.property ("mute-point")) != 0) {
169                 _mute_point = (MutePoint) string_2_enum (prop->value(), _mute_point);
170                 cerr << "Mute point set from STATE string, now " << _mute_point << endl;
171         }
172
173         if ((prop = node.property ("muted")) != 0) {
174                 _self_muted = string_is_affirmative (prop->value());
175         } else {
176                 _self_muted = (_mute_point != MutePoint (0));
177         }
178
179         if ((prop = node.property ("muted-by-others")) != 0) {
180                 if (sscanf (prop->value().c_str(), "%u", &_muted_by_others) != 1) {
181                         _muted_by_others = 0;
182                 }
183         } else {
184                 _muted_by_others = 0;
185         }
186
187         return 0;
188 }
189
190 XMLNode&
191 MuteMaster::get_state()
192 {
193         XMLNode* node = new XMLNode (X_("MuteMaster"));
194         node->add_property ("mute-point", enum_2_string (_mute_point));
195         node->add_property ("muted", (_self_muted ? X_("yes") : X_("no")));
196
197         char buf[32];
198         snprintf (buf, sizeof (buf), "%u", _muted_by_others);
199         node->add_property ("muted-by-others", buf);
200
201         return *node;
202 }