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.
24 #include <cstdio> // so libraptor doesn't complain
32 #include <pbd/compose.h>
33 #include <pbd/error.h>
34 #include <pbd/pathscanner.h>
35 #include <pbd/xml++.h>
36 #include <pbd/stacktrace.h>
38 #include <ardour/ardour.h>
39 #include <ardour/session.h>
40 #include <ardour/audioengine.h>
41 #include <ardour/plugin.h>
42 #include <ardour/ladspa_plugin.h>
43 #include <ardour/plugin_manager.h>
45 #ifdef HAVE_AUDIOUNITS
46 #include <ardour/audio_unit.h>
50 #include <ardour/lv2_plugin.h>
53 #include <pbd/stl_delete.h>
58 using namespace ARDOUR;
61 Plugin::Plugin (AudioEngine& e, Session& s)
62 : _engine (e), _session (s)
66 Plugin::Plugin (const Plugin& other)
67 : _engine (other._engine), _session (other._session), _info (other._info)
72 Plugin::setup_controls ()
74 uint32_t port_cnt = parameter_count();
76 /* set up a vector of null pointers for the controls.
77 we'll fill this in on an as-needed basis.
80 controls.assign (port_cnt, (PortControllable*) 0);
85 for (vector<PortControllable*>::iterator i = controls.begin(); i != controls.end(); ++i) {
93 Plugin::make_nth_control (uint32_t n, const XMLNode& node)
96 /* already constructed */
100 Plugin::ParameterDescriptor desc;
102 get_parameter_descriptor (n, desc);
104 controls[n] = new PortControllable (node, *this, n,
105 desc.lower, desc.upper, desc.toggled, desc.logarithmic);
109 Plugin::get_nth_control (uint32_t n, bool do_not_create)
111 if (n >= parameter_count()) {
115 if (controls[n] == 0 && !do_not_create) {
117 Plugin::ParameterDescriptor desc;
119 get_parameter_descriptor (n, desc);
121 controls[n] = new PortControllable (describe_parameter (n), *this, n,
122 desc.lower, desc.upper, desc.toggled, desc.logarithmic);
128 Plugin::PortControllable::PortControllable (string name, Plugin& p, uint32_t port_id,
129 float low, float up, bool t, bool loga)
130 : Controllable (name), plugin (p), absolute_port (port_id)
136 range = upper - lower;
139 Plugin::PortControllable::PortControllable (const XMLNode& node, Plugin& p, uint32_t port_id,
140 float low, float up, bool t, bool loga)
141 : Controllable (node), plugin (p), absolute_port (port_id)
147 range = upper - lower;
151 Plugin::PortControllable::set_value (float value)
162 value = lower + (range * value);
169 value = exp(_lower + log(range) * value);
173 plugin.set_parameter (absolute_port, value);
177 Plugin::PortControllable::get_value (void) const
179 float val = plugin.get_parameter (absolute_port);
191 return ((val - lower) / range);
196 Plugin::get_presets()
198 vector<string> labels;
200 std::string unique (unique_id());
202 /* XXX problem: AU plugins don't have numeric ID's.
203 Solution: they have a different method of providing presets.
204 XXX sub-problem: implement it.
207 if (!isdigit (unique[0])) {
211 id = atol (unique.c_str());
213 lrdf_uris* set_uris = lrdf_get_setting_uris(id);
216 for (uint32_t i = 0; i < (uint32_t) set_uris->count; ++i) {
217 if (char* label = lrdf_get_label(set_uris->items[i])) {
218 labels.push_back(label);
219 presets[label] = set_uris->items[i];
222 lrdf_free_uris(set_uris);
225 // GTK2FIX find an equivalent way to do this with a vector (needed by GUI apis)
232 Plugin::load_preset(const string preset_label)
234 lrdf_defaults* defs = lrdf_get_setting_values(presets[preset_label].c_str());
237 for (uint32_t i = 0; i < (uint32_t) defs->count; ++i) {
238 // The defs->items[i].pid < defs->count check is to work around
239 // a bug in liblrdf that saves invalid values into the presets file.
240 if (((uint32_t) defs->items[i].pid < (uint32_t) defs->count) && parameter_is_input (defs->items[i].pid)) {
241 set_parameter(defs->items[i].pid, defs->items[i].value);
244 lrdf_free_setting_values(defs);
251 Plugin::save_preset (string name, string domain)
253 lrdf_portvalue portvalues[parameter_count()];
254 lrdf_defaults defaults;
256 std::string unique (unique_id());
258 /* XXX problem: AU plugins don't have numeric ID's.
259 Solution: they have a different method of providing/saving presets.
260 XXX sub-problem: implement it.
263 if (!isdigit (unique[0])) {
267 id = atol (unique.c_str());
269 defaults.count = parameter_count();
270 defaults.items = portvalues;
272 for (uint32_t i = 0; i < parameter_count(); ++i) {
273 if (parameter_is_input (i)) {
274 portvalues[i].pid = i;
275 portvalues[i].value = get_parameter(i);
280 if ((envvar = getenv ("HOME")) == 0) {
281 warning << _("Could not locate HOME. Preset not saved.") << endmsg;
285 string source(string_compose("file:%1/.%2/rdf/ardour-presets.n3", envvar, domain));
287 free(lrdf_add_preset(source.c_str(), name.c_str(), id, &defaults));
289 string path = string_compose("%1/.%2", envvar, domain);
290 if (g_mkdir_with_parents (path.c_str(), 0775)) {
291 warning << string_compose(_("Could not create %1. Preset not saved. (%2)"), path, strerror(errno)) << endmsg;
296 if (g_mkdir_with_parents (path.c_str(), 0775)) {
297 warning << string_compose(_("Could not create %1. Preset not saved. (%2)"), path, strerror(errno)) << endmsg;
301 if (lrdf_export_by_source(source.c_str(), source.substr(5).c_str())) {
302 warning << string_compose(_("Error saving presets file %1."), source) << endmsg;
310 ARDOUR::find_plugin(Session& session, string identifier, PluginType type)
312 PluginManager *mgr = PluginManager::the_manager();
313 PluginInfoList plugs;
317 plugs = mgr->ladspa_plugin_info();
322 plugs = mgr->lv2_plugin_info();
328 plugs = mgr->vst_plugin_info();
332 #ifdef HAVE_AUDIOUNITS
333 case ARDOUR::AudioUnit:
334 plugs = mgr->au_plugin_info();
339 return PluginPtr ((Plugin *) 0);
342 PluginInfoList::iterator i;
344 for (i = plugs.begin(); i != plugs.end(); ++i) {
345 if (identifier == (*i)->unique_id){
346 return (*i)->load (session);
351 /* hmm, we didn't find it. could be because in older versions of Ardour.
352 we used to store the name of a VST plugin, not its unique ID. so try
356 for (i = plugs.begin(); i != plugs.end(); ++i) {
357 if (identifier == (*i)->name){
358 return (*i)->load (session);
363 return PluginPtr ((Plugin*) 0);
367 Plugin::configure_io (int32_t in, int32_t out)
369 /* parent Plugin class assumes static output stream count.
370 Derived classes can override.
373 Glib::Mutex::Lock em (_session.engine().process_lock());
374 IO::MoreOutputs (output_streams());
380 Plugin::can_do (int32_t in, int32_t& out)
382 int32_t outputs = get_info()->n_outputs;
383 int32_t inputs = get_info()->n_inputs;
387 /* instrument plugin, always legal, but it throws
388 away any existing active streams.
395 if (outputs == 1 && inputs == 1) {
396 /* mono plugin, replicate as needed */
407 if ((inputs < in) && (inputs % in == 0)) {
409 /* number of inputs is a factor of the requested input
410 configuration, so we can replicate.
413 int nplugs = in/inputs;
414 out = outputs * nplugs;
424 Plugin::output_streams () const
426 /* LADSPA & VST should not get here because they do not
427 return negative i/o counts.
433 Plugin::input_streams () const
435 /* LADSPA & VST should not get here because they do not
436 return negative i/o counts.