3eff1a6b4554512f1274a5ef99ada88dffda9927
[ardour.git] / libs / ardour / vca.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 "pbd/convert.h"
20
21 #include "ardour/automation_control.h"
22 #include "ardour/debug.h"
23 #include "ardour/gain_control.h"
24 #include "ardour/monitor_control.h"
25 #include "ardour/rc_configuration.h"
26 #include "ardour/route.h"
27 #include "ardour/session.h"
28 #include "ardour/vca.h"
29
30 #include "pbd/i18n.h"
31
32 using namespace ARDOUR;
33 using namespace PBD;
34 using std::string;
35
36 Glib::Threads::Mutex VCA::number_lock;
37 int32_t VCA::next_number = 1;
38 string VCA::xml_node_name (X_("VCA"));
39
40 string
41 VCA::default_name_template ()
42 {
43         return _("VCA %n");
44 }
45
46 int32_t
47 VCA::next_vca_number ()
48 {
49         /* we could use atomic inc here, but elsewhere we need more complete
50            mutex semantics, so we have to do it here also.
51         */
52         Glib::Threads::Mutex::Lock lm (number_lock);
53         return next_number++;
54 }
55
56 void
57 VCA::set_next_vca_number (int32_t n)
58 {
59         Glib::Threads::Mutex::Lock lm (number_lock);
60         next_number = n;
61 }
62
63 int32_t
64 VCA::get_next_vca_number ()
65 {
66         Glib::Threads::Mutex::Lock lm (number_lock);
67         return next_number;
68 }
69
70 VCA::VCA (Session& s, int32_t num, const string& name)
71         : Stripable (s, name, PresentationInfo (num, PresentationInfo::VCA))
72         , Muteable (s, name)
73         , _number (num)
74         , _gain_control (new GainControl (s, Evoral::Parameter (GainAutomation), boost::shared_ptr<AutomationList> ()))
75 {
76 }
77
78 int
79 VCA::init ()
80 {
81         _solo_control.reset (new SoloControl (_session, X_("solo"), *this, *this));
82         _mute_control.reset (new MuteControl (_session, X_("mute"), *this));
83
84         add_control (_gain_control);
85         add_control (_solo_control);
86         add_control (_mute_control);
87
88         return 0;
89 }
90
91 VCA::~VCA ()
92 {
93         DEBUG_TRACE (DEBUG::Destruction, string_compose ("delete VCA %1\n", number()));
94         {
95                 Glib::Threads::Mutex::Lock lm (number_lock);
96                 if (_number == next_number - 1) {
97                         /* this was the "last" VCA added, so rewind the next number so
98                          * that future VCAs get numbered as intended
99                          */
100                         next_number--;
101                 }
102         }
103 }
104
105 string
106 VCA::full_name() const
107 {
108         /* name() is never empty - default is VCA %n */
109         return string_compose (_("VCA %1 : %2"), _number, name());
110 }
111
112 XMLNode&
113 VCA::get_state ()
114 {
115         XMLNode* node = new XMLNode (xml_node_name);
116         node->set_property (X_("name"), name());
117         node->set_property (X_("number"), _number);
118
119         node->add_child_nocopy (_presentation_info.get_state());
120
121         node->add_child_nocopy (_gain_control->get_state());
122         node->add_child_nocopy (_solo_control->get_state());
123         node->add_child_nocopy (_mute_control->get_state());
124         node->add_child_nocopy (get_automation_xml_state());
125
126         node->add_child_nocopy (Slavable::get_state());
127
128         return *node;
129 }
130
131 int
132 VCA::set_state (XMLNode const& node, int version)
133 {
134         Stripable::set_state (node, version);
135
136         std::string str;
137         if (node.get_property ("name", str)) {
138                 set_name (str);
139         }
140
141         node.get_property ("number", _number);
142
143         XMLNodeList const &children (node.children());
144         for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
145                 if ((*i)->name() == Controllable::xml_node_name) {
146
147                         if (!(*i)->get_property ("name", str)) {
148                                 continue;
149                         }
150
151                         if (str == _gain_control->name()) {
152                                 _gain_control->set_state (**i, version);
153                         }
154                         if (str == _solo_control->name()) {
155                                 _solo_control->set_state (**i, version);
156                         }
157                         if (str == _mute_control->name()) {
158                                 _mute_control->set_state (**i, version);
159                         }
160                 } else if ((*i)->name() == Slavable::xml_node_name) {
161                         Slavable::set_state (**i, version);
162                 }
163         }
164
165         return 0;
166 }
167
168 void
169 VCA::clear_all_solo_state ()
170 {
171         _solo_control->clear_all_solo_state ();
172 }
173
174 MonitorState
175 VCA::monitoring_state () const
176 {
177         /* XXX this has to get more complex but not clear how */
178         return MonitoringInput;
179 }
180
181 bool
182 VCA::slaved () const
183 {
184         if (!_gain_control) {
185                 return false;
186         }
187         /* just test one particular control, not all of them */
188         return _gain_control->slaved ();
189 }
190
191 bool
192 VCA::slaved_to (boost::shared_ptr<VCA> vca) const
193 {
194         if (!vca || !_gain_control) {
195                 return false;
196         }
197
198         /* just test one particular control, not all of them */
199
200         return _gain_control->slaved_to (vca->gain_control());
201 }