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