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