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