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)
66 , _have_pending_stop_events (false)
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)
93 , _have_pending_stop_events (false)
95 handle = other.handle;
97 if ((_fst = fst_instantiate (handle, Session::vst_callback, this)) == 0) {
98 throw failed_constructor();
100 _plugin = _fst->plugin;
102 // Plugin::setup_controls ();
105 VSTPlugin::~VSTPlugin ()
112 VSTPlugin::set_block_size (pframes_t nframes)
115 _plugin->dispatcher (_plugin, effSetBlockSize, 0, nframes, NULL, 0.0f);
121 VSTPlugin::default_value (uint32_t port)
127 VSTPlugin::set_parameter (uint32_t which, float val)
129 _plugin->setParameter (_plugin, which, val);
130 //ParameterChanged (which, val); /* EMIT SIGNAL */
134 VSTPlugin::get_parameter (uint32_t which) const
136 return _plugin->getParameter (_plugin, which);
141 VSTPlugin::nth_parameter (uint32_t n, bool& ok) const
147 /** Get VST chunk as base64-encoded data.
148 * @param single true for single program, false for all programs.
149 * @return 0-terminated base64-encoded data; must be passed to g_free () by caller.
152 VSTPlugin::get_chunk (bool single)
155 int32_t data_size = _plugin->dispatcher (_plugin, 23 /* effGetChunk */, single ? 1 : 0, 0, &data, 0);
156 if (data_size == 0) {
160 return g_base64_encode (data, data_size);
163 /** Set VST chunk from base64-encoded data.
164 * @param 0-terminated base64-encoded data.
165 * @param single true for single program, false for all programs.
166 * @return 0 on success, non-0 on failure
169 VSTPlugin::set_chunk (gchar const * data, bool single)
172 guchar* raw_data = g_base64_decode (data, &size);
173 int const r = _plugin->dispatcher (_plugin, 24 /* effSetChunk */, single ? 1 : 0, size, raw_data, 0);
179 VSTPlugin::get_state()
181 XMLNode *root = new XMLNode (state_node_name());
182 LocaleGuard lg (X_("POSIX"));
184 if (_fst->current_program != -1) {
186 snprintf (buf, sizeof (buf), "%d", _fst->current_program);
187 root->add_property ("current-program", buf);
190 if (_plugin->flags & 32 /* effFlagsProgramsChunks */) {
192 gchar* data = get_chunk (false);
197 /* store information */
199 XMLNode* chunk_node = new XMLNode (X_("chunk"));
201 chunk_node->add_content (data);
204 root->add_child_nocopy (*chunk_node);
208 XMLNode* parameters = new XMLNode ("parameters");
210 for (int32_t n = 0; n < _plugin->numParams; ++n) {
213 snprintf (index, sizeof (index), "param_%d", n);
214 snprintf (val, sizeof (val), "%.12g", _plugin->getParameter (_plugin, n));
215 parameters->add_property (index, val);
218 root->add_child_nocopy (*parameters);
225 VSTPlugin::set_state(const XMLNode& node, int)
227 LocaleGuard lg (X_("POSIX"));
229 if (node.name() != state_node_name()) {
230 error << _("Bad node sent to VSTPlugin::set_state") << endmsg;
234 const XMLProperty* prop;
236 if ((prop = node.property ("current-program")) != 0) {
237 _fst->current_program = atoi (prop->value().c_str());
243 if ((child = find_named_node (node, X_("chunk"))) != 0) {
245 XMLPropertyList::const_iterator i;
246 XMLNodeList::const_iterator n;
249 for (n = child->children ().begin (); n != child->children ().end (); ++n) {
250 if ((*n)->is_content ()) {
251 /* XXX: this may be dubious for the same reasons that we delay
252 execution of load_preset.
254 ret = set_chunk ((*n)->content().c_str(), false);
258 } else if ((child = find_named_node (node, X_("parameters"))) != 0) {
260 XMLPropertyList::const_iterator i;
262 for (i = child->properties().begin(); i != child->properties().end(); ++i) {
266 sscanf ((*i)->name().c_str(), "param_%d", ¶m);
267 sscanf ((*i)->value().c_str(), "%f", &val);
269 _plugin->setParameter (_plugin, param, val);
272 /* program number is not knowable */
274 _fst->current_program = -1;
284 VSTPlugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor& desc) const
286 VstParameterProperties prop;
288 desc.min_unbound = false;
289 desc.max_unbound = false;
292 if (_plugin->dispatcher (_plugin, effGetParameterProperties, which, 0, &prop, 0)) {
294 #ifdef VESTIGE_COMPLETE
296 /* i have yet to find or hear of a VST plugin that uses this */
298 if (prop.flags & kVstParameterUsesIntegerMinMax) {
299 desc.lower = prop.minInteger;
300 desc.upper = prop.maxInteger;
306 if (prop.flags & kVstParameterUsesIntStep) {
308 desc.step = prop.stepInteger;
309 desc.smallstep = prop.stepInteger;
310 desc.largestep = prop.stepInteger;
312 } else if (prop.flags & kVstParameterUsesFloatStep) {
314 desc.step = prop.stepFloat;
315 desc.smallstep = prop.smallStepFloat;
316 desc.largestep = prop.largeStepFloat;
320 float range = desc.upper - desc.lower;
322 desc.step = range / 100.0f;
323 desc.smallstep = desc.step / 2.0f;
324 desc.largestep = desc.step * 10.0f;
327 desc.toggled = prop.flags & kVstParameterIsSwitch;
328 desc.logarithmic = false;
329 desc.sr_dependent = false;
330 desc.label = prop.label;
340 _plugin->dispatcher (_plugin, effGetParamName, which, 0, label, 0);
343 desc.integer_step = false;
347 desc.smallstep = 0.005f;
348 desc.largestep = 0.1f;
349 desc.toggled = false;
350 desc.logarithmic = false;
351 desc.sr_dependent = false;
358 VSTPlugin::load_preset (const string& name)
360 if (_plugin->flags & 32 /* effFlagsProgramsChunks */) {
362 XMLTree* t = presets_tree ();
367 XMLNode* root = t->root ();
369 /* Load a user preset chunk from our XML file and send it via a circuitous route to the plugin */
371 for (XMLNodeList::const_iterator i = root->children().begin(); i != root->children().end(); ++i) {
372 assert ((*i)->name() == X_("ChunkPreset"));
374 XMLProperty* uri = (*i)->property (X_("uri"));
375 XMLProperty* label = (*i)->property (X_("label"));
380 if (label->value() == name) {
382 if (_fst->wanted_chunk) {
383 g_free (_fst->wanted_chunk);
386 for (XMLNodeList::const_iterator j = (*i)->children().begin(); j != (*i)->children().end(); ++j) {
387 if ((*j)->is_content ()) {
388 /* we can't dispatch directly here; too many plugins expect only one GUI thread */
390 guchar* raw_data = g_base64_decode ((*j)->content().c_str(), &size);
391 _fst->wanted_chunk = raw_data;
392 _fst->wanted_chunk_size = size;
393 _fst->want_chunk = 1;
407 VSTPlugin::do_save_preset (string name)
409 if (_plugin->flags & 32 /* effFlagsProgramsChunks */) {
411 XMLTree* t = presets_tree ();
416 /* Add a chunk to our XML file of user presets */
418 XMLNode* p = new XMLNode (X_("ChunkPreset"));
419 /* XXX: use of _presets.size() + 1 for the unique ID here is dubious at best */
420 string const uri = string_compose (X_("VST:%1:%2"), unique_id (), _presets.size() + 1);
421 p->add_property (X_("uri"), uri);
422 p->add_property (X_("label"), name);
423 gchar* data = get_chunk (true);
424 p->add_content (string (data));
426 t->root()->add_child_nocopy (*p);
428 sys::path f = ARDOUR::user_config_directory ();
432 t->write (f.to_string ());
441 VSTPlugin::do_remove_preset (string name)
443 if (_plugin->flags & 32 /* effFlagsProgramsChunks */) {
447 error << _("no support for presets using chunks at this time")
454 VSTPlugin::describe_parameter (Evoral::Parameter param)
457 _plugin->dispatcher (_plugin, effGetParamName, param.id(), 0, name, 0);
462 VSTPlugin::signal_latency () const
465 return _user_latency;
468 #ifdef VESTIGE_HEADER
469 return *((framecnt_t *) (((char *) &_plugin->flags) + 12)); /* initialDelay */
471 return _plugin->initial_delay;
475 set<Evoral::Parameter>
476 VSTPlugin::automatable () const
478 set<Evoral::Parameter> ret;
480 for (uint32_t i = 0; i < parameter_count(); ++i){
481 ret.insert (ret.end(), Evoral::Parameter(PluginAutomation, 0, i));
488 VSTPlugin::connect_and_run (BufferSet& bufs,
489 ChanMapping in_map, ChanMapping out_map,
490 pframes_t nframes, framecnt_t 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) {
520 /* Track notes that we are sending to the plugin */
521 MidiBuffer& b = bufs.get_midi (0);
523 _tracker.track (b.begin(), b.end(), looped);
525 if (_have_pending_stop_events) {
526 /* Transmit note-offs that are pending from the last transport stop */
527 bufs.merge_from (_pending_stop_events, 0);
528 _have_pending_stop_events = false;
531 VstEvents* v = bufs.get_vst_midi (0);
532 _plugin->dispatcher (_plugin, effProcessEvents, 0, 0, v, 0);
535 /* we already know it can support processReplacing */
537 _plugin->processReplacing (_plugin, ins, outs, nframes);
543 VSTPlugin::deactivate ()
545 _plugin->dispatcher (_plugin, effMainsChanged, 0, 0, NULL, 0.0f);
549 VSTPlugin::activate ()
551 _plugin->dispatcher (_plugin, effMainsChanged, 0, 1, NULL, 0.0f);
555 VSTPlugin::unique_id() const
559 #ifdef VESTIGE_HEADER
560 snprintf (buf, sizeof (buf), "%d", *((int32_t*) &_plugin->unused_id));
562 snprintf (buf, sizeof (buf), "%d", _plugin->uniqueID);
569 VSTPlugin::name () const
575 VSTPlugin::maker () const
577 return _info->creator.c_str();
581 VSTPlugin::label () const
587 VSTPlugin::parameter_count() const
589 return _plugin->numParams;
593 VSTPlugin::has_editor () const
595 return _plugin->flags & effFlagsHasEditor;
599 VSTPlugin::print_parameter (uint32_t param, char *buf, uint32_t len) const
603 _plugin->dispatcher (_plugin, 7 /* effGetParamDisplay */, param, 0, buf, 0);
605 if (buf[0] == '\0') {
610 while (*first_nonws && isspace (*first_nonws)) {
613 if (*first_nonws == '\0') {
617 memmove (buf, first_nonws, strlen (buf) - (first_nonws - buf) + 1);
621 VSTPluginInfo::load (Session& session)
626 if (Config->get_use_vst()) {
629 handle = fst_load(path.c_str());
631 if ( (int)handle == -1) {
632 error << string_compose(_("VST: cannot load module from \"%1\""), path) << endmsg;
634 plugin.reset (new VSTPlugin (session.engine(), session, handle));
637 error << _("You asked ardour to not use any VST plugins") << endmsg;
638 return PluginPtr ((Plugin*) 0);
641 plugin->set_info(PluginInfoPtr(new VSTPluginInfo(*this)));
645 catch (failed_constructor &err) {
646 return PluginPtr ((Plugin*) 0);
650 vector<Plugin::PresetRecord>
651 VSTPlugin::get_presets ()
653 vector<PresetRecord> p;
655 /* Built-in presets */
657 int const vst_version = _plugin->dispatcher (_plugin, effGetVstVersion, 0, 0, NULL, 0);
658 for (int i = 0; i < _plugin->numPrograms; ++i) {
659 PresetRecord r (string_compose (X_("VST:%1:%2"), unique_id (), i), "");
661 if (vst_version >= 2) {
663 _plugin->dispatcher (_plugin, 29, i, 0, buf, 0);
666 r.label = string_compose (_("Preset %1"), i);
670 _presets.insert (make_pair (r.uri, r));
673 /* User presets from our XML file */
675 XMLTree* t = presets_tree ();
678 XMLNode* root = t->root ();
679 for (XMLNodeList::const_iterator i = root->children().begin(); i != root->children().end(); ++i) {
681 assert ((*i)->name() == X_("ChunkPreset"));
683 XMLProperty* uri = (*i)->property (X_("uri"));
684 XMLProperty* label = (*i)->property (X_("label"));
689 PresetRecord r (uri->value(), label->value());
691 _presets.insert (make_pair (r.uri, r));
700 /** @return XMLTree with our user presets; could be a new one if no existing
701 * one was found, or 0 if one was present but badly-formatted.
704 VSTPlugin::presets_tree () const
706 XMLTree* t = new XMLTree;
708 sys::path p = ARDOUR::user_config_directory ();
711 if (!is_directory (p)) {
712 create_directory (p);
718 t->set_root (new XMLNode (X_("VSTPresets")));
722 t->set_filename (p.to_string ());
731 /** @return Index of the first user preset in our lists */
733 VSTPlugin::first_user_preset_index () const
735 return _plugin->numPrograms;
739 VSTPlugin::realtime_handle_transport_stopped ()
741 /* Create note-offs for any active notes and put them in _pending_stop_events, to be picked
742 up on the next call to connect_and_run ().
745 _pending_stop_events.ensure_buffers (DataType::MIDI, 1, 4096);
746 _pending_stop_events.get_midi(0).clear ();
747 _tracker.resolve_notes (_pending_stop_events.get_midi (0), 0);
748 _have_pending_stop_events = true;
751 VSTPluginInfo::VSTPluginInfo()