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