bf624127005324ae31d424c23131d905eac7afe9
[ardour.git] / libs / ardour / plugin_manager.cc
1 /*
2     Copyright (C) 2000-2006 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 */
19
20 #define __STDC_FORMAT_MACROS 1
21 #include <stdint.h>
22
23 #include <sys/types.h>
24 #include <cstdio>
25 #include <lrdf.h>
26 #include <dlfcn.h>
27 #include <cstdlib>
28 #include <fstream>
29
30 #ifdef VST_SUPPORT
31 #include <fst.h>
32 #include "pbd/basename.h"
33 #include <cstring>
34 #endif // VST_SUPPORT
35
36 #include <glibmm/miscutils.h>
37
38 #include "pbd/pathscanner.h"
39
40 #include "ardour/ladspa.h"
41 #include "ardour/session.h"
42 #include "ardour/plugin_manager.h"
43 #include "ardour/plugin.h"
44 #include "ardour/ladspa_plugin.h"
45 #include "ardour/filesystem_paths.h"
46
47 #ifdef HAVE_SLV2
48 #include <slv2/slv2.h>
49 #include "ardour/lv2_plugin.h"
50 #endif
51
52 #ifdef VST_SUPPORT
53 #include "ardour/vst_plugin.h"
54 #endif
55
56 #ifdef HAVE_AUDIOUNITS
57 #include "ardour/audio_unit.h"
58 #include <Carbon/Carbon.h>
59 #endif
60
61 #include "pbd/error.h"
62 #include "pbd/stl_delete.h"
63
64 #include "i18n.h"
65
66 using namespace ARDOUR;
67 using namespace PBD;
68 using namespace std;
69
70 PluginManager* PluginManager::_manager = 0;
71
72 PluginManager::PluginManager ()
73 {
74         char* s;
75         string lrdf_path;
76
77         load_favorites ();
78
79 #ifdef GTKOSX
80         ProcessSerialNumber psn = { 0, kCurrentProcess }; 
81         OSStatus returnCode = TransformProcessType(& psn, kProcessTransformToForegroundApplication);
82         if( returnCode != 0) {
83                 error << _("Cannot become GUI app") << endmsg;
84         }
85 #endif
86
87         if ((s = getenv ("LADSPA_RDF_PATH"))){
88                 lrdf_path = s;
89         }
90
91         if (lrdf_path.length() == 0) {
92                 lrdf_path = "/usr/local/share/ladspa/rdf:/usr/share/ladspa/rdf";
93         }
94
95         add_lrdf_data(lrdf_path);
96         add_ladspa_presets();
97 #ifdef VST_SUPPORT
98         if (Config->get_use_vst()) {
99                 add_vst_presets();
100         }
101 #endif /* VST_SUPPORT */
102
103         if ((s = getenv ("LADSPA_PATH"))) {
104                 ladspa_path = s;
105         }
106
107         if ((s = getenv ("VST_PATH"))) {
108                 vst_path = s;
109         } else if ((s = getenv ("VST_PLUGINS"))) {
110                 vst_path = s;
111         }
112
113         if (_manager == 0) {
114                 _manager = this;
115         }
116
117         /* the plugin manager is constructed too early to use Profile */
118
119         if (getenv ("ARDOUR_SAE")) {
120                 ladspa_plugin_whitelist.push_back (1203); // single band parametric
121                 ladspa_plugin_whitelist.push_back (1772); // caps compressor
122                 ladspa_plugin_whitelist.push_back (1913); // fast lookahead limiter
123                 ladspa_plugin_whitelist.push_back (1075); // simple RMS expander
124                 ladspa_plugin_whitelist.push_back (1061); // feedback delay line (max 5s)
125                 ladspa_plugin_whitelist.push_back (1216); // gverb
126                 ladspa_plugin_whitelist.push_back (2150); // tap pitch shifter
127         } 
128
129 #ifdef HAVE_SLV2
130         _lv2_world = new LV2World();
131 #endif
132
133         BootMessage (_("Discovering Plugins"));
134
135         refresh ();
136 }
137
138 void
139 PluginManager::refresh ()
140 {
141         ladspa_refresh ();
142 #ifdef HAVE_SLV2
143         lv2_refresh ();
144 #endif
145 #ifdef VST_SUPPORT
146         if (Config->get_use_vst()) {
147                 vst_refresh ();
148         }
149 #endif // VST_SUPPORT
150 #ifdef HAVE_AUDIOUNITS
151         au_refresh ();
152 #endif
153 }
154
155 void
156 PluginManager::ladspa_refresh ()
157 {
158         _ladspa_plugin_info.clear ();
159         
160         static const char *standard_paths[] = {
161                 "/usr/local/lib64/ladspa",
162                 "/usr/local/lib/ladspa",
163                 "/usr/lib64/ladspa",
164                 "/usr/lib/ladspa",
165                 "/Library/Audio/Plug-Ins/LADSPA",
166                 ""
167         };
168         
169         /* allow LADSPA_PATH to augment, not override standard locations */
170  
171         /* Only add standard locations to ladspa_path if it doesn't
172          * already contain them. Check for trailing '/'s too.
173          */
174          
175         int i;
176         for (i = 0; standard_paths[i][0]; i++) {
177                 size_t found = ladspa_path.find(standard_paths[i]);
178                 if (found != ladspa_path.npos) {
179                         switch (ladspa_path[found + strlen(standard_paths[i])]) {
180                                 case ':' :
181                                 case '\0':
182                                         continue;
183                                 case '/' :
184                                         if (ladspa_path[found + strlen(standard_paths[i]) + 1] == ':' ||
185                                             ladspa_path[found + strlen(standard_paths[i]) + 1] == '\0') {
186                                                 continue;
187                                         }
188                         }
189                 }
190                 if (!ladspa_path.empty())
191                         ladspa_path += ":";
192  
193                 ladspa_path += standard_paths[i]; 
194                 
195         }
196   
197         ladspa_discover_from_path (ladspa_path);
198 }
199
200
201 int
202 PluginManager::add_ladspa_directory (string path)
203 {
204         if (ladspa_discover_from_path (path) == 0) {
205                 ladspa_path += ':';
206                 ladspa_path += path;
207                 return 0;
208         } 
209         return -1;
210 }
211
212 static bool ladspa_filter (const string& str, void *arg)
213 {
214         /* Not a dotfile, has a prefix before a period, suffix is "so" */
215         
216         return str[0] != '.' && (str.length() > 3 && str.find (".so") == (str.length() - 3));
217 }
218
219 int
220 PluginManager::ladspa_discover_from_path (string path)
221 {
222         PathScanner scanner;
223         vector<string *> *plugin_objects;
224         vector<string *>::iterator x;
225         int ret = 0;
226
227         plugin_objects = scanner (ladspa_path, ladspa_filter, 0, true, true);
228
229         if (plugin_objects) {
230                 for (x = plugin_objects->begin(); x != plugin_objects->end (); ++x) {
231                         ladspa_discover (**x);
232                 }
233         }
234
235         vector_delete (plugin_objects);
236         return ret;
237 }
238
239 static bool rdf_filter (const string &str, void *arg)
240 {
241         return str[0] != '.' && 
242                    ((str.find(".rdf")  == (str.length() - 4)) ||
243             (str.find(".rdfs") == (str.length() - 5)) ||
244                     (str.find(".n3")   == (str.length() - 3)));
245 }
246
247 void
248 PluginManager::add_ladspa_presets()
249 {
250         add_presets ("ladspa");
251 }
252
253 void
254 PluginManager::add_vst_presets()
255 {
256         add_presets ("vst");
257 }
258 void
259 PluginManager::add_presets(string domain)
260 {
261
262         PathScanner scanner;
263         vector<string *> *presets;
264         vector<string *>::iterator x;
265
266         char* envvar;
267         if ((envvar = getenv ("HOME")) == 0) {
268                 return;
269         }
270
271         string path = string_compose("%1/.%2/rdf", envvar, domain);
272         presets = scanner (path, rdf_filter, 0, true, true);
273
274         if (presets) {
275                 for (x = presets->begin(); x != presets->end (); ++x) {
276                         string file = "file:" + **x;
277                         if (lrdf_read_file(file.c_str())) {
278                                 warning << string_compose(_("Could not parse rdf file: %1"), *x) << endmsg;
279                         }
280                 }
281         }
282
283         vector_delete (presets);
284 }
285
286 void
287 PluginManager::add_lrdf_data (const string &path)
288 {
289         PathScanner scanner;
290         vector<string *>* rdf_files;
291         vector<string *>::iterator x;
292         string uri;
293
294         rdf_files = scanner (path, rdf_filter, 0, true, true);
295
296         if (rdf_files) {
297                 for (x = rdf_files->begin(); x != rdf_files->end (); ++x) {
298                         uri = "file://" + **x;
299
300                         if (lrdf_read_file(uri.c_str())) {
301                                 warning << "Could not parse rdf file: " << uri << endmsg;
302                         }
303                 }
304         }
305
306         vector_delete (rdf_files);
307 }
308
309 int 
310 PluginManager::ladspa_discover (string path)
311 {
312         void *module;
313         const LADSPA_Descriptor *descriptor;
314         LADSPA_Descriptor_Function dfunc;
315         const char *errstr;
316
317         if ((module = dlopen (path.c_str(), RTLD_NOW)) == 0) {
318                 error << string_compose(_("LADSPA: cannot load module \"%1\" (%2)"), path, dlerror()) << endmsg;
319                 return -1;
320         }
321
322         dfunc = (LADSPA_Descriptor_Function) dlsym (module, "ladspa_descriptor");
323
324         if ((errstr = dlerror()) != 0) {
325                 error << string_compose(_("LADSPA: module \"%1\" has no descriptor function."), path) << endmsg;
326                 error << errstr << endmsg;
327                 dlclose (module);
328                 return -1;
329         }
330
331         for (uint32_t i = 0; ; ++i) {
332                 if ((descriptor = dfunc (i)) == 0) {
333                         break;
334                 }
335
336                 if (!ladspa_plugin_whitelist.empty()) {
337                         if (find (ladspa_plugin_whitelist.begin(), ladspa_plugin_whitelist.end(), descriptor->UniqueID) == ladspa_plugin_whitelist.end()) {
338                                 continue;
339                         }
340                 } 
341
342                 PluginInfoPtr info(new LadspaPluginInfo);
343                 info->name = descriptor->Name;
344                 info->category = get_ladspa_category(descriptor->UniqueID);
345                 info->creator = descriptor->Maker;
346                 info->path = path;
347                 info->index = i;
348                 info->n_inputs = ChanCount();
349                 info->n_outputs = ChanCount();
350                 info->type = ARDOUR::LADSPA;
351                 
352                 char buf[32];
353                 snprintf (buf, sizeof (buf), "%lu", descriptor->UniqueID);
354                 info->unique_id = buf;
355                 
356                 for (uint32_t n=0; n < descriptor->PortCount; ++n) {
357                         if ( LADSPA_IS_PORT_AUDIO (descriptor->PortDescriptors[n]) ) {
358                                 if ( LADSPA_IS_PORT_INPUT (descriptor->PortDescriptors[n]) ) {
359                                         info->n_inputs.set_audio(info->n_inputs.n_audio() + 1);
360                                 }
361                                 else if ( LADSPA_IS_PORT_OUTPUT (descriptor->PortDescriptors[n]) ) {
362                                         info->n_outputs.set_audio(info->n_outputs.n_audio() + 1);
363                                 }
364                         }
365                 }
366
367                 _ladspa_plugin_info.push_back (info);
368         }
369
370 // GDB WILL NOT LIKE YOU IF YOU DO THIS
371 //      dlclose (module);
372
373         return 0;
374 }
375
376 string
377 PluginManager::get_ladspa_category (uint32_t plugin_id)
378 {
379         char buf[256];
380         lrdf_statement pattern;
381
382         snprintf(buf, sizeof(buf), "%s%" PRIu32, LADSPA_BASE, plugin_id);
383         pattern.subject = buf;
384         pattern.predicate = (char*)RDF_TYPE;
385         pattern.object = 0;
386         pattern.object_type = lrdf_uri;
387
388         lrdf_statement* matches1 = lrdf_matches (&pattern);
389
390         if (!matches1) {
391                 return "";
392         }
393
394         pattern.subject = matches1->object;
395         pattern.predicate = (char*)(LADSPA_BASE "hasLabel");
396         pattern.object = 0;
397         pattern.object_type = lrdf_literal;
398
399         lrdf_statement* matches2 = lrdf_matches (&pattern);
400         lrdf_free_statements(matches1);
401
402         if (!matches2) {
403                 return ("");
404         }
405
406         string label = matches2->object;
407         lrdf_free_statements(matches2);
408
409         return label;
410 }
411
412 #ifdef HAVE_SLV2
413 void
414 PluginManager::lv2_refresh ()
415 {
416         lv2_discover();
417 }
418
419 int
420 PluginManager::lv2_discover ()
421 {
422         _lv2_plugin_info = LV2PluginInfo::discover(_lv2_world);
423         return 0;
424 }
425 #endif
426
427 #ifdef HAVE_AUDIOUNITS
428 void
429 PluginManager::au_refresh ()
430 {
431         au_discover();
432 }
433
434 int
435 PluginManager::au_discover ()
436 {
437         _au_plugin_info = AUPluginInfo::discover();
438         return 0;
439 }
440
441 #endif
442
443 #ifdef VST_SUPPORT
444
445 void
446 PluginManager::vst_refresh ()
447 {
448         _vst_plugin_info.clear ();
449
450         if (vst_path.length() == 0) {
451                 vst_path = "/usr/local/lib/vst:/usr/lib/vst";
452         }
453
454         vst_discover_from_path (vst_path);
455 }
456
457 int
458 PluginManager::add_vst_directory (string path)
459 {
460         if (vst_discover_from_path (path) == 0) {
461                 vst_path += ':';
462                 vst_path += path;
463                 return 0;
464         } 
465         return -1;
466 }
467
468 static bool vst_filter (const string& str, void *arg)
469 {
470         /* Not a dotfile, has a prefix before a period, suffix is "dll" */
471
472         return str[0] != '.' && (str.length() > 4 && str.find (".dll") == (str.length() - 4));
473 }
474
475 int
476 PluginManager::vst_discover_from_path (string path)
477 {
478         PathScanner scanner;
479         vector<string *> *plugin_objects;
480         vector<string *>::iterator x;
481         int ret = 0;
482
483         info << "detecting VST plugins along " << path << endmsg;
484
485         plugin_objects = scanner (vst_path, vst_filter, 0, true, true);
486
487         if (plugin_objects) {
488                 for (x = plugin_objects->begin(); x != plugin_objects->end (); ++x) {
489                         vst_discover (**x);
490                 }
491         }
492
493         vector_delete (plugin_objects);
494         return ret;
495 }
496
497 int
498 PluginManager::vst_discover (string path)
499 {
500         FSTInfo* finfo;
501         char buf[32];
502
503         if ((finfo = fst_get_info (const_cast<char *> (path.c_str()))) == 0) {
504                 warning << "Cannot get VST information from " << path << endmsg;
505                 return -1;
506         }
507
508         if (!finfo->canProcessReplacing) {
509                 warning << string_compose (_("VST plugin %1 does not support processReplacing, and so cannot be used in ardour at this time"),
510                                     finfo->name)
511                         << endl;
512         }
513         
514         PluginInfoPtr info(new VSTPluginInfo);
515
516         /* what a joke freeware VST is */
517
518         if (!strcasecmp ("The Unnamed plugin", finfo->name)) {
519                 info->name = PBD::basename_nosuffix (path);
520         } else {
521                 info->name = finfo->name;
522         }
523
524         
525         snprintf (buf, sizeof (buf), "%d", finfo->UniqueID);
526         info->unique_id = buf;
527         info->category = "VST";
528         info->path = path;
529         // need to set info->creator but FST doesn't provide it
530         info->index = 0;
531         info->n_inputs = finfo->numInputs;
532         info->n_outputs = finfo->numOutputs;
533         info->type = ARDOUR::VST;
534         
535         _vst_plugin_info.push_back (info);
536         fst_free_info (finfo);
537
538         return 0;
539 }
540
541 #endif // VST_SUPPORT
542
543 bool
544 PluginManager::is_a_favorite_plugin (const PluginInfoPtr& pi)
545 {
546         FavoritePlugin fp (pi->type, pi->unique_id);
547         return find (favorites.begin(), favorites.end(), fp) !=  favorites.end();
548 }
549
550 void
551 PluginManager::save_favorites ()
552 {
553         ofstream ofs;
554         sys::path path = user_config_directory();
555         path /= "favorite_plugins";
556
557         ofs.open (path.to_string().c_str(), ios_base::openmode (ios::out|ios::trunc));
558
559         if (!ofs) {
560                 return;
561         }
562
563         for (FavoritePluginList::iterator i = favorites.begin(); i != favorites.end(); ++i) {
564                 switch ((*i).type) {
565                 case LADSPA:
566                         ofs << "LADSPA";
567                         break;
568                 case AudioUnit:
569                         ofs << "AudioUnit";
570                         break;
571                 case LV2:
572                         ofs << "LV2";
573                         break;
574                 case VST:
575                         ofs << "VST";
576                         break;
577                 }
578                 
579                 ofs << ' ' << (*i).unique_id << endl;
580         }
581
582         ofs.close ();
583 }
584
585 void
586 PluginManager::load_favorites ()
587 {
588         sys::path path = user_config_directory();
589         path /= "favorite_plugins";
590         ifstream ifs (path.to_string().c_str());
591
592         if (!ifs) {
593                 return;
594         }
595         
596         std::string stype;
597         std::string id;
598         PluginType type;
599
600         while (ifs) {
601
602                 ifs >> stype;
603                 if (!ifs) {
604                         break;
605
606                 }
607                 ifs >> id;
608                 if (!ifs) {
609                         break;
610                 }
611
612                 if (stype == "LADSPA") {
613                         type = LADSPA;
614                 } else if (stype == "AudioUnit") {
615                         type = AudioUnit;
616                 } else if (stype == "LV2") {
617                         type = LV2;
618                 } else if (stype == "VST") {
619                         type = VST;
620                 } else {
621                         error << string_compose (_("unknown favorite plugin type \"%1\" - ignored"), stype)
622                               << endmsg;
623                         continue;
624                 }
625                 
626                 add_favorite (type, id);
627         }
628         
629         ifs.close ();
630 }
631
632 void
633 PluginManager::add_favorite (PluginType t, string id)
634 {
635         FavoritePlugin fp (t, id);
636         pair<FavoritePluginList::iterator,bool> res = favorites.insert (fp);
637         //cerr << "Added " << t << " " << id << " success ? " << res.second << endl;
638 }
639
640 void
641 PluginManager::remove_favorite (PluginType t, string id)
642 {
643         FavoritePlugin fp (t, id);
644         favorites.erase (fp);
645 }