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