1160a0015b62980e5d61bd99e9ac36caeb5d82cd
[ardour.git] / libs / ardour / plugin.cc
1 /*
2     Copyright (C) 2000-2002 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 <vector>
21 #include <string>
22
23 #include <cstdlib>
24 #include <cstdio> // so libraptor doesn't complain
25 #include <cmath>
26 #include <dirent.h>
27 #include <sys/stat.h>
28 #include <cerrno>
29
30 #include <lrdf.h>
31
32 #include <pbd/compose.h>
33 #include <pbd/error.h>
34 #include <pbd/pathscanner.h>
35 #include <pbd/xml++.h>
36 #include <pbd/stacktrace.h>
37
38 #include <ardour/ardour.h>
39 #include <ardour/session.h>
40 #include <ardour/audioengine.h>
41 #include <ardour/plugin.h>
42 #include <ardour/ladspa_plugin.h>
43 #include <ardour/plugin_manager.h>
44
45 #ifdef HAVE_AUDIOUNITS
46 #include <ardour/audio_unit.h>
47 #endif
48
49 #ifdef HAVE_SLV2
50 #include <ardour/lv2_plugin.h>
51 #endif
52
53 #include <pbd/stl_delete.h>
54
55 #include "i18n.h"
56 #include <locale.h>
57
58 using namespace ARDOUR;
59 using namespace PBD;
60
61 Plugin::Plugin (AudioEngine& e, Session& s)
62         : _engine (e), _session (s)
63 {
64 }
65
66 Plugin::Plugin (const Plugin& other)
67         : _engine (other._engine), _session (other._session), _info (other._info)
68 {
69 }
70
71 void
72 Plugin::setup_controls ()
73 {
74         uint32_t port_cnt = parameter_count();
75
76         /* set up a vector of null pointers for the controls.
77            we'll fill this in on an as-needed basis.
78         */
79
80         controls.assign (port_cnt, (PortControllable*) 0);
81 }
82
83 Plugin::~Plugin ()
84 {
85         for (vector<PortControllable*>::iterator i = controls.begin(); i != controls.end(); ++i) {
86                 if (*i) {
87                         delete *i;
88                 }
89         }
90 }
91
92 void
93 Plugin::make_nth_control (uint32_t n, const XMLNode& node)
94 {
95         if (controls[n]) {
96                 /* already constructed */
97                 return;
98         }
99
100         Plugin::ParameterDescriptor desc;
101         
102         get_parameter_descriptor (n, desc);
103         
104         controls[n] = new PortControllable (node, *this, n, 
105                                             desc.lower, desc.upper, desc.toggled, desc.logarithmic);
106 }
107
108 Controllable *
109 Plugin::get_nth_control (uint32_t n, bool do_not_create)
110 {
111         if (n >= parameter_count()) {
112                 return 0;
113         }
114
115         if (controls[n] == 0 && !do_not_create) {
116
117                 Plugin::ParameterDescriptor desc;
118                 
119                 get_parameter_descriptor (n, desc);
120
121                 controls[n] = new PortControllable (describe_parameter (n), *this, n, 
122                                                     desc.lower, desc.upper, desc.toggled, desc.logarithmic);
123         } 
124
125         return controls[n];
126 }
127
128 Plugin::PortControllable::PortControllable (string name, Plugin& p, uint32_t port_id, 
129                                             float low, float up, bool t, bool loga)
130         : Controllable (name), plugin (p), absolute_port (port_id)
131 {
132         toggled = t;
133         logarithmic = loga;
134         lower = low;
135         upper = up;
136         range = upper - lower;
137 }
138
139 Plugin::PortControllable::PortControllable (const XMLNode& node, Plugin& p, uint32_t port_id, 
140                                             float low, float up, bool t, bool loga)
141         : Controllable (node), plugin (p), absolute_port (port_id)
142 {
143         toggled = t;
144         logarithmic = loga;
145         lower = low;
146         upper = up;
147         range = upper - lower;
148 }
149
150 void
151 Plugin::PortControllable::set_value (float value)
152 {
153         if (toggled) {
154                 if (value > 0.5) {
155                         value = 1.0;
156                 } else {
157                         value = 0.0;
158                 }
159         } else {
160
161                 if (!logarithmic) {
162                         value = lower + (range * value);
163                 } else {
164                         float _lower = 0.0f;
165                         if (lower > 0.0f) {
166                                 _lower = log(lower);
167                         }
168
169                         value = exp(_lower + log(range) * value);
170                 }
171         }
172
173         plugin.set_parameter (absolute_port, value);
174 }
175
176 float
177 Plugin::PortControllable::get_value (void) const
178 {
179         float val = plugin.get_parameter (absolute_port);
180
181         if (toggled) {
182                 
183                 return val;
184                 
185         } else {
186                 
187                 if (logarithmic) {
188                         val = log(val);
189                 }
190                 
191                 return ((val - lower) / range);
192         }
193 }       
194
195 vector<string>
196 Plugin::get_presets()
197 {
198         vector<string> labels;
199         uint32_t id;
200         std::string unique (unique_id());
201
202         /* XXX problem: AU plugins don't have numeric ID's. 
203            Solution: they have a different method of providing presets.
204            XXX sub-problem: implement it.
205         */
206
207         if (!isdigit (unique[0])) {
208                 return labels;
209         }
210
211         id = atol (unique.c_str());
212
213         lrdf_uris* set_uris = lrdf_get_setting_uris(id);
214
215         if (set_uris) {
216                 for (uint32_t i = 0; i < (uint32_t) set_uris->count; ++i) {
217                         if (char* label = lrdf_get_label(set_uris->items[i])) {
218                                 labels.push_back(label);
219                                 presets[label] = set_uris->items[i];
220                         }
221                 }
222                 lrdf_free_uris(set_uris);
223         }
224
225         // GTK2FIX find an equivalent way to do this with a vector (needed by GUI apis)
226         // labels.unique();
227
228         return labels;
229 }
230
231 bool
232 Plugin::load_preset(const string preset_label)
233 {
234         lrdf_defaults* defs = lrdf_get_setting_values(presets[preset_label].c_str());
235
236         if (defs) {
237                 for (uint32_t i = 0; i < (uint32_t) defs->count; ++i) {
238                         // The defs->items[i].pid < defs->count check is to work around 
239                         // a bug in liblrdf that saves invalid values into the presets file.
240                         if (((uint32_t) defs->items[i].pid < (uint32_t) defs->count) && parameter_is_input (defs->items[i].pid)) {
241                                 set_parameter(defs->items[i].pid, defs->items[i].value);
242                         }
243                 }
244                 lrdf_free_setting_values(defs);
245         }
246
247         return true;
248 }
249
250 bool
251 Plugin::save_preset (string name, string domain)
252 {
253         lrdf_portvalue portvalues[parameter_count()];
254         lrdf_defaults defaults;
255         uint32_t id;
256         std::string unique (unique_id());
257
258         /* XXX problem: AU plugins don't have numeric ID's. 
259            Solution: they have a different method of providing/saving presets.
260            XXX sub-problem: implement it.
261         */
262
263         if (!isdigit (unique[0])) {
264                 return false;
265         }
266
267         id = atol (unique.c_str());
268
269         defaults.count = parameter_count();
270         defaults.items = portvalues;
271
272         for (uint32_t i = 0; i < parameter_count(); ++i) {
273                 if (parameter_is_input (i)) {
274                         portvalues[i].pid = i;
275                         portvalues[i].value = get_parameter(i);
276                 }
277         }
278
279         char* envvar;
280         if ((envvar = getenv ("HOME")) == 0) {
281                 warning << _("Could not locate HOME.  Preset not saved.") << endmsg;
282                 return false;
283         }
284         
285         string source(string_compose("file:%1/.%2/rdf/ardour-presets.n3", envvar, domain));
286
287         free(lrdf_add_preset(source.c_str(), name.c_str(), id,  &defaults));
288
289         string path = string_compose("%1/.%2", envvar, domain);
290         if (g_mkdir_with_parents (path.c_str(), 0775)) {
291                 warning << string_compose(_("Could not create %1.  Preset not saved. (%2)"), path, strerror(errno)) << endmsg;
292                 return false;
293         }
294         
295         path += "/rdf";
296         if (g_mkdir_with_parents (path.c_str(), 0775)) {
297                 warning << string_compose(_("Could not create %1.  Preset not saved. (%2)"), path, strerror(errno)) << endmsg;
298                 return false;
299         }
300         
301         if (lrdf_export_by_source(source.c_str(), source.substr(5).c_str())) {
302                 warning << string_compose(_("Error saving presets file %1."), source) << endmsg;
303                 return false;
304         }
305
306         return true;
307 }
308
309 PluginPtr
310 ARDOUR::find_plugin(Session& session, string identifier, PluginType type)
311 {
312         PluginManager *mgr = PluginManager::the_manager();
313         PluginInfoList plugs;
314
315         switch (type) {
316         case ARDOUR::LADSPA:
317                 plugs = mgr->ladspa_plugin_info();
318                 break;
319         
320 #ifdef HAVE_SLV2
321         case ARDOUR::LV2:
322                 plugs = mgr->lv2_plugin_info();
323                 break;
324 #endif
325
326 #ifdef VST_SUPPORT
327         case ARDOUR::VST:
328                 plugs = mgr->vst_plugin_info();
329                 break;
330 #endif
331
332 #ifdef HAVE_AUDIOUNITS
333         case ARDOUR::AudioUnit:
334                 plugs = mgr->au_plugin_info();
335                 break;
336 #endif
337
338         default:
339                 return PluginPtr ((Plugin *) 0);
340         }
341
342         PluginInfoList::iterator i;
343
344         for (i = plugs.begin(); i != plugs.end(); ++i) {
345                 if (identifier == (*i)->unique_id){
346                         return (*i)->load (session);
347                 }
348         }
349
350 #ifdef VST_SUPPORT
351         /* hmm, we didn't find it. could be because in older versions of Ardour.
352            we used to store the name of a VST plugin, not its unique ID. so try
353            again.
354         */
355
356         for (i = plugs.begin(); i != plugs.end(); ++i) {
357                 if (identifier == (*i)->name){
358                         return (*i)->load (session);
359                 }
360         }
361 #endif
362         
363         return PluginPtr ((Plugin*) 0);
364 }
365 int32_t
366 Plugin::can_support_input_configuration (int32_t in)
367 {
368         /* LADSPA & VST should not get here because they do not
369            return negative i/o counts.
370         */
371         return -1;
372 }
373
374 int32_t
375 Plugin::compute_output_streams (int32_t nplugins)
376 {
377         /* LADSPA & VST should not get here because they do not
378            return negative i/o counts.
379         */
380         return -1;
381 }
382
383 uint32_t
384 Plugin::output_streams () const
385 {
386         /* LADSPA & VST should not get here because they do not
387            return negative i/o counts.
388         */
389         return 0;
390 }
391
392 uint32_t
393 Plugin::input_streams () const
394 {
395         /* LADSPA & VST should not get here because they do not
396            return negative i/o counts.
397         */
398         return 0;
399 }
400
401