Trim include dependency tree (particularly on evoral/Sequence.hpp).
[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, Placement p)
62         : SessionObject(session, name)
63         , AutomatableControls(session)
64         , _active(false)
65         , _next_ab_is_active(false)
66         , _configured(false)
67         , _placement(p)
68         , _gui(0)
69 {
70 }
71
72 void
73 Processor::set_sort_key (uint32_t key)
74 {
75         _sort_key = key;
76 }
77         
78 void
79 Processor::set_placement (Placement p)
80 {
81         if (_placement != p) {
82                 _placement = p;
83                  PlacementChanged (); /* EMIT SIGNAL */
84         }
85 }
86
87 XMLNode&
88 Processor::get_state (void)
89 {
90         return state (true);
91 }
92
93 /* NODE STRUCTURE 
94    
95     <Automation [optionally with visible="...." ]>
96        <parameter-N>
97          <AutomationList id=N>
98            <events>
99            X1 Y1
100            X2 Y2
101            ....
102            </events>
103        </parameter-N>
104     <Automation>
105 */
106
107 XMLNode&
108 Processor::state (bool full_state)
109 {
110         XMLNode* node = new XMLNode (state_node_name);
111         stringstream sstr;
112         
113         // FIXME: This conflicts with "id" used by plugin for name in legacy sessions (ugh).
114         // Do we need to serialize this?
115         /*
116         char buf[64];
117         id().print (buf, sizeof (buf));
118         node->add_property("id", buf);
119         */
120
121         node->add_property("name", _name);
122         node->add_property("active", active() ? "yes" : "no");  
123         node->add_property("placement", enum_2_string (_placement));
124
125         if (_extra_xml){
126                 node->add_child_copy (*_extra_xml);
127         }
128         
129         if (full_state) {
130
131                 XMLNode& automation = Automatable::get_automation_state(); 
132                 
133                 for (set<Evoral::Parameter>::iterator x = _visible_controls.begin(); 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
142                 node->add_child_nocopy (automation);
143         }
144
145         return *node;
146 }
147
148 int
149 Processor::set_state (const XMLNode& node)
150 {
151         const XMLProperty *prop;
152         const XMLProperty *legacy_active = 0;
153         const XMLProperty *legacy_placement = 0;
154
155         // may not exist for legacy sessions
156         if ((prop = node.property ("name")) != 0) {
157                 set_name(prop->value());
158         }
159
160         XMLNodeList nlist = node.children();
161         XMLNodeIterator niter;
162
163         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
164
165                 if ((*niter)->name() == X_("Automation")) {
166
167
168                         XMLProperty *prop;
169                         
170                         if ((prop = (*niter)->property ("path")) != 0) {
171                                 old_set_automation_state (*(*niter));
172                         } else {
173                                 set_automation_state (*(*niter), Evoral::Parameter(PluginAutomation));
174                         }
175
176                         if ((prop = (*niter)->property ("visible")) != 0) {
177                                 uint32_t what;
178                                 stringstream sstr;
179
180                                 _visible_controls.clear ();
181                                 
182                                 sstr << prop->value();
183                                 while (1) {
184                                         sstr >> what;
185                                         if (sstr.fail()) {
186                                                 break;
187                                         }
188                                         // FIXME: other automation types?
189                                         mark_automation_visible (Evoral::Parameter(PluginAutomation, 0, what), true);
190                                 }
191                         }
192
193                 } else if ((*niter)->name() == "Extra") {
194                         _extra_xml = new XMLNode (*(*niter));
195                 } else if ((*niter)->name() == "Redirect") {
196                         if ( !(legacy_active = (*niter)->property("active"))) {
197                                 error << string_compose(_("No %1 property flag in element %2"), "active", (*niter)->name()) << endl;
198                         }
199                         if ( !(legacy_placement = (*niter)->property("placement"))) {
200                                 error << string_compose(_("No %1 property flag in element %2"), "placement", (*niter)->name()) << endl;
201                         }
202                 }
203         }
204
205         if ((prop = node.property ("active")) == 0) {
206                 warning << _("XML node describing a processor is missing the `active' field, trying legacy active flag from child node") << endmsg;
207                 if (legacy_active) {
208                         prop = legacy_active;
209                 } else {
210                         error << _("No child node with active property") << endmsg;
211                         return -1;
212                 }
213         }
214
215         if (_active != (prop->value() == "yes")) {
216                 _active = !_active;
217                 ActiveChanged (); /* EMIT_SIGNAL */
218         }       
219
220         if ((prop = node.property ("placement")) == 0) {
221                 warning << _("XML node describing a processor is missing the `placement' field, trying legacy placement flag from child node") << endmsg;
222                 if (legacy_placement) {
223                         prop = legacy_placement; 
224                 } else {
225                         error << _("No child node with placement property") << endmsg;
226                         return -1;
227                 }
228         }
229
230         /* hack to handle older sessions before we only used EnumWriter */
231
232         string pstr;
233
234         if (prop->value() == "pre") {
235                 pstr = "PreFader";
236         } else if (prop->value() == "post") {
237                 pstr = "PostFader";
238         } else {
239                 pstr = prop->value();
240         }
241
242         Placement p = Placement (string_2_enum (pstr, p));
243         set_placement (p);
244
245         return 0;
246 }
247
248 bool
249 Processor::configure_io (ChanCount in, ChanCount out)
250 {
251         /* this class assumes static output stream count.
252            Derived classes must override, and must set "out"
253            to reflect "in" before calling this.
254         */
255
256         _configured_input = in; 
257         _configured = true;
258
259         return true;
260 }