design changes to various SlavableAutomationControls to make it possible to get the...
[ardour.git] / libs / ardour / mute_control.cc
1 /*
2     Copyright (C) 2016 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify it
5     under the terms of the GNU General Public License as published by the Free
6     Software Foundation; either version 2 of the License, or (at your option)
7     any later version.
8
9     This program is distributed in the hope that it will be useful, but WITHOUT
10     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12     for more details.
13
14     You should have received a copy of the GNU General Public License along
15     with this program; if not, write to the Free Software Foundation, Inc.,
16     675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include "evoral/ControlList.hpp"
20
21 #include "ardour/mute_master.h"
22 #include "ardour/session.h"
23 #include "ardour/mute_control.h"
24
25 #include "i18n.h"
26
27 using namespace ARDOUR;
28 using namespace std;
29
30
31 MuteControl::MuteControl (Session& session, std::string const & name, Muteable& m)
32         : SlavableAutomationControl (session, MuteAutomation, ParameterDescriptor (MuteAutomation),
33                                      boost::shared_ptr<AutomationList> (new AutomationList (Evoral::Parameter (MuteAutomation))),
34                                      name)
35         , _muteable (m)
36 {
37         _list->set_interpolation (Evoral::ControlList::Discrete);
38         /* mute changes must be synchronized by the process cycle */
39         set_flags (Controllable::Flag (flags() | Controllable::RealTime));
40 }
41
42 void
43 MuteControl::post_add_master (boost::shared_ptr<AutomationControl> m)
44 {
45         if (m->get_value()) {
46
47                 /* boolean masters records are not updated until AFTER
48                  * ::post_add_master() is called, so we can use them to check
49                  * on whether any master was already enabled before the new
50                  * one was added.
51                  */
52
53                 if (!muted_by_self() && !get_boolean_masters()) {
54                         Changed (false, Controllable::NoGroup);
55                 }
56         }
57 }
58
59 void
60 MuteControl::pre_remove_master (boost::shared_ptr<AutomationControl> m)
61 {
62         if (!m) {
63                 /* null control ptr means we're removing all masters */
64                 _muteable.mute_master()->set_muted_by_others (false);
65                 /* Changed will be emitted in SlavableAutomationControl::clear_masters() */
66                 return;
67         }
68
69         if (m->get_value()) {
70                 if (!muted_by_self() && (muted_by_others() == 1)) {
71                         Changed (false, Controllable::NoGroup);
72                 }
73         }
74 }
75
76 void
77 MuteControl::actually_set_value (double val, Controllable::GroupControlDisposition gcd)
78 {
79         if (muted_by_self() != bool (val)) {
80                 _muteable.mute_master()->set_muted_by_self (val);
81
82                 /* allow the Muteable to respond to the mute change
83                    before anybody else knows about it.
84                 */
85                 _muteable.act_on_mute ();
86         }
87
88         AutomationControl::actually_set_value (val, gcd);
89 }
90
91 void
92 MuteControl::master_changed (bool self_change, Controllable::GroupControlDisposition gcd, boost::shared_ptr<AutomationControl> m)
93 {
94         bool send_signal = false;
95         const double changed_master_value = m->get_value();
96         boost::shared_ptr<MuteControl> mc = boost::dynamic_pointer_cast<MuteControl> (m);
97
98         if (m) {
99                 cerr << "master changed, self ? " << self_change << " self muted = "
100                      << mc->muted_by_self() << " others " << mc->muted_by_others()
101                      << endl;
102         }
103
104         if (changed_master_value) {
105                 /* this master is now enabled */
106                 if (!muted_by_self() && get_boolean_masters() == 0) {
107                         send_signal = true;
108                 }
109         } else {
110                 if (!muted_by_self() && get_boolean_masters() == 1) {
111                         send_signal = true;
112                 }
113         }
114
115         update_boolean_masters_records (m);
116
117         if (send_signal) {
118                 Changed (false, Controllable::NoGroup);
119         }
120 }
121
122 double
123 MuteControl::get_value () const
124 {
125         if (slaved ()) {
126                 Glib::Threads::RWLock::ReaderLock lm (master_lock);
127                 return get_masters_value_locked ();
128         }
129
130         if (_list && boost::dynamic_pointer_cast<AutomationList>(_list)->automation_playback()) {
131                 // Playing back automation, get the value from the list
132                 return AutomationControl::get_value();
133         }
134
135         return muted() ? 1.0 : 0.0;
136 }
137
138 void
139 MuteControl::set_mute_points (MuteMaster::MutePoint mp)
140 {
141         _muteable.mute_master()->set_mute_points (mp);
142         _muteable.mute_points_changed (); /* EMIT SIGNAL */
143
144         if (_muteable.mute_master()->muted_by_self()) {
145                 Changed (true, Controllable::UseGroup); /* EMIT SIGNAL */
146         }
147 }
148
149 MuteMaster::MutePoint
150 MuteControl::mute_points () const
151 {
152         return _muteable.mute_master()->mute_points ();
153 }
154
155 bool
156 MuteControl::muted () const
157 {
158         return muted_by_self() || muted_by_others();
159 }
160
161 bool
162 MuteControl::muted_by_self () const
163 {
164         return _muteable.mute_master()->muted_by_self();
165 }
166
167 bool
168 MuteControl::muted_by_others () const
169 {
170         return get_masters_value ();
171 }