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"
52 #include "ardour/midi_buffer.h"
54 #include "pbd/stl_delete.h"
60 using namespace ARDOUR;
65 VSTPlugin::VSTPlugin (AudioEngine& e, Session& session, FSTHandle* h)
70 if ((_fst = fst_instantiate (handle, Session::vst_callback, this)) == 0) {
71 throw failed_constructor();
74 _plugin = _fst->plugin;
77 /* set rate and blocksize */
79 _plugin->dispatcher (_plugin, effSetSampleRate, 0, 0, NULL,
80 (float) session.frame_rate());
81 _plugin->dispatcher (_plugin, effSetBlockSize, 0,
82 session.get_block_size(), NULL, 0.0f);
84 /* set program to zero */
86 _plugin->dispatcher (_plugin, effSetProgram, 0, 0, NULL, 0.0f);
88 // Plugin::setup_controls ();
91 VSTPlugin::VSTPlugin (const VSTPlugin &other)
94 handle = other.handle;
96 if ((_fst = fst_instantiate (handle, Session::vst_callback, this)) == 0) {
97 throw failed_constructor();
99 _plugin = _fst->plugin;
101 // Plugin::setup_controls ();
104 VSTPlugin::~VSTPlugin ()
111 VSTPlugin::set_block_size (nframes_t nframes)
114 _plugin->dispatcher (_plugin, effSetBlockSize, 0, nframes, NULL, 0.0f);
120 VSTPlugin::default_value (uint32_t port)
126 VSTPlugin::set_parameter (uint32_t which, float val)
128 _plugin->setParameter (_plugin, which, val);
129 //ParameterChanged (which, val); /* EMIT SIGNAL */
133 VSTPlugin::get_parameter (uint32_t which) const
135 return _plugin->getParameter (_plugin, which);
140 VSTPlugin::nth_parameter (uint32_t n, bool& ok) const
147 VSTPlugin::get_state()
149 XMLNode *root = new XMLNode (state_node_name());
150 LocaleGuard lg (X_("POSIX"));
152 if (_fst->current_program != -1) {
154 snprintf (buf, sizeof (buf), "%d", _fst->current_program);
155 root->add_property ("current-program", buf);
158 if (_plugin->flags & 32 /* effFlagsProgramsChunks */) {
160 /* fetch the current chunk */
165 if ((data_size = _plugin->dispatcher (_plugin, 23 /* effGetChunk */, 0, 0, &data, false)) == 0) {
169 /* store information */
171 XMLNode* chunk_node = new XMLNode (X_("chunk"));
173 gchar * encoded_data = g_base64_encode (data, data_size);
174 chunk_node->add_content (encoded_data);
175 g_free (encoded_data);
177 root->add_child_nocopy (*chunk_node);
181 XMLNode* parameters = new XMLNode ("parameters");
183 for (int32_t n = 0; n < _plugin->numParams; ++n) {
186 snprintf (index, sizeof (index), "param_%ld", n);
187 snprintf (val, sizeof (val), "%.12g", _plugin->getParameter (_plugin, n));
188 parameters->add_property (index, val);
191 root->add_child_nocopy (*parameters);
198 VSTPlugin::set_state(const XMLNode& node, int)
200 LocaleGuard lg (X_("POSIX"));
202 if (node.name() != state_node_name()) {
203 error << _("Bad node sent to VSTPlugin::set_state") << endmsg;
207 const XMLProperty* prop;
209 if ((prop = node.property ("current-program")) != 0) {
210 _fst->current_program = atoi (prop->value().c_str());
216 if ((child = find_named_node (node, X_("chunk"))) != 0) {
218 XMLPropertyList::const_iterator i;
219 XMLNodeList::const_iterator n;
222 for (n = child->children ().begin (); n != child->children ().end (); ++n) {
223 if ((*n)->is_content ()) {
224 gsize chunk_size = 0;
225 guchar * data = g_base64_decode ((*n)->content ().c_str (), &chunk_size);
226 //cerr << "Dispatch setChunk for " << name() << endl;
227 ret = _plugin->dispatcher (_plugin, 24 /* effSetChunk */, 0, chunk_size, data, 0);
232 } else if ((child = find_named_node (node, X_("parameters"))) != 0) {
234 XMLPropertyList::const_iterator i;
236 for (i = child->properties().begin(); i != child->properties().end(); ++i) {
240 sscanf ((*i)->name().c_str(), "param_%d", ¶m);
241 sscanf ((*i)->value().c_str(), "%f", &val);
243 _plugin->setParameter (_plugin, param, val);
246 /* program number is not knowable */
248 _fst->current_program = -1;
258 VSTPlugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor& desc) const
260 VstParameterProperties prop;
262 desc.min_unbound = false;
263 desc.max_unbound = false;
266 if (_plugin->dispatcher (_plugin, effGetParameterProperties, which, 0, &prop, 0)) {
268 #ifdef VESTIGE_COMPLETE
270 /* i have yet to find or hear of a VST plugin that uses this */
272 if (prop.flags & kVstParameterUsesIntegerMinMax) {
273 desc.lower = prop.minInteger;
274 desc.upper = prop.maxInteger;
280 if (prop.flags & kVstParameterUsesIntStep) {
282 desc.step = prop.stepInteger;
283 desc.smallstep = prop.stepInteger;
284 desc.largestep = prop.stepInteger;
286 } else if (prop.flags & kVstParameterUsesFloatStep) {
288 desc.step = prop.stepFloat;
289 desc.smallstep = prop.smallStepFloat;
290 desc.largestep = prop.largeStepFloat;
294 float range = desc.upper - desc.lower;
296 desc.step = range / 100.0f;
297 desc.smallstep = desc.step / 2.0f;
298 desc.largestep = desc.step * 10.0f;
301 desc.toggled = prop.flags & kVstParameterIsSwitch;
302 desc.logarithmic = false;
303 desc.sr_dependent = false;
304 desc.label = prop.label;
314 _plugin->dispatcher (_plugin, effGetParamName, which, 0, label, 0);
317 desc.integer_step = false;
321 desc.smallstep = 0.005f;
322 desc.largestep = 0.1f;
323 desc.toggled = false;
324 desc.logarithmic = false;
325 desc.sr_dependent = false;
332 VSTPlugin::load_preset (const string& name)
334 if (_plugin->flags & 32 /* effFlagsProgramsChunks */) {
335 error << _("no support for presets using chunks at this time")
339 return Plugin::load_preset (name);
343 VSTPlugin::save_preset (string name)
345 if (_plugin->flags & 32 /* effFlagsProgramsChunks */) {
346 error << _("no support for presets using chunks at this time")
350 return Plugin::save_preset (name, "vst");
354 VSTPlugin::describe_parameter (Evoral::Parameter param)
357 _plugin->dispatcher (_plugin, effGetParamName, param.id(), 0, name, 0);
362 VSTPlugin::signal_latency () const
365 return _user_latency;
368 #ifdef VESTIGE_HEADER
369 return *((nframes_t *) (((char *) &_plugin->flags) + 12)); /* initialDelay */
371 return _plugin->initial_delay;
375 set<Evoral::Parameter>
376 VSTPlugin::automatable () const
378 set<Evoral::Parameter> ret;
380 for (uint32_t i = 0; i < parameter_count(); ++i){
381 ret.insert (ret.end(), Evoral::Parameter(PluginAutomation, 0, i));
388 VSTPlugin::connect_and_run (BufferSet& bufs,
389 ChanMapping in_map, ChanMapping out_map,
390 nframes_t nframes, nframes_t offset)
392 float *ins[_plugin->numInputs];
393 float *outs[_plugin->numOutputs];
396 const uint32_t nbufs = bufs.count().n_audio();
399 for (i = 0; i < (int32_t) _plugin->numInputs; ++i) {
400 ins[i] = bufs.get_audio(min((uint32_t) in_index, nbufs - 1)).data() + offset;
405 for (i = 0; i < (int32_t) _plugin->numOutputs; ++i) {
406 outs[i] = bufs.get_audio(min((uint32_t) out_index, nbufs - 1)).data() + offset;
408 /* unbelievably, several VST plugins still rely on Cubase
409 behaviour and do not silence the buffer in processReplacing
410 when they have no output.
413 // memset (outs[i], 0, sizeof (Sample) * nframes);
418 if (bufs.count().n_midi() > 0) {
419 VstEvents* v = bufs.get_vst_midi (0);
420 _plugin->dispatcher (_plugin, effProcessEvents, 0, 0, v, 0);
423 /* we already know it can support processReplacing */
425 _plugin->processReplacing (_plugin, ins, outs, nframes);
431 VSTPlugin::deactivate ()
433 _plugin->dispatcher (_plugin, effMainsChanged, 0, 0, NULL, 0.0f);
437 VSTPlugin::activate ()
439 _plugin->dispatcher (_plugin, effMainsChanged, 0, 1, NULL, 0.0f);
443 VSTPlugin::unique_id() const
447 #ifdef VESTIGE_HEADER
448 snprintf (buf, sizeof (buf), "%d", *((int32_t*) &_plugin->unused_id));
450 snprintf (buf, sizeof (buf), "%d", _plugin->uniqueID);
457 VSTPlugin::name () const
463 VSTPlugin::maker () const
469 VSTPlugin::label () const
475 VSTPlugin::parameter_count() const
477 return _plugin->numParams;
481 VSTPlugin::has_editor () const
483 return _plugin->flags & effFlagsHasEditor;
487 VSTPlugin::print_parameter (uint32_t param, char *buf, uint32_t len) const
491 _plugin->dispatcher (_plugin, 7 /* effGetParamDisplay */, param, 0, buf, 0);
493 if (buf[0] == '\0') {
498 while (*first_nonws && isspace (*first_nonws)) {
501 if (*first_nonws == '\0') {
505 memmove (buf, first_nonws, strlen (buf) - (first_nonws - buf) + 1);
509 VSTPluginInfo::load (Session& session)
514 if (Config->get_use_vst()) {
517 handle = fst_load(path.c_str());
519 if ( (int)handle == -1) {
520 error << string_compose(_("VST: cannot load module from \"%1\""), path) << endmsg;
522 plugin.reset (new VSTPlugin (session.engine(), session, handle));
525 error << _("You asked ardour to not use any VST plugins") << endmsg;
526 return PluginPtr ((Plugin*) 0);
529 plugin->set_info(PluginInfoPtr(new VSTPluginInfo(*this)));
533 catch (failed_constructor &err) {
534 return PluginPtr ((Plugin*) 0);
538 VSTPluginInfo::VSTPluginInfo()