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