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"
53 #include "pbd/stl_delete.h"
58 using namespace ARDOUR;
63 VSTPlugin::VSTPlugin (AudioEngine& e, Session& session, FSTHandle* h)
68 if ((_fst = fst_instantiate (handle, Session::vst_callback, this)) == 0) {
69 throw failed_constructor();
72 _plugin = _fst->plugin;
75 /* set rate and blocksize */
77 _plugin->dispatcher (_plugin, effSetSampleRate, 0, 0, NULL,
78 (float) session.frame_rate());
79 _plugin->dispatcher (_plugin, effSetBlockSize, 0,
80 session.get_block_size(), NULL, 0.0f);
82 /* set program to zero */
84 _plugin->dispatcher (_plugin, effSetProgram, 0, 0, NULL, 0.0f);
86 // Plugin::setup_controls ();
89 VSTPlugin::VSTPlugin (const VSTPlugin &other)
92 handle = other.handle;
94 if ((_fst = fst_instantiate (handle, Session::vst_callback, this)) == 0) {
95 throw failed_constructor();
97 _plugin = _fst->plugin;
99 // Plugin::setup_controls ();
102 VSTPlugin::~VSTPlugin ()
105 GoingAway (); /* EMIT SIGNAL */
110 VSTPlugin::set_block_size (nframes_t nframes)
113 _plugin->dispatcher (_plugin, effSetBlockSize, 0, nframes, NULL, 0.0f);
118 VSTPlugin::default_value (uint32_t port)
124 VSTPlugin::set_parameter (uint32_t which, float val)
126 _plugin->setParameter (_plugin, which, val);
127 //ParameterChanged (which, val); /* EMIT SIGNAL */
131 VSTPlugin::get_parameter (uint32_t which) const
133 return _plugin->getParameter (_plugin, which);
138 VSTPlugin::nth_parameter (uint32_t n, bool& ok) const
145 VSTPlugin::get_state()
147 XMLNode *root = new XMLNode (state_node_name());
148 LocaleGuard lg (X_("POSIX"));
150 if (_plugin->flags & 32 /* effFlagsProgramsChunks */) {
152 /* fetch the current chunk */
157 if ((data_size = _plugin->dispatcher (_plugin, 23 /* effGetChunk */, 0, 0, &data, false)) == 0) {
161 /* save it to a file */
163 sys::path user_vst_directory(user_config_directory());
164 user_vst_directory /= "vst";
167 sys::create_directories (user_vst_directory);
169 catch (const sys::filesystem_error& ex)
171 error << "Could not create user configuration directory" << endmsg;
175 sys::path file (user_vst_directory);
179 /* store information */
181 XMLNode* chunk_node = new XMLNode (X_("chunk"));
182 chunk_node->add_property ("path", file.to_string());
184 root->add_child_nocopy (*chunk_node);
188 XMLNode* parameters = new XMLNode ("parameters");
190 for (long n = 0; n < _plugin->numParams; ++n) {
193 snprintf (index, sizeof (index), "param_%ld", n);
194 snprintf (val, sizeof (val), "%.12g", _plugin->getParameter (_plugin, n));
195 parameters->add_property (index, val);
198 root->add_child_nocopy (*parameters);
205 VSTPlugin::set_state(const XMLNode& node)
207 LocaleGuard lg (X_("POSIX"));
209 if (node.name() != state_node_name()) {
210 error << _("Bad node sent to VSTPlugin::set_state") << endmsg;
216 if ((child = find_named_node (node, X_("chunks"))) != 0) {
220 } else if ((child = find_named_node (node, X_("parameters"))) != 0) {
222 XMLPropertyList::const_iterator i;
224 for (i = child->properties().begin(); i != child->properties().end(); ++i) {
228 sscanf ((*i)->name().c_str(), "param_%ld", ¶m);
229 sscanf ((*i)->value().c_str(), "%f", &val);
231 _plugin->setParameter (_plugin, param, val);
241 VSTPlugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor& desc) const
243 VstParameterProperties prop;
245 desc.min_unbound = false;
246 desc.max_unbound = false;
248 if (_plugin->dispatcher (_plugin, effGetParameterProperties, which, 0, &prop, 0)) {
250 #ifdef VESTIGE_COMPLETE
252 /* i have yet to find or hear of a VST plugin that uses this */
254 if (prop.flags & kVstParameterUsesIntegerMinMax) {
255 desc.lower = prop.minInteger;
256 desc.upper = prop.maxInteger;
262 if (prop.flags & kVstParameterUsesIntStep) {
264 desc.step = prop.stepInteger;
265 desc.smallstep = prop.stepInteger;
266 desc.largestep = prop.stepInteger;
268 } else if (prop.flags & kVstParameterUsesFloatStep) {
270 desc.step = prop.stepFloat;
271 desc.smallstep = prop.smallStepFloat;
272 desc.largestep = prop.largeStepFloat;
276 float range = desc.upper - desc.lower;
278 desc.step = range / 100.0f;
279 desc.smallstep = desc.step / 2.0f;
280 desc.largestep = desc.step * 10.0f;
283 desc.toggled = prop.flags & kVstParameterIsSwitch;
284 desc.logarithmic = false;
285 desc.sr_dependent = false;
286 desc.label = prop.label;
296 _plugin->dispatcher (_plugin, effGetParamName, which, 0, label, 0);
299 desc.integer_step = false;
303 desc.smallstep = 0.005f;
304 desc.largestep = 0.1f;
305 desc.toggled = false;
306 desc.logarithmic = false;
307 desc.sr_dependent = false;
314 VSTPlugin::load_preset (string name)
316 if (_plugin->flags & 32 /* effFlagsProgramsChunks */) {
317 error << _("no support for presets using chunks at this time")
321 return Plugin::load_preset (name);
325 VSTPlugin::save_preset (string name)
327 if (_plugin->flags & 32 /* effFlagsProgramsChunks */) {
328 error << _("no support for presets using chunks at this time")
332 return Plugin::save_preset (name, "vst");
336 VSTPlugin::describe_parameter (Evoral::Parameter param)
339 _plugin->dispatcher (_plugin, effGetParamName, param.id(), 0, name, 0);
344 VSTPlugin::signal_latency () const
347 return _user_latency;
350 #ifdef VESTIGE_HEADER
351 return *((nframes_t *) (((char *) &_plugin->flags) + 12)); /* initialDelay */
353 return _plugin->initial_delay;
357 set<Evoral::Parameter>
358 VSTPlugin::automatable () const
360 set<Evoral::Parameter> ret;
362 for (uint32_t i = 0; i < parameter_count(); ++i){
363 ret.insert (ret.end(), Evoral::Parameter(PluginAutomation, 0, i));
370 VSTPlugin::connect_and_run (BufferSet& bufs, uint32_t& in_index, uint32_t& out_index, nframes_t nframes, nframes_t offset)
372 float *ins[_plugin->numInputs];
373 float *outs[_plugin->numOutputs];
376 const uint32_t nbufs = bufs.count().n_audio();
378 for (i = 0; i < (int32_t) _plugin->numInputs; ++i) {
379 ins[i] = bufs.get_audio(min((uint32_t) in_index, nbufs - 1)).data() + offset;
383 for (i = 0; i < (int32_t) _plugin->numOutputs; ++i) {
384 outs[i] = bufs.get_audio(min((uint32_t) out_index, nbufs - 1)).data() + offset;
386 /* unbelievably, several VST plugins still rely on Cubase
387 behaviour and do not silence the buffer in processReplacing
388 when they have no output.
391 // memset (outs[i], 0, sizeof (Sample) * nframes);
396 /* we already know it can support processReplacing */
398 _plugin->processReplacing (_plugin, ins, outs, nframes);
404 VSTPlugin::deactivate ()
406 _plugin->dispatcher (_plugin, effMainsChanged, 0, 0, NULL, 0.0f);
410 VSTPlugin::activate ()
412 _plugin->dispatcher (_plugin, effMainsChanged, 0, 1, NULL, 0.0f);
416 VSTPlugin::unique_id() const
420 #ifdef VESTIGE_HEADER
421 snprintf (buf, sizeof (buf), "%d", *((int32_t*) &_plugin->unused_id));
423 snprintf (buf, sizeof (buf), "%d", _plugin->uniqueID);
430 VSTPlugin::name () const
436 VSTPlugin::maker () const
442 VSTPlugin::label () const
448 VSTPlugin::parameter_count() const
450 return _plugin->numParams;
454 VSTPlugin::has_editor () const
456 return _plugin->flags & effFlagsHasEditor;
460 VSTPlugin::print_parameter (uint32_t param, char *buf, uint32_t len) const
464 _plugin->dispatcher (_plugin, 7 /* effGetParamDisplay */, param, 0, buf, 0);
466 if (buf[0] == '\0') {
471 while (*first_nonws && isspace (*first_nonws)) {
474 if (*first_nonws == '\0') {
478 memmove (buf, first_nonws, strlen (buf) - (first_nonws - buf) + 1);
482 VSTPluginInfo::load (Session& session)
487 if (Config->get_use_vst()) {
490 handle = fst_load(path.c_str());
492 if ( (int)handle == -1) {
493 error << string_compose(_("VST: cannot load module from \"%1\""), path) << endmsg;
495 plugin.reset (new VSTPlugin (session.engine(), session, handle));
498 error << _("You asked ardour to not use any VST plugins") << endmsg;
499 return PluginPtr ((Plugin*) 0);
502 plugin->set_info(PluginInfoPtr(new VSTPluginInfo(*this)));
506 catch (failed_constructor &err) {
507 return PluginPtr ((Plugin*) 0);