Remove beat entry from meter dialog (beats are not allowed in API), clean up some...
[ardour.git] / libs / ardour / plugin.cc
1 /*
2     Copyright (C) 2000-2002 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 <vector>
21 #include <string>
22
23 #include <cstdlib>
24 #include <cstdio> // so libraptor doesn't complain
25 #include <cmath>
26 #include <dirent.h>
27 #include <sys/stat.h>
28 #include <cerrno>
29
30 #include <lrdf.h>
31
32 #include <pbd/compose.h>
33 #include <pbd/error.h>
34 #include <pbd/pathscanner.h>
35 #include <pbd/xml++.h>
36 #include <pbd/stacktrace.h>
37
38 #include <ardour/ardour.h>
39 #include <ardour/session.h>
40 #include <ardour/audioengine.h>
41 #include <ardour/plugin.h>
42 #include <ardour/ladspa_plugin.h>
43 #include <ardour/plugin_manager.h>
44
45 #ifdef HAVE_AUDIOUNITS
46 #include <ardour/audio_unit.h>
47 #endif
48
49 #ifdef HAVE_SLV2
50 #include <ardour/lv2_plugin.h>
51 #endif
52
53 #include <pbd/stl_delete.h>
54
55 #include "i18n.h"
56 #include <locale.h>
57
58 using namespace ARDOUR;
59 using namespace PBD;
60
61 Plugin::Plugin (AudioEngine& e, Session& s)
62         : _engine (e), _session (s)
63 {
64 }
65
66 Plugin::Plugin (const Plugin& other)
67         : _engine (other._engine), _session (other._session), _info (other._info)
68 {
69 }
70
71 void
72 Plugin::setup_controls ()
73 {
74         uint32_t port_cnt = parameter_count();
75
76         /* set up a vector of null pointers for the controls.
77            we'll fill this in on an as-needed basis.
78         */
79
80         for (uint32_t i = 0; i < port_cnt; ++i) {
81                 controls.push_back (0);
82         }
83 }
84
85 Plugin::~Plugin ()
86 {
87         for (vector<PortControllable*>::iterator i = controls.begin(); i != controls.end(); ++i) {
88                 if (*i) {
89                         delete *i;
90                 }
91         }
92 }
93
94 void
95 Plugin::make_nth_control (uint32_t n, const XMLNode& node)
96 {
97         if (controls[n]) {
98                 /* already constructed */
99                 return;
100         }
101
102         Plugin::ParameterDescriptor desc;
103         
104         get_parameter_descriptor (n, desc);
105         
106         controls[n] = new PortControllable (node, *this, n, 
107                                             desc.lower, desc.upper, desc.toggled, desc.logarithmic);
108 }
109
110 Controllable *
111 Plugin::get_nth_control (uint32_t n, bool do_not_create)
112 {
113         if (n >= parameter_count()) {
114                 return 0;
115         }
116
117         if (controls[n] == 0 && !do_not_create) {
118
119                 Plugin::ParameterDescriptor desc;
120                 
121                 get_parameter_descriptor (n, desc);
122
123                 controls[n] = new PortControllable (describe_parameter (n), *this, n, 
124                                                     desc.lower, desc.upper, desc.toggled, desc.logarithmic);
125         } 
126
127         return controls[n];
128 }
129
130 Plugin::PortControllable::PortControllable (string name, Plugin& p, uint32_t port_id, 
131                                             float low, float up, bool t, bool loga)
132         : Controllable (name), plugin (p), absolute_port (port_id)
133 {
134         toggled = t;
135         logarithmic = loga;
136         lower = low;
137         upper = up;
138         range = upper - lower;
139 }
140
141 Plugin::PortControllable::PortControllable (const XMLNode& node, Plugin& p, uint32_t port_id, 
142                                             float low, float up, bool t, bool loga)
143         : Controllable (node), plugin (p), absolute_port (port_id)
144 {
145         toggled = t;
146         logarithmic = loga;
147         lower = low;
148         upper = up;
149         range = upper - lower;
150 }
151
152 void
153 Plugin::PortControllable::set_value (float value)
154 {
155         if (toggled) {
156                 if (value > 0.5) {
157                         value = 1.0;
158                 } else {
159                         value = 0.0;
160                 }
161         } else {
162
163                 if (!logarithmic) {
164                         value = lower + (range * value);
165                 } else {
166                         float _lower = 0.0f;
167                         if (lower > 0.0f) {
168                                 _lower = log(lower);
169                         }
170
171                         value = exp(_lower + log(range) * value);
172                 }
173         }
174
175         plugin.set_parameter (absolute_port, value);
176 }
177
178 float
179 Plugin::PortControllable::get_value (void) const
180 {
181         float val = plugin.get_parameter (absolute_port);
182
183         if (toggled) {
184                 
185                 return val;
186                 
187         } else {
188                 
189                 if (logarithmic) {
190                         val = log(val);
191                 }
192                 
193                 return ((val - lower) / range);
194         }
195 }       
196
197 vector<string>
198 Plugin::get_presets()
199 {
200         vector<string> labels;
201         uint32_t id;
202         std::string unique (unique_id());
203
204         /* XXX problem: AU plugins don't have numeric ID's. 
205            Solution: they have a different method of providing presets.
206            XXX sub-problem: implement it.
207         */
208
209         if (!isdigit (unique[0])) {
210                 return labels;
211         }
212
213         id = atol (unique.c_str());
214
215         lrdf_uris* set_uris = lrdf_get_setting_uris(id);
216
217         if (set_uris) {
218                 for (uint32_t i = 0; i < (uint32_t) set_uris->count; ++i) {
219                         if (char* label = lrdf_get_label(set_uris->items[i])) {
220                                 labels.push_back(label);
221                                 presets[label] = set_uris->items[i];
222                         }
223                 }
224                 lrdf_free_uris(set_uris);
225         }
226
227         // GTK2FIX find an equivalent way to do this with a vector (needed by GUI apis)
228         // labels.unique();
229
230         return labels;
231 }
232
233 bool
234 Plugin::load_preset(const string preset_label)
235 {
236         lrdf_defaults* defs = lrdf_get_setting_values(presets[preset_label].c_str());
237
238         if (defs) {
239                 for (uint32_t i = 0; i < (uint32_t) defs->count; ++i) {
240                         // The defs->items[i].pid < defs->count check is to work around 
241                         // a bug in liblrdf that saves invalid values into the presets file.
242                         if (((uint32_t) defs->items[i].pid < (uint32_t) defs->count) && parameter_is_input (defs->items[i].pid)) {
243                                 set_parameter(defs->items[i].pid, defs->items[i].value);
244                         }
245                 }
246                 lrdf_free_setting_values(defs);
247         }
248
249         return true;
250 }
251
252 bool
253 Plugin::save_preset (string name, string domain)
254 {
255         lrdf_portvalue portvalues[parameter_count()];
256         lrdf_defaults defaults;
257         uint32_t id;
258         std::string unique (unique_id());
259
260         /* XXX problem: AU plugins don't have numeric ID's. 
261            Solution: they have a different method of providing/saving presets.
262            XXX sub-problem: implement it.
263         */
264
265         if (!isdigit (unique[0])) {
266                 return false;
267         }
268
269         id = atol (unique.c_str());
270
271         defaults.count = parameter_count();
272         defaults.items = portvalues;
273
274         for (uint32_t i = 0; i < parameter_count(); ++i) {
275                 if (parameter_is_input (i)) {
276                         portvalues[i].pid = i;
277                         portvalues[i].value = get_parameter(i);
278                 }
279         }
280
281         char* envvar;
282         if ((envvar = getenv ("HOME")) == 0) {
283                 warning << _("Could not locate HOME.  Preset not saved.") << endmsg;
284                 return false;
285         }
286         
287         string source(string_compose("file:%1/.%2/rdf/ardour-presets.n3", envvar, domain));
288
289         free(lrdf_add_preset(source.c_str(), name.c_str(), id,  &defaults));
290
291         string path = string_compose("%1/.%2", envvar, domain);
292         if (g_mkdir_with_parents (path.c_str(), 0775)) {
293                 warning << string_compose(_("Could not create %1.  Preset not saved. (%2)"), path, strerror(errno)) << endmsg;
294                 return false;
295         }
296         
297         path += "/rdf";
298         if (g_mkdir_with_parents (path.c_str(), 0775)) {
299                 warning << string_compose(_("Could not create %1.  Preset not saved. (%2)"), path, strerror(errno)) << endmsg;
300                 return false;
301         }
302         
303         if (lrdf_export_by_source(source.c_str(), source.substr(5).c_str())) {
304                 warning << string_compose(_("Error saving presets file %1."), source) << endmsg;
305                 return false;
306         }
307
308         return true;
309 }
310
311 PluginPtr
312 ARDOUR::find_plugin(Session& session, string identifier, PluginType type)
313 {
314         PluginManager *mgr = PluginManager::the_manager();
315         PluginInfoList plugs;
316
317         switch (type) {
318         case ARDOUR::LADSPA:
319                 plugs = mgr->ladspa_plugin_info();
320                 break;
321         
322 #ifdef HAVE_SLV2
323         case ARDOUR::LV2:
324                 plugs = mgr->lv2_plugin_info();
325                 break;
326 #endif
327
328 #ifdef VST_SUPPORT
329         case ARDOUR::VST:
330                 plugs = mgr->vst_plugin_info();
331                 break;
332 #endif
333
334 #ifdef HAVE_AUDIOUNITS
335         case ARDOUR::AudioUnit:
336                 plugs = mgr->au_plugin_info();
337                 break;
338 #endif
339
340         default:
341                 return PluginPtr ((Plugin *) 0);
342         }
343
344         PluginInfoList::iterator i;
345
346         for (i = plugs.begin(); i != plugs.end(); ++i) {
347                 if (identifier == (*i)->unique_id){
348                         return (*i)->load (session);
349                 }
350         }
351
352 #ifdef VST_SUPPORT
353         /* hmm, we didn't find it. could be because in older versions of Ardour.
354            we used to store the name of a VST plugin, not its unique ID. so try
355            again.
356         */
357
358         for (i = plugs.begin(); i != plugs.end(); ++i) {
359                 if (identifier == (*i)->name){
360                         return (*i)->load (session);
361                 }
362         }
363 #endif
364         
365         return PluginPtr ((Plugin*) 0);
366 }
367 int32_t
368 Plugin::can_support_input_configuration (int32_t in)
369 {
370         /* LADSPA & VST should not get here because they do not
371            return negative i/o counts.
372         */
373         return -1;
374 }
375
376 int32_t
377 Plugin::compute_output_streams (int32_t nplugins)
378 {
379         /* LADSPA & VST should not get here because they do not
380            return negative i/o counts.
381         */
382         return -1;
383 }
384
385 uint32_t
386 Plugin::output_streams () const
387 {
388         /* LADSPA & VST should not get here because they do not
389            return negative i/o counts.
390         */
391         return 0;
392 }
393
394 uint32_t
395 Plugin::input_streams () const
396 {
397         /* LADSPA & VST should not get here because they do not
398            return negative i/o counts.
399         */
400         return 0;
401 }
402
403