b9db471cede5e454a73b33c4d6014370c78ff02b
[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 #include <utility>
30
31 #include <lrdf.h>
32
33 #include "pbd/compose.h"
34 #include "pbd/error.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 #ifdef HAVE_AUDIOUNITS
45 #include "ardour/audio_unit.h"
46 #endif
47
48 #ifdef HAVE_SLV2
49 #include "ardour/lv2_plugin.h"
50 #endif
51
52 #include "pbd/stl_delete.h"
53
54 #include "i18n.h"
55 #include <locale.h>
56
57 using namespace ARDOUR;
58 using namespace PBD;
59
60 Plugin::Plugin (AudioEngine& e, Session& s)
61         : _engine (e)
62         , _session (s)
63         , _cycles (0)
64 {
65 }
66
67 Plugin::Plugin (const Plugin& other)
68         : _engine (other._engine)
69         , _session (other._session)
70         , _info (other._info)
71         , _cycles (0)
72         , presets (other.presets)
73 {
74 }
75
76 Plugin::~Plugin ()
77 {
78 }
79
80 const Plugin::PresetRecord*
81 Plugin::preset_by_label(const string& label)
82 {
83         // FIXME: O(n)
84         for (map<string,PresetRecord>::const_iterator i = presets.begin(); i != presets.end(); ++i) {
85                 if (i->second.label == label) {
86                         return &i->second;
87                 }
88         }
89         return NULL;
90 }
91
92 const Plugin::PresetRecord*
93 Plugin::preset_by_uri(const string& uri)
94 {
95         map<string,PresetRecord>::const_iterator pr = presets.find(uri);
96         if (pr != presets.end()) {
97                 return &pr->second;
98         } else {
99                 return NULL;
100         }
101 }
102
103 vector<Plugin::PresetRecord>
104 Plugin::get_presets()
105 {
106         vector<PresetRecord> result;
107         uint32_t id;
108         std::string unique (unique_id());
109
110         /* XXX problem: AU plugins don't have numeric ID's. 
111            Solution: they have a different method of providing presets.
112            XXX sub-problem: implement it.
113         */
114
115         if (!isdigit (unique[0])) {
116                 return result;
117         }
118
119         id = atol (unique.c_str());
120
121         lrdf_uris* set_uris = lrdf_get_setting_uris(id);
122
123         if (set_uris) {
124                 for (uint32_t i = 0; i < (uint32_t) set_uris->count; ++i) {
125                         if (char* label = lrdf_get_label(set_uris->items[i])) {
126                                 PresetRecord rec(set_uris->items[i], label);
127                                 result.push_back(rec);
128                                 presets.insert(std::make_pair(set_uris->items[i], rec));
129                         }
130                 }
131                 lrdf_free_uris(set_uris);
132         }
133
134         return result;
135 }
136
137 bool
138 Plugin::load_preset(const string preset_uri)
139 {
140         lrdf_defaults* defs = lrdf_get_setting_values(preset_uri.c_str());
141
142         if (defs) {
143                 for (uint32_t i = 0; i < (uint32_t) defs->count; ++i) {
144                         // The defs->items[i].pid < defs->count check is to work around 
145                         // a bug in liblrdf that saves invalid values into the presets file.
146                         if (((uint32_t) defs->items[i].pid < (uint32_t) defs->count) && parameter_is_input (defs->items[i].pid)) {
147                                 set_parameter(defs->items[i].pid, defs->items[i].value);
148                         }
149                 }
150                 lrdf_free_setting_values(defs);
151         }
152
153         return true;
154 }
155
156 bool
157 Plugin::save_preset (string uri, string domain)
158 {
159         lrdf_portvalue portvalues[parameter_count()];
160         lrdf_defaults defaults;
161         uint32_t id;
162         std::string unique (unique_id());
163
164         /* XXX problem: AU plugins don't have numeric ID's. 
165            Solution: they have a different method of providing/saving presets.
166            XXX sub-problem: implement it.
167         */
168
169         if (!isdigit (unique[0])) {
170                 return false;
171         }
172
173         id = atol (unique.c_str());
174
175         defaults.count = parameter_count();
176         defaults.items = portvalues;
177
178         for (uint32_t i = 0; i < parameter_count(); ++i) {
179                 if (parameter_is_input (i)) {
180                         portvalues[i].pid = i;
181                         portvalues[i].value = get_parameter(i);
182                 }
183         }
184
185         char* envvar;
186         if ((envvar = getenv ("HOME")) == 0) {
187                 warning << _("Could not locate HOME.  Preset not saved.") << endmsg;
188                 return false;
189         }
190         
191         string source(string_compose("file:%1/.%2/rdf/ardour-presets.n3", envvar, domain));
192
193         map<string,PresetRecord>::const_iterator pr = presets.find(uri);
194         if (pr == presets.end()) {
195                 warning << _("Could not find preset ") << uri << endmsg;
196                 return false;
197         }
198         free(lrdf_add_preset(source.c_str(), pr->second.label.c_str(), id,  &defaults));
199
200         string path = string_compose("%1/.%2", envvar, domain);
201         if (g_mkdir_with_parents (path.c_str(), 0775)) {
202                 warning << string_compose(_("Could not create %1.  Preset not saved. (%2)"), path, strerror(errno)) << endmsg;
203                 return false;
204         }
205         
206         path += "/rdf";
207         if (g_mkdir_with_parents (path.c_str(), 0775)) {
208                 warning << string_compose(_("Could not create %1.  Preset not saved. (%2)"), path, strerror(errno)) << endmsg;
209                 return false;
210         }
211         
212         if (lrdf_export_by_source(source.c_str(), source.substr(5).c_str())) {
213                 warning << string_compose(_("Error saving presets file %1."), source) << endmsg;
214                 return false;
215         }
216
217         return true;
218 }
219
220 PluginPtr
221 ARDOUR::find_plugin(Session& session, string identifier, PluginType type)
222 {
223         PluginManager *mgr = PluginManager::the_manager();
224         PluginInfoList plugs;
225
226         switch (type) {
227         case ARDOUR::LADSPA:
228                 plugs = mgr->ladspa_plugin_info();
229                 break;
230         
231 #ifdef HAVE_SLV2
232         case ARDOUR::LV2:
233                 plugs = mgr->lv2_plugin_info();
234                 break;
235 #endif
236
237 #ifdef VST_SUPPORT
238         case ARDOUR::VST:
239                 plugs = mgr->vst_plugin_info();
240                 break;
241 #endif
242
243 #ifdef HAVE_AUDIOUNITS
244         case ARDOUR::AudioUnit:
245                 plugs = mgr->au_plugin_info();
246                 break;
247 #endif
248
249         default:
250                 return PluginPtr ((Plugin *) 0);
251         }
252
253         PluginInfoList::iterator i;
254
255         for (i = plugs.begin(); i != plugs.end(); ++i) {
256                 if (identifier == (*i)->unique_id){
257                         return (*i)->load (session);
258                 }
259         }
260
261 #ifdef VST_SUPPORT
262         /* hmm, we didn't find it. could be because in older versions of Ardour.
263            we used to store the name of a VST plugin, not its unique ID. so try
264            again.
265         */
266
267         for (i = plugs.begin(); i != plugs.end(); ++i) {
268                 if (identifier == (*i)->name){
269                         return (*i)->load (session);
270                 }
271         }
272 #endif
273         
274         return PluginPtr ((Plugin*) 0);
275 }
276
277 ChanCount
278 Plugin::output_streams () const
279 {
280         /* LADSPA & VST should not get here because they do not
281            return "infinite" i/o counts.
282         */
283         return ChanCount::ZERO;
284 }
285
286 ChanCount
287 Plugin::input_streams () const
288 {
289         /* LADSPA & VST should not get here because they do not
290            return "infinite" i/o counts.
291         */
292         return ChanCount::ZERO;
293 }
294
295