Merged up to trunk R732
[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
41 #include <pbd/error.h>
42 #include <pbd/stl_delete.h>
43
44 #ifdef HAVE_COREAUDIO
45 #include <CoreServices/CoreServices.h>
46 #include <AudioUnit/AudioUnit.h>
47 #endif // HAVE_COREAUDIO
48
49 #include "i18n.h"
50
51 using namespace ARDOUR;
52 using namespace PBD;
53
54 PluginManager* PluginManager::_manager = 0;
55
56 PluginManager::PluginManager (AudioEngine& e)
57         : _engine (e)
58 {
59         char* s;
60         string lrdf_path;
61
62         if ((s = getenv ("LADSPA_RDF_PATH"))){
63                 lrdf_path = s;
64         }
65
66         if (lrdf_path.length() == 0) {
67                 lrdf_path = "/usr/local/share/ladspa/rdf:/usr/share/ladspa/rdf";
68         }
69
70         add_lrdf_data(lrdf_path);
71         add_ladspa_presets();
72 #ifdef VST_SUPPORT
73         if (Config->get_use_vst()) {
74                 add_vst_presets();
75         }
76 #endif /* VST_SUPPORT */
77
78         if ((s = getenv ("LADSPA_PATH"))) {
79                 ladspa_path = s;
80         }
81
82         if ((s = getenv ("VST_PATH"))) {
83                 vst_path = s;
84         } else if ((s = getenv ("VST_PLUGINS"))) {
85                 vst_path = s;
86         }
87
88         refresh ();
89
90         if (_manager == 0) {
91                 _manager = this;
92         }
93 }
94
95 void
96 PluginManager::refresh ()
97 {
98         ladspa_refresh ();
99 #ifdef VST_SUPPORT
100         if (Config->get_use_vst()) {
101                 vst_refresh ();
102         }
103 #endif // VST_SUPPORT
104
105 #ifdef HAVE_COREAUDIO
106         au_discover ();
107 #endif // HAVE_COREAUDIO
108 }
109
110 void
111 PluginManager::ladspa_refresh ()
112 {
113         for (std::list<PluginInfo*>::iterator i = _ladspa_plugin_info.begin(); i != _ladspa_plugin_info.end(); ++i) {
114                 delete *i;
115         }
116
117         _ladspa_plugin_info.clear ();
118
119         if (ladspa_path.length() == 0) {
120                 ladspa_path = "/usr/local/lib/ladspa:/usr/lib/ladspa";
121         }
122
123         ladspa_discover_from_path (ladspa_path);
124 }
125
126 int
127 PluginManager::add_ladspa_directory (string path)
128 {
129         if (ladspa_discover_from_path (path) == 0) {
130                 ladspa_path += ':';
131                 ladspa_path += path;
132                 return 0;
133         } 
134         return -1;
135 }
136
137 static bool ladspa_filter (const string& str, void *arg)
138 {
139         /* Not a dotfile, has a prefix before a period, suffix is "so" */
140         
141         return str[0] != '.' && (str.length() > 3 && str.find (".so") == (str.length() - 3));
142 }
143
144 int
145 PluginManager::ladspa_discover_from_path (string path)
146 {
147         PathScanner scanner;
148         vector<string *> *plugin_objects;
149         vector<string *>::iterator x;
150         int ret = 0;
151
152         plugin_objects = scanner (ladspa_path, ladspa_filter, 0, true, true);
153
154         if (plugin_objects) {
155                 for (x = plugin_objects->begin(); x != plugin_objects->end (); ++x) {
156                         ladspa_discover (**x);
157                 }
158         }
159
160         vector_delete (plugin_objects);
161         return ret;
162 }
163
164 static bool rdf_filter (const string &str, void *arg)
165 {
166         return str[0] != '.' && 
167                    ((str.find(".rdf")  == (str.length() - 4)) ||
168             (str.find(".rdfs") == (str.length() - 5)) ||
169                     (str.find(".n3")   == (str.length() - 3)));
170 }
171
172 void
173 PluginManager::add_ladspa_presets()
174 {
175         add_presets ("ladspa");
176 }
177
178 void
179 PluginManager::add_vst_presets()
180 {
181         add_presets ("vst");
182 }
183 void
184 PluginManager::add_presets(string domain)
185 {
186
187         PathScanner scanner;
188         vector<string *> *presets;
189         vector<string *>::iterator x;
190
191         char* envvar;
192         if ((envvar = getenv ("HOME")) == 0) {
193                 return;
194         }
195
196         string path = string_compose("%1/.%2/rdf", envvar, domain);
197         presets = scanner (path, rdf_filter, 0, true, true);
198
199         if (presets) {
200                 for (x = presets->begin(); x != presets->end (); ++x) {
201                         string file = "file:" + **x;
202                         if (lrdf_read_file(file.c_str())) {
203                                 warning << string_compose(_("Could not parse rdf file: %1"), *x) << endmsg;
204                         }
205                 }
206         }
207
208         vector_delete (presets);
209 }
210
211 void
212 PluginManager::add_lrdf_data (const string &path)
213 {
214         PathScanner scanner;
215         vector<string *>* rdf_files;
216         vector<string *>::iterator x;
217         string uri;
218
219         rdf_files = scanner (path, rdf_filter, 0, true, true);
220
221         if (rdf_files) {
222                 for (x = rdf_files->begin(); x != rdf_files->end (); ++x) {
223                         uri = "file://" + **x;
224
225                         if (lrdf_read_file(uri.c_str())) {
226                                 warning << "Could not parse rdf file: " << uri << endmsg;
227                         }
228                 }
229         }
230
231         vector_delete (rdf_files);
232 }
233
234 int 
235 PluginManager::ladspa_discover (string path)
236 {
237         PluginInfo *info;
238         void *module;
239         const LADSPA_Descriptor *descriptor;
240         LADSPA_Descriptor_Function dfunc;
241         const char *errstr;
242
243         if ((module = dlopen (path.c_str(), RTLD_NOW)) == 0) {
244                 error << string_compose(_("LADSPA: cannot load module \"%1\" (%2)"), path, dlerror()) << endmsg;
245                 return -1;
246         }
247
248         dfunc = (LADSPA_Descriptor_Function) dlsym (module, "ladspa_descriptor");
249
250         if ((errstr = dlerror()) != 0) {
251                 error << string_compose(_("LADSPA: module \"%1\" has no descriptor function."), path) << endmsg;
252                 error << errstr << endmsg;
253                 dlclose (module);
254                 return -1;
255         }
256
257         for (uint32_t i = 0; ; ++i) {
258                 if ((descriptor = dfunc (i)) == 0) {
259                         break;
260                 }
261
262                 info = new PluginInfo;
263                 info->name = descriptor->Name;
264                 info->category = get_ladspa_category(descriptor->UniqueID);
265                 info->path = path;
266                 info->index = i;
267                 info->n_inputs = 0;
268                 info->n_outputs = 0;
269                 info->type = PluginInfo::LADSPA;
270                 info->unique_id = descriptor->UniqueID;
271                 
272                 for (uint32_t n=0; n < descriptor->PortCount; ++n) {
273                         if ( LADSPA_IS_PORT_AUDIO (descriptor->PortDescriptors[n]) ) {
274                                 if ( LADSPA_IS_PORT_INPUT (descriptor->PortDescriptors[n]) ) {
275                                         info->n_inputs++;
276                                 }
277                                 else if ( LADSPA_IS_PORT_OUTPUT (descriptor->PortDescriptors[n]) ) {
278                                         info->n_outputs++;
279                                 }
280                         }
281                 }
282
283                 _ladspa_plugin_info.push_back (info);
284         }
285
286 // GDB WILL NOT LIKE YOU IF YOU DO THIS
287 //      dlclose (module);
288
289         return 0;
290 }
291
292 boost::shared_ptr<Plugin>
293 PluginManager::load (Session& session, PluginInfo *info)
294 {
295         void *module;
296
297         try {
298                 boost::shared_ptr<Plugin> plugin;
299
300                 if (info->type == PluginInfo::VST) {
301
302 #ifdef VST_SUPPORT                      
303                         if (Config->get_use_vst()) {
304                                 FSTHandle* handle;
305                                 
306                                 if ((handle = fst_load (info->path.c_str())) == 0) {
307                                         error << string_compose(_("VST: cannot load module from \"%1\""), info->path) << endmsg;
308                                 } else {
309                                         plugin.reset (new VSTPlugin (_engine, session, handle));
310                                 }
311                         } else {
312                                 error << _("You asked ardour to not use any VST plugins") << endmsg;
313                         }
314 #else // !VST_SUPPORT
315                         error << _("This version of ardour has no support for VST plugins") << endmsg;
316                         return boost::shared_ptr<Plugin> ((Plugin*) 0);
317 #endif // !VST_SUPPORT
318                                 
319                 } else {
320
321                         if ((module = dlopen (info->path.c_str(), RTLD_NOW)) == 0) {
322                                 error << string_compose(_("LADSPA: cannot load module from \"%1\""), info->path) << endmsg;
323                                 error << dlerror() << endmsg;
324                         } else {
325                                 plugin.reset (new LadspaPlugin (module, _engine, session, info->index, session.frame_rate()));
326                         }
327                 }
328
329                 plugin->set_info(*info);
330                 return plugin;
331         }
332
333         catch (failed_constructor &err) {
334                 return boost::shared_ptr<Plugin> ((Plugin*) 0);
335         }
336 }
337
338 boost::shared_ptr<Plugin>
339 ARDOUR::find_plugin(Session& session, string name, long unique_id, PluginInfo::Type type)
340 {
341         PluginManager *mgr = PluginManager::the_manager();
342         list<PluginInfo *>::iterator i;
343         list<PluginInfo *>* plugs = 0;
344
345         switch (type) {
346         case PluginInfo::LADSPA:
347                 plugs = &mgr->ladspa_plugin_info();
348                 break;
349         case PluginInfo::VST:
350                 plugs = &mgr->vst_plugin_info();
351                 unique_id = 0; // VST plugins don't have a unique id.
352                 break;
353         case PluginInfo::AudioUnit:
354                 plugs = &mgr->au_plugin_info();
355                 unique_id = 0;
356                 break;
357         default:
358                 return boost::shared_ptr<Plugin> ((Plugin *) 0);
359         }
360
361         for (i = plugs->begin(); i != plugs->end(); ++i) {
362                 if ((name == "" || (*i)->name == name) &&
363                         (unique_id == 0 || (*i)->unique_id == unique_id)) {     
364                         return mgr->load (session, *i);
365                 }
366         }
367         
368         return boost::shared_ptr<Plugin> ((Plugin*) 0);
369 }
370
371 string
372 PluginManager::get_ladspa_category (uint32_t plugin_id)
373 {
374         char buf[256];
375         lrdf_statement pattern;
376
377         snprintf(buf, sizeof(buf), "%s%" PRIu32, LADSPA_BASE, plugin_id);
378         pattern.subject = buf;
379         pattern.predicate = RDF_TYPE;
380         pattern.object = 0;
381         pattern.object_type = lrdf_uri;
382
383         lrdf_statement* matches1 = lrdf_matches (&pattern);
384
385         if (!matches1) {
386                 return _("Unknown");
387         }
388
389         pattern.subject = matches1->object;
390         pattern.predicate = LADSPA_BASE "hasLabel";
391         pattern.object = 0;
392         pattern.object_type = lrdf_literal;
393
394         lrdf_statement* matches2 = lrdf_matches (&pattern);
395         lrdf_free_statements(matches1);
396
397         if (!matches2) {
398                 return _("Unknown");
399         }
400
401         string label = matches2->object;
402         lrdf_free_statements(matches2);
403
404         return label;
405 }
406
407 #ifdef VST_SUPPORT
408
409 void
410 PluginManager::vst_refresh ()
411 {
412         for (std::list<PluginInfo*>::iterator i = _vst_plugin_info.begin(); i != _vst_plugin_info.end(); ++i) {
413                 delete *i;
414         }
415
416         _vst_plugin_info.clear ();
417
418         if (vst_path.length() == 0) {
419                 vst_path = "/usr/local/lib/vst:/usr/lib/vst";
420         }
421
422         vst_discover_from_path (vst_path);
423 }
424
425 int
426 PluginManager::add_vst_directory (string path)
427 {
428         if (vst_discover_from_path (path) == 0) {
429                 vst_path += ':';
430                 vst_path += path;
431                 return 0;
432         } 
433         return -1;
434 }
435
436 static bool vst_filter (const string& str, void *arg)
437 {
438         /* Not a dotfile, has a prefix before a period, suffix is "dll" */
439         
440         return str[0] != '.' && (str.length() > 4 && str.find (".dll") == (str.length() - 4));
441 }
442
443 int
444 PluginManager::vst_discover_from_path (string path)
445 {
446         PathScanner scanner;
447         vector<string *> *plugin_objects;
448         vector<string *>::iterator x;
449         int ret = 0;
450
451         info << "detecting VST plugins along " << path << endmsg;
452
453         plugin_objects = scanner (vst_path, vst_filter, 0, true, true);
454
455         if (plugin_objects) {
456                 for (x = plugin_objects->begin(); x != plugin_objects->end (); ++x) {
457                         vst_discover (**x);
458                 }
459         }
460
461         vector_delete (plugin_objects);
462         return ret;
463 }
464
465 int
466 PluginManager::vst_discover (string path)
467 {
468         FSTInfo* finfo;
469         PluginInfo* info;
470
471         if ((finfo = fst_get_info (const_cast<char *> (path.c_str()))) == 0) {
472                 return -1;
473         }
474
475         if (!finfo->canProcessReplacing) {
476                 warning << string_compose (_("VST plugin %1 does not support processReplacing, and so cannot be used in ardour at this time"),
477                                     finfo->name)
478                         << endl;
479         }
480         
481         info = new PluginInfo;
482
483         /* what a goddam joke freeware VST is */
484
485         if (!strcasecmp ("The Unnamed plugin", finfo->name)) {
486                 info->name = PBD::basename_nosuffix (path);
487         } else {
488                 info->name = finfo->name;
489         }
490
491         info->category = "VST";
492         info->path = path;
493         info->index = 0;
494         info->n_inputs = finfo->numInputs;
495         info->n_outputs = finfo->numOutputs;
496         info->type = PluginInfo::VST;
497         
498         _vst_plugin_info.push_back (info);
499         fst_free_info (finfo);
500
501         return 0;
502 }
503
504 #endif // VST_SUPPORT
505
506 #ifdef HAVE_COREAUDIO
507
508 int
509 PluginManager::au_discover ()
510 {
511         _au_plugin_info.clear ();
512         
513         int numTypes = 2;    // this magic number was retrieved from the apple AUHost example.
514
515         ComponentDescription desc;
516         desc.componentFlags = 0;
517         desc.componentFlagsMask = 0;
518         desc.componentSubType = 0;
519         desc.componentManufacturer = 0;
520         
521         vector<ComponentDescription> vCompDescs;
522
523         for (int i = 0; i < numTypes; ++i) {
524                 if (i == 1) {
525                         desc.componentType = kAudioUnitType_MusicEffect;
526                 } else {
527                         desc.componentType = kAudioUnitType_Effect;
528                 }
529                 
530                 Component comp = 0;
531
532                 comp = FindNextComponent (NULL, &desc);
533                 while (comp != NULL) {
534                         ComponentDescription temp;
535                         GetComponentInfo (comp, &temp, NULL, NULL, NULL);
536                         vCompDescs.push_back(temp);
537                         comp = FindNextComponent (comp, &desc);
538                 }
539         }
540
541         PluginInfo* plug;
542         for (unsigned int i = 0; i < vCompDescs.size(); ++i) {
543
544                 // the following large block is just for determining the name of the plugin.
545                 CFStringRef itemName = NULL;
546                 // Marc Poirier -style item name
547                 Component auComponent = FindNextComponent (0, &(vCompDescs[i]));
548                 if (auComponent != NULL) {
549                         ComponentDescription dummydesc;
550                         Handle nameHandle = NewHandle(sizeof(void*));
551                         if (nameHandle != NULL) {
552                                 OSErr err = GetComponentInfo(auComponent, &dummydesc, nameHandle, NULL, NULL);
553                                 if (err == noErr) {
554                                         ConstStr255Param nameString = (ConstStr255Param) (*nameHandle);
555                                         if (nameString != NULL) {
556                                                 itemName = CFStringCreateWithPascalString(kCFAllocatorDefault, nameString, CFStringGetSystemEncoding());
557                                         }
558                                 }
559                                 DisposeHandle(nameHandle);
560                         }
561                 }
562                 
563                 // if Marc-style fails, do the original way
564                 if (itemName == NULL) {
565                         CFStringRef compTypeString = UTCreateStringForOSType(vCompDescs[i].componentType);
566                         CFStringRef compSubTypeString = UTCreateStringForOSType(vCompDescs[i].componentSubType);
567                         CFStringRef compManufacturerString = UTCreateStringForOSType(vCompDescs[i].componentManufacturer);
568                         
569                         itemName = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%@ - %@ - %@"), 
570                                 compTypeString, compManufacturerString, compSubTypeString);
571
572                         if (compTypeString != NULL)
573                                 CFRelease(compTypeString);
574                         if (compSubTypeString != NULL)
575                                 CFRelease(compSubTypeString);
576                         if (compManufacturerString != NULL)
577                                 CFRelease(compManufacturerString);
578                 }
579                 string realname = CFStringRefToStdString(itemName);
580                 
581                 plug = new PluginInfo;
582                 plug->name = realname;
583                 plug->type = PluginInfo::AudioUnit;
584                 plug->n_inputs = 0;
585                 plug->n_outputs = 0;
586                 plug->category = "AudioUnit";
587                 
588                 _au_plugin_info.push_back(plug);
589         }
590
591         return 0;
592 }
593
594 #endif // HAVE_COREAUDIO
595