Deep "automation regions" support.
[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
28 #ifdef VST_SUPPORT
29 #include <fst.h>
30 #include <pbd/basename.h>
31 #include <string.h>
32 #endif // VST_SUPPORT
33
34 #include <pbd/pathscanner.h>
35
36 #include <ardour/ladspa.h>
37 #include <ardour/session.h>
38 #include <ardour/plugin_manager.h>
39 #include <ardour/plugin.h>
40 #include <ardour/ladspa_plugin.h>
41
42 #ifdef VST_SUPPORT
43 #include <ardour/vst_plugin.h>
44 #endif
45
46 #include <pbd/error.h>
47 #include <pbd/stl_delete.h>
48
49 #include "i18n.h"
50
51 using namespace ARDOUR;
52 using namespace PBD;
53
54 PluginManager* PluginManager::_manager = 0;
55
56 PluginManager::PluginManager ()
57 {
58         char* s;
59         string lrdf_path;
60
61         if ((s = getenv ("LADSPA_RDF_PATH"))){
62                 lrdf_path = s;
63         }
64
65         if (lrdf_path.length() == 0) {
66                 lrdf_path = "/usr/local/share/ladspa/rdf:/usr/share/ladspa/rdf";
67         }
68
69         add_lrdf_data(lrdf_path);
70         add_ladspa_presets();
71 #ifdef VST_SUPPORT
72         if (Config->get_use_vst()) {
73                 add_vst_presets();
74         }
75 #endif /* VST_SUPPORT */
76
77         if ((s = getenv ("LADSPA_PATH"))) {
78                 ladspa_path = s;
79         }
80
81         if ((s = getenv ("VST_PATH"))) {
82                 vst_path = s;
83         } else if ((s = getenv ("VST_PLUGINS"))) {
84                 vst_path = s;
85         }
86
87         refresh ();
88         if (_manager == 0) {
89                 _manager = this;
90         }
91 }
92
93 void
94 PluginManager::refresh ()
95 {
96         ladspa_refresh ();
97 #ifdef VST_SUPPORT
98         if (Config->get_use_vst()) {
99                 vst_refresh ();
100         }
101 #endif // VST_SUPPORT
102 }
103
104 void
105 PluginManager::ladspa_refresh ()
106 {
107         _ladspa_plugin_info.clear ();
108
109         if (ladspa_path.length() == 0) {
110                 ladspa_path = "/usr/local/lib64/ladspa:/usr/local/lib/ladspa:/usr/lib64/ladspa:/usr/lib/ladspa:/Library/Audio/Plug-Ins/LADSPA";
111         }
112
113         ladspa_discover_from_path (ladspa_path);
114 }
115
116 int
117 PluginManager::add_ladspa_directory (string path)
118 {
119         if (ladspa_discover_from_path (path) == 0) {
120                 ladspa_path += ':';
121                 ladspa_path += path;
122                 return 0;
123         } 
124         return -1;
125 }
126
127 static bool ladspa_filter (const string& str, void *arg)
128 {
129         /* Not a dotfile, has a prefix before a period, suffix is "so" */
130         
131         return str[0] != '.' && (str.length() > 3 && str.find (".so") == (str.length() - 3));
132 }
133
134 int
135 PluginManager::ladspa_discover_from_path (string path)
136 {
137         PathScanner scanner;
138         vector<string *> *plugin_objects;
139         vector<string *>::iterator x;
140         int ret = 0;
141
142         plugin_objects = scanner (ladspa_path, ladspa_filter, 0, true, true);
143
144         if (plugin_objects) {
145                 for (x = plugin_objects->begin(); x != plugin_objects->end (); ++x) {
146                         ladspa_discover (**x);
147                 }
148         }
149
150         vector_delete (plugin_objects);
151         return ret;
152 }
153
154 static bool rdf_filter (const string &str, void *arg)
155 {
156         return str[0] != '.' && 
157                    ((str.find(".rdf")  == (str.length() - 4)) ||
158             (str.find(".rdfs") == (str.length() - 5)) ||
159                     (str.find(".n3")   == (str.length() - 3)));
160 }
161
162 void
163 PluginManager::add_ladspa_presets()
164 {
165         add_presets ("ladspa");
166 }
167
168 void
169 PluginManager::add_vst_presets()
170 {
171         add_presets ("vst");
172 }
173 void
174 PluginManager::add_presets(string domain)
175 {
176
177         PathScanner scanner;
178         vector<string *> *presets;
179         vector<string *>::iterator x;
180
181         char* envvar;
182         if ((envvar = getenv ("HOME")) == 0) {
183                 return;
184         }
185
186         string path = string_compose("%1/.%2/rdf", envvar, domain);
187         presets = scanner (path, rdf_filter, 0, true, true);
188
189         if (presets) {
190                 for (x = presets->begin(); x != presets->end (); ++x) {
191                         string file = "file:" + **x;
192                         if (lrdf_read_file(file.c_str())) {
193                                 warning << string_compose(_("Could not parse rdf file: %1"), *x) << endmsg;
194                         }
195                 }
196         }
197
198         vector_delete (presets);
199 }
200
201 void
202 PluginManager::add_lrdf_data (const string &path)
203 {
204         PathScanner scanner;
205         vector<string *>* rdf_files;
206         vector<string *>::iterator x;
207         string uri;
208
209         rdf_files = scanner (path, rdf_filter, 0, true, true);
210
211         if (rdf_files) {
212                 for (x = rdf_files->begin(); x != rdf_files->end (); ++x) {
213                         uri = "file://" + **x;
214
215                         if (lrdf_read_file(uri.c_str())) {
216                                 warning << "Could not parse rdf file: " << uri << endmsg;
217                         }
218                 }
219         }
220
221         vector_delete (rdf_files);
222 }
223
224 int 
225 PluginManager::ladspa_discover (string path)
226 {
227         void *module;
228         const LADSPA_Descriptor *descriptor;
229         LADSPA_Descriptor_Function dfunc;
230         const char *errstr;
231
232         if ((module = dlopen (path.c_str(), RTLD_NOW)) == 0) {
233                 error << string_compose(_("LADSPA: cannot load module \"%1\" (%2)"), path, dlerror()) << endmsg;
234                 return -1;
235         }
236
237         dfunc = (LADSPA_Descriptor_Function) dlsym (module, "ladspa_descriptor");
238
239         if ((errstr = dlerror()) != 0) {
240                 error << string_compose(_("LADSPA: module \"%1\" has no descriptor function."), path) << endmsg;
241                 error << errstr << endmsg;
242                 dlclose (module);
243                 return -1;
244         }
245
246         for (uint32_t i = 0; ; ++i) {
247                 if ((descriptor = dfunc (i)) == 0) {
248                         break;
249                 }
250
251                 PluginInfoPtr info(new LadspaPluginInfo);
252                 info->name = descriptor->Name;
253                 info->category = get_ladspa_category(descriptor->UniqueID);
254                 info->creator = descriptor->Maker;
255                 info->path = path;
256                 info->index = i;
257                 info->n_inputs = ChanCount();
258                 info->n_outputs = ChanCount();
259                 info->type = ARDOUR::LADSPA;
260                 info->unique_id = descriptor->UniqueID;
261                 
262                 for (uint32_t n=0; n < descriptor->PortCount; ++n) {
263                         if ( LADSPA_IS_PORT_AUDIO (descriptor->PortDescriptors[n]) ) {
264                                 if ( LADSPA_IS_PORT_INPUT (descriptor->PortDescriptors[n]) ) {
265                                         info->n_inputs.set_audio(info->n_inputs.n_audio() + 1);
266                                 }
267                                 else if ( LADSPA_IS_PORT_OUTPUT (descriptor->PortDescriptors[n]) ) {
268                                         info->n_outputs.set_audio(info->n_outputs.n_audio() + 1);
269                                 }
270                         }
271                 }
272
273                 _ladspa_plugin_info.push_back (info);
274         }
275
276 // GDB WILL NOT LIKE YOU IF YOU DO THIS
277 //      dlclose (module);
278
279         return 0;
280 }
281
282 string
283 PluginManager::get_ladspa_category (uint32_t plugin_id)
284 {
285         char buf[256];
286         lrdf_statement pattern;
287
288         snprintf(buf, sizeof(buf), "%s%" PRIu32, LADSPA_BASE, plugin_id);
289         pattern.subject = buf;
290         pattern.predicate = (char*)RDF_TYPE;
291         pattern.object = 0;
292         pattern.object_type = lrdf_uri;
293
294         lrdf_statement* matches1 = lrdf_matches (&pattern);
295
296         if (!matches1) {
297                 return _("Unknown");
298         }
299
300         pattern.subject = matches1->object;
301         pattern.predicate = (char*)LADSPA_BASE "hasLabel";
302         pattern.object = 0;
303         pattern.object_type = lrdf_literal;
304
305         lrdf_statement* matches2 = lrdf_matches (&pattern);
306         lrdf_free_statements(matches1);
307
308         if (!matches2) {
309                 return _("Unknown");
310         }
311
312         string label = matches2->object;
313         lrdf_free_statements(matches2);
314
315         return label;
316 }
317
318 #ifdef VST_SUPPORT
319
320 void
321 PluginManager::vst_refresh ()
322 {
323         _vst_plugin_info.clear ();
324
325         if (vst_path.length() == 0) {
326                 vst_path = "/usr/local/lib/vst:/usr/lib/vst";
327         }
328
329         vst_discover_from_path (vst_path);
330 }
331
332 int
333 PluginManager::add_vst_directory (string path)
334 {
335         if (vst_discover_from_path (path) == 0) {
336                 vst_path += ':';
337                 vst_path += path;
338                 return 0;
339         } 
340         return -1;
341 }
342
343 static bool vst_filter (const string& str, void *arg)
344 {
345         /* Not a dotfile, has a prefix before a period, suffix is "dll" */
346
347         return str[0] != '.' && (str.length() > 4 && str.find (".dll") == (str.length() - 4));
348 }
349
350 int
351 PluginManager::vst_discover_from_path (string path)
352 {
353         PathScanner scanner;
354         vector<string *> *plugin_objects;
355         vector<string *>::iterator x;
356         int ret = 0;
357
358         info << "detecting VST plugins along " << path << endmsg;
359
360         plugin_objects = scanner (vst_path, vst_filter, 0, true, true);
361
362         if (plugin_objects) {
363                 for (x = plugin_objects->begin(); x != plugin_objects->end (); ++x) {
364                         vst_discover (**x);
365                 }
366         }
367
368         vector_delete (plugin_objects);
369         return ret;
370 }
371
372 int
373 PluginManager::vst_discover (string path)
374 {
375         FSTInfo* finfo;
376
377         if ((finfo = fst_get_info (const_cast<char *> (path.c_str()))) == 0) {
378                 warning << "Cannot get VST information from " << path << endmsg;
379                 return -1;
380         }
381
382         if (!finfo->canProcessReplacing) {
383                 warning << string_compose (_("VST plugin %1 does not support processReplacing, and so cannot be used in ardour at this time"),
384                                     finfo->name)
385                         << endl;
386         }
387         
388         PluginInfoPtr info(new VSTPluginInfo);
389
390         /* what a goddam joke freeware VST is */
391
392         if (!strcasecmp ("The Unnamed plugin", finfo->name)) {
393                 info->name = PBD::basename_nosuffix (path);
394         } else {
395                 info->name = finfo->name;
396         }
397
398         info->category = "VST";
399         info->path = path;
400         // need to set info->creator but FST doesn't provide it
401         info->index = 0;
402         info->n_inputs = finfo->numInputs;
403         info->n_outputs = finfo->numOutputs;
404         info->type = ARDOUR::VST;
405         
406         _vst_plugin_info.push_back (info);
407         fst_free_info (finfo);
408
409         return 0;
410 }
411
412 #endif // VST_SUPPORT