* first primitive implementation of MidiPatchManager
[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         refresh ();
134 }
135
136 void
137 PluginManager::refresh ()
138 {
139         ladspa_refresh ();
140 #ifdef HAVE_SLV2
141         lv2_refresh ();
142 #endif
143 #ifdef VST_SUPPORT
144         if (Config->get_use_vst()) {
145                 vst_refresh ();
146         }
147 #endif // VST_SUPPORT
148 #ifdef HAVE_AUDIOUNITS
149         au_refresh ();
150 #endif
151 }
152
153 void
154 PluginManager::ladspa_refresh ()
155 {
156         _ladspa_plugin_info.clear ();
157
158         if (ladspa_path.length() == 0) {
159                 ladspa_path = "/usr/local/lib64/ladspa:/usr/local/lib/ladspa:/usr/lib64/ladspa:/usr/lib/ladspa:/Library/Audio/Plug-Ins/LADSPA";
160         }
161
162         ladspa_discover_from_path (ladspa_path);
163 }
164
165
166 int
167 PluginManager::add_ladspa_directory (string path)
168 {
169         if (ladspa_discover_from_path (path) == 0) {
170                 ladspa_path += ':';
171                 ladspa_path += path;
172                 return 0;
173         } 
174         return -1;
175 }
176
177 static bool ladspa_filter (const string& str, void *arg)
178 {
179         /* Not a dotfile, has a prefix before a period, suffix is "so" */
180         
181         return str[0] != '.' && (str.length() > 3 && str.find (".so") == (str.length() - 3));
182 }
183
184 int
185 PluginManager::ladspa_discover_from_path (string path)
186 {
187         PathScanner scanner;
188         vector<string *> *plugin_objects;
189         vector<string *>::iterator x;
190         int ret = 0;
191
192         plugin_objects = scanner (ladspa_path, ladspa_filter, 0, true, true);
193
194         if (plugin_objects) {
195                 for (x = plugin_objects->begin(); x != plugin_objects->end (); ++x) {
196                         ladspa_discover (**x);
197                 }
198         }
199
200         vector_delete (plugin_objects);
201         return ret;
202 }
203
204 static bool rdf_filter (const string &str, void *arg)
205 {
206         return str[0] != '.' && 
207                    ((str.find(".rdf")  == (str.length() - 4)) ||
208             (str.find(".rdfs") == (str.length() - 5)) ||
209                     (str.find(".n3")   == (str.length() - 3)));
210 }
211
212 void
213 PluginManager::add_ladspa_presets()
214 {
215         add_presets ("ladspa");
216 }
217
218 void
219 PluginManager::add_vst_presets()
220 {
221         add_presets ("vst");
222 }
223 void
224 PluginManager::add_presets(string domain)
225 {
226
227         PathScanner scanner;
228         vector<string *> *presets;
229         vector<string *>::iterator x;
230
231         char* envvar;
232         if ((envvar = getenv ("HOME")) == 0) {
233                 return;
234         }
235
236         string path = string_compose("%1/.%2/rdf", envvar, domain);
237         presets = scanner (path, rdf_filter, 0, true, true);
238
239         if (presets) {
240                 for (x = presets->begin(); x != presets->end (); ++x) {
241                         string file = "file:" + **x;
242                         if (lrdf_read_file(file.c_str())) {
243                                 warning << string_compose(_("Could not parse rdf file: %1"), *x) << endmsg;
244                         }
245                 }
246         }
247
248         vector_delete (presets);
249 }
250
251 void
252 PluginManager::add_lrdf_data (const string &path)
253 {
254         PathScanner scanner;
255         vector<string *>* rdf_files;
256         vector<string *>::iterator x;
257         string uri;
258
259         rdf_files = scanner (path, rdf_filter, 0, true, true);
260
261         if (rdf_files) {
262                 for (x = rdf_files->begin(); x != rdf_files->end (); ++x) {
263                         uri = "file://" + **x;
264
265                         if (lrdf_read_file(uri.c_str())) {
266                                 warning << "Could not parse rdf file: " << uri << endmsg;
267                         }
268                 }
269         }
270
271         vector_delete (rdf_files);
272 }
273
274 int 
275 PluginManager::ladspa_discover (string path)
276 {
277         void *module;
278         const LADSPA_Descriptor *descriptor;
279         LADSPA_Descriptor_Function dfunc;
280         const char *errstr;
281
282         if ((module = dlopen (path.c_str(), RTLD_NOW)) == 0) {
283                 error << string_compose(_("LADSPA: cannot load module \"%1\" (%2)"), path, dlerror()) << endmsg;
284                 return -1;
285         }
286
287         dfunc = (LADSPA_Descriptor_Function) dlsym (module, "ladspa_descriptor");
288
289         if ((errstr = dlerror()) != 0) {
290                 error << string_compose(_("LADSPA: module \"%1\" has no descriptor function."), path) << endmsg;
291                 error << errstr << endmsg;
292                 dlclose (module);
293                 return -1;
294         }
295
296         for (uint32_t i = 0; ; ++i) {
297                 if ((descriptor = dfunc (i)) == 0) {
298                         break;
299                 }
300
301                 if (!ladspa_plugin_whitelist.empty()) {
302                         if (find (ladspa_plugin_whitelist.begin(), ladspa_plugin_whitelist.end(), descriptor->UniqueID) == ladspa_plugin_whitelist.end()) {
303                                 continue;
304                         }
305                 } 
306
307                 PluginInfoPtr info(new LadspaPluginInfo);
308                 info->name = descriptor->Name;
309                 info->category = get_ladspa_category(descriptor->UniqueID);
310                 info->creator = descriptor->Maker;
311                 info->path = path;
312                 info->index = i;
313                 info->n_inputs = ChanCount();
314                 info->n_outputs = ChanCount();
315                 info->type = ARDOUR::LADSPA;
316                 
317                 char buf[32];
318                 snprintf (buf, sizeof (buf), "%lu", descriptor->UniqueID);
319                 info->unique_id = buf;
320                 
321                 for (uint32_t n=0; n < descriptor->PortCount; ++n) {
322                         if ( LADSPA_IS_PORT_AUDIO (descriptor->PortDescriptors[n]) ) {
323                                 if ( LADSPA_IS_PORT_INPUT (descriptor->PortDescriptors[n]) ) {
324                                         info->n_inputs.set_audio(info->n_inputs.n_audio() + 1);
325                                 }
326                                 else if ( LADSPA_IS_PORT_OUTPUT (descriptor->PortDescriptors[n]) ) {
327                                         info->n_outputs.set_audio(info->n_outputs.n_audio() + 1);
328                                 }
329                         }
330                 }
331
332                 _ladspa_plugin_info.push_back (info);
333         }
334
335 // GDB WILL NOT LIKE YOU IF YOU DO THIS
336 //      dlclose (module);
337
338         return 0;
339 }
340
341 string
342 PluginManager::get_ladspa_category (uint32_t plugin_id)
343 {
344         char buf[256];
345         lrdf_statement pattern;
346
347         snprintf(buf, sizeof(buf), "%s%" PRIu32, LADSPA_BASE, plugin_id);
348         pattern.subject = buf;
349         pattern.predicate = (char*)RDF_TYPE;
350         pattern.object = 0;
351         pattern.object_type = lrdf_uri;
352
353         lrdf_statement* matches1 = lrdf_matches (&pattern);
354
355         if (!matches1) {
356                 return "";
357         }
358
359         pattern.subject = matches1->object;
360         pattern.predicate = (char*)(LADSPA_BASE "hasLabel");
361         pattern.object = 0;
362         pattern.object_type = lrdf_literal;
363
364         lrdf_statement* matches2 = lrdf_matches (&pattern);
365         lrdf_free_statements(matches1);
366
367         if (!matches2) {
368                 return ("");
369         }
370
371         string label = matches2->object;
372         lrdf_free_statements(matches2);
373
374         return label;
375 }
376
377 #ifdef HAVE_SLV2
378 void
379 PluginManager::lv2_refresh ()
380 {
381         lv2_discover();
382 }
383
384 int
385 PluginManager::lv2_discover ()
386 {
387         _lv2_plugin_info = LV2PluginInfo::discover(_lv2_world);
388         return 0;
389 }
390 #endif
391
392 #ifdef HAVE_AUDIOUNITS
393 void
394 PluginManager::au_refresh ()
395 {
396         au_discover();
397 }
398
399 int
400 PluginManager::au_discover ()
401 {
402         _au_plugin_info = AUPluginInfo::discover();
403         return 0;
404 }
405
406 #endif
407
408 #ifdef VST_SUPPORT
409
410 void
411 PluginManager::vst_refresh ()
412 {
413         _vst_plugin_info.clear ();
414
415         if (vst_path.length() == 0) {
416                 vst_path = "/usr/local/lib/vst:/usr/lib/vst";
417         }
418
419         vst_discover_from_path (vst_path);
420 }
421
422 int
423 PluginManager::add_vst_directory (string path)
424 {
425         if (vst_discover_from_path (path) == 0) {
426                 vst_path += ':';
427                 vst_path += path;
428                 return 0;
429         } 
430         return -1;
431 }
432
433 static bool vst_filter (const string& str, void *arg)
434 {
435         /* Not a dotfile, has a prefix before a period, suffix is "dll" */
436
437         return str[0] != '.' && (str.length() > 4 && str.find (".dll") == (str.length() - 4));
438 }
439
440 int
441 PluginManager::vst_discover_from_path (string path)
442 {
443         PathScanner scanner;
444         vector<string *> *plugin_objects;
445         vector<string *>::iterator x;
446         int ret = 0;
447
448         info << "detecting VST plugins along " << path << endmsg;
449
450         plugin_objects = scanner (vst_path, vst_filter, 0, true, true);
451
452         if (plugin_objects) {
453                 for (x = plugin_objects->begin(); x != plugin_objects->end (); ++x) {
454                         vst_discover (**x);
455                 }
456         }
457
458         vector_delete (plugin_objects);
459         return ret;
460 }
461
462 int
463 PluginManager::vst_discover (string path)
464 {
465         FSTInfo* finfo;
466         char buf[32];
467
468         if ((finfo = fst_get_info (const_cast<char *> (path.c_str()))) == 0) {
469                 warning << "Cannot get VST information from " << path << endmsg;
470                 return -1;
471         }
472
473         if (!finfo->canProcessReplacing) {
474                 warning << string_compose (_("VST plugin %1 does not support processReplacing, and so cannot be used in ardour at this time"),
475                                     finfo->name)
476                         << endl;
477         }
478         
479         PluginInfoPtr info(new VSTPluginInfo);
480
481         /* what a joke freeware VST is */
482
483         if (!strcasecmp ("The Unnamed plugin", finfo->name)) {
484                 info->name = PBD::basename_nosuffix (path);
485         } else {
486                 info->name = finfo->name;
487         }
488
489         
490         snprintf (buf, sizeof (buf), "%d", finfo->UniqueID);
491         info->unique_id = buf;
492         info->category = "VST";
493         info->path = path;
494         // need to set info->creator but FST doesn't provide it
495         info->index = 0;
496         info->n_inputs = finfo->numInputs;
497         info->n_outputs = finfo->numOutputs;
498         info->type = ARDOUR::VST;
499         
500         _vst_plugin_info.push_back (info);
501         fst_free_info (finfo);
502
503         return 0;
504 }
505
506 #endif // VST_SUPPORT
507
508 bool
509 PluginManager::is_a_favorite_plugin (const PluginInfoPtr& pi)
510 {
511         FavoritePlugin fp (pi->type, pi->unique_id);
512         return find (favorites.begin(), favorites.end(), fp) !=  favorites.end();
513 }
514
515 void
516 PluginManager::save_favorites ()
517 {
518         ofstream ofs;
519         sys::path path = user_config_directory();
520         path /= "favorite_plugins";
521
522         ofs.open (path.to_string().c_str(), ios_base::openmode (ios::out|ios::trunc));
523
524         if (!ofs) {
525                 return;
526         }
527
528         for (FavoritePluginList::iterator i = favorites.begin(); i != favorites.end(); ++i) {
529                 switch ((*i).type) {
530                 case LADSPA:
531                         ofs << "LADSPA";
532                         break;
533                 case AudioUnit:
534                         ofs << "AudioUnit";
535                         break;
536                 case LV2:
537                         ofs << "LV2";
538                         break;
539                 case VST:
540                         ofs << "VST";
541                         break;
542                 }
543                 
544                 ofs << ' ' << (*i).unique_id << endl;
545         }
546
547         ofs.close ();
548 }
549
550 void
551 PluginManager::load_favorites ()
552 {
553         sys::path path = user_config_directory();
554         path /= "favorite_plugins";
555         ifstream ifs (path.to_string().c_str());
556
557         if (!ifs) {
558                 return;
559         }
560         
561         std::string stype;
562         std::string id;
563         PluginType type;
564
565         while (ifs) {
566
567                 ifs >> stype;
568                 if (!ifs) {
569                         break;
570
571                 }
572                 ifs >> id;
573                 if (!ifs) {
574                         break;
575                 }
576
577                 if (stype == "LADSPA") {
578                         type = LADSPA;
579                 } else if (stype == "AudioUnit") {
580                         type = AudioUnit;
581                 } else if (stype == "LV2") {
582                         type = LV2;
583                 } else if (stype == "VST") {
584                         type = VST;
585                 } else {
586                         error << string_compose (_("unknown favorite plugin type \"%1\" - ignored"), stype)
587                               << endmsg;
588                         continue;
589                 }
590                 
591                 add_favorite (type, id);
592         }
593         
594         ifs.close ();
595 }
596
597 void
598 PluginManager::add_favorite (PluginType t, string id)
599 {
600         FavoritePlugin fp (t, id);
601         pair<FavoritePluginList::iterator,bool> res = favorites.insert (fp);
602         cerr << "Added " << t << " " << id << " success ? " << res.second << endl;
603 }
604
605 void
606 PluginManager::remove_favorite (PluginType t, string id)
607 {
608         FavoritePlugin fp (t, id);
609         favorites.erase (fp);
610 }