remove debug output
[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/slavable.h"
25 #include "ardour/vca.h"
26 #include "ardour/vca_manager.h"
27
28 #include "i18n.h"
29
30 using namespace ARDOUR;
31 using namespace Glib::Threads;
32 using namespace PBD;
33 using std::string;
34
35 string VCAManager::xml_node_name (X_("VCAManager"));
36
37 VCAManager::VCAManager (Session& s)
38         : SessionHandleRef (s)
39         , _vcas_loaded (false)
40 {
41 }
42
43 VCAManager::~VCAManager ()
44 {
45         clear ();
46 }
47
48 void
49 VCAManager::clear ()
50 {
51         Mutex::Lock lm (lock);
52         for (VCAList::const_iterator i = _vcas.begin(); i != _vcas.end(); ++i) {
53                 (*i)->DropReferences ();
54         }
55         _vcas.clear ();
56 }
57
58 VCAList
59 VCAManager::vcas () const
60 {
61         Mutex::Lock lm (lock);
62         return _vcas;
63 }
64
65 int
66 VCAManager::create_vca (uint32_t howmany, std::string const & name_template)
67 {
68         VCAList vcal;
69
70         {
71                 Mutex::Lock lm (lock);
72
73                 for (uint32_t n = 0; n < howmany; ++n) {
74
75                         int num = VCA::next_vca_number ();
76                         string name = name_template;
77
78                         if (name.find ("%n")) {
79                                 string sn = PBD::to_string (num, std::dec);
80                                 replace_all (name, "%n", sn);
81                         }
82
83                         boost::shared_ptr<VCA> vca = boost::shared_ptr<VCA> (new VCA (_session, num, name));
84
85                         vca->init ();
86
87                         _vcas.push_back (vca);
88                         vcal.push_back (vca);
89                 }
90         }
91
92         VCAAdded (vcal); /* EMIT SIGNAL */
93
94         return 0;
95 }
96
97
98 void
99 VCAManager::remove_vca (boost::shared_ptr<VCA> vca)
100 {
101         {
102                 Mutex::Lock lm (lock);
103                 _vcas.remove (vca);
104         }
105
106         /* this should cause deassignment and deletion */
107
108         vca->DropReferences ();
109 }
110
111 boost::shared_ptr<VCA>
112 VCAManager::vca_by_number (uint32_t n) const
113 {
114         Mutex::Lock lm (lock);
115
116         for (VCAList::const_iterator i = _vcas.begin(); i != _vcas.end(); ++i) {
117                 if ((*i)->number() == n) {
118                         return *i;
119                 }
120         }
121
122         return boost::shared_ptr<VCA>();
123 }
124
125 XMLNode&
126 VCAManager::get_state ()
127 {
128         XMLNode* node = new XMLNode (xml_node_name);
129
130         {
131                 Mutex::Lock lm (lock);
132
133                 for (VCAList::const_iterator i = _vcas.begin(); i != _vcas.end(); ++i) {
134                         node->add_child_nocopy ((*i)->get_state());
135                 }
136         }
137
138         return *node;
139 }
140
141 int
142 VCAManager::set_state (XMLNode const& node, int version)
143 {
144         if (node.name() != xml_node_name) {
145                 return -1;
146         }
147
148         XMLNodeList const & children = node.children();
149         VCAList vcal;
150
151         _vcas_loaded = false;
152
153         for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
154                 if ((*i)->name() == VCA::xml_node_name) {
155                         boost::shared_ptr<VCA> vca = boost::shared_ptr<VCA> (new VCA (_session, 0, X_("tobereset")));
156
157                         if (vca->init() || vca->set_state (**i, version)) {
158                                 error << _("Cannot set state of a VCA") << endmsg;
159                                 return -1;
160                         }
161
162                         /* can't hold the lock for the entire loop,
163                          * because the new VCA maybe slaved and needs
164                          * to call back into us to set up its own
165                          * slave/master relationship
166                          */
167
168                         {
169                                 Mutex::Lock lm (lock);
170                                 _vcas.push_back (vca);
171                                 vcal.push_back (vca);
172                         }
173                 }
174         }
175
176         _vcas_loaded = true;
177
178         VCAAdded (vcal); /* EMIT SIGNAL */
179
180         return 0;
181 }
182
183 void
184 VCAManager::clear_all_solo_state ()
185 {
186         Mutex::Lock lm (lock);
187
188         for (VCAList::const_iterator i = _vcas.begin(); i != _vcas.end(); ++i) {
189                 (*i)->clear_all_solo_state ();
190         }
191 }