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 <cstring> // for memmove
33 #include <glibmm/ustring.h>
34 #include <glibmm/miscutils.h>
39 #include "pbd/compose.h"
40 #include "pbd/error.h"
41 #include "pbd/pathscanner.h"
42 #include "pbd/xml++.h"
46 #include "ardour/session.h"
47 #include "ardour/audioengine.h"
48 #include "ardour/filesystem_paths.h"
49 #include "ardour/vst_plugin.h"
50 #include "ardour/buffer_set.h"
51 #include "ardour/audio_buffer.h"
53 #include "pbd/stl_delete.h"
58 using namespace ARDOUR;
63 VSTPlugin::VSTPlugin (AudioEngine& e, Session& session, FSTHandle* h)
68 if ((_fst = fst_instantiate (handle, Session::vst_callback, this)) == 0) {
69 throw failed_constructor();
72 _plugin = _fst->plugin;
75 /* set rate and blocksize */
77 _plugin->dispatcher (_plugin, effSetSampleRate, 0, 0, NULL,
78 (float) session.frame_rate());
79 _plugin->dispatcher (_plugin, effSetBlockSize, 0,
80 session.get_block_size(), NULL, 0.0f);
82 /* set program to zero */
84 _plugin->dispatcher (_plugin, effSetProgram, 0, 0, NULL, 0.0f);
86 // Plugin::setup_controls ();
89 VSTPlugin::VSTPlugin (const VSTPlugin &other)
92 handle = other.handle;
94 if ((_fst = fst_instantiate (handle, Session::vst_callback, this)) == 0) {
95 throw failed_constructor();
97 _plugin = _fst->plugin;
99 // Plugin::setup_controls ();
102 VSTPlugin::~VSTPlugin ()
105 GoingAway (); /* EMIT SIGNAL */
110 VSTPlugin::set_block_size (nframes_t nframes)
113 _plugin->dispatcher (_plugin, effSetBlockSize, 0, nframes, NULL, 0.0f);
118 VSTPlugin::default_value (uint32_t port)
124 VSTPlugin::set_parameter (uint32_t which, float val)
126 _plugin->setParameter (_plugin, which, val);
127 //ParameterChanged (which, val); /* EMIT SIGNAL */
131 VSTPlugin::get_parameter (uint32_t which) const
133 return _plugin->getParameter (_plugin, which);
138 VSTPlugin::nth_parameter (uint32_t n, bool& ok) const
145 VSTPlugin::get_state()
147 XMLNode *root = new XMLNode (state_node_name());
148 LocaleGuard lg (X_("POSIX"));
150 if (_fst->current_program != -1) {
152 snprintf (buf, sizeof (buf), "%d", _fst->current_program);
153 root->add_property ("current-program", buf);
156 if (_plugin->flags & 32 /* effFlagsProgramsChunks */) {
158 /* fetch the current chunk */
163 if ((data_size = _plugin->dispatcher (_plugin, 23 /* effGetChunk */, 0, 0, &data, false)) == 0) {
167 /* store information */
169 XMLNode* chunk_node = new XMLNode (X_("chunk"));
171 gchar * encoded_data = g_base64_encode (data, data_size);
172 chunk_node->add_content (encoded_data);
173 g_free (encoded_data);
175 root->add_child_nocopy (*chunk_node);
179 XMLNode* parameters = new XMLNode ("parameters");
181 for (long n = 0; n < _plugin->numParams; ++n) {
184 snprintf (index, sizeof (index), "param_%ld", n);
185 snprintf (val, sizeof (val), "%.12g", _plugin->getParameter (_plugin, n));
186 parameters->add_property (index, val);
189 root->add_child_nocopy (*parameters);
196 VSTPlugin::set_state(const XMLNode& node)
198 LocaleGuard lg (X_("POSIX"));
200 if (node.name() != state_node_name()) {
201 error << _("Bad node sent to VSTPlugin::set_state") << endmsg;
205 const XMLProperty* prop;
207 if ((prop = node.property ("current-program")) != 0) {
208 _fst->current_program = atoi (prop->value());
214 if ((child = find_named_node (node, X_("chunk"))) != 0) {
216 XMLPropertyList::const_iterator i;
217 XMLNodeList::const_iterator n;
220 for (n = child->children ().begin (); n != child->children ().end (); ++n) {
221 if ((*n)->is_content ()) {
222 gsize chunk_size = 0;
223 guchar * data = g_base64_decode ((*n)->content ().c_str (), &chunk_size);
224 //cerr << "Dispatch setChunk for " << name() << endl;
225 ret = _plugin->dispatcher (_plugin, 24 /* effSetChunk */, 0, chunk_size, data, 0);
230 } else if ((child = find_named_node (node, X_("parameters"))) != 0) {
232 XMLPropertyList::const_iterator i;
234 for (i = child->properties().begin(); i != child->properties().end(); ++i) {
238 sscanf ((*i)->name().c_str(), "param_%ld", ¶m);
239 sscanf ((*i)->value().c_str(), "%f", &val);
241 _plugin->setParameter (_plugin, param, val);
244 /* program number is not knowable */
246 _fst->current_program = -1;
256 VSTPlugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor& desc) const
258 VstParameterProperties prop;
260 desc.min_unbound = false;
261 desc.max_unbound = false;
264 if (_plugin->dispatcher (_plugin, effGetParameterProperties, which, 0, &prop, 0)) {
266 #ifdef VESTIGE_COMPLETE
268 /* i have yet to find or hear of a VST plugin that uses this */
270 if (prop.flags & kVstParameterUsesIntegerMinMax) {
271 desc.lower = prop.minInteger;
272 desc.upper = prop.maxInteger;
278 if (prop.flags & kVstParameterUsesIntStep) {
280 desc.step = prop.stepInteger;
281 desc.smallstep = prop.stepInteger;
282 desc.largestep = prop.stepInteger;
284 } else if (prop.flags & kVstParameterUsesFloatStep) {
286 desc.step = prop.stepFloat;
287 desc.smallstep = prop.smallStepFloat;
288 desc.largestep = prop.largeStepFloat;
292 float range = desc.upper - desc.lower;
294 desc.step = range / 100.0f;
295 desc.smallstep = desc.step / 2.0f;
296 desc.largestep = desc.step * 10.0f;
299 desc.toggled = prop.flags & kVstParameterIsSwitch;
300 desc.logarithmic = false;
301 desc.sr_dependent = false;
302 desc.label = prop.label;
312 _plugin->dispatcher (_plugin, effGetParamName, which, 0, label, 0);
315 desc.integer_step = false;
319 desc.smallstep = 0.005f;
320 desc.largestep = 0.1f;
321 desc.toggled = false;
322 desc.logarithmic = false;
323 desc.sr_dependent = false;
330 VSTPlugin::load_preset (string name)
332 if (_plugin->flags & 32 /* effFlagsProgramsChunks */) {
333 error << _("no support for presets using chunks at this time")
337 return Plugin::load_preset (name);
341 VSTPlugin::save_preset (string name)
343 if (_plugin->flags & 32 /* effFlagsProgramsChunks */) {
344 error << _("no support for presets using chunks at this time")
348 return Plugin::save_preset (name, "vst");
352 VSTPlugin::describe_parameter (Evoral::Parameter param)
355 _plugin->dispatcher (_plugin, effGetParamName, param.id(), 0, name, 0);
360 VSTPlugin::signal_latency () const
363 return _user_latency;
366 #ifdef VESTIGE_HEADER
367 return *((nframes_t *) (((char *) &_plugin->flags) + 12)); /* initialDelay */
369 return _plugin->initial_delay;
373 set<Evoral::Parameter>
374 VSTPlugin::automatable () const
376 set<Evoral::Parameter> ret;
378 for (uint32_t i = 0; i < parameter_count(); ++i){
379 ret.insert (ret.end(), Evoral::Parameter(PluginAutomation, 0, i));
386 VSTPlugin::connect_and_run (BufferSet& bufs, uint32_t& in_index, uint32_t& out_index, nframes_t nframes, nframes_t offset)
388 float *ins[_plugin->numInputs];
389 float *outs[_plugin->numOutputs];
392 const uint32_t nbufs = bufs.count().n_audio();
394 for (i = 0; i < (int32_t) _plugin->numInputs; ++i) {
395 ins[i] = bufs.get_audio(min((uint32_t) in_index, nbufs - 1)).data() + offset;
399 for (i = 0; i < (int32_t) _plugin->numOutputs; ++i) {
400 outs[i] = bufs.get_audio(min((uint32_t) out_index, nbufs - 1)).data() + 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
436 #ifdef VESTIGE_HEADER
437 snprintf (buf, sizeof (buf), "%d", *((int32_t*) &_plugin->unused_id));
439 snprintf (buf, sizeof (buf), "%d", _plugin->uniqueID);
446 VSTPlugin::name () const
452 VSTPlugin::maker () const
458 VSTPlugin::label () const
464 VSTPlugin::parameter_count() const
466 return _plugin->numParams;
470 VSTPlugin::has_editor () const
472 return _plugin->flags & effFlagsHasEditor;
476 VSTPlugin::print_parameter (uint32_t param, char *buf, uint32_t len) const
480 _plugin->dispatcher (_plugin, 7 /* effGetParamDisplay */, param, 0, buf, 0);
482 if (buf[0] == '\0') {
487 while (*first_nonws && isspace (*first_nonws)) {
490 if (*first_nonws == '\0') {
494 memmove (buf, first_nonws, strlen (buf) - (first_nonws - buf) + 1);
498 VSTPluginInfo::load (Session& session)
503 if (Config->get_use_vst()) {
506 handle = fst_load(path.c_str());
508 if ( (int)handle == -1) {
509 error << string_compose(_("VST: cannot load module from \"%1\""), path) << endmsg;
511 plugin.reset (new VSTPlugin (session.engine(), session, handle));
514 error << _("You asked ardour to not use any VST plugins") << endmsg;
515 return PluginPtr ((Plugin*) 0);
518 plugin->set_info(PluginInfoPtr(new VSTPluginInfo(*this)));
522 catch (failed_constructor &err) {
523 return PluginPtr ((Plugin*) 0);