switch to using boost::signals2 instead of sigc++, at least for libardour. not finish...
[ardour.git] / libs / ardour / processor.cc
1 /*
2     Copyright (C) 2000 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 #ifdef WAF_BUILD
21 #include "libardour-config.h"
22 #endif
23
24 #include <string>
25
26
27 #include "pbd/failed_constructor.h"
28 #include "pbd/enumwriter.h"
29 #include "pbd/xml++.h"
30
31 #include "ardour/processor.h"
32 #include "ardour/plugin.h"
33 #include "ardour/port.h"
34 #include "ardour/route.h"
35 #include "ardour/ladspa_plugin.h"
36 #include "ardour/buffer_set.h"
37 #include "ardour/send.h"
38 #include "ardour/port_insert.h"
39 #include "ardour/plugin_insert.h"
40
41 #ifdef VST_SUPPORT
42 #include "ardour/vst_plugin.h"
43 #endif
44
45 #ifdef HAVE_AUDIOUNITS
46 #include "ardour/audio_unit.h"
47 #endif
48
49 #include "ardour/audioengine.h"
50 #include "ardour/session.h"
51 #include "ardour/types.h"
52
53 #include "i18n.h"
54
55 using namespace std;
56 using namespace ARDOUR;
57 using namespace PBD;
58
59 boost::signals2::signal<void(Processor*)> Processor::ProcessorCreated;
60
61 // Always saved as Processor, but may be IOProcessor or Send in legacy sessions
62 const string Processor::state_node_name = "Processor";
63
64 Processor::Processor(Session& session, const string& name)
65         : SessionObject(session, name)
66         , AutomatableControls(session)
67         , _pending_active(false)
68         , _active(false)
69         , _next_ab_is_active(false)
70         , _configured(false)
71         , _gui(0)
72         , _display_to_user (true)
73 {
74 }
75
76 Processor::Processor (Session& session, const XMLNode& node)
77         : SessionObject(session, "renameMe")
78         , AutomatableControls(session)
79         , _pending_active(false)
80         , _active(false)
81         , _next_ab_is_active(false)
82         , _configured(false)
83         , _gui(0)
84         , _display_to_user (true)
85 {
86         set_state (node, Stateful::loading_state_version);
87         _pending_active = _active;
88 }
89
90 XMLNode&
91 Processor::get_state (void)
92 {
93         return state (true);
94 }
95
96 /* NODE STRUCTURE
97
98     <Automation [optionally with visible="...." ]>
99        <parameter-N>
100          <AutomationList id=N>
101            <events>
102            X1 Y1
103            X2 Y2
104            ....
105            </events>
106        </parameter-N>
107     <Automation>
108 */
109
110 XMLNode&
111 Processor::state (bool full_state)
112 {
113         XMLNode* node = new XMLNode (state_node_name);
114         stringstream sstr;
115         char buf[64];
116
117         id().print (buf, sizeof (buf));
118         node->add_property("id", buf);
119         node->add_property("name", _name);
120         node->add_property("active", active() ? "yes" : "no");
121
122         if (_extra_xml){
123                 node->add_child_copy (*_extra_xml);
124         }
125
126         if (full_state) {
127                 XMLNode& automation = Automatable::get_automation_state();
128                 if (!automation.children().empty()
129                                 || !automation.properties().empty()
130                                 || !_visible_controls.empty()) {
131
132                         for (set<Evoral::Parameter>::iterator x = _visible_controls.begin();
133                                         x != _visible_controls.end(); ++x) {
134                                 if (x != _visible_controls.begin()) {
135                                         sstr << ' ';
136                                 }
137                                 sstr << *x;
138                         }
139
140                         automation.add_property ("visible", sstr.str());
141                         node->add_child_nocopy (automation);
142                 }
143         }
144
145         return *node;
146 }
147
148 int
149 Processor::set_state_2X (const XMLNode & node, int /*version*/)
150 {
151         XMLProperty const * prop;
152
153         XMLNodeList children = node.children ();
154
155         for (XMLNodeIterator i = children.begin(); i != children.end(); ++i) {
156
157                 if ((*i)->name() == X_("IO")) {
158
159                         if ((prop = (*i)->property ("name")) != 0) {
160                                 set_name (prop->value ());
161                         }
162
163                         if ((prop = (*i)->property ("id")) != 0) {
164                                 _id = prop->value ();
165                         }
166
167                         if ((prop = (*i)->property ("active")) != 0) {
168                                 if (_active != string_is_affirmative (prop->value())) {
169                                         _active = !_active;
170                                         ActiveChanged (); /* EMIT_SIGNAL */
171                                 }
172                         }
173                 }
174         }
175
176         return 0;
177 }
178
179 int
180 Processor::set_state (const XMLNode& node, int version)
181 {
182         if (version < 3000) {
183                 return set_state_2X (node, version);
184         }
185         
186         const XMLProperty *prop;
187         const XMLProperty *legacy_active = 0;
188
189         // may not exist for legacy 3.0 sessions
190         if ((prop = node.property ("name")) != 0) {
191                 set_name(prop->value());
192         }
193
194         // may not exist for legacy 3.0 sessions
195         if ((prop = node.property ("id")) != 0) {
196                 _id = prop->value();
197         }
198
199         XMLNodeList nlist = node.children();
200         XMLNodeIterator niter;
201
202         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
203
204                 if ((*niter)->name() == X_("Automation")) {
205
206                         XMLProperty *prop;
207
208                         if ((prop = (*niter)->property ("path")) != 0) {
209                                 old_set_automation_state (*(*niter));
210                         } else {
211                                 set_automation_state (*(*niter), Evoral::Parameter(PluginAutomation));
212                         }
213
214                         if ((prop = (*niter)->property ("visible")) != 0) {
215                                 uint32_t what;
216                                 stringstream sstr;
217
218                                 _visible_controls.clear ();
219
220                                 sstr << prop->value();
221                                 while (1) {
222                                         sstr >> what;
223                                         if (sstr.fail()) {
224                                                 break;
225                                         }
226                                         // FIXME: other automation types?
227                                         mark_automation_visible (Evoral::Parameter(PluginAutomation, 0, what), true);
228                                 }
229                         }
230
231                 } else if ((*niter)->name() == "Extra") {
232                         _extra_xml = new XMLNode (*(*niter));
233                 } else if ((*niter)->name() == "Redirect") {
234                         if ( !(legacy_active = (*niter)->property("active"))) {
235                                 error << string_compose(_("No %1 property flag in element %2"), "active", (*niter)->name()) << endl;
236                         }
237                 }
238         }
239
240         if ((prop = node.property ("active")) == 0) {
241                 warning << _("XML node describing a processor is missing the `active' field,"
242                            "trying legacy active flag from child node") << endmsg;
243                 if (legacy_active) {
244                         prop = legacy_active;
245                 } else {
246                         error << _("No child node with active property") << endmsg;
247                         return -1;
248                 }
249         }
250
251         if (_active != string_is_affirmative (prop->value())) {
252                 _active = !_active;
253                 ActiveChanged (); /* EMIT_SIGNAL */
254         }
255
256         return 0;
257 }
258
259 bool
260 Processor::configure_io (ChanCount in, ChanCount out)
261 {
262         /* This class assumes 1:1 input:output.static output stream count.
263            Derived classes must override and set _configured_output appropriately
264            if this is not the case
265         */
266
267         _configured_input = in;
268         _configured_output = out;
269         _configured = true;
270
271         ConfigurationChanged (in, out); /* EMIT SIGNAL */
272
273         return true;
274 }
275
276 void
277 Processor::set_display_to_user (bool yn) 
278 {
279         _display_to_user = yn;
280 }
281