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