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 ()
109 VSTPlugin::set_block_size (nframes_t nframes)
112 _plugin->dispatcher (_plugin, effSetBlockSize, 0, nframes, NULL, 0.0f);
117 VSTPlugin::default_value (uint32_t port)
123 VSTPlugin::set_parameter (uint32_t which, float val)
125 _plugin->setParameter (_plugin, which, val);
126 //ParameterChanged (which, val); /* EMIT SIGNAL */
130 VSTPlugin::get_parameter (uint32_t which) const
132 return _plugin->getParameter (_plugin, which);
137 VSTPlugin::nth_parameter (uint32_t n, bool& ok) const
144 VSTPlugin::get_state()
146 XMLNode *root = new XMLNode (state_node_name());
147 LocaleGuard lg (X_("POSIX"));
149 if (_fst->current_program != -1) {
151 snprintf (buf, sizeof (buf), "%d", _fst->current_program);
152 root->add_property ("current-program", buf);
155 if (_plugin->flags & 32 /* effFlagsProgramsChunks */) {
157 /* fetch the current chunk */
162 if ((data_size = _plugin->dispatcher (_plugin, 23 /* effGetChunk */, 0, 0, &data, false)) == 0) {
166 /* store information */
168 XMLNode* chunk_node = new XMLNode (X_("chunk"));
170 gchar * encoded_data = g_base64_encode (data, data_size);
171 chunk_node->add_content (encoded_data);
172 g_free (encoded_data);
174 root->add_child_nocopy (*chunk_node);
178 XMLNode* parameters = new XMLNode ("parameters");
180 for (long n = 0; n < _plugin->numParams; ++n) {
183 snprintf (index, sizeof (index), "param_%ld", n);
184 snprintf (val, sizeof (val), "%.12g", _plugin->getParameter (_plugin, n));
185 parameters->add_property (index, val);
188 root->add_child_nocopy (*parameters);
195 VSTPlugin::set_state(const XMLNode& node)
197 LocaleGuard lg (X_("POSIX"));
199 if (node.name() != state_node_name()) {
200 error << _("Bad node sent to VSTPlugin::set_state") << endmsg;
204 const XMLProperty* prop;
206 if ((prop = node.property ("current-program")) != 0) {
207 _fst->current_program = atoi (prop->value());
213 if ((child = find_named_node (node, X_("chunk"))) != 0) {
215 XMLPropertyList::const_iterator i;
216 XMLNodeList::const_iterator n;
219 for (n = child->children ().begin (); n != child->children ().end (); ++n) {
220 if ((*n)->is_content ()) {
221 gsize chunk_size = 0;
222 guchar * data = g_base64_decode ((*n)->content ().c_str (), &chunk_size);
223 //cerr << "Dispatch setChunk for " << name() << endl;
224 ret = _plugin->dispatcher (_plugin, 24 /* effSetChunk */, 0, chunk_size, data, 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);
243 /* program number is not knowable */
245 _fst->current_program = -1;
255 VSTPlugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor& desc) const
257 VstParameterProperties prop;
259 desc.min_unbound = false;
260 desc.max_unbound = false;
263 if (_plugin->dispatcher (_plugin, effGetParameterProperties, which, 0, &prop, 0)) {
265 #ifdef VESTIGE_COMPLETE
267 /* i have yet to find or hear of a VST plugin that uses this */
269 if (prop.flags & kVstParameterUsesIntegerMinMax) {
270 desc.lower = prop.minInteger;
271 desc.upper = prop.maxInteger;
277 if (prop.flags & kVstParameterUsesIntStep) {
279 desc.step = prop.stepInteger;
280 desc.smallstep = prop.stepInteger;
281 desc.largestep = prop.stepInteger;
283 } else if (prop.flags & kVstParameterUsesFloatStep) {
285 desc.step = prop.stepFloat;
286 desc.smallstep = prop.smallStepFloat;
287 desc.largestep = prop.largeStepFloat;
291 float range = desc.upper - desc.lower;
293 desc.step = range / 100.0f;
294 desc.smallstep = desc.step / 2.0f;
295 desc.largestep = desc.step * 10.0f;
298 desc.toggled = prop.flags & kVstParameterIsSwitch;
299 desc.logarithmic = false;
300 desc.sr_dependent = false;
301 desc.label = prop.label;
311 _plugin->dispatcher (_plugin, effGetParamName, which, 0, label, 0);
314 desc.integer_step = false;
318 desc.smallstep = 0.005f;
319 desc.largestep = 0.1f;
320 desc.toggled = false;
321 desc.logarithmic = false;
322 desc.sr_dependent = false;
329 VSTPlugin::load_preset (const string& name)
331 if (_plugin->flags & 32 /* effFlagsProgramsChunks */) {
332 error << _("no support for presets using chunks at this time")
336 return Plugin::load_preset (name);
340 VSTPlugin::save_preset (string name)
342 if (_plugin->flags & 32 /* effFlagsProgramsChunks */) {
343 error << _("no support for presets using chunks at this time")
347 return Plugin::save_preset (name, "vst");
351 VSTPlugin::describe_parameter (Evoral::Parameter param)
354 _plugin->dispatcher (_plugin, effGetParamName, param.id(), 0, name, 0);
359 VSTPlugin::signal_latency () const
362 return _user_latency;
365 #ifdef VESTIGE_HEADER
366 return *((nframes_t *) (((char *) &_plugin->flags) + 12)); /* initialDelay */
368 return _plugin->initial_delay;
372 set<Evoral::Parameter>
373 VSTPlugin::automatable () const
375 set<Evoral::Parameter> ret;
377 for (uint32_t i = 0; i < parameter_count(); ++i){
378 ret.insert (ret.end(), Evoral::Parameter(PluginAutomation, 0, i));
385 VSTPlugin::connect_and_run (BufferSet& bufs,
386 ChanMapping in_map, ChanMapping out_map,
387 nframes_t nframes, nframes_t offset)
389 float *ins[_plugin->numInputs];
390 float *outs[_plugin->numOutputs];
393 const uint32_t nbufs = bufs.count().n_audio();
395 for (i = 0; i < (int32_t) _plugin->numInputs; ++i) {
396 ins[i] = bufs.get_audio(min((uint32_t) in_index, nbufs - 1)).data() + offset;
400 for (i = 0; i < (int32_t) _plugin->numOutputs; ++i) {
401 outs[i] = bufs.get_audio(min((uint32_t) out_index, nbufs - 1)).data() + offset;
403 /* unbelievably, several VST plugins still rely on Cubase
404 behaviour and do not silence the buffer in processReplacing
405 when they have no output.
408 // memset (outs[i], 0, sizeof (Sample) * nframes);
413 /* we already know it can support processReplacing */
415 _plugin->processReplacing (_plugin, ins, outs, nframes);
421 VSTPlugin::deactivate ()
423 _plugin->dispatcher (_plugin, effMainsChanged, 0, 0, NULL, 0.0f);
427 VSTPlugin::activate ()
429 _plugin->dispatcher (_plugin, effMainsChanged, 0, 1, NULL, 0.0f);
433 VSTPlugin::unique_id() const
437 #ifdef VESTIGE_HEADER
438 snprintf (buf, sizeof (buf), "%d", *((int32_t*) &_plugin->unused_id));
440 snprintf (buf, sizeof (buf), "%d", _plugin->uniqueID);
447 VSTPlugin::name () const
453 VSTPlugin::maker () const
459 VSTPlugin::label () const
465 VSTPlugin::parameter_count() const
467 return _plugin->numParams;
471 VSTPlugin::has_editor () const
473 return _plugin->flags & effFlagsHasEditor;
477 VSTPlugin::print_parameter (uint32_t param, char *buf, uint32_t len) const
481 _plugin->dispatcher (_plugin, 7 /* effGetParamDisplay */, param, 0, buf, 0);
483 if (buf[0] == '\0') {
488 while (*first_nonws && isspace (*first_nonws)) {
491 if (*first_nonws == '\0') {
495 memmove (buf, first_nonws, strlen (buf) - (first_nonws - buf) + 1);
499 VSTPluginInfo::load (Session& session)
504 if (Config->get_use_vst()) {
507 handle = fst_load(path.c_str());
509 if ( (int)handle == -1) {
510 error << string_compose(_("VST: cannot load module from \"%1\""), path) << endmsg;
512 plugin.reset (new VSTPlugin (session.engine(), session, handle));
515 error << _("You asked ardour to not use any VST plugins") << endmsg;
516 return PluginPtr ((Plugin*) 0);
519 plugin->set_info(PluginInfoPtr(new VSTPluginInfo(*this)));
523 catch (failed_constructor &err) {
524 return PluginPtr ((Plugin*) 0);
528 VSTPluginInfo::VSTPluginInfo()