Fixup prev commit (LV2 X11 UI) -- #7837
[ardour.git] / libs / ardour / vca.cc
1 /*
2  * Copyright (C) 2016-2017 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2017 Robin Gareus <robin@gareus.org>
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 along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19
20 #include "pbd/convert.h"
21
22 #include "ardour/automation_control.h"
23 #include "ardour/debug.h"
24 #include "ardour/gain_control.h"
25 #include "ardour/monitor_control.h"
26 #include "ardour/rc_configuration.h"
27 #include "ardour/route.h"
28 #include "ardour/session.h"
29 #include "ardour/vca.h"
30
31 #include "pbd/i18n.h"
32
33 using namespace ARDOUR;
34 using namespace PBD;
35 using std::string;
36
37 Glib::Threads::Mutex VCA::number_lock;
38 int32_t VCA::next_number = 1;
39 string VCA::xml_node_name (X_("VCA"));
40
41 string
42 VCA::default_name_template ()
43 {
44         return _("VCA %n");
45 }
46
47 int32_t
48 VCA::next_vca_number ()
49 {
50         /* we could use atomic inc here, but elsewhere we need more complete
51            mutex semantics, so we have to do it here also.
52         */
53         Glib::Threads::Mutex::Lock lm (number_lock);
54         return next_number++;
55 }
56
57 void
58 VCA::set_next_vca_number (int32_t n)
59 {
60         Glib::Threads::Mutex::Lock lm (number_lock);
61         next_number = n;
62 }
63
64 int32_t
65 VCA::get_next_vca_number ()
66 {
67         Glib::Threads::Mutex::Lock lm (number_lock);
68         return next_number;
69 }
70
71 VCA::VCA (Session& s, int32_t num, const string& name)
72         : Stripable (s, name, PresentationInfo (num, PresentationInfo::VCA))
73         , Muteable (s, name)
74         , _number (num)
75         , _gain_control (new GainControl (s, Evoral::Parameter (GainAutomation), boost::shared_ptr<AutomationList> ()))
76 {
77 }
78
79 int
80 VCA::init ()
81 {
82         _solo_control.reset (new SoloControl (_session, X_("solo"), *this, *this));
83         _mute_control.reset (new MuteControl (_session, X_("mute"), *this));
84
85         add_control (_gain_control);
86         add_control (_solo_control);
87         add_control (_mute_control);
88
89         return 0;
90 }
91
92 VCA::~VCA ()
93 {
94         DEBUG_TRACE (DEBUG::Destruction, string_compose ("delete VCA %1\n", number()));
95         {
96                 Glib::Threads::Mutex::Lock lm (_control_lock);
97                 for (Controls::const_iterator li = _controls.begin(); li != _controls.end(); ++li) {
98                         boost::dynamic_pointer_cast<AutomationControl>(li->second)->drop_references ();
99                 }
100         }
101         {
102                 Glib::Threads::Mutex::Lock lm (number_lock);
103                 if (_number == next_number - 1) {
104                         /* this was the "last" VCA added, so rewind the next number so
105                          * that future VCAs get numbered as intended
106                          */
107                         next_number--;
108                 }
109         }
110 }
111
112 string
113 VCA::full_name() const
114 {
115         /* name() is never empty - default is VCA %n */
116         return string_compose (_("VCA %1 : %2"), _number, name());
117 }
118
119 XMLNode&
120 VCA::get_state ()
121 {
122         XMLNode* node = new XMLNode (xml_node_name);
123         node->set_property (X_("name"), name());
124         node->set_property (X_("number"), _number);
125
126         node->add_child_nocopy (_presentation_info.get_state());
127
128         node->add_child_nocopy (_gain_control->get_state());
129         node->add_child_nocopy (_solo_control->get_state());
130         node->add_child_nocopy (_mute_control->get_state());
131         node->add_child_nocopy (get_automation_xml_state());
132
133         node->add_child_nocopy (Slavable::get_state());
134
135         return *node;
136 }
137
138 int
139 VCA::set_state (XMLNode const& node, int version)
140 {
141         Stripable::set_state (node, version);
142
143         std::string str;
144         if (node.get_property ("name", str)) {
145                 set_name (str);
146         }
147
148         node.get_property ("number", _number);
149
150         XMLNodeList const &children (node.children());
151         for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
152                 if ((*i)->name() == Controllable::xml_node_name) {
153
154                         if (!(*i)->get_property ("name", str)) {
155                                 continue;
156                         }
157
158                         if (str == _gain_control->name()) {
159                                 _gain_control->set_state (**i, version);
160                         }
161                         if (str == _solo_control->name()) {
162                                 _solo_control->set_state (**i, version);
163                         }
164                         if (str == _mute_control->name()) {
165                                 _mute_control->set_state (**i, version);
166                         }
167                 } else if ((*i)->name() == Slavable::xml_node_name) {
168                         Slavable::set_state (**i, version);
169                 } else if ((*i)->name() == Automatable::xml_node_name) {
170                         set_automation_xml_state (**i, Evoral::Parameter(NullAutomation));
171                 }
172         }
173
174         return 0;
175 }
176
177 void
178 VCA::clear_all_solo_state ()
179 {
180         _solo_control->clear_all_solo_state ();
181 }
182
183 MonitorState
184 VCA::monitoring_state () const
185 {
186         /* XXX this has to get more complex but not clear how */
187         return MonitoringInput;
188 }
189
190 bool
191 VCA::slaved () const
192 {
193         if (!_gain_control) {
194                 return false;
195         }
196         /* just test one particular control, not all of them */
197         return _gain_control->slaved ();
198 }
199
200 bool
201 VCA::slaved_to (boost::shared_ptr<VCA> vca) const
202 {
203         if (!vca || !_gain_control) {
204                 return false;
205         }
206
207         /* just test one particular control, not all of them */
208
209         return _gain_control->slaved_to (vca->gain_control());
210 }
211
212 void
213 VCA::assign (boost::shared_ptr<VCA> v)
214 {
215         /* prevent recursive assignments */
216         if (assigned_to (_session.vca_manager_ptr (), v)) {
217                 warning << _("Master assignment inored to prevent recursion") << endmsg;
218                 return;
219         }
220         Slavable::assign (v);
221 }
222
223 SlavableControlList
224 VCA::slavables () const
225 {
226         SlavableControlList rv;
227         rv.push_back (_gain_control);
228         rv.push_back (_mute_control);
229         rv.push_back (_solo_control);
230         return rv;
231 }