LV2 support.
[ardour.git] / libs / ardour / plugin_manager.cc
1 /*
2     Copyright (C) 2000-2006 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 <sys/types.h>
21 #include <cstdio>
22 #include <lrdf.h>
23 #include <dlfcn.h>
24
25 #ifdef VST_SUPPORT
26 #include <fst.h>
27 #include <pbd/basename.h>
28 #include <string.h>
29 #endif // VST_SUPPORT
30
31 #include <pbd/pathscanner.h>
32
33 #include <ardour/ladspa.h>
34 #include <ardour/session.h>
35 #include <ardour/plugin_manager.h>
36 #include <ardour/plugin.h>
37 #include <ardour/ladspa_plugin.h>
38
39 #ifdef HAVE_SLV2
40 #include <slv2/slv2.h>
41 #include <ardour/lv2_plugin.h>
42 #endif
43
44 #ifdef VST_SUPPORT
45 #include <ardour/vst_plugin.h>
46 #endif
47
48 #ifdef HAVE_AUDIOUNITS
49 #include <ardour/audio_unit.h>
50 #endif
51
52 #include <pbd/error.h>
53 #include <pbd/stl_delete.h>
54
55 #include "i18n.h"
56
57 using namespace ARDOUR;
58 using namespace PBD;
59
60 PluginManager* PluginManager::_manager = 0;
61
62 PluginManager::PluginManager ()
63 {
64         char* s;
65         string lrdf_path;
66
67         if ((s = getenv ("LADSPA_RDF_PATH"))){
68                 lrdf_path = s;
69         }
70
71         if (lrdf_path.length() == 0) {
72                 lrdf_path = "/usr/local/share/ladspa/rdf:/usr/share/ladspa/rdf";
73         }
74
75         add_lrdf_data(lrdf_path);
76         add_ladspa_presets();
77 #ifdef VST_SUPPORT
78         if (Config->get_use_vst()) {
79                 add_vst_presets();
80         }
81 #endif /* VST_SUPPORT */
82
83         if ((s = getenv ("LADSPA_PATH"))) {
84                 ladspa_path = s;
85         }
86
87         if ((s = getenv ("VST_PATH"))) {
88                 vst_path = s;
89         } else if ((s = getenv ("VST_PLUGINS"))) {
90                 vst_path = s;
91         }
92
93         if (_manager == 0) {
94                 _manager = this;
95         }
96
97         /* the plugin manager is constructed too early to use Profile */
98
99         if (getenv ("ARDOUR_SAE")) {
100                 ladspa_plugin_whitelist.push_back (1203); // single band parametric
101                 ladspa_plugin_whitelist.push_back (1772); // caps compressor
102                 ladspa_plugin_whitelist.push_back (1913); // fast lookahead limiter
103                 ladspa_plugin_whitelist.push_back (1075); // simple RMS expander
104                 ladspa_plugin_whitelist.push_back (1061); // feedback delay line (max 5s)
105                 ladspa_plugin_whitelist.push_back (1216); // gverb
106                 ladspa_plugin_whitelist.push_back (2150); // tap pitch shifter
107         } 
108
109         _lv2_world = slv2_world_new();
110         slv2_world_load_all(_lv2_world);
111
112         refresh ();
113 }
114
115 void
116 PluginManager::refresh ()
117 {
118         ladspa_refresh ();
119 #ifdef HAVE_SLV2
120         lv2_refresh ();
121 #endif
122 #ifdef VST_SUPPORT
123         if (Config->get_use_vst()) {
124                 vst_refresh ();
125         }
126 #endif // VST_SUPPORT
127 #ifdef HAVE_AUDIOUNITS
128         au_refresh ();
129 #endif
130 }
131
132 void
133 PluginManager::ladspa_refresh ()
134 {
135         _ladspa_plugin_info.clear ();
136
137         if (ladspa_path.length() == 0) {
138                 ladspa_path = "/usr/local/lib64/ladspa:/usr/local/lib/ladspa:/usr/lib64/ladspa:/usr/lib/ladspa:/Library/Audio/Plug-Ins/LADSPA";
139         }
140
141         ladspa_discover_from_path (ladspa_path);
142 }
143
144
145 int
146 PluginManager::add_ladspa_directory (string path)
147 {
148         if (ladspa_discover_from_path (path) == 0) {
149                 ladspa_path += ':';
150                 ladspa_path += path;
151                 return 0;
152         } 
153         return -1;
154 }
155
156 static bool ladspa_filter (const string& str, void *arg)
157 {
158         /* Not a dotfile, has a prefix before a period, suffix is "so" */
159         
160         return str[0] != '.' && (str.length() > 3 && str.find (".so") == (str.length() - 3));
161 }
162
163 int
164 PluginManager::ladspa_discover_from_path (string path)
165 {
166         PathScanner scanner;
167         vector<string *> *plugin_objects;
168         vector<string *>::iterator x;
169         int ret = 0;
170
171         plugin_objects = scanner (ladspa_path, ladspa_filter, 0, true, true);
172
173         if (plugin_objects) {
174                 for (x = plugin_objects->begin(); x != plugin_objects->end (); ++x) {
175                         ladspa_discover (**x);
176                 }
177         }
178
179         vector_delete (plugin_objects);
180         return ret;
181 }
182
183 static bool rdf_filter (const string &str, void *arg)
184 {
185         return str[0] != '.' && 
186                    ((str.find(".rdf")  == (str.length() - 4)) ||
187             (str.find(".rdfs") == (str.length() - 5)) ||
188                     (str.find(".n3")   == (str.length() - 3)));
189 }
190
191 void
192 PluginManager::add_ladspa_presets()
193 {
194         add_presets ("ladspa");
195 }
196
197 void
198 PluginManager::add_vst_presets()
199 {
200         add_presets ("vst");
201 }
202 void
203 PluginManager::add_presets(string domain)
204 {
205
206         PathScanner scanner;
207         vector<string *> *presets;
208         vector<string *>::iterator x;
209
210         char* envvar;
211         if ((envvar = getenv ("HOME")) == 0) {
212                 return;
213         }
214
215         string path = string_compose("%1/.%2/rdf", envvar, domain);
216         presets = scanner (path, rdf_filter, 0, true, true);
217
218         if (presets) {
219                 for (x = presets->begin(); x != presets->end (); ++x) {
220                         string file = "file:" + **x;
221                         if (lrdf_read_file(file.c_str())) {
222                                 warning << string_compose(_("Could not parse rdf file: %1"), *x) << endmsg;
223                         }
224                 }
225         }
226
227         vector_delete (presets);
228 }
229
230 void
231 PluginManager::add_lrdf_data (const string &path)
232 {
233         PathScanner scanner;
234         vector<string *>* rdf_files;
235         vector<string *>::iterator x;
236         string uri;
237
238         rdf_files = scanner (path, rdf_filter, 0, true, true);
239
240         if (rdf_files) {
241                 for (x = rdf_files->begin(); x != rdf_files->end (); ++x) {
242                         uri = "file://" + **x;
243
244                         if (lrdf_read_file(uri.c_str())) {
245                                 warning << "Could not parse rdf file: " << uri << endmsg;
246                         }
247                 }
248         }
249
250         vector_delete (rdf_files);
251 }
252
253 int 
254 PluginManager::ladspa_discover (string path)
255 {
256         void *module;
257         const LADSPA_Descriptor *descriptor;
258         LADSPA_Descriptor_Function dfunc;
259         const char *errstr;
260
261         if ((module = dlopen (path.c_str(), RTLD_NOW)) == 0) {
262                 error << string_compose(_("LADSPA: cannot load module \"%1\" (%2)"), path, dlerror()) << endmsg;
263                 return -1;
264         }
265
266         dfunc = (LADSPA_Descriptor_Function) dlsym (module, "ladspa_descriptor");
267
268         if ((errstr = dlerror()) != 0) {
269                 error << string_compose(_("LADSPA: module \"%1\" has no descriptor function."), path) << endmsg;
270                 error << errstr << endmsg;
271                 dlclose (module);
272                 return -1;
273         }
274
275         for (uint32_t i = 0; ; ++i) {
276                 if ((descriptor = dfunc (i)) == 0) {
277                         break;
278                 }
279
280                 if (!ladspa_plugin_whitelist.empty()) {
281                         if (find (ladspa_plugin_whitelist.begin(), ladspa_plugin_whitelist.end(), descriptor->UniqueID) == ladspa_plugin_whitelist.end()) {
282                                 continue;
283                         }
284                 } 
285
286                 PluginInfoPtr info(new LadspaPluginInfo);
287                 info->name = descriptor->Name;
288                 info->category = get_ladspa_category(descriptor->UniqueID);
289                 info->creator = descriptor->Maker;
290                 info->path = path;
291                 info->index = i;
292                 info->n_inputs = 0;
293                 info->n_outputs = 0;
294                 info->type = ARDOUR::LADSPA;
295                 
296                 char buf[32];
297                 snprintf (buf, sizeof (buf), "%lu", descriptor->UniqueID);
298                 info->unique_id = buf;
299                 
300                 for (uint32_t n=0; n < descriptor->PortCount; ++n) {
301                         if ( LADSPA_IS_PORT_AUDIO (descriptor->PortDescriptors[n]) ) {
302                                 if ( LADSPA_IS_PORT_INPUT (descriptor->PortDescriptors[n]) ) {
303                                         info->n_inputs++;
304                                 }
305                                 else if ( LADSPA_IS_PORT_OUTPUT (descriptor->PortDescriptors[n]) ) {
306                                         info->n_outputs++;
307                                 }
308                         }
309                 }
310
311                 _ladspa_plugin_info.push_back (info);
312         }
313
314 // GDB WILL NOT LIKE YOU IF YOU DO THIS
315 //      dlclose (module);
316
317         return 0;
318 }
319
320 string
321 PluginManager::get_ladspa_category (uint32_t plugin_id)
322 {
323         char buf[256];
324         lrdf_statement pattern;
325
326         snprintf(buf, sizeof(buf), "%s%" PRIu32, LADSPA_BASE, plugin_id);
327         pattern.subject = buf;
328         pattern.predicate = (char*)RDF_TYPE;
329         pattern.object = 0;
330         pattern.object_type = lrdf_uri;
331
332         lrdf_statement* matches1 = lrdf_matches (&pattern);
333
334         if (!matches1) {
335                 return "";
336         }
337
338         pattern.subject = matches1->object;
339         pattern.predicate = (char*)(LADSPA_BASE "hasLabel");
340         pattern.object = 0;
341         pattern.object_type = lrdf_literal;
342
343         lrdf_statement* matches2 = lrdf_matches (&pattern);
344         lrdf_free_statements(matches1);
345
346         if (!matches2) {
347                 return ("");
348         }
349
350         string label = matches2->object;
351         lrdf_free_statements(matches2);
352
353         return label;
354 }
355
356 #ifdef HAVE_SLV2
357 void
358 PluginManager::lv2_refresh ()
359 {
360         lv2_discover();
361 }
362
363 int
364 PluginManager::lv2_discover ()
365 {
366         _lv2_plugin_info = LV2PluginInfo::discover(_lv2_world);
367         return 0;
368 }
369 #endif
370
371 #ifdef HAVE_AUDIOUNITS
372 void
373 PluginManager::au_refresh ()
374 {
375         au_discover();
376 }
377
378 int
379 PluginManager::au_discover ()
380 {
381         _au_plugin_info = AUPluginInfo::discover();
382         return 0;
383 }
384
385 #endif
386
387 #ifdef VST_SUPPORT
388
389 void
390 PluginManager::vst_refresh ()
391 {
392         _vst_plugin_info.clear ();
393
394         if (vst_path.length() == 0) {
395                 vst_path = "/usr/local/lib/vst:/usr/lib/vst";
396         }
397
398         vst_discover_from_path (vst_path);
399 }
400
401 int
402 PluginManager::add_vst_directory (string path)
403 {
404         if (vst_discover_from_path (path) == 0) {
405                 vst_path += ':';
406                 vst_path += path;
407                 return 0;
408         } 
409         return -1;
410 }
411
412 static bool vst_filter (const string& str, void *arg)
413 {
414         /* Not a dotfile, has a prefix before a period, suffix is "dll" */
415
416         return str[0] != '.' && (str.length() > 4 && str.find (".dll") == (str.length() - 4));
417 }
418
419 int
420 PluginManager::vst_discover_from_path (string path)
421 {
422         PathScanner scanner;
423         vector<string *> *plugin_objects;
424         vector<string *>::iterator x;
425         int ret = 0;
426
427         info << "detecting VST plugins along " << path << endmsg;
428
429         plugin_objects = scanner (vst_path, vst_filter, 0, true, true);
430
431         if (plugin_objects) {
432                 for (x = plugin_objects->begin(); x != plugin_objects->end (); ++x) {
433                         vst_discover (**x);
434                 }
435         }
436
437         vector_delete (plugin_objects);
438         return ret;
439 }
440
441 int
442 PluginManager::vst_discover (string path)
443 {
444         FSTInfo* finfo;
445         char buf[32];
446
447         if ((finfo = fst_get_info (const_cast<char *> (path.c_str()))) == 0) {
448                 warning << "Cannot get VST information from " << path << endmsg;
449                 return -1;
450         }
451
452         if (!finfo->canProcessReplacing) {
453                 warning << string_compose (_("VST plugin %1 does not support processReplacing, and so cannot be used in ardour at this time"),
454                                     finfo->name)
455                         << endl;
456         }
457         
458         PluginInfoPtr info(new VSTPluginInfo);
459
460         /* what a goddam joke freeware VST is */
461
462         if (!strcasecmp ("The Unnamed plugin", finfo->name)) {
463                 info->name = PBD::basename_nosuffix (path);
464         } else {
465                 info->name = finfo->name;
466         }
467
468         
469         snprintf (buf, sizeof (buf), "%d", finfo->UniqueID);
470         info->unique_id = buf;
471         info->category = "VST";
472         info->path = path;
473         // need to set info->creator but FST doesn't provide it
474         info->index = 0;
475         info->n_inputs = finfo->numInputs;
476         info->n_outputs = finfo->numOutputs;
477         info->type = ARDOUR::VST;
478         
479         _vst_plugin_info.push_back (info);
480         fst_free_info (finfo);
481
482         return 0;
483 }
484
485 #endif // VST_SUPPORT