2 Copyright (C) 2010 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.
21 #include "pbd/gstdio_compat.h"
23 #include <glibmm/fileutils.h>
24 #include <glibmm/miscutils.h>
25 #include <glibmm/convert.h>
27 #include "pbd/floating.h"
28 #include "pbd/locale_guard.h"
30 #include "ardour/vst_plugin.h"
31 #include "ardour/vestige/aeffectx.h"
32 #include "ardour/session.h"
33 #include "ardour/vst_types.h"
34 #include "ardour/filesystem_paths.h"
35 #include "ardour/audio_buffer.h"
41 using namespace ARDOUR;
43 VSTPlugin::VSTPlugin (AudioEngine& engine, Session& session, VSTHandle* handle)
44 : Plugin (engine, session)
50 , _transport_frame (0)
51 , _transport_speed (0.f)
52 , _eff_bypassed (false)
54 memset (&_timeInfo, 0, sizeof(_timeInfo));
57 VSTPlugin::VSTPlugin (const VSTPlugin& other)
59 , _handle (other._handle)
60 , _state (other._state)
61 , _plugin (other._plugin)
64 , _midi_out_buf (other._midi_out_buf)
65 , _transport_frame (0)
66 , _transport_speed (0.f)
67 , _parameter_defaults (other._parameter_defaults)
68 , _eff_bypassed (other._eff_bypassed)
70 memset (&_timeInfo, 0, sizeof(_timeInfo));
73 VSTPlugin::~VSTPlugin ()
79 VSTPlugin::open_plugin ()
81 _plugin = _state->plugin;
82 assert (_plugin->user == this); // should have been set by {mac_vst|fst|lxvst}_instantiate
84 _state->plugin->dispatcher (_plugin, effOpen, 0, 0, 0, 0);
85 _state->vst_version = _plugin->dispatcher (_plugin, effGetVstVersion, 0, 0, 0, 0);
89 VSTPlugin::init_plugin ()
91 /* set rate and blocksize */
92 _plugin->dispatcher (_plugin, effSetSampleRate, 0, 0, NULL, (float) _session.frame_rate());
93 _plugin->dispatcher (_plugin, effSetBlockSize, 0, _session.get_block_size(), NULL, 0.0f);
98 VSTPlugin::designated_bypass_port ()
100 if (_plugin->dispatcher (_plugin, effCanDo, 0, 0, const_cast<char*> ("bypass"), 0.0f) != 0) {
101 #ifdef ALLOW_VST_BYPASS_TO_FAIL // yet unused, see also plugin_insert.cc
102 return UINT32_MAX - 1; // emulate a port
104 /* check if plugin actually supports it,
105 * e.g. u-he Presswerk CanDo "bypass" but calling effSetBypass is a NO-OP.
106 * (presumably the plugin-author thinks hard-bypassing is a bad idea,
107 * particularly since the plugin itself provides a bypass-port)
109 intptr_t value = 0; // not bypassed
110 if (0 != _plugin->dispatcher (_plugin, 44 /*effSetBypass*/, 0, value, NULL, 0)) {
111 cerr << "Emulate VST Bypass Port for " << name() << endl; // XXX DEBUG
112 return UINT32_MAX - 1; // emulate a port
114 cerr << "Do *not* Emulate VST Bypass Port for " << name() << endl; // XXX DEBUG
122 VSTPlugin::deactivate ()
124 _plugin->dispatcher (_plugin, effMainsChanged, 0, 0, NULL, 0.0f);
128 VSTPlugin::activate ()
130 _plugin->dispatcher (_plugin, effMainsChanged, 0, 1, NULL, 0.0f);
134 VSTPlugin::set_block_size (pframes_t nframes)
137 _plugin->dispatcher (_plugin, effSetBlockSize, 0, nframes, NULL, 0.0f);
143 VSTPlugin::default_value (uint32_t which)
145 return _parameter_defaults[which];
149 VSTPlugin::get_parameter (uint32_t which) const
151 if (which == UINT32_MAX - 1) {
152 // ardour uses enable-semantics: 1: enabled, 0: bypassed
153 return _eff_bypassed ? 0.f : 1.f;
155 return _plugin->getParameter (_plugin, which);
159 VSTPlugin::set_parameter (uint32_t which, float newval)
161 if (which == UINT32_MAX - 1) {
162 // ardour uses enable-semantics: 1: enabled, 0: bypassed
163 intptr_t value = (newval <= 0.f) ? 1 : 0;
164 cerr << "effSetBypass " << value << endl; // XXX DEBUG
165 int rv = _plugin->dispatcher (_plugin, 44 /*effSetBypass*/, 0, value, NULL, 0);
167 _eff_bypassed = (value == 1);
169 cerr << "effSetBypass failed rv=" << rv << endl; // XXX DEBUG
170 #ifdef ALLOW_VST_BYPASS_TO_FAIL // yet unused, see also vst_plugin.cc
171 // emit signal.. hard un/bypass from here?!
177 float oldval = get_parameter (which);
179 if (PBD::floateq (oldval, newval, 1)) {
183 _plugin->setParameter (_plugin, which, newval);
185 float curval = get_parameter (which);
187 if (!PBD::floateq (curval, oldval, 1)) {
188 /* value has changed, follow rest of the notification path */
189 Plugin::set_parameter (which, newval);
194 VSTPlugin::parameter_changed_externally (uint32_t which, float value )
196 ParameterChangedExternally (which, value); /* EMIT SIGNAL */
197 Plugin::set_parameter (which, value);
202 VSTPlugin::nth_parameter (uint32_t n, bool& ok) const
208 /** Get VST chunk as base64-encoded data.
209 * @param single true for single program, false for all programs.
210 * @return 0-terminated base64-encoded data; must be passed to g_free () by caller.
213 VSTPlugin::get_chunk (bool single) const
216 int32_t data_size = _plugin->dispatcher (_plugin, 23 /* effGetChunk */, single ? 1 : 0, 0, &data, 0);
217 if (data_size == 0) {
221 return g_base64_encode (data, data_size);
224 /** Set VST chunk from base64-encoded data.
225 * @param 0-terminated base64-encoded data.
226 * @param single true for single program, false for all programs.
227 * @return 0 on success, non-0 on failure
230 VSTPlugin::set_chunk (gchar const * data, bool single)
234 guchar* raw_data = g_base64_decode (data, &size);
236 pthread_mutex_lock (&_state->state_lock);
237 r = _plugin->dispatcher (_plugin, 24 /* effSetChunk */, single ? 1 : 0, size, raw_data, 0);
238 pthread_mutex_unlock (&_state->state_lock);
245 VSTPlugin::add_state (XMLNode* root) const
249 if (_plugin->flags & 32 /* effFlagsProgramsChunks */) {
251 gchar* data = get_chunk (false);
256 /* store information */
258 XMLNode* chunk_node = new XMLNode (X_("chunk"));
260 chunk_node->add_content (data);
263 root->add_child_nocopy (*chunk_node);
267 XMLNode* parameters = new XMLNode ("parameters");
269 for (int32_t n = 0; n < _plugin->numParams; ++n) {
271 snprintf (index, sizeof (index), "param-%d", n);
272 parameters->set_property (index, _plugin->getParameter (_plugin, n));
275 root->add_child_nocopy (*parameters);
280 VSTPlugin::set_state (const XMLNode& node, int version)
285 #ifndef NO_PLUGIN_STATE
288 if ((child = find_named_node (node, X_("chunk"))) != 0) {
290 XMLPropertyList::const_iterator i;
291 XMLNodeList::const_iterator n;
293 for (n = child->children ().begin (); n != child->children ().end (); ++n) {
294 if ((*n)->is_content ()) {
295 /* XXX: this may be dubious for the same reasons that we delay
296 execution of load_preset.
298 ret = set_chunk ((*n)->content().c_str(), false);
302 } else if ((child = find_named_node (node, X_("parameters"))) != 0) {
304 XMLPropertyList::const_iterator i;
306 for (i = child->properties().begin(); i != child->properties().end(); ++i) {
309 sscanf ((*i)->name().c_str(), "param-%d", ¶m);
310 float value = string_to<float>((*i)->value());
312 _plugin->setParameter (_plugin, param, value);
320 Plugin::set_state (node, version);
325 VSTPlugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor& desc) const
327 VstParameterProperties prop;
329 memset (&prop, 0, sizeof (VstParameterProperties));
332 if (_plugin->dispatcher (_plugin, effGetParameterProperties, which, 0, &prop, 0)) {
334 /* i have yet to find or hear of a VST plugin that uses this */
335 /* RG: faust2vsti does use this :) */
337 if (prop.flags & kVstParameterUsesIntegerMinMax) {
338 desc.lower = prop.minInteger;
339 desc.upper = prop.maxInteger;
345 if (prop.flags & kVstParameterUsesIntStep) {
347 desc.step = prop.stepInteger;
348 desc.smallstep = prop.stepInteger;
349 desc.largestep = prop.stepInteger;
350 desc.integer_step = true;
352 } else if (prop.flags & kVstParameterUsesFloatStep) {
354 desc.step = prop.stepFloat;
355 desc.smallstep = prop.smallStepFloat;
356 desc.largestep = prop.largeStepFloat;
357 // desc.rangesteps = (desc.upper - desc.lower) / prop.smallStepFloat; // XXX
361 float range = desc.upper - desc.lower;
363 desc.step = range / 100.0f;
364 desc.smallstep = desc.step / 2.0f;
365 desc.largestep = desc.step * 10.0f;
368 if (strlen(prop.label) == 0) {
369 _plugin->dispatcher (_plugin, effGetParamName, which, 0, prop.label, 0);
372 desc.toggled = prop.flags & kVstParameterIsSwitch;
373 desc.label = Glib::locale_to_utf8 (prop.label);
379 char label[VestigeMaxLabelLen];
380 /* some VST plugins expect this buffer to be zero-filled */
381 memset (label, 0, sizeof (label));
383 _plugin->dispatcher (_plugin, effGetParamName, which, 0, label, 0);
385 desc.label = Glib::locale_to_utf8 (label);
389 desc.smallstep = 0.005f;
390 desc.largestep = 0.1f;
393 if (_parameter_defaults.find (which) == _parameter_defaults.end ()) {
394 _parameter_defaults[which] = get_parameter (which);
396 desc.normal = _parameter_defaults[which];
403 VSTPlugin::load_preset (PresetRecord r)
408 s = load_user_preset (r);
410 s = load_plugin_preset (r);
414 Plugin::load_preset (r);
421 VSTPlugin::load_plugin_preset (PresetRecord r)
423 /* This is a plugin-provided preset.
424 We can't dispatch directly here; too many plugins expects only one GUI thread.
427 /* Extract the index of this preset from the URI */
431 int const p = sscanf (r.uri.c_str(), "VST:%d:%d", &id, &index);
434 sscanf (r.uri.c_str(), "VST:%d:%d", &id, &index);
436 _state->want_program = index;
437 LoadPresetProgram (); /* EMIT SIGNAL */ /* used for macvst */
442 VSTPlugin::load_user_preset (PresetRecord r)
444 /* This is a user preset; we load it, and this code also knows about the
445 non-direct-dispatch thing.
448 boost::shared_ptr<XMLTree> t (presets_tree ());
453 XMLNode* root = t->root ();
455 for (XMLNodeList::const_iterator i = root->children().begin(); i != root->children().end(); ++i) {
457 (*i)->get_property (X_("label"), label);
459 if (label != r.label) {
463 if (_plugin->flags & 32 /* effFlagsProgramsChunks */) {
465 /* Load a user preset chunk from our XML file and send it via a circuitous route to the plugin */
467 if (_state->wanted_chunk) {
468 g_free (_state->wanted_chunk);
471 for (XMLNodeList::const_iterator j = (*i)->children().begin(); j != (*i)->children().end(); ++j) {
472 if ((*j)->is_content ()) {
473 /* we can't dispatch directly here; too many plugins expect only one GUI thread */
475 guchar* raw_data = g_base64_decode ((*j)->content().c_str(), &size);
476 _state->wanted_chunk = raw_data;
477 _state->wanted_chunk_size = size;
478 _state->want_chunk = 1;
479 LoadPresetProgram (); /* EMIT SIGNAL */ /* used for macvst */
488 for (XMLNodeList::const_iterator j = (*i)->children().begin(); j != (*i)->children().end(); ++j) {
489 if ((*j)->name() == X_("Parameter")) {
493 if (!(*j)->get_property (X_("index"), index) ||
494 !(*j)->get_property (X_("value"), value)) {
495 // flag error and continue?
499 set_parameter (index, value);
500 PresetPortSetValue (index, value); /* EMIT SIGNAL */
512 VSTPlugin::do_save_preset (string name)
514 boost::shared_ptr<XMLTree> t (presets_tree ());
519 // prevent dups -- just in case
520 t->root()->remove_nodes_and_delete (X_("label"), name);
525 snprintf (tmp, 31, "%ld", _presets.size() + 1);
531 sha1_write (&s, (const uint8_t *) name.c_str(), name.size ());
532 sha1_write (&s, (const uint8_t *) tmp, strlen(tmp));
533 sha1_result_hash (&s, hash);
535 string const uri = string_compose (X_("VST:%1:x%2"), unique_id (), hash);
537 if (_plugin->flags & 32 /* effFlagsProgramsChunks */) {
539 p = new XMLNode (X_("ChunkPreset"));
540 p->set_property (X_("uri"), uri);
541 p->set_property (X_("label"), name);
542 gchar* data = get_chunk (true);
543 p->add_content (string (data));
548 p = new XMLNode (X_("Preset"));
549 p->set_property (X_("uri"), uri);
550 p->set_property (X_("label"), name);
552 for (uint32_t i = 0; i < parameter_count(); ++i) {
553 if (parameter_is_input (i)) {
554 XMLNode* c = new XMLNode (X_("Parameter"));
555 c->set_property (X_("index"), i);
556 c->set_property (X_("value"), get_parameter (i));
557 p->add_child_nocopy (*c);
562 t->root()->add_child_nocopy (*p);
564 std::string f = Glib::build_filename (ARDOUR::user_config_directory (), "presets");
565 f = Glib::build_filename (f, presets_file ());
572 VSTPlugin::do_remove_preset (string name)
574 boost::shared_ptr<XMLTree> t (presets_tree ());
579 t->root()->remove_nodes_and_delete (X_("label"), name);
581 std::string f = Glib::build_filename (ARDOUR::user_config_directory (), "presets");
582 f = Glib::build_filename (f, presets_file ());
588 VSTPlugin::describe_parameter (Evoral::Parameter param)
590 char name[VestigeMaxLabelLen];
591 if (param.id() == UINT32_MAX - 1) {
592 strcpy (name, _("Plugin Enable"));
596 memset (name, 0, sizeof (name));
598 /* some VST plugins expect this buffer to be zero-filled */
600 _plugin->dispatcher (_plugin, effGetParamName, param.id(), 0, name, 0);
602 if (name[0] == '\0') {
603 strcpy (name, _("Unknown"));
610 VSTPlugin::signal_latency () const
613 return _user_latency;
616 #if ( defined(__x86_64__) || defined(_M_X64) )
617 return *((int32_t *) (((char *) &_plugin->flags) + 24)); /* initialDelay */
619 return *((int32_t *) (((char *) &_plugin->flags) + 12)); /* initialDelay */
623 set<Evoral::Parameter>
624 VSTPlugin::automatable () const
626 set<Evoral::Parameter> ret;
628 for (uint32_t i = 0; i < parameter_count(); ++i) {
629 ret.insert (ret.end(), Evoral::Parameter(PluginAutomation, 0, i));
636 VSTPlugin::connect_and_run (BufferSet& bufs,
637 framepos_t start, framepos_t end, double speed,
638 ChanMapping in_map, ChanMapping out_map,
639 pframes_t nframes, framecnt_t offset)
641 Plugin::connect_and_run(bufs, start, end, speed, in_map, out_map, nframes, offset);
643 if (pthread_mutex_trylock (&_state->state_lock)) {
644 /* by convention 'effSetChunk' should not be called while processing
645 * http://www.reaper.fm/sdk/vst/vst_ext.php
647 * All VSTs don't use in-place, PluginInsert::connect_and_run()
648 * does clear output buffers, so we can just return.
653 _transport_frame = start;
654 _transport_speed = speed;
656 ChanCount bufs_count;
657 bufs_count.set(DataType::AUDIO, 1);
658 bufs_count.set(DataType::MIDI, 1);
661 BufferSet& silent_bufs = _session.get_silent_buffers(bufs_count);
662 BufferSet& scratch_bufs = _session.get_scratch_buffers(bufs_count);
664 /* VC++ doesn't support the C99 extension that allows
666 typeName foo[variableDefiningSize];
668 Use alloca instead of dynamic array (rather than std::vector which
669 allocs on the heap) because this is realtime code.
672 float** ins = (float**)alloca(_plugin->numInputs*sizeof(float*));
673 float** outs = (float**)alloca(_plugin->numOutputs*sizeof(float*));
677 uint32_t in_index = 0;
678 for (i = 0; i < (int32_t) _plugin->numInputs; ++i) {
681 index = in_map.get(DataType::AUDIO, in_index++, &valid);
683 ? bufs.get_audio(index).data(offset)
684 : silent_bufs.get_audio(0).data(offset);
687 uint32_t out_index = 0;
688 for (i = 0; i < (int32_t) _plugin->numOutputs; ++i) {
691 index = out_map.get(DataType::AUDIO, out_index++, &valid);
693 ? bufs.get_audio(index).data(offset)
694 : scratch_bufs.get_audio(0).data(offset);
697 if (bufs.count().n_midi() > 0) {
700 const uint32_t buf_index_in = in_map.get(DataType::MIDI, 0, &valid);
702 v = bufs.get_vst_midi (buf_index_in);
705 const uint32_t buf_index_out = out_map.get(DataType::MIDI, 0, &valid);
707 _midi_out_buf = &bufs.get_midi(buf_index_out);
708 _midi_out_buf->silence(0, 0);
713 _plugin->dispatcher (_plugin, effProcessEvents, 0, 0, v, 0);
717 /* we already know it can support processReplacing */
718 _plugin->processReplacing (_plugin, &ins[0], &outs[0], nframes);
721 pthread_mutex_unlock (&_state->state_lock);
726 VSTPlugin::unique_id () const
730 snprintf (buf, sizeof (buf), "%d", _plugin->uniqueID);
737 VSTPlugin::name () const
739 if (!_info->name.empty ()) {
740 return _info->name.c_str();
742 return _handle->name;
746 VSTPlugin::maker () const
748 return _info->creator.c_str();
752 VSTPlugin::label () const
754 return _handle->name;
758 VSTPlugin::parameter_count () const
760 return _plugin->numParams;
764 VSTPlugin::has_editor () const
766 return _plugin->flags & effFlagsHasEditor;
770 VSTPlugin::print_parameter (uint32_t param, char *buf, uint32_t /*len*/) const
774 _plugin->dispatcher (_plugin, 7 /* effGetParamDisplay */, param, 0, buf, 0);
776 if (buf[0] == '\0') {
781 while (*first_nonws && isspace (*first_nonws)) {
785 if (*first_nonws == '\0') {
789 memmove (buf, first_nonws, strlen (buf) - (first_nonws - buf) + 1);
793 VSTPlugin::find_presets ()
795 /* Built-in presets */
797 int const vst_version = _plugin->dispatcher (_plugin, effGetVstVersion, 0, 0, NULL, 0);
798 for (int i = 0; i < _plugin->numPrograms; ++i) {
799 PresetRecord r (string_compose (X_("VST:%1:%2"), unique_id (), i), "", false);
801 if (vst_version >= 2) {
803 if (_plugin->dispatcher (_plugin, 29, i, 0, buf, 0) == 1) {
806 r.label = string_compose (_("Preset %1"), i);
809 r.label = string_compose (_("Preset %1"), i);
812 _presets.insert (make_pair (r.uri, r));
815 /* User presets from our XML file */
817 boost::shared_ptr<XMLTree> t (presets_tree ());
820 XMLNode* root = t->root ();
821 for (XMLNodeList::const_iterator i = root->children().begin(); i != root->children().end(); ++i) {
825 if (!(*i)->get_property (X_("uri"), uri) || !(*i)->get_property (X_("label"), label)) {
829 PresetRecord r (uri, label, true);
830 _presets.insert (make_pair (r.uri, r));
836 /** @return XMLTree with our user presets; could be a new one if no existing
837 * one was found, or 0 if one was present but badly-formatted.
840 VSTPlugin::presets_tree () const
842 XMLTree* t = new XMLTree;
844 std::string p = Glib::build_filename (ARDOUR::user_config_directory (), "presets");
846 if (!Glib::file_test (p, Glib::FILE_TEST_IS_DIR)) {
847 if (g_mkdir_with_parents (p.c_str(), 0755) != 0) {
848 error << _("Unable to make VST presets directory") << endmsg;
852 p = Glib::build_filename (p, presets_file ());
854 if (!Glib::file_test (p, Glib::FILE_TEST_EXISTS)) {
855 t->set_root (new XMLNode (X_("VSTPresets")));
868 /** @return Index of the first user preset in our lists */
870 VSTPlugin::first_user_preset_index () const
872 return _plugin->numPrograms;
876 VSTPlugin::presets_file () const
878 return string("vst-") + unique_id ();