API and implementation tweaks for destroying VCAs
[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 gint 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 int
47 VCA::next_vca_number ()
48 {
49         /* recall that atomic_int_add() returns the value before the add. We
50          * start at one, then next one will be two etc.
51          */
52         return g_atomic_int_add (&next_number, 1);
53 }
54
55 void
56 VCA::set_next_vca_number (uint32_t n)
57 {
58         g_atomic_int_set (&next_number, n);
59 }
60
61 uint32_t
62 VCA::get_next_vca_number ()
63 {
64         return g_atomic_int_get (&next_number);
65 }
66
67 VCA::VCA (Session& s,  uint32_t num, const string& name)
68         : Stripable (s, name)
69         , Muteable (s, name)
70         , Automatable (s)
71         , _number (num)
72         , _gain_control (new GainControl (s, Evoral::Parameter (GainAutomation), boost::shared_ptr<AutomationList> ()))
73 {
74 }
75
76 int
77 VCA::init ()
78 {
79         _solo_control.reset (new SoloControl (_session, X_("solo"), *this, *this));
80         _mute_control.reset (new MuteControl (_session, X_("mute"), *this));
81         _recenable_control.reset (new RecordEnableControl (_session, X_("recenable"), *this));
82         _monitor_control.reset (new MonitorControl (_session, X_("monitoring"), *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
96 uint32_t
97 VCA::remote_control_id () const
98 {
99         return 9999999 + _number;
100 }
101
102 XMLNode&
103 VCA::get_state ()
104 {
105         XMLNode* node = new XMLNode (xml_node_name);
106         node->add_property (X_("name"), _name);
107         node->add_property (X_("number"), _number);
108
109         node->add_child_nocopy (_gain_control->get_state());
110         node->add_child_nocopy (_solo_control->get_state());
111         node->add_child_nocopy (_mute_control->get_state());
112         node->add_child_nocopy (get_automation_xml_state());
113
114         node->add_child_nocopy (Slavable::get_state());
115
116         return *node;
117 }
118
119 int
120 VCA::set_state (XMLNode const& node, int version)
121 {
122         XMLProperty const* prop;
123
124         if ((prop = node.property ("name")) != 0) {
125                 set_name (prop->value());
126         }
127
128         if ((prop = node.property ("number")) != 0) {
129                 _number = atoi (prop->value());
130         }
131
132         XMLNodeList const &children (node.children());
133         for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
134                 if ((*i)->name() == Controllable::xml_node_name) {
135
136                         XMLProperty* prop = (*i)->property ("name");
137
138                         if (!prop) {
139                                 continue;
140                         }
141
142                         if (prop->value() == _gain_control->name()) {
143                                 _gain_control->set_state (**i, version);
144                         }
145                         if (prop->value() == _solo_control->name()) {
146                                 _solo_control->set_state (**i, version);
147                         }
148                         if (prop->value() == _mute_control->name()) {
149                                 _mute_control->set_state (**i, version);
150                         }
151                 } else if ((*i)->name() == Slavable::xml_node_name) {
152                         Slavable::set_state (**i, version);
153                 }
154         }
155
156         return 0;
157 }
158
159 void
160 VCA::clear_all_solo_state ()
161 {
162         _solo_control->clear_all_solo_state ();
163 }
164
165 MonitorState
166 VCA::monitoring_state () const
167 {
168         /* XXX this has to get more complex but not clear how */
169         return MonitoringInput;
170 }