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