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.
26 #include <cstdio> // so libraptor doesn't complain
29 #include <string.h> // for memmove
36 #include <pbd/compose.h>
37 #include <pbd/error.h>
38 #include <pbd/pathscanner.h>
39 #include <pbd/xml++.h>
41 #include <vst/aeffectx.h>
43 #include <ardour/ardour.h>
44 #include <ardour/session.h>
45 #include <ardour/audioengine.h>
46 #include <ardour/vst_plugin.h>
48 #include <pbd/stl_delete.h>
53 using namespace ARDOUR;
58 VSTPlugin::VSTPlugin (AudioEngine& e, Session& session, FSTHandle* h)
63 if ((_fst = fst_instantiate (handle, Session::vst_callback, this)) == 0) {
64 throw failed_constructor();
67 _plugin = _fst->plugin;
70 /* set rate and blocksize */
72 _plugin->dispatcher (_plugin, effSetSampleRate, 0, 0, NULL,
73 (float) session.frame_rate());
74 _plugin->dispatcher (_plugin, effSetBlockSize, 0,
75 session.get_block_size(), NULL, 0.0f);
77 /* set program to zero */
79 _plugin->dispatcher (_plugin, effSetProgram, 0, 0, NULL, 0.0f);
81 Plugin::setup_controls ();
84 VSTPlugin::VSTPlugin (const VSTPlugin &other)
87 handle = other.handle;
89 if ((_fst = fst_instantiate (handle, Session::vst_callback, this)) == 0) {
90 throw failed_constructor();
92 _plugin = _fst->plugin;
94 Plugin::setup_controls ();
97 VSTPlugin::~VSTPlugin ()
100 GoingAway (); /* EMIT SIGNAL */
105 VSTPlugin::set_block_size (nframes_t nframes)
108 _plugin->dispatcher (_plugin, effSetBlockSize, 0, nframes, NULL, 0.0f);
113 VSTPlugin::default_value (uint32_t port)
119 VSTPlugin::set_parameter (uint32_t which, float val)
121 _plugin->setParameter (_plugin, which, val);
122 ParameterChanged (which, val); /* EMIT SIGNAL */
126 VSTPlugin::get_parameter (uint32_t which) const
128 return _plugin->getParameter (_plugin, which);
133 VSTPlugin::nth_parameter (uint32_t n, bool& ok) const
140 VSTPlugin::get_state()
142 XMLNode *root = new XMLNode (state_node_name());
143 LocaleGuard lg (X_("POSIX"));
145 if (_plugin->flags & effFlagsProgramChunks) {
147 /* fetch the current chunk */
152 if ((data_size = _plugin->dispatcher (_plugin, effGetChunk, 0, 0, &data, false)) == 0) {
156 /* save it to a file */
161 path = getenv ("HOME");
162 path += "/.ardour/vst";
164 if (stat (path.c_str(), &sbuf)) {
165 if (errno == ENOENT) {
166 if (g_mkdir_with_parents (path.c_str(), 0600)) {
167 error << string_compose (_("cannot create VST chunk directory: %1"),
175 warning << string_compose (_("cannot check VST chunk directory: %1"), strerror (errno))
180 } else if (!S_ISDIR (sbuf.st_mode)) {
181 error << string_compose (_("%1 exists but is not a directory"), path)
188 /* store information */
190 XMLNode* chunk_node = new XMLNode (X_("chunk"));
191 chunk_node->add_property ("path", path);
193 root->add_child_nocopy (*chunk_node);
197 XMLNode* parameters = new XMLNode ("parameters");
199 for (long n = 0; n < _plugin->numParams; ++n) {
202 snprintf (index, sizeof (index), "param_%ld", n);
203 snprintf (val, sizeof (val), "%.12g", _plugin->getParameter (_plugin, n));
204 parameters->add_property (index, val);
207 root->add_child_nocopy (*parameters);
214 VSTPlugin::set_state(const XMLNode& node)
216 LocaleGuard lg (X_("POSIX"));
218 if (node.name() != state_node_name()) {
219 error << _("Bad node sent to VSTPlugin::set_state") << endmsg;
225 if ((child = find_named_node (node, X_("chunks"))) != 0) {
229 } else if ((child = find_named_node (node, X_("parameters"))) != 0) {
231 XMLPropertyList::const_iterator i;
233 for (i = child->properties().begin(); i != child->properties().end(); ++i) {
237 sscanf ((*i)->name().c_str(), "param_%ld", ¶m);
238 sscanf ((*i)->value().c_str(), "%f", &val);
240 _plugin->setParameter (_plugin, param, val);
250 VSTPlugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor& desc) const
252 VstParameterProperties prop;
254 desc.min_unbound = false;
255 desc.max_unbound = false;
257 if (_plugin->dispatcher (_plugin, effGetParameterProperties, which, 0, &prop, 0)) {
259 /* i have yet to find or hear of a VST plugin that uses this */
261 if (prop.flags & kVstParameterUsesIntegerMinMax) {
262 desc.lower = prop.minInteger;
263 desc.upper = prop.maxInteger;
269 if (prop.flags & kVstParameterUsesIntStep) {
271 desc.step = prop.stepInteger;
272 desc.smallstep = prop.stepInteger;
273 desc.largestep = prop.stepInteger;
275 } else if (prop.flags & kVstParameterUsesFloatStep) {
277 desc.step = prop.stepFloat;
278 desc.smallstep = prop.smallStepFloat;
279 desc.largestep = prop.largeStepFloat;
283 float range = desc.upper - desc.lower;
285 desc.step = range / 100.0f;
286 desc.smallstep = desc.step / 2.0f;
287 desc.largestep = desc.step * 10.0f;
290 desc.toggled = prop.flags & kVstParameterIsSwitch;
291 desc.logarithmic = false;
292 desc.sr_dependent = false;
293 desc.label = prop.label;
302 _plugin->dispatcher (_plugin, effGetParamName, which, 0, label, 0);
305 desc.integer_step = false;
309 desc.smallstep = 0.005f;
310 desc.largestep = 0.1f;
311 desc.toggled = false;
312 desc.logarithmic = false;
313 desc.sr_dependent = false;
320 VSTPlugin::load_preset (string name)
322 if (_plugin->flags & effFlagsProgramChunks) {
323 error << _("no support for presets using chunks at this time")
327 return Plugin::load_preset (name);
331 VSTPlugin::save_preset (string name)
333 if (_plugin->flags & effFlagsProgramChunks) {
334 error << _("no support for presets using chunks at this time")
338 return Plugin::save_preset (name, "vst");
342 VSTPlugin::describe_parameter (uint32_t param)
345 _plugin->dispatcher (_plugin, effGetParamName, param, 0, name, 0);
350 VSTPlugin::latency () const
352 return _plugin->initialDelay;
356 VSTPlugin::automatable () const
360 for (uint32_t i = 0; i < parameter_count(); ++i){
361 ret.insert (ret.end(), i);
368 VSTPlugin::connect_and_run (vector<Sample*>& bufs, uint32_t maxbuf, int32_t& in_index, int32_t& out_index, nframes_t nframes, nframes_t offset)
370 float *ins[_plugin->numInputs];
371 float *outs[_plugin->numOutputs];
374 for (i = 0; i < (int32_t) _plugin->numInputs; ++i) {
375 ins[i] = bufs[min((uint32_t) in_index,maxbuf - 1)] + offset;
379 for (i = 0; i < (int32_t) _plugin->numOutputs; ++i) {
380 outs[i] = bufs[min((uint32_t) out_index,maxbuf - 1)] + offset;
382 /* unbelievably, several VST plugins still rely on Cubase
383 behaviour and do not silence the buffer in processReplacing
384 when they have no output.
387 // memset (outs[i], 0, sizeof (Sample) * nframes);
392 /* we already know it can support processReplacing */
394 _plugin->processReplacing (_plugin, ins, outs, nframes);
400 VSTPlugin::deactivate ()
402 _plugin->dispatcher (_plugin, effMainsChanged, 0, 0, NULL, 0.0f);
406 VSTPlugin::activate ()
408 _plugin->dispatcher (_plugin, effMainsChanged, 0, 1, NULL, 0.0f);
412 VSTPlugin::unique_id() const
414 return _plugin->uniqueID;
419 VSTPlugin::name () const
425 VSTPlugin::maker () const
431 VSTPlugin::label () const
437 VSTPlugin::parameter_count() const
439 return _plugin->numParams;
443 VSTPlugin::has_editor () const
445 return _plugin->flags & effFlagsHasEditor;
449 VSTPlugin::print_parameter (uint32_t param, char *buf, uint32_t len) const
454 _plugin->dispatcher (_plugin, effGetParamLabel, param, 0, lab, 0);
455 _plugin->dispatcher (_plugin, effGetParamDisplay, param, 0, buf, 0);
457 if (buf[0] == '\0') {
462 while (*first_nonws && isspace (*first_nonws)) {
465 if (*first_nonws == '\0') {
469 memmove (buf, first_nonws, strlen (buf) - (first_nonws - buf) + 1);
473 VSTPluginInfo::load (Session& session)
478 if (Config->get_use_vst()) {
481 handle = fst_load(path.c_str());
483 if ( (int)handle == -1) {
484 error << string_compose(_("VST: cannot load module from \"%1\""), path) << endmsg;
486 plugin.reset (new VSTPlugin (session.engine(), session, handle));
489 error << _("You asked ardour to not use any VST plugins") << endmsg;
490 return PluginPtr ((Plugin*) 0);
493 plugin->set_info(PluginInfoPtr(new VSTPluginInfo(*this)));
497 catch (failed_constructor &err) {
498 return PluginPtr ((Plugin*) 0);