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 <ardour/ardour.h>
45 #include <ardour/session.h>
46 #include <ardour/audioengine.h>
47 #include <ardour/vst_plugin.h>
49 #include <pbd/stl_delete.h>
54 using namespace ARDOUR;
59 VSTPlugin::VSTPlugin (AudioEngine& e, Session& session, FSTHandle* h)
64 if ((_fst = fst_instantiate (handle, Session::vst_callback, this)) == 0) {
65 throw failed_constructor();
68 _plugin = _fst->plugin;
71 /* set rate and blocksize */
73 cerr << name() << " dispatch effSetSampleRate\n";
74 _plugin->dispatcher (_plugin, effSetSampleRate, 0, 0, NULL,
75 (float) session.frame_rate());
76 cerr << name() << " dispatch effSetBlockSize\n";
77 _plugin->dispatcher (_plugin, effSetBlockSize, 0,
78 session.get_block_size(), NULL, 0.0f);
80 /* set program to zero */
82 cerr << name() << " dispatch effSetProgram\n";
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 cerr << name() << " dispatch effSetBlockSize\n";
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 /* effFlagsProgramChunks */) {
152 /* fetch the current chunk */
157 cerr << name() << " dispatch = GetChunk\n";
158 if ((data_size = _plugin->dispatcher (_plugin, 23 /* effGetChunk */, 0, 0, &data, false)) == 0) {
162 /* save it to a file */
164 Glib::ustring path = Glib::build_filename (get_user_ardour_path (), "vst");
167 if (stat (path.c_str(), &sbuf)) {
168 if (errno == ENOENT) {
169 if (g_mkdir_with_parents (path.c_str(), 0600)) {
170 error << string_compose (_("cannot create VST chunk directory: %1"),
178 warning << string_compose (_("cannot check VST chunk directory: %1"), strerror (errno))
183 } else if (!S_ISDIR (sbuf.st_mode)) {
184 error << string_compose (_("%1 exists but is not a directory"), path)
189 path = Glib::build_filename (path, "something");
191 /* store information */
193 XMLNode* chunk_node = new XMLNode (X_("chunk"));
194 chunk_node->add_property ("path", path);
196 root->add_child_nocopy (*chunk_node);
200 XMLNode* parameters = new XMLNode ("parameters");
202 for (long n = 0; n < _plugin->numParams; ++n) {
205 snprintf (index, sizeof (index), "param_%ld", n);
206 snprintf (val, sizeof (val), "%.12g", _plugin->getParameter (_plugin, n));
207 parameters->add_property (index, val);
210 root->add_child_nocopy (*parameters);
218 VSTPlugin::set_state(const XMLNode& node)
220 LocaleGuard lg (X_("POSIX"));
222 if (node.name() != state_node_name()) {
223 error << _("Bad node sent to VSTPlugin::set_state") << endmsg;
229 if ((child = find_named_node (node, X_("chunks"))) != 0) {
233 } else if ((child = find_named_node (node, X_("parameters"))) != 0) {
235 XMLPropertyList::const_iterator i;
237 for (i = child->properties().begin(); i != child->properties().end(); ++i) {
241 sscanf ((*i)->name().c_str(), "param_%ld", ¶m);
242 sscanf ((*i)->value().c_str(), "%f", &val);
244 _plugin->setParameter (_plugin, param, val);
254 VSTPlugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor& desc) const
256 VstParameterProperties prop;
258 desc.min_unbound = false;
259 desc.max_unbound = false;
261 cerr << name() << " dispatch ParamProps\n";
262 if (_plugin->dispatcher (_plugin, effGetParameterProperties, which, 0, &prop, 0)) {
264 #ifdef VESTIGE_COMPLETE
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;
309 cerr << name() << " dispatch effGetParamName\n";
310 _plugin->dispatcher (_plugin, effGetParamName, which, 0, label, 0);
313 desc.integer_step = false;
317 desc.smallstep = 0.005f;
318 desc.largestep = 0.1f;
319 desc.toggled = false;
320 desc.logarithmic = false;
321 desc.sr_dependent = false;
328 VSTPlugin::load_preset (string name)
331 if (_plugin->flags & 32 /* effFlagsProgramChunks */) {
332 error << _("no support for presets using chunks at this time")
336 return Plugin::load_preset (name);
340 VSTPlugin::save_preset (string name)
342 if (_plugin->flags & 32 /* effFlagsProgramChunks */) {
343 error << _("no support for presets using chunks at this time")
348 return Plugin::save_preset (name, "vst");
352 VSTPlugin::describe_parameter (uint32_t param)
355 cerr << this->name() << " dispatch effGetParamName\n";
356 _plugin->dispatcher (_plugin, effGetParamName, param, 0, name, 0);
361 VSTPlugin::latency () const
363 #ifdef VESTIGE_HEADER
364 return *((nframes_t *) (((char *) &_plugin->flags) + 12)); /* initialDelay */
371 VSTPlugin::automatable () const
375 for (uint32_t i = 0; i < parameter_count(); ++i){
376 ret.insert (ret.end(), i);
383 VSTPlugin::connect_and_run (vector<Sample*>& bufs, uint32_t maxbuf, int32_t& in_index, int32_t& out_index, nframes_t nframes, nframes_t offset)
385 float *ins[_plugin->numInputs];
386 float *outs[_plugin->numOutputs];
390 for (i = 0; i < (int32_t) _plugin->numInputs; ++i) {
391 ins[i] = bufs[min((uint32_t) in_index,maxbuf - 1)] + offset;
395 for (i = 0; i < (int32_t) _plugin->numOutputs; ++i) {
396 outs[i] = bufs[min((uint32_t) out_index,maxbuf - 1)] + offset;
398 /* unbelievably, several VST plugins still rely on Cubase
399 behaviour and do not silence the buffer in processReplacing
400 when they have no output.
403 // memset (outs[i], 0, sizeof (Sample) * nframes);
408 /* we already know it can support processReplacing */
410 cerr << "++++++++++ " << name() << " RUN in " << pthread_self() << endl;
411 _plugin->processReplacing (_plugin, ins, outs, nframes);
418 VSTPlugin::deactivate ()
420 cerr << "\n\n\n ************ " << name() << " DEACTIVATE in " << pthread_self() << endl;
421 _plugin->dispatcher (_plugin, effMainsChanged, 0, 0, NULL, 0.0f);
425 VSTPlugin::activate ()
427 cerr << "\n\n\n ************ " << name() << " ACTIVATE in " << pthread_self() << endl;
428 _plugin->dispatcher (_plugin, effMainsChanged, 0, 1, NULL, 0.0f);
432 VSTPlugin::unique_id() const
435 #ifdef VESTIGE_HEADER
436 snprintf (buf, sizeof (buf), "%d", *((int32_t*) &_plugin->unused_id));
438 snprintf (buf, sizeof (buf), "%d", _plugin->uniqueID);
445 VSTPlugin::name () const
451 VSTPlugin::maker () const
457 VSTPlugin::label () const
463 VSTPlugin::parameter_count() const
465 return _plugin->numParams;
469 VSTPlugin::has_editor () const
471 return _plugin->flags & effFlagsHasEditor;
475 VSTPlugin::print_parameter (uint32_t param, char *buf, uint32_t len) const
479 cerr << name() << " dispatch paramDisplay\n";
480 _plugin->dispatcher (_plugin, 7 /* effGetParamDisplay */, param, 0, buf, 0);
482 if (buf[0] == '\0') {
487 while (*first_nonws && isspace (*first_nonws)) {
490 if (*first_nonws == '\0') {
494 memmove (buf, first_nonws, strlen (buf) - (first_nonws - buf) + 1);
498 VSTPluginInfo::load (Session& session)
503 if (Config->get_use_vst()) {
506 handle = fst_load(path.c_str());
508 if ( (int)handle == -1) {
509 error << string_compose(_("VST: cannot load module from \"%1\""), path) << endmsg;
511 plugin.reset (new VSTPlugin (session.engine(), session, handle));
514 error << _("You asked ardour to not use any VST plugins") << endmsg;
515 return PluginPtr ((Plugin*) 0);
518 plugin->set_info(PluginInfoPtr(new VSTPluginInfo(*this)));
522 catch (failed_constructor &err) {
523 return PluginPtr ((Plugin*) 0);