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