336763faa1d7761717074ba37b1d3f58025a755a
[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 #ifdef HAVE_SLV2
110         _lv2_world = new LV2World();
111 #endif
112
113         refresh ();
114 }
115
116 void
117 PluginManager::refresh ()
118 {
119         ladspa_refresh ();
120 #ifdef HAVE_SLV2
121         lv2_refresh ();
122 #endif
123 #ifdef VST_SUPPORT
124         if (Config->get_use_vst()) {
125                 vst_refresh ();
126         }
127 #endif // VST_SUPPORT
128 #ifdef HAVE_AUDIOUNITS
129         au_refresh ();
130 #endif
131 }
132
133 void
134 PluginManager::ladspa_refresh ()
135 {
136         _ladspa_plugin_info.clear ();
137
138         if (ladspa_path.length() == 0) {
139                 ladspa_path = "/usr/local/lib64/ladspa:/usr/local/lib/ladspa:/usr/lib64/ladspa:/usr/lib/ladspa:/Library/Audio/Plug-Ins/LADSPA";
140         }
141
142         ladspa_discover_from_path (ladspa_path);
143 }
144
145
146 int
147 PluginManager::add_ladspa_directory (string path)
148 {
149         if (ladspa_discover_from_path (path) == 0) {
150                 ladspa_path += ':';
151                 ladspa_path += path;
152                 return 0;
153         } 
154         return -1;
155 }
156
157 static bool ladspa_filter (const string& str, void *arg)
158 {
159         /* Not a dotfile, has a prefix before a period, suffix is "so" */
160         
161         return str[0] != '.' && (str.length() > 3 && str.find (".so") == (str.length() - 3));
162 }
163
164 int
165 PluginManager::ladspa_discover_from_path (string path)
166 {
167         PathScanner scanner;
168         vector<string *> *plugin_objects;
169         vector<string *>::iterator x;
170         int ret = 0;
171
172         plugin_objects = scanner (ladspa_path, ladspa_filter, 0, true, true);
173
174         if (plugin_objects) {
175                 for (x = plugin_objects->begin(); x != plugin_objects->end (); ++x) {
176                         ladspa_discover (**x);
177                 }
178         }
179
180         vector_delete (plugin_objects);
181         return ret;
182 }
183
184 static bool rdf_filter (const string &str, void *arg)
185 {
186         return str[0] != '.' && 
187                    ((str.find(".rdf")  == (str.length() - 4)) ||
188             (str.find(".rdfs") == (str.length() - 5)) ||
189                     (str.find(".n3")   == (str.length() - 3)));
190 }
191
192 void
193 PluginManager::add_ladspa_presets()
194 {
195         add_presets ("ladspa");
196 }
197
198 void
199 PluginManager::add_vst_presets()
200 {
201         add_presets ("vst");
202 }
203 void
204 PluginManager::add_presets(string domain)
205 {
206
207         PathScanner scanner;
208         vector<string *> *presets;
209         vector<string *>::iterator x;
210
211         char* envvar;
212         if ((envvar = getenv ("HOME")) == 0) {
213                 return;
214         }
215
216         string path = string_compose("%1/.%2/rdf", envvar, domain);
217         presets = scanner (path, rdf_filter, 0, true, true);
218
219         if (presets) {
220                 for (x = presets->begin(); x != presets->end (); ++x) {
221                         string file = "file:" + **x;
222                         if (lrdf_read_file(file.c_str())) {
223                                 warning << string_compose(_("Could not parse rdf file: %1"), *x) << endmsg;
224                         }
225                 }
226         }
227
228         vector_delete (presets);
229 }
230
231 void
232 PluginManager::add_lrdf_data (const string &path)
233 {
234         PathScanner scanner;
235         vector<string *>* rdf_files;
236         vector<string *>::iterator x;
237         string uri;
238
239         rdf_files = scanner (path, rdf_filter, 0, true, true);
240
241         if (rdf_files) {
242                 for (x = rdf_files->begin(); x != rdf_files->end (); ++x) {
243                         uri = "file://" + **x;
244
245                         if (lrdf_read_file(uri.c_str())) {
246                                 warning << "Could not parse rdf file: " << uri << endmsg;
247                         }
248                 }
249         }
250
251         vector_delete (rdf_files);
252 }
253
254 int 
255 PluginManager::ladspa_discover (string path)
256 {
257         void *module;
258         const LADSPA_Descriptor *descriptor;
259         LADSPA_Descriptor_Function dfunc;
260         const char *errstr;
261
262         if ((module = dlopen (path.c_str(), RTLD_NOW)) == 0) {
263                 error << string_compose(_("LADSPA: cannot load module \"%1\" (%2)"), path, dlerror()) << endmsg;
264                 return -1;
265         }
266
267         dfunc = (LADSPA_Descriptor_Function) dlsym (module, "ladspa_descriptor");
268
269         if ((errstr = dlerror()) != 0) {
270                 error << string_compose(_("LADSPA: module \"%1\" has no descriptor function."), path) << endmsg;
271                 error << errstr << endmsg;
272                 dlclose (module);
273                 return -1;
274         }
275
276         for (uint32_t i = 0; ; ++i) {
277                 if ((descriptor = dfunc (i)) == 0) {
278                         break;
279                 }
280
281                 if (!ladspa_plugin_whitelist.empty()) {
282                         if (find (ladspa_plugin_whitelist.begin(), ladspa_plugin_whitelist.end(), descriptor->UniqueID) == ladspa_plugin_whitelist.end()) {
283                                 continue;
284                         }
285                 } 
286
287                 PluginInfoPtr info(new LadspaPluginInfo);
288                 info->name = descriptor->Name;
289                 info->category = get_ladspa_category(descriptor->UniqueID);
290                 info->creator = descriptor->Maker;
291                 info->path = path;
292                 info->index = i;
293                 info->n_inputs = 0;
294                 info->n_outputs = 0;
295                 info->type = ARDOUR::LADSPA;
296                 
297                 char buf[32];
298                 snprintf (buf, sizeof (buf), "%lu", descriptor->UniqueID);
299                 info->unique_id = buf;
300                 
301                 for (uint32_t n=0; n < descriptor->PortCount; ++n) {
302                         if ( LADSPA_IS_PORT_AUDIO (descriptor->PortDescriptors[n]) ) {
303                                 if ( LADSPA_IS_PORT_INPUT (descriptor->PortDescriptors[n]) ) {
304                                         info->n_inputs++;
305                                 }
306                                 else if ( LADSPA_IS_PORT_OUTPUT (descriptor->PortDescriptors[n]) ) {
307                                         info->n_outputs++;
308                                 }
309                         }
310                 }
311
312                 _ladspa_plugin_info.push_back (info);
313         }
314
315 // GDB WILL NOT LIKE YOU IF YOU DO THIS
316 //      dlclose (module);
317
318         return 0;
319 }
320
321 string
322 PluginManager::get_ladspa_category (uint32_t plugin_id)
323 {
324         char buf[256];
325         lrdf_statement pattern;
326
327         snprintf(buf, sizeof(buf), "%s%" PRIu32, LADSPA_BASE, plugin_id);
328         pattern.subject = buf;
329         pattern.predicate = (char*)RDF_TYPE;
330         pattern.object = 0;
331         pattern.object_type = lrdf_uri;
332
333         lrdf_statement* matches1 = lrdf_matches (&pattern);
334
335         if (!matches1) {
336                 return "";
337         }
338
339         pattern.subject = matches1->object;
340         pattern.predicate = (char*)(LADSPA_BASE "hasLabel");
341         pattern.object = 0;
342         pattern.object_type = lrdf_literal;
343
344         lrdf_statement* matches2 = lrdf_matches (&pattern);
345         lrdf_free_statements(matches1);
346
347         if (!matches2) {
348                 return ("");
349         }
350
351         string label = matches2->object;
352         lrdf_free_statements(matches2);
353
354         return label;
355 }
356
357 #ifdef HAVE_SLV2
358 void
359 PluginManager::lv2_refresh ()
360 {
361         lv2_discover();
362 }
363
364 int
365 PluginManager::lv2_discover ()
366 {
367         _lv2_plugin_info = LV2PluginInfo::discover(_lv2_world);
368         return 0;
369 }
370 #endif
371
372 #ifdef HAVE_AUDIOUNITS
373 void
374 PluginManager::au_refresh ()
375 {
376         au_discover();
377 }
378
379 int
380 PluginManager::au_discover ()
381 {
382         _au_plugin_info = AUPluginInfo::discover();
383         return 0;
384 }
385
386 #endif
387
388 #ifdef VST_SUPPORT
389
390 void
391 PluginManager::vst_refresh ()
392 {
393         _vst_plugin_info.clear ();
394
395         if (vst_path.length() == 0) {
396                 vst_path = "/usr/local/lib/vst:/usr/lib/vst";
397         }
398
399         vst_discover_from_path (vst_path);
400 }
401
402 int
403 PluginManager::add_vst_directory (string path)
404 {
405         if (vst_discover_from_path (path) == 0) {
406                 vst_path += ':';
407                 vst_path += path;
408                 return 0;
409         } 
410         return -1;
411 }
412
413 static bool vst_filter (const string& str, void *arg)
414 {
415         /* Not a dotfile, has a prefix before a period, suffix is "dll" */
416
417         return str[0] != '.' && (str.length() > 4 && str.find (".dll") == (str.length() - 4));
418 }
419
420 int
421 PluginManager::vst_discover_from_path (string path)
422 {
423         PathScanner scanner;
424         vector<string *> *plugin_objects;
425         vector<string *>::iterator x;
426         int ret = 0;
427
428         info << "detecting VST plugins along " << path << endmsg;
429
430         plugin_objects = scanner (vst_path, vst_filter, 0, true, true);
431
432         if (plugin_objects) {
433                 for (x = plugin_objects->begin(); x != plugin_objects->end (); ++x) {
434                         vst_discover (**x);
435                 }
436         }
437
438         vector_delete (plugin_objects);
439         return ret;
440 }
441
442 int
443 PluginManager::vst_discover (string path)
444 {
445         FSTInfo* finfo;
446         char buf[32];
447
448         if ((finfo = fst_get_info (const_cast<char *> (path.c_str()))) == 0) {
449                 warning << "Cannot get VST information from " << path << endmsg;
450                 return -1;
451         }
452
453         if (!finfo->canProcessReplacing) {
454                 warning << string_compose (_("VST plugin %1 does not support processReplacing, and so cannot be used in ardour at this time"),
455                                     finfo->name)
456                         << endl;
457         }
458         
459         PluginInfoPtr info(new VSTPluginInfo);
460
461         /* what a goddam joke freeware VST is */
462
463         if (!strcasecmp ("The Unnamed plugin", finfo->name)) {
464                 info->name = PBD::basename_nosuffix (path);
465         } else {
466                 info->name = finfo->name;
467         }
468
469         
470         snprintf (buf, sizeof (buf), "%d", finfo->UniqueID);
471         info->unique_id = buf;
472         info->category = "VST";
473         info->path = path;
474         // need to set info->creator but FST doesn't provide it
475         info->index = 0;
476         info->n_inputs = finfo->numInputs;
477         info->n_outputs = finfo->numOutputs;
478         info->type = ARDOUR::VST;
479         
480         _vst_plugin_info.push_back (info);
481         fst_free_info (finfo);
482
483         return 0;
484 }
485
486 #endif // VST_SUPPORT