add new sigc++2 directory
[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 #ifdef HAVE_AUDIOUNITS
44 #include <ardour/audio_unit.h>
45 #endif
46
47 #ifdef HAVE_SLV2
48 #include <ardour/lv2_plugin.h>
49 #endif
50
51 #include <pbd/stl_delete.h>
52
53 #include "i18n.h"
54 #include <locale.h>
55
56 using namespace ARDOUR;
57 using namespace PBD;
58
59 Plugin::Plugin (AudioEngine& e, Session& s)
60         : _engine (e), _session (s)
61 {
62 }
63
64 Plugin::Plugin (const Plugin& other)
65         : _engine (other._engine), _session (other._session), _info (other._info)
66 {
67 }
68
69 Plugin::~Plugin ()
70 {
71 }
72
73 vector<string>
74 Plugin::get_presets()
75 {
76         vector<string> labels;
77         uint32_t id;
78         std::string unique (unique_id());
79
80         /* XXX problem: AU plugins don't have numeric ID's. 
81            Solution: they have a different method of providing presets.
82            XXX sub-problem: implement it.
83         */
84
85         if (!isdigit (unique[0])) {
86                 return labels;
87         }
88
89         id = atol (unique.c_str());
90
91         lrdf_uris* set_uris = lrdf_get_setting_uris(id);
92
93         if (set_uris) {
94                 for (uint32_t i = 0; i < (uint32_t) set_uris->count; ++i) {
95                         if (char* label = lrdf_get_label(set_uris->items[i])) {
96                                 labels.push_back(label);
97                                 presets[label] = set_uris->items[i];
98                         }
99                 }
100                 lrdf_free_uris(set_uris);
101         }
102
103         // GTK2FIX find an equivalent way to do this with a vector (needed by GUI apis)
104         // labels.unique();
105
106         return labels;
107 }
108
109 bool
110 Plugin::load_preset(const string preset_label)
111 {
112         lrdf_defaults* defs = lrdf_get_setting_values(presets[preset_label].c_str());
113
114         if (defs) {
115                 for (uint32_t i = 0; i < (uint32_t) defs->count; ++i) {
116                         // The defs->items[i].pid < defs->count check is to work around 
117                         // a bug in liblrdf that saves invalid values into the presets file.
118                         if (((uint32_t) defs->items[i].pid < (uint32_t) defs->count) && parameter_is_input (defs->items[i].pid)) {
119                                 set_parameter(defs->items[i].pid, defs->items[i].value);
120                         }
121                 }
122                 lrdf_free_setting_values(defs);
123         }
124
125         return true;
126 }
127
128 bool
129 Plugin::save_preset (string name, string domain)
130 {
131         lrdf_portvalue portvalues[parameter_count()];
132         lrdf_defaults defaults;
133         uint32_t id;
134         std::string unique (unique_id());
135
136         /* XXX problem: AU plugins don't have numeric ID's. 
137            Solution: they have a different method of providing/saving presets.
138            XXX sub-problem: implement it.
139         */
140
141         if (!isdigit (unique[0])) {
142                 return false;
143         }
144
145         id = atol (unique.c_str());
146
147         defaults.count = parameter_count();
148         defaults.items = portvalues;
149
150         for (uint32_t i = 0; i < parameter_count(); ++i) {
151                 if (parameter_is_input (i)) {
152                         portvalues[i].pid = i;
153                         portvalues[i].value = get_parameter(i);
154                 }
155         }
156
157         char* envvar;
158         if ((envvar = getenv ("HOME")) == 0) {
159                 warning << _("Could not locate HOME.  Preset not saved.") << endmsg;
160                 return false;
161         }
162         
163         string source(string_compose("file:%1/.%2/rdf/ardour-presets.n3", envvar, domain));
164
165         free(lrdf_add_preset(source.c_str(), name.c_str(), id,  &defaults));
166
167         string path = string_compose("%1/.%2", envvar, domain);
168         if (g_mkdir_with_parents (path.c_str(), 0775)) {
169                 warning << string_compose(_("Could not create %1.  Preset not saved. (%2)"), path, strerror(errno)) << endmsg;
170                 return false;
171         }
172         
173         path += "/rdf";
174         if (g_mkdir_with_parents (path.c_str(), 0775)) {
175                 warning << string_compose(_("Could not create %1.  Preset not saved. (%2)"), path, strerror(errno)) << endmsg;
176                 return false;
177         }
178         
179         if (lrdf_export_by_source(source.c_str(), source.substr(5).c_str())) {
180                 warning << string_compose(_("Error saving presets file %1."), source) << endmsg;
181                 return false;
182         }
183
184         return true;
185 }
186
187 PluginPtr
188 ARDOUR::find_plugin(Session& session, string identifier, PluginType type)
189 {
190         PluginManager *mgr = PluginManager::the_manager();
191         PluginInfoList plugs;
192
193         switch (type) {
194         case ARDOUR::LADSPA:
195                 plugs = mgr->ladspa_plugin_info();
196                 break;
197         
198 #ifdef HAVE_SLV2
199         case ARDOUR::LV2:
200                 plugs = mgr->lv2_plugin_info();
201                 break;
202 #endif
203
204 #ifdef VST_SUPPORT
205         case ARDOUR::VST:
206                 plugs = mgr->vst_plugin_info();
207                 break;
208 #endif
209
210 #ifdef HAVE_AUDIOUNITS
211         case ARDOUR::AudioUnit:
212                 plugs = mgr->au_plugin_info();
213                 break;
214 #endif
215
216         default:
217                 return PluginPtr ((Plugin *) 0);
218         }
219
220         PluginInfoList::iterator i;
221
222         for (i = plugs.begin(); i != plugs.end(); ++i) {
223                 if (identifier == (*i)->unique_id){
224                         return (*i)->load (session);
225                 }
226         }
227
228 #ifdef VST_SUPPORT
229         /* hmm, we didn't find it. could be because in older versions of Ardour.
230            we used to store the name of a VST plugin, not its unique ID. so try
231            again.
232         */
233
234         for (i = plugs.begin(); i != plugs.end(); ++i) {
235                 if (identifier == (*i)->name){
236                         return (*i)->load (session);
237                 }
238         }
239 #endif
240         
241         return PluginPtr ((Plugin*) 0);
242 }
243