b1cb97a475c2f2dc1bc3313db6b3b73f89d10669
[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/xml++.h>
35
36 #include <ardour/ardour.h>
37 #include <ardour/session.h>
38 #include <ardour/audioengine.h>
39 #include <ardour/plugin.h>
40 #include <ardour/ladspa_plugin.h>
41 #include <ardour/plugin_manager.h>
42
43 #include <pbd/stl_delete.h>
44
45 #include "i18n.h"
46 #include <locale.h>
47
48 using namespace ARDOUR;
49 using namespace PBD;
50
51 Plugin::Plugin (AudioEngine& e, Session& s)
52         : _engine (e), _session (s)
53 {
54 }
55
56 Plugin::Plugin (const Plugin& other)
57         : _engine (other._engine), _session (other._session), _info (other._info)
58 {
59 }
60
61 void
62 Plugin::setup_controls ()
63 {
64         uint32_t port_cnt = parameter_count();
65
66         /* set up a vector of null pointers for the controls.
67            we'll fill this in on an as-needed basis.
68         */
69
70         for (uint32_t i = 0; i < port_cnt; ++i) {
71                 controls.push_back (0);
72         }
73 }
74
75 Plugin::~Plugin ()
76 {
77         for (vector<PortControllable*>::iterator i = controls.begin(); i != controls.end(); ++i) {
78                 if (*i) {
79                         delete *i;
80                 }
81         }
82 }
83
84 Controllable *
85 Plugin::get_nth_control (uint32_t n)
86 {
87         if (n >= parameter_count()) {
88                 return 0;
89         }
90
91         if (controls[n] == 0) {
92
93                 Plugin::ParameterDescriptor desc;
94
95                 get_parameter_descriptor (n, desc);
96         
97                 controls[n] = new PortControllable (describe_parameter (n), *this, n, 
98                                                     desc.lower, desc.upper, desc.toggled, desc.logarithmic);
99         } 
100
101         return controls[n];
102 }
103
104 Plugin::PortControllable::PortControllable (string name, Plugin& p, uint32_t port_id, 
105                                             float low, float up, bool t, bool loga)
106         : Controllable (name), plugin (p), absolute_port (port_id)
107 {
108         toggled = t;
109         logarithmic = loga;
110         lower = low;
111         upper = up;
112         range = upper - lower;
113 }
114
115 void
116 Plugin::PortControllable::set_value (float value)
117 {
118         if (toggled) {
119                 if (value > 0.5) {
120                         value = 1.0;
121                 } else {
122                         value = 0.0;
123                 }
124         } else {
125
126                 if (!logarithmic) {
127                         value = lower + (range * value);
128                 } else {
129                         float _lower = 0.0f;
130                         if (lower > 0.0f) {
131                                 _lower = log(lower);
132                         }
133
134                         value = exp(_lower + log(range) * value);
135                 }
136         }
137
138         plugin.set_parameter (absolute_port, value);
139 }
140
141 float
142 Plugin::PortControllable::get_value (void) const
143 {
144         float val = plugin.get_parameter (absolute_port);
145
146         if (toggled) {
147                 
148                 return val;
149                 
150         } else {
151                 
152                 if (logarithmic) {
153                         val = log(val);
154                 }
155                 
156                 return ((val - lower) / range);
157         }
158 }       
159
160 vector<string>
161 Plugin::get_presets()
162 {
163         vector<string> labels;
164         lrdf_uris* set_uris = lrdf_get_setting_uris(unique_id());
165
166         if (set_uris) {
167                 for (uint32_t i = 0; i < (uint32_t) set_uris->count; ++i) {
168                         if (char* label = lrdf_get_label(set_uris->items[i])) {
169                                 labels.push_back(label);
170                                 presets[label] = set_uris->items[i];
171                         }
172                 }
173                 lrdf_free_uris(set_uris);
174         }
175
176         // GTK2FIX find an equivalent way to do this with a vector (needed by GUI apis)
177         // labels.unique();
178
179         return labels;
180 }
181
182 bool
183 Plugin::load_preset(const string preset_label)
184 {
185         lrdf_defaults* defs = lrdf_get_setting_values(presets[preset_label].c_str());
186
187         if (defs) {
188                 for (uint32_t i = 0; i < (uint32_t) defs->count; ++i) {
189                         // The defs->items[i].pid < defs->count check is to work around 
190                         // a bug in liblrdf that saves invalid values into the presets file.
191                         if (((uint32_t) defs->items[i].pid < (uint32_t) defs->count) && parameter_is_input (defs->items[i].pid)) {
192                                 set_parameter(defs->items[i].pid, defs->items[i].value);
193                         }
194                 }
195                 lrdf_free_setting_values(defs);
196         }
197
198         return true;
199 }
200
201 bool
202 Plugin::save_preset (string name, string domain)
203 {
204         lrdf_portvalue portvalues[parameter_count()];
205         lrdf_defaults defaults;
206         defaults.count = parameter_count();
207         defaults.items = portvalues;
208
209         for (uint32_t i = 0; i < parameter_count(); ++i) {
210                 if (parameter_is_input (i)) {
211                         portvalues[i].pid = i;
212                         portvalues[i].value = get_parameter(i);
213                 }
214         }
215
216         char* envvar;
217         if ((envvar = getenv ("HOME")) == 0) {
218                 warning << _("Could not locate HOME.  Preset not saved.") << endmsg;
219                 return false;
220         }
221         
222         string source(string_compose("file:%1/.%2/rdf/ardour-presets.n3", envvar, domain));
223
224         free(lrdf_add_preset(source.c_str(), name.c_str(), unique_id(), &defaults));
225
226         string path = string_compose("%1/.%2", envvar, domain);
227         if (g_mkdir_with_parents (path.c_str(), 0775)) {
228                 warning << string_compose(_("Could not create %1.  Preset not saved. (%2)"), path, strerror(errno)) << endmsg;
229                 return false;
230         }
231         
232         path += "/rdf";
233         if (g_mkdir_with_parents (path.c_str(), 0775)) {
234                 warning << string_compose(_("Could not create %1.  Preset not saved. (%2)"), path, strerror(errno)) << endmsg;
235                 return false;
236         }
237         
238         if (lrdf_export_by_source(source.c_str(), source.substr(5).c_str())) {
239                 warning << string_compose(_("Error saving presets file %1."), source) << endmsg;
240                 return false;
241         }
242
243         return true;
244 }
245
246 PluginPtr
247 ARDOUR::find_plugin(Session& session, string name, long unique_id, PluginType type)
248 {
249         PluginManager *mgr = PluginManager::the_manager();
250         PluginInfoList plugs;
251
252         switch (type) {
253         case ARDOUR::LADSPA:
254                 plugs = mgr->ladspa_plugin_info();
255                 break;
256
257 #ifdef VST_SUPPORT
258         case ARDOUR::VST:
259                 plugs = mgr->vst_plugin_info();
260                 unique_id = 0; // VST plugins don't have a unique id.
261                 break;
262 #endif
263
264 #ifdef HAVE_AUDIOUNITS
265         case ARDOUR::AudioUnit:
266                 plugs = AUPluginInfo::discover ();
267                 unique_id = 0; // Neither do AU.
268                 break;
269 #endif
270
271         default:
272                 return PluginPtr ((Plugin *) 0);
273         }
274
275         PluginInfoList::iterator i;
276         for (i = plugs.begin(); i != plugs.end(); ++i) {
277                 if ((name == "" || (*i)->name == name) &&
278                         (unique_id == 0 || (*i)->unique_id == unique_id)) {
279                                 return (*i)->load (session);
280                 }
281         }
282         
283         return PluginPtr ((Plugin*) 0);
284 }
285