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 (pframes_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
145 /** Get VST chunk as base64-encoded data.
146 * @param single true for single program, false for all programs.
147 * @return 0-terminated base64-encoded data; must be passed to g_free () by caller.
150 VSTPlugin::get_chunk (bool single)
153 int32_t data_size = _plugin->dispatcher (_plugin, 23 /* effGetChunk */, single ? 1 : 0, 0, &data, 0);
154 if (data_size == 0) {
158 return g_base64_encode (data, data_size);
161 /** Set VST chunk from base64-encoded data.
162 * @param 0-terminated base64-encoded data.
163 * @param single true for single program, false for all programs.
164 * @return 0 on success, non-0 on failure
167 VSTPlugin::set_chunk (gchar const * data, bool single)
170 guchar* raw_data = g_base64_decode (data, &size);
171 int const r = _plugin->dispatcher (_plugin, 24 /* effSetChunk */, single ? 1 : 0, size, raw_data, 0);
177 VSTPlugin::get_state()
179 XMLNode *root = new XMLNode (state_node_name());
180 LocaleGuard lg (X_("POSIX"));
182 if (_fst->current_program != -1) {
184 snprintf (buf, sizeof (buf), "%d", _fst->current_program);
185 root->add_property ("current-program", buf);
188 if (_plugin->flags & 32 /* effFlagsProgramsChunks */) {
190 gchar* data = get_chunk (false);
195 /* store information */
197 XMLNode* chunk_node = new XMLNode (X_("chunk"));
199 chunk_node->add_content (data);
202 root->add_child_nocopy (*chunk_node);
206 XMLNode* parameters = new XMLNode ("parameters");
208 for (int32_t n = 0; n < _plugin->numParams; ++n) {
211 snprintf (index, sizeof (index), "param_%d", n);
212 snprintf (val, sizeof (val), "%.12g", _plugin->getParameter (_plugin, n));
213 parameters->add_property (index, val);
216 root->add_child_nocopy (*parameters);
223 VSTPlugin::set_state(const XMLNode& node, int)
225 LocaleGuard lg (X_("POSIX"));
227 if (node.name() != state_node_name()) {
228 error << _("Bad node sent to VSTPlugin::set_state") << endmsg;
232 const XMLProperty* prop;
234 if ((prop = node.property ("current-program")) != 0) {
235 _fst->current_program = atoi (prop->value().c_str());
241 if ((child = find_named_node (node, X_("chunk"))) != 0) {
243 XMLPropertyList::const_iterator i;
244 XMLNodeList::const_iterator n;
247 for (n = child->children ().begin (); n != child->children ().end (); ++n) {
248 if ((*n)->is_content ()) {
249 /* XXX: this may be dubious for the same reasons that we delay
250 execution of load_preset.
252 ret = set_chunk ((*n)->content().c_str(), false);
256 } else if ((child = find_named_node (node, X_("parameters"))) != 0) {
258 XMLPropertyList::const_iterator i;
260 for (i = child->properties().begin(); i != child->properties().end(); ++i) {
264 sscanf ((*i)->name().c_str(), "param_%d", ¶m);
265 sscanf ((*i)->value().c_str(), "%f", &val);
267 _plugin->setParameter (_plugin, param, val);
270 /* program number is not knowable */
272 _fst->current_program = -1;
282 VSTPlugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor& desc) const
284 VstParameterProperties prop;
286 desc.min_unbound = false;
287 desc.max_unbound = false;
290 if (_plugin->dispatcher (_plugin, effGetParameterProperties, which, 0, &prop, 0)) {
292 #ifdef VESTIGE_COMPLETE
294 /* i have yet to find or hear of a VST plugin that uses this */
296 if (prop.flags & kVstParameterUsesIntegerMinMax) {
297 desc.lower = prop.minInteger;
298 desc.upper = prop.maxInteger;
304 if (prop.flags & kVstParameterUsesIntStep) {
306 desc.step = prop.stepInteger;
307 desc.smallstep = prop.stepInteger;
308 desc.largestep = prop.stepInteger;
310 } else if (prop.flags & kVstParameterUsesFloatStep) {
312 desc.step = prop.stepFloat;
313 desc.smallstep = prop.smallStepFloat;
314 desc.largestep = prop.largeStepFloat;
318 float range = desc.upper - desc.lower;
320 desc.step = range / 100.0f;
321 desc.smallstep = desc.step / 2.0f;
322 desc.largestep = desc.step * 10.0f;
325 desc.toggled = prop.flags & kVstParameterIsSwitch;
326 desc.logarithmic = false;
327 desc.sr_dependent = false;
328 desc.label = prop.label;
338 _plugin->dispatcher (_plugin, effGetParamName, which, 0, label, 0);
341 desc.integer_step = false;
345 desc.smallstep = 0.005f;
346 desc.largestep = 0.1f;
347 desc.toggled = false;
348 desc.logarithmic = false;
349 desc.sr_dependent = false;
356 VSTPlugin::load_preset (const string& name)
358 if (_plugin->flags & 32 /* effFlagsProgramsChunks */) {
360 XMLTree* t = presets_tree ();
365 XMLNode* root = t->root ();
367 /* Load a user preset chunk from our XML file and send it via a circuitous route to the plugin */
369 for (XMLNodeList::const_iterator i = root->children().begin(); i != root->children().end(); ++i) {
370 assert ((*i)->name() == X_("ChunkPreset"));
372 XMLProperty* uri = (*i)->property (X_("uri"));
373 XMLProperty* label = (*i)->property (X_("label"));
378 if (label->value() == name) {
380 if (_fst->wanted_chunk) {
381 g_free (_fst->wanted_chunk);
384 for (XMLNodeList::const_iterator j = (*i)->children().begin(); j != (*i)->children().end(); ++j) {
385 if ((*j)->is_content ()) {
386 /* we can't dispatch directly here; too many plugins expect only one GUI thread */
388 guchar* raw_data = g_base64_decode ((*j)->content().c_str(), &size);
389 _fst->wanted_chunk = raw_data;
390 _fst->wanted_chunk_size = size;
391 _fst->want_chunk = 1;
405 VSTPlugin::do_save_preset (string name)
407 if (_plugin->flags & 32 /* effFlagsProgramsChunks */) {
409 XMLTree* t = presets_tree ();
414 /* Add a chunk to our XML file of user presets */
416 XMLNode* p = new XMLNode (X_("ChunkPreset"));
417 /* XXX: use of _presets.size() + 1 for the unique ID here is dubious at best */
418 string const uri = string_compose (X_("VST:%1:%2"), unique_id (), _presets.size() + 1);
419 p->add_property (X_("uri"), uri);
420 p->add_property (X_("label"), name);
421 gchar* data = get_chunk (true);
422 p->add_content (string (data));
424 t->root()->add_child_nocopy (*p);
426 sys::path f = ARDOUR::user_config_directory ();
430 t->write (f.to_string ());
439 VSTPlugin::do_remove_preset (string name)
441 if (_plugin->flags & 32 /* effFlagsProgramsChunks */) {
445 error << _("no support for presets using chunks at this time")
452 VSTPlugin::describe_parameter (Evoral::Parameter param)
455 _plugin->dispatcher (_plugin, effGetParamName, param.id(), 0, name, 0);
460 VSTPlugin::signal_latency () const
463 return _user_latency;
466 #ifdef VESTIGE_HEADER
467 return *((framecnt_t *) (((char *) &_plugin->flags) + 12)); /* initialDelay */
469 return _plugin->initial_delay;
473 set<Evoral::Parameter>
474 VSTPlugin::automatable () const
476 set<Evoral::Parameter> ret;
478 for (uint32_t i = 0; i < parameter_count(); ++i){
479 ret.insert (ret.end(), Evoral::Parameter(PluginAutomation, 0, i));
486 VSTPlugin::connect_and_run (BufferSet& bufs,
487 ChanMapping in_map, ChanMapping out_map,
488 pframes_t nframes, framecnt_t offset)
490 Plugin::connect_and_run (bufs, in_map, out_map, nframes, offset);
492 float *ins[_plugin->numInputs];
493 float *outs[_plugin->numOutputs];
496 const uint32_t nbufs = bufs.count().n_audio();
499 for (i = 0; i < (int32_t) _plugin->numInputs; ++i) {
500 ins[i] = bufs.get_audio(min((uint32_t) in_index, nbufs - 1)).data() + offset;
505 for (i = 0; i < (int32_t) _plugin->numOutputs; ++i) {
506 outs[i] = bufs.get_audio(min((uint32_t) out_index, nbufs - 1)).data() + offset;
508 /* unbelievably, several VST plugins still rely on Cubase
509 behaviour and do not silence the buffer in processReplacing
510 when they have no output.
513 // memset (outs[i], 0, sizeof (Sample) * nframes);
518 if (bufs.count().n_midi() > 0) {
519 VstEvents* v = bufs.get_vst_midi (0);
520 _plugin->dispatcher (_plugin, effProcessEvents, 0, 0, v, 0);
523 /* we already know it can support processReplacing */
525 _plugin->processReplacing (_plugin, ins, outs, nframes);
531 VSTPlugin::deactivate ()
533 _plugin->dispatcher (_plugin, effMainsChanged, 0, 0, NULL, 0.0f);
537 VSTPlugin::activate ()
539 _plugin->dispatcher (_plugin, effMainsChanged, 0, 1, NULL, 0.0f);
543 VSTPlugin::unique_id() const
547 #ifdef VESTIGE_HEADER
548 snprintf (buf, sizeof (buf), "%d", *((int32_t*) &_plugin->unused_id));
550 snprintf (buf, sizeof (buf), "%d", _plugin->uniqueID);
557 VSTPlugin::name () const
563 VSTPlugin::maker () const
565 return _info->creator.c_str();
569 VSTPlugin::label () const
575 VSTPlugin::parameter_count() const
577 return _plugin->numParams;
581 VSTPlugin::has_editor () const
583 return _plugin->flags & effFlagsHasEditor;
587 VSTPlugin::print_parameter (uint32_t param, char *buf, uint32_t len) const
591 _plugin->dispatcher (_plugin, 7 /* effGetParamDisplay */, param, 0, buf, 0);
593 if (buf[0] == '\0') {
598 while (*first_nonws && isspace (*first_nonws)) {
601 if (*first_nonws == '\0') {
605 memmove (buf, first_nonws, strlen (buf) - (first_nonws - buf) + 1);
609 VSTPluginInfo::load (Session& session)
614 if (Config->get_use_vst()) {
617 handle = fst_load(path.c_str());
619 if ( (int)handle == -1) {
620 error << string_compose(_("VST: cannot load module from \"%1\""), path) << endmsg;
622 plugin.reset (new VSTPlugin (session.engine(), session, handle));
625 error << _("You asked ardour to not use any VST plugins") << endmsg;
626 return PluginPtr ((Plugin*) 0);
629 plugin->set_info(PluginInfoPtr(new VSTPluginInfo(*this)));
633 catch (failed_constructor &err) {
634 return PluginPtr ((Plugin*) 0);
638 vector<Plugin::PresetRecord>
639 VSTPlugin::get_presets ()
641 vector<PresetRecord> p;
643 /* Built-in presets */
645 int const vst_version = _plugin->dispatcher (_plugin, effGetVstVersion, 0, 0, NULL, 0);
646 for (int i = 0; i < _plugin->numPrograms; ++i) {
647 PresetRecord r (string_compose (X_("VST:%1:%2"), unique_id (), i), "");
649 if (vst_version >= 2) {
651 _plugin->dispatcher (_plugin, 29, i, 0, buf, 0);
654 r.label = string_compose (_("Preset %1"), i);
658 _presets.insert (make_pair (r.uri, r));
661 /* User presets from our XML file */
663 XMLTree* t = presets_tree ();
666 XMLNode* root = t->root ();
667 for (XMLNodeList::const_iterator i = root->children().begin(); i != root->children().end(); ++i) {
669 assert ((*i)->name() == X_("ChunkPreset"));
671 XMLProperty* uri = (*i)->property (X_("uri"));
672 XMLProperty* label = (*i)->property (X_("label"));
677 PresetRecord r (uri->value(), label->value());
679 _presets.insert (make_pair (r.uri, r));
688 /** @return XMLTree with our user presets; could be a new one if no existing
689 * one was found, or 0 if one was present but badly-formatted.
692 VSTPlugin::presets_tree () const
694 XMLTree* t = new XMLTree;
696 sys::path p = ARDOUR::user_config_directory ();
699 if (!is_directory (p)) {
700 create_directory (p);
706 t->set_root (new XMLNode (X_("VSTPresets")));
710 t->set_filename (p.to_string ());
719 /** @return Index of the first user preset in our lists */
721 VSTPlugin::first_user_preset_index () const
723 return _plugin->numPrograms;
726 VSTPluginInfo::VSTPluginInfo()