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