2 Copyright (C) 2000-2002 Paul Davis
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.
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.
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.
21 #include "libardour-config.h"
28 #include <cstdio> // so libraptor doesn't complain
37 #include "pbd/compose.h"
38 #include "pbd/error.h"
39 #include "pbd/xml++.h"
41 #include "ardour/ardour.h"
42 #include "ardour/session.h"
43 #include "ardour/audioengine.h"
44 #include "ardour/plugin.h"
45 #include "ardour/ladspa_plugin.h"
46 #include "ardour/plugin_manager.h"
48 #ifdef HAVE_AUDIOUNITS
49 #include "ardour/audio_unit.h"
53 #include "ardour/lv2_plugin.h"
56 #include "pbd/stl_delete.h"
62 using namespace ARDOUR;
65 PBD::Signal0<bool> Plugin::PresetFileExists;
67 Plugin::Plugin (AudioEngine& e, Session& s)
74 Plugin::Plugin (const Plugin& other)
75 : StatefulDestructible()
77 , _engine (other._engine)
78 , _session (other._session)
81 , presets (other.presets)
89 const Plugin::PresetRecord*
90 Plugin::preset_by_label(const string& label)
93 for (map<string,PresetRecord>::const_iterator i = presets.begin(); i != presets.end(); ++i) {
94 if (i->second.label == label) {
101 const Plugin::PresetRecord*
102 Plugin::preset_by_uri(const string& uri)
104 map<string,PresetRecord>::const_iterator pr = presets.find(uri);
105 if (pr != presets.end()) {
112 vector<Plugin::PresetRecord>
113 Plugin::get_presets()
115 vector<PresetRecord> result;
117 std::string unique (unique_id());
119 /* XXX problem: AU plugins don't have numeric ID's.
120 Solution: they have a different method of providing presets.
121 XXX sub-problem: implement it.
124 if (!isdigit (unique[0])) {
128 id = atol (unique.c_str());
130 lrdf_uris* set_uris = lrdf_get_setting_uris(id);
133 for (uint32_t i = 0; i < (uint32_t) set_uris->count; ++i) {
134 if (char* label = lrdf_get_label(set_uris->items[i])) {
135 PresetRecord rec(set_uris->items[i], label);
136 result.push_back(rec);
137 presets.insert(std::make_pair(set_uris->items[i], rec));
140 lrdf_free_uris(set_uris);
147 Plugin::load_preset(const string& preset_uri)
149 lrdf_defaults* defs = lrdf_get_setting_values(preset_uri.c_str());
152 for (uint32_t i = 0; i < (uint32_t) defs->count; ++i) {
153 // The defs->items[i].pid < defs->count check is to work around
154 // a bug in liblrdf that saves invalid values into the presets file.
155 if (((uint32_t) defs->items[i].pid < (uint32_t) defs->count) && parameter_is_input (defs->items[i].pid)) {
156 set_parameter(defs->items[i].pid, defs->items[i].value);
159 lrdf_free_setting_values(defs);
165 /* XXX: should be in liblrdf */
167 lrdf_remove_preset (const char *source, const char *setting_uri)
172 char setting_uri_copy[64];
175 strncpy(setting_uri_copy, setting_uri, sizeof(setting_uri_copy));
177 p.subject = setting_uri_copy;
178 strncpy(buf, LADSPA_BASE "hasPortValue", sizeof(buf));
181 q = lrdf_matches(&p);
185 for (i = q; i; i = i->next) {
186 p.subject = i->object;
187 lrdf_remove_matches(&p);
190 lrdf_free_statements(q);
193 strncpy(buf, LADSPA_BASE "hasSetting", sizeof(buf));
195 p.object = setting_uri_copy;
196 lrdf_remove_matches(&p);
198 p.subject = setting_uri_copy;
201 lrdf_remove_matches (&p);
205 Plugin::remove_preset (string name, string domain)
207 string const envvar = preset_envvar ();
208 if (envvar.empty()) {
209 warning << _("Could not locate HOME. Preset not removed.") << endmsg;
213 Plugin::PresetRecord const * p = preset_by_label (name);
218 string const source = preset_source (envvar, domain);
219 lrdf_remove_preset (source.c_str(), p->uri.c_str ());
221 presets.erase (p->uri);
223 write_preset_file (envvar, domain);
225 PresetRemoved (); /* EMIT SIGNAL */
229 Plugin::preset_envvar () const
232 if ((envvar = getenv ("HOME")) == 0) {
240 Plugin::preset_source (string envvar, string domain) const
242 return string_compose ("file:%1/.%2/rdf/ardour-presets.n3", envvar, domain);
246 Plugin::write_preset_file (string envvar, string domain)
248 string path = string_compose("%1/.%2", envvar, domain);
249 if (g_mkdir_with_parents (path.c_str(), 0775)) {
250 warning << string_compose(_("Could not create %1. Preset not saved. (%2)"), path, strerror(errno)) << endmsg;
255 if (g_mkdir_with_parents (path.c_str(), 0775)) {
256 warning << string_compose(_("Could not create %1. Preset not saved. (%2)"), path, strerror(errno)) << endmsg;
260 string const source = preset_source (envvar, domain);
262 if (lrdf_export_by_source (source.c_str(), source.substr(5).c_str())) {
263 warning << string_compose(_("Error saving presets file %1."), source) << endmsg;
271 Plugin::save_preset (string name, string domain)
273 lrdf_portvalue portvalues[parameter_count()];
274 lrdf_defaults defaults;
276 std::string unique (unique_id());
278 /* XXX problem: AU plugins don't have numeric ID's.
279 Solution: they have a different method of providing/saving presets.
280 XXX sub-problem: implement it.
283 if (!isdigit (unique[0])) {
287 id = atol (unique.c_str());
289 defaults.count = parameter_count();
290 defaults.items = portvalues;
292 for (uint32_t i = 0; i < parameter_count(); ++i) {
293 if (parameter_is_input (i)) {
294 portvalues[i].pid = i;
295 portvalues[i].value = get_parameter(i);
299 string const envvar = preset_envvar ();
300 if (envvar.empty()) {
301 warning << _("Could not locate HOME. Preset not saved.") << endmsg;
305 string const source = preset_source (envvar, domain);
307 char* uri = lrdf_add_preset (source.c_str(), name.c_str(), id, &defaults);
309 /* XXX: why is the uri apparently kept as the key in the `presets' map and also in the PresetRecord? */
311 presets.insert (make_pair (uri, PresetRecord (uri, name)));
314 bool const r = write_preset_file (envvar, domain);
316 PresetAdded (); /* EMIT SIGNAL */
322 ARDOUR::find_plugin(Session& session, string identifier, PluginType type)
324 PluginManager *mgr = PluginManager::the_manager();
325 PluginInfoList plugs;
329 plugs = mgr->ladspa_plugin_info();
334 plugs = mgr->lv2_plugin_info();
340 plugs = mgr->vst_plugin_info();
344 #ifdef HAVE_AUDIOUNITS
345 case ARDOUR::AudioUnit:
346 plugs = mgr->au_plugin_info();
351 return PluginPtr ((Plugin *) 0);
354 PluginInfoList::iterator i;
356 for (i = plugs.begin(); i != plugs.end(); ++i) {
357 if (identifier == (*i)->unique_id){
358 return (*i)->load (session);
363 /* hmm, we didn't find it. could be because in older versions of Ardour.
364 we used to store the name of a VST plugin, not its unique ID. so try
368 for (i = plugs.begin(); i != plugs.end(); ++i) {
369 if (identifier == (*i)->name){
370 return (*i)->load (session);
375 return PluginPtr ((Plugin*) 0);
379 Plugin::output_streams () const
381 /* LADSPA & VST should not get here because they do not
382 return "infinite" i/o counts.
384 return ChanCount::ZERO;
388 Plugin::input_streams () const
390 /* LADSPA & VST should not get here because they do not
391 return "infinite" i/o counts.
393 return ChanCount::ZERO;