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