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>
44 #include <vst/aeffectx.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>
52 #include <pbd/stl_delete.h>
57 using namespace ARDOUR;
62 VSTPlugin::VSTPlugin (AudioEngine& e, Session& session, FSTHandle* h)
67 if ((_fst = fst_instantiate (handle, Session::vst_callback, this)) == 0) {
68 throw failed_constructor();
71 _plugin = _fst->plugin;
74 /* set rate and blocksize */
76 _plugin->dispatcher (_plugin, effSetSampleRate, 0, 0, NULL,
77 (float) session.frame_rate());
78 _plugin->dispatcher (_plugin, effSetBlockSize, 0,
79 session.get_block_size(), NULL, 0.0f);
81 /* set program to zero */
83 _plugin->dispatcher (_plugin, effSetProgram, 0, 0, NULL, 0.0f);
85 Plugin::setup_controls ();
88 VSTPlugin::VSTPlugin (const VSTPlugin &other)
91 handle = other.handle;
93 if ((_fst = fst_instantiate (handle, Session::vst_callback, this)) == 0) {
94 throw failed_constructor();
96 _plugin = _fst->plugin;
98 Plugin::setup_controls ();
101 VSTPlugin::~VSTPlugin ()
104 GoingAway (); /* EMIT SIGNAL */
109 VSTPlugin::set_block_size (nframes_t nframes)
112 _plugin->dispatcher (_plugin, effSetBlockSize, 0, nframes, NULL, 0.0f);
117 VSTPlugin::default_value (uint32_t port)
123 VSTPlugin::set_parameter (uint32_t which, float val)
125 _plugin->setParameter (_plugin, which, val);
126 //ParameterChanged (which, val); /* EMIT SIGNAL */
130 VSTPlugin::get_parameter (uint32_t which) const
132 return _plugin->getParameter (_plugin, which);
137 VSTPlugin::nth_parameter (uint32_t n, bool& ok) const
144 VSTPlugin::get_state()
146 XMLNode *root = new XMLNode (state_node_name());
147 LocaleGuard lg (X_("POSIX"));
149 if (_plugin->flags & effFlagsProgramChunks) {
151 /* fetch the current chunk */
156 if ((data_size = _plugin->dispatcher (_plugin, effGetChunk, 0, 0, &data, false)) == 0) {
160 /* save it to a file */
162 Glib::ustring path = Glib::build_filename (get_user_ardour_path (), "vst");
165 sys::path user_vst_directory(user_config_directory());
167 user_vst_directory /= "vst";
168 path = user_vst_directory.to_string();
170 if (stat (path.c_str(), &sbuf)) {
171 if (errno == ENOENT) {
172 if (g_mkdir_with_parents (path.c_str(), 0600)) {
173 error << string_compose (_("cannot create VST chunk directory: %1"),
181 warning << string_compose (_("cannot check VST chunk directory: %1"), strerror (errno))
186 } else if (!S_ISDIR (sbuf.st_mode)) {
187 error << string_compose (_("%1 exists but is not a directory"), path)
192 path = Glib::build_filename (path, "something");
194 /* store information */
196 XMLNode* chunk_node = new XMLNode (X_("chunk"));
197 chunk_node->add_property ("path", path);
199 root->add_child_nocopy (*chunk_node);
203 XMLNode* parameters = new XMLNode ("parameters");
205 for (long n = 0; n < _plugin->numParams; ++n) {
208 snprintf (index, sizeof (index), "param_%ld", n);
209 snprintf (val, sizeof (val), "%.12g", _plugin->getParameter (_plugin, n));
210 parameters->add_property (index, val);
213 root->add_child_nocopy (*parameters);
220 VSTPlugin::set_state(const XMLNode& node)
222 LocaleGuard lg (X_("POSIX"));
224 if (node.name() != state_node_name()) {
225 error << _("Bad node sent to VSTPlugin::set_state") << endmsg;
231 if ((child = find_named_node (node, X_("chunks"))) != 0) {
235 } else if ((child = find_named_node (node, X_("parameters"))) != 0) {
237 XMLPropertyList::const_iterator i;
239 for (i = child->properties().begin(); i != child->properties().end(); ++i) {
243 sscanf ((*i)->name().c_str(), "param_%ld", ¶m);
244 sscanf ((*i)->value().c_str(), "%f", &val);
246 _plugin->setParameter (_plugin, param, val);
256 VSTPlugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor& desc) const
258 VstParameterProperties prop;
260 desc.min_unbound = false;
261 desc.max_unbound = false;
263 if (_plugin->dispatcher (_plugin, effGetParameterProperties, which, 0, &prop, 0)) {
265 /* i have yet to find or hear of a VST plugin that uses this */
267 if (prop.flags & kVstParameterUsesIntegerMinMax) {
268 desc.lower = prop.minInteger;
269 desc.upper = prop.maxInteger;
275 if (prop.flags & kVstParameterUsesIntStep) {
277 desc.step = prop.stepInteger;
278 desc.smallstep = prop.stepInteger;
279 desc.largestep = prop.stepInteger;
281 } else if (prop.flags & kVstParameterUsesFloatStep) {
283 desc.step = prop.stepFloat;
284 desc.smallstep = prop.smallStepFloat;
285 desc.largestep = prop.largeStepFloat;
289 float range = desc.upper - desc.lower;
291 desc.step = range / 100.0f;
292 desc.smallstep = desc.step / 2.0f;
293 desc.largestep = desc.step * 10.0f;
296 desc.toggled = prop.flags & kVstParameterIsSwitch;
297 desc.logarithmic = false;
298 desc.sr_dependent = false;
299 desc.label = prop.label;
308 _plugin->dispatcher (_plugin, effGetParamName, which, 0, label, 0);
311 desc.integer_step = false;
315 desc.smallstep = 0.005f;
316 desc.largestep = 0.1f;
317 desc.toggled = false;
318 desc.logarithmic = false;
319 desc.sr_dependent = false;
326 VSTPlugin::load_preset (string name)
328 if (_plugin->flags & effFlagsProgramChunks) {
329 error << _("no support for presets using chunks at this time")
333 return Plugin::load_preset (name);
337 VSTPlugin::save_preset (string name)
339 if (_plugin->flags & effFlagsProgramChunks) {
340 error << _("no support for presets using chunks at this time")
344 return Plugin::save_preset (name, "vst");
348 VSTPlugin::describe_parameter (uint32_t param)
351 _plugin->dispatcher (_plugin, effGetParamName, param, 0, name, 0);
356 VSTPlugin::signal_latency () const
359 return _user_latency;
362 return _plugin->initialDelay;
366 VSTPlugin::automatable () const
370 for (uint32_t i = 0; i < parameter_count(); ++i){
371 ret.insert (ret.end(), i);
378 VSTPlugin::connect_and_run (BufferSet& bufs, uint32_t& in_index, uint32_t& out_index, nframes_t nframes, nframes_t offset)
380 float *ins[_plugin->numInputs];
381 float *outs[_plugin->numOutputs];
384 const uint32_t nbufs = bufs.count().n_audio();
386 for (i = 0; i < (int32_t) _plugin->numInputs; ++i) {
387 ins[i] = bufs.get_audio(min((uint32_t) in_index, nbufs - 1)).data() + offset;
391 for (i = 0; i < (int32_t) _plugin->numOutputs; ++i) {
392 outs[i] = bufs.get_audio(min((uint32_t) out_index, nbufs - 1)).data() + offset;
394 /* unbelievably, several VST plugins still rely on Cubase
395 behaviour and do not silence the buffer in processReplacing
396 when they have no output.
399 // memset (outs[i], 0, sizeof (Sample) * nframes);
404 /* we already know it can support processReplacing */
406 _plugin->processReplacing (_plugin, ins, outs, nframes);
412 VSTPlugin::deactivate ()
414 _plugin->dispatcher (_plugin, effMainsChanged, 0, 0, NULL, 0.0f);
418 VSTPlugin::activate ()
420 _plugin->dispatcher (_plugin, effMainsChanged, 0, 1, NULL, 0.0f);
424 VSTPlugin::unique_id() const
427 snprintf (buf, sizeof (buf), "%d", _plugin->uniqueID);
433 VSTPlugin::name () const
439 VSTPlugin::maker () const
445 VSTPlugin::label () const
451 VSTPlugin::parameter_count() const
453 return _plugin->numParams;
457 VSTPlugin::has_editor () const
459 return _plugin->flags & effFlagsHasEditor;
463 VSTPlugin::print_parameter (uint32_t param, char *buf, uint32_t len) const
468 _plugin->dispatcher (_plugin, effGetParamLabel, param, 0, lab, 0);
469 _plugin->dispatcher (_plugin, effGetParamDisplay, param, 0, buf, 0);
471 if (buf[0] == '\0') {
476 while (*first_nonws && isspace (*first_nonws)) {
479 if (*first_nonws == '\0') {
483 memmove (buf, first_nonws, strlen (buf) - (first_nonws - buf) + 1);
487 VSTPluginInfo::load (Session& session)
492 if (Config->get_use_vst()) {
495 handle = fst_load(path.c_str());
497 if ( (int)handle == -1) {
498 error << string_compose(_("VST: cannot load module from \"%1\""), path) << endmsg;
500 plugin.reset (new VSTPlugin (session.engine(), session, handle));
503 error << _("You asked ardour to not use any VST plugins") << endmsg;
504 return PluginPtr ((Plugin*) 0);
507 plugin->set_info(PluginInfoPtr(new VSTPluginInfo(*this)));
511 catch (failed_constructor &err) {
512 return PluginPtr ((Plugin*) 0);