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