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.
20 /**********************************************************************/
21 /*Native linuxVST (LXVST) variant of vst_plugin.cc etc */
22 /**********************************************************************/
31 #include <cstdio> // so libraptor doesn't complain
34 #include <cstring> // for memmove
38 #include <glibmm/miscutils.h>
42 /*Include for the new native vst engine - vstfx.h*/
44 #include <ardour/vstfx.h>
46 #include "pbd/compose.h"
47 #include "pbd/error.h"
48 #include "pbd/pathscanner.h"
49 #include "pbd/xml++.h"
51 #include "ardour/session.h"
52 #include "ardour/audioengine.h"
53 #include "ardour/filesystem_paths.h"
54 #include "ardour/lxvst_plugin.h"
55 #include "ardour/buffer_set.h"
56 #include "ardour/audio_buffer.h"
57 #include "ardour/midi_buffer.h"
59 #include "pbd/stl_delete.h"
65 using namespace ARDOUR;
70 LXVSTPlugin::LXVSTPlugin (AudioEngine& e, Session& session, VSTFXHandle* h)
75 /*Instantiate the plugin and return a VSTFX* */
77 if ((_vstfx = vstfx_instantiate (handle, Session::lxvst_callback, this)) == 0) {
78 throw failed_constructor();
81 /* Call into vstfx to get a pointer to the instance of the VST plugin*/
83 _plugin = _vstfx->plugin;
86 /* set rate and blocksize */
88 _plugin->dispatcher (_plugin, effSetSampleRate, 0, 0, NULL, (float) session.frame_rate());
90 _plugin->dispatcher (_plugin, effSetBlockSize, 0, session.get_block_size(), NULL, 0.0f);
92 /* set program to zero */
94 _plugin->dispatcher (_plugin, effSetProgram, 0, 0, NULL, 0.0f);
96 // Plugin::setup_controls ();
99 LXVSTPlugin::LXVSTPlugin (const LXVSTPlugin &other)
102 handle = other.handle;
104 if ((_vstfx = vstfx_instantiate (handle, Session::lxvst_callback, this)) == 0) {
105 throw failed_constructor();
107 _plugin = _vstfx->plugin;
109 // Plugin::setup_controls ();
112 LXVSTPlugin::~LXVSTPlugin ()
115 vstfx_close (_vstfx);
119 LXVSTPlugin::set_block_size (pframes_t nframes)
122 _plugin->dispatcher (_plugin, effSetBlockSize, 0, nframes, NULL, 0.0f);
128 LXVSTPlugin::default_value (uint32_t port)
134 LXVSTPlugin::set_parameter (uint32_t which, float val)
136 _plugin->setParameter (_plugin, which, val);
138 if (_vstfx->want_program == -1 && _vstfx->want_chunk == 0) {
139 /* Heinous hack: Plugin::set_parameter below updates the `modified' status of the
140 current preset, but if _vstfx->want_program is not -1 then there is a preset
141 setup pending or in progress, which we don't want any `modified' updates
142 to happen for. So we only do this if _vstfx->want_program is -1.
144 Plugin::set_parameter (which, val);
149 LXVSTPlugin::get_parameter (uint32_t which) const
151 return _plugin->getParameter (_plugin, which);
156 LXVSTPlugin::nth_parameter (uint32_t n, bool& ok) const
162 /** Get VST chunk as base64-encoded data.
163 * @param single true for single program, false for all programs.
164 * @return 0-terminated base64-encoded data; must be passed to g_free () by caller.
166 gchar *LXVSTPlugin::get_chunk (bool single) const
169 int32_t data_size = _plugin->dispatcher (_plugin, 23 /* effGetChunk */, single ? 1 : 0, 0, &data, 0);
171 if (data_size == 0) {
175 return g_base64_encode (data, data_size);
178 /** Set VST chunk from base64-encoded data.
179 * @param 0-terminated base64-encoded data.
180 * @param single true for single program, false for all programs.
181 * @return 0 on success, non-0 on failure
184 LXVSTPlugin::set_chunk (gchar const * data, bool single)
187 guchar* raw_data = g_base64_decode (data, &size);
188 int const r = _plugin->dispatcher (_plugin, 24 /* effSetChunk */, single ? 1 : 0, size, raw_data, 0);
194 LXVSTPlugin::add_state (XMLNode* root) const
196 LocaleGuard lg (X_("POSIX"));
198 if (_vstfx->current_program != -1) {
200 snprintf (buf, sizeof (buf), "%d", _vstfx->current_program);
201 root->add_property ("current-program", buf);
204 if (_plugin->flags & 32 /* effFlagsProgramsChunks */) {
205 gchar* data = get_chunk (false);
211 /* store information */
213 XMLNode* chunk_node = new XMLNode (X_("chunk"));
215 chunk_node->add_content (data);
218 root->add_child_nocopy (*chunk_node);
221 XMLNode* parameters = new XMLNode ("parameters");
223 for (int32_t n = 0; n < _plugin->numParams; ++n) {
226 snprintf (index, sizeof (index), "param_%d", n);
227 snprintf (val, sizeof (val), "%.12g", _plugin->getParameter (_plugin, n));
228 parameters->add_property (index, val);
231 root->add_child_nocopy (*parameters);
236 LXVSTPlugin::set_state (const XMLNode& node, int version)
238 LocaleGuard lg (X_("POSIX"));
240 if (node.name() != state_node_name()) {
241 error << _("Bad node sent to VSTPlugin::set_state") << endmsg;
245 const XMLProperty* prop;
247 if ((prop = node.property ("current-program")) != 0) {
248 _vstfx->want_program = atoi (prop->value().c_str());
254 if ((child = find_named_node (node, X_("chunk"))) != 0) {
255 XMLPropertyList::const_iterator i;
256 XMLNodeList::const_iterator n;
259 for (n = child->children ().begin (); n != child->children ().end (); ++n) {
260 if ((*n)->is_content ()) {
261 /* XXX: this may be dubious for the same reasons that we delay
262 execution of load_preset.
264 ret = set_chunk ((*n)->content().c_str(), false);
268 } else if ((child = find_named_node (node, X_("parameters"))) != 0) {
269 XMLPropertyList::const_iterator i;
271 for (i = child->properties().begin(); i != child->properties().end(); ++i) {
275 sscanf ((*i)->name().c_str(), "param-%d", ¶m); //This was param_%d (from vst_plugin) which caused all sorts of odd behaviour
276 sscanf ((*i)->value().c_str(), "%f", &val);
278 _plugin->setParameter (_plugin, param, val);
281 /* program number is not knowable */
283 _vstfx->current_program = -1;
288 Plugin::set_state (node, version);
293 LXVSTPlugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor& desc) const
295 VstParameterProperties prop;
297 desc.min_unbound = false;
298 desc.max_unbound = false;
306 _plugin->dispatcher (_plugin, effGetParamName, which, 0, label, 0);
309 desc.integer_step = false;
313 desc.smallstep = 0.005f;
314 desc.largestep = 0.1f;
315 desc.toggled = false;
316 desc.logarithmic = false;
317 desc.sr_dependent = false;
323 LXVSTPlugin::load_preset (PresetRecord r)
328 s = load_user_preset (r);
331 s = load_plugin_preset (r);
335 Plugin::load_preset (r);
342 LXVSTPlugin::load_plugin_preset (PresetRecord r)
344 /* This is a plugin-provided preset.
345 We can't dispatch directly here; too many plugins expects only one GUI thread.
348 /* Extract the index of this preset from the URI */
351 int const p = sscanf (r.uri.c_str(), "VST:%d:%d", &id, &index);
354 _vstfx->want_program = index;
359 LXVSTPlugin::load_user_preset (PresetRecord r)
361 /* This is a user preset; we load it, and this code also knows about the
362 non-direct-dispatch thing.
365 boost::shared_ptr<XMLTree> t (presets_tree ());
370 XMLNode* root = t->root ();
372 for (XMLNodeList::const_iterator i = root->children().begin(); i != root->children().end(); ++i) {
373 XMLProperty* uri = (*i)->property (X_("uri"));
374 XMLProperty* label = (*i)->property (X_("label"));
379 if (label->value() != r.label) {
383 if (_plugin->flags & 32 /* effFlagsProgramsChunks */) {
385 /* Load a user preset chunk from our XML file and send it via a circuitous route to the plugin */
387 if (_vstfx->wanted_chunk) {
388 g_free (_vstfx->wanted_chunk);
391 for (XMLNodeList::const_iterator j = (*i)->children().begin(); j != (*i)->children().end(); ++j) {
392 if ((*j)->is_content ()) {
393 /* we can't dispatch directly here; too many plugins expect only one GUI thread */
395 guchar* raw_data = g_base64_decode ((*j)->content().c_str(), &size);
396 _vstfx->wanted_chunk = raw_data;
397 _vstfx->wanted_chunk_size = size;
398 _vstfx->want_chunk = 1;
407 for (XMLNodeList::const_iterator j = (*i)->children().begin(); j != (*i)->children().end(); ++j) {
408 if ((*j)->name() == X_("Parameter")) {
409 XMLProperty* index = (*j)->property (X_("index"));
410 XMLProperty* value = (*j)->property (X_("value"));
415 set_parameter (atoi (index->value().c_str()), atof (value->value().c_str ()));
425 LXVSTPlugin::do_save_preset (string name)
427 boost::shared_ptr<XMLTree> t (presets_tree ());
433 /* XXX: use of _presets.size() + 1 for the unique ID here is dubious at best */
434 string const uri = string_compose (X_("VST:%1:%2"), unique_id (), _presets.size() + 1);
436 if (_plugin->flags & 32 /* effFlagsProgramsChunks */) {
438 p = new XMLNode (X_("ChunkPreset"));
439 p->add_property (X_("uri"), uri);
440 p->add_property (X_("label"), name);
441 gchar* data = get_chunk (true);
442 p->add_content (string (data));
448 p = new XMLNode (X_("Preset"));
449 p->add_property (X_("uri"), uri);
450 p->add_property (X_("label"), name);
452 for (uint32_t i = 0; i < parameter_count(); ++i) {
453 if (parameter_is_input (i)) {
454 XMLNode* c = new XMLNode (X_("Parameter"));
455 c->add_property (X_("index"), string_compose ("%1", i));
456 c->add_property (X_("value"), string_compose ("%1", get_parameter (i)));
457 p->add_child_nocopy (*c);
462 t->root()->add_child_nocopy (*p);
464 sys::path f = ARDOUR::user_config_directory ();
466 f /= presets_file ();
468 t->write (f.to_string ());
473 LXVSTPlugin::do_remove_preset (string name)
475 boost::shared_ptr<XMLTree> t (presets_tree ());
480 t->root()->remove_nodes_and_delete (X_("label"), name);
482 sys::path f = ARDOUR::user_config_directory ();
484 f /= presets_file ();
486 t->write (f.to_string ());
490 LXVSTPlugin::describe_parameter (Evoral::Parameter param)
492 char name[64] = "Unkown";
493 _plugin->dispatcher (_plugin, effGetParamName, param.id(), 0, name, 0);
498 LXVSTPlugin::signal_latency () const
501 return _user_latency;
504 #ifdef VESTIGE_HEADER
505 return *((framecnt_t *) (((char *) &_plugin->flags) + 12)); /* initialDelay */
507 return _plugin->initial_delay;
511 set<Evoral::Parameter>
512 LXVSTPlugin::automatable () const
514 set<Evoral::Parameter> ret;
516 for (uint32_t i = 0; i < parameter_count(); ++i) {
517 ret.insert (ret.end(), Evoral::Parameter(PluginAutomation, 0, i));
524 LXVSTPlugin::connect_and_run (BufferSet& bufs,
525 ChanMapping in_map, ChanMapping out_map,
526 pframes_t nframes, framecnt_t offset)
528 Plugin::connect_and_run (bufs, in_map, out_map, nframes, offset);
530 float *ins[_plugin->numInputs];
531 float *outs[_plugin->numOutputs];
534 const uint32_t nbufs = bufs.count().n_audio();
537 for (i = 0; i < (int32_t) _plugin->numInputs; ++i) {
538 ins[i] = bufs.get_audio(min((uint32_t) in_index, nbufs - 1)).data() + offset;
543 for (i = 0; i < (int32_t) _plugin->numOutputs; ++i) {
544 outs[i] = bufs.get_audio(min((uint32_t) out_index, nbufs - 1)).data() + offset;
548 if (bufs.count().n_midi() > 0) {
549 VstEvents* v = bufs.get_vst_midi (0);
550 _plugin->dispatcher (_plugin, effProcessEvents, 0, 0, v, 0);
553 /* we already know it can support processReplacing */
556 _plugin->processReplacing (_plugin, ins, outs, nframes);
559 #if defined LXVST_64BIT && defined VESTIGE_HEADER
561 /* Vestige doesn't work for 64Bit - some of the AEffect struct member types
562 appear not to be correct which throws the data alignment. We have two choices
564 1) Fix Vestige - preferable from a technical standpoint, but perhaps
565 not viable without affecting its 'clean room' status
567 2) Correct for the alignment error - a bit of a kludge, but it can work,
568 assuming the following data types / sizes on x86-64
570 char 1Byte : Byte aligned
571 int 4Bytes : 4Byte aligned
572 long 8Bytes : 8Byte aligned
573 pointers 8Bytes : 8Byte aligned
575 This gives an offset of 8 Bytes - inclusive of padding
576 to translate to the correct address for processReplacing
579 ((AEffect*)(((char*)(_plugin)) + 8))->processReplacing(_plugin, ins, outs, nframes);
581 #elif defined LXVST_64BIT
583 _plugin->processReplacing(_plugin, ins, outs, nframes);
591 LXVSTPlugin::deactivate ()
593 _plugin->dispatcher (_plugin, effMainsChanged, 0, 0, NULL, 0.0f);
597 LXVSTPlugin::activate ()
599 _plugin->dispatcher (_plugin, effMainsChanged, 0, 1, NULL, 0.0f);
603 LXVSTPlugin::unique_id() const
607 #if defined LXVST_64BIT && defined VESTIGE_HEADER
609 /*The vestige header appears not to have correct data
610 alignment in AEffect struct for 64Bit, possibly due
611 to incorrect data types - see previous comments*/
613 snprintf (buf, sizeof (buf), "%d", *((int32_t*) &((AEffect*)(((char*)(_plugin)) + 12))->unused_id));
615 #elif defined LXVST_32BIT && defined VESTIGE_HEADER
617 snprintf (buf, sizeof (buf), "%d", *((int32_t*) &_plugin->unused_id));
621 snprintf (buf, sizeof (buf), "%d", _plugin->uniqueID);
630 LXVSTPlugin::name () const
636 LXVSTPlugin::maker () const
638 return _info->creator.c_str();
642 LXVSTPlugin::label () const
648 LXVSTPlugin::parameter_count() const
650 return _plugin->numParams;
654 LXVSTPlugin::has_editor () const
656 return _plugin->flags & effFlagsHasEditor;
660 LXVSTPlugin::print_parameter (uint32_t param, char *buf, uint32_t len) const
664 _plugin->dispatcher (_plugin, 7 /* effGetParamDisplay */, param, 0, buf, 0);
666 if (buf[0] == '\0') {
671 while (*first_nonws && isspace (*first_nonws)) {
675 if (*first_nonws == '\0') {
679 memmove (buf, first_nonws, strlen (buf) - (first_nonws - buf) + 1);
683 LXVSTPluginInfo::load (Session& session)
688 if (Config->get_use_lxvst()) {
691 handle = vstfx_load(path.c_str());
693 if (handle == NULL) {
694 error << string_compose(_("LXVST: cannot load module from \"%1\""), path) << endmsg;
697 plugin.reset (new LXVSTPlugin (session.engine(), session, handle));
701 error << _("You asked ardour to not use any LXVST plugins") << endmsg;
702 return PluginPtr ((Plugin*) 0);
705 plugin->set_info(PluginInfoPtr(new LXVSTPluginInfo(*this)));
709 catch (failed_constructor &err) {
710 return PluginPtr ((Plugin*) 0);
715 LXVSTPlugin::find_presets ()
717 /* Built-in presets */
719 int const vst_version = _plugin->dispatcher (_plugin, effGetVstVersion, 0, 0, NULL, 0);
721 for (int i = 0; i < _plugin->numPrograms; ++i) {
722 PresetRecord r (string_compose (X_("LXVST:%1:%2"), unique_id (), i), "", false);
724 if (vst_version >= 2) {
726 if (_plugin->dispatcher (_plugin, 29, i, 0, buf, 0) == 1) {
729 r.label = string_compose (_("Preset %1"), i);
732 r.label = string_compose (_("Preset %1"), i);
735 _presets.insert (make_pair (r.uri, r));
738 /* User presets from our XML file */
740 boost::shared_ptr<XMLTree> t (presets_tree ());
743 XMLNode* root = t->root ();
744 for (XMLNodeList::const_iterator i = root->children().begin(); i != root->children().end(); ++i) {
745 XMLProperty* uri = (*i)->property (X_("uri"));
746 XMLProperty* label = (*i)->property (X_("label"));
751 PresetRecord r (uri->value(), label->value(), true);
752 _presets.insert (make_pair (r.uri, r));
757 /** @return XMLTree with our user presets; could be a new one if no existing
758 * one was found, or 0 if one was present but badly-formatted.
761 LXVSTPlugin::presets_tree () const
763 XMLTree* t = new XMLTree;
765 sys::path p = ARDOUR::user_config_directory ();
768 if (!is_directory (p)) {
769 create_directory (p);
772 p /= presets_file ();
775 t->set_root (new XMLNode (X_("LXVSTPresets")));
779 t->set_filename (p.to_string ());
788 /** @return Index of the first user preset in our lists */
790 LXVSTPlugin::first_user_preset_index () const
792 return _plugin->numPrograms;
795 string LXVSTPlugin::presets_file () const
797 return string_compose ("lxvst-%1", unique_id ());
800 LXVSTPluginInfo::LXVSTPluginInfo()
802 type = ARDOUR::LXVST;