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;
61 VSTPlugin::VSTPlugin (AudioEngine& e, Session& session, FSTHandle* h)
66 if ((_fst = fst_instantiate (handle, Session::vst_callback, this)) == 0) {
67 throw failed_constructor();
70 _plugin = _fst->plugin;
73 /* set rate and blocksize */
75 _plugin->dispatcher (_plugin, effSetSampleRate, 0, 0, NULL,
76 (float) session.frame_rate());
77 _plugin->dispatcher (_plugin, effSetBlockSize, 0,
78 session.get_block_size(), NULL, 0.0f);
80 /* set program to zero */
82 _plugin->dispatcher (_plugin, effSetProgram, 0, 0, NULL, 0.0f);
84 Plugin::setup_midi_controls ();
87 VSTPlugin::VSTPlugin (const VSTPlugin &other)
90 handle = other.handle;
92 if ((_fst = fst_instantiate (handle, Session::vst_callback, this)) == 0) {
93 throw failed_constructor();
95 _plugin = _fst->plugin;
97 Plugin::setup_midi_controls ();
100 VSTPlugin::~VSTPlugin ()
103 GoingAway (this); /* EMIT SIGNAL */
108 VSTPlugin::set_block_size (jack_nframes_t nframes)
111 _plugin->dispatcher (_plugin, effSetBlockSize, 0, nframes, NULL, 0.0f);
116 VSTPlugin::store_state (PluginState& state)
121 VSTPlugin::restore_state (PluginState& state)
126 VSTPlugin::default_value (uint32_t port)
132 VSTPlugin::set_parameter (uint32_t which, float val)
134 _plugin->setParameter (_plugin, which, val);
135 ParameterChanged (which, val); /* EMIT SIGNAL */
137 if (session().get_midi_feedback()) {
139 if (which < parameter_count() && midi_controls[which]) {
140 midi_controls[which]->send_feedback (val);
146 VSTPlugin::get_parameter (uint32_t which) const
148 return _plugin->getParameter (_plugin, which);
153 VSTPlugin::nth_parameter (uint32_t n, bool& ok) const
160 VSTPlugin::get_state()
162 XMLNode *root = new XMLNode (state_node_name());
163 LocaleGuard lg (X_("POSIX"));
165 if (_plugin->flags & effFlagsProgramChunks) {
167 /* fetch the current chunk */
172 if ((data_size = _plugin->dispatcher (_plugin, effGetChunk, 0, 0, &data, false)) == 0) {
176 /* save it to a file */
181 path = getenv ("HOME");
182 path += "/.ardour/vst";
184 if (stat (path.c_str(), &sbuf)) {
185 if (errno == ENOENT) {
186 if (mkdir (path.c_str(), 0600)) {
187 error << string_compose (_("cannot create VST chunk directory: %1"),
195 error << string_compose (_("cannot check VST chunk directory: %1"),
201 } else if (!S_ISDIR (sbuf.st_mode)) {
202 error << string_compose (_("%1 exists but is not a directory"), path)
209 /* store information */
211 XMLNode* chunk_node = new XMLNode (X_("chunk"));
212 chunk_node->add_property ("path", path);
214 root->add_child_nocopy (*chunk_node);
218 XMLNode* parameters = new XMLNode ("parameters");
220 for (long n = 0; n < _plugin->numParams; ++n) {
223 snprintf (index, sizeof (index), "param_%ld", n);
224 snprintf (val, sizeof (val), "%f", _plugin->getParameter (_plugin, n));
225 parameters->add_property (index, val);
228 root->add_child_nocopy (*parameters);
235 VSTPlugin::set_state(const XMLNode& node)
237 LocaleGuard lg (X_("POSIX"));
239 if (node.name() != state_node_name()) {
240 error << _("Bad node sent to VSTPlugin::set_state") << endmsg;
246 if ((child = find_named_node (node, X_("chunks"))) != 0) {
250 } else if ((child = find_named_node (node, X_("parameters"))) != 0) {
252 XMLPropertyList::const_iterator i;
254 for (i = child->properties().begin(); i != child->properties().end(); ++i) {
257 sscanf ((*i)->name().c_str(), "param_%ld", ¶m);
258 sscanf ((*i)->value().c_str(), "%f", &val);
260 _plugin->setParameter (_plugin, param, val);
270 VSTPlugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor& desc) const
272 VstParameterProperties prop;
274 desc.min_unbound = false;
275 desc.max_unbound = false;
277 if (_plugin->dispatcher (_plugin, effGetParameterProperties, which, 0, &prop, 0)) {
279 /* i have yet to find or hear of a VST plugin that uses this */
281 if (prop.flags & kVstParameterUsesIntegerMinMax) {
282 desc.lower = prop.minInteger;
283 desc.upper = prop.maxInteger;
289 if (prop.flags & kVstParameterUsesIntStep) {
291 desc.step = prop.stepInteger;
292 desc.smallstep = prop.stepInteger;
293 desc.largestep = prop.stepInteger;
295 } else if (prop.flags & kVstParameterUsesFloatStep) {
297 desc.step = prop.stepFloat;
298 desc.smallstep = prop.smallStepFloat;
299 desc.largestep = prop.largeStepFloat;
303 float range = desc.upper - desc.lower;
305 desc.step = range / 100.0f;
306 desc.smallstep = desc.step / 2.0f;
307 desc.largestep = desc.step * 10.0f;
310 desc.toggled = prop.flags & kVstParameterIsSwitch;
311 desc.logarithmic = false;
312 desc.sr_dependent = false;
313 desc.label = prop.label;
322 _plugin->dispatcher (_plugin, effGetParamName, which, 0, label, 0);
325 desc.integer_step = false;
329 desc.smallstep = 0.005f;
330 desc.largestep = 0.1f;
331 desc.toggled = false;
332 desc.logarithmic = false;
333 desc.sr_dependent = false;
340 VSTPlugin::load_preset (string name)
342 if (_plugin->flags & effFlagsProgramChunks) {
343 error << _("no support for presets using chunks at this time")
347 return Plugin::load_preset (name);
351 VSTPlugin::save_preset (string name)
353 if (_plugin->flags & effFlagsProgramChunks) {
354 error << _("no support for presets using chunks at this time")
358 return Plugin::save_preset (name, "vst");
362 VSTPlugin::describe_parameter (uint32_t param)
365 _plugin->dispatcher (_plugin, effGetParamName, param, 0, name, 0);
370 VSTPlugin::latency () const
372 return _plugin->initialDelay;
376 VSTPlugin::automatable () const
380 for (uint32_t i = 0; i < parameter_count(); ++i){
381 ret.insert (ret.end(), i);
388 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)
390 float *ins[_plugin->numInputs];
391 float *outs[_plugin->numOutputs];
394 for (i = 0; i < (int32_t) _plugin->numInputs; ++i) {
395 ins[i] = bufs[min((uint32_t) in_index,maxbuf - 1)] + offset;
399 for (i = 0; i < (int32_t) _plugin->numOutputs; ++i) {
400 outs[i] = bufs[min((uint32_t) out_index,maxbuf - 1)] + offset;
402 /* unbelievably, several VST plugins still rely on Cubase
403 behaviour and do not silence the buffer in processReplacing
404 when they have no output.
407 // memset (outs[i], 0, sizeof (Sample) * nframes);
412 /* we already know it can support processReplacing */
414 _plugin->processReplacing (_plugin, ins, outs, nframes);
420 VSTPlugin::deactivate ()
422 _plugin->dispatcher (_plugin, effMainsChanged, 0, 0, NULL, 0.0f);
426 VSTPlugin::activate ()
428 _plugin->dispatcher (_plugin, effMainsChanged, 0, 1, NULL, 0.0f);
432 VSTPlugin::unique_id() const
434 return _plugin->uniqueID;
439 VSTPlugin::name () const
445 VSTPlugin::maker () const
451 VSTPlugin::label () const
457 VSTPlugin::parameter_count() const
459 return _plugin->numParams;
463 VSTPlugin::has_editor () const
465 return _plugin->flags & effFlagsHasEditor;
469 VSTPlugin::print_parameter (uint32_t param, char *buf, uint32_t len) const
474 _plugin->dispatcher (_plugin, effGetParamLabel, param, 0, lab, 0);
475 _plugin->dispatcher (_plugin, effGetParamDisplay, param, 0, buf, 0);
477 if (buf[0] == '\0') {
482 while (*first_nonws && isspace (*first_nonws)) {
485 if (*first_nonws == '\0') {
489 memmove (buf, first_nonws, strlen (buf) - (first_nonws - buf) + 1);