save and restore vca assignments
[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/gain_control.h"
23 #include "ardour/rc_configuration.h"
24 #include "ardour/route.h"
25 #include "ardour/session.h"
26 #include "ardour/vca.h"
27
28 #include "i18n.h"
29
30 using namespace ARDOUR;
31 using namespace PBD;
32 using std::string;
33
34 gint VCA::next_number = 1;
35 string VCA::xml_node_name (X_("VCA"));
36
37 string
38 VCA::default_name_template ()
39 {
40         return _("VCA %n");
41 }
42
43 int
44 VCA::next_vca_number ()
45 {
46         /* recall that atomic_int_add() returns the value before the add. We
47          * start at one, then next one will be two etc.
48          */
49         return g_atomic_int_add (&next_number, 1);
50 }
51
52 void
53 VCA::set_next_vca_number (uint32_t n)
54 {
55         g_atomic_int_set (&next_number, n);
56 }
57
58 uint32_t
59 VCA::get_next_vca_number ()
60 {
61         return g_atomic_int_get (&next_number);
62 }
63
64 VCA::VCA (Session& s,  uint32_t num, const string& name)
65         : Stripable (s, name)
66         , Muteable (s, name)
67         , Automatable (s)
68         , _number (num)
69         , _gain_control (new GainControl (s, Evoral::Parameter (GainAutomation), boost::shared_ptr<AutomationList> ()))
70 {
71 }
72
73 int
74 VCA::init ()
75 {
76         _solo_control.reset (new SoloControl (_session, X_("solo"), *this, *this));
77         _mute_control.reset (new MuteControl (_session, X_("mute"), *this));
78
79         add_control (_gain_control);
80         add_control (_solo_control);
81         add_control (_mute_control);
82
83         return 0;
84 }
85
86 VCA::~VCA ()
87 {
88         DropReferences (); /* emit signal */
89 }
90
91 uint32_t
92 VCA::remote_control_id () const
93 {
94         return 9999999 + _number;
95 }
96
97 XMLNode&
98 VCA::get_state ()
99 {
100         XMLNode* node = new XMLNode (xml_node_name);
101         node->add_property (X_("name"), _name);
102         node->add_property (X_("number"), _number);
103
104         node->add_child_nocopy (_gain_control->get_state());
105         node->add_child_nocopy (_solo_control->get_state());
106         node->add_child_nocopy (_mute_control->get_state());
107         node->add_child_nocopy (get_automation_xml_state());
108
109         node->add_child_nocopy (Slavable::get_state());
110
111         return *node;
112 }
113
114 int
115 VCA::set_state (XMLNode const& node, int version)
116 {
117         XMLProperty const* prop;
118
119         if ((prop = node.property ("name")) != 0) {
120                 set_name (prop->value());
121         }
122
123         if ((prop = node.property ("number")) != 0) {
124                 _number = atoi (prop->value());
125         }
126
127         XMLNodeList const &children (node.children());
128         for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
129                 if ((*i)->name() == Controllable::xml_node_name) {
130
131                         XMLProperty* prop = (*i)->property ("name");
132
133                         if (!prop) {
134                                 continue;
135                         }
136
137                         if (prop->value() == _gain_control->name()) {
138                                 _gain_control->set_state (**i, version);
139                         }
140                         if (prop->value() == _solo_control->name()) {
141                                 _solo_control->set_state (**i, version);
142                         }
143                         if (prop->value() == _mute_control->name()) {
144                                 _mute_control->set_state (**i, version);
145                         }
146                 } else if ((*i)->name() == Slavable::xml_node_name) {
147                         Slavable::set_state (**i, version);
148                 }
149         }
150
151         return 0;
152 }
153
154 void
155 VCA::clear_all_solo_state ()
156 {
157         _solo_control->clear_all_solo_state ();
158 }
159
160 int
161 VCA::assign_controls (boost::shared_ptr<VCA> vca)
162 {
163         _gain_control->add_master (vca->gain_control());
164         _solo_control->add_master (vca->solo_control());
165         _mute_control->add_master (vca->mute_control());
166
167         return 0;
168 }
169
170 int
171 VCA::unassign_controls (boost::shared_ptr<VCA> vca)
172 {
173         if (!vca) {
174                 /* unassign from all */
175                 _gain_control->clear_masters ();
176                 _solo_control->clear_masters ();
177                 _mute_control->clear_masters ();
178         } else {
179                 _gain_control->remove_master (vca->gain_control());
180                 _solo_control->remove_master (vca->solo_control());
181                 _mute_control->remove_master (vca->mute_control());
182         }
183
184         return 0;
185 }