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