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