extensive changes to PresentationInfo API
[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 "i18n.h"
31
32 using namespace ARDOUR;
33 using namespace PBD;
34 using std::string;
35
36 Glib::Threads::Mutex VCA::number_lock;
37 uint32_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 uint32_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 (uint32_t n)
58 {
59         Glib::Threads::Mutex::Lock lm (number_lock);
60         next_number = n;
61 }
62
63 uint32_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,  uint32_t num, const string& name)
71         : Stripable (s, name, PresentationInfo (num, PresentationInfo::VCA))
72         , Muteable (s, name)
73         , Automatable (s)
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 (number_lock);
97                 if (_number == next_number - 1) {
98                         /* this was the "last" VCA added, so rewind the next number so
99                          * that future VCAs get numbered as intended
100                          */
101                         next_number--;
102                 }
103         }
104 }
105
106 XMLNode&
107 VCA::get_state ()
108 {
109         XMLNode* node = new XMLNode (xml_node_name);
110         node->add_property (X_("name"), _name);
111         node->add_property (X_("number"), _number);
112
113         node->add_child_nocopy (_presentation_info.get_state());
114
115         node->add_child_nocopy (_gain_control->get_state());
116         node->add_child_nocopy (_solo_control->get_state());
117         node->add_child_nocopy (_mute_control->get_state());
118         node->add_child_nocopy (get_automation_xml_state());
119
120         node->add_child_nocopy (Slavable::get_state());
121
122         return *node;
123 }
124
125 int
126 VCA::set_state (XMLNode const& node, int version)
127 {
128         XMLProperty const* prop;
129
130         Stripable::set_state (node, version);
131
132         if ((prop = node.property ("name")) != 0) {
133                 set_name (prop->value());
134         }
135
136         if ((prop = node.property ("number")) != 0) {
137                 _number = atoi (prop->value());
138         }
139
140         XMLNodeList const &children (node.children());
141         for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
142                 if ((*i)->name() == Controllable::xml_node_name) {
143
144                         XMLProperty* prop = (*i)->property ("name");
145
146                         if (!prop) {
147                                 continue;
148                         }
149
150                         if (prop->value() == _gain_control->name()) {
151                                 _gain_control->set_state (**i, version);
152                         }
153                         if (prop->value() == _solo_control->name()) {
154                                 _solo_control->set_state (**i, version);
155                         }
156                         if (prop->value() == _mute_control->name()) {
157                                 _mute_control->set_state (**i, version);
158                         }
159                 } else if ((*i)->name() == Slavable::xml_node_name) {
160                         Slavable::set_state (**i, version);
161                 }
162         }
163
164         return 0;
165 }
166
167 void
168 VCA::clear_all_solo_state ()
169 {
170         _solo_control->clear_all_solo_state ();
171 }
172
173 MonitorState
174 VCA::monitoring_state () const
175 {
176         /* XXX this has to get more complex but not clear how */
177         return MonitoringInput;
178 }
179
180 bool
181 VCA::slaved () const
182 {
183         if (!_gain_control) {
184                 return false;
185         }
186         /* just test one particular control, not all of them */
187         return _gain_control->slaved ();
188 }
189
190 bool
191 VCA::slaved_to (boost::shared_ptr<VCA> vca) const
192 {
193         if (!vca || !_gain_control) {
194                 return false;
195         }
196
197         /* just test one particular control, not all of them */
198
199         return _gain_control->slaved_to (vca->gain_control());
200 }