redesign Route and VCA objects to inherit from ARDOUR::Stripable
[ardour.git] / libs / ardour / vca_manager.cc
1 /*
2   Copyright (C) 2016 Paul Davis
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License as published by
6   the Free Software Foundation; either version 2 of the License, or
7   (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU General Public License for more details.
13
14   You should have received a copy of the GNU General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "pbd/convert.h"
21 #include "pbd/error.h"
22 #include "pbd/replace_all.h"
23
24 #include "ardour/vca.h"
25 #include "ardour/vca_manager.h"
26
27 #include "i18n.h"
28
29 using namespace ARDOUR;
30 using namespace Glib::Threads;
31 using namespace PBD;
32 using std::string;
33
34 string VCAManager::xml_node_name (X_("VCAManager"));
35
36 VCAManager::VCAManager (Session& s)
37         : SessionHandleRef (s)
38         , _vcas_loaded (false)
39 {
40 }
41
42 VCAManager::~VCAManager ()
43 {
44         clear ();
45 }
46
47 void
48 VCAManager::clear ()
49 {
50         Mutex::Lock lm (lock);
51         _vcas.clear ();
52 }
53
54 VCAList
55 VCAManager::vcas () const
56 {
57         Mutex::Lock lm (lock);
58         return _vcas;
59 }
60
61 int
62 VCAManager::create_vca (uint32_t howmany, std::string const & name_template)
63 {
64         VCAList vcal;
65
66         {
67                 Mutex::Lock lm (lock);
68
69                 for (uint32_t n = 0; n < howmany; ++n) {
70
71                         int num = VCA::next_vca_number ();
72                         string name = name_template;
73
74                         if (name.find ("%n")) {
75                                 string sn = PBD::to_string (num, std::dec);
76                                 replace_all (name, "%n", sn);
77                         }
78
79                         boost::shared_ptr<VCA> vca = boost::shared_ptr<VCA> (new VCA (_session, num, name));
80
81                         vca->init ();
82
83                         _vcas.push_back (vca);
84                         vcal.push_back (vca);
85                 }
86         }
87
88         VCAAdded (vcal); /* EMIT SIGNAL */
89
90         return 0;
91 }
92
93
94 void
95 VCAManager::remove_vca (boost::shared_ptr<VCA> vca)
96 {
97         {
98                 Mutex::Lock lm (lock);
99                 _vcas.remove (vca);
100         }
101
102         VCAList vcal;
103         vcal.push_back (vca);
104
105         VCARemoved (vcal); /* EMIT SIGNAL */
106 }
107
108 boost::shared_ptr<VCA>
109 VCAManager::vca_by_number (uint32_t n) const
110 {
111         Mutex::Lock lm (lock);
112
113         for (VCAList::const_iterator i = _vcas.begin(); i != _vcas.end(); ++i) {
114                 if ((*i)->number() == n) {
115                         return *i;
116                 }
117         }
118
119         return boost::shared_ptr<VCA>();
120 }
121
122 XMLNode&
123 VCAManager::get_state ()
124 {
125         XMLNode* node = new XMLNode (xml_node_name);
126
127         {
128                 Mutex::Lock lm (lock);
129
130                 for (VCAList::const_iterator i = _vcas.begin(); i != _vcas.end(); ++i) {
131                         node->add_child_nocopy ((*i)->get_state());
132                 }
133         }
134
135         return *node;
136 }
137
138 int
139 VCAManager::set_state (XMLNode const& node, int version)
140 {
141         if (node.name() != xml_node_name) {
142                 return -1;
143         }
144
145         XMLNodeList const & children = node.children();
146         VCAList vcal;
147
148         _vcas_loaded = false;
149
150         for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
151                 if ((*i)->name() == VCA::xml_node_name) {
152                         boost::shared_ptr<VCA> vca = boost::shared_ptr<VCA> (new VCA (_session, 0, X_("tobereset")));
153
154                         if (vca->init() || vca->set_state (**i, version)) {
155                                 error << _("Cannot set state of a VCA") << endmsg;
156                                 return -1;
157                         }
158
159                         /* can't hold the lock for the entire loop,
160                          * because the new VCA maybe slaved and needs
161                          * to call back into us to set up its own
162                          * slave/master relationship
163                          */
164
165                         {
166                                 Mutex::Lock lm (lock);
167                                 _vcas.push_back (vca);
168                                 vcal.push_back (vca);
169                         }
170                 }
171         }
172
173         _vcas_loaded = true;
174
175         VCAsLoaded (); /* EMIT SIGNAL */
176         VCAAdded (vcal); /* EMIT SIGNAL */
177
178         return 0;
179 }