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/miscutils.h>
38 #include "pbd/compose.h"
39 #include "pbd/error.h"
40 #include "pbd/pathscanner.h"
41 #include "pbd/xml++.h"
45 #include "ardour/session.h"
46 #include "ardour/audioengine.h"
47 #include "ardour/filesystem_paths.h"
48 #include "ardour/vst_plugin.h"
49 #include "ardour/buffer_set.h"
50 #include "ardour/audio_buffer.h"
51 #include "ardour/midi_buffer.h"
53 #include "pbd/stl_delete.h"
59 using namespace ARDOUR;
64 VSTPlugin::VSTPlugin (AudioEngine& e, Session& session, FSTHandle* h)
69 if ((_fst = fst_instantiate (handle, Session::vst_callback, this)) == 0) {
70 throw failed_constructor();
73 _plugin = _fst->plugin;
76 /* set rate and blocksize */
78 _plugin->dispatcher (_plugin, effSetSampleRate, 0, 0, NULL,
79 (float) session.frame_rate());
80 _plugin->dispatcher (_plugin, effSetBlockSize, 0,
81 session.get_block_size(), NULL, 0.0f);
83 /* set program to zero */
85 _plugin->dispatcher (_plugin, effSetProgram, 0, 0, NULL, 0.0f);
87 // Plugin::setup_controls ();
90 VSTPlugin::VSTPlugin (const VSTPlugin &other)
93 handle = other.handle;
95 if ((_fst = fst_instantiate (handle, Session::vst_callback, this)) == 0) {
96 throw failed_constructor();
98 _plugin = _fst->plugin;
100 // Plugin::setup_controls ();
103 VSTPlugin::~VSTPlugin ()
110 VSTPlugin::set_block_size (nframes_t nframes)
113 _plugin->dispatcher (_plugin, effSetBlockSize, 0, nframes, NULL, 0.0f);
119 VSTPlugin::default_value (uint32_t port)
125 VSTPlugin::set_parameter (uint32_t which, float val)
127 _plugin->setParameter (_plugin, which, val);
128 //ParameterChanged (which, val); /* EMIT SIGNAL */
132 VSTPlugin::get_parameter (uint32_t which) const
134 return _plugin->getParameter (_plugin, which);
139 VSTPlugin::nth_parameter (uint32_t n, bool& ok) const
146 VSTPlugin::get_state()
148 XMLNode *root = new XMLNode (state_node_name());
149 LocaleGuard lg (X_("POSIX"));
151 if (_fst->current_program != -1) {
153 snprintf (buf, sizeof (buf), "%d", _fst->current_program);
154 root->add_property ("current-program", buf);
157 if (_plugin->flags & 32 /* effFlagsProgramsChunks */) {
159 /* fetch the current chunk */
164 if ((data_size = _plugin->dispatcher (_plugin, 23 /* effGetChunk */, 0, 0, &data, false)) == 0) {
168 /* store information */
170 XMLNode* chunk_node = new XMLNode (X_("chunk"));
172 gchar * encoded_data = g_base64_encode (data, data_size);
173 chunk_node->add_content (encoded_data);
174 g_free (encoded_data);
176 root->add_child_nocopy (*chunk_node);
180 XMLNode* parameters = new XMLNode ("parameters");
182 for (int32_t n = 0; n < _plugin->numParams; ++n) {
185 snprintf (index, sizeof (index), "param_%ld", n);
186 snprintf (val, sizeof (val), "%.12g", _plugin->getParameter (_plugin, n));
187 parameters->add_property (index, val);
190 root->add_child_nocopy (*parameters);
197 VSTPlugin::set_state(const XMLNode& node, int)
199 LocaleGuard lg (X_("POSIX"));
201 if (node.name() != state_node_name()) {
202 error << _("Bad node sent to VSTPlugin::set_state") << endmsg;
206 const XMLProperty* prop;
208 if ((prop = node.property ("current-program")) != 0) {
209 _fst->current_program = atoi (prop->value().c_str());
215 if ((child = find_named_node (node, X_("chunk"))) != 0) {
217 XMLPropertyList::const_iterator i;
218 XMLNodeList::const_iterator n;
221 for (n = child->children ().begin (); n != child->children ().end (); ++n) {
222 if ((*n)->is_content ()) {
223 gsize chunk_size = 0;
224 guchar * data = g_base64_decode ((*n)->content ().c_str (), &chunk_size);
225 //cerr << "Dispatch setChunk for " << name() << endl;
226 ret = _plugin->dispatcher (_plugin, 24 /* effSetChunk */, 0, chunk_size, data, 0);
231 } else if ((child = find_named_node (node, X_("parameters"))) != 0) {
233 XMLPropertyList::const_iterator i;
235 for (i = child->properties().begin(); i != child->properties().end(); ++i) {
239 sscanf ((*i)->name().c_str(), "param_%d", ¶m);
240 sscanf ((*i)->value().c_str(), "%f", &val);
242 _plugin->setParameter (_plugin, param, val);
245 /* program number is not knowable */
247 _fst->current_program = -1;
257 VSTPlugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor& desc) const
259 VstParameterProperties prop;
261 desc.min_unbound = false;
262 desc.max_unbound = false;
265 if (_plugin->dispatcher (_plugin, effGetParameterProperties, which, 0, &prop, 0)) {
267 #ifdef VESTIGE_COMPLETE
269 /* i have yet to find or hear of a VST plugin that uses this */
271 if (prop.flags & kVstParameterUsesIntegerMinMax) {
272 desc.lower = prop.minInteger;
273 desc.upper = prop.maxInteger;
279 if (prop.flags & kVstParameterUsesIntStep) {
281 desc.step = prop.stepInteger;
282 desc.smallstep = prop.stepInteger;
283 desc.largestep = prop.stepInteger;
285 } else if (prop.flags & kVstParameterUsesFloatStep) {
287 desc.step = prop.stepFloat;
288 desc.smallstep = prop.smallStepFloat;
289 desc.largestep = prop.largeStepFloat;
293 float range = desc.upper - desc.lower;
295 desc.step = range / 100.0f;
296 desc.smallstep = desc.step / 2.0f;
297 desc.largestep = desc.step * 10.0f;
300 desc.toggled = prop.flags & kVstParameterIsSwitch;
301 desc.logarithmic = false;
302 desc.sr_dependent = false;
303 desc.label = prop.label;
313 _plugin->dispatcher (_plugin, effGetParamName, which, 0, label, 0);
316 desc.integer_step = false;
320 desc.smallstep = 0.005f;
321 desc.largestep = 0.1f;
322 desc.toggled = false;
323 desc.logarithmic = false;
324 desc.sr_dependent = false;
331 VSTPlugin::load_preset (const string& name)
333 if (_plugin->flags & 32 /* effFlagsProgramsChunks */) {
334 error << _("no support for presets using chunks at this time")
338 return Plugin::load_preset (name);
342 VSTPlugin::save_preset (string name)
344 if (_plugin->flags & 32 /* effFlagsProgramsChunks */) {
345 error << _("no support for presets using chunks at this time")
349 return Plugin::save_preset (name, "vst");
353 VSTPlugin::describe_parameter (Evoral::Parameter param)
356 _plugin->dispatcher (_plugin, effGetParamName, param.id(), 0, name, 0);
361 VSTPlugin::signal_latency () const
364 return _user_latency;
367 #ifdef VESTIGE_HEADER
368 return *((nframes_t *) (((char *) &_plugin->flags) + 12)); /* initialDelay */
370 return _plugin->initial_delay;
374 set<Evoral::Parameter>
375 VSTPlugin::automatable () const
377 set<Evoral::Parameter> ret;
379 for (uint32_t i = 0; i < parameter_count(); ++i){
380 ret.insert (ret.end(), Evoral::Parameter(PluginAutomation, 0, i));
387 VSTPlugin::connect_and_run (BufferSet& bufs,
388 ChanMapping in_map, ChanMapping out_map,
389 nframes_t nframes, nframes_t offset)
391 float *ins[_plugin->numInputs];
392 float *outs[_plugin->numOutputs];
395 const uint32_t nbufs = bufs.count().n_audio();
398 for (i = 0; i < (int32_t) _plugin->numInputs; ++i) {
399 ins[i] = bufs.get_audio(min((uint32_t) in_index, nbufs - 1)).data() + offset;
404 for (i = 0; i < (int32_t) _plugin->numOutputs; ++i) {
405 outs[i] = bufs.get_audio(min((uint32_t) out_index, nbufs - 1)).data() + offset;
407 /* unbelievably, several VST plugins still rely on Cubase
408 behaviour and do not silence the buffer in processReplacing
409 when they have no output.
412 // memset (outs[i], 0, sizeof (Sample) * nframes);
417 if (bufs.count().n_midi() > 0) {
418 VstEvents* v = bufs.get_vst_midi (0);
419 _plugin->dispatcher (_plugin, effProcessEvents, 0, 0, v, 0);
422 /* we already know it can support processReplacing */
424 _plugin->processReplacing (_plugin, ins, outs, nframes);
430 VSTPlugin::deactivate ()
432 _plugin->dispatcher (_plugin, effMainsChanged, 0, 0, NULL, 0.0f);
436 VSTPlugin::activate ()
438 _plugin->dispatcher (_plugin, effMainsChanged, 0, 1, NULL, 0.0f);
442 VSTPlugin::unique_id() const
446 #ifdef VESTIGE_HEADER
447 snprintf (buf, sizeof (buf), "%d", *((int32_t*) &_plugin->unused_id));
449 snprintf (buf, sizeof (buf), "%d", _plugin->uniqueID);
456 VSTPlugin::name () const
462 VSTPlugin::maker () const
468 VSTPlugin::label () const
474 VSTPlugin::parameter_count() const
476 return _plugin->numParams;
480 VSTPlugin::has_editor () const
482 return _plugin->flags & effFlagsHasEditor;
486 VSTPlugin::print_parameter (uint32_t param, char *buf, uint32_t len) const
490 _plugin->dispatcher (_plugin, 7 /* effGetParamDisplay */, param, 0, buf, 0);
492 if (buf[0] == '\0') {
497 while (*first_nonws && isspace (*first_nonws)) {
500 if (*first_nonws == '\0') {
504 memmove (buf, first_nonws, strlen (buf) - (first_nonws - buf) + 1);
508 VSTPluginInfo::load (Session& session)
513 if (Config->get_use_vst()) {
516 handle = fst_load(path.c_str());
518 if ( (int)handle == -1) {
519 error << string_compose(_("VST: cannot load module from \"%1\""), path) << endmsg;
521 plugin.reset (new VSTPlugin (session.engine(), session, handle));
524 error << _("You asked ardour to not use any VST plugins") << endmsg;
525 return PluginPtr ((Plugin*) 0);
528 plugin->set_info(PluginInfoPtr(new VSTPluginInfo(*this)));
532 catch (failed_constructor &err) {
533 return PluginPtr ((Plugin*) 0);
537 VSTPluginInfo::VSTPluginInfo()