2 Copyright (C) 2004 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.
27 #include <cstdio> // so libraptor doesn't complain
30 #include <string.h> // for memmove
37 #include <pbd/compose.h>
38 #include <pbd/error.h>
39 #include <pbd/pathscanner.h>
40 #include <pbd/xml++.h>
42 #include <vst/aeffectx.h>
44 #include <ardour/ardour.h>
45 #include <ardour/session.h>
46 #include <ardour/audioengine.h>
47 #include <ardour/vst_plugin.h>
49 #include <pbd/stl_delete.h>
54 using namespace ARDOUR;
59 VSTPlugin::VSTPlugin (AudioEngine& e, Session& session, FSTHandle* h)
64 if ((_fst = fst_instantiate (handle, Session::vst_callback, this)) == 0) {
65 throw failed_constructor();
68 _plugin = _fst->plugin;
71 /* set rate and blocksize */
73 _plugin->dispatcher (_plugin, effSetSampleRate, 0, 0, NULL,
74 (float) session.frame_rate());
75 _plugin->dispatcher (_plugin, effSetBlockSize, 0,
76 session.get_block_size(), NULL, 0.0f);
78 /* set program to zero */
80 _plugin->dispatcher (_plugin, effSetProgram, 0, 0, NULL, 0.0f);
82 Plugin::setup_controls ();
85 VSTPlugin::VSTPlugin (const VSTPlugin &other)
88 handle = other.handle;
90 if ((_fst = fst_instantiate (handle, Session::vst_callback, this)) == 0) {
91 throw failed_constructor();
93 _plugin = _fst->plugin;
95 Plugin::setup_controls ();
98 VSTPlugin::~VSTPlugin ()
101 GoingAway (); /* EMIT SIGNAL */
106 VSTPlugin::set_block_size (nframes_t nframes)
109 _plugin->dispatcher (_plugin, effSetBlockSize, 0, nframes, NULL, 0.0f);
114 VSTPlugin::store_state (PluginState& state)
119 VSTPlugin::restore_state (PluginState& state)
124 VSTPlugin::default_value (uint32_t port)
130 VSTPlugin::set_parameter (uint32_t which, float val)
132 _plugin->setParameter (_plugin, which, val);
133 ParameterChanged (which, val); /* EMIT SIGNAL */
137 VSTPlugin::get_parameter (uint32_t which) const
139 return _plugin->getParameter (_plugin, which);
144 VSTPlugin::nth_parameter (uint32_t n, bool& ok) const
151 VSTPlugin::get_state()
153 XMLNode *root = new XMLNode (state_node_name());
154 LocaleGuard lg (X_("POSIX"));
156 if (_plugin->flags & effFlagsProgramChunks) {
158 /* fetch the current chunk */
163 if ((data_size = _plugin->dispatcher (_plugin, effGetChunk, 0, 0, &data, false)) == 0) {
167 /* save it to a file */
172 path = getenv ("HOME");
173 path += "/.ardour/vst";
175 if (stat (path.c_str(), &sbuf)) {
176 if (errno == ENOENT) {
177 if (g_mkdir_with_parents (path.c_str(), 0600)) {
178 error << string_compose (_("cannot create VST chunk directory: %1"),
186 error << string_compose (_("cannot check VST chunk directory: %1"),
192 } else if (!S_ISDIR (sbuf.st_mode)) {
193 error << string_compose (_("%1 exists but is not a directory"), path)
200 /* store information */
202 XMLNode* chunk_node = new XMLNode (X_("chunk"));
203 chunk_node->add_property ("path", path);
205 root->add_child_nocopy (*chunk_node);
209 XMLNode* parameters = new XMLNode ("parameters");
211 for (long n = 0; n < _plugin->numParams; ++n) {
214 snprintf (index, sizeof (index), "param_%ld", n);
215 snprintf (val, sizeof (val), "%f", _plugin->getParameter (_plugin, n));
216 parameters->add_property (index, val);
219 root->add_child_nocopy (*parameters);
226 VSTPlugin::set_state(const XMLNode& node)
228 LocaleGuard lg (X_("POSIX"));
230 if (node.name() != state_node_name()) {
231 error << _("Bad node sent to VSTPlugin::set_state") << endmsg;
237 if ((child = find_named_node (node, X_("chunks"))) != 0) {
241 } else if ((child = find_named_node (node, X_("parameters"))) != 0) {
243 XMLPropertyList::const_iterator i;
245 for (i = child->properties().begin(); i != child->properties().end(); ++i) {
248 sscanf ((*i)->name().c_str(), "param_%ld", ¶m);
249 sscanf ((*i)->value().c_str(), "%f", &val);
251 _plugin->setParameter (_plugin, param, val);
261 VSTPlugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor& desc) const
263 VstParameterProperties prop;
265 desc.min_unbound = false;
266 desc.max_unbound = false;
268 if (_plugin->dispatcher (_plugin, effGetParameterProperties, which, 0, &prop, 0)) {
270 /* i have yet to find or hear of a VST plugin that uses this */
272 if (prop.flags & kVstParameterUsesIntegerMinMax) {
273 desc.lower = prop.minInteger;
274 desc.upper = prop.maxInteger;
280 if (prop.flags & kVstParameterUsesIntStep) {
282 desc.step = prop.stepInteger;
283 desc.smallstep = prop.stepInteger;
284 desc.largestep = prop.stepInteger;
286 } else if (prop.flags & kVstParameterUsesFloatStep) {
288 desc.step = prop.stepFloat;
289 desc.smallstep = prop.smallStepFloat;
290 desc.largestep = prop.largeStepFloat;
294 float range = desc.upper - desc.lower;
296 desc.step = range / 100.0f;
297 desc.smallstep = desc.step / 2.0f;
298 desc.largestep = desc.step * 10.0f;
301 desc.toggled = prop.flags & kVstParameterIsSwitch;
302 desc.logarithmic = false;
303 desc.sr_dependent = false;
304 desc.label = prop.label;
313 _plugin->dispatcher (_plugin, effGetParamName, which, 0, label, 0);
316 desc.integer_step = false;
320 desc.smallstep = 0.005f;
321 desc.largestep = 0.1f;
322 desc.toggled = false;
323 desc.logarithmic = false;
324 desc.sr_dependent = false;
331 VSTPlugin::load_preset (string name)
333 if (_plugin->flags & effFlagsProgramChunks) {
334 error << _("no support for presets using chunks at this time")
338 return Plugin::load_preset (name);
342 VSTPlugin::save_preset (string name)
344 if (_plugin->flags & effFlagsProgramChunks) {
345 error << _("no support for presets using chunks at this time")
349 return Plugin::save_preset (name, "vst");
353 VSTPlugin::describe_parameter (uint32_t param)
356 _plugin->dispatcher (_plugin, effGetParamName, param, 0, name, 0);
361 VSTPlugin::latency () const
363 return _plugin->initialDelay;
367 VSTPlugin::automatable () const
371 for (uint32_t i = 0; i < parameter_count(); ++i){
372 ret.insert (ret.end(), i);
379 VSTPlugin::connect_and_run (vector<Sample*>& bufs, uint32_t maxbuf, int32_t& in_index, int32_t& out_index, nframes_t nframes, nframes_t offset)
381 float *ins[_plugin->numInputs];
382 float *outs[_plugin->numOutputs];
385 for (i = 0; i < (int32_t) _plugin->numInputs; ++i) {
386 ins[i] = bufs[min((uint32_t) in_index,maxbuf - 1)] + offset;
390 for (i = 0; i < (int32_t) _plugin->numOutputs; ++i) {
391 outs[i] = bufs[min((uint32_t) out_index,maxbuf - 1)] + offset;
393 /* unbelievably, several VST plugins still rely on Cubase
394 behaviour and do not silence the buffer in processReplacing
395 when they have no output.
398 // memset (outs[i], 0, sizeof (Sample) * nframes);
403 /* we already know it can support processReplacing */
405 _plugin->processReplacing (_plugin, ins, outs, nframes);
411 VSTPlugin::deactivate ()
413 _plugin->dispatcher (_plugin, effMainsChanged, 0, 0, NULL, 0.0f);
417 VSTPlugin::activate ()
419 _plugin->dispatcher (_plugin, effMainsChanged, 0, 1, NULL, 0.0f);
423 VSTPlugin::unique_id() const
425 return _plugin->uniqueID;
430 VSTPlugin::name () const
436 VSTPlugin::maker () const
442 VSTPlugin::label () const
448 VSTPlugin::parameter_count() const
450 return _plugin->numParams;
454 VSTPlugin::has_editor () const
456 return _plugin->flags & effFlagsHasEditor;
460 VSTPlugin::print_parameter (uint32_t param, char *buf, uint32_t len) const
465 _plugin->dispatcher (_plugin, effGetParamLabel, param, 0, lab, 0);
466 _plugin->dispatcher (_plugin, effGetParamDisplay, param, 0, buf, 0);
468 if (buf[0] == '\0') {
473 while (*first_nonws && isspace (*first_nonws)) {
476 if (*first_nonws == '\0') {
480 memmove (buf, first_nonws, strlen (buf) - (first_nonws - buf) + 1);
484 VSTPluginInfo::load (Session& session)
489 if (Config->get_use_vst()) {
492 handle = fst_load(path.c_str());
494 if ( (int)handle == -1) {
495 error << string_compose(_("VST: cannot load module from \"%1\""), path) << endmsg;
497 plugin.reset (new VSTPlugin (session.engine(), session, handle));
500 error << _("You asked ardour to not use any VST plugins") << endmsg;
501 return PluginPtr ((Plugin*) 0);
504 plugin->set_info(PluginInfoPtr(new VSTPluginInfo(*this)));
508 catch (failed_constructor &err) {
509 return PluginPtr ((Plugin*) 0);