VCA: fix numbering scheme to allow contiguous numbers after removing the last VCA...
[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/record_enable_control.h"
27 #include "ardour/route.h"
28 #include "ardour/session.h"
29 #include "ardour/vca.h"
30
31 #include "i18n.h"
32
33 using namespace ARDOUR;
34 using namespace PBD;
35 using std::string;
36
37 Glib::Threads::Mutex VCA::number_lock;
38 uint32_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 uint32_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 (uint32_t n)
59 {
60         Glib::Threads::Mutex::Lock lm (number_lock);
61         next_number = n;
62 }
63
64 uint32_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,  uint32_t num, const string& name)
72         : Stripable (s, name, PresentationInfo (num, PresentationInfo::VCA))
73         , Muteable (s, name)
74         , Automatable (s)
75         , _number (num)
76         , _gain_control (new GainControl (s, Evoral::Parameter (GainAutomation), boost::shared_ptr<AutomationList> ()))
77 {
78 }
79
80 int
81 VCA::init ()
82 {
83         _solo_control.reset (new SoloControl (_session, X_("solo"), *this, *this));
84         _mute_control.reset (new MuteControl (_session, X_("mute"), *this));
85         _recenable_control.reset (new RecordEnableControl (_session, X_("recenable"), *this));
86         _monitor_control.reset (new MonitorControl (_session, X_("monitoring"), *this));
87
88         add_control (_gain_control);
89         add_control (_solo_control);
90         add_control (_mute_control);
91
92         return 0;
93 }
94
95 VCA::~VCA ()
96 {
97         DEBUG_TRACE (DEBUG::Destruction, string_compose ("delete VCA %1\n", number()));
98         {
99                 Glib::Threads::Mutex::Lock lm (number_lock);
100                 if (_number == next_number - 1) {
101                         /* this was the "last" VCA added, so rewind the next number so
102                          * that future VCAs get numbered as intended
103                          */
104                         next_number--;
105                 }
106         }
107 }
108
109 XMLNode&
110 VCA::get_state ()
111 {
112         XMLNode* node = new XMLNode (xml_node_name);
113         node->add_property (X_("name"), _name);
114         node->add_property (X_("number"), _number);
115
116         Stripable::add_state (*node);
117
118         node->add_child_nocopy (_gain_control->get_state());
119         node->add_child_nocopy (_solo_control->get_state());
120         node->add_child_nocopy (_mute_control->get_state());
121         node->add_child_nocopy (get_automation_xml_state());
122
123         node->add_child_nocopy (Slavable::get_state());
124
125         return *node;
126 }
127
128 int
129 VCA::set_state (XMLNode const& node, int version)
130 {
131         XMLProperty const* prop;
132
133         Stripable::set_state (node, version);
134
135         if ((prop = node.property ("name")) != 0) {
136                 set_name (prop->value());
137         }
138
139         if ((prop = node.property ("number")) != 0) {
140                 _number = atoi (prop->value());
141         }
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                         XMLProperty* prop = (*i)->property ("name");
148
149                         if (!prop) {
150                                 continue;
151                         }
152
153                         if (prop->value() == _gain_control->name()) {
154                                 _gain_control->set_state (**i, version);
155                         }
156                         if (prop->value() == _solo_control->name()) {
157                                 _solo_control->set_state (**i, version);
158                         }
159                         if (prop->value() == _mute_control->name()) {
160                                 _mute_control->set_state (**i, version);
161                         }
162                 } else if ((*i)->name() == Slavable::xml_node_name) {
163                         Slavable::set_state (**i, version);
164                 }
165         }
166
167         return 0;
168 }
169
170 void
171 VCA::clear_all_solo_state ()
172 {
173         _solo_control->clear_all_solo_state ();
174 }
175
176 MonitorState
177 VCA::monitoring_state () const
178 {
179         /* XXX this has to get more complex but not clear how */
180         return MonitoringInput;
181 }
182
183 bool
184 VCA::slaved () const
185 {
186         if (!_gain_control) {
187                 return false;
188         }
189         /* just test one particular control, not all of them */
190         return _gain_control->slaved ();
191 }
192
193 bool
194 VCA::slaved_to (boost::shared_ptr<VCA> vca) const
195 {
196         if (!vca || !_gain_control) {
197                 return false;
198         }
199
200         /* just test one particular control, not all of them */
201
202         return _gain_control->slaved_to (vca->gain_control());
203 }