Optimize automation-event process splitting
[ardour.git] / libs / ardour / mute_master.cc
1 /*
2     Copyright (C) 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 #include "pbd/enumwriter.h"
21 #include "pbd/xml++.h"
22 #include "pbd/enum_convert.h"
23
24 #include "ardour/types.h"
25 #include "ardour/mute_master.h"
26 #include "ardour/session.h"
27
28 #include "pbd/i18n.h"
29
30 namespace PBD {
31         DEFINE_ENUM_CONVERT(ARDOUR::MuteMaster::MutePoint);
32 }
33
34 using namespace ARDOUR;
35 using namespace std;
36
37 const string MuteMaster::xml_node_name (X_("MuteMaster"));
38
39 const MuteMaster::MutePoint MuteMaster::AllPoints = MuteMaster::MutePoint(
40         PreFader|PostFader|Listen|Main);
41
42 MuteMaster::MuteMaster (Session& s, Muteable& m, const std::string&)
43         : SessionHandleRef (s)
44         , _muteable (&m)
45         , _mute_point (MutePoint (0))
46         , _muted_by_self (false)
47         , _soloed_by_self (false)
48         , _soloed_by_others (false)
49         , _muted_by_masters (0)
50 {
51
52         if (Config->get_mute_affects_pre_fader ()) {
53                 _mute_point = MutePoint (_mute_point | PreFader);
54         }
55
56         if (Config->get_mute_affects_post_fader ()) {
57                 _mute_point = MutePoint (_mute_point | PostFader);
58         }
59
60         if (Config->get_mute_affects_control_outs ()) {
61                 _mute_point = MutePoint (_mute_point | Listen);
62         }
63
64         if (Config->get_mute_affects_main_outs ()) {
65                 _mute_point = MutePoint (_mute_point | Main);
66         }
67 }
68
69 void
70 MuteMaster::mute_at (MutePoint mp)
71 {
72         if ((_mute_point & mp) != mp) {
73                 _mute_point = MutePoint (_mute_point | mp);
74                 MutePointChanged (); // EMIT SIGNAL
75         }
76 }
77
78 void
79 MuteMaster::unmute_at (MutePoint mp)
80 {
81         if ((_mute_point & mp) == mp) {
82                 _mute_point = MutePoint (_mute_point & ~mp);
83                 MutePointChanged (); // EMIT SIGNAL
84         }
85 }
86
87 gain_t
88 MuteMaster::mute_gain_at (MutePoint mp) const
89 {
90         gain_t gain;
91
92         if (Config->get_solo_mute_override()) {
93                 if (_soloed_by_self) {
94                         gain = GAIN_COEFF_UNITY;
95                 } else if (muted_by_self_at (mp) || muted_by_masters_at (mp)) {
96                         gain = GAIN_COEFF_ZERO;
97                 } else {
98                         if (!_soloed_by_others && muted_by_others_soloing_at (mp)) {
99                                 gain = Config->get_solo_mute_gain ();
100                         } else {
101                                 gain = GAIN_COEFF_UNITY;
102                         }
103                 }
104         } else {
105                 if (muted_by_self_at (mp) || muted_by_masters_at (mp)) {
106                         gain = GAIN_COEFF_ZERO;
107                 } else if (_soloed_by_self || _soloed_by_others) {
108                         gain = GAIN_COEFF_UNITY;
109                 } else {
110                         if (muted_by_others_soloing_at (mp)) {
111                                 gain = Config->get_solo_mute_gain ();
112                         } else {
113                                 gain = GAIN_COEFF_UNITY;
114                         }
115                 }
116         }
117
118         return gain;
119 }
120
121 void
122 MuteMaster::set_mute_points (const std::string& mute_point)
123 {
124         MutePoint old = _mute_point;
125
126         _mute_point = (MutePoint) string_2_enum (mute_point, _mute_point);
127
128         if (old != _mute_point) {
129                 MutePointChanged(); /* EMIT SIGNAL */
130         }
131 }
132
133 void
134 MuteMaster::set_mute_points (MutePoint mp)
135 {
136         if (_mute_point != mp) {
137                 _mute_point = mp;
138                 MutePointChanged (); /* EMIT SIGNAL */
139         }
140 }
141
142 int
143 MuteMaster::set_state (const XMLNode& node, int /*version*/)
144 {
145         node.get_property ("mute-point", _mute_point);
146
147         if (!node.get_property ("muted", _muted_by_self)) {
148                 _muted_by_self = (_mute_point != MutePoint (0));
149         }
150
151         return 0;
152 }
153
154 XMLNode&
155 MuteMaster::get_state()
156 {
157         XMLNode* node = new XMLNode (xml_node_name);
158         node->set_property ("mute-point", _mute_point);
159         node->set_property ("muted", _muted_by_self);
160         return *node;
161 }
162
163 bool
164 MuteMaster::muted_by_others_soloing_at (MutePoint mp) const
165 {
166         return _muteable->muted_by_others_soloing() && (_mute_point & mp);
167 }
168
169 void
170 MuteMaster::set_muted_by_masters (bool yn)
171 {
172         _muted_by_masters = yn;
173 }