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