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 <midi++/manager.h>
46 #include <ardour/ardour.h>
47 #include <ardour/session.h>
48 #include <ardour/audioengine.h>
49 #include <ardour/vst_plugin.h>
51 #include <pbd/stl_delete.h>
56 using namespace ARDOUR;
60 VSTPlugin::VSTPlugin (AudioEngine& e, Session& session, FSTHandle* h)
65 if ((_fst = fst_instantiate (handle, Session::vst_callback, this)) == 0) {
66 throw failed_constructor();
69 _plugin = _fst->plugin;
72 /* set rate and blocksize */
74 _plugin->dispatcher (_plugin, effSetSampleRate, 0, 0, NULL,
75 (float) session.frame_rate());
76 _plugin->dispatcher (_plugin, effSetBlockSize, 0,
77 session.get_block_size(), NULL, 0.0f);
79 /* set program to zero */
81 _plugin->dispatcher (_plugin, effSetProgram, 0, 0, NULL, 0.0f);
83 Plugin::setup_midi_controls ();
86 VSTPlugin::VSTPlugin (const VSTPlugin &other)
89 handle = other.handle;
91 if ((_fst = fst_instantiate (handle, Session::vst_callback, this)) == 0) {
92 throw failed_constructor();
94 _plugin = _fst->plugin;
96 Plugin::setup_midi_controls ();
99 VSTPlugin::~VSTPlugin ()
102 GoingAway (this); /* EMIT SIGNAL */
107 VSTPlugin::set_block_size (jack_nframes_t nframes)
110 _plugin->dispatcher (_plugin, effSetBlockSize, 0, nframes, NULL, 0.0f);
115 VSTPlugin::store_state (PluginState& state)
120 VSTPlugin::restore_state (PluginState& state)
125 VSTPlugin::default_value (uint32_t port)
131 VSTPlugin::set_parameter (uint32_t which, float val)
133 _plugin->setParameter (_plugin, which, val);
134 ParameterChanged (which, val); /* EMIT SIGNAL */
136 if (session().get_midi_feedback()) {
138 if (which < parameter_count() && midi_controls[which]) {
139 midi_controls[which]->send_feedback (val);
145 VSTPlugin::get_parameter (uint32_t which) const
147 return _plugin->getParameter (_plugin, which);
152 VSTPlugin::nth_parameter (uint32_t n, bool& ok) const
159 VSTPlugin::get_state()
161 XMLNode *root = new XMLNode (state_node_name());
162 LocaleGuard lg (X_("POSIX"));
164 if (_plugin->flags & effFlagsProgramChunks) {
166 /* fetch the current chunk */
171 if ((data_size = _plugin->dispatcher (_plugin, effGetChunk, 0, 0, &data, false)) == 0) {
175 /* save it to a file */
180 path = getenv ("HOME");
181 path += "/.ardour/vst";
183 if (stat (path.c_str(), &sbuf)) {
184 if (errno == ENOENT) {
185 if (mkdir (path.c_str(), 0600)) {
186 error << string_compose (_("cannot create VST chunk directory: %1"),
194 error << string_compose (_("cannot check VST chunk directory: %1"),
200 } else if (!S_ISDIR (sbuf.st_mode)) {
201 error << string_compose (_("%1 exists but is not a directory"), path)
208 /* store information */
210 XMLNode* chunk_node = new XMLNode (X_("chunk"));
211 chunk_node->add_property ("path", path);
213 root->add_child_nocopy (*chunk_node);
217 XMLNode* parameters = new XMLNode ("parameters");
219 for (long n = 0; n < _plugin->numParams; ++n) {
222 snprintf (index, sizeof (index), "param_%ld", n);
223 snprintf (val, sizeof (val), "%f", _plugin->getParameter (_plugin, n));
224 parameters->add_property (index, val);
227 root->add_child_nocopy (*parameters);
234 VSTPlugin::set_state(const XMLNode& node)
236 LocaleGuard lg (X_("POSIX"));
238 if (node.name() != state_node_name()) {
239 error << _("Bad node sent to VSTPlugin::set_state") << endmsg;
245 if ((child = find_named_node (node, X_("chunks"))) != 0) {
249 } else if ((child = find_named_node (node, X_("parameters"))) != 0) {
251 XMLPropertyList::const_iterator i;
253 for (i = child->properties().begin(); i != child->properties().end(); ++i) {
256 sscanf ((*i)->name().c_str(), "param_%ld", ¶m);
257 sscanf ((*i)->value().c_str(), "%f", &val);
259 _plugin->setParameter (_plugin, param, val);
269 VSTPlugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor& desc) const
271 VstParameterProperties prop;
273 desc.min_unbound = false;
274 desc.max_unbound = false;
276 if (_plugin->dispatcher (_plugin, effGetParameterProperties, which, 0, &prop, 0)) {
278 /* i have yet to find or hear of a VST plugin that uses this */
280 if (prop.flags & kVstParameterUsesIntegerMinMax) {
281 desc.lower = prop.minInteger;
282 desc.upper = prop.maxInteger;
288 if (prop.flags & kVstParameterUsesIntStep) {
290 desc.step = prop.stepInteger;
291 desc.smallstep = prop.stepInteger;
292 desc.largestep = prop.stepInteger;
294 } else if (prop.flags & kVstParameterUsesFloatStep) {
296 desc.step = prop.stepFloat;
297 desc.smallstep = prop.smallStepFloat;
298 desc.largestep = prop.largeStepFloat;
302 float range = desc.upper - desc.lower;
304 desc.step = range / 100.0f;
305 desc.smallstep = desc.step / 2.0f;
306 desc.largestep = desc.step * 10.0f;
309 desc.toggled = prop.flags & kVstParameterIsSwitch;
310 desc.logarithmic = false;
311 desc.sr_dependent = false;
312 desc.label = prop.label;
321 _plugin->dispatcher (_plugin, effGetParamName, which, 0, label, 0);
324 desc.integer_step = false;
328 desc.smallstep = 0.005f;
329 desc.largestep = 0.1f;
330 desc.toggled = false;
331 desc.logarithmic = false;
332 desc.sr_dependent = false;
339 VSTPlugin::load_preset (string name)
341 if (_plugin->flags & effFlagsProgramChunks) {
342 error << _("no support for presets using chunks at this time")
346 return Plugin::load_preset (name);
350 VSTPlugin::save_preset (string name)
352 if (_plugin->flags & effFlagsProgramChunks) {
353 error << _("no support for presets using chunks at this time")
357 return Plugin::save_preset (name, "vst");
361 VSTPlugin::describe_parameter (uint32_t param)
364 _plugin->dispatcher (_plugin, effGetParamName, param, 0, name, 0);
369 VSTPlugin::latency () const
371 return _plugin->initialDelay;
375 VSTPlugin::automatable () const
379 for (uint32_t i = 0; i < parameter_count(); ++i){
380 ret.insert (ret.end(), i);
387 VSTPlugin::connect_and_run (vector<Sample*>& bufs, uint32_t maxbuf, int32_t& in_index, int32_t& out_index, jack_nframes_t nframes, jack_nframes_t offset)
389 float *ins[_plugin->numInputs];
390 float *outs[_plugin->numOutputs];
393 for (i = 0; i < (int32_t) _plugin->numInputs; ++i) {
394 ins[i] = bufs[min((uint32_t) in_index,maxbuf)];
398 for (i = 0; i < (int32_t) _plugin->numOutputs; ++i) {
399 outs[i] = bufs[min((uint32_t) out_index,maxbuf)];
401 /* unbelievably, several VST plugins still rely on Cubase
402 behaviour and do not silence the buffer in processReplacing
403 when they have no output.
406 // memset (outs[i], 0, sizeof (Sample) * nframes);
411 /* we already know it can support processReplacing */
413 _plugin->processReplacing (_plugin, ins, outs, nframes);
419 VSTPlugin::deactivate ()
421 _plugin->dispatcher (_plugin, effMainsChanged, 0, 0, NULL, 0.0f);
425 VSTPlugin::activate ()
427 _plugin->dispatcher (_plugin, effMainsChanged, 0, 1, NULL, 0.0f);
431 VSTPlugin::unique_id() const
433 return _plugin->uniqueID;
438 VSTPlugin::name () const
444 VSTPlugin::maker () const
450 VSTPlugin::label () const
456 VSTPlugin::parameter_count() const
458 return _plugin->numParams;
462 VSTPlugin::has_editor () const
464 return _plugin->flags & effFlagsHasEditor;
468 VSTPlugin::print_parameter (uint32_t param, char *buf, uint32_t len) const
473 _plugin->dispatcher (_plugin, effGetParamLabel, param, 0, lab, 0);
474 _plugin->dispatcher (_plugin, effGetParamDisplay, param, 0, buf, 0);
476 if (buf[0] == '\0') {
481 while (*first_nonws && isspace (*first_nonws)) {
484 if (*first_nonws == '\0') {
488 memmove (buf, first_nonws, strlen (buf) - (first_nonws - buf) + 1);