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